usb-uclass.c 20 KB

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