1 /* $OpenBSD: uhidev.c,v 1.15 2006/06/17 16:27:58 miod Exp $ */ 2 /* $NetBSD: uhidev.c,v 1.14 2003/03/11 16:44:00 augustss Exp $ */ 3 4 /* 5 * Copyright (c) 2001 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Lennart Augustsson (lennart@augustsson.net) at 10 * Carlstedt Research & Technology. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by the NetBSD 23 * Foundation, Inc. and its contributors. 24 * 4. Neither the name of The NetBSD Foundation nor the names of its 25 * contributors may be used to endorse or promote products derived 26 * from this software without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 38 * POSSIBILITY OF SUCH DAMAGE. 39 */ 40 41 /* 42 * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf 43 */ 44 45 #include <sys/param.h> 46 #include <sys/systm.h> 47 #include <sys/kernel.h> 48 #include <sys/malloc.h> 49 #include <sys/signalvar.h> 50 #include <sys/device.h> 51 #include <sys/ioctl.h> 52 #include <sys/conf.h> 53 54 #include <dev/usb/usb.h> 55 #include <dev/usb/usbhid.h> 56 57 #include <dev/usb/usbdevs.h> 58 #include <dev/usb/usbdi.h> 59 #include <dev/usb/usbdi_util.h> 60 #include <dev/usb/hid.h> 61 #include <dev/usb/usb_quirks.h> 62 63 #include <dev/usb/uhidev.h> 64 65 /* Report descriptor for broken Wacom Graphire */ 66 #include <dev/usb/ugraphire_rdesc.h> 67 68 #ifdef UHIDEV_DEBUG 69 #define DPRINTF(x) do { if (uhidevdebug) logprintf x; } while (0) 70 #define DPRINTFN(n,x) do { if (uhidevdebug>(n)) logprintf x; } while (0) 71 int uhidevdebug = 0; 72 #else 73 #define DPRINTF(x) 74 #define DPRINTFN(n,x) 75 #endif 76 77 Static void uhidev_intr(usbd_xfer_handle, usbd_private_handle, usbd_status); 78 79 Static int uhidev_maxrepid(void *buf, int len); 80 Static int uhidevprint(void *aux, const char *pnp); 81 #if defined(__NetBSD__) 82 Static int uhidevsubmatch(struct device *parent, struct cfdata *cf, void *aux); 83 #else 84 Static int uhidevsubmatch(struct device *parent, void *cf, void *aux); 85 #endif 86 87 USB_DECLARE_DRIVER(uhidev); 88 89 USB_MATCH(uhidev) 90 { 91 USB_MATCH_START(uhidev, uaa); 92 usb_interface_descriptor_t *id; 93 94 if (uaa->iface == NULL) 95 return (UMATCH_NONE); 96 id = usbd_get_interface_descriptor(uaa->iface); 97 if (id == NULL || id->bInterfaceClass != UICLASS_HID) 98 return (UMATCH_NONE); 99 if (usbd_get_quirks(uaa->device)->uq_flags & UQ_BAD_HID) 100 return (UMATCH_NONE); 101 if (uaa->matchlvl) 102 return (uaa->matchlvl); 103 104 #ifdef __macppc__ 105 /* 106 * Some Apple laptops have USB phantom devices which match 107 * the ADB devices. We want to ignore them to avoid 108 * confusing users, as the real hardware underneath is adb 109 * and has already attached. 110 */ 111 if (uaa->vendor == USB_VENDOR_APPLE && 112 uaa->product == USB_PRODUCT_APPLE_ADB) 113 return (UMATCH_NONE); 114 #endif 115 116 return (UMATCH_IFACECLASS_GENERIC); 117 } 118 119 USB_ATTACH(uhidev) 120 { 121 USB_ATTACH_START(uhidev, sc, uaa); 122 usbd_interface_handle iface = uaa->iface; 123 usb_interface_descriptor_t *id; 124 usb_endpoint_descriptor_t *ed; 125 struct uhidev_attach_arg uha; 126 struct uhidev *dev; 127 int size, nrepid, repid, repsz; 128 int repsizes[256]; 129 void *desc; 130 const void *descptr; 131 usbd_status err; 132 char *devinfop; 133 134 sc->sc_udev = uaa->device; 135 sc->sc_iface = iface; 136 id = usbd_get_interface_descriptor(iface); 137 138 devinfop = usbd_devinfo_alloc(uaa->device, 0); 139 USB_ATTACH_SETUP; 140 printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev), 141 devinfop, id->bInterfaceClass, id->bInterfaceSubClass); 142 usbd_devinfo_free(devinfop); 143 144 (void)usbd_set_idle(iface, 0, 0); 145 #if 0 146 147 qflags = usbd_get_quirks(sc->sc_udev)->uq_flags; 148 if ((qflags & UQ_NO_SET_PROTO) == 0 && 149 id->bInterfaceSubClass != UISUBCLASS_BOOT) 150 (void)usbd_set_protocol(iface, 1); 151 #endif 152 153 ed = usbd_interface2endpoint_descriptor(iface, 0); 154 if (ed == NULL) { 155 printf("%s: could not read endpoint descriptor\n", 156 USBDEVNAME(sc->sc_dev)); 157 sc->sc_dying = 1; 158 USB_ATTACH_ERROR_RETURN; 159 } 160 161 DPRINTFN(10,("uhidev_attach: bLength=%d bDescriptorType=%d " 162 "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d" 163 " bInterval=%d\n", 164 ed->bLength, ed->bDescriptorType, 165 ed->bEndpointAddress & UE_ADDR, 166 UE_GET_DIR(ed->bEndpointAddress)==UE_DIR_IN? "in" : "out", 167 ed->bmAttributes & UE_XFERTYPE, 168 UGETW(ed->wMaxPacketSize), ed->bInterval)); 169 170 if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_IN || 171 (ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) { 172 printf("%s: unexpected endpoint\n", USBDEVNAME(sc->sc_dev)); 173 sc->sc_dying = 1; 174 USB_ATTACH_ERROR_RETURN; 175 } 176 177 sc->sc_ep_addr = ed->bEndpointAddress; 178 179 /* XXX need to extend this */ 180 descptr = NULL; 181 if (uaa->vendor == USB_VENDOR_WACOM) { 182 static uByte reportbuf[] = {2, 2, 2}; 183 184 /* The report descriptor for the Wacom Graphire is broken. */ 185 switch (uaa->product) { 186 case USB_PRODUCT_WACOM_GRAPHIRE: 187 size = sizeof uhid_graphire_report_descr; 188 descptr = uhid_graphire_report_descr; 189 break; 190 case USB_PRODUCT_WACOM_GRAPHIRE3_4X5: 191 case USB_PRODUCT_WACOM_GRAPHIRE4_4X5: 192 usbd_set_report(uaa->iface, UHID_FEATURE_REPORT, 2, 193 &reportbuf, sizeof reportbuf); 194 size = sizeof uhid_graphire3_4x5_report_descr; 195 descptr = uhid_graphire3_4x5_report_descr; 196 break; 197 default: 198 /* Keep descriptor */ 199 break; 200 } 201 } 202 203 if (descptr) { 204 desc = malloc(size, M_USBDEV, M_NOWAIT); 205 if (desc == NULL) 206 err = USBD_NOMEM; 207 else { 208 err = USBD_NORMAL_COMPLETION; 209 memcpy(desc, descptr, size); 210 } 211 } else { 212 desc = NULL; 213 err = usbd_read_report_desc(uaa->iface, &desc, &size, M_USBDEV); 214 } 215 if (err) { 216 printf("%s: no report descriptor\n", USBDEVNAME(sc->sc_dev)); 217 sc->sc_dying = 1; 218 USB_ATTACH_ERROR_RETURN; 219 } 220 221 sc->sc_repdesc = desc; 222 sc->sc_repdesc_size = size; 223 224 uha.uaa = uaa; 225 nrepid = uhidev_maxrepid(desc, size); 226 if (nrepid < 0) 227 USB_ATTACH_SUCCESS_RETURN; 228 if (nrepid > 0) 229 printf("%s: %d report ids\n", USBDEVNAME(sc->sc_dev), nrepid); 230 nrepid++; 231 sc->sc_subdevs = malloc(nrepid * sizeof(device_ptr_t), 232 M_USBDEV, M_NOWAIT); 233 bzero(sc->sc_subdevs, nrepid * sizeof(device_ptr_t)); 234 if (sc->sc_subdevs == NULL) { 235 printf("%s: no memory\n", USBDEVNAME(sc->sc_dev)); 236 USB_ATTACH_ERROR_RETURN; 237 } 238 sc->sc_nrepid = nrepid; 239 sc->sc_isize = 0; 240 241 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, 242 USBDEV(sc->sc_dev)); 243 244 for (repid = 0; repid < nrepid; repid++) { 245 repsz = hid_report_size(desc, size, hid_input, repid); 246 DPRINTF(("uhidev_match: repid=%d, repsz=%d\n", repid, repsz)); 247 repsizes[repid] = repsz; 248 if (repsz > 0) { 249 if (repsz > sc->sc_isize) 250 sc->sc_isize = repsz; 251 } 252 } 253 sc->sc_isize += nrepid != 1; /* space for report ID */ 254 DPRINTF(("uhidev_attach: isize=%d\n", sc->sc_isize)); 255 256 uha.parent = sc; 257 for (repid = 0; repid < nrepid; repid++) { 258 DPRINTF(("uhidev_match: try repid=%d\n", repid)); 259 if (hid_report_size(desc, size, hid_input, repid) == 0 && 260 hid_report_size(desc, size, hid_output, repid) == 0 && 261 hid_report_size(desc, size, hid_feature, repid) == 0) { 262 ; /* already NULL in sc->sc_subdevs[repid] */ 263 } else { 264 uha.reportid = repid; 265 dev = (struct uhidev *)config_found_sm(self, &uha, 266 uhidevprint, uhidevsubmatch); 267 sc->sc_subdevs[repid] = dev; 268 if (dev != NULL) { 269 dev->sc_in_rep_size = repsizes[repid]; 270 #ifdef DIAGNOSTIC 271 DPRINTF(("uhidev_match: repid=%d dev=%p\n", 272 repid, dev)); 273 if (dev->sc_intr == NULL) { 274 printf("%s: sc_intr == NULL\n", 275 USBDEVNAME(sc->sc_dev)); 276 USB_ATTACH_ERROR_RETURN; 277 } 278 #endif 279 } 280 } 281 } 282 283 USB_ATTACH_SUCCESS_RETURN; 284 } 285 286 int 287 uhidev_maxrepid(void *buf, int len) 288 { 289 struct hid_data *d; 290 struct hid_item h; 291 int maxid; 292 293 maxid = -1; 294 h.report_ID = 0; 295 for (d = hid_start_parse(buf, len, hid_none); hid_get_item(d, &h); ) 296 if (h.report_ID > maxid) 297 maxid = h.report_ID; 298 hid_end_parse(d); 299 return (maxid); 300 } 301 302 int 303 uhidevprint(void *aux, const char *pnp) 304 { 305 struct uhidev_attach_arg *uha = aux; 306 307 if (pnp) 308 printf("uhid at %s", pnp); 309 if (uha->reportid != 0) 310 printf(" reportid %d", uha->reportid); 311 return (UNCONF); 312 } 313 314 #if defined(__NetBSD__) 315 Static int uhidevsubmatch(struct device *parent, struct cfdata *cf, void *aux) 316 #else 317 Static int uhidevsubmatch(struct device *parent, void *match, void *aux) 318 #endif 319 { 320 struct uhidev_attach_arg *uha = aux; 321 #if defined(__OpenBSD__) 322 struct cfdata *cf = match; 323 #endif 324 325 if (cf->uhidevcf_reportid != UHIDEV_UNK_REPORTID && 326 cf->uhidevcf_reportid != uha->reportid) 327 return (0); 328 if (cf->uhidevcf_reportid == uha->reportid) 329 uha->matchlvl = UMATCH_VENDOR_PRODUCT; 330 else 331 uha->matchlvl = 0; 332 return ((*cf->cf_attach->ca_match)(parent, cf, aux)); 333 } 334 335 int 336 uhidev_activate(device_ptr_t self, enum devact act) 337 { 338 struct uhidev_softc *sc = (struct uhidev_softc *)self; 339 int i, rv; 340 341 switch (act) { 342 case DVACT_ACTIVATE: 343 return (EOPNOTSUPP); 344 345 case DVACT_DEACTIVATE: 346 rv = 0; 347 for (i = 0; i < sc->sc_nrepid; i++) 348 if (sc->sc_subdevs[i] != NULL) 349 rv |= config_deactivate( 350 &sc->sc_subdevs[i]->sc_dev); 351 sc->sc_dying = 1; 352 break; 353 } 354 return (rv); 355 } 356 357 USB_DETACH(uhidev) 358 { 359 USB_DETACH_START(uhidev, sc); 360 int i, rv; 361 362 DPRINTF(("uhidev_detach: sc=%p flags=%d\n", sc, flags)); 363 364 sc->sc_dying = 1; 365 if (sc->sc_intrpipe != NULL) 366 usbd_abort_pipe(sc->sc_intrpipe); 367 368 if (sc->sc_repdesc != NULL) 369 free(sc->sc_repdesc, M_USBDEV); 370 371 rv = 0; 372 for (i = 0; i < sc->sc_nrepid; i++) { 373 if (sc->sc_subdevs[i] != NULL) { 374 rv |= config_detach(&sc->sc_subdevs[i]->sc_dev, flags); 375 sc->sc_subdevs[i] = NULL; 376 } 377 } 378 379 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, 380 USBDEV(sc->sc_dev)); 381 382 return (rv); 383 } 384 385 void 386 uhidev_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status) 387 { 388 struct uhidev_softc *sc = addr; 389 struct uhidev *scd; 390 u_char *p; 391 u_int rep; 392 u_int32_t cc; 393 394 usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL); 395 396 #ifdef UHIDEV_DEBUG 397 if (uhidevdebug > 5) { 398 u_int32_t i; 399 400 DPRINTF(("uhidev_intr: status=%d cc=%d\n", status, cc)); 401 DPRINTF(("uhidev_intr: data =")); 402 for (i = 0; i < cc; i++) 403 DPRINTF((" %02x", sc->sc_ibuf[i])); 404 DPRINTF(("\n")); 405 } 406 #endif 407 408 if (status == USBD_CANCELLED) 409 return; 410 411 if (status != USBD_NORMAL_COMPLETION) { 412 DPRINTF(("%s: interrupt status=%d\n", USBDEVNAME(sc->sc_dev), 413 status)); 414 usbd_clear_endpoint_stall_async(sc->sc_intrpipe); 415 return; 416 } 417 418 p = sc->sc_ibuf; 419 if (sc->sc_nrepid != 1) 420 rep = *p++, cc--; 421 else 422 rep = 0; 423 if (rep >= sc->sc_nrepid) { 424 printf("uhidev_intr: bad repid %d\n", rep); 425 return; 426 } 427 scd = sc->sc_subdevs[rep]; 428 DPRINTFN(5,("uhidev_intr: rep=%d, scd=%p state=0x%x\n", 429 rep, scd, scd ? scd->sc_state : 0)); 430 if (scd == NULL || !(scd->sc_state & UHIDEV_OPEN)) 431 return; 432 #ifdef DIAGNOSTIC 433 if (scd->sc_in_rep_size != cc) 434 printf("%s: bad input length %d != %d\n",USBDEVNAME(sc->sc_dev), 435 scd->sc_in_rep_size, cc); 436 #endif 437 scd->sc_intr(scd, p, cc); 438 } 439 440 void 441 uhidev_get_report_desc(struct uhidev_softc *sc, void **desc, int *size) 442 { 443 *desc = sc->sc_repdesc; 444 *size = sc->sc_repdesc_size; 445 } 446 447 int 448 uhidev_open(struct uhidev *scd) 449 { 450 struct uhidev_softc *sc = scd->sc_parent; 451 usbd_status err; 452 453 DPRINTF(("uhidev_open: open pipe, state=%d refcnt=%d\n", 454 scd->sc_state, sc->sc_refcnt)); 455 456 if (scd->sc_state & UHIDEV_OPEN) 457 return (EBUSY); 458 scd->sc_state |= UHIDEV_OPEN; 459 if (sc->sc_refcnt++) 460 return (0); 461 462 if (sc->sc_isize == 0) 463 return (0); 464 465 sc->sc_ibuf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK); 466 467 /* Set up interrupt pipe. */ 468 DPRINTF(("uhidev_open: isize=%d, ep=0x%02x\n", sc->sc_isize, 469 sc->sc_ep_addr)); 470 err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr, 471 USBD_SHORT_XFER_OK, &sc->sc_intrpipe, sc, sc->sc_ibuf, 472 sc->sc_isize, uhidev_intr, USBD_DEFAULT_INTERVAL); 473 if (err) { 474 DPRINTF(("uhidopen: usbd_open_pipe_intr failed, " 475 "error=%d\n",err)); 476 free(sc->sc_ibuf, M_USBDEV); 477 scd->sc_state &= ~UHIDEV_OPEN; 478 sc->sc_refcnt = 0; 479 sc->sc_intrpipe = NULL; 480 return (EIO); 481 } 482 return (0); 483 } 484 485 void 486 uhidev_close(struct uhidev *scd) 487 { 488 struct uhidev_softc *sc = scd->sc_parent; 489 490 if (!(scd->sc_state & UHIDEV_OPEN)) 491 return; 492 scd->sc_state &= ~UHIDEV_OPEN; 493 if (--sc->sc_refcnt) 494 return; 495 DPRINTF(("uhidev_close: close pipe\n")); 496 497 /* Disable interrupts. */ 498 if (sc->sc_intrpipe != NULL) { 499 usbd_abort_pipe(sc->sc_intrpipe); 500 usbd_close_pipe(sc->sc_intrpipe); 501 sc->sc_intrpipe = NULL; 502 } 503 504 if (sc->sc_ibuf != NULL) { 505 free(sc->sc_ibuf, M_USBDEV); 506 sc->sc_ibuf = NULL; 507 } 508 } 509 510 usbd_status 511 uhidev_set_report(struct uhidev *scd, int type, void *data, int len) 512 { 513 char *buf; 514 usbd_status retstat; 515 516 if (scd->sc_report_id == 0) 517 return usbd_set_report(scd->sc_parent->sc_iface, type, 518 scd->sc_report_id, data, len); 519 520 buf = malloc(len + 1, M_TEMP, M_WAITOK); 521 buf[0] = scd->sc_report_id; 522 memcpy(buf+1, data, len); 523 524 retstat = usbd_set_report(scd->sc_parent->sc_iface, type, 525 scd->sc_report_id, data, len + 1); 526 527 free(buf, M_TEMP); 528 529 return retstat; 530 } 531 532 void 533 uhidev_set_report_async(struct uhidev *scd, int type, void *data, int len) 534 { 535 /* XXX */ 536 char buf[100]; 537 if (scd->sc_report_id) { 538 buf[0] = scd->sc_report_id; 539 memcpy(buf+1, data, len); 540 len++; 541 data = buf; 542 } 543 544 usbd_set_report_async(scd->sc_parent->sc_iface, type, 545 scd->sc_report_id, data, len); 546 } 547 548 usbd_status 549 uhidev_get_report(struct uhidev *scd, int type, void *data, int len) 550 { 551 return usbd_get_report(scd->sc_parent->sc_iface, type, 552 scd->sc_report_id, data, len); 553 } 554