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