xref: /openbmc/u-boot/drivers/usb/host/usb-uclass.c (revision 1f154a63)
1 /*
2  * (C) Copyright 2015 Google, Inc
3  * Written by Simon Glass <sjg@chromium.org>
4  *
5  * usb_match_device() modified from Linux kernel v4.0.
6  *
7  * SPDX-License-Identifier:	GPL-2.0+
8  */
9 
10 #include <common.h>
11 #include <dm.h>
12 #include <errno.h>
13 #include <memalign.h>
14 #include <usb.h>
15 #include <dm/device-internal.h>
16 #include <dm/lists.h>
17 #include <dm/uclass-internal.h>
18 
19 extern bool usb_started; /* flag for the started/stopped USB status */
20 static bool asynch_allowed;
21 
22 struct usb_uclass_priv {
23 	int companion_device_count;
24 };
25 
26 int usb_disable_asynch(int disable)
27 {
28 	int old_value = asynch_allowed;
29 
30 	asynch_allowed = !disable;
31 	return old_value;
32 }
33 
34 int submit_int_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
35 		   int length, int interval)
36 {
37 	struct udevice *bus = udev->controller_dev;
38 	struct dm_usb_ops *ops = usb_get_ops(bus);
39 
40 	if (!ops->interrupt)
41 		return -ENOSYS;
42 
43 	return ops->interrupt(bus, udev, pipe, buffer, length, interval);
44 }
45 
46 int submit_control_msg(struct usb_device *udev, unsigned long pipe,
47 		       void *buffer, int length, struct devrequest *setup)
48 {
49 	struct udevice *bus = udev->controller_dev;
50 	struct dm_usb_ops *ops = usb_get_ops(bus);
51 	struct usb_uclass_priv *uc_priv = bus->uclass->priv;
52 	int err;
53 
54 	if (!ops->control)
55 		return -ENOSYS;
56 
57 	err = ops->control(bus, udev, pipe, buffer, length, setup);
58 	if (setup->request == USB_REQ_SET_FEATURE &&
59 	    setup->requesttype == USB_RT_PORT &&
60 	    setup->value == cpu_to_le16(USB_PORT_FEAT_RESET) &&
61 	    err == -ENXIO) {
62 		/* Device handed over to companion after port reset */
63 		uc_priv->companion_device_count++;
64 	}
65 
66 	return err;
67 }
68 
69 int submit_bulk_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
70 		    int length)
71 {
72 	struct udevice *bus = udev->controller_dev;
73 	struct dm_usb_ops *ops = usb_get_ops(bus);
74 
75 	if (!ops->bulk)
76 		return -ENOSYS;
77 
78 	return ops->bulk(bus, udev, pipe, buffer, length);
79 }
80 
81 struct int_queue *create_int_queue(struct usb_device *udev,
82 		unsigned long pipe, int queuesize, int elementsize,
83 		void *buffer, int interval)
84 {
85 	struct udevice *bus = udev->controller_dev;
86 	struct dm_usb_ops *ops = usb_get_ops(bus);
87 
88 	if (!ops->create_int_queue)
89 		return NULL;
90 
91 	return ops->create_int_queue(bus, udev, pipe, queuesize, elementsize,
92 				     buffer, interval);
93 }
94 
95 void *poll_int_queue(struct usb_device *udev, struct int_queue *queue)
96 {
97 	struct udevice *bus = udev->controller_dev;
98 	struct dm_usb_ops *ops = usb_get_ops(bus);
99 
100 	if (!ops->poll_int_queue)
101 		return NULL;
102 
103 	return ops->poll_int_queue(bus, udev, queue);
104 }
105 
106 int destroy_int_queue(struct usb_device *udev, struct int_queue *queue)
107 {
108 	struct udevice *bus = udev->controller_dev;
109 	struct dm_usb_ops *ops = usb_get_ops(bus);
110 
111 	if (!ops->destroy_int_queue)
112 		return -ENOSYS;
113 
114 	return ops->destroy_int_queue(bus, udev, queue);
115 }
116 
117 int usb_alloc_device(struct usb_device *udev)
118 {
119 	struct udevice *bus = udev->controller_dev;
120 	struct dm_usb_ops *ops = usb_get_ops(bus);
121 
122 	/* This is only requird by some controllers - current XHCI */
123 	if (!ops->alloc_device)
124 		return 0;
125 
126 	return ops->alloc_device(bus, udev);
127 }
128 
129 int usb_reset_root_port(struct usb_device *udev)
130 {
131 	struct udevice *bus = udev->controller_dev;
132 	struct dm_usb_ops *ops = usb_get_ops(bus);
133 
134 	if (!ops->reset_root_port)
135 		return -ENOSYS;
136 
137 	return ops->reset_root_port(bus, udev);
138 }
139 
140 int usb_update_hub_device(struct usb_device *udev)
141 {
142 	struct udevice *bus = udev->controller_dev;
143 	struct dm_usb_ops *ops = usb_get_ops(bus);
144 
145 	if (!ops->update_hub_device)
146 		return -ENOSYS;
147 
148 	return ops->update_hub_device(bus, udev);
149 }
150 
151 int usb_get_max_xfer_size(struct usb_device *udev, size_t *size)
152 {
153 	struct udevice *bus = udev->controller_dev;
154 	struct dm_usb_ops *ops = usb_get_ops(bus);
155 
156 	if (!ops->get_max_xfer_size)
157 		return -ENOSYS;
158 
159 	return ops->get_max_xfer_size(bus, size);
160 }
161 
162 int usb_stop(void)
163 {
164 	struct udevice *bus;
165 	struct udevice *rh;
166 	struct uclass *uc;
167 	struct usb_uclass_priv *uc_priv;
168 	int err = 0, ret;
169 
170 	/* De-activate any devices that have been activated */
171 	ret = uclass_get(UCLASS_USB, &uc);
172 	if (ret)
173 		return ret;
174 
175 	uc_priv = uc->priv;
176 
177 	uclass_foreach_dev(bus, uc) {
178 		ret = device_remove(bus, DM_REMOVE_NORMAL);
179 		if (ret && !err)
180 			err = ret;
181 
182 		/* Locate root hub device */
183 		device_find_first_child(bus, &rh);
184 		if (rh) {
185 			/*
186 			 * All USB devices are children of root hub.
187 			 * Unbinding root hub will unbind all of its children.
188 			 */
189 			ret = device_unbind(rh);
190 			if (ret && !err)
191 				err = ret;
192 		}
193 	}
194 
195 #ifdef CONFIG_USB_STORAGE
196 	usb_stor_reset();
197 #endif
198 	uc_priv->companion_device_count = 0;
199 	usb_started = 0;
200 
201 	return err;
202 }
203 
204 static void usb_scan_bus(struct udevice *bus, bool recurse)
205 {
206 	struct usb_bus_priv *priv;
207 	struct udevice *dev;
208 	int ret;
209 
210 	priv = dev_get_uclass_priv(bus);
211 
212 	assert(recurse);	/* TODO: Support non-recusive */
213 
214 	printf("scanning bus %d for devices... ", bus->seq);
215 	debug("\n");
216 	ret = usb_scan_device(bus, 0, USB_SPEED_FULL, &dev);
217 	if (ret)
218 		printf("failed, error %d\n", ret);
219 	else if (priv->next_addr == 0)
220 		printf("No USB Device found\n");
221 	else
222 		printf("%d USB Device(s) found\n", priv->next_addr);
223 }
224 
225 static void remove_inactive_children(struct uclass *uc, struct udevice *bus)
226 {
227 	uclass_foreach_dev(bus, uc) {
228 		struct udevice *dev, *next;
229 
230 		if (!device_active(bus))
231 			continue;
232 		device_foreach_child_safe(dev, next, bus) {
233 			if (!device_active(dev))
234 				device_unbind(dev);
235 		}
236 	}
237 }
238 
239 int usb_init(void)
240 {
241 	int controllers_initialized = 0;
242 	struct usb_uclass_priv *uc_priv;
243 	struct usb_bus_priv *priv;
244 	struct udevice *bus;
245 	struct uclass *uc;
246 	int count = 0;
247 	int ret;
248 
249 	asynch_allowed = 1;
250 
251 	ret = uclass_get(UCLASS_USB, &uc);
252 	if (ret)
253 		return ret;
254 
255 	uc_priv = uc->priv;
256 
257 	uclass_foreach_dev(bus, uc) {
258 		/* init low_level USB */
259 		printf("USB%d:   ", count);
260 		count++;
261 
262 #ifdef CONFIG_SANDBOX
263 		/*
264 		 * For Sandbox, we need scan the device tree each time when we
265 		 * start the USB stack, in order to re-create the emulated USB
266 		 * devices and bind drivers for them before we actually do the
267 		 * driver probe.
268 		 */
269 		ret = dm_scan_fdt_dev(bus);
270 		if (ret) {
271 			printf("Sandbox USB device scan failed (%d)\n", ret);
272 			continue;
273 		}
274 #endif
275 
276 		ret = device_probe(bus);
277 		if (ret == -ENODEV) {	/* No such device. */
278 			puts("Port not available.\n");
279 			controllers_initialized++;
280 			continue;
281 		}
282 
283 		if (ret) {		/* Other error. */
284 			printf("probe failed, error %d\n", ret);
285 			continue;
286 		}
287 		controllers_initialized++;
288 		usb_started = true;
289 	}
290 
291 	/*
292 	 * lowlevel init done, now scan the bus for devices i.e. search HUBs
293 	 * and configure them, first scan primary controllers.
294 	 */
295 	uclass_foreach_dev(bus, uc) {
296 		if (!device_active(bus))
297 			continue;
298 
299 		priv = dev_get_uclass_priv(bus);
300 		if (!priv->companion)
301 			usb_scan_bus(bus, true);
302 	}
303 
304 	/*
305 	 * Now that the primary controllers have been scanned and have handed
306 	 * over any devices they do not understand to their companions, scan
307 	 * the companions if necessary.
308 	 */
309 	if (uc_priv->companion_device_count) {
310 		uclass_foreach_dev(bus, uc) {
311 			if (!device_active(bus))
312 				continue;
313 
314 			priv = dev_get_uclass_priv(bus);
315 			if (priv->companion)
316 				usb_scan_bus(bus, true);
317 		}
318 	}
319 
320 	debug("scan end\n");
321 
322 	/* Remove any devices that were not found on this scan */
323 	remove_inactive_children(uc, bus);
324 
325 	ret = uclass_get(UCLASS_USB_HUB, &uc);
326 	if (ret)
327 		return ret;
328 	remove_inactive_children(uc, bus);
329 
330 	/* if we were not able to find at least one working bus, bail out */
331 	if (!count)
332 		printf("No controllers found\n");
333 	else if (controllers_initialized == 0)
334 		printf("USB error: all controllers failed lowlevel init\n");
335 
336 	return usb_started ? 0 : -1;
337 }
338 
339 /*
340  * TODO(sjg@chromium.org): Remove this legacy function. At present it is needed
341  * to support boards which use driver model for USB but not Ethernet, and want
342  * to use USB Ethernet.
343  *
344  * The #if clause is here to ensure that remains the only case.
345  */
346 #if !defined(CONFIG_DM_ETH) && defined(CONFIG_USB_HOST_ETHER)
347 static struct usb_device *find_child_devnum(struct udevice *parent, int devnum)
348 {
349 	struct usb_device *udev;
350 	struct udevice *dev;
351 
352 	if (!device_active(parent))
353 		return NULL;
354 	udev = dev_get_parent_priv(parent);
355 	if (udev->devnum == devnum)
356 		return udev;
357 
358 	for (device_find_first_child(parent, &dev);
359 	     dev;
360 	     device_find_next_child(&dev)) {
361 		udev = find_child_devnum(dev, devnum);
362 		if (udev)
363 			return udev;
364 	}
365 
366 	return NULL;
367 }
368 
369 struct usb_device *usb_get_dev_index(struct udevice *bus, int index)
370 {
371 	struct udevice *dev;
372 	int devnum = index + 1; /* Addresses are allocated from 1 on USB */
373 
374 	device_find_first_child(bus, &dev);
375 	if (!dev)
376 		return NULL;
377 
378 	return find_child_devnum(dev, devnum);
379 }
380 #endif
381 
382 int usb_setup_ehci_gadget(struct ehci_ctrl **ctlrp)
383 {
384 	struct usb_platdata *plat;
385 	struct udevice *dev;
386 	int ret;
387 
388 	/* Find the old device and remove it */
389 	ret = uclass_find_device_by_seq(UCLASS_USB, 0, true, &dev);
390 	if (ret)
391 		return ret;
392 	ret = device_remove(dev, DM_REMOVE_NORMAL);
393 	if (ret)
394 		return ret;
395 
396 	plat = dev_get_platdata(dev);
397 	plat->init_type = USB_INIT_DEVICE;
398 	ret = device_probe(dev);
399 	if (ret)
400 		return ret;
401 	*ctlrp = dev_get_priv(dev);
402 
403 	return 0;
404 }
405 
406 /* returns 0 if no match, 1 if match */
407 static int usb_match_device(const struct usb_device_descriptor *desc,
408 			    const struct usb_device_id *id)
409 {
410 	if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
411 	    id->idVendor != le16_to_cpu(desc->idVendor))
412 		return 0;
413 
414 	if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
415 	    id->idProduct != le16_to_cpu(desc->idProduct))
416 		return 0;
417 
418 	/* No need to test id->bcdDevice_lo != 0, since 0 is never
419 	   greater than any unsigned number. */
420 	if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
421 	    (id->bcdDevice_lo > le16_to_cpu(desc->bcdDevice)))
422 		return 0;
423 
424 	if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
425 	    (id->bcdDevice_hi < le16_to_cpu(desc->bcdDevice)))
426 		return 0;
427 
428 	if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
429 	    (id->bDeviceClass != desc->bDeviceClass))
430 		return 0;
431 
432 	if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
433 	    (id->bDeviceSubClass != desc->bDeviceSubClass))
434 		return 0;
435 
436 	if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
437 	    (id->bDeviceProtocol != desc->bDeviceProtocol))
438 		return 0;
439 
440 	return 1;
441 }
442 
443 /* returns 0 if no match, 1 if match */
444 static int usb_match_one_id_intf(const struct usb_device_descriptor *desc,
445 			const struct usb_interface_descriptor *int_desc,
446 			const struct usb_device_id *id)
447 {
448 	/* The interface class, subclass, protocol and number should never be
449 	 * checked for a match if the device class is Vendor Specific,
450 	 * unless the match record specifies the Vendor ID. */
451 	if (desc->bDeviceClass == USB_CLASS_VENDOR_SPEC &&
452 	    !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
453 	    (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
454 				USB_DEVICE_ID_MATCH_INT_SUBCLASS |
455 				USB_DEVICE_ID_MATCH_INT_PROTOCOL |
456 				USB_DEVICE_ID_MATCH_INT_NUMBER)))
457 		return 0;
458 
459 	if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
460 	    (id->bInterfaceClass != int_desc->bInterfaceClass))
461 		return 0;
462 
463 	if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
464 	    (id->bInterfaceSubClass != int_desc->bInterfaceSubClass))
465 		return 0;
466 
467 	if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
468 	    (id->bInterfaceProtocol != int_desc->bInterfaceProtocol))
469 		return 0;
470 
471 	if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
472 	    (id->bInterfaceNumber != int_desc->bInterfaceNumber))
473 		return 0;
474 
475 	return 1;
476 }
477 
478 /* returns 0 if no match, 1 if match */
479 static int usb_match_one_id(struct usb_device_descriptor *desc,
480 			    struct usb_interface_descriptor *int_desc,
481 			    const struct usb_device_id *id)
482 {
483 	if (!usb_match_device(desc, id))
484 		return 0;
485 
486 	return usb_match_one_id_intf(desc, int_desc, id);
487 }
488 
489 /**
490  * usb_find_and_bind_driver() - Find and bind the right USB driver
491  *
492  * This only looks at certain fields in the descriptor.
493  */
494 static int usb_find_and_bind_driver(struct udevice *parent,
495 				    struct usb_device_descriptor *desc,
496 				    struct usb_interface_descriptor *iface,
497 				    int bus_seq, int devnum,
498 				    struct udevice **devp)
499 {
500 	struct usb_driver_entry *start, *entry;
501 	int n_ents;
502 	int ret;
503 	char name[30], *str;
504 
505 	*devp = NULL;
506 	debug("%s: Searching for driver\n", __func__);
507 	start = ll_entry_start(struct usb_driver_entry, usb_driver_entry);
508 	n_ents = ll_entry_count(struct usb_driver_entry, usb_driver_entry);
509 	for (entry = start; entry != start + n_ents; entry++) {
510 		const struct usb_device_id *id;
511 		struct udevice *dev;
512 		const struct driver *drv;
513 		struct usb_dev_platdata *plat;
514 
515 		for (id = entry->match; id->match_flags; id++) {
516 			if (!usb_match_one_id(desc, iface, id))
517 				continue;
518 
519 			drv = entry->driver;
520 			/*
521 			 * We could pass the descriptor to the driver as
522 			 * platdata (instead of NULL) and allow its bind()
523 			 * method to return -ENOENT if it doesn't support this
524 			 * device. That way we could continue the search to
525 			 * find another driver. For now this doesn't seem
526 			 * necesssary, so just bind the first match.
527 			 */
528 			ret = device_bind(parent, drv, drv->name, NULL, -1,
529 					  &dev);
530 			if (ret)
531 				goto error;
532 			debug("%s: Match found: %s\n", __func__, drv->name);
533 			dev->driver_data = id->driver_info;
534 			plat = dev_get_parent_platdata(dev);
535 			plat->id = *id;
536 			*devp = dev;
537 			return 0;
538 		}
539 	}
540 
541 	/* Bind a generic driver so that the device can be used */
542 	snprintf(name, sizeof(name), "generic_bus_%x_dev_%x", bus_seq, devnum);
543 	str = strdup(name);
544 	if (!str)
545 		return -ENOMEM;
546 	ret = device_bind_driver(parent, "usb_dev_generic_drv", str, devp);
547 
548 error:
549 	debug("%s: No match found: %d\n", __func__, ret);
550 	return ret;
551 }
552 
553 /**
554  * usb_find_child() - Find an existing device which matches our needs
555  *
556  *
557  */
558 static int usb_find_child(struct udevice *parent,
559 			  struct usb_device_descriptor *desc,
560 			  struct usb_interface_descriptor *iface,
561 			  struct udevice **devp)
562 {
563 	struct udevice *dev;
564 
565 	*devp = NULL;
566 	for (device_find_first_child(parent, &dev);
567 	     dev;
568 	     device_find_next_child(&dev)) {
569 		struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
570 
571 		/* If this device is already in use, skip it */
572 		if (device_active(dev))
573 			continue;
574 		debug("   %s: name='%s', plat=%d, desc=%d\n", __func__,
575 		      dev->name, plat->id.bDeviceClass, desc->bDeviceClass);
576 		if (usb_match_one_id(desc, iface, &plat->id)) {
577 			*devp = dev;
578 			return 0;
579 		}
580 	}
581 
582 	return -ENOENT;
583 }
584 
585 int usb_scan_device(struct udevice *parent, int port,
586 		    enum usb_device_speed speed, struct udevice **devp)
587 {
588 	struct udevice *dev;
589 	bool created = false;
590 	struct usb_dev_platdata *plat;
591 	struct usb_bus_priv *priv;
592 	struct usb_device *parent_udev;
593 	int ret;
594 	ALLOC_CACHE_ALIGN_BUFFER(struct usb_device, udev, 1);
595 	struct usb_interface_descriptor *iface = &udev->config.if_desc[0].desc;
596 
597 	*devp = NULL;
598 	memset(udev, '\0', sizeof(*udev));
599 	udev->controller_dev = usb_get_bus(parent);
600 	priv = dev_get_uclass_priv(udev->controller_dev);
601 
602 	/*
603 	 * Somewhat nasty, this. We create a local device and use the normal
604 	 * USB stack to read its descriptor. Then we know what type of device
605 	 * to create for real.
606 	 *
607 	 * udev->dev is set to the parent, since we don't have a real device
608 	 * yet. The USB stack should not access udev.dev anyway, except perhaps
609 	 * to find the controller, and the controller will either be @parent,
610 	 * or some parent of @parent.
611 	 *
612 	 * Another option might be to create the device as a generic USB
613 	 * device, then morph it into the correct one when we know what it
614 	 * should be. This means that a generic USB device would morph into
615 	 * a network controller, or a USB flash stick, for example. However,
616 	 * we don't support such morphing and it isn't clear that it would
617 	 * be easy to do.
618 	 *
619 	 * Yet another option is to split out the USB stack parts of udev
620 	 * into something like a 'struct urb' (as Linux does) which can exist
621 	 * independently of any device. This feels cleaner, but calls for quite
622 	 * a big change to the USB stack.
623 	 *
624 	 * For now, the approach is to set up an empty udev, read its
625 	 * descriptor and assign it an address, then bind a real device and
626 	 * stash the resulting information into the device's parent
627 	 * platform data. Then when we probe it, usb_child_pre_probe() is called
628 	 * and it will pull the information out of the stash.
629 	 */
630 	udev->dev = parent;
631 	udev->speed = speed;
632 	udev->devnum = priv->next_addr + 1;
633 	udev->portnr = port;
634 	debug("Calling usb_setup_device(), portnr=%d\n", udev->portnr);
635 	parent_udev = device_get_uclass_id(parent) == UCLASS_USB_HUB ?
636 		dev_get_parent_priv(parent) : NULL;
637 	ret = usb_setup_device(udev, priv->desc_before_addr, parent_udev);
638 	debug("read_descriptor for '%s': ret=%d\n", parent->name, ret);
639 	if (ret)
640 		return ret;
641 	ret = usb_find_child(parent, &udev->descriptor, iface, &dev);
642 	debug("** usb_find_child returns %d\n", ret);
643 	if (ret) {
644 		if (ret != -ENOENT)
645 			return ret;
646 		ret = usb_find_and_bind_driver(parent, &udev->descriptor, iface,
647 					       udev->controller_dev->seq,
648 					       udev->devnum, &dev);
649 		if (ret)
650 			return ret;
651 		created = true;
652 	}
653 	plat = dev_get_parent_platdata(dev);
654 	debug("%s: Probing '%s', plat=%p\n", __func__, dev->name, plat);
655 	plat->devnum = udev->devnum;
656 	plat->udev = udev;
657 	priv->next_addr++;
658 	ret = device_probe(dev);
659 	if (ret) {
660 		debug("%s: Device '%s' probe failed\n", __func__, dev->name);
661 		priv->next_addr--;
662 		if (created)
663 			device_unbind(dev);
664 		return ret;
665 	}
666 	*devp = dev;
667 
668 	return 0;
669 }
670 
671 /*
672  * Detect if a USB device has been plugged or unplugged.
673  */
674 int usb_detect_change(void)
675 {
676 	struct udevice *hub;
677 	struct uclass *uc;
678 	int change = 0;
679 	int ret;
680 
681 	ret = uclass_get(UCLASS_USB_HUB, &uc);
682 	if (ret)
683 		return ret;
684 
685 	uclass_foreach_dev(hub, uc) {
686 		struct usb_device *udev;
687 		struct udevice *dev;
688 
689 		if (!device_active(hub))
690 			continue;
691 		for (device_find_first_child(hub, &dev);
692 		     dev;
693 		     device_find_next_child(&dev)) {
694 			struct usb_port_status status;
695 
696 			if (!device_active(dev))
697 				continue;
698 
699 			udev = dev_get_parent_priv(dev);
700 			if (usb_get_port_status(udev, udev->portnr, &status)
701 					< 0)
702 				/* USB request failed */
703 				continue;
704 
705 			if (le16_to_cpu(status.wPortChange) &
706 			    USB_PORT_STAT_C_CONNECTION)
707 				change++;
708 		}
709 	}
710 
711 	return change;
712 }
713 
714 static int usb_child_post_bind(struct udevice *dev)
715 {
716 	struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
717 	int val;
718 
719 	if (!dev_of_valid(dev))
720 		return 0;
721 
722 	/* We only support matching a few things */
723 	val = dev_read_u32_default(dev, "usb,device-class", -1);
724 	if (val != -1) {
725 		plat->id.match_flags |= USB_DEVICE_ID_MATCH_DEV_CLASS;
726 		plat->id.bDeviceClass = val;
727 	}
728 	val = dev_read_u32_default(dev, "usb,interface-class", -1);
729 	if (val != -1) {
730 		plat->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
731 		plat->id.bInterfaceClass = val;
732 	}
733 
734 	return 0;
735 }
736 
737 struct udevice *usb_get_bus(struct udevice *dev)
738 {
739 	struct udevice *bus;
740 
741 	for (bus = dev; bus && device_get_uclass_id(bus) != UCLASS_USB; )
742 		bus = bus->parent;
743 	if (!bus) {
744 		/* By design this cannot happen */
745 		assert(bus);
746 		debug("USB HUB '%s' does not have a controller\n", dev->name);
747 	}
748 
749 	return bus;
750 }
751 
752 int usb_child_pre_probe(struct udevice *dev)
753 {
754 	struct usb_device *udev = dev_get_parent_priv(dev);
755 	struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
756 	int ret;
757 
758 	if (plat->udev) {
759 		/*
760 		 * Copy over all the values set in the on stack struct
761 		 * usb_device in usb_scan_device() to our final struct
762 		 * usb_device for this dev.
763 		 */
764 		*udev = *(plat->udev);
765 		/* And clear plat->udev as it will not be valid for long */
766 		plat->udev = NULL;
767 		udev->dev = dev;
768 	} else {
769 		/*
770 		 * This happens with devices which are explicitly bound
771 		 * instead of being discovered through usb_scan_device()
772 		 * such as sandbox emul devices.
773 		 */
774 		udev->dev = dev;
775 		udev->controller_dev = usb_get_bus(dev);
776 		udev->devnum = plat->devnum;
777 
778 		/*
779 		 * udev did not go through usb_scan_device(), so we need to
780 		 * select the config and read the config descriptors.
781 		 */
782 		ret = usb_select_config(udev);
783 		if (ret)
784 			return ret;
785 	}
786 
787 	return 0;
788 }
789 
790 UCLASS_DRIVER(usb) = {
791 	.id		= UCLASS_USB,
792 	.name		= "usb",
793 	.flags		= DM_UC_FLAG_SEQ_ALIAS,
794 	.post_bind	= dm_scan_fdt_dev,
795 	.priv_auto_alloc_size = sizeof(struct usb_uclass_priv),
796 	.per_child_auto_alloc_size = sizeof(struct usb_device),
797 	.per_device_auto_alloc_size = sizeof(struct usb_bus_priv),
798 	.child_post_bind = usb_child_post_bind,
799 	.child_pre_probe = usb_child_pre_probe,
800 	.per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),
801 };
802 
803 UCLASS_DRIVER(usb_dev_generic) = {
804 	.id		= UCLASS_USB_DEV_GENERIC,
805 	.name		= "usb_dev_generic",
806 };
807 
808 U_BOOT_DRIVER(usb_dev_generic_drv) = {
809 	.id		= UCLASS_USB_DEV_GENERIC,
810 	.name		= "usb_dev_generic_drv",
811 };
812