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 int usb_disable_asynch(int disable)
26 int old_value = asynch_allowed;
28 asynch_allowed = !disable;
32 int submit_int_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
33 int length, int interval)
35 struct udevice *bus = udev->controller_dev;
36 struct dm_usb_ops *ops = usb_get_ops(bus);
41 return ops->interrupt(bus, udev, pipe, buffer, length, interval);
44 int submit_control_msg(struct usb_device *udev, unsigned long pipe,
45 void *buffer, int length, struct devrequest *setup)
47 struct udevice *bus = udev->controller_dev;
48 struct dm_usb_ops *ops = usb_get_ops(bus);
53 return ops->control(bus, udev, pipe, buffer, length, setup);
56 int submit_bulk_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
59 struct udevice *bus = udev->controller_dev;
60 struct dm_usb_ops *ops = usb_get_ops(bus);
65 return ops->bulk(bus, udev, pipe, buffer, length);
68 int usb_alloc_device(struct usb_device *udev)
70 struct udevice *bus = udev->controller_dev;
71 struct dm_usb_ops *ops = usb_get_ops(bus);
73 /* This is only requird by some controllers - current XHCI */
74 if (!ops->alloc_device)
77 return ops->alloc_device(bus, udev);
86 /* De-activate any devices that have been activated */
87 ret = uclass_get(UCLASS_USB, &uc);
90 uclass_foreach_dev(bus, uc) {
91 ret = device_remove(bus);
99 /* Reset all enulation devices */
100 ret = uclass_get(UCLASS_USB_EMUL, &uc);
104 uclass_foreach_dev(dev, uc)
114 static int usb_scan_bus(struct udevice *bus, bool recurse)
116 struct usb_bus_priv *priv;
120 priv = dev_get_uclass_priv(bus);
122 assert(recurse); /* TODO: Support non-recusive */
124 ret = usb_scan_device(bus, 0, USB_SPEED_FULL, &dev);
128 return priv->next_addr;
133 int controllers_initialized = 0;
142 ret = uclass_get(UCLASS_USB, &uc);
146 uclass_foreach_dev(bus, uc) {
147 /* init low_level USB */
148 printf("USB%d: ", count);
150 ret = device_probe(bus);
151 if (ret == -ENODEV) { /* No such device. */
152 puts("Port not available.\n");
153 controllers_initialized++;
157 if (ret) { /* Other error. */
158 printf("probe failed, error %d\n", ret);
162 * lowlevel init is OK, now scan the bus for devices
163 * i.e. search HUBs and configure them
165 controllers_initialized++;
166 printf("scanning bus %d for devices... ", bus->seq);
168 ret = usb_scan_bus(bus, true);
170 printf("failed, error %d\n", ret);
172 printf("No USB Device found\n");
174 printf("%d USB Device(s) found\n", ret);
179 /* if we were not able to find at least one working bus, bail out */
181 printf("No controllers found\n");
182 else if (controllers_initialized == 0)
183 printf("USB error: all controllers failed lowlevel init\n");
185 return usb_started ? 0 : -1;
188 int usb_reset_root_port(void)
193 static struct usb_device *find_child_devnum(struct udevice *parent, int devnum)
195 struct usb_device *udev;
198 if (!device_active(parent))
200 udev = dev_get_parentdata(parent);
201 if (udev->devnum == devnum)
204 for (device_find_first_child(parent, &dev);
206 device_find_next_child(&dev)) {
207 udev = find_child_devnum(dev, devnum);
215 struct usb_device *usb_get_dev_index(struct udevice *bus, int index)
218 int devnum = index + 1; /* Addresses are allocated from 1 on USB */
220 device_find_first_child(bus, &hub);
221 if (device_get_uclass_id(hub) == UCLASS_USB_HUB)
222 return find_child_devnum(hub, devnum);
227 int usb_post_bind(struct udevice *dev)
229 /* Scan the bus for devices */
230 return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
233 int usb_port_reset(struct usb_device *parent, int portnr)
235 unsigned short portstatus;
238 debug("%s: start\n", __func__);
241 /* reset the port for the second time */
243 debug("%s: reset %d\n", __func__, portnr - 1);
244 ret = legacy_hub_port_reset(parent, portnr - 1, &portstatus);
246 printf("\n Couldn't reset port %i\n", portnr);
250 debug("%s: reset root\n", __func__);
251 usb_reset_root_port();
257 int usb_legacy_port_reset(struct usb_device *parent, int portnr)
259 return usb_port_reset(parent, portnr);
262 int usb_setup_ehci_gadget(struct ehci_ctrl **ctlrp)
264 struct usb_platdata *plat;
268 /* Find the old device and remove it */
269 ret = uclass_find_device_by_seq(UCLASS_USB, 0, true, &dev);
272 ret = device_remove(dev);
276 plat = dev_get_platdata(dev);
277 plat->init_type = USB_INIT_DEVICE;
278 ret = device_probe(dev);
281 *ctlrp = dev_get_priv(dev);
286 /* returns 0 if no match, 1 if match */
287 int usb_match_device(const struct usb_device_descriptor *desc,
288 const struct usb_device_id *id)
290 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
291 id->idVendor != le16_to_cpu(desc->idVendor))
294 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
295 id->idProduct != le16_to_cpu(desc->idProduct))
298 /* No need to test id->bcdDevice_lo != 0, since 0 is never
299 greater than any unsigned number. */
300 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
301 (id->bcdDevice_lo > le16_to_cpu(desc->bcdDevice)))
304 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
305 (id->bcdDevice_hi < le16_to_cpu(desc->bcdDevice)))
308 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
309 (id->bDeviceClass != desc->bDeviceClass))
312 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
313 (id->bDeviceSubClass != desc->bDeviceSubClass))
316 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
317 (id->bDeviceProtocol != desc->bDeviceProtocol))
323 /* returns 0 if no match, 1 if match */
324 int usb_match_one_id_intf(const struct usb_device_descriptor *desc,
325 const struct usb_interface_descriptor *int_desc,
326 const struct usb_device_id *id)
328 /* The interface class, subclass, protocol and number should never be
329 * checked for a match if the device class is Vendor Specific,
330 * unless the match record specifies the Vendor ID. */
331 if (desc->bDeviceClass == USB_CLASS_VENDOR_SPEC &&
332 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
333 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
334 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
335 USB_DEVICE_ID_MATCH_INT_PROTOCOL |
336 USB_DEVICE_ID_MATCH_INT_NUMBER)))
339 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
340 (id->bInterfaceClass != int_desc->bInterfaceClass))
343 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
344 (id->bInterfaceSubClass != int_desc->bInterfaceSubClass))
347 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
348 (id->bInterfaceProtocol != int_desc->bInterfaceProtocol))
351 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
352 (id->bInterfaceNumber != int_desc->bInterfaceNumber))
358 /* returns 0 if no match, 1 if match */
359 int usb_match_one_id(struct usb_device_descriptor *desc,
360 struct usb_interface_descriptor *int_desc,
361 const struct usb_device_id *id)
363 if (!usb_match_device(desc, id))
366 return usb_match_one_id_intf(desc, int_desc, id);
370 * usb_find_and_bind_driver() - Find and bind the right USB driver
372 * This only looks at certain fields in the descriptor.
374 static int usb_find_and_bind_driver(struct udevice *parent,
375 struct usb_device_descriptor *desc,
376 struct usb_interface_descriptor *iface,
377 int bus_seq, int devnum,
378 struct udevice **devp)
380 struct usb_driver_entry *start, *entry;
386 debug("%s: Searching for driver\n", __func__);
387 start = ll_entry_start(struct usb_driver_entry, usb_driver_entry);
388 n_ents = ll_entry_count(struct usb_driver_entry, usb_driver_entry);
389 for (entry = start; entry != start + n_ents; entry++) {
390 const struct usb_device_id *id;
392 const struct driver *drv;
393 struct usb_dev_platdata *plat;
395 for (id = entry->match; id->match_flags; id++) {
396 if (!usb_match_one_id(desc, iface, id))
401 * We could pass the descriptor to the driver as
402 * platdata (instead of NULL) and allow its bind()
403 * method to return -ENOENT if it doesn't support this
404 * device. That way we could continue the search to
405 * find another driver. For now this doesn't seem
406 * necesssary, so just bind the first match.
408 ret = device_bind(parent, drv, drv->name, NULL, -1,
412 debug("%s: Match found: %s\n", __func__, drv->name);
413 dev->driver_data = id->driver_info;
414 plat = dev_get_parent_platdata(dev);
421 /* Bind a generic driver so that the device can be used */
422 snprintf(name, sizeof(name), "generic_bus_%x_dev_%x", bus_seq, devnum);
426 ret = device_bind_driver(parent, "usb_dev_generic_drv", str, devp);
429 debug("%s: No match found: %d\n", __func__, ret);
434 * usb_find_child() - Find an existing device which matches our needs
438 static int usb_find_child(struct udevice *parent,
439 struct usb_device_descriptor *desc,
440 struct usb_interface_descriptor *iface,
441 struct udevice **devp)
446 for (device_find_first_child(parent, &dev);
448 device_find_next_child(&dev)) {
449 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
451 /* If this device is already in use, skip it */
452 if (device_active(dev))
454 debug(" %s: name='%s', plat=%d, desc=%d\n", __func__,
455 dev->name, plat->id.bDeviceClass, desc->bDeviceClass);
456 if (usb_match_one_id(desc, iface, &plat->id)) {
465 int usb_scan_device(struct udevice *parent, int port,
466 enum usb_device_speed speed, struct udevice **devp)
469 bool created = false;
470 struct usb_dev_platdata *plat;
471 struct usb_bus_priv *priv;
472 struct usb_device *parent_udev;
474 ALLOC_CACHE_ALIGN_BUFFER(struct usb_device, udev, 1);
475 struct usb_interface_descriptor *iface = &udev->config.if_desc[0].desc;
478 memset(udev, '\0', sizeof(*udev));
479 udev->controller_dev = usb_get_bus(parent);
480 priv = dev_get_uclass_priv(udev->controller_dev);
483 * Somewhat nasty, this. We create a local device and use the normal
484 * USB stack to read its descriptor. Then we know what type of device
485 * to create for real.
487 * udev->dev is set to the parent, since we don't have a real device
488 * yet. The USB stack should not access udev.dev anyway, except perhaps
489 * to find the controller, and the controller will either be @parent,
490 * or some parent of @parent.
492 * Another option might be to create the device as a generic USB
493 * device, then morph it into the correct one when we know what it
494 * should be. This means that a generic USB device would morph into
495 * a network controller, or a USB flash stick, for example. However,
496 * we don't support such morphing and it isn't clear that it would
499 * Yet another option is to split out the USB stack parts of udev
500 * into something like a 'struct urb' (as Linux does) which can exist
501 * independently of any device. This feels cleaner, but calls for quite
502 * a big change to the USB stack.
504 * For now, the approach is to set up an empty udev, read its
505 * descriptor and assign it an address, then bind a real device and
506 * stash the resulting information into the device's parent
507 * platform data. Then when we probe it, usb_child_pre_probe() is called
508 * and it will pull the information out of the stash.
512 udev->devnum = priv->next_addr + 1;
514 debug("Calling usb_setup_device(), portnr=%d\n", udev->portnr);
515 parent_udev = device_get_uclass_id(parent) == UCLASS_USB_HUB ?
516 dev_get_parentdata(parent) : NULL;
517 ret = usb_setup_device(udev, priv->desc_before_addr, parent_udev, port);
518 debug("read_descriptor for '%s': ret=%d\n", parent->name, ret);
521 ret = usb_find_child(parent, &udev->descriptor, iface, &dev);
522 debug("** usb_find_child returns %d\n", ret);
526 ret = usb_find_and_bind_driver(parent, &udev->descriptor, iface,
527 udev->controller_dev->seq,
533 plat = dev_get_parent_platdata(dev);
534 debug("%s: Probing '%s', plat=%p\n", __func__, dev->name, plat);
535 plat->devnum = udev->devnum;
538 ret = device_probe(dev);
540 debug("%s: Device '%s' probe failed\n", __func__, dev->name);
551 int usb_child_post_bind(struct udevice *dev)
553 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
554 const void *blob = gd->fdt_blob;
557 if (dev->of_offset == -1)
560 /* We only support matching a few things */
561 val = fdtdec_get_int(blob, dev->of_offset, "usb,device-class", -1);
563 plat->id.match_flags |= USB_DEVICE_ID_MATCH_DEV_CLASS;
564 plat->id.bDeviceClass = val;
566 val = fdtdec_get_int(blob, dev->of_offset, "usb,interface-class", -1);
568 plat->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
569 plat->id.bInterfaceClass = val;
575 struct udevice *usb_get_bus(struct udevice *dev)
579 for (bus = dev; bus && device_get_uclass_id(bus) != UCLASS_USB; )
582 /* By design this cannot happen */
584 debug("USB HUB '%s' does not have a controller\n", dev->name);
590 int usb_child_pre_probe(struct udevice *dev)
592 struct usb_device *udev = dev_get_parentdata(dev);
593 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
598 * Copy over all the values set in the on stack struct
599 * usb_device in usb_scan_device() to our final struct
600 * usb_device for this dev.
602 *udev = *(plat->udev);
603 /* And clear plat->udev as it will not be valid for long */
608 * This happens with devices which are explicitly bound
609 * instead of being discovered through usb_scan_device()
610 * such as sandbox emul devices.
613 udev->controller_dev = usb_get_bus(dev);
614 udev->devnum = plat->devnum;
617 * udev did not go through usb_scan_device(), so we need to
618 * select the config and read the config descriptors.
620 ret = usb_select_config(udev);
628 UCLASS_DRIVER(usb) = {
631 .flags = DM_UC_FLAG_SEQ_ALIAS,
632 .post_bind = usb_post_bind,
633 .per_child_auto_alloc_size = sizeof(struct usb_device),
634 .per_device_auto_alloc_size = sizeof(struct usb_bus_priv),
635 .child_post_bind = usb_child_post_bind,
636 .child_pre_probe = usb_child_pre_probe,
637 .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),
640 UCLASS_DRIVER(usb_dev_generic) = {
641 .id = UCLASS_USB_DEV_GENERIC,
642 .name = "usb_dev_generic",
645 U_BOOT_DRIVER(usb_dev_generic_drv) = {
646 .id = UCLASS_USB_DEV_GENERIC,
647 .name = "usb_dev_generic_drv",