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