uhid.c revision 1.92.4.9 1 /* $NetBSD: uhid.c,v 1.92.4.9 2016/05/29 08:44:31 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.9 2016/05/29 08:44:31 skrll 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, uhid_detach, uhid_activate);
147
148 int
149 uhid_match(device_t parent, cfdata_t match, void *aux)
150 {
151 #ifdef UHID_DEBUG
152 struct uhidev_attach_arg *uha = aux;
153 #endif
154
155 DPRINTF(("uhid_match: report=%d\n", uha->reportid));
156
157 if (match->cf_flags & 1)
158 return UMATCH_HIGHEST;
159 else
160 return UMATCH_IFACECLASS_GENERIC;
161 }
162
163 void
164 uhid_attach(device_t parent, device_t self, void *aux)
165 {
166 struct uhid_softc *sc = device_private(self);
167 struct uhidev_attach_arg *uha = aux;
168 int size, repid;
169 void *desc;
170
171 sc->sc_hdev.sc_dev = self;
172 selinit(&sc->sc_rsel);
173 sc->sc_hdev.sc_intr = uhid_intr;
174 sc->sc_hdev.sc_parent = uha->parent;
175 sc->sc_hdev.sc_report_id = uha->reportid;
176 sc->sc_sih = softint_establish(SOFTINT_CLOCK,
177 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 usb_detach_wait(sc->sc_hdev.sc_dev,
233 &sc->sc_detach_cv, &sc->sc_lock);
234 }
235 }
236 mutex_exit(&sc->sc_lock);
237
238 /* locate the major number */
239 maj = cdevsw_lookup_major(&uhid_cdevsw);
240
241 /* Nuke the vnodes for any open instances (calls close). */
242 mn = device_unit(self);
243 vdevgone(maj, mn, mn, VCHR);
244
245 #if 0
246 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH,
247 sc->sc_hdev.sc_parent->sc_udev,
248 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_access_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_access_lock);
328 return EBUSY;
329 }
330 if (clalloc(&sc->sc_q, UHID_BSIZE, 0) == -1) {
331 mutex_exit(&sc->sc_access_lock);
332 return ENOMEM;
333 }
334
335 error = uhidev_open(&sc->sc_hdev);
336 if (error) {
337 clfree(&sc->sc_q);
338 mutex_exit(&sc->sc_access_lock);
339 return error;
340 }
341 mutex_exit(&sc->sc_access_lock);
342
343 if (sc->sc_osize > 0)
344 sc->sc_obuf = kmem_alloc(sc->sc_osize, KM_SLEEP);
345 else
346 sc->sc_obuf = NULL;
347 sc->sc_state &= ~UHID_IMMED;
348
349 mutex_enter(proc_lock);
350 sc->sc_async = NULL;
351 mutex_exit(proc_lock);
352
353 return 0;
354 }
355
356 int
357 uhidclose(dev_t dev, int flag, int mode, struct lwp *l)
358 {
359 struct uhid_softc *sc;
360
361 sc = device_lookup_private(&uhid_cd, UHIDUNIT(dev));
362
363 DPRINTF(("uhidclose: sc=%p\n", sc));
364
365 mutex_enter(proc_lock);
366 sc->sc_async = NULL;
367 mutex_exit(proc_lock);
368
369 mutex_enter(&sc->sc_access_lock);
370
371 uhidev_stop(&sc->sc_hdev);
372
373 clfree(&sc->sc_q);
374 if (sc->sc_osize > 0)
375 kmem_free(sc->sc_obuf, sc->sc_osize);
376
377 uhidev_close(&sc->sc_hdev);
378
379 mutex_exit(&sc->sc_access_lock);
380
381 return 0;
382 }
383
384 int
385 uhid_do_read(struct uhid_softc *sc, struct uio *uio, int flag)
386 {
387 int error = 0;
388 int extra;
389 size_t length;
390 u_char buffer[UHID_CHUNK];
391 usbd_status err;
392
393 DPRINTFN(1, ("uhidread\n"));
394 if (sc->sc_state & UHID_IMMED) {
395 DPRINTFN(1, ("uhidread immed\n"));
396 extra = sc->sc_hdev.sc_report_id != 0;
397 err = uhidev_get_report(&sc->sc_hdev, UHID_INPUT_REPORT,
398 buffer, sc->sc_isize + extra);
399 if (err)
400 return EIO;
401 return uiomove(buffer+extra, sc->sc_isize, uio);
402 }
403
404 mutex_enter(&sc->sc_lock);
405 while (sc->sc_q.c_cc == 0) {
406 if (flag & IO_NDELAY) {
407 mutex_exit(&sc->sc_lock);
408 return EWOULDBLOCK;
409 }
410 sc->sc_state |= UHID_ASLP;
411 DPRINTFN(5, ("uhidread: sleep on %p\n", &sc->sc_q));
412 error = cv_wait_sig(&sc->sc_cv, &sc->sc_lock);
413 DPRINTFN(5, ("uhidread: woke, error=%d\n", error));
414 if (sc->sc_dying)
415 error = EIO;
416 if (error) {
417 sc->sc_state &= ~UHID_ASLP;
418 break;
419 }
420 }
421
422 /* Transfer as many chunks as possible. */
423 while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0 && !error) {
424 length = min(sc->sc_q.c_cc, uio->uio_resid);
425 if (length > sizeof(buffer))
426 length = sizeof(buffer);
427
428 /* Remove a small chunk from the input queue. */
429 (void) q_to_b(&sc->sc_q, buffer, length);
430 DPRINTFN(5, ("uhidread: got %lu chars\n", (u_long)length));
431
432 /* Copy the data to the user process. */
433 mutex_exit(&sc->sc_lock);
434 if ((error = uiomove(buffer, length, uio)) != 0)
435 return error;
436 mutex_enter(&sc->sc_lock);
437 }
438
439 mutex_exit(&sc->sc_lock);
440 return error;
441 }
442
443 int
444 uhidread(dev_t dev, struct uio *uio, int flag)
445 {
446 struct uhid_softc *sc;
447 int error;
448
449 sc = device_lookup_private(&uhid_cd, UHIDUNIT(dev));
450
451 mutex_enter(&sc->sc_lock);
452 sc->sc_refcnt++;
453 mutex_exit(&sc->sc_lock);
454
455 mutex_enter(&sc->sc_access_lock);
456 error = uhid_do_read(sc, uio, flag);
457 mutex_exit(&sc->sc_access_lock);
458
459 mutex_enter(&sc->sc_lock);
460 if (--sc->sc_refcnt < 0)
461 usb_detach_broadcast(sc->sc_hdev.sc_dev, &sc->sc_detach_cv);
462 mutex_exit(&sc->sc_lock);
463 return error;
464 }
465
466 int
467 uhid_do_write(struct uhid_softc *sc, struct uio *uio, int flag)
468 {
469 int error;
470 int size;
471 usbd_status err;
472
473 DPRINTFN(1, ("uhidwrite\n"));
474
475 if (sc->sc_dying)
476 return EIO;
477
478 size = sc->sc_osize;
479 error = 0;
480 if (uio->uio_resid != size || size == 0)
481 return EINVAL;
482 error = uiomove(sc->sc_obuf, size, uio);
483 if (!error) {
484 err = uhidev_set_report(&sc->sc_hdev, UHID_OUTPUT_REPORT,
485 sc->sc_obuf, size);
486 if (err)
487 error = EIO;
488 }
489
490 return error;
491 }
492
493 int
494 uhidwrite(dev_t dev, struct uio *uio, int flag)
495 {
496 struct uhid_softc *sc;
497 int error;
498
499 sc = device_lookup_private(&uhid_cd, UHIDUNIT(dev));
500
501 mutex_enter(&sc->sc_lock);
502 sc->sc_refcnt++;
503 mutex_exit(&sc->sc_lock);
504
505 mutex_enter(&sc->sc_access_lock);
506 error = uhid_do_write(sc, uio, flag);
507 mutex_exit(&sc->sc_access_lock);
508
509 mutex_enter(&sc->sc_lock);
510 if (--sc->sc_refcnt < 0)
511 usb_detach_broadcast(sc->sc_hdev.sc_dev, &sc->sc_detach_cv);
512 mutex_exit(&sc->sc_lock);
513 return error;
514 }
515
516 int
517 uhid_do_ioctl(struct uhid_softc *sc, u_long cmd, void *addr,
518 int flag, struct lwp *l)
519 {
520 struct usb_ctl_report_desc *rd;
521 struct usb_ctl_report *re;
522 u_char buffer[UHID_CHUNK];
523 int size, extra;
524 usbd_status err;
525 void *desc;
526
527 DPRINTFN(2, ("uhidioctl: cmd=%lx\n", cmd));
528
529 if (sc->sc_dying)
530 return EIO;
531
532 switch (cmd) {
533 case FIONBIO:
534 /* All handled in the upper FS layer. */
535 break;
536
537 case FIOASYNC:
538 mutex_enter(proc_lock);
539 if (*(int *)addr) {
540 if (sc->sc_async != NULL)
541 return EBUSY;
542 sc->sc_async = l->l_proc;
543 DPRINTF(("uhid_do_ioctl: FIOASYNC %p\n", l->l_proc));
544 } else
545 sc->sc_async = NULL;
546 mutex_exit(proc_lock);
547 break;
548
549 /* XXX this is not the most general solution. */
550 case TIOCSPGRP:
551 mutex_enter(proc_lock);
552 if (sc->sc_async == NULL) {
553 mutex_exit(proc_lock);
554 return EINVAL;
555 }
556 if (*(int *)addr != sc->sc_async->p_pgid) {
557 mutex_exit(proc_lock);
558 return EPERM;
559 }
560 mutex_exit(proc_lock);
561 break;
562
563 case FIOSETOWN:
564 mutex_enter(proc_lock);
565 if (sc->sc_async == NULL) {
566 mutex_exit(proc_lock);
567 return EINVAL;
568 }
569 if (-*(int *)addr != sc->sc_async->p_pgid
570 && *(int *)addr != sc->sc_async->p_pid) {
571 mutex_exit(proc_lock);
572 return EPERM;
573 }
574 mutex_exit(proc_lock);
575 break;
576
577 case USB_GET_REPORT_DESC:
578 uhidev_get_report_desc(sc->sc_hdev.sc_parent, &desc, &size);
579 rd = (struct usb_ctl_report_desc *)addr;
580 size = min(size, sizeof(rd->ucrd_data));
581 rd->ucrd_size = size;
582 memcpy(rd->ucrd_data, desc, size);
583 break;
584
585 case USB_SET_IMMED:
586 if (*(int *)addr) {
587 extra = sc->sc_hdev.sc_report_id != 0;
588 err = uhidev_get_report(&sc->sc_hdev, UHID_INPUT_REPORT,
589 buffer, sc->sc_isize + extra);
590 if (err)
591 return EOPNOTSUPP;
592
593 sc->sc_state |= UHID_IMMED;
594 } else
595 sc->sc_state &= ~UHID_IMMED;
596 break;
597
598 case USB_GET_REPORT:
599 re = (struct usb_ctl_report *)addr;
600 switch (re->ucr_report) {
601 case UHID_INPUT_REPORT:
602 size = sc->sc_isize;
603 break;
604 case UHID_OUTPUT_REPORT:
605 size = sc->sc_osize;
606 break;
607 case UHID_FEATURE_REPORT:
608 size = sc->sc_fsize;
609 break;
610 default:
611 return EINVAL;
612 }
613 extra = sc->sc_hdev.sc_report_id != 0;
614 err = uhidev_get_report(&sc->sc_hdev, re->ucr_report,
615 re->ucr_data, size + extra);
616 if (extra)
617 memcpy(re->ucr_data, re->ucr_data+1, size);
618 if (err)
619 return EIO;
620 break;
621
622 case USB_SET_REPORT:
623 re = (struct usb_ctl_report *)addr;
624 switch (re->ucr_report) {
625 case UHID_INPUT_REPORT:
626 size = sc->sc_isize;
627 break;
628 case UHID_OUTPUT_REPORT:
629 size = sc->sc_osize;
630 break;
631 case UHID_FEATURE_REPORT:
632 size = sc->sc_fsize;
633 break;
634 default:
635 return EINVAL;
636 }
637 err = uhidev_set_report(&sc->sc_hdev, re->ucr_report,
638 re->ucr_data, size);
639 if (err)
640 return EIO;
641 break;
642
643 case USB_GET_REPORT_ID:
644 *(int *)addr = sc->sc_hdev.sc_report_id;
645 break;
646
647 case USB_GET_DEVICE_DESC:
648 *(usb_device_descriptor_t *)addr =
649 *usbd_get_device_descriptor(sc->sc_hdev.sc_parent->sc_udev);
650 break;
651
652 case USB_GET_DEVICEINFO:
653 usbd_fill_deviceinfo(sc->sc_hdev.sc_parent->sc_udev,
654 (struct usb_device_info *)addr, 0);
655 break;
656 #ifdef COMPAT_30
657 case USB_GET_DEVICEINFO_OLD:
658 usbd_fill_deviceinfo_old(sc->sc_hdev.sc_parent->sc_udev,
659 (struct usb_device_info_old *)addr, 0);
660
661 break;
662 #endif
663 case USB_GET_STRING_DESC:
664 {
665 struct usb_string_desc *si = (struct usb_string_desc *)addr;
666 err = usbd_get_string_desc(sc->sc_hdev.sc_parent->sc_udev,
667 si->usd_string_index,
668 si->usd_language_id, &si->usd_desc, &size);
669 if (err)
670 return EINVAL;
671 break;
672 }
673
674 default:
675 return EINVAL;
676 }
677 return 0;
678 }
679
680 int
681 uhidioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
682 {
683 struct uhid_softc *sc;
684 int error;
685
686 sc = device_lookup_private(&uhid_cd, UHIDUNIT(dev));
687 if (sc == NULL)
688 return ENXIO;
689
690 if (sc->sc_dying)
691 return EIO;
692
693 mutex_enter(&sc->sc_lock);
694 sc->sc_refcnt++;
695 mutex_exit(&sc->sc_lock);
696
697 mutex_enter(&sc->sc_access_lock);
698 error = uhid_do_ioctl(sc, cmd, addr, flag, l);
699 mutex_exit(&sc->sc_access_lock);
700
701 mutex_enter(&sc->sc_lock);
702 if (--sc->sc_refcnt < 0)
703 usb_detach_broadcast(sc->sc_hdev.sc_dev, &sc->sc_detach_cv);
704 mutex_exit(&sc->sc_lock);
705 return error;
706 }
707
708 int
709 uhidpoll(dev_t dev, int events, struct lwp *l)
710 {
711 struct uhid_softc *sc;
712 int revents = 0;
713
714 sc = device_lookup_private(&uhid_cd, UHIDUNIT(dev));
715 if (sc == NULL)
716 return ENXIO;
717
718 if (sc->sc_dying)
719 return EIO;
720
721 mutex_enter(&sc->sc_lock);
722 if (events & (POLLOUT | POLLWRNORM))
723 revents |= events & (POLLOUT | POLLWRNORM);
724 if (events & (POLLIN | POLLRDNORM)) {
725 if (sc->sc_q.c_cc > 0)
726 revents |= events & (POLLIN | POLLRDNORM);
727 else
728 selrecord(l, &sc->sc_rsel);
729 }
730 mutex_exit(&sc->sc_lock);
731
732 return revents;
733 }
734
735 static void
736 filt_uhidrdetach(struct knote *kn)
737 {
738 struct uhid_softc *sc = kn->kn_hook;
739
740 mutex_enter(&sc->sc_lock);
741 SLIST_REMOVE(&sc->sc_rsel.sel_klist, kn, knote, kn_selnext);
742 mutex_exit(&sc->sc_lock);
743 }
744
745 static int
746 filt_uhidread(struct knote *kn, long hint)
747 {
748 struct uhid_softc *sc = kn->kn_hook;
749
750 kn->kn_data = sc->sc_q.c_cc;
751 return kn->kn_data > 0;
752 }
753
754 static const struct filterops uhidread_filtops =
755 { 1, NULL, filt_uhidrdetach, filt_uhidread };
756
757 static const struct filterops uhid_seltrue_filtops =
758 { 1, NULL, filt_uhidrdetach, filt_seltrue };
759
760 int
761 uhidkqfilter(dev_t dev, struct knote *kn)
762 {
763 struct uhid_softc *sc;
764 struct klist *klist;
765
766 sc = device_lookup_private(&uhid_cd, UHIDUNIT(dev));
767
768 if (sc->sc_dying)
769 return ENXIO;
770
771 switch (kn->kn_filter) {
772 case EVFILT_READ:
773 klist = &sc->sc_rsel.sel_klist;
774 kn->kn_fop = &uhidread_filtops;
775 break;
776
777 case EVFILT_WRITE:
778 klist = &sc->sc_rsel.sel_klist;
779 kn->kn_fop = &uhid_seltrue_filtops;
780 break;
781
782 default:
783 return EINVAL;
784 }
785
786 kn->kn_hook = sc;
787
788 mutex_enter(&sc->sc_lock);
789 SLIST_INSERT_HEAD(klist, kn, kn_selnext);
790 mutex_exit(&sc->sc_lock);
791
792 return 0;
793 }
794