usb-uclass.c 21 KB

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