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