sandbox_keyb.c 5.4 KB

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