usb-uclass.c 20 KB

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