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