1 /* $NetBSD: ums.c,v 1.80 2010/01/14 09:30:39 matthias Exp $ */ 2 3 /* 4 * Copyright (c) 1998 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Lennart Augustsson (lennart@augustsson.net) at 9 * Carlstedt Research & Technology. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /* 34 * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf 35 */ 36 37 #include <sys/cdefs.h> 38 __KERNEL_RCSID(0, "$NetBSD: ums.c,v 1.80 2010/01/14 09:30:39 matthias Exp $"); 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/kernel.h> 43 #include <sys/malloc.h> 44 #include <sys/device.h> 45 #include <sys/ioctl.h> 46 #include <sys/file.h> 47 #include <sys/select.h> 48 #include <sys/proc.h> 49 #include <sys/vnode.h> 50 #include <sys/poll.h> 51 52 #include <dev/usb/usb.h> 53 #include <dev/usb/usbhid.h> 54 55 #include <dev/usb/usbdi.h> 56 #include <dev/usb/usbdi_util.h> 57 #include <dev/usb/usbdevs.h> 58 #include <dev/usb/usb_quirks.h> 59 #include <dev/usb/uhidev.h> 60 #include <dev/usb/hid.h> 61 62 #include <dev/wscons/wsconsio.h> 63 #include <dev/wscons/wsmousevar.h> 64 65 #ifdef USB_DEBUG 66 #define DPRINTF(x) if (umsdebug) logprintf x 67 #define DPRINTFN(n,x) if (umsdebug>(n)) logprintf x 68 int umsdebug = 0; 69 #else 70 #define DPRINTF(x) 71 #define DPRINTFN(n,x) 72 #endif 73 74 #define UMS_BUT(i) ((i) == 1 || (i) == 2 ? 3 - (i) : i) 75 76 #define UMSUNIT(s) (minor(s)) 77 78 #define PS2LBUTMASK x01 79 #define PS2RBUTMASK x02 80 #define PS2MBUTMASK x04 81 #define PS2BUTMASK 0x0f 82 83 #define MAX_BUTTONS 31 /* must not exceed size of sc_buttons */ 84 85 struct ums_softc { 86 struct uhidev sc_hdev; 87 88 struct hid_location sc_loc_x, sc_loc_y, sc_loc_z, sc_loc_w; 89 struct hid_location sc_loc_btn[MAX_BUTTONS]; 90 91 int sc_enabled; 92 93 int flags; /* device configuration */ 94 #define UMS_Z 0x01 /* z direction available */ 95 #define UMS_SPUR_BUT_UP 0x02 /* spurious button up events */ 96 #define UMS_REVZ 0x04 /* Z-axis is reversed */ 97 #define UMS_W 0x08 /* w direction/tilt available */ 98 #define UMS_ABS 0x10 /* absolute position, touchpanel */ 99 100 int nbuttons; 101 102 u_int32_t sc_buttons; /* mouse button status */ 103 device_t sc_wsmousedev; 104 105 char sc_dying; 106 }; 107 108 #define MOUSE_FLAGS_MASK (HIO_CONST|HIO_RELATIVE) 109 110 Static void ums_intr(struct uhidev *addr, void *ibuf, u_int len); 111 112 Static int ums_enable(void *); 113 Static void ums_disable(void *); 114 Static int ums_ioctl(void *, u_long, void *, int, struct lwp * ); 115 116 const struct wsmouse_accessops ums_accessops = { 117 ums_enable, 118 ums_ioctl, 119 ums_disable, 120 }; 121 122 int ums_match(device_t, cfdata_t, void *); 123 void ums_attach(device_t, device_t, void *); 124 void ums_childdet(device_t, device_t); 125 int ums_detach(device_t, int); 126 int ums_activate(device_t, enum devact); 127 extern struct cfdriver ums_cd; 128 CFATTACH_DECL2_NEW(ums, sizeof(struct ums_softc), ums_match, ums_attach, 129 ums_detach, ums_activate, NULL, ums_childdet); 130 131 int 132 ums_match(device_t parent, cfdata_t match, void *aux) 133 { 134 struct uhidev_attach_arg *uha = aux; 135 int size; 136 void *desc; 137 138 /* 139 * Some (older) Griffin PowerMate knobs may masquerade as a 140 * mouse, avoid treating them as such, they have only one axis. 141 */ 142 if (uha->uaa->vendor == USB_VENDOR_GRIFFIN && 143 uha->uaa->product == USB_PRODUCT_GRIFFIN_POWERMATE) 144 return (UMATCH_NONE); 145 146 uhidev_get_report_desc(uha->parent, &desc, &size); 147 if (!hid_is_collection(desc, size, uha->reportid, 148 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE)) && 149 !hid_is_collection(desc, size, uha->reportid, 150 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_POINTER))) 151 return (UMATCH_NONE); 152 153 return (UMATCH_IFACECLASS); 154 } 155 156 void 157 ums_attach(device_t parent, device_t self, void *aux) 158 { 159 struct ums_softc *sc = device_private(self); 160 struct uhidev_attach_arg *uha = aux; 161 struct wsmousedev_attach_args a; 162 int size; 163 void *desc; 164 u_int32_t flags, quirks; 165 int i, hl; 166 struct hid_location *zloc; 167 struct hid_location loc_btn; 168 169 aprint_naive("\n"); 170 171 sc->sc_hdev.sc_dev = self; 172 sc->sc_hdev.sc_intr = ums_intr; 173 sc->sc_hdev.sc_parent = uha->parent; 174 sc->sc_hdev.sc_report_id = uha->reportid; 175 176 quirks = usbd_get_quirks(uha->parent->sc_udev)->uq_flags; 177 if (quirks & UQ_MS_REVZ) 178 sc->flags |= UMS_REVZ; 179 if (quirks & UQ_SPUR_BUT_UP) 180 sc->flags |= UMS_SPUR_BUT_UP; 181 182 uhidev_get_report_desc(uha->parent, &desc, &size); 183 184 if (!pmf_device_register(self, NULL, NULL)) 185 aprint_error_dev(self, "couldn't establish power handler\n"); 186 187 if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X), 188 uha->reportid, hid_input, &sc->sc_loc_x, &flags)) { 189 aprint_error("\n%s: mouse has no X report\n", 190 USBDEVNAME(sc->sc_hdev.sc_dev)); 191 USB_ATTACH_ERROR_RETURN; 192 } 193 switch (flags & MOUSE_FLAGS_MASK) { 194 case 0: 195 sc->flags |= UMS_ABS; 196 break; 197 case HIO_RELATIVE: 198 break; 199 default: 200 aprint_error("\n%s: X report 0x%04x not supported\n", 201 USBDEVNAME(sc->sc_hdev.sc_dev), flags); 202 USB_ATTACH_ERROR_RETURN; 203 } 204 205 if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y), 206 uha->reportid, hid_input, &sc->sc_loc_y, &flags)) { 207 aprint_error("\n%s: mouse has no Y report\n", 208 USBDEVNAME(sc->sc_hdev.sc_dev)); 209 USB_ATTACH_ERROR_RETURN; 210 } 211 switch (flags & MOUSE_FLAGS_MASK) { 212 case 0: 213 sc->flags |= UMS_ABS; 214 break; 215 case HIO_RELATIVE: 216 break; 217 default: 218 aprint_error("\n%s: Y report 0x%04x not supported\n", 219 USBDEVNAME(sc->sc_hdev.sc_dev), flags); 220 USB_ATTACH_ERROR_RETURN; 221 } 222 223 /* Try the wheel first as the Z activator since it's tradition. */ 224 hl = hid_locate(desc, 225 size, 226 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_WHEEL), 227 uha->reportid, 228 hid_input, 229 &sc->sc_loc_z, 230 &flags); 231 232 zloc = &sc->sc_loc_z; 233 if (hl) { 234 if ((flags & MOUSE_FLAGS_MASK) != HIO_RELATIVE) { 235 aprint_verbose("\n%s: Wheel report 0x%04x not " 236 "supported\n", USBDEVNAME(sc->sc_hdev.sc_dev), 237 flags); 238 sc->sc_loc_z.size = 0; /* Bad Z coord, ignore it */ 239 } else { 240 sc->flags |= UMS_Z; 241 /* Wheels need the Z axis reversed. */ 242 sc->flags ^= UMS_REVZ; 243 /* Put Z on the W coordinate */ 244 zloc = &sc->sc_loc_w; 245 } 246 } 247 248 hl = hid_locate(desc, 249 size, 250 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Z), 251 uha->reportid, 252 hid_input, 253 zloc, 254 &flags); 255 256 /* 257 * The horizontal component of the scrollball can also be given by 258 * Application Control Pan in the Consumer page, so if we didnt see 259 * any Z then check that. 260 */ 261 if (!hl) { 262 hl = hid_locate(desc, 263 size, 264 HID_USAGE2(HUP_CONSUMER, HUC_AC_PAN), 265 uha->reportid, 266 hid_input, 267 zloc, 268 &flags); 269 } 270 271 if (hl) { 272 if ((flags & MOUSE_FLAGS_MASK) != HIO_RELATIVE) { 273 aprint_verbose("\n%s: Z report 0x%04x not supported\n", 274 USBDEVNAME(sc->sc_hdev.sc_dev), flags); 275 zloc->size = 0; /* Bad Z coord, ignore it */ 276 } else { 277 if (sc->flags & UMS_Z) 278 sc->flags |= UMS_W; 279 else 280 sc->flags |= UMS_Z; 281 } 282 } 283 284 /* 285 * The Microsoft Wireless Laser Mouse 6000 v2.0 reports a bad 286 * position for the wheel and wheel tilt controls -- should be 287 * in bytes 3 & 4 of the report. Fix this if necessary. 288 */ 289 if (uha->uaa->vendor == USB_VENDOR_MICROSOFT && 290 (uha->uaa->product == USB_PRODUCT_MICROSOFT_24GHZ_XCVR10 || 291 uha->uaa->product == USB_PRODUCT_MICROSOFT_24GHZ_XCVR20)) { 292 if ((sc->flags & UMS_Z) && sc->sc_loc_z.pos == 0) 293 sc->sc_loc_z.pos = 24; 294 if ((sc->flags & UMS_W) && sc->sc_loc_w.pos == 0) 295 sc->sc_loc_w.pos = sc->sc_loc_z.pos + 8; 296 } 297 298 /* figure out the number of buttons */ 299 for (i = 1; i <= MAX_BUTTONS; i++) 300 if (!hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, i), 301 uha->reportid, hid_input, &loc_btn, 0)) 302 break; 303 sc->nbuttons = i - 1; 304 305 aprint_normal(": %d button%s%s%s%s\n", 306 sc->nbuttons, sc->nbuttons == 1 ? "" : "s", 307 sc->flags & UMS_W ? ", W" : "", 308 sc->flags & UMS_Z ? " and Z dir" : "", 309 sc->flags & UMS_W ? "s" : ""); 310 311 for (i = 1; i <= sc->nbuttons; i++) 312 hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, i), 313 uha->reportid, hid_input, 314 &sc->sc_loc_btn[i-1], 0); 315 316 #ifdef USB_DEBUG 317 DPRINTF(("ums_attach: sc=%p\n", sc)); 318 DPRINTF(("ums_attach: X\t%d/%d\n", 319 sc->sc_loc_x.pos, sc->sc_loc_x.size)); 320 DPRINTF(("ums_attach: Y\t%d/%d\n", 321 sc->sc_loc_y.pos, sc->sc_loc_y.size)); 322 if (sc->flags & UMS_Z) 323 DPRINTF(("ums_attach: Z\t%d/%d\n", 324 sc->sc_loc_z.pos, sc->sc_loc_z.size)); 325 if (sc->flags & UMS_W) 326 DPRINTF(("ums_attach: W\t%d/%d\n", 327 sc->sc_loc_w.pos, sc->sc_loc_w.size)); 328 for (i = 1; i <= sc->nbuttons; i++) { 329 DPRINTF(("ums_attach: B%d\t%d/%d\n", 330 i, sc->sc_loc_btn[i-1].pos,sc->sc_loc_btn[i-1].size)); 331 } 332 #endif 333 334 a.accessops = &ums_accessops; 335 a.accesscookie = sc; 336 337 sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint); 338 339 USB_ATTACH_SUCCESS_RETURN; 340 } 341 342 int 343 ums_activate(device_ptr_t self, enum devact act) 344 { 345 struct ums_softc *sc = device_private(self); 346 347 switch (act) { 348 case DVACT_DEACTIVATE: 349 sc->sc_dying = 1; 350 return 0; 351 default: 352 return EOPNOTSUPP; 353 } 354 } 355 356 void 357 ums_childdet(device_t self, device_t child) 358 { 359 struct ums_softc *sc = device_private(self); 360 361 KASSERT(sc->sc_wsmousedev == child); 362 sc->sc_wsmousedev = NULL; 363 } 364 365 int 366 ums_detach(device_t self, int flags) 367 { 368 struct ums_softc *sc = device_private(self); 369 int rv = 0; 370 371 DPRINTF(("ums_detach: sc=%p flags=%d\n", sc, flags)); 372 373 /* No need to do reference counting of ums, wsmouse has all the goo. */ 374 if (sc->sc_wsmousedev != NULL) 375 rv = config_detach(sc->sc_wsmousedev, flags); 376 377 pmf_device_deregister(self); 378 379 return (rv); 380 } 381 382 void 383 ums_intr(struct uhidev *addr, void *ibuf, u_int len) 384 { 385 struct ums_softc *sc = (struct ums_softc *)addr; 386 int dx, dy, dz, dw; 387 u_int32_t buttons = 0; 388 int i, flags, s; 389 390 DPRINTFN(5,("ums_intr: len=%d\n", len)); 391 392 flags = WSMOUSE_INPUT_DELTA; /* equals 0 */ 393 394 dx = hid_get_data(ibuf, &sc->sc_loc_x); 395 if (sc->flags & UMS_ABS) { 396 flags |= (WSMOUSE_INPUT_ABSOLUTE_X | WSMOUSE_INPUT_ABSOLUTE_Y); 397 dy = hid_get_data(ibuf, &sc->sc_loc_y); 398 } else 399 dy = -hid_get_data(ibuf, &sc->sc_loc_y); 400 dz = hid_get_data(ibuf, &sc->sc_loc_z); 401 dw = hid_get_data(ibuf, &sc->sc_loc_w); 402 403 if (sc->flags & UMS_REVZ) 404 dz = -dz; 405 for (i = 0; i < sc->nbuttons; i++) 406 if (hid_get_data(ibuf, &sc->sc_loc_btn[i])) 407 buttons |= (1 << UMS_BUT(i)); 408 409 if (dx != 0 || dy != 0 || dz != 0 || dw != 0 || 410 buttons != sc->sc_buttons) { 411 DPRINTFN(10, ("ums_intr: x:%d y:%d z:%d w:%d buttons:0x%x\n", 412 dx, dy, dz, dw, buttons)); 413 sc->sc_buttons = buttons; 414 if (sc->sc_wsmousedev != NULL) { 415 s = spltty(); 416 wsmouse_input(sc->sc_wsmousedev, buttons, dx, dy, dz, 417 dw, flags); 418 splx(s); 419 } 420 } 421 } 422 423 Static int 424 ums_enable(void *v) 425 { 426 struct ums_softc *sc = v; 427 428 DPRINTFN(1,("ums_enable: sc=%p\n", sc)); 429 430 if (sc->sc_dying) 431 return (EIO); 432 433 if (sc->sc_enabled) 434 return (EBUSY); 435 436 sc->sc_enabled = 1; 437 sc->sc_buttons = 0; 438 439 return (uhidev_open(&sc->sc_hdev)); 440 } 441 442 Static void 443 ums_disable(void *v) 444 { 445 struct ums_softc *sc = v; 446 447 DPRINTFN(1,("ums_disable: sc=%p\n", sc)); 448 #ifdef DIAGNOSTIC 449 if (!sc->sc_enabled) { 450 printf("ums_disable: not enabled\n"); 451 return; 452 } 453 #endif 454 455 sc->sc_enabled = 0; 456 uhidev_close(&sc->sc_hdev); 457 } 458 459 Static int 460 ums_ioctl(void *v, u_long cmd, void *data, int flag, 461 struct lwp * p) 462 463 { 464 struct ums_softc *sc = v; 465 466 switch (cmd) { 467 case WSMOUSEIO_GTYPE: 468 if (sc->flags & UMS_ABS) 469 *(u_int *)data = WSMOUSE_TYPE_TPANEL; 470 else 471 *(u_int *)data = WSMOUSE_TYPE_USB; 472 return (0); 473 } 474 475 return (EPASSTHROUGH); 476 } 477