Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2015 Google, Inc |
| 3 | * Written by Simon Glass <sjg@chromium.org> |
| 4 | * |
Simon Glass | 0566e24 | 2015-03-25 12:22:30 -0600 | [diff] [blame] | 5 | * usb_match_device() modified from Linux kernel v4.0. |
| 6 | * |
Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 7 | * SPDX-License-Identifier: GPL-2.0+ |
| 8 | */ |
| 9 | |
| 10 | #include <common.h> |
| 11 | #include <dm.h> |
| 12 | #include <errno.h> |
Simon Glass | cf92e05 | 2015-09-02 17:24:58 -0600 | [diff] [blame] | 13 | #include <memalign.h> |
Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 14 | #include <usb.h> |
| 15 | #include <dm/device-internal.h> |
| 16 | #include <dm/lists.h> |
Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 17 | #include <dm/uclass-internal.h> |
| 18 | |
| 19 | DECLARE_GLOBAL_DATA_PTR; |
| 20 | |
| 21 | extern bool usb_started; /* flag for the started/stopped USB status */ |
| 22 | static bool asynch_allowed; |
| 23 | |
Hans de Goede | e253637 | 2015-05-10 14:10:21 +0200 | [diff] [blame] | 24 | struct usb_uclass_priv { |
| 25 | int companion_device_count; |
| 26 | }; |
| 27 | |
Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 28 | int usb_disable_asynch(int disable) |
| 29 | { |
| 30 | int old_value = asynch_allowed; |
| 31 | |
| 32 | asynch_allowed = !disable; |
| 33 | return old_value; |
| 34 | } |
| 35 | |
| 36 | int submit_int_msg(struct usb_device *udev, unsigned long pipe, void *buffer, |
| 37 | int length, int interval) |
| 38 | { |
| 39 | struct udevice *bus = udev->controller_dev; |
| 40 | struct dm_usb_ops *ops = usb_get_ops(bus); |
| 41 | |
| 42 | if (!ops->interrupt) |
| 43 | return -ENOSYS; |
| 44 | |
| 45 | return ops->interrupt(bus, udev, pipe, buffer, length, interval); |
| 46 | } |
| 47 | |
| 48 | int submit_control_msg(struct usb_device *udev, unsigned long pipe, |
| 49 | void *buffer, int length, struct devrequest *setup) |
| 50 | { |
| 51 | struct udevice *bus = udev->controller_dev; |
| 52 | struct dm_usb_ops *ops = usb_get_ops(bus); |
Hans de Goede | e253637 | 2015-05-10 14:10:21 +0200 | [diff] [blame] | 53 | struct usb_uclass_priv *uc_priv = bus->uclass->priv; |
| 54 | int err; |
Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 55 | |
| 56 | if (!ops->control) |
| 57 | return -ENOSYS; |
| 58 | |
Hans de Goede | e253637 | 2015-05-10 14:10:21 +0200 | [diff] [blame] | 59 | err = ops->control(bus, udev, pipe, buffer, length, setup); |
| 60 | if (setup->request == USB_REQ_SET_FEATURE && |
| 61 | setup->requesttype == USB_RT_PORT && |
| 62 | setup->value == cpu_to_le16(USB_PORT_FEAT_RESET) && |
| 63 | err == -ENXIO) { |
| 64 | /* Device handed over to companion after port reset */ |
| 65 | uc_priv->companion_device_count++; |
| 66 | } |
| 67 | |
| 68 | return err; |
Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | int submit_bulk_msg(struct usb_device *udev, unsigned long pipe, void *buffer, |
| 72 | int length) |
| 73 | { |
| 74 | struct udevice *bus = udev->controller_dev; |
| 75 | struct dm_usb_ops *ops = usb_get_ops(bus); |
| 76 | |
| 77 | if (!ops->bulk) |
| 78 | return -ENOSYS; |
| 79 | |
| 80 | return ops->bulk(bus, udev, pipe, buffer, length); |
| 81 | } |
| 82 | |
Hans de Goede | 8a5f066 | 2015-05-10 14:10:18 +0200 | [diff] [blame] | 83 | struct int_queue *create_int_queue(struct usb_device *udev, |
| 84 | unsigned long pipe, int queuesize, int elementsize, |
| 85 | void *buffer, int interval) |
| 86 | { |
| 87 | struct udevice *bus = udev->controller_dev; |
| 88 | struct dm_usb_ops *ops = usb_get_ops(bus); |
| 89 | |
| 90 | if (!ops->create_int_queue) |
| 91 | return NULL; |
| 92 | |
| 93 | return ops->create_int_queue(bus, udev, pipe, queuesize, elementsize, |
| 94 | buffer, interval); |
| 95 | } |
| 96 | |
| 97 | void *poll_int_queue(struct usb_device *udev, struct int_queue *queue) |
| 98 | { |
| 99 | struct udevice *bus = udev->controller_dev; |
| 100 | struct dm_usb_ops *ops = usb_get_ops(bus); |
| 101 | |
| 102 | if (!ops->poll_int_queue) |
| 103 | return NULL; |
| 104 | |
| 105 | return ops->poll_int_queue(bus, udev, queue); |
| 106 | } |
| 107 | |
| 108 | int destroy_int_queue(struct usb_device *udev, struct int_queue *queue) |
| 109 | { |
| 110 | struct udevice *bus = udev->controller_dev; |
| 111 | struct dm_usb_ops *ops = usb_get_ops(bus); |
| 112 | |
| 113 | if (!ops->destroy_int_queue) |
| 114 | return -ENOSYS; |
| 115 | |
| 116 | return ops->destroy_int_queue(bus, udev, queue); |
| 117 | } |
| 118 | |
Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 119 | int usb_alloc_device(struct usb_device *udev) |
| 120 | { |
| 121 | struct udevice *bus = udev->controller_dev; |
| 122 | struct dm_usb_ops *ops = usb_get_ops(bus); |
| 123 | |
| 124 | /* This is only requird by some controllers - current XHCI */ |
| 125 | if (!ops->alloc_device) |
| 126 | return 0; |
| 127 | |
| 128 | return ops->alloc_device(bus, udev); |
| 129 | } |
| 130 | |
Hans de Goede | b2f219b | 2015-06-17 21:33:52 +0200 | [diff] [blame] | 131 | int usb_reset_root_port(struct usb_device *udev) |
| 132 | { |
| 133 | struct udevice *bus = udev->controller_dev; |
| 134 | struct dm_usb_ops *ops = usb_get_ops(bus); |
| 135 | |
| 136 | if (!ops->reset_root_port) |
| 137 | return -ENOSYS; |
| 138 | |
| 139 | return ops->reset_root_port(bus, udev); |
| 140 | } |
| 141 | |
Bin Meng | 9ca1b4b | 2017-07-19 21:51:17 +0800 | [diff] [blame] | 142 | int usb_update_hub_device(struct usb_device *udev) |
| 143 | { |
| 144 | struct udevice *bus = udev->controller_dev; |
| 145 | struct dm_usb_ops *ops = usb_get_ops(bus); |
| 146 | |
| 147 | if (!ops->update_hub_device) |
| 148 | return -ENOSYS; |
| 149 | |
| 150 | return ops->update_hub_device(bus, udev); |
| 151 | } |
| 152 | |
Bin Meng | 3e59f59 | 2017-09-07 06:13:17 -0700 | [diff] [blame^] | 153 | int usb_get_max_xfer_size(struct usb_device *udev, size_t *size) |
| 154 | { |
| 155 | struct udevice *bus = udev->controller_dev; |
| 156 | struct dm_usb_ops *ops = usb_get_ops(bus); |
| 157 | |
| 158 | if (!ops->get_max_xfer_size) |
| 159 | return -ENOSYS; |
| 160 | |
| 161 | return ops->get_max_xfer_size(bus, size); |
| 162 | } |
| 163 | |
Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 164 | int usb_stop(void) |
| 165 | { |
| 166 | struct udevice *bus; |
| 167 | struct uclass *uc; |
Hans de Goede | e253637 | 2015-05-10 14:10:21 +0200 | [diff] [blame] | 168 | struct usb_uclass_priv *uc_priv; |
Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 169 | int err = 0, ret; |
| 170 | |
| 171 | /* De-activate any devices that have been activated */ |
| 172 | ret = uclass_get(UCLASS_USB, &uc); |
| 173 | if (ret) |
| 174 | return ret; |
Hans de Goede | e253637 | 2015-05-10 14:10:21 +0200 | [diff] [blame] | 175 | |
| 176 | uc_priv = uc->priv; |
| 177 | |
Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 178 | uclass_foreach_dev(bus, uc) { |
Stefan Roese | 706865a | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 179 | ret = device_remove(bus, DM_REMOVE_NORMAL); |
Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 180 | if (ret && !err) |
| 181 | err = ret; |
| 182 | } |
Simon Glass | 9b97756 | 2016-03-13 08:22:33 -0600 | [diff] [blame] | 183 | #ifdef CONFIG_BLK |
| 184 | ret = blk_unbind_all(IF_TYPE_USB); |
| 185 | if (ret && !err) |
| 186 | err = ret; |
| 187 | #endif |
Simon Glass | 095fdef | 2015-03-25 12:22:38 -0600 | [diff] [blame] | 188 | #ifdef CONFIG_SANDBOX |
| 189 | struct udevice *dev; |
| 190 | |
| 191 | /* Reset all enulation devices */ |
| 192 | ret = uclass_get(UCLASS_USB_EMUL, &uc); |
| 193 | if (ret) |
| 194 | return ret; |
| 195 | |
| 196 | uclass_foreach_dev(dev, uc) |
| 197 | usb_emul_reset(dev); |
| 198 | #endif |
Paul Kocialkowski | 0fa5999 | 2015-08-04 17:04:12 +0200 | [diff] [blame] | 199 | #ifdef CONFIG_USB_STORAGE |
Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 200 | usb_stor_reset(); |
Paul Kocialkowski | 0fa5999 | 2015-08-04 17:04:12 +0200 | [diff] [blame] | 201 | #endif |
Hans de Goede | e253637 | 2015-05-10 14:10:21 +0200 | [diff] [blame] | 202 | uc_priv->companion_device_count = 0; |
Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 203 | usb_started = 0; |
| 204 | |
| 205 | return err; |
| 206 | } |
| 207 | |
Hans de Goede | a24a0e9 | 2015-05-10 14:10:19 +0200 | [diff] [blame] | 208 | static void usb_scan_bus(struct udevice *bus, bool recurse) |
Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 209 | { |
| 210 | struct usb_bus_priv *priv; |
| 211 | struct udevice *dev; |
| 212 | int ret; |
| 213 | |
| 214 | priv = dev_get_uclass_priv(bus); |
| 215 | |
| 216 | assert(recurse); /* TODO: Support non-recusive */ |
| 217 | |
Hans de Goede | a24a0e9 | 2015-05-10 14:10:19 +0200 | [diff] [blame] | 218 | printf("scanning bus %d for devices... ", bus->seq); |
| 219 | debug("\n"); |
Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 220 | ret = usb_scan_device(bus, 0, USB_SPEED_FULL, &dev); |
| 221 | if (ret) |
Hans de Goede | a24a0e9 | 2015-05-10 14:10:19 +0200 | [diff] [blame] | 222 | printf("failed, error %d\n", ret); |
| 223 | else if (priv->next_addr == 0) |
| 224 | printf("No USB Device found\n"); |
| 225 | else |
| 226 | printf("%d USB Device(s) found\n", priv->next_addr); |
Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 227 | } |
| 228 | |
Simon Glass | eae11be | 2015-11-08 23:48:00 -0700 | [diff] [blame] | 229 | static void remove_inactive_children(struct uclass *uc, struct udevice *bus) |
| 230 | { |
| 231 | uclass_foreach_dev(bus, uc) { |
| 232 | struct udevice *dev, *next; |
| 233 | |
| 234 | if (!device_active(bus)) |
| 235 | continue; |
| 236 | device_foreach_child_safe(dev, next, bus) { |
| 237 | if (!device_active(dev)) |
| 238 | device_unbind(dev); |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 243 | int usb_init(void) |
| 244 | { |
| 245 | int controllers_initialized = 0; |
Hans de Goede | e253637 | 2015-05-10 14:10:21 +0200 | [diff] [blame] | 246 | struct usb_uclass_priv *uc_priv; |
Hans de Goede | b6de4d1 | 2015-05-10 14:10:20 +0200 | [diff] [blame] | 247 | struct usb_bus_priv *priv; |
Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 248 | struct udevice *bus; |
| 249 | struct uclass *uc; |
| 250 | int count = 0; |
| 251 | int ret; |
| 252 | |
| 253 | asynch_allowed = 1; |
Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 254 | |
| 255 | ret = uclass_get(UCLASS_USB, &uc); |
| 256 | if (ret) |
| 257 | return ret; |
| 258 | |
Hans de Goede | e253637 | 2015-05-10 14:10:21 +0200 | [diff] [blame] | 259 | uc_priv = uc->priv; |
| 260 | |
Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 261 | uclass_foreach_dev(bus, uc) { |
| 262 | /* init low_level USB */ |
Hans de Goede | 134692a | 2015-05-04 21:33:26 +0200 | [diff] [blame] | 263 | printf("USB%d: ", count); |
Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 264 | count++; |
Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 265 | ret = device_probe(bus); |
| 266 | if (ret == -ENODEV) { /* No such device. */ |
| 267 | puts("Port not available.\n"); |
| 268 | controllers_initialized++; |
| 269 | continue; |
| 270 | } |
| 271 | |
| 272 | if (ret) { /* Other error. */ |
| 273 | printf("probe failed, error %d\n", ret); |
| 274 | continue; |
| 275 | } |
Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 276 | controllers_initialized++; |
Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 277 | usb_started = true; |
| 278 | } |
| 279 | |
Hans de Goede | b6de4d1 | 2015-05-10 14:10:20 +0200 | [diff] [blame] | 280 | /* |
| 281 | * lowlevel init done, now scan the bus for devices i.e. search HUBs |
| 282 | * and configure them, first scan primary controllers. |
| 283 | */ |
| 284 | uclass_foreach_dev(bus, uc) { |
| 285 | if (!device_active(bus)) |
| 286 | continue; |
| 287 | |
| 288 | priv = dev_get_uclass_priv(bus); |
| 289 | if (!priv->companion) |
| 290 | usb_scan_bus(bus, true); |
| 291 | } |
| 292 | |
| 293 | /* |
| 294 | * Now that the primary controllers have been scanned and have handed |
| 295 | * over any devices they do not understand to their companions, scan |
Hans de Goede | e253637 | 2015-05-10 14:10:21 +0200 | [diff] [blame] | 296 | * the companions if necessary. |
Hans de Goede | b6de4d1 | 2015-05-10 14:10:20 +0200 | [diff] [blame] | 297 | */ |
Hans de Goede | e253637 | 2015-05-10 14:10:21 +0200 | [diff] [blame] | 298 | if (uc_priv->companion_device_count) { |
| 299 | uclass_foreach_dev(bus, uc) { |
| 300 | if (!device_active(bus)) |
| 301 | continue; |
Hans de Goede | b6de4d1 | 2015-05-10 14:10:20 +0200 | [diff] [blame] | 302 | |
Hans de Goede | e253637 | 2015-05-10 14:10:21 +0200 | [diff] [blame] | 303 | priv = dev_get_uclass_priv(bus); |
| 304 | if (priv->companion) |
| 305 | usb_scan_bus(bus, true); |
| 306 | } |
Hans de Goede | b6de4d1 | 2015-05-10 14:10:20 +0200 | [diff] [blame] | 307 | } |
| 308 | |
Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 309 | debug("scan end\n"); |
Simon Glass | eae11be | 2015-11-08 23:48:00 -0700 | [diff] [blame] | 310 | |
| 311 | /* Remove any devices that were not found on this scan */ |
| 312 | remove_inactive_children(uc, bus); |
| 313 | |
| 314 | ret = uclass_get(UCLASS_USB_HUB, &uc); |
| 315 | if (ret) |
| 316 | return ret; |
| 317 | remove_inactive_children(uc, bus); |
| 318 | |
Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 319 | /* if we were not able to find at least one working bus, bail out */ |
| 320 | if (!count) |
| 321 | printf("No controllers found\n"); |
| 322 | else if (controllers_initialized == 0) |
| 323 | printf("USB error: all controllers failed lowlevel init\n"); |
| 324 | |
| 325 | return usb_started ? 0 : -1; |
| 326 | } |
| 327 | |
Simon Glass | e8ea5e8 | 2015-11-08 23:47:59 -0700 | [diff] [blame] | 328 | /* |
| 329 | * TODO(sjg@chromium.org): Remove this legacy function. At present it is needed |
| 330 | * to support boards which use driver model for USB but not Ethernet, and want |
| 331 | * to use USB Ethernet. |
| 332 | * |
| 333 | * The #if clause is here to ensure that remains the only case. |
| 334 | */ |
| 335 | #if !defined(CONFIG_DM_ETH) && defined(CONFIG_USB_HOST_ETHER) |
Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 336 | static struct usb_device *find_child_devnum(struct udevice *parent, int devnum) |
| 337 | { |
| 338 | struct usb_device *udev; |
| 339 | struct udevice *dev; |
| 340 | |
| 341 | if (!device_active(parent)) |
| 342 | return NULL; |
Simon Glass | bcbe3d1 | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 343 | udev = dev_get_parent_priv(parent); |
Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 344 | if (udev->devnum == devnum) |
| 345 | return udev; |
| 346 | |
| 347 | for (device_find_first_child(parent, &dev); |
| 348 | dev; |
| 349 | device_find_next_child(&dev)) { |
| 350 | udev = find_child_devnum(dev, devnum); |
| 351 | if (udev) |
| 352 | return udev; |
| 353 | } |
| 354 | |
| 355 | return NULL; |
| 356 | } |
| 357 | |
| 358 | struct usb_device *usb_get_dev_index(struct udevice *bus, int index) |
| 359 | { |
Hans de Goede | fd1bd21 | 2015-06-17 21:33:53 +0200 | [diff] [blame] | 360 | struct udevice *dev; |
Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 361 | int devnum = index + 1; /* Addresses are allocated from 1 on USB */ |
| 362 | |
Hans de Goede | fd1bd21 | 2015-06-17 21:33:53 +0200 | [diff] [blame] | 363 | device_find_first_child(bus, &dev); |
| 364 | if (!dev) |
| 365 | return NULL; |
Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 366 | |
Hans de Goede | fd1bd21 | 2015-06-17 21:33:53 +0200 | [diff] [blame] | 367 | return find_child_devnum(dev, devnum); |
Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 368 | } |
Simon Glass | e8ea5e8 | 2015-11-08 23:47:59 -0700 | [diff] [blame] | 369 | #endif |
Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 370 | |
Simon Glass | fbeceb2 | 2015-03-25 12:22:32 -0600 | [diff] [blame] | 371 | int usb_setup_ehci_gadget(struct ehci_ctrl **ctlrp) |
| 372 | { |
| 373 | struct usb_platdata *plat; |
| 374 | struct udevice *dev; |
| 375 | int ret; |
| 376 | |
| 377 | /* Find the old device and remove it */ |
| 378 | ret = uclass_find_device_by_seq(UCLASS_USB, 0, true, &dev); |
| 379 | if (ret) |
| 380 | return ret; |
Stefan Roese | 706865a | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 381 | ret = device_remove(dev, DM_REMOVE_NORMAL); |
Simon Glass | fbeceb2 | 2015-03-25 12:22:32 -0600 | [diff] [blame] | 382 | if (ret) |
| 383 | return ret; |
| 384 | |
| 385 | plat = dev_get_platdata(dev); |
| 386 | plat->init_type = USB_INIT_DEVICE; |
| 387 | ret = device_probe(dev); |
| 388 | if (ret) |
| 389 | return ret; |
| 390 | *ctlrp = dev_get_priv(dev); |
| 391 | |
| 392 | return 0; |
| 393 | } |
| 394 | |
Simon Glass | 0566e24 | 2015-03-25 12:22:30 -0600 | [diff] [blame] | 395 | /* returns 0 if no match, 1 if match */ |
Masahiro Yamada | 121a4d1 | 2017-06-22 16:35:14 +0900 | [diff] [blame] | 396 | static int usb_match_device(const struct usb_device_descriptor *desc, |
| 397 | const struct usb_device_id *id) |
Simon Glass | 0566e24 | 2015-03-25 12:22:30 -0600 | [diff] [blame] | 398 | { |
| 399 | if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) && |
| 400 | id->idVendor != le16_to_cpu(desc->idVendor)) |
| 401 | return 0; |
| 402 | |
| 403 | if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) && |
| 404 | id->idProduct != le16_to_cpu(desc->idProduct)) |
| 405 | return 0; |
| 406 | |
| 407 | /* No need to test id->bcdDevice_lo != 0, since 0 is never |
| 408 | greater than any unsigned number. */ |
| 409 | if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) && |
| 410 | (id->bcdDevice_lo > le16_to_cpu(desc->bcdDevice))) |
| 411 | return 0; |
| 412 | |
| 413 | if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) && |
| 414 | (id->bcdDevice_hi < le16_to_cpu(desc->bcdDevice))) |
| 415 | return 0; |
| 416 | |
| 417 | if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) && |
| 418 | (id->bDeviceClass != desc->bDeviceClass)) |
| 419 | return 0; |
| 420 | |
| 421 | if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) && |
| 422 | (id->bDeviceSubClass != desc->bDeviceSubClass)) |
| 423 | return 0; |
| 424 | |
| 425 | if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) && |
| 426 | (id->bDeviceProtocol != desc->bDeviceProtocol)) |
| 427 | return 0; |
| 428 | |
| 429 | return 1; |
| 430 | } |
| 431 | |
| 432 | /* returns 0 if no match, 1 if match */ |
Masahiro Yamada | 121a4d1 | 2017-06-22 16:35:14 +0900 | [diff] [blame] | 433 | static int usb_match_one_id_intf(const struct usb_device_descriptor *desc, |
| 434 | const struct usb_interface_descriptor *int_desc, |
| 435 | const struct usb_device_id *id) |
Simon Glass | 0566e24 | 2015-03-25 12:22:30 -0600 | [diff] [blame] | 436 | { |
| 437 | /* The interface class, subclass, protocol and number should never be |
| 438 | * checked for a match if the device class is Vendor Specific, |
| 439 | * unless the match record specifies the Vendor ID. */ |
| 440 | if (desc->bDeviceClass == USB_CLASS_VENDOR_SPEC && |
| 441 | !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) && |
| 442 | (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS | |
| 443 | USB_DEVICE_ID_MATCH_INT_SUBCLASS | |
| 444 | USB_DEVICE_ID_MATCH_INT_PROTOCOL | |
| 445 | USB_DEVICE_ID_MATCH_INT_NUMBER))) |
| 446 | return 0; |
| 447 | |
| 448 | if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) && |
| 449 | (id->bInterfaceClass != int_desc->bInterfaceClass)) |
| 450 | return 0; |
| 451 | |
| 452 | if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) && |
| 453 | (id->bInterfaceSubClass != int_desc->bInterfaceSubClass)) |
| 454 | return 0; |
| 455 | |
| 456 | if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) && |
| 457 | (id->bInterfaceProtocol != int_desc->bInterfaceProtocol)) |
| 458 | return 0; |
| 459 | |
| 460 | if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) && |
| 461 | (id->bInterfaceNumber != int_desc->bInterfaceNumber)) |
| 462 | return 0; |
| 463 | |
| 464 | return 1; |
| 465 | } |
| 466 | |
| 467 | /* returns 0 if no match, 1 if match */ |
Masahiro Yamada | 121a4d1 | 2017-06-22 16:35:14 +0900 | [diff] [blame] | 468 | static int usb_match_one_id(struct usb_device_descriptor *desc, |
| 469 | struct usb_interface_descriptor *int_desc, |
| 470 | const struct usb_device_id *id) |
Simon Glass | 0566e24 | 2015-03-25 12:22:30 -0600 | [diff] [blame] | 471 | { |
| 472 | if (!usb_match_device(desc, id)) |
| 473 | return 0; |
| 474 | |
| 475 | return usb_match_one_id_intf(desc, int_desc, id); |
| 476 | } |
| 477 | |
| 478 | /** |
| 479 | * usb_find_and_bind_driver() - Find and bind the right USB driver |
| 480 | * |
| 481 | * This only looks at certain fields in the descriptor. |
| 482 | */ |
| 483 | static int usb_find_and_bind_driver(struct udevice *parent, |
| 484 | struct usb_device_descriptor *desc, |
| 485 | struct usb_interface_descriptor *iface, |
| 486 | int bus_seq, int devnum, |
| 487 | struct udevice **devp) |
| 488 | { |
| 489 | struct usb_driver_entry *start, *entry; |
| 490 | int n_ents; |
| 491 | int ret; |
| 492 | char name[30], *str; |
| 493 | |
| 494 | *devp = NULL; |
| 495 | debug("%s: Searching for driver\n", __func__); |
| 496 | start = ll_entry_start(struct usb_driver_entry, usb_driver_entry); |
| 497 | n_ents = ll_entry_count(struct usb_driver_entry, usb_driver_entry); |
| 498 | for (entry = start; entry != start + n_ents; entry++) { |
| 499 | const struct usb_device_id *id; |
| 500 | struct udevice *dev; |
| 501 | const struct driver *drv; |
| 502 | struct usb_dev_platdata *plat; |
| 503 | |
| 504 | for (id = entry->match; id->match_flags; id++) { |
| 505 | if (!usb_match_one_id(desc, iface, id)) |
| 506 | continue; |
| 507 | |
| 508 | drv = entry->driver; |
| 509 | /* |
| 510 | * We could pass the descriptor to the driver as |
| 511 | * platdata (instead of NULL) and allow its bind() |
| 512 | * method to return -ENOENT if it doesn't support this |
| 513 | * device. That way we could continue the search to |
| 514 | * find another driver. For now this doesn't seem |
| 515 | * necesssary, so just bind the first match. |
| 516 | */ |
| 517 | ret = device_bind(parent, drv, drv->name, NULL, -1, |
| 518 | &dev); |
| 519 | if (ret) |
| 520 | goto error; |
| 521 | debug("%s: Match found: %s\n", __func__, drv->name); |
| 522 | dev->driver_data = id->driver_info; |
| 523 | plat = dev_get_parent_platdata(dev); |
| 524 | plat->id = *id; |
| 525 | *devp = dev; |
| 526 | return 0; |
| 527 | } |
| 528 | } |
| 529 | |
Simon Glass | 449230f | 2015-03-25 12:22:31 -0600 | [diff] [blame] | 530 | /* Bind a generic driver so that the device can be used */ |
| 531 | snprintf(name, sizeof(name), "generic_bus_%x_dev_%x", bus_seq, devnum); |
| 532 | str = strdup(name); |
| 533 | if (!str) |
| 534 | return -ENOMEM; |
| 535 | ret = device_bind_driver(parent, "usb_dev_generic_drv", str, devp); |
| 536 | |
Simon Glass | 0566e24 | 2015-03-25 12:22:30 -0600 | [diff] [blame] | 537 | error: |
| 538 | debug("%s: No match found: %d\n", __func__, ret); |
| 539 | return ret; |
| 540 | } |
| 541 | |
| 542 | /** |
Simon Glass | 1b6a1df | 2015-11-08 23:47:56 -0700 | [diff] [blame] | 543 | * usb_find_child() - Find an existing device which matches our needs |
| 544 | * |
| 545 | * |
Simon Glass | 0566e24 | 2015-03-25 12:22:30 -0600 | [diff] [blame] | 546 | */ |
Simon Glass | 1b6a1df | 2015-11-08 23:47:56 -0700 | [diff] [blame] | 547 | static int usb_find_child(struct udevice *parent, |
| 548 | struct usb_device_descriptor *desc, |
| 549 | struct usb_interface_descriptor *iface, |
| 550 | struct udevice **devp) |
Simon Glass | 0566e24 | 2015-03-25 12:22:30 -0600 | [diff] [blame] | 551 | { |
| 552 | struct udevice *dev; |
| 553 | |
| 554 | *devp = NULL; |
| 555 | for (device_find_first_child(parent, &dev); |
| 556 | dev; |
| 557 | device_find_next_child(&dev)) { |
| 558 | struct usb_dev_platdata *plat = dev_get_parent_platdata(dev); |
| 559 | |
| 560 | /* If this device is already in use, skip it */ |
| 561 | if (device_active(dev)) |
| 562 | continue; |
| 563 | debug(" %s: name='%s', plat=%d, desc=%d\n", __func__, |
| 564 | dev->name, plat->id.bDeviceClass, desc->bDeviceClass); |
| 565 | if (usb_match_one_id(desc, iface, &plat->id)) { |
| 566 | *devp = dev; |
| 567 | return 0; |
| 568 | } |
| 569 | } |
Simon Glass | 1b6a1df | 2015-11-08 23:47:56 -0700 | [diff] [blame] | 570 | |
Simon Glass | 0566e24 | 2015-03-25 12:22:30 -0600 | [diff] [blame] | 571 | return -ENOENT; |
| 572 | } |
| 573 | |
Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 574 | int usb_scan_device(struct udevice *parent, int port, |
| 575 | enum usb_device_speed speed, struct udevice **devp) |
| 576 | { |
| 577 | struct udevice *dev; |
| 578 | bool created = false; |
| 579 | struct usb_dev_platdata *plat; |
| 580 | struct usb_bus_priv *priv; |
| 581 | struct usb_device *parent_udev; |
| 582 | int ret; |
| 583 | ALLOC_CACHE_ALIGN_BUFFER(struct usb_device, udev, 1); |
| 584 | struct usb_interface_descriptor *iface = &udev->config.if_desc[0].desc; |
| 585 | |
| 586 | *devp = NULL; |
| 587 | memset(udev, '\0', sizeof(*udev)); |
Hans de Goede | f78a5c0 | 2015-05-05 11:54:31 +0200 | [diff] [blame] | 588 | udev->controller_dev = usb_get_bus(parent); |
Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 589 | priv = dev_get_uclass_priv(udev->controller_dev); |
| 590 | |
| 591 | /* |
| 592 | * Somewhat nasty, this. We create a local device and use the normal |
| 593 | * USB stack to read its descriptor. Then we know what type of device |
| 594 | * to create for real. |
| 595 | * |
| 596 | * udev->dev is set to the parent, since we don't have a real device |
| 597 | * yet. The USB stack should not access udev.dev anyway, except perhaps |
| 598 | * to find the controller, and the controller will either be @parent, |
| 599 | * or some parent of @parent. |
| 600 | * |
| 601 | * Another option might be to create the device as a generic USB |
| 602 | * device, then morph it into the correct one when we know what it |
| 603 | * should be. This means that a generic USB device would morph into |
| 604 | * a network controller, or a USB flash stick, for example. However, |
| 605 | * we don't support such morphing and it isn't clear that it would |
| 606 | * be easy to do. |
| 607 | * |
| 608 | * Yet another option is to split out the USB stack parts of udev |
| 609 | * into something like a 'struct urb' (as Linux does) which can exist |
| 610 | * independently of any device. This feels cleaner, but calls for quite |
| 611 | * a big change to the USB stack. |
| 612 | * |
| 613 | * For now, the approach is to set up an empty udev, read its |
| 614 | * descriptor and assign it an address, then bind a real device and |
| 615 | * stash the resulting information into the device's parent |
| 616 | * platform data. Then when we probe it, usb_child_pre_probe() is called |
| 617 | * and it will pull the information out of the stash. |
| 618 | */ |
| 619 | udev->dev = parent; |
| 620 | udev->speed = speed; |
| 621 | udev->devnum = priv->next_addr + 1; |
| 622 | udev->portnr = port; |
| 623 | debug("Calling usb_setup_device(), portnr=%d\n", udev->portnr); |
| 624 | parent_udev = device_get_uclass_id(parent) == UCLASS_USB_HUB ? |
Simon Glass | bcbe3d1 | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 625 | dev_get_parent_priv(parent) : NULL; |
Hans de Goede | 9eb72dd | 2015-06-17 21:33:46 +0200 | [diff] [blame] | 626 | ret = usb_setup_device(udev, priv->desc_before_addr, parent_udev); |
Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 627 | debug("read_descriptor for '%s': ret=%d\n", parent->name, ret); |
| 628 | if (ret) |
| 629 | return ret; |
Simon Glass | 1b6a1df | 2015-11-08 23:47:56 -0700 | [diff] [blame] | 630 | ret = usb_find_child(parent, &udev->descriptor, iface, &dev); |
| 631 | debug("** usb_find_child returns %d\n", ret); |
Simon Glass | 0566e24 | 2015-03-25 12:22:30 -0600 | [diff] [blame] | 632 | if (ret) { |
| 633 | if (ret != -ENOENT) |
| 634 | return ret; |
| 635 | ret = usb_find_and_bind_driver(parent, &udev->descriptor, iface, |
| 636 | udev->controller_dev->seq, |
| 637 | udev->devnum, &dev); |
| 638 | if (ret) |
| 639 | return ret; |
| 640 | created = true; |
| 641 | } |
| 642 | plat = dev_get_parent_platdata(dev); |
| 643 | debug("%s: Probing '%s', plat=%p\n", __func__, dev->name, plat); |
| 644 | plat->devnum = udev->devnum; |
Hans de Goede | 7f1a075 | 2015-05-05 11:54:32 +0200 | [diff] [blame] | 645 | plat->udev = udev; |
Simon Glass | 0566e24 | 2015-03-25 12:22:30 -0600 | [diff] [blame] | 646 | priv->next_addr++; |
| 647 | ret = device_probe(dev); |
| 648 | if (ret) { |
| 649 | debug("%s: Device '%s' probe failed\n", __func__, dev->name); |
| 650 | priv->next_addr--; |
| 651 | if (created) |
| 652 | device_unbind(dev); |
| 653 | return ret; |
| 654 | } |
| 655 | *devp = dev; |
Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 656 | |
Simon Glass | 0566e24 | 2015-03-25 12:22:30 -0600 | [diff] [blame] | 657 | return 0; |
Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 658 | } |
| 659 | |
Simon Glass | 0c5dd9a | 2015-05-13 07:02:23 -0600 | [diff] [blame] | 660 | /* |
| 661 | * Detect if a USB device has been plugged or unplugged. |
| 662 | */ |
| 663 | int usb_detect_change(void) |
| 664 | { |
| 665 | struct udevice *hub; |
| 666 | struct uclass *uc; |
| 667 | int change = 0; |
| 668 | int ret; |
| 669 | |
| 670 | ret = uclass_get(UCLASS_USB_HUB, &uc); |
| 671 | if (ret) |
| 672 | return ret; |
| 673 | |
| 674 | uclass_foreach_dev(hub, uc) { |
| 675 | struct usb_device *udev; |
| 676 | struct udevice *dev; |
| 677 | |
| 678 | if (!device_active(hub)) |
| 679 | continue; |
| 680 | for (device_find_first_child(hub, &dev); |
| 681 | dev; |
| 682 | device_find_next_child(&dev)) { |
| 683 | struct usb_port_status status; |
| 684 | |
| 685 | if (!device_active(dev)) |
| 686 | continue; |
| 687 | |
Simon Glass | bcbe3d1 | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 688 | udev = dev_get_parent_priv(dev); |
Simon Glass | 0c5dd9a | 2015-05-13 07:02:23 -0600 | [diff] [blame] | 689 | if (usb_get_port_status(udev, udev->portnr, &status) |
| 690 | < 0) |
| 691 | /* USB request failed */ |
| 692 | continue; |
| 693 | |
| 694 | if (le16_to_cpu(status.wPortChange) & |
| 695 | USB_PORT_STAT_C_CONNECTION) |
| 696 | change++; |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | return change; |
| 701 | } |
| 702 | |
Masahiro Yamada | 121a4d1 | 2017-06-22 16:35:14 +0900 | [diff] [blame] | 703 | static int usb_child_post_bind(struct udevice *dev) |
Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 704 | { |
| 705 | struct usb_dev_platdata *plat = dev_get_parent_platdata(dev); |
Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 706 | int val; |
| 707 | |
Simon Glass | d20fd27 | 2017-05-18 20:09:38 -0600 | [diff] [blame] | 708 | if (!dev_of_valid(dev)) |
Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 709 | return 0; |
| 710 | |
| 711 | /* We only support matching a few things */ |
Simon Glass | d20fd27 | 2017-05-18 20:09:38 -0600 | [diff] [blame] | 712 | val = dev_read_u32_default(dev, "usb,device-class", -1); |
Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 713 | if (val != -1) { |
| 714 | plat->id.match_flags |= USB_DEVICE_ID_MATCH_DEV_CLASS; |
| 715 | plat->id.bDeviceClass = val; |
| 716 | } |
Simon Glass | d20fd27 | 2017-05-18 20:09:38 -0600 | [diff] [blame] | 717 | val = dev_read_u32_default(dev, "usb,interface-class", -1); |
Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 718 | if (val != -1) { |
| 719 | plat->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS; |
| 720 | plat->id.bInterfaceClass = val; |
| 721 | } |
| 722 | |
| 723 | return 0; |
| 724 | } |
| 725 | |
Hans de Goede | f78a5c0 | 2015-05-05 11:54:31 +0200 | [diff] [blame] | 726 | struct udevice *usb_get_bus(struct udevice *dev) |
Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 727 | { |
| 728 | struct udevice *bus; |
| 729 | |
Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 730 | for (bus = dev; bus && device_get_uclass_id(bus) != UCLASS_USB; ) |
| 731 | bus = bus->parent; |
| 732 | if (!bus) { |
| 733 | /* By design this cannot happen */ |
| 734 | assert(bus); |
| 735 | debug("USB HUB '%s' does not have a controller\n", dev->name); |
Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 736 | } |
Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 737 | |
Hans de Goede | f78a5c0 | 2015-05-05 11:54:31 +0200 | [diff] [blame] | 738 | return bus; |
Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | int usb_child_pre_probe(struct udevice *dev) |
| 742 | { |
Simon Glass | bcbe3d1 | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 743 | struct usb_device *udev = dev_get_parent_priv(dev); |
Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 744 | struct usb_dev_platdata *plat = dev_get_parent_platdata(dev); |
| 745 | int ret; |
| 746 | |
Hans de Goede | 7f1a075 | 2015-05-05 11:54:32 +0200 | [diff] [blame] | 747 | if (plat->udev) { |
| 748 | /* |
| 749 | * Copy over all the values set in the on stack struct |
| 750 | * usb_device in usb_scan_device() to our final struct |
| 751 | * usb_device for this dev. |
| 752 | */ |
| 753 | *udev = *(plat->udev); |
| 754 | /* And clear plat->udev as it will not be valid for long */ |
| 755 | plat->udev = NULL; |
| 756 | udev->dev = dev; |
| 757 | } else { |
| 758 | /* |
| 759 | * This happens with devices which are explicitly bound |
| 760 | * instead of being discovered through usb_scan_device() |
| 761 | * such as sandbox emul devices. |
| 762 | */ |
| 763 | udev->dev = dev; |
| 764 | udev->controller_dev = usb_get_bus(dev); |
| 765 | udev->devnum = plat->devnum; |
Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 766 | |
Hans de Goede | 7f1a075 | 2015-05-05 11:54:32 +0200 | [diff] [blame] | 767 | /* |
| 768 | * udev did not go through usb_scan_device(), so we need to |
| 769 | * select the config and read the config descriptors. |
| 770 | */ |
| 771 | ret = usb_select_config(udev); |
| 772 | if (ret) |
| 773 | return ret; |
| 774 | } |
Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 775 | |
| 776 | return 0; |
| 777 | } |
| 778 | |
| 779 | UCLASS_DRIVER(usb) = { |
| 780 | .id = UCLASS_USB, |
| 781 | .name = "usb", |
| 782 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
Simon Glass | 9119548 | 2016-07-05 17:10:10 -0600 | [diff] [blame] | 783 | .post_bind = dm_scan_fdt_dev, |
Hans de Goede | e253637 | 2015-05-10 14:10:21 +0200 | [diff] [blame] | 784 | .priv_auto_alloc_size = sizeof(struct usb_uclass_priv), |
Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 785 | .per_child_auto_alloc_size = sizeof(struct usb_device), |
| 786 | .per_device_auto_alloc_size = sizeof(struct usb_bus_priv), |
| 787 | .child_post_bind = usb_child_post_bind, |
| 788 | .child_pre_probe = usb_child_pre_probe, |
| 789 | .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata), |
| 790 | }; |
Simon Glass | 449230f | 2015-03-25 12:22:31 -0600 | [diff] [blame] | 791 | |
| 792 | UCLASS_DRIVER(usb_dev_generic) = { |
| 793 | .id = UCLASS_USB_DEV_GENERIC, |
| 794 | .name = "usb_dev_generic", |
| 795 | }; |
| 796 | |
| 797 | U_BOOT_DRIVER(usb_dev_generic_drv) = { |
| 798 | .id = UCLASS_USB_DEV_GENERIC, |
| 799 | .name = "usb_dev_generic_drv", |
| 800 | }; |