2 * (C) Copyright 2015 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
5 * SPDX-License-Identifier: GPL-2.0+
15 * This driver emulates a USB keyboard using the USB HID specification (boot
20 SANDBOX_KEYB_EP_IN = 1, /* endpoints */
30 STRINGID_MANUFACTURER = 1,
38 * struct sandbox_keyb_priv - private state for this driver
41 struct sandbox_keyb_priv {
45 struct sandbox_keyb_plat {
46 struct usb_string keyb_strings[STRINGID_COUNT];
49 static struct usb_device_descriptor keyb_device_desc = {
50 .bLength = sizeof(keyb_device_desc),
51 .bDescriptorType = USB_DT_DEVICE,
53 .bcdUSB = __constant_cpu_to_le16(0x0100),
59 .idVendor = __constant_cpu_to_le16(0x1234),
60 .idProduct = __constant_cpu_to_le16(0x5679),
61 .iManufacturer = STRINGID_MANUFACTURER,
62 .iProduct = STRINGID_PRODUCT,
63 .iSerialNumber = STRINGID_SERIAL,
64 .bNumConfigurations = 1,
67 static struct usb_config_descriptor keyb_config0 = {
68 .bLength = sizeof(keyb_config0),
69 .bDescriptorType = USB_DT_CONFIG,
71 /* wTotalLength is set up by usb-emul-uclass */
73 .bConfigurationValue = 0,
75 .bmAttributes = 1 << 7 | 1 << 5,
79 static struct usb_interface_descriptor keyb_interface0 = {
80 .bLength = sizeof(keyb_interface0),
81 .bDescriptorType = USB_DT_INTERFACE,
83 .bInterfaceNumber = 0,
84 .bAlternateSetting = 0,
86 .bInterfaceClass = USB_CLASS_HID,
87 .bInterfaceSubClass = USB_SUB_HID_BOOT,
88 .bInterfaceProtocol = USB_PROT_HID_KEYBOARD,
92 static struct usb_class_hid_descriptor keyb_report0 = {
93 .bLength = sizeof(keyb_report0),
94 .bDescriptorType = USB_DT_HID,
98 .bDescriptorType0 = USB_DT_HID_REPORT,
99 .wDescriptorLength0 = 0x3f,
102 static struct usb_endpoint_descriptor keyb_endpoint0_in = {
103 .bLength = USB_DT_ENDPOINT_SIZE,
104 .bDescriptorType = USB_DT_ENDPOINT,
106 .bEndpointAddress = SANDBOX_KEYB_EP_IN | USB_ENDPOINT_DIR_MASK,
107 .bmAttributes = USB_ENDPOINT_XFER_BULK |
108 USB_ENDPOINT_XFER_ISOC,
109 .wMaxPacketSize = __constant_cpu_to_le16(8),
113 static struct usb_interface_descriptor keyb_interface1 = {
114 .bLength = sizeof(keyb_interface1),
115 .bDescriptorType = USB_DT_INTERFACE,
117 .bInterfaceNumber = 1,
118 .bAlternateSetting = 0,
120 .bInterfaceClass = USB_CLASS_HID,
121 .bInterfaceSubClass = USB_SUB_HID_BOOT,
122 .bInterfaceProtocol = USB_PROT_HID_MOUSE,
126 static struct usb_class_hid_descriptor keyb_report1 = {
127 .bLength = sizeof(struct usb_class_hid_descriptor),
128 .bDescriptorType = USB_DT_HID,
131 .bNumDescriptors = 1,
132 .bDescriptorType0 = USB_DT_HID_REPORT,
133 .wDescriptorLength0 = 0x32,
136 static struct usb_endpoint_descriptor keyb_endpoint1_in = {
137 .bLength = USB_DT_ENDPOINT_SIZE,
138 .bDescriptorType = USB_DT_ENDPOINT,
140 .bEndpointAddress = SANDBOX_KEYB_EP_IN | USB_ENDPOINT_DIR_MASK,
141 .bmAttributes = USB_ENDPOINT_XFER_BULK |
142 USB_ENDPOINT_XFER_ISOC,
143 .wMaxPacketSize = __constant_cpu_to_le16(8),
147 static void *keyb_desc_list[] = {
159 int sandbox_usb_keyb_add_string(struct udevice *dev, const char *str)
161 struct sandbox_keyb_priv *priv = dev_get_priv(dev);
165 ret = membuff_put(&priv->in, str, len);
172 static int sandbox_keyb_control(struct udevice *dev, struct usb_device *udev,
173 unsigned long pipe, void *buff, int len,
174 struct devrequest *setup)
176 debug("pipe=%lx\n", pipe);
181 static int sandbox_keyb_interrupt(struct udevice *dev, struct usb_device *udev,
182 unsigned long pipe, void *buffer, int length, int interval)
184 struct sandbox_keyb_priv *priv = dev_get_priv(dev);
185 uint8_t *data = buffer;
188 memset(data, '\0', length);
189 ch = membuff_getbyte(&priv->in);
191 data[2] = 4 + ch - 'a';
196 static int sandbox_keyb_bind(struct udevice *dev)
198 struct sandbox_keyb_plat *plat = dev_get_platdata(dev);
199 struct usb_string *fs;
201 fs = plat->keyb_strings;
202 fs[0].id = STRINGID_MANUFACTURER;
204 fs[1].id = STRINGID_PRODUCT;
205 fs[1].s = "keyboard";
206 fs[2].id = STRINGID_SERIAL;
209 return usb_emul_setup_device(dev, plat->keyb_strings, keyb_desc_list);
212 static int sandbox_keyb_probe(struct udevice *dev)
214 struct sandbox_keyb_priv *priv = dev_get_priv(dev);
216 return membuff_new(&priv->in, 256);
219 static const struct dm_usb_ops sandbox_usb_keyb_ops = {
220 .control = sandbox_keyb_control,
221 .interrupt = sandbox_keyb_interrupt,
224 static const struct udevice_id sandbox_usb_keyb_ids[] = {
225 { .compatible = "sandbox,usb-keyb" },
229 U_BOOT_DRIVER(usb_sandbox_keyb) = {
230 .name = "usb_sandbox_keyb",
231 .id = UCLASS_USB_EMUL,
232 .of_match = sandbox_usb_keyb_ids,
233 .bind = sandbox_keyb_bind,
234 .probe = sandbox_keyb_probe,
235 .ops = &sandbox_usb_keyb_ops,
236 .priv_auto_alloc_size = sizeof(struct sandbox_keyb_priv),
237 .platdata_auto_alloc_size = sizeof(struct sandbox_keyb_plat),