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