xref: /openbsd-src/sys/dev/usb/uhid.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: uhid.c,v 1.14 2001/05/03 02:20:33 aaron Exp $ */
2 /*	$NetBSD: uhid.c,v 1.42 2000/12/29 01:47:49 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 	return (UMATCH_IFACECLASS_GENERIC);
176 }
177 
178 USB_ATTACH(uhid)
179 {
180 	USB_ATTACH_START(uhid, sc, uaa);
181 	usbd_interface_handle iface = uaa->iface;
182 	usb_interface_descriptor_t *id;
183 	usb_endpoint_descriptor_t *ed;
184 	int size;
185 	void *desc;
186 	usbd_status err;
187 	char devinfo[1024];
188 
189 	sc->sc_udev = uaa->device;
190 	sc->sc_iface = iface;
191 	id = usbd_get_interface_descriptor(iface);
192 	usbd_devinfo(uaa->device, 0, devinfo);
193 	USB_ATTACH_SETUP;
194 	printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
195 	       devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
196 
197 	ed = usbd_interface2endpoint_descriptor(iface, 0);
198 	if (ed == NULL) {
199 		printf("%s: could not read endpoint descriptor\n",
200 		       USBDEVNAME(sc->sc_dev));
201 		sc->sc_dying = 1;
202 		USB_ATTACH_ERROR_RETURN;
203 	}
204 
205 	DPRINTFN(10,("uhid_attach: bLength=%d bDescriptorType=%d "
206 		     "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d"
207 		     " bInterval=%d\n",
208 		     ed->bLength, ed->bDescriptorType,
209 		     ed->bEndpointAddress & UE_ADDR,
210 		     UE_GET_DIR(ed->bEndpointAddress)==UE_DIR_IN? "in" : "out",
211 		     ed->bmAttributes & UE_XFERTYPE,
212 		     UGETW(ed->wMaxPacketSize), ed->bInterval));
213 
214 	if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_IN ||
215 	    (ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
216 		printf("%s: unexpected endpoint\n", USBDEVNAME(sc->sc_dev));
217 		sc->sc_dying = 1;
218 		USB_ATTACH_ERROR_RETURN;
219 	}
220 
221 	sc->sc_ep_addr = ed->bEndpointAddress;
222 
223 	if (uaa->vendor == USB_VENDOR_WACOM &&
224 	    uaa->product == USB_PRODUCT_WACOM_GRAPHIRE /* &&
225 	    uaa->revision == 0x???? */) { /* XXX should use revision */
226 		/* The report descriptor for the Wacom Graphire is broken. */
227 		size = sizeof uhid_graphire_report_descr;
228 		desc = malloc(size, M_USBDEV, M_NOWAIT);
229 		if (desc == NULL)
230 			err = USBD_NOMEM;
231 		else {
232 			err = USBD_NORMAL_COMPLETION;
233 			memcpy(desc, uhid_graphire_report_descr, size);
234 		}
235 	} else {
236 		desc = NULL;
237 		err = usbd_alloc_report_desc(uaa->iface, &desc, &size,M_USBDEV);
238 	}
239 	if (err) {
240 		printf("%s: no report descriptor\n", USBDEVNAME(sc->sc_dev));
241 		sc->sc_dying = 1;
242 		USB_ATTACH_ERROR_RETURN;
243 	}
244 
245 	(void)usbd_set_idle(iface, 0, 0);
246 
247 	sc->sc_isize = hid_report_size(desc, size, hid_input,   &sc->sc_iid);
248 	sc->sc_osize = hid_report_size(desc, size, hid_output,  &sc->sc_oid);
249 	sc->sc_fsize = hid_report_size(desc, size, hid_feature, &sc->sc_fid);
250 
251 	sc->sc_repdesc = desc;
252 	sc->sc_repdesc_size = size;
253 
254 #ifdef __FreeBSD__
255 	{
256 		static int global_init_done = 0;
257 
258 		if (!global_init_done) {
259 			cdevsw_add(&uhid_cdevsw);
260 			global_init_done = 1;
261 		}
262 	}
263 #endif
264 
265 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
266 			   USBDEV(sc->sc_dev));
267 
268 	USB_ATTACH_SUCCESS_RETURN;
269 }
270 
271 #if defined(__NetBSD__) || defined(__OpenBSD__)
272 int
273 uhid_activate(device_ptr_t self, enum devact act)
274 {
275 	struct uhid_softc *sc = (struct uhid_softc *)self;
276 
277 	switch (act) {
278 	case DVACT_ACTIVATE:
279 		return (EOPNOTSUPP);
280 		break;
281 
282 	case DVACT_DEACTIVATE:
283 		sc->sc_dying = 1;
284 		break;
285 	}
286 	return (0);
287 }
288 #endif
289 
290 USB_DETACH(uhid)
291 {
292 	USB_DETACH_START(uhid, sc);
293 	int s;
294 #if defined(__NetBSD__) || defined(__OpenBSD__)
295 	int maj, mn;
296 
297 	DPRINTF(("uhid_detach: sc=%p flags=%d\n", sc, flags));
298 #else
299 	DPRINTF(("uhid_detach: sc=%p\n", sc));
300 #endif
301 
302 	sc->sc_dying = 1;
303 	if (sc->sc_intrpipe != NULL)
304 		usbd_abort_pipe(sc->sc_intrpipe);
305 
306 	if (sc->sc_state & UHID_OPEN) {
307 		s = splusb();
308 		if (--sc->sc_refcnt >= 0) {
309 			/* Wake everyone */
310 			wakeup(&sc->sc_q);
311 			/* Wait for processes to go away. */
312 			usb_detach_wait(USBDEV(sc->sc_dev));
313 		}
314 		splx(s);
315 	}
316 
317 #if defined(__NetBSD__) || defined(__OpenBSD__)
318 	/* locate the major number */
319 	for (maj = 0; maj < nchrdev; maj++)
320 		if (cdevsw[maj].d_open == uhidopen)
321 			break;
322 
323 	/* Nuke the vnodes for any open instances (calls close). */
324 	mn = self->dv_unit;
325 	vdevgone(maj, mn, mn, VCHR);
326 #elif defined(__FreeBSD__)
327 	/* XXX not implemented yet */
328 #endif
329 
330 	if (sc->sc_repdesc)
331 		free(sc->sc_repdesc, M_USBDEV);
332 
333 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
334 			   USBDEV(sc->sc_dev));
335 
336 	return (0);
337 }
338 
339 void
340 uhid_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
341 {
342 	struct uhid_softc *sc = addr;
343 
344 #ifdef UHID_DEBUG
345 	if (uhiddebug > 5) {
346 		u_int32_t cc, i;
347 
348 		usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
349 		DPRINTF(("uhid_intr: status=%d cc=%d\n", status, cc));
350 		DPRINTF(("uhid_intr: data ="));
351 		for (i = 0; i < cc; i++)
352 			DPRINTF((" %02x", sc->sc_ibuf[i]));
353 		DPRINTF(("\n"));
354 	}
355 #endif
356 
357 	if (status == USBD_CANCELLED)
358 		return;
359 
360 	if (status != USBD_NORMAL_COMPLETION) {
361 		DPRINTF(("uhid_intr: status=%d\n", status));
362 		sc->sc_state |= UHID_NEEDCLEAR;
363 		return;
364 	}
365 
366 	(void) b_to_q(sc->sc_ibuf, sc->sc_isize, &sc->sc_q);
367 
368 	if (sc->sc_state & UHID_ASLP) {
369 		sc->sc_state &= ~UHID_ASLP;
370 		DPRINTFN(5, ("uhid_intr: waking %p\n", sc));
371 		wakeup(&sc->sc_q);
372 	}
373 	selwakeup(&sc->sc_rsel);
374 	if (sc->sc_async != NULL) {
375 		DPRINTFN(3, ("uhid_intr: sending SIGIO %p\n", sc->sc_async));
376 		psignal(sc->sc_async, SIGIO);
377 	}
378 }
379 
380 int
381 uhidopen(dev_t dev, int flag, int mode, struct proc *p)
382 {
383 	struct uhid_softc *sc;
384 	usbd_status err;
385 
386 	USB_GET_SC_OPEN(uhid, UHIDUNIT(dev), sc);
387 
388 	DPRINTF(("uhidopen: sc=%p\n", sc));
389 
390 	if (sc->sc_dying)
391 		return (ENXIO);
392 
393 	if (sc->sc_state & UHID_OPEN)
394 		return (EBUSY);
395 	sc->sc_state |= UHID_OPEN;
396 
397 	if (clalloc(&sc->sc_q, UHID_BSIZE, 0) == -1) {
398 		sc->sc_state &= ~UHID_OPEN;
399 		return (ENOMEM);
400 	}
401 
402 	sc->sc_ibuf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
403 	sc->sc_obuf = malloc(sc->sc_osize, M_USBDEV, M_WAITOK);
404 
405 	/* Set up interrupt pipe. */
406 	err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
407 		  USBD_SHORT_XFER_OK, &sc->sc_intrpipe, sc, sc->sc_ibuf,
408 		  sc->sc_isize, uhid_intr, USBD_DEFAULT_INTERVAL);
409 	if (err) {
410 		DPRINTF(("uhidopen: usbd_open_pipe_intr failed, "
411 			 "error=%d\n",err));
412 		free(sc->sc_ibuf, M_USBDEV);
413 		free(sc->sc_obuf, M_USBDEV);
414 		sc->sc_state &= ~UHID_OPEN;
415 		return (EIO);
416 	}
417 
418 	sc->sc_state &= ~UHID_IMMED;
419 
420 	sc->sc_async = 0;
421 
422 	return (0);
423 }
424 
425 int
426 uhidclose(dev_t dev, int flag, int mode, struct proc *p)
427 {
428 	struct uhid_softc *sc;
429 
430 	USB_GET_SC(uhid, UHIDUNIT(dev), sc);
431 
432 	DPRINTF(("uhidclose: sc=%p\n", sc));
433 
434 	/* Disable interrupts. */
435 	usbd_abort_pipe(sc->sc_intrpipe);
436 	usbd_close_pipe(sc->sc_intrpipe);
437 	sc->sc_intrpipe = 0;
438 
439 	clfree(&sc->sc_q);
440 
441 	free(sc->sc_ibuf, M_USBDEV);
442 	free(sc->sc_obuf, M_USBDEV);
443 
444 	sc->sc_state &= ~UHID_OPEN;
445 
446 	sc->sc_async = 0;
447 
448 	return (0);
449 }
450 
451 int
452 uhid_do_read(struct uhid_softc *sc, struct uio *uio, int flag)
453 {
454 	int s;
455 	int error = 0;
456 	size_t length;
457 	u_char buffer[UHID_CHUNK];
458 	usbd_status err;
459 
460 	DPRINTFN(1, ("uhidread\n"));
461 	if (sc->sc_state & UHID_IMMED) {
462 		DPRINTFN(1, ("uhidread immed\n"));
463 
464 		err = usbd_get_report(sc->sc_iface, UHID_INPUT_REPORT,
465 				    sc->sc_iid, buffer, sc->sc_isize);
466 		if (err)
467 			return (EIO);
468 		return (uiomove(buffer, sc->sc_isize, uio));
469 	}
470 
471 	s = splusb();
472 	while (sc->sc_q.c_cc == 0) {
473 		if (flag & IO_NDELAY) {
474 			splx(s);
475 			return (EWOULDBLOCK);
476 		}
477 		sc->sc_state |= UHID_ASLP;
478 		DPRINTFN(5, ("uhidread: sleep on %p\n", sc));
479 		error = tsleep(&sc->sc_q, PZERO | PCATCH, "uhidrea", 0);
480 		DPRINTFN(5, ("uhidread: woke, error=%d\n", error));
481 		if (sc->sc_dying)
482 			error = EIO;
483 		if (error) {
484 			sc->sc_state &= ~UHID_ASLP;
485 			break;
486 		}
487 		if (sc->sc_state & UHID_NEEDCLEAR) {
488 			DPRINTFN(-1,("uhidread: clearing stall\n"));
489 			sc->sc_state &= ~UHID_NEEDCLEAR;
490 			usbd_clear_endpoint_stall(sc->sc_intrpipe);
491 		}
492 	}
493 	splx(s);
494 
495 	/* Transfer as many chunks as possible. */
496 	while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0 && !error) {
497 		length = min(sc->sc_q.c_cc, uio->uio_resid);
498 		if (length > sizeof(buffer))
499 			length = sizeof(buffer);
500 
501 		/* Remove a small chunk from the input queue. */
502 		(void) q_to_b(&sc->sc_q, buffer, length);
503 		DPRINTFN(5, ("uhidread: got %lu chars\n", (u_long)length));
504 
505 		/* Copy the data to the user process. */
506 		if ((error = uiomove(buffer, length, uio)) != 0)
507 			break;
508 	}
509 
510 	return (error);
511 }
512 
513 int
514 uhidread(dev_t dev, struct uio *uio, int flag)
515 {
516 	struct uhid_softc *sc;
517 	int error;
518 
519 	USB_GET_SC(uhid, UHIDUNIT(dev), sc);
520 
521 	sc->sc_refcnt++;
522 	error = uhid_do_read(sc, uio, flag);
523 	if (--sc->sc_refcnt < 0)
524 		usb_detach_wakeup(USBDEV(sc->sc_dev));
525 	return (error);
526 }
527 
528 int
529 uhid_do_write(struct uhid_softc *sc, struct uio *uio, int flag)
530 {
531 	int error;
532 	int size;
533 	usbd_status err;
534 
535 	DPRINTFN(1, ("uhidwrite\n"));
536 
537 	if (sc->sc_dying)
538 		return (EIO);
539 
540 	size = sc->sc_osize;
541 	error = 0;
542 	if (uio->uio_resid != size)
543 		return (EINVAL);
544 	error = uiomove(sc->sc_obuf, size, uio);
545 	if (!error) {
546 		if (sc->sc_oid)
547 			err = usbd_set_report(sc->sc_iface, UHID_OUTPUT_REPORT,
548 				  sc->sc_obuf[0], sc->sc_obuf+1, size-1);
549 		else
550 			err = usbd_set_report(sc->sc_iface, UHID_OUTPUT_REPORT,
551 				  0, sc->sc_obuf, size);
552 		if (err)
553 			error = EIO;
554 	}
555 
556 	return (error);
557 }
558 
559 int
560 uhidwrite(dev_t dev, struct uio *uio, int flag)
561 {
562 	struct uhid_softc *sc;
563 	int error;
564 
565 	USB_GET_SC(uhid, UHIDUNIT(dev), sc);
566 
567 	sc->sc_refcnt++;
568 	error = uhid_do_write(sc, uio, flag);
569 	if (--sc->sc_refcnt < 0)
570 		usb_detach_wakeup(USBDEV(sc->sc_dev));
571 	return (error);
572 }
573 
574 int
575 uhid_do_ioctl(struct uhid_softc *sc, u_long cmd, caddr_t addr,
576 	      int flag, struct proc *p)
577 {
578 	struct usb_ctl_report_desc *rd;
579 	struct usb_ctl_report *re;
580 	int size, id;
581 	usbd_status err;
582 
583 	DPRINTFN(2, ("uhidioctl: cmd=%lx\n", cmd));
584 
585 	if (sc->sc_dying)
586 		return (EIO);
587 
588 	switch (cmd) {
589 	case FIONBIO:
590 		/* All handled in the upper FS layer. */
591 		break;
592 
593 	case FIOASYNC:
594 		if (*(int *)addr) {
595 			if (sc->sc_async != NULL)
596 				return (EBUSY);
597 			sc->sc_async = p;
598 			DPRINTF(("uhid_do_ioctl: FIOASYNC %p\n", p));
599 		} else
600 			sc->sc_async = NULL;
601 		break;
602 
603 	/* XXX this is not the most general solution. */
604 	case TIOCSPGRP:
605 		if (sc->sc_async == NULL)
606 			return (EINVAL);
607 		if (*(int *)addr != sc->sc_async->p_pgid)
608 			return (EPERM);
609 		break;
610 
611 	case USB_GET_REPORT_DESC:
612 		rd = (struct usb_ctl_report_desc *)addr;
613 		size = min(sc->sc_repdesc_size, sizeof rd->data);
614 		rd->size = size;
615 		memcpy(rd->data, sc->sc_repdesc, size);
616 		break;
617 
618 	case USB_SET_IMMED:
619 		if (*(int *)addr) {
620 			/* XXX should read into ibuf, but does it matter? */
621 			err = usbd_get_report(sc->sc_iface, UHID_INPUT_REPORT,
622 				  sc->sc_iid, sc->sc_ibuf, sc->sc_isize);
623 			if (err)
624 				return (EOPNOTSUPP);
625 
626 			sc->sc_state |=  UHID_IMMED;
627 		} else
628 			sc->sc_state &= ~UHID_IMMED;
629 		break;
630 
631 	case USB_GET_REPORT:
632 		re = (struct usb_ctl_report *)addr;
633 		switch (re->report) {
634 		case UHID_INPUT_REPORT:
635 			size = sc->sc_isize;
636 			id = sc->sc_iid;
637 			break;
638 		case UHID_OUTPUT_REPORT:
639 			size = sc->sc_osize;
640 			id = sc->sc_oid;
641 			break;
642 		case UHID_FEATURE_REPORT:
643 			size = sc->sc_fsize;
644 			id = sc->sc_fid;
645 			break;
646 		default:
647 			return (EINVAL);
648 		}
649 		err = usbd_get_report(sc->sc_iface, re->report, id, re->data,
650 			  size);
651 		if (err)
652 			return (EIO);
653 		break;
654 
655 	case USB_SET_REPORT:
656 		re = (struct usb_ctl_report *)addr;
657 		switch (re->report) {
658 		case UHID_INPUT_REPORT:
659 			size = sc->sc_isize;
660 			id = sc->sc_iid;
661 			break;
662 		case UHID_OUTPUT_REPORT:
663 			size = sc->sc_osize;
664 			id = sc->sc_oid;
665 			break;
666 		case UHID_FEATURE_REPORT:
667 			size = sc->sc_fsize;
668 			id = sc->sc_fid;
669 			break;
670 		default:
671 			return (EINVAL);
672 		}
673 		err = usbd_set_report(sc->sc_iface, re->report, id, re->data,
674 			  size);
675 		if (err)
676 			return (EIO);
677 		break;
678 
679 	default:
680 		return (EINVAL);
681 	}
682 	return (0);
683 }
684 
685 int
686 uhidioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
687 {
688 	struct uhid_softc *sc;
689 	int error;
690 
691 	USB_GET_SC(uhid, UHIDUNIT(dev), sc);
692 
693 	sc->sc_refcnt++;
694 	error = uhid_do_ioctl(sc, cmd, addr, flag, p);
695 	if (--sc->sc_refcnt < 0)
696 		usb_detach_wakeup(USBDEV(sc->sc_dev));
697 	return (error);
698 }
699 
700 int
701 uhidpoll(dev_t dev, int events, struct proc *p)
702 {
703 	struct uhid_softc *sc;
704 	int revents = 0;
705 	int s;
706 
707 	USB_GET_SC(uhid, UHIDUNIT(dev), sc);
708 
709 	if (sc->sc_dying)
710 		return (EIO);
711 
712 	s = splusb();
713 	if (events & (POLLOUT | POLLWRNORM))
714 		revents |= events & (POLLOUT | POLLWRNORM);
715 	if (events & (POLLIN | POLLRDNORM)) {
716 		if (sc->sc_q.c_cc > 0)
717 			revents |= events & (POLLIN | POLLRDNORM);
718 		else
719 			selrecord(p, &sc->sc_rsel);
720 	}
721 
722 	splx(s);
723 	return (revents);
724 }
725 
726 #if defined(__FreeBSD__)
727 DRIVER_MODULE(uhid, uhub, uhid_driver, uhid_devclass, usbd_driver_load, 0);
728 #endif
729