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