uhid.c revision 1.92.4.4 1 /* $NetBSD: uhid.c,v 1.92.4.4 2014/12/05 09:37:49 skrll Exp $ */
2
3 /*
4 * Copyright (c) 1998, 2004, 2008, 2012 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 and Matthew R. Green (mrg (at) eterna.com.au).
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.92.4.4 2014/12/05 09:37:49 skrll Exp $");
39
40 #ifdef _KERNEL_OPT
41 #include "opt_compat_netbsd.h"
42 #endif
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/kmem.h>
48 #include <sys/signalvar.h>
49 #include <sys/device.h>
50 #include <sys/ioctl.h>
51 #include <sys/conf.h>
52 #include <sys/tty.h>
53 #include <sys/file.h>
54 #include <sys/select.h>
55 #include <sys/proc.h>
56 #include <sys/vnode.h>
57 #include <sys/poll.h>
58 #include <sys/intr.h>
59
60 #include <dev/usb/usb.h>
61 #include <dev/usb/usbhid.h>
62
63 #include <dev/usb/usbdevs.h>
64 #include <dev/usb/usbdi.h>
65 #include <dev/usb/usbdi_util.h>
66 #include <dev/usb/hid.h>
67 #include <dev/usb/usb_quirks.h>
68
69 #include <dev/usb/uhidev.h>
70
71 #ifdef UHID_DEBUG
72 #define DPRINTF(x) if (uhiddebug) printf x
73 #define DPRINTFN(n,x) if (uhiddebug>(n)) printf x
74 int uhiddebug = 0;
75 #else
76 #define DPRINTF(x)
77 #define DPRINTFN(n,x)
78 #endif
79
80 struct uhid_softc {
81 struct uhidev sc_hdev;
82
83 kmutex_t sc_access_lock; /* serialises syscall accesses */
84 kmutex_t sc_lock; /* protects refcnt, others */
85 kcondvar_t sc_cv;
86 kcondvar_t sc_detach_cv;
87
88 int sc_isize;
89 int sc_osize;
90 int sc_fsize;
91
92 u_char *sc_obuf;
93
94 struct clist sc_q;
95 struct selinfo sc_rsel;
96 proc_t *sc_async; /* process that wants SIGIO */
97 void *sc_sih;
98 u_char sc_state; /* driver state */
99 #define UHID_ASLP 0x01 /* waiting for device data */
100 #define UHID_IMMED 0x02 /* return read data immediately */
101
102 int sc_refcnt;
103 u_char sc_dying;
104 };
105
106 #define UHIDUNIT(dev) (minor(dev))
107 #define UHID_CHUNK 128 /* chunk size for read */
108 #define UHID_BSIZE 1020 /* buffer size */
109
110 dev_type_open(uhidopen);
111 dev_type_close(uhidclose);
112 dev_type_read(uhidread);
113 dev_type_write(uhidwrite);
114 dev_type_ioctl(uhidioctl);
115 dev_type_poll(uhidpoll);
116 dev_type_kqfilter(uhidkqfilter);
117
118 const struct cdevsw uhid_cdevsw = {
119 .d_open = uhidopen,
120 .d_close = uhidclose,
121 .d_read = uhidread,
122 .d_write = uhidwrite,
123 .d_ioctl = uhidioctl,
124 .d_stop = nostop,
125 .d_tty = notty,
126 .d_poll = uhidpoll,
127 .d_mmap = nommap,
128 .d_kqfilter = uhidkqfilter,
129 .d_discard = nodiscard,
130 .d_flag = D_OTHER | D_MPSAFE
131 };
132
133 Static void uhid_intr(struct uhidev *, void *, u_int len);
134 Static void uhid_softintr(void *);
135
136 Static int uhid_do_read(struct uhid_softc *, struct uio *uio, int);
137 Static int uhid_do_write(struct uhid_softc *, struct uio *uio, int);
138 Static int uhid_do_ioctl(struct uhid_softc*, u_long, void *, int, struct lwp *);
139
140 int uhid_match(device_t, cfdata_t, void *);
141 void uhid_attach(device_t, device_t, void *);
142 int uhid_detach(device_t, int);
143 int uhid_activate(device_t, enum devact);
144 extern struct cfdriver uhid_cd;
145 CFATTACH_DECL_NEW(uhid, sizeof(struct uhid_softc), uhid_match, uhid_attach, uhid_detach, uhid_activate);
146
147 int
148 uhid_match(device_t parent, cfdata_t match, void *aux)
149 {
150 #ifdef UHID_DEBUG
151 struct uhidev_attach_arg *uha = aux;
152 #endif
153
154 DPRINTF(("uhid_match: report=%d\n", uha->reportid));
155
156 if (match->cf_flags & 1)
157 return UMATCH_HIGHEST;
158 else
159 return UMATCH_IFACECLASS_GENERIC;
160 }
161
162 void
163 uhid_attach(device_t parent, device_t self, void *aux)
164 {
165 struct uhid_softc *sc = device_private(self);
166 struct uhidev_attach_arg *uha = aux;
167 int size, repid;
168 void *desc;
169
170 sc->sc_hdev.sc_dev = self;
171 selinit(&sc->sc_rsel);
172 sc->sc_hdev.sc_intr = uhid_intr;
173 sc->sc_hdev.sc_parent = uha->parent;
174 sc->sc_hdev.sc_report_id = uha->reportid;
175 sc->sc_sih = softint_establish(SOFTINT_MPSAFE | SOFTINT_CLOCK,
176 uhid_softintr, sc);
177
178 uhidev_get_report_desc(uha->parent, &desc, &size);
179 repid = uha->reportid;
180 sc->sc_isize = hid_report_size(desc, size, hid_input, repid);
181 sc->sc_osize = hid_report_size(desc, size, hid_output, repid);
182 sc->sc_fsize = hid_report_size(desc, size, hid_feature, repid);
183
184 aprint_naive("\n");
185 aprint_normal(": input=%d, output=%d, feature=%d\n",
186 sc->sc_isize, sc->sc_osize, sc->sc_fsize);
187
188 mutex_init(&sc->sc_access_lock, MUTEX_DEFAULT, IPL_NONE);
189 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_USB);
190 cv_init(&sc->sc_cv, "uhidrea");
191 cv_init(&sc->sc_detach_cv, "uhiddet");
192
193 if (!pmf_device_register(self, NULL, NULL))
194 aprint_error_dev(self, "couldn't establish power handler\n");
195
196 return;
197 }
198
199 int
200 uhid_activate(device_t self, enum devact act)
201 {
202 struct uhid_softc *sc = device_private(self);
203
204 switch (act) {
205 case DVACT_DEACTIVATE:
206 sc->sc_dying = 1;
207 return 0;
208 default:
209 return EOPNOTSUPP;
210 }
211 }
212
213 int
214 uhid_detach(device_t self, int flags)
215 {
216 struct uhid_softc *sc = device_private(self);
217 int maj, mn;
218
219 DPRINTF(("uhid_detach: sc=%p flags=%d\n", sc, flags));
220
221 sc->sc_dying = 1;
222
223 pmf_device_deregister(self);
224
225 mutex_enter(&sc->sc_lock);
226 if (sc->sc_hdev.sc_state & UHIDEV_OPEN) {
227 if (--sc->sc_refcnt >= 0) {
228 /* Wake everyone */
229 cv_broadcast(&sc->sc_cv);
230 /* Wait for processes to go away. */
231 usb_detach_wait(sc->sc_hdev.sc_dev,
232 &sc->sc_detach_cv, &sc->sc_lock);
233 }
234 }
235 mutex_exit(&sc->sc_lock);
236
237 /* locate the major number */
238 maj = cdevsw_lookup_major(&uhid_cdevsw);
239
240 /* Nuke the vnodes for any open instances (calls close). */
241 mn = device_unit(self);
242 vdevgone(maj, mn, mn, VCHR);
243
244 #if 0
245 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH,
246 sc->sc_hdev.sc_parent->sc_udev,
247 sc->sc_hdev.sc_dev);
248 #endif
249 cv_destroy(&sc->sc_cv);
250 cv_destroy(&sc->sc_detach_cv);
251 mutex_destroy(&sc->sc_lock);
252 mutex_destroy(&sc->sc_access_lock);
253 seldestroy(&sc->sc_rsel);
254 softint_disestablish(sc->sc_sih);
255
256 return 0;
257 }
258
259 void
260 uhid_intr(struct uhidev *addr, void *data, u_int len)
261 {
262 struct uhid_softc *sc = (struct uhid_softc *)addr;
263
264 #ifdef UHID_DEBUG
265 if (uhiddebug > 5) {
266 uint32_t i;
267
268 DPRINTF(("uhid_intr: data ="));
269 for (i = 0; i < len; i++)
270 DPRINTF((" %02x", ((u_char *)data)[i]));
271 DPRINTF(("\n"));
272 }
273 #endif
274
275 mutex_enter(&sc->sc_lock);
276 (void)b_to_q(data, len, &sc->sc_q);
277
278 if (sc->sc_state & UHID_ASLP) {
279 sc->sc_state &= ~UHID_ASLP;
280 DPRINTFN(5, ("uhid_intr: waking %p\n", &sc->sc_q));
281 cv_broadcast(&sc->sc_cv);
282 }
283 selnotify(&sc->sc_rsel, 0, 0);
284 if (sc->sc_async != NULL) {
285 DPRINTFN(3, ("uhid_intr: sending SIGIO %p\n", sc->sc_async));
286 softint_schedule(sc->sc_sih);
287 }
288 mutex_exit(&sc->sc_lock);
289 }
290
291 void
292 uhid_softintr(void *cookie)
293 {
294 struct uhid_softc *sc;
295
296 sc = cookie;
297
298 mutex_enter(proc_lock);
299 if (sc->sc_async != NULL)
300 psignal(sc->sc_async, SIGIO);
301 mutex_exit(proc_lock);
302 }
303
304 int
305 uhidopen(dev_t dev, int flag, int mode, struct lwp *l)
306 {
307 struct uhid_softc *sc;
308 int error;
309
310 sc = device_lookup_private(&uhid_cd, UHIDUNIT(dev));
311 if (sc == NULL)
312 return ENXIO;
313
314 DPRINTF(("uhidopen: sc=%p\n", sc));
315
316 if (sc->sc_dying)
317 return ENXIO;
318
319 mutex_enter(&sc->sc_access_lock);
320 error = uhidev_open(&sc->sc_hdev);
321 if (error) {
322 mutex_exit(&sc->sc_access_lock);
323 return error;
324 }
325 mutex_exit(&sc->sc_access_lock);
326
327 if (clalloc(&sc->sc_q, UHID_BSIZE, 0) == -1) {
328 mutex_enter(&sc->sc_access_lock);
329 uhidev_close(&sc->sc_hdev);
330 mutex_exit(&sc->sc_access_lock);
331 return ENOMEM;
332 }
333 sc->sc_obuf = kmem_alloc(sc->sc_osize, KM_SLEEP);
334 sc->sc_state &= ~UHID_IMMED;
335
336 mutex_enter(proc_lock);
337 sc->sc_async = NULL;
338 mutex_exit(proc_lock);
339
340 return 0;
341 }
342
343 int
344 uhidclose(dev_t dev, int flag, int mode, struct lwp *l)
345 {
346 struct uhid_softc *sc;
347
348 sc = device_lookup_private(&uhid_cd, UHIDUNIT(dev));
349
350 DPRINTF(("uhidclose: sc=%p\n", sc));
351
352 clfree(&sc->sc_q);
353 kmem_free(sc->sc_obuf, sc->sc_osize);
354
355 mutex_enter(proc_lock);
356 sc->sc_async = NULL;
357 mutex_exit(proc_lock);
358
359 mutex_enter(&sc->sc_access_lock);
360 uhidev_close(&sc->sc_hdev);
361 mutex_exit(&sc->sc_access_lock);
362
363 return 0;
364 }
365
366 int
367 uhid_do_read(struct uhid_softc *sc, struct uio *uio, int flag)
368 {
369 int error = 0;
370 int extra;
371 size_t length;
372 u_char buffer[UHID_CHUNK];
373 usbd_status err;
374
375 DPRINTFN(1, ("uhidread\n"));
376 if (sc->sc_state & UHID_IMMED) {
377 DPRINTFN(1, ("uhidread immed\n"));
378 extra = sc->sc_hdev.sc_report_id != 0;
379 err = uhidev_get_report(&sc->sc_hdev, UHID_INPUT_REPORT,
380 buffer, sc->sc_isize + extra);
381 if (err)
382 return EIO;
383 return uiomove(buffer+extra, sc->sc_isize, uio);
384 }
385
386 mutex_enter(&sc->sc_lock);
387 while (sc->sc_q.c_cc == 0) {
388 if (flag & IO_NDELAY) {
389 mutex_exit(&sc->sc_lock);
390 return EWOULDBLOCK;
391 }
392 sc->sc_state |= UHID_ASLP;
393 DPRINTFN(5, ("uhidread: sleep on %p\n", &sc->sc_q));
394 error = cv_wait_sig(&sc->sc_cv, &sc->sc_lock);
395 DPRINTFN(5, ("uhidread: woke, error=%d\n", error));
396 if (sc->sc_dying)
397 error = EIO;
398 if (error) {
399 sc->sc_state &= ~UHID_ASLP;
400 break;
401 }
402 }
403 mutex_exit(&sc->sc_lock);
404
405 /* Transfer as many chunks as possible. */
406 while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0 && !error) {
407 length = min(sc->sc_q.c_cc, uio->uio_resid);
408 if (length > sizeof(buffer))
409 length = sizeof(buffer);
410
411 /* Remove a small chunk from the input queue. */
412 (void) q_to_b(&sc->sc_q, buffer, length);
413 DPRINTFN(5, ("uhidread: got %lu chars\n", (u_long)length));
414
415 /* Copy the data to the user process. */
416 if ((error = uiomove(buffer, length, uio)) != 0)
417 break;
418 }
419
420 return error;
421 }
422
423 int
424 uhidread(dev_t dev, struct uio *uio, int flag)
425 {
426 struct uhid_softc *sc;
427 int error;
428
429 sc = device_lookup_private(&uhid_cd, UHIDUNIT(dev));
430
431 mutex_enter(&sc->sc_lock);
432 sc->sc_refcnt++;
433 mutex_exit(&sc->sc_lock);
434
435 mutex_enter(&sc->sc_access_lock);
436 error = uhid_do_read(sc, uio, flag);
437 mutex_exit(&sc->sc_access_lock);
438
439 mutex_enter(&sc->sc_lock);
440 if (--sc->sc_refcnt < 0)
441 usb_detach_broadcast(sc->sc_hdev.sc_dev, &sc->sc_detach_cv);
442 mutex_exit(&sc->sc_lock);
443 return error;
444 }
445
446 int
447 uhid_do_write(struct uhid_softc *sc, struct uio *uio, int flag)
448 {
449 int error;
450 int size;
451 usbd_status err;
452
453 DPRINTFN(1, ("uhidwrite\n"));
454
455 if (sc->sc_dying)
456 return EIO;
457
458 size = sc->sc_osize;
459 error = 0;
460 if (uio->uio_resid != size)
461 return EINVAL;
462 error = uiomove(sc->sc_obuf, size, uio);
463 if (!error) {
464 err = uhidev_set_report(&sc->sc_hdev, UHID_OUTPUT_REPORT,
465 sc->sc_obuf, size);
466 if (err)
467 error = EIO;
468 }
469
470 return error;
471 }
472
473 int
474 uhidwrite(dev_t dev, struct uio *uio, int flag)
475 {
476 struct uhid_softc *sc;
477 int error;
478
479 sc = device_lookup_private(&uhid_cd, UHIDUNIT(dev));
480
481 mutex_enter(&sc->sc_lock);
482 sc->sc_refcnt++;
483 mutex_exit(&sc->sc_lock);
484
485 mutex_enter(&sc->sc_access_lock);
486 error = uhid_do_write(sc, uio, flag);
487 mutex_exit(&sc->sc_access_lock);
488
489 mutex_enter(&sc->sc_lock);
490 if (--sc->sc_refcnt < 0)
491 usb_detach_broadcast(sc->sc_hdev.sc_dev, &sc->sc_detach_cv);
492 mutex_exit(&sc->sc_lock);
493 return error;
494 }
495
496 int
497 uhid_do_ioctl(struct uhid_softc *sc, u_long cmd, void *addr,
498 int flag, struct lwp *l)
499 {
500 struct usb_ctl_report_desc *rd;
501 struct usb_ctl_report *re;
502 u_char buffer[UHID_CHUNK];
503 int size, extra;
504 usbd_status err;
505 void *desc;
506
507 DPRINTFN(2, ("uhidioctl: cmd=%lx\n", cmd));
508
509 if (sc->sc_dying)
510 return EIO;
511
512 switch (cmd) {
513 case FIONBIO:
514 /* All handled in the upper FS layer. */
515 break;
516
517 case FIOASYNC:
518 mutex_enter(proc_lock);
519 if (*(int *)addr) {
520 if (sc->sc_async != NULL)
521 return EBUSY;
522 sc->sc_async = l->l_proc;
523 DPRINTF(("uhid_do_ioctl: FIOASYNC %p\n", l->l_proc));
524 } else
525 sc->sc_async = NULL;
526 mutex_exit(proc_lock);
527 break;
528
529 /* XXX this is not the most general solution. */
530 case TIOCSPGRP:
531 mutex_enter(proc_lock);
532 if (sc->sc_async == NULL) {
533 mutex_exit(proc_lock);
534 return EINVAL;
535 }
536 if (*(int *)addr != sc->sc_async->p_pgid) {
537 mutex_exit(proc_lock);
538 return EPERM;
539 }
540 mutex_exit(proc_lock);
541 break;
542
543 case FIOSETOWN:
544 mutex_enter(proc_lock);
545 if (sc->sc_async == NULL) {
546 mutex_exit(proc_lock);
547 return EINVAL;
548 }
549 if (-*(int *)addr != sc->sc_async->p_pgid
550 && *(int *)addr != sc->sc_async->p_pid) {
551 mutex_exit(proc_lock);
552 return EPERM;
553 }
554 mutex_exit(proc_lock);
555 break;
556
557 case USB_GET_REPORT_DESC:
558 uhidev_get_report_desc(sc->sc_hdev.sc_parent, &desc, &size);
559 rd = (struct usb_ctl_report_desc *)addr;
560 size = min(size, sizeof rd->ucrd_data);
561 rd->ucrd_size = size;
562 memcpy(rd->ucrd_data, desc, size);
563 break;
564
565 case USB_SET_IMMED:
566 if (*(int *)addr) {
567 extra = sc->sc_hdev.sc_report_id != 0;
568 err = uhidev_get_report(&sc->sc_hdev, UHID_INPUT_REPORT,
569 buffer, sc->sc_isize + extra);
570 if (err)
571 return EOPNOTSUPP;
572
573 sc->sc_state |= UHID_IMMED;
574 } else
575 sc->sc_state &= ~UHID_IMMED;
576 break;
577
578 case USB_GET_REPORT:
579 re = (struct usb_ctl_report *)addr;
580 switch (re->ucr_report) {
581 case UHID_INPUT_REPORT:
582 size = sc->sc_isize;
583 break;
584 case UHID_OUTPUT_REPORT:
585 size = sc->sc_osize;
586 break;
587 case UHID_FEATURE_REPORT:
588 size = sc->sc_fsize;
589 break;
590 default:
591 return EINVAL;
592 }
593 extra = sc->sc_hdev.sc_report_id != 0;
594 err = uhidev_get_report(&sc->sc_hdev, re->ucr_report,
595 re->ucr_data, size + extra);
596 if (extra)
597 memcpy(re->ucr_data, re->ucr_data+1, size);
598 if (err)
599 return EIO;
600 break;
601
602 case USB_SET_REPORT:
603 re = (struct usb_ctl_report *)addr;
604 switch (re->ucr_report) {
605 case UHID_INPUT_REPORT:
606 size = sc->sc_isize;
607 break;
608 case UHID_OUTPUT_REPORT:
609 size = sc->sc_osize;
610 break;
611 case UHID_FEATURE_REPORT:
612 size = sc->sc_fsize;
613 break;
614 default:
615 return EINVAL;
616 }
617 err = uhidev_set_report(&sc->sc_hdev, re->ucr_report,
618 re->ucr_data, size);
619 if (err)
620 return EIO;
621 break;
622
623 case USB_GET_REPORT_ID:
624 *(int *)addr = sc->sc_hdev.sc_report_id;
625 break;
626
627 case USB_GET_DEVICE_DESC:
628 *(usb_device_descriptor_t *)addr =
629 *usbd_get_device_descriptor(sc->sc_hdev.sc_parent->sc_udev);
630 break;
631
632 case USB_GET_DEVICEINFO:
633 usbd_fill_deviceinfo(sc->sc_hdev.sc_parent->sc_udev,
634 (struct usb_device_info *)addr, 0);
635 break;
636 #ifdef COMPAT_30
637 case USB_GET_DEVICEINFO_OLD:
638 usbd_fill_deviceinfo_old(sc->sc_hdev.sc_parent->sc_udev,
639 (struct usb_device_info_old *)addr, 0);
640
641 break;
642 #endif
643 case USB_GET_STRING_DESC:
644 {
645 struct usb_string_desc *si = (struct usb_string_desc *)addr;
646 err = usbd_get_string_desc(sc->sc_hdev.sc_parent->sc_udev,
647 si->usd_string_index,
648 si->usd_language_id, &si->usd_desc, &size);
649 if (err)
650 return EINVAL;
651 break;
652 }
653
654 default:
655 return EINVAL;
656 }
657 return 0;
658 }
659
660 int
661 uhidioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
662 {
663 struct uhid_softc *sc;
664 int error;
665
666 sc = device_lookup_private(&uhid_cd, UHIDUNIT(dev));
667 if (sc == NULL)
668 return ENXIO;
669
670 if (sc->sc_dying)
671 return EIO;
672
673 mutex_enter(&sc->sc_lock);
674 sc->sc_refcnt++;
675 mutex_exit(&sc->sc_lock);
676
677 mutex_enter(&sc->sc_access_lock);
678 error = uhid_do_ioctl(sc, cmd, addr, flag, l);
679 mutex_exit(&sc->sc_access_lock);
680
681 mutex_enter(&sc->sc_lock);
682 if (--sc->sc_refcnt < 0)
683 usb_detach_broadcast(sc->sc_hdev.sc_dev, &sc->sc_detach_cv);
684 mutex_exit(&sc->sc_lock);
685 return error;
686 }
687
688 int
689 uhidpoll(dev_t dev, int events, struct lwp *l)
690 {
691 struct uhid_softc *sc;
692 int revents = 0;
693
694 sc = device_lookup_private(&uhid_cd, UHIDUNIT(dev));
695 if (sc == NULL)
696 return ENXIO;
697
698 if (sc->sc_dying)
699 return EIO;
700
701 mutex_enter(&sc->sc_lock);
702 if (events & (POLLOUT | POLLWRNORM))
703 revents |= events & (POLLOUT | POLLWRNORM);
704 if (events & (POLLIN | POLLRDNORM)) {
705 if (sc->sc_q.c_cc > 0)
706 revents |= events & (POLLIN | POLLRDNORM);
707 else
708 selrecord(l, &sc->sc_rsel);
709 }
710 mutex_exit(&sc->sc_lock);
711
712 return revents;
713 }
714
715 static void
716 filt_uhidrdetach(struct knote *kn)
717 {
718 struct uhid_softc *sc = kn->kn_hook;
719
720 mutex_enter(&sc->sc_lock);
721 SLIST_REMOVE(&sc->sc_rsel.sel_klist, kn, knote, kn_selnext);
722 mutex_exit(&sc->sc_lock);
723 }
724
725 static int
726 filt_uhidread(struct knote *kn, long hint)
727 {
728 struct uhid_softc *sc = kn->kn_hook;
729
730 kn->kn_data = sc->sc_q.c_cc;
731 return kn->kn_data > 0;
732 }
733
734 static const struct filterops uhidread_filtops =
735 { 1, NULL, filt_uhidrdetach, filt_uhidread };
736
737 static const struct filterops uhid_seltrue_filtops =
738 { 1, NULL, filt_uhidrdetach, filt_seltrue };
739
740 int
741 uhidkqfilter(dev_t dev, struct knote *kn)
742 {
743 struct uhid_softc *sc;
744 struct klist *klist;
745
746 sc = device_lookup_private(&uhid_cd, UHIDUNIT(dev));
747
748 if (sc->sc_dying)
749 return ENXIO;
750
751 switch (kn->kn_filter) {
752 case EVFILT_READ:
753 klist = &sc->sc_rsel.sel_klist;
754 kn->kn_fop = &uhidread_filtops;
755 break;
756
757 case EVFILT_WRITE:
758 klist = &sc->sc_rsel.sel_klist;
759 kn->kn_fop = &uhid_seltrue_filtops;
760 break;
761
762 default:
763 return EINVAL;
764 }
765
766 kn->kn_hook = sc;
767
768 mutex_enter(&sc->sc_lock);
769 SLIST_INSERT_HEAD(klist, kn, kn_selnext);
770 mutex_exit(&sc->sc_lock);
771
772 return 0;
773 }
774