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