usb-uclass.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * (C) Copyright 2015 Google, Inc
  3. * Written by Simon Glass <sjg@chromium.org>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <dm.h>
  9. #include <errno.h>
  10. #include <usb.h>
  11. #include <dm/device-internal.h>
  12. #include <dm/lists.h>
  13. #include <dm/root.h>
  14. #include <dm/uclass-internal.h>
  15. DECLARE_GLOBAL_DATA_PTR;
  16. extern bool usb_started; /* flag for the started/stopped USB status */
  17. static bool asynch_allowed;
  18. int usb_disable_asynch(int disable)
  19. {
  20. int old_value = asynch_allowed;
  21. asynch_allowed = !disable;
  22. return old_value;
  23. }
  24. int submit_int_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
  25. int length, int interval)
  26. {
  27. struct udevice *bus = udev->controller_dev;
  28. struct dm_usb_ops *ops = usb_get_ops(bus);
  29. if (!ops->interrupt)
  30. return -ENOSYS;
  31. return ops->interrupt(bus, udev, pipe, buffer, length, interval);
  32. }
  33. int submit_control_msg(struct usb_device *udev, unsigned long pipe,
  34. void *buffer, int length, struct devrequest *setup)
  35. {
  36. struct udevice *bus = udev->controller_dev;
  37. struct dm_usb_ops *ops = usb_get_ops(bus);
  38. if (!ops->control)
  39. return -ENOSYS;
  40. return ops->control(bus, udev, pipe, buffer, length, setup);
  41. }
  42. int submit_bulk_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
  43. int length)
  44. {
  45. struct udevice *bus = udev->controller_dev;
  46. struct dm_usb_ops *ops = usb_get_ops(bus);
  47. if (!ops->bulk)
  48. return -ENOSYS;
  49. return ops->bulk(bus, udev, pipe, buffer, length);
  50. }
  51. int usb_alloc_device(struct usb_device *udev)
  52. {
  53. struct udevice *bus = udev->controller_dev;
  54. struct dm_usb_ops *ops = usb_get_ops(bus);
  55. /* This is only requird by some controllers - current XHCI */
  56. if (!ops->alloc_device)
  57. return 0;
  58. return ops->alloc_device(bus, udev);
  59. }
  60. int usb_stop(void)
  61. {
  62. struct udevice *bus;
  63. struct uclass *uc;
  64. int err = 0, ret;
  65. /* De-activate any devices that have been activated */
  66. ret = uclass_get(UCLASS_USB, &uc);
  67. if (ret)
  68. return ret;
  69. uclass_foreach_dev(bus, uc) {
  70. ret = device_remove(bus);
  71. if (ret && !err)
  72. err = ret;
  73. }
  74. usb_stor_reset();
  75. usb_hub_reset();
  76. usb_started = 0;
  77. return err;
  78. }
  79. static int usb_scan_bus(struct udevice *bus, bool recurse)
  80. {
  81. struct usb_bus_priv *priv;
  82. struct udevice *dev;
  83. int ret;
  84. priv = dev_get_uclass_priv(bus);
  85. assert(recurse); /* TODO: Support non-recusive */
  86. ret = usb_scan_device(bus, 0, USB_SPEED_FULL, &dev);
  87. if (ret)
  88. return ret;
  89. return priv->next_addr;
  90. }
  91. int usb_init(void)
  92. {
  93. int controllers_initialized = 0;
  94. struct udevice *bus;
  95. struct uclass *uc;
  96. int count = 0;
  97. int ret;
  98. asynch_allowed = 1;
  99. usb_hub_reset();
  100. ret = uclass_get(UCLASS_USB, &uc);
  101. if (ret)
  102. return ret;
  103. uclass_foreach_dev(bus, uc) {
  104. /* init low_level USB */
  105. count++;
  106. printf("USB");
  107. printf("%d: ", bus->seq);
  108. ret = device_probe(bus);
  109. if (ret == -ENODEV) { /* No such device. */
  110. puts("Port not available.\n");
  111. controllers_initialized++;
  112. continue;
  113. }
  114. if (ret) { /* Other error. */
  115. printf("probe failed, error %d\n", ret);
  116. continue;
  117. }
  118. /*
  119. * lowlevel init is OK, now scan the bus for devices
  120. * i.e. search HUBs and configure them
  121. */
  122. controllers_initialized++;
  123. printf("scanning bus %d for devices... ", bus->seq);
  124. debug("\n");
  125. ret = usb_scan_bus(bus, true);
  126. if (ret < 0)
  127. printf("failed, error %d\n", ret);
  128. else if (!ret)
  129. printf("No USB Device found\n");
  130. else
  131. printf("%d USB Device(s) found\n", ret);
  132. usb_started = true;
  133. }
  134. debug("scan end\n");
  135. /* if we were not able to find at least one working bus, bail out */
  136. if (!count)
  137. printf("No controllers found\n");
  138. else if (controllers_initialized == 0)
  139. printf("USB error: all controllers failed lowlevel init\n");
  140. return usb_started ? 0 : -1;
  141. }
  142. int usb_reset_root_port(void)
  143. {
  144. return -ENOSYS;
  145. }
  146. static struct usb_device *find_child_devnum(struct udevice *parent, int devnum)
  147. {
  148. struct usb_device *udev;
  149. struct udevice *dev;
  150. if (!device_active(parent))
  151. return NULL;
  152. udev = dev_get_parentdata(parent);
  153. if (udev->devnum == devnum)
  154. return udev;
  155. for (device_find_first_child(parent, &dev);
  156. dev;
  157. device_find_next_child(&dev)) {
  158. udev = find_child_devnum(dev, devnum);
  159. if (udev)
  160. return udev;
  161. }
  162. return NULL;
  163. }
  164. struct usb_device *usb_get_dev_index(struct udevice *bus, int index)
  165. {
  166. struct udevice *hub;
  167. int devnum = index + 1; /* Addresses are allocated from 1 on USB */
  168. device_find_first_child(bus, &hub);
  169. if (device_get_uclass_id(hub) == UCLASS_USB_HUB)
  170. return find_child_devnum(hub, devnum);
  171. return NULL;
  172. }
  173. int usb_post_bind(struct udevice *dev)
  174. {
  175. /* Scan the bus for devices */
  176. return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
  177. }
  178. int usb_port_reset(struct usb_device *parent, int portnr)
  179. {
  180. unsigned short portstatus;
  181. int ret;
  182. debug("%s: start\n", __func__);
  183. if (parent) {
  184. /* reset the port for the second time */
  185. assert(portnr > 0);
  186. debug("%s: reset %d\n", __func__, portnr - 1);
  187. ret = legacy_hub_port_reset(parent, portnr - 1, &portstatus);
  188. if (ret < 0) {
  189. printf("\n Couldn't reset port %i\n", portnr);
  190. return ret;
  191. }
  192. } else {
  193. debug("%s: reset root\n", __func__);
  194. usb_reset_root_port();
  195. }
  196. return 0;
  197. }
  198. int usb_legacy_port_reset(struct usb_device *parent, int portnr)
  199. {
  200. return usb_port_reset(parent, portnr);
  201. }
  202. int usb_scan_device(struct udevice *parent, int port,
  203. enum usb_device_speed speed, struct udevice **devp)
  204. {
  205. struct udevice *dev;
  206. bool created = false;
  207. struct usb_dev_platdata *plat;
  208. struct usb_bus_priv *priv;
  209. struct usb_device *parent_udev;
  210. int ret;
  211. ALLOC_CACHE_ALIGN_BUFFER(struct usb_device, udev, 1);
  212. struct usb_interface_descriptor *iface = &udev->config.if_desc[0].desc;
  213. *devp = NULL;
  214. memset(udev, '\0', sizeof(*udev));
  215. ret = usb_get_bus(parent, &udev->controller_dev);
  216. if (ret)
  217. return ret;
  218. priv = dev_get_uclass_priv(udev->controller_dev);
  219. /*
  220. * Somewhat nasty, this. We create a local device and use the normal
  221. * USB stack to read its descriptor. Then we know what type of device
  222. * to create for real.
  223. *
  224. * udev->dev is set to the parent, since we don't have a real device
  225. * yet. The USB stack should not access udev.dev anyway, except perhaps
  226. * to find the controller, and the controller will either be @parent,
  227. * or some parent of @parent.
  228. *
  229. * Another option might be to create the device as a generic USB
  230. * device, then morph it into the correct one when we know what it
  231. * should be. This means that a generic USB device would morph into
  232. * a network controller, or a USB flash stick, for example. However,
  233. * we don't support such morphing and it isn't clear that it would
  234. * be easy to do.
  235. *
  236. * Yet another option is to split out the USB stack parts of udev
  237. * into something like a 'struct urb' (as Linux does) which can exist
  238. * independently of any device. This feels cleaner, but calls for quite
  239. * a big change to the USB stack.
  240. *
  241. * For now, the approach is to set up an empty udev, read its
  242. * descriptor and assign it an address, then bind a real device and
  243. * stash the resulting information into the device's parent
  244. * platform data. Then when we probe it, usb_child_pre_probe() is called
  245. * and it will pull the information out of the stash.
  246. */
  247. udev->dev = parent;
  248. udev->speed = speed;
  249. udev->devnum = priv->next_addr + 1;
  250. udev->portnr = port;
  251. debug("Calling usb_setup_device(), portnr=%d\n", udev->portnr);
  252. parent_udev = device_get_uclass_id(parent) == UCLASS_USB_HUB ?
  253. dev_get_parentdata(parent) : NULL;
  254. ret = usb_setup_device(udev, priv->desc_before_addr, parent_udev, port);
  255. debug("read_descriptor for '%s': ret=%d\n", parent->name, ret);
  256. if (ret)
  257. return ret;
  258. ret = usb_find_child(parent, &udev->descriptor, iface, &dev);
  259. debug("** usb_find_child returns %d\n", ret);
  260. /* TODO: Find a suitable driver and create the device */
  261. return -ENOENT;
  262. }
  263. int usb_child_post_bind(struct udevice *dev)
  264. {
  265. struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
  266. const void *blob = gd->fdt_blob;
  267. int val;
  268. if (dev->of_offset == -1)
  269. return 0;
  270. /* We only support matching a few things */
  271. val = fdtdec_get_int(blob, dev->of_offset, "usb,device-class", -1);
  272. if (val != -1) {
  273. plat->id.match_flags |= USB_DEVICE_ID_MATCH_DEV_CLASS;
  274. plat->id.bDeviceClass = val;
  275. }
  276. val = fdtdec_get_int(blob, dev->of_offset, "usb,interface-class", -1);
  277. if (val != -1) {
  278. plat->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
  279. plat->id.bInterfaceClass = val;
  280. }
  281. return 0;
  282. }
  283. int usb_get_bus(struct udevice *dev, struct udevice **busp)
  284. {
  285. struct udevice *bus;
  286. *busp = NULL;
  287. for (bus = dev; bus && device_get_uclass_id(bus) != UCLASS_USB; )
  288. bus = bus->parent;
  289. if (!bus) {
  290. /* By design this cannot happen */
  291. assert(bus);
  292. debug("USB HUB '%s' does not have a controller\n", dev->name);
  293. return -EXDEV;
  294. }
  295. *busp = bus;
  296. return 0;
  297. }
  298. int usb_child_pre_probe(struct udevice *dev)
  299. {
  300. struct udevice *bus;
  301. struct usb_device *udev = dev_get_parentdata(dev);
  302. struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
  303. int ret;
  304. ret = usb_get_bus(dev, &bus);
  305. if (ret)
  306. return ret;
  307. udev->controller_dev = bus;
  308. udev->dev = dev;
  309. udev->devnum = plat->devnum;
  310. udev->slot_id = plat->slot_id;
  311. udev->portnr = plat->portnr;
  312. udev->speed = plat->speed;
  313. debug("** device '%s': getting slot_id=%d\n", dev->name, plat->slot_id);
  314. ret = usb_select_config(udev);
  315. if (ret)
  316. return ret;
  317. return 0;
  318. }
  319. UCLASS_DRIVER(usb) = {
  320. .id = UCLASS_USB,
  321. .name = "usb",
  322. .flags = DM_UC_FLAG_SEQ_ALIAS,
  323. .post_bind = usb_post_bind,
  324. .per_child_auto_alloc_size = sizeof(struct usb_device),
  325. .per_device_auto_alloc_size = sizeof(struct usb_bus_priv),
  326. .child_post_bind = usb_child_post_bind,
  327. .child_pre_probe = usb_child_pre_probe,
  328. .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),
  329. };