sandbox_keyb.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 <os.h>
  10. #include <scsi.h>
  11. #include <usb.h>
  12. DECLARE_GLOBAL_DATA_PTR;
  13. /*
  14. * This driver emulates a USB keyboard using the USB HID specification (boot
  15. * protocol)
  16. */
  17. enum {
  18. SANDBOX_KEYB_EP_IN = 1, /* endpoints */
  19. };
  20. enum cmd_phase {
  21. PHASE_START,
  22. PHASE_DATA,
  23. PHASE_STATUS,
  24. };
  25. enum {
  26. STRINGID_MANUFACTURER = 1,
  27. STRINGID_PRODUCT,
  28. STRINGID_SERIAL,
  29. STRINGID_COUNT,
  30. };
  31. /**
  32. * struct sandbox_keyb_priv - private state for this driver
  33. *
  34. */
  35. struct sandbox_keyb_priv {
  36. struct membuff in;
  37. };
  38. struct sandbox_keyb_plat {
  39. struct usb_string keyb_strings[STRINGID_COUNT];
  40. };
  41. static struct usb_device_descriptor keyb_device_desc = {
  42. .bLength = sizeof(keyb_device_desc),
  43. .bDescriptorType = USB_DT_DEVICE,
  44. .bcdUSB = __constant_cpu_to_le16(0x0100),
  45. .bDeviceClass = 0,
  46. .bDeviceSubClass = 0,
  47. .bDeviceProtocol = 0,
  48. .idVendor = __constant_cpu_to_le16(0x1234),
  49. .idProduct = __constant_cpu_to_le16(0x5679),
  50. .iManufacturer = STRINGID_MANUFACTURER,
  51. .iProduct = STRINGID_PRODUCT,
  52. .iSerialNumber = STRINGID_SERIAL,
  53. .bNumConfigurations = 1,
  54. };
  55. static struct usb_config_descriptor keyb_config0 = {
  56. .bLength = sizeof(keyb_config0),
  57. .bDescriptorType = USB_DT_CONFIG,
  58. /* wTotalLength is set up by usb-emul-uclass */
  59. .bNumInterfaces = 2,
  60. .bConfigurationValue = 0,
  61. .iConfiguration = 0,
  62. .bmAttributes = 1 << 7 | 1 << 5,
  63. .bMaxPower = 50,
  64. };
  65. static struct usb_interface_descriptor keyb_interface0 = {
  66. .bLength = sizeof(keyb_interface0),
  67. .bDescriptorType = USB_DT_INTERFACE,
  68. .bInterfaceNumber = 0,
  69. .bAlternateSetting = 0,
  70. .bNumEndpoints = 1,
  71. .bInterfaceClass = USB_CLASS_HID,
  72. .bInterfaceSubClass = USB_SUB_HID_BOOT,
  73. .bInterfaceProtocol = USB_PROT_HID_KEYBOARD,
  74. .iInterface = 0,
  75. };
  76. static struct usb_class_hid_descriptor keyb_report0 = {
  77. .bLength = sizeof(keyb_report0),
  78. .bDescriptorType = USB_DT_HID,
  79. .bcdCDC = 0x101,
  80. .bCountryCode = 0,
  81. .bNumDescriptors = 1,
  82. .bDescriptorType0 = USB_DT_HID_REPORT,
  83. .wDescriptorLength0 = 0x3f,
  84. };
  85. static struct usb_endpoint_descriptor keyb_endpoint0_in = {
  86. .bLength = USB_DT_ENDPOINT_SIZE,
  87. .bDescriptorType = USB_DT_ENDPOINT,
  88. .bEndpointAddress = SANDBOX_KEYB_EP_IN | USB_ENDPOINT_DIR_MASK,
  89. .bmAttributes = USB_ENDPOINT_XFER_BULK |
  90. USB_ENDPOINT_XFER_ISOC,
  91. .wMaxPacketSize = __constant_cpu_to_le16(8),
  92. .bInterval = 0xa,
  93. };
  94. static struct usb_interface_descriptor keyb_interface1 = {
  95. .bLength = sizeof(keyb_interface1),
  96. .bDescriptorType = USB_DT_INTERFACE,
  97. .bInterfaceNumber = 1,
  98. .bAlternateSetting = 0,
  99. .bNumEndpoints = 1,
  100. .bInterfaceClass = USB_CLASS_HID,
  101. .bInterfaceSubClass = USB_SUB_HID_BOOT,
  102. .bInterfaceProtocol = USB_PROT_HID_MOUSE,
  103. .iInterface = 0,
  104. };
  105. static struct usb_class_hid_descriptor keyb_report1 = {
  106. .bLength = sizeof(struct usb_class_hid_descriptor),
  107. .bDescriptorType = USB_DT_HID,
  108. .bcdCDC = 0x101,
  109. .bCountryCode = 0,
  110. .bNumDescriptors = 1,
  111. .bDescriptorType0 = USB_DT_HID_REPORT,
  112. .wDescriptorLength0 = 0x32,
  113. };
  114. static struct usb_endpoint_descriptor keyb_endpoint1_in = {
  115. .bLength = USB_DT_ENDPOINT_SIZE,
  116. .bDescriptorType = USB_DT_ENDPOINT,
  117. .bEndpointAddress = SANDBOX_KEYB_EP_IN | USB_ENDPOINT_DIR_MASK,
  118. .bmAttributes = USB_ENDPOINT_XFER_BULK |
  119. USB_ENDPOINT_XFER_ISOC,
  120. .wMaxPacketSize = __constant_cpu_to_le16(8),
  121. .bInterval = 0xa,
  122. };
  123. static void *keyb_desc_list[] = {
  124. &keyb_device_desc,
  125. &keyb_config0,
  126. &keyb_interface0,
  127. &keyb_report0,
  128. &keyb_endpoint0_in,
  129. &keyb_interface1,
  130. &keyb_report1,
  131. &keyb_endpoint1_in,
  132. NULL,
  133. };
  134. int sandbox_usb_keyb_add_string(struct udevice *dev, const char *str)
  135. {
  136. struct sandbox_keyb_priv *priv = dev_get_priv(dev);
  137. int len, ret;
  138. len = strlen(str);
  139. ret = membuff_put(&priv->in, str, len);
  140. if (ret != len)
  141. return -ENOSPC;
  142. return 0;
  143. }
  144. static int sandbox_keyb_control(struct udevice *dev, struct usb_device *udev,
  145. unsigned long pipe, void *buff, int len,
  146. struct devrequest *setup)
  147. {
  148. debug("pipe=%lx\n", pipe);
  149. return -EIO;
  150. }
  151. static int sandbox_keyb_interrupt(struct udevice *dev, struct usb_device *udev,
  152. unsigned long pipe, void *buffer, int length, int interval)
  153. {
  154. struct sandbox_keyb_priv *priv = dev_get_priv(dev);
  155. uint8_t *data = buffer;
  156. int ch;
  157. memset(data, '\0', length);
  158. ch = membuff_getbyte(&priv->in);
  159. if (ch != -1)
  160. data[2] = 4 + ch - 'a';
  161. return 0;
  162. }
  163. static int sandbox_keyb_bind(struct udevice *dev)
  164. {
  165. struct sandbox_keyb_plat *plat = dev_get_platdata(dev);
  166. struct usb_string *fs;
  167. fs = plat->keyb_strings;
  168. fs[0].id = STRINGID_MANUFACTURER;
  169. fs[0].s = "sandbox";
  170. fs[1].id = STRINGID_PRODUCT;
  171. fs[1].s = "keyboard";
  172. fs[2].id = STRINGID_SERIAL;
  173. fs[2].s = dev->name;
  174. return usb_emul_setup_device(dev, PACKET_SIZE_8, plat->keyb_strings,
  175. keyb_desc_list);
  176. }
  177. static int sandbox_keyb_probe(struct udevice *dev)
  178. {
  179. struct sandbox_keyb_priv *priv = dev_get_priv(dev);
  180. return membuff_new(&priv->in, 256);
  181. }
  182. static const struct dm_usb_ops sandbox_usb_keyb_ops = {
  183. .control = sandbox_keyb_control,
  184. .interrupt = sandbox_keyb_interrupt,
  185. };
  186. static const struct udevice_id sandbox_usb_keyb_ids[] = {
  187. { .compatible = "sandbox,usb-keyb" },
  188. { }
  189. };
  190. U_BOOT_DRIVER(usb_sandbox_keyb) = {
  191. .name = "usb_sandbox_keyb",
  192. .id = UCLASS_USB_EMUL,
  193. .of_match = sandbox_usb_keyb_ids,
  194. .bind = sandbox_keyb_bind,
  195. .probe = sandbox_keyb_probe,
  196. .ops = &sandbox_usb_keyb_ops,
  197. .priv_auto_alloc_size = sizeof(struct sandbox_keyb_priv),
  198. .platdata_auto_alloc_size = sizeof(struct sandbox_keyb_plat),
  199. };