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