usb-uclass.c 20 KB

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