usb-uclass.c 21 KB

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