usb-uclass.c 20 KB

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