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+
14 #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);
135 struct usb_uclass_priv *uc_priv;
138 /* De-activate any devices that have been activated */
139 ret = uclass_get(UCLASS_USB, &uc);
145 uclass_foreach_dev(bus, uc) {
146 ret = device_remove(bus);
149 ret = device_unbind_children(bus);
154 #ifdef CONFIG_SANDBOX
157 /* Reset all enulation devices */
158 ret = uclass_get(UCLASS_USB_EMUL, &uc);
162 uclass_foreach_dev(dev, uc)
167 uc_priv->companion_device_count = 0;
173 static void usb_scan_bus(struct udevice *bus, bool recurse)
175 struct usb_bus_priv *priv;
179 priv = dev_get_uclass_priv(bus);
181 assert(recurse); /* TODO: Support non-recusive */
183 printf("scanning bus %d for devices... ", bus->seq);
185 ret = usb_scan_device(bus, 0, USB_SPEED_FULL, &dev);
187 printf("failed, error %d\n", ret);
188 else if (priv->next_addr == 0)
189 printf("No USB Device found\n");
191 printf("%d USB Device(s) found\n", priv->next_addr);
196 int controllers_initialized = 0;
197 struct usb_uclass_priv *uc_priv;
198 struct usb_bus_priv *priv;
207 ret = uclass_get(UCLASS_USB, &uc);
213 uclass_foreach_dev(bus, uc) {
214 /* init low_level USB */
215 printf("USB%d: ", count);
217 ret = device_probe(bus);
218 if (ret == -ENODEV) { /* No such device. */
219 puts("Port not available.\n");
220 controllers_initialized++;
224 if (ret) { /* Other error. */
225 printf("probe failed, error %d\n", ret);
228 controllers_initialized++;
233 * lowlevel init done, now scan the bus for devices i.e. search HUBs
234 * and configure them, first scan primary controllers.
236 uclass_foreach_dev(bus, uc) {
237 if (!device_active(bus))
240 priv = dev_get_uclass_priv(bus);
241 if (!priv->companion)
242 usb_scan_bus(bus, true);
246 * Now that the primary controllers have been scanned and have handed
247 * over any devices they do not understand to their companions, scan
248 * the companions if necessary.
250 if (uc_priv->companion_device_count) {
251 uclass_foreach_dev(bus, uc) {
252 if (!device_active(bus))
255 priv = dev_get_uclass_priv(bus);
257 usb_scan_bus(bus, true);
262 /* if we were not able to find at least one working bus, bail out */
264 printf("No controllers found\n");
265 else if (controllers_initialized == 0)
266 printf("USB error: all controllers failed lowlevel init\n");
268 return usb_started ? 0 : -1;
271 int usb_reset_root_port(struct usb_device *udev)
276 static struct usb_device *find_child_devnum(struct udevice *parent, int devnum)
278 struct usb_device *udev;
281 if (!device_active(parent))
283 udev = dev_get_parentdata(parent);
284 if (udev->devnum == devnum)
287 for (device_find_first_child(parent, &dev);
289 device_find_next_child(&dev)) {
290 udev = find_child_devnum(dev, devnum);
298 struct usb_device *usb_get_dev_index(struct udevice *bus, int index)
301 int devnum = index + 1; /* Addresses are allocated from 1 on USB */
303 device_find_first_child(bus, &hub);
304 if (device_get_uclass_id(hub) == UCLASS_USB_HUB)
305 return find_child_devnum(hub, devnum);
310 int usb_post_bind(struct udevice *dev)
312 /* Scan the bus for devices */
313 return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
316 int usb_setup_ehci_gadget(struct ehci_ctrl **ctlrp)
318 struct usb_platdata *plat;
322 /* Find the old device and remove it */
323 ret = uclass_find_device_by_seq(UCLASS_USB, 0, true, &dev);
326 ret = device_remove(dev);
330 plat = dev_get_platdata(dev);
331 plat->init_type = USB_INIT_DEVICE;
332 ret = device_probe(dev);
335 *ctlrp = dev_get_priv(dev);
340 /* returns 0 if no match, 1 if match */
341 int usb_match_device(const struct usb_device_descriptor *desc,
342 const struct usb_device_id *id)
344 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
345 id->idVendor != le16_to_cpu(desc->idVendor))
348 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
349 id->idProduct != le16_to_cpu(desc->idProduct))
352 /* No need to test id->bcdDevice_lo != 0, since 0 is never
353 greater than any unsigned number. */
354 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
355 (id->bcdDevice_lo > le16_to_cpu(desc->bcdDevice)))
358 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
359 (id->bcdDevice_hi < le16_to_cpu(desc->bcdDevice)))
362 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
363 (id->bDeviceClass != desc->bDeviceClass))
366 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
367 (id->bDeviceSubClass != desc->bDeviceSubClass))
370 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
371 (id->bDeviceProtocol != desc->bDeviceProtocol))
377 /* returns 0 if no match, 1 if match */
378 int usb_match_one_id_intf(const struct usb_device_descriptor *desc,
379 const struct usb_interface_descriptor *int_desc,
380 const struct usb_device_id *id)
382 /* The interface class, subclass, protocol and number should never be
383 * checked for a match if the device class is Vendor Specific,
384 * unless the match record specifies the Vendor ID. */
385 if (desc->bDeviceClass == USB_CLASS_VENDOR_SPEC &&
386 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
387 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
388 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
389 USB_DEVICE_ID_MATCH_INT_PROTOCOL |
390 USB_DEVICE_ID_MATCH_INT_NUMBER)))
393 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
394 (id->bInterfaceClass != int_desc->bInterfaceClass))
397 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
398 (id->bInterfaceSubClass != int_desc->bInterfaceSubClass))
401 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
402 (id->bInterfaceProtocol != int_desc->bInterfaceProtocol))
405 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
406 (id->bInterfaceNumber != int_desc->bInterfaceNumber))
412 /* returns 0 if no match, 1 if match */
413 int usb_match_one_id(struct usb_device_descriptor *desc,
414 struct usb_interface_descriptor *int_desc,
415 const struct usb_device_id *id)
417 if (!usb_match_device(desc, id))
420 return usb_match_one_id_intf(desc, int_desc, id);
424 * usb_find_and_bind_driver() - Find and bind the right USB driver
426 * This only looks at certain fields in the descriptor.
428 static int usb_find_and_bind_driver(struct udevice *parent,
429 struct usb_device_descriptor *desc,
430 struct usb_interface_descriptor *iface,
431 int bus_seq, int devnum,
432 struct udevice **devp)
434 struct usb_driver_entry *start, *entry;
440 debug("%s: Searching for driver\n", __func__);
441 start = ll_entry_start(struct usb_driver_entry, usb_driver_entry);
442 n_ents = ll_entry_count(struct usb_driver_entry, usb_driver_entry);
443 for (entry = start; entry != start + n_ents; entry++) {
444 const struct usb_device_id *id;
446 const struct driver *drv;
447 struct usb_dev_platdata *plat;
449 for (id = entry->match; id->match_flags; id++) {
450 if (!usb_match_one_id(desc, iface, id))
455 * We could pass the descriptor to the driver as
456 * platdata (instead of NULL) and allow its bind()
457 * method to return -ENOENT if it doesn't support this
458 * device. That way we could continue the search to
459 * find another driver. For now this doesn't seem
460 * necesssary, so just bind the first match.
462 ret = device_bind(parent, drv, drv->name, NULL, -1,
466 debug("%s: Match found: %s\n", __func__, drv->name);
467 dev->driver_data = id->driver_info;
468 plat = dev_get_parent_platdata(dev);
475 /* Bind a generic driver so that the device can be used */
476 snprintf(name, sizeof(name), "generic_bus_%x_dev_%x", bus_seq, devnum);
480 ret = device_bind_driver(parent, "usb_dev_generic_drv", str, devp);
483 debug("%s: No match found: %d\n", __func__, ret);
488 * usb_find_child() - Find an existing device which matches our needs
492 static int usb_find_child(struct udevice *parent,
493 struct usb_device_descriptor *desc,
494 struct usb_interface_descriptor *iface,
495 struct udevice **devp)
500 for (device_find_first_child(parent, &dev);
502 device_find_next_child(&dev)) {
503 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
505 /* If this device is already in use, skip it */
506 if (device_active(dev))
508 debug(" %s: name='%s', plat=%d, desc=%d\n", __func__,
509 dev->name, plat->id.bDeviceClass, desc->bDeviceClass);
510 if (usb_match_one_id(desc, iface, &plat->id)) {
519 int usb_scan_device(struct udevice *parent, int port,
520 enum usb_device_speed speed, struct udevice **devp)
523 bool created = false;
524 struct usb_dev_platdata *plat;
525 struct usb_bus_priv *priv;
526 struct usb_device *parent_udev;
528 ALLOC_CACHE_ALIGN_BUFFER(struct usb_device, udev, 1);
529 struct usb_interface_descriptor *iface = &udev->config.if_desc[0].desc;
532 memset(udev, '\0', sizeof(*udev));
533 udev->controller_dev = usb_get_bus(parent);
534 priv = dev_get_uclass_priv(udev->controller_dev);
537 * Somewhat nasty, this. We create a local device and use the normal
538 * USB stack to read its descriptor. Then we know what type of device
539 * to create for real.
541 * udev->dev is set to the parent, since we don't have a real device
542 * yet. The USB stack should not access udev.dev anyway, except perhaps
543 * to find the controller, and the controller will either be @parent,
544 * or some parent of @parent.
546 * Another option might be to create the device as a generic USB
547 * device, then morph it into the correct one when we know what it
548 * should be. This means that a generic USB device would morph into
549 * a network controller, or a USB flash stick, for example. However,
550 * we don't support such morphing and it isn't clear that it would
553 * Yet another option is to split out the USB stack parts of udev
554 * into something like a 'struct urb' (as Linux does) which can exist
555 * independently of any device. This feels cleaner, but calls for quite
556 * a big change to the USB stack.
558 * For now, the approach is to set up an empty udev, read its
559 * descriptor and assign it an address, then bind a real device and
560 * stash the resulting information into the device's parent
561 * platform data. Then when we probe it, usb_child_pre_probe() is called
562 * and it will pull the information out of the stash.
566 udev->devnum = priv->next_addr + 1;
568 debug("Calling usb_setup_device(), portnr=%d\n", udev->portnr);
569 parent_udev = device_get_uclass_id(parent) == UCLASS_USB_HUB ?
570 dev_get_parentdata(parent) : NULL;
571 ret = usb_setup_device(udev, priv->desc_before_addr, parent_udev);
572 debug("read_descriptor for '%s': ret=%d\n", parent->name, ret);
575 ret = usb_find_child(parent, &udev->descriptor, iface, &dev);
576 debug("** usb_find_child returns %d\n", ret);
580 ret = usb_find_and_bind_driver(parent, &udev->descriptor, iface,
581 udev->controller_dev->seq,
587 plat = dev_get_parent_platdata(dev);
588 debug("%s: Probing '%s', plat=%p\n", __func__, dev->name, plat);
589 plat->devnum = udev->devnum;
592 ret = device_probe(dev);
594 debug("%s: Device '%s' probe failed\n", __func__, dev->name);
606 * Detect if a USB device has been plugged or unplugged.
608 int usb_detect_change(void)
615 ret = uclass_get(UCLASS_USB_HUB, &uc);
619 uclass_foreach_dev(hub, uc) {
620 struct usb_device *udev;
623 if (!device_active(hub))
625 for (device_find_first_child(hub, &dev);
627 device_find_next_child(&dev)) {
628 struct usb_port_status status;
630 if (!device_active(dev))
633 udev = dev_get_parentdata(dev);
634 if (usb_get_port_status(udev, udev->portnr, &status)
636 /* USB request failed */
639 if (le16_to_cpu(status.wPortChange) &
640 USB_PORT_STAT_C_CONNECTION)
648 int usb_child_post_bind(struct udevice *dev)
650 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
651 const void *blob = gd->fdt_blob;
654 if (dev->of_offset == -1)
657 /* We only support matching a few things */
658 val = fdtdec_get_int(blob, dev->of_offset, "usb,device-class", -1);
660 plat->id.match_flags |= USB_DEVICE_ID_MATCH_DEV_CLASS;
661 plat->id.bDeviceClass = val;
663 val = fdtdec_get_int(blob, dev->of_offset, "usb,interface-class", -1);
665 plat->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
666 plat->id.bInterfaceClass = val;
672 struct udevice *usb_get_bus(struct udevice *dev)
676 for (bus = dev; bus && device_get_uclass_id(bus) != UCLASS_USB; )
679 /* By design this cannot happen */
681 debug("USB HUB '%s' does not have a controller\n", dev->name);
687 int usb_child_pre_probe(struct udevice *dev)
689 struct usb_device *udev = dev_get_parentdata(dev);
690 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
695 * Copy over all the values set in the on stack struct
696 * usb_device in usb_scan_device() to our final struct
697 * usb_device for this dev.
699 *udev = *(plat->udev);
700 /* And clear plat->udev as it will not be valid for long */
705 * This happens with devices which are explicitly bound
706 * instead of being discovered through usb_scan_device()
707 * such as sandbox emul devices.
710 udev->controller_dev = usb_get_bus(dev);
711 udev->devnum = plat->devnum;
714 * udev did not go through usb_scan_device(), so we need to
715 * select the config and read the config descriptors.
717 ret = usb_select_config(udev);
725 UCLASS_DRIVER(usb) = {
728 .flags = DM_UC_FLAG_SEQ_ALIAS,
729 .post_bind = usb_post_bind,
730 .priv_auto_alloc_size = sizeof(struct usb_uclass_priv),
731 .per_child_auto_alloc_size = sizeof(struct usb_device),
732 .per_device_auto_alloc_size = sizeof(struct usb_bus_priv),
733 .child_post_bind = usb_child_post_bind,
734 .child_pre_probe = usb_child_pre_probe,
735 .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),
738 UCLASS_DRIVER(usb_dev_generic) = {
739 .id = UCLASS_USB_DEV_GENERIC,
740 .name = "usb_dev_generic",
743 U_BOOT_DRIVER(usb_dev_generic_drv) = {
744 .id = UCLASS_USB_DEV_GENERIC,
745 .name = "usb_dev_generic_drv",