usb-uclass.c 19 KB

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