1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2010 Hans Petter Selasky. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 /*
29 * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
30 */
31
32 #include <sys/param.h>
33 #include <sys/stdint.h>
34 #include <sys/stddef.h>
35 #include <sys/queue.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/bus.h>
39 #include <sys/linker_set.h>
40 #include <sys/module.h>
41 #include <sys/lock.h>
42 #include <sys/mutex.h>
43 #include <sys/condvar.h>
44 #include <sys/sysctl.h>
45 #include <sys/sx.h>
46 #include <sys/unistd.h>
47 #include <sys/callout.h>
48 #include <sys/malloc.h>
49 #include <sys/priv.h>
50
51 #include <dev/usb/usb.h>
52 #include <dev/usb/usbdi.h>
53 #include <dev/usb/usbdi_util.h>
54 #include <dev/usb/usbhid.h>
55 #include "usb_if.h"
56
57 #define USB_DEBUG_VAR g_keyboard_debug
58 #include <dev/usb/usb_debug.h>
59
60 #include <dev/usb/gadget/g_keyboard.h>
61
62 static SYSCTL_NODE(_hw_usb, OID_AUTO, g_keyboard,
63 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
64 "USB keyboard gadget");
65
66 #ifdef USB_DEBUG
67 static int g_keyboard_debug = 0;
68
69 SYSCTL_INT(_hw_usb_g_keyboard, OID_AUTO, debug, CTLFLAG_RWTUN,
70 &g_keyboard_debug, 0, "Debug level");
71 #endif
72
73 static int g_keyboard_mode = 0;
74
75 SYSCTL_INT(_hw_usb_g_keyboard, OID_AUTO, mode, CTLFLAG_RWTUN,
76 &g_keyboard_mode, 0, "Mode selection");
77
78 static int g_keyboard_key_press_interval = 1000;
79
80 SYSCTL_INT(_hw_usb_g_keyboard, OID_AUTO, key_press_interval, CTLFLAG_RWTUN,
81 &g_keyboard_key_press_interval, 0, "Key Press Interval in milliseconds");
82
83 static char g_keyboard_key_press_pattern[G_KEYBOARD_MAX_STRLEN];
84
85 SYSCTL_STRING(_hw_usb_g_keyboard, OID_AUTO, key_press_pattern, CTLFLAG_RW,
86 g_keyboard_key_press_pattern, sizeof(g_keyboard_key_press_pattern),
87 "Key Press Patterns");
88
89 #define UPROTO_BOOT_KEYBOARD 1
90
91 #define G_KEYBOARD_NMOD 8 /* units */
92 #define G_KEYBOARD_NKEYCODE 6 /* units */
93
94 struct g_keyboard_data {
95 uint8_t modifiers;
96 #define MOD_CONTROL_L 0x01
97 #define MOD_CONTROL_R 0x10
98 #define MOD_SHIFT_L 0x02
99 #define MOD_SHIFT_R 0x20
100 #define MOD_ALT_L 0x04
101 #define MOD_ALT_R 0x40
102 #define MOD_WIN_L 0x08
103 #define MOD_WIN_R 0x80
104 uint8_t reserved;
105 uint8_t keycode[G_KEYBOARD_NKEYCODE];
106 };
107
108 enum {
109 G_KEYBOARD_INTR_DT,
110 G_KEYBOARD_N_TRANSFER,
111 };
112
113 struct g_keyboard_softc {
114 struct mtx sc_mtx;
115 struct usb_callout sc_callout;
116 struct g_keyboard_data sc_data[2];
117 struct usb_xfer *sc_xfer[G_KEYBOARD_N_TRANSFER];
118
119 int sc_mode;
120 int sc_state;
121 int sc_pattern_len;
122
123 char sc_pattern[G_KEYBOARD_MAX_STRLEN];
124
125 uint8_t sc_led_state[4];
126 };
127
128 static device_probe_t g_keyboard_probe;
129 static device_attach_t g_keyboard_attach;
130 static device_detach_t g_keyboard_detach;
131 static usb_handle_request_t g_keyboard_handle_request;
132 static usb_callback_t g_keyboard_intr_callback;
133
134 static device_method_t g_keyboard_methods[] = {
135 /* USB interface */
136 DEVMETHOD(usb_handle_request, g_keyboard_handle_request),
137
138 /* Device interface */
139 DEVMETHOD(device_probe, g_keyboard_probe),
140 DEVMETHOD(device_attach, g_keyboard_attach),
141 DEVMETHOD(device_detach, g_keyboard_detach),
142
143 DEVMETHOD_END
144 };
145
146 static driver_t g_keyboard_driver = {
147 .name = "g_keyboard",
148 .methods = g_keyboard_methods,
149 .size = sizeof(struct g_keyboard_softc),
150 };
151
152 DRIVER_MODULE(g_keyboard, uhub, g_keyboard_driver, 0, 0);
153 MODULE_DEPEND(g_keyboard, usb, 1, 1, 1);
154
155 static const struct usb_config g_keyboard_config[G_KEYBOARD_N_TRANSFER] = {
156 [G_KEYBOARD_INTR_DT] = {
157 .type = UE_INTERRUPT,
158 .endpoint = UE_ADDR_ANY,
159 .direction = UE_DIR_IN,
160 .flags = {.ext_buffer = 1,.pipe_bof = 1,},
161 .bufsize = sizeof(struct g_keyboard_data),
162 .callback = &g_keyboard_intr_callback,
163 .frames = 2,
164 .usb_mode = USB_MODE_DEVICE,
165 },
166 };
167
168 static void g_keyboard_timeout(void *arg);
169
170 static void
g_keyboard_timeout_reset(struct g_keyboard_softc * sc)171 g_keyboard_timeout_reset(struct g_keyboard_softc *sc)
172 {
173 int i = g_keyboard_key_press_interval;
174
175 if (i <= 0)
176 i = 1;
177 else if (i > 1023)
178 i = 1023;
179
180 i = USB_MS_TO_TICKS(i);
181
182 usb_callout_reset(&sc->sc_callout, i, &g_keyboard_timeout, sc);
183 }
184
185 static void
g_keyboard_timeout(void * arg)186 g_keyboard_timeout(void *arg)
187 {
188 struct g_keyboard_softc *sc = arg;
189
190 sc->sc_mode = g_keyboard_mode;
191
192 memcpy(sc->sc_pattern, g_keyboard_key_press_pattern, sizeof(sc->sc_pattern));
193
194 sc->sc_pattern[G_KEYBOARD_MAX_STRLEN - 1] = 0;
195
196 sc->sc_pattern_len = strlen(sc->sc_pattern);
197
198 DPRINTFN(11, "Timeout %p\n", sc->sc_xfer[G_KEYBOARD_INTR_DT]);
199
200 usbd_transfer_start(sc->sc_xfer[G_KEYBOARD_INTR_DT]);
201
202 g_keyboard_timeout_reset(sc);
203 }
204
205 static int
g_keyboard_probe(device_t dev)206 g_keyboard_probe(device_t dev)
207 {
208 struct usb_attach_arg *uaa = device_get_ivars(dev);
209
210 DPRINTFN(11, "\n");
211
212 if (uaa->usb_mode != USB_MODE_DEVICE)
213 return (ENXIO);
214
215 if ((uaa->info.bInterfaceClass == UICLASS_HID) &&
216 (uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) &&
217 (uaa->info.bInterfaceProtocol == UPROTO_BOOT_KEYBOARD))
218 return (0);
219
220 return (ENXIO);
221 }
222
223 static int
g_keyboard_attach(device_t dev)224 g_keyboard_attach(device_t dev)
225 {
226 struct g_keyboard_softc *sc = device_get_softc(dev);
227 struct usb_attach_arg *uaa = device_get_ivars(dev);
228 int error;
229
230 DPRINTFN(11, "\n");
231
232 device_set_usb_desc(dev);
233
234 mtx_init(&sc->sc_mtx, "g_keyboard", NULL, MTX_DEF);
235
236 usb_callout_init_mtx(&sc->sc_callout, &sc->sc_mtx, 0);
237
238 sc->sc_mode = G_KEYBOARD_MODE_SILENT;
239
240 error = usbd_transfer_setup(uaa->device,
241 &uaa->info.bIfaceIndex, sc->sc_xfer, g_keyboard_config,
242 G_KEYBOARD_N_TRANSFER, sc, &sc->sc_mtx);
243
244 if (error) {
245 DPRINTF("error=%s\n", usbd_errstr(error));
246 goto detach;
247 }
248 mtx_lock(&sc->sc_mtx);
249 g_keyboard_timeout_reset(sc);
250 mtx_unlock(&sc->sc_mtx);
251
252 return (0); /* success */
253
254 detach:
255 g_keyboard_detach(dev);
256
257 return (ENXIO); /* error */
258 }
259
260 static int
g_keyboard_detach(device_t dev)261 g_keyboard_detach(device_t dev)
262 {
263 struct g_keyboard_softc *sc = device_get_softc(dev);
264
265 DPRINTF("\n");
266
267 mtx_lock(&sc->sc_mtx);
268 usb_callout_stop(&sc->sc_callout);
269 mtx_unlock(&sc->sc_mtx);
270
271 usbd_transfer_unsetup(sc->sc_xfer, G_KEYBOARD_N_TRANSFER);
272
273 usb_callout_drain(&sc->sc_callout);
274
275 mtx_destroy(&sc->sc_mtx);
276
277 return (0);
278 }
279
280 static uint8_t
g_keyboard_get_keycode(struct g_keyboard_softc * sc,int index)281 g_keyboard_get_keycode(struct g_keyboard_softc *sc, int index)
282 {
283 int key;
284 int mod = sc->sc_pattern_len;
285
286 if (mod == 0)
287 index = 0;
288 else
289 index %= mod;
290
291 if ((index >= 0) && (index < sc->sc_pattern_len))
292 key = sc->sc_pattern[index];
293 else
294 key = 'a';
295
296 if (key >= 'a' && key <= 'z')
297 return (key - 'a' + 0x04);
298 else
299 return (0x04);
300 }
301
302 static void
g_keyboard_intr_callback(struct usb_xfer * xfer,usb_error_t error)303 g_keyboard_intr_callback(struct usb_xfer *xfer, usb_error_t error)
304 {
305 struct g_keyboard_softc *sc = usbd_xfer_softc(xfer);
306 int actlen;
307 int aframes;
308
309 usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
310
311 DPRINTF("st=%d aframes=%d actlen=%d bytes\n",
312 USB_GET_STATE(xfer), aframes, actlen);
313
314 switch (USB_GET_STATE(xfer)) {
315 case USB_ST_TRANSFERRED:
316 break;
317
318 case USB_ST_SETUP:
319 tr_setup:
320 if (sc->sc_mode == G_KEYBOARD_MODE_SILENT) {
321 memset(&sc->sc_data, 0, sizeof(sc->sc_data));
322 usbd_xfer_set_frame_data(xfer, 0, &sc->sc_data[0], sizeof(sc->sc_data[0]));
323 usbd_xfer_set_frame_data(xfer, 1, &sc->sc_data[1], sizeof(sc->sc_data[1]));
324 usbd_xfer_set_frames(xfer, 2);
325 usbd_transfer_submit(xfer);
326
327 } else if (sc->sc_mode == G_KEYBOARD_MODE_PATTERN) {
328 memset(&sc->sc_data, 0, sizeof(sc->sc_data));
329
330 if ((sc->sc_state < 0) || (sc->sc_state >= G_KEYBOARD_MAX_STRLEN))
331 sc->sc_state = 0;
332
333 switch (sc->sc_state % 6) {
334 case 0:
335 sc->sc_data[0].keycode[0] =
336 g_keyboard_get_keycode(sc, sc->sc_state + 0);
337 case 1:
338 sc->sc_data[0].keycode[1] =
339 g_keyboard_get_keycode(sc, sc->sc_state + 1);
340 case 2:
341 sc->sc_data[0].keycode[2] =
342 g_keyboard_get_keycode(sc, sc->sc_state + 2);
343 case 3:
344 sc->sc_data[0].keycode[3] =
345 g_keyboard_get_keycode(sc, sc->sc_state + 3);
346 case 4:
347 sc->sc_data[0].keycode[4] =
348 g_keyboard_get_keycode(sc, sc->sc_state + 4);
349 default:
350 sc->sc_data[0].keycode[5] =
351 g_keyboard_get_keycode(sc, sc->sc_state + 5);
352 }
353
354 sc->sc_state++;
355
356 usbd_xfer_set_frame_data(xfer, 0, &sc->sc_data[0], sizeof(sc->sc_data[0]));
357 usbd_xfer_set_frame_data(xfer, 1, &sc->sc_data[1], sizeof(sc->sc_data[1]));
358 usbd_xfer_set_frames(xfer, 2);
359 usbd_transfer_submit(xfer);
360 }
361 break;
362
363 default: /* Error */
364 DPRINTF("error=%s\n", usbd_errstr(error));
365
366 if (error != USB_ERR_CANCELLED) {
367 /* try to clear stall first */
368 usbd_xfer_set_stall(xfer);
369 goto tr_setup;
370 }
371 break;
372 }
373 }
374
375 static int
g_keyboard_handle_request(device_t dev,const void * preq,void ** pptr,uint16_t * plen,uint16_t offset,uint8_t * pstate)376 g_keyboard_handle_request(device_t dev,
377 const void *preq, void **pptr, uint16_t *plen,
378 uint16_t offset, uint8_t *pstate)
379 {
380 struct g_keyboard_softc *sc = device_get_softc(dev);
381 const struct usb_device_request *req = preq;
382 uint8_t is_complete = *pstate;
383
384 if (!is_complete) {
385 if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) &&
386 (req->bRequest == UR_SET_REPORT) &&
387 (req->wValue[0] == 0x00) &&
388 (req->wValue[1] == 0x02)) {
389 if (offset == 0) {
390 *plen = sizeof(sc->sc_led_state);
391 *pptr = &sc->sc_led_state;
392 } else {
393 *plen = 0;
394 }
395 return (0);
396 } else if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) &&
397 (req->bRequest == UR_SET_PROTOCOL) &&
398 (req->wValue[0] == 0x00) &&
399 (req->wValue[1] == 0x00)) {
400 *plen = 0;
401 return (0);
402 } else if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) &&
403 (req->bRequest == UR_SET_IDLE)) {
404 *plen = 0;
405 return (0);
406 }
407 }
408 return (ENXIO); /* use builtin handler */
409 }
410