1 /* $NetBSD: uhid.c,v 1.83 2009/12/06 21:40:31 dyoung Exp $ */ 2 3 /* 4 * Copyright (c) 1998, 2004, 2008 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: uhid.c,v 1.83 2009/12/06 21:40:31 dyoung Exp $"); 39 40 #include "opt_compat_netbsd.h" 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/kernel.h> 45 #include <sys/malloc.h> 46 #include <sys/signalvar.h> 47 #include <sys/device.h> 48 #include <sys/ioctl.h> 49 #include <sys/conf.h> 50 #include <sys/tty.h> 51 #include <sys/file.h> 52 #include <sys/select.h> 53 #include <sys/proc.h> 54 #include <sys/vnode.h> 55 #include <sys/poll.h> 56 #include <sys/intr.h> 57 58 #include <dev/usb/usb.h> 59 #include <dev/usb/usbhid.h> 60 61 #include <dev/usb/usbdevs.h> 62 #include <dev/usb/usbdi.h> 63 #include <dev/usb/usbdi_util.h> 64 #include <dev/usb/hid.h> 65 #include <dev/usb/usb_quirks.h> 66 67 #include <dev/usb/uhidev.h> 68 69 #ifdef UHID_DEBUG 70 #define DPRINTF(x) if (uhiddebug) logprintf x 71 #define DPRINTFN(n,x) if (uhiddebug>(n)) logprintf x 72 int uhiddebug = 0; 73 #else 74 #define DPRINTF(x) 75 #define DPRINTFN(n,x) 76 #endif 77 78 struct uhid_softc { 79 struct uhidev sc_hdev; 80 81 int sc_isize; 82 int sc_osize; 83 int sc_fsize; 84 85 u_char *sc_obuf; 86 87 struct clist sc_q; 88 struct selinfo sc_rsel; 89 usb_proc_ptr sc_async; /* process that wants SIGIO */ 90 void *sc_sih; 91 u_char sc_state; /* driver state */ 92 #define UHID_ASLP 0x01 /* waiting for device data */ 93 #define UHID_IMMED 0x02 /* return read data immediately */ 94 95 int sc_refcnt; 96 u_char sc_dying; 97 }; 98 99 #define UHIDUNIT(dev) (minor(dev)) 100 #define UHID_CHUNK 128 /* chunk size for read */ 101 #define UHID_BSIZE 1020 /* buffer size */ 102 103 dev_type_open(uhidopen); 104 dev_type_close(uhidclose); 105 dev_type_read(uhidread); 106 dev_type_write(uhidwrite); 107 dev_type_ioctl(uhidioctl); 108 dev_type_poll(uhidpoll); 109 dev_type_kqfilter(uhidkqfilter); 110 111 const struct cdevsw uhid_cdevsw = { 112 uhidopen, uhidclose, uhidread, uhidwrite, uhidioctl, 113 nostop, notty, uhidpoll, nommap, uhidkqfilter, D_OTHER, 114 }; 115 116 Static void uhid_intr(struct uhidev *, void *, u_int len); 117 Static void uhid_softintr(void *); 118 119 Static int uhid_do_read(struct uhid_softc *, struct uio *uio, int); 120 Static int uhid_do_write(struct uhid_softc *, struct uio *uio, int); 121 Static int uhid_do_ioctl(struct uhid_softc*, u_long, void *, int, struct lwp *); 122 123 USB_DECLARE_DRIVER(uhid); 124 125 int 126 uhid_match(device_t parent, cfdata_t match, void *aux) 127 { 128 #ifdef UHID_DEBUG 129 struct uhidev_attach_arg *uha = aux; 130 #endif 131 132 DPRINTF(("uhid_match: report=%d\n", uha->reportid)); 133 134 if (match->cf_flags & 1) 135 return (UMATCH_HIGHEST); 136 else 137 return (UMATCH_IFACECLASS_GENERIC); 138 } 139 140 void 141 uhid_attach(device_t parent, device_t self, void *aux) 142 { 143 struct uhid_softc *sc = device_private(self); 144 struct uhidev_attach_arg *uha = aux; 145 int size, repid; 146 void *desc; 147 148 sc->sc_hdev.sc_dev = self; 149 selinit(&sc->sc_rsel); 150 sc->sc_hdev.sc_intr = uhid_intr; 151 sc->sc_hdev.sc_parent = uha->parent; 152 sc->sc_hdev.sc_report_id = uha->reportid; 153 sc->sc_sih = softint_establish(SOFTINT_MPSAFE | SOFTINT_CLOCK, 154 uhid_softintr, sc); 155 156 uhidev_get_report_desc(uha->parent, &desc, &size); 157 repid = uha->reportid; 158 sc->sc_isize = hid_report_size(desc, size, hid_input, repid); 159 sc->sc_osize = hid_report_size(desc, size, hid_output, repid); 160 sc->sc_fsize = hid_report_size(desc, size, hid_feature, repid); 161 162 aprint_naive("\n"); 163 aprint_normal(": input=%d, output=%d, feature=%d\n", 164 sc->sc_isize, sc->sc_osize, sc->sc_fsize); 165 166 if (!pmf_device_register(self, NULL, NULL)) 167 aprint_error_dev(self, "couldn't establish power handler\n"); 168 169 USB_ATTACH_SUCCESS_RETURN; 170 } 171 172 int 173 uhid_activate(device_ptr_t self, enum devact act) 174 { 175 struct uhid_softc *sc = device_private(self); 176 177 switch (act) { 178 case DVACT_DEACTIVATE: 179 sc->sc_dying = 1; 180 return 0; 181 default: 182 return EOPNOTSUPP; 183 } 184 } 185 186 int 187 uhid_detach(device_t self, int flags) 188 { 189 struct uhid_softc *sc = device_private(self); 190 int s; 191 int maj, mn; 192 193 DPRINTF(("uhid_detach: sc=%p flags=%d\n", sc, flags)); 194 195 sc->sc_dying = 1; 196 197 if (sc->sc_hdev.sc_state & UHIDEV_OPEN) { 198 s = splusb(); 199 if (--sc->sc_refcnt >= 0) { 200 /* Wake everyone */ 201 wakeup(&sc->sc_q); 202 /* Wait for processes to go away. */ 203 usb_detach_wait(USBDEV(sc->sc_hdev.sc_dev)); 204 } 205 splx(s); 206 } 207 208 /* locate the major number */ 209 #if defined(__NetBSD__) 210 maj = cdevsw_lookup_major(&uhid_cdevsw); 211 #elif defined(__OpenBSD__) 212 for (maj = 0; maj < nchrdev; maj++) 213 if (cdevsw[maj].d_open == uhidopen) 214 break; 215 #endif 216 217 /* Nuke the vnodes for any open instances (calls close). */ 218 mn = device_unit(self); 219 vdevgone(maj, mn, mn, VCHR); 220 221 #if 0 222 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, 223 sc->sc_hdev.sc_parent->sc_udev, 224 USBDEV(sc->sc_hdev.sc_dev)); 225 #endif 226 seldestroy(&sc->sc_rsel); 227 softint_disestablish(sc->sc_sih); 228 229 return (0); 230 } 231 232 void 233 uhid_intr(struct uhidev *addr, void *data, u_int len) 234 { 235 struct uhid_softc *sc = (struct uhid_softc *)addr; 236 237 #ifdef UHID_DEBUG 238 if (uhiddebug > 5) { 239 u_int32_t i; 240 241 DPRINTF(("uhid_intr: data =")); 242 for (i = 0; i < len; i++) 243 DPRINTF((" %02x", ((u_char *)data)[i])); 244 DPRINTF(("\n")); 245 } 246 #endif 247 248 (void)b_to_q(data, len, &sc->sc_q); 249 250 if (sc->sc_state & UHID_ASLP) { 251 sc->sc_state &= ~UHID_ASLP; 252 DPRINTFN(5, ("uhid_intr: waking %p\n", &sc->sc_q)); 253 wakeup(&sc->sc_q); 254 } 255 selnotify(&sc->sc_rsel, 0, 0); 256 if (sc->sc_async != NULL) { 257 DPRINTFN(3, ("uhid_intr: sending SIGIO %p\n", sc->sc_async)); 258 softint_schedule(sc->sc_sih); 259 } 260 } 261 262 void 263 uhid_softintr(void *cookie) 264 { 265 struct uhid_softc *sc; 266 267 sc = cookie; 268 269 mutex_enter(proc_lock); 270 if (sc->sc_async != NULL) 271 psignal(sc->sc_async, SIGIO); 272 mutex_exit(proc_lock); 273 } 274 275 int 276 uhidopen(dev_t dev, int flag, int mode, 277 struct lwp *l) 278 { 279 struct uhid_softc *sc; 280 int error; 281 282 USB_GET_SC_OPEN(uhid, UHIDUNIT(dev), sc); 283 284 DPRINTF(("uhidopen: sc=%p\n", sc)); 285 286 if (sc->sc_dying) 287 return (ENXIO); 288 289 error = uhidev_open(&sc->sc_hdev); 290 if (error) 291 return (error); 292 293 if (clalloc(&sc->sc_q, UHID_BSIZE, 0) == -1) { 294 uhidev_close(&sc->sc_hdev); 295 return (ENOMEM); 296 } 297 sc->sc_obuf = malloc(sc->sc_osize, M_USBDEV, M_WAITOK); 298 sc->sc_state &= ~UHID_IMMED; 299 mutex_enter(proc_lock); 300 sc->sc_async = NULL; 301 mutex_exit(proc_lock); 302 303 return (0); 304 } 305 306 int 307 uhidclose(dev_t dev, int flag, int mode, 308 struct lwp *l) 309 { 310 struct uhid_softc *sc; 311 312 USB_GET_SC(uhid, UHIDUNIT(dev), sc); 313 314 DPRINTF(("uhidclose: sc=%p\n", sc)); 315 316 clfree(&sc->sc_q); 317 free(sc->sc_obuf, M_USBDEV); 318 mutex_enter(proc_lock); 319 sc->sc_async = NULL; 320 mutex_exit(proc_lock); 321 uhidev_close(&sc->sc_hdev); 322 323 return (0); 324 } 325 326 int 327 uhid_do_read(struct uhid_softc *sc, struct uio *uio, int flag) 328 { 329 int s; 330 int error = 0; 331 int extra; 332 size_t length; 333 u_char buffer[UHID_CHUNK]; 334 usbd_status err; 335 336 DPRINTFN(1, ("uhidread\n")); 337 if (sc->sc_state & UHID_IMMED) { 338 DPRINTFN(1, ("uhidread immed\n")); 339 extra = sc->sc_hdev.sc_report_id != 0; 340 err = uhidev_get_report(&sc->sc_hdev, UHID_INPUT_REPORT, 341 buffer, sc->sc_isize + extra); 342 if (err) 343 return (EIO); 344 return (uiomove(buffer+extra, sc->sc_isize, uio)); 345 } 346 347 s = splusb(); 348 while (sc->sc_q.c_cc == 0) { 349 if (flag & IO_NDELAY) { 350 splx(s); 351 return (EWOULDBLOCK); 352 } 353 sc->sc_state |= UHID_ASLP; 354 DPRINTFN(5, ("uhidread: sleep on %p\n", &sc->sc_q)); 355 error = tsleep(&sc->sc_q, PZERO | PCATCH, "uhidrea", 0); 356 DPRINTFN(5, ("uhidread: woke, error=%d\n", error)); 357 if (sc->sc_dying) 358 error = EIO; 359 if (error) { 360 sc->sc_state &= ~UHID_ASLP; 361 break; 362 } 363 } 364 splx(s); 365 366 /* Transfer as many chunks as possible. */ 367 while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0 && !error) { 368 length = min(sc->sc_q.c_cc, uio->uio_resid); 369 if (length > sizeof(buffer)) 370 length = sizeof(buffer); 371 372 /* Remove a small chunk from the input queue. */ 373 (void) q_to_b(&sc->sc_q, buffer, length); 374 DPRINTFN(5, ("uhidread: got %lu chars\n", (u_long)length)); 375 376 /* Copy the data to the user process. */ 377 if ((error = uiomove(buffer, length, uio)) != 0) 378 break; 379 } 380 381 return (error); 382 } 383 384 int 385 uhidread(dev_t dev, struct uio *uio, int flag) 386 { 387 struct uhid_softc *sc; 388 int error; 389 390 USB_GET_SC(uhid, UHIDUNIT(dev), sc); 391 392 sc->sc_refcnt++; 393 error = uhid_do_read(sc, uio, flag); 394 if (--sc->sc_refcnt < 0) 395 usb_detach_wakeup(USBDEV(sc->sc_hdev.sc_dev)); 396 return (error); 397 } 398 399 int 400 uhid_do_write(struct uhid_softc *sc, struct uio *uio, int flag) 401 { 402 int error; 403 int size; 404 usbd_status err; 405 406 DPRINTFN(1, ("uhidwrite\n")); 407 408 if (sc->sc_dying) 409 return (EIO); 410 411 size = sc->sc_osize; 412 error = 0; 413 if (uio->uio_resid != size) 414 return (EINVAL); 415 error = uiomove(sc->sc_obuf, size, uio); 416 if (!error) { 417 err = uhidev_set_report(&sc->sc_hdev, UHID_OUTPUT_REPORT, 418 sc->sc_obuf, size); 419 if (err) 420 error = EIO; 421 } 422 423 return (error); 424 } 425 426 int 427 uhidwrite(dev_t dev, struct uio *uio, int flag) 428 { 429 struct uhid_softc *sc; 430 int error; 431 432 USB_GET_SC(uhid, UHIDUNIT(dev), sc); 433 434 sc->sc_refcnt++; 435 error = uhid_do_write(sc, uio, flag); 436 if (--sc->sc_refcnt < 0) 437 usb_detach_wakeup(USBDEV(sc->sc_hdev.sc_dev)); 438 return (error); 439 } 440 441 int 442 uhid_do_ioctl(struct uhid_softc *sc, u_long cmd, void *addr, 443 int flag, struct lwp *l) 444 { 445 struct usb_ctl_report_desc *rd; 446 struct usb_ctl_report *re; 447 u_char buffer[UHID_CHUNK]; 448 int size, extra; 449 usbd_status err; 450 void *desc; 451 452 DPRINTFN(2, ("uhidioctl: cmd=%lx\n", cmd)); 453 454 if (sc->sc_dying) 455 return (EIO); 456 457 switch (cmd) { 458 case FIONBIO: 459 /* All handled in the upper FS layer. */ 460 break; 461 462 case FIOASYNC: 463 mutex_enter(proc_lock); 464 if (*(int *)addr) { 465 if (sc->sc_async != NULL) 466 return (EBUSY); 467 sc->sc_async = l->l_proc; 468 DPRINTF(("uhid_do_ioctl: FIOASYNC %p\n", l->l_proc)); 469 } else 470 sc->sc_async = NULL; 471 mutex_exit(proc_lock); 472 break; 473 474 /* XXX this is not the most general solution. */ 475 case TIOCSPGRP: 476 mutex_enter(proc_lock); 477 if (sc->sc_async == NULL) { 478 mutex_exit(proc_lock); 479 return (EINVAL); 480 } 481 if (*(int *)addr != sc->sc_async->p_pgid) { 482 mutex_exit(proc_lock); 483 return (EPERM); 484 } 485 mutex_exit(proc_lock); 486 break; 487 488 case FIOSETOWN: 489 mutex_enter(proc_lock); 490 if (sc->sc_async == NULL) { 491 mutex_exit(proc_lock); 492 return (EINVAL); 493 } 494 if (-*(int *)addr != sc->sc_async->p_pgid 495 && *(int *)addr != sc->sc_async->p_pid) { 496 mutex_exit(proc_lock); 497 return (EPERM); 498 } 499 mutex_exit(proc_lock); 500 break; 501 502 case USB_GET_REPORT_DESC: 503 uhidev_get_report_desc(sc->sc_hdev.sc_parent, &desc, &size); 504 rd = (struct usb_ctl_report_desc *)addr; 505 size = min(size, sizeof rd->ucrd_data); 506 rd->ucrd_size = size; 507 memcpy(rd->ucrd_data, desc, size); 508 break; 509 510 case USB_SET_IMMED: 511 if (*(int *)addr) { 512 extra = sc->sc_hdev.sc_report_id != 0; 513 err = uhidev_get_report(&sc->sc_hdev, UHID_INPUT_REPORT, 514 buffer, sc->sc_isize + extra); 515 if (err) 516 return (EOPNOTSUPP); 517 518 sc->sc_state |= UHID_IMMED; 519 } else 520 sc->sc_state &= ~UHID_IMMED; 521 break; 522 523 case USB_GET_REPORT: 524 re = (struct usb_ctl_report *)addr; 525 switch (re->ucr_report) { 526 case UHID_INPUT_REPORT: 527 size = sc->sc_isize; 528 break; 529 case UHID_OUTPUT_REPORT: 530 size = sc->sc_osize; 531 break; 532 case UHID_FEATURE_REPORT: 533 size = sc->sc_fsize; 534 break; 535 default: 536 return (EINVAL); 537 } 538 extra = sc->sc_hdev.sc_report_id != 0; 539 err = uhidev_get_report(&sc->sc_hdev, re->ucr_report, 540 re->ucr_data, size + extra); 541 if (extra) 542 memcpy(re->ucr_data, re->ucr_data+1, size); 543 if (err) 544 return (EIO); 545 break; 546 547 case USB_SET_REPORT: 548 re = (struct usb_ctl_report *)addr; 549 switch (re->ucr_report) { 550 case UHID_INPUT_REPORT: 551 size = sc->sc_isize; 552 break; 553 case UHID_OUTPUT_REPORT: 554 size = sc->sc_osize; 555 break; 556 case UHID_FEATURE_REPORT: 557 size = sc->sc_fsize; 558 break; 559 default: 560 return (EINVAL); 561 } 562 err = uhidev_set_report(&sc->sc_hdev, re->ucr_report, 563 re->ucr_data, size); 564 if (err) 565 return (EIO); 566 break; 567 568 case USB_GET_REPORT_ID: 569 *(int *)addr = sc->sc_hdev.sc_report_id; 570 break; 571 572 case USB_GET_DEVICEINFO: 573 usbd_fill_deviceinfo(sc->sc_hdev.sc_parent->sc_udev, 574 (struct usb_device_info *)addr, 0); 575 break; 576 #ifdef COMPAT_30 577 case USB_GET_DEVICEINFO_OLD: 578 usbd_fill_deviceinfo_old(sc->sc_hdev.sc_parent->sc_udev, 579 (struct usb_device_info_old *)addr, 0); 580 581 break; 582 #endif 583 case USB_GET_STRING_DESC: 584 { 585 struct usb_string_desc *si = (struct usb_string_desc *)addr; 586 err = usbd_get_string_desc(sc->sc_hdev.sc_parent->sc_udev, 587 si->usd_string_index, 588 si->usd_language_id, &si->usd_desc, &size); 589 if (err) 590 return (EINVAL); 591 break; 592 } 593 594 default: 595 return (EINVAL); 596 } 597 return (0); 598 } 599 600 int 601 uhidioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l) 602 { 603 struct uhid_softc *sc; 604 int error; 605 606 USB_GET_SC(uhid, UHIDUNIT(dev), sc); 607 608 sc->sc_refcnt++; 609 error = uhid_do_ioctl(sc, cmd, addr, flag, l); 610 if (--sc->sc_refcnt < 0) 611 usb_detach_wakeup(USBDEV(sc->sc_hdev.sc_dev)); 612 return (error); 613 } 614 615 int 616 uhidpoll(dev_t dev, int events, struct lwp *l) 617 { 618 struct uhid_softc *sc; 619 int revents = 0; 620 int s; 621 622 USB_GET_SC(uhid, UHIDUNIT(dev), sc); 623 624 if (sc->sc_dying) 625 return (POLLHUP); 626 627 s = splusb(); 628 if (events & (POLLOUT | POLLWRNORM)) 629 revents |= events & (POLLOUT | POLLWRNORM); 630 if (events & (POLLIN | POLLRDNORM)) { 631 if (sc->sc_q.c_cc > 0) 632 revents |= events & (POLLIN | POLLRDNORM); 633 else 634 selrecord(l, &sc->sc_rsel); 635 } 636 637 splx(s); 638 return (revents); 639 } 640 641 static void 642 filt_uhidrdetach(struct knote *kn) 643 { 644 struct uhid_softc *sc = kn->kn_hook; 645 int s; 646 647 s = splusb(); 648 SLIST_REMOVE(&sc->sc_rsel.sel_klist, kn, knote, kn_selnext); 649 splx(s); 650 } 651 652 static int 653 filt_uhidread(struct knote *kn, long hint) 654 { 655 struct uhid_softc *sc = kn->kn_hook; 656 657 kn->kn_data = sc->sc_q.c_cc; 658 return (kn->kn_data > 0); 659 } 660 661 static const struct filterops uhidread_filtops = 662 { 1, NULL, filt_uhidrdetach, filt_uhidread }; 663 664 static const struct filterops uhid_seltrue_filtops = 665 { 1, NULL, filt_uhidrdetach, filt_seltrue }; 666 667 int 668 uhidkqfilter(dev_t dev, struct knote *kn) 669 { 670 struct uhid_softc *sc; 671 struct klist *klist; 672 int s; 673 674 USB_GET_SC(uhid, UHIDUNIT(dev), sc); 675 676 if (sc->sc_dying) 677 return (ENXIO); 678 679 switch (kn->kn_filter) { 680 case EVFILT_READ: 681 klist = &sc->sc_rsel.sel_klist; 682 kn->kn_fop = &uhidread_filtops; 683 break; 684 685 case EVFILT_WRITE: 686 klist = &sc->sc_rsel.sel_klist; 687 kn->kn_fop = &uhid_seltrue_filtops; 688 break; 689 690 default: 691 return (EINVAL); 692 } 693 694 kn->kn_hook = sc; 695 696 s = splusb(); 697 SLIST_INSERT_HEAD(klist, kn, kn_selnext); 698 splx(s); 699 700 return (0); 701 } 702