blob: dc43880d27e69bc9882ddd3847303bd4e9544b91 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassbe4ebd12015-11-08 23:48:06 -07002/*
3 * (C) Copyright 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glassbe4ebd12015-11-08 23:48:06 -07005 */
6
7#include <common.h>
8#include <dm.h>
9#include <os.h>
10#include <scsi.h>
11#include <usb.h>
12
Simon Glassbe4ebd12015-11-08 23:48:06 -070013/*
14 * This driver emulates a USB keyboard using the USB HID specification (boot
15 * protocol)
16 */
17
18enum {
19 SANDBOX_KEYB_EP_IN = 1, /* endpoints */
20};
21
22enum cmd_phase {
23 PHASE_START,
24 PHASE_DATA,
25 PHASE_STATUS,
26};
27
28enum {
29 STRINGID_MANUFACTURER = 1,
30 STRINGID_PRODUCT,
31 STRINGID_SERIAL,
32
33 STRINGID_COUNT,
34};
35
36/**
37 * struct sandbox_keyb_priv - private state for this driver
38 *
39 */
40struct sandbox_keyb_priv {
41 struct membuff in;
42};
43
44struct sandbox_keyb_plat {
45 struct usb_string keyb_strings[STRINGID_COUNT];
46};
47
48static struct usb_device_descriptor keyb_device_desc = {
49 .bLength = sizeof(keyb_device_desc),
50 .bDescriptorType = USB_DT_DEVICE,
51
52 .bcdUSB = __constant_cpu_to_le16(0x0100),
53
54 .bDeviceClass = 0,
55 .bDeviceSubClass = 0,
56 .bDeviceProtocol = 0,
57
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,
64};
65
66static struct usb_config_descriptor keyb_config0 = {
67 .bLength = sizeof(keyb_config0),
68 .bDescriptorType = USB_DT_CONFIG,
69
70 /* wTotalLength is set up by usb-emul-uclass */
71 .bNumInterfaces = 2,
72 .bConfigurationValue = 0,
73 .iConfiguration = 0,
74 .bmAttributes = 1 << 7 | 1 << 5,
75 .bMaxPower = 50,
76};
77
78static struct usb_interface_descriptor keyb_interface0 = {
79 .bLength = sizeof(keyb_interface0),
80 .bDescriptorType = USB_DT_INTERFACE,
81
82 .bInterfaceNumber = 0,
83 .bAlternateSetting = 0,
84 .bNumEndpoints = 1,
85 .bInterfaceClass = USB_CLASS_HID,
86 .bInterfaceSubClass = USB_SUB_HID_BOOT,
87 .bInterfaceProtocol = USB_PROT_HID_KEYBOARD,
88 .iInterface = 0,
89};
90
91static struct usb_class_hid_descriptor keyb_report0 = {
92 .bLength = sizeof(keyb_report0),
93 .bDescriptorType = USB_DT_HID,
94 .bcdCDC = 0x101,
95 .bCountryCode = 0,
96 .bNumDescriptors = 1,
97 .bDescriptorType0 = USB_DT_HID_REPORT,
98 .wDescriptorLength0 = 0x3f,
99};
100
101static struct usb_endpoint_descriptor keyb_endpoint0_in = {
102 .bLength = USB_DT_ENDPOINT_SIZE,
103 .bDescriptorType = USB_DT_ENDPOINT,
104
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),
109 .bInterval = 0xa,
110};
111
112static struct usb_interface_descriptor keyb_interface1 = {
113 .bLength = sizeof(keyb_interface1),
114 .bDescriptorType = USB_DT_INTERFACE,
115
116 .bInterfaceNumber = 1,
117 .bAlternateSetting = 0,
118 .bNumEndpoints = 1,
119 .bInterfaceClass = USB_CLASS_HID,
120 .bInterfaceSubClass = USB_SUB_HID_BOOT,
121 .bInterfaceProtocol = USB_PROT_HID_MOUSE,
122 .iInterface = 0,
123};
124
125static struct usb_class_hid_descriptor keyb_report1 = {
126 .bLength = sizeof(struct usb_class_hid_descriptor),
127 .bDescriptorType = USB_DT_HID,
128 .bcdCDC = 0x101,
129 .bCountryCode = 0,
130 .bNumDescriptors = 1,
131 .bDescriptorType0 = USB_DT_HID_REPORT,
132 .wDescriptorLength0 = 0x32,
133};
134
135static struct usb_endpoint_descriptor keyb_endpoint1_in = {
136 .bLength = USB_DT_ENDPOINT_SIZE,
137 .bDescriptorType = USB_DT_ENDPOINT,
138
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),
143 .bInterval = 0xa,
144};
145
146static void *keyb_desc_list[] = {
147 &keyb_device_desc,
148 &keyb_config0,
149 &keyb_interface0,
150 &keyb_report0,
151 &keyb_endpoint0_in,
152 &keyb_interface1,
153 &keyb_report1,
154 &keyb_endpoint1_in,
155 NULL,
156};
157
158int sandbox_usb_keyb_add_string(struct udevice *dev, const char *str)
159{
160 struct sandbox_keyb_priv *priv = dev_get_priv(dev);
161 int len, ret;
162
163 len = strlen(str);
164 ret = membuff_put(&priv->in, str, len);
165 if (ret != len)
166 return -ENOSPC;
167
168 return 0;
169}
170
171static int sandbox_keyb_control(struct udevice *dev, struct usb_device *udev,
172 unsigned long pipe, void *buff, int len,
173 struct devrequest *setup)
174{
175 debug("pipe=%lx\n", pipe);
176
177 return -EIO;
178}
179
180static int sandbox_keyb_interrupt(struct udevice *dev, struct usb_device *udev,
Michal Suchanek1c95b9f2019-08-18 10:55:27 +0200181 unsigned long pipe, void *buffer, int length, int interval,
182 bool nonblock)
Simon Glassbe4ebd12015-11-08 23:48:06 -0700183{
184 struct sandbox_keyb_priv *priv = dev_get_priv(dev);
185 uint8_t *data = buffer;
186 int ch;
187
188 memset(data, '\0', length);
189 ch = membuff_getbyte(&priv->in);
190 if (ch != -1)
191 data[2] = 4 + ch - 'a';
192
193 return 0;
194}
195
196static int sandbox_keyb_bind(struct udevice *dev)
197{
198 struct sandbox_keyb_plat *plat = dev_get_platdata(dev);
199 struct usb_string *fs;
200
201 fs = plat->keyb_strings;
202 fs[0].id = STRINGID_MANUFACTURER;
203 fs[0].s = "sandbox";
204 fs[1].id = STRINGID_PRODUCT;
205 fs[1].s = "keyboard";
206 fs[2].id = STRINGID_SERIAL;
207 fs[2].s = dev->name;
208
Bin Mengd502cf12017-10-01 06:19:36 -0700209 return usb_emul_setup_device(dev, plat->keyb_strings, keyb_desc_list);
Simon Glassbe4ebd12015-11-08 23:48:06 -0700210}
211
212static int sandbox_keyb_probe(struct udevice *dev)
213{
214 struct sandbox_keyb_priv *priv = dev_get_priv(dev);
215
216 return membuff_new(&priv->in, 256);
217}
218
219static const struct dm_usb_ops sandbox_usb_keyb_ops = {
220 .control = sandbox_keyb_control,
221 .interrupt = sandbox_keyb_interrupt,
222};
223
224static const struct udevice_id sandbox_usb_keyb_ids[] = {
225 { .compatible = "sandbox,usb-keyb" },
226 { }
227};
228
229U_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),
238};