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