1 /* $OpenBSD: uhidev.c,v 1.12 2006/01/06 03:36:32 brad 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 (uaa->matchlvl) 100 return (uaa->matchlvl); 101 return (UMATCH_IFACECLASS_GENERIC); 102 } 103 104 USB_ATTACH(uhidev) 105 { 106 USB_ATTACH_START(uhidev, sc, uaa); 107 usbd_interface_handle iface = uaa->iface; 108 usb_interface_descriptor_t *id; 109 usb_endpoint_descriptor_t *ed; 110 struct uhidev_attach_arg uha; 111 struct uhidev *dev; 112 int size, nrepid, repid, repsz; 113 int repsizes[256]; 114 void *desc; 115 const void *descptr; 116 usbd_status err; 117 char *devinfop; 118 119 sc->sc_udev = uaa->device; 120 sc->sc_iface = iface; 121 id = usbd_get_interface_descriptor(iface); 122 123 devinfop = usbd_devinfo_alloc(uaa->device, 0); 124 USB_ATTACH_SETUP; 125 printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev), 126 devinfop, id->bInterfaceClass, id->bInterfaceSubClass); 127 usbd_devinfo_free(devinfop); 128 129 (void)usbd_set_idle(iface, 0, 0); 130 #if 0 131 132 qflags = usbd_get_quirks(sc->sc_udev)->uq_flags; 133 if ((qflags & UQ_NO_SET_PROTO) == 0 && 134 id->bInterfaceSubClass != UISUBCLASS_BOOT) 135 (void)usbd_set_protocol(iface, 1); 136 #endif 137 138 ed = usbd_interface2endpoint_descriptor(iface, 0); 139 if (ed == NULL) { 140 printf("%s: could not read endpoint descriptor\n", 141 USBDEVNAME(sc->sc_dev)); 142 sc->sc_dying = 1; 143 USB_ATTACH_ERROR_RETURN; 144 } 145 146 DPRINTFN(10,("uhidev_attach: bLength=%d bDescriptorType=%d " 147 "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d" 148 " bInterval=%d\n", 149 ed->bLength, ed->bDescriptorType, 150 ed->bEndpointAddress & UE_ADDR, 151 UE_GET_DIR(ed->bEndpointAddress)==UE_DIR_IN? "in" : "out", 152 ed->bmAttributes & UE_XFERTYPE, 153 UGETW(ed->wMaxPacketSize), ed->bInterval)); 154 155 if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_IN || 156 (ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) { 157 printf("%s: unexpected endpoint\n", USBDEVNAME(sc->sc_dev)); 158 sc->sc_dying = 1; 159 USB_ATTACH_ERROR_RETURN; 160 } 161 162 sc->sc_ep_addr = ed->bEndpointAddress; 163 164 /* XXX need to extend this */ 165 descptr = NULL; 166 if (uaa->vendor == USB_VENDOR_WACOM) { 167 static uByte reportbuf[] = {2, 2, 2}; 168 169 /* The report descriptor for the Wacom Graphire is broken. */ 170 switch (uaa->product) { 171 case USB_PRODUCT_WACOM_GRAPHIRE: 172 size = sizeof uhid_graphire_report_descr; 173 descptr = uhid_graphire_report_descr; 174 break; 175 case USB_PRODUCT_WACOM_GRAPHIRE3_4X5: 176 case USB_PRODUCT_WACOM_GRAPHIRE4_4X5: 177 usbd_set_report(uaa->iface, UHID_FEATURE_REPORT, 2, 178 &reportbuf, sizeof reportbuf); 179 size = sizeof uhid_graphire3_4x5_report_descr; 180 descptr = uhid_graphire3_4x5_report_descr; 181 break; 182 default: 183 /* Keep descriptor */ 184 break; 185 } 186 } 187 188 if (descptr) { 189 desc = malloc(size, M_USBDEV, M_NOWAIT); 190 if (desc == NULL) 191 err = USBD_NOMEM; 192 else { 193 err = USBD_NORMAL_COMPLETION; 194 memcpy(desc, descptr, size); 195 } 196 } else { 197 desc = NULL; 198 err = usbd_read_report_desc(uaa->iface, &desc, &size, M_USBDEV); 199 } 200 if (err) { 201 printf("%s: no report descriptor\n", USBDEVNAME(sc->sc_dev)); 202 sc->sc_dying = 1; 203 USB_ATTACH_ERROR_RETURN; 204 } 205 206 sc->sc_repdesc = desc; 207 sc->sc_repdesc_size = size; 208 209 uha.uaa = uaa; 210 nrepid = uhidev_maxrepid(desc, size); 211 if (nrepid < 0) 212 USB_ATTACH_SUCCESS_RETURN; 213 if (nrepid > 0) 214 printf("%s: %d report ids\n", USBDEVNAME(sc->sc_dev), nrepid); 215 nrepid++; 216 sc->sc_subdevs = malloc(nrepid * sizeof(device_ptr_t), 217 M_USBDEV, M_NOWAIT); 218 bzero(sc->sc_subdevs, nrepid * sizeof(device_ptr_t)); 219 if (sc->sc_subdevs == NULL) { 220 printf("%s: no memory\n", USBDEVNAME(sc->sc_dev)); 221 USB_ATTACH_ERROR_RETURN; 222 } 223 sc->sc_nrepid = nrepid; 224 sc->sc_isize = 0; 225 226 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, 227 USBDEV(sc->sc_dev)); 228 229 for (repid = 0; repid < nrepid; repid++) { 230 repsz = hid_report_size(desc, size, hid_input, repid); 231 DPRINTF(("uhidev_match: repid=%d, repsz=%d\n", repid, repsz)); 232 repsizes[repid] = repsz; 233 if (repsz > 0) { 234 if (repsz > sc->sc_isize) 235 sc->sc_isize = repsz; 236 } 237 } 238 sc->sc_isize += nrepid != 1; /* space for report ID */ 239 DPRINTF(("uhidev_attach: isize=%d\n", sc->sc_isize)); 240 241 uha.parent = sc; 242 for (repid = 0; repid < nrepid; repid++) { 243 DPRINTF(("uhidev_match: try repid=%d\n", repid)); 244 if (hid_report_size(desc, size, hid_input, repid) == 0 && 245 hid_report_size(desc, size, hid_output, repid) == 0 && 246 hid_report_size(desc, size, hid_feature, repid) == 0) { 247 ; /* already NULL in sc->sc_subdevs[repid] */ 248 } else { 249 uha.reportid = repid; 250 dev = (struct uhidev *)config_found_sm(self, &uha, 251 uhidevprint, uhidevsubmatch); 252 sc->sc_subdevs[repid] = dev; 253 if (dev != NULL) { 254 dev->sc_in_rep_size = repsizes[repid]; 255 #ifdef DIAGNOSTIC 256 DPRINTF(("uhidev_match: repid=%d dev=%p\n", 257 repid, dev)); 258 if (dev->sc_intr == NULL) { 259 printf("%s: sc_intr == NULL\n", 260 USBDEVNAME(sc->sc_dev)); 261 USB_ATTACH_ERROR_RETURN; 262 } 263 #endif 264 #if defined(__NetBSD__) && NRND > 0 265 rnd_attach_source(&dev->rnd_source, 266 USBDEVNAME(dev->sc_dev), 267 RND_TYPE_TTY, 0); 268 #endif 269 } 270 } 271 } 272 273 USB_ATTACH_SUCCESS_RETURN; 274 } 275 276 int 277 uhidev_maxrepid(void *buf, int len) 278 { 279 struct hid_data *d; 280 struct hid_item h; 281 int maxid; 282 283 maxid = -1; 284 h.report_ID = 0; 285 for (d = hid_start_parse(buf, len, hid_none); hid_get_item(d, &h); ) 286 if (h.report_ID > maxid) 287 maxid = h.report_ID; 288 hid_end_parse(d); 289 return (maxid); 290 } 291 292 int 293 uhidevprint(void *aux, const char *pnp) 294 { 295 struct uhidev_attach_arg *uha = aux; 296 297 if (pnp) 298 printf("uhid at %s", pnp); 299 if (uha->reportid != 0) 300 printf(" reportid %d", uha->reportid); 301 return (UNCONF); 302 } 303 304 #if defined(__NetBSD__) 305 Static int uhidevsubmatch(struct device *parent, struct cfdata *cf, void *aux) 306 #else 307 Static int uhidevsubmatch(struct device *parent, void *match, void *aux) 308 #endif 309 { 310 struct uhidev_attach_arg *uha = aux; 311 #if defined(__OpenBSD__) 312 struct cfdata *cf = match; 313 #endif 314 315 if (cf->uhidevcf_reportid != UHIDEV_UNK_REPORTID && 316 cf->uhidevcf_reportid != uha->reportid) 317 return (0); 318 if (cf->uhidevcf_reportid == uha->reportid) 319 uha->matchlvl = UMATCH_VENDOR_PRODUCT; 320 else 321 uha->matchlvl = 0; 322 return ((*cf->cf_attach->ca_match)(parent, cf, aux)); 323 } 324 325 int 326 uhidev_activate(device_ptr_t self, enum devact act) 327 { 328 struct uhidev_softc *sc = (struct uhidev_softc *)self; 329 int i, rv; 330 331 switch (act) { 332 case DVACT_ACTIVATE: 333 return (EOPNOTSUPP); 334 335 case DVACT_DEACTIVATE: 336 rv = 0; 337 for (i = 0; i < sc->sc_nrepid; i++) 338 if (sc->sc_subdevs[i] != NULL) 339 rv |= config_deactivate( 340 &sc->sc_subdevs[i]->sc_dev); 341 sc->sc_dying = 1; 342 break; 343 } 344 return (rv); 345 } 346 347 USB_DETACH(uhidev) 348 { 349 USB_DETACH_START(uhidev, sc); 350 int i, rv; 351 352 DPRINTF(("uhidev_detach: sc=%p flags=%d\n", sc, flags)); 353 354 sc->sc_dying = 1; 355 if (sc->sc_intrpipe != NULL) 356 usbd_abort_pipe(sc->sc_intrpipe); 357 358 if (sc->sc_repdesc != NULL) 359 free(sc->sc_repdesc, M_USBDEV); 360 361 rv = 0; 362 for (i = 0; i < sc->sc_nrepid; i++) { 363 if (sc->sc_subdevs[i] != NULL) { 364 #if defined(__NetBSD__) && NRND > 0 365 rnd_detach_source(&sc->sc_subdevs[i]->rnd_source); 366 #endif 367 rv |= config_detach(&sc->sc_subdevs[i]->sc_dev, flags); 368 sc->sc_subdevs[i] = NULL; 369 } 370 } 371 372 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, 373 USBDEV(sc->sc_dev)); 374 375 return (rv); 376 } 377 378 void 379 uhidev_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status) 380 { 381 struct uhidev_softc *sc = addr; 382 struct uhidev *scd; 383 u_char *p; 384 u_int rep; 385 u_int32_t cc; 386 387 usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL); 388 389 #ifdef UHIDEV_DEBUG 390 if (uhidevdebug > 5) { 391 u_int32_t i; 392 393 DPRINTF(("uhidev_intr: status=%d cc=%d\n", status, cc)); 394 DPRINTF(("uhidev_intr: data =")); 395 for (i = 0; i < cc; i++) 396 DPRINTF((" %02x", sc->sc_ibuf[i])); 397 DPRINTF(("\n")); 398 } 399 #endif 400 401 if (status == USBD_CANCELLED) 402 return; 403 404 if (status != USBD_NORMAL_COMPLETION) { 405 DPRINTF(("%s: interrupt status=%d\n", USBDEVNAME(sc->sc_dev), 406 status)); 407 usbd_clear_endpoint_stall_async(sc->sc_intrpipe); 408 return; 409 } 410 411 p = sc->sc_ibuf; 412 if (sc->sc_nrepid != 1) 413 rep = *p++, cc--; 414 else 415 rep = 0; 416 if (rep >= sc->sc_nrepid) { 417 printf("uhidev_intr: bad repid %d\n", rep); 418 return; 419 } 420 scd = sc->sc_subdevs[rep]; 421 DPRINTFN(5,("uhidev_intr: rep=%d, scd=%p state=0x%x\n", 422 rep, scd, scd ? scd->sc_state : 0)); 423 if (scd == NULL || !(scd->sc_state & UHIDEV_OPEN)) 424 return; 425 #ifdef DIAGNOSTIC 426 if (scd->sc_in_rep_size != cc) 427 printf("%s: bad input length %d != %d\n",USBDEVNAME(sc->sc_dev), 428 scd->sc_in_rep_size, cc); 429 #endif 430 #if defined(__NetBSD__) && NRND > 0 431 rnd_add_uint32(&scd->rnd_source, (uintptr_t)(sc->sc_ibuf)); 432 #endif 433 scd->sc_intr(scd, p, cc); 434 } 435 436 void 437 uhidev_get_report_desc(struct uhidev_softc *sc, void **desc, int *size) 438 { 439 *desc = sc->sc_repdesc; 440 *size = sc->sc_repdesc_size; 441 } 442 443 int 444 uhidev_open(struct uhidev *scd) 445 { 446 struct uhidev_softc *sc = scd->sc_parent; 447 usbd_status err; 448 449 DPRINTF(("uhidev_open: open pipe, state=%d refcnt=%d\n", 450 scd->sc_state, sc->sc_refcnt)); 451 452 if (scd->sc_state & UHIDEV_OPEN) 453 return (EBUSY); 454 scd->sc_state |= UHIDEV_OPEN; 455 if (sc->sc_refcnt++) 456 return (0); 457 458 if (sc->sc_isize == 0) 459 return (0); 460 461 sc->sc_ibuf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK); 462 463 /* Set up interrupt pipe. */ 464 DPRINTF(("uhidev_open: isize=%d, ep=0x%02x\n", sc->sc_isize, 465 sc->sc_ep_addr)); 466 err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr, 467 USBD_SHORT_XFER_OK, &sc->sc_intrpipe, sc, sc->sc_ibuf, 468 sc->sc_isize, uhidev_intr, USBD_DEFAULT_INTERVAL); 469 if (err) { 470 DPRINTF(("uhidopen: usbd_open_pipe_intr failed, " 471 "error=%d\n",err)); 472 free(sc->sc_ibuf, M_USBDEV); 473 scd->sc_state &= ~UHIDEV_OPEN; 474 sc->sc_refcnt = 0; 475 sc->sc_intrpipe = NULL; 476 return (EIO); 477 } 478 return (0); 479 } 480 481 void 482 uhidev_close(struct uhidev *scd) 483 { 484 struct uhidev_softc *sc = scd->sc_parent; 485 486 if (!(scd->sc_state & UHIDEV_OPEN)) 487 return; 488 scd->sc_state &= ~UHIDEV_OPEN; 489 if (--sc->sc_refcnt) 490 return; 491 DPRINTF(("uhidev_close: close pipe\n")); 492 493 /* Disable interrupts. */ 494 if (sc->sc_intrpipe != NULL) { 495 usbd_abort_pipe(sc->sc_intrpipe); 496 usbd_close_pipe(sc->sc_intrpipe); 497 sc->sc_intrpipe = NULL; 498 } 499 500 if (sc->sc_ibuf != NULL) { 501 free(sc->sc_ibuf, M_USBDEV); 502 sc->sc_ibuf = NULL; 503 } 504 } 505 506 usbd_status 507 uhidev_set_report(struct uhidev *scd, int type, void *data, int len) 508 { 509 char *buf; 510 usbd_status retstat; 511 512 if (scd->sc_report_id == 0) 513 return usbd_set_report(scd->sc_parent->sc_iface, type, 514 scd->sc_report_id, data, len); 515 516 buf = malloc(len + 1, M_TEMP, M_WAITOK); 517 buf[0] = scd->sc_report_id; 518 memcpy(buf+1, data, len); 519 520 retstat = usbd_set_report(scd->sc_parent->sc_iface, type, 521 scd->sc_report_id, data, len + 1); 522 523 free(buf, M_TEMP); 524 525 return retstat; 526 } 527 528 void 529 uhidev_set_report_async(struct uhidev *scd, int type, void *data, int len) 530 { 531 /* XXX */ 532 char buf[100]; 533 if (scd->sc_report_id) { 534 buf[0] = scd->sc_report_id; 535 memcpy(buf+1, data, len); 536 len++; 537 data = buf; 538 } 539 540 usbd_set_report_async(scd->sc_parent->sc_iface, type, 541 scd->sc_report_id, data, len); 542 } 543 544 usbd_status 545 uhidev_get_report(struct uhidev *scd, int type, void *data, int len) 546 { 547 return usbd_get_report(scd->sc_parent->sc_iface, type, 548 scd->sc_report_id, data, len); 549 } 550