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