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
);
103 static int usb_scan_bus(struct udevice
*bus
, bool recurse
)
105 struct usb_bus_priv
*priv
;
109 priv
= dev_get_uclass_priv(bus
);
111 assert(recurse
); /* TODO: Support non-recusive */
113 ret
= usb_scan_device(bus
, 0, USB_SPEED_FULL
, &dev
);
117 return priv
->next_addr
;
122 int controllers_initialized
= 0;
131 ret
= uclass_get(UCLASS_USB
, &uc
);
135 uclass_foreach_dev(bus
, uc
) {
136 /* init low_level USB */
139 printf("%d: ", bus
->seq
);
140 ret
= device_probe(bus
);
141 if (ret
== -ENODEV
) { /* No such device. */
142 puts("Port not available.\n");
143 controllers_initialized
++;
147 if (ret
) { /* Other error. */
148 printf("probe failed, error %d\n", ret
);
152 * lowlevel init is OK, now scan the bus for devices
153 * i.e. search HUBs and configure them
155 controllers_initialized
++;
156 printf("scanning bus %d for devices... ", bus
->seq
);
158 ret
= usb_scan_bus(bus
, true);
160 printf("failed, error %d\n", ret
);
162 printf("No USB Device found\n");
164 printf("%d USB Device(s) found\n", ret
);
169 /* if we were not able to find at least one working bus, bail out */
171 printf("No controllers found\n");
172 else if (controllers_initialized
== 0)
173 printf("USB error: all controllers failed lowlevel init\n");
175 return usb_started
? 0 : -1;
178 int usb_reset_root_port(void)
183 static struct usb_device
*find_child_devnum(struct udevice
*parent
, int devnum
)
185 struct usb_device
*udev
;
188 if (!device_active(parent
))
190 udev
= dev_get_parentdata(parent
);
191 if (udev
->devnum
== devnum
)
194 for (device_find_first_child(parent
, &dev
);
196 device_find_next_child(&dev
)) {
197 udev
= find_child_devnum(dev
, devnum
);
205 struct usb_device
*usb_get_dev_index(struct udevice
*bus
, int index
)
208 int devnum
= index
+ 1; /* Addresses are allocated from 1 on USB */
210 device_find_first_child(bus
, &hub
);
211 if (device_get_uclass_id(hub
) == UCLASS_USB_HUB
)
212 return find_child_devnum(hub
, devnum
);
217 int usb_post_bind(struct udevice
*dev
)
219 /* Scan the bus for devices */
220 return dm_scan_fdt_node(dev
, gd
->fdt_blob
, dev
->of_offset
, false);
223 int usb_port_reset(struct usb_device
*parent
, int portnr
)
225 unsigned short portstatus
;
228 debug("%s: start\n", __func__
);
231 /* reset the port for the second time */
233 debug("%s: reset %d\n", __func__
, portnr
- 1);
234 ret
= legacy_hub_port_reset(parent
, portnr
- 1, &portstatus
);
236 printf("\n Couldn't reset port %i\n", portnr
);
240 debug("%s: reset root\n", __func__
);
241 usb_reset_root_port();
247 int usb_legacy_port_reset(struct usb_device
*parent
, int portnr
)
249 return usb_port_reset(parent
, portnr
);
252 /* returns 0 if no match, 1 if match */
253 int usb_match_device(const struct usb_device_descriptor
*desc
,
254 const struct usb_device_id
*id
)
256 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_VENDOR
) &&
257 id
->idVendor
!= le16_to_cpu(desc
->idVendor
))
260 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_PRODUCT
) &&
261 id
->idProduct
!= le16_to_cpu(desc
->idProduct
))
264 /* No need to test id->bcdDevice_lo != 0, since 0 is never
265 greater than any unsigned number. */
266 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_LO
) &&
267 (id
->bcdDevice_lo
> le16_to_cpu(desc
->bcdDevice
)))
270 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_HI
) &&
271 (id
->bcdDevice_hi
< le16_to_cpu(desc
->bcdDevice
)))
274 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_CLASS
) &&
275 (id
->bDeviceClass
!= desc
->bDeviceClass
))
278 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_SUBCLASS
) &&
279 (id
->bDeviceSubClass
!= desc
->bDeviceSubClass
))
282 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_PROTOCOL
) &&
283 (id
->bDeviceProtocol
!= desc
->bDeviceProtocol
))
289 /* returns 0 if no match, 1 if match */
290 int usb_match_one_id_intf(const struct usb_device_descriptor
*desc
,
291 const struct usb_interface_descriptor
*int_desc
,
292 const struct usb_device_id
*id
)
294 /* The interface class, subclass, protocol and number should never be
295 * checked for a match if the device class is Vendor Specific,
296 * unless the match record specifies the Vendor ID. */
297 if (desc
->bDeviceClass
== USB_CLASS_VENDOR_SPEC
&&
298 !(id
->match_flags
& USB_DEVICE_ID_MATCH_VENDOR
) &&
299 (id
->match_flags
& (USB_DEVICE_ID_MATCH_INT_CLASS
|
300 USB_DEVICE_ID_MATCH_INT_SUBCLASS
|
301 USB_DEVICE_ID_MATCH_INT_PROTOCOL
|
302 USB_DEVICE_ID_MATCH_INT_NUMBER
)))
305 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_CLASS
) &&
306 (id
->bInterfaceClass
!= int_desc
->bInterfaceClass
))
309 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_SUBCLASS
) &&
310 (id
->bInterfaceSubClass
!= int_desc
->bInterfaceSubClass
))
313 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_PROTOCOL
) &&
314 (id
->bInterfaceProtocol
!= int_desc
->bInterfaceProtocol
))
317 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_NUMBER
) &&
318 (id
->bInterfaceNumber
!= int_desc
->bInterfaceNumber
))
324 /* returns 0 if no match, 1 if match */
325 int usb_match_one_id(struct usb_device_descriptor
*desc
,
326 struct usb_interface_descriptor
*int_desc
,
327 const struct usb_device_id
*id
)
329 if (!usb_match_device(desc
, id
))
332 return usb_match_one_id_intf(desc
, int_desc
, id
);
336 * usb_find_and_bind_driver() - Find and bind the right USB driver
338 * This only looks at certain fields in the descriptor.
340 static int usb_find_and_bind_driver(struct udevice
*parent
,
341 struct usb_device_descriptor
*desc
,
342 struct usb_interface_descriptor
*iface
,
343 int bus_seq
, int devnum
,
344 struct udevice
**devp
)
346 struct usb_driver_entry
*start
, *entry
;
352 debug("%s: Searching for driver\n", __func__
);
353 start
= ll_entry_start(struct usb_driver_entry
, usb_driver_entry
);
354 n_ents
= ll_entry_count(struct usb_driver_entry
, usb_driver_entry
);
355 for (entry
= start
; entry
!= start
+ n_ents
; entry
++) {
356 const struct usb_device_id
*id
;
358 const struct driver
*drv
;
359 struct usb_dev_platdata
*plat
;
361 for (id
= entry
->match
; id
->match_flags
; id
++) {
362 if (!usb_match_one_id(desc
, iface
, id
))
367 * We could pass the descriptor to the driver as
368 * platdata (instead of NULL) and allow its bind()
369 * method to return -ENOENT if it doesn't support this
370 * device. That way we could continue the search to
371 * find another driver. For now this doesn't seem
372 * necesssary, so just bind the first match.
374 ret
= device_bind(parent
, drv
, drv
->name
, NULL
, -1,
378 debug("%s: Match found: %s\n", __func__
, drv
->name
);
379 dev
->driver_data
= id
->driver_info
;
380 plat
= dev_get_parent_platdata(dev
);
389 debug("%s: No match found: %d\n", __func__
, ret
);
394 * usb_find_child() - Find an existing device which matches our needs
398 static int usb_find_child(struct udevice
*parent
,
399 struct usb_device_descriptor
*desc
,
400 struct usb_interface_descriptor
*iface
,
401 struct udevice
**devp
)
406 for (device_find_first_child(parent
, &dev
);
408 device_find_next_child(&dev
)) {
409 struct usb_dev_platdata
*plat
= dev_get_parent_platdata(dev
);
411 /* If this device is already in use, skip it */
412 if (device_active(dev
))
414 debug(" %s: name='%s', plat=%d, desc=%d\n", __func__
,
415 dev
->name
, plat
->id
.bDeviceClass
, desc
->bDeviceClass
);
416 if (usb_match_one_id(desc
, iface
, &plat
->id
)) {
425 int usb_scan_device(struct udevice
*parent
, int port
,
426 enum usb_device_speed speed
, struct udevice
**devp
)
429 bool created
= false;
430 struct usb_dev_platdata
*plat
;
431 struct usb_bus_priv
*priv
;
432 struct usb_device
*parent_udev
;
434 ALLOC_CACHE_ALIGN_BUFFER(struct usb_device
, udev
, 1);
435 struct usb_interface_descriptor
*iface
= &udev
->config
.if_desc
[0].desc
;
438 memset(udev
, '\0', sizeof(*udev
));
439 ret
= usb_get_bus(parent
, &udev
->controller_dev
);
442 priv
= dev_get_uclass_priv(udev
->controller_dev
);
445 * Somewhat nasty, this. We create a local device and use the normal
446 * USB stack to read its descriptor. Then we know what type of device
447 * to create for real.
449 * udev->dev is set to the parent, since we don't have a real device
450 * yet. The USB stack should not access udev.dev anyway, except perhaps
451 * to find the controller, and the controller will either be @parent,
452 * or some parent of @parent.
454 * Another option might be to create the device as a generic USB
455 * device, then morph it into the correct one when we know what it
456 * should be. This means that a generic USB device would morph into
457 * a network controller, or a USB flash stick, for example. However,
458 * we don't support such morphing and it isn't clear that it would
461 * Yet another option is to split out the USB stack parts of udev
462 * into something like a 'struct urb' (as Linux does) which can exist
463 * independently of any device. This feels cleaner, but calls for quite
464 * a big change to the USB stack.
466 * For now, the approach is to set up an empty udev, read its
467 * descriptor and assign it an address, then bind a real device and
468 * stash the resulting information into the device's parent
469 * platform data. Then when we probe it, usb_child_pre_probe() is called
470 * and it will pull the information out of the stash.
474 udev
->devnum
= priv
->next_addr
+ 1;
476 debug("Calling usb_setup_device(), portnr=%d\n", udev
->portnr
);
477 parent_udev
= device_get_uclass_id(parent
) == UCLASS_USB_HUB
?
478 dev_get_parentdata(parent
) : NULL
;
479 ret
= usb_setup_device(udev
, priv
->desc_before_addr
, parent_udev
, port
);
480 debug("read_descriptor for '%s': ret=%d\n", parent
->name
, ret
);
483 ret
= usb_find_child(parent
, &udev
->descriptor
, iface
, &dev
);
484 debug("** usb_find_child returns %d\n", ret
);
488 ret
= usb_find_and_bind_driver(parent
, &udev
->descriptor
, iface
,
489 udev
->controller_dev
->seq
,
495 plat
= dev_get_parent_platdata(dev
);
496 debug("%s: Probing '%s', plat=%p\n", __func__
, dev
->name
, plat
);
497 plat
->devnum
= udev
->devnum
;
498 plat
->speed
= udev
->speed
;
499 plat
->slot_id
= udev
->slot_id
;
501 debug("** device '%s': stashing slot_id=%d\n", dev
->name
,
504 ret
= device_probe(dev
);
506 debug("%s: Device '%s' probe failed\n", __func__
, dev
->name
);
517 int usb_child_post_bind(struct udevice
*dev
)
519 struct usb_dev_platdata
*plat
= dev_get_parent_platdata(dev
);
520 const void *blob
= gd
->fdt_blob
;
523 if (dev
->of_offset
== -1)
526 /* We only support matching a few things */
527 val
= fdtdec_get_int(blob
, dev
->of_offset
, "usb,device-class", -1);
529 plat
->id
.match_flags
|= USB_DEVICE_ID_MATCH_DEV_CLASS
;
530 plat
->id
.bDeviceClass
= val
;
532 val
= fdtdec_get_int(blob
, dev
->of_offset
, "usb,interface-class", -1);
534 plat
->id
.match_flags
|= USB_DEVICE_ID_MATCH_INT_CLASS
;
535 plat
->id
.bInterfaceClass
= val
;
541 int usb_get_bus(struct udevice
*dev
, struct udevice
**busp
)
546 for (bus
= dev
; bus
&& device_get_uclass_id(bus
) != UCLASS_USB
; )
549 /* By design this cannot happen */
551 debug("USB HUB '%s' does not have a controller\n", dev
->name
);
559 int usb_child_pre_probe(struct udevice
*dev
)
562 struct usb_device
*udev
= dev_get_parentdata(dev
);
563 struct usb_dev_platdata
*plat
= dev_get_parent_platdata(dev
);
566 ret
= usb_get_bus(dev
, &bus
);
569 udev
->controller_dev
= bus
;
571 udev
->devnum
= plat
->devnum
;
572 udev
->slot_id
= plat
->slot_id
;
573 udev
->portnr
= plat
->portnr
;
574 udev
->speed
= plat
->speed
;
575 debug("** device '%s': getting slot_id=%d\n", dev
->name
, plat
->slot_id
);
577 ret
= usb_select_config(udev
);
584 UCLASS_DRIVER(usb
) = {
587 .flags
= DM_UC_FLAG_SEQ_ALIAS
,
588 .post_bind
= usb_post_bind
,
589 .per_child_auto_alloc_size
= sizeof(struct usb_device
),
590 .per_device_auto_alloc_size
= sizeof(struct usb_bus_priv
),
591 .child_post_bind
= usb_child_post_bind
,
592 .child_pre_probe
= usb_child_pre_probe
,
593 .per_child_platdata_auto_alloc_size
= sizeof(struct usb_dev_platdata
),