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