[BACK]Return to uhid.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / dev / usb

Annotation of src/sys/dev/usb/uhid.c, Revision 1.72

1.72    ! christos    1: /*     $NetBSD: uhid.c,v 1.71 2006/10/12 01:31:59 christos Exp $       */
1.1       augustss    2:
                      3: /*
1.63      mycroft     4:  * Copyright (c) 1998, 2004 The NetBSD Foundation, Inc.
1.1       augustss    5:  * All rights reserved.
                      6:  *
1.6       augustss    7:  * This code is derived from software contributed to The NetBSD Foundation
1.38      augustss    8:  * by Lennart Augustsson (lennart@augustsson.net) at
1.6       augustss    9:  * Carlstedt Research & Technology.
1.1       augustss   10:  *
                     11:  * Redistribution and use in source and binary forms, with or without
                     12:  * modification, are permitted provided that the following conditions
                     13:  * are met:
                     14:  * 1. Redistributions of source code must retain the above copyright
                     15:  *    notice, this list of conditions and the following disclaimer.
                     16:  * 2. Redistributions in binary form must reproduce the above copyright
                     17:  *    notice, this list of conditions and the following disclaimer in the
                     18:  *    documentation and/or other materials provided with the distribution.
                     19:  * 3. All advertising materials mentioning features or use of this software
                     20:  *    must display the following acknowledgement:
                     21:  *        This product includes software developed by the NetBSD
                     22:  *        Foundation, Inc. and its contributors.
                     23:  * 4. Neither the name of The NetBSD Foundation nor the names of its
                     24:  *    contributors may be used to endorse or promote products derived
                     25:  *    from this software without specific prior written permission.
                     26:  *
                     27:  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
                     28:  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
                     29:  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                     30:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
                     31:  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
                     32:  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
                     33:  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
                     34:  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
                     35:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
                     36:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
                     37:  * POSSIBILITY OF SUCH DAMAGE.
                     38:  */
                     39:
1.15      augustss   40: /*
1.57      augustss   41:  * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
1.15      augustss   42:  */
1.46      lukem      43:
                     44: #include <sys/cdefs.h>
1.72    ! christos   45: __KERNEL_RCSID(0, "$NetBSD: uhid.c,v 1.71 2006/10/12 01:31:59 christos Exp $");
1.1       augustss   46:
                     47: #include <sys/param.h>
                     48: #include <sys/systm.h>
                     49: #include <sys/kernel.h>
                     50: #include <sys/malloc.h>
1.37      augustss   51: #include <sys/signalvar.h>
1.14      augustss   52: #include <sys/device.h>
1.12      augustss   53: #include <sys/ioctl.h>
1.18      augustss   54: #include <sys/conf.h>
1.1       augustss   55: #include <sys/tty.h>
                     56: #include <sys/file.h>
                     57: #include <sys/select.h>
                     58: #include <sys/proc.h>
                     59: #include <sys/vnode.h>
                     60: #include <sys/poll.h>
                     61:
                     62: #include <dev/usb/usb.h>
                     63: #include <dev/usb/usbhid.h>
                     64:
1.42      augustss   65: #include <dev/usb/usbdevs.h>
1.1       augustss   66: #include <dev/usb/usbdi.h>
                     67: #include <dev/usb/usbdi_util.h>
                     68: #include <dev/usb/hid.h>
                     69: #include <dev/usb/usb_quirks.h>
                     70:
1.47      augustss   71: #include <dev/usb/uhidev.h>
1.42      augustss   72:
1.26      augustss   73: #ifdef UHID_DEBUG
1.19      augustss   74: #define DPRINTF(x)     if (uhiddebug) logprintf x
                     75: #define DPRINTFN(n,x)  if (uhiddebug>(n)) logprintf x
