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