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