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