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