1.1       augustss   76: int    uhiddebug = 0;
                     77: #else
                     78: #define DPRINTF(x)
                     79: #define DPRINTFN(n,x)
                     80: #endif
                     81:
                     82: struct uhid_softc {
1.47      augustss   83:        struct uhidev sc_hdev;
1.1       augustss   84:
                     85:        int sc_isize;
                     86:        int sc_osize;
1.2       augustss   87:        int sc_fsize;
1.1       augustss   88:
1.33      augustss   89:        u_char *sc_obuf;
1.1       augustss   90:
                     91:        struct clist sc_q;
                     92:        struct selinfo sc_rsel;
1.48      augustss   93:        usb_proc_ptr sc_async;  /* process that wants SIGIO */
1.1       augustss   94:        u_char sc_state;        /* driver state */
1.47      augustss   95: #define        UHID_ASLP       0x01    /* waiting for device data */
                     96: #define UHID_IMMED     0x02    /* return read data immediately */
1.18      augustss   97:
                     98:        int sc_refcnt;
                     99:        u_char sc_dying;
1.1       augustss  100: };
                    101:
                    102: #define        UHIDUNIT(dev)   (minor(dev))
                    103: #define        UHID_CHUNK      128     /* chunk size for read */
                    104: #define        UHID_BSIZE      1020    /* buffer size */
                    105:
1.53      gehenna   106: dev_type_open(uhidopen);
                    107: dev_type_close(uhidclose);
                    108: dev_type_read(uhidread);
                    109: dev_type_write(uhidwrite);
                    110: dev_type_ioctl(uhidioctl);
                    111: dev_type_poll(uhidpoll);
1.55      jdolecek  112: dev_type_kqfilter(uhidkqfilter);
1.53      gehenna   113:
                    114: const struct cdevsw uhid_cdevsw = {
                    115:        uhidopen, uhidclose, uhidread, uhidwrite, uhidioctl,
1.70      christos  116:        nostop, notty, uhidpoll, nommap, uhidkqfilter, D_OTHER,
1.53      gehenna   117: };
1.19      augustss  118:
1.47      augustss  119: Static void uhid_intr(struct uhidev *, void *, u_int len);
1.18      augustss  120:
1.39      augustss  121: Static int uhid_do_read(struct uhid_softc *, struct uio *uio, int);
                    122: Static int uhid_do_write(struct uhid_softc *, struct uio *uio, int);
1.68      christos  123: Static int uhid_do_ioctl(struct uhid_softc*, u_long, caddr_t, int, struct lwp *);
1.1       augustss  124:
1.12      augustss  125: USB_DECLARE_DRIVER(uhid);
1.1       augustss  126:
1.47      augustss  127: int
1.72    ! christos  128: uhid_match(struct device *parent, struct cfdata *match,
        !           129:     void *aux)
1.1       augustss  130: {
1.66      tron      131: #ifdef UHID_DEBUG
1.47      augustss  132:        struct uhidev_attach_arg *uha = aux;
1.66      tron      133: #endif
1.47      augustss  134:
                    135:        DPRINTF(("uhid_match: report=%d\n", uha->reportid));
                    136:
1.65      augustss  137:        if (match->cf_flags & 1)
1.67      augustss  138:                return (UMATCH_HIGHEST);
                    139:        else
                    140:                return (UMATCH_IFACECLASS_GENERIC);
1.1       augustss  141: }
                    142:
1.47      augustss  143: void
1.72    ! christos  144: uhid_attach(struct device *parent, struct device *self, void *aux)
1.1       augustss  145: {
1.47      augustss  146:        struct uhid_softc *sc = (struct uhid_softc *)self;
                    147:        struct uhidev_attach_arg *uha = aux;
                    148:        int size, repid;
1.1       augustss  149:        void *desc;
1.52      augustss  150:
1.47      augustss  151:        sc->sc_hdev.sc_intr = uhid_intr;
                    152:        sc->sc_hdev.sc_parent = uha->parent;
                    153:        sc->sc_hdev.sc_report_id = uha->reportid;
                    154:
                    155:        uhidev_get_report_desc(uha->parent, &desc, &size);
                    156:        repid = uha->reportid;
                    157:        sc->sc_isize = hid_report_size(desc, size, hid_input,   repid);
                    158:        sc->sc_osize = hid_report_size(desc, size, hid_output,  repid);
                    159:        sc->sc_fsize = hid_report_size(desc, size, hid_feature, repid);
1.1       augustss  160:
1.47      augustss  161:        printf(": input=%d, output=%d, feature=%d\n",
                    162:               sc->sc_isize, sc->sc_osize, sc->sc_fsize);
1.32      augustss  163:
1.12      augustss  164:        USB_ATTACH_SUCCESS_RETURN;
1.1       augustss  165: }
                    166:
