1 /* $NetBSD: uhid.c,v 1.82 2008/05/24 16:40:58 cube 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.82 2008/05/24 16:40:58 cube 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_ACTIVATE: 179 return (EOPNOTSUPP); 180 181 case DVACT_DEACTIVATE: 182 sc->sc_dying = 1; 183 break; 184 } 185 return (0); 186 } 187 188 int 189 uhid_detach(device_t self, int flags) 190 { 191 struct uhid_softc *sc = device_private(self); 192 int s; 193 int maj, mn; 194 195 DPRINTF(("uhid_detach: sc=%p flags=%d\n", sc, flags)); 196 197 sc->sc_dying = 1; 198 199 if (sc->sc_hdev.sc_state & UHIDEV_OPEN) { 200 s = splusb(); 201 if (--sc->sc_refcnt >= 0) { 202 /* Wake everyone */ 203 wakeup(&sc->sc_q); 204 /* Wait for processes to go away. */ 205 usb_detach_wait(USBDEV(sc->sc_hdev.sc_dev)); 206 } 207 splx(s); 208 } 209 210 /* locate the major number */ 211 #if defined(__NetBSD__) 212 maj = cdevsw_lookup_major(&uhid_cdevsw); 213 #elif defined(__OpenBSD__) 214 for (maj = 0; maj < nchrdev; maj++) 215 if (cdevsw[maj].d_open == uhidopen) 216 break; 217 #endif 218 219 /* Nuke the vnodes for any open instances (calls close). */ 220 mn = device_unit(self); 221 vdevgone(maj, mn, mn, VCHR); 222 223 #if 0 224 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, 225 sc->sc_hdev.sc_parent->sc_udev, 226 USBDEV(sc->sc_hdev.sc_dev)); 227 #endif 228 seldestroy(&sc->sc_rsel); 229 softint_disestablish(sc->sc_sih); 230 231 return (0); 232 } 233 234 void 235 uhid_intr(struct uhidev *addr, void *data, u_int len) 236 { 237 struct uhid_softc *sc = (struct uhid_softc *)addr; 238 239 #ifdef UHID_DEBUG 240 if (uhiddebug > 5) { 241 u_int32_t i; 242 243 DPRINTF(("uhid_intr: data =")); 244 for (i = 0; i < len; i++) 245 DPRINTF((" %02x", ((u_char *)data)[i])); 246 DPRINTF(("\n")); 247 } 248 #endif 249 250 (void)b_to_q(data, len, &sc->sc_q); 251 252 if (sc->sc_state & UHID_ASLP) { 253 sc->sc_state &= ~UHID_ASLP; 254 DPRINTFN(5, ("uhid_intr: waking %p\n", &sc->sc_q)); 255 wakeup(&sc->sc_q); 256 } 257 selnotify(&sc->sc_rsel, 0, 0); 258 if (sc->sc_async != NULL) { 259 DPRINTFN(3, ("uhid_intr: sending SIGIO %p\n", sc->sc_async)); 260 softint_schedule(sc->sc_sih); 261 } 262 } 263 264 void 265 uhid_softintr(void *cookie) 266 { 267 struct uhid_softc *sc; 268 269 sc = cookie; 270 271 mutex_enter(proc_lock); 272 if (sc->sc_async != NULL) 273 psignal(sc->sc_async, SIGIO); 274 mutex_exit(proc_lock); 275 } 276 277 int 278 uhidopen(dev_t dev, int flag, int mode, 279 struct lwp *l) 280 { 281 struct uhid_softc *sc; 282 int error; 283 284 USB_GET_SC_OPEN(uhid, UHIDUNIT(dev), sc); 285 286 DPRINTF(("uhidopen: sc=%p\n", sc)); 287 288 if (sc->sc_dying) 289 return (ENXIO); 290 291 error = uhidev_open(&sc->sc_hdev); 292 if (error) 293 return (error); 294 295 if (clalloc(&sc->sc_q, UHID_BSIZE, 0) == -1) { 296 uhidev_close(&sc->sc_hdev); 297 return (ENOMEM); 298 } 299 sc->sc_obuf = malloc(sc->sc_osize, M_USBDEV, M_WAITOK); 300 sc->sc_state &= ~UHID_IMMED; 301 mutex_enter(proc_lock); 302 sc->sc_async = NULL; 303 mutex_exit(proc_lock); 304 305 return (0); 306 } 307 308 int 309 uhidclose(dev_t dev, int flag, int mode, 310 struct lwp *l) 311 { 312 struct uhid_softc *sc; 313 314 USB_GET_SC(uhid, UHIDUNIT(dev), sc); 315 316 DPRINTF(("uhidclose: sc=%p\n", sc)); 317 318 clfree(&sc->sc_q); 319 free(sc->sc_obuf, M_USBDEV); 320 mutex_enter(proc_lock); 321 sc->sc_async = NULL; 322 mutex_exit(proc_lock); 323 uhidev_close(&sc->sc_hdev); 324 325 return (0); 326 } 327 328 int 329 uhid_do_read(struct uhid_softc *sc, struct uio *uio, int flag) 330 { 331 int s; 332 int error = 0; 333 int extra; 334 size_t length; 335 u_char buffer[UHID_CHUNK]; 336 usbd_status err; 337 338 DPRINTFN(1, ("uhidread\n")); 339 if (sc->sc_state & UHID_IMMED) { 340 DPRINTFN(1, ("uhidread immed\n")); 341 extra = sc->sc_hdev.sc_report_id != 0; 342 err = uhidev_get_report(&sc->sc_hdev, UHID_INPUT_REPORT, 343 buffer, sc->sc_isize + extra); 344 if (err) 345 return (EIO); 346 return (uiomove(buffer+extra, sc->sc_isize, uio)); 347 } 348 349 s = splusb(); 350 while (sc->sc_q.c_cc == 0) { 351 if (flag & IO_NDELAY) { 352 splx(s); 353 return (EWOULDBLOCK); 354 } 355 sc->sc_state |= UHID_ASLP; 356 DPRINTFN(5, ("uhidread: sleep on %p\n", &sc->sc_q)); 357 error = tsleep(&sc->sc_q, PZERO | PCATCH, "uhidrea", 0); 358 DPRINTFN(5, ("uhidread: woke, error=%d\n", error)); 359 if (sc->sc_dying) 360 error = EIO; 361 if (error) { 362 sc->sc_state &= ~UHID_ASLP; 363 break; 364 } 365 } 366 splx(s); 367 368 /* Transfer as many chunks as possible. */ 369 while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0 && !error) { 370 length = min(sc->sc_q.c_cc, uio->uio_resid); 371 if (length > sizeof(buffer)) 372 length = sizeof(buffer); 373 374 /* Remove a small chunk from the input queue. */ 375 (void) q_to_b(&sc->sc_q, buffer, length); 376 DPRINTFN(5, ("uhidread: got %lu chars\n", (u_long)length)); 377 378 /* Copy the data to the user process. */ 379 if ((error = uiomove(buffer, length, uio)) != 0) 380 break; 381 } 382 383 return (error); 384 } 385 386 int 387 uhidread(dev_t dev, struct uio *uio, int flag) 388 { 389 struct uhid_softc *sc; 390 int error; 391 392 USB_GET_SC(uhid, UHIDUNIT(dev), sc); 393 394 sc->sc_refcnt++; 395 error = uhid_do_read(sc, uio, flag); 396 if (--sc->sc_refcnt < 0) 397 usb_detach_wakeup(USBDEV(sc->sc_hdev.sc_dev)); 398 return (error); 399 } 400 401 int 402 uhid_do_write(struct uhid_softc *sc, struct uio *uio, int flag) 403 { 404 int error; 405 int size; 406 usbd_status err; 407 408 DPRINTFN(1, ("uhidwrite\n")); 409 410 if (sc->sc_dying) 411 return (EIO); 412 413 size = sc->sc_osize; 414 error = 0; 415 if (uio->uio_resid != size) 416 return (EINVAL); 417 error = uiomove(sc->sc_obuf, size, uio); 418 if (!error) { 419 err = uhidev_set_report(&sc->sc_hdev, UHID_OUTPUT_REPORT, 420 sc->sc_obuf, size); 421 if (err) 422 error = EIO; 423 } 424 425 return (error); 426 } 427 428 int 429 uhidwrite(dev_t dev, struct uio *uio, int flag) 430 { 431 struct uhid_softc *sc; 432 int error; 433 434 USB_GET_SC(uhid, UHIDUNIT(dev), sc); 435 436 sc->sc_refcnt++; 437 error = uhid_do_write(sc, uio, flag); 438 if (--sc->sc_refcnt < 0) 439 usb_detach_wakeup(USBDEV(sc->sc_hdev.sc_dev)); 440 return (error); 441 } 442 443 int 444 uhid_do_ioctl(struct uhid_softc *sc, u_long cmd, void *addr, 445 int flag, struct lwp *l) 446 { 447 struct usb_ctl_report_desc *rd; 448 struct usb_ctl_report *re; 449 u_char buffer[UHID_CHUNK]; 450 int size, extra; 451 usbd_status err; 452 void *desc; 453 454 DPRINTFN(2, ("uhidioctl: cmd=%lx\n", cmd)); 455 456 if (sc->sc_dying) 457 return (EIO); 458 459 switch (cmd) { 460 case FIONBIO: 461 /* All handled in the upper FS layer. */ 462 break; 463 464 case FIOASYNC: 465 mutex_enter(proc_lock); 466 if (*(int *)addr) { 467 if (sc->sc_async != NULL) 468 return (EBUSY); 469 sc->sc_async = l->l_proc; 470 DPRINTF(("uhid_do_ioctl: FIOASYNC %p\n", l->l_proc)); 471 } else 472 sc->sc_async = NULL; 473 mutex_exit(proc_lock); 474 break; 475 476 /* XXX this is not the most general solution. */ 477 case TIOCSPGRP: 478 mutex_enter(proc_lock); 479 if (sc->sc_async == NULL) { 480 mutex_exit(proc_lock); 481 return (EINVAL); 482 } 483 if (*(int *)addr != sc->sc_async->p_pgid) { 484 mutex_exit(proc_lock); 485 return (EPERM); 486 } 487 mutex_exit(proc_lock); 488 break; 489 490 case FIOSETOWN: 491 mutex_enter(proc_lock); 492 if (sc->sc_async == NULL) { 493 mutex_exit(proc_lock); 494 return (EINVAL); 495 } 496 if (-*(int *)addr != sc->sc_async->p_pgid 497 && *(int *)addr != sc->sc_async->p_pid) { 498 mutex_exit(proc_lock); 499 return (EPERM); 500 } 501 mutex_exit(proc_lock); 502 break; 503 504 case USB_GET_REPORT_DESC: 505 uhidev_get_report_desc(sc->sc_hdev.sc_parent, &desc, &size); 506 rd = (struct usb_ctl_report_desc *)addr; 507 size = min(size, sizeof rd->ucrd_data); 508 rd->ucrd_size = size; 509 memcpy(rd->ucrd_data, desc, size); 510 break; 511 512 case USB_SET_IMMED: 513 if (*(int *)addr) { 514 extra = sc->sc_hdev.sc_report_id != 0; 515 err = uhidev_get_report(&sc->sc_hdev, UHID_INPUT_REPORT, 516 buffer, sc->sc_isize + extra); 517 if (err) 518 return (EOPNOTSUPP); 519 520 sc->sc_state |= UHID_IMMED; 521 } else 522 sc->sc_state &= ~UHID_IMMED; 523 break; 524 525 case USB_GET_REPORT: 526 re = (struct usb_ctl_report *)addr; 527 switch (re->ucr_report) { 528 case UHID_INPUT_REPORT: 529 size = sc->sc_isize; 530 break; 531 case UHID_OUTPUT_REPORT: 532 size = sc->sc_osize; 533 break; 534 case UHID_FEATURE_REPORT: 535 size = sc->sc_fsize; 536 break; 537 default: 538 return (EINVAL); 539 } 540 extra = sc->sc_hdev.sc_report_id != 0; 541 err = uhidev_get_report(&sc->sc_hdev, re->ucr_report, 542 re->ucr_data, size + extra); 543 if (extra) 544 memcpy(re->ucr_data, re->ucr_data+1, size); 545 if (err) 546 return (EIO); 547 break; 548 549 case USB_SET_REPORT: 550 re = (struct usb_ctl_report *)addr; 551 switch (re->ucr_report) { 552 case UHID_INPUT_REPORT: 553 size = sc->sc_isize; 554 break; 555 case UHID_OUTPUT_REPORT: 556 size = sc->sc_osize; 557 break; 558 case UHID_FEATURE_REPORT: 559 size = sc->sc_fsize; 560 break; 561 default: 562 return (EINVAL); 563 } 564 err = uhidev_set_report(&sc->sc_hdev, re->ucr_report, 565 re->ucr_data, size); 566 if (err) 567 return (EIO); 568 break; 569 570 case USB_GET_REPORT_ID: 571 *(int *)addr = sc->sc_hdev.sc_report_id; 572 break; 573 574 case USB_GET_DEVICEINFO: 575 usbd_fill_deviceinfo(sc->sc_hdev.sc_parent->sc_udev, 576 (struct usb_device_info *)addr, 0); 577 break; 578 #ifdef COMPAT_30 579 case USB_GET_DEVICEINFO_OLD: 580 usbd_fill_deviceinfo_old(sc->sc_hdev.sc_parent->sc_udev, 581 (struct usb_device_info_old *)addr, 0); 582 583 break; 584 #endif 585 case USB_GET_STRING_DESC: 586 { 587 struct usb_string_desc *si = (struct usb_string_desc *)addr; 588 err = usbd_get_string_desc(sc->sc_hdev.sc_parent->sc_udev, 589 si->usd_string_index, 590 si->usd_language_id, &si->usd_desc, &size); 591 if (err) 592 return (EINVAL); 593 break; 594 } 595 596 default: 597 return (EINVAL); 598 } 599 return (0); 600 } 601 602 int 603 uhidioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l) 604 { 605 struct uhid_softc *sc; 606 int error; 607 608 USB_GET_SC(uhid, UHIDUNIT(dev), sc); 609 610 sc->sc_refcnt++; 611 error = uhid_do_ioctl(sc, cmd, addr, flag, l); 612 if (--sc->sc_refcnt < 0) 613 usb_detach_wakeup(USBDEV(sc->sc_hdev.sc_dev)); 614 return (error); 615 } 616 617 int 618 uhidpoll(dev_t dev, int events, struct lwp *l) 619 { 620 struct uhid_softc *sc; 621 int revents = 0; 622 int s; 623 624 USB_GET_SC(uhid, UHIDUNIT(dev), sc); 625 626 if (sc->sc_dying) 627 return (POLLHUP); 628 629 s = splusb(); 630 if (events & (POLLOUT | POLLWRNORM)) 631 revents |= events & (POLLOUT | POLLWRNORM); 632 if (events & (POLLIN | POLLRDNORM)) { 633 if (sc->sc_q.c_cc > 0) 634 revents |= events & (POLLIN | POLLRDNORM); 635 else 636 selrecord(l, &sc->sc_rsel); 637 } 638 639 splx(s); 640 return (revents); 641 } 642 643 static void 644 filt_uhidrdetach(struct knote *kn) 645 { 646 struct uhid_softc *sc = kn->kn_hook; 647 int s; 648 649 s = splusb(); 650 SLIST_REMOVE(&sc->sc_rsel.sel_klist, kn, knote, kn_selnext); 651 splx(s); 652 } 653 654 static int 655 filt_uhidread(struct knote *kn, long hint) 656 { 657 struct uhid_softc *sc = kn->kn_hook; 658 659 kn->kn_data = sc->sc_q.c_cc; 660 return (kn->kn_data > 0); 661 } 662 663 static const struct filterops uhidread_filtops = 664 { 1, NULL, filt_uhidrdetach, filt_uhidread }; 665 666 static const struct filterops uhid_seltrue_filtops = 667 { 1, NULL, filt_uhidrdetach, filt_seltrue }; 668 669 int 670 uhidkqfilter(dev_t dev, struct knote *kn) 671 { 672 struct uhid_softc *sc; 673 struct klist *klist; 674 int s; 675 676 USB_GET_SC(uhid, UHIDUNIT(dev), sc); 677 678 if (sc->sc_dying) 679 return (ENXIO); 680 681 switch (kn->kn_filter) { 682 case EVFILT_READ: 683 klist = &sc->sc_rsel.sel_klist; 684 kn->kn_fop = &uhidread_filtops; 685 break; 686 687 case EVFILT_WRITE: 688 klist = &sc->sc_rsel.sel_klist; 689 kn->kn_fop = &uhid_seltrue_filtops; 690 break; 691 692 default: 693 return (EINVAL); 694 } 695 696 kn->kn_hook = sc; 697 698 s = splusb(); 699 SLIST_INSERT_HEAD(klist, kn, kn_selnext); 700 splx(s); 701 702 return (0); 703 } 704