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