1.18      augustss  167: int
1.39      augustss  168: uhid_activate(device_ptr_t self, enum devact act)
1.18      augustss  169: {
1.21      augustss  170:        struct uhid_softc *sc = (struct uhid_softc *)self;
                    171:
                    172:        switch (act) {
                    173:        case DVACT_ACTIVATE:
                    174:                return (EOPNOTSUPP);
                    175:
                    176:        case DVACT_DEACTIVATE:
                    177:                sc->sc_dying = 1;
                    178:                break;
                    179:        }
1.18      augustss  180:        return (0);
1.12      augustss  181: }
                    182:
1.47      augustss  183: int
1.72    ! christos  184: uhid_detach(struct device *self, int flags)
1.1       augustss  185: {
1.47      augustss  186:        struct uhid_softc *sc = (struct uhid_softc *)self;
1.26      augustss  187:        int s;
1.18      augustss  188:        int maj, mn;
                    189:
                    190:        DPRINTF(("uhid_detach: sc=%p flags=%d\n", sc, flags));
1.3       augustss  191:
1.18      augustss  192:        sc->sc_dying = 1;
                    193:
1.47      augustss  194:        if (sc->sc_hdev.sc_state & UHIDEV_OPEN) {
1.18      augustss  195:                s = splusb();
                    196:                if (--sc->sc_refcnt >= 0) {
                    197:                        /* Wake everyone */
                    198:                        wakeup(&sc->sc_q);
                    199:                        /* Wait for processes to go away. */
1.47      augustss  200:                        usb_detach_wait(USBDEV(sc->sc_hdev.sc_dev));
1.18      augustss  201:                }
                    202:                splx(s);
                    203:        }
                    204:
                    205:        /* locate the major number */
1.53      gehenna   206: #if defined(__NetBSD__)
                    207:        maj = cdevsw_lookup_major(&uhid_cdevsw);
                    208: #elif defined(__OpenBSD__)
1.18      augustss  209:        for (maj = 0; maj < nchrdev; maj++)
                    210:                if (cdevsw[maj].d_open == uhidopen)
                    211:                        break;
1.53      gehenna   212: #endif
1.18      augustss  213:
                    214:        /* Nuke the vnodes for any open instances (calls close). */
1.69      thorpej   215:        mn = device_unit(self);
1.18      augustss  216:        vdevgone(maj, mn, mn, VCHR);
1.47      augustss  217:
                    218: #if 0
1.52      augustss  219:        usbd_add_drv_event(USB_EVENT_DRIVER_DETACH,
1.47      augustss  220:                           sc->sc_hdev.sc_parent->sc_udev,
                    221:                           USBDEV(sc->sc_hdev.sc_dev));
1.26      augustss  222: #endif
1.18      augustss  223:
                    224:        return (0);
1.1       augustss  225: }
                    226:
                    227: void
