xref: /freebsd/sys/dev/usb/gadget/g_mouse.c (revision 62cfcf62f627e5093fb37026a6d8c98e4d2ef04c)
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_mouse_debug
60 #include <dev/usb/usb_debug.h>
61 
62 #include <dev/usb/gadget/g_mouse.h>
63 
64 static SYSCTL_NODE(_hw_usb, OID_AUTO, g_mouse, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
65     "USB mouse gadget");
66 
67 #ifdef USB_DEBUG
68 static int g_mouse_debug = 0;
69 
70 SYSCTL_INT(_hw_usb_g_mouse, OID_AUTO, debug, CTLFLAG_RWTUN,
71     &g_mouse_debug, 0, "Debug level");
72 #endif
73 
74 static int g_mouse_mode = 0;
75 
76 SYSCTL_INT(_hw_usb_g_mouse, OID_AUTO, mode, CTLFLAG_RWTUN,
77     &g_mouse_mode, 0, "Mode selection");
78 
79 static int g_mouse_button_press_interval = 0;
80 
81 SYSCTL_INT(_hw_usb_g_mouse, OID_AUTO, button_press_interval, CTLFLAG_RWTUN,
82     &g_mouse_button_press_interval, 0, "Mouse button update interval in milliseconds");
83 
84 static int g_mouse_cursor_update_interval = 1023;
85 
86 SYSCTL_INT(_hw_usb_g_mouse, OID_AUTO, cursor_update_interval, CTLFLAG_RWTUN,
87     &g_mouse_cursor_update_interval, 0, "Mouse cursor update interval in milliseconds");
88 
89 static int g_mouse_cursor_radius = 128;
90 
91 SYSCTL_INT(_hw_usb_g_mouse, OID_AUTO, cursor_radius, CTLFLAG_RWTUN,
92     &g_mouse_cursor_radius, 0, "Mouse cursor radius in pixels");
93 
94 struct g_mouse_data {
95 	uint8_t buttons;
96 #define	BUT_0 0x01
97 #define	BUT_1 0x02
98 #define	BUT_2 0x04
99 	int8_t dx;
100 	int8_t dy;
101 	int8_t dz;
102 };
103 
104 enum {
105 	G_MOUSE_INTR_DT,
106 	G_MOUSE_N_TRANSFER,
107 };
108 
109 struct g_mouse_softc {
110 	struct mtx sc_mtx;
111 	struct usb_callout sc_button_press_callout;
112 	struct usb_callout sc_cursor_update_callout;
113 	struct g_mouse_data sc_data;
114 	struct usb_xfer *sc_xfer[G_MOUSE_N_TRANSFER];
115 
116 	int	sc_mode;
117 	int	sc_radius;
118 	int	sc_last_x_state;
119 	int	sc_last_y_state;
120 	int	sc_curr_x_state;
121 	int	sc_curr_y_state;
122 	int	sc_tick;
123 
124 	uint8_t sc_do_cursor_update;
125 	uint8_t sc_do_button_update;
126 };
127 
128 static device_probe_t g_mouse_probe;
129 static device_attach_t g_mouse_attach;
130 static device_detach_t g_mouse_detach;
131 static usb_handle_request_t g_mouse_handle_request;
132 static usb_callback_t g_mouse_intr_callback;
133 
134 static devclass_t g_mouse_devclass;
135 
136 static device_method_t g_mouse_methods[] = {
137 	/* USB interface */
138 	DEVMETHOD(usb_handle_request, g_mouse_handle_request),
139 
140 	/* Device interface */
141 	DEVMETHOD(device_probe, g_mouse_probe),
142 	DEVMETHOD(device_attach, g_mouse_attach),
143 	DEVMETHOD(device_detach, g_mouse_detach),
144 
145 	DEVMETHOD_END
146 };
147 
148 static driver_t g_mouse_driver = {
149 	.name = "g_mouse",
150 	.methods = g_mouse_methods,
151 	.size = sizeof(struct g_mouse_softc),
152 };
153 
154 DRIVER_MODULE(g_mouse, uhub, g_mouse_driver, g_mouse_devclass, 0, 0);
155 MODULE_DEPEND(g_mouse, usb, 1, 1, 1);
156 
157 static const struct usb_config g_mouse_config[G_MOUSE_N_TRANSFER] = {
158 
159 	[G_MOUSE_INTR_DT] = {
160 		.type = UE_INTERRUPT,
161 		.endpoint = UE_ADDR_ANY,
162 		.direction = UE_DIR_IN,
163 		.flags = {.ext_buffer = 1,.pipe_bof = 1,},
164 		.bufsize = sizeof(struct g_mouse_data),
165 		.callback = &g_mouse_intr_callback,
166 		.frames = 1,
167 		.usb_mode = USB_MODE_DEVICE,
168 	},
169 };
170 
171 static void g_mouse_button_press_timeout(void *arg);
172 static void g_mouse_cursor_update_timeout(void *arg);
173 
174 static void
175 g_mouse_button_press_timeout_reset(struct g_mouse_softc *sc)
176 {
177 	int i = g_mouse_button_press_interval;
178 
179 	if (i <= 0) {
180 		sc->sc_data.buttons = 0;
181 		sc->sc_do_button_update = 0;
182 	} else {
183 		sc->sc_do_button_update = 1;
184 	}
185 
186 	if ((i <= 0) || (i > 1023))
187 		i = 1023;
188 
189 	i = USB_MS_TO_TICKS(i);
190 
191 	usb_callout_reset(&sc->sc_button_press_callout, i,
192 	    &g_mouse_button_press_timeout, sc);
193 }
194 
195 static void
196 g_mouse_cursor_update_timeout_reset(struct g_mouse_softc *sc)
197 {
198 	int i = g_mouse_cursor_update_interval;
199 
200 	if (i <= 0) {
201 		sc->sc_data.dx = 0;
202 		sc->sc_data.dy = 0;
203 		sc->sc_do_cursor_update = 0;
204 		sc->sc_tick = 0;
205 	} else {
206 		sc->sc_do_cursor_update = 1;
207 	}
208 
209 	if ((i <= 0) || (i > 1023))
210 		i = 1023;
211 
212 	i = USB_MS_TO_TICKS(i);
213 
214 	usb_callout_reset(&sc->sc_cursor_update_callout, i,
215 	    &g_mouse_cursor_update_timeout, sc);
216 }
217 
218 static void
219 g_mouse_update_mode_radius(struct g_mouse_softc *sc)
220 {
221 	sc->sc_mode = g_mouse_mode;
222 	sc->sc_radius = g_mouse_cursor_radius;
223 
224 	if (sc->sc_radius < 0)
225 		sc->sc_radius = 0;
226 	else if (sc->sc_radius > 1023)
227 		sc->sc_radius = 1023;
228 }
229 
230 static void
231 g_mouse_button_press_timeout(void *arg)
232 {
233 	struct g_mouse_softc *sc = arg;
234 
235 	g_mouse_update_mode_radius(sc);
236 
237 	DPRINTFN(11, "Timeout %p (button press)\n", sc->sc_xfer[G_MOUSE_INTR_DT]);
238 
239 	g_mouse_button_press_timeout_reset(sc);
240 
241 	usbd_transfer_start(sc->sc_xfer[G_MOUSE_INTR_DT]);
242 }
243 
244 static void
245 g_mouse_cursor_update_timeout(void *arg)
246 {
247 	struct g_mouse_softc *sc = arg;
248 
249 	g_mouse_update_mode_radius(sc);
250 
251 	DPRINTFN(11, "Timeout %p (cursor update)\n", sc->sc_xfer[G_MOUSE_INTR_DT]);
252 
253 	g_mouse_cursor_update_timeout_reset(sc);
254 
255 	usbd_transfer_start(sc->sc_xfer[G_MOUSE_INTR_DT]);
256 }
257 
258 static int
259 g_mouse_probe(device_t dev)
260 {
261 	struct usb_attach_arg *uaa = device_get_ivars(dev);
262 
263 	DPRINTFN(11, "\n");
264 
265 	if (uaa->usb_mode != USB_MODE_DEVICE)
266 		return (ENXIO);
267 
268 	if ((uaa->info.bInterfaceClass == UICLASS_HID) &&
269 	    (uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) &&
270 	    (uaa->info.bInterfaceProtocol == UIPROTO_MOUSE))
271 		return (0);
272 
273 	return (ENXIO);
274 }
275 
276 static int
277 g_mouse_attach(device_t dev)
278 {
279 	struct g_mouse_softc *sc = device_get_softc(dev);
280 	struct usb_attach_arg *uaa = device_get_ivars(dev);
281 	int error;
282 
283 	DPRINTFN(11, "\n");
284 
285 	device_set_usb_desc(dev);
286 
287 	mtx_init(&sc->sc_mtx, "g_mouse", NULL, MTX_DEF);
288 
289 	usb_callout_init_mtx(&sc->sc_button_press_callout, &sc->sc_mtx, 0);
290 	usb_callout_init_mtx(&sc->sc_cursor_update_callout, &sc->sc_mtx, 0);
291 
292 	sc->sc_mode = G_MOUSE_MODE_SILENT;
293 
294 	error = usbd_transfer_setup(uaa->device,
295 	    &uaa->info.bIfaceIndex, sc->sc_xfer, g_mouse_config,
296 	    G_MOUSE_N_TRANSFER, sc, &sc->sc_mtx);
297 
298 	if (error) {
299 		DPRINTF("error=%s\n", usbd_errstr(error));
300 		goto detach;
301 	}
302 
303 	mtx_lock(&sc->sc_mtx);
304 	g_mouse_button_press_timeout_reset(sc);
305 	g_mouse_cursor_update_timeout_reset(sc);
306 	mtx_unlock(&sc->sc_mtx);
307 
308 	return (0);			/* success */
309 
310 detach:
311 	g_mouse_detach(dev);
312 
313 	return (ENXIO);			/* error */
314 }
315 
316 static int
317 g_mouse_detach(device_t dev)
318 {
319 	struct g_mouse_softc *sc = device_get_softc(dev);
320 
321 	DPRINTF("\n");
322 
323 	mtx_lock(&sc->sc_mtx);
324 	usb_callout_stop(&sc->sc_button_press_callout);
325 	usb_callout_stop(&sc->sc_cursor_update_callout);
326 	mtx_unlock(&sc->sc_mtx);
327 
328 	usbd_transfer_unsetup(sc->sc_xfer, G_MOUSE_N_TRANSFER);
329 
330 	usb_callout_drain(&sc->sc_button_press_callout);
331 	usb_callout_drain(&sc->sc_cursor_update_callout);
332 
333 	mtx_destroy(&sc->sc_mtx);
334 
335 	return (0);
336 }
337 
338 static void
339 g_mouse_intr_callback(struct usb_xfer *xfer, usb_error_t error)
340 {
341 	struct g_mouse_softc *sc = usbd_xfer_softc(xfer);
342 	int actlen;
343 	int aframes;
344 	int dx;
345 	int dy;
346 	int radius;
347 
348 	usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
349 
350 	DPRINTF("st=%d aframes=%d actlen=%d bytes\n",
351 	    USB_GET_STATE(xfer), aframes, actlen);
352 
353 	switch (USB_GET_STATE(xfer)) {
354 	case USB_ST_TRANSFERRED:
355 		if (!(sc->sc_do_cursor_update || sc->sc_do_button_update))
356 			break;
357 
358 	case USB_ST_SETUP:
359 tr_setup:
360 
361 	  if (sc->sc_do_cursor_update) {
362 		sc->sc_do_cursor_update = 0;
363 		sc->sc_tick += 80;
364 		if ((sc->sc_tick < 0) || (sc->sc_tick > 7999))
365 			sc->sc_tick = 0;
366 	  }
367 
368 	  if (sc->sc_do_button_update) {
369 			sc->sc_do_button_update = 0;
370 			sc->sc_data.buttons ^= BUT_0;
371 	  }
372 
373 	  radius = sc->sc_radius;
374 
375 		switch (sc->sc_mode) {
376 		case G_MOUSE_MODE_SILENT:
377 			sc->sc_data.buttons = 0;
378 			break;
379 		case G_MOUSE_MODE_SPIRAL:
380 			radius = (radius * (8000-sc->sc_tick)) / 8000;
381 		case G_MOUSE_MODE_CIRCLE:
382 			/* TODO */
383 			sc->sc_curr_y_state = 0;
384 			sc->sc_curr_x_state = 0;
385 			break;
386 		case G_MOUSE_MODE_BOX:
387 			if (sc->sc_tick < 2000) {
388 				sc->sc_curr_x_state = (sc->sc_tick * radius) / 2000;
389 				sc->sc_curr_y_state = 0;
390 			} else if (sc->sc_tick < 4000) {
391 				sc->sc_curr_x_state = radius;
392 				sc->sc_curr_y_state = -(((sc->sc_tick - 2000) * radius) / 2000);
393 			} else if (sc->sc_tick < 6000) {
394 				sc->sc_curr_x_state = radius - (((sc->sc_tick - 4000) * radius) / 2000);
395 				sc->sc_curr_y_state = -radius;
396 			} else {
397 				sc->sc_curr_x_state = 0;
398 				sc->sc_curr_y_state = -radius + (((sc->sc_tick - 6000) * radius) / 2000);
399 			}
400 			break;
401 		default:
402 			break;
403 		}
404 
405 		dx = sc->sc_curr_x_state - sc->sc_last_x_state;
406 		dy = sc->sc_curr_y_state - sc->sc_last_y_state;
407 
408 		if (dx < -63)
409 		  dx = -63;
410 		else if (dx > 63)
411 		  dx = 63;
412 
413 		if (dy < -63)
414 		  dy = -63;
415 		else if (dy > 63)
416 		  dy = 63;
417 
418 		sc->sc_last_x_state += dx;
419 		sc->sc_last_y_state += dy;
420 
421 		sc->sc_data.dx = dx;
422 		sc->sc_data.dy = dy;
423 
424 		usbd_xfer_set_frame_data(xfer, 0, &sc->sc_data, sizeof(sc->sc_data));
425 		usbd_xfer_set_frames(xfer, 1);
426 		usbd_transfer_submit(xfer);
427 		break;
428 
429 	default:			/* Error */
430 		DPRINTF("error=%s\n", usbd_errstr(error));
431 
432 		if (error != USB_ERR_CANCELLED) {
433 			/* try to clear stall first */
434 			usbd_xfer_set_stall(xfer);
435 			goto tr_setup;
436 		}
437 		break;
438 	}
439 }
440 
441 static int
442 g_mouse_handle_request(device_t dev,
443     const void *preq, void **pptr, uint16_t *plen,
444     uint16_t offset, uint8_t *pstate)
445 {
446 	const struct usb_device_request *req = preq;
447 	uint8_t is_complete = *pstate;
448 
449 	if (!is_complete) {
450 		if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) &&
451 		    (req->bRequest == UR_SET_PROTOCOL) &&
452 		    (req->wValue[0] == 0x00) &&
453 		    (req->wValue[1] == 0x00)) {
454 			*plen = 0;
455 			return (0);
456 		}
457 	}
458 	return (ENXIO);			/* use builtin handler */
459 }
460