1 /*- 2 * Copyright (c) 1998 The NetBSD Foundation, Inc. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to The NetBSD Foundation 6 * by Lennart Augustsson (lennart@augustsson.net) at 7 * Carlstedt Research & Technology. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 /* 35 * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf 36 */ 37 38 #include "opt_evdev.h" 39 40 #include <sys/stdint.h> 41 #include <sys/stddef.h> 42 #include <sys/param.h> 43 #include <sys/queue.h> 44 #include <sys/types.h> 45 #include <sys/systm.h> 46 #include <sys/kernel.h> 47 #include <sys/bus.h> 48 #include <sys/module.h> 49 #include <sys/lock.h> 50 #include <sys/mutex.h> 51 #include <sys/condvar.h> 52 #include <sys/sysctl.h> 53 #include <sys/sx.h> 54 #include <sys/unistd.h> 55 #include <sys/callout.h> 56 #include <sys/malloc.h> 57 #include <sys/priv.h> 58 #include <sys/conf.h> 59 #include <sys/fcntl.h> 60 #include <sys/sbuf.h> 61 62 #include <dev/usb/usb.h> 63 #include <dev/usb/usbdi.h> 64 #include <dev/usb/usbdi_util.h> 65 #include <dev/usb/usbhid.h> 66 #include "usbdevs.h" 67 68 #define USB_DEBUG_VAR ums_debug 69 #include <dev/usb/usb_debug.h> 70 71 #include <dev/usb/quirk/usb_quirk.h> 72 73 #ifdef EVDEV 74 #include <dev/evdev/input.h> 75 #include <dev/evdev/evdev.h> 76 #endif 77 78 #include <sys/ioccom.h> 79 #include <sys/filio.h> 80 #include <sys/tty.h> 81 #include <sys/mouse.h> 82 83 #ifdef USB_DEBUG 84 static int ums_debug = 0; 85 86 static SYSCTL_NODE(_hw_usb, OID_AUTO, ums, CTLFLAG_RW, 0, "USB ums"); 87 SYSCTL_INT(_hw_usb_ums, OID_AUTO, debug, CTLFLAG_RWTUN, 88 &ums_debug, 0, "Debug level"); 89 #endif 90 91 #define MOUSE_FLAGS_MASK (HIO_CONST|HIO_RELATIVE) 92 #define MOUSE_FLAGS (HIO_RELATIVE) 93 94 #define UMS_BUF_SIZE 8 /* bytes */ 95 #define UMS_IFQ_MAXLEN 50 /* units */ 96 #define UMS_BUTTON_MAX 31 /* exclusive, must be less than 32 */ 97 #define UMS_BUT(i) ((i) < 3 ? (((i) + 2) % 3) : (i)) 98 #define UMS_INFO_MAX 2 /* maximum number of HID sets */ 99 100 enum { 101 UMS_INTR_DT, 102 UMS_N_TRANSFER, 103 }; 104 105 struct ums_info { 106 struct hid_location sc_loc_w; 107 struct hid_location sc_loc_x; 108 struct hid_location sc_loc_y; 109 struct hid_location sc_loc_z; 110 struct hid_location sc_loc_t; 111 struct hid_location sc_loc_btn[UMS_BUTTON_MAX]; 112 113 uint32_t sc_flags; 114 #define UMS_FLAG_X_AXIS 0x0001 115 #define UMS_FLAG_Y_AXIS 0x0002 116 #define UMS_FLAG_Z_AXIS 0x0004 117 #define UMS_FLAG_T_AXIS 0x0008 118 #define UMS_FLAG_SBU 0x0010 /* spurious button up events */ 119 #define UMS_FLAG_REVZ 0x0020 /* Z-axis is reversed */ 120 #define UMS_FLAG_W_AXIS 0x0040 121 122 uint8_t sc_iid_w; 123 uint8_t sc_iid_x; 124 uint8_t sc_iid_y; 125 uint8_t sc_iid_z; 126 uint8_t sc_iid_t; 127 uint8_t sc_iid_btn[UMS_BUTTON_MAX]; 128 uint8_t sc_buttons; 129 }; 130 131 struct ums_softc { 132 struct usb_fifo_sc sc_fifo; 133 struct mtx sc_mtx; 134 struct usb_callout sc_callout; 135 struct ums_info sc_info[UMS_INFO_MAX]; 136 137 mousehw_t sc_hw; 138 mousemode_t sc_mode; 139 mousestatus_t sc_status; 140 141 struct usb_xfer *sc_xfer[UMS_N_TRANSFER]; 142 143 int sc_pollrate; 144 int sc_fflags; 145 #ifdef EVDEV 146 int sc_evflags; 147 #define UMS_EVDEV_OPENED 1 148 #endif 149 150 uint8_t sc_buttons; 151 uint8_t sc_iid; 152 uint8_t sc_temp[64]; 153 154 #ifdef EVDEV 155 struct evdev_dev *sc_evdev; 156 #endif 157 }; 158 159 static void ums_put_queue_timeout(void *__sc); 160 161 static usb_callback_t ums_intr_callback; 162 163 static device_probe_t ums_probe; 164 static device_attach_t ums_attach; 165 static device_detach_t ums_detach; 166 167 static usb_fifo_cmd_t ums_fifo_start_read; 168 static usb_fifo_cmd_t ums_fifo_stop_read; 169 static usb_fifo_open_t ums_fifo_open; 170 static usb_fifo_close_t ums_fifo_close; 171 static usb_fifo_ioctl_t ums_fifo_ioctl; 172 173 #ifdef EVDEV 174 static evdev_open_t ums_ev_open; 175 static evdev_close_t ums_ev_close; 176 #endif 177 178 static void ums_start_rx(struct ums_softc *); 179 static void ums_stop_rx(struct ums_softc *); 180 static void ums_put_queue(struct ums_softc *, int32_t, int32_t, 181 int32_t, int32_t, int32_t); 182 static int ums_sysctl_handler_parseinfo(SYSCTL_HANDLER_ARGS); 183 184 static struct usb_fifo_methods ums_fifo_methods = { 185 .f_open = &ums_fifo_open, 186 .f_close = &ums_fifo_close, 187 .f_ioctl = &ums_fifo_ioctl, 188 .f_start_read = &ums_fifo_start_read, 189 .f_stop_read = &ums_fifo_stop_read, 190 .basename[0] = "ums", 191 }; 192 193 #ifdef EVDEV 194 static struct evdev_methods ums_evdev_methods = { 195 .ev_open = &ums_ev_open, 196 .ev_close = &ums_ev_close, 197 }; 198 #endif 199 200 static void 201 ums_put_queue_timeout(void *__sc) 202 { 203 struct ums_softc *sc = __sc; 204 205 mtx_assert(&sc->sc_mtx, MA_OWNED); 206 207 ums_put_queue(sc, 0, 0, 0, 0, 0); 208 } 209 210 static void 211 ums_intr_callback(struct usb_xfer *xfer, usb_error_t error) 212 { 213 struct ums_softc *sc = usbd_xfer_softc(xfer); 214 struct ums_info *info = &sc->sc_info[0]; 215 struct usb_page_cache *pc; 216 uint8_t *buf = sc->sc_temp; 217 int32_t buttons = 0; 218 int32_t buttons_found = 0; 219 int32_t dw = 0; 220 int32_t dx = 0; 221 int32_t dy = 0; 222 int32_t dz = 0; 223 int32_t dt = 0; 224 uint8_t i; 225 uint8_t id; 226 int len; 227 228 usbd_xfer_status(xfer, &len, NULL, NULL, NULL); 229 230 switch (USB_GET_STATE(xfer)) { 231 case USB_ST_TRANSFERRED: 232 DPRINTFN(6, "sc=%p actlen=%d\n", sc, len); 233 234 if (len > (int)sizeof(sc->sc_temp)) { 235 DPRINTFN(6, "truncating large packet to %zu bytes\n", 236 sizeof(sc->sc_temp)); 237 len = sizeof(sc->sc_temp); 238 } 239 if (len == 0) 240 goto tr_setup; 241 242 pc = usbd_xfer_get_frame(xfer, 0); 243 usbd_copy_out(pc, 0, buf, len); 244 245 DPRINTFN(6, "data = %02x %02x %02x %02x " 246 "%02x %02x %02x %02x\n", 247 (len > 0) ? buf[0] : 0, (len > 1) ? buf[1] : 0, 248 (len > 2) ? buf[2] : 0, (len > 3) ? buf[3] : 0, 249 (len > 4) ? buf[4] : 0, (len > 5) ? buf[5] : 0, 250 (len > 6) ? buf[6] : 0, (len > 7) ? buf[7] : 0); 251 252 if (sc->sc_iid) { 253 id = *buf; 254 255 len--; 256 buf++; 257 258 } else { 259 id = 0; 260 if (sc->sc_info[0].sc_flags & UMS_FLAG_SBU) { 261 if ((*buf == 0x14) || (*buf == 0x15)) { 262 goto tr_setup; 263 } 264 } 265 } 266 267 repeat: 268 if ((info->sc_flags & UMS_FLAG_W_AXIS) && 269 (id == info->sc_iid_w)) 270 dw += hid_get_data(buf, len, &info->sc_loc_w); 271 272 if ((info->sc_flags & UMS_FLAG_X_AXIS) && 273 (id == info->sc_iid_x)) 274 dx += hid_get_data(buf, len, &info->sc_loc_x); 275 276 if ((info->sc_flags & UMS_FLAG_Y_AXIS) && 277 (id == info->sc_iid_y)) 278 dy = -hid_get_data(buf, len, &info->sc_loc_y); 279 280 if ((info->sc_flags & UMS_FLAG_Z_AXIS) && 281 (id == info->sc_iid_z)) { 282 int32_t temp; 283 temp = hid_get_data(buf, len, &info->sc_loc_z); 284 if (info->sc_flags & UMS_FLAG_REVZ) 285 temp = -temp; 286 dz -= temp; 287 } 288 289 if ((info->sc_flags & UMS_FLAG_T_AXIS) && 290 (id == info->sc_iid_t)) 291 dt -= hid_get_data(buf, len, &info->sc_loc_t); 292 293 for (i = 0; i < info->sc_buttons; i++) { 294 uint32_t mask; 295 mask = 1UL << UMS_BUT(i); 296 /* check for correct button ID */ 297 if (id != info->sc_iid_btn[i]) 298 continue; 299 /* check for button pressed */ 300 if (hid_get_data(buf, len, &info->sc_loc_btn[i])) 301 buttons |= mask; 302 /* register button mask */ 303 buttons_found |= mask; 304 } 305 306 if (++info != &sc->sc_info[UMS_INFO_MAX]) 307 goto repeat; 308 309 /* keep old button value(s) for non-detected buttons */ 310 buttons |= sc->sc_status.button & ~buttons_found; 311 312 if (dx || dy || dz || dt || dw || 313 (buttons != sc->sc_status.button)) { 314 315 DPRINTFN(6, "x:%d y:%d z:%d t:%d w:%d buttons:0x%08x\n", 316 dx, dy, dz, dt, dw, buttons); 317 318 /* translate T-axis into button presses until further */ 319 if (dt > 0) 320 buttons |= 1UL << 5; 321 else if (dt < 0) 322 buttons |= 1UL << 6; 323 324 sc->sc_status.button = buttons; 325 sc->sc_status.dx += dx; 326 sc->sc_status.dy += dy; 327 sc->sc_status.dz += dz; 328 /* 329 * sc->sc_status.dt += dt; 330 * no way to export this yet 331 */ 332 333 /* 334 * The Qtronix keyboard has a built in PS/2 335 * port for a mouse. The firmware once in a 336 * while posts a spurious button up 337 * event. This event we ignore by doing a 338 * timeout for 50 msecs. If we receive 339 * dx=dy=dz=buttons=0 before we add the event 340 * to the queue. In any other case we delete 341 * the timeout event. 342 */ 343 if ((sc->sc_info[0].sc_flags & UMS_FLAG_SBU) && 344 (dx == 0) && (dy == 0) && (dz == 0) && (dt == 0) && 345 (dw == 0) && (buttons == 0)) { 346 347 usb_callout_reset(&sc->sc_callout, hz / 20, 348 &ums_put_queue_timeout, sc); 349 } else { 350 351 usb_callout_stop(&sc->sc_callout); 352 353 ums_put_queue(sc, dx, dy, dz, dt, buttons); 354 } 355 } 356 case USB_ST_SETUP: 357 tr_setup: 358 /* check if we can put more data into the FIFO */ 359 if (usb_fifo_put_bytes_max(sc->sc_fifo.fp[USB_FIFO_RX]) == 0) { 360 #ifdef EVDEV 361 if (sc->sc_evflags == 0) 362 break; 363 #else 364 break; 365 #endif 366 } 367 368 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 369 usbd_transfer_submit(xfer); 370 break; 371 372 default: /* Error */ 373 if (error != USB_ERR_CANCELLED) { 374 /* try clear stall first */ 375 usbd_xfer_set_stall(xfer); 376 goto tr_setup; 377 } 378 break; 379 } 380 } 381 382 static const struct usb_config ums_config[UMS_N_TRANSFER] = { 383 384 [UMS_INTR_DT] = { 385 .type = UE_INTERRUPT, 386 .endpoint = UE_ADDR_ANY, 387 .direction = UE_DIR_IN, 388 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 389 .bufsize = 0, /* use wMaxPacketSize */ 390 .callback = &ums_intr_callback, 391 }, 392 }; 393 394 /* A match on these entries will load ums */ 395 static const STRUCT_USB_HOST_ID __used ums_devs[] = { 396 {USB_IFACE_CLASS(UICLASS_HID), 397 USB_IFACE_SUBCLASS(UISUBCLASS_BOOT), 398 USB_IFACE_PROTOCOL(UIPROTO_MOUSE),}, 399 }; 400 401 static int 402 ums_probe(device_t dev) 403 { 404 struct usb_attach_arg *uaa = device_get_ivars(dev); 405 void *d_ptr; 406 int error; 407 uint16_t d_len; 408 409 DPRINTFN(11, "\n"); 410 411 if (uaa->usb_mode != USB_MODE_HOST) 412 return (ENXIO); 413 414 if (uaa->info.bInterfaceClass != UICLASS_HID) 415 return (ENXIO); 416 417 if (usb_test_quirk(uaa, UQ_UMS_IGNORE)) 418 return (ENXIO); 419 420 if ((uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) && 421 (uaa->info.bInterfaceProtocol == UIPROTO_MOUSE)) 422 return (BUS_PROBE_DEFAULT); 423 424 error = usbd_req_get_hid_desc(uaa->device, NULL, 425 &d_ptr, &d_len, M_TEMP, uaa->info.bIfaceIndex); 426 427 if (error) 428 return (ENXIO); 429 430 if (hid_is_mouse(d_ptr, d_len)) 431 error = BUS_PROBE_DEFAULT; 432 else 433 error = ENXIO; 434 435 free(d_ptr, M_TEMP); 436 return (error); 437 } 438 439 static void 440 ums_hid_parse(struct ums_softc *sc, device_t dev, const uint8_t *buf, 441 uint16_t len, uint8_t index) 442 { 443 struct ums_info *info = &sc->sc_info[index]; 444 uint32_t flags; 445 uint8_t i; 446 uint8_t j; 447 448 if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X), 449 hid_input, index, &info->sc_loc_x, &flags, &info->sc_iid_x)) { 450 451 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) { 452 info->sc_flags |= UMS_FLAG_X_AXIS; 453 } 454 } 455 if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y), 456 hid_input, index, &info->sc_loc_y, &flags, &info->sc_iid_y)) { 457 458 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) { 459 info->sc_flags |= UMS_FLAG_Y_AXIS; 460 } 461 } 462 /* Try the wheel first as the Z activator since it's tradition. */ 463 if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, 464 HUG_WHEEL), hid_input, index, &info->sc_loc_z, &flags, 465 &info->sc_iid_z) || 466 hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, 467 HUG_TWHEEL), hid_input, index, &info->sc_loc_z, &flags, 468 &info->sc_iid_z)) { 469 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) { 470 info->sc_flags |= UMS_FLAG_Z_AXIS; 471 } 472 /* 473 * We might have both a wheel and Z direction, if so put 474 * put the Z on the W coordinate. 475 */ 476 if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, 477 HUG_Z), hid_input, index, &info->sc_loc_w, &flags, 478 &info->sc_iid_w)) { 479 480 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) { 481 info->sc_flags |= UMS_FLAG_W_AXIS; 482 } 483 } 484 } else if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, 485 HUG_Z), hid_input, index, &info->sc_loc_z, &flags, 486 &info->sc_iid_z)) { 487 488 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) { 489 info->sc_flags |= UMS_FLAG_Z_AXIS; 490 } 491 } 492 /* 493 * The Microsoft Wireless Intellimouse 2.0 reports it's wheel 494 * using 0x0048, which is HUG_TWHEEL, and seems to expect you 495 * to know that the byte after the wheel is the tilt axis. 496 * There are no other HID axis descriptors other than X,Y and 497 * TWHEEL 498 */ 499 if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, 500 HUG_TWHEEL), hid_input, index, &info->sc_loc_t, 501 &flags, &info->sc_iid_t)) { 502 503 info->sc_loc_t.pos += 8; 504 505 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) { 506 info->sc_flags |= UMS_FLAG_T_AXIS; 507 } 508 } else if (hid_locate(buf, len, HID_USAGE2(HUP_CONSUMER, 509 HUC_AC_PAN), hid_input, index, &info->sc_loc_t, 510 &flags, &info->sc_iid_t)) { 511 512 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) 513 info->sc_flags |= UMS_FLAG_T_AXIS; 514 } 515 /* figure out the number of buttons */ 516 517 for (i = 0; i < UMS_BUTTON_MAX; i++) { 518 if (!hid_locate(buf, len, HID_USAGE2(HUP_BUTTON, (i + 1)), 519 hid_input, index, &info->sc_loc_btn[i], NULL, 520 &info->sc_iid_btn[i])) { 521 break; 522 } 523 } 524 525 /* detect other buttons */ 526 527 for (j = 0; (i < UMS_BUTTON_MAX) && (j < 2); i++, j++) { 528 if (!hid_locate(buf, len, HID_USAGE2(HUP_MICROSOFT, (j + 1)), 529 hid_input, index, &info->sc_loc_btn[i], NULL, 530 &info->sc_iid_btn[i])) { 531 break; 532 } 533 } 534 535 info->sc_buttons = i; 536 537 if (i > sc->sc_buttons) 538 sc->sc_buttons = i; 539 540 if (info->sc_flags == 0) 541 return; 542 543 /* announce information about the mouse */ 544 device_printf(dev, "%d buttons and [%s%s%s%s%s] coordinates ID=%u\n", 545 (info->sc_buttons), 546 (info->sc_flags & UMS_FLAG_X_AXIS) ? "X" : "", 547 (info->sc_flags & UMS_FLAG_Y_AXIS) ? "Y" : "", 548 (info->sc_flags & UMS_FLAG_Z_AXIS) ? "Z" : "", 549 (info->sc_flags & UMS_FLAG_T_AXIS) ? "T" : "", 550 (info->sc_flags & UMS_FLAG_W_AXIS) ? "W" : "", 551 info->sc_iid_x); 552 } 553 554 static int 555 ums_attach(device_t dev) 556 { 557 struct usb_attach_arg *uaa = device_get_ivars(dev); 558 struct ums_softc *sc = device_get_softc(dev); 559 struct ums_info *info; 560 void *d_ptr = NULL; 561 int isize; 562 int err; 563 uint16_t d_len; 564 uint8_t i; 565 #ifdef USB_DEBUG 566 uint8_t j; 567 #endif 568 569 DPRINTFN(11, "sc=%p\n", sc); 570 571 device_set_usb_desc(dev); 572 573 mtx_init(&sc->sc_mtx, "ums lock", NULL, MTX_DEF | MTX_RECURSE); 574 575 usb_callout_init_mtx(&sc->sc_callout, &sc->sc_mtx, 0); 576 577 /* 578 * Force the report (non-boot) protocol. 579 * 580 * Mice without boot protocol support may choose not to implement 581 * Set_Protocol at all; Ignore any error. 582 */ 583 err = usbd_req_set_protocol(uaa->device, NULL, 584 uaa->info.bIfaceIndex, 1); 585 586 err = usbd_transfer_setup(uaa->device, 587 &uaa->info.bIfaceIndex, sc->sc_xfer, ums_config, 588 UMS_N_TRANSFER, sc, &sc->sc_mtx); 589 590 if (err) { 591 DPRINTF("error=%s\n", usbd_errstr(err)); 592 goto detach; 593 } 594 595 /* Get HID descriptor */ 596 err = usbd_req_get_hid_desc(uaa->device, NULL, &d_ptr, 597 &d_len, M_TEMP, uaa->info.bIfaceIndex); 598 599 if (err) { 600 device_printf(dev, "error reading report description\n"); 601 goto detach; 602 } 603 604 isize = hid_report_size(d_ptr, d_len, hid_input, &sc->sc_iid); 605 606 /* 607 * The Microsoft Wireless Notebook Optical Mouse seems to be in worse 608 * shape than the Wireless Intellimouse 2.0, as its X, Y, wheel, and 609 * all of its other button positions are all off. It also reports that 610 * it has two additional buttons and a tilt wheel. 611 */ 612 if (usb_test_quirk(uaa, UQ_MS_BAD_CLASS)) { 613 614 sc->sc_iid = 0; 615 616 info = &sc->sc_info[0]; 617 info->sc_flags = (UMS_FLAG_X_AXIS | 618 UMS_FLAG_Y_AXIS | 619 UMS_FLAG_Z_AXIS | 620 UMS_FLAG_SBU); 621 info->sc_buttons = 3; 622 isize = 5; 623 /* 1st byte of descriptor report contains garbage */ 624 info->sc_loc_x.pos = 16; 625 info->sc_loc_x.size = 8; 626 info->sc_loc_y.pos = 24; 627 info->sc_loc_y.size = 8; 628 info->sc_loc_z.pos = 32; 629 info->sc_loc_z.size = 8; 630 info->sc_loc_btn[0].pos = 8; 631 info->sc_loc_btn[0].size = 1; 632 info->sc_loc_btn[1].pos = 9; 633 info->sc_loc_btn[1].size = 1; 634 info->sc_loc_btn[2].pos = 10; 635 info->sc_loc_btn[2].size = 1; 636 637 /* Announce device */ 638 device_printf(dev, "3 buttons and [XYZ] " 639 "coordinates ID=0\n"); 640 641 } else { 642 /* Search the HID descriptor and announce device */ 643 for (i = 0; i < UMS_INFO_MAX; i++) { 644 ums_hid_parse(sc, dev, d_ptr, d_len, i); 645 } 646 } 647 648 if (usb_test_quirk(uaa, UQ_MS_REVZ)) { 649 info = &sc->sc_info[0]; 650 /* Some wheels need the Z axis reversed. */ 651 info->sc_flags |= UMS_FLAG_REVZ; 652 } 653 if (isize > (int)usbd_xfer_max_framelen(sc->sc_xfer[UMS_INTR_DT])) { 654 DPRINTF("WARNING: report size, %d bytes, is larger " 655 "than interrupt size, %d bytes!\n", isize, 656 usbd_xfer_max_framelen(sc->sc_xfer[UMS_INTR_DT])); 657 } 658 free(d_ptr, M_TEMP); 659 d_ptr = NULL; 660 661 #ifdef USB_DEBUG 662 for (j = 0; j < UMS_INFO_MAX; j++) { 663 info = &sc->sc_info[j]; 664 665 DPRINTF("sc=%p, index=%d\n", sc, j); 666 DPRINTF("X\t%d/%d id=%d\n", info->sc_loc_x.pos, 667 info->sc_loc_x.size, info->sc_iid_x); 668 DPRINTF("Y\t%d/%d id=%d\n", info->sc_loc_y.pos, 669 info->sc_loc_y.size, info->sc_iid_y); 670 DPRINTF("Z\t%d/%d id=%d\n", info->sc_loc_z.pos, 671 info->sc_loc_z.size, info->sc_iid_z); 672 DPRINTF("T\t%d/%d id=%d\n", info->sc_loc_t.pos, 673 info->sc_loc_t.size, info->sc_iid_t); 674 DPRINTF("W\t%d/%d id=%d\n", info->sc_loc_w.pos, 675 info->sc_loc_w.size, info->sc_iid_w); 676 677 for (i = 0; i < info->sc_buttons; i++) { 678 DPRINTF("B%d\t%d/%d id=%d\n", 679 i + 1, info->sc_loc_btn[i].pos, 680 info->sc_loc_btn[i].size, info->sc_iid_btn[i]); 681 } 682 } 683 DPRINTF("size=%d, id=%d\n", isize, sc->sc_iid); 684 #endif 685 686 err = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx, 687 &ums_fifo_methods, &sc->sc_fifo, 688 device_get_unit(dev), -1, uaa->info.bIfaceIndex, 689 UID_ROOT, GID_OPERATOR, 0644); 690 if (err) 691 goto detach; 692 693 #ifdef EVDEV 694 sc->sc_evdev = evdev_alloc(); 695 evdev_set_name(sc->sc_evdev, device_get_desc(dev)); 696 evdev_set_phys(sc->sc_evdev, device_get_nameunit(dev)); 697 evdev_set_id(sc->sc_evdev, BUS_USB, uaa->info.idVendor, 698 uaa->info.idProduct, 0); 699 evdev_set_serial(sc->sc_evdev, usb_get_serial(uaa->device)); 700 evdev_set_methods(sc->sc_evdev, sc, &ums_evdev_methods); 701 evdev_support_prop(sc->sc_evdev, INPUT_PROP_POINTER); 702 evdev_support_event(sc->sc_evdev, EV_SYN); 703 evdev_support_event(sc->sc_evdev, EV_REL); 704 evdev_support_event(sc->sc_evdev, EV_KEY); 705 706 info = &sc->sc_info[0]; 707 708 if (info->sc_flags & UMS_FLAG_X_AXIS) 709 evdev_support_rel(sc->sc_evdev, REL_X); 710 711 if (info->sc_flags & UMS_FLAG_Y_AXIS) 712 evdev_support_rel(sc->sc_evdev, REL_Y); 713 714 if (info->sc_flags & UMS_FLAG_Z_AXIS) 715 evdev_support_rel(sc->sc_evdev, REL_WHEEL); 716 717 if (info->sc_flags & UMS_FLAG_T_AXIS) 718 evdev_support_rel(sc->sc_evdev, REL_HWHEEL); 719 720 for (i = 0; i < info->sc_buttons; i++) 721 evdev_support_key(sc->sc_evdev, BTN_MOUSE + i); 722 723 err = evdev_register(sc->sc_evdev); 724 if (err) 725 goto detach; 726 #endif 727 728 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 729 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), 730 OID_AUTO, "parseinfo", CTLTYPE_STRING|CTLFLAG_RD, 731 sc, 0, ums_sysctl_handler_parseinfo, 732 "", "Dump of parsed HID report descriptor"); 733 734 return (0); 735 736 detach: 737 if (d_ptr) { 738 free(d_ptr, M_TEMP); 739 } 740 ums_detach(dev); 741 return (ENOMEM); 742 } 743 744 static int 745 ums_detach(device_t self) 746 { 747 struct ums_softc *sc = device_get_softc(self); 748 749 DPRINTF("sc=%p\n", sc); 750 751 usb_fifo_detach(&sc->sc_fifo); 752 753 #ifdef EVDEV 754 evdev_free(sc->sc_evdev); 755 #endif 756 757 usbd_transfer_unsetup(sc->sc_xfer, UMS_N_TRANSFER); 758 759 usb_callout_drain(&sc->sc_callout); 760 761 mtx_destroy(&sc->sc_mtx); 762 763 return (0); 764 } 765 766 static void 767 ums_reset(struct ums_softc *sc) 768 { 769 770 /* reset all USB mouse parameters */ 771 772 if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON) 773 sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON; 774 else 775 sc->sc_hw.buttons = sc->sc_buttons; 776 777 sc->sc_hw.iftype = MOUSE_IF_USB; 778 sc->sc_hw.type = MOUSE_MOUSE; 779 sc->sc_hw.model = MOUSE_MODEL_GENERIC; 780 sc->sc_hw.hwid = 0; 781 782 sc->sc_mode.protocol = MOUSE_PROTO_MSC; 783 sc->sc_mode.rate = -1; 784 sc->sc_mode.resolution = MOUSE_RES_UNKNOWN; 785 sc->sc_mode.accelfactor = 0; 786 sc->sc_mode.level = 0; 787 sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE; 788 sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK; 789 sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC; 790 791 /* reset status */ 792 793 sc->sc_status.flags = 0; 794 sc->sc_status.button = 0; 795 sc->sc_status.obutton = 0; 796 sc->sc_status.dx = 0; 797 sc->sc_status.dy = 0; 798 sc->sc_status.dz = 0; 799 /* sc->sc_status.dt = 0; */ 800 } 801 802 static void 803 ums_start_rx(struct ums_softc *sc) 804 { 805 int rate; 806 807 /* Check if we should override the default polling interval */ 808 rate = sc->sc_pollrate; 809 /* Range check rate */ 810 if (rate > 1000) 811 rate = 1000; 812 /* Check for set rate */ 813 if ((rate > 0) && (sc->sc_xfer[UMS_INTR_DT] != NULL)) { 814 DPRINTF("Setting pollrate = %d\n", rate); 815 /* Stop current transfer, if any */ 816 usbd_transfer_stop(sc->sc_xfer[UMS_INTR_DT]); 817 /* Set new interval */ 818 usbd_xfer_set_interval(sc->sc_xfer[UMS_INTR_DT], 1000 / rate); 819 /* Only set pollrate once */ 820 sc->sc_pollrate = 0; 821 } 822 823 usbd_transfer_start(sc->sc_xfer[UMS_INTR_DT]); 824 } 825 826 static void 827 ums_stop_rx(struct ums_softc *sc) 828 { 829 usbd_transfer_stop(sc->sc_xfer[UMS_INTR_DT]); 830 usb_callout_stop(&sc->sc_callout); 831 } 832 833 static void 834 ums_fifo_start_read(struct usb_fifo *fifo) 835 { 836 struct ums_softc *sc = usb_fifo_softc(fifo); 837 838 ums_start_rx(sc); 839 } 840 841 static void 842 ums_fifo_stop_read(struct usb_fifo *fifo) 843 { 844 struct ums_softc *sc = usb_fifo_softc(fifo); 845 846 ums_stop_rx(sc); 847 } 848 849 850 #if ((MOUSE_SYS_PACKETSIZE != 8) || \ 851 (MOUSE_MSC_PACKETSIZE != 5)) 852 #error "Software assumptions are not met. Please update code." 853 #endif 854 855 static void 856 ums_put_queue(struct ums_softc *sc, int32_t dx, int32_t dy, 857 int32_t dz, int32_t dt, int32_t buttons) 858 { 859 uint8_t buf[8]; 860 861 if (1) { 862 863 if (dx > 254) 864 dx = 254; 865 if (dx < -256) 866 dx = -256; 867 if (dy > 254) 868 dy = 254; 869 if (dy < -256) 870 dy = -256; 871 if (dz > 126) 872 dz = 126; 873 if (dz < -128) 874 dz = -128; 875 if (dt > 126) 876 dt = 126; 877 if (dt < -128) 878 dt = -128; 879 880 buf[0] = sc->sc_mode.syncmask[1]; 881 buf[0] |= (~buttons) & MOUSE_MSC_BUTTONS; 882 buf[1] = dx >> 1; 883 buf[2] = dy >> 1; 884 buf[3] = dx - (dx >> 1); 885 buf[4] = dy - (dy >> 1); 886 887 if (sc->sc_mode.level == 1) { 888 buf[5] = dz >> 1; 889 buf[6] = dz - (dz >> 1); 890 buf[7] = (((~buttons) >> 3) & MOUSE_SYS_EXTBUTTONS); 891 } 892 usb_fifo_put_data_linear(sc->sc_fifo.fp[USB_FIFO_RX], buf, 893 sc->sc_mode.packetsize, 1); 894 895 #ifdef EVDEV 896 if (evdev_rcpt_mask & EVDEV_RCPT_HW_MOUSE) { 897 /* Push evdev event */ 898 evdev_push_event(sc->sc_evdev, EV_REL, REL_X, dx); 899 evdev_push_event(sc->sc_evdev, EV_REL, REL_Y, -dy); 900 evdev_push_event(sc->sc_evdev, EV_REL, REL_WHEEL, -dz); 901 evdev_push_event(sc->sc_evdev, EV_REL, REL_HWHEEL, dt); 902 evdev_push_mouse_btn(sc->sc_evdev, 903 (buttons & ~MOUSE_STDBUTTONS) | 904 (buttons & (1 << 2) ? MOUSE_BUTTON1DOWN : 0) | 905 (buttons & (1 << 1) ? MOUSE_BUTTON2DOWN : 0) | 906 (buttons & (1 << 0) ? MOUSE_BUTTON3DOWN : 0)); 907 evdev_sync(sc->sc_evdev); 908 } 909 #endif 910 } else { 911 DPRINTF("Buffer full, discarded packet\n"); 912 } 913 } 914 915 static void 916 ums_reset_buf(struct ums_softc *sc) 917 { 918 /* reset read queue, must be called locked */ 919 usb_fifo_reset(sc->sc_fifo.fp[USB_FIFO_RX]); 920 } 921 922 #ifdef EVDEV 923 static int 924 ums_ev_open(struct evdev_dev *evdev, void *ev_softc) 925 { 926 struct ums_softc *sc = (struct ums_softc *)ev_softc; 927 928 mtx_lock(&sc->sc_mtx); 929 930 sc->sc_evflags = UMS_EVDEV_OPENED; 931 932 if (sc->sc_fflags == 0) { 933 ums_reset(sc); 934 ums_start_rx(sc); 935 } 936 937 mtx_unlock(&sc->sc_mtx); 938 939 return (0); 940 } 941 942 static void 943 ums_ev_close(struct evdev_dev *evdev, void *ev_softc) 944 { 945 struct ums_softc *sc = (struct ums_softc *)ev_softc; 946 947 mtx_lock(&sc->sc_mtx); 948 949 sc->sc_evflags = 0; 950 951 if (sc->sc_fflags == 0) 952 ums_stop_rx(sc); 953 954 mtx_unlock(&sc->sc_mtx); 955 } 956 #endif 957 958 static int 959 ums_fifo_open(struct usb_fifo *fifo, int fflags) 960 { 961 struct ums_softc *sc = usb_fifo_softc(fifo); 962 963 DPRINTFN(2, "\n"); 964 965 /* check for duplicate open, should not happen */ 966 if (sc->sc_fflags & fflags) 967 return (EBUSY); 968 969 /* check for first open */ 970 #ifdef EVDEV 971 if (sc->sc_fflags == 0 && sc->sc_evflags == 0) 972 ums_reset(sc); 973 #else 974 if (sc->sc_fflags == 0) 975 ums_reset(sc); 976 #endif 977 978 if (fflags & FREAD) { 979 /* allocate RX buffer */ 980 if (usb_fifo_alloc_buffer(fifo, 981 UMS_BUF_SIZE, UMS_IFQ_MAXLEN)) { 982 return (ENOMEM); 983 } 984 } 985 986 sc->sc_fflags |= fflags & (FREAD | FWRITE); 987 return (0); 988 } 989 990 static void 991 ums_fifo_close(struct usb_fifo *fifo, int fflags) 992 { 993 struct ums_softc *sc = usb_fifo_softc(fifo); 994 995 DPRINTFN(2, "\n"); 996 997 if (fflags & FREAD) 998 usb_fifo_free_buffer(fifo); 999 1000 sc->sc_fflags &= ~(fflags & (FREAD | FWRITE)); 1001 } 1002 1003 static int 1004 ums_fifo_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags) 1005 { 1006 struct ums_softc *sc = usb_fifo_softc(fifo); 1007 mousemode_t mode; 1008 int error = 0; 1009 1010 DPRINTFN(2, "\n"); 1011 1012 mtx_lock(&sc->sc_mtx); 1013 1014 switch (cmd) { 1015 case MOUSE_GETHWINFO: 1016 *(mousehw_t *)addr = sc->sc_hw; 1017 break; 1018 1019 case MOUSE_GETMODE: 1020 *(mousemode_t *)addr = sc->sc_mode; 1021 break; 1022 1023 case MOUSE_SETMODE: 1024 mode = *(mousemode_t *)addr; 1025 1026 if (mode.level == -1) { 1027 /* don't change the current setting */ 1028 } else if ((mode.level < 0) || (mode.level > 1)) { 1029 error = EINVAL; 1030 break; 1031 } else { 1032 sc->sc_mode.level = mode.level; 1033 } 1034 1035 /* store polling rate */ 1036 sc->sc_pollrate = mode.rate; 1037 1038 if (sc->sc_mode.level == 0) { 1039 if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON) 1040 sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON; 1041 else 1042 sc->sc_hw.buttons = sc->sc_buttons; 1043 sc->sc_mode.protocol = MOUSE_PROTO_MSC; 1044 sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE; 1045 sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK; 1046 sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC; 1047 } else if (sc->sc_mode.level == 1) { 1048 if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON) 1049 sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON; 1050 else 1051 sc->sc_hw.buttons = sc->sc_buttons; 1052 sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE; 1053 sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE; 1054 sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK; 1055 sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC; 1056 } 1057 ums_reset_buf(sc); 1058 break; 1059 1060 case MOUSE_GETLEVEL: 1061 *(int *)addr = sc->sc_mode.level; 1062 break; 1063 1064 case MOUSE_SETLEVEL: 1065 if (*(int *)addr < 0 || *(int *)addr > 1) { 1066 error = EINVAL; 1067 break; 1068 } 1069 sc->sc_mode.level = *(int *)addr; 1070 1071 if (sc->sc_mode.level == 0) { 1072 if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON) 1073 sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON; 1074 else 1075 sc->sc_hw.buttons = sc->sc_buttons; 1076 sc->sc_mode.protocol = MOUSE_PROTO_MSC; 1077 sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE; 1078 sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK; 1079 sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC; 1080 } else if (sc->sc_mode.level == 1) { 1081 if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON) 1082 sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON; 1083 else 1084 sc->sc_hw.buttons = sc->sc_buttons; 1085 sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE; 1086 sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE; 1087 sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK; 1088 sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC; 1089 } 1090 ums_reset_buf(sc); 1091 break; 1092 1093 case MOUSE_GETSTATUS:{ 1094 mousestatus_t *status = (mousestatus_t *)addr; 1095 1096 *status = sc->sc_status; 1097 sc->sc_status.obutton = sc->sc_status.button; 1098 sc->sc_status.button = 0; 1099 sc->sc_status.dx = 0; 1100 sc->sc_status.dy = 0; 1101 sc->sc_status.dz = 0; 1102 /* sc->sc_status.dt = 0; */ 1103 1104 if (status->dx || status->dy || status->dz /* || status->dt */ ) { 1105 status->flags |= MOUSE_POSCHANGED; 1106 } 1107 if (status->button != status->obutton) { 1108 status->flags |= MOUSE_BUTTONSCHANGED; 1109 } 1110 break; 1111 } 1112 default: 1113 error = ENOTTY; 1114 break; 1115 } 1116 1117 mtx_unlock(&sc->sc_mtx); 1118 return (error); 1119 } 1120 1121 static int 1122 ums_sysctl_handler_parseinfo(SYSCTL_HANDLER_ARGS) 1123 { 1124 struct ums_softc *sc = arg1; 1125 struct ums_info *info; 1126 struct sbuf *sb; 1127 int i, j, err, had_output; 1128 1129 sb = sbuf_new_auto(); 1130 for (i = 0, had_output = 0; i < UMS_INFO_MAX; i++) { 1131 info = &sc->sc_info[i]; 1132 1133 /* Don't emit empty info */ 1134 if ((info->sc_flags & 1135 (UMS_FLAG_X_AXIS | UMS_FLAG_Y_AXIS | UMS_FLAG_Z_AXIS | 1136 UMS_FLAG_T_AXIS | UMS_FLAG_W_AXIS)) == 0 && 1137 info->sc_buttons == 0) 1138 continue; 1139 1140 if (had_output) 1141 sbuf_printf(sb, "\n"); 1142 had_output = 1; 1143 sbuf_printf(sb, "i%d:", i + 1); 1144 if (info->sc_flags & UMS_FLAG_X_AXIS) 1145 sbuf_printf(sb, " X:r%d, p%d, s%d;", 1146 (int)info->sc_iid_x, 1147 (int)info->sc_loc_x.pos, 1148 (int)info->sc_loc_x.size); 1149 if (info->sc_flags & UMS_FLAG_Y_AXIS) 1150 sbuf_printf(sb, " Y:r%d, p%d, s%d;", 1151 (int)info->sc_iid_y, 1152 (int)info->sc_loc_y.pos, 1153 (int)info->sc_loc_y.size); 1154 if (info->sc_flags & UMS_FLAG_Z_AXIS) 1155 sbuf_printf(sb, " Z:r%d, p%d, s%d;", 1156 (int)info->sc_iid_z, 1157 (int)info->sc_loc_z.pos, 1158 (int)info->sc_loc_z.size); 1159 if (info->sc_flags & UMS_FLAG_T_AXIS) 1160 sbuf_printf(sb, " T:r%d, p%d, s%d;", 1161 (int)info->sc_iid_t, 1162 (int)info->sc_loc_t.pos, 1163 (int)info->sc_loc_t.size); 1164 if (info->sc_flags & UMS_FLAG_W_AXIS) 1165 sbuf_printf(sb, " W:r%d, p%d, s%d;", 1166 (int)info->sc_iid_w, 1167 (int)info->sc_loc_w.pos, 1168 (int)info->sc_loc_w.size); 1169 1170 for (j = 0; j < info->sc_buttons; j++) { 1171 sbuf_printf(sb, " B%d:r%d, p%d, s%d;", j + 1, 1172 (int)info->sc_iid_btn[j], 1173 (int)info->sc_loc_btn[j].pos, 1174 (int)info->sc_loc_btn[j].size); 1175 } 1176 } 1177 sbuf_finish(sb); 1178 err = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1); 1179 sbuf_delete(sb); 1180 1181 return (err); 1182 } 1183 1184 static devclass_t ums_devclass; 1185 1186 static device_method_t ums_methods[] = { 1187 DEVMETHOD(device_probe, ums_probe), 1188 DEVMETHOD(device_attach, ums_attach), 1189 DEVMETHOD(device_detach, ums_detach), 1190 1191 DEVMETHOD_END 1192 }; 1193 1194 static driver_t ums_driver = { 1195 .name = "ums", 1196 .methods = ums_methods, 1197 .size = sizeof(struct ums_softc), 1198 }; 1199 1200 DRIVER_MODULE(ums, uhub, ums_driver, ums_devclass, NULL, 0); 1201 MODULE_DEPEND(ums, usb, 1, 1, 1); 1202 #ifdef EVDEV 1203 MODULE_DEPEND(ums, evdev, 1, 1, 1); 1204 #endif 1205 MODULE_VERSION(ums, 1); 1206 USB_PNP_HOST_INFO(ums_devs); 1207