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