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