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