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