Home | History | Annotate | Line # | Download | only in usb
uhid.c revision 1.26.2.1
      1  1.26.2.1    bouyer /*	$NetBSD: uhid.c,v 1.26.2.1 2000/11/20 11:43:27 bouyer Exp $	*/
      2  1.26.2.1    bouyer /*	$FreeBSD: src/sys/dev/usb/uhid.c,v 1.22 1999/11/17 22:33:43 n_hibma Exp $	*/
      3       1.1  augustss 
      4       1.1  augustss /*
      5       1.1  augustss  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      6       1.1  augustss  * All rights reserved.
      7       1.1  augustss  *
      8       1.6  augustss  * This code is derived from software contributed to The NetBSD Foundation
      9  1.26.2.1    bouyer  * by Lennart Augustsson (lennart (at) augustsson.net) at
     10       1.6  augustss  * Carlstedt Research & Technology.
     11       1.1  augustss  *
     12       1.1  augustss  * Redistribution and use in source and binary forms, with or without
     13       1.1  augustss  * modification, are permitted provided that the following conditions
     14       1.1  augustss  * are met:
     15       1.1  augustss  * 1. Redistributions of source code must retain the above copyright
     16       1.1  augustss  *    notice, this list of conditions and the following disclaimer.
     17       1.1  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     18       1.1  augustss  *    notice, this list of conditions and the following disclaimer in the
     19       1.1  augustss  *    documentation and/or other materials provided with the distribution.
     20       1.1  augustss  * 3. All advertising materials mentioning features or use of this software
     21       1.1  augustss  *    must display the following acknowledgement:
     22       1.1  augustss  *        This product includes software developed by the NetBSD
     23       1.1  augustss  *        Foundation, Inc. and its contributors.
     24       1.1  augustss  * 4. Neither the name of The NetBSD Foundation nor the names of its
     25       1.1  augustss  *    contributors may be used to endorse or promote products derived
     26       1.1  augustss  *    from this software without specific prior written permission.
     27       1.1  augustss  *
     28       1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     29       1.1  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     30       1.1  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     31       1.1  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     32       1.1  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     33       1.1  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     34       1.1  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     35       1.1  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     36       1.1  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     37       1.1  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     38       1.1  augustss  * POSSIBILITY OF SUCH DAMAGE.
     39       1.1  augustss  */
     40       1.1  augustss 
     41      1.15  augustss /*
     42      1.15  augustss  * HID spec: http://www.usb.org/developers/data/usbhid10.pdf
     43      1.15  augustss  */
     44       1.1  augustss 
     45       1.1  augustss #include <sys/param.h>
     46       1.1  augustss #include <sys/systm.h>
     47       1.1  augustss #include <sys/kernel.h>
     48       1.1  augustss #include <sys/malloc.h>
     49  1.26.2.1    bouyer #include <sys/signalvar.h>
     50      1.20  augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
     51      1.14  augustss #include <sys/device.h>
     52      1.12  augustss #include <sys/ioctl.h>
     53      1.12  augustss #elif defined(__FreeBSD__)
     54      1.12  augustss #include <sys/ioccom.h>
     55      1.12  augustss #include <sys/filio.h>
     56      1.12  augustss #include <sys/module.h>
     57      1.12  augustss #include <sys/bus.h>
     58      1.14  augustss #include <sys/ioccom.h>
     59      1.12  augustss #endif
     60      1.18  augustss #include <sys/conf.h>
     61       1.1  augustss #include <sys/tty.h>
     62       1.1  augustss #include <sys/file.h>
     63       1.1  augustss #include <sys/select.h>
     64       1.1  augustss #include <sys/proc.h>
     65       1.1  augustss #include <sys/vnode.h>
     66       1.1  augustss #include <sys/poll.h>
     67       1.1  augustss 
     68       1.1  augustss #include <dev/usb/usb.h>
     69       1.1  augustss #include <dev/usb/usbhid.h>
     70       1.1  augustss 
     71       1.1  augustss #include <dev/usb/usbdi.h>
     72       1.1  augustss #include <dev/usb/usbdi_util.h>
     73       1.1  augustss #include <dev/usb/hid.h>
     74       1.1  augustss #include <dev/usb/usb_quirks.h>
     75       1.1  augustss 
     76      1.26  augustss #ifdef UHID_DEBUG
     77      1.19  augustss #define DPRINTF(x)	if (uhiddebug) logprintf x
     78      1.19  augustss #define DPRINTFN(n,x)	if (uhiddebug>(n)) logprintf x
     79       1.1  augustss int	uhiddebug = 0;
     80       1.1  augustss #else
     81       1.1  augustss #define DPRINTF(x)
     82       1.1  augustss #define DPRINTFN(n,x)
     83       1.1  augustss #endif
     84       1.1  augustss 
     85       1.1  augustss struct uhid_softc {
     86      1.24  augustss 	USBBASEDEVICE sc_dev;			/* base device */
     87  1.26.2.1    bouyer 	usbd_device_handle sc_udev;
     88       1.1  augustss 	usbd_interface_handle sc_iface;	/* interface */
     89       1.1  augustss 	usbd_pipe_handle sc_intrpipe;	/* interrupt pipe */
     90       1.1  augustss 	int sc_ep_addr;
     91       1.1  augustss 
     92       1.1  augustss 	int sc_isize;
     93       1.1  augustss 	int sc_osize;
     94       1.2  augustss 	int sc_fsize;
     95       1.1  augustss 	u_int8_t sc_iid;
     96       1.1  augustss 	u_int8_t sc_oid;
     97       1.2  augustss 	u_int8_t sc_fid;
     98       1.1  augustss 
     99  1.26.2.1    bouyer 	u_char *sc_ibuf;
    100  1.26.2.1    bouyer 	u_char *sc_obuf;
    101       1.1  augustss 
    102       1.1  augustss 	void *sc_repdesc;
    103       1.1  augustss 	int sc_repdesc_size;
    104       1.1  augustss 
    105       1.1  augustss 	struct clist sc_q;
    106       1.1  augustss 	struct selinfo sc_rsel;
    107  1.26.2.1    bouyer 	struct proc *sc_async;	/* process that wants SIGIO */
    108       1.1  augustss 	u_char sc_state;	/* driver state */
    109       1.1  augustss #define	UHID_OPEN	0x01	/* device is open */
    110       1.5  augustss #define	UHID_ASLP	0x02	/* waiting for device data */
    111       1.1  augustss #define UHID_NEEDCLEAR	0x04	/* needs clearing endpoint stall */
    112       1.2  augustss #define UHID_IMMED	0x08	/* return read data immediately */
    113      1.18  augustss 
    114      1.18  augustss 	int sc_refcnt;
    115      1.18  augustss 	u_char sc_dying;
    116       1.1  augustss };
    117       1.1  augustss 
    118       1.1  augustss #define	UHIDUNIT(dev)	(minor(dev))
    119       1.1  augustss #define	UHID_CHUNK	128	/* chunk size for read */
    120       1.1  augustss #define	UHID_BSIZE	1020	/* buffer size */
    121       1.1  augustss 
    122      1.20  augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
    123      1.19  augustss cdev_decl(uhid);
    124      1.19  augustss #elif defined(__FreeBSD__)
    125      1.19  augustss d_open_t	uhidopen;
    126      1.19  augustss d_close_t	uhidclose;
    127      1.19  augustss d_read_t	uhidread;
    128      1.19  augustss d_write_t	uhidwrite;
    129      1.19  augustss d_ioctl_t	uhidioctl;
    130      1.19  augustss d_poll_t	uhidpoll;
    131      1.19  augustss 
    132      1.19  augustss #define		UHID_CDEV_MAJOR 122
    133      1.19  augustss 
    134  1.26.2.1    bouyer Static struct cdevsw uhid_cdevsw = {
    135      1.19  augustss 	/* open */	uhidopen,
    136      1.19  augustss 	/* close */	uhidclose,
    137      1.19  augustss 	/* read */	uhidread,
    138      1.19  augustss 	/* write */	uhidwrite,
    139      1.19  augustss 	/* ioctl */	uhidioctl,
    140      1.19  augustss 	/* poll */	uhidpoll,
    141      1.19  augustss 	/* mmap */	nommap,
    142      1.19  augustss 	/* strategy */	nostrategy,
    143      1.19  augustss 	/* name */	"uhid",
    144      1.19  augustss 	/* maj */	UHID_CDEV_MAJOR,
    145      1.19  augustss 	/* dump */	nodump,
    146      1.19  augustss 	/* psize */	nopsize,
    147      1.19  augustss 	/* flags */	0,
    148      1.19  augustss 	/* bmaj */	-1
    149      1.19  augustss };
    150      1.19  augustss #endif
    151      1.19  augustss 
    152  1.26.2.1    bouyer Static void uhid_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
    153      1.18  augustss 
    154  1.26.2.1    bouyer Static int uhid_do_read(struct uhid_softc *, struct uio *uio, int);
    155  1.26.2.1    bouyer Static int uhid_do_write(struct uhid_softc *, struct uio *uio, int);
    156  1.26.2.1    bouyer Static int uhid_do_ioctl(struct uhid_softc*, u_long, caddr_t, int,struct proc*);
    157       1.1  augustss 
    158      1.12  augustss USB_DECLARE_DRIVER(uhid);
    159       1.1  augustss 
    160      1.12  augustss USB_MATCH(uhid)
    161       1.1  augustss {
    162      1.12  augustss 	USB_MATCH_START(uhid, uaa);
    163       1.1  augustss 	usb_interface_descriptor_t *id;
    164       1.1  augustss 
    165  1.26.2.1    bouyer 	if (uaa->iface == NULL)
    166       1.1  augustss 		return (UMATCH_NONE);
    167       1.1  augustss 	id = usbd_get_interface_descriptor(uaa->iface);
    168  1.26.2.1    bouyer 	if (id == NULL || id->bInterfaceClass != UICLASS_HID)
    169       1.1  augustss 		return (UMATCH_NONE);
    170       1.1  augustss 	return (UMATCH_IFACECLASS_GENERIC);
    171       1.1  augustss }
    172       1.1  augustss 
    173      1.12  augustss USB_ATTACH(uhid)
    174       1.1  augustss {
    175      1.12  augustss 	USB_ATTACH_START(uhid, sc, uaa);
    176       1.1  augustss 	usbd_interface_handle iface = uaa->iface;
    177       1.1  augustss 	usb_interface_descriptor_t *id;
    178       1.1  augustss 	usb_endpoint_descriptor_t *ed;
    179       1.1  augustss 	int size;
    180       1.1  augustss 	void *desc;
    181  1.26.2.1    bouyer 	usbd_status err;
    182       1.1  augustss 	char devinfo[1024];
    183       1.1  augustss 
    184  1.26.2.1    bouyer 	sc->sc_udev = uaa->device;
    185       1.1  augustss 	sc->sc_iface = iface;
    186       1.1  augustss 	id = usbd_get_interface_descriptor(iface);
    187       1.1  augustss 	usbd_devinfo(uaa->device, 0, devinfo);
    188      1.12  augustss 	USB_ATTACH_SETUP;
    189      1.12  augustss 	printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
    190       1.7  augustss 	       devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
    191      1.12  augustss 
    192       1.1  augustss 	ed = usbd_interface2endpoint_descriptor(iface, 0);
    193  1.26.2.1    bouyer 	if (ed == NULL) {
    194       1.1  augustss 		printf("%s: could not read endpoint descriptor\n",
    195      1.12  augustss 		       USBDEVNAME(sc->sc_dev));
    196      1.18  augustss 		sc->sc_dying = 1;
    197      1.12  augustss 		USB_ATTACH_ERROR_RETURN;
    198       1.1  augustss 	}
    199       1.1  augustss 
    200      1.11  augustss 	DPRINTFN(10,("uhid_attach: bLength=%d bDescriptorType=%d "
    201      1.11  augustss 		     "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d"
    202      1.11  augustss 		     " bInterval=%d\n",
    203      1.11  augustss 		     ed->bLength, ed->bDescriptorType,
    204      1.11  augustss 		     ed->bEndpointAddress & UE_ADDR,
    205      1.23  augustss 		     UE_GET_DIR(ed->bEndpointAddress)==UE_DIR_IN? "in" : "out",
    206      1.11  augustss 		     ed->bmAttributes & UE_XFERTYPE,
    207      1.11  augustss 		     UGETW(ed->wMaxPacketSize), ed->bInterval));
    208       1.1  augustss 
    209      1.23  augustss 	if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_IN ||
    210       1.1  augustss 	    (ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
    211      1.12  augustss 		printf("%s: unexpected endpoint\n", USBDEVNAME(sc->sc_dev));
    212      1.18  augustss 		sc->sc_dying = 1;
    213      1.12  augustss 		USB_ATTACH_ERROR_RETURN;
    214       1.1  augustss 	}
    215       1.1  augustss 
    216       1.1  augustss 	sc->sc_ep_addr = ed->bEndpointAddress;
    217       1.1  augustss 
    218  1.26.2.1    bouyer 	desc = NULL;
    219  1.26.2.1    bouyer 	err = usbd_alloc_report_desc(uaa->iface, &desc, &size, M_USBDEV);
    220  1.26.2.1    bouyer 	if (err) {
    221      1.12  augustss 		printf("%s: no report descriptor\n", USBDEVNAME(sc->sc_dev));
    222      1.18  augustss 		sc->sc_dying = 1;
    223      1.12  augustss 		USB_ATTACH_ERROR_RETURN;
    224       1.1  augustss 	}
    225       1.1  augustss 
    226       1.1  augustss 	(void)usbd_set_idle(iface, 0, 0);
    227       1.1  augustss 
    228       1.2  augustss 	sc->sc_isize = hid_report_size(desc, size, hid_input,   &sc->sc_iid);
    229       1.2  augustss 	sc->sc_osize = hid_report_size(desc, size, hid_output,  &sc->sc_oid);
    230       1.2  augustss 	sc->sc_fsize = hid_report_size(desc, size, hid_feature, &sc->sc_fid);
    231       1.1  augustss 
    232       1.1  augustss 	sc->sc_repdesc = desc;
    233       1.1  augustss 	sc->sc_repdesc_size = size;
    234      1.12  augustss 
    235  1.26.2.1    bouyer #ifdef __FreeBSD__
    236  1.26.2.1    bouyer 	{
    237  1.26.2.1    bouyer 		static int global_init_done = 0;
    238  1.26.2.1    bouyer 
    239  1.26.2.1    bouyer 		if (!global_init_done) {
    240  1.26.2.1    bouyer 			cdevsw_add(&uhid_cdevsw);
    241  1.26.2.1    bouyer 			global_init_done = 1;
    242  1.26.2.1    bouyer 		}
    243  1.26.2.1    bouyer 	}
    244  1.26.2.1    bouyer #endif
    245  1.26.2.1    bouyer 
    246  1.26.2.1    bouyer 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    247  1.26.2.1    bouyer 			   USBDEV(sc->sc_dev));
    248  1.26.2.1    bouyer 
    249      1.12  augustss 	USB_ATTACH_SUCCESS_RETURN;
    250       1.1  augustss }
    251       1.1  augustss 
    252      1.26  augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
    253      1.18  augustss int
    254  1.26.2.1    bouyer uhid_activate(device_ptr_t self, enum devact act)
    255      1.18  augustss {
    256      1.21  augustss 	struct uhid_softc *sc = (struct uhid_softc *)self;
    257      1.21  augustss 
    258      1.21  augustss 	switch (act) {
    259      1.21  augustss 	case DVACT_ACTIVATE:
    260      1.21  augustss 		return (EOPNOTSUPP);
    261      1.21  augustss 		break;
    262      1.21  augustss 
    263      1.21  augustss 	case DVACT_DEACTIVATE:
    264      1.21  augustss 		sc->sc_dying = 1;
    265      1.21  augustss 		break;
    266      1.21  augustss 	}
    267      1.18  augustss 	return (0);
    268      1.12  augustss }
    269      1.26  augustss #endif
    270      1.12  augustss 
    271      1.26  augustss USB_DETACH(uhid)
    272       1.1  augustss {
    273      1.26  augustss 	USB_DETACH_START(uhid, sc);
    274      1.26  augustss 	int s;
    275      1.26  augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
    276      1.18  augustss 	int maj, mn;
    277      1.18  augustss 
    278      1.18  augustss 	DPRINTF(("uhid_detach: sc=%p flags=%d\n", sc, flags));
    279      1.26  augustss #else
    280      1.26  augustss 	DPRINTF(("uhid_detach: sc=%p\n", sc));
    281      1.26  augustss #endif
    282       1.3  augustss 
    283      1.18  augustss 	sc->sc_dying = 1;
    284  1.26.2.1    bouyer 	if (sc->sc_intrpipe != NULL)
    285      1.18  augustss 		usbd_abort_pipe(sc->sc_intrpipe);
    286      1.18  augustss 
    287      1.18  augustss 	if (sc->sc_state & UHID_OPEN) {
    288      1.18  augustss 		s = splusb();
    289      1.18  augustss 		if (--sc->sc_refcnt >= 0) {
    290      1.18  augustss 			/* Wake everyone */
    291      1.18  augustss 			wakeup(&sc->sc_q);
    292      1.18  augustss 			/* Wait for processes to go away. */
    293      1.24  augustss 			usb_detach_wait(USBDEV(sc->sc_dev));
    294      1.18  augustss 		}
    295      1.18  augustss 		splx(s);
    296      1.18  augustss 	}
    297      1.18  augustss 
    298      1.26  augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
    299      1.18  augustss 	/* locate the major number */
    300      1.18  augustss 	for (maj = 0; maj < nchrdev; maj++)
    301      1.18  augustss 		if (cdevsw[maj].d_open == uhidopen)
    302      1.18  augustss 			break;
    303      1.18  augustss 
    304      1.18  augustss 	/* Nuke the vnodes for any open instances (calls close). */
    305      1.18  augustss 	mn = self->dv_unit;
    306      1.18  augustss 	vdevgone(maj, mn, mn, VCHR);
    307      1.26  augustss #elif defined(__FreeBSD__)
    308      1.26  augustss 	/* XXX not implemented yet */
    309      1.26  augustss #endif
    310      1.18  augustss 
    311  1.26.2.1    bouyer 	if (sc->sc_repdesc)
    312  1.26.2.1    bouyer 		free(sc->sc_repdesc, M_USBDEV);
    313  1.26.2.1    bouyer 
    314  1.26.2.1    bouyer 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    315  1.26.2.1    bouyer 			   USBDEV(sc->sc_dev));
    316      1.18  augustss 
    317      1.18  augustss 	return (0);
    318       1.1  augustss }
    319       1.1  augustss 
    320       1.1  augustss void
    321  1.26.2.1    bouyer uhid_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
    322       1.1  augustss {
    323       1.1  augustss 	struct uhid_softc *sc = addr;
    324       1.1  augustss 
    325  1.26.2.1    bouyer #ifdef UHID_DEBUG
    326  1.26.2.1    bouyer 	if (uhiddebug > 5) {
    327  1.26.2.1    bouyer 		u_int32_t cc, i;
    328  1.26.2.1    bouyer 
    329  1.26.2.1    bouyer 		usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
    330  1.26.2.1    bouyer 		DPRINTF(("uhid_intr: status=%d cc=%d\n", status, cc));
    331  1.26.2.1    bouyer 		DPRINTF(("uhid_intr: data ="));
    332  1.26.2.1    bouyer 		for (i = 0; i < cc; i++)
    333  1.26.2.1    bouyer 			DPRINTF((" %02x", sc->sc_ibuf[i]));
    334  1.26.2.1    bouyer 		DPRINTF(("\n"));
    335  1.26.2.1    bouyer 	}
    336  1.26.2.1    bouyer #endif
    337       1.1  augustss 
    338       1.1  augustss 	if (status == USBD_CANCELLED)
    339       1.1  augustss 		return;
    340       1.1  augustss 
    341       1.1  augustss 	if (status != USBD_NORMAL_COMPLETION) {
    342       1.1  augustss 		DPRINTF(("uhid_intr: status=%d\n", status));
    343       1.1  augustss 		sc->sc_state |= UHID_NEEDCLEAR;
    344       1.1  augustss 		return;
    345       1.1  augustss 	}
    346       1.1  augustss 
    347       1.1  augustss 	(void) b_to_q(sc->sc_ibuf, sc->sc_isize, &sc->sc_q);
    348       1.1  augustss 
    349       1.1  augustss 	if (sc->sc_state & UHID_ASLP) {
    350       1.1  augustss 		sc->sc_state &= ~UHID_ASLP;
    351       1.1  augustss 		DPRINTFN(5, ("uhid_intr: waking %p\n", sc));
    352      1.18  augustss 		wakeup(&sc->sc_q);
    353       1.1  augustss 	}
    354       1.1  augustss 	selwakeup(&sc->sc_rsel);
    355  1.26.2.1    bouyer 	if (sc->sc_async != NULL) {
    356  1.26.2.1    bouyer 		DPRINTFN(3, ("uhid_intr: sending SIGIO %p\n", sc->sc_async));
    357  1.26.2.1    bouyer 		psignal(sc->sc_async, SIGIO);
    358  1.26.2.1    bouyer 	}
    359       1.1  augustss }
    360       1.1  augustss 
    361       1.1  augustss int
    362  1.26.2.1    bouyer uhidopen(dev_t dev, int flag, int mode, struct proc *p)
    363       1.1  augustss {
    364      1.25  augustss 	struct uhid_softc *sc;
    365  1.26.2.1    bouyer 	usbd_status err;
    366      1.25  augustss 
    367      1.12  augustss 	USB_GET_SC_OPEN(uhid, UHIDUNIT(dev), sc);
    368       1.1  augustss 
    369      1.18  augustss 	DPRINTF(("uhidopen: sc=%p\n", sc));
    370       1.1  augustss 
    371      1.18  augustss 	if (sc->sc_dying)
    372      1.18  augustss 		return (ENXIO);
    373       1.1  augustss 
    374       1.1  augustss 	if (sc->sc_state & UHID_OPEN)
    375      1.12  augustss 		return (EBUSY);
    376      1.17  augustss 	sc->sc_state |= UHID_OPEN;
    377       1.1  augustss 
    378      1.17  augustss 	if (clalloc(&sc->sc_q, UHID_BSIZE, 0) == -1) {
    379      1.17  augustss 		sc->sc_state &= ~UHID_OPEN;
    380      1.12  augustss 		return (ENOMEM);
    381      1.17  augustss 	}
    382       1.1  augustss 
    383      1.18  augustss 	sc->sc_ibuf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
    384      1.18  augustss 	sc->sc_obuf = malloc(sc->sc_osize, M_USBDEV, M_WAITOK);
    385       1.1  augustss 
    386       1.1  augustss 	/* Set up interrupt pipe. */
    387  1.26.2.1    bouyer 	err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
    388  1.26.2.1    bouyer 		  USBD_SHORT_XFER_OK, &sc->sc_intrpipe, sc, sc->sc_ibuf,
    389  1.26.2.1    bouyer 		  sc->sc_isize, uhid_intr, USBD_DEFAULT_INTERVAL);
    390  1.26.2.1    bouyer 	if (err) {
    391      1.11  augustss 		DPRINTF(("uhidopen: usbd_open_pipe_intr failed, "
    392  1.26.2.1    bouyer 			 "error=%d\n",err));
    393      1.18  augustss 		free(sc->sc_ibuf, M_USBDEV);
    394      1.18  augustss 		free(sc->sc_obuf, M_USBDEV);
    395       1.1  augustss 		sc->sc_state &= ~UHID_OPEN;
    396       1.1  augustss 		return (EIO);
    397       1.1  augustss 	}
    398      1.17  augustss 
    399      1.17  augustss 	sc->sc_state &= ~UHID_IMMED;
    400      1.17  augustss 
    401  1.26.2.1    bouyer 	sc->sc_async = 0;
    402  1.26.2.1    bouyer 
    403      1.12  augustss 	return (0);
    404       1.1  augustss }
    405       1.1  augustss 
    406       1.1  augustss int
    407  1.26.2.1    bouyer uhidclose(dev_t dev, int flag, int mode, struct proc *p)
    408       1.1  augustss {
    409      1.25  augustss 	struct uhid_softc *sc;
    410      1.25  augustss 
    411      1.12  augustss 	USB_GET_SC(uhid, UHIDUNIT(dev), sc);
    412       1.1  augustss 
    413       1.1  augustss 	DPRINTF(("uhidclose: sc=%p\n", sc));
    414       1.1  augustss 
    415       1.1  augustss 	/* Disable interrupts. */
    416       1.1  augustss 	usbd_abort_pipe(sc->sc_intrpipe);
    417       1.1  augustss 	usbd_close_pipe(sc->sc_intrpipe);
    418      1.18  augustss 	sc->sc_intrpipe = 0;
    419       1.1  augustss 
    420       1.1  augustss 	clfree(&sc->sc_q);
    421       1.1  augustss 
    422      1.18  augustss 	free(sc->sc_ibuf, M_USBDEV);
    423      1.18  augustss 	free(sc->sc_obuf, M_USBDEV);
    424      1.17  augustss 
    425      1.17  augustss 	sc->sc_state &= ~UHID_OPEN;
    426       1.1  augustss 
    427  1.26.2.1    bouyer 	sc->sc_async = 0;
    428  1.26.2.1    bouyer 
    429      1.12  augustss 	return (0);
    430       1.1  augustss }
    431       1.1  augustss 
    432       1.1  augustss int
    433  1.26.2.1    bouyer uhid_do_read(struct uhid_softc *sc, struct uio *uio, int flag)
    434       1.1  augustss {
    435       1.1  augustss 	int s;
    436       1.1  augustss 	int error = 0;
    437       1.1  augustss 	size_t length;
    438       1.1  augustss 	u_char buffer[UHID_CHUNK];
    439  1.26.2.1    bouyer 	usbd_status err;
    440       1.1  augustss 
    441       1.1  augustss 	DPRINTFN(1, ("uhidread\n"));
    442       1.2  augustss 	if (sc->sc_state & UHID_IMMED) {
    443       1.2  augustss 		DPRINTFN(1, ("uhidread immed\n"));
    444       1.2  augustss 
    445  1.26.2.1    bouyer 		err = usbd_get_report(sc->sc_iface, UHID_INPUT_REPORT,
    446      1.16  augustss 				    sc->sc_iid, buffer, sc->sc_isize);
    447  1.26.2.1    bouyer 		if (err)
    448       1.2  augustss 			return (EIO);
    449       1.2  augustss 		return (uiomove(buffer, sc->sc_isize, uio));
    450       1.2  augustss 	}
    451       1.2  augustss 
    452      1.10  augustss 	s = splusb();
    453       1.1  augustss 	while (sc->sc_q.c_cc == 0) {
    454       1.1  augustss 		if (flag & IO_NDELAY) {
    455       1.1  augustss 			splx(s);
    456      1.18  augustss 			return (EWOULDBLOCK);
    457       1.1  augustss 		}
    458       1.1  augustss 		sc->sc_state |= UHID_ASLP;
    459       1.1  augustss 		DPRINTFN(5, ("uhidread: sleep on %p\n", sc));
    460      1.18  augustss 		error = tsleep(&sc->sc_q, PZERO | PCATCH, "uhidrea", 0);
    461       1.1  augustss 		DPRINTFN(5, ("uhidread: woke, error=%d\n", error));
    462      1.18  augustss 		if (sc->sc_dying)
    463      1.18  augustss 			error = EIO;
    464       1.1  augustss 		if (error) {
    465       1.1  augustss 			sc->sc_state &= ~UHID_ASLP;
    466      1.18  augustss 			break;
    467       1.1  augustss 		}
    468       1.1  augustss 		if (sc->sc_state & UHID_NEEDCLEAR) {
    469       1.1  augustss 			DPRINTFN(-1,("uhidread: clearing stall\n"));
    470       1.1  augustss 			sc->sc_state &= ~UHID_NEEDCLEAR;
    471       1.1  augustss 			usbd_clear_endpoint_stall(sc->sc_intrpipe);
    472       1.1  augustss 		}
    473       1.1  augustss 	}
    474       1.1  augustss 	splx(s);
    475       1.1  augustss 
    476       1.1  augustss 	/* Transfer as many chunks as possible. */
    477      1.18  augustss 	while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0 && !error) {
    478       1.1  augustss 		length = min(sc->sc_q.c_cc, uio->uio_resid);
    479       1.1  augustss 		if (length > sizeof(buffer))
    480       1.1  augustss 			length = sizeof(buffer);
    481       1.1  augustss 
    482       1.1  augustss 		/* Remove a small chunk from the input queue. */
    483       1.1  augustss 		(void) q_to_b(&sc->sc_q, buffer, length);
    484  1.26.2.1    bouyer 		DPRINTFN(5, ("uhidread: got %lu chars\n", (u_long)length));
    485       1.1  augustss 
    486       1.1  augustss 		/* Copy the data to the user process. */
    487       1.1  augustss 		if ((error = uiomove(buffer, length, uio)) != 0)
    488       1.1  augustss 			break;
    489       1.1  augustss 	}
    490       1.1  augustss 
    491       1.1  augustss 	return (error);
    492       1.1  augustss }
    493       1.1  augustss 
    494       1.1  augustss int
    495  1.26.2.1    bouyer uhidread(dev_t dev, struct uio *uio, int flag)
    496       1.1  augustss {
    497      1.25  augustss 	struct uhid_softc *sc;
    498      1.25  augustss 	int error;
    499      1.25  augustss 
    500      1.18  augustss 	USB_GET_SC(uhid, UHIDUNIT(dev), sc);
    501      1.18  augustss 
    502      1.18  augustss 	sc->sc_refcnt++;
    503      1.18  augustss 	error = uhid_do_read(sc, uio, flag);
    504      1.18  augustss 	if (--sc->sc_refcnt < 0)
    505      1.24  augustss 		usb_detach_wakeup(USBDEV(sc->sc_dev));
    506      1.18  augustss 	return (error);
    507      1.18  augustss }
    508      1.18  augustss 
    509      1.18  augustss int
    510  1.26.2.1    bouyer uhid_do_write(struct uhid_softc *sc, struct uio *uio, int flag)
    511      1.18  augustss {
    512       1.1  augustss 	int error;
    513       1.1  augustss 	int size;
    514  1.26.2.1    bouyer 	usbd_status err;
    515       1.1  augustss 
    516      1.18  augustss 	DPRINTFN(1, ("uhidwrite\n"));
    517      1.18  augustss 
    518      1.18  augustss 	if (sc->sc_dying)
    519       1.1  augustss 		return (EIO);
    520       1.1  augustss 
    521       1.1  augustss 	size = sc->sc_osize;
    522       1.1  augustss 	error = 0;
    523      1.18  augustss 	if (uio->uio_resid != size)
    524      1.18  augustss 		return (EINVAL);
    525      1.18  augustss 	error = uiomove(sc->sc_obuf, size, uio);
    526      1.18  augustss 	if (!error) {
    527       1.1  augustss 		if (sc->sc_oid)
    528  1.26.2.1    bouyer 			err = usbd_set_report(sc->sc_iface, UHID_OUTPUT_REPORT,
    529  1.26.2.1    bouyer 				  sc->sc_obuf[0], sc->sc_obuf+1, size-1);
    530       1.1  augustss 		else
    531  1.26.2.1    bouyer 			err = usbd_set_report(sc->sc_iface, UHID_OUTPUT_REPORT,
    532  1.26.2.1    bouyer 				  0, sc->sc_obuf, size);
    533  1.26.2.1    bouyer 		if (err)
    534       1.1  augustss 			error = EIO;
    535       1.1  augustss 	}
    536      1.18  augustss 
    537       1.1  augustss 	return (error);
    538       1.1  augustss }
    539       1.1  augustss 
    540       1.1  augustss int
    541  1.26.2.1    bouyer uhidwrite(dev_t dev, struct uio *uio, int flag)
    542      1.18  augustss {
    543      1.25  augustss 	struct uhid_softc *sc;
    544      1.25  augustss 	int error;
    545      1.25  augustss 
    546      1.18  augustss 	USB_GET_SC(uhid, UHIDUNIT(dev), sc);
    547      1.18  augustss 
    548      1.18  augustss 	sc->sc_refcnt++;
    549      1.18  augustss 	error = uhid_do_write(sc, uio, flag);
    550      1.18  augustss 	if (--sc->sc_refcnt < 0)
    551      1.24  augustss 		usb_detach_wakeup(USBDEV(sc->sc_dev));
    552      1.18  augustss 	return (error);
    553      1.18  augustss }
    554      1.18  augustss 
    555      1.18  augustss int
    556  1.26.2.1    bouyer uhid_do_ioctl(struct uhid_softc *sc, u_long cmd, caddr_t addr,
    557  1.26.2.1    bouyer 	      int flag, struct proc *p)
    558       1.1  augustss {
    559       1.1  augustss 	struct usb_ctl_report_desc *rd;
    560       1.2  augustss 	struct usb_ctl_report *re;
    561       1.2  augustss 	int size, id;
    562  1.26.2.1    bouyer 	usbd_status err;
    563       1.1  augustss 
    564      1.18  augustss 	DPRINTFN(2, ("uhidioctl: cmd=%lx\n", cmd));
    565      1.18  augustss 
    566      1.18  augustss 	if (sc->sc_dying)
    567       1.1  augustss 		return (EIO);
    568       1.1  augustss 
    569       1.1  augustss 	switch (cmd) {
    570       1.2  augustss 	case FIONBIO:
    571       1.2  augustss 		/* All handled in the upper FS layer. */
    572       1.2  augustss 		break;
    573       1.2  augustss 
    574  1.26.2.1    bouyer 	case FIOASYNC:
    575  1.26.2.1    bouyer 		if (*(int *)addr) {
    576  1.26.2.1    bouyer 			if (sc->sc_async != NULL)
    577  1.26.2.1    bouyer 				return (EBUSY);
    578  1.26.2.1    bouyer 			sc->sc_async = p;
    579  1.26.2.1    bouyer 			DPRINTF(("uhid_do_ioctl: FIOASYNC %p\n", p));
    580  1.26.2.1    bouyer 		} else
    581  1.26.2.1    bouyer 			sc->sc_async = NULL;
    582  1.26.2.1    bouyer 		break;
    583  1.26.2.1    bouyer 
    584  1.26.2.1    bouyer 	/* XXX this is not the most general solution. */
    585  1.26.2.1    bouyer 	case TIOCSPGRP:
    586  1.26.2.1    bouyer 		if (sc->sc_async == NULL)
    587  1.26.2.1    bouyer 			return (EINVAL);
    588  1.26.2.1    bouyer 		if (*(int *)addr != sc->sc_async->p_pgid)
    589  1.26.2.1    bouyer 			return (EPERM);
    590  1.26.2.1    bouyer 		break;
    591  1.26.2.1    bouyer 
    592       1.1  augustss 	case USB_GET_REPORT_DESC:
    593       1.1  augustss 		rd = (struct usb_ctl_report_desc *)addr;
    594       1.1  augustss 		size = min(sc->sc_repdesc_size, sizeof rd->data);
    595       1.1  augustss 		rd->size = size;
    596       1.1  augustss 		memcpy(rd->data, sc->sc_repdesc, size);
    597       1.1  augustss 		break;
    598       1.2  augustss 
    599       1.2  augustss 	case USB_SET_IMMED:
    600       1.9  augustss 		if (*(int *)addr) {
    601  1.26.2.1    bouyer 			/* XXX should read into ibuf, but does it matter? */
    602  1.26.2.1    bouyer 			err = usbd_get_report(sc->sc_iface, UHID_INPUT_REPORT,
    603  1.26.2.1    bouyer 				  sc->sc_iid, sc->sc_ibuf, sc->sc_isize);
    604  1.26.2.1    bouyer 			if (err)
    605       1.9  augustss 				return (EOPNOTSUPP);
    606      1.12  augustss 
    607       1.2  augustss 			sc->sc_state |=  UHID_IMMED;
    608       1.9  augustss 		} else
    609       1.2  augustss 			sc->sc_state &= ~UHID_IMMED;
    610       1.2  augustss 		break;
    611       1.2  augustss 
    612       1.2  augustss 	case USB_GET_REPORT:
    613       1.2  augustss 		re = (struct usb_ctl_report *)addr;
    614       1.2  augustss 		switch (re->report) {
    615       1.2  augustss 		case UHID_INPUT_REPORT:
    616       1.2  augustss 			size = sc->sc_isize;
    617       1.2  augustss 			id = sc->sc_iid;
    618       1.2  augustss 			break;
    619       1.2  augustss 		case UHID_OUTPUT_REPORT:
    620       1.2  augustss 			size = sc->sc_osize;
    621       1.2  augustss 			id = sc->sc_oid;
    622       1.2  augustss 			break;
    623       1.2  augustss 		case UHID_FEATURE_REPORT:
    624       1.2  augustss 			size = sc->sc_fsize;
    625       1.2  augustss 			id = sc->sc_fid;
    626       1.2  augustss 			break;
    627       1.2  augustss 		default:
    628       1.2  augustss 			return (EINVAL);
    629       1.2  augustss 		}
    630  1.26.2.1    bouyer 		err = usbd_get_report(sc->sc_iface, re->report, id, re->data,
    631  1.26.2.1    bouyer 			  size);
    632  1.26.2.1    bouyer 		if (err)
    633  1.26.2.1    bouyer 			return (EIO);
    634  1.26.2.1    bouyer 		break;
    635  1.26.2.1    bouyer 
    636  1.26.2.1    bouyer 	case USB_SET_REPORT:
    637  1.26.2.1    bouyer 		re = (struct usb_ctl_report *)addr;
    638  1.26.2.1    bouyer 		switch (re->report) {
    639  1.26.2.1    bouyer 		case UHID_INPUT_REPORT:
    640  1.26.2.1    bouyer 			size = sc->sc_isize;
    641  1.26.2.1    bouyer 			id = sc->sc_iid;
    642  1.26.2.1    bouyer 			break;
    643  1.26.2.1    bouyer 		case UHID_OUTPUT_REPORT:
    644  1.26.2.1    bouyer 			size = sc->sc_osize;
    645  1.26.2.1    bouyer 			id = sc->sc_oid;
    646  1.26.2.1    bouyer 			break;
    647  1.26.2.1    bouyer 		case UHID_FEATURE_REPORT:
    648  1.26.2.1    bouyer 			size = sc->sc_fsize;
    649  1.26.2.1    bouyer 			id = sc->sc_fid;
    650  1.26.2.1    bouyer 			break;
    651  1.26.2.1    bouyer 		default:
    652  1.26.2.1    bouyer 			return (EINVAL);
    653  1.26.2.1    bouyer 		}
    654  1.26.2.1    bouyer 		err = usbd_set_report(sc->sc_iface, re->report, id, re->data,
    655  1.26.2.1    bouyer 			  size);
    656  1.26.2.1    bouyer 		if (err)
    657       1.2  augustss 			return (EIO);
    658       1.2  augustss 		break;
    659       1.2  augustss 
    660       1.1  augustss 	default:
    661       1.1  augustss 		return (EINVAL);
    662       1.1  augustss 	}
    663       1.1  augustss 	return (0);
    664       1.1  augustss }
    665       1.1  augustss 
    666       1.1  augustss int
    667  1.26.2.1    bouyer uhidioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
    668      1.18  augustss {
    669      1.25  augustss 	struct uhid_softc *sc;
    670      1.25  augustss 	int error;
    671      1.25  augustss 
    672      1.18  augustss 	USB_GET_SC(uhid, UHIDUNIT(dev), sc);
    673      1.18  augustss 
    674      1.18  augustss 	sc->sc_refcnt++;
    675      1.18  augustss 	error = uhid_do_ioctl(sc, cmd, addr, flag, p);
    676      1.18  augustss 	if (--sc->sc_refcnt < 0)
    677      1.24  augustss 		usb_detach_wakeup(USBDEV(sc->sc_dev));
    678      1.18  augustss 	return (error);
    679      1.18  augustss }
    680      1.18  augustss 
    681      1.18  augustss int
    682  1.26.2.1    bouyer uhidpoll(dev_t dev, int events, struct proc *p)
    683       1.1  augustss {
    684      1.25  augustss 	struct uhid_softc *sc;
    685       1.1  augustss 	int revents = 0;
    686       1.1  augustss 	int s;
    687      1.25  augustss 
    688      1.12  augustss 	USB_GET_SC(uhid, UHIDUNIT(dev), sc);
    689       1.1  augustss 
    690      1.18  augustss 	if (sc->sc_dying)
    691       1.1  augustss 		return (EIO);
    692       1.1  augustss 
    693      1.10  augustss 	s = splusb();
    694       1.1  augustss 	if (events & (POLLOUT | POLLWRNORM))
    695       1.1  augustss 		revents |= events & (POLLOUT | POLLWRNORM);
    696       1.4     veego 	if (events & (POLLIN | POLLRDNORM)) {
    697       1.1  augustss 		if (sc->sc_q.c_cc > 0)
    698       1.1  augustss 			revents |= events & (POLLIN | POLLRDNORM);
    699       1.1  augustss 		else
    700       1.1  augustss 			selrecord(p, &sc->sc_rsel);
    701       1.4     veego 	}
    702       1.1  augustss 
    703       1.1  augustss 	splx(s);
    704       1.1  augustss 	return (revents);
    705       1.1  augustss }
    706      1.12  augustss 
    707      1.12  augustss #if defined(__FreeBSD__)
    708  1.26.2.1    bouyer DRIVER_MODULE(uhid, uhub, uhid_driver, uhid_devclass, usbd_driver_load, 0);
    709      1.12  augustss #endif
    710