1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
14 * This driver emulates a USB keyboard using the USB HID specification (boot
19 SANDBOX_KEYB_EP_IN = 1, /* endpoints */
29 STRINGID_MANUFACTURER = 1,
37 * struct sandbox_keyb_priv - private state for this driver
40 struct sandbox_keyb_priv {
44 struct sandbox_keyb_plat {
45 struct usb_string keyb_strings[STRINGID_COUNT];
48 static struct usb_device_descriptor keyb_device_desc = {
49 .bLength = sizeof(keyb_device_desc),
50 .bDescriptorType = USB_DT_DEVICE,
52 .bcdUSB = __constant_cpu_to_le16(0x0100),
58 .idVendor = __constant_cpu_to_le16(0x1234),
59 .idProduct = __constant_cpu_to_le16(0x5679),
60 .iManufacturer = STRINGID_MANUFACTURER,
61 .iProduct = STRINGID_PRODUCT,
62 .iSerialNumber = STRINGID_SERIAL,
63 .bNumConfigurations = 1,
66 static struct usb_config_descriptor keyb_config0 = {
67 .bLength = sizeof(keyb_config0),
68 .bDescriptorType = USB_DT_CONFIG,
70 /* wTotalLength is set up by usb-emul-uclass */
72 .bConfigurationValue = 0,
74 .bmAttributes = 1 << 7 | 1 << 5,
78 static struct usb_interface_descriptor keyb_interface0 = {
79 .bLength = sizeof(keyb_interface0),
80 .bDescriptorType = USB_DT_INTERFACE,
82 .bInterfaceNumber = 0,
83 .bAlternateSetting = 0,
85 .bInterfaceClass = USB_CLASS_HID,
86 .bInterfaceSubClass = USB_SUB_HID_BOOT,
87 .bInterfaceProtocol = USB_PROT_HID_KEYBOARD,
91 static struct usb_class_hid_descriptor keyb_report0 = {
92 .bLength = sizeof(keyb_report0),
93 .bDescriptorType = USB_DT_HID,
97 .bDescriptorType0 = USB_DT_HID_REPORT,
98 .wDescriptorLength0 = 0x3f,
101 static struct usb_endpoint_descriptor keyb_endpoint0_in = {
102 .bLength = USB_DT_ENDPOINT_SIZE,
103 .bDescriptorType = USB_DT_ENDPOINT,
105 .bEndpointAddress = SANDBOX_KEYB_EP_IN | USB_ENDPOINT_DIR_MASK,
106 .bmAttributes = USB_ENDPOINT_XFER_BULK |
107 USB_ENDPOINT_XFER_ISOC,
108 .wMaxPacketSize = __constant_cpu_to_le16(8),
112 static struct usb_interface_descriptor keyb_interface1 = {
113 .bLength = sizeof(keyb_interface1),
114 .bDescriptorType = USB_DT_INTERFACE,
116 .bInterfaceNumber = 1,
117 .bAlternateSetting = 0,
119 .bInterfaceClass = USB_CLASS_HID,
120 .bInterfaceSubClass = USB_SUB_HID_BOOT,
121 .bInterfaceProtocol = USB_PROT_HID_MOUSE,
125 static struct usb_class_hid_descriptor keyb_report1 = {
126 .bLength = sizeof(struct usb_class_hid_descriptor),
127 .bDescriptorType = USB_DT_HID,
130 .bNumDescriptors = 1,
131 .bDescriptorType0 = USB_DT_HID_REPORT,
132 .wDescriptorLength0 = 0x32,
135 static struct usb_endpoint_descriptor keyb_endpoint1_in = {
136 .bLength = USB_DT_ENDPOINT_SIZE,
137 .bDescriptorType = USB_DT_ENDPOINT,
139 .bEndpointAddress = SANDBOX_KEYB_EP_IN | USB_ENDPOINT_DIR_MASK,
140 .bmAttributes = USB_ENDPOINT_XFER_BULK |
141 USB_ENDPOINT_XFER_ISOC,
142 .wMaxPacketSize = __constant_cpu_to_le16(8),
146 static void *keyb_desc_list[] = {
158 int sandbox_usb_keyb_add_string(struct udevice *dev, const char *str)
160 struct sandbox_keyb_priv *priv = dev_get_priv(dev);
164 ret = membuff_put(&priv->in, str, len);
171 static int sandbox_keyb_control(struct udevice *dev, struct usb_device *udev,
172 unsigned long pipe, void *buff, int len,
173 struct devrequest *setup)
175 debug("pipe=%lx\n", pipe);
180 static int sandbox_keyb_interrupt(struct udevice *dev, struct usb_device *udev,
181 unsigned long pipe, void *buffer, int length, int interval)
183 struct sandbox_keyb_priv *priv = dev_get_priv(dev);
184 uint8_t *data = buffer;
187 memset(data, '\0', length);
188 ch = membuff_getbyte(&priv->in);
190 data[2] = 4 + ch - 'a';
195 static int sandbox_keyb_bind(struct udevice *dev)
197 struct sandbox_keyb_plat *plat = dev_get_platdata(dev);
198 struct usb_string *fs;
200 fs = plat->keyb_strings;
201 fs[0].id = STRINGID_MANUFACTURER;
203 fs[1].id = STRINGID_PRODUCT;
204 fs[1].s = "keyboard";
205 fs[2].id = STRINGID_SERIAL;
208 return usb_emul_setup_device(dev, plat->keyb_strings, keyb_desc_list);
211 static int sandbox_keyb_probe(struct udevice *dev)
213 struct sandbox_keyb_priv *priv = dev_get_priv(dev);
215 return membuff_new(&priv->in, 256);
218 static const struct dm_usb_ops sandbox_usb_keyb_ops = {
219 .control = sandbox_keyb_control,
220 .interrupt = sandbox_keyb_interrupt,
223 static const struct udevice_id sandbox_usb_keyb_ids[] = {
224 { .compatible = "sandbox,usb-keyb" },
228 U_BOOT_DRIVER(usb_sandbox_keyb) = {
229 .name = "usb_sandbox_keyb",
230 .id = UCLASS_USB_EMUL,
231 .of_match = sandbox_usb_keyb_ids,
232 .bind = sandbox_keyb_bind,
233 .probe = sandbox_keyb_probe,
234 .ops = &sandbox_usb_keyb_ops,
235 .priv_auto_alloc_size = sizeof(struct sandbox_keyb_priv),
236 .platdata_auto_alloc_size = sizeof(struct sandbox_keyb_plat),