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