usb-uclass.c 20 KB

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