1.47      augustss  228: uhid_intr(struct uhidev *addr, void *data, u_int len)
1.1       augustss  229: {
1.47      augustss  230:        struct uhid_softc *sc = (struct uhid_softc *)addr;
1.1       augustss  231:
1.33      augustss  232: #ifdef UHID_DEBUG
                    233:        if (uhiddebug > 5) {
1.47      augustss  234:                u_int32_t i;
1.52      augustss  235:
1.33      augustss  236:                DPRINTF(("uhid_intr: data ="));
1.47      augustss  237:                for (i = 0; i < len; i++)
1.51      augustss  238:                        DPRINTF((" %02x", ((u_char *)data)[i]));
1.33      augustss  239:                DPRINTF(("\n"));
                    240:        }
                    241: #endif
1.1       augustss  242:
1.47      augustss  243:        (void)b_to_q(data, len, &sc->sc_q);
1.52      augustss  244:
1.1       augustss  245:        if (sc->sc_state & UHID_ASLP) {
                    246:                sc->sc_state &= ~UHID_ASLP;
1.44      yamt      247:                DPRINTFN(5, ("uhid_intr: waking %p\n", &sc->sc_q));
1.18      augustss  248:                wakeup(&sc->sc_q);
1.1       augustss  249:        }
1.55      jdolecek  250:        selnotify(&sc->sc_rsel, 0);
1.37      augustss  251:        if (sc->sc_async != NULL) {
                    252:                DPRINTFN(3, ("uhid_intr: sending SIGIO %p\n", sc->sc_async));
                    253:                psignal(sc->sc_async, SIGIO);
                    254:        }
1.1       augustss  255: }
                    256:
                    257: int
1.72    ! christos  258: uhidopen(dev_t dev, int flag, int mode,
        !           259:     struct lwp *l)
1.1       augustss  260: {
1.25      augustss  261:        struct uhid_softc *sc;
1.47      augustss  262:        int error;
1.25      augustss  263:
1.12      augustss  264:        USB_GET_SC_OPEN(uhid, UHIDUNIT(dev), sc);
1.1       augustss  265:
1.18      augustss  266:        DPRINTF(("uhidopen: sc=%p\n", sc));
1.1       augustss  267:
1.18      augustss  268:        if (sc->sc_dying)
                    269:                return (ENXIO);
1.1       augustss  270:
1.47      augustss  271:        error = uhidev_open(&sc->sc_hdev);
                    272:        if (error)
                    273:                return (error);
1.1       augustss  274:
1.17      augustss  275:        if (clalloc(&sc->sc_q, UHID_BSIZE, 0) == -1) {
1.47      augustss  276:                uhidev_close(&sc->sc_hdev);
1.12      augustss  277:                return (ENOMEM);
1.17      augustss  278:        }
1.18      augustss  279:        sc->sc_obuf = malloc(sc->sc_osize, M_USBDEV, M_WAITOK);
1.17      augustss  280:        sc->sc_state &= ~UHID_IMMED;
1.47      augustss  281:        sc->sc_async = NULL;
1.37      augustss  282:
1.12      augustss  283:        return (0);
1.1       augustss  284: }
                    285:
                    286: int
1.72    ! christos  287: uhidclose(dev_t dev, int flag, int mode,
        !           288:     struct lwp *l)
1.1       augustss  289: {
1.25      augustss  290:        struct uhid_softc *sc;
                    291:
1.12      augustss  292:        USB_GET_SC(uhid, UHIDUNIT(dev), sc);
1.1       augustss  293:
                    294:        DPRINTF(("uhidclose: sc=%p\n", sc));
                    295:
                    296:        clfree(&sc->sc_q);
1.18      augustss  297:        free(sc->sc_obuf, M_USBDEV);
1.47      augustss  298:        sc->sc_async = NULL;
                    299:        uhidev_close(&sc->sc_hdev);
1.37      augustss  300:
1.12      augustss  301:        return (0);
1.1       augustss  302: }
                    303:
                    304: int
