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>
18 #include <dm/uclass-internal.h>
20 DECLARE_GLOBAL_DATA_PTR
;
22 extern bool usb_started
; /* flag for the started/stopped USB status */
23 static bool asynch_allowed
;
25 struct usb_uclass_priv
{
26 int companion_device_count
;
29 int usb_disable_asynch(int disable
)
31 int old_value
= asynch_allowed
;
33 asynch_allowed
= !disable
;
37 int submit_int_msg(struct usb_device
*udev
, unsigned long pipe
, void *buffer
,
38 int length
, int interval
)
40 struct udevice
*bus
= udev
->controller_dev
;
41 struct dm_usb_ops
*ops
= usb_get_ops(bus
);
46 return ops
->interrupt(bus
, udev
, pipe
, buffer
, length
, interval
);
49 int submit_control_msg(struct usb_device
*udev
, unsigned long pipe
,
50 void *buffer
, int length
, struct devrequest
*setup
)
52 struct udevice
*bus
= udev
->controller_dev
;
53 struct dm_usb_ops
*ops
= usb_get_ops(bus
);
54 struct usb_uclass_priv
*uc_priv
= bus
->uclass
->priv
;
60 err
= ops
->control(bus
, udev
, pipe
, buffer
, length
, setup
);
61 if (setup
->request
== USB_REQ_SET_FEATURE
&&
62 setup
->requesttype
== USB_RT_PORT
&&
63 setup
->value
== cpu_to_le16(USB_PORT_FEAT_RESET
) &&
65 /* Device handed over to companion after port reset */
66 uc_priv
->companion_device_count
++;
72 int submit_bulk_msg(struct usb_device
*udev
, unsigned long pipe
, void *buffer
,
75 struct udevice
*bus
= udev
->controller_dev
;
76 struct dm_usb_ops
*ops
= usb_get_ops(bus
);
81 return ops
->bulk(bus
, udev
, pipe
, buffer
, length
);
84 struct int_queue
*create_int_queue(struct usb_device
*udev
,
85 unsigned long pipe
, int queuesize
, int elementsize
,
86 void *buffer
, int interval
)
88 struct udevice
*bus
= udev
->controller_dev
;
89 struct dm_usb_ops
*ops
= usb_get_ops(bus
);
91 if (!ops
->create_int_queue
)
94 return ops
->create_int_queue(bus
, udev
, pipe
, queuesize
, elementsize
,
98 void *poll_int_queue(struct usb_device
*udev
, struct int_queue
*queue
)
100 struct udevice
*bus
= udev
->controller_dev
;
101 struct dm_usb_ops
*ops
= usb_get_ops(bus
);
103 if (!ops
->poll_int_queue
)
106 return ops
->poll_int_queue(bus
, udev
, queue
);
109 int destroy_int_queue(struct usb_device
*udev
, struct int_queue
*queue
)
111 struct udevice
*bus
= udev
->controller_dev
;
112 struct dm_usb_ops
*ops
= usb_get_ops(bus
);
114 if (!ops
->destroy_int_queue
)
117 return ops
->destroy_int_queue(bus
, udev
, queue
);
120 int usb_alloc_device(struct usb_device
*udev
)
122 struct udevice
*bus
= udev
->controller_dev
;
123 struct dm_usb_ops
*ops
= usb_get_ops(bus
);
125 /* This is only requird by some controllers - current XHCI */
126 if (!ops
->alloc_device
)
129 return ops
->alloc_device(bus
, udev
);
132 int usb_reset_root_port(struct usb_device
*udev
)
134 struct udevice
*bus
= udev
->controller_dev
;
135 struct dm_usb_ops
*ops
= usb_get_ops(bus
);
137 if (!ops
->reset_root_port
)
140 return ops
->reset_root_port(bus
, udev
);
147 struct usb_uclass_priv
*uc_priv
;
150 /* De-activate any devices that have been activated */
151 ret
= uclass_get(UCLASS_USB
, &uc
);
157 uclass_foreach_dev(bus
, uc
) {
158 ret
= device_remove(bus
);
163 #ifdef CONFIG_SANDBOX
166 /* Reset all enulation devices */
167 ret
= uclass_get(UCLASS_USB_EMUL
, &uc
);
171 uclass_foreach_dev(dev
, uc
)
174 #ifdef CONFIG_USB_STORAGE
178 uc_priv
->companion_device_count
= 0;
184 static void usb_scan_bus(struct udevice
*bus
, bool recurse
)
186 struct usb_bus_priv
*priv
;
190 priv
= dev_get_uclass_priv(bus
);
192 assert(recurse
); /* TODO: Support non-recusive */
194 printf("scanning bus %d for devices... ", bus
->seq
);
196 ret
= usb_scan_device(bus
, 0, USB_SPEED_FULL
, &dev
);
198 printf("failed, error %d\n", ret
);
199 else if (priv
->next_addr
== 0)
200 printf("No USB Device found\n");
202 printf("%d USB Device(s) found\n", priv
->next_addr
);
205 static void remove_inactive_children(struct uclass
*uc
, struct udevice
*bus
)
207 uclass_foreach_dev(bus
, uc
) {
208 struct udevice
*dev
, *next
;
210 if (!device_active(bus
))
212 device_foreach_child_safe(dev
, next
, bus
) {
213 if (!device_active(dev
))
221 int controllers_initialized
= 0;
222 struct usb_uclass_priv
*uc_priv
;
223 struct usb_bus_priv
*priv
;
232 ret
= uclass_get(UCLASS_USB
, &uc
);
238 uclass_foreach_dev(bus
, uc
) {
239 /* init low_level USB */
240 printf("USB%d: ", count
);
242 ret
= device_probe(bus
);
243 if (ret
== -ENODEV
) { /* No such device. */
244 puts("Port not available.\n");
245 controllers_initialized
++;
249 if (ret
) { /* Other error. */
250 printf("probe failed, error %d\n", ret
);
253 controllers_initialized
++;
258 * lowlevel init done, now scan the bus for devices i.e. search HUBs
259 * and configure them, first scan primary controllers.
261 uclass_foreach_dev(bus
, uc
) {
262 if (!device_active(bus
))
265 priv
= dev_get_uclass_priv(bus
);
266 if (!priv
->companion
)
267 usb_scan_bus(bus
, true);
271 * Now that the primary controllers have been scanned and have handed
272 * over any devices they do not understand to their companions, scan
273 * the companions if necessary.
275 if (uc_priv
->companion_device_count
) {
276 uclass_foreach_dev(bus
, uc
) {
277 if (!device_active(bus
))
280 priv
= dev_get_uclass_priv(bus
);
282 usb_scan_bus(bus
, true);
288 /* Remove any devices that were not found on this scan */
289 remove_inactive_children(uc
, bus
);
291 ret
= uclass_get(UCLASS_USB_HUB
, &uc
);
294 remove_inactive_children(uc
, bus
);
296 /* if we were not able to find at least one working bus, bail out */
298 printf("No controllers found\n");
299 else if (controllers_initialized
== 0)
300 printf("USB error: all controllers failed lowlevel init\n");
302 return usb_started
? 0 : -1;
306 * TODO(sjg@chromium.org): Remove this legacy function. At present it is needed
307 * to support boards which use driver model for USB but not Ethernet, and want
308 * to use USB Ethernet.
310 * The #if clause is here to ensure that remains the only case.
312 #if !defined(CONFIG_DM_ETH) && defined(CONFIG_USB_HOST_ETHER)
313 static struct usb_device
*find_child_devnum(struct udevice
*parent
, int devnum
)
315 struct usb_device
*udev
;
318 if (!device_active(parent
))
320 udev
= dev_get_parent_priv(parent
);
321 if (udev
->devnum
== devnum
)
324 for (device_find_first_child(parent
, &dev
);
326 device_find_next_child(&dev
)) {
327 udev
= find_child_devnum(dev
, devnum
);
335 struct usb_device
*usb_get_dev_index(struct udevice
*bus
, int index
)
338 int devnum
= index
+ 1; /* Addresses are allocated from 1 on USB */
340 device_find_first_child(bus
, &dev
);
344 return find_child_devnum(dev
, devnum
);
348 int usb_post_bind(struct udevice
*dev
)
350 /* Scan the bus for devices */
351 return dm_scan_fdt_node(dev
, gd
->fdt_blob
, dev
->of_offset
, false);
354 int usb_setup_ehci_gadget(struct ehci_ctrl
**ctlrp
)
356 struct usb_platdata
*plat
;
360 /* Find the old device and remove it */
361 ret
= uclass_find_device_by_seq(UCLASS_USB
, 0, true, &dev
);
364 ret
= device_remove(dev
);
368 plat
= dev_get_platdata(dev
);
369 plat
->init_type
= USB_INIT_DEVICE
;
370 ret
= device_probe(dev
);
373 *ctlrp
= dev_get_priv(dev
);
378 /* returns 0 if no match, 1 if match */
379 int usb_match_device(const struct usb_device_descriptor
*desc
,
380 const struct usb_device_id
*id
)
382 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_VENDOR
) &&
383 id
->idVendor
!= le16_to_cpu(desc
->idVendor
))
386 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_PRODUCT
) &&
387 id
->idProduct
!= le16_to_cpu(desc
->idProduct
))
390 /* No need to test id->bcdDevice_lo != 0, since 0 is never
391 greater than any unsigned number. */
392 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_LO
) &&
393 (id
->bcdDevice_lo
> le16_to_cpu(desc
->bcdDevice
)))
396 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_HI
) &&
397 (id
->bcdDevice_hi
< le16_to_cpu(desc
->bcdDevice
)))
400 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_CLASS
) &&
401 (id
->bDeviceClass
!= desc
->bDeviceClass
))
404 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_SUBCLASS
) &&
405 (id
->bDeviceSubClass
!= desc
->bDeviceSubClass
))
408 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_PROTOCOL
) &&
409 (id
->bDeviceProtocol
!= desc
->bDeviceProtocol
))
415 /* returns 0 if no match, 1 if match */
416 int usb_match_one_id_intf(const struct usb_device_descriptor
*desc
,
417 const struct usb_interface_descriptor
*int_desc
,
418 const struct usb_device_id
*id
)
420 /* The interface class, subclass, protocol and number should never be
421 * checked for a match if the device class is Vendor Specific,
422 * unless the match record specifies the Vendor ID. */
423 if (desc
->bDeviceClass
== USB_CLASS_VENDOR_SPEC
&&
424 !(id
->match_flags
& USB_DEVICE_ID_MATCH_VENDOR
) &&
425 (id
->match_flags
& (USB_DEVICE_ID_MATCH_INT_CLASS
|
426 USB_DEVICE_ID_MATCH_INT_SUBCLASS
|
427 USB_DEVICE_ID_MATCH_INT_PROTOCOL
|
428 USB_DEVICE_ID_MATCH_INT_NUMBER
)))
431 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_CLASS
) &&
432 (id
->bInterfaceClass
!= int_desc
->bInterfaceClass
))
435 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_SUBCLASS
) &&
436 (id
->bInterfaceSubClass
!= int_desc
->bInterfaceSubClass
))
439 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_PROTOCOL
) &&
440 (id
->bInterfaceProtocol
!= int_desc
->bInterfaceProtocol
))
443 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_NUMBER
) &&
444 (id
->bInterfaceNumber
!= int_desc
->bInterfaceNumber
))
450 /* returns 0 if no match, 1 if match */
451 int usb_match_one_id(struct usb_device_descriptor
*desc
,
452 struct usb_interface_descriptor
*int_desc
,
453 const struct usb_device_id
*id
)
455 if (!usb_match_device(desc
, id
))
458 return usb_match_one_id_intf(desc
, int_desc
, id
);
462 * usb_find_and_bind_driver() - Find and bind the right USB driver
464 * This only looks at certain fields in the descriptor.
466 static int usb_find_and_bind_driver(struct udevice
*parent
,
467 struct usb_device_descriptor
*desc
,
468 struct usb_interface_descriptor
*iface
,
469 int bus_seq
, int devnum
,
470 struct udevice
**devp
)
472 struct usb_driver_entry
*start
, *entry
;
478 debug("%s: Searching for driver\n", __func__
);
479 start
= ll_entry_start(struct usb_driver_entry
, usb_driver_entry
);
480 n_ents
= ll_entry_count(struct usb_driver_entry
, usb_driver_entry
);
481 for (entry
= start
; entry
!= start
+ n_ents
; entry
++) {
482 const struct usb_device_id
*id
;
484 const struct driver
*drv
;
485 struct usb_dev_platdata
*plat
;
487 for (id
= entry
->match
; id
->match_flags
; id
++) {
488 if (!usb_match_one_id(desc
, iface
, id
))
493 * We could pass the descriptor to the driver as
494 * platdata (instead of NULL) and allow its bind()
495 * method to return -ENOENT if it doesn't support this
496 * device. That way we could continue the search to
497 * find another driver. For now this doesn't seem
498 * necesssary, so just bind the first match.
500 ret
= device_bind(parent
, drv
, drv
->name
, NULL
, -1,
504 debug("%s: Match found: %s\n", __func__
, drv
->name
);
505 dev
->driver_data
= id
->driver_info
;
506 plat
= dev_get_parent_platdata(dev
);
513 /* Bind a generic driver so that the device can be used */
514 snprintf(name
, sizeof(name
), "generic_bus_%x_dev_%x", bus_seq
, devnum
);
518 ret
= device_bind_driver(parent
, "usb_dev_generic_drv", str
, devp
);
521 debug("%s: No match found: %d\n", __func__
, ret
);
526 * usb_find_child() - Find an existing device which matches our needs
530 static int usb_find_child(struct udevice
*parent
,
531 struct usb_device_descriptor
*desc
,
532 struct usb_interface_descriptor
*iface
,
533 struct udevice
**devp
)
538 for (device_find_first_child(parent
, &dev
);
540 device_find_next_child(&dev
)) {
541 struct usb_dev_platdata
*plat
= dev_get_parent_platdata(dev
);
543 /* If this device is already in use, skip it */
544 if (device_active(dev
))
546 debug(" %s: name='%s', plat=%d, desc=%d\n", __func__
,
547 dev
->name
, plat
->id
.bDeviceClass
, desc
->bDeviceClass
);
548 if (usb_match_one_id(desc
, iface
, &plat
->id
)) {
557 int usb_scan_device(struct udevice
*parent
, int port
,
558 enum usb_device_speed speed
, struct udevice
**devp
)
561 bool created
= false;
562 struct usb_dev_platdata
*plat
;
563 struct usb_bus_priv
*priv
;
564 struct usb_device
*parent_udev
;
566 ALLOC_CACHE_ALIGN_BUFFER(struct usb_device
, udev
, 1);
567 struct usb_interface_descriptor
*iface
= &udev
->config
.if_desc
[0].desc
;
570 memset(udev
, '\0', sizeof(*udev
));
571 udev
->controller_dev
= usb_get_bus(parent
);
572 priv
= dev_get_uclass_priv(udev
->controller_dev
);
575 * Somewhat nasty, this. We create a local device and use the normal
576 * USB stack to read its descriptor. Then we know what type of device
577 * to create for real.
579 * udev->dev is set to the parent, since we don't have a real device
580 * yet. The USB stack should not access udev.dev anyway, except perhaps
581 * to find the controller, and the controller will either be @parent,
582 * or some parent of @parent.
584 * Another option might be to create the device as a generic USB
585 * device, then morph it into the correct one when we know what it
586 * should be. This means that a generic USB device would morph into
587 * a network controller, or a USB flash stick, for example. However,
588 * we don't support such morphing and it isn't clear that it would
591 * Yet another option is to split out the USB stack parts of udev
592 * into something like a 'struct urb' (as Linux does) which can exist
593 * independently of any device. This feels cleaner, but calls for quite
594 * a big change to the USB stack.
596 * For now, the approach is to set up an empty udev, read its
597 * descriptor and assign it an address, then bind a real device and
598 * stash the resulting information into the device's parent
599 * platform data. Then when we probe it, usb_child_pre_probe() is called
600 * and it will pull the information out of the stash.
604 udev
->devnum
= priv
->next_addr
+ 1;
606 debug("Calling usb_setup_device(), portnr=%d\n", udev
->portnr
);
607 parent_udev
= device_get_uclass_id(parent
) == UCLASS_USB_HUB
?
608 dev_get_parent_priv(parent
) : NULL
;
609 ret
= usb_setup_device(udev
, priv
->desc_before_addr
, parent_udev
);
610 debug("read_descriptor for '%s': ret=%d\n", parent
->name
, ret
);
613 ret
= usb_find_child(parent
, &udev
->descriptor
, iface
, &dev
);
614 debug("** usb_find_child returns %d\n", ret
);
618 ret
= usb_find_and_bind_driver(parent
, &udev
->descriptor
, iface
,
619 udev
->controller_dev
->seq
,
625 plat
= dev_get_parent_platdata(dev
);
626 debug("%s: Probing '%s', plat=%p\n", __func__
, dev
->name
, plat
);
627 plat
->devnum
= udev
->devnum
;
630 ret
= device_probe(dev
);
632 debug("%s: Device '%s' probe failed\n", __func__
, dev
->name
);
644 * Detect if a USB device has been plugged or unplugged.
646 int usb_detect_change(void)
653 ret
= uclass_get(UCLASS_USB_HUB
, &uc
);
657 uclass_foreach_dev(hub
, uc
) {
658 struct usb_device
*udev
;
661 if (!device_active(hub
))
663 for (device_find_first_child(hub
, &dev
);
665 device_find_next_child(&dev
)) {
666 struct usb_port_status status
;
668 if (!device_active(dev
))
671 udev
= dev_get_parent_priv(dev
);
672 if (usb_get_port_status(udev
, udev
->portnr
, &status
)
674 /* USB request failed */
677 if (le16_to_cpu(status
.wPortChange
) &
678 USB_PORT_STAT_C_CONNECTION
)
686 int usb_child_post_bind(struct udevice
*dev
)
688 struct usb_dev_platdata
*plat
= dev_get_parent_platdata(dev
);
689 const void *blob
= gd
->fdt_blob
;
692 if (dev
->of_offset
== -1)
695 /* We only support matching a few things */
696 val
= fdtdec_get_int(blob
, dev
->of_offset
, "usb,device-class", -1);
698 plat
->id
.match_flags
|= USB_DEVICE_ID_MATCH_DEV_CLASS
;
699 plat
->id
.bDeviceClass
= val
;
701 val
= fdtdec_get_int(blob
, dev
->of_offset
, "usb,interface-class", -1);
703 plat
->id
.match_flags
|= USB_DEVICE_ID_MATCH_INT_CLASS
;
704 plat
->id
.bInterfaceClass
= val
;
710 struct udevice
*usb_get_bus(struct udevice
*dev
)
714 for (bus
= dev
; bus
&& device_get_uclass_id(bus
) != UCLASS_USB
; )
717 /* By design this cannot happen */
719 debug("USB HUB '%s' does not have a controller\n", dev
->name
);
725 int usb_child_pre_probe(struct udevice
*dev
)
727 struct usb_device
*udev
= dev_get_parent_priv(dev
);
728 struct usb_dev_platdata
*plat
= dev_get_parent_platdata(dev
);
733 * Copy over all the values set in the on stack struct
734 * usb_device in usb_scan_device() to our final struct
735 * usb_device for this dev.
737 *udev
= *(plat
->udev
);
738 /* And clear plat->udev as it will not be valid for long */
743 * This happens with devices which are explicitly bound
744 * instead of being discovered through usb_scan_device()
745 * such as sandbox emul devices.
748 udev
->controller_dev
= usb_get_bus(dev
);
749 udev
->devnum
= plat
->devnum
;
752 * udev did not go through usb_scan_device(), so we need to
753 * select the config and read the config descriptors.
755 ret
= usb_select_config(udev
);
763 UCLASS_DRIVER(usb
) = {
766 .flags
= DM_UC_FLAG_SEQ_ALIAS
,
767 .post_bind
= usb_post_bind
,
768 .priv_auto_alloc_size
= sizeof(struct usb_uclass_priv
),
769 .per_child_auto_alloc_size
= sizeof(struct usb_device
),
770 .per_device_auto_alloc_size
= sizeof(struct usb_bus_priv
),
771 .child_post_bind
= usb_child_post_bind
,
772 .child_pre_probe
= usb_child_pre_probe
,
773 .per_child_platdata_auto_alloc_size
= sizeof(struct usb_dev_platdata
),
776 UCLASS_DRIVER(usb_dev_generic
) = {
777 .id
= UCLASS_USB_DEV_GENERIC
,
778 .name
= "usb_dev_generic",
781 U_BOOT_DRIVER(usb_dev_generic_drv
) = {
782 .id
= UCLASS_USB_DEV_GENERIC
,
783 .name
= "usb_dev_generic_drv",