xref: /netbsd-src/sys/dev/usb/uhid.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: uhid.c,v 1.91 2014/07/02 20:17:30 christos 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.91 2014/07/02 20:17:30 christos 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 	pmf_device_deregister(self);
223 
224 	mutex_enter(&sc->sc_lock);
225 	if (sc->sc_hdev.sc_state & UHIDEV_OPEN) {
226 		if (--sc->sc_refcnt >= 0) {
227 			/* Wake everyone */
228 			cv_broadcast(&sc->sc_cv);
229 			/* Wait for processes to go away. */
230 			usb_detach_wait(sc->sc_hdev.sc_dev,
231 			    &sc->sc_detach_cv, &sc->sc_lock);
232 		}
233 	}
234 	mutex_exit(&sc->sc_lock);
235 
236 	/* locate the major number */
237 	maj = cdevsw_lookup_major(&uhid_cdevsw);
238 
239 	/* Nuke the vnodes for any open instances (calls close). */
240 	mn = device_unit(self);
241 	vdevgone(maj, mn, mn, VCHR);
242 
243 #if 0
244 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH,
245 			   sc->sc_hdev.sc_parent->sc_udev,
246 			   sc->sc_hdev.sc_dev);
247 #endif
248 	cv_destroy(&sc->sc_cv);
249 	cv_destroy(&sc->sc_detach_cv);
250 	mutex_destroy(&sc->sc_lock);
251 	mutex_destroy(&sc->sc_access_lock);
252 	seldestroy(&sc->sc_rsel);
253 	softint_disestablish(sc->sc_sih);
254 
255 	return (0);
256 }
257 
258 void
259 uhid_intr(struct uhidev *addr, void *data, u_int len)
260 {
261 	struct uhid_softc *sc = (struct uhid_softc *)addr;
262 
263 #ifdef UHID_DEBUG
264 	if (uhiddebug > 5) {
265 		u_int32_t i;
266 
267 		DPRINTF(("uhid_intr: data ="));
268 		for (i = 0; i < len; i++)
269 			DPRINTF((" %02x", ((u_char *)data)[i]));
270 		DPRINTF(("\n"));
271 	}
272 #endif
273 
274 	mutex_enter(&sc->sc_lock);
275 	(void)b_to_q(data, len, &sc->sc_q);
276 
277 	if (sc->sc_state & UHID_ASLP) {
278 		sc->sc_state &= ~UHID_ASLP;
279 		DPRINTFN(5, ("uhid_intr: waking %p\n", &sc->sc_q));
280 		cv_broadcast(&sc->sc_cv);
281 	}
282 	selnotify(&sc->sc_rsel, 0, 0);
283 	if (sc->sc_async != NULL) {
284 		DPRINTFN(3, ("uhid_intr: sending SIGIO %p\n", sc->sc_async));
285 		softint_schedule(sc->sc_sih);
286 	}
287 	mutex_exit(&sc->sc_lock);
288 }
289 
290 void
291 uhid_softintr(void *cookie)
292 {
293 	struct uhid_softc *sc;
294 
295 	sc = cookie;
296 
297 	mutex_enter(proc_lock);
298 	if (sc->sc_async != NULL)
299 		 psignal(sc->sc_async, SIGIO);
300 	mutex_exit(proc_lock);
301 }
302 
303 int
304 uhidopen(dev_t dev, int flag, int mode, struct lwp *l)
305 {
306 	struct uhid_softc *sc;
307 	int error;
308 
309 	sc = device_lookup_private(&uhid_cd, UHIDUNIT(dev));
310 	if (sc == NULL)
311 		return ENXIO;
312 
313 	DPRINTF(("uhidopen: sc=%p\n", sc));
314 
315 	if (sc->sc_dying)
316 		return (ENXIO);
317 
318 	mutex_enter(&sc->sc_access_lock);
319 	error = uhidev_open(&sc->sc_hdev);
320 	if (error) {
321 		mutex_exit(&sc->sc_access_lock);
322 		return (error);
323 	}
324 	mutex_exit(&sc->sc_access_lock);
325 
326 	if (clalloc(&sc->sc_q, UHID_BSIZE, 0) == -1) {
327 		mutex_enter(&sc->sc_access_lock);
328 		uhidev_close(&sc->sc_hdev);
329 		mutex_exit(&sc->sc_access_lock);
330 		return (ENOMEM);
331 	}
332 	sc->sc_obuf = malloc(sc->sc_osize, M_USBDEV, M_WAITOK);
333 	sc->sc_state &= ~UHID_IMMED;
334 
335 	mutex_enter(proc_lock);
336 	sc->sc_async = NULL;
337 	mutex_exit(proc_lock);
338 
339 	return (0);
340 }
341 
342 int
343 uhidclose(dev_t dev, int flag, int mode, struct lwp *l)
344 {
345 	struct uhid_softc *sc;
346 
347 	sc = device_lookup_private(&uhid_cd, UHIDUNIT(dev));
348 
349 	DPRINTF(("uhidclose: sc=%p\n", sc));
350 
351 	clfree(&sc->sc_q);
352 	free(sc->sc_obuf, M_USBDEV);
353 
354 	mutex_enter(proc_lock);
355 	sc->sc_async = NULL;
356 	mutex_exit(proc_lock);
357 
358 	mutex_enter(&sc->sc_access_lock);
359 	uhidev_close(&sc->sc_hdev);
360 	mutex_exit(&sc->sc_access_lock);
361 
362 	return (0);
363 }
364 
365 int
366 uhid_do_read(struct uhid_softc *sc, struct uio *uio, int flag)
367 {
368 	int error = 0;
369 	int extra;
370 	size_t length;
371 	u_char buffer[UHID_CHUNK];
372 	usbd_status err;
373 
374 	DPRINTFN(1, ("uhidread\n"));
375 	if (sc->sc_state & UHID_IMMED) {
376 		DPRINTFN(1, ("uhidread immed\n"));
377 		extra = sc->sc_hdev.sc_report_id != 0;
378 		err = uhidev_get_report(&sc->sc_hdev, UHID_INPUT_REPORT,
379 					buffer, sc->sc_isize + extra);
380 		if (err)
381 			return (EIO);
382 		return (uiomove(buffer+extra, sc->sc_isize, uio));
383 	}
384 
385 	mutex_enter(&sc->sc_lock);
386 	while (sc->sc_q.c_cc == 0) {
387 		if (flag & IO_NDELAY) {
388 			mutex_exit(&sc->sc_lock);
389 			return (EWOULDBLOCK);
390 		}
391 		sc->sc_state |= UHID_ASLP;
392 		DPRINTFN(5, ("uhidread: sleep on %p\n", &sc->sc_q));
393 		error = cv_wait_sig(&sc->sc_cv, &sc->sc_lock);
394 		DPRINTFN(5, ("uhidread: woke, error=%d\n", error));
395 		if (sc->sc_dying)
396 			error = EIO;
397 		if (error) {
398 			sc->sc_state &= ~UHID_ASLP;
399 			break;
400 		}
401 	}
402 	mutex_exit(&sc->sc_lock);
403 
404 	/* Transfer as many chunks as possible. */
405 	while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0 && !error) {
406 		length = min(sc->sc_q.c_cc, uio->uio_resid);
407 		if (length > sizeof(buffer))
408 			length = sizeof(buffer);
409 
410 		/* Remove a small chunk from the input queue. */
411 		(void) q_to_b(&sc->sc_q, buffer, length);
412 		DPRINTFN(5, ("uhidread: got %lu chars\n", (u_long)length));
413 
414 		/* Copy the data to the user process. */
415 		if ((error = uiomove(buffer, length, uio)) != 0)
416 			break;
417 	}
418 
419 	return (error);
420 }
421 
422 int
423 uhidread(dev_t dev, struct uio *uio, int flag)
424 {
425 	struct uhid_softc *sc;
426 	int error;
427 
428 	sc = device_lookup_private(&uhid_cd, UHIDUNIT(dev));
429 
430 	mutex_enter(&sc->sc_lock);
431 	sc->sc_refcnt++;
432 	mutex_exit(&sc->sc_lock);
433 
434 	mutex_enter(&sc->sc_access_lock);
435 	error = uhid_do_read(sc, uio, flag);
436 	mutex_exit(&sc->sc_access_lock);
437 
438 	mutex_enter(&sc->sc_lock);
439 	if (--sc->sc_refcnt < 0)
440 		usb_detach_broadcast(sc->sc_hdev.sc_dev, &sc->sc_detach_cv);
441 	mutex_exit(&sc->sc_lock);
442 	return (error);
443 }
444 
445 int
446 uhid_do_write(struct uhid_softc *sc, struct uio *uio, int flag)
447 {
448 	int error;
449 	int size;
450 	usbd_status err;
451 
452 	DPRINTFN(1, ("uhidwrite\n"));
453 
454 	if (sc->sc_dying)
455 		return (EIO);
456 
457 	size = sc->sc_osize;
458 	error = 0;
459 	if (uio->uio_resid != size)
460 		return (EINVAL);
461 	error = uiomove(sc->sc_obuf, size, uio);
462 	if (!error) {
463 		err = uhidev_set_report(&sc->sc_hdev, UHID_OUTPUT_REPORT,
464 					sc->sc_obuf, size);
465 		if (err)
466 			error = EIO;
467 	}
468 
469 	return (error);
470 }
471 
472 int
473 uhidwrite(dev_t dev, struct uio *uio, int flag)
474 {
475 	struct uhid_softc *sc;
476 	int error;
477 
478 	sc = device_lookup_private(&uhid_cd, UHIDUNIT(dev));
479 
480 	mutex_enter(&sc->sc_lock);
481 	sc->sc_refcnt++;
482 	mutex_exit(&sc->sc_lock);
483 
484 	mutex_enter(&sc->sc_access_lock);
485 	error = uhid_do_write(sc, uio, flag);
486 	mutex_exit(&sc->sc_access_lock);
487 
488 	mutex_enter(&sc->sc_lock);
489 	if (--sc->sc_refcnt < 0)
490 		usb_detach_broadcast(sc->sc_hdev.sc_dev, &sc->sc_detach_cv);
491 	mutex_exit(&sc->sc_lock);
492 	return (error);
493 }
494 
495 int
496 uhid_do_ioctl(struct uhid_softc *sc, u_long cmd, void *addr,
497     int flag, struct lwp *l)
498 {
499 	struct usb_ctl_report_desc *rd;
500 	struct usb_ctl_report *re;
501 	u_char buffer[UHID_CHUNK];
502 	int size, extra;
503 	usbd_status err;
504 	void *desc;
505 
506 	DPRINTFN(2, ("uhidioctl: cmd=%lx\n", cmd));
507 
508 	if (sc->sc_dying)
509 		return (EIO);
510 
511 	switch (cmd) {
512 	case FIONBIO:
513 		/* All handled in the upper FS layer. */
514 		break;
515 
516 	case FIOASYNC:
517 		mutex_enter(proc_lock);
518 		if (*(int *)addr) {
519 			if (sc->sc_async != NULL)
520 				return (EBUSY);
521 			sc->sc_async = l->l_proc;
522 			DPRINTF(("uhid_do_ioctl: FIOASYNC %p\n", l->l_proc));
523 		} else
524 			sc->sc_async = NULL;
525 		mutex_exit(proc_lock);
526 		break;
527 
528 	/* XXX this is not the most general solution. */
529 	case TIOCSPGRP:
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 			mutex_exit(proc_lock);
537 			return (EPERM);
538 		}
539 		mutex_exit(proc_lock);
540 		break;
541 
542 	case FIOSETOWN:
543 		mutex_enter(proc_lock);
544 		if (sc->sc_async == NULL) {
545 			mutex_exit(proc_lock);
546 			return (EINVAL);
547 		}
548 		if (-*(int *)addr != sc->sc_async->p_pgid
549 		    && *(int *)addr != sc->sc_async->p_pid) {
550 			mutex_exit(proc_lock);
551 			return (EPERM);
552 		}
553 		mutex_exit(proc_lock);
554 		break;
555 
556 	case USB_GET_REPORT_DESC:
557 		uhidev_get_report_desc(sc->sc_hdev.sc_parent, &desc, &size);
558 		rd = (struct usb_ctl_report_desc *)addr;
559 		size = min(size, sizeof rd->ucrd_data);
560 		rd->ucrd_size = size;
561 		memcpy(rd->ucrd_data, desc, size);
562 		break;
563 
564 	case USB_SET_IMMED:
565 		if (*(int *)addr) {
566 			extra = sc->sc_hdev.sc_report_id != 0;
567 			err = uhidev_get_report(&sc->sc_hdev, UHID_INPUT_REPORT,
568 						buffer, sc->sc_isize + extra);
569 			if (err)
570 				return (EOPNOTSUPP);
571 
572 			sc->sc_state |=  UHID_IMMED;
573 		} else
574 			sc->sc_state &= ~UHID_IMMED;
575 		break;
576 
577 	case USB_GET_REPORT:
578 		re = (struct usb_ctl_report *)addr;
579 		switch (re->ucr_report) {
580 		case UHID_INPUT_REPORT:
581 			size = sc->sc_isize;
582 			break;
583 		case UHID_OUTPUT_REPORT:
584 			size = sc->sc_osize;
585 			break;
586 		case UHID_FEATURE_REPORT:
587 			size = sc->sc_fsize;
588 			break;
589 		default:
590 			return (EINVAL);
591 		}
592 		extra = sc->sc_hdev.sc_report_id != 0;
593 		err = uhidev_get_report(&sc->sc_hdev, re->ucr_report,
594 		    re->ucr_data, size + extra);
595 		if (extra)
596 			memcpy(re->ucr_data, re->ucr_data+1, size);
597 		if (err)
598 			return (EIO);
599 		break;
600 
601 	case USB_SET_REPORT:
602 		re = (struct usb_ctl_report *)addr;
603 		switch (re->ucr_report) {
604 		case UHID_INPUT_REPORT:
605 			size = sc->sc_isize;
606 			break;
607 		case UHID_OUTPUT_REPORT:
608 			size = sc->sc_osize;
609 			break;
610 		case UHID_FEATURE_REPORT:
611 			size = sc->sc_fsize;
612 			break;
613 		default:
614 			return (EINVAL);
615 		}
616 		err = uhidev_set_report(&sc->sc_hdev, re->ucr_report,
617 		    re->ucr_data, size);
618 		if (err)
619 			return (EIO);
620 		break;
621 
622 	case USB_GET_REPORT_ID:
623 		*(int *)addr = sc->sc_hdev.sc_report_id;
624 		break;
625 
626 	case USB_GET_DEVICE_DESC:
627 		*(usb_device_descriptor_t *)addr =
628 			*usbd_get_device_descriptor(sc->sc_hdev.sc_parent->sc_udev);
629 		break;
630 
631 	case USB_GET_DEVICEINFO:
632 		usbd_fill_deviceinfo(sc->sc_hdev.sc_parent->sc_udev,
633 			             (struct usb_device_info *)addr, 0);
634 		break;
635 #ifdef COMPAT_30
636 	case USB_GET_DEVICEINFO_OLD:
637 		usbd_fill_deviceinfo_old(sc->sc_hdev.sc_parent->sc_udev,
638 					 (struct usb_device_info_old *)addr, 0);
639 
640 		break;
641 #endif
642         case USB_GET_STRING_DESC:
643 	    {
644                 struct usb_string_desc *si = (struct usb_string_desc *)addr;
645                 err = usbd_get_string_desc(sc->sc_hdev.sc_parent->sc_udev,
646 			si->usd_string_index,
647                 	si->usd_language_id, &si->usd_desc, &size);
648                 if (err)
649                         return (EINVAL);
650                 break;
651 	    }
652 
653 	default:
654 		return (EINVAL);
655 	}
656 	return (0);
657 }
658 
659 int
660 uhidioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
661 {
662 	struct uhid_softc *sc;
663 	int error;
664 
665 	sc = device_lookup_private(&uhid_cd, UHIDUNIT(dev));
666 	if (sc == NULL)
667 		return ENXIO;
668 
669 	if (sc->sc_dying)
670 		return EIO;
671 
672 	mutex_enter(&sc->sc_lock);
673 	sc->sc_refcnt++;
674 	mutex_exit(&sc->sc_lock);
675 
676 	mutex_enter(&sc->sc_access_lock);
677 	error = uhid_do_ioctl(sc, cmd, addr, flag, l);
678 	mutex_exit(&sc->sc_access_lock);
679 
680 	mutex_enter(&sc->sc_lock);
681 	if (--sc->sc_refcnt < 0)
682 		usb_detach_broadcast(sc->sc_hdev.sc_dev, &sc->sc_detach_cv);
683 	mutex_exit(&sc->sc_lock);
684 	return (error);
685 }
686 
687 int
688 uhidpoll(dev_t dev, int events, struct lwp *l)
689 {
690 	struct uhid_softc *sc;
691 	int revents = 0;
692 
693 	sc = device_lookup_private(&uhid_cd, UHIDUNIT(dev));
694 	if (sc == NULL)
695 		return ENXIO;
696 
697 	if (sc->sc_dying)
698 		return EIO;
699 
700 	mutex_enter(&sc->sc_lock);
701 	if (events & (POLLOUT | POLLWRNORM))
702 		revents |= events & (POLLOUT | POLLWRNORM);
703 	if (events & (POLLIN | POLLRDNORM)) {
704 		if (sc->sc_q.c_cc > 0)
705 			revents |= events & (POLLIN | POLLRDNORM);
706 		else
707 			selrecord(l, &sc->sc_rsel);
708 	}
709 	mutex_exit(&sc->sc_lock);
710 
711 	return (revents);
712 }
713 
714 static void
715 filt_uhidrdetach(struct knote *kn)
716 {
717 	struct uhid_softc *sc = kn->kn_hook;
718 
719 	mutex_enter(&sc->sc_lock);
720 	SLIST_REMOVE(&sc->sc_rsel.sel_klist, kn, knote, kn_selnext);
721 	mutex_exit(&sc->sc_lock);
722 }
723 
724 static int
725 filt_uhidread(struct knote *kn, long hint)
726 {
727 	struct uhid_softc *sc = kn->kn_hook;
728 
729 	kn->kn_data = sc->sc_q.c_cc;
730 	return (kn->kn_data > 0);
731 }
732 
733 static const struct filterops uhidread_filtops =
734 	{ 1, NULL, filt_uhidrdetach, filt_uhidread };
735 
736 static const struct filterops uhid_seltrue_filtops =
737 	{ 1, NULL, filt_uhidrdetach, filt_seltrue };
738 
739 int
740 uhidkqfilter(dev_t dev, struct knote *kn)
741 {
742 	struct uhid_softc *sc;
743 	struct klist *klist;
744 
745 	sc = device_lookup_private(&uhid_cd, UHIDUNIT(dev));
746 
747 	if (sc->sc_dying)
748 		return (ENXIO);
749 
750 	switch (kn->kn_filter) {
751 	case EVFILT_READ:
752 		klist = &sc->sc_rsel.sel_klist;
753 		kn->kn_fop = &uhidread_filtops;
754 		break;
755 
756 	case EVFILT_WRITE:
757 		klist = &sc->sc_rsel.sel_klist;
758 		kn->kn_fop = &uhid_seltrue_filtops;
759 		break;
760 
761 	default:
762 		return (EINVAL);
763 	}
764 
765 	kn->kn_hook = sc;
766 
767 	mutex_enter(&sc->sc_lock);
768 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
769 	mutex_exit(&sc->sc_lock);
770 
771 	return (0);
772 }
773