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