2 * (C) Copyright 2015 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
5 * usb_match_device() modified from Linux kernel v4.0.
7 * SPDX-License-Identifier: GPL-2.0+
15 #include <dm/device-internal.h>
17 #include <dm/uclass-internal.h>
19 extern bool usb_started; /* flag for the started/stopped USB status */
20 static bool asynch_allowed;
22 struct usb_uclass_priv {
23 int companion_device_count;
26 int usb_disable_asynch(int disable)
28 int old_value = asynch_allowed;
30 asynch_allowed = !disable;
34 int submit_int_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
35 int length, int interval)
37 struct udevice *bus = udev->controller_dev;
38 struct dm_usb_ops *ops = usb_get_ops(bus);
43 return ops->interrupt(bus, udev, pipe, buffer, length, interval);
46 int submit_control_msg(struct usb_device *udev, unsigned long pipe,
47 void *buffer, int length, struct devrequest *setup)
49 struct udevice *bus = udev->controller_dev;
50 struct dm_usb_ops *ops = usb_get_ops(bus);
51 struct usb_uclass_priv *uc_priv = bus->uclass->priv;
57 err = ops->control(bus, udev, pipe, buffer, length, setup);
58 if (setup->request == USB_REQ_SET_FEATURE &&
59 setup->requesttype == USB_RT_PORT &&
60 setup->value == cpu_to_le16(USB_PORT_FEAT_RESET) &&
62 /* Device handed over to companion after port reset */
63 uc_priv->companion_device_count++;
69 int submit_bulk_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
72 struct udevice *bus = udev->controller_dev;
73 struct dm_usb_ops *ops = usb_get_ops(bus);
78 return ops->bulk(bus, udev, pipe, buffer, length);
81 struct int_queue *create_int_queue(struct usb_device *udev,
82 unsigned long pipe, int queuesize, int elementsize,
83 void *buffer, int interval)
85 struct udevice *bus = udev->controller_dev;
86 struct dm_usb_ops *ops = usb_get_ops(bus);
88 if (!ops->create_int_queue)
91 return ops->create_int_queue(bus, udev, pipe, queuesize, elementsize,
95 void *poll_int_queue(struct usb_device *udev, struct int_queue *queue)
97 struct udevice *bus = udev->controller_dev;
98 struct dm_usb_ops *ops = usb_get_ops(bus);
100 if (!ops->poll_int_queue)
103 return ops->poll_int_queue(bus, udev, queue);
106 int destroy_int_queue(struct usb_device *udev, struct int_queue *queue)
108 struct udevice *bus = udev->controller_dev;
109 struct dm_usb_ops *ops = usb_get_ops(bus);
111 if (!ops->destroy_int_queue)
114 return ops->destroy_int_queue(bus, udev, queue);
117 int usb_alloc_device(struct usb_device *udev)
119 struct udevice *bus = udev->controller_dev;
120 struct dm_usb_ops *ops = usb_get_ops(bus);
122 /* This is only requird by some controllers - current XHCI */
123 if (!ops->alloc_device)
126 return ops->alloc_device(bus, udev);
129 int usb_reset_root_port(struct usb_device *udev)
131 struct udevice *bus = udev->controller_dev;
132 struct dm_usb_ops *ops = usb_get_ops(bus);
134 if (!ops->reset_root_port)
137 return ops->reset_root_port(bus, udev);
140 int usb_update_hub_device(struct usb_device *udev)
142 struct udevice *bus = udev->controller_dev;
143 struct dm_usb_ops *ops = usb_get_ops(bus);
145 if (!ops->update_hub_device)
148 return ops->update_hub_device(bus, udev);
151 int usb_get_max_xfer_size(struct usb_device *udev, size_t *size)
153 struct udevice *bus = udev->controller_dev;
154 struct dm_usb_ops *ops = usb_get_ops(bus);
156 if (!ops->get_max_xfer_size)
159 return ops->get_max_xfer_size(bus, size);
167 struct usb_uclass_priv *uc_priv;
170 /* De-activate any devices that have been activated */
171 ret = uclass_get(UCLASS_USB, &uc);
177 uclass_foreach_dev(bus, uc) {
178 ret = device_remove(bus, DM_REMOVE_NORMAL);
182 /* Locate root hub device */
183 device_find_first_child(bus, &rh);
186 * All USB devices are children of root hub.
187 * Unbinding root hub will unbind all of its children.
189 ret = device_unbind(rh);
195 #ifdef CONFIG_USB_STORAGE
198 uc_priv->companion_device_count = 0;
204 static void usb_scan_bus(struct udevice *bus, bool recurse)
206 struct usb_bus_priv *priv;
210 priv = dev_get_uclass_priv(bus);
212 assert(recurse); /* TODO: Support non-recusive */
214 printf("scanning bus %d for devices... ", bus->seq);
216 ret = usb_scan_device(bus, 0, USB_SPEED_FULL, &dev);
218 printf("failed, error %d\n", ret);
219 else if (priv->next_addr == 0)
220 printf("No USB Device found\n");
222 printf("%d USB Device(s) found\n", priv->next_addr);
225 static void remove_inactive_children(struct uclass *uc, struct udevice *bus)
227 uclass_foreach_dev(bus, uc) {
228 struct udevice *dev, *next;
230 if (!device_active(bus))
232 device_foreach_child_safe(dev, next, bus) {
233 if (!device_active(dev))
241 int controllers_initialized = 0;
242 struct usb_uclass_priv *uc_priv;
243 struct usb_bus_priv *priv;
251 ret = uclass_get(UCLASS_USB, &uc);
257 uclass_foreach_dev(bus, uc) {
258 /* init low_level USB */
259 printf("USB%d: ", count);
262 #ifdef CONFIG_SANDBOX
264 * For Sandbox, we need scan the device tree each time when we
265 * start the USB stack, in order to re-create the emulated USB
266 * devices and bind drivers for them before we actually do the
269 ret = dm_scan_fdt_dev(bus);
271 printf("Sandbox USB device scan failed (%d)\n", ret);
276 ret = device_probe(bus);
277 if (ret == -ENODEV) { /* No such device. */
278 puts("Port not available.\n");
279 controllers_initialized++;
283 if (ret) { /* Other error. */
284 printf("probe failed, error %d\n", ret);
287 controllers_initialized++;
292 * lowlevel init done, now scan the bus for devices i.e. search HUBs
293 * and configure them, first scan primary controllers.
295 uclass_foreach_dev(bus, uc) {
296 if (!device_active(bus))
299 priv = dev_get_uclass_priv(bus);
300 if (!priv->companion)
301 usb_scan_bus(bus, true);
305 * Now that the primary controllers have been scanned and have handed
306 * over any devices they do not understand to their companions, scan
307 * the companions if necessary.
309 if (uc_priv->companion_device_count) {
310 uclass_foreach_dev(bus, uc) {
311 if (!device_active(bus))
314 priv = dev_get_uclass_priv(bus);
316 usb_scan_bus(bus, true);
322 /* Remove any devices that were not found on this scan */
323 remove_inactive_children(uc, bus);
325 ret = uclass_get(UCLASS_USB_HUB, &uc);
328 remove_inactive_children(uc, bus);
330 /* if we were not able to find at least one working bus, bail out */
332 printf("No controllers found\n");
333 else if (controllers_initialized == 0)
334 printf("USB error: all controllers failed lowlevel init\n");
336 return usb_started ? 0 : -1;
340 * TODO(sjg@chromium.org): Remove this legacy function. At present it is needed
341 * to support boards which use driver model for USB but not Ethernet, and want
342 * to use USB Ethernet.
344 * The #if clause is here to ensure that remains the only case.
346 #if !defined(CONFIG_DM_ETH) && defined(CONFIG_USB_HOST_ETHER)
347 static struct usb_device *find_child_devnum(struct udevice *parent, int devnum)
349 struct usb_device *udev;
352 if (!device_active(parent))
354 udev = dev_get_parent_priv(parent);
355 if (udev->devnum == devnum)
358 for (device_find_first_child(parent, &dev);
360 device_find_next_child(&dev)) {
361 udev = find_child_devnum(dev, devnum);
369 struct usb_device *usb_get_dev_index(struct udevice *bus, int index)
372 int devnum = index + 1; /* Addresses are allocated from 1 on USB */
374 device_find_first_child(bus, &dev);
378 return find_child_devnum(dev, devnum);
382 int usb_setup_ehci_gadget(struct ehci_ctrl **ctlrp)
384 struct usb_platdata *plat;
388 /* Find the old device and remove it */
389 ret = uclass_find_device_by_seq(UCLASS_USB, 0, true, &dev);
392 ret = device_remove(dev, DM_REMOVE_NORMAL);
396 plat = dev_get_platdata(dev);
397 plat->init_type = USB_INIT_DEVICE;
398 ret = device_probe(dev);
401 *ctlrp = dev_get_priv(dev);
406 /* returns 0 if no match, 1 if match */
407 static int usb_match_device(const struct usb_device_descriptor *desc,
408 const struct usb_device_id *id)
410 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
411 id->idVendor != le16_to_cpu(desc->idVendor))
414 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
415 id->idProduct != le16_to_cpu(desc->idProduct))
418 /* No need to test id->bcdDevice_lo != 0, since 0 is never
419 greater than any unsigned number. */
420 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
421 (id->bcdDevice_lo > le16_to_cpu(desc->bcdDevice)))
424 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
425 (id->bcdDevice_hi < le16_to_cpu(desc->bcdDevice)))
428 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
429 (id->bDeviceClass != desc->bDeviceClass))
432 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
433 (id->bDeviceSubClass != desc->bDeviceSubClass))
436 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
437 (id->bDeviceProtocol != desc->bDeviceProtocol))
443 /* returns 0 if no match, 1 if match */
444 static int usb_match_one_id_intf(const struct usb_device_descriptor *desc,
445 const struct usb_interface_descriptor *int_desc,
446 const struct usb_device_id *id)
448 /* The interface class, subclass, protocol and number should never be
449 * checked for a match if the device class is Vendor Specific,
450 * unless the match record specifies the Vendor ID. */
451 if (desc->bDeviceClass == USB_CLASS_VENDOR_SPEC &&
452 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
453 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
454 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
455 USB_DEVICE_ID_MATCH_INT_PROTOCOL |
456 USB_DEVICE_ID_MATCH_INT_NUMBER)))
459 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
460 (id->bInterfaceClass != int_desc->bInterfaceClass))
463 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
464 (id->bInterfaceSubClass != int_desc->bInterfaceSubClass))
467 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
468 (id->bInterfaceProtocol != int_desc->bInterfaceProtocol))
471 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
472 (id->bInterfaceNumber != int_desc->bInterfaceNumber))
478 /* returns 0 if no match, 1 if match */
479 static int usb_match_one_id(struct usb_device_descriptor *desc,
480 struct usb_interface_descriptor *int_desc,
481 const struct usb_device_id *id)
483 if (!usb_match_device(desc, id))
486 return usb_match_one_id_intf(desc, int_desc, id);
490 * usb_find_and_bind_driver() - Find and bind the right USB driver
492 * This only looks at certain fields in the descriptor.
494 static int usb_find_and_bind_driver(struct udevice *parent,
495 struct usb_device_descriptor *desc,
496 struct usb_interface_descriptor *iface,
497 int bus_seq, int devnum,
498 struct udevice **devp)
500 struct usb_driver_entry *start, *entry;
506 debug("%s: Searching for driver\n", __func__);
507 start = ll_entry_start(struct usb_driver_entry, usb_driver_entry);
508 n_ents = ll_entry_count(struct usb_driver_entry, usb_driver_entry);
509 for (entry = start; entry != start + n_ents; entry++) {
510 const struct usb_device_id *id;
512 const struct driver *drv;
513 struct usb_dev_platdata *plat;
515 for (id = entry->match; id->match_flags; id++) {
516 if (!usb_match_one_id(desc, iface, id))
521 * We could pass the descriptor to the driver as
522 * platdata (instead of NULL) and allow its bind()
523 * method to return -ENOENT if it doesn't support this
524 * device. That way we could continue the search to
525 * find another driver. For now this doesn't seem
526 * necesssary, so just bind the first match.
528 ret = device_bind(parent, drv, drv->name, NULL, -1,
532 debug("%s: Match found: %s\n", __func__, drv->name);
533 dev->driver_data = id->driver_info;
534 plat = dev_get_parent_platdata(dev);
541 /* Bind a generic driver so that the device can be used */
542 snprintf(name, sizeof(name), "generic_bus_%x_dev_%x", bus_seq, devnum);
546 ret = device_bind_driver(parent, "usb_dev_generic_drv", str, devp);
549 debug("%s: No match found: %d\n", __func__, ret);
554 * usb_find_child() - Find an existing device which matches our needs
558 static int usb_find_child(struct udevice *parent,
559 struct usb_device_descriptor *desc,
560 struct usb_interface_descriptor *iface,
561 struct udevice **devp)
566 for (device_find_first_child(parent, &dev);
568 device_find_next_child(&dev)) {
569 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
571 /* If this device is already in use, skip it */
572 if (device_active(dev))
574 debug(" %s: name='%s', plat=%d, desc=%d\n", __func__,
575 dev->name, plat->id.bDeviceClass, desc->bDeviceClass);
576 if (usb_match_one_id(desc, iface, &plat->id)) {
585 int usb_scan_device(struct udevice *parent, int port,
586 enum usb_device_speed speed, struct udevice **devp)
589 bool created = false;
590 struct usb_dev_platdata *plat;
591 struct usb_bus_priv *priv;
592 struct usb_device *parent_udev;
594 ALLOC_CACHE_ALIGN_BUFFER(struct usb_device, udev, 1);
595 struct usb_interface_descriptor *iface = &udev->config.if_desc[0].desc;
598 memset(udev, '\0', sizeof(*udev));
599 udev->controller_dev = usb_get_bus(parent);
600 priv = dev_get_uclass_priv(udev->controller_dev);
603 * Somewhat nasty, this. We create a local device and use the normal
604 * USB stack to read its descriptor. Then we know what type of device
605 * to create for real.
607 * udev->dev is set to the parent, since we don't have a real device
608 * yet. The USB stack should not access udev.dev anyway, except perhaps
609 * to find the controller, and the controller will either be @parent,
610 * or some parent of @parent.
612 * Another option might be to create the device as a generic USB
613 * device, then morph it into the correct one when we know what it
614 * should be. This means that a generic USB device would morph into
615 * a network controller, or a USB flash stick, for example. However,
616 * we don't support such morphing and it isn't clear that it would
619 * Yet another option is to split out the USB stack parts of udev
620 * into something like a 'struct urb' (as Linux does) which can exist
621 * independently of any device. This feels cleaner, but calls for quite
622 * a big change to the USB stack.
624 * For now, the approach is to set up an empty udev, read its
625 * descriptor and assign it an address, then bind a real device and
626 * stash the resulting information into the device's parent
627 * platform data. Then when we probe it, usb_child_pre_probe() is called
628 * and it will pull the information out of the stash.
632 udev->devnum = priv->next_addr + 1;
634 debug("Calling usb_setup_device(), portnr=%d\n", udev->portnr);
635 parent_udev = device_get_uclass_id(parent) == UCLASS_USB_HUB ?
636 dev_get_parent_priv(parent) : NULL;
637 ret = usb_setup_device(udev, priv->desc_before_addr, parent_udev);
638 debug("read_descriptor for '%s': ret=%d\n", parent->name, ret);
641 ret = usb_find_child(parent, &udev->descriptor, iface, &dev);
642 debug("** usb_find_child returns %d\n", ret);
646 ret = usb_find_and_bind_driver(parent, &udev->descriptor, iface,
647 udev->controller_dev->seq,
653 plat = dev_get_parent_platdata(dev);
654 debug("%s: Probing '%s', plat=%p\n", __func__, dev->name, plat);
655 plat->devnum = udev->devnum;
658 ret = device_probe(dev);
660 debug("%s: Device '%s' probe failed\n", __func__, dev->name);
672 * Detect if a USB device has been plugged or unplugged.
674 int usb_detect_change(void)
681 ret = uclass_get(UCLASS_USB_HUB, &uc);
685 uclass_foreach_dev(hub, uc) {
686 struct usb_device *udev;
689 if (!device_active(hub))
691 for (device_find_first_child(hub, &dev);
693 device_find_next_child(&dev)) {
694 struct usb_port_status status;
696 if (!device_active(dev))
699 udev = dev_get_parent_priv(dev);
700 if (usb_get_port_status(udev, udev->portnr, &status)
702 /* USB request failed */
705 if (le16_to_cpu(status.wPortChange) &
706 USB_PORT_STAT_C_CONNECTION)
714 static int usb_child_post_bind(struct udevice *dev)
716 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
719 if (!dev_of_valid(dev))
722 /* We only support matching a few things */
723 val = dev_read_u32_default(dev, "usb,device-class", -1);
725 plat->id.match_flags |= USB_DEVICE_ID_MATCH_DEV_CLASS;
726 plat->id.bDeviceClass = val;
728 val = dev_read_u32_default(dev, "usb,interface-class", -1);
730 plat->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
731 plat->id.bInterfaceClass = val;
737 struct udevice *usb_get_bus(struct udevice *dev)
741 for (bus = dev; bus && device_get_uclass_id(bus) != UCLASS_USB; )
744 /* By design this cannot happen */
746 debug("USB HUB '%s' does not have a controller\n", dev->name);
752 int usb_child_pre_probe(struct udevice *dev)
754 struct usb_device *udev = dev_get_parent_priv(dev);
755 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
760 * Copy over all the values set in the on stack struct
761 * usb_device in usb_scan_device() to our final struct
762 * usb_device for this dev.
764 *udev = *(plat->udev);
765 /* And clear plat->udev as it will not be valid for long */
770 * This happens with devices which are explicitly bound
771 * instead of being discovered through usb_scan_device()
772 * such as sandbox emul devices.
775 udev->controller_dev = usb_get_bus(dev);
776 udev->devnum = plat->devnum;
779 * udev did not go through usb_scan_device(), so we need to
780 * select the config and read the config descriptors.
782 ret = usb_select_config(udev);
790 UCLASS_DRIVER(usb) = {
793 .flags = DM_UC_FLAG_SEQ_ALIAS,
794 .post_bind = dm_scan_fdt_dev,
795 .priv_auto_alloc_size = sizeof(struct usb_uclass_priv),
796 .per_child_auto_alloc_size = sizeof(struct usb_device),
797 .per_device_auto_alloc_size = sizeof(struct usb_bus_priv),
798 .child_post_bind = usb_child_post_bind,
799 .child_pre_probe = usb_child_pre_probe,
800 .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),
803 UCLASS_DRIVER(usb_dev_generic) = {
804 .id = UCLASS_USB_DEV_GENERIC,
805 .name = "usb_dev_generic",
808 U_BOOT_DRIVER(usb_dev_generic_drv) = {
809 .id = UCLASS_USB_DEV_GENERIC,
810 .name = "usb_dev_generic_drv",