usb-uclass.c 20 KB

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