xref: /openbsd-src/sys/dev/usb/uhid.c (revision 3a3fbb3f2e2521ab7c4a56b7ff7462ebd9095ec5)
1 /*	$OpenBSD: uhid.c,v 1.15 2001/10/31 04:24:44 nate Exp $ */
2 /*	$NetBSD: uhid.c,v 1.45 2001/10/26 17:58:21 augustss Exp $	*/
3 /*	$FreeBSD: src/sys/dev/usb/uhid.c,v 1.22 1999/11/17 22:33:43 n_hibma Exp $	*/
4 
5 /*
6  * Copyright (c) 1998 The NetBSD Foundation, Inc.
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to The NetBSD Foundation
10  * by Lennart Augustsson (lennart@augustsson.net) at
11  * Carlstedt Research & Technology.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *        This product includes software developed by the NetBSD
24  *        Foundation, Inc. and its contributors.
25  * 4. Neither the name of The NetBSD Foundation nor the names of its
26  *    contributors may be used to endorse or promote products derived
27  *    from this software without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
30  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
33  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39  * POSSIBILITY OF SUCH DAMAGE.
40  */
41 
42 /*
43  * HID spec: http://www.usb.org/developers/data/devclass/hid1_1.pdf
44  */
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #include <sys/signalvar.h>
51 #if defined(__NetBSD__) || defined(__OpenBSD__)
52 #include <sys/device.h>
53 #include <sys/ioctl.h>
54 #elif defined(__FreeBSD__)
55 #include <sys/ioccom.h>
56 #include <sys/filio.h>
57 #include <sys/module.h>
58 #include <sys/bus.h>
59 #include <sys/ioccom.h>
60 #endif
61 #include <sys/conf.h>
62 #include <sys/tty.h>
63 #include <sys/file.h>
64 #include <sys/select.h>
65 #include <sys/proc.h>
66 #include <sys/vnode.h>
67 #include <sys/poll.h>
68 
69 #include <dev/usb/usb.h>
70 #include <dev/usb/usbhid.h>
71 
72 #include <dev/usb/usbdevs.h>
73 #include <dev/usb/usbdi.h>
74 #include <dev/usb/usbdi_util.h>
75 #include <dev/usb/hid.h>
76 #include <dev/usb/usb_quirks.h>
77 
78 /* Report descriptor for broken Wacom Graphire */
79 #include <dev/usb/ugraphire_rdesc.h>
80 
81 #ifdef UHID_DEBUG
82 #define DPRINTF(x)	if (uhiddebug) logprintf x
83 #define DPRINTFN(n,x)	if (uhiddebug>(n)) logprintf x
84 int	uhiddebug = 0;
85 #else
86 #define DPRINTF(x)
87 #define DPRINTFN(n,x)
88 #endif
89 
90 struct uhid_softc {
91 	USBBASEDEVICE sc_dev;			/* base device */
92 	usbd_device_handle sc_udev;
93 	usbd_interface_handle sc_iface;	/* interface */
94 	usbd_pipe_handle sc_intrpipe;	/* interrupt pipe */
95 	int sc_ep_addr;
96 
97 	int sc_isize;
98 	int sc_osize;
99 	int sc_fsize;
100 	u_int8_t sc_iid;
101 	u_int8_t sc_oid;
102 	u_int8_t sc_fid;
103 
104 	u_char *sc_ibuf;
105 	u_char *sc_obuf;
106 
107 	void *sc_repdesc;
108 	int sc_repdesc_size;
109 
110 	struct clist sc_q;
111 	struct selinfo sc_rsel;
112 	struct proc *sc_async;	/* process that wants SIGIO */
113 	u_char sc_state;	/* driver state */
114 #define	UHID_OPEN	0x01	/* device is open */
115 #define	UHID_ASLP	0x02	/* waiting for device data */
116 #define UHID_NEEDCLEAR	0x04	/* needs clearing endpoint stall */
117 #define UHID_IMMED	0x08	/* return read data immediately */
118 
119 	int sc_refcnt;
120 	u_char sc_dying;
121 };
122 
123 #define	UHIDUNIT(dev)	(minor(dev))
124 #define	UHID_CHUNK	128	/* chunk size for read */
125 #define	UHID_BSIZE	1020	/* buffer size */
126 
127 #if defined(__NetBSD__) || defined(__OpenBSD__)
128 cdev_decl(uhid);
129 #elif defined(__FreeBSD__)
130 d_open_t	uhidopen;
131 d_close_t	uhidclose;
132 d_read_t	uhidread;
133 d_write_t	uhidwrite;
134 d_ioctl_t	uhidioctl;
135 d_poll_t	uhidpoll;
136 
137 #define		UHID_CDEV_MAJOR 122
138 
139 Static struct cdevsw uhid_cdevsw = {
140 	/* open */	uhidopen,
141 	/* close */	uhidclose,
142 	/* read */	uhidread,
143 	/* write */	uhidwrite,
144 	/* ioctl */	uhidioctl,
145 	/* poll */	uhidpoll,
146 	/* mmap */	nommap,
147 	/* strategy */	nostrategy,
148 	/* name */	"uhid",
149 	/* maj */	UHID_CDEV_MAJOR,
150 	/* dump */	nodump,
151 	/* psize */	nopsize,
152 	/* flags */	0,
153 	/* bmaj */	-1
154 };
155 #endif
156 
157 Static void uhid_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
158 
159 Static int uhid_do_read(struct uhid_softc *, struct uio *uio, int);
160 Static int uhid_do_write(struct uhid_softc *, struct uio *uio, int);
161 Static int uhid_do_ioctl(struct uhid_softc*, u_long, caddr_t, int,struct proc*);
162 
163 USB_DECLARE_DRIVER(uhid);
164 
165 USB_MATCH(uhid)
166 {
167 	USB_MATCH_START(uhid, uaa);
168 	usb_interface_descriptor_t *id;
169 
170 	if (uaa->iface == NULL)
171 		return (UMATCH_NONE);
172 	id = usbd_get_interface_descriptor(uaa->iface);
173 	if (id == NULL || id->bInterfaceClass != UICLASS_HID)
174 		return (UMATCH_NONE);
175 	if (uaa->matchlvl)
176 		return (uaa->matchlvl);
177 	return (UMATCH_IFACECLASS_GENERIC);
178 }
179 
180 USB_ATTACH(uhid)
181 {
182 	USB_ATTACH_START(uhid, sc, uaa);
183 	usbd_interface_handle iface = uaa->iface;
184 	usb_interface_descriptor_t *id;
185 	usb_endpoint_descriptor_t *ed;
186 	int size;
187 	void *desc;
188 	usbd_status err;
189 	char devinfo[1024];
190 
191 	sc->sc_udev = uaa->device;
192 	sc->sc_iface = iface;
193 	id = usbd_get_interface_descriptor(iface);
194 	usbd_devinfo(uaa->device, 0, devinfo);
195 	USB_ATTACH_SETUP;
196 	printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
197 	       devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
198 
199 	ed = usbd_interface2endpoint_descriptor(iface, 0);
200 	if (ed == NULL) {
201 		printf("%s: could not read endpoint descriptor\n",
202 		       USBDEVNAME(sc->sc_dev));
203 		sc->sc_dying = 1;
204 		USB_ATTACH_ERROR_RETURN;
205 	}
206 
207 	DPRINTFN(10,("uhid_attach: bLength=%d bDescriptorType=%d "
208 		     "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d"
209 		     " bInterval=%d\n",
210 		     ed->bLength, ed->bDescriptorType,
211 		     ed->bEndpointAddress & UE_ADDR,
212 		     UE_GET_DIR(ed->bEndpointAddress)==UE_DIR_IN? "in" : "out",
213 		     ed->bmAttributes & UE_XFERTYPE,
214 		     UGETW(ed->wMaxPacketSize), ed->bInterval));
215 
216 	if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_IN ||
217 	    (ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
218 		printf("%s: unexpected endpoint\n", USBDEVNAME(sc->sc_dev));
219 		sc->sc_dying = 1;
220 		USB_ATTACH_ERROR_RETURN;
221 	}
222 
223 	sc->sc_ep_addr = ed->bEndpointAddress;
224 
225 	if (uaa->vendor == USB_VENDOR_WACOM &&
226 	    uaa->product == USB_PRODUCT_WACOM_GRAPHIRE /* &&
227 	    uaa->revision == 0x???? */) { /* XXX should use revision */
228 		/* The report descriptor for the Wacom Graphire is broken. */
229 		size = sizeof uhid_graphire_report_descr;
230 		desc = malloc(size, M_USBDEV, M_NOWAIT);
231 		if (desc == NULL)
232 			err = USBD_NOMEM;
233 		else {
234 			err = USBD_NORMAL_COMPLETION;
235 			memcpy(desc, uhid_graphire_report_descr, size);
236 		}
237 	} else {
238 		desc = NULL;
239 		err = usbd_read_report_desc(uaa->iface, &desc, &size,M_USBDEV);
240 	}
241 	if (err) {
242 		printf("%s: no report descriptor\n", USBDEVNAME(sc->sc_dev));
243 		sc->sc_dying = 1;
244 		USB_ATTACH_ERROR_RETURN;
245 	}
246 
247 	(void)usbd_set_idle(iface, 0, 0);
248 
249 	sc->sc_isize = hid_report_size(desc, size, hid_input,   &sc->sc_iid);
250 	sc->sc_osize = hid_report_size(desc, size, hid_output,  &sc->sc_oid);
251 	sc->sc_fsize = hid_report_size(desc, size, hid_feature, &sc->sc_fid);
252 
253 	sc->sc_repdesc = desc;
254 	sc->sc_repdesc_size = size;
255 
256 #ifdef __FreeBSD__
257 	{
258 		static int global_init_done = 0;
259 
260 		if (!global_init_done) {
261 			cdevsw_add(&uhid_cdevsw);
262 			global_init_done = 1;
263 		}
264 	}
265 #endif
266 
267 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
268 			   USBDEV(sc->sc_dev));
269 
270 	USB_ATTACH_SUCCESS_RETURN;
271 }
272 
273 #if defined(__NetBSD__) || defined(__OpenBSD__)
274 int
275 uhid_activate(device_ptr_t self, enum devact act)
276 {
277 	struct uhid_softc *sc = (struct uhid_softc *)self;
278 
279 	switch (act) {
280 	case DVACT_ACTIVATE:
281 		return (EOPNOTSUPP);
282 		break;
283 
284 	case DVACT_DEACTIVATE:
285 		sc->sc_dying = 1;
286 		break;
287 	}
288 	return (0);
289 }
290 #endif
291 
292 USB_DETACH(uhid)
293 {
294 	USB_DETACH_START(uhid, sc);
295 	int s;
296 #if defined(__NetBSD__) || defined(__OpenBSD__)
297 	int maj, mn;
298 
299 	DPRINTF(("uhid_detach: sc=%p flags=%d\n", sc, flags));
300 #else
301 	DPRINTF(("uhid_detach: sc=%p\n", sc));
302 #endif
303 
304 	sc->sc_dying = 1;
305 	if (sc->sc_intrpipe != NULL)
306 		usbd_abort_pipe(sc->sc_intrpipe);
307 
308 	if (sc->sc_state & UHID_OPEN) {
309 		s = splusb();
310 		if (--sc->sc_refcnt >= 0) {
311 			/* Wake everyone */
312 			wakeup(&sc->sc_q);
313 			/* Wait for processes to go away. */
314 			usb_detach_wait(USBDEV(sc->sc_dev));
315 		}
316 		splx(s);
317 	}
318 
319 #if defined(__NetBSD__) || defined(__OpenBSD__)
320 	/* locate the major number */
321 	for (maj = 0; maj < nchrdev; maj++)
322 		if (cdevsw[maj].d_open == uhidopen)
323 			break;
324 
325 	/* Nuke the vnodes for any open instances (calls close). */
326 	mn = self->dv_unit;
327 	vdevgone(maj, mn, mn, VCHR);
328 #elif defined(__FreeBSD__)
329 	/* XXX not implemented yet */
330 #endif
331 
332 	if (sc->sc_repdesc)
333 		free(sc->sc_repdesc, M_USBDEV);
334 
335 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
336 			   USBDEV(sc->sc_dev));
337 
338 	return (0);
339 }
340 
341 void
342 uhid_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
343 {
344 	struct uhid_softc *sc = addr;
345 
346 #ifdef UHID_DEBUG
347 	if (uhiddebug > 5) {
348 		u_int32_t cc, i;
349 
350 		usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
351 		DPRINTF(("uhid_intr: status=%d cc=%d\n", status, cc));
352 		DPRINTF(("uhid_intr: data ="));
353 		for (i = 0; i < cc; i++)
354 			DPRINTF((" %02x", sc->sc_ibuf[i]));
355 		DPRINTF(("\n"));
356 	}
357 #endif
358 
359 	if (status == USBD_CANCELLED)
360 		return;
361 
362 	if (status != USBD_NORMAL_COMPLETION) {
363 		DPRINTF(("uhid_intr: status=%d\n", status));
364 		sc->sc_state |= UHID_NEEDCLEAR;
365 		return;
366 	}
367 
368 	(void) b_to_q(sc->sc_ibuf, sc->sc_isize, &sc->sc_q);
369 
370 	if (sc->sc_state & UHID_ASLP) {
371 		sc->sc_state &= ~UHID_ASLP;
372 		DPRINTFN(5, ("uhid_intr: waking %p\n", &sc->sc_q));
373 		wakeup(&sc->sc_q);
374 	}
375 	selwakeup(&sc->sc_rsel);
376 	if (sc->sc_async != NULL) {
377 		DPRINTFN(3, ("uhid_intr: sending SIGIO %p\n", sc->sc_async));
378 		psignal(sc->sc_async, SIGIO);
379 	}
380 }
381 
382 int
383 uhidopen(dev_t dev, int flag, int mode, struct proc *p)
384 {
385 	struct uhid_softc *sc;
386 	usbd_status err;
387 
388 	USB_GET_SC_OPEN(uhid, UHIDUNIT(dev), sc);
389 
390 	DPRINTF(("uhidopen: sc=%p\n", sc));
391 
392 	if (sc->sc_dying)
393 		return (ENXIO);
394 
395 	if (sc->sc_state & UHID_OPEN)
396 		return (EBUSY);
397 	sc->sc_state |= UHID_OPEN;
398 
399 	if (clalloc(&sc->sc_q, UHID_BSIZE, 0) == -1) {
400 		sc->sc_state &= ~UHID_OPEN;
401 		return (ENOMEM);
402 	}
403 
404 	sc->sc_ibuf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
405 	sc->sc_obuf = malloc(sc->sc_osize, M_USBDEV, M_WAITOK);
406 
407 	/* Set up interrupt pipe. */
408 	err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
409 		  USBD_SHORT_XFER_OK, &sc->sc_intrpipe, sc, sc->sc_ibuf,
410 		  sc->sc_isize, uhid_intr, USBD_DEFAULT_INTERVAL);
411 	if (err) {
412 		DPRINTF(("uhidopen: usbd_open_pipe_intr failed, "
413 			 "error=%d\n",err));
414 		free(sc->sc_ibuf, M_USBDEV);
415 		free(sc->sc_obuf, M_USBDEV);
416 		sc->sc_state &= ~UHID_OPEN;
417 		return (EIO);
418 	}
419 
420 	sc->sc_state &= ~UHID_IMMED;
421 
422 	sc->sc_async = 0;
423 
424 	return (0);
425 }
426 
427 int
428 uhidclose(dev_t dev, int flag, int mode, struct proc *p)
429 {
430 	struct uhid_softc *sc;
431 
432 	USB_GET_SC(uhid, UHIDUNIT(dev), sc);
433 
434 	DPRINTF(("uhidclose: sc=%p\n", sc));
435 
436 	/* Disable interrupts. */
437 	usbd_abort_pipe(sc->sc_intrpipe);
438 	usbd_close_pipe(sc->sc_intrpipe);
439 	sc->sc_intrpipe = 0;
440 
441 	clfree(&sc->sc_q);
442 
443 	free(sc->sc_ibuf, M_USBDEV);
444 	free(sc->sc_obuf, M_USBDEV);
445 
446 	sc->sc_state &= ~UHID_OPEN;
447 
448 	sc->sc_async = 0;
449 
450 	return (0);
451 }
452 
453 int
454 uhid_do_read(struct uhid_softc *sc, struct uio *uio, int flag)
455 {
456 	int s;
457 	int error = 0;
458 	size_t length;
459 	u_char buffer[UHID_CHUNK];
460 	usbd_status err;
461 
462 	DPRINTFN(1, ("uhidread\n"));
463 	if (sc->sc_state & UHID_IMMED) {
464 		DPRINTFN(1, ("uhidread immed\n"));
465 
466 		err = usbd_get_report(sc->sc_iface, UHID_INPUT_REPORT,
467 				    sc->sc_iid, buffer, sc->sc_isize);
468 		if (err)
469 			return (EIO);
470 		return (uiomove(buffer, sc->sc_isize, uio));
471 	}
472 
473 	s = splusb();
474 	while (sc->sc_q.c_cc == 0) {
475 		if (flag & IO_NDELAY) {
476 			splx(s);
477 			return (EWOULDBLOCK);
478 		}
479 		sc->sc_state |= UHID_ASLP;
480 		DPRINTFN(5, ("uhidread: sleep on %p\n", &sc->sc_q));
481 		error = tsleep(&sc->sc_q, PZERO | PCATCH, "uhidrea", 0);
482 		DPRINTFN(5, ("uhidread: woke, error=%d\n", error));
483 		if (sc->sc_dying)
484 			error = EIO;
485 		if (error) {
486 			sc->sc_state &= ~UHID_ASLP;
487 			break;
488 		}
489 		if (sc->sc_state & UHID_NEEDCLEAR) {
490 			DPRINTFN(-1,("uhidread: clearing stall\n"));
491 			sc->sc_state &= ~UHID_NEEDCLEAR;
492 			usbd_clear_endpoint_stall(sc->sc_intrpipe);
493 		}
494 	}
495 	splx(s);
496 
497 	/* Transfer as many chunks as possible. */
498 	while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0 && !error) {
499 		length = min(sc->sc_q.c_cc, uio->uio_resid);
500 		if (length > sizeof(buffer))
501 			length = sizeof(buffer);
502 
503 		/* Remove a small chunk from the input queue. */
504 		(void) q_to_b(&sc->sc_q, buffer, length);
505 		DPRINTFN(5, ("uhidread: got %lu chars\n", (u_long)length));
506 
507 		/* Copy the data to the user process. */
508 		if ((error = uiomove(buffer, length, uio)) != 0)
509 			break;
510 	}
511 
512 	return (error);
513 }
514 
515 int
516 uhidread(dev_t dev, struct uio *uio, int flag)
517 {
518 	struct uhid_softc *sc;
519 	int error;
520 
521 	USB_GET_SC(uhid, UHIDUNIT(dev), sc);
522 
523 	sc->sc_refcnt++;
524 	error = uhid_do_read(sc, uio, flag);
525 	if (--sc->sc_refcnt < 0)
526 		usb_detach_wakeup(USBDEV(sc->sc_dev));
527 	return (error);
528 }
529 
530 int
531 uhid_do_write(struct uhid_softc *sc, struct uio *uio, int flag)
532 {
533 	int error;
534 	int size;
535 	usbd_status err;
536 
537 	DPRINTFN(1, ("uhidwrite\n"));
538 
539 	if (sc->sc_dying)
540 		return (EIO);
541 
542 	size = sc->sc_osize;
543 	error = 0;
544 	if (uio->uio_resid != size)
545 		return (EINVAL);
546 	error = uiomove(sc->sc_obuf, size, uio);
547 	if (!error) {
548 		if (sc->sc_oid)
549 			err = usbd_set_report(sc->sc_iface, UHID_OUTPUT_REPORT,
550 				  sc->sc_obuf[0], sc->sc_obuf+1, size-1);
551 		else
552 			err = usbd_set_report(sc->sc_iface, UHID_OUTPUT_REPORT,
553 				  0, sc->sc_obuf, size);
554 		if (err)
555 			error = EIO;
556 	}
557 
558 	return (error);
559 }
560 
561 int
562 uhidwrite(dev_t dev, struct uio *uio, int flag)
563 {
564 	struct uhid_softc *sc;
565 	int error;
566 
567 	USB_GET_SC(uhid, UHIDUNIT(dev), sc);
568 
569 	sc->sc_refcnt++;
570 	error = uhid_do_write(sc, uio, flag);
571 	if (--sc->sc_refcnt < 0)
572 		usb_detach_wakeup(USBDEV(sc->sc_dev));
573 	return (error);
574 }
575 
576 int
577 uhid_do_ioctl(struct uhid_softc *sc, u_long cmd, caddr_t addr,
578 	      int flag, struct proc *p)
579 {
580 	struct usb_ctl_report_desc *rd;
581 	struct usb_ctl_report *re;
582 	int size, id;
583 	usbd_status err;
584 
585 	DPRINTFN(2, ("uhidioctl: cmd=%lx\n", cmd));
586 
587 	if (sc->sc_dying)
588 		return (EIO);
589 
590 	switch (cmd) {
591 	case FIONBIO:
592 		/* All handled in the upper FS layer. */
593 		break;
594 
595 	case FIOASYNC:
596 		if (*(int *)addr) {
597 			if (sc->sc_async != NULL)
598 				return (EBUSY);
599 			sc->sc_async = p;
600 			DPRINTF(("uhid_do_ioctl: FIOASYNC %p\n", p));
601 		} else
602 			sc->sc_async = NULL;
603 		break;
604 
605 	/* XXX this is not the most general solution. */
606 	case TIOCSPGRP:
607 		if (sc->sc_async == NULL)
608 			return (EINVAL);
609 		if (*(int *)addr != sc->sc_async->p_pgid)
610 			return (EPERM);
611 		break;
612 
613 	case USB_GET_REPORT_DESC:
614 		rd = (struct usb_ctl_report_desc *)addr;
615 		size = min(sc->sc_repdesc_size, sizeof rd->data);
616 		rd->size = size;
617 		memcpy(rd->data, sc->sc_repdesc, size);
618 		break;
619 
620 	case USB_SET_IMMED:
621 		if (*(int *)addr) {
622 			/* XXX should read into ibuf, but does it matter? */
623 			err = usbd_get_report(sc->sc_iface, UHID_INPUT_REPORT,
624 				  sc->sc_iid, sc->sc_ibuf, sc->sc_isize);
625 			if (err)
626 				return (EOPNOTSUPP);
627 
628 			sc->sc_state |=  UHID_IMMED;
629 		} else
630 			sc->sc_state &= ~UHID_IMMED;
631 		break;
632 
633 	case USB_GET_REPORT:
634 		re = (struct usb_ctl_report *)addr;
635 		switch (re->report) {
636 		case UHID_INPUT_REPORT:
637 			size = sc->sc_isize;
638 			id = sc->sc_iid;
639 			break;
640 		case UHID_OUTPUT_REPORT:
641 			size = sc->sc_osize;
642 			id = sc->sc_oid;
643 			break;
644 		case UHID_FEATURE_REPORT:
645 			size = sc->sc_fsize;
646 			id = sc->sc_fid;
647 			break;
648 		default:
649 			return (EINVAL);
650 		}
651 		err = usbd_get_report(sc->sc_iface, re->report, id, re->data,
652 			  size);
653 		if (err)
654 			return (EIO);
655 		break;
656 
657 	case USB_SET_REPORT:
658 		re = (struct usb_ctl_report *)addr;
659 		switch (re->report) {
660 		case UHID_INPUT_REPORT:
661 			size = sc->sc_isize;
662 			id = sc->sc_iid;
663 			break;
664 		case UHID_OUTPUT_REPORT:
665 			size = sc->sc_osize;
666 			id = sc->sc_oid;
667 			break;
668 		case UHID_FEATURE_REPORT:
669 			size = sc->sc_fsize;
670 			id = sc->sc_fid;
671 			break;
672 		default:
673 			return (EINVAL);
674 		}
675 		err = usbd_set_report(sc->sc_iface, re->report, id, re->data,
676 			  size);
677 		if (err)
678 			return (EIO);
679 		break;
680 
681 	default:
682 		return (EINVAL);
683 	}
684 	return (0);
685 }
686 
687 int
688 uhidioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
689 {
690 	struct uhid_softc *sc;
691 	int error;
692 
693 	USB_GET_SC(uhid, UHIDUNIT(dev), sc);
694 
695 	sc->sc_refcnt++;
696 	error = uhid_do_ioctl(sc, cmd, addr, flag, p);
697 	if (--sc->sc_refcnt < 0)
698 		usb_detach_wakeup(USBDEV(sc->sc_dev));
699 	return (error);
700 }
701 
702 int
703 uhidpoll(dev_t dev, int events, struct proc *p)
704 {
705 	struct uhid_softc *sc;
706 	int revents = 0;
707 	int s;
708 
709 	USB_GET_SC(uhid, UHIDUNIT(dev), sc);
710 
711 	if (sc->sc_dying)
712 		return (EIO);
713 
714 	s = splusb();
715 	if (events & (POLLOUT | POLLWRNORM))
716 		revents |= events & (POLLOUT | POLLWRNORM);
717 	if (events & (POLLIN | POLLRDNORM)) {
718 		if (sc->sc_q.c_cc > 0)
719 			revents |= events & (POLLIN | POLLRDNORM);
720 		else
721 			selrecord(p, &sc->sc_rsel);
722 	}
723 
724 	splx(s);
725 	return (revents);
726 }
727 
728 #if defined(__FreeBSD__)
729 DRIVER_MODULE(uhid, uhub, uhid_driver, uhid_devclass, usbd_driver_load, 0);
730 #endif
731