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