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