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
);
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
181 uc_priv
->companion_device_count
= 0;
187 static void usb_scan_bus(struct udevice
*bus
, bool recurse
)
189 struct usb_bus_priv
*priv
;
193 priv
= dev_get_uclass_priv(bus
);
195 assert(recurse
); /* TODO: Support non-recusive */
197 printf("scanning bus %d for devices... ", bus
->seq
);
199 ret
= usb_scan_device(bus
, 0, USB_SPEED_FULL
, &dev
);
201 printf("failed, error %d\n", ret
);
202 else if (priv
->next_addr
== 0)
203 printf("No USB Device found\n");
205 printf("%d USB Device(s) found\n", priv
->next_addr
);
208 static void remove_inactive_children(struct uclass
*uc
, struct udevice
*bus
)
210 uclass_foreach_dev(bus
, uc
) {
211 struct udevice
*dev
, *next
;
213 if (!device_active(bus
))
215 device_foreach_child_safe(dev
, next
, bus
) {
216 if (!device_active(dev
))
224 int controllers_initialized
= 0;
225 struct usb_uclass_priv
*uc_priv
;
226 struct usb_bus_priv
*priv
;
235 ret
= uclass_get(UCLASS_USB
, &uc
);
241 uclass_foreach_dev(bus
, uc
) {
242 /* init low_level USB */
243 printf("USB%d: ", count
);
245 ret
= device_probe(bus
);
246 if (ret
== -ENODEV
) { /* No such device. */
247 puts("Port not available.\n");
248 controllers_initialized
++;
252 if (ret
) { /* Other error. */
253 printf("probe failed, error %d\n", ret
);
256 controllers_initialized
++;
261 * lowlevel init done, now scan the bus for devices i.e. search HUBs
262 * and configure them, first scan primary controllers.
264 uclass_foreach_dev(bus
, uc
) {
265 if (!device_active(bus
))
268 priv
= dev_get_uclass_priv(bus
);
269 if (!priv
->companion
)
270 usb_scan_bus(bus
, true);
274 * Now that the primary controllers have been scanned and have handed
275 * over any devices they do not understand to their companions, scan
276 * the companions if necessary.
278 if (uc_priv
->companion_device_count
) {
279 uclass_foreach_dev(bus
, uc
) {
280 if (!device_active(bus
))
283 priv
= dev_get_uclass_priv(bus
);
285 usb_scan_bus(bus
, true);
291 /* Remove any devices that were not found on this scan */
292 remove_inactive_children(uc
, bus
);
294 ret
= uclass_get(UCLASS_USB_HUB
, &uc
);
297 remove_inactive_children(uc
, bus
);
299 /* if we were not able to find at least one working bus, bail out */
301 printf("No controllers found\n");
302 else if (controllers_initialized
== 0)
303 printf("USB error: all controllers failed lowlevel init\n");
305 return usb_started
? 0 : -1;
309 * TODO(sjg@chromium.org): Remove this legacy function. At present it is needed
310 * to support boards which use driver model for USB but not Ethernet, and want
311 * to use USB Ethernet.
313 * The #if clause is here to ensure that remains the only case.
315 #if !defined(CONFIG_DM_ETH) && defined(CONFIG_USB_HOST_ETHER)
316 static struct usb_device
*find_child_devnum(struct udevice
*parent
, int devnum
)
318 struct usb_device
*udev
;
321 if (!device_active(parent
))
323 udev
= dev_get_parent_priv(parent
);
324 if (udev
->devnum
== devnum
)
327 for (device_find_first_child(parent
, &dev
);
329 device_find_next_child(&dev
)) {
330 udev
= find_child_devnum(dev
, devnum
);
338 struct usb_device
*usb_get_dev_index(struct udevice
*bus
, int index
)
341 int devnum
= index
+ 1; /* Addresses are allocated from 1 on USB */
343 device_find_first_child(bus
, &dev
);
347 return find_child_devnum(dev
, devnum
);
351 int usb_setup_ehci_gadget(struct ehci_ctrl
**ctlrp
)
353 struct usb_platdata
*plat
;
357 /* Find the old device and remove it */
358 ret
= uclass_find_device_by_seq(UCLASS_USB
, 0, true, &dev
);
361 ret
= device_remove(dev
);
365 plat
= dev_get_platdata(dev
);
366 plat
->init_type
= USB_INIT_DEVICE
;
367 ret
= device_probe(dev
);
370 *ctlrp
= dev_get_priv(dev
);
375 /* returns 0 if no match, 1 if match */
376 int usb_match_device(const struct usb_device_descriptor
*desc
,
377 const struct usb_device_id
*id
)
379 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_VENDOR
) &&
380 id
->idVendor
!= le16_to_cpu(desc
->idVendor
))
383 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_PRODUCT
) &&
384 id
->idProduct
!= le16_to_cpu(desc
->idProduct
))
387 /* No need to test id->bcdDevice_lo != 0, since 0 is never
388 greater than any unsigned number. */
389 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_LO
) &&
390 (id
->bcdDevice_lo
> le16_to_cpu(desc
->bcdDevice
)))
393 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_HI
) &&
394 (id
->bcdDevice_hi
< le16_to_cpu(desc
->bcdDevice
)))
397 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_CLASS
) &&
398 (id
->bDeviceClass
!= desc
->bDeviceClass
))
401 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_SUBCLASS
) &&
402 (id
->bDeviceSubClass
!= desc
->bDeviceSubClass
))
405 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_PROTOCOL
) &&
406 (id
->bDeviceProtocol
!= desc
->bDeviceProtocol
))
412 /* returns 0 if no match, 1 if match */
413 int usb_match_one_id_intf(const struct usb_device_descriptor
*desc
,
414 const struct usb_interface_descriptor
*int_desc
,
415 const struct usb_device_id
*id
)
417 /* The interface class, subclass, protocol and number should never be
418 * checked for a match if the device class is Vendor Specific,
419 * unless the match record specifies the Vendor ID. */
420 if (desc
->bDeviceClass
== USB_CLASS_VENDOR_SPEC
&&
421 !(id
->match_flags
& USB_DEVICE_ID_MATCH_VENDOR
) &&
422 (id
->match_flags
& (USB_DEVICE_ID_MATCH_INT_CLASS
|
423 USB_DEVICE_ID_MATCH_INT_SUBCLASS
|
424 USB_DEVICE_ID_MATCH_INT_PROTOCOL
|
425 USB_DEVICE_ID_MATCH_INT_NUMBER
)))
428 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_CLASS
) &&
429 (id
->bInterfaceClass
!= int_desc
->bInterfaceClass
))
432 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_SUBCLASS
) &&
433 (id
->bInterfaceSubClass
!= int_desc
->bInterfaceSubClass
))
436 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_PROTOCOL
) &&
437 (id
->bInterfaceProtocol
!= int_desc
->bInterfaceProtocol
))
440 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_NUMBER
) &&
441 (id
->bInterfaceNumber
!= int_desc
->bInterfaceNumber
))
447 /* returns 0 if no match, 1 if match */
448 int usb_match_one_id(struct usb_device_descriptor
*desc
,
449 struct usb_interface_descriptor
*int_desc
,
450 const struct usb_device_id
*id
)
452 if (!usb_match_device(desc
, id
))
455 return usb_match_one_id_intf(desc
, int_desc
, id
);
459 * usb_find_and_bind_driver() - Find and bind the right USB driver
461 * This only looks at certain fields in the descriptor.
463 static int usb_find_and_bind_driver(struct udevice
*parent
,
464 struct usb_device_descriptor
*desc
,
465 struct usb_interface_descriptor
*iface
,
466 int bus_seq
, int devnum
,
467 struct udevice
**devp
)
469 struct usb_driver_entry
*start
, *entry
;
475 debug("%s: Searching for driver\n", __func__
);
476 start
= ll_entry_start(struct usb_driver_entry
, usb_driver_entry
);
477 n_ents
= ll_entry_count(struct usb_driver_entry
, usb_driver_entry
);
478 for (entry
= start
; entry
!= start
+ n_ents
; entry
++) {
479 const struct usb_device_id
*id
;
481 const struct driver
*drv
;
482 struct usb_dev_platdata
*plat
;
484 for (id
= entry
->match
; id
->match_flags
; id
++) {
485 if (!usb_match_one_id(desc
, iface
, id
))
490 * We could pass the descriptor to the driver as
491 * platdata (instead of NULL) and allow its bind()
492 * method to return -ENOENT if it doesn't support this
493 * device. That way we could continue the search to
494 * find another driver. For now this doesn't seem
495 * necesssary, so just bind the first match.
497 ret
= device_bind(parent
, drv
, drv
->name
, NULL
, -1,
501 debug("%s: Match found: %s\n", __func__
, drv
->name
);
502 dev
->driver_data
= id
->driver_info
;
503 plat
= dev_get_parent_platdata(dev
);
510 /* Bind a generic driver so that the device can be used */
511 snprintf(name
, sizeof(name
), "generic_bus_%x_dev_%x", bus_seq
, devnum
);
515 ret
= device_bind_driver(parent
, "usb_dev_generic_drv", str
, devp
);
518 debug("%s: No match found: %d\n", __func__
, ret
);
523 * usb_find_child() - Find an existing device which matches our needs
527 static int usb_find_child(struct udevice
*parent
,
528 struct usb_device_descriptor
*desc
,
529 struct usb_interface_descriptor
*iface
,
530 struct udevice
**devp
)
535 for (device_find_first_child(parent
, &dev
);
537 device_find_next_child(&dev
)) {
538 struct usb_dev_platdata
*plat
= dev_get_parent_platdata(dev
);
540 /* If this device is already in use, skip it */
541 if (device_active(dev
))
543 debug(" %s: name='%s', plat=%d, desc=%d\n", __func__
,
544 dev
->name
, plat
->id
.bDeviceClass
, desc
->bDeviceClass
);
545 if (usb_match_one_id(desc
, iface
, &plat
->id
)) {
554 int usb_scan_device(struct udevice
*parent
, int port
,
555 enum usb_device_speed speed
, struct udevice
**devp
)
558 bool created
= false;
559 struct usb_dev_platdata
*plat
;
560 struct usb_bus_priv
*priv
;
561 struct usb_device
*parent_udev
;
563 ALLOC_CACHE_ALIGN_BUFFER(struct usb_device
, udev
, 1);
564 struct usb_interface_descriptor
*iface
= &udev
->config
.if_desc
[0].desc
;
567 memset(udev
, '\0', sizeof(*udev
));
568 udev
->controller_dev
= usb_get_bus(parent
);
569 priv
= dev_get_uclass_priv(udev
->controller_dev
);
572 * Somewhat nasty, this. We create a local device and use the normal
573 * USB stack to read its descriptor. Then we know what type of device
574 * to create for real.
576 * udev->dev is set to the parent, since we don't have a real device
577 * yet. The USB stack should not access udev.dev anyway, except perhaps
578 * to find the controller, and the controller will either be @parent,
579 * or some parent of @parent.
581 * Another option might be to create the device as a generic USB
582 * device, then morph it into the correct one when we know what it
583 * should be. This means that a generic USB device would morph into
584 * a network controller, or a USB flash stick, for example. However,
585 * we don't support such morphing and it isn't clear that it would
588 * Yet another option is to split out the USB stack parts of udev
589 * into something like a 'struct urb' (as Linux does) which can exist
590 * independently of any device. This feels cleaner, but calls for quite
591 * a big change to the USB stack.
593 * For now, the approach is to set up an empty udev, read its
594 * descriptor and assign it an address, then bind a real device and
595 * stash the resulting information into the device's parent
596 * platform data. Then when we probe it, usb_child_pre_probe() is called
597 * and it will pull the information out of the stash.
601 udev
->devnum
= priv
->next_addr
+ 1;
603 debug("Calling usb_setup_device(), portnr=%d\n", udev
->portnr
);
604 parent_udev
= device_get_uclass_id(parent
) == UCLASS_USB_HUB
?
605 dev_get_parent_priv(parent
) : NULL
;
606 ret
= usb_setup_device(udev
, priv
->desc_before_addr
, parent_udev
);
607 debug("read_descriptor for '%s': ret=%d\n", parent
->name
, ret
);
610 ret
= usb_find_child(parent
, &udev
->descriptor
, iface
, &dev
);
611 debug("** usb_find_child returns %d\n", ret
);
615 ret
= usb_find_and_bind_driver(parent
, &udev
->descriptor
, iface
,
616 udev
->controller_dev
->seq
,
622 plat
= dev_get_parent_platdata(dev
);
623 debug("%s: Probing '%s', plat=%p\n", __func__
, dev
->name
, plat
);
624 plat
->devnum
= udev
->devnum
;
627 ret
= device_probe(dev
);
629 debug("%s: Device '%s' probe failed\n", __func__
, dev
->name
);
641 * Detect if a USB device has been plugged or unplugged.
643 int usb_detect_change(void)
650 ret
= uclass_get(UCLASS_USB_HUB
, &uc
);
654 uclass_foreach_dev(hub
, uc
) {
655 struct usb_device
*udev
;
658 if (!device_active(hub
))
660 for (device_find_first_child(hub
, &dev
);
662 device_find_next_child(&dev
)) {
663 struct usb_port_status status
;
665 if (!device_active(dev
))
668 udev
= dev_get_parent_priv(dev
);
669 if (usb_get_port_status(udev
, udev
->portnr
, &status
)
671 /* USB request failed */
674 if (le16_to_cpu(status
.wPortChange
) &
675 USB_PORT_STAT_C_CONNECTION
)
683 int usb_child_post_bind(struct udevice
*dev
)
685 struct usb_dev_platdata
*plat
= dev_get_parent_platdata(dev
);
686 const void *blob
= gd
->fdt_blob
;
689 if (dev
->of_offset
== -1)
692 /* We only support matching a few things */
693 val
= fdtdec_get_int(blob
, dev
->of_offset
, "usb,device-class", -1);
695 plat
->id
.match_flags
|= USB_DEVICE_ID_MATCH_DEV_CLASS
;
696 plat
->id
.bDeviceClass
= val
;
698 val
= fdtdec_get_int(blob
, dev
->of_offset
, "usb,interface-class", -1);
700 plat
->id
.match_flags
|= USB_DEVICE_ID_MATCH_INT_CLASS
;
701 plat
->id
.bInterfaceClass
= val
;
707 struct udevice
*usb_get_bus(struct udevice
*dev
)
711 for (bus
= dev
; bus
&& device_get_uclass_id(bus
) != UCLASS_USB
; )
714 /* By design this cannot happen */
716 debug("USB HUB '%s' does not have a controller\n", dev
->name
);
722 int usb_child_pre_probe(struct udevice
*dev
)
724 struct usb_device
*udev
= dev_get_parent_priv(dev
);
725 struct usb_dev_platdata
*plat
= dev_get_parent_platdata(dev
);
730 * Copy over all the values set in the on stack struct
731 * usb_device in usb_scan_device() to our final struct
732 * usb_device for this dev.
734 *udev
= *(plat
->udev
);
735 /* And clear plat->udev as it will not be valid for long */
740 * This happens with devices which are explicitly bound
741 * instead of being discovered through usb_scan_device()
742 * such as sandbox emul devices.
745 udev
->controller_dev
= usb_get_bus(dev
);
746 udev
->devnum
= plat
->devnum
;
749 * udev did not go through usb_scan_device(), so we need to
750 * select the config and read the config descriptors.
752 ret
= usb_select_config(udev
);
760 UCLASS_DRIVER(usb
) = {
763 .flags
= DM_UC_FLAG_SEQ_ALIAS
,
764 .post_bind
= dm_scan_fdt_dev
,
765 .priv_auto_alloc_size
= sizeof(struct usb_uclass_priv
),
766 .per_child_auto_alloc_size
= sizeof(struct usb_device
),
767 .per_device_auto_alloc_size
= sizeof(struct usb_bus_priv
),
768 .child_post_bind
= usb_child_post_bind
,
769 .child_pre_probe
= usb_child_pre_probe
,
770 .per_child_platdata_auto_alloc_size
= sizeof(struct usb_dev_platdata
),
773 UCLASS_DRIVER(usb_dev_generic
) = {
774 .id
= UCLASS_USB_DEV_GENERIC
,
775 .name
= "usb_dev_generic",
778 U_BOOT_DRIVER(usb_dev_generic_drv
) = {
779 .id
= UCLASS_USB_DEV_GENERIC
,
780 .name
= "usb_dev_generic_drv",