usb-emul-uclass.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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, 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. if (udev->devnum == devnum) {
  104. debug("%s: Found emulator '%s', addr %d\n", __func__,
  105. dev->name, udev->devnum);
  106. *emulp = dev;
  107. return 0;
  108. }
  109. }
  110. debug("%s: No emulator found, addr %d\n", __func__, devnum);
  111. return -ENOENT;
  112. }
  113. int usb_emul_find(struct udevice *bus, ulong pipe, struct udevice **emulp)
  114. {
  115. int devnum = usb_pipedevice(pipe);
  116. return usb_emul_find_devnum(devnum, emulp);
  117. }
  118. int usb_emul_find_for_dev(struct udevice *dev, struct udevice **emulp)
  119. {
  120. struct usb_dev_platdata *udev = dev_get_parent_platdata(dev);
  121. return usb_emul_find_devnum(udev->devnum, emulp);
  122. }
  123. int usb_emul_control(struct udevice *emul, struct usb_device *udev,
  124. unsigned long pipe, void *buffer, int length,
  125. struct devrequest *setup)
  126. {
  127. struct dm_usb_ops *ops = usb_get_emul_ops(emul);
  128. struct usb_dev_platdata *plat;
  129. int ret;
  130. /* We permit getting the descriptor before we are probed */
  131. plat = dev_get_parent_platdata(emul);
  132. if (!ops->control)
  133. return -ENOSYS;
  134. debug("%s: dev=%s\n", __func__, emul->name);
  135. if (pipe == usb_rcvctrlpipe(udev, 0)) {
  136. switch (setup->request) {
  137. case USB_REQ_GET_DESCRIPTOR: {
  138. return usb_emul_get_descriptor(plat, setup->value,
  139. buffer, length);
  140. }
  141. default:
  142. ret = device_probe(emul);
  143. if (ret)
  144. return ret;
  145. return ops->control(emul, udev, pipe, buffer, length,
  146. setup);
  147. }
  148. } else if (pipe == usb_snddefctrl(udev)) {
  149. switch (setup->request) {
  150. case USB_REQ_SET_ADDRESS:
  151. debug(" ** set address %s %d\n", emul->name,
  152. setup->value);
  153. plat->devnum = setup->value;
  154. return 0;
  155. default:
  156. debug("requestsend =%x\n", setup->request);
  157. break;
  158. }
  159. } else if (pipe == usb_sndctrlpipe(udev, 0)) {
  160. switch (setup->request) {
  161. case USB_REQ_SET_CONFIGURATION:
  162. plat->configno = setup->value;
  163. return 0;
  164. default:
  165. ret = device_probe(emul);
  166. if (ret)
  167. return ret;
  168. return ops->control(emul, udev, pipe, buffer, length,
  169. setup);
  170. }
  171. }
  172. debug("pipe=%lx\n", pipe);
  173. return -EIO;
  174. }
  175. int usb_emul_bulk(struct udevice *emul, struct usb_device *udev,
  176. unsigned long pipe, void *buffer, int length)
  177. {
  178. struct dm_usb_ops *ops = usb_get_emul_ops(emul);
  179. int ret;
  180. /* We permit getting the descriptor before we are probed */
  181. if (!ops->bulk)
  182. return -ENOSYS;
  183. debug("%s: dev=%s\n", __func__, emul->name);
  184. ret = device_probe(emul);
  185. if (ret)
  186. return ret;
  187. return ops->bulk(emul, udev, pipe, buffer, length);
  188. }
  189. int usb_emul_int(struct udevice *emul, struct usb_device *udev,
  190. unsigned long pipe, void *buffer, int length, int interval)
  191. {
  192. struct dm_usb_ops *ops = usb_get_emul_ops(emul);
  193. if (!ops->interrupt)
  194. return -ENOSYS;
  195. debug("%s: dev=%s\n", __func__, emul->name);
  196. return ops->interrupt(emul, udev, pipe, buffer, length, interval);
  197. }
  198. int usb_emul_setup_device(struct udevice *dev, int maxpacketsize,
  199. struct usb_string *strings, void **desc_list)
  200. {
  201. struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
  202. struct usb_generic_descriptor **ptr;
  203. struct usb_config_descriptor *cdesc;
  204. int upto;
  205. plat->strings = strings;
  206. plat->desc_list = (struct usb_generic_descriptor **)desc_list;
  207. /* Fill in wTotalLength for each configuration descriptor */
  208. ptr = plat->desc_list;
  209. for (cdesc = NULL, upto = 0; *ptr; upto += (*ptr)->bLength, ptr++) {
  210. debug(" - upto=%d, type=%d\n", upto, (*ptr)->bDescriptorType);
  211. if ((*ptr)->bDescriptorType == USB_DT_CONFIG) {
  212. if (cdesc) {
  213. cdesc->wTotalLength = upto;
  214. debug("%s: config %d length %d\n", __func__,
  215. cdesc->bConfigurationValue,
  216. cdesc->bLength);
  217. }
  218. cdesc = (struct usb_config_descriptor *)*ptr;
  219. upto = 0;
  220. }
  221. }
  222. if (cdesc) {
  223. cdesc->wTotalLength = upto;
  224. debug("%s: config %d length %d\n", __func__,
  225. cdesc->bConfigurationValue, cdesc->wTotalLength);
  226. }
  227. return 0;
  228. }
  229. void usb_emul_reset(struct udevice *dev)
  230. {
  231. struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
  232. plat->devnum = 0;
  233. plat->configno = 0;
  234. }
  235. UCLASS_DRIVER(usb_emul) = {
  236. .id = UCLASS_USB_EMUL,
  237. .name = "usb_emul",
  238. .post_bind = dm_scan_fdt_dev,
  239. .per_child_auto_alloc_size = sizeof(struct usb_device),
  240. .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),
  241. };