usb-emul-uclass.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 <usb.h>
  10. #include <dm/device-internal.h>
  11. DECLARE_GLOBAL_DATA_PTR;
  12. static int copy_to_unicode(char *buff, int length, const char *str)
  13. {
  14. int ptr;
  15. int i;
  16. if (length < 2)
  17. return 0;
  18. buff[1] = USB_DT_STRING;
  19. for (ptr = 2, i = 0; ptr + 1 < length && *str; i++, ptr += 2) {
  20. buff[ptr] = str[i];
  21. buff[ptr + 1] = 0;
  22. }
  23. buff[0] = ptr;
  24. return ptr;
  25. }
  26. static int usb_emul_get_string(struct usb_string *strings, int index,
  27. char *buff, int length)
  28. {
  29. if (index == 0) {
  30. char *desc = buff;
  31. desc[0] = 4;
  32. desc[1] = USB_DT_STRING;
  33. desc[2] = 0x09;
  34. desc[3] = 0x14;
  35. return 4;
  36. } else if (strings) {
  37. struct usb_string *ptr;
  38. for (ptr = strings; ptr->s; ptr++) {
  39. if (ptr->id == index)
  40. return copy_to_unicode(buff, length, ptr->s);
  41. }
  42. }
  43. return -EINVAL;
  44. }
  45. static struct usb_generic_descriptor **find_descriptor(
  46. struct usb_generic_descriptor **ptr, int type, int index)
  47. {
  48. debug("%s: type=%x, index=%d\n", __func__, type, index);
  49. for (; *ptr; ptr++) {
  50. if ((*ptr)->bDescriptorType != type)
  51. continue;
  52. switch (type) {
  53. case USB_DT_CONFIG: {
  54. struct usb_config_descriptor *cdesc;
  55. cdesc = (struct usb_config_descriptor *)*ptr;
  56. if (cdesc && cdesc->bConfigurationValue == index)
  57. return ptr;
  58. break;
  59. }
  60. default:
  61. return ptr;
  62. }
  63. }
  64. debug("%s: config ptr=%p\n", __func__, *ptr);
  65. return ptr;
  66. }
  67. static int usb_emul_get_descriptor(struct usb_dev_platdata *plat, int value,
  68. void *buffer, int length)
  69. {
  70. struct usb_generic_descriptor **ptr;
  71. int type = value >> 8;
  72. int index = value & 0xff;
  73. int upto, todo;
  74. debug("%s: type=%d, index=%d, plat=%p\n", __func__, type, index, plat);
  75. if (type == USB_DT_STRING) {
  76. return usb_emul_get_string(plat->strings, index, buffer,
  77. length);
  78. }
  79. ptr = find_descriptor((struct usb_generic_descriptor **)plat->desc_list,
  80. type, index);
  81. if (!ptr) {
  82. debug("%s: Could not find descriptor type %d, index %d\n",
  83. __func__, type, index);
  84. return -ENOENT;
  85. }
  86. for (upto = 0; *ptr && upto < length; ptr++, upto += todo) {
  87. todo = min(length - upto, (int)(*ptr)->bLength);
  88. memcpy(buffer + upto, *ptr, todo);
  89. }
  90. return upto ? upto : length ? -EIO : 0;
  91. }
  92. static int usb_emul_find_devnum(int devnum, int port1, struct udevice **emulp)
  93. {
  94. struct udevice *dev;
  95. struct uclass *uc;
  96. int ret;
  97. *emulp = NULL;
  98. ret = uclass_get(UCLASS_USB_EMUL, &uc);
  99. if (ret)
  100. return ret;
  101. uclass_foreach_dev(dev, uc) {
  102. struct usb_dev_platdata *udev = dev_get_parent_platdata(dev);
  103. /*
  104. * devnum is initialzied to zero at the beginning of the
  105. * enumeration process in usb_setup_device(). At this
  106. * point, udev->devnum has not been assigned to any valid
  107. * USB address either, so we can't rely on the comparison
  108. * result between udev->devnum and devnum to select an
  109. * emulator device.
  110. */
  111. if (!devnum) {
  112. struct usb_emul_platdata *plat;
  113. /*
  114. * If the parent is sandbox USB controller, we are
  115. * the root hub. And there is only one root hub
  116. * in the system.
  117. */
  118. if (device_get_uclass_id(dev->parent) == UCLASS_USB) {
  119. debug("%s: Found emulator '%s'\n",
  120. __func__, dev->name);
  121. *emulp = dev;
  122. return 0;
  123. }
  124. plat = dev_get_uclass_platdata(dev);
  125. if (plat->port1 == port1) {
  126. debug("%s: Found emulator '%s', port %d\n",
  127. __func__, dev->name, port1);
  128. *emulp = dev;
  129. return 0;
  130. }
  131. } else if (udev->devnum == devnum) {
  132. debug("%s: Found emulator '%s', addr %d\n", __func__,
  133. dev->name, udev->devnum);
  134. *emulp = dev;
  135. return 0;
  136. }
  137. }
  138. debug("%s: No emulator found, addr %d\n", __func__, devnum);
  139. return -ENOENT;
  140. }
  141. int usb_emul_find(struct udevice *bus, ulong pipe, int port1,
  142. struct udevice **emulp)
  143. {
  144. int devnum = usb_pipedevice(pipe);
  145. return usb_emul_find_devnum(devnum, port1, emulp);
  146. }
  147. int usb_emul_find_for_dev(struct udevice *dev, struct udevice **emulp)
  148. {
  149. struct usb_dev_platdata *udev = dev_get_parent_platdata(dev);
  150. return usb_emul_find_devnum(udev->devnum, 0, emulp);
  151. }
  152. int usb_emul_control(struct udevice *emul, struct usb_device *udev,
  153. unsigned long pipe, void *buffer, int length,
  154. struct devrequest *setup)
  155. {
  156. struct dm_usb_ops *ops = usb_get_emul_ops(emul);
  157. struct usb_dev_platdata *plat;
  158. int ret;
  159. /* We permit getting the descriptor before we are probed */
  160. plat = dev_get_parent_platdata(emul);
  161. if (!ops->control)
  162. return -ENOSYS;
  163. debug("%s: dev=%s\n", __func__, emul->name);
  164. if (pipe == usb_rcvctrlpipe(udev, 0)) {
  165. switch (setup->request) {
  166. case USB_REQ_GET_DESCRIPTOR: {
  167. return usb_emul_get_descriptor(plat, setup->value,
  168. buffer, length);
  169. }
  170. default:
  171. ret = device_probe(emul);
  172. if (ret)
  173. return ret;
  174. return ops->control(emul, udev, pipe, buffer, length,
  175. setup);
  176. }
  177. } else if (pipe == usb_snddefctrl(udev)) {
  178. switch (setup->request) {
  179. case USB_REQ_SET_ADDRESS:
  180. debug(" ** set address %s %d\n", emul->name,
  181. setup->value);
  182. plat->devnum = setup->value;
  183. return 0;
  184. default:
  185. debug("requestsend =%x\n", setup->request);
  186. break;
  187. }
  188. } else if (pipe == usb_sndctrlpipe(udev, 0)) {
  189. switch (setup->request) {
  190. case USB_REQ_SET_CONFIGURATION:
  191. plat->configno = setup->value;
  192. return 0;
  193. default:
  194. ret = device_probe(emul);
  195. if (ret)
  196. return ret;
  197. return ops->control(emul, udev, pipe, buffer, length,
  198. setup);
  199. }
  200. }
  201. debug("pipe=%lx\n", pipe);
  202. return -EIO;
  203. }
  204. int usb_emul_bulk(struct udevice *emul, struct usb_device *udev,
  205. unsigned long pipe, void *buffer, int length)
  206. {
  207. struct dm_usb_ops *ops = usb_get_emul_ops(emul);
  208. int ret;
  209. /* We permit getting the descriptor before we are probed */
  210. if (!ops->bulk)
  211. return -ENOSYS;
  212. debug("%s: dev=%s\n", __func__, emul->name);
  213. ret = device_probe(emul);
  214. if (ret)
  215. return ret;
  216. return ops->bulk(emul, udev, pipe, buffer, length);
  217. }
  218. int usb_emul_int(struct udevice *emul, struct usb_device *udev,
  219. unsigned long pipe, void *buffer, int length, int interval)
  220. {
  221. struct dm_usb_ops *ops = usb_get_emul_ops(emul);
  222. if (!ops->interrupt)
  223. return -ENOSYS;
  224. debug("%s: dev=%s\n", __func__, emul->name);
  225. return ops->interrupt(emul, udev, pipe, buffer, length, interval);
  226. }
  227. int usb_emul_setup_device(struct udevice *dev, struct usb_string *strings,
  228. void **desc_list)
  229. {
  230. struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
  231. struct usb_generic_descriptor **ptr;
  232. struct usb_config_descriptor *cdesc;
  233. int upto;
  234. plat->strings = strings;
  235. plat->desc_list = (struct usb_generic_descriptor **)desc_list;
  236. /* Fill in wTotalLength for each configuration descriptor */
  237. ptr = plat->desc_list;
  238. for (cdesc = NULL, upto = 0; *ptr; upto += (*ptr)->bLength, ptr++) {
  239. debug(" - upto=%d, type=%d\n", upto, (*ptr)->bDescriptorType);
  240. if ((*ptr)->bDescriptorType == USB_DT_CONFIG) {
  241. if (cdesc) {
  242. cdesc->wTotalLength = upto;
  243. debug("%s: config %d length %d\n", __func__,
  244. cdesc->bConfigurationValue,
  245. cdesc->bLength);
  246. }
  247. cdesc = (struct usb_config_descriptor *)*ptr;
  248. upto = 0;
  249. }
  250. }
  251. if (cdesc) {
  252. cdesc->wTotalLength = upto;
  253. debug("%s: config %d length %d\n", __func__,
  254. cdesc->bConfigurationValue, cdesc->wTotalLength);
  255. }
  256. return 0;
  257. }
  258. void usb_emul_reset(struct udevice *dev)
  259. {
  260. struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
  261. plat->devnum = 0;
  262. plat->configno = 0;
  263. }
  264. UCLASS_DRIVER(usb_emul) = {
  265. .id = UCLASS_USB_EMUL,
  266. .name = "usb_emul",
  267. .post_bind = dm_scan_fdt_dev,
  268. .per_device_platdata_auto_alloc_size = sizeof(struct usb_emul_platdata),
  269. .per_child_auto_alloc_size = sizeof(struct usb_device),
  270. .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),
  271. };