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 67 #include <dev/usb/usb.h> 68 #include <dev/usb/usbdi.h> 69 #include <dev/usb/usbdi_util.h> 70 #include <dev/usb/usbhid.h> 71 #include "usbdevs.h" 72 73 #define USB_DEBUG_VAR ums_debug 74 #include <dev/usb/usb_debug.h> 75 76 #include <dev/usb/quirk/usb_quirk.h> 77 78 #include <sys/ioccom.h> 79 #include <sys/filio.h> 80 #include <sys/tty.h> 81 #include <sys/mouse.h> 82 83 #if USB_DEBUG 84 static int ums_debug = 0; 85 86 SYSCTL_NODE(_hw_usb, OID_AUTO, ums, CTLFLAG_RW, 0, "USB ums"); 87 SYSCTL_INT(_hw_usb_ums, OID_AUTO, debug, CTLFLAG_RW, 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 uint8_t sc_buttons; 144 uint8_t sc_iid; 145 uint8_t sc_temp[64]; 146 }; 147 148 static void ums_put_queue_timeout(void *__sc); 149 150 static usb_callback_t ums_intr_callback; 151 152 static device_probe_t ums_probe; 153 static device_attach_t ums_attach; 154 static device_detach_t ums_detach; 155 156 static usb_fifo_cmd_t ums_start_read; 157 static usb_fifo_cmd_t ums_stop_read; 158 static usb_fifo_open_t ums_open; 159 static usb_fifo_close_t ums_close; 160 static usb_fifo_ioctl_t ums_ioctl; 161 162 static void ums_put_queue(struct ums_softc *sc, int32_t dx, int32_t dy, int32_t dz, int32_t dt, int32_t buttons); 163 164 static struct usb_fifo_methods ums_fifo_methods = { 165 .f_open = &ums_open, 166 .f_close = &ums_close, 167 .f_ioctl = &ums_ioctl, 168 .f_start_read = &ums_start_read, 169 .f_stop_read = &ums_stop_read, 170 .basename[0] = "ums", 171 }; 172 173 static void 174 ums_put_queue_timeout(void *__sc) 175 { 176 struct ums_softc *sc = __sc; 177 178 mtx_assert(&sc->sc_mtx, MA_OWNED); 179 180 ums_put_queue(sc, 0, 0, 0, 0, 0); 181 } 182 183 static void 184 ums_intr_callback(struct usb_xfer *xfer, usb_error_t error) 185 { 186 struct ums_softc *sc = usbd_xfer_softc(xfer); 187 struct ums_info *info = &sc->sc_info[0]; 188 struct usb_page_cache *pc; 189 uint8_t *buf = sc->sc_temp; 190 int32_t buttons = 0; 191 int32_t dw = 0; 192 int32_t dx = 0; 193 int32_t dy = 0; 194 int32_t dz = 0; 195 int32_t dt = 0; 196 uint8_t i; 197 uint8_t id; 198 int len; 199 200 usbd_xfer_status(xfer, &len, NULL, NULL, NULL); 201 202 switch (USB_GET_STATE(xfer)) { 203 case USB_ST_TRANSFERRED: 204 DPRINTFN(6, "sc=%p actlen=%d\n", sc, len); 205 206 if (len > sizeof(sc->sc_temp)) { 207 DPRINTFN(6, "truncating large packet to %zu bytes\n", 208 sizeof(sc->sc_temp)); 209 len = sizeof(sc->sc_temp); 210 } 211 if (len == 0) 212 goto tr_setup; 213 214 pc = usbd_xfer_get_frame(xfer, 0); 215 usbd_copy_out(pc, 0, buf, len); 216 217 DPRINTFN(6, "data = %02x %02x %02x %02x " 218 "%02x %02x %02x %02x\n", 219 (len > 0) ? buf[0] : 0, (len > 1) ? buf[1] : 0, 220 (len > 2) ? buf[2] : 0, (len > 3) ? buf[3] : 0, 221 (len > 4) ? buf[4] : 0, (len > 5) ? buf[5] : 0, 222 (len > 6) ? buf[6] : 0, (len > 7) ? buf[7] : 0); 223 224 if (sc->sc_iid) { 225 id = *buf; 226 227 len--; 228 buf++; 229 230 } else { 231 id = 0; 232 if (sc->sc_info[0].sc_flags & UMS_FLAG_SBU) { 233 if ((*buf == 0x14) || (*buf == 0x15)) { 234 goto tr_setup; 235 } 236 } 237 } 238 239 repeat: 240 if ((info->sc_flags & UMS_FLAG_W_AXIS) && 241 (id == info->sc_iid_w)) 242 dw += hid_get_data(buf, len, &info->sc_loc_w); 243 244 if ((info->sc_flags & UMS_FLAG_X_AXIS) && 245 (id == info->sc_iid_x)) 246 dx += hid_get_data(buf, len, &info->sc_loc_x); 247 248 if ((info->sc_flags & UMS_FLAG_Y_AXIS) && 249 (id == info->sc_iid_y)) 250 dy = -hid_get_data(buf, len, &info->sc_loc_y); 251 252 if ((info->sc_flags & UMS_FLAG_Z_AXIS) && 253 (id == info->sc_iid_z)) { 254 int32_t temp; 255 temp = hid_get_data(buf, len, &info->sc_loc_z); 256 if (info->sc_flags & UMS_FLAG_REVZ) 257 temp = -temp; 258 dz -= temp; 259 } 260 261 if ((info->sc_flags & UMS_FLAG_T_AXIS) && 262 (id == info->sc_iid_t)) 263 dt -= hid_get_data(buf, len, &info->sc_loc_t); 264 265 for (i = 0; i < info->sc_buttons; i++) { 266 if (id != info->sc_iid_btn[i]) 267 continue; 268 if (hid_get_data(buf, len, &info->sc_loc_btn[i])) { 269 buttons |= (1 << UMS_BUT(i)); 270 } 271 } 272 273 if (++info != &sc->sc_info[UMS_INFO_MAX]) 274 goto repeat; 275 276 if (dx || dy || dz || dt || dw || 277 (buttons != sc->sc_status.button)) { 278 279 DPRINTFN(6, "x:%d y:%d z:%d t:%d w:%d buttons:0x%08x\n", 280 dx, dy, dz, dt, dw, buttons); 281 282 sc->sc_status.button = buttons; 283 sc->sc_status.dx += dx; 284 sc->sc_status.dy += dy; 285 sc->sc_status.dz += dz; 286 /* 287 * sc->sc_status.dt += dt; 288 * no way to export this yet 289 */ 290 291 /* 292 * The Qtronix keyboard has a built in PS/2 293 * port for a mouse. The firmware once in a 294 * while posts a spurious button up 295 * event. This event we ignore by doing a 296 * timeout for 50 msecs. If we receive 297 * dx=dy=dz=buttons=0 before we add the event 298 * to the queue. In any other case we delete 299 * the timeout event. 300 */ 301 if ((sc->sc_info[0].sc_flags & UMS_FLAG_SBU) && 302 (dx == 0) && (dy == 0) && (dz == 0) && (dt == 0) && 303 (dw == 0) && (buttons == 0)) { 304 305 usb_callout_reset(&sc->sc_callout, hz / 20, 306 &ums_put_queue_timeout, sc); 307 } else { 308 309 usb_callout_stop(&sc->sc_callout); 310 311 ums_put_queue(sc, dx, dy, dz, dt, buttons); 312 } 313 } 314 case USB_ST_SETUP: 315 tr_setup: 316 /* check if we can put more data into the FIFO */ 317 if (usb_fifo_put_bytes_max( 318 sc->sc_fifo.fp[USB_FIFO_RX]) != 0) { 319 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 320 usbd_transfer_submit(xfer); 321 } 322 break; 323 324 default: /* Error */ 325 if (error != USB_ERR_CANCELLED) { 326 /* try clear stall first */ 327 usbd_xfer_set_stall(xfer); 328 goto tr_setup; 329 } 330 break; 331 } 332 } 333 334 static const struct usb_config ums_config[UMS_N_TRANSFER] = { 335 336 [UMS_INTR_DT] = { 337 .type = UE_INTERRUPT, 338 .endpoint = UE_ADDR_ANY, 339 .direction = UE_DIR_IN, 340 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 341 .bufsize = 0, /* use wMaxPacketSize */ 342 .callback = &ums_intr_callback, 343 }, 344 }; 345 346 static int 347 ums_probe(device_t dev) 348 { 349 struct usb_attach_arg *uaa = device_get_ivars(dev); 350 void *d_ptr; 351 int error; 352 uint16_t d_len; 353 354 DPRINTFN(11, "\n"); 355 356 if (uaa->usb_mode != USB_MODE_HOST) 357 return (ENXIO); 358 359 if (uaa->info.bInterfaceClass != UICLASS_HID) 360 return (ENXIO); 361 362 if ((uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) && 363 (uaa->info.bInterfaceProtocol == UIPROTO_MOUSE)) 364 return (0); 365 366 error = usbd_req_get_hid_desc(uaa->device, NULL, 367 &d_ptr, &d_len, M_TEMP, uaa->info.bIfaceIndex); 368 369 if (error) 370 return (ENXIO); 371 372 if (hid_is_collection(d_ptr, d_len, 373 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE))) 374 error = 0; 375 else 376 error = ENXIO; 377 378 free(d_ptr, M_TEMP); 379 return (error); 380 } 381 382 static void 383 ums_hid_parse(struct ums_softc *sc, device_t dev, const uint8_t *buf, 384 uint16_t len, uint8_t index) 385 { 386 struct ums_info *info = &sc->sc_info[index]; 387 uint32_t flags; 388 uint8_t i; 389 390 if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X), 391 hid_input, index, &info->sc_loc_x, &flags, &info->sc_iid_x)) { 392 393 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) { 394 info->sc_flags |= UMS_FLAG_X_AXIS; 395 } 396 } 397 if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y), 398 hid_input, index, &info->sc_loc_y, &flags, &info->sc_iid_y)) { 399 400 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) { 401 info->sc_flags |= UMS_FLAG_Y_AXIS; 402 } 403 } 404 /* Try the wheel first as the Z activator since it's tradition. */ 405 if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, 406 HUG_WHEEL), hid_input, index, &info->sc_loc_z, &flags, 407 &info->sc_iid_z) || 408 hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, 409 HUG_TWHEEL), hid_input, index, &info->sc_loc_z, &flags, 410 &info->sc_iid_z)) { 411 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) { 412 info->sc_flags |= UMS_FLAG_Z_AXIS; 413 } 414 /* 415 * We might have both a wheel and Z direction, if so put 416 * put the Z on the W coordinate. 417 */ 418 if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, 419 HUG_Z), hid_input, index, &info->sc_loc_w, &flags, 420 &info->sc_iid_w)) { 421 422 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) { 423 info->sc_flags |= UMS_FLAG_W_AXIS; 424 } 425 } 426 } else if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, 427 HUG_Z), hid_input, index, &info->sc_loc_z, &flags, 428 &info->sc_iid_z)) { 429 430 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) { 431 info->sc_flags |= UMS_FLAG_Z_AXIS; 432 } 433 } 434 /* 435 * The Microsoft Wireless Intellimouse 2.0 reports it's wheel 436 * using 0x0048, which is HUG_TWHEEL, and seems to expect you 437 * to know that the byte after the wheel is the tilt axis. 438 * There are no other HID axis descriptors other than X,Y and 439 * TWHEEL 440 */ 441 if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, 442 HUG_TWHEEL), hid_input, index, &info->sc_loc_t, 443 &flags, &info->sc_iid_t)) { 444 445 info->sc_loc_t.pos += 8; 446 447 if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) { 448 info->sc_flags |= UMS_FLAG_T_AXIS; 449 } 450 } 451 /* figure out the number of buttons */ 452 453 for (i = 0; i < UMS_BUTTON_MAX; i++) { 454 if (!hid_locate(buf, len, HID_USAGE2(HUP_BUTTON, (i + 1)), 455 hid_input, index, &info->sc_loc_btn[i], NULL, 456 &info->sc_iid_btn[i])) { 457 break; 458 } 459 } 460 info->sc_buttons = i; 461 462 if (i > sc->sc_buttons) 463 sc->sc_buttons = i; 464 465 if (info->sc_flags == 0) 466 return; 467 468 /* announce information about the mouse */ 469 device_printf(dev, "%d buttons and [%s%s%s%s%s] coordinates ID=%u\n", 470 (info->sc_buttons), 471 (info->sc_flags & UMS_FLAG_X_AXIS) ? "X" : "", 472 (info->sc_flags & UMS_FLAG_Y_AXIS) ? "Y" : "", 473 (info->sc_flags & UMS_FLAG_Z_AXIS) ? "Z" : "", 474 (info->sc_flags & UMS_FLAG_T_AXIS) ? "T" : "", 475 (info->sc_flags & UMS_FLAG_W_AXIS) ? "W" : "", 476 info->sc_iid_x); 477 } 478 479 static int 480 ums_attach(device_t dev) 481 { 482 struct usb_attach_arg *uaa = device_get_ivars(dev); 483 struct ums_softc *sc = device_get_softc(dev); 484 struct ums_info *info; 485 void *d_ptr = NULL; 486 int isize; 487 int err; 488 uint16_t d_len; 489 uint8_t i; 490 uint8_t j; 491 492 DPRINTFN(11, "sc=%p\n", sc); 493 494 device_set_usb_desc(dev); 495 496 mtx_init(&sc->sc_mtx, "ums lock", NULL, MTX_DEF | MTX_RECURSE); 497 498 usb_callout_init_mtx(&sc->sc_callout, &sc->sc_mtx, 0); 499 500 /* 501 * Force the report (non-boot) protocol. 502 * 503 * Mice without boot protocol support may choose not to implement 504 * Set_Protocol at all; Ignore any error. 505 */ 506 err = usbd_req_set_protocol(uaa->device, NULL, 507 uaa->info.bIfaceIndex, 1); 508 509 err = usbd_transfer_setup(uaa->device, 510 &uaa->info.bIfaceIndex, sc->sc_xfer, ums_config, 511 UMS_N_TRANSFER, sc, &sc->sc_mtx); 512 513 if (err) { 514 DPRINTF("error=%s\n", usbd_errstr(err)); 515 goto detach; 516 } 517 err = usbd_req_get_hid_desc(uaa->device, NULL, &d_ptr, 518 &d_len, M_TEMP, uaa->info.bIfaceIndex); 519 520 if (err) { 521 device_printf(dev, "error reading report description\n"); 522 goto detach; 523 } 524 525 isize = hid_report_size(d_ptr, d_len, hid_input, &sc->sc_iid); 526 527 /* 528 * The Microsoft Wireless Notebook Optical Mouse seems to be in worse 529 * shape than the Wireless Intellimouse 2.0, as its X, Y, wheel, and 530 * all of its other button positions are all off. It also reports that 531 * it has two addional buttons and a tilt wheel. 532 */ 533 if (usb_test_quirk(uaa, UQ_MS_BAD_CLASS)) { 534 info = &sc->sc_info[0]; 535 info->sc_flags = (UMS_FLAG_X_AXIS | 536 UMS_FLAG_Y_AXIS | 537 UMS_FLAG_Z_AXIS | 538 UMS_FLAG_SBU); 539 info->sc_buttons = 3; 540 isize = 5; 541 /* 1st byte of descriptor report contains garbage */ 542 info->sc_loc_x.pos = 16; 543 info->sc_loc_y.pos = 24; 544 info->sc_loc_z.pos = 32; 545 info->sc_loc_btn[0].pos = 8; 546 info->sc_loc_btn[1].pos = 9; 547 info->sc_loc_btn[2].pos = 10; 548 549 /* Announce device */ 550 device_printf(dev, "3 buttons and [XYZ] " 551 "coordinates ID=0\n"); 552 553 } else { 554 /* Search the HID descriptor and announce device */ 555 for (i = 0; i < UMS_INFO_MAX; i++) { 556 ums_hid_parse(sc, dev, d_ptr, d_len, i); 557 } 558 } 559 560 if (usb_test_quirk(uaa, UQ_MS_REVZ)) { 561 info = &sc->sc_info[0]; 562 /* Some wheels need the Z axis reversed. */ 563 info->sc_flags |= UMS_FLAG_REVZ; 564 } 565 if (isize > usbd_xfer_max_framelen(sc->sc_xfer[UMS_INTR_DT])) { 566 DPRINTF("WARNING: report size, %d bytes, is larger " 567 "than interrupt size, %d bytes!\n", isize, 568 usbd_xfer_max_framelen(sc->sc_xfer[UMS_INTR_DT])); 569 } 570 free(d_ptr, M_TEMP); 571 d_ptr = NULL; 572 573 #if USB_DEBUG 574 for (j = 0; j < UMS_INFO_MAX; j++) { 575 info = &sc->sc_info[j]; 576 577 DPRINTF("sc=%p, index=%d\n", sc, j); 578 DPRINTF("X\t%d/%d id=%d\n", info->sc_loc_x.pos, 579 info->sc_loc_x.size, info->sc_iid_x); 580 DPRINTF("Y\t%d/%d id=%d\n", info->sc_loc_y.pos, 581 info->sc_loc_y.size, info->sc_iid_y); 582 DPRINTF("Z\t%d/%d id=%d\n", info->sc_loc_z.pos, 583 info->sc_loc_z.size, info->sc_iid_z); 584 DPRINTF("T\t%d/%d id=%d\n", info->sc_loc_t.pos, 585 info->sc_loc_t.size, info->sc_iid_t); 586 DPRINTF("W\t%d/%d id=%d\n", info->sc_loc_w.pos, 587 info->sc_loc_w.size, info->sc_iid_w); 588 589 for (i = 0; i < info->sc_buttons; i++) { 590 DPRINTF("B%d\t%d/%d id=%d\n", 591 i + 1, info->sc_loc_btn[i].pos, 592 info->sc_loc_btn[i].size, info->sc_iid_btn[i]); 593 } 594 } 595 DPRINTF("size=%d, id=%d\n", isize, sc->sc_iid); 596 #endif 597 598 if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON) 599 sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON; 600 else 601 sc->sc_hw.buttons = sc->sc_buttons; 602 603 sc->sc_hw.iftype = MOUSE_IF_USB; 604 sc->sc_hw.type = MOUSE_MOUSE; 605 sc->sc_hw.model = MOUSE_MODEL_GENERIC; 606 sc->sc_hw.hwid = 0; 607 608 sc->sc_mode.protocol = MOUSE_PROTO_MSC; 609 sc->sc_mode.rate = -1; 610 sc->sc_mode.resolution = MOUSE_RES_UNKNOWN; 611 sc->sc_mode.accelfactor = 0; 612 sc->sc_mode.level = 0; 613 sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE; 614 sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK; 615 sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC; 616 617 err = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx, 618 &ums_fifo_methods, &sc->sc_fifo, 619 device_get_unit(dev), 0 - 1, uaa->info.bIfaceIndex, 620 UID_ROOT, GID_OPERATOR, 0644); 621 if (err) { 622 goto detach; 623 } 624 return (0); 625 626 detach: 627 if (d_ptr) { 628 free(d_ptr, M_TEMP); 629 } 630 ums_detach(dev); 631 return (ENOMEM); 632 } 633 634 static int 635 ums_detach(device_t self) 636 { 637 struct ums_softc *sc = device_get_softc(self); 638 639 DPRINTF("sc=%p\n", sc); 640 641 usb_fifo_detach(&sc->sc_fifo); 642 643 usbd_transfer_unsetup(sc->sc_xfer, UMS_N_TRANSFER); 644 645 usb_callout_drain(&sc->sc_callout); 646 647 mtx_destroy(&sc->sc_mtx); 648 649 return (0); 650 } 651 652 static void 653 ums_start_read(struct usb_fifo *fifo) 654 { 655 struct ums_softc *sc = usb_fifo_softc(fifo); 656 657 usbd_transfer_start(sc->sc_xfer[UMS_INTR_DT]); 658 } 659 660 static void 661 ums_stop_read(struct usb_fifo *fifo) 662 { 663 struct ums_softc *sc = usb_fifo_softc(fifo); 664 665 usbd_transfer_stop(sc->sc_xfer[UMS_INTR_DT]); 666 usb_callout_stop(&sc->sc_callout); 667 } 668 669 670 #if ((MOUSE_SYS_PACKETSIZE != 8) || \ 671 (MOUSE_MSC_PACKETSIZE != 5)) 672 #error "Software assumptions are not met. Please update code." 673 #endif 674 675 static void 676 ums_put_queue(struct ums_softc *sc, int32_t dx, int32_t dy, 677 int32_t dz, int32_t dt, int32_t buttons) 678 { 679 uint8_t buf[8]; 680 681 if (1) { 682 683 if (dx > 254) 684 dx = 254; 685 if (dx < -256) 686 dx = -256; 687 if (dy > 254) 688 dy = 254; 689 if (dy < -256) 690 dy = -256; 691 if (dz > 126) 692 dz = 126; 693 if (dz < -128) 694 dz = -128; 695 if (dt > 126) 696 dt = 126; 697 if (dt < -128) 698 dt = -128; 699 700 buf[0] = sc->sc_mode.syncmask[1]; 701 buf[0] |= (~buttons) & MOUSE_MSC_BUTTONS; 702 buf[1] = dx >> 1; 703 buf[2] = dy >> 1; 704 buf[3] = dx - (dx >> 1); 705 buf[4] = dy - (dy >> 1); 706 707 if (sc->sc_mode.level == 1) { 708 buf[5] = dz >> 1; 709 buf[6] = dz - (dz >> 1); 710 buf[7] = (((~buttons) >> 3) & MOUSE_SYS_EXTBUTTONS); 711 } 712 usb_fifo_put_data_linear(sc->sc_fifo.fp[USB_FIFO_RX], buf, 713 sc->sc_mode.packetsize, 1); 714 715 } else { 716 DPRINTF("Buffer full, discarded packet\n"); 717 } 718 } 719 720 static void 721 ums_reset_buf(struct ums_softc *sc) 722 { 723 /* reset read queue */ 724 usb_fifo_reset(sc->sc_fifo.fp[USB_FIFO_RX]); 725 } 726 727 static int 728 ums_open(struct usb_fifo *fifo, int fflags) 729 { 730 struct ums_softc *sc = usb_fifo_softc(fifo); 731 732 DPRINTFN(2, "\n"); 733 734 if (fflags & FREAD) { 735 736 /* reset status */ 737 738 sc->sc_status.flags = 0; 739 sc->sc_status.button = 0; 740 sc->sc_status.obutton = 0; 741 sc->sc_status.dx = 0; 742 sc->sc_status.dy = 0; 743 sc->sc_status.dz = 0; 744 /* sc->sc_status.dt = 0; */ 745 746 if (usb_fifo_alloc_buffer(fifo, 747 UMS_BUF_SIZE, UMS_IFQ_MAXLEN)) { 748 return (ENOMEM); 749 } 750 } 751 return (0); 752 } 753 754 static void 755 ums_close(struct usb_fifo *fifo, int fflags) 756 { 757 if (fflags & FREAD) { 758 usb_fifo_free_buffer(fifo); 759 } 760 } 761 762 static int 763 ums_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags) 764 { 765 struct ums_softc *sc = usb_fifo_softc(fifo); 766 mousemode_t mode; 767 int error = 0; 768 769 DPRINTFN(2, "\n"); 770 771 mtx_lock(&sc->sc_mtx); 772 773 switch (cmd) { 774 case MOUSE_GETHWINFO: 775 *(mousehw_t *)addr = sc->sc_hw; 776 break; 777 778 case MOUSE_GETMODE: 779 *(mousemode_t *)addr = sc->sc_mode; 780 break; 781 782 case MOUSE_SETMODE: 783 mode = *(mousemode_t *)addr; 784 785 if (mode.level == -1) { 786 /* don't change the current setting */ 787 } else if ((mode.level < 0) || (mode.level > 1)) { 788 error = EINVAL; 789 goto done; 790 } else { 791 sc->sc_mode.level = mode.level; 792 } 793 794 if (sc->sc_mode.level == 0) { 795 if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON) 796 sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON; 797 else 798 sc->sc_hw.buttons = sc->sc_buttons; 799 sc->sc_mode.protocol = MOUSE_PROTO_MSC; 800 sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE; 801 sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK; 802 sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC; 803 } else if (sc->sc_mode.level == 1) { 804 if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON) 805 sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON; 806 else 807 sc->sc_hw.buttons = sc->sc_buttons; 808 sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE; 809 sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE; 810 sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK; 811 sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC; 812 } 813 ums_reset_buf(sc); 814 break; 815 816 case MOUSE_GETLEVEL: 817 *(int *)addr = sc->sc_mode.level; 818 break; 819 820 case MOUSE_SETLEVEL: 821 if (*(int *)addr < 0 || *(int *)addr > 1) { 822 error = EINVAL; 823 goto done; 824 } 825 sc->sc_mode.level = *(int *)addr; 826 827 if (sc->sc_mode.level == 0) { 828 if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON) 829 sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON; 830 else 831 sc->sc_hw.buttons = sc->sc_buttons; 832 sc->sc_mode.protocol = MOUSE_PROTO_MSC; 833 sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE; 834 sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK; 835 sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC; 836 } else if (sc->sc_mode.level == 1) { 837 if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON) 838 sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON; 839 else 840 sc->sc_hw.buttons = sc->sc_buttons; 841 sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE; 842 sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE; 843 sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK; 844 sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC; 845 } 846 ums_reset_buf(sc); 847 break; 848 849 case MOUSE_GETSTATUS:{ 850 mousestatus_t *status = (mousestatus_t *)addr; 851 852 *status = sc->sc_status; 853 sc->sc_status.obutton = sc->sc_status.button; 854 sc->sc_status.button = 0; 855 sc->sc_status.dx = 0; 856 sc->sc_status.dy = 0; 857 sc->sc_status.dz = 0; 858 /* sc->sc_status.dt = 0; */ 859 860 if (status->dx || status->dy || status->dz /* || status->dt */ ) { 861 status->flags |= MOUSE_POSCHANGED; 862 } 863 if (status->button != status->obutton) { 864 status->flags |= MOUSE_BUTTONSCHANGED; 865 } 866 break; 867 } 868 default: 869 error = ENOTTY; 870 } 871 872 done: 873 mtx_unlock(&sc->sc_mtx); 874 return (error); 875 } 876 877 static devclass_t ums_devclass; 878 879 static device_method_t ums_methods[] = { 880 DEVMETHOD(device_probe, ums_probe), 881 DEVMETHOD(device_attach, ums_attach), 882 DEVMETHOD(device_detach, ums_detach), 883 {0, 0} 884 }; 885 886 static driver_t ums_driver = { 887 .name = "ums", 888 .methods = ums_methods, 889 .size = sizeof(struct ums_softc), 890 }; 891 892 DRIVER_MODULE(ums, uhub, ums_driver, ums_devclass, NULL, 0); 893 MODULE_DEPEND(ums, usb, 1, 1, 1); 894