1 /* $NetBSD: uhid.c,v 1.78 2008/02/12 19:37:51 drochner Exp $ */ 2 3 /* 4 * Copyright (c) 1998, 2004 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.78 2008/02/12 19:37:51 drochner 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 64 #include <dev/usb/usb.h> 65 #include <dev/usb/usbhid.h> 66 67 #include <dev/usb/usbdevs.h> 68 #include <dev/usb/usbdi.h> 69 #include <dev/usb/usbdi_util.h> 70 #include <dev/usb/hid.h> 71 #include <dev/usb/usb_quirks.h> 72 73 #include <dev/usb/uhidev.h> 74 75 #ifdef UHID_DEBUG 76 #define DPRINTF(x) if (uhiddebug) logprintf x 77 #define DPRINTFN(n,x) if (uhiddebug>(n)) logprintf x 78 int uhiddebug = 0; 79 #else 80 #define DPRINTF(x) 81 #define DPRINTFN(n,x) 82 #endif 83 84 struct uhid_softc { 85 struct uhidev sc_hdev; 86 87 int sc_isize; 88 int sc_osize; 89 int sc_fsize; 90 91 u_char *sc_obuf; 92 93 struct clist sc_q; 94 struct selinfo sc_rsel; 95 usb_proc_ptr sc_async; /* process that wants SIGIO */ 96 u_char sc_state; /* driver state */ 97 #define UHID_ASLP 0x01 /* waiting for device data */ 98 #define UHID_IMMED 0x02 /* return read data immediately */ 99 100 int sc_refcnt; 101 u_char sc_dying; 102 }; 103 104 #define UHIDUNIT(dev) (minor(dev)) 105 #define UHID_CHUNK 128 /* chunk size for read */ 106 #define UHID_BSIZE 1020 /* buffer size */ 107 108 dev_type_open(uhidopen); 109 dev_type_close(uhidclose); 110 dev_type_read(uhidread); 111 dev_type_write(uhidwrite); 112 dev_type_ioctl(uhidioctl); 113 dev_type_poll(uhidpoll); 114 dev_type_kqfilter(uhidkqfilter); 115 116 const struct cdevsw uhid_cdevsw = { 117 uhidopen, uhidclose, uhidread, uhidwrite, uhidioctl, 118 nostop, notty, uhidpoll, nommap, uhidkqfilter, D_OTHER, 119 }; 120 121 Static void uhid_intr(struct uhidev *, void *, u_int len); 122 123 Static int uhid_do_read(struct uhid_softc *, struct uio *uio, int); 124 Static int uhid_do_write(struct uhid_softc *, struct uio *uio, int); 125 Static int uhid_do_ioctl(struct uhid_softc*, u_long, void *, int, struct lwp *); 126 127 USB_DECLARE_DRIVER(uhid); 128 129 int 130 uhid_match(struct device *parent, struct cfdata *match, 131 void *aux) 132 { 133 #ifdef UHID_DEBUG 134 struct uhidev_attach_arg *uha = aux; 135 #endif 136 137 DPRINTF(("uhid_match: report=%d\n", uha->reportid)); 138 139 if (match->cf_flags & 1) 140 return (UMATCH_HIGHEST); 141 else 142 return (UMATCH_IFACECLASS_GENERIC); 143 } 144 145 void 146 uhid_attach(struct device *parent, struct device *self, void *aux) 147 { 148 struct uhid_softc *sc = (struct uhid_softc *)self; 149 struct uhidev_attach_arg *uha = aux; 150 int size, repid; 151 void *desc; 152 153 sc->sc_hdev.sc_intr = uhid_intr; 154 sc->sc_hdev.sc_parent = uha->parent; 155 sc->sc_hdev.sc_report_id = uha->reportid; 156 157 uhidev_get_report_desc(uha->parent, &desc, &size); 158 repid = uha->reportid; 159 sc->sc_isize = hid_report_size(desc, size, hid_input, repid); 160 sc->sc_osize = hid_report_size(desc, size, hid_output, repid); 161 sc->sc_fsize = hid_report_size(desc, size, hid_feature, repid); 162 163 printf(": 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 = (struct uhid_softc *)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(struct device *self, int flags) 190 { 191 struct uhid_softc *sc = (struct uhid_softc *)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 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); 256 if (sc->sc_async != NULL) { 257 DPRINTFN(3, ("uhid_intr: sending SIGIO %p\n", sc->sc_async)); 258 mutex_enter(&proclist_mutex); 259 psignal(sc->sc_async, SIGIO); 260 mutex_exit(&proclist_mutex); 261 } 262 } 263 264 int 265 uhidopen(dev_t dev, int flag, int mode, 266 struct lwp *l) 267 { 268 struct uhid_softc *sc; 269 int error; 270 271 USB_GET_SC_OPEN(uhid, UHIDUNIT(dev), sc); 272 273 DPRINTF(("uhidopen: sc=%p\n", sc)); 274 275 if (sc->sc_dying) 276 return (ENXIO); 277 278 error = uhidev_open(&sc->sc_hdev); 279 if (error) 280 return (error); 281 282 if (clalloc(&sc->sc_q, UHID_BSIZE, 0) == -1) { 283 uhidev_close(&sc->sc_hdev); 284 return (ENOMEM); 285 } 286 sc->sc_obuf = malloc(sc->sc_osize, M_USBDEV, M_WAITOK); 287 sc->sc_state &= ~UHID_IMMED; 288 sc->sc_async = NULL; 289 290 return (0); 291 } 292 293 int 294 uhidclose(dev_t dev, int flag, int mode, 295 struct lwp *l) 296 { 297 struct uhid_softc *sc; 298 299 USB_GET_SC(uhid, UHIDUNIT(dev), sc); 300 301 DPRINTF(("uhidclose: sc=%p\n", sc)); 302 303 clfree(&sc->sc_q); 304 free(sc->sc_obuf, M_USBDEV); 305 sc->sc_async = NULL; 306 uhidev_close(&sc->sc_hdev); 307 308 return (0); 309 } 310 311 int 312 uhid_do_read(struct uhid_softc *sc, struct uio *uio, int flag) 313 { 314 int s; 315 int error = 0; 316 int extra; 317 size_t length; 318 u_char buffer[UHID_CHUNK]; 319 usbd_status err; 320 321 DPRINTFN(1, ("uhidread\n")); 322 if (sc->sc_state & UHID_IMMED) { 323 DPRINTFN(1, ("uhidread immed\n")); 324 extra = sc->sc_hdev.sc_report_id != 0; 325 err = uhidev_get_report(&sc->sc_hdev, UHID_INPUT_REPORT, 326 buffer, sc->sc_isize + extra); 327 if (err) 328 return (EIO); 329 return (uiomove(buffer+extra, sc->sc_isize, uio)); 330 } 331 332 s = splusb(); 333 while (sc->sc_q.c_cc == 0) { 334 if (flag & IO_NDELAY) { 335 splx(s); 336 return (EWOULDBLOCK); 337 } 338 sc->sc_state |= UHID_ASLP; 339 DPRINTFN(5, ("uhidread: sleep on %p\n", &sc->sc_q)); 340 error = tsleep(&sc->sc_q, PZERO | PCATCH, "uhidrea", 0); 341 DPRINTFN(5, ("uhidread: woke, error=%d\n", error)); 342 if (sc->sc_dying) 343 error = EIO; 344 if (error) { 345 sc->sc_state &= ~UHID_ASLP; 346 break; 347 } 348 } 349 splx(s); 350 351 /* Transfer as many chunks as possible. */ 352 while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0 && !error) { 353 length = min(sc->sc_q.c_cc, uio->uio_resid); 354 if (length > sizeof(buffer)) 355 length = sizeof(buffer); 356 357 /* Remove a small chunk from the input queue. */ 358 (void) q_to_b(&sc->sc_q, buffer, length); 359 DPRINTFN(5, ("uhidread: got %lu chars\n", (u_long)length)); 360 361 /* Copy the data to the user process. */ 362 if ((error = uiomove(buffer, length, uio)) != 0) 363 break; 364 } 365 366 return (error); 367 } 368 369 int 370 uhidread(dev_t dev, struct uio *uio, int flag) 371 { 372 struct uhid_softc *sc; 373 int error; 374 375 USB_GET_SC(uhid, UHIDUNIT(dev), sc); 376 377 sc->sc_refcnt++; 378 error = uhid_do_read(sc, uio, flag); 379 if (--sc->sc_refcnt < 0) 380 usb_detach_wakeup(USBDEV(sc->sc_hdev.sc_dev)); 381 return (error); 382 } 383 384 int 385 uhid_do_write(struct uhid_softc *sc, struct uio *uio, int flag) 386 { 387 int error; 388 int size; 389 usbd_status err; 390 391 DPRINTFN(1, ("uhidwrite\n")); 392 393 if (sc->sc_dying) 394 return (EIO); 395 396 size = sc->sc_osize; 397 error = 0; 398 if (uio->uio_resid != size) 399 return (EINVAL); 400 error = uiomove(sc->sc_obuf, size, uio); 401 if (!error) { 402 err = uhidev_set_report(&sc->sc_hdev, UHID_OUTPUT_REPORT, 403 sc->sc_obuf, size); 404 if (err) 405 error = EIO; 406 } 407 408 return (error); 409 } 410 411 int 412 uhidwrite(dev_t dev, struct uio *uio, int flag) 413 { 414 struct uhid_softc *sc; 415 int error; 416 417 USB_GET_SC(uhid, UHIDUNIT(dev), sc); 418 419 sc->sc_refcnt++; 420 error = uhid_do_write(sc, uio, flag); 421 if (--sc->sc_refcnt < 0) 422 usb_detach_wakeup(USBDEV(sc->sc_hdev.sc_dev)); 423 return (error); 424 } 425 426 int 427 uhid_do_ioctl(struct uhid_softc *sc, u_long cmd, void *addr, 428 int flag, struct lwp *l) 429 { 430 struct usb_ctl_report_desc *rd; 431 struct usb_ctl_report *re; 432 u_char buffer[UHID_CHUNK]; 433 int size, extra; 434 usbd_status err; 435 void *desc; 436 437 DPRINTFN(2, ("uhidioctl: cmd=%lx\n", cmd)); 438 439 if (sc->sc_dying) 440 return (EIO); 441 442 switch (cmd) { 443 case FIONBIO: 444 /* All handled in the upper FS layer. */ 445 break; 446 447 case FIOASYNC: 448 if (*(int *)addr) { 449 if (sc->sc_async != NULL) 450 return (EBUSY); 451 sc->sc_async = l->l_proc; 452 DPRINTF(("uhid_do_ioctl: FIOASYNC %p\n", l->l_proc)); 453 } else 454 sc->sc_async = NULL; 455 break; 456 457 /* XXX this is not the most general solution. */ 458 case TIOCSPGRP: 459 if (sc->sc_async == NULL) 460 return (EINVAL); 461 if (*(int *)addr != sc->sc_async->p_pgid) 462 return (EPERM); 463 break; 464 465 case FIOSETOWN: 466 if (sc->sc_async == NULL) 467 return (EINVAL); 468 if (-*(int *)addr != sc->sc_async->p_pgid 469 && *(int *)addr != sc->sc_async->p_pid) 470 return (EPERM); 471 break; 472 473 case USB_GET_REPORT_DESC: 474 uhidev_get_report_desc(sc->sc_hdev.sc_parent, &desc, &size); 475 rd = (struct usb_ctl_report_desc *)addr; 476 size = min(size, sizeof rd->ucrd_data); 477 rd->ucrd_size = size; 478 memcpy(rd->ucrd_data, desc, size); 479 break; 480 481 case USB_SET_IMMED: 482 if (*(int *)addr) { 483 extra = sc->sc_hdev.sc_report_id != 0; 484 err = uhidev_get_report(&sc->sc_hdev, UHID_INPUT_REPORT, 485 buffer, sc->sc_isize + extra); 486 if (err) 487 return (EOPNOTSUPP); 488 489 sc->sc_state |= UHID_IMMED; 490 } else 491 sc->sc_state &= ~UHID_IMMED; 492 break; 493 494 case USB_GET_REPORT: 495 re = (struct usb_ctl_report *)addr; 496 switch (re->ucr_report) { 497 case UHID_INPUT_REPORT: 498 size = sc->sc_isize; 499 break; 500 case UHID_OUTPUT_REPORT: 501 size = sc->sc_osize; 502 break; 503 case UHID_FEATURE_REPORT: 504 size = sc->sc_fsize; 505 break; 506 default: 507 return (EINVAL); 508 } 509 extra = sc->sc_hdev.sc_report_id != 0; 510 err = uhidev_get_report(&sc->sc_hdev, re->ucr_report, 511 re->ucr_data, size + extra); 512 if (extra) 513 memcpy(re->ucr_data, re->ucr_data+1, size); 514 if (err) 515 return (EIO); 516 break; 517 518 case USB_SET_REPORT: 519 re = (struct usb_ctl_report *)addr; 520 switch (re->ucr_report) { 521 case UHID_INPUT_REPORT: 522 size = sc->sc_isize; 523 break; 524 case UHID_OUTPUT_REPORT: 525 size = sc->sc_osize; 526 break; 527 case UHID_FEATURE_REPORT: 528 size = sc->sc_fsize; 529 break; 530 default: 531 return (EINVAL); 532 } 533 err = uhidev_set_report(&sc->sc_hdev, re->ucr_report, 534 re->ucr_data, size); 535 if (err) 536 return (EIO); 537 break; 538 539 case USB_GET_REPORT_ID: 540 *(int *)addr = sc->sc_hdev.sc_report_id; 541 break; 542 543 case USB_GET_DEVICEINFO: 544 usbd_fill_deviceinfo(sc->sc_hdev.sc_parent->sc_udev, 545 (struct usb_device_info *)addr, 0); 546 break; 547 #ifdef COMPAT_30 548 case USB_GET_DEVICEINFO_OLD: 549 usbd_fill_deviceinfo_old(sc->sc_hdev.sc_parent->sc_udev, 550 (struct usb_device_info_old *)addr, 0); 551 552 break; 553 #endif 554 case USB_GET_STRING_DESC: 555 { 556 struct usb_string_desc *si = (struct usb_string_desc *)addr; 557 err = usbd_get_string_desc(sc->sc_hdev.sc_parent->sc_udev, 558 si->usd_string_index, 559 si->usd_language_id, &si->usd_desc, &size); 560 if (err) 561 return (EINVAL); 562 break; 563 } 564 565 default: 566 return (EINVAL); 567 } 568 return (0); 569 } 570 571 int 572 uhidioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l) 573 { 574 struct uhid_softc *sc; 575 int error; 576 577 USB_GET_SC(uhid, UHIDUNIT(dev), sc); 578 579 sc->sc_refcnt++; 580 error = uhid_do_ioctl(sc, cmd, addr, flag, l); 581 if (--sc->sc_refcnt < 0) 582 usb_detach_wakeup(USBDEV(sc->sc_hdev.sc_dev)); 583 return (error); 584 } 585 586 int 587 uhidpoll(dev_t dev, int events, struct lwp *l) 588 { 589 struct uhid_softc *sc; 590 int revents = 0; 591 int s; 592 593 USB_GET_SC(uhid, UHIDUNIT(dev), sc); 594 595 if (sc->sc_dying) 596 return (POLLHUP); 597 598 s = splusb(); 599 if (events & (POLLOUT | POLLWRNORM)) 600 revents |= events & (POLLOUT | POLLWRNORM); 601 if (events & (POLLIN | POLLRDNORM)) { 602 if (sc->sc_q.c_cc > 0) 603 revents |= events & (POLLIN | POLLRDNORM); 604 else 605 selrecord(l, &sc->sc_rsel); 606 } 607 608 splx(s); 609 return (revents); 610 } 611 612 static void 613 filt_uhidrdetach(struct knote *kn) 614 { 615 struct uhid_softc *sc = kn->kn_hook; 616 int s; 617 618 s = splusb(); 619 SLIST_REMOVE(&sc->sc_rsel.sel_klist, kn, knote, kn_selnext); 620 splx(s); 621 } 622 623 static int 624 filt_uhidread(struct knote *kn, long hint) 625 { 626 struct uhid_softc *sc = kn->kn_hook; 627 628 kn->kn_data = sc->sc_q.c_cc; 629 return (kn->kn_data > 0); 630 } 631 632 static const struct filterops uhidread_filtops = 633 { 1, NULL, filt_uhidrdetach, filt_uhidread }; 634 635 static const struct filterops uhid_seltrue_filtops = 636 { 1, NULL, filt_uhidrdetach, filt_seltrue }; 637 638 int 639 uhidkqfilter(dev_t dev, struct knote *kn) 640 { 641 struct uhid_softc *sc; 642 struct klist *klist; 643 int s; 644 645 USB_GET_SC(uhid, UHIDUNIT(dev), sc); 646 647 if (sc->sc_dying) 648 return (ENXIO); 649 650 switch (kn->kn_filter) { 651 case EVFILT_READ: 652 klist = &sc->sc_rsel.sel_klist; 653 kn->kn_fop = &uhidread_filtops; 654 break; 655 656 case EVFILT_WRITE: 657 klist = &sc->sc_rsel.sel_klist; 658 kn->kn_fop = &uhid_seltrue_filtops; 659 break; 660 661 default: 662 return (EINVAL); 663 } 664 665 kn->kn_hook = sc; 666 667 s = splusb(); 668 SLIST_INSERT_HEAD(klist, kn, kn_selnext); 669 splx(s); 670 671 return (0); 672 } 673