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