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 DECLARE_GLOBAL_DATA_PTR;
21 extern bool usb_started; /* flag for the started/stopped USB status */
22 static bool asynch_allowed;
24 struct usb_uclass_priv {
25 int companion_device_count;
28 int usb_disable_asynch(int disable)
30 int old_value = asynch_allowed;
32 asynch_allowed = !disable;
36 int submit_int_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
37 int length, int interval)
39 struct udevice *bus = udev->controller_dev;
40 struct dm_usb_ops *ops = usb_get_ops(bus);
45 return ops->interrupt(bus, udev, pipe, buffer, length, interval);
48 int submit_control_msg(struct usb_device *udev, unsigned long pipe,
49 void *buffer, int length, struct devrequest *setup)
51 struct udevice *bus = udev->controller_dev;
52 struct dm_usb_ops *ops = usb_get_ops(bus);
53 struct usb_uclass_priv *uc_priv = bus->uclass->priv;
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) &&
64 /* Device handed over to companion after port reset */
65 uc_priv->companion_device_count++;
71 int submit_bulk_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
74 struct udevice *bus = udev->controller_dev;
75 struct dm_usb_ops *ops = usb_get_ops(bus);
80 return ops->bulk(bus, udev, pipe, buffer, length);
83 struct int_queue *create_int_queue(struct usb_device *udev,
84 unsigned long pipe, int queuesize, int elementsize,
85 void *buffer, int interval)
87 struct udevice *bus = udev->controller_dev;
88 struct dm_usb_ops *ops = usb_get_ops(bus);
90 if (!ops->create_int_queue)
93 return ops->create_int_queue(bus, udev, pipe, queuesize, elementsize,
97 void *poll_int_queue(struct usb_device *udev, struct int_queue *queue)
99 struct udevice *bus = udev->controller_dev;
100 struct dm_usb_ops *ops = usb_get_ops(bus);
102 if (!ops->poll_int_queue)
105 return ops->poll_int_queue(bus, udev, queue);
108 int destroy_int_queue(struct usb_device *udev, struct int_queue *queue)
110 struct udevice *bus = udev->controller_dev;
111 struct dm_usb_ops *ops = usb_get_ops(bus);
113 if (!ops->destroy_int_queue)
116 return ops->destroy_int_queue(bus, udev, queue);
119 int usb_alloc_device(struct usb_device *udev)
121 struct udevice *bus = udev->controller_dev;
122 struct dm_usb_ops *ops = usb_get_ops(bus);
124 /* This is only requird by some controllers - current XHCI */
125 if (!ops->alloc_device)
128 return ops->alloc_device(bus, udev);
131 int usb_reset_root_port(struct usb_device *udev)
133 struct udevice *bus = udev->controller_dev;
134 struct dm_usb_ops *ops = usb_get_ops(bus);
136 if (!ops->reset_root_port)
139 return ops->reset_root_port(bus, udev);
146 struct usb_uclass_priv *uc_priv;
149 /* De-activate any devices that have been activated */
150 ret = uclass_get(UCLASS_USB, &uc);
156 uclass_foreach_dev(bus, uc) {
157 ret = device_remove(bus, DM_REMOVE_NORMAL);
162 ret = blk_unbind_all(IF_TYPE_USB);
166 #ifdef CONFIG_SANDBOX
169 /* Reset all enulation devices */
170 ret = uclass_get(UCLASS_USB_EMUL, &uc);
174 uclass_foreach_dev(dev, uc)
177 #ifdef CONFIG_USB_STORAGE
180 uc_priv->companion_device_count = 0;
186 static void usb_scan_bus(struct udevice *bus, bool recurse)
188 struct usb_bus_priv *priv;
192 priv = dev_get_uclass_priv(bus);
194 assert(recurse); /* TODO: Support non-recusive */
196 printf("scanning bus %d for devices... ", bus->seq);
198 ret = usb_scan_device(bus, 0, USB_SPEED_FULL, &dev);
200 printf("failed, error %d\n", ret);
201 else if (priv->next_addr == 0)
202 printf("No USB Device found\n");
204 printf("%d USB Device(s) found\n", priv->next_addr);
207 static void remove_inactive_children(struct uclass *uc, struct udevice *bus)
209 uclass_foreach_dev(bus, uc) {
210 struct udevice *dev, *next;
212 if (!device_active(bus))
214 device_foreach_child_safe(dev, next, bus) {
215 if (!device_active(dev))
223 int controllers_initialized = 0;
224 struct usb_uclass_priv *uc_priv;
225 struct usb_bus_priv *priv;
233 ret = uclass_get(UCLASS_USB, &uc);
239 uclass_foreach_dev(bus, uc) {
240 /* init low_level USB */
241 printf("USB%d: ", count);
243 ret = device_probe(bus);
244 if (ret == -ENODEV) { /* No such device. */
245 puts("Port not available.\n");
246 controllers_initialized++;
250 if (ret) { /* Other error. */
251 printf("probe failed, error %d\n", ret);
254 controllers_initialized++;
259 * lowlevel init done, now scan the bus for devices i.e. search HUBs
260 * and configure them, first scan primary controllers.
262 uclass_foreach_dev(bus, uc) {
263 if (!device_active(bus))
266 priv = dev_get_uclass_priv(bus);
267 if (!priv->companion)
268 usb_scan_bus(bus, true);
272 * Now that the primary controllers have been scanned and have handed
273 * over any devices they do not understand to their companions, scan
274 * the companions if necessary.
276 if (uc_priv->companion_device_count) {
277 uclass_foreach_dev(bus, uc) {
278 if (!device_active(bus))
281 priv = dev_get_uclass_priv(bus);
283 usb_scan_bus(bus, true);
289 /* Remove any devices that were not found on this scan */
290 remove_inactive_children(uc, bus);
292 ret = uclass_get(UCLASS_USB_HUB, &uc);
295 remove_inactive_children(uc, bus);
297 /* if we were not able to find at least one working bus, bail out */
299 printf("No controllers found\n");
300 else if (controllers_initialized == 0)
301 printf("USB error: all controllers failed lowlevel init\n");
303 return usb_started ? 0 : -1;
307 * TODO(sjg@chromium.org): Remove this legacy function. At present it is needed
308 * to support boards which use driver model for USB but not Ethernet, and want
309 * to use USB Ethernet.
311 * The #if clause is here to ensure that remains the only case.
313 #if !defined(CONFIG_DM_ETH) && defined(CONFIG_USB_HOST_ETHER)
314 static struct usb_device *find_child_devnum(struct udevice *parent, int devnum)
316 struct usb_device *udev;
319 if (!device_active(parent))
321 udev = dev_get_parent_priv(parent);
322 if (udev->devnum == devnum)
325 for (device_find_first_child(parent, &dev);
327 device_find_next_child(&dev)) {
328 udev = find_child_devnum(dev, devnum);
336 struct usb_device *usb_get_dev_index(struct udevice *bus, int index)
339 int devnum = index + 1; /* Addresses are allocated from 1 on USB */
341 device_find_first_child(bus, &dev);
345 return find_child_devnum(dev, devnum);
349 int usb_setup_ehci_gadget(struct ehci_ctrl **ctlrp)
351 struct usb_platdata *plat;
355 /* Find the old device and remove it */
356 ret = uclass_find_device_by_seq(UCLASS_USB, 0, true, &dev);
359 ret = device_remove(dev, DM_REMOVE_NORMAL);
363 plat = dev_get_platdata(dev);
364 plat->init_type = USB_INIT_DEVICE;
365 ret = device_probe(dev);
368 *ctlrp = dev_get_priv(dev);
373 /* returns 0 if no match, 1 if match */
374 static int usb_match_device(const struct usb_device_descriptor *desc,
375 const struct usb_device_id *id)
377 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
378 id->idVendor != le16_to_cpu(desc->idVendor))
381 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
382 id->idProduct != le16_to_cpu(desc->idProduct))
385 /* No need to test id->bcdDevice_lo != 0, since 0 is never
386 greater than any unsigned number. */
387 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
388 (id->bcdDevice_lo > le16_to_cpu(desc->bcdDevice)))
391 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
392 (id->bcdDevice_hi < le16_to_cpu(desc->bcdDevice)))
395 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
396 (id->bDeviceClass != desc->bDeviceClass))
399 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
400 (id->bDeviceSubClass != desc->bDeviceSubClass))
403 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
404 (id->bDeviceProtocol != desc->bDeviceProtocol))
410 /* returns 0 if no match, 1 if match */
411 static int usb_match_one_id_intf(const struct usb_device_descriptor *desc,
412 const struct usb_interface_descriptor *int_desc,
413 const struct usb_device_id *id)
415 /* The interface class, subclass, protocol and number should never be
416 * checked for a match if the device class is Vendor Specific,
417 * unless the match record specifies the Vendor ID. */
418 if (desc->bDeviceClass == USB_CLASS_VENDOR_SPEC &&
419 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
420 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
421 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
422 USB_DEVICE_ID_MATCH_INT_PROTOCOL |
423 USB_DEVICE_ID_MATCH_INT_NUMBER)))
426 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
427 (id->bInterfaceClass != int_desc->bInterfaceClass))
430 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
431 (id->bInterfaceSubClass != int_desc->bInterfaceSubClass))
434 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
435 (id->bInterfaceProtocol != int_desc->bInterfaceProtocol))
438 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
439 (id->bInterfaceNumber != int_desc->bInterfaceNumber))
445 /* returns 0 if no match, 1 if match */
446 static int usb_match_one_id(struct usb_device_descriptor *desc,
447 struct usb_interface_descriptor *int_desc,
448 const struct usb_device_id *id)
450 if (!usb_match_device(desc, id))
453 return usb_match_one_id_intf(desc, int_desc, id);
457 * usb_find_and_bind_driver() - Find and bind the right USB driver
459 * This only looks at certain fields in the descriptor.
461 static int usb_find_and_bind_driver(struct udevice *parent,
462 struct usb_device_descriptor *desc,
463 struct usb_interface_descriptor *iface,
464 int bus_seq, int devnum,
465 struct udevice **devp)
467 struct usb_driver_entry *start, *entry;
473 debug("%s: Searching for driver\n", __func__);
474 start = ll_entry_start(struct usb_driver_entry, usb_driver_entry);
475 n_ents = ll_entry_count(struct usb_driver_entry, usb_driver_entry);
476 for (entry = start; entry != start + n_ents; entry++) {
477 const struct usb_device_id *id;
479 const struct driver *drv;
480 struct usb_dev_platdata *plat;
482 for (id = entry->match; id->match_flags; id++) {
483 if (!usb_match_one_id(desc, iface, id))
488 * We could pass the descriptor to the driver as
489 * platdata (instead of NULL) and allow its bind()
490 * method to return -ENOENT if it doesn't support this
491 * device. That way we could continue the search to
492 * find another driver. For now this doesn't seem
493 * necesssary, so just bind the first match.
495 ret = device_bind(parent, drv, drv->name, NULL, -1,
499 debug("%s: Match found: %s\n", __func__, drv->name);
500 dev->driver_data = id->driver_info;
501 plat = dev_get_parent_platdata(dev);
508 /* Bind a generic driver so that the device can be used */
509 snprintf(name, sizeof(name), "generic_bus_%x_dev_%x", bus_seq, devnum);
513 ret = device_bind_driver(parent, "usb_dev_generic_drv", str, devp);
516 debug("%s: No match found: %d\n", __func__, ret);
521 * usb_find_child() - Find an existing device which matches our needs
525 static int usb_find_child(struct udevice *parent,
526 struct usb_device_descriptor *desc,
527 struct usb_interface_descriptor *iface,
528 struct udevice **devp)
533 for (device_find_first_child(parent, &dev);
535 device_find_next_child(&dev)) {
536 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
538 /* If this device is already in use, skip it */
539 if (device_active(dev))
541 debug(" %s: name='%s', plat=%d, desc=%d\n", __func__,
542 dev->name, plat->id.bDeviceClass, desc->bDeviceClass);
543 if (usb_match_one_id(desc, iface, &plat->id)) {
552 int usb_scan_device(struct udevice *parent, int port,
553 enum usb_device_speed speed, struct udevice **devp)
556 bool created = false;
557 struct usb_dev_platdata *plat;
558 struct usb_bus_priv *priv;
559 struct usb_device *parent_udev;
561 ALLOC_CACHE_ALIGN_BUFFER(struct usb_device, udev, 1);
562 struct usb_interface_descriptor *iface = &udev->config.if_desc[0].desc;
565 memset(udev, '\0', sizeof(*udev));
566 udev->controller_dev = usb_get_bus(parent);
567 priv = dev_get_uclass_priv(udev->controller_dev);
570 * Somewhat nasty, this. We create a local device and use the normal
571 * USB stack to read its descriptor. Then we know what type of device
572 * to create for real.
574 * udev->dev is set to the parent, since we don't have a real device
575 * yet. The USB stack should not access udev.dev anyway, except perhaps
576 * to find the controller, and the controller will either be @parent,
577 * or some parent of @parent.
579 * Another option might be to create the device as a generic USB
580 * device, then morph it into the correct one when we know what it
581 * should be. This means that a generic USB device would morph into
582 * a network controller, or a USB flash stick, for example. However,
583 * we don't support such morphing and it isn't clear that it would
586 * Yet another option is to split out the USB stack parts of udev
587 * into something like a 'struct urb' (as Linux does) which can exist
588 * independently of any device. This feels cleaner, but calls for quite
589 * a big change to the USB stack.
591 * For now, the approach is to set up an empty udev, read its
592 * descriptor and assign it an address, then bind a real device and
593 * stash the resulting information into the device's parent
594 * platform data. Then when we probe it, usb_child_pre_probe() is called
595 * and it will pull the information out of the stash.
599 udev->devnum = priv->next_addr + 1;
601 debug("Calling usb_setup_device(), portnr=%d\n", udev->portnr);
602 parent_udev = device_get_uclass_id(parent) == UCLASS_USB_HUB ?
603 dev_get_parent_priv(parent) : NULL;
604 ret = usb_setup_device(udev, priv->desc_before_addr, parent_udev);
605 debug("read_descriptor for '%s': ret=%d\n", parent->name, ret);
608 ret = usb_find_child(parent, &udev->descriptor, iface, &dev);
609 debug("** usb_find_child returns %d\n", ret);
613 ret = usb_find_and_bind_driver(parent, &udev->descriptor, iface,
614 udev->controller_dev->seq,
620 plat = dev_get_parent_platdata(dev);
621 debug("%s: Probing '%s', plat=%p\n", __func__, dev->name, plat);
622 plat->devnum = udev->devnum;
625 ret = device_probe(dev);
627 debug("%s: Device '%s' probe failed\n", __func__, dev->name);
639 * Detect if a USB device has been plugged or unplugged.
641 int usb_detect_change(void)
648 ret = uclass_get(UCLASS_USB_HUB, &uc);
652 uclass_foreach_dev(hub, uc) {
653 struct usb_device *udev;
656 if (!device_active(hub))
658 for (device_find_first_child(hub, &dev);
660 device_find_next_child(&dev)) {
661 struct usb_port_status status;
663 if (!device_active(dev))
666 udev = dev_get_parent_priv(dev);
667 if (usb_get_port_status(udev, udev->portnr, &status)
669 /* USB request failed */
672 if (le16_to_cpu(status.wPortChange) &
673 USB_PORT_STAT_C_CONNECTION)
681 static int usb_child_post_bind(struct udevice *dev)
683 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
686 if (!dev_of_valid(dev))
689 /* We only support matching a few things */
690 val = dev_read_u32_default(dev, "usb,device-class", -1);
692 plat->id.match_flags |= USB_DEVICE_ID_MATCH_DEV_CLASS;
693 plat->id.bDeviceClass = val;
695 val = dev_read_u32_default(dev, "usb,interface-class", -1);
697 plat->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
698 plat->id.bInterfaceClass = val;
704 struct udevice *usb_get_bus(struct udevice *dev)
708 for (bus = dev; bus && device_get_uclass_id(bus) != UCLASS_USB; )
711 /* By design this cannot happen */
713 debug("USB HUB '%s' does not have a controller\n", dev->name);
719 int usb_child_pre_probe(struct udevice *dev)
721 struct usb_device *udev = dev_get_parent_priv(dev);
722 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
727 * Copy over all the values set in the on stack struct
728 * usb_device in usb_scan_device() to our final struct
729 * usb_device for this dev.
731 *udev = *(plat->udev);
732 /* And clear plat->udev as it will not be valid for long */
737 * This happens with devices which are explicitly bound
738 * instead of being discovered through usb_scan_device()
739 * such as sandbox emul devices.
742 udev->controller_dev = usb_get_bus(dev);
743 udev->devnum = plat->devnum;
746 * udev did not go through usb_scan_device(), so we need to
747 * select the config and read the config descriptors.
749 ret = usb_select_config(udev);
757 UCLASS_DRIVER(usb) = {
760 .flags = DM_UC_FLAG_SEQ_ALIAS,
761 .post_bind = dm_scan_fdt_dev,
762 .priv_auto_alloc_size = sizeof(struct usb_uclass_priv),
763 .per_child_auto_alloc_size = sizeof(struct usb_device),
764 .per_device_auto_alloc_size = sizeof(struct usb_bus_priv),
765 .child_post_bind = usb_child_post_bind,
766 .child_pre_probe = usb_child_pre_probe,
767 .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),
770 UCLASS_DRIVER(usb_dev_generic) = {
771 .id = UCLASS_USB_DEV_GENERIC,
772 .name = "usb_dev_generic",
775 U_BOOT_DRIVER(usb_dev_generic_drv) = {
776 .id = UCLASS_USB_DEV_GENERIC,
777 .name = "usb_dev_generic_drv",