Home | History | Annotate | Line # | Download | only in usb
uhid.c revision 1.125
      1  1.125  christos /*	$NetBSD: uhid.c,v 1.125 2022/03/31 17:43:50 christos Exp $	*/
      2    1.1  augustss 
      3    1.1  augustss /*
      4   1.88       mrg  * Copyright (c) 1998, 2004, 2008, 2012 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.88       mrg  * Carlstedt Research & Technology and Matthew R. Green (mrg (at) eterna.com.au).
     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  *
     20    1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21    1.1  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22    1.1  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23    1.1  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24    1.1  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25    1.1  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26    1.1  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27    1.1  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28    1.1  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29    1.1  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30    1.1  augustss  * POSSIBILITY OF SUCH DAMAGE.
     31    1.1  augustss  */
     32    1.1  augustss 
     33   1.15  augustss /*
     34   1.57  augustss  * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
     35   1.15  augustss  */
     36   1.46     lukem 
     37   1.46     lukem #include <sys/cdefs.h>
     38  1.125  christos __KERNEL_RCSID(0, "$NetBSD: uhid.c,v 1.125 2022/03/31 17:43:50 christos Exp $");
     39   1.73     pavel 
     40   1.89  christos #ifdef _KERNEL_OPT
     41   1.73     pavel #include "opt_compat_netbsd.h"
     42   1.96  jakllsch #include "opt_usb.h"
     43   1.89  christos #endif
     44    1.1  augustss 
     45    1.1  augustss #include <sys/param.h>
     46  1.115  riastrad #include <sys/types.h>
     47  1.115  riastrad 
     48  1.115  riastrad #include <sys/atomic.h>
     49  1.115  riastrad #include <sys/compat_stub.h>
     50  1.115  riastrad #include <sys/conf.h>
     51  1.115  riastrad #include <sys/device.h>
     52  1.115  riastrad #include <sys/file.h>
     53  1.115  riastrad #include <sys/intr.h>
     54  1.115  riastrad #include <sys/ioctl.h>
     55    1.1  augustss #include <sys/kernel.h>
     56   1.95     skrll #include <sys/kmem.h>
     57  1.115  riastrad #include <sys/poll.h>
     58  1.115  riastrad #include <sys/proc.h>
     59  1.115  riastrad #include <sys/select.h>
     60   1.37  augustss #include <sys/signalvar.h>
     61  1.115  riastrad #include <sys/systm.h>
     62    1.1  augustss #include <sys/tty.h>
     63    1.1  augustss #include <sys/vnode.h>
     64    1.1  augustss 
     65    1.1  augustss #include <dev/usb/usb.h>
     66    1.1  augustss #include <dev/usb/usbhid.h>
     67    1.1  augustss 
     68   1.42  augustss #include <dev/usb/usbdevs.h>
     69    1.1  augustss #include <dev/usb/usbdi.h>
     70    1.1  augustss #include <dev/usb/usbdi_util.h>
     71    1.1  augustss #include <dev/usb/usb_quirks.h>
     72  1.101    bouyer #include <dev/hid/hid.h>
     73    1.1  augustss 
     74   1.47  augustss #include <dev/usb/uhidev.h>
     75   1.42  augustss 
     76  1.108       mrg #include "ioconf.h"
     77  1.108       mrg 
     78   1.26  augustss #ifdef UHID_DEBUG
     79   1.84    dyoung #define DPRINTF(x)	if (uhiddebug) printf x
     80   1.84    dyoung #define DPRINTFN(n,x)	if (uhiddebug>(n)) printf x
     81    1.1  augustss int	uhiddebug = 0;
     82    1.1  augustss #else
     83    1.1  augustss #define DPRINTF(x)
     84    1.1  augustss #define DPRINTFN(n,x)
     85    1.1  augustss #endif
     86    1.1  augustss 
     87    1.1  augustss struct uhid_softc {
     88  1.123  riastrad 	device_t sc_dev;
     89  1.123  riastrad 	struct uhidev *sc_hdev;
     90  1.122  riastrad 	struct usbd_device *sc_udev;
     91  1.123  riastrad 	uint8_t sc_report_id;
     92    1.1  augustss 
     93  1.115  riastrad 	kmutex_t sc_lock;
     94   1.88       mrg 	kcondvar_t sc_cv;
     95   1.88       mrg 
     96    1.1  augustss 	int sc_isize;
     97    1.1  augustss 	int sc_osize;
     98    1.2  augustss 	int sc_fsize;
     99    1.1  augustss 
    100   1.33  augustss 	u_char *sc_obuf;
    101    1.1  augustss 
    102   1.93       mrg 	struct clist sc_q;	/* protected by sc_lock */
    103    1.1  augustss 	struct selinfo sc_rsel;
    104   1.84    dyoung 	proc_t *sc_async;	/* process that wants SIGIO */
    105   1.80        ad 	void *sc_sih;
    106  1.115  riastrad 	volatile uint32_t sc_state;	/* driver state */
    107   1.47  augustss #define UHID_IMMED	0x02	/* return read data immediately */
    108   1.18  augustss 
    109  1.112  christos 	int sc_raw;
    110  1.120  riastrad 	enum {
    111  1.120  riastrad 		UHID_CLOSED,
    112  1.120  riastrad 		UHID_OPENING,
    113  1.120  riastrad 		UHID_OPEN,
    114  1.120  riastrad 	} sc_open;
    115  1.120  riastrad 	bool sc_closing;
    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.109      maxv static dev_type_open(uhidopen);
    123  1.120  riastrad static dev_type_cancel(uhidcancel);
    124  1.109      maxv static dev_type_close(uhidclose);
    125  1.109      maxv static dev_type_read(uhidread);
    126  1.109      maxv static dev_type_write(uhidwrite);
    127  1.109      maxv static dev_type_ioctl(uhidioctl);
    128  1.109      maxv static dev_type_poll(uhidpoll);
    129  1.109      maxv static dev_type_kqfilter(uhidkqfilter);
    130   1.53   gehenna 
    131   1.53   gehenna const struct cdevsw uhid_cdevsw = {
    132   1.90  dholland 	.d_open = uhidopen,
    133  1.120  riastrad 	.d_cancel = uhidcancel,
    134   1.90  dholland 	.d_close = uhidclose,
    135   1.90  dholland 	.d_read = uhidread,
    136   1.90  dholland 	.d_write = uhidwrite,
    137   1.90  dholland 	.d_ioctl = uhidioctl,
    138   1.90  dholland 	.d_stop = nostop,
    139   1.90  dholland 	.d_tty = notty,
    140   1.90  dholland 	.d_poll = uhidpoll,
    141   1.90  dholland 	.d_mmap = nommap,
    142   1.90  dholland 	.d_kqfilter = uhidkqfilter,
    143   1.92  dholland 	.d_discard = nodiscard,
    144  1.120  riastrad 	.d_cfdriver = &uhid_cd,
    145  1.120  riastrad 	.d_devtounit = dev_minor_unit,
    146   1.93       mrg 	.d_flag = D_OTHER
    147   1.53   gehenna };
    148   1.19  augustss 
    149  1.123  riastrad static void uhid_intr(void *, void *, u_int);
    150    1.1  augustss 
    151  1.109      maxv static int	uhid_match(device_t, cfdata_t, void *);
    152  1.109      maxv static void	uhid_attach(device_t, device_t, void *);
    153  1.109      maxv static int	uhid_detach(device_t, int);
    154  1.108       mrg 
    155   1.98   msaitoh CFATTACH_DECL_NEW(uhid, sizeof(struct uhid_softc), uhid_match, uhid_attach,
    156  1.120  riastrad     uhid_detach, NULL);
    157    1.1  augustss 
    158  1.109      maxv static int
    159   1.82      cube uhid_match(device_t parent, cfdata_t match, void *aux)
    160    1.1  augustss {
    161   1.66      tron #ifdef UHID_DEBUG
    162   1.47  augustss 	struct uhidev_attach_arg *uha = aux;
    163   1.66      tron #endif
    164   1.47  augustss 
    165   1.47  augustss 	DPRINTF(("uhid_match: report=%d\n", uha->reportid));
    166   1.47  augustss 
    167   1.65  augustss 	if (match->cf_flags & 1)
    168   1.93       mrg 		return UMATCH_HIGHEST;
    169   1.67  augustss 	else
    170   1.93       mrg 		return UMATCH_IFACECLASS_GENERIC;
    171    1.1  augustss }
    172    1.1  augustss 
    173  1.109      maxv static void
    174   1.82      cube uhid_attach(device_t parent, device_t self, void *aux)
    175    1.1  augustss {
    176   1.82      cube 	struct uhid_softc *sc = device_private(self);
    177   1.47  augustss 	struct uhidev_attach_arg *uha = aux;
    178   1.47  augustss 	int size, repid;
    179    1.1  augustss 	void *desc;
    180   1.52  augustss 
    181  1.123  riastrad 	sc->sc_dev = self;
    182  1.123  riastrad 	sc->sc_hdev = uha->parent;
    183  1.123  riastrad 	sc->sc_udev = uha->uiaa->uiaa_device;
    184  1.123  riastrad 	sc->sc_report_id = uha->reportid;
    185  1.123  riastrad 
    186   1.79     rmind 	selinit(&sc->sc_rsel);
    187  1.122  riastrad 
    188   1.47  augustss 	uhidev_get_report_desc(uha->parent, &desc, &size);
    189   1.47  augustss 	repid = uha->reportid;
    190   1.47  augustss 	sc->sc_isize = hid_report_size(desc, size, hid_input,   repid);
    191   1.47  augustss 	sc->sc_osize = hid_report_size(desc, size, hid_output,  repid);
    192   1.47  augustss 	sc->sc_fsize = hid_report_size(desc, size, hid_feature, repid);
    193  1.112  christos 	sc->sc_raw =  hid_is_collection(desc, size, uha->reportid,
    194  1.112  christos 	    HID_USAGE2(HUP_FIDO, HUF_U2FHID));
    195    1.1  augustss 
    196   1.82      cube 	aprint_naive("\n");
    197   1.82      cube 	aprint_normal(": input=%d, output=%d, feature=%d\n",
    198   1.47  augustss 	       sc->sc_isize, sc->sc_osize, sc->sc_fsize);
    199   1.32  augustss 
    200   1.95     skrll 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
    201   1.88       mrg 	cv_init(&sc->sc_cv, "uhidrea");
    202   1.88       mrg 
    203   1.78  drochner 	if (!pmf_device_register(self, NULL, NULL))
    204   1.78  drochner 		aprint_error_dev(self, "couldn't establish power handler\n");
    205   1.78  drochner 
    206   1.84    dyoung 	return;
    207    1.1  augustss }
    208    1.1  augustss 
    209  1.109      maxv static int
    210   1.82      cube uhid_detach(device_t self, int flags)
    211    1.1  augustss {
    212   1.82      cube 	struct uhid_softc *sc = device_private(self);
    213   1.18  augustss 	int maj, mn;
    214   1.18  augustss 
    215   1.18  augustss 	DPRINTF(("uhid_detach: sc=%p flags=%d\n", sc, flags));
    216    1.3  augustss 
    217  1.115  riastrad 	pmf_device_deregister(self);
    218  1.115  riastrad 
    219   1.18  augustss 	/* locate the major number */
    220   1.53   gehenna 	maj = cdevsw_lookup_major(&uhid_cdevsw);
    221   1.18  augustss 
    222   1.18  augustss 	/* Nuke the vnodes for any open instances (calls close). */
    223   1.69   thorpej 	mn = device_unit(self);
    224   1.18  augustss 	vdevgone(maj, mn, mn, VCHR);
    225   1.47  augustss 
    226  1.120  riastrad 	KASSERTMSG(sc->sc_open == UHID_CLOSED, "open=%d", (int)sc->sc_open);
    227  1.115  riastrad 
    228   1.88       mrg 	cv_destroy(&sc->sc_cv);
    229   1.88       mrg 	mutex_destroy(&sc->sc_lock);
    230   1.79     rmind 	seldestroy(&sc->sc_rsel);
    231   1.18  augustss 
    232   1.93       mrg 	return 0;
    233    1.1  augustss }
    234    1.1  augustss 
    235  1.120  riastrad static void
    236  1.123  riastrad uhid_intr(void *cookie, void *data, u_int len)
    237    1.1  augustss {
    238  1.123  riastrad 	struct uhid_softc *sc = cookie;
    239    1.1  augustss 
    240   1.33  augustss #ifdef UHID_DEBUG
    241   1.33  augustss 	if (uhiddebug > 5) {
    242   1.95     skrll 		uint32_t i;
    243   1.52  augustss 
    244   1.33  augustss 		DPRINTF(("uhid_intr: data ="));
    245   1.47  augustss 		for (i = 0; i < len; i++)
    246   1.51  augustss 			DPRINTF((" %02x", ((u_char *)data)[i]));
    247   1.33  augustss 		DPRINTF(("\n"));
    248   1.33  augustss 	}
    249   1.33  augustss #endif
    250    1.1  augustss 
    251   1.88       mrg 	mutex_enter(&sc->sc_lock);
    252   1.47  augustss 	(void)b_to_q(data, len, &sc->sc_q);
    253   1.52  augustss 
    254  1.115  riastrad 	DPRINTFN(5, ("uhid_intr: waking %p\n", &sc->sc_q));
    255  1.115  riastrad 	cv_broadcast(&sc->sc_cv);
    256  1.115  riastrad 	selnotify(&sc->sc_rsel, 0, NOTE_SUBMIT);
    257  1.116  riastrad 	if (atomic_load_relaxed(&sc->sc_async) != NULL) {
    258  1.116  riastrad 		mutex_enter(&proc_lock);
    259  1.116  riastrad 		if (sc->sc_async != NULL) {
    260  1.116  riastrad 			DPRINTFN(3, ("uhid_intr: sending SIGIO to %jd\n",
    261  1.116  riastrad 				(intmax_t)sc->sc_async->p_pid));
    262  1.116  riastrad 			psignal(sc->sc_async, SIGIO);
    263  1.116  riastrad 		}
    264  1.116  riastrad 		mutex_exit(&proc_lock);
    265   1.37  augustss 	}
    266   1.88       mrg 	mutex_exit(&sc->sc_lock);
    267    1.1  augustss }
    268    1.1  augustss 
    269  1.109      maxv static int
    270   1.88       mrg uhidopen(dev_t dev, int flag, int mode, struct lwp *l)
    271    1.1  augustss {
    272  1.120  riastrad 	struct uhid_softc *sc = device_lookup_private(&uhid_cd, UHIDUNIT(dev));
    273   1.47  augustss 	int error;
    274   1.25  augustss 
    275  1.120  riastrad 	DPRINTF(("uhidopen: sc=%p\n", sc));
    276   1.84    dyoung 	if (sc == NULL)
    277   1.84    dyoung 		return ENXIO;
    278    1.1  augustss 
    279   1.93       mrg 	/*
    280  1.120  riastrad 	 * Try to open.  If closing in progress, give up.  If already
    281  1.120  riastrad 	 * open (or opening), fail -- opens are exclusive.
    282   1.93       mrg 	 */
    283  1.115  riastrad 	mutex_enter(&sc->sc_lock);
    284  1.120  riastrad 	if (sc->sc_open != UHID_CLOSED || sc->sc_closing) {
    285  1.107       mrg 		mutex_exit(&sc->sc_lock);
    286   1.93       mrg 		return EBUSY;
    287   1.93       mrg 	}
    288  1.120  riastrad 	sc->sc_open = UHID_OPENING;
    289  1.115  riastrad 	atomic_store_relaxed(&sc->sc_state, 0);
    290  1.107       mrg 	mutex_exit(&sc->sc_lock);
    291  1.107       mrg 
    292  1.115  riastrad 	/* uhid interrupts aren't enabled yet, so setup sc_q now */
    293   1.93       mrg 	if (clalloc(&sc->sc_q, UHID_BSIZE, 0) == -1) {
    294  1.115  riastrad 		error = ENOMEM;
    295  1.115  riastrad 		goto fail0;
    296   1.93       mrg 	}
    297   1.93       mrg 
    298  1.115  riastrad 	/* Allocate an output buffer if needed.  */
    299   1.97   mlelstv 	if (sc->sc_osize > 0)
    300   1.97   mlelstv 		sc->sc_obuf = kmem_alloc(sc->sc_osize, KM_SLEEP);
    301   1.97   mlelstv 	else
    302   1.97   mlelstv 		sc->sc_obuf = NULL;
    303   1.88       mrg 
    304  1.115  riastrad 	/* Paranoia: reset SIGIO before enabling interrputs.  */
    305  1.114        ad 	mutex_enter(&proc_lock);
    306  1.116  riastrad 	atomic_store_relaxed(&sc->sc_async, NULL);
    307  1.114        ad 	mutex_exit(&proc_lock);
    308   1.37  augustss 
    309  1.115  riastrad 	/* Open the uhidev -- after this point we can get interrupts.  */
    310  1.123  riastrad 	error = uhidev_open(sc->sc_hdev, &uhid_intr, sc);
    311  1.115  riastrad 	if (error)
    312  1.115  riastrad 		goto fail1;
    313  1.115  riastrad 
    314  1.115  riastrad 	/* We are open for business.  */
    315  1.115  riastrad 	mutex_enter(&sc->sc_lock);
    316  1.120  riastrad 	sc->sc_open = UHID_OPEN;
    317  1.120  riastrad 	cv_broadcast(&sc->sc_cv);
    318  1.115  riastrad 	mutex_exit(&sc->sc_lock);
    319  1.115  riastrad 
    320   1.93       mrg 	return 0;
    321  1.115  riastrad 
    322  1.115  riastrad fail1:	selnotify(&sc->sc_rsel, POLLHUP, 0);
    323  1.115  riastrad 	mutex_enter(&proc_lock);
    324  1.116  riastrad 	atomic_store_relaxed(&sc->sc_async, NULL);
    325  1.115  riastrad 	mutex_exit(&proc_lock);
    326  1.115  riastrad 	if (sc->sc_osize > 0) {
    327  1.115  riastrad 		kmem_free(sc->sc_obuf, sc->sc_osize);
    328  1.115  riastrad 		sc->sc_obuf = NULL;
    329  1.115  riastrad 	}
    330  1.115  riastrad 	clfree(&sc->sc_q);
    331  1.115  riastrad fail0:	mutex_enter(&sc->sc_lock);
    332  1.120  riastrad 	KASSERT(sc->sc_open == UHID_OPENING);
    333  1.120  riastrad 	sc->sc_open = UHID_CLOSED;
    334  1.120  riastrad 	cv_broadcast(&sc->sc_cv);
    335  1.115  riastrad 	atomic_store_relaxed(&sc->sc_state, 0);
    336  1.115  riastrad 	mutex_exit(&sc->sc_lock);
    337  1.115  riastrad 	return error;
    338    1.1  augustss }
    339    1.1  augustss 
    340  1.109      maxv static int
    341  1.120  riastrad uhidcancel(dev_t dev, int flag, int mode, struct lwp *l)
    342  1.120  riastrad {
    343  1.120  riastrad 	struct uhid_softc *sc = device_lookup_private(&uhid_cd, UHIDUNIT(dev));
    344  1.120  riastrad 
    345  1.120  riastrad 	DPRINTF(("uhidcancel: sc=%p\n", sc));
    346  1.120  riastrad 	if (sc == NULL)
    347  1.120  riastrad 		return 0;
    348  1.120  riastrad 
    349  1.120  riastrad 	/*
    350  1.120  riastrad 	 * Mark it closing, wake any waiters, and suspend output.
    351  1.120  riastrad 	 * After this point, no new xfers can be submitted.
    352  1.120  riastrad 	 */
    353  1.120  riastrad 	mutex_enter(&sc->sc_lock);
    354  1.120  riastrad 	sc->sc_closing = true;
    355  1.120  riastrad 	cv_broadcast(&sc->sc_cv);
    356  1.120  riastrad 	mutex_exit(&sc->sc_lock);
    357  1.120  riastrad 
    358  1.123  riastrad 	uhidev_stop(sc->sc_hdev);
    359  1.120  riastrad 
    360  1.120  riastrad 	return 0;
    361  1.120  riastrad }
    362  1.120  riastrad 
    363  1.120  riastrad static int
    364   1.88       mrg uhidclose(dev_t dev, int flag, int mode, struct lwp *l)
    365    1.1  augustss {
    366  1.120  riastrad 	struct uhid_softc *sc = device_lookup_private(&uhid_cd, UHIDUNIT(dev));
    367    1.1  augustss 
    368    1.1  augustss 	DPRINTF(("uhidclose: sc=%p\n", sc));
    369  1.120  riastrad 	if (sc == NULL)
    370  1.120  riastrad 		return 0;
    371    1.1  augustss 
    372  1.115  riastrad 	mutex_enter(&sc->sc_lock);
    373  1.120  riastrad 	KASSERT(sc->sc_closing);
    374  1.120  riastrad 	KASSERTMSG(sc->sc_open == UHID_OPEN || sc->sc_open == UHID_CLOSED,
    375  1.120  riastrad 	    "sc_open=%d", sc->sc_open);
    376  1.120  riastrad 	if (sc->sc_open == UHID_CLOSED)
    377  1.120  riastrad 		goto out;
    378  1.120  riastrad 
    379  1.120  riastrad 	/* Release the lock while we free things.  */
    380  1.115  riastrad 	mutex_exit(&sc->sc_lock);
    381  1.115  riastrad 
    382  1.115  riastrad 	/* Prevent further interrupts.  */
    383  1.123  riastrad 	uhidev_close(sc->sc_hdev);
    384  1.115  riastrad 
    385  1.115  riastrad 	/* Hang up all select/poll.  */
    386  1.115  riastrad 	selnotify(&sc->sc_rsel, POLLHUP, 0);
    387  1.115  riastrad 
    388  1.115  riastrad 	/* Reset SIGIO.  */
    389  1.114        ad 	mutex_enter(&proc_lock);
    390  1.116  riastrad 	atomic_store_relaxed(&sc->sc_async, NULL);
    391  1.114        ad 	mutex_exit(&proc_lock);
    392   1.88       mrg 
    393  1.115  riastrad 	/* Free the buffer and queue.  */
    394  1.115  riastrad 	if (sc->sc_osize > 0) {
    395  1.115  riastrad 		kmem_free(sc->sc_obuf, sc->sc_osize);
    396  1.115  riastrad 		sc->sc_obuf = NULL;
    397  1.115  riastrad 	}
    398  1.115  riastrad 	clfree(&sc->sc_q);
    399  1.115  riastrad 
    400  1.120  riastrad 	mutex_enter(&sc->sc_lock);
    401  1.120  riastrad 
    402  1.115  riastrad 	/* All set.  We are now closed.  */
    403  1.120  riastrad 	sc->sc_open = UHID_CLOSED;
    404  1.120  riastrad out:	KASSERT(sc->sc_open == UHID_CLOSED);
    405  1.120  riastrad 	sc->sc_closing = false;
    406  1.120  riastrad 	cv_broadcast(&sc->sc_cv);
    407  1.115  riastrad 	atomic_store_relaxed(&sc->sc_state, 0);
    408  1.115  riastrad 	mutex_exit(&sc->sc_lock);
    409   1.93       mrg 
    410  1.115  riastrad 	return 0;
    411  1.115  riastrad }
    412  1.115  riastrad 
    413  1.115  riastrad static int
    414  1.120  riastrad uhidread(dev_t dev, struct uio *uio, int flag)
    415    1.1  augustss {
    416  1.120  riastrad 	struct uhid_softc *sc = device_lookup_private(&uhid_cd, UHIDUNIT(dev));
    417    1.1  augustss 	int error = 0;
    418   1.47  augustss 	int extra;
    419    1.1  augustss 	size_t length;
    420    1.1  augustss 	u_char buffer[UHID_CHUNK];
    421   1.27  augustss 	usbd_status err;
    422    1.1  augustss 
    423    1.1  augustss 	DPRINTFN(1, ("uhidread\n"));
    424  1.115  riastrad 	if (atomic_load_relaxed(&sc->sc_state) & UHID_IMMED) {
    425    1.2  augustss 		DPRINTFN(1, ("uhidread immed\n"));
    426  1.123  riastrad 		extra = sc->sc_report_id != 0;
    427  1.111      maxv 		if (sc->sc_isize + extra > sizeof(buffer))
    428  1.111      maxv 			return ENOBUFS;
    429  1.123  riastrad 		err = uhidev_get_report(sc->sc_hdev, UHID_INPUT_REPORT,
    430   1.47  augustss 					buffer, sc->sc_isize + extra);
    431   1.27  augustss 		if (err)
    432   1.93       mrg 			return EIO;
    433   1.93       mrg 		return uiomove(buffer+extra, sc->sc_isize, uio);
    434    1.2  augustss 	}
    435    1.2  augustss 
    436   1.88       mrg 	mutex_enter(&sc->sc_lock);
    437    1.1  augustss 	while (sc->sc_q.c_cc == 0) {
    438    1.1  augustss 		if (flag & IO_NDELAY) {
    439   1.88       mrg 			mutex_exit(&sc->sc_lock);
    440   1.93       mrg 			return EWOULDBLOCK;
    441    1.1  augustss 		}
    442  1.120  riastrad 		if (sc->sc_closing) {
    443  1.115  riastrad 			mutex_exit(&sc->sc_lock);
    444  1.115  riastrad 			return EIO;
    445  1.115  riastrad 		}
    446   1.44      yamt 		DPRINTFN(5, ("uhidread: sleep on %p\n", &sc->sc_q));
    447   1.88       mrg 		error = cv_wait_sig(&sc->sc_cv, &sc->sc_lock);
    448    1.1  augustss 		DPRINTFN(5, ("uhidread: woke, error=%d\n", error));
    449    1.1  augustss 		if (error) {
    450   1.18  augustss 			break;
    451    1.1  augustss 		}
    452    1.1  augustss 	}
    453    1.1  augustss 
    454    1.1  augustss 	/* Transfer as many chunks as possible. */
    455   1.18  augustss 	while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0 && !error) {
    456  1.102  riastrad 		length = uimin(sc->sc_q.c_cc, uio->uio_resid);
    457    1.1  augustss 		if (length > sizeof(buffer))
    458    1.1  augustss 			length = sizeof(buffer);
    459    1.1  augustss 
    460    1.1  augustss 		/* Remove a small chunk from the input queue. */
    461    1.1  augustss 		(void) q_to_b(&sc->sc_q, buffer, length);
    462   1.29  augustss 		DPRINTFN(5, ("uhidread: got %lu chars\n", (u_long)length));
    463    1.1  augustss 
    464    1.1  augustss 		/* Copy the data to the user process. */
    465   1.93       mrg 		mutex_exit(&sc->sc_lock);
    466    1.1  augustss 		if ((error = uiomove(buffer, length, uio)) != 0)
    467   1.93       mrg 			return error;
    468   1.93       mrg 		mutex_enter(&sc->sc_lock);
    469    1.1  augustss 	}
    470    1.1  augustss 
    471   1.93       mrg 	mutex_exit(&sc->sc_lock);
    472   1.93       mrg 	return error;
    473    1.1  augustss }
    474    1.1  augustss 
    475  1.109      maxv static int
    476  1.120  riastrad uhidwrite(dev_t dev, struct uio *uio, int flag)
    477   1.18  augustss {
    478  1.120  riastrad 	struct uhid_softc *sc = device_lookup_private(&uhid_cd, UHIDUNIT(dev));
    479    1.1  augustss 	int error;
    480    1.1  augustss 	int size;
    481   1.27  augustss 	usbd_status err;
    482    1.1  augustss 
    483   1.18  augustss 	DPRINTFN(1, ("uhidwrite\n"));
    484   1.52  augustss 
    485    1.1  augustss 	size = sc->sc_osize;
    486   1.97   mlelstv 	if (uio->uio_resid != size || size == 0)
    487   1.93       mrg 		return EINVAL;
    488   1.18  augustss 	error = uiomove(sc->sc_obuf, size, uio);
    489  1.112  christos #ifdef UHID_DEBUG
    490  1.112  christos 	if (uhiddebug > 5) {
    491  1.112  christos 		uint32_t i;
    492  1.112  christos 
    493  1.123  riastrad 		DPRINTF(("%s: outdata[%d] =", device_xname(sc->sc_dev),
    494  1.112  christos 		    error));
    495  1.112  christos 		for (i = 0; i < size; i++)
    496  1.112  christos 			DPRINTF((" %02x", sc->sc_obuf[i]));
    497  1.112  christos 		DPRINTF(("\n"));
    498  1.112  christos 	}
    499  1.112  christos #endif
    500   1.18  augustss 	if (!error) {
    501  1.112  christos 		if (sc->sc_raw)
    502  1.123  riastrad 			err = uhidev_write(sc->sc_hdev, sc->sc_obuf, size);
    503  1.112  christos 		else
    504  1.123  riastrad 			err = uhidev_set_report(sc->sc_hdev,
    505  1.112  christos 			    UHID_OUTPUT_REPORT, sc->sc_obuf, size);
    506  1.112  christos 		if (err) {
    507  1.112  christos 			DPRINTF(("%s: err = %d\n",
    508  1.123  riastrad 			    device_xname(sc->sc_dev), err));
    509    1.1  augustss 			error = EIO;
    510  1.112  christos 		}
    511    1.1  augustss 	}
    512   1.18  augustss 
    513   1.93       mrg 	return error;
    514    1.1  augustss }
    515    1.1  augustss 
    516  1.120  riastrad static int
    517  1.120  riastrad uhidioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
    518    1.1  augustss {
    519  1.120  riastrad 	struct uhid_softc *sc = device_lookup_private(&uhid_cd, UHIDUNIT(dev));
    520    1.1  augustss 	struct usb_ctl_report_desc *rd;
    521    1.2  augustss 	struct usb_ctl_report *re;
    522   1.47  augustss 	u_char buffer[UHID_CHUNK];
    523   1.47  augustss 	int size, extra;
    524   1.27  augustss 	usbd_status err;
    525   1.47  augustss 	void *desc;
    526    1.1  augustss 
    527   1.18  augustss 	DPRINTFN(2, ("uhidioctl: cmd=%lx\n", cmd));
    528   1.18  augustss 
    529    1.1  augustss 	switch (cmd) {
    530    1.2  augustss 	case FIONBIO:
    531    1.2  augustss 		/* All handled in the upper FS layer. */
    532   1.37  augustss 		break;
    533   1.37  augustss 
    534   1.37  augustss 	case FIOASYNC:
    535  1.114        ad 		mutex_enter(&proc_lock);
    536   1.37  augustss 		if (*(int *)addr) {
    537  1.111      maxv 			if (sc->sc_async != NULL) {
    538  1.114        ad 				mutex_exit(&proc_lock);
    539   1.93       mrg 				return EBUSY;
    540  1.111      maxv 			}
    541  1.116  riastrad 			atomic_store_relaxed(&sc->sc_async, l->l_proc);
    542   1.68  christos 			DPRINTF(("uhid_do_ioctl: FIOASYNC %p\n", l->l_proc));
    543   1.37  augustss 		} else
    544  1.116  riastrad 			atomic_store_relaxed(&sc->sc_async, NULL);
    545  1.114        ad 		mutex_exit(&proc_lock);
    546   1.37  augustss 		break;
    547   1.37  augustss 
    548   1.37  augustss 	/* XXX this is not the most general solution. */
    549   1.37  augustss 	case TIOCSPGRP:
    550  1.114        ad 		mutex_enter(&proc_lock);
    551   1.80        ad 		if (sc->sc_async == NULL) {
    552  1.114        ad 			mutex_exit(&proc_lock);
    553   1.93       mrg 			return EINVAL;
    554   1.80        ad 		}
    555   1.80        ad 		if (*(int *)addr != sc->sc_async->p_pgid) {
    556  1.114        ad 			mutex_exit(&proc_lock);
    557   1.93       mrg 			return EPERM;
    558   1.80        ad 		}
    559  1.114        ad 		mutex_exit(&proc_lock);
    560   1.60  jdolecek 		break;
    561   1.60  jdolecek 
    562   1.60  jdolecek 	case FIOSETOWN:
    563  1.114        ad 		mutex_enter(&proc_lock);
    564   1.80        ad 		if (sc->sc_async == NULL) {
    565  1.114        ad 			mutex_exit(&proc_lock);
    566   1.93       mrg 			return EINVAL;
    567   1.80        ad 		}
    568   1.60  jdolecek 		if (-*(int *)addr != sc->sc_async->p_pgid
    569   1.80        ad 		    && *(int *)addr != sc->sc_async->p_pid) {
    570  1.114        ad 			mutex_exit(&proc_lock);
    571   1.93       mrg 			return EPERM;
    572   1.80        ad 		}
    573  1.114        ad 		mutex_exit(&proc_lock);
    574    1.2  augustss 		break;
    575    1.2  augustss 
    576  1.113  christos 	case USB_HID_GET_RAW:
    577  1.113  christos 		*(int *)addr = sc->sc_raw;
    578  1.113  christos 		break;
    579  1.113  christos 
    580  1.113  christos 	case USB_HID_SET_RAW:
    581  1.113  christos 		sc->sc_raw = *(int *)addr;
    582  1.113  christos 		break;
    583  1.113  christos 
    584    1.1  augustss 	case USB_GET_REPORT_DESC:
    585  1.123  riastrad 		uhidev_get_report_desc(sc->sc_hdev, &desc, &size);
    586    1.1  augustss 		rd = (struct usb_ctl_report_desc *)addr;
    587  1.102  riastrad 		size = uimin(size, sizeof(rd->ucrd_data));
    588   1.50  christos 		rd->ucrd_size = size;
    589   1.50  christos 		memcpy(rd->ucrd_data, desc, size);
    590    1.1  augustss 		break;
    591    1.2  augustss 
    592    1.2  augustss 	case USB_SET_IMMED:
    593    1.9  augustss 		if (*(int *)addr) {
    594  1.123  riastrad 			extra = sc->sc_report_id != 0;
    595  1.111      maxv 			if (sc->sc_isize + extra > sizeof(buffer))
    596  1.111      maxv 				return ENOBUFS;
    597  1.123  riastrad 			err = uhidev_get_report(sc->sc_hdev, UHID_INPUT_REPORT,
    598   1.47  augustss 						buffer, sc->sc_isize + extra);
    599   1.27  augustss 			if (err)
    600   1.93       mrg 				return EOPNOTSUPP;
    601   1.12  augustss 
    602  1.115  riastrad 			atomic_or_32(&sc->sc_state, UHID_IMMED);
    603    1.9  augustss 		} else
    604  1.115  riastrad 			atomic_and_32(&sc->sc_state, ~UHID_IMMED);
    605    1.2  augustss 		break;
    606    1.2  augustss 
    607    1.2  augustss 	case USB_GET_REPORT:
    608    1.2  augustss 		re = (struct usb_ctl_report *)addr;
    609   1.50  christos 		switch (re->ucr_report) {
    610    1.2  augustss 		case UHID_INPUT_REPORT:
    611    1.2  augustss 			size = sc->sc_isize;
    612    1.2  augustss 			break;
    613    1.2  augustss 		case UHID_OUTPUT_REPORT:
    614    1.2  augustss 			size = sc->sc_osize;
    615    1.2  augustss 			break;
    616    1.2  augustss 		case UHID_FEATURE_REPORT:
    617    1.2  augustss 			size = sc->sc_fsize;
    618    1.2  augustss 			break;
    619    1.2  augustss 		default:
    620   1.93       mrg 			return EINVAL;
    621    1.2  augustss 		}
    622  1.123  riastrad 		extra = sc->sc_report_id != 0;
    623  1.111      maxv 		if (size + extra > sizeof(re->ucr_data))
    624  1.111      maxv 			return ENOBUFS;
    625  1.123  riastrad 		err = uhidev_get_report(sc->sc_hdev, re->ucr_report,
    626   1.50  christos 		    re->ucr_data, size + extra);
    627   1.47  augustss 		if (extra)
    628   1.99      maya 			memmove(re->ucr_data, re->ucr_data+1, size);
    629   1.35  augustss 		if (err)
    630   1.93       mrg 			return EIO;
    631   1.35  augustss 		break;
    632   1.35  augustss 
    633   1.35  augustss 	case USB_SET_REPORT:
    634   1.35  augustss 		re = (struct usb_ctl_report *)addr;
    635   1.50  christos 		switch (re->ucr_report) {
    636   1.35  augustss 		case UHID_INPUT_REPORT:
    637   1.35  augustss 			size = sc->sc_isize;
    638   1.35  augustss 			break;
    639   1.35  augustss 		case UHID_OUTPUT_REPORT:
    640   1.35  augustss 			size = sc->sc_osize;
    641   1.35  augustss 			break;
    642   1.35  augustss 		case UHID_FEATURE_REPORT:
    643   1.35  augustss 			size = sc->sc_fsize;
    644   1.35  augustss 			break;
    645   1.35  augustss 		default:
    646   1.93       mrg 			return EINVAL;
    647   1.35  augustss 		}
    648  1.111      maxv 		if (size > sizeof(re->ucr_data))
    649  1.111      maxv 			return ENOBUFS;
    650  1.123  riastrad 		err = uhidev_set_report(sc->sc_hdev, re->ucr_report,
    651   1.50  christos 		    re->ucr_data, size);
    652   1.27  augustss 		if (err)
    653   1.93       mrg 			return EIO;
    654    1.2  augustss 		break;
    655    1.2  augustss 
    656   1.47  augustss 	case USB_GET_REPORT_ID:
    657  1.123  riastrad 		*(int *)addr = sc->sc_report_id;
    658   1.47  augustss 		break;
    659   1.47  augustss 
    660   1.87       erh 	case USB_GET_DEVICE_DESC:
    661   1.87       erh 		*(usb_device_descriptor_t *)addr =
    662  1.122  riastrad 			*usbd_get_device_descriptor(sc->sc_udev);
    663   1.87       erh 		break;
    664   1.87       erh 
    665   1.61  jdolecek 	case USB_GET_DEVICEINFO:
    666  1.122  riastrad 		usbd_fill_deviceinfo(sc->sc_udev,
    667   1.95     skrll 				     (struct usb_device_info *)addr, 0);
    668   1.61  jdolecek 		break;
    669   1.73     pavel 	case USB_GET_DEVICEINFO_OLD:
    670  1.106  pgoyette 		MODULE_HOOK_CALL(usb_subr_fill_30_hook,
    671  1.122  riastrad                     (sc->sc_udev,
    672  1.103  pgoyette 		      (struct usb_device_info_old *)addr, 0,
    673  1.103  pgoyette                       usbd_devinfo_vp, usbd_printBCD),
    674  1.103  pgoyette                     enosys(), err);
    675  1.103  pgoyette 		if (err == 0)
    676  1.103  pgoyette 			return 0;
    677   1.73     pavel 		break;
    678   1.95     skrll 	case USB_GET_STRING_DESC:
    679   1.61  jdolecek 	    {
    680   1.95     skrll 		struct usb_string_desc *si = (struct usb_string_desc *)addr;
    681  1.122  riastrad 		err = usbd_get_string_desc(sc->sc_udev,
    682   1.61  jdolecek 			si->usd_string_index,
    683   1.95     skrll 			si->usd_language_id, &si->usd_desc, &size);
    684   1.95     skrll 		if (err)
    685   1.95     skrll 			return EINVAL;
    686   1.95     skrll 		break;
    687   1.61  jdolecek 	    }
    688   1.61  jdolecek 
    689    1.1  augustss 	default:
    690   1.93       mrg 		return EINVAL;
    691    1.1  augustss 	}
    692   1.93       mrg 	return 0;
    693    1.1  augustss }
    694    1.1  augustss 
    695  1.109      maxv static int
    696   1.68  christos uhidpoll(dev_t dev, int events, struct lwp *l)
    697    1.1  augustss {
    698  1.120  riastrad 	struct uhid_softc *sc = device_lookup_private(&uhid_cd, UHIDUNIT(dev));
    699    1.1  augustss 	int revents = 0;
    700   1.25  augustss 
    701   1.88       mrg 	mutex_enter(&sc->sc_lock);
    702    1.1  augustss 	if (events & (POLLOUT | POLLWRNORM))
    703    1.1  augustss 		revents |= events & (POLLOUT | POLLWRNORM);
    704    1.4     veego 	if (events & (POLLIN | POLLRDNORM)) {
    705    1.1  augustss 		if (sc->sc_q.c_cc > 0)
    706    1.1  augustss 			revents |= events & (POLLIN | POLLRDNORM);
    707    1.1  augustss 		else
    708   1.68  christos 			selrecord(l, &sc->sc_rsel);
    709    1.4     veego 	}
    710   1.88       mrg 	mutex_exit(&sc->sc_lock);
    711    1.1  augustss 
    712   1.93       mrg 	return revents;
    713   1.55  jdolecek }
    714   1.55  jdolecek 
    715   1.55  jdolecek static void
    716   1.55  jdolecek filt_uhidrdetach(struct knote *kn)
    717   1.55  jdolecek {
    718   1.55  jdolecek 	struct uhid_softc *sc = kn->kn_hook;
    719   1.55  jdolecek 
    720   1.88       mrg 	mutex_enter(&sc->sc_lock);
    721  1.117   thorpej 	selremove_knote(&sc->sc_rsel, kn);
    722   1.88       mrg 	mutex_exit(&sc->sc_lock);
    723   1.55  jdolecek }
    724   1.55  jdolecek 
    725   1.55  jdolecek static int
    726   1.72  christos filt_uhidread(struct knote *kn, long hint)
    727   1.55  jdolecek {
    728   1.55  jdolecek 	struct uhid_softc *sc = kn->kn_hook;
    729   1.55  jdolecek 
    730  1.115  riastrad 	if (hint == NOTE_SUBMIT)
    731  1.115  riastrad 		KASSERT(mutex_owned(&sc->sc_lock));
    732  1.115  riastrad 	else
    733  1.115  riastrad 		mutex_enter(&sc->sc_lock);
    734  1.115  riastrad 
    735   1.55  jdolecek 	kn->kn_data = sc->sc_q.c_cc;
    736  1.115  riastrad 
    737  1.115  riastrad 	if (hint == NOTE_SUBMIT)
    738  1.115  riastrad 		KASSERT(mutex_owned(&sc->sc_lock));
    739  1.115  riastrad 	else
    740  1.115  riastrad 		mutex_exit(&sc->sc_lock);
    741  1.115  riastrad 
    742   1.95     skrll 	return kn->kn_data > 0;
    743   1.55  jdolecek }
    744   1.55  jdolecek 
    745  1.100      maya static const struct filterops uhidread_filtops = {
    746  1.118   thorpej 	.f_flags = FILTEROP_ISFD,
    747  1.100      maya 	.f_attach = NULL,
    748  1.100      maya 	.f_detach = filt_uhidrdetach,
    749  1.100      maya 	.f_event = filt_uhidread,
    750  1.100      maya };
    751   1.55  jdolecek 
    752  1.109      maxv static int
    753   1.55  jdolecek uhidkqfilter(dev_t dev, struct knote *kn)
    754   1.55  jdolecek {
    755  1.120  riastrad 	struct uhid_softc *sc = device_lookup_private(&uhid_cd, UHIDUNIT(dev));
    756   1.55  jdolecek 
    757   1.55  jdolecek 	switch (kn->kn_filter) {
    758   1.55  jdolecek 	case EVFILT_READ:
    759   1.55  jdolecek 		kn->kn_fop = &uhidread_filtops;
    760  1.119   thorpej 		kn->kn_hook = sc;
    761  1.119   thorpej 		mutex_enter(&sc->sc_lock);
    762  1.119   thorpej 		selrecord_knote(&sc->sc_rsel, kn);
    763  1.119   thorpej 		mutex_exit(&sc->sc_lock);
    764  1.125  christos 		return 0;
    765  1.119   thorpej 
    766   1.55  jdolecek 	case EVFILT_WRITE:
    767  1.119   thorpej 		kn->kn_fop = &seltrue_filtops;
    768  1.125  christos 		return 0;
    769  1.119   thorpej 
    770   1.55  jdolecek 	default:
    771  1.125  christos 		return EINVAL;
    772   1.55  jdolecek 	}
    773    1.1  augustss }
    774