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