usb-uclass.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  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. #include <common.h>
  10. #include <dm.h>
  11. #include <errno.h>
  12. #include <usb.h>
  13. #include <dm/device-internal.h>
  14. #include <dm/lists.h>
  15. #include <dm/root.h>
  16. #include <dm/uclass-internal.h>
  17. DECLARE_GLOBAL_DATA_PTR;
  18. extern bool usb_started; /* flag for the started/stopped USB status */
  19. static bool asynch_allowed;
  20. struct usb_uclass_priv {
  21. int companion_device_count;
  22. };
  23. int usb_disable_asynch(int disable)
  24. {
  25. int old_value = asynch_allowed;
  26. asynch_allowed = !disable;
  27. return old_value;
  28. }
  29. int submit_int_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
  30. int length, int interval)
  31. {
  32. struct udevice *bus = udev->controller_dev;
  33. struct dm_usb_ops *ops = usb_get_ops(bus);
  34. if (!ops->interrupt)
  35. return -ENOSYS;
  36. return ops->interrupt(bus, udev, pipe, buffer, length, interval);
  37. }
  38. int submit_control_msg(struct usb_device *udev, unsigned long pipe,
  39. void *buffer, int length, struct devrequest *setup)
  40. {
  41. struct udevice *bus = udev->controller_dev;
  42. struct dm_usb_ops *ops = usb_get_ops(bus);
  43. struct usb_uclass_priv *uc_priv = bus->uclass->priv;
  44. int err;
  45. if (!ops->control)
  46. return -ENOSYS;
  47. err = ops->control(bus, udev, pipe, buffer, length, setup);
  48. if (setup->request == USB_REQ_SET_FEATURE &&
  49. setup->requesttype == USB_RT_PORT &&
  50. setup->value == cpu_to_le16(USB_PORT_FEAT_RESET) &&
  51. err == -ENXIO) {
  52. /* Device handed over to companion after port reset */
  53. uc_priv->companion_device_count++;
  54. }
  55. return err;
  56. }
  57. int submit_bulk_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
  58. int length)
  59. {
  60. struct udevice *bus = udev->controller_dev;
  61. struct dm_usb_ops *ops = usb_get_ops(bus);
  62. if (!ops->bulk)
  63. return -ENOSYS;
  64. return ops->bulk(bus, udev, pipe, buffer, length);
  65. }
  66. struct int_queue *create_int_queue(struct usb_device *udev,
  67. unsigned long pipe, int queuesize, int elementsize,
  68. void *buffer, int interval)
  69. {
  70. struct udevice *bus = udev->controller_dev;
  71. struct dm_usb_ops *ops = usb_get_ops(bus);
  72. if (!ops->create_int_queue)
  73. return NULL;
  74. return ops->create_int_queue(bus, udev, pipe, queuesize, elementsize,
  75. buffer, interval);
  76. }
  77. void *poll_int_queue(struct usb_device *udev, struct int_queue *queue)
  78. {
  79. struct udevice *bus = udev->controller_dev;
  80. struct dm_usb_ops *ops = usb_get_ops(bus);
  81. if (!ops->poll_int_queue)
  82. return NULL;
  83. return ops->poll_int_queue(bus, udev, queue);
  84. }
  85. int destroy_int_queue(struct usb_device *udev, struct int_queue *queue)
  86. {
  87. struct udevice *bus = udev->controller_dev;
  88. struct dm_usb_ops *ops = usb_get_ops(bus);
  89. if (!ops->destroy_int_queue)
  90. return -ENOSYS;
  91. return ops->destroy_int_queue(bus, udev, queue);
  92. }
  93. int usb_alloc_device(struct usb_device *udev)
  94. {
  95. struct udevice *bus = udev->controller_dev;
  96. struct dm_usb_ops *ops = usb_get_ops(bus);
  97. /* This is only requird by some controllers - current XHCI */
  98. if (!ops->alloc_device)
  99. return 0;
  100. return ops->alloc_device(bus, udev);
  101. }
  102. int usb_stop(void)
  103. {
  104. struct udevice *bus;
  105. struct uclass *uc;
  106. struct usb_uclass_priv *uc_priv;
  107. int err = 0, ret;
  108. /* De-activate any devices that have been activated */
  109. ret = uclass_get(UCLASS_USB, &uc);
  110. if (ret)
  111. return ret;
  112. uc_priv = uc->priv;
  113. uclass_foreach_dev(bus, uc) {
  114. ret = device_remove(bus);
  115. if (ret && !err)
  116. err = ret;
  117. }
  118. #ifdef CONFIG_SANDBOX
  119. struct udevice *dev;
  120. /* Reset all enulation devices */
  121. ret = uclass_get(UCLASS_USB_EMUL, &uc);
  122. if (ret)
  123. return ret;
  124. uclass_foreach_dev(dev, uc)
  125. usb_emul_reset(dev);
  126. #endif
  127. usb_stor_reset();
  128. usb_hub_reset();
  129. uc_priv->companion_device_count = 0;
  130. usb_started = 0;
  131. return err;
  132. }
  133. static void usb_scan_bus(struct udevice *bus, bool recurse)
  134. {
  135. struct usb_bus_priv *priv;
  136. struct udevice *dev;
  137. int ret;
  138. priv = dev_get_uclass_priv(bus);
  139. assert(recurse); /* TODO: Support non-recusive */
  140. printf("scanning bus %d for devices... ", bus->seq);
  141. debug("\n");
  142. ret = usb_scan_device(bus, 0, USB_SPEED_FULL, &dev);
  143. if (ret)
  144. printf("failed, error %d\n", ret);
  145. else if (priv->next_addr == 0)
  146. printf("No USB Device found\n");
  147. else
  148. printf("%d USB Device(s) found\n", priv->next_addr);
  149. }
  150. int usb_init(void)
  151. {
  152. int controllers_initialized = 0;
  153. struct usb_uclass_priv *uc_priv;
  154. struct usb_bus_priv *priv;
  155. struct udevice *bus;
  156. struct uclass *uc;
  157. int count = 0;
  158. int ret;
  159. asynch_allowed = 1;
  160. usb_hub_reset();
  161. ret = uclass_get(UCLASS_USB, &uc);
  162. if (ret)
  163. return ret;
  164. uc_priv = uc->priv;
  165. uclass_foreach_dev(bus, uc) {
  166. /* init low_level USB */
  167. printf("USB%d: ", count);
  168. count++;
  169. ret = device_probe(bus);
  170. if (ret == -ENODEV) { /* No such device. */
  171. puts("Port not available.\n");
  172. controllers_initialized++;
  173. continue;
  174. }
  175. if (ret) { /* Other error. */
  176. printf("probe failed, error %d\n", ret);
  177. continue;
  178. }
  179. controllers_initialized++;
  180. usb_started = true;
  181. }
  182. /*
  183. * lowlevel init done, now scan the bus for devices i.e. search HUBs
  184. * and configure them, first scan primary controllers.
  185. */
  186. uclass_foreach_dev(bus, uc) {
  187. if (!device_active(bus))
  188. continue;
  189. priv = dev_get_uclass_priv(bus);
  190. if (!priv->companion)
  191. usb_scan_bus(bus, true);
  192. }
  193. /*
  194. * Now that the primary controllers have been scanned and have handed
  195. * over any devices they do not understand to their companions, scan
  196. * the companions if necessary.
  197. */
  198. if (uc_priv->companion_device_count) {
  199. uclass_foreach_dev(bus, uc) {
  200. if (!device_active(bus))
  201. continue;
  202. priv = dev_get_uclass_priv(bus);
  203. if (priv->companion)
  204. usb_scan_bus(bus, true);
  205. }
  206. }
  207. debug("scan end\n");
  208. /* if we were not able to find at least one working bus, bail out */
  209. if (!count)
  210. printf("No controllers found\n");
  211. else if (controllers_initialized == 0)
  212. printf("USB error: all controllers failed lowlevel init\n");
  213. return usb_started ? 0 : -1;
  214. }
  215. int usb_reset_root_port(void)
  216. {
  217. return -ENOSYS;
  218. }
  219. static struct usb_device *find_child_devnum(struct udevice *parent, int devnum)
  220. {
  221. struct usb_device *udev;
  222. struct udevice *dev;
  223. if (!device_active(parent))
  224. return NULL;
  225. udev = dev_get_parentdata(parent);
  226. if (udev->devnum == devnum)
  227. return udev;
  228. for (device_find_first_child(parent, &dev);
  229. dev;
  230. device_find_next_child(&dev)) {
  231. udev = find_child_devnum(dev, devnum);
  232. if (udev)
  233. return udev;
  234. }
  235. return NULL;
  236. }
  237. struct usb_device *usb_get_dev_index(struct udevice *bus, int index)
  238. {
  239. struct udevice *hub;
  240. int devnum = index + 1; /* Addresses are allocated from 1 on USB */
  241. device_find_first_child(bus, &hub);
  242. if (device_get_uclass_id(hub) == UCLASS_USB_HUB)
  243. return find_child_devnum(hub, devnum);
  244. return NULL;
  245. }
  246. int usb_post_bind(struct udevice *dev)
  247. {
  248. /* Scan the bus for devices */
  249. return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
  250. }
  251. int usb_port_reset(struct usb_device *parent, int portnr)
  252. {
  253. unsigned short portstatus;
  254. int ret;
  255. debug("%s: start\n", __func__);
  256. if (parent) {
  257. /* reset the port for the second time */
  258. assert(portnr > 0);
  259. debug("%s: reset %d\n", __func__, portnr - 1);
  260. ret = legacy_hub_port_reset(parent, portnr - 1, &portstatus);
  261. if (ret < 0) {
  262. printf("\n Couldn't reset port %i\n", portnr);
  263. return ret;
  264. }
  265. } else {
  266. debug("%s: reset root\n", __func__);
  267. usb_reset_root_port();
  268. }
  269. return 0;
  270. }
  271. int usb_legacy_port_reset(struct usb_device *parent, int portnr)
  272. {
  273. return usb_port_reset(parent, portnr);
  274. }
  275. int usb_setup_ehci_gadget(struct ehci_ctrl **ctlrp)
  276. {
  277. struct usb_platdata *plat;
  278. struct udevice *dev;
  279. int ret;
  280. /* Find the old device and remove it */
  281. ret = uclass_find_device_by_seq(UCLASS_USB, 0, true, &dev);
  282. if (ret)
  283. return ret;
  284. ret = device_remove(dev);
  285. if (ret)
  286. return ret;
  287. plat = dev_get_platdata(dev);
  288. plat->init_type = USB_INIT_DEVICE;
  289. ret = device_probe(dev);
  290. if (ret)
  291. return ret;
  292. *ctlrp = dev_get_priv(dev);
  293. return 0;
  294. }
  295. /* returns 0 if no match, 1 if match */
  296. int usb_match_device(const struct usb_device_descriptor *desc,
  297. const struct usb_device_id *id)
  298. {
  299. if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
  300. id->idVendor != le16_to_cpu(desc->idVendor))
  301. return 0;
  302. if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
  303. id->idProduct != le16_to_cpu(desc->idProduct))
  304. return 0;
  305. /* No need to test id->bcdDevice_lo != 0, since 0 is never
  306. greater than any unsigned number. */
  307. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
  308. (id->bcdDevice_lo > le16_to_cpu(desc->bcdDevice)))
  309. return 0;
  310. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
  311. (id->bcdDevice_hi < le16_to_cpu(desc->bcdDevice)))
  312. return 0;
  313. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
  314. (id->bDeviceClass != desc->bDeviceClass))
  315. return 0;
  316. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
  317. (id->bDeviceSubClass != desc->bDeviceSubClass))
  318. return 0;
  319. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
  320. (id->bDeviceProtocol != desc->bDeviceProtocol))
  321. return 0;
  322. return 1;
  323. }
  324. /* returns 0 if no match, 1 if match */
  325. int usb_match_one_id_intf(const struct usb_device_descriptor *desc,
  326. const struct usb_interface_descriptor *int_desc,
  327. const struct usb_device_id *id)
  328. {
  329. /* The interface class, subclass, protocol and number should never be
  330. * checked for a match if the device class is Vendor Specific,
  331. * unless the match record specifies the Vendor ID. */
  332. if (desc->bDeviceClass == USB_CLASS_VENDOR_SPEC &&
  333. !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
  334. (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
  335. USB_DEVICE_ID_MATCH_INT_SUBCLASS |
  336. USB_DEVICE_ID_MATCH_INT_PROTOCOL |
  337. USB_DEVICE_ID_MATCH_INT_NUMBER)))
  338. return 0;
  339. if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
  340. (id->bInterfaceClass != int_desc->bInterfaceClass))
  341. return 0;
  342. if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
  343. (id->bInterfaceSubClass != int_desc->bInterfaceSubClass))
  344. return 0;
  345. if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
  346. (id->bInterfaceProtocol != int_desc->bInterfaceProtocol))
  347. return 0;
  348. if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
  349. (id->bInterfaceNumber != int_desc->bInterfaceNumber))
  350. return 0;
  351. return 1;
  352. }
  353. /* returns 0 if no match, 1 if match */
  354. int usb_match_one_id(struct usb_device_descriptor *desc,
  355. struct usb_interface_descriptor *int_desc,
  356. const struct usb_device_id *id)
  357. {
  358. if (!usb_match_device(desc, id))
  359. return 0;
  360. return usb_match_one_id_intf(desc, int_desc, id);
  361. }
  362. /**
  363. * usb_find_and_bind_driver() - Find and bind the right USB driver
  364. *
  365. * This only looks at certain fields in the descriptor.
  366. */
  367. static int usb_find_and_bind_driver(struct udevice *parent,
  368. struct usb_device_descriptor *desc,
  369. struct usb_interface_descriptor *iface,
  370. int bus_seq, int devnum,
  371. struct udevice **devp)
  372. {
  373. struct usb_driver_entry *start, *entry;
  374. int n_ents;
  375. int ret;
  376. char name[30], *str;
  377. *devp = NULL;
  378. debug("%s: Searching for driver\n", __func__);
  379. start = ll_entry_start(struct usb_driver_entry, usb_driver_entry);
  380. n_ents = ll_entry_count(struct usb_driver_entry, usb_driver_entry);
  381. for (entry = start; entry != start + n_ents; entry++) {
  382. const struct usb_device_id *id;
  383. struct udevice *dev;
  384. const struct driver *drv;
  385. struct usb_dev_platdata *plat;
  386. for (id = entry->match; id->match_flags; id++) {
  387. if (!usb_match_one_id(desc, iface, id))
  388. continue;
  389. drv = entry->driver;
  390. /*
  391. * We could pass the descriptor to the driver as
  392. * platdata (instead of NULL) and allow its bind()
  393. * method to return -ENOENT if it doesn't support this
  394. * device. That way we could continue the search to
  395. * find another driver. For now this doesn't seem
  396. * necesssary, so just bind the first match.
  397. */
  398. ret = device_bind(parent, drv, drv->name, NULL, -1,
  399. &dev);
  400. if (ret)
  401. goto error;
  402. debug("%s: Match found: %s\n", __func__, drv->name);
  403. dev->driver_data = id->driver_info;
  404. plat = dev_get_parent_platdata(dev);
  405. plat->id = *id;
  406. *devp = dev;
  407. return 0;
  408. }
  409. }
  410. /* Bind a generic driver so that the device can be used */
  411. snprintf(name, sizeof(name), "generic_bus_%x_dev_%x", bus_seq, devnum);
  412. str = strdup(name);
  413. if (!str)
  414. return -ENOMEM;
  415. ret = device_bind_driver(parent, "usb_dev_generic_drv", str, devp);
  416. error:
  417. debug("%s: No match found: %d\n", __func__, ret);
  418. return ret;
  419. }
  420. /**
  421. * usb_find_child() - Find an existing device which matches our needs
  422. *
  423. *
  424. */
  425. static int usb_find_child(struct udevice *parent,
  426. struct usb_device_descriptor *desc,
  427. struct usb_interface_descriptor *iface,
  428. struct udevice **devp)
  429. {
  430. struct udevice *dev;
  431. *devp = NULL;
  432. for (device_find_first_child(parent, &dev);
  433. dev;
  434. device_find_next_child(&dev)) {
  435. struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
  436. /* If this device is already in use, skip it */
  437. if (device_active(dev))
  438. continue;
  439. debug(" %s: name='%s', plat=%d, desc=%d\n", __func__,
  440. dev->name, plat->id.bDeviceClass, desc->bDeviceClass);
  441. if (usb_match_one_id(desc, iface, &plat->id)) {
  442. *devp = dev;
  443. return 0;
  444. }
  445. }
  446. return -ENOENT;
  447. }
  448. int usb_scan_device(struct udevice *parent, int port,
  449. enum usb_device_speed speed, struct udevice **devp)
  450. {
  451. struct udevice *dev;
  452. bool created = false;
  453. struct usb_dev_platdata *plat;
  454. struct usb_bus_priv *priv;
  455. struct usb_device *parent_udev;
  456. int ret;
  457. ALLOC_CACHE_ALIGN_BUFFER(struct usb_device, udev, 1);
  458. struct usb_interface_descriptor *iface = &udev->config.if_desc[0].desc;
  459. *devp = NULL;
  460. memset(udev, '\0', sizeof(*udev));
  461. udev->controller_dev = usb_get_bus(parent);
  462. priv = dev_get_uclass_priv(udev->controller_dev);
  463. /*
  464. * Somewhat nasty, this. We create a local device and use the normal
  465. * USB stack to read its descriptor. Then we know what type of device
  466. * to create for real.
  467. *
  468. * udev->dev is set to the parent, since we don't have a real device
  469. * yet. The USB stack should not access udev.dev anyway, except perhaps
  470. * to find the controller, and the controller will either be @parent,
  471. * or some parent of @parent.
  472. *
  473. * Another option might be to create the device as a generic USB
  474. * device, then morph it into the correct one when we know what it
  475. * should be. This means that a generic USB device would morph into
  476. * a network controller, or a USB flash stick, for example. However,
  477. * we don't support such morphing and it isn't clear that it would
  478. * be easy to do.
  479. *
  480. * Yet another option is to split out the USB stack parts of udev
  481. * into something like a 'struct urb' (as Linux does) which can exist
  482. * independently of any device. This feels cleaner, but calls for quite
  483. * a big change to the USB stack.
  484. *
  485. * For now, the approach is to set up an empty udev, read its
  486. * descriptor and assign it an address, then bind a real device and
  487. * stash the resulting information into the device's parent
  488. * platform data. Then when we probe it, usb_child_pre_probe() is called
  489. * and it will pull the information out of the stash.
  490. */
  491. udev->dev = parent;
  492. udev->speed = speed;
  493. udev->devnum = priv->next_addr + 1;
  494. udev->portnr = port;
  495. debug("Calling usb_setup_device(), portnr=%d\n", udev->portnr);
  496. parent_udev = device_get_uclass_id(parent) == UCLASS_USB_HUB ?
  497. dev_get_parentdata(parent) : NULL;
  498. ret = usb_setup_device(udev, priv->desc_before_addr, parent_udev, port);
  499. debug("read_descriptor for '%s': ret=%d\n", parent->name, ret);
  500. if (ret)
  501. return ret;
  502. ret = usb_find_child(parent, &udev->descriptor, iface, &dev);
  503. debug("** usb_find_child returns %d\n", ret);
  504. if (ret) {
  505. if (ret != -ENOENT)
  506. return ret;
  507. ret = usb_find_and_bind_driver(parent, &udev->descriptor, iface,
  508. udev->controller_dev->seq,
  509. udev->devnum, &dev);
  510. if (ret)
  511. return ret;
  512. created = true;
  513. }
  514. plat = dev_get_parent_platdata(dev);
  515. debug("%s: Probing '%s', plat=%p\n", __func__, dev->name, plat);
  516. plat->devnum = udev->devnum;
  517. plat->udev = udev;
  518. priv->next_addr++;
  519. ret = device_probe(dev);
  520. if (ret) {
  521. debug("%s: Device '%s' probe failed\n", __func__, dev->name);
  522. priv->next_addr--;
  523. if (created)
  524. device_unbind(dev);
  525. return ret;
  526. }
  527. *devp = dev;
  528. return 0;
  529. }
  530. int usb_child_post_bind(struct udevice *dev)
  531. {
  532. struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
  533. const void *blob = gd->fdt_blob;
  534. int val;
  535. if (dev->of_offset == -1)
  536. return 0;
  537. /* We only support matching a few things */
  538. val = fdtdec_get_int(blob, dev->of_offset, "usb,device-class", -1);
  539. if (val != -1) {
  540. plat->id.match_flags |= USB_DEVICE_ID_MATCH_DEV_CLASS;
  541. plat->id.bDeviceClass = val;
  542. }
  543. val = fdtdec_get_int(blob, dev->of_offset, "usb,interface-class", -1);
  544. if (val != -1) {
  545. plat->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
  546. plat->id.bInterfaceClass = val;
  547. }
  548. return 0;
  549. }
  550. struct udevice *usb_get_bus(struct udevice *dev)
  551. {
  552. struct udevice *bus;
  553. for (bus = dev; bus && device_get_uclass_id(bus) != UCLASS_USB; )
  554. bus = bus->parent;
  555. if (!bus) {
  556. /* By design this cannot happen */
  557. assert(bus);
  558. debug("USB HUB '%s' does not have a controller\n", dev->name);
  559. }
  560. return bus;
  561. }
  562. int usb_child_pre_probe(struct udevice *dev)
  563. {
  564. struct usb_device *udev = dev_get_parentdata(dev);
  565. struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
  566. int ret;
  567. if (plat->udev) {
  568. /*
  569. * Copy over all the values set in the on stack struct
  570. * usb_device in usb_scan_device() to our final struct
  571. * usb_device for this dev.
  572. */
  573. *udev = *(plat->udev);
  574. /* And clear plat->udev as it will not be valid for long */
  575. plat->udev = NULL;
  576. udev->dev = dev;
  577. } else {
  578. /*
  579. * This happens with devices which are explicitly bound
  580. * instead of being discovered through usb_scan_device()
  581. * such as sandbox emul devices.
  582. */
  583. udev->dev = dev;
  584. udev->controller_dev = usb_get_bus(dev);
  585. udev->devnum = plat->devnum;
  586. /*
  587. * udev did not go through usb_scan_device(), so we need to
  588. * select the config and read the config descriptors.
  589. */
  590. ret = usb_select_config(udev);
  591. if (ret)
  592. return ret;
  593. }
  594. return 0;
  595. }
  596. UCLASS_DRIVER(usb) = {
  597. .id = UCLASS_USB,
  598. .name = "usb",
  599. .flags = DM_UC_FLAG_SEQ_ALIAS,
  600. .post_bind = usb_post_bind,
  601. .priv_auto_alloc_size = sizeof(struct usb_uclass_priv),
  602. .per_child_auto_alloc_size = sizeof(struct usb_device),
  603. .per_device_auto_alloc_size = sizeof(struct usb_bus_priv),
  604. .child_post_bind = usb_child_post_bind,
  605. .child_pre_probe = usb_child_pre_probe,
  606. .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),
  607. };
  608. UCLASS_DRIVER(usb_dev_generic) = {
  609. .id = UCLASS_USB_DEV_GENERIC,
  610. .name = "usb_dev_generic",
  611. };
  612. U_BOOT_DRIVER(usb_dev_generic_drv) = {
  613. .id = UCLASS_USB_DEV_GENERIC,
  614. .name = "usb_dev_generic_drv",
  615. };