Home | History | Annotate | Line # | Download | only in usb
uhid.c revision 1.26.4.1
      1  1.26.4.1      fvdl /*	$NetBSD: uhid.c,v 1.26.4.1 1999/11/15 00:41:34 fvdl 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.6  augustss  * by Lennart Augustsson (augustss (at) carlstedt.se) 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.15  augustss  * HID spec: http://www.usb.org/developers/data/usbhid10.pdf
     42      1.15  augustss  */
     43       1.1  augustss 
     44       1.1  augustss #include <sys/param.h>
     45       1.1  augustss #include <sys/systm.h>
     46       1.1  augustss #include <sys/kernel.h>
     47       1.1  augustss #include <sys/malloc.h>
     48      1.20  augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
     49      1.14  augustss #include <sys/device.h>
     50      1.12  augustss #include <sys/ioctl.h>
     51      1.12  augustss #elif defined(__FreeBSD__)
     52      1.12  augustss #include <sys/ioccom.h>
     53      1.12  augustss #include <sys/filio.h>
     54      1.12  augustss #include <sys/module.h>
     55      1.12  augustss #include <sys/bus.h>
     56      1.14  augustss #include <sys/ioccom.h>
     57      1.12  augustss #endif
     58      1.18  augustss #include <sys/conf.h>
     59       1.1  augustss #include <sys/tty.h>
     60       1.1  augustss #include <sys/file.h>
     61       1.1  augustss #include <sys/select.h>
     62       1.1  augustss #include <sys/proc.h>
     63       1.1  augustss #include <sys/vnode.h>
     64       1.1  augustss #include <sys/poll.h>
     65       1.1  augustss 
     66       1.1  augustss #include <dev/usb/usb.h>
     67       1.1  augustss #include <dev/usb/usbhid.h>
     68       1.1  augustss 
     69       1.1  augustss #include <dev/usb/usbdi.h>
     70       1.1  augustss #include <dev/usb/usbdi_util.h>
     71       1.1  augustss #include <dev/usb/hid.h>
     72       1.1  augustss #include <dev/usb/usb_quirks.h>
     73       1.1  augustss 
     74      1.26  augustss #ifdef UHID_DEBUG
     75      1.19  augustss #define DPRINTF(x)	if (uhiddebug) logprintf x
     76      1.19  augustss #define DPRINTFN(n,x)	if (uhiddebug>(n)) logprintf x
     77       1.1  augustss int	uhiddebug = 0;
     78       1.1  augustss #else
     79       1.1  augustss #define DPRINTF(x)
     80       1.1  augustss #define DPRINTFN(n,x)
     81       1.1  augustss #endif
     82       1.1  augustss 
     83       1.1  augustss struct uhid_softc {
     84      1.24  augustss 	USBBASEDEVICE sc_dev;			/* base device */
     85       1.1  augustss 	usbd_interface_handle sc_iface;	/* interface */
     86       1.1  augustss 	usbd_pipe_handle sc_intrpipe;	/* interrupt pipe */
     87       1.1  augustss 	int sc_ep_addr;
     88       1.1  augustss 
     89       1.1  augustss 	int sc_isize;
     90       1.1  augustss 	int sc_osize;
     91       1.2  augustss 	int sc_fsize;
     92       1.1  augustss 	u_int8_t sc_iid;
     93       1.1  augustss 	u_int8_t sc_oid;
     94       1.2  augustss 	u_int8_t sc_fid;
     95       1.1  augustss 
     96       1.1  augustss 	char *sc_ibuf;
     97       1.1  augustss 	char *sc_obuf;
     98       1.1  augustss 
     99       1.1  augustss 	void *sc_repdesc;
    100       1.1  augustss 	int sc_repdesc_size;
    101       1.1  augustss 
    102       1.1  augustss 	struct clist sc_q;
    103       1.1  augustss 	struct selinfo sc_rsel;
    104       1.1  augustss 	u_char sc_state;	/* driver state */
    105       1.1  augustss #define	UHID_OPEN	0x01	/* device is open */
    106       1.5  augustss #define	UHID_ASLP	0x02	/* waiting for device data */
    107       1.1  augustss #define UHID_NEEDCLEAR	0x04	/* needs clearing endpoint stall */
    108       1.2  augustss #define UHID_IMMED	0x08	/* return read data immediately */
    109      1.18  augustss 
    110      1.18  augustss 	int sc_refcnt;
    111      1.18  augustss 	u_char sc_dying;
    112       1.1  augustss };
    113       1.1  augustss 
    114       1.1  augustss #define	UHIDUNIT(dev)	(minor(dev))
    115       1.1  augustss #define	UHID_CHUNK	128	/* chunk size for read */
    116       1.1  augustss #define	UHID_BSIZE	1020	/* buffer size */
    117       1.1  augustss 
    118      1.20  augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
    119      1.19  augustss cdev_decl(uhid);
    120      1.19  augustss #elif defined(__FreeBSD__)
    121      1.19  augustss d_open_t	uhidopen;
    122      1.19  augustss d_close_t	uhidclose;
    123      1.19  augustss d_read_t	uhidread;
    124      1.19  augustss d_write_t	uhidwrite;
    125      1.19  augustss d_ioctl_t	uhidioctl;
    126      1.19  augustss d_poll_t	uhidpoll;
    127      1.19  augustss 
    128      1.19  augustss #define		UHID_CDEV_MAJOR 122
    129      1.19  augustss 
    130      1.19  augustss static struct cdevsw uhid_cdevsw = {
    131      1.19  augustss 	/* open */	uhidopen,
    132      1.19  augustss 	/* close */	uhidclose,
    133      1.19  augustss 	/* read */	uhidread,
    134      1.19  augustss 	/* write */	uhidwrite,
    135      1.19  augustss 	/* ioctl */	uhidioctl,
    136      1.19  augustss 	/* poll */	uhidpoll,
    137      1.19  augustss 	/* mmap */	nommap,
    138      1.19  augustss 	/* strategy */	nostrategy,
    139      1.19  augustss 	/* name */	"uhid",
    140      1.19  augustss 	/* maj */	UHID_CDEV_MAJOR,
    141      1.19  augustss 	/* dump */	nodump,
    142      1.19  augustss 	/* psize */	nopsize,
    143      1.19  augustss 	/* flags */	0,
    144      1.19  augustss 	/* bmaj */	-1
    145      1.19  augustss };
    146      1.19  augustss #endif
    147      1.19  augustss 
    148  1.26.4.1      fvdl static void uhid_intr __P((usbd_xfer_handle, usbd_private_handle,
    149  1.26.4.1      fvdl 			   usbd_status));
    150      1.18  augustss 
    151  1.26.4.1      fvdl static int uhid_do_read __P((struct uhid_softc *, struct uio *uio, int));
    152  1.26.4.1      fvdl static int uhid_do_write __P((struct uhid_softc *, struct uio *uio, int));
    153  1.26.4.1      fvdl static int uhid_do_ioctl __P((struct uhid_softc *, u_long, caddr_t, int,
    154  1.26.4.1      fvdl 			      struct proc *));
    155       1.1  augustss 
    156      1.12  augustss USB_DECLARE_DRIVER(uhid);
    157       1.1  augustss 
    158      1.12  augustss USB_MATCH(uhid)
    159       1.1  augustss {
    160      1.12  augustss 	USB_MATCH_START(uhid, uaa);
    161       1.1  augustss 	usb_interface_descriptor_t *id;
    162       1.1  augustss 
    163       1.1  augustss 	if (!uaa->iface)
    164       1.1  augustss 		return (UMATCH_NONE);
    165       1.1  augustss 	id = usbd_get_interface_descriptor(uaa->iface);
    166       1.1  augustss 	if (!id || id->bInterfaceClass != UCLASS_HID)
    167       1.1  augustss 		return (UMATCH_NONE);
    168       1.1  augustss 	return (UMATCH_IFACECLASS_GENERIC);
    169       1.1  augustss }
    170       1.1  augustss 
    171      1.12  augustss USB_ATTACH(uhid)
    172       1.1  augustss {
    173      1.12  augustss 	USB_ATTACH_START(uhid, sc, uaa);
    174       1.1  augustss 	usbd_interface_handle iface = uaa->iface;
    175       1.1  augustss 	usb_interface_descriptor_t *id;
    176       1.1  augustss 	usb_endpoint_descriptor_t *ed;
    177       1.1  augustss 	int size;
    178       1.1  augustss 	void *desc;
    179  1.26.4.1      fvdl 	usbd_status err;
    180       1.1  augustss 	char devinfo[1024];
    181       1.1  augustss 
    182       1.1  augustss 	sc->sc_iface = iface;
    183       1.1  augustss 	id = usbd_get_interface_descriptor(iface);
    184       1.1  augustss 	usbd_devinfo(uaa->device, 0, devinfo);
    185      1.12  augustss 	USB_ATTACH_SETUP;
    186      1.12  augustss 	printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
    187       1.7  augustss 	       devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
    188      1.12  augustss 
    189       1.1  augustss 	ed = usbd_interface2endpoint_descriptor(iface, 0);
    190  1.26.4.1      fvdl 	if (ed == NULL) {
    191       1.1  augustss 		printf("%s: could not read endpoint descriptor\n",
    192      1.12  augustss 		       USBDEVNAME(sc->sc_dev));
    193      1.18  augustss 		sc->sc_dying = 1;
    194      1.12  augustss 		USB_ATTACH_ERROR_RETURN;
    195       1.1  augustss 	}
    196       1.1  augustss 
    197      1.11  augustss 	DPRINTFN(10,("uhid_attach: bLength=%d bDescriptorType=%d "
    198      1.11  augustss 		     "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d"
    199      1.11  augustss 		     " bInterval=%d\n",
    200      1.11  augustss 		     ed->bLength, ed->bDescriptorType,
    201      1.11  augustss 		     ed->bEndpointAddress & UE_ADDR,
    202      1.23  augustss 		     UE_GET_DIR(ed->bEndpointAddress)==UE_DIR_IN? "in" : "out",
    203      1.11  augustss 		     ed->bmAttributes & UE_XFERTYPE,
    204      1.11  augustss 		     UGETW(ed->wMaxPacketSize), ed->bInterval));
    205       1.1  augustss 
    206      1.23  augustss 	if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_IN ||
    207       1.1  augustss 	    (ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
    208      1.12  augustss 		printf("%s: unexpected endpoint\n", USBDEVNAME(sc->sc_dev));
    209      1.18  augustss 		sc->sc_dying = 1;
    210      1.12  augustss 		USB_ATTACH_ERROR_RETURN;
    211       1.1  augustss 	}
    212       1.1  augustss 
    213       1.1  augustss 	sc->sc_ep_addr = ed->bEndpointAddress;
    214       1.1  augustss 
    215      1.18  augustss 	desc = 0;
    216  1.26.4.1      fvdl 	err = usbd_alloc_report_desc(uaa->iface, &desc, &size, M_USBDEV);
    217  1.26.4.1      fvdl 	if (err) {
    218      1.12  augustss 		printf("%s: no report descriptor\n", USBDEVNAME(sc->sc_dev));
    219      1.18  augustss 		sc->sc_dying = 1;
    220  1.26.4.1      fvdl 		if (desc != NULL)
    221      1.18  augustss 			free(desc, M_USBDEV);
    222      1.12  augustss 		USB_ATTACH_ERROR_RETURN;
    223       1.1  augustss 	}
    224       1.1  augustss 
    225       1.1  augustss 	(void)usbd_set_idle(iface, 0, 0);
    226       1.1  augustss 
    227       1.2  augustss 	sc->sc_isize = hid_report_size(desc, size, hid_input,   &sc->sc_iid);
    228       1.2  augustss 	sc->sc_osize = hid_report_size(desc, size, hid_output,  &sc->sc_oid);
    229       1.2  augustss 	sc->sc_fsize = hid_report_size(desc, size, hid_feature, &sc->sc_fid);
    230       1.1  augustss 
    231       1.1  augustss 	sc->sc_repdesc = desc;
    232       1.1  augustss 	sc->sc_repdesc_size = size;
    233      1.12  augustss 
    234      1.12  augustss 	USB_ATTACH_SUCCESS_RETURN;
    235       1.1  augustss }
    236       1.1  augustss 
    237      1.26  augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
    238      1.18  augustss int
    239      1.18  augustss uhid_activate(self, act)
    240      1.24  augustss 	device_ptr_t self;
    241      1.18  augustss 	enum devact act;
    242      1.18  augustss {
    243      1.21  augustss 	struct uhid_softc *sc = (struct uhid_softc *)self;
    244      1.21  augustss 
    245      1.21  augustss 	switch (act) {
    246      1.21  augustss 	case DVACT_ACTIVATE:
    247      1.21  augustss 		return (EOPNOTSUPP);
    248      1.21  augustss 		break;
    249      1.21  augustss 
    250      1.21  augustss 	case DVACT_DEACTIVATE:
    251      1.21  augustss 		sc->sc_dying = 1;
    252      1.21  augustss 		break;
    253      1.21  augustss 	}
    254      1.18  augustss 	return (0);
    255      1.12  augustss }
    256      1.26  augustss #endif
    257      1.12  augustss 
    258      1.26  augustss USB_DETACH(uhid)
    259       1.1  augustss {
    260      1.26  augustss 	USB_DETACH_START(uhid, sc);
    261      1.26  augustss 	int s;
    262      1.26  augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
    263      1.18  augustss 	int maj, mn;
    264      1.18  augustss 
    265      1.18  augustss 	DPRINTF(("uhid_detach: sc=%p flags=%d\n", sc, flags));
    266      1.26  augustss #else
    267      1.26  augustss 	DPRINTF(("uhid_detach: sc=%p\n", sc));
    268      1.26  augustss #endif
    269       1.3  augustss 
    270      1.18  augustss 	sc->sc_dying = 1;
    271  1.26.4.1      fvdl 	if (sc->sc_intrpipe != NULL)
    272      1.18  augustss 		usbd_abort_pipe(sc->sc_intrpipe);
    273      1.18  augustss 
    274      1.18  augustss 	if (sc->sc_state & UHID_OPEN) {
    275      1.18  augustss 		s = splusb();
    276      1.18  augustss 		if (--sc->sc_refcnt >= 0) {
    277      1.18  augustss 			/* Wake everyone */
    278      1.18  augustss 			wakeup(&sc->sc_q);
    279      1.18  augustss 			/* Wait for processes to go away. */
    280      1.24  augustss 			usb_detach_wait(USBDEV(sc->sc_dev));
    281      1.18  augustss 		}
    282      1.18  augustss 		splx(s);
    283      1.18  augustss 	}
    284      1.18  augustss 
    285      1.26  augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
    286      1.18  augustss 	/* locate the major number */
    287      1.18  augustss 	for (maj = 0; maj < nchrdev; maj++)
    288      1.18  augustss 		if (cdevsw[maj].d_open == uhidopen)
    289      1.18  augustss 			break;
    290      1.18  augustss 
    291      1.18  augustss 	/* Nuke the vnodes for any open instances (calls close). */
    292      1.18  augustss 	mn = self->dv_unit;
    293      1.18  augustss 	vdevgone(maj, mn, mn, VCHR);
    294      1.26  augustss #elif defined(__FreeBSD__)
    295      1.26  augustss 	/* XXX not implemented yet */
    296      1.26  augustss #endif
    297      1.18  augustss 
    298      1.18  augustss 	free(sc->sc_repdesc, M_USBDEV);
    299      1.18  augustss 
    300      1.18  augustss 	return (0);
    301       1.1  augustss }
    302       1.1  augustss 
    303       1.1  augustss void
    304  1.26.4.1      fvdl uhid_intr(xfer, addr, status)
    305  1.26.4.1      fvdl 	usbd_xfer_handle xfer;
    306       1.1  augustss 	usbd_private_handle addr;
    307       1.1  augustss 	usbd_status status;
    308       1.1  augustss {
    309       1.1  augustss 	struct uhid_softc *sc = addr;
    310       1.1  augustss 
    311       1.1  augustss 	DPRINTFN(5, ("uhid_intr: status=%d\n", status));
    312       1.1  augustss 	DPRINTFN(5, ("uhid_intr: data = %02x %02x %02x\n",
    313       1.1  augustss 		     sc->sc_ibuf[0], sc->sc_ibuf[1], sc->sc_ibuf[2]));
    314       1.1  augustss 
    315       1.1  augustss 	if (status == USBD_CANCELLED)
    316       1.1  augustss 		return;
    317       1.1  augustss 
    318       1.1  augustss 	if (status != USBD_NORMAL_COMPLETION) {
    319       1.1  augustss 		DPRINTF(("uhid_intr: status=%d\n", status));
    320       1.1  augustss 		sc->sc_state |= UHID_NEEDCLEAR;
    321       1.1  augustss 		return;
    322       1.1  augustss 	}
    323       1.1  augustss 
    324       1.1  augustss 	(void) b_to_q(sc->sc_ibuf, sc->sc_isize, &sc->sc_q);
    325       1.1  augustss 
    326       1.1  augustss 	if (sc->sc_state & UHID_ASLP) {
    327       1.1  augustss 		sc->sc_state &= ~UHID_ASLP;
    328       1.1  augustss 		DPRINTFN(5, ("uhid_intr: waking %p\n", sc));
    329      1.18  augustss 		wakeup(&sc->sc_q);
    330       1.1  augustss 	}
    331       1.1  augustss 	selwakeup(&sc->sc_rsel);
    332       1.1  augustss }
    333       1.1  augustss 
    334       1.1  augustss int
    335       1.1  augustss uhidopen(dev, flag, mode, p)
    336       1.1  augustss 	dev_t dev;
    337       1.1  augustss 	int flag;
    338       1.1  augustss 	int mode;
    339       1.1  augustss 	struct proc *p;
    340       1.1  augustss {
    341      1.25  augustss 	struct uhid_softc *sc;
    342  1.26.4.1      fvdl 	usbd_status err;
    343      1.25  augustss 
    344      1.12  augustss 	USB_GET_SC_OPEN(uhid, UHIDUNIT(dev), sc);
    345       1.1  augustss 
    346      1.18  augustss 	DPRINTF(("uhidopen: sc=%p\n", sc));
    347       1.1  augustss 
    348      1.18  augustss 	if (sc->sc_dying)
    349      1.18  augustss 		return (ENXIO);
    350       1.1  augustss 
    351       1.1  augustss 	if (sc->sc_state & UHID_OPEN)
    352      1.12  augustss 		return (EBUSY);
    353      1.17  augustss 	sc->sc_state |= UHID_OPEN;
    354       1.1  augustss 
    355      1.17  augustss 	if (clalloc(&sc->sc_q, UHID_BSIZE, 0) == -1) {
    356      1.17  augustss 		sc->sc_state &= ~UHID_OPEN;
    357      1.12  augustss 		return (ENOMEM);
    358      1.17  augustss 	}
    359       1.1  augustss 
    360      1.18  augustss 	sc->sc_ibuf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
    361      1.18  augustss 	sc->sc_obuf = malloc(sc->sc_osize, M_USBDEV, M_WAITOK);
    362       1.1  augustss 
    363       1.1  augustss 	/* Set up interrupt pipe. */
    364  1.26.4.1      fvdl 	err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
    365  1.26.4.1      fvdl 		  USBD_SHORT_XFER_OK, &sc->sc_intrpipe, sc, sc->sc_ibuf,
    366  1.26.4.1      fvdl 		  sc->sc_isize, uhid_intr);
    367  1.26.4.1      fvdl 	if (err) {
    368      1.11  augustss 		DPRINTF(("uhidopen: usbd_open_pipe_intr failed, "
    369  1.26.4.1      fvdl 			 "error=%d\n",err));
    370      1.18  augustss 		free(sc->sc_ibuf, M_USBDEV);
    371      1.18  augustss 		free(sc->sc_obuf, M_USBDEV);
    372       1.1  augustss 		sc->sc_state &= ~UHID_OPEN;
    373       1.1  augustss 		return (EIO);
    374       1.1  augustss 	}
    375      1.17  augustss 
    376      1.17  augustss 	sc->sc_state &= ~UHID_IMMED;
    377      1.17  augustss 
    378      1.12  augustss 	return (0);
    379       1.1  augustss }
    380       1.1  augustss 
    381       1.1  augustss int
    382       1.1  augustss uhidclose(dev, flag, mode, p)
    383       1.1  augustss 	dev_t dev;
    384       1.1  augustss 	int flag;
    385       1.1  augustss 	int mode;
    386       1.1  augustss 	struct proc *p;
    387       1.1  augustss {
    388      1.25  augustss 	struct uhid_softc *sc;
    389      1.25  augustss 
    390      1.12  augustss 	USB_GET_SC(uhid, UHIDUNIT(dev), sc);
    391       1.1  augustss 
    392       1.1  augustss 	DPRINTF(("uhidclose: sc=%p\n", sc));
    393       1.1  augustss 
    394       1.1  augustss 	/* Disable interrupts. */
    395       1.1  augustss 	usbd_abort_pipe(sc->sc_intrpipe);
    396       1.1  augustss 	usbd_close_pipe(sc->sc_intrpipe);
    397      1.18  augustss 	sc->sc_intrpipe = 0;
    398       1.1  augustss 
    399       1.1  augustss 	clfree(&sc->sc_q);
    400       1.1  augustss 
    401      1.18  augustss 	free(sc->sc_ibuf, M_USBDEV);
    402      1.18  augustss 	free(sc->sc_obuf, M_USBDEV);
    403      1.17  augustss 
    404      1.17  augustss 	sc->sc_state &= ~UHID_OPEN;
    405       1.1  augustss 
    406      1.12  augustss 	return (0);
    407       1.1  augustss }
    408       1.1  augustss 
    409       1.1  augustss int
    410      1.18  augustss uhid_do_read(sc, uio, flag)
    411      1.18  augustss 	struct uhid_softc *sc;
    412       1.1  augustss 	struct uio *uio;
    413       1.1  augustss 	int flag;
    414       1.1  augustss {
    415       1.1  augustss 	int s;
    416       1.1  augustss 	int error = 0;
    417       1.1  augustss 	size_t length;
    418       1.1  augustss 	u_char buffer[UHID_CHUNK];
    419  1.26.4.1      fvdl 	usbd_status err;
    420       1.1  augustss 
    421       1.1  augustss 	DPRINTFN(1, ("uhidread\n"));
    422       1.2  augustss 	if (sc->sc_state & UHID_IMMED) {
    423       1.2  augustss 		DPRINTFN(1, ("uhidread immed\n"));
    424       1.2  augustss 
    425  1.26.4.1      fvdl 		err = usbd_get_report(sc->sc_iface, UHID_INPUT_REPORT,
    426      1.16  augustss 				    sc->sc_iid, buffer, sc->sc_isize);
    427  1.26.4.1      fvdl 		if (err)
    428       1.2  augustss 			return (EIO);
    429       1.2  augustss 		return (uiomove(buffer, sc->sc_isize, uio));
    430       1.2  augustss 	}
    431       1.2  augustss 
    432      1.10  augustss 	s = splusb();
    433       1.1  augustss 	while (sc->sc_q.c_cc == 0) {
    434       1.1  augustss 		if (flag & IO_NDELAY) {
    435       1.1  augustss 			splx(s);
    436      1.18  augustss 			return (EWOULDBLOCK);
    437       1.1  augustss 		}
    438       1.1  augustss 		sc->sc_state |= UHID_ASLP;
    439       1.1  augustss 		DPRINTFN(5, ("uhidread: sleep on %p\n", sc));
    440      1.18  augustss 		error = tsleep(&sc->sc_q, PZERO | PCATCH, "uhidrea", 0);
    441       1.1  augustss 		DPRINTFN(5, ("uhidread: woke, error=%d\n", error));
    442      1.18  augustss 		if (sc->sc_dying)
    443      1.18  augustss 			error = EIO;
    444       1.1  augustss 		if (error) {
    445       1.1  augustss 			sc->sc_state &= ~UHID_ASLP;
    446      1.18  augustss 			break;
    447       1.1  augustss 		}
    448       1.1  augustss 		if (sc->sc_state & UHID_NEEDCLEAR) {
    449       1.1  augustss 			DPRINTFN(-1,("uhidread: clearing stall\n"));
    450       1.1  augustss 			sc->sc_state &= ~UHID_NEEDCLEAR;
    451       1.1  augustss 			usbd_clear_endpoint_stall(sc->sc_intrpipe);
    452       1.1  augustss 		}
    453       1.1  augustss 	}
    454       1.1  augustss 	splx(s);
    455       1.1  augustss 
    456       1.1  augustss 	/* Transfer as many chunks as possible. */
    457      1.18  augustss 	while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0 && !error) {
    458       1.1  augustss 		length = min(sc->sc_q.c_cc, uio->uio_resid);
    459       1.1  augustss 		if (length > sizeof(buffer))
    460       1.1  augustss 			length = sizeof(buffer);
    461       1.1  augustss 
    462       1.1  augustss 		/* Remove a small chunk from the input queue. */
    463       1.1  augustss 		(void) q_to_b(&sc->sc_q, buffer, length);
    464       1.1  augustss 		DPRINTFN(5, ("uhidread: got %d chars\n", length));
    465       1.1  augustss 
    466       1.1  augustss 		/* Copy the data to the user process. */
    467       1.1  augustss 		if ((error = uiomove(buffer, length, uio)) != 0)
    468       1.1  augustss 			break;
    469       1.1  augustss 	}
    470       1.1  augustss 
    471       1.1  augustss 	return (error);
    472       1.1  augustss }
    473       1.1  augustss 
    474       1.1  augustss int
    475      1.18  augustss uhidread(dev, uio, flag)
    476       1.1  augustss 	dev_t dev;
    477       1.1  augustss 	struct uio *uio;
    478       1.1  augustss 	int flag;
    479       1.1  augustss {
    480      1.25  augustss 	struct uhid_softc *sc;
    481      1.25  augustss 	int error;
    482      1.25  augustss 
    483      1.18  augustss 	USB_GET_SC(uhid, UHIDUNIT(dev), sc);
    484      1.18  augustss 
    485      1.18  augustss 	sc->sc_refcnt++;
    486      1.18  augustss 	error = uhid_do_read(sc, uio, flag);
    487      1.18  augustss 	if (--sc->sc_refcnt < 0)
    488      1.24  augustss 		usb_detach_wakeup(USBDEV(sc->sc_dev));
    489      1.18  augustss 	return (error);
    490      1.18  augustss }
    491      1.18  augustss 
    492      1.18  augustss int
    493      1.18  augustss uhid_do_write(sc, uio, flag)
    494      1.18  augustss 	struct uhid_softc *sc;
    495      1.18  augustss 	struct uio *uio;
    496      1.18  augustss 	int flag;
    497      1.18  augustss {
    498       1.1  augustss 	int error;
    499       1.1  augustss 	int size;
    500  1.26.4.1      fvdl 	usbd_status err;
    501       1.1  augustss 
    502      1.18  augustss 	DPRINTFN(1, ("uhidwrite\n"));
    503      1.18  augustss 
    504      1.18  augustss 	if (sc->sc_dying)
    505       1.1  augustss 		return (EIO);
    506       1.1  augustss 
    507       1.1  augustss 	size = sc->sc_osize;
    508       1.1  augustss 	error = 0;
    509      1.18  augustss 	if (uio->uio_resid != size)
    510      1.18  augustss 		return (EINVAL);
    511      1.18  augustss 	error = uiomove(sc->sc_obuf, size, uio);
    512      1.18  augustss 	if (!error) {
    513       1.1  augustss 		if (sc->sc_oid)
    514  1.26.4.1      fvdl 			err = usbd_set_report(sc->sc_iface, UHID_OUTPUT_REPORT,
    515  1.26.4.1      fvdl 				  sc->sc_obuf[0], sc->sc_obuf+1, size-1);
    516       1.1  augustss 		else
    517  1.26.4.1      fvdl 			err = usbd_set_report(sc->sc_iface, UHID_OUTPUT_REPORT,
    518  1.26.4.1      fvdl 				  0, sc->sc_obuf, size);
    519  1.26.4.1      fvdl 		if (err)
    520       1.1  augustss 			error = EIO;
    521       1.1  augustss 	}
    522      1.18  augustss 
    523       1.1  augustss 	return (error);
    524       1.1  augustss }
    525       1.1  augustss 
    526       1.1  augustss int
    527      1.18  augustss uhidwrite(dev, uio, flag)
    528       1.1  augustss 	dev_t dev;
    529      1.18  augustss 	struct uio *uio;
    530      1.18  augustss 	int flag;
    531      1.18  augustss {
    532      1.25  augustss 	struct uhid_softc *sc;
    533      1.25  augustss 	int error;
    534      1.25  augustss 
    535      1.18  augustss 	USB_GET_SC(uhid, UHIDUNIT(dev), sc);
    536      1.18  augustss 
    537      1.18  augustss 	sc->sc_refcnt++;
    538      1.18  augustss 	error = uhid_do_write(sc, uio, flag);
    539      1.18  augustss 	if (--sc->sc_refcnt < 0)
    540      1.24  augustss 		usb_detach_wakeup(USBDEV(sc->sc_dev));
    541      1.18  augustss 	return (error);
    542      1.18  augustss }
    543      1.18  augustss 
    544      1.18  augustss int
    545      1.18  augustss uhid_do_ioctl(sc, cmd, addr, flag, p)
    546      1.18  augustss 	struct uhid_softc *sc;
    547       1.1  augustss 	u_long cmd;
    548       1.1  augustss 	caddr_t addr;
    549       1.1  augustss 	int flag;
    550       1.1  augustss 	struct proc *p;
    551       1.1  augustss {
    552       1.1  augustss 	struct usb_ctl_report_desc *rd;
    553       1.2  augustss 	struct usb_ctl_report *re;
    554       1.2  augustss 	int size, id;
    555  1.26.4.1      fvdl 	usbd_status err;
    556       1.1  augustss 
    557      1.18  augustss 	DPRINTFN(2, ("uhidioctl: cmd=%lx\n", cmd));
    558      1.18  augustss 
    559      1.18  augustss 	if (sc->sc_dying)
    560       1.1  augustss 		return (EIO);
    561       1.1  augustss 
    562       1.1  augustss 	switch (cmd) {
    563       1.2  augustss 	case FIONBIO:
    564       1.2  augustss 		/* All handled in the upper FS layer. */
    565       1.2  augustss 		break;
    566       1.2  augustss 
    567       1.1  augustss 	case USB_GET_REPORT_DESC:
    568       1.1  augustss 		rd = (struct usb_ctl_report_desc *)addr;
    569       1.1  augustss 		size = min(sc->sc_repdesc_size, sizeof rd->data);
    570       1.1  augustss 		rd->size = size;
    571       1.1  augustss 		memcpy(rd->data, sc->sc_repdesc, size);
    572       1.1  augustss 		break;
    573       1.2  augustss 
    574       1.2  augustss 	case USB_SET_IMMED:
    575       1.9  augustss 		if (*(int *)addr) {
    576  1.26.4.1      fvdl 			/* XXX should read into ibuf, but does it matter? */
    577  1.26.4.1      fvdl 			err = usbd_get_report(sc->sc_iface, UHID_INPUT_REPORT,
    578  1.26.4.1      fvdl 				  sc->sc_iid, sc->sc_ibuf, sc->sc_isize);
    579  1.26.4.1      fvdl 			if (err)
    580       1.9  augustss 				return (EOPNOTSUPP);
    581      1.12  augustss 
    582       1.2  augustss 			sc->sc_state |=  UHID_IMMED;
    583       1.9  augustss 		} else
    584       1.2  augustss 			sc->sc_state &= ~UHID_IMMED;
    585       1.2  augustss 		break;
    586       1.2  augustss 
    587       1.2  augustss 	case USB_GET_REPORT:
    588       1.2  augustss 		re = (struct usb_ctl_report *)addr;
    589       1.2  augustss 		switch (re->report) {
    590       1.2  augustss 		case UHID_INPUT_REPORT:
    591       1.2  augustss 			size = sc->sc_isize;
    592       1.2  augustss 			id = sc->sc_iid;
    593       1.2  augustss 			break;
    594       1.2  augustss 		case UHID_OUTPUT_REPORT:
    595       1.2  augustss 			size = sc->sc_osize;
    596       1.2  augustss 			id = sc->sc_oid;
    597       1.2  augustss 			break;
    598       1.2  augustss 		case UHID_FEATURE_REPORT:
    599       1.2  augustss 			size = sc->sc_fsize;
    600       1.2  augustss 			id = sc->sc_fid;
    601       1.2  augustss 			break;
    602       1.2  augustss 		default:
    603       1.2  augustss 			return (EINVAL);
    604       1.2  augustss 		}
    605  1.26.4.1      fvdl 		err = usbd_get_report(sc->sc_iface, re->report, id, re->data,
    606  1.26.4.1      fvdl 			  size);
    607  1.26.4.1      fvdl 		if (err)
    608       1.2  augustss 			return (EIO);
    609       1.2  augustss 		break;
    610       1.2  augustss 
    611       1.1  augustss 	default:
    612       1.1  augustss 		return (EINVAL);
    613       1.1  augustss 	}
    614       1.1  augustss 	return (0);
    615       1.1  augustss }
    616       1.1  augustss 
    617       1.1  augustss int
    618      1.18  augustss uhidioctl(dev, cmd, addr, flag, p)
    619      1.18  augustss 	dev_t dev;
    620      1.18  augustss 	u_long cmd;
    621      1.18  augustss 	caddr_t addr;
    622      1.18  augustss 	int flag;
    623      1.18  augustss 	struct proc *p;
    624      1.18  augustss {
    625      1.25  augustss 	struct uhid_softc *sc;
    626      1.25  augustss 	int error;
    627      1.25  augustss 
    628      1.18  augustss 	USB_GET_SC(uhid, UHIDUNIT(dev), sc);
    629      1.18  augustss 
    630      1.18  augustss 	sc->sc_refcnt++;
    631      1.18  augustss 	error = uhid_do_ioctl(sc, cmd, addr, flag, p);
    632      1.18  augustss 	if (--sc->sc_refcnt < 0)
    633      1.24  augustss 		usb_detach_wakeup(USBDEV(sc->sc_dev));
    634      1.18  augustss 	return (error);
    635      1.18  augustss }
    636      1.18  augustss 
    637      1.18  augustss int
    638       1.1  augustss uhidpoll(dev, events, p)
    639       1.1  augustss 	dev_t dev;
    640       1.1  augustss 	int events;
    641       1.1  augustss 	struct proc *p;
    642       1.1  augustss {
    643      1.25  augustss 	struct uhid_softc *sc;
    644       1.1  augustss 	int revents = 0;
    645       1.1  augustss 	int s;
    646      1.25  augustss 
    647      1.12  augustss 	USB_GET_SC(uhid, UHIDUNIT(dev), sc);
    648       1.1  augustss 
    649      1.18  augustss 	if (sc->sc_dying)
    650       1.1  augustss 		return (EIO);
    651       1.1  augustss 
    652      1.10  augustss 	s = splusb();
    653       1.1  augustss 	if (events & (POLLOUT | POLLWRNORM))
    654       1.1  augustss 		revents |= events & (POLLOUT | POLLWRNORM);
    655       1.4     veego 	if (events & (POLLIN | POLLRDNORM)) {
    656       1.1  augustss 		if (sc->sc_q.c_cc > 0)
    657       1.1  augustss 			revents |= events & (POLLIN | POLLRDNORM);
    658       1.1  augustss 		else
    659       1.1  augustss 			selrecord(p, &sc->sc_rsel);
    660       1.4     veego 	}
    661       1.1  augustss 
    662       1.1  augustss 	splx(s);
    663       1.1  augustss 	return (revents);
    664       1.1  augustss }
    665      1.12  augustss 
    666      1.12  augustss #if defined(__FreeBSD__)
    667      1.19  augustss DEV_DRIVER_MODULE(uhid, uhub, uhid_driver, uhid_devclass,
    668      1.19  augustss 		  uhid_cdevsw, usbd_driver_load, 0);
    669      1.12  augustss #endif
    670