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