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