2 * Copyright (c) 2010 Hans Petter Selasky. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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
25 * $FreeBSD: head/sys/dev/usb/gadget/g_mouse.c 253618 2013-07-24 18:32:15Z obrien $
29 * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
32 #include <sys/stdint.h>
33 #include <sys/queue.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
37 #include <sys/linker_set.h>
38 #include <sys/module.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>
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>
53 #define USB_DEBUG_VAR g_mouse_debug
54 #include <bus/u4b/usb_debug.h>
56 #include <bus/u4b/gadget/g_mouse.h>
58 static SYSCTL_NODE(_hw_usb, OID_AUTO, g_mouse, CTLFLAG_RW, 0, "USB mouse gadget");
61 static int g_mouse_debug = 0;
63 SYSCTL_INT(_hw_usb_g_mouse, OID_AUTO, debug, CTLFLAG_RW,
64 &g_mouse_debug, 0, "Debug level");
67 static int g_mouse_mode = 0;
69 SYSCTL_INT(_hw_usb_g_mouse, OID_AUTO, mode, CTLFLAG_RW,
70 &g_mouse_mode, 0, "Mode selection");
72 static int g_mouse_button_press_interval = 0;
74 SYSCTL_INT(_hw_usb_g_mouse, OID_AUTO, button_press_interval, CTLFLAG_RW,
75 &g_mouse_button_press_interval, 0, "Mouse button update interval in milliseconds");
77 static int g_mouse_cursor_update_interval = 1023;
79 SYSCTL_INT(_hw_usb_g_mouse, OID_AUTO, cursor_update_interval, CTLFLAG_RW,
80 &g_mouse_cursor_update_interval, 0, "Mouse cursor update interval in milliseconds");
82 static int g_mouse_cursor_radius = 128;
84 SYSCTL_INT(_hw_usb_g_mouse, OID_AUTO, cursor_radius, CTLFLAG_RW,
85 &g_mouse_cursor_radius, 0, "Mouse cursor radius in pixels");
102 struct g_mouse_softc {
104 struct usb_callout sc_button_press_callout;
105 struct usb_callout sc_cursor_update_callout;
106 struct g_mouse_data sc_data;
107 struct usb_xfer *sc_xfer[G_MOUSE_N_TRANSFER];
117 uint8_t sc_do_cursor_update;
118 uint8_t sc_do_button_update;
121 static device_probe_t g_mouse_probe;
122 static device_attach_t g_mouse_attach;
123 static device_detach_t g_mouse_detach;
124 static usb_handle_request_t g_mouse_handle_request;
125 static usb_callback_t g_mouse_intr_callback;
127 static devclass_t g_mouse_devclass;
129 static device_method_t g_mouse_methods[] = {
131 DEVMETHOD(usb_handle_request, g_mouse_handle_request),
133 /* Device interface */
134 DEVMETHOD(device_probe, g_mouse_probe),
135 DEVMETHOD(device_attach, g_mouse_attach),
136 DEVMETHOD(device_detach, g_mouse_detach),
141 static driver_t g_mouse_driver = {
143 .methods = g_mouse_methods,
144 .size = sizeof(struct g_mouse_softc),
147 DRIVER_MODULE(g_mouse, uhub, g_mouse_driver, g_mouse_devclass, NULL, NULL);
148 MODULE_DEPEND(g_mouse, usb, 1, 1, 1);
150 static const struct usb_config g_mouse_config[G_MOUSE_N_TRANSFER] = {
152 [G_MOUSE_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_mouse_data),
158 .callback = &g_mouse_intr_callback,
160 .usb_mode = USB_MODE_DEVICE,
164 static void g_mouse_button_press_timeout(void *arg);
165 static void g_mouse_cursor_update_timeout(void *arg);
168 g_mouse_button_press_timeout_reset(struct g_mouse_softc *sc)
170 int i = g_mouse_button_press_interval;
173 sc->sc_data.buttons = 0;
174 sc->sc_do_button_update = 0;
176 sc->sc_do_button_update = 1;
179 if ((i <= 0) || (i > 1023))
182 i = USB_MS_TO_TICKS(i);
184 usb_callout_reset(&sc->sc_button_press_callout, i,
185 &g_mouse_button_press_timeout, sc);
189 g_mouse_cursor_update_timeout_reset(struct g_mouse_softc *sc)
191 int i = g_mouse_cursor_update_interval;
196 sc->sc_do_cursor_update = 0;
199 sc->sc_do_cursor_update = 1;
202 if ((i <= 0) || (i > 1023))
205 i = USB_MS_TO_TICKS(i);
207 usb_callout_reset(&sc->sc_cursor_update_callout, i,
208 &g_mouse_cursor_update_timeout, sc);
212 g_mouse_update_mode_radius(struct g_mouse_softc *sc)
214 sc->sc_mode = g_mouse_mode;
215 sc->sc_radius = g_mouse_cursor_radius;
217 if (sc->sc_radius < 0)
219 else if (sc->sc_radius > 1023)
220 sc->sc_radius = 1023;
224 g_mouse_button_press_timeout(void *arg)
226 struct g_mouse_softc *sc = arg;
228 g_mouse_update_mode_radius(sc);
230 DPRINTFN(11, "Timeout %p (button press)\n", sc->sc_xfer[G_MOUSE_INTR_DT]);
232 g_mouse_button_press_timeout_reset(sc);
234 usbd_transfer_start(sc->sc_xfer[G_MOUSE_INTR_DT]);
238 g_mouse_cursor_update_timeout(void *arg)
240 struct g_mouse_softc *sc = arg;
242 g_mouse_update_mode_radius(sc);
244 DPRINTFN(11, "Timeout %p (cursor update)\n", sc->sc_xfer[G_MOUSE_INTR_DT]);
246 g_mouse_cursor_update_timeout_reset(sc);
248 usbd_transfer_start(sc->sc_xfer[G_MOUSE_INTR_DT]);
252 g_mouse_probe(device_t dev)
254 struct usb_attach_arg *uaa = device_get_ivars(dev);
258 if (uaa->usb_mode != USB_MODE_DEVICE)
261 if ((uaa->info.bInterfaceClass == UICLASS_HID) &&
262 (uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) &&
263 (uaa->info.bInterfaceProtocol == UIPROTO_MOUSE))
270 g_mouse_attach(device_t dev)
272 struct g_mouse_softc *sc = device_get_softc(dev);
273 struct usb_attach_arg *uaa = device_get_ivars(dev);
278 device_set_usb_desc(dev);
280 lockinit(&sc->sc_lock, "g_mouse", 0, 0);
282 usb_callout_init_mtx(&sc->sc_button_press_callout, &sc->sc_lock, 0);
283 usb_callout_init_mtx(&sc->sc_cursor_update_callout, &sc->sc_lock, 0);
285 sc->sc_mode = G_MOUSE_MODE_SILENT;
287 error = usbd_transfer_setup(uaa->device,
288 &uaa->info.bIfaceIndex, sc->sc_xfer, g_mouse_config,
289 G_MOUSE_N_TRANSFER, sc, &sc->sc_lock);
292 DPRINTF("error=%s\n", usbd_errstr(error));
296 lockmgr(&sc->sc_lock, LK_EXCLUSIVE);
297 g_mouse_button_press_timeout_reset(sc);
298 g_mouse_cursor_update_timeout_reset(sc);
299 lockmgr(&sc->sc_lock, LK_RELEASE);
301 return (0); /* success */
306 return (ENXIO); /* error */
310 g_mouse_detach(device_t dev)
312 struct g_mouse_softc *sc = device_get_softc(dev);
316 lockmgr(&sc->sc_lock, LK_EXCLUSIVE);
317 usb_callout_stop(&sc->sc_button_press_callout);
318 usb_callout_stop(&sc->sc_cursor_update_callout);
319 lockmgr(&sc->sc_lock, LK_RELEASE);
321 usbd_transfer_unsetup(sc->sc_xfer, G_MOUSE_N_TRANSFER);
323 usb_callout_drain(&sc->sc_button_press_callout);
324 usb_callout_drain(&sc->sc_cursor_update_callout);
326 lockuninit(&sc->sc_lock);
332 g_mouse_intr_callback(struct usb_xfer *xfer, usb_error_t error)
334 struct g_mouse_softc *sc = usbd_xfer_softc(xfer);
341 usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
343 DPRINTF("st=%d aframes=%d actlen=%d bytes\n",
344 USB_GET_STATE(xfer), aframes, actlen);
346 switch (USB_GET_STATE(xfer)) {
347 case USB_ST_TRANSFERRED:
348 if (!(sc->sc_do_cursor_update || sc->sc_do_button_update))
354 if (sc->sc_do_cursor_update) {
355 sc->sc_do_cursor_update = 0;
357 if ((sc->sc_tick < 0) || (sc->sc_tick > 7999))
361 if (sc->sc_do_button_update) {
362 sc->sc_do_button_update = 0;
363 sc->sc_data.buttons ^= BUT_0;
366 radius = sc->sc_radius;
368 switch (sc->sc_mode) {
369 case G_MOUSE_MODE_SILENT:
370 sc->sc_data.buttons = 0;
372 case G_MOUSE_MODE_SPIRAL:
373 radius = (radius * (8000-sc->sc_tick)) / 8000;
374 case G_MOUSE_MODE_CIRCLE:
376 sc->sc_curr_y_state = 0;
377 sc->sc_curr_x_state = 0;
379 case G_MOUSE_MODE_BOX:
380 if (sc->sc_tick < 2000) {
381 sc->sc_curr_x_state = (sc->sc_tick * radius) / 2000;
382 sc->sc_curr_y_state = 0;
383 } else if (sc->sc_tick < 4000) {
384 sc->sc_curr_x_state = radius;
385 sc->sc_curr_y_state = -(((sc->sc_tick - 2000) * radius) / 2000);
386 } else if (sc->sc_tick < 6000) {
387 sc->sc_curr_x_state = radius - (((sc->sc_tick - 4000) * radius) / 2000);
388 sc->sc_curr_y_state = -radius;
390 sc->sc_curr_x_state = 0;
391 sc->sc_curr_y_state = -radius + (((sc->sc_tick - 6000) * radius) / 2000);
398 dx = sc->sc_curr_x_state - sc->sc_last_x_state;
399 dy = sc->sc_curr_y_state - sc->sc_last_y_state;
411 sc->sc_last_x_state += dx;
412 sc->sc_last_y_state += dy;
417 usbd_xfer_set_frame_data(xfer, 0, &sc->sc_data, sizeof(sc->sc_data));
418 usbd_xfer_set_frames(xfer, 1);
419 usbd_transfer_submit(xfer);
423 DPRINTF("error=%s\n", usbd_errstr(error));
425 if (error != USB_ERR_CANCELLED) {
426 /* try to clear stall first */
427 usbd_xfer_set_stall(xfer);
435 g_mouse_handle_request(device_t dev,
436 const void *preq, void **pptr, uint16_t *plen,
437 uint16_t offset, uint8_t *pstate)
439 const struct usb_device_request *req = preq;
440 uint8_t is_complete = *pstate;
443 if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) &&
444 (req->bRequest == UR_SET_PROTOCOL) &&
445 (req->wValue[0] == 0x00) &&
446 (req->wValue[1] == 0x00)) {
451 return (ENXIO); /* use builtin handler */