1.39      augustss  305: uhid_do_read(struct uhid_softc *sc, struct uio *uio, int flag)
1.1       augustss  306: {
                    307:        int s;
                    308:        int error = 0;
1.47      augustss  309:        int extra;
1.1       augustss  310:        size_t length;
                    311:        u_char buffer[UHID_CHUNK];
1.27      augustss  312:        usbd_status err;
1.1       augustss  313:
                    314:        DPRINTFN(1, ("uhidread\n"));
1.2       augustss  315:        if (sc->sc_state & UHID_IMMED) {
                    316:                DPRINTFN(1, ("uhidread immed\n"));
1.47      augustss  317:                extra = sc->sc_hdev.sc_report_id != 0;
                    318:                err = uhidev_get_report(&sc->sc_hdev, UHID_INPUT_REPORT,
                    319:                                        buffer, sc->sc_isize + extra);
1.27      augustss  320:                if (err)
1.2       augustss  321:                        return (EIO);
1.47      augustss  322:                return (uiomove(buffer+extra, sc->sc_isize, uio));
1.2       augustss  323:        }
                    324:
1.10      augustss  325:        s = splusb();
1.1       augustss  326:        while (sc->sc_q.c_cc == 0) {
                    327:                if (flag & IO_NDELAY) {
                    328:                        splx(s);
1.18      augustss  329:                        return (EWOULDBLOCK);
1.1       augustss  330:                }
                    331:                sc->sc_state |= UHID_ASLP;
1.44      yamt      332:                DPRINTFN(5, ("uhidread: sleep on %p\n", &sc->sc_q));
1.18      augustss  333:                error = tsleep(&sc->sc_q, PZERO | PCATCH, "uhidrea", 0);
1.1       augustss  334:                DPRINTFN(5, ("uhidread: woke, error=%d\n", error));
1.18      augustss  335:                if (sc->sc_dying)
                    336:                        error = EIO;
1.1       augustss  337:                if (error) {
                    338:                        sc->sc_state &= ~UHID_ASLP;
1.18      augustss  339:                        break;
1.1       augustss  340:                }
                    341:        }
                    342:        splx(s);
                    343:
                    344:        /* Transfer as many chunks as possible. */
1.18      augustss  345:        while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0 && !error) {
1.1       augustss  346:                length = min(sc->sc_q.c_cc, uio->uio_resid);
                    347:                if (length > sizeof(buffer))
                    348:                        length = sizeof(buffer);
                    349:
                    350:                /* Remove a small chunk from the input queue. */
                    351:                (void) q_to_b(&sc->sc_q, buffer, length);
1.29      augustss  352:                DPRINTFN(5, ("uhidread: got %lu chars\n", (u_long)length));
1.1       augustss  353:
                    354:                /* Copy the data to the user process. */
                    355:                if ((error = uiomove(buffer, length, uio)) != 0)
                    356:                        break;
                    357:        }
                    358:
                    359:        return (error);
                    360: }
                    361:
                    362: int
1.39      augustss  363: uhidread(dev_t dev, struct uio *uio, int flag)
1.1       augustss  364: {
1.25      augustss  365:        struct uhid_softc *sc;
                    366:        int error;
                    367:
1.18      augustss  368:        USB_GET_SC(uhid, UHIDUNIT(dev), sc);
                    369:
                    370:        sc->sc_refcnt++;
                    371:        error = uhid_do_read(sc, uio, flag);
                    372:        if (--sc->sc_refcnt < 0)
1.47      augustss  373:                usb_detach_wakeup(USBDEV(sc->sc_hdev.sc_dev));
1.18      augustss  374:        return (error);
                    375: }
                    376:
                    377: int
1.72    ! christos  378: uhid_do_write(struct uhid_softc *sc, struct uio *uio, int flag)
1.18      augustss  379: {
1.1       augustss  380:        int error;
                    381:        int size;
1.27      augustss  382:        usbd_status err;
1.1       augustss  383:
1.18      augustss  384:        DPRINTFN(1, ("uhidwrite\n"));
1.52      augustss  385:
1.18      augustss  386:        if (sc->sc_dying)
1.1       augustss  387:                return (EIO);
                    388:
                    389:        size = sc->sc_osize;
                    390:        error = 0;
1.18      augustss  391:        if (uio->uio_resid != size)
                    392:                return (EINVAL);
                    393:        error = uiomove(sc->sc_obuf, size, uio);
                    394:        if (!error) {
1.47      augustss  395:                err = uhidev_set_report(&sc->sc_hdev, UHID_OUTPUT_REPORT,
                    396:                                        sc->sc_obuf, size);
1.27      augustss  397:                if (err)
1.1       augustss  398:                        error = EIO;
                    399:        }
1.18      augustss  400:
1.1       augustss  401:        return (error);
                    402: }
                    403:
                    404: int
