xref: /openbmc/u-boot/drivers/usb/host/usb-uclass.c (revision 089fddfd)
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 <usb.h>
14 #include <dm/device-internal.h>
15 #include <dm/lists.h>
16 #include <dm/root.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_stop(void)
143 {
144 	struct udevice *bus;
145 	struct uclass *uc;
146 	struct usb_uclass_priv *uc_priv;
147 	int err = 0, ret;
148 
149 	/* De-activate any devices that have been activated */
150 	ret = uclass_get(UCLASS_USB, &uc);
151 	if (ret)
152 		return ret;
153 
154 	uc_priv = uc->priv;
155 
156 	uclass_foreach_dev(bus, uc) {
157 		ret = device_remove(bus);
158 		if (ret && !err)
159 			err = ret;
160 		ret = device_unbind_children(bus);
161 		if (ret && !err)
162 			err = ret;
163 	}
164 
165 #ifdef CONFIG_SANDBOX
166 	struct udevice *dev;
167 
168 	/* Reset all enulation devices */
169 	ret = uclass_get(UCLASS_USB_EMUL, &uc);
170 	if (ret)
171 		return ret;
172 
173 	uclass_foreach_dev(dev, uc)
174 		usb_emul_reset(dev);
175 #endif
176 	usb_stor_reset();
177 	usb_hub_reset();
178 	uc_priv->companion_device_count = 0;
179 	usb_started = 0;
180 
181 	return err;
182 }
183 
184 static void usb_scan_bus(struct udevice *bus, bool recurse)
185 {
186 	struct usb_bus_priv *priv;
187 	struct udevice *dev;
188 	int ret;
189 
190 	priv = dev_get_uclass_priv(bus);
191 
192 	assert(recurse);	/* TODO: Support non-recusive */
193 
194 	printf("scanning bus %d for devices... ", bus->seq);
195 	debug("\n");
196 	ret = usb_scan_device(bus, 0, USB_SPEED_FULL, &dev);
197 	if (ret)
198 		printf("failed, error %d\n", ret);
199 	else if (priv->next_addr == 0)
200 		printf("No USB Device found\n");
201 	else
202 		printf("%d USB Device(s) found\n", priv->next_addr);
203 }
204 
205 int usb_init(void)
206 {
207 	int controllers_initialized = 0;
208 	struct usb_uclass_priv *uc_priv;
209 	struct usb_bus_priv *priv;
210 	struct udevice *bus;
211 	struct uclass *uc;
212 	int count = 0;
213 	int ret;
214 
215 	asynch_allowed = 1;
216 	usb_hub_reset();
217 
218 	ret = uclass_get(UCLASS_USB, &uc);
219 	if (ret)
220 		return ret;
221 
222 	uc_priv = uc->priv;
223 
224 	uclass_foreach_dev(bus, uc) {
225 		/* init low_level USB */
226 		printf("USB%d:   ", count);
227 		count++;
228 		ret = device_probe(bus);
229 		if (ret == -ENODEV) {	/* No such device. */
230 			puts("Port not available.\n");
231 			controllers_initialized++;
232 			continue;
233 		}
234 
235 		if (ret) {		/* Other error. */
236 			printf("probe failed, error %d\n", ret);
237 			continue;
238 		}
239 		controllers_initialized++;
240 		usb_started = true;
241 	}
242 
243 	/*
244 	 * lowlevel init done, now scan the bus for devices i.e. search HUBs
245 	 * and configure them, first scan primary controllers.
246 	 */
247 	uclass_foreach_dev(bus, uc) {
248 		if (!device_active(bus))
249 			continue;
250 
251 		priv = dev_get_uclass_priv(bus);
252 		if (!priv->companion)
253 			usb_scan_bus(bus, true);
254 	}
255 
256 	/*
257 	 * Now that the primary controllers have been scanned and have handed
258 	 * over any devices they do not understand to their companions, scan
259 	 * the companions if necessary.
260 	 */
261 	if (uc_priv->companion_device_count) {
262 		uclass_foreach_dev(bus, uc) {
263 			if (!device_active(bus))
264 				continue;
265 
266 			priv = dev_get_uclass_priv(bus);
267 			if (priv->companion)
268 				usb_scan_bus(bus, true);
269 		}
270 	}
271 
272 	debug("scan end\n");
273 	/* if we were not able to find at least one working bus, bail out */
274 	if (!count)
275 		printf("No controllers found\n");
276 	else if (controllers_initialized == 0)
277 		printf("USB error: all controllers failed lowlevel init\n");
278 
279 	return usb_started ? 0 : -1;
280 }
281 
282 static struct usb_device *find_child_devnum(struct udevice *parent, int devnum)
283 {
284 	struct usb_device *udev;
285 	struct udevice *dev;
286 
287 	if (!device_active(parent))
288 		return NULL;
289 	udev = dev_get_parentdata(parent);
290 	if (udev->devnum == devnum)
291 		return udev;
292 
293 	for (device_find_first_child(parent, &dev);
294 	     dev;
295 	     device_find_next_child(&dev)) {
296 		udev = find_child_devnum(dev, devnum);
297 		if (udev)
298 			return udev;
299 	}
300 
301 	return NULL;
302 }
303 
304 struct usb_device *usb_get_dev_index(struct udevice *bus, int index)
305 {
306 	struct udevice *dev;
307 	int devnum = index + 1; /* Addresses are allocated from 1 on USB */
308 
309 	device_find_first_child(bus, &dev);
310 	if (!dev)
311 		return NULL;
312 
313 	return find_child_devnum(dev, devnum);
314 }
315 
316 int usb_post_bind(struct udevice *dev)
317 {
318 	/* Scan the bus for devices */
319 	return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
320 }
321 
322 int usb_setup_ehci_gadget(struct ehci_ctrl **ctlrp)
323 {
324 	struct usb_platdata *plat;
325 	struct udevice *dev;
326 	int ret;
327 
328 	/* Find the old device and remove it */
329 	ret = uclass_find_device_by_seq(UCLASS_USB, 0, true, &dev);
330 	if (ret)
331 		return ret;
332 	ret = device_remove(dev);
333 	if (ret)
334 		return ret;
335 
336 	plat = dev_get_platdata(dev);
337 	plat->init_type = USB_INIT_DEVICE;
338 	ret = device_probe(dev);
339 	if (ret)
340 		return ret;
341 	*ctlrp = dev_get_priv(dev);
342 
343 	return 0;
344 }
345 
346 /* returns 0 if no match, 1 if match */
347 int usb_match_device(const struct usb_device_descriptor *desc,
348 		     const struct usb_device_id *id)
349 {
350 	if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
351 	    id->idVendor != le16_to_cpu(desc->idVendor))
352 		return 0;
353 
354 	if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
355 	    id->idProduct != le16_to_cpu(desc->idProduct))
356 		return 0;
357 
358 	/* No need to test id->bcdDevice_lo != 0, since 0 is never
359 	   greater than any unsigned number. */
360 	if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
361 	    (id->bcdDevice_lo > le16_to_cpu(desc->bcdDevice)))
362 		return 0;
363 
364 	if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
365 	    (id->bcdDevice_hi < le16_to_cpu(desc->bcdDevice)))
366 		return 0;
367 
368 	if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
369 	    (id->bDeviceClass != desc->bDeviceClass))
370 		return 0;
371 
372 	if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
373 	    (id->bDeviceSubClass != desc->bDeviceSubClass))
374 		return 0;
375 
376 	if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
377 	    (id->bDeviceProtocol != desc->bDeviceProtocol))
378 		return 0;
379 
380 	return 1;
381 }
382 
383 /* returns 0 if no match, 1 if match */
384 int usb_match_one_id_intf(const struct usb_device_descriptor *desc,
385 			  const struct usb_interface_descriptor *int_desc,
386 			  const struct usb_device_id *id)
387 {
388 	/* The interface class, subclass, protocol and number should never be
389 	 * checked for a match if the device class is Vendor Specific,
390 	 * unless the match record specifies the Vendor ID. */
391 	if (desc->bDeviceClass == USB_CLASS_VENDOR_SPEC &&
392 	    !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
393 	    (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
394 				USB_DEVICE_ID_MATCH_INT_SUBCLASS |
395 				USB_DEVICE_ID_MATCH_INT_PROTOCOL |
396 				USB_DEVICE_ID_MATCH_INT_NUMBER)))
397 		return 0;
398 
399 	if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
400 	    (id->bInterfaceClass != int_desc->bInterfaceClass))
401 		return 0;
402 
403 	if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
404 	    (id->bInterfaceSubClass != int_desc->bInterfaceSubClass))
405 		return 0;
406 
407 	if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
408 	    (id->bInterfaceProtocol != int_desc->bInterfaceProtocol))
409 		return 0;
410 
411 	if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
412 	    (id->bInterfaceNumber != int_desc->bInterfaceNumber))
413 		return 0;
414 
415 	return 1;
416 }
417 
418 /* returns 0 if no match, 1 if match */
419 int usb_match_one_id(struct usb_device_descriptor *desc,
420 		     struct usb_interface_descriptor *int_desc,
421 		     const struct usb_device_id *id)
422 {
423 	if (!usb_match_device(desc, id))
424 		return 0;
425 
426 	return usb_match_one_id_intf(desc, int_desc, id);
427 }
428 
429 /**
430  * usb_find_and_bind_driver() - Find and bind the right USB driver
431  *
432  * This only looks at certain fields in the descriptor.
433  */
434 static int usb_find_and_bind_driver(struct udevice *parent,
435 				    struct usb_device_descriptor *desc,
436 				    struct usb_interface_descriptor *iface,
437 				    int bus_seq, int devnum,
438 				    struct udevice **devp)
439 {
440 	struct usb_driver_entry *start, *entry;
441 	int n_ents;
442 	int ret;
443 	char name[30], *str;
444 
445 	*devp = NULL;
446 	debug("%s: Searching for driver\n", __func__);
447 	start = ll_entry_start(struct usb_driver_entry, usb_driver_entry);
448 	n_ents = ll_entry_count(struct usb_driver_entry, usb_driver_entry);
449 	for (entry = start; entry != start + n_ents; entry++) {
450 		const struct usb_device_id *id;
451 		struct udevice *dev;
452 		const struct driver *drv;
453 		struct usb_dev_platdata *plat;
454 
455 		for (id = entry->match; id->match_flags; id++) {
456 			if (!usb_match_one_id(desc, iface, id))
457 				continue;
458 
459 			drv = entry->driver;
460 			/*
461 			 * We could pass the descriptor to the driver as
462 			 * platdata (instead of NULL) and allow its bind()
463 			 * method to return -ENOENT if it doesn't support this
464 			 * device. That way we could continue the search to
465 			 * find another driver. For now this doesn't seem
466 			 * necesssary, so just bind the first match.
467 			 */
468 			ret = device_bind(parent, drv, drv->name, NULL, -1,
469 					  &dev);
470 			if (ret)
471 				goto error;
472 			debug("%s: Match found: %s\n", __func__, drv->name);
473 			dev->driver_data = id->driver_info;
474 			plat = dev_get_parent_platdata(dev);
475 			plat->id = *id;
476 			*devp = dev;
477 			return 0;
478 		}
479 	}
480 
481 	/* Bind a generic driver so that the device can be used */
482 	snprintf(name, sizeof(name), "generic_bus_%x_dev_%x", bus_seq, devnum);
483 	str = strdup(name);
484 	if (!str)
485 		return -ENOMEM;
486 	ret = device_bind_driver(parent, "usb_dev_generic_drv", str, devp);
487 
488 error:
489 	debug("%s: No match found: %d\n", __func__, ret);
490 	return ret;
491 }
492 
493 /**
494  * usb_find_emul_child() - Find an existing device for emulated devices
495  */
496 static int usb_find_emul_child(struct udevice *parent,
497 			       struct usb_device_descriptor *desc,
498 			       struct usb_interface_descriptor *iface,
499 			       struct udevice **devp)
500 {
501 #ifdef CONFIG_SANDBOX
502 	struct udevice *dev;
503 
504 	*devp = NULL;
505 	for (device_find_first_child(parent, &dev);
506 	     dev;
507 	     device_find_next_child(&dev)) {
508 		struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
509 
510 		/* If this device is already in use, skip it */
511 		if (device_active(dev))
512 			continue;
513 		debug("   %s: name='%s', plat=%d, desc=%d\n", __func__,
514 		      dev->name, plat->id.bDeviceClass, desc->bDeviceClass);
515 		if (usb_match_one_id(desc, iface, &plat->id)) {
516 			*devp = dev;
517 			return 0;
518 		}
519 	}
520 #endif
521 	return -ENOENT;
522 }
523 
524 int usb_scan_device(struct udevice *parent, int port,
525 		    enum usb_device_speed speed, struct udevice **devp)
526 {
527 	struct udevice *dev;
528 	bool created = false;
529 	struct usb_dev_platdata *plat;
530 	struct usb_bus_priv *priv;
531 	struct usb_device *parent_udev;
532 	int ret;
533 	ALLOC_CACHE_ALIGN_BUFFER(struct usb_device, udev, 1);
534 	struct usb_interface_descriptor *iface = &udev->config.if_desc[0].desc;
535 
536 	*devp = NULL;
537 	memset(udev, '\0', sizeof(*udev));
538 	udev->controller_dev = usb_get_bus(parent);
539 	priv = dev_get_uclass_priv(udev->controller_dev);
540 
541 	/*
542 	 * Somewhat nasty, this. We create a local device and use the normal
543 	 * USB stack to read its descriptor. Then we know what type of device
544 	 * to create for real.
545 	 *
546 	 * udev->dev is set to the parent, since we don't have a real device
547 	 * yet. The USB stack should not access udev.dev anyway, except perhaps
548 	 * to find the controller, and the controller will either be @parent,
549 	 * or some parent of @parent.
550 	 *
551 	 * Another option might be to create the device as a generic USB
552 	 * device, then morph it into the correct one when we know what it
553 	 * should be. This means that a generic USB device would morph into
554 	 * a network controller, or a USB flash stick, for example. However,
555 	 * we don't support such morphing and it isn't clear that it would
556 	 * be easy to do.
557 	 *
558 	 * Yet another option is to split out the USB stack parts of udev
559 	 * into something like a 'struct urb' (as Linux does) which can exist
560 	 * independently of any device. This feels cleaner, but calls for quite
561 	 * a big change to the USB stack.
562 	 *
563 	 * For now, the approach is to set up an empty udev, read its
564 	 * descriptor and assign it an address, then bind a real device and
565 	 * stash the resulting information into the device's parent
566 	 * platform data. Then when we probe it, usb_child_pre_probe() is called
567 	 * and it will pull the information out of the stash.
568 	 */
569 	udev->dev = parent;
570 	udev->speed = speed;
571 	udev->devnum = priv->next_addr + 1;
572 	udev->portnr = port;
573 	debug("Calling usb_setup_device(), portnr=%d\n", udev->portnr);
574 	parent_udev = device_get_uclass_id(parent) == UCLASS_USB_HUB ?
575 		dev_get_parentdata(parent) : NULL;
576 	ret = usb_setup_device(udev, priv->desc_before_addr, parent_udev);
577 	debug("read_descriptor for '%s': ret=%d\n", parent->name, ret);
578 	if (ret)
579 		return ret;
580 	ret = usb_find_emul_child(parent, &udev->descriptor, iface, &dev);
581 	debug("** usb_find_emul_child returns %d\n", ret);
582 	if (ret) {
583 		if (ret != -ENOENT)
584 			return ret;
585 		ret = usb_find_and_bind_driver(parent, &udev->descriptor, iface,
586 					       udev->controller_dev->seq,
587 					       udev->devnum, &dev);
588 		if (ret)
589 			return ret;
590 		created = true;
591 	}
592 	plat = dev_get_parent_platdata(dev);
593 	debug("%s: Probing '%s', plat=%p\n", __func__, dev->name, plat);
594 	plat->devnum = udev->devnum;
595 	plat->udev = udev;
596 	priv->next_addr++;
597 	ret = device_probe(dev);
598 	if (ret) {
599 		debug("%s: Device '%s' probe failed\n", __func__, dev->name);
600 		priv->next_addr--;
601 		if (created)
602 			device_unbind(dev);
603 		return ret;
604 	}
605 	*devp = dev;
606 
607 	return 0;
608 }
609 
610 /*
611  * Detect if a USB device has been plugged or unplugged.
612  */
613 int usb_detect_change(void)
614 {
615 	struct udevice *hub;
616 	struct uclass *uc;
617 	int change = 0;
618 	int ret;
619 
620 	ret = uclass_get(UCLASS_USB_HUB, &uc);
621 	if (ret)
622 		return ret;
623 
624 	uclass_foreach_dev(hub, uc) {
625 		struct usb_device *udev;
626 		struct udevice *dev;
627 
628 		if (!device_active(hub))
629 			continue;
630 		for (device_find_first_child(hub, &dev);
631 		     dev;
632 		     device_find_next_child(&dev)) {
633 			struct usb_port_status status;
634 
635 			if (!device_active(dev))
636 				continue;
637 
638 			udev = dev_get_parentdata(dev);
639 			if (usb_get_port_status(udev, udev->portnr, &status)
640 					< 0)
641 				/* USB request failed */
642 				continue;
643 
644 			if (le16_to_cpu(status.wPortChange) &
645 			    USB_PORT_STAT_C_CONNECTION)
646 				change++;
647 		}
648 	}
649 
650 	return change;
651 }
652 
653 int usb_child_post_bind(struct udevice *dev)
654 {
655 	struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
656 	const void *blob = gd->fdt_blob;
657 	int val;
658 
659 	if (dev->of_offset == -1)
660 		return 0;
661 
662 	/* We only support matching a few things */
663 	val = fdtdec_get_int(blob, dev->of_offset, "usb,device-class", -1);
664 	if (val != -1) {
665 		plat->id.match_flags |= USB_DEVICE_ID_MATCH_DEV_CLASS;
666 		plat->id.bDeviceClass = val;
667 	}
668 	val = fdtdec_get_int(blob, dev->of_offset, "usb,interface-class", -1);
669 	if (val != -1) {
670 		plat->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
671 		plat->id.bInterfaceClass = val;
672 	}
673 
674 	return 0;
675 }
676 
677 struct udevice *usb_get_bus(struct udevice *dev)
678 {
679 	struct udevice *bus;
680 
681 	for (bus = dev; bus && device_get_uclass_id(bus) != UCLASS_USB; )
682 		bus = bus->parent;
683 	if (!bus) {
684 		/* By design this cannot happen */
685 		assert(bus);
686 		debug("USB HUB '%s' does not have a controller\n", dev->name);
687 	}
688 
689 	return bus;
690 }
691 
692 int usb_child_pre_probe(struct udevice *dev)
693 {
694 	struct usb_device *udev = dev_get_parentdata(dev);
695 	struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
696 	int ret;
697 
698 	if (plat->udev) {
699 		/*
700 		 * Copy over all the values set in the on stack struct
701 		 * usb_device in usb_scan_device() to our final struct
702 		 * usb_device for this dev.
703 		 */
704 		*udev = *(plat->udev);
705 		/* And clear plat->udev as it will not be valid for long */
706 		plat->udev = NULL;
707 		udev->dev = dev;
708 	} else {
709 		/*
710 		 * This happens with devices which are explicitly bound
711 		 * instead of being discovered through usb_scan_device()
712 		 * such as sandbox emul devices.
713 		 */
714 		udev->dev = dev;
715 		udev->controller_dev = usb_get_bus(dev);
716 		udev->devnum = plat->devnum;
717 
718 		/*
719 		 * udev did not go through usb_scan_device(), so we need to
720 		 * select the config and read the config descriptors.
721 		 */
722 		ret = usb_select_config(udev);
723 		if (ret)
724 			return ret;
725 	}
726 
727 	return 0;
728 }
729 
730 UCLASS_DRIVER(usb) = {
731 	.id		= UCLASS_USB,
732 	.name		= "usb",
733 	.flags		= DM_UC_FLAG_SEQ_ALIAS,
734 	.post_bind	= usb_post_bind,
735 	.priv_auto_alloc_size = sizeof(struct usb_uclass_priv),
736 	.per_child_auto_alloc_size = sizeof(struct usb_device),
737 	.per_device_auto_alloc_size = sizeof(struct usb_bus_priv),
738 	.child_post_bind = usb_child_post_bind,
739 	.child_pre_probe = usb_child_pre_probe,
740 	.per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),
741 };
742 
743 UCLASS_DRIVER(usb_dev_generic) = {
744 	.id		= UCLASS_USB_DEV_GENERIC,
745 	.name		= "usb_dev_generic",
746 };
747 
748 U_BOOT_DRIVER(usb_dev_generic_drv) = {
749 	.id		= UCLASS_USB_DEV_GENERIC,
750 	.name		= "usb_dev_generic_drv",
751 };
752