usb-emul-uclass.c 7.4 KB

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