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