Home | History | Annotate | Line # | Download | only in usb
uhid.c revision 1.2
      1  1.2  augustss /*	$NetBSD: uhid.c,v 1.2 1998/07/13 10:49:41 augustss 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.1  augustss  * Author: Lennart Augustsson <augustss (at) carlstedt.se>
      8  1.1  augustss  *         Carlstedt Research & Technology
      9  1.1  augustss  *
     10  1.1  augustss  * Redistribution and use in source and binary forms, with or without
     11  1.1  augustss  * modification, are permitted provided that the following conditions
     12  1.1  augustss  * are met:
     13  1.1  augustss  * 1. Redistributions of source code must retain the above copyright
     14  1.1  augustss  *    notice, this list of conditions and the following disclaimer.
     15  1.1  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  augustss  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  augustss  *    documentation and/or other materials provided with the distribution.
     18  1.1  augustss  * 3. All advertising materials mentioning features or use of this software
     19  1.1  augustss  *    must display the following acknowledgement:
     20  1.1  augustss  *        This product includes software developed by the NetBSD
     21  1.1  augustss  *        Foundation, Inc. and its contributors.
     22  1.1  augustss  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  augustss  *    contributors may be used to endorse or promote products derived
     24  1.1  augustss  *    from this software without specific prior written permission.
     25  1.1  augustss  *
     26  1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  augustss  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  augustss  */
     38  1.1  augustss 
     39  1.1  augustss 
     40  1.1  augustss #include <sys/param.h>
     41  1.1  augustss #include <sys/systm.h>
     42  1.1  augustss #include <sys/kernel.h>
     43  1.1  augustss #include <sys/malloc.h>
     44  1.1  augustss #include <sys/device.h>
     45  1.1  augustss #include <sys/ioctl.h>
     46  1.1  augustss #include <sys/tty.h>
     47  1.1  augustss #include <sys/file.h>
     48  1.1  augustss #include <sys/select.h>
     49  1.1  augustss #include <sys/proc.h>
     50  1.1  augustss #include <sys/vnode.h>
     51  1.1  augustss #include <sys/device.h>
     52  1.1  augustss #include <sys/poll.h>
     53  1.1  augustss 
     54  1.1  augustss #include <dev/usb/usb.h>
     55  1.1  augustss #include <dev/usb/usbhid.h>
     56  1.1  augustss 
     57  1.1  augustss #include <dev/usb/usbdi.h>
     58  1.1  augustss #include <dev/usb/usbdi_util.h>
     59  1.1  augustss #include <dev/usb/hid.h>
     60  1.1  augustss #include <dev/usb/usb_quirks.h>
     61  1.1  augustss 
     62  1.1  augustss #ifdef USB_DEBUG
     63  1.1  augustss #define DPRINTF(x)	if (uhiddebug) printf x
     64  1.1  augustss #define DPRINTFN(n,x)	if (uhiddebug>(n)) printf x
     65  1.1  augustss int	uhiddebug = 0;
     66  1.1  augustss #else
     67  1.1  augustss #define DPRINTF(x)
     68  1.1  augustss #define DPRINTFN(n,x)
     69  1.1  augustss #endif
     70  1.1  augustss 
     71  1.1  augustss struct uhid_softc {
     72  1.1  augustss 	struct device sc_dev;		/* base device */
     73  1.1  augustss 	usbd_interface_handle sc_iface;	/* interface */
     74  1.1  augustss 	usbd_pipe_handle sc_intrpipe;	/* interrupt pipe */
     75  1.1  augustss 	int sc_ep_addr;
     76  1.1  augustss 
     77  1.1  augustss 	int sc_isize;
     78  1.1  augustss 	int sc_osize;
     79  1.2  augustss 	int sc_fsize;
     80  1.1  augustss 	u_int8_t sc_iid;
     81  1.1  augustss 	u_int8_t sc_oid;
     82  1.2  augustss 	u_int8_t sc_fid;
     83  1.1  augustss 
     84  1.1  augustss 	char *sc_ibuf;
     85  1.1  augustss 	char *sc_obuf;
     86  1.1  augustss 
     87  1.1  augustss 	void *sc_repdesc;
     88  1.1  augustss 	int sc_repdesc_size;
     89  1.1  augustss 
     90  1.1  augustss 	struct clist sc_q;
     91  1.1  augustss 	struct selinfo sc_rsel;
     92  1.1  augustss 	u_char sc_state;	/* driver state */
     93  1.1  augustss #define	UHID_OPEN	0x01	/* device is open */
     94  1.1  augustss #define	UHID_ASLP	0x02	/* waiting for mouse data */
     95  1.1  augustss #define UHID_NEEDCLEAR	0x04	/* needs clearing endpoint stall */
     96  1.2  augustss #define UHID_IMMED	0x08	/* return read data immediately */
     97  1.1  augustss 	int sc_disconnected;	/* device is gone */
     98  1.1  augustss };
     99  1.1  augustss 
    100  1.1  augustss #define	UHIDUNIT(dev)	(minor(dev))
    101  1.1  augustss #define	UHID_CHUNK	128	/* chunk size for read */
    102  1.1  augustss #define	UHID_BSIZE	1020	/* buffer size */
    103  1.1  augustss 
    104  1.1  augustss int uhid_match __P((struct device *, struct cfdata *, void *));
    105  1.1  augustss void uhid_attach __P((struct device *, struct device *, void *));
    106  1.1  augustss 
    107  1.1  augustss int uhidopen __P((dev_t, int, int, struct proc *));
    108  1.1  augustss int uhidclose __P((dev_t, int, int, struct proc *p));
    109  1.1  augustss int uhidread __P((dev_t, struct uio *uio, int));
    110  1.1  augustss int uhidwrite __P((dev_t, struct uio *uio, int));
    111  1.1  augustss int uhidioctl __P((dev_t, u_long, caddr_t, int, struct proc *));
    112  1.1  augustss int uhidpoll __P((dev_t, int, struct proc *));
    113  1.1  augustss void uhid_intr __P((usbd_request_handle, usbd_private_handle, usbd_status));
    114  1.1  augustss void uhid_disco __P((void *));
    115  1.1  augustss 
    116  1.1  augustss extern struct cfdriver uhid_cd;
    117  1.1  augustss 
    118  1.1  augustss struct cfattach uhid_ca = {
    119  1.1  augustss 	sizeof(struct uhid_softc), uhid_match, uhid_attach
    120  1.1  augustss };
    121  1.1  augustss 
    122  1.1  augustss int
    123  1.1  augustss uhid_match(parent, match, aux)
    124  1.1  augustss 	struct device *parent;
    125  1.1  augustss 	struct cfdata *match;
    126  1.1  augustss 	void *aux;
    127  1.1  augustss {
    128  1.1  augustss 	struct usb_attach_arg *uaa = aux;
    129  1.1  augustss 	usb_interface_descriptor_t *id;
    130  1.1  augustss 
    131  1.1  augustss 	if (!uaa->iface)
    132  1.1  augustss 		return (UMATCH_NONE);
    133  1.1  augustss 	id = usbd_get_interface_descriptor(uaa->iface);
    134  1.1  augustss 	if (!id || id->bInterfaceClass != UCLASS_HID)
    135  1.1  augustss 		return (UMATCH_NONE);
    136  1.1  augustss 	return (UMATCH_IFACECLASS_GENERIC);
    137  1.1  augustss }
    138  1.1  augustss 
    139  1.1  augustss void
    140  1.1  augustss uhid_attach(parent, self, aux)
    141  1.1  augustss 	struct device *parent;
    142  1.1  augustss 	struct device *self;
    143  1.1  augustss 	void *aux;
    144  1.1  augustss {
    145  1.1  augustss 	struct uhid_softc *sc = (struct uhid_softc *)self;
    146  1.1  augustss 	struct usb_attach_arg *uaa = aux;
    147  1.1  augustss 	usbd_interface_handle iface = uaa->iface;
    148  1.1  augustss 	usb_interface_descriptor_t *id;
    149  1.1  augustss 	usb_endpoint_descriptor_t *ed;
    150  1.1  augustss 	int size;
    151  1.1  augustss 	void *desc;
    152  1.1  augustss 	usbd_status r;
    153  1.1  augustss 	char devinfo[1024];
    154  1.1  augustss 
    155  1.1  augustss 	sc->sc_disconnected = 1;
    156  1.1  augustss 	sc->sc_iface = iface;
    157  1.1  augustss 	id = usbd_get_interface_descriptor(iface);
    158  1.1  augustss 	usbd_devinfo(uaa->device, 0, devinfo);
    159  1.1  augustss 	printf(": %s (interface class %d/%d)\n", devinfo,
    160  1.1  augustss 	       id->bInterfaceClass, id->bInterfaceSubClass);
    161  1.1  augustss 	ed = usbd_interface2endpoint_descriptor(iface, 0);
    162  1.1  augustss 	if (!ed) {
    163  1.1  augustss 		printf("%s: could not read endpoint descriptor\n",
    164  1.1  augustss 		       sc->sc_dev.dv_xname);
    165  1.1  augustss 		return;
    166  1.1  augustss 	}
    167  1.1  augustss 
    168  1.1  augustss 	DPRINTFN(10,("uhid_attach: \
    169  1.1  augustss bLength=%d bDescriptorType=%d bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d bInterval=%d\n",
    170  1.1  augustss 	       ed->bLength, ed->bDescriptorType, ed->bEndpointAddress & UE_ADDR,
    171  1.1  augustss 	       ed->bEndpointAddress & UE_IN ? "in" : "out",
    172  1.1  augustss 	       ed->bmAttributes & UE_XFERTYPE,
    173  1.1  augustss 	       UGETW(ed->wMaxPacketSize), ed->bInterval));
    174  1.1  augustss 
    175  1.1  augustss 	if ((ed->bEndpointAddress & UE_IN) != UE_IN ||
    176  1.1  augustss 	    (ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
    177  1.1  augustss 		printf("%s: unexpected endpoint\n",
    178  1.1  augustss 		       sc->sc_dev.dv_xname);
    179  1.1  augustss 		return;
    180  1.1  augustss 	}
    181  1.1  augustss 
    182  1.1  augustss 	sc->sc_ep_addr = ed->bEndpointAddress;
    183  1.1  augustss 	sc->sc_disconnected = 0;
    184  1.1  augustss 
    185  1.1  augustss 	r = usbd_alloc_report_desc(uaa->iface, &desc, &size, M_USB);
    186  1.1  augustss 	if (r != USBD_NORMAL_COMPLETION) {
    187  1.1  augustss 		printf("%s: no report descriptor\n", sc->sc_dev.dv_xname);
    188  1.1  augustss 		return;
    189  1.1  augustss 	}
    190  1.1  augustss 
    191  1.1  augustss 	(void)usbd_set_idle(iface, 0, 0);
    192  1.1  augustss 
    193  1.2  augustss 	sc->sc_isize = hid_report_size(desc, size, hid_input,   &sc->sc_iid);
    194  1.2  augustss 	sc->sc_osize = hid_report_size(desc, size, hid_output,  &sc->sc_oid);
    195  1.2  augustss 	sc->sc_fsize = hid_report_size(desc, size, hid_feature, &sc->sc_fid);
    196  1.1  augustss 
    197  1.1  augustss 	sc->sc_repdesc = desc;
    198  1.1  augustss 	sc->sc_repdesc_size = size;
    199  1.1  augustss }
    200  1.1  augustss 
    201  1.1  augustss void
    202  1.1  augustss uhid_disco(p)
    203  1.1  augustss 	void *p;
    204  1.1  augustss {
    205  1.1  augustss 	struct uhid_softc *sc = p;
    206  1.1  augustss 	sc->sc_disconnected = 1;
    207  1.1  augustss }
    208  1.1  augustss 
    209  1.1  augustss void
    210  1.1  augustss uhid_intr(reqh, addr, status)
    211  1.1  augustss 	usbd_request_handle reqh;
    212  1.1  augustss 	usbd_private_handle addr;
    213  1.1  augustss 	usbd_status status;
    214  1.1  augustss {
    215  1.1  augustss 	struct uhid_softc *sc = addr;
    216  1.1  augustss 
    217  1.1  augustss 	DPRINTFN(5, ("uhid_intr: status=%d\n", status));
    218  1.1  augustss 	DPRINTFN(5, ("uhid_intr: data = %02x %02x %02x\n",
    219  1.1  augustss 		     sc->sc_ibuf[0], sc->sc_ibuf[1], sc->sc_ibuf[2]));
    220  1.1  augustss 
    221  1.1  augustss 	if (status == USBD_CANCELLED)
    222  1.1  augustss 		return;
    223  1.1  augustss 
    224  1.1  augustss 	if (status != USBD_NORMAL_COMPLETION) {
    225  1.1  augustss 		DPRINTF(("uhid_intr: status=%d\n", status));
    226  1.1  augustss 		sc->sc_state |= UHID_NEEDCLEAR;
    227  1.1  augustss 		return;
    228  1.1  augustss 	}
    229  1.1  augustss 
    230  1.1  augustss 	(void) b_to_q(sc->sc_ibuf, sc->sc_isize, &sc->sc_q);
    231  1.1  augustss 
    232  1.1  augustss 	if (sc->sc_state & UHID_ASLP) {
    233  1.1  augustss 		sc->sc_state &= ~UHID_ASLP;
    234  1.1  augustss 		DPRINTFN(5, ("uhid_intr: waking %p\n", sc));
    235  1.1  augustss 		wakeup((caddr_t)sc);
    236  1.1  augustss 	}
    237  1.1  augustss 	selwakeup(&sc->sc_rsel);
    238  1.1  augustss }
    239  1.1  augustss 
    240  1.1  augustss int
    241  1.1  augustss uhidopen(dev, flag, mode, p)
    242  1.1  augustss 	dev_t dev;
    243  1.1  augustss 	int flag;
    244  1.1  augustss 	int mode;
    245  1.1  augustss 	struct proc *p;
    246  1.1  augustss {
    247  1.1  augustss 	int unit = UHIDUNIT(dev);
    248  1.1  augustss 	struct uhid_softc *sc;
    249  1.1  augustss 	usbd_status r;
    250  1.1  augustss 
    251  1.1  augustss 	if (unit >= uhid_cd.cd_ndevs)
    252  1.1  augustss 		return ENXIO;
    253  1.1  augustss 	sc = uhid_cd.cd_devs[unit];
    254  1.1  augustss 	if (!sc)
    255  1.1  augustss 		return ENXIO;
    256  1.1  augustss 
    257  1.1  augustss 	DPRINTF(("uhidopen: sc=%p, disco=%d\n", sc, sc->sc_disconnected));
    258  1.1  augustss 
    259  1.1  augustss 	if (sc->sc_disconnected)
    260  1.1  augustss 		return (EIO);
    261  1.1  augustss 
    262  1.1  augustss 	if (sc->sc_state & UHID_OPEN)
    263  1.1  augustss 		return EBUSY;
    264  1.1  augustss 
    265  1.1  augustss 	if (clalloc(&sc->sc_q, UHID_BSIZE, 0) == -1)
    266  1.1  augustss 		return ENOMEM;
    267  1.1  augustss 
    268  1.1  augustss 	sc->sc_state |= UHID_OPEN;
    269  1.2  augustss 	sc->sc_state &= ~UHID_IMMED;
    270  1.1  augustss 
    271  1.1  augustss 	sc->sc_ibuf = malloc(sc->sc_isize, M_USB, M_WAITOK);
    272  1.1  augustss 	sc->sc_obuf = malloc(sc->sc_osize, M_USB, M_WAITOK);
    273  1.1  augustss 
    274  1.1  augustss 	/* Set up interrupt pipe. */
    275  1.1  augustss 	r = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
    276  1.1  augustss 				USBD_SHORT_XFER_OK,
    277  1.1  augustss 				&sc->sc_intrpipe, sc, sc->sc_ibuf,
    278  1.1  augustss 				sc->sc_isize, uhid_intr);
    279  1.1  augustss 	if (r != USBD_NORMAL_COMPLETION) {
    280  1.1  augustss 		DPRINTF(("uhidopen: usbd_open_pipe_intr failed, error=%d\n",r));
    281  1.1  augustss 		sc->sc_state &= ~UHID_OPEN;
    282  1.1  augustss 		return (EIO);
    283  1.1  augustss 	}
    284  1.1  augustss 	usbd_set_disco(sc->sc_intrpipe, uhid_disco, sc);
    285  1.1  augustss 
    286  1.1  augustss 	return 0;
    287  1.1  augustss }
    288  1.1  augustss 
    289  1.1  augustss int
    290  1.1  augustss uhidclose(dev, flag, mode, p)
    291  1.1  augustss 	dev_t dev;
    292  1.1  augustss 	int flag;
    293  1.1  augustss 	int mode;
    294  1.1  augustss 	struct proc *p;
    295  1.1  augustss {
    296  1.1  augustss 	struct uhid_softc *sc = uhid_cd.cd_devs[UHIDUNIT(dev)];
    297  1.1  augustss 
    298  1.1  augustss 	if (sc->sc_disconnected)
    299  1.1  augustss 		return (EIO);
    300  1.1  augustss 
    301  1.1  augustss 	DPRINTF(("uhidclose: sc=%p\n", sc));
    302  1.1  augustss 
    303  1.1  augustss 	/* Disable interrupts. */
    304  1.1  augustss 	usbd_abort_pipe(sc->sc_intrpipe);
    305  1.1  augustss 	usbd_close_pipe(sc->sc_intrpipe);
    306  1.1  augustss 
    307  1.1  augustss 	sc->sc_state &= ~UHID_OPEN;
    308  1.1  augustss 
    309  1.1  augustss 	clfree(&sc->sc_q);
    310  1.1  augustss 
    311  1.1  augustss 	free(sc->sc_ibuf, M_USB);
    312  1.1  augustss 	free(sc->sc_obuf, M_USB);
    313  1.1  augustss 
    314  1.1  augustss 	return 0;
    315  1.1  augustss }
    316  1.1  augustss 
    317  1.1  augustss int
    318  1.1  augustss uhidread(dev, uio, flag)
    319  1.1  augustss 	dev_t dev;
    320  1.1  augustss 	struct uio *uio;
    321  1.1  augustss 	int flag;
    322  1.1  augustss {
    323  1.1  augustss 	struct uhid_softc *sc = uhid_cd.cd_devs[UHIDUNIT(dev)];
    324  1.1  augustss 	int s;
    325  1.1  augustss 	int error = 0;
    326  1.1  augustss 	size_t length;
    327  1.1  augustss 	u_char buffer[UHID_CHUNK];
    328  1.2  augustss 	usbd_status r;
    329  1.1  augustss 
    330  1.1  augustss 	if (sc->sc_disconnected)
    331  1.1  augustss 		return (EIO);
    332  1.1  augustss 
    333  1.1  augustss 	DPRINTFN(1, ("uhidread\n"));
    334  1.2  augustss 	if (sc->sc_state & UHID_IMMED) {
    335  1.2  augustss 		DPRINTFN(1, ("uhidread immed\n"));
    336  1.2  augustss 
    337  1.2  augustss 		r = usbd_get_report(sc->sc_iface, UHID_INPUT_REPORT,
    338  1.2  augustss 				    sc->sc_iid, sc->sc_ibuf, sc->sc_isize);
    339  1.2  augustss 		if (r != USBD_NORMAL_COMPLETION)
    340  1.2  augustss 			return (EIO);
    341  1.2  augustss 		return (uiomove(buffer, sc->sc_isize, uio));
    342  1.2  augustss 	}
    343  1.2  augustss 
    344  1.1  augustss 	s = spltty();
    345  1.1  augustss 	while (sc->sc_q.c_cc == 0) {
    346  1.1  augustss 		if (flag & IO_NDELAY) {
    347  1.1  augustss 			splx(s);
    348  1.1  augustss 			return EWOULDBLOCK;
    349  1.1  augustss 		}
    350  1.1  augustss 		sc->sc_state |= UHID_ASLP;
    351  1.1  augustss 		DPRINTFN(5, ("uhidread: sleep on %p\n", sc));
    352  1.1  augustss 		error = tsleep((caddr_t)sc, PZERO | PCATCH, "uhidrea", 0);
    353  1.1  augustss 		DPRINTFN(5, ("uhidread: woke, error=%d\n", error));
    354  1.1  augustss 		if (error) {
    355  1.1  augustss 			sc->sc_state &= ~UHID_ASLP;
    356  1.1  augustss 			splx(s);
    357  1.1  augustss 			return (error);
    358  1.1  augustss 		}
    359  1.1  augustss 		if (sc->sc_state & UHID_NEEDCLEAR) {
    360  1.1  augustss 			DPRINTFN(-1,("uhidread: clearing stall\n"));
    361  1.1  augustss 			sc->sc_state &= ~UHID_NEEDCLEAR;
    362  1.1  augustss 			usbd_clear_endpoint_stall(sc->sc_intrpipe);
    363  1.1  augustss 		}
    364  1.1  augustss 	}
    365  1.1  augustss 	splx(s);
    366  1.1  augustss 
    367  1.1  augustss 	/* Transfer as many chunks as possible. */
    368  1.1  augustss 	while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0) {
    369  1.1  augustss 		length = min(sc->sc_q.c_cc, uio->uio_resid);
    370  1.1  augustss 		if (length > sizeof(buffer))
    371  1.1  augustss 			length = sizeof(buffer);
    372  1.1  augustss 
    373  1.1  augustss 		/* Remove a small chunk from the input queue. */
    374  1.1  augustss 		(void) q_to_b(&sc->sc_q, buffer, length);
    375  1.1  augustss 		DPRINTFN(5, ("uhidread: got %d chars\n", length));
    376  1.1  augustss 
    377  1.1  augustss 		/* Copy the data to the user process. */
    378  1.1  augustss 		if ((error = uiomove(buffer, length, uio)) != 0)
    379  1.1  augustss 			break;
    380  1.1  augustss 	}
    381  1.1  augustss 
    382  1.1  augustss 	return (error);
    383  1.1  augustss }
    384  1.1  augustss 
    385  1.1  augustss int
    386  1.1  augustss uhidwrite(dev, uio, flag)
    387  1.1  augustss 	dev_t dev;
    388  1.1  augustss 	struct uio *uio;
    389  1.1  augustss 	int flag;
    390  1.1  augustss {
    391  1.1  augustss 	struct uhid_softc *sc = uhid_cd.cd_devs[UHIDUNIT(dev)];
    392  1.1  augustss 	int error;
    393  1.1  augustss 	int size;
    394  1.1  augustss 	usbd_status r;
    395  1.1  augustss 
    396  1.1  augustss 	if (sc->sc_disconnected)
    397  1.1  augustss 		return (EIO);
    398  1.1  augustss 
    399  1.1  augustss 	DPRINTFN(1, ("uhidwrite\n"));
    400  1.1  augustss 
    401  1.1  augustss 	size = sc->sc_osize;
    402  1.1  augustss 	error = 0;
    403  1.1  augustss 	while (uio->uio_resid > 0) {
    404  1.1  augustss 		if (uio->uio_resid != size)
    405  1.1  augustss 			return (0);
    406  1.1  augustss 		if ((error = uiomove(sc->sc_obuf, size, uio)) != 0)
    407  1.1  augustss 			break;
    408  1.1  augustss 		if (sc->sc_oid)
    409  1.1  augustss 			r = usbd_set_report(sc->sc_iface, UHID_OUTPUT_REPORT,
    410  1.1  augustss 					    sc->sc_obuf[0],
    411  1.1  augustss 					    sc->sc_obuf+1, size-1);
    412  1.1  augustss 		else
    413  1.1  augustss 			r = usbd_set_report(sc->sc_iface, UHID_OUTPUT_REPORT,
    414  1.1  augustss 					    0, sc->sc_obuf, size);
    415  1.1  augustss 		if (r != USBD_NORMAL_COMPLETION) {
    416  1.1  augustss 			error = EIO;
    417  1.1  augustss 			break;
    418  1.1  augustss 		}
    419  1.1  augustss 	}
    420  1.1  augustss 	return (error);
    421  1.1  augustss }
    422  1.1  augustss 
    423  1.1  augustss int
    424  1.1  augustss uhidioctl(dev, cmd, addr, flag, p)
    425  1.1  augustss 	dev_t dev;
    426  1.1  augustss 	u_long cmd;
    427  1.1  augustss 	caddr_t addr;
    428  1.1  augustss 	int flag;
    429  1.1  augustss 	struct proc *p;
    430  1.1  augustss {
    431  1.1  augustss 	struct uhid_softc *sc = uhid_cd.cd_devs[UHIDUNIT(dev)];
    432  1.1  augustss 	struct usb_ctl_report_desc *rd;
    433  1.2  augustss 	struct usb_ctl_report *re;
    434  1.2  augustss 	int size, id;
    435  1.2  augustss 	usbd_status r;
    436  1.1  augustss 
    437  1.1  augustss 	if (sc->sc_disconnected)
    438  1.1  augustss 		return (EIO);
    439  1.1  augustss 
    440  1.1  augustss 	DPRINTFN(2, ("uhidioctl: cmd=%lx\n", cmd));
    441  1.1  augustss 	switch (cmd) {
    442  1.2  augustss 	case FIONBIO:
    443  1.2  augustss 		/* All handled in the upper FS layer. */
    444  1.2  augustss 		break;
    445  1.2  augustss 
    446  1.1  augustss 	case USB_GET_REPORT_DESC:
    447  1.1  augustss 		rd = (struct usb_ctl_report_desc *)addr;
    448  1.1  augustss 		size = min(sc->sc_repdesc_size, sizeof rd->data);
    449  1.1  augustss 		rd->size = size;
    450  1.1  augustss 		memcpy(rd->data, sc->sc_repdesc, size);
    451  1.1  augustss 		break;
    452  1.2  augustss 
    453  1.2  augustss 	case USB_SET_IMMED:
    454  1.2  augustss 		if (*(int *)addr)
    455  1.2  augustss 			sc->sc_state |=  UHID_IMMED;
    456  1.2  augustss 		else
    457  1.2  augustss 			sc->sc_state &= ~UHID_IMMED;
    458  1.2  augustss 		break;
    459  1.2  augustss 
    460  1.2  augustss 	case USB_GET_REPORT:
    461  1.2  augustss 		re = (struct usb_ctl_report *)addr;
    462  1.2  augustss 		switch (re->report) {
    463  1.2  augustss 		case UHID_INPUT_REPORT:
    464  1.2  augustss 			size = sc->sc_isize;
    465  1.2  augustss 			id = sc->sc_iid;
    466  1.2  augustss 			break;
    467  1.2  augustss 		case UHID_OUTPUT_REPORT:
    468  1.2  augustss 			size = sc->sc_osize;
    469  1.2  augustss 			id = sc->sc_oid;
    470  1.2  augustss 			break;
    471  1.2  augustss 		case UHID_FEATURE_REPORT:
    472  1.2  augustss 			size = sc->sc_fsize;
    473  1.2  augustss 			id = sc->sc_fid;
    474  1.2  augustss 			break;
    475  1.2  augustss 		default:
    476  1.2  augustss 			return (EINVAL);
    477  1.2  augustss 		}
    478  1.2  augustss 		r = usbd_get_report(sc->sc_iface, re->report, id,
    479  1.2  augustss 				    re->data, size);
    480  1.2  augustss 		if (r != USBD_NORMAL_COMPLETION)
    481  1.2  augustss 			return (EIO);
    482  1.2  augustss 		break;
    483  1.2  augustss 
    484  1.1  augustss 	default:
    485  1.1  augustss 		return (EINVAL);
    486  1.1  augustss 	}
    487  1.1  augustss 	return (0);
    488  1.1  augustss }
    489  1.1  augustss 
    490  1.1  augustss int
    491  1.1  augustss uhidpoll(dev, events, p)
    492  1.1  augustss 	dev_t dev;
    493  1.1  augustss 	int events;
    494  1.1  augustss 	struct proc *p;
    495  1.1  augustss {
    496  1.1  augustss 	struct uhid_softc *sc = uhid_cd.cd_devs[UHIDUNIT(dev)];
    497  1.1  augustss 	int revents = 0;
    498  1.1  augustss 	int s;
    499  1.1  augustss 
    500  1.1  augustss 	if (sc->sc_disconnected)
    501  1.1  augustss 		return (EIO);
    502  1.1  augustss 
    503  1.1  augustss 	s = spltty();
    504  1.1  augustss 	if (events & (POLLOUT | POLLWRNORM))
    505  1.1  augustss 		revents |= events & (POLLOUT | POLLWRNORM);
    506  1.1  augustss 	if (events & (POLLIN | POLLRDNORM))
    507  1.1  augustss 		if (sc->sc_q.c_cc > 0)
    508  1.1  augustss 			revents |= events & (POLLIN | POLLRDNORM);
    509  1.1  augustss 		else
    510  1.1  augustss 			selrecord(p, &sc->sc_rsel);
    511  1.1  augustss 
    512  1.1  augustss 	splx(s);
    513  1.1  augustss 	return (revents);
    514  1.1  augustss }
    515