usb-emul-uclass.c 6.9 KB

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