usb-uclass.c 20 KB

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