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