usb-uclass.c 19 KB

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