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