sandbox_keyb.c 5.8 KB

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