1.39      augustss  405: uhidwrite(dev_t dev, struct uio *uio, int flag)
1.18      augustss  406: {
1.25      augustss  407:        struct uhid_softc *sc;
                    408:        int error;
                    409:
1.18      augustss  410:        USB_GET_SC(uhid, UHIDUNIT(dev), sc);
                    411:
                    412:        sc->sc_refcnt++;
                    413:        error = uhid_do_write(sc, uio, flag);
                    414:        if (--sc->sc_refcnt < 0)
1.47      augustss  415:                usb_detach_wakeup(USBDEV(sc->sc_hdev.sc_dev));
1.18      augustss  416:        return (error);
                    417: }
                    418:
                    419: int
1.39      augustss  420: uhid_do_ioctl(struct uhid_softc *sc, u_long cmd, caddr_t addr,
1.72    ! christos  421:     int flag, struct lwp *l)
1.1       augustss  422: {
                    423:        struct usb_ctl_report_desc *rd;
1.2       augustss  424:        struct usb_ctl_report *re;
1.47      augustss  425:        u_char buffer[UHID_CHUNK];
                    426:        int size, extra;
1.27      augustss  427:        usbd_status err;
1.47      augustss  428:        void *desc;
1.1       augustss  429:
1.18      augustss  430:        DPRINTFN(2, ("uhidioctl: cmd=%lx\n", cmd));
                    431:
                    432:        if (sc->sc_dying)
1.1       augustss  433:                return (EIO);
                    434:
                    435:        switch (cmd) {
1.2       augustss  436:        case FIONBIO:
                    437:                /* All handled in the upper FS layer. */
1.37      augustss  438:                break;
                    439:
                    440:        case FIOASYNC:
                    441:                if (*(int *)addr) {
                    442:                        if (sc->sc_async != NULL)
                    443:                                return (EBUSY);
1.68      christos  444:                        sc->sc_async = l->l_proc;
                    445:                        DPRINTF(("uhid_do_ioctl: FIOASYNC %p\n", l->l_proc));
1.37      augustss  446:                } else
                    447:                        sc->sc_async = NULL;
                    448:                break;
                    449:
                    450:        /* XXX this is not the most general solution. */
                    451:        case TIOCSPGRP:
                    452:                if (sc->sc_async == NULL)
                    453:                        return (EINVAL);
                    454:                if (*(int *)addr != sc->sc_async->p_pgid)
1.60      jdolecek  455:                        return (EPERM);
                    456:                break;
                    457:
                    458:        case FIOSETOWN:
                    459:                if (sc->sc_async == NULL)
                    460:                        return (EINVAL);
                    461:                if (-*(int *)addr != sc->sc_async->p_pgid
1.70      christos  462:                    && *(int *)addr != sc->sc_async->p_pid)
1.37      augustss  463:                        return (EPERM);
1.2       augustss  464:                break;
                    465:
1.1       augustss  466:        case USB_GET_REPORT_DESC:
1.47      augustss  467:                uhidev_get_report_desc(sc->sc_hdev.sc_parent, &desc, &size);
1.1       augustss  468:                rd = (struct usb_ctl_report_desc *)addr;
1.50      christos  469:                size = min(size, sizeof rd->ucrd_data);
                    470:                rd->ucrd_size = size;
                    471:                memcpy(rd->ucrd_data, desc, size);
1.1       augustss  472:                break;
1.2       augustss  473:
                    474:        case USB_SET_IMMED:
1.9       augustss  475:                if (*(int *)addr) {
1.47      augustss  476:                        extra = sc->sc_hdev.sc_report_id != 0;
                    477:                        err = uhidev_get_report(&sc->sc_hdev, UHID_INPUT_REPORT,
                    478:                                                buffer, sc->sc_isize + extra);
1.27      augustss  479:                        if (err)
1.9       augustss  480:                                return (EOPNOTSUPP);
1.12      augustss  481:
1.2       augustss  482:                        sc->sc_state |=  UHID_IMMED;
1.9       augustss  483:                } else
1.2       augustss  484:                        sc->sc_state &= ~UHID_IMMED;
                    485:                break;
                    486:
                    487:        case USB_GET_REPORT:
                    488:                re = (struct usb_ctl_report *)addr;
1.50      christos  489:                switch (re->ucr_report) {
1.2       augustss  490:                case UHID_INPUT_REPORT:
                    491:                        size = sc->sc_isize;
                    492:                        break;
                    493:                case UHID_OUTPUT_REPORT:
                    494:                        size = sc->sc_osize;
                    495:                        break;
                    496:                case UHID_FEATURE_REPORT:
                    497:                        size = sc->sc_fsize;
                    498:                        break;
                    499:                default:
                    500:                        return (EINVAL);
                    501:                }
1.47      augustss  502:                extra = sc->sc_hdev.sc_report_id != 0;
1.50      christos  503:                err = uhidev_get_report(&sc->sc_hdev, re->ucr_report,
                    504:                    re->ucr_data, size + extra);
1.47      augustss  505:                if (extra)
1.50      christos  506:                        memcpy(re->ucr_data, re->ucr_data+1, size);
1.35      augustss  507:                if (err)
                    508:                        return (EIO);
                    509:                break;
                    510:
                    511:        case USB_SET_REPORT:
                    512:                re = (struct usb_ctl_report *)addr;
1.50      christos  513:                switch (re->ucr_report) {
1.35      augustss  514:                case UHID_INPUT_REPORT:
                    515:                        size = sc->sc_isize;
                    516:                        break;
                    517:                case UHID_OUTPUT_REPORT:
                    518:                        size = sc->sc_osize;
                    519:                        break;
                    520:                case UHID_FEATURE_REPORT:
                    521:                        size = sc->sc_fsize;
                    522:                        break;
                    523:                default:
                    524:                        return (EINVAL);
                    525:                }
1.50      christos  526:                err = uhidev_set_report(&sc->sc_hdev, re->ucr_report,
                    527:                    re->ucr_data, size);
1.27      augustss  528:                if (err)
1.2       augustss  529:                        return (EIO);
                    530:                break;
                    531:
1.47      augustss  532:        case USB_GET_REPORT_ID:
                    533:                *(int *)addr = sc->sc_hdev.sc_report_id;
                    534:                break;
                    535:
1.61      jdolecek  536:        case USB_GET_DEVICEINFO:
                    537:                usbd_fill_deviceinfo(sc->sc_hdev.sc_parent->sc_udev,
                    538:                                     (struct usb_device_info *)addr, 1);
                    539:                break;
                    540:
                    541:         case USB_GET_STRING_DESC:
                    542:            {
                    543:                 struct usb_string_desc *si = (struct usb_string_desc *)addr;
                    544:                 err = usbd_get_string_desc(sc->sc_hdev.sc_parent->sc_udev,
                    545:                        si->usd_string_index,
1.62      mycroft   546:                        si->usd_language_id, &si->usd_desc, &size);
1.61      jdolecek  547:                 if (err)
                    548:                         return (EINVAL);
                    549:                 break;
                    550:            }
                    551:
1.1       augustss  552:        default:
                    553:                return (EINVAL);
                    554:        }
                    555:        return (0);
                    556: }
                    557:
                    558: int
