usb-emul-uclass.c 7.4 KB

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