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