1.68      christos  559: uhidioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct lwp *l)
1.18      augustss  560: {
1.25      augustss  561:        struct uhid_softc *sc;
                    562:        int error;
                    563:
1.18      augustss  564:        USB_GET_SC(uhid, UHIDUNIT(dev), sc);
                    565:
                    566:        sc->sc_refcnt++;
1.68      christos  567:        error = uhid_do_ioctl(sc, cmd, addr, flag, l);
1.18      augustss  568:        if (--sc->sc_refcnt < 0)
1.47      augustss  569:                usb_detach_wakeup(USBDEV(sc->sc_hdev.sc_dev));
1.18      augustss  570:        return (error);
                    571: }
                    572:
                    573: int
1.68      christos  574: uhidpoll(dev_t dev, int events, struct lwp *l)
1.1       augustss  575: {
1.25      augustss  576:        struct uhid_softc *sc;
1.1       augustss  577:        int revents = 0;
                    578:        int s;
1.25      augustss  579:
1.12      augustss  580:        USB_GET_SC(uhid, UHIDUNIT(dev), sc);
1.1       augustss  581:
1.18      augustss  582:        if (sc->sc_dying)
1.64      ws        583:                return (POLLHUP);
1.1       augustss  584:
1.10      augustss  585:        s = splusb();
1.1       augustss  586:        if (events & (POLLOUT | POLLWRNORM))
                    587:                revents |= events & (POLLOUT | POLLWRNORM);
1.4       veego     588:        if (events & (POLLIN | POLLRDNORM)) {
1.1       augustss  589:                if (sc->sc_q.c_cc > 0)
                    590:                        revents |= events & (POLLIN | POLLRDNORM);
                    591:                else
1.68      christos  592:                        selrecord(l, &sc->sc_rsel);
1.4       veego     593:        }
1.1       augustss  594:
                    595:        splx(s);
                    596:        return (revents);
1.55      jdolecek  597: }
                    598:
                    599: static void
                    600: filt_uhidrdetach(struct knote *kn)
                    601: {
                    602:        struct uhid_softc *sc = kn->kn_hook;
                    603:        int s;
                    604:
                    605:        s = splusb();
1.56      christos  606:        SLIST_REMOVE(&sc->sc_rsel.sel_klist, kn, knote, kn_selnext);
1.55      jdolecek  607:        splx(s);
                    608: }
                    609:
                    610: static int
1.72    ! christos  611: filt_uhidread(struct knote *kn, long hint)
1.55      jdolecek  612: {
                    613:        struct uhid_softc *sc = kn->kn_hook;
                    614:
                    615:        kn->kn_data = sc->sc_q.c_cc;
                    616:        return (kn->kn_data > 0);
                    617: }
                    618:
                    619: static const struct filterops uhidread_filtops =
                    620:        { 1, NULL, filt_uhidrdetach, filt_uhidread };
                    621:
                    622: static const struct filterops uhid_seltrue_filtops =
                    623:        { 1, NULL, filt_uhidrdetach, filt_seltrue };
                    624:
                    625: int
                    626: uhidkqfilter(dev_t dev, struct knote *kn)
                    627: {
                    628:        struct uhid_softc *sc;
                    629:        struct klist *klist;
                    630:        int s;
                    631:
                    632:        USB_GET_SC(uhid, UHIDUNIT(dev), sc);
                    633:
                    634:        if (sc->sc_dying)
                    635:                return (EIO);
                    636:
                    637:        switch (kn->kn_filter) {
                    638:        case EVFILT_READ:
1.56      christos  639:                klist = &sc->sc_rsel.sel_klist;
1.55      jdolecek  640:                kn->kn_fop = &uhidread_filtops;
                    641:                break;
                    642:
                    643:        case EVFILT_WRITE:
1.56      christos  644:                klist = &sc->sc_rsel.sel_klist;
1.55      jdolecek  645:                kn->kn_fop = &uhid_seltrue_filtops;
                    646:                break;
                    647:
                    648:        default:
                    649:                return (1);
                    650:        }
                    651:
                    652:        kn->kn_hook = sc;
                    653:
                    654:        s = splusb();
                    655:        SLIST_INSERT_HEAD(klist, kn, kn_selnext);
                    656:        splx(s);
                    657:
                    658:        return (0);
1.1       augustss  659: }

CVSweb <webmaster@jp.NetBSD.org>