usb-uclass.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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 <usb.h>
  13. #include <dm/device-internal.h>
  14. #include <dm/lists.h>
  15. #include <dm/root.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. int usb_disable_asynch(int disable)
  21. {
  22. int old_value = asynch_allowed;
  23. asynch_allowed = !disable;
  24. return old_value;
  25. }
  26. int submit_int_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
  27. int length, int interval)
  28. {
  29. struct udevice *bus = udev->controller_dev;
  30. struct dm_usb_ops *ops = usb_get_ops(bus);
  31. if (!ops->interrupt)
  32. return -ENOSYS;
  33. return ops->interrupt(bus, udev, pipe, buffer, length, interval);
  34. }
  35. int submit_control_msg(struct usb_device *udev, unsigned long pipe,
  36. void *buffer, int length, struct devrequest *setup)
  37. {
  38. struct udevice *bus = udev->controller_dev;
  39. struct dm_usb_ops *ops = usb_get_ops(bus);
  40. if (!ops->control)
  41. return -ENOSYS;
  42. return ops->control(bus, udev, pipe, buffer, length, setup);
  43. }
  44. int submit_bulk_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
  45. int length)
  46. {
  47. struct udevice *bus = udev->controller_dev;
  48. struct dm_usb_ops *ops = usb_get_ops(bus);
  49. if (!ops->bulk)
  50. return -ENOSYS;
  51. return ops->bulk(bus, udev, pipe, buffer, length);
  52. }
  53. int usb_alloc_device(struct usb_device *udev)
  54. {
  55. struct udevice *bus = udev->controller_dev;
  56. struct dm_usb_ops *ops = usb_get_ops(bus);
  57. /* This is only requird by some controllers - current XHCI */
  58. if (!ops->alloc_device)
  59. return 0;
  60. return ops->alloc_device(bus, udev);
  61. }
  62. int usb_stop(void)
  63. {
  64. struct udevice *bus;
  65. struct uclass *uc;
  66. int err = 0, ret;
  67. /* De-activate any devices that have been activated */
  68. ret = uclass_get(UCLASS_USB, &uc);
  69. if (ret)
  70. return ret;
  71. uclass_foreach_dev(bus, uc) {
  72. ret = device_remove(bus);
  73. if (ret && !err)
  74. err = ret;
  75. }
  76. usb_stor_reset();
  77. usb_hub_reset();
  78. usb_started = 0;
  79. return err;
  80. }
  81. static int usb_scan_bus(struct udevice *bus, bool recurse)
  82. {
  83. struct usb_bus_priv *priv;
  84. struct udevice *dev;
  85. int ret;
  86. priv = dev_get_uclass_priv(bus);
  87. assert(recurse); /* TODO: Support non-recusive */
  88. ret = usb_scan_device(bus, 0, USB_SPEED_FULL, &dev);
  89. if (ret)
  90. return ret;
  91. return priv->next_addr;
  92. }
  93. int usb_init(void)
  94. {
  95. int controllers_initialized = 0;
  96. struct udevice *bus;
  97. struct uclass *uc;
  98. int count = 0;
  99. int ret;
  100. asynch_allowed = 1;
  101. usb_hub_reset();
  102. ret = uclass_get(UCLASS_USB, &uc);
  103. if (ret)
  104. return ret;
  105. uclass_foreach_dev(bus, uc) {
  106. /* init low_level USB */
  107. count++;
  108. printf("USB");
  109. printf("%d: ", bus->seq);
  110. ret = device_probe(bus);
  111. if (ret == -ENODEV) { /* No such device. */
  112. puts("Port not available.\n");
  113. controllers_initialized++;
  114. continue;
  115. }
  116. if (ret) { /* Other error. */
  117. printf("probe failed, error %d\n", ret);
  118. continue;
  119. }
  120. /*
  121. * lowlevel init is OK, now scan the bus for devices
  122. * i.e. search HUBs and configure them
  123. */
  124. controllers_initialized++;
  125. printf("scanning bus %d for devices... ", bus->seq);
  126. debug("\n");
  127. ret = usb_scan_bus(bus, true);
  128. if (ret < 0)
  129. printf("failed, error %d\n", ret);
  130. else if (!ret)
  131. printf("No USB Device found\n");
  132. else
  133. printf("%d USB Device(s) found\n", ret);
  134. usb_started = true;
  135. }
  136. debug("scan end\n");
  137. /* if we were not able to find at least one working bus, bail out */
  138. if (!count)
  139. printf("No controllers found\n");
  140. else if (controllers_initialized == 0)
  141. printf("USB error: all controllers failed lowlevel init\n");
  142. return usb_started ? 0 : -1;
  143. }
  144. int usb_reset_root_port(void)
  145. {
  146. return -ENOSYS;
  147. }
  148. static struct usb_device *find_child_devnum(struct udevice *parent, int devnum)
  149. {
  150. struct usb_device *udev;
  151. struct udevice *dev;
  152. if (!device_active(parent))
  153. return NULL;
  154. udev = dev_get_parentdata(parent);
  155. if (udev->devnum == devnum)
  156. return udev;
  157. for (device_find_first_child(parent, &dev);
  158. dev;
  159. device_find_next_child(&dev)) {
  160. udev = find_child_devnum(dev, devnum);
  161. if (udev)
  162. return udev;
  163. }
  164. return NULL;
  165. }
  166. struct usb_device *usb_get_dev_index(struct udevice *bus, int index)
  167. {
  168. struct udevice *hub;
  169. int devnum = index + 1; /* Addresses are allocated from 1 on USB */
  170. device_find_first_child(bus, &hub);
  171. if (device_get_uclass_id(hub) == UCLASS_USB_HUB)
  172. return find_child_devnum(hub, devnum);
  173. return NULL;
  174. }
  175. int usb_post_bind(struct udevice *dev)
  176. {
  177. /* Scan the bus for devices */
  178. return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
  179. }
  180. int usb_port_reset(struct usb_device *parent, int portnr)
  181. {
  182. unsigned short portstatus;
  183. int ret;
  184. debug("%s: start\n", __func__);
  185. if (parent) {
  186. /* reset the port for the second time */
  187. assert(portnr > 0);
  188. debug("%s: reset %d\n", __func__, portnr - 1);
  189. ret = legacy_hub_port_reset(parent, portnr - 1, &portstatus);
  190. if (ret < 0) {
  191. printf("\n Couldn't reset port %i\n", portnr);
  192. return ret;
  193. }
  194. } else {
  195. debug("%s: reset root\n", __func__);
  196. usb_reset_root_port();
  197. }
  198. return 0;
  199. }
  200. int usb_legacy_port_reset(struct usb_device *parent, int portnr)
  201. {
  202. return usb_port_reset(parent, portnr);
  203. }
  204. int usb_setup_ehci_gadget(struct ehci_ctrl **ctlrp)
  205. {
  206. struct usb_platdata *plat;
  207. struct udevice *dev;
  208. int ret;
  209. /* Find the old device and remove it */
  210. ret = uclass_find_device_by_seq(UCLASS_USB, 0, true, &dev);
  211. if (ret)
  212. return ret;
  213. ret = device_remove(dev);
  214. if (ret)
  215. return ret;
  216. plat = dev_get_platdata(dev);
  217. plat->init_type = USB_INIT_DEVICE;
  218. ret = device_probe(dev);
  219. if (ret)
  220. return ret;
  221. *ctlrp = dev_get_priv(dev);
  222. return 0;
  223. }
  224. /* returns 0 if no match, 1 if match */
  225. int usb_match_device(const struct usb_device_descriptor *desc,
  226. const struct usb_device_id *id)
  227. {
  228. if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
  229. id->idVendor != le16_to_cpu(desc->idVendor))
  230. return 0;
  231. if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
  232. id->idProduct != le16_to_cpu(desc->idProduct))
  233. return 0;
  234. /* No need to test id->bcdDevice_lo != 0, since 0 is never
  235. greater than any unsigned number. */
  236. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
  237. (id->bcdDevice_lo > le16_to_cpu(desc->bcdDevice)))
  238. return 0;
  239. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
  240. (id->bcdDevice_hi < le16_to_cpu(desc->bcdDevice)))
  241. return 0;
  242. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
  243. (id->bDeviceClass != desc->bDeviceClass))
  244. return 0;
  245. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
  246. (id->bDeviceSubClass != desc->bDeviceSubClass))
  247. return 0;
  248. if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
  249. (id->bDeviceProtocol != desc->bDeviceProtocol))
  250. return 0;
  251. return 1;
  252. }
  253. /* returns 0 if no match, 1 if match */
  254. int usb_match_one_id_intf(const struct usb_device_descriptor *desc,
  255. const struct usb_interface_descriptor *int_desc,
  256. const struct usb_device_id *id)
  257. {
  258. /* The interface class, subclass, protocol and number should never be
  259. * checked for a match if the device class is Vendor Specific,
  260. * unless the match record specifies the Vendor ID. */
  261. if (desc->bDeviceClass == USB_CLASS_VENDOR_SPEC &&
  262. !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
  263. (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
  264. USB_DEVICE_ID_MATCH_INT_SUBCLASS |
  265. USB_DEVICE_ID_MATCH_INT_PROTOCOL |
  266. USB_DEVICE_ID_MATCH_INT_NUMBER)))
  267. return 0;
  268. if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
  269. (id->bInterfaceClass != int_desc->bInterfaceClass))
  270. return 0;
  271. if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
  272. (id->bInterfaceSubClass != int_desc->bInterfaceSubClass))
  273. return 0;
  274. if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
  275. (id->bInterfaceProtocol != int_desc->bInterfaceProtocol))
  276. return 0;
  277. if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
  278. (id->bInterfaceNumber != int_desc->bInterfaceNumber))
  279. return 0;
  280. return 1;
  281. }
  282. /* returns 0 if no match, 1 if match */
  283. int usb_match_one_id(struct usb_device_descriptor *desc,
  284. struct usb_interface_descriptor *int_desc,
  285. const struct usb_device_id *id)
  286. {
  287. if (!usb_match_device(desc, id))
  288. return 0;
  289. return usb_match_one_id_intf(desc, int_desc, id);
  290. }
  291. /**
  292. * usb_find_and_bind_driver() - Find and bind the right USB driver
  293. *
  294. * This only looks at certain fields in the descriptor.
  295. */
  296. static int usb_find_and_bind_driver(struct udevice *parent,
  297. struct usb_device_descriptor *desc,
  298. struct usb_interface_descriptor *iface,
  299. int bus_seq, int devnum,
  300. struct udevice **devp)
  301. {
  302. struct usb_driver_entry *start, *entry;
  303. int n_ents;
  304. int ret;
  305. char name[30], *str;
  306. *devp = NULL;
  307. debug("%s: Searching for driver\n", __func__);
  308. start = ll_entry_start(struct usb_driver_entry, usb_driver_entry);
  309. n_ents = ll_entry_count(struct usb_driver_entry, usb_driver_entry);
  310. for (entry = start; entry != start + n_ents; entry++) {
  311. const struct usb_device_id *id;
  312. struct udevice *dev;
  313. const struct driver *drv;
  314. struct usb_dev_platdata *plat;
  315. for (id = entry->match; id->match_flags; id++) {
  316. if (!usb_match_one_id(desc, iface, id))
  317. continue;
  318. drv = entry->driver;
  319. /*
  320. * We could pass the descriptor to the driver as
  321. * platdata (instead of NULL) and allow its bind()
  322. * method to return -ENOENT if it doesn't support this
  323. * device. That way we could continue the search to
  324. * find another driver. For now this doesn't seem
  325. * necesssary, so just bind the first match.
  326. */
  327. ret = device_bind(parent, drv, drv->name, NULL, -1,
  328. &dev);
  329. if (ret)
  330. goto error;
  331. debug("%s: Match found: %s\n", __func__, drv->name);
  332. dev->driver_data = id->driver_info;
  333. plat = dev_get_parent_platdata(dev);
  334. plat->id = *id;
  335. *devp = dev;
  336. return 0;
  337. }
  338. }
  339. /* Bind a generic driver so that the device can be used */
  340. snprintf(name, sizeof(name), "generic_bus_%x_dev_%x", bus_seq, devnum);
  341. str = strdup(name);
  342. if (!str)
  343. return -ENOMEM;
  344. ret = device_bind_driver(parent, "usb_dev_generic_drv", str, devp);
  345. error:
  346. debug("%s: No match found: %d\n", __func__, ret);
  347. return ret;
  348. }
  349. /**
  350. * usb_find_child() - Find an existing device which matches our needs
  351. *
  352. *
  353. */
  354. static int usb_find_child(struct udevice *parent,
  355. struct usb_device_descriptor *desc,
  356. struct usb_interface_descriptor *iface,
  357. struct udevice **devp)
  358. {
  359. struct udevice *dev;
  360. *devp = NULL;
  361. for (device_find_first_child(parent, &dev);
  362. dev;
  363. device_find_next_child(&dev)) {
  364. struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
  365. /* If this device is already in use, skip it */
  366. if (device_active(dev))
  367. continue;
  368. debug(" %s: name='%s', plat=%d, desc=%d\n", __func__,
  369. dev->name, plat->id.bDeviceClass, desc->bDeviceClass);
  370. if (usb_match_one_id(desc, iface, &plat->id)) {
  371. *devp = dev;
  372. return 0;
  373. }
  374. }
  375. return -ENOENT;
  376. }
  377. int usb_scan_device(struct udevice *parent, int port,
  378. enum usb_device_speed speed, struct udevice **devp)
  379. {
  380. struct udevice *dev;
  381. bool created = false;
  382. struct usb_dev_platdata *plat;
  383. struct usb_bus_priv *priv;
  384. struct usb_device *parent_udev;
  385. int ret;
  386. ALLOC_CACHE_ALIGN_BUFFER(struct usb_device, udev, 1);
  387. struct usb_interface_descriptor *iface = &udev->config.if_desc[0].desc;
  388. *devp = NULL;
  389. memset(udev, '\0', sizeof(*udev));
  390. ret = usb_get_bus(parent, &udev->controller_dev);
  391. if (ret)
  392. return ret;
  393. priv = dev_get_uclass_priv(udev->controller_dev);
  394. /*
  395. * Somewhat nasty, this. We create a local device and use the normal
  396. * USB stack to read its descriptor. Then we know what type of device
  397. * to create for real.
  398. *
  399. * udev->dev is set to the parent, since we don't have a real device
  400. * yet. The USB stack should not access udev.dev anyway, except perhaps
  401. * to find the controller, and the controller will either be @parent,
  402. * or some parent of @parent.
  403. *
  404. * Another option might be to create the device as a generic USB
  405. * device, then morph it into the correct one when we know what it
  406. * should be. This means that a generic USB device would morph into
  407. * a network controller, or a USB flash stick, for example. However,
  408. * we don't support such morphing and it isn't clear that it would
  409. * be easy to do.
  410. *
  411. * Yet another option is to split out the USB stack parts of udev
  412. * into something like a 'struct urb' (as Linux does) which can exist
  413. * independently of any device. This feels cleaner, but calls for quite
  414. * a big change to the USB stack.
  415. *
  416. * For now, the approach is to set up an empty udev, read its
  417. * descriptor and assign it an address, then bind a real device and
  418. * stash the resulting information into the device's parent
  419. * platform data. Then when we probe it, usb_child_pre_probe() is called
  420. * and it will pull the information out of the stash.
  421. */
  422. udev->dev = parent;
  423. udev->speed = speed;
  424. udev->devnum = priv->next_addr + 1;
  425. udev->portnr = port;
  426. debug("Calling usb_setup_device(), portnr=%d\n", udev->portnr);
  427. parent_udev = device_get_uclass_id(parent) == UCLASS_USB_HUB ?
  428. dev_get_parentdata(parent) : NULL;
  429. ret = usb_setup_device(udev, priv->desc_before_addr, parent_udev, port);
  430. debug("read_descriptor for '%s': ret=%d\n", parent->name, ret);
  431. if (ret)
  432. return ret;
  433. ret = usb_find_child(parent, &udev->descriptor, iface, &dev);
  434. debug("** usb_find_child returns %d\n", ret);
  435. if (ret) {
  436. if (ret != -ENOENT)
  437. return ret;
  438. ret = usb_find_and_bind_driver(parent, &udev->descriptor, iface,
  439. udev->controller_dev->seq,
  440. udev->devnum, &dev);
  441. if (ret)
  442. return ret;
  443. created = true;
  444. }
  445. plat = dev_get_parent_platdata(dev);
  446. debug("%s: Probing '%s', plat=%p\n", __func__, dev->name, plat);
  447. plat->devnum = udev->devnum;
  448. plat->speed = udev->speed;
  449. plat->slot_id = udev->slot_id;
  450. plat->portnr = port;
  451. debug("** device '%s': stashing slot_id=%d\n", dev->name,
  452. plat->slot_id);
  453. priv->next_addr++;
  454. ret = device_probe(dev);
  455. if (ret) {
  456. debug("%s: Device '%s' probe failed\n", __func__, dev->name);
  457. priv->next_addr--;
  458. if (created)
  459. device_unbind(dev);
  460. return ret;
  461. }
  462. *devp = dev;
  463. return 0;
  464. }
  465. int usb_child_post_bind(struct udevice *dev)
  466. {
  467. struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
  468. const void *blob = gd->fdt_blob;
  469. int val;
  470. if (dev->of_offset == -1)
  471. return 0;
  472. /* We only support matching a few things */
  473. val = fdtdec_get_int(blob, dev->of_offset, "usb,device-class", -1);
  474. if (val != -1) {
  475. plat->id.match_flags |= USB_DEVICE_ID_MATCH_DEV_CLASS;
  476. plat->id.bDeviceClass = val;
  477. }
  478. val = fdtdec_get_int(blob, dev->of_offset, "usb,interface-class", -1);
  479. if (val != -1) {
  480. plat->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
  481. plat->id.bInterfaceClass = val;
  482. }
  483. return 0;
  484. }
  485. int usb_get_bus(struct udevice *dev, struct udevice **busp)
  486. {
  487. struct udevice *bus;
  488. *busp = NULL;
  489. for (bus = dev; bus && device_get_uclass_id(bus) != UCLASS_USB; )
  490. bus = bus->parent;
  491. if (!bus) {
  492. /* By design this cannot happen */
  493. assert(bus);
  494. debug("USB HUB '%s' does not have a controller\n", dev->name);
  495. return -EXDEV;
  496. }
  497. *busp = bus;
  498. return 0;
  499. }
  500. int usb_child_pre_probe(struct udevice *dev)
  501. {
  502. struct udevice *bus;
  503. struct usb_device *udev = dev_get_parentdata(dev);
  504. struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
  505. int ret;
  506. ret = usb_get_bus(dev, &bus);
  507. if (ret)
  508. return ret;
  509. udev->controller_dev = bus;
  510. udev->dev = dev;
  511. udev->devnum = plat->devnum;
  512. udev->slot_id = plat->slot_id;
  513. udev->portnr = plat->portnr;
  514. udev->speed = plat->speed;
  515. debug("** device '%s': getting slot_id=%d\n", dev->name, plat->slot_id);
  516. ret = usb_select_config(udev);
  517. if (ret)
  518. return ret;
  519. return 0;
  520. }
  521. UCLASS_DRIVER(usb) = {
  522. .id = UCLASS_USB,
  523. .name = "usb",
  524. .flags = DM_UC_FLAG_SEQ_ALIAS,
  525. .post_bind = usb_post_bind,
  526. .per_child_auto_alloc_size = sizeof(struct usb_device),
  527. .per_device_auto_alloc_size = sizeof(struct usb_bus_priv),
  528. .child_post_bind = usb_child_post_bind,
  529. .child_pre_probe = usb_child_pre_probe,
  530. .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),
  531. };
  532. UCLASS_DRIVER(usb_dev_generic) = {
  533. .id = UCLASS_USB_DEV_GENERIC,
  534. .name = "usb_dev_generic",
  535. };
  536. U_BOOT_DRIVER(usb_dev_generic_drv) = {
  537. .id = UCLASS_USB_DEV_GENERIC,
  538. .name = "usb_dev_generic_drv",
  539. };