Home | History | Annotate | Line # | Download | only in usb
ugen.c revision 1.159
      1  1.159  riastrad /*	$NetBSD: ugen.c,v 1.159 2021/09/07 10:42:22 riastradh Exp $	*/
      2    1.1  augustss 
      3    1.1  augustss /*
      4   1.71   mycroft  * Copyright (c) 1998, 2004 The NetBSD Foundation, Inc.
      5    1.1  augustss  * All rights reserved.
      6    1.1  augustss  *
      7    1.1  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.1  augustss  * Carlstedt Research & Technology.
     10    1.1  augustss  *
     11   1.84       gdt  * Copyright (c) 2006 BBN Technologies Corp.  All rights reserved.
     12   1.84       gdt  * Effort sponsored in part by the Defense Advanced Research Projects
     13   1.84       gdt  * Agency (DARPA) and the Department of the Interior National Business
     14   1.84       gdt  * Center under agreement number NBCHC050166.
     15   1.84       gdt  *
     16    1.1  augustss  * Redistribution and use in source and binary forms, with or without
     17    1.1  augustss  * modification, are permitted provided that the following conditions
     18    1.1  augustss  * are met:
     19    1.1  augustss  * 1. Redistributions of source code must retain the above copyright
     20    1.1  augustss  *    notice, this list of conditions and the following disclaimer.
     21    1.1  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     22    1.1  augustss  *    notice, this list of conditions and the following disclaimer in the
     23    1.1  augustss  *    documentation and/or other materials provided with the distribution.
     24    1.1  augustss  *
     25    1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     26    1.1  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27    1.1  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28    1.1  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     29    1.1  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30    1.1  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31    1.1  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32    1.1  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33    1.1  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34    1.1  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35    1.1  augustss  * POSSIBILITY OF SUCH DAMAGE.
     36    1.1  augustss  */
     37    1.1  augustss 
     38   1.50     lukem 
     39   1.50     lukem #include <sys/cdefs.h>
     40  1.159  riastrad __KERNEL_RCSID(0, "$NetBSD: ugen.c,v 1.159 2021/09/07 10:42:22 riastradh Exp $");
     41   1.84       gdt 
     42  1.122  christos #ifdef _KERNEL_OPT
     43   1.89     pavel #include "opt_compat_netbsd.h"
     44  1.126      gson #include "opt_usb.h"
     45  1.122  christos #endif
     46    1.1  augustss 
     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.133     skrll #include <sys/kmem.h>
     51    1.1  augustss #include <sys/device.h>
     52    1.1  augustss #include <sys/ioctl.h>
     53   1.12  augustss #include <sys/conf.h>
     54    1.1  augustss #include <sys/tty.h>
     55    1.1  augustss #include <sys/file.h>
     56    1.1  augustss #include <sys/select.h>
     57    1.1  augustss #include <sys/proc.h>
     58    1.1  augustss #include <sys/vnode.h>
     59    1.1  augustss #include <sys/poll.h>
     60  1.142  pgoyette #include <sys/compat_stub.h>
     61  1.155  riastrad #include <sys/module.h>
     62  1.155  riastrad #include <sys/rbtree.h>
     63    1.1  augustss 
     64    1.1  augustss #include <dev/usb/usb.h>
     65    1.1  augustss #include <dev/usb/usbdi.h>
     66    1.1  augustss #include <dev/usb/usbdi_util.h>
     67    1.1  augustss 
     68  1.146       mrg #include "ioconf.h"
     69  1.146       mrg 
     70   1.25  augustss #ifdef UGEN_DEBUG
     71  1.111    dyoung #define DPRINTF(x)	if (ugendebug) printf x
     72  1.111    dyoung #define DPRINTFN(n,x)	if (ugendebug>(n)) printf x
     73    1.2  augustss int	ugendebug = 0;
     74    1.1  augustss #else
     75    1.1  augustss #define DPRINTF(x)
     76    1.1  augustss #define DPRINTFN(n,x)
     77    1.1  augustss #endif
     78    1.1  augustss 
     79   1.41  augustss #define	UGEN_CHUNK	128	/* chunk size for read */
     80   1.41  augustss #define	UGEN_IBSIZE	1020	/* buffer size */
     81   1.41  augustss #define	UGEN_BBSIZE	1024
     82   1.41  augustss 
     83  1.110  jakllsch #define UGEN_NISOREQS	4	/* number of outstanding xfer requests */
     84  1.110  jakllsch #define UGEN_NISORFRMS	8	/* number of transactions per req */
     85  1.110  jakllsch #define UGEN_NISOFRAMES	(UGEN_NISORFRMS * UGEN_NISOREQS)
     86   1.41  augustss 
     87   1.84       gdt #define UGEN_BULK_RA_WB_BUFSIZE	16384		/* default buffer size */
     88   1.84       gdt #define UGEN_BULK_RA_WB_BUFMAX	(1 << 20)	/* maximum allowed buffer */
     89   1.84       gdt 
     90  1.125      matt struct isoreq {
     91  1.125      matt 	struct ugen_endpoint *sce;
     92  1.133     skrll 	struct usbd_xfer *xfer;
     93  1.125      matt 	void *dmabuf;
     94  1.133     skrll 	uint16_t sizes[UGEN_NISORFRMS];
     95  1.125      matt };
     96  1.125      matt 
     97    1.1  augustss struct ugen_endpoint {
     98    1.1  augustss 	struct ugen_softc *sc;
     99    1.1  augustss 	usb_endpoint_descriptor_t *edesc;
    100  1.133     skrll 	struct usbd_interface *iface;
    101    1.1  augustss 	int state;
    102    1.8  augustss #define UGEN_SHORT_OK	0x04	/* short xfers are OK */
    103   1.84       gdt #define UGEN_BULK_RA	0x08	/* in bulk read-ahead mode */
    104   1.84       gdt #define UGEN_BULK_WB	0x10	/* in bulk write-behind mode */
    105   1.84       gdt #define UGEN_RA_WB_STOP	0x20	/* RA/WB xfer is stopped (buffer full/empty) */
    106  1.133     skrll 	struct usbd_pipe *pipeh;
    107    1.1  augustss 	struct clist q;
    108   1.41  augustss 	u_char *ibuf;		/* start of buffer (circular for isoc) */
    109   1.41  augustss 	u_char *fill;		/* location for input (isoc) */
    110   1.41  augustss 	u_char *limit;		/* end of circular buffer (isoc) */
    111   1.41  augustss 	u_char *cur;		/* current read location (isoc) */
    112  1.133     skrll 	uint32_t timeout;
    113  1.133     skrll 	uint32_t ra_wb_bufsize; /* requested size for RA/WB buffer */
    114  1.133     skrll 	uint32_t ra_wb_reqsize; /* requested xfer length for RA/WB */
    115  1.133     skrll 	uint32_t ra_wb_used;	 /* how much is in buffer */
    116  1.133     skrll 	uint32_t ra_wb_xferlen; /* current xfer length for RA/WB */
    117  1.133     skrll 	struct usbd_xfer *ra_wb_xfer;
    118  1.125      matt 	struct isoreq isoreqs[UGEN_NISOREQS];
    119  1.121  riastrad 	/* Keep these last; we don't overwrite them in ugen_set_config() */
    120  1.121  riastrad #define UGEN_ENDPOINT_NONZERO_CRUFT	offsetof(struct ugen_endpoint, rsel)
    121  1.121  riastrad 	struct selinfo rsel;
    122  1.121  riastrad 	kcondvar_t cv;
    123    1.1  augustss };
    124    1.1  augustss 
    125    1.1  augustss struct ugen_softc {
    126  1.111    dyoung 	device_t sc_dev;		/* base device */
    127  1.133     skrll 	struct usbd_device *sc_udev;
    128  1.155  riastrad 	struct rb_node sc_node;
    129  1.155  riastrad 	unsigned sc_unit;
    130    1.1  augustss 
    131  1.120       mrg 	kmutex_t		sc_lock;
    132  1.120       mrg 	kcondvar_t		sc_detach_cv;
    133  1.120       mrg 
    134   1.13  augustss 	char sc_is_open[USB_MAX_ENDPOINTS];
    135    1.1  augustss 	struct ugen_endpoint sc_endpoints[USB_MAX_ENDPOINTS][2];
    136   1.21  augustss #define OUT 0
    137   1.21  augustss #define IN  1
    138    1.1  augustss 
    139   1.12  augustss 	int sc_refcnt;
    140   1.83  christos 	char sc_buffer[UGEN_BBSIZE];
    141   1.12  augustss 	u_char sc_dying;
    142  1.157  riastrad 	u_char sc_attached;
    143    1.1  augustss };
    144    1.1  augustss 
    145  1.155  riastrad static struct {
    146  1.155  riastrad 	kmutex_t	lock;
    147  1.155  riastrad 	rb_tree_t	tree;
    148  1.155  riastrad } ugenif __cacheline_aligned;
    149  1.155  riastrad 
    150  1.155  riastrad static int
    151  1.155  riastrad compare_ugen(void *cookie, const void *vsca, const void *vscb)
    152  1.155  riastrad {
    153  1.155  riastrad 	const struct ugen_softc *sca = vsca;
    154  1.155  riastrad 	const struct ugen_softc *scb = vscb;
    155  1.155  riastrad 
    156  1.155  riastrad 	if (sca->sc_unit < scb->sc_unit)
    157  1.155  riastrad 		return -1;
    158  1.155  riastrad 	if (sca->sc_unit > scb->sc_unit)
    159  1.155  riastrad 		return +1;
    160  1.155  riastrad 	return 0;
    161  1.155  riastrad }
    162  1.155  riastrad 
    163  1.155  riastrad static int
    164  1.155  riastrad compare_ugen_key(void *cookie, const void *vsc, const void *vk)
    165  1.155  riastrad {
    166  1.155  riastrad 	const struct ugen_softc *sc = vsc;
    167  1.155  riastrad 	const unsigned *k = vk;
    168  1.155  riastrad 
    169  1.155  riastrad 	if (sc->sc_unit < *k)
    170  1.155  riastrad 		return -1;
    171  1.155  riastrad 	if (sc->sc_unit > *k)
    172  1.155  riastrad 		return +1;
    173  1.155  riastrad 	return 0;
    174  1.155  riastrad }
    175  1.155  riastrad 
    176  1.155  riastrad static const rb_tree_ops_t ugenif_tree_ops = {
    177  1.155  riastrad 	.rbto_compare_nodes = compare_ugen,
    178  1.155  riastrad 	.rbto_compare_key = compare_ugen_key,
    179  1.156  riastrad 	.rbto_node_offset = offsetof(struct ugen_softc, sc_node),
    180  1.155  riastrad };
    181  1.155  riastrad 
    182  1.155  riastrad static void
    183  1.155  riastrad ugenif_get_unit(struct ugen_softc *sc)
    184  1.155  riastrad {
    185  1.155  riastrad 	struct ugen_softc *sc0;
    186  1.155  riastrad 	unsigned i;
    187  1.155  riastrad 
    188  1.155  riastrad 	mutex_enter(&ugenif.lock);
    189  1.155  riastrad 	for (i = 0, sc0 = RB_TREE_MIN(&ugenif.tree);
    190  1.155  riastrad 	     sc0 != NULL && i == sc0->sc_unit;
    191  1.155  riastrad 	     i++, sc0 = RB_TREE_NEXT(&ugenif.tree, sc0))
    192  1.155  riastrad 		KASSERT(i < UINT_MAX);
    193  1.155  riastrad 	KASSERT(rb_tree_find_node(&ugenif.tree, &i) == NULL);
    194  1.155  riastrad 	sc->sc_unit = i;
    195  1.155  riastrad 	sc0 = rb_tree_insert_node(&ugenif.tree, sc);
    196  1.155  riastrad 	KASSERT(sc0 == sc);
    197  1.155  riastrad 	KASSERT(rb_tree_find_node(&ugenif.tree, &i) == sc);
    198  1.155  riastrad 	mutex_exit(&ugenif.lock);
    199  1.155  riastrad }
    200  1.155  riastrad 
    201  1.155  riastrad static void
    202  1.155  riastrad ugenif_put_unit(struct ugen_softc *sc)
    203  1.155  riastrad {
    204  1.155  riastrad 
    205  1.155  riastrad 	mutex_enter(&ugenif.lock);
    206  1.155  riastrad 	KASSERT(rb_tree_find_node(&ugenif.tree, &sc->sc_unit) == sc);
    207  1.155  riastrad 	rb_tree_remove_node(&ugenif.tree, sc);
    208  1.155  riastrad 	sc->sc_unit = -1;
    209  1.155  riastrad 	mutex_exit(&ugenif.lock);
    210  1.155  riastrad }
    211  1.155  riastrad 
    212  1.155  riastrad static struct ugen_softc *
    213  1.155  riastrad ugenif_acquire(unsigned unit)
    214  1.155  riastrad {
    215  1.155  riastrad 	struct ugen_softc *sc;
    216  1.155  riastrad 
    217  1.155  riastrad 	mutex_enter(&ugenif.lock);
    218  1.155  riastrad 	sc = rb_tree_find_node(&ugenif.tree, &unit);
    219  1.156  riastrad 	if (sc == NULL)
    220  1.156  riastrad 		goto out;
    221  1.156  riastrad 	mutex_enter(&sc->sc_lock);
    222  1.156  riastrad 	if (sc->sc_dying) {
    223  1.155  riastrad 		mutex_exit(&sc->sc_lock);
    224  1.156  riastrad 		sc = NULL;
    225  1.156  riastrad 		goto out;
    226  1.155  riastrad 	}
    227  1.156  riastrad 	KASSERT(sc->sc_refcnt < INT_MAX);
    228  1.156  riastrad 	sc->sc_refcnt++;
    229  1.156  riastrad 	mutex_exit(&sc->sc_lock);
    230  1.156  riastrad out:	mutex_exit(&ugenif.lock);
    231  1.155  riastrad 
    232  1.155  riastrad 	return sc;
    233  1.155  riastrad }
    234  1.155  riastrad 
    235  1.155  riastrad static void
    236  1.155  riastrad ugenif_release(struct ugen_softc *sc)
    237  1.155  riastrad {
    238  1.155  riastrad 
    239  1.155  riastrad 	mutex_enter(&sc->sc_lock);
    240  1.155  riastrad 	if (--sc->sc_refcnt < 0)
    241  1.155  riastrad 		cv_broadcast(&sc->sc_detach_cv);
    242  1.155  riastrad 	mutex_exit(&sc->sc_lock);
    243  1.155  riastrad }
    244  1.155  riastrad 
    245  1.147      maxv static dev_type_open(ugenopen);
    246  1.147      maxv static dev_type_close(ugenclose);
    247  1.147      maxv static dev_type_read(ugenread);
    248  1.147      maxv static dev_type_write(ugenwrite);
    249  1.147      maxv static dev_type_ioctl(ugenioctl);
    250  1.147      maxv static dev_type_poll(ugenpoll);
    251  1.147      maxv static dev_type_kqfilter(ugenkqfilter);
    252   1.60   gehenna 
    253   1.60   gehenna const struct cdevsw ugen_cdevsw = {
    254  1.123  dholland 	.d_open = ugenopen,
    255  1.123  dholland 	.d_close = ugenclose,
    256  1.123  dholland 	.d_read = ugenread,
    257  1.123  dholland 	.d_write = ugenwrite,
    258  1.123  dholland 	.d_ioctl = ugenioctl,
    259  1.123  dholland 	.d_stop = nostop,
    260  1.123  dholland 	.d_tty = notty,
    261  1.123  dholland 	.d_poll = ugenpoll,
    262  1.123  dholland 	.d_mmap = nommap,
    263  1.123  dholland 	.d_kqfilter = ugenkqfilter,
    264  1.124  dholland 	.d_discard = nodiscard,
    265  1.123  dholland 	.d_flag = D_OTHER,
    266   1.60   gehenna };
    267   1.14  augustss 
    268  1.133     skrll Static void ugenintr(struct usbd_xfer *, void *,
    269  1.133     skrll 		     usbd_status);
    270  1.133     skrll Static void ugen_isoc_rintr(struct usbd_xfer *, void *,
    271  1.133     skrll 			    usbd_status);
    272  1.133     skrll Static void ugen_bulkra_intr(struct usbd_xfer *, void *,
    273  1.133     skrll 			     usbd_status);
    274  1.133     skrll Static void ugen_bulkwb_intr(struct usbd_xfer *, void *,
    275  1.133     skrll 			     usbd_status);
    276   1.40  augustss Static int ugen_do_read(struct ugen_softc *, int, struct uio *, int);
    277   1.40  augustss Static int ugen_do_write(struct ugen_softc *, int, struct uio *, int);
    278   1.55  augustss Static int ugen_do_ioctl(struct ugen_softc *, int, u_long,
    279   1.92  christos 			 void *, int, struct lwp *);
    280  1.138        ws Static int ugen_set_config(struct ugen_softc *, int, int);
    281  1.133     skrll Static usb_config_descriptor_t *ugen_get_cdesc(struct ugen_softc *,
    282  1.133     skrll 					       int, int *);
    283   1.40  augustss Static usbd_status ugen_set_interface(struct ugen_softc *, int, int);
    284  1.133     skrll Static int ugen_get_alt_index(struct ugen_softc *, int);
    285  1.131     skrll Static void ugen_clear_endpoints(struct ugen_softc *);
    286    1.1  augustss 
    287   1.12  augustss #define UGENUNIT(n) ((minor(n) >> 4) & 0xf)
    288   1.12  augustss #define UGENENDPOINT(n) (minor(n) & 0xf)
    289   1.12  augustss #define UGENDEV(u, e) (makedev(0, ((u) << 4) | (e)))
    290    1.1  augustss 
    291  1.147      maxv static int	ugenif_match(device_t, cfdata_t, void *);
    292  1.147      maxv static void	ugenif_attach(device_t, device_t, void *);
    293  1.147      maxv static int	ugen_match(device_t, cfdata_t, void *);
    294  1.147      maxv static void	ugen_attach(device_t, device_t, void *);
    295  1.147      maxv static int	ugen_detach(device_t, int);
    296  1.147      maxv static int	ugen_activate(device_t, enum devact);
    297  1.146       mrg 
    298  1.138        ws CFATTACH_DECL_NEW(ugen, sizeof(struct ugen_softc), ugen_match,
    299  1.138        ws     ugen_attach, ugen_detach, ugen_activate);
    300  1.138        ws CFATTACH_DECL_NEW(ugenif, sizeof(struct ugen_softc), ugenif_match,
    301  1.138        ws     ugenif_attach, ugen_detach, ugen_activate);
    302    1.1  augustss 
    303  1.105     pooka /* toggle to control attach priority. -1 means "let autoconf decide" */
    304  1.105     pooka int ugen_override = -1;
    305  1.105     pooka 
    306  1.147      maxv static int
    307  1.111    dyoung ugen_match(device_t parent, cfdata_t match, void *aux)
    308    1.1  augustss {
    309  1.111    dyoung 	struct usb_attach_arg *uaa = aux;
    310  1.105     pooka 	int override;
    311  1.105     pooka 
    312  1.105     pooka 	if (ugen_override != -1)
    313  1.105     pooka 		override = ugen_override;
    314  1.105     pooka 	else
    315  1.105     pooka 		override = match->cf_flags & 1;
    316    1.1  augustss 
    317  1.105     pooka 	if (override)
    318  1.133     skrll 		return UMATCH_HIGHEST;
    319  1.133     skrll 	else if (uaa->uaa_usegeneric)
    320  1.133     skrll 		return UMATCH_GENERIC;
    321    1.1  augustss 	else
    322  1.133     skrll 		return UMATCH_NONE;
    323    1.1  augustss }
    324    1.1  augustss 
    325  1.147      maxv static int
    326  1.138        ws ugenif_match(device_t parent, cfdata_t match, void *aux)
    327  1.138        ws {
    328  1.139        ws 	/* Assume that they knew what they configured! (see ugenif(4)) */
    329  1.139        ws 	return UMATCH_HIGHEST;
    330  1.138        ws }
    331  1.138        ws 
    332  1.147      maxv static void
    333  1.111    dyoung ugen_attach(device_t parent, device_t self, void *aux)
    334    1.1  augustss {
    335  1.138        ws 	struct usb_attach_arg *uaa = aux;
    336  1.138        ws 	struct usbif_attach_arg uiaa;
    337  1.138        ws 
    338  1.151     skrll 	memset(&uiaa, 0, sizeof(uiaa));
    339  1.138        ws 	uiaa.uiaa_port = uaa->uaa_port;
    340  1.138        ws 	uiaa.uiaa_vendor = uaa->uaa_vendor;
    341  1.138        ws 	uiaa.uiaa_product = uaa->uaa_product;
    342  1.138        ws 	uiaa.uiaa_release = uaa->uaa_release;
    343  1.138        ws 	uiaa.uiaa_device = uaa->uaa_device;
    344  1.138        ws 	uiaa.uiaa_configno = -1;
    345  1.138        ws 	uiaa.uiaa_ifaceno = -1;
    346  1.138        ws 
    347  1.138        ws 	ugenif_attach(parent, self, &uiaa);
    348  1.138        ws }
    349  1.138        ws 
    350  1.147      maxv static void
    351  1.138        ws ugenif_attach(device_t parent, device_t self, void *aux)
    352  1.138        ws {
    353  1.111    dyoung 	struct ugen_softc *sc = device_private(self);
    354  1.138        ws 	struct usbif_attach_arg *uiaa = aux;
    355  1.133     skrll 	struct usbd_device *udev;
    356   1.72  augustss 	char *devinfop;
    357   1.28  augustss 	usbd_status err;
    358   1.97     rmind 	int i, dir, conf;
    359   1.59  augustss 
    360  1.103    plunky 	aprint_naive("\n");
    361  1.103    plunky 	aprint_normal("\n");
    362  1.103    plunky 
    363  1.133     skrll 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
    364  1.120       mrg 	cv_init(&sc->sc_detach_cv, "ugendet");
    365  1.120       mrg 
    366  1.138        ws 	devinfop = usbd_devinfo_alloc(uiaa->uiaa_device, 0);
    367   1.99      cube 	aprint_normal_dev(self, "%s\n", devinfop);
    368   1.72  augustss 	usbd_devinfo_free(devinfop);
    369    1.1  augustss 
    370   1.99      cube 	sc->sc_dev = self;
    371  1.138        ws 	sc->sc_udev = udev = uiaa->uiaa_device;
    372   1.31  augustss 
    373  1.121  riastrad 	for (i = 0; i < USB_MAX_ENDPOINTS; i++) {
    374  1.121  riastrad 		for (dir = OUT; dir <= IN; dir++) {
    375  1.121  riastrad 			struct ugen_endpoint *sce;
    376  1.121  riastrad 
    377  1.121  riastrad 			sce = &sc->sc_endpoints[i][dir];
    378  1.121  riastrad 			selinit(&sce->rsel);
    379  1.121  riastrad 			cv_init(&sce->cv, "ugensce");
    380  1.121  riastrad 		}
    381  1.121  riastrad 	}
    382  1.121  riastrad 
    383  1.157  riastrad 	if (!pmf_device_register(self, NULL, NULL))
    384  1.157  riastrad 		aprint_error_dev(self, "couldn't establish power handler\n");
    385  1.157  riastrad 
    386  1.138        ws 	if (uiaa->uiaa_ifaceno < 0) {
    387  1.138        ws 		/*
    388  1.138        ws 		 * If we attach the whole device,
    389  1.138        ws 		 * set configuration index 0, the default one.
    390  1.138        ws 		 */
    391  1.138        ws 		err = usbd_set_config_index(udev, 0, 0);
    392  1.138        ws 		if (err) {
    393  1.138        ws 			aprint_error_dev(self,
    394  1.138        ws 			    "setting configuration index 0 failed\n");
    395  1.138        ws 			return;
    396  1.138        ws 		}
    397   1.31  augustss 	}
    398  1.138        ws 
    399  1.138        ws 	/* Get current configuration */
    400   1.31  augustss 	conf = usbd_get_config_descriptor(udev)->bConfigurationValue;
    401   1.31  augustss 
    402   1.31  augustss 	/* Set up all the local state for this configuration. */
    403  1.138        ws 	err = ugen_set_config(sc, conf, uiaa->uiaa_ifaceno < 0);
    404   1.28  augustss 	if (err) {
    405   1.99      cube 		aprint_error_dev(self, "setting configuration %d failed\n",
    406   1.99      cube 		    conf);
    407  1.111    dyoung 		return;
    408    1.1  augustss 	}
    409   1.30  augustss 
    410  1.155  riastrad 	ugenif_get_unit(sc);
    411  1.134   msaitoh 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
    412  1.157  riastrad 	sc->sc_attached = 1;
    413    1.1  augustss }
    414    1.1  augustss 
    415  1.130     skrll Static void
    416  1.130     skrll ugen_clear_endpoints(struct ugen_softc *sc)
    417  1.130     skrll {
    418  1.130     skrll 
    419  1.130     skrll 	/* Clear out the old info, but leave the selinfo and cv initialised. */
    420  1.130     skrll 	for (int i = 0; i < USB_MAX_ENDPOINTS; i++) {
    421  1.130     skrll 		for (int dir = OUT; dir <= IN; dir++) {
    422  1.130     skrll 			struct ugen_endpoint *sce = &sc->sc_endpoints[i][dir];
    423  1.130     skrll 			memset(sce, 0, UGEN_ENDPOINT_NONZERO_CRUFT);
    424  1.130     skrll 		}
    425  1.130     skrll 	}
    426  1.130     skrll }
    427  1.130     skrll 
    428   1.37  augustss Static int
    429  1.138        ws ugen_set_config(struct ugen_softc *sc, int configno, int chkopen)
    430    1.1  augustss {
    431  1.133     skrll 	struct usbd_device *dev = sc->sc_udev;
    432  1.102  drochner 	usb_config_descriptor_t *cdesc;
    433  1.133     skrll 	struct usbd_interface *iface;
    434    1.1  augustss 	usb_endpoint_descriptor_t *ed;
    435    1.1  augustss 	struct ugen_endpoint *sce;
    436  1.133     skrll 	uint8_t niface, nendpt;
    437    1.1  augustss 	int ifaceno, endptno, endpt;
    438   1.28  augustss 	usbd_status err;
    439  1.130     skrll 	int dir;
    440    1.1  augustss 
    441    1.1  augustss 	DPRINTFN(1,("ugen_set_config: %s to configno %d, sc=%p\n",
    442  1.111    dyoung 		    device_xname(sc->sc_dev), configno, sc));
    443   1.53  augustss 
    444  1.159  riastrad 	KASSERT(KERNEL_LOCKED_P()); /* sc_is_open */
    445  1.159  riastrad 
    446  1.138        ws 	if (chkopen) {
    447  1.138        ws 		/*
    448  1.138        ws 		 * We start at 1, not 0, because we don't care whether the
    449  1.138        ws 		 * control endpoint is open or not. It is always present.
    450  1.138        ws 		 */
    451  1.138        ws 		for (endptno = 1; endptno < USB_MAX_ENDPOINTS; endptno++)
    452  1.138        ws 			if (sc->sc_is_open[endptno]) {
    453  1.138        ws 				DPRINTFN(1,
    454  1.138        ws 				     ("ugen_set_config: %s - endpoint %d is open\n",
    455  1.138        ws 				      device_xname(sc->sc_dev), endptno));
    456  1.138        ws 				return USBD_IN_USE;
    457  1.138        ws 			}
    458  1.138        ws 	}
    459   1.53  augustss 
    460   1.31  augustss 	/* Avoid setting the current value. */
    461  1.102  drochner 	cdesc = usbd_get_config_descriptor(dev);
    462  1.102  drochner 	if (!cdesc || cdesc->bConfigurationValue != configno) {
    463   1.43  augustss 		err = usbd_set_config_no(dev, configno, 1);
    464   1.28  augustss 		if (err)
    465  1.133     skrll 			return err;
    466    1.7  augustss 	}
    467    1.1  augustss 
    468  1.138        ws 	ugen_clear_endpoints(sc);
    469  1.138        ws 
    470   1.28  augustss 	err = usbd_interface_count(dev, &niface);
    471   1.28  augustss 	if (err)
    472  1.133     skrll 		return err;
    473  1.120       mrg 
    474    1.1  augustss 	for (ifaceno = 0; ifaceno < niface; ifaceno++) {
    475    1.1  augustss 		DPRINTFN(1,("ugen_set_config: ifaceno %d\n", ifaceno));
    476   1.28  augustss 		err = usbd_device2interface_handle(dev, ifaceno, &iface);
    477   1.28  augustss 		if (err)
    478  1.133     skrll 			return err;
    479   1.28  augustss 		err = usbd_endpoint_count(iface, &nendpt);
    480   1.28  augustss 		if (err)
    481  1.133     skrll 			return err;
    482    1.1  augustss 		for (endptno = 0; endptno < nendpt; endptno++) {
    483    1.1  augustss 			ed = usbd_interface2endpoint_descriptor(iface,endptno);
    484   1.82  christos 			KASSERT(ed != NULL);
    485    1.1  augustss 			endpt = ed->bEndpointAddress;
    486   1.21  augustss 			dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
    487   1.21  augustss 			sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
    488  1.150  christos 			DPRINTFN(1,("ugen_set_config: endptno %d, endpt=0x%02x"
    489   1.55  augustss 				    "(%d,%d), sce=%p\n",
    490    1.1  augustss 				    endptno, endpt, UE_GET_ADDR(endpt),
    491   1.21  augustss 				    UE_GET_DIR(endpt), sce));
    492    1.1  augustss 			sce->sc = sc;
    493    1.1  augustss 			sce->edesc = ed;
    494    1.1  augustss 			sce->iface = iface;
    495    1.1  augustss 		}
    496    1.1  augustss 	}
    497  1.133     skrll 	return USBD_NORMAL_COMPLETION;
    498    1.1  augustss }
    499    1.1  augustss 
    500  1.147      maxv static int
    501   1.88  christos ugenopen(dev_t dev, int flag, int mode, struct lwp *l)
    502    1.1  augustss {
    503   1.24  augustss 	struct ugen_softc *sc;
    504    1.1  augustss 	int unit = UGENUNIT(dev);
    505    1.1  augustss 	int endpt = UGENENDPOINT(dev);
    506    1.1  augustss 	usb_endpoint_descriptor_t *edesc;
    507    1.1  augustss 	struct ugen_endpoint *sce;
    508    1.1  augustss 	int dir, isize;
    509   1.28  augustss 	usbd_status err;
    510  1.133     skrll 	struct usbd_xfer *xfer;
    511   1.41  augustss 	int i, j;
    512  1.155  riastrad 	int error;
    513    1.1  augustss 
    514  1.159  riastrad 	KASSERT(KERNEL_LOCKED_P()); /* sc_is_open */
    515  1.159  riastrad 
    516  1.155  riastrad 	if ((sc = ugenif_acquire(unit)) == NULL)
    517  1.111    dyoung 		return ENXIO;
    518   1.30  augustss 
    519   1.55  augustss 	DPRINTFN(5, ("ugenopen: flag=%d, mode=%d, unit=%d endpt=%d\n",
    520    1.9  augustss 		     flag, mode, unit, endpt));
    521    1.1  augustss 
    522   1.73  augustss 	/* The control endpoint allows multiple opens. */
    523    1.1  augustss 	if (endpt == USB_CONTROL_ENDPOINT) {
    524   1.13  augustss 		sc->sc_is_open[USB_CONTROL_ENDPOINT] = 1;
    525  1.155  riastrad 		error = 0;
    526  1.155  riastrad 		goto out;
    527   1.13  augustss 	}
    528   1.35  augustss 
    529  1.155  riastrad 	if (sc->sc_is_open[endpt]) {
    530  1.155  riastrad 		error = EBUSY;
    531  1.155  riastrad 		goto out;
    532  1.155  riastrad 	}
    533   1.73  augustss 
    534   1.13  augustss 	/* Make sure there are pipes for all directions. */
    535   1.13  augustss 	for (dir = OUT; dir <= IN; dir++) {
    536   1.13  augustss 		if (flag & (dir == OUT ? FWRITE : FREAD)) {
    537   1.13  augustss 			sce = &sc->sc_endpoints[endpt][dir];
    538  1.155  riastrad 			if (sce->edesc == NULL) {
    539  1.155  riastrad 				error = ENXIO;
    540  1.155  riastrad 				goto out;
    541  1.155  riastrad 			}
    542    1.1  augustss 		}
    543   1.13  augustss 	}
    544   1.13  augustss 
    545   1.13  augustss 	/* Actually open the pipes. */
    546   1.13  augustss 	/* XXX Should back out properly if it fails. */
    547   1.13  augustss 	for (dir = OUT; dir <= IN; dir++) {
    548   1.13  augustss 		if (!(flag & (dir == OUT ? FWRITE : FREAD)))
    549   1.13  augustss 			continue;
    550    1.1  augustss 		sce = &sc->sc_endpoints[endpt][dir];
    551   1.13  augustss 		sce->state = 0;
    552   1.17  augustss 		sce->timeout = USBD_NO_TIMEOUT;
    553   1.55  augustss 		DPRINTFN(5, ("ugenopen: sc=%p, endpt=%d, dir=%d, sce=%p\n",
    554    1.3  augustss 			     sc, endpt, dir, sce));
    555    1.1  augustss 		edesc = sce->edesc;
    556    1.1  augustss 		switch (edesc->bmAttributes & UE_XFERTYPE) {
    557    1.1  augustss 		case UE_INTERRUPT:
    558   1.69  augustss 			if (dir == OUT) {
    559   1.69  augustss 				err = usbd_open_pipe(sce->iface,
    560   1.69  augustss 				    edesc->bEndpointAddress, 0, &sce->pipeh);
    561  1.155  riastrad 				if (err) {
    562  1.155  riastrad 					error = EIO;
    563  1.155  riastrad 					goto out;
    564  1.155  riastrad 				}
    565   1.69  augustss 				break;
    566   1.69  augustss 			}
    567    1.1  augustss 			isize = UGETW(edesc->wMaxPacketSize);
    568  1.155  riastrad 			if (isize == 0) {	/* shouldn't happen */
    569  1.155  riastrad 				error = EINVAL;
    570  1.155  riastrad 				goto out;
    571  1.155  riastrad 			}
    572  1.133     skrll 			sce->ibuf = kmem_alloc(isize, KM_SLEEP);
    573   1.55  augustss 			DPRINTFN(5, ("ugenopen: intr endpt=%d,isize=%d\n",
    574    1.1  augustss 				     endpt, isize));
    575  1.121  riastrad 			if (clalloc(&sce->q, UGEN_IBSIZE, 0) == -1) {
    576  1.133     skrll 				kmem_free(sce->ibuf, isize);
    577  1.121  riastrad 				sce->ibuf = NULL;
    578  1.155  riastrad 				error = ENOMEM;
    579  1.155  riastrad 				goto out;
    580  1.121  riastrad 			}
    581   1.55  augustss 			err = usbd_open_pipe_intr(sce->iface,
    582   1.55  augustss 				  edesc->bEndpointAddress,
    583   1.55  augustss 				  USBD_SHORT_XFER_OK, &sce->pipeh, sce,
    584   1.55  augustss 				  sce->ibuf, isize, ugenintr,
    585   1.32  augustss 				  USBD_DEFAULT_INTERVAL);
    586   1.28  augustss 			if (err) {
    587  1.121  riastrad 				clfree(&sce->q);
    588  1.133     skrll 				kmem_free(sce->ibuf, isize);
    589  1.121  riastrad 				sce->ibuf = NULL;
    590  1.155  riastrad 				error = EIO;
    591  1.155  riastrad 				goto out;
    592    1.1  augustss 			}
    593    1.1  augustss 			DPRINTFN(5, ("ugenopen: interrupt open done\n"));
    594    1.1  augustss 			break;
    595    1.1  augustss 		case UE_BULK:
    596   1.55  augustss 			err = usbd_open_pipe(sce->iface,
    597   1.28  augustss 				  edesc->bEndpointAddress, 0, &sce->pipeh);
    598  1.155  riastrad 			if (err) {
    599  1.155  riastrad 				error = EIO;
    600  1.155  riastrad 				goto out;
    601  1.155  riastrad 			}
    602   1.84       gdt 			sce->ra_wb_bufsize = UGEN_BULK_RA_WB_BUFSIZE;
    603  1.121  riastrad 			/*
    604   1.84       gdt 			 * Use request size for non-RA/WB transfers
    605   1.84       gdt 			 * as the default.
    606   1.84       gdt 			 */
    607   1.84       gdt 			sce->ra_wb_reqsize = UGEN_BBSIZE;
    608    1.1  augustss 			break;
    609   1.41  augustss 		case UE_ISOCHRONOUS:
    610  1.155  riastrad 			if (dir == OUT) {
    611  1.155  riastrad 				error = EINVAL;
    612  1.155  riastrad 				goto out;
    613  1.155  riastrad 			}
    614   1.41  augustss 			isize = UGETW(edesc->wMaxPacketSize);
    615  1.156  riastrad 			if (isize == 0) {	/* shouldn't happen */
    616  1.156  riastrad 				error = EINVAL;
    617  1.156  riastrad 				goto out;
    618  1.156  riastrad 			}
    619  1.133     skrll 			sce->ibuf = kmem_alloc(isize * UGEN_NISOFRAMES,
    620  1.133     skrll 				KM_SLEEP);
    621   1.41  augustss 			sce->cur = sce->fill = sce->ibuf;
    622   1.41  augustss 			sce->limit = sce->ibuf + isize * UGEN_NISOFRAMES;
    623   1.55  augustss 			DPRINTFN(5, ("ugenopen: isoc endpt=%d, isize=%d\n",
    624   1.41  augustss 				     endpt, isize));
    625   1.41  augustss 			err = usbd_open_pipe(sce->iface,
    626   1.41  augustss 				  edesc->bEndpointAddress, 0, &sce->pipeh);
    627   1.41  augustss 			if (err) {
    628  1.133     skrll 				kmem_free(sce->ibuf, isize * UGEN_NISOFRAMES);
    629  1.121  riastrad 				sce->ibuf = NULL;
    630  1.155  riastrad 				error = EIO;
    631  1.155  riastrad 				goto out;
    632   1.41  augustss 			}
    633  1.132     skrll 			for (i = 0; i < UGEN_NISOREQS; ++i) {
    634   1.41  augustss 				sce->isoreqs[i].sce = sce;
    635  1.133     skrll 				err = usbd_create_xfer(sce->pipeh,
    636  1.133     skrll 				    isize * UGEN_NISORFRMS, 0, UGEN_NISORFRMS,
    637  1.133     skrll 				    &xfer);
    638  1.133     skrll 				if (err)
    639   1.41  augustss 					goto bad;
    640   1.41  augustss 				sce->isoreqs[i].xfer = xfer;
    641  1.133     skrll 				sce->isoreqs[i].dmabuf = usbd_get_buffer(xfer);
    642  1.132     skrll 				for (j = 0; j < UGEN_NISORFRMS; ++j)
    643   1.41  augustss 					sce->isoreqs[i].sizes[j] = isize;
    644  1.133     skrll 				usbd_setup_isoc_xfer(xfer, &sce->isoreqs[i],
    645  1.133     skrll 				    sce->isoreqs[i].sizes, UGEN_NISORFRMS, 0,
    646  1.133     skrll 				    ugen_isoc_rintr);
    647   1.41  augustss 				(void)usbd_transfer(xfer);
    648   1.41  augustss 			}
    649   1.41  augustss 			DPRINTFN(5, ("ugenopen: isoc open done\n"));
    650   1.41  augustss 			break;
    651   1.41  augustss 		bad:
    652   1.41  augustss 			while (--i >= 0) /* implicit buffer free */
    653  1.133     skrll 				usbd_destroy_xfer(sce->isoreqs[i].xfer);
    654  1.121  riastrad 			usbd_close_pipe(sce->pipeh);
    655  1.121  riastrad 			sce->pipeh = NULL;
    656  1.133     skrll 			kmem_free(sce->ibuf, isize * UGEN_NISOFRAMES);
    657  1.121  riastrad 			sce->ibuf = NULL;
    658  1.155  riastrad 			error = ENOMEM;
    659  1.155  riastrad 			goto out;
    660    1.1  augustss 		case UE_CONTROL:
    661   1.57  augustss 			sce->timeout = USBD_DEFAULT_TIMEOUT;
    662  1.155  riastrad 			error = EINVAL;
    663  1.155  riastrad 			goto out;
    664    1.1  augustss 		}
    665    1.1  augustss 	}
    666   1.13  augustss 	sc->sc_is_open[endpt] = 1;
    667  1.155  riastrad 	error = 0;
    668  1.155  riastrad out:	ugenif_release(sc);
    669  1.155  riastrad 	return error;
    670    1.1  augustss }
    671    1.1  augustss 
    672  1.147      maxv static int
    673   1.88  christos ugenclose(dev_t dev, int flag, int mode, struct lwp *l)
    674    1.1  augustss {
    675    1.1  augustss 	int endpt = UGENENDPOINT(dev);
    676   1.24  augustss 	struct ugen_softc *sc;
    677    1.1  augustss 	struct ugen_endpoint *sce;
    678    1.1  augustss 	int dir;
    679   1.41  augustss 	int i;
    680  1.155  riastrad 	int error;
    681    1.1  augustss 
    682  1.159  riastrad 	KASSERT(KERNEL_LOCKED_P()); /* sc_is_open */
    683  1.159  riastrad 
    684  1.155  riastrad 	if ((sc = ugenif_acquire(UGENUNIT(dev))) == NULL)
    685  1.111    dyoung 		return ENXIO;
    686   1.30  augustss 
    687  1.101  drochner 	DPRINTFN(5, ("ugenclose: flag=%d, mode=%d, unit=%d, endpt=%d\n",
    688   1.13  augustss 		     flag, mode, UGENUNIT(dev), endpt));
    689   1.13  augustss 
    690  1.154  riastrad 	KASSERT(sc->sc_is_open[endpt]);
    691    1.1  augustss 
    692    1.1  augustss 	if (endpt == USB_CONTROL_ENDPOINT) {
    693    1.9  augustss 		DPRINTFN(5, ("ugenclose: close control\n"));
    694   1.13  augustss 		sc->sc_is_open[endpt] = 0;
    695  1.155  riastrad 		error = 0;
    696  1.155  riastrad 		goto out;
    697    1.1  augustss 	}
    698    1.9  augustss 
    699    1.9  augustss 	for (dir = OUT; dir <= IN; dir++) {
    700   1.13  augustss 		if (!(flag & (dir == OUT ? FWRITE : FREAD)))
    701   1.13  augustss 			continue;
    702   1.13  augustss 		sce = &sc->sc_endpoints[endpt][dir];
    703  1.129     skrll 		if (sce->pipeh == NULL)
    704   1.13  augustss 			continue;
    705   1.55  augustss 		DPRINTFN(5, ("ugenclose: endpt=%d dir=%d sce=%p\n",
    706   1.13  augustss 			     endpt, dir, sce));
    707   1.59  augustss 
    708   1.13  augustss 		usbd_abort_pipe(sce->pipeh);
    709  1.133     skrll 
    710  1.133     skrll 		int isize = UGETW(sce->edesc->wMaxPacketSize);
    711  1.133     skrll 		int msize = 0;
    712   1.41  augustss 
    713   1.56  augustss 		switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
    714   1.56  augustss 		case UE_INTERRUPT:
    715   1.56  augustss 			ndflush(&sce->q, sce->q.c_cc);
    716   1.56  augustss 			clfree(&sce->q);
    717  1.133     skrll 			msize = isize;
    718   1.56  augustss 			break;
    719   1.56  augustss 		case UE_ISOCHRONOUS:
    720   1.56  augustss 			for (i = 0; i < UGEN_NISOREQS; ++i)
    721  1.133     skrll 				usbd_destroy_xfer(sce->isoreqs[i].xfer);
    722  1.133     skrll 			msize = isize * UGEN_NISOFRAMES;
    723   1.84       gdt 			break;
    724   1.84       gdt 		case UE_BULK:
    725  1.133     skrll 			if (sce->state & (UGEN_BULK_RA | UGEN_BULK_WB)) {
    726  1.133     skrll 				usbd_destroy_xfer(sce->ra_wb_xfer);
    727  1.133     skrll 				msize = sce->ra_wb_bufsize;
    728  1.133     skrll 			}
    729   1.84       gdt 			break;
    730   1.56  augustss 		default:
    731   1.56  augustss 			break;
    732   1.41  augustss 		}
    733  1.133     skrll 		usbd_close_pipe(sce->pipeh);
    734  1.133     skrll 		sce->pipeh = NULL;
    735   1.28  augustss 		if (sce->ibuf != NULL) {
    736  1.133     skrll 			kmem_free(sce->ibuf, msize);
    737   1.30  augustss 			sce->ibuf = NULL;
    738    1.9  augustss 		}
    739    1.1  augustss 	}
    740   1.13  augustss 	sc->sc_is_open[endpt] = 0;
    741  1.155  riastrad 	error = 0;
    742    1.1  augustss 
    743  1.155  riastrad out:	ugenif_release(sc);
    744  1.155  riastrad 	return error;
    745    1.1  augustss }
    746    1.1  augustss 
    747   1.37  augustss Static int
    748   1.40  augustss ugen_do_read(struct ugen_softc *sc, int endpt, struct uio *uio, int flag)
    749    1.1  augustss {
    750    1.1  augustss 	struct ugen_endpoint *sce = &sc->sc_endpoints[endpt][IN];
    751  1.133     skrll 	uint32_t n, tn;
    752  1.133     skrll 	struct usbd_xfer *xfer;
    753   1.28  augustss 	usbd_status err;
    754    1.1  augustss 	int error = 0;
    755    1.1  augustss 
    756  1.111    dyoung 	DPRINTFN(5, ("%s: ugenread: %d\n", device_xname(sc->sc_dev), endpt));
    757   1.26  augustss 
    758   1.26  augustss 	if (endpt == USB_CONTROL_ENDPOINT)
    759  1.133     skrll 		return ENODEV;
    760   1.26  augustss 
    761  1.154  riastrad 	KASSERT(sce->edesc);
    762  1.154  riastrad 	KASSERT(sce->pipeh);
    763    1.1  augustss 
    764    1.1  augustss 	switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
    765    1.1  augustss 	case UE_INTERRUPT:
    766   1.47       wiz 		/* Block until activity occurred. */
    767  1.120       mrg 		mutex_enter(&sc->sc_lock);
    768    1.1  augustss 		while (sce->q.c_cc == 0) {
    769    1.1  augustss 			if (flag & IO_NDELAY) {
    770  1.120       mrg 				mutex_exit(&sc->sc_lock);
    771  1.133     skrll 				return EWOULDBLOCK;
    772    1.1  augustss 			}
    773   1.46      yamt 			DPRINTFN(5, ("ugenread: sleep on %p\n", sce));
    774  1.120       mrg 			/* "ugenri" */
    775  1.120       mrg 			error = cv_timedwait_sig(&sce->cv, &sc->sc_lock,
    776  1.120       mrg 			    mstohz(sce->timeout));
    777    1.1  augustss 			DPRINTFN(5, ("ugenread: woke, error=%d\n", error));
    778   1.12  augustss 			if (sc->sc_dying)
    779   1.12  augustss 				error = EIO;
    780  1.152  riastrad 			if (error)
    781   1.12  augustss 				break;
    782    1.1  augustss 		}
    783  1.120       mrg 		mutex_exit(&sc->sc_lock);
    784    1.1  augustss 
    785    1.1  augustss 		/* Transfer as many chunks as possible. */
    786   1.12  augustss 		while (sce->q.c_cc > 0 && uio->uio_resid > 0 && !error) {
    787  1.140  riastrad 			n = uimin(sce->q.c_cc, uio->uio_resid);
    788   1.83  christos 			if (n > sizeof(sc->sc_buffer))
    789   1.83  christos 				n = sizeof(sc->sc_buffer);
    790    1.1  augustss 
    791    1.1  augustss 			/* Remove a small chunk from the input queue. */
    792   1.83  christos 			q_to_b(&sce->q, sc->sc_buffer, n);
    793    1.1  augustss 			DPRINTFN(5, ("ugenread: got %d chars\n", n));
    794    1.1  augustss 
    795    1.1  augustss 			/* Copy the data to the user process. */
    796   1.83  christos 			error = uiomove(sc->sc_buffer, n, uio);
    797    1.1  augustss 			if (error)
    798    1.1  augustss 				break;
    799    1.1  augustss 		}
    800    1.1  augustss 		break;
    801    1.1  augustss 	case UE_BULK:
    802   1.84       gdt 		if (sce->state & UGEN_BULK_RA) {
    803   1.90       wiz 			DPRINTFN(5, ("ugenread: BULK_RA req: %zd used: %d\n",
    804   1.84       gdt 				     uio->uio_resid, sce->ra_wb_used));
    805   1.84       gdt 			xfer = sce->ra_wb_xfer;
    806   1.84       gdt 
    807  1.120       mrg 			mutex_enter(&sc->sc_lock);
    808   1.84       gdt 			if (sce->ra_wb_used == 0 && flag & IO_NDELAY) {
    809  1.120       mrg 				mutex_exit(&sc->sc_lock);
    810  1.133     skrll 				return EWOULDBLOCK;
    811   1.84       gdt 			}
    812   1.84       gdt 			while (uio->uio_resid > 0 && !error) {
    813   1.84       gdt 				while (sce->ra_wb_used == 0) {
    814   1.84       gdt 					DPRINTFN(5,
    815   1.84       gdt 						 ("ugenread: sleep on %p\n",
    816   1.84       gdt 						  sce));
    817  1.120       mrg 					/* "ugenrb" */
    818  1.120       mrg 					error = cv_timedwait_sig(&sce->cv,
    819  1.120       mrg 					    &sc->sc_lock, mstohz(sce->timeout));
    820   1.84       gdt 					DPRINTFN(5,
    821   1.84       gdt 						 ("ugenread: woke, error=%d\n",
    822   1.84       gdt 						  error));
    823   1.84       gdt 					if (sc->sc_dying)
    824   1.84       gdt 						error = EIO;
    825  1.152  riastrad 					if (error)
    826   1.84       gdt 						break;
    827   1.84       gdt 				}
    828   1.84       gdt 
    829   1.84       gdt 				/* Copy data to the process. */
    830   1.84       gdt 				while (uio->uio_resid > 0
    831   1.84       gdt 				       && sce->ra_wb_used > 0) {
    832  1.140  riastrad 					n = uimin(uio->uio_resid,
    833   1.84       gdt 						sce->ra_wb_used);
    834  1.140  riastrad 					n = uimin(n, sce->limit - sce->cur);
    835   1.84       gdt 					error = uiomove(sce->cur, n, uio);
    836   1.84       gdt 					if (error)
    837   1.84       gdt 						break;
    838   1.84       gdt 					sce->cur += n;
    839   1.84       gdt 					sce->ra_wb_used -= n;
    840   1.84       gdt 					if (sce->cur == sce->limit)
    841   1.84       gdt 						sce->cur = sce->ibuf;
    842   1.84       gdt 				}
    843   1.84       gdt 
    844  1.122  christos 				/*
    845   1.84       gdt 				 * If the transfers stopped because the
    846   1.84       gdt 				 * buffer was full, restart them.
    847   1.84       gdt 				 */
    848   1.84       gdt 				if (sce->state & UGEN_RA_WB_STOP &&
    849   1.84       gdt 				    sce->ra_wb_used < sce->limit - sce->ibuf) {
    850   1.84       gdt 					n = (sce->limit - sce->ibuf)
    851   1.84       gdt 					    - sce->ra_wb_used;
    852  1.133     skrll 					usbd_setup_xfer(xfer, sce, NULL,
    853  1.140  riastrad 					    uimin(n, sce->ra_wb_xferlen),
    854  1.133     skrll 					    0, USBD_NO_TIMEOUT,
    855   1.84       gdt 					    ugen_bulkra_intr);
    856   1.84       gdt 					sce->state &= ~UGEN_RA_WB_STOP;
    857   1.84       gdt 					err = usbd_transfer(xfer);
    858   1.84       gdt 					if (err != USBD_IN_PROGRESS)
    859   1.84       gdt 						/*
    860   1.84       gdt 						 * The transfer has not been
    861   1.84       gdt 						 * queued.  Setting STOP
    862   1.84       gdt 						 * will make us try
    863   1.84       gdt 						 * again at the next read.
    864   1.84       gdt 						 */
    865   1.84       gdt 						sce->state |= UGEN_RA_WB_STOP;
    866   1.84       gdt 				}
    867   1.84       gdt 			}
    868  1.120       mrg 			mutex_exit(&sc->sc_lock);
    869   1.84       gdt 			break;
    870   1.84       gdt 		}
    871  1.133     skrll 		error = usbd_create_xfer(sce->pipeh, UGEN_BBSIZE,
    872  1.137     skrll 		    0, 0, &xfer);
    873  1.133     skrll 		if (error)
    874  1.133     skrll 			return error;
    875  1.140  riastrad 		while ((n = uimin(UGEN_BBSIZE, uio->uio_resid)) != 0) {
    876   1.10  augustss 			DPRINTFN(1, ("ugenread: start transfer %d bytes\n",n));
    877   1.10  augustss 			tn = n;
    878  1.133     skrll 			err = usbd_bulk_transfer(xfer, sce->pipeh,
    879  1.133     skrll 			    sce->state & UGEN_SHORT_OK ? USBD_SHORT_XFER_OK : 0,
    880  1.133     skrll 			    sce->timeout, sc->sc_buffer, &tn);
    881   1.30  augustss 			if (err) {
    882   1.28  augustss 				if (err == USBD_INTERRUPTED)
    883    1.8  augustss 					error = EINTR;
    884   1.28  augustss 				else if (err == USBD_TIMEOUT)
    885   1.17  augustss 					error = ETIMEDOUT;
    886    1.8  augustss 				else
    887    1.8  augustss 					error = EIO;
    888    1.1  augustss 				break;
    889    1.1  augustss 			}
    890   1.10  augustss 			DPRINTFN(1, ("ugenread: got %d bytes\n", tn));
    891   1.83  christos 			error = uiomove(sc->sc_buffer, tn, uio);
    892    1.9  augustss 			if (error || tn < n)
    893    1.1  augustss 				break;
    894    1.1  augustss 		}
    895  1.133     skrll 		usbd_destroy_xfer(xfer);
    896    1.1  augustss 		break;
    897   1.41  augustss 	case UE_ISOCHRONOUS:
    898  1.120       mrg 		mutex_enter(&sc->sc_lock);
    899   1.41  augustss 		while (sce->cur == sce->fill) {
    900   1.41  augustss 			if (flag & IO_NDELAY) {
    901  1.120       mrg 				mutex_exit(&sc->sc_lock);
    902  1.133     skrll 				return EWOULDBLOCK;
    903   1.41  augustss 			}
    904  1.120       mrg 			/* "ugenri" */
    905   1.46      yamt 			DPRINTFN(5, ("ugenread: sleep on %p\n", sce));
    906  1.120       mrg 			error = cv_timedwait_sig(&sce->cv, &sc->sc_lock,
    907  1.120       mrg 			    mstohz(sce->timeout));
    908   1.41  augustss 			DPRINTFN(5, ("ugenread: woke, error=%d\n", error));
    909   1.41  augustss 			if (sc->sc_dying)
    910   1.41  augustss 				error = EIO;
    911  1.152  riastrad 			if (error)
    912   1.41  augustss 				break;
    913   1.41  augustss 		}
    914   1.41  augustss 
    915   1.41  augustss 		while (sce->cur != sce->fill && uio->uio_resid > 0 && !error) {
    916   1.41  augustss 			if(sce->fill > sce->cur)
    917  1.140  riastrad 				n = uimin(sce->fill - sce->cur, uio->uio_resid);
    918   1.41  augustss 			else
    919  1.140  riastrad 				n = uimin(sce->limit - sce->cur, uio->uio_resid);
    920   1.41  augustss 
    921   1.41  augustss 			DPRINTFN(5, ("ugenread: isoc got %d chars\n", n));
    922   1.41  augustss 
    923   1.41  augustss 			/* Copy the data to the user process. */
    924   1.41  augustss 			error = uiomove(sce->cur, n, uio);
    925   1.41  augustss 			if (error)
    926   1.41  augustss 				break;
    927   1.41  augustss 			sce->cur += n;
    928  1.120       mrg 			if (sce->cur >= sce->limit)
    929   1.41  augustss 				sce->cur = sce->ibuf;
    930   1.41  augustss 		}
    931  1.120       mrg 		mutex_exit(&sc->sc_lock);
    932   1.41  augustss 		break;
    933   1.41  augustss 
    934   1.59  augustss 
    935    1.1  augustss 	default:
    936  1.133     skrll 		return ENXIO;
    937    1.1  augustss 	}
    938  1.133     skrll 	return error;
    939    1.1  augustss }
    940    1.1  augustss 
    941  1.147      maxv static int
    942   1.40  augustss ugenread(dev_t dev, struct uio *uio, int flag)
    943    1.1  augustss {
    944    1.1  augustss 	int endpt = UGENENDPOINT(dev);
    945   1.24  augustss 	struct ugen_softc *sc;
    946   1.12  augustss 	int error;
    947   1.12  augustss 
    948  1.155  riastrad 	if ((sc = ugenif_acquire(UGENUNIT(dev))) == NULL)
    949  1.111    dyoung 		return ENXIO;
    950   1.12  augustss 	error = ugen_do_read(sc, endpt, uio, flag);
    951  1.155  riastrad 	ugenif_release(sc);
    952  1.120       mrg 
    953  1.133     skrll 	return error;
    954   1.12  augustss }
    955   1.12  augustss 
    956   1.37  augustss Static int
    957   1.87   xtraeme ugen_do_write(struct ugen_softc *sc, int endpt, struct uio *uio,
    958   1.88  christos 	int flag)
    959   1.12  augustss {
    960   1.10  augustss 	struct ugen_endpoint *sce = &sc->sc_endpoints[endpt][OUT];
    961  1.133     skrll 	uint32_t n;
    962    1.1  augustss 	int error = 0;
    963  1.133     skrll 	uint32_t tn;
    964   1.84       gdt 	char *dbuf;
    965  1.133     skrll 	struct usbd_xfer *xfer;
    966   1.28  augustss 	usbd_status err;
    967    1.1  augustss 
    968  1.111    dyoung 	DPRINTFN(5, ("%s: ugenwrite: %d\n", device_xname(sc->sc_dev), endpt));
    969   1.26  augustss 
    970   1.26  augustss 	if (endpt == USB_CONTROL_ENDPOINT)
    971  1.133     skrll 		return ENODEV;
    972   1.26  augustss 
    973  1.154  riastrad 	KASSERT(sce->edesc);
    974  1.154  riastrad 	KASSERT(sce->pipeh);
    975    1.1  augustss 
    976    1.1  augustss 	switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
    977    1.1  augustss 	case UE_BULK:
    978   1.84       gdt 		if (sce->state & UGEN_BULK_WB) {
    979   1.90       wiz 			DPRINTFN(5, ("ugenwrite: BULK_WB req: %zd used: %d\n",
    980   1.84       gdt 				     uio->uio_resid, sce->ra_wb_used));
    981   1.84       gdt 			xfer = sce->ra_wb_xfer;
    982   1.84       gdt 
    983  1.120       mrg 			mutex_enter(&sc->sc_lock);
    984   1.84       gdt 			if (sce->ra_wb_used == sce->limit - sce->ibuf &&
    985   1.84       gdt 			    flag & IO_NDELAY) {
    986  1.120       mrg 				mutex_exit(&sc->sc_lock);
    987  1.133     skrll 				return EWOULDBLOCK;
    988   1.84       gdt 			}
    989   1.84       gdt 			while (uio->uio_resid > 0 && !error) {
    990  1.122  christos 				while (sce->ra_wb_used ==
    991   1.84       gdt 				       sce->limit - sce->ibuf) {
    992   1.84       gdt 					DPRINTFN(5,
    993   1.84       gdt 						 ("ugenwrite: sleep on %p\n",
    994   1.84       gdt 						  sce));
    995  1.120       mrg 					/* "ugenwb" */
    996  1.120       mrg 					error = cv_timedwait_sig(&sce->cv,
    997  1.120       mrg 					    &sc->sc_lock, mstohz(sce->timeout));
    998   1.84       gdt 					DPRINTFN(5,
    999   1.84       gdt 						 ("ugenwrite: woke, error=%d\n",
   1000   1.84       gdt 						  error));
   1001   1.84       gdt 					if (sc->sc_dying)
   1002   1.84       gdt 						error = EIO;
   1003  1.152  riastrad 					if (error)
   1004   1.84       gdt 						break;
   1005   1.84       gdt 				}
   1006   1.84       gdt 
   1007   1.84       gdt 				/* Copy data from the process. */
   1008   1.84       gdt 				while (uio->uio_resid > 0 &&
   1009   1.84       gdt 				    sce->ra_wb_used < sce->limit - sce->ibuf) {
   1010  1.140  riastrad 					n = uimin(uio->uio_resid,
   1011   1.84       gdt 						(sce->limit - sce->ibuf)
   1012   1.84       gdt 						 - sce->ra_wb_used);
   1013  1.140  riastrad 					n = uimin(n, sce->limit - sce->fill);
   1014   1.84       gdt 					error = uiomove(sce->fill, n, uio);
   1015   1.84       gdt 					if (error)
   1016   1.84       gdt 						break;
   1017   1.84       gdt 					sce->fill += n;
   1018   1.84       gdt 					sce->ra_wb_used += n;
   1019   1.84       gdt 					if (sce->fill == sce->limit)
   1020   1.84       gdt 						sce->fill = sce->ibuf;
   1021   1.84       gdt 				}
   1022   1.84       gdt 
   1023   1.84       gdt 				/*
   1024   1.84       gdt 				 * If the transfers stopped because the
   1025   1.84       gdt 				 * buffer was empty, restart them.
   1026   1.84       gdt 				 */
   1027   1.84       gdt 				if (sce->state & UGEN_RA_WB_STOP &&
   1028   1.84       gdt 				    sce->ra_wb_used > 0) {
   1029   1.84       gdt 					dbuf = (char *)usbd_get_buffer(xfer);
   1030  1.140  riastrad 					n = uimin(sce->ra_wb_used,
   1031   1.84       gdt 						sce->ra_wb_xferlen);
   1032  1.140  riastrad 					tn = uimin(n, sce->limit - sce->cur);
   1033   1.84       gdt 					memcpy(dbuf, sce->cur, tn);
   1034   1.84       gdt 					dbuf += tn;
   1035   1.84       gdt 					if (n - tn > 0)
   1036   1.84       gdt 						memcpy(dbuf, sce->ibuf,
   1037   1.84       gdt 						       n - tn);
   1038  1.133     skrll 					usbd_setup_xfer(xfer, sce, NULL, n,
   1039  1.133     skrll 					    0, USBD_NO_TIMEOUT,
   1040   1.84       gdt 					    ugen_bulkwb_intr);
   1041   1.84       gdt 					sce->state &= ~UGEN_RA_WB_STOP;
   1042   1.84       gdt 					err = usbd_transfer(xfer);
   1043   1.84       gdt 					if (err != USBD_IN_PROGRESS)
   1044   1.84       gdt 						/*
   1045   1.84       gdt 						 * The transfer has not been
   1046   1.84       gdt 						 * queued.  Setting STOP
   1047   1.84       gdt 						 * will make us try again
   1048   1.84       gdt 						 * at the next read.
   1049   1.84       gdt 						 */
   1050   1.84       gdt 						sce->state |= UGEN_RA_WB_STOP;
   1051   1.84       gdt 				}
   1052   1.84       gdt 			}
   1053  1.120       mrg 			mutex_exit(&sc->sc_lock);
   1054   1.84       gdt 			break;
   1055   1.84       gdt 		}
   1056  1.133     skrll 		error = usbd_create_xfer(sce->pipeh, UGEN_BBSIZE,
   1057  1.137     skrll 		    0, 0, &xfer);
   1058  1.133     skrll 		if (error)
   1059  1.133     skrll 			return error;
   1060  1.140  riastrad 		while ((n = uimin(UGEN_BBSIZE, uio->uio_resid)) != 0) {
   1061   1.83  christos 			error = uiomove(sc->sc_buffer, n, uio);
   1062    1.1  augustss 			if (error)
   1063    1.1  augustss 				break;
   1064    1.1  augustss 			DPRINTFN(1, ("ugenwrite: transfer %d bytes\n", n));
   1065  1.133     skrll 			err = usbd_bulk_transfer(xfer, sce->pipeh, 0, sce->timeout,
   1066  1.133     skrll 			    sc->sc_buffer, &n);
   1067   1.28  augustss 			if (err) {
   1068   1.28  augustss 				if (err == USBD_INTERRUPTED)
   1069    1.8  augustss 					error = EINTR;
   1070   1.36  augustss 				else if (err == USBD_TIMEOUT)
   1071   1.36  augustss 					error = ETIMEDOUT;
   1072    1.8  augustss 				else
   1073    1.8  augustss 					error = EIO;
   1074    1.1  augustss 				break;
   1075    1.1  augustss 			}
   1076    1.1  augustss 		}
   1077  1.133     skrll 		usbd_destroy_xfer(xfer);
   1078    1.1  augustss 		break;
   1079   1.69  augustss 	case UE_INTERRUPT:
   1080  1.133     skrll 		error = usbd_create_xfer(sce->pipeh,
   1081  1.133     skrll 		    UGETW(sce->edesc->wMaxPacketSize), 0, 0, &xfer);
   1082  1.133     skrll 		if (error)
   1083  1.133     skrll 			return error;
   1084  1.140  riastrad 		while ((n = uimin(UGETW(sce->edesc->wMaxPacketSize),
   1085   1.69  augustss 		    uio->uio_resid)) != 0) {
   1086   1.83  christos 			error = uiomove(sc->sc_buffer, n, uio);
   1087   1.69  augustss 			if (error)
   1088   1.69  augustss 				break;
   1089   1.69  augustss 			DPRINTFN(1, ("ugenwrite: transfer %d bytes\n", n));
   1090   1.69  augustss 			err = usbd_intr_transfer(xfer, sce->pipeh, 0,
   1091  1.133     skrll 			    sce->timeout, sc->sc_buffer, &n);
   1092   1.69  augustss 			if (err) {
   1093   1.69  augustss 				if (err == USBD_INTERRUPTED)
   1094   1.69  augustss 					error = EINTR;
   1095   1.69  augustss 				else if (err == USBD_TIMEOUT)
   1096   1.69  augustss 					error = ETIMEDOUT;
   1097   1.69  augustss 				else
   1098   1.69  augustss 					error = EIO;
   1099   1.69  augustss 				break;
   1100   1.69  augustss 			}
   1101   1.69  augustss 		}
   1102  1.133     skrll 		usbd_destroy_xfer(xfer);
   1103   1.69  augustss 		break;
   1104    1.1  augustss 	default:
   1105  1.133     skrll 		return ENXIO;
   1106    1.1  augustss 	}
   1107  1.133     skrll 	return error;
   1108    1.1  augustss }
   1109    1.1  augustss 
   1110  1.147      maxv static int
   1111   1.40  augustss ugenwrite(dev_t dev, struct uio *uio, int flag)
   1112   1.12  augustss {
   1113   1.12  augustss 	int endpt = UGENENDPOINT(dev);
   1114   1.24  augustss 	struct ugen_softc *sc;
   1115   1.12  augustss 	int error;
   1116   1.12  augustss 
   1117  1.155  riastrad 	if ((sc = ugenif_acquire(UGENUNIT(dev))) == NULL)
   1118  1.111    dyoung 		return ENXIO;
   1119   1.12  augustss 	error = ugen_do_write(sc, endpt, uio, flag);
   1120  1.155  riastrad 	ugenif_release(sc);
   1121  1.120       mrg 
   1122  1.133     skrll 	return error;
   1123   1.12  augustss }
   1124   1.12  augustss 
   1125  1.147      maxv static int
   1126  1.111    dyoung ugen_activate(device_t self, enum devact act)
   1127   1.12  augustss {
   1128   1.99      cube 	struct ugen_softc *sc = device_private(self);
   1129   1.18  augustss 
   1130   1.18  augustss 	switch (act) {
   1131   1.18  augustss 	case DVACT_DEACTIVATE:
   1132   1.18  augustss 		sc->sc_dying = 1;
   1133  1.106    dyoung 		return 0;
   1134  1.106    dyoung 	default:
   1135  1.106    dyoung 		return EOPNOTSUPP;
   1136   1.18  augustss 	}
   1137   1.12  augustss }
   1138   1.12  augustss 
   1139  1.147      maxv static int
   1140  1.111    dyoung ugen_detach(device_t self, int flags)
   1141   1.12  augustss {
   1142  1.111    dyoung 	struct ugen_softc *sc = device_private(self);
   1143   1.12  augustss 	struct ugen_endpoint *sce;
   1144   1.12  augustss 	int i, dir;
   1145   1.25  augustss 	int maj, mn;
   1146   1.12  augustss 
   1147   1.12  augustss 	DPRINTF(("ugen_detach: sc=%p flags=%d\n", sc, flags));
   1148   1.12  augustss 
   1149   1.12  augustss 	sc->sc_dying = 1;
   1150   1.96       smb 	pmf_device_deregister(self);
   1151  1.157  riastrad 
   1152  1.157  riastrad 	if (!sc->sc_attached)
   1153  1.157  riastrad 		goto out;
   1154  1.157  riastrad 
   1155   1.12  augustss 	/* Abort all pipes.  Causes processes waiting for transfer to wake. */
   1156   1.12  augustss 	for (i = 0; i < USB_MAX_ENDPOINTS; i++) {
   1157   1.12  augustss 		for (dir = OUT; dir <= IN; dir++) {
   1158   1.12  augustss 			sce = &sc->sc_endpoints[i][dir];
   1159  1.129     skrll 			if (sce->pipeh)
   1160   1.12  augustss 				usbd_abort_pipe(sce->pipeh);
   1161   1.12  augustss 		}
   1162   1.12  augustss 	}
   1163   1.12  augustss 
   1164  1.120       mrg 	mutex_enter(&sc->sc_lock);
   1165   1.12  augustss 	if (--sc->sc_refcnt >= 0) {
   1166   1.12  augustss 		/* Wake everyone */
   1167   1.12  augustss 		for (i = 0; i < USB_MAX_ENDPOINTS; i++)
   1168  1.120       mrg 			cv_signal(&sc->sc_endpoints[i][IN].cv);
   1169   1.12  augustss 		/* Wait for processes to go away. */
   1170  1.144     skrll 		if (cv_timedwait(&sc->sc_detach_cv, &sc->sc_lock, hz * 60))
   1171  1.144     skrll 			aprint_error_dev(self, ": didn't detach\n");
   1172   1.12  augustss 	}
   1173  1.120       mrg 	mutex_exit(&sc->sc_lock);
   1174   1.12  augustss 
   1175   1.12  augustss 	/* locate the major number */
   1176   1.60   gehenna 	maj = cdevsw_lookup_major(&ugen_cdevsw);
   1177   1.12  augustss 
   1178   1.12  augustss 	/* Nuke the vnodes for any open instances (calls close). */
   1179  1.155  riastrad 	mn = sc->sc_unit * USB_MAX_ENDPOINTS;
   1180   1.12  augustss 	vdevgone(maj, mn, mn + USB_MAX_ENDPOINTS - 1, VCHR);
   1181   1.33  augustss 
   1182  1.134   msaitoh 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
   1183  1.155  riastrad 	ugenif_put_unit(sc);
   1184   1.12  augustss 
   1185  1.157  riastrad out:	for (i = 0; i < USB_MAX_ENDPOINTS; i++) {
   1186   1.97     rmind 		for (dir = OUT; dir <= IN; dir++) {
   1187   1.97     rmind 			sce = &sc->sc_endpoints[i][dir];
   1188   1.97     rmind 			seldestroy(&sce->rsel);
   1189  1.120       mrg 			cv_destroy(&sce->cv);
   1190   1.97     rmind 		}
   1191   1.97     rmind 	}
   1192   1.97     rmind 
   1193  1.120       mrg 	cv_destroy(&sc->sc_detach_cv);
   1194  1.120       mrg 	mutex_destroy(&sc->sc_lock);
   1195  1.120       mrg 
   1196  1.133     skrll 	return 0;
   1197   1.12  augustss }
   1198    1.1  augustss 
   1199   1.37  augustss Static void
   1200  1.133     skrll ugenintr(struct usbd_xfer *xfer, void *addr, usbd_status status)
   1201    1.1  augustss {
   1202    1.1  augustss 	struct ugen_endpoint *sce = addr;
   1203  1.120       mrg 	struct ugen_softc *sc = sce->sc;
   1204  1.133     skrll 	uint32_t count;
   1205    1.1  augustss 	u_char *ibuf;
   1206    1.1  augustss 
   1207    1.1  augustss 	if (status == USBD_CANCELLED)
   1208    1.1  augustss 		return;
   1209    1.1  augustss 
   1210    1.1  augustss 	if (status != USBD_NORMAL_COMPLETION) {
   1211    1.1  augustss 		DPRINTF(("ugenintr: status=%d\n", status));
   1212   1.52  augustss 		if (status == USBD_STALLED)
   1213   1.52  augustss 		    usbd_clear_endpoint_stall_async(sce->pipeh);
   1214    1.1  augustss 		return;
   1215    1.1  augustss 	}
   1216    1.1  augustss 
   1217   1.34  augustss 	usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
   1218    1.1  augustss 	ibuf = sce->ibuf;
   1219    1.1  augustss 
   1220   1.55  augustss 	DPRINTFN(5, ("ugenintr: xfer=%p status=%d count=%d\n",
   1221   1.28  augustss 		     xfer, status, count));
   1222    1.1  augustss 	DPRINTFN(5, ("          data = %02x %02x %02x\n",
   1223    1.8  augustss 		     ibuf[0], ibuf[1], ibuf[2]));
   1224    1.1  augustss 
   1225  1.152  riastrad 	mutex_enter(&sc->sc_lock);
   1226    1.8  augustss 	(void)b_to_q(ibuf, count, &sce->q);
   1227  1.152  riastrad 	cv_signal(&sce->cv);
   1228  1.120       mrg 	mutex_exit(&sc->sc_lock);
   1229   1.97     rmind 	selnotify(&sce->rsel, 0, 0);
   1230    1.1  augustss }
   1231    1.1  augustss 
   1232   1.41  augustss Static void
   1233  1.133     skrll ugen_isoc_rintr(struct usbd_xfer *xfer, void *addr,
   1234   1.42  augustss 		usbd_status status)
   1235   1.41  augustss {
   1236   1.41  augustss 	struct isoreq *req = addr;
   1237   1.41  augustss 	struct ugen_endpoint *sce = req->sce;
   1238  1.120       mrg 	struct ugen_softc *sc = sce->sc;
   1239  1.133     skrll 	uint32_t count, n;
   1240   1.48      yamt 	int i, isize;
   1241   1.41  augustss 
   1242   1.41  augustss 	/* Return if we are aborting. */
   1243   1.41  augustss 	if (status == USBD_CANCELLED)
   1244   1.41  augustss 		return;
   1245   1.41  augustss 
   1246   1.41  augustss 	usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
   1247   1.66   mycroft 	DPRINTFN(5,("ugen_isoc_rintr: xfer %ld, count=%d\n",
   1248   1.66   mycroft 	    (long)(req - sce->isoreqs), count));
   1249   1.41  augustss 
   1250  1.152  riastrad 	mutex_enter(&sc->sc_lock);
   1251  1.152  riastrad 
   1252   1.41  augustss 	/* throw away oldest input if the buffer is full */
   1253  1.152  riastrad 	if (sce->fill < sce->cur && sce->cur <= sce->fill + count) {
   1254   1.41  augustss 		sce->cur += count;
   1255  1.152  riastrad 		if (sce->cur >= sce->limit)
   1256   1.41  augustss 			sce->cur = sce->ibuf + (sce->limit - sce->cur);
   1257   1.41  augustss 		DPRINTFN(5, ("ugen_isoc_rintr: throwing away %d bytes\n",
   1258   1.41  augustss 			     count));
   1259   1.41  augustss 	}
   1260   1.41  augustss 
   1261   1.48      yamt 	isize = UGETW(sce->edesc->wMaxPacketSize);
   1262   1.48      yamt 	for (i = 0; i < UGEN_NISORFRMS; i++) {
   1263  1.133     skrll 		uint32_t actlen = req->sizes[i];
   1264   1.74  christos 		char const *tbuf = (char const *)req->dmabuf + isize * i;
   1265   1.48      yamt 
   1266   1.48      yamt 		/* copy data to buffer */
   1267   1.48      yamt 		while (actlen > 0) {
   1268  1.140  riastrad 			n = uimin(actlen, sce->limit - sce->fill);
   1269   1.74  christos 			memcpy(sce->fill, tbuf, n);
   1270   1.48      yamt 
   1271   1.74  christos 			tbuf += n;
   1272   1.48      yamt 			actlen -= n;
   1273   1.48      yamt 			sce->fill += n;
   1274  1.152  riastrad 			if (sce->fill == sce->limit)
   1275   1.48      yamt 				sce->fill = sce->ibuf;
   1276   1.48      yamt 		}
   1277   1.48      yamt 
   1278   1.48      yamt 		/* setup size for next transfer */
   1279   1.48      yamt 		req->sizes[i] = isize;
   1280   1.41  augustss 	}
   1281   1.41  augustss 
   1282  1.133     skrll 	usbd_setup_isoc_xfer(xfer, req, req->sizes, UGEN_NISORFRMS, 0,
   1283  1.133     skrll 	    ugen_isoc_rintr);
   1284   1.41  augustss 	(void)usbd_transfer(xfer);
   1285   1.41  augustss 
   1286  1.152  riastrad 	cv_signal(&sce->cv);
   1287  1.120       mrg 	mutex_exit(&sc->sc_lock);
   1288   1.97     rmind 	selnotify(&sce->rsel, 0, 0);
   1289   1.41  augustss }
   1290   1.41  augustss 
   1291   1.84       gdt Static void
   1292  1.133     skrll ugen_bulkra_intr(struct usbd_xfer *xfer, void *addr,
   1293   1.84       gdt 		 usbd_status status)
   1294   1.84       gdt {
   1295   1.84       gdt 	struct ugen_endpoint *sce = addr;
   1296  1.120       mrg 	struct ugen_softc *sc = sce->sc;
   1297  1.133     skrll 	uint32_t count, n;
   1298   1.84       gdt 	char const *tbuf;
   1299   1.84       gdt 	usbd_status err;
   1300   1.84       gdt 
   1301   1.84       gdt 	/* Return if we are aborting. */
   1302   1.84       gdt 	if (status == USBD_CANCELLED)
   1303   1.84       gdt 		return;
   1304   1.84       gdt 
   1305   1.84       gdt 	if (status != USBD_NORMAL_COMPLETION) {
   1306   1.84       gdt 		DPRINTF(("ugen_bulkra_intr: status=%d\n", status));
   1307   1.84       gdt 		sce->state |= UGEN_RA_WB_STOP;
   1308   1.84       gdt 		if (status == USBD_STALLED)
   1309   1.84       gdt 		    usbd_clear_endpoint_stall_async(sce->pipeh);
   1310   1.84       gdt 		return;
   1311   1.84       gdt 	}
   1312   1.84       gdt 
   1313   1.84       gdt 	usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
   1314   1.84       gdt 
   1315  1.152  riastrad 	mutex_enter(&sc->sc_lock);
   1316  1.152  riastrad 
   1317   1.84       gdt 	/* Keep track of how much is in the buffer. */
   1318   1.84       gdt 	sce->ra_wb_used += count;
   1319   1.84       gdt 
   1320   1.84       gdt 	/* Copy data to buffer. */
   1321   1.84       gdt 	tbuf = (char const *)usbd_get_buffer(sce->ra_wb_xfer);
   1322  1.140  riastrad 	n = uimin(count, sce->limit - sce->fill);
   1323   1.84       gdt 	memcpy(sce->fill, tbuf, n);
   1324   1.84       gdt 	tbuf += n;
   1325   1.84       gdt 	count -= n;
   1326   1.84       gdt 	sce->fill += n;
   1327   1.84       gdt 	if (sce->fill == sce->limit)
   1328   1.84       gdt 		sce->fill = sce->ibuf;
   1329   1.84       gdt 	if (count > 0) {
   1330   1.84       gdt 		memcpy(sce->fill, tbuf, count);
   1331   1.84       gdt 		sce->fill += count;
   1332   1.84       gdt 	}
   1333   1.84       gdt 
   1334   1.84       gdt 	/* Set up the next request if necessary. */
   1335   1.84       gdt 	n = (sce->limit - sce->ibuf) - sce->ra_wb_used;
   1336   1.84       gdt 	if (n > 0) {
   1337  1.140  riastrad 		usbd_setup_xfer(xfer, sce, NULL, uimin(n, sce->ra_wb_xferlen), 0,
   1338   1.84       gdt 		    USBD_NO_TIMEOUT, ugen_bulkra_intr);
   1339   1.84       gdt 		err = usbd_transfer(xfer);
   1340   1.84       gdt 		if (err != USBD_IN_PROGRESS) {
   1341   1.84       gdt 			printf("usbd_bulkra_intr: error=%d\n", err);
   1342   1.84       gdt 			/*
   1343   1.84       gdt 			 * The transfer has not been queued.  Setting STOP
   1344   1.84       gdt 			 * will make us try again at the next read.
   1345   1.84       gdt 			 */
   1346   1.84       gdt 			sce->state |= UGEN_RA_WB_STOP;
   1347   1.84       gdt 		}
   1348   1.84       gdt 	}
   1349   1.84       gdt 	else
   1350   1.84       gdt 		sce->state |= UGEN_RA_WB_STOP;
   1351   1.84       gdt 
   1352  1.152  riastrad 	cv_signal(&sce->cv);
   1353  1.120       mrg 	mutex_exit(&sc->sc_lock);
   1354   1.97     rmind 	selnotify(&sce->rsel, 0, 0);
   1355   1.84       gdt }
   1356   1.84       gdt 
   1357   1.84       gdt Static void
   1358  1.133     skrll ugen_bulkwb_intr(struct usbd_xfer *xfer, void *addr,
   1359   1.84       gdt 		 usbd_status status)
   1360   1.84       gdt {
   1361   1.84       gdt 	struct ugen_endpoint *sce = addr;
   1362  1.120       mrg 	struct ugen_softc *sc = sce->sc;
   1363  1.133     skrll 	uint32_t count, n;
   1364   1.84       gdt 	char *tbuf;
   1365   1.84       gdt 	usbd_status err;
   1366   1.84       gdt 
   1367   1.84       gdt 	/* Return if we are aborting. */
   1368   1.84       gdt 	if (status == USBD_CANCELLED)
   1369   1.84       gdt 		return;
   1370   1.84       gdt 
   1371   1.84       gdt 	if (status != USBD_NORMAL_COMPLETION) {
   1372   1.84       gdt 		DPRINTF(("ugen_bulkwb_intr: status=%d\n", status));
   1373   1.84       gdt 		sce->state |= UGEN_RA_WB_STOP;
   1374   1.84       gdt 		if (status == USBD_STALLED)
   1375   1.84       gdt 		    usbd_clear_endpoint_stall_async(sce->pipeh);
   1376   1.84       gdt 		return;
   1377   1.84       gdt 	}
   1378   1.84       gdt 
   1379   1.84       gdt 	usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
   1380   1.84       gdt 
   1381  1.152  riastrad 	mutex_enter(&sc->sc_lock);
   1382  1.152  riastrad 
   1383   1.84       gdt 	/* Keep track of how much is in the buffer. */
   1384   1.84       gdt 	sce->ra_wb_used -= count;
   1385   1.84       gdt 
   1386   1.84       gdt 	/* Update buffer pointers. */
   1387   1.84       gdt 	sce->cur += count;
   1388   1.84       gdt 	if (sce->cur >= sce->limit)
   1389  1.122  christos 		sce->cur = sce->ibuf + (sce->cur - sce->limit);
   1390   1.84       gdt 
   1391   1.84       gdt 	/* Set up next request if necessary. */
   1392   1.84       gdt 	if (sce->ra_wb_used > 0) {
   1393   1.84       gdt 		/* copy data from buffer */
   1394   1.84       gdt 		tbuf = (char *)usbd_get_buffer(sce->ra_wb_xfer);
   1395  1.140  riastrad 		count = uimin(sce->ra_wb_used, sce->ra_wb_xferlen);
   1396  1.140  riastrad 		n = uimin(count, sce->limit - sce->cur);
   1397   1.84       gdt 		memcpy(tbuf, sce->cur, n);
   1398   1.84       gdt 		tbuf += n;
   1399   1.84       gdt 		if (count - n > 0)
   1400   1.84       gdt 			memcpy(tbuf, sce->ibuf, count - n);
   1401   1.84       gdt 
   1402  1.133     skrll 		usbd_setup_xfer(xfer, sce, NULL, count, 0, USBD_NO_TIMEOUT,
   1403  1.133     skrll 		    ugen_bulkwb_intr);
   1404   1.84       gdt 		err = usbd_transfer(xfer);
   1405   1.84       gdt 		if (err != USBD_IN_PROGRESS) {
   1406   1.84       gdt 			printf("usbd_bulkwb_intr: error=%d\n", err);
   1407   1.84       gdt 			/*
   1408   1.84       gdt 			 * The transfer has not been queued.  Setting STOP
   1409   1.84       gdt 			 * will make us try again at the next write.
   1410   1.84       gdt 			 */
   1411   1.84       gdt 			sce->state |= UGEN_RA_WB_STOP;
   1412   1.84       gdt 		}
   1413   1.84       gdt 	}
   1414   1.84       gdt 	else
   1415   1.84       gdt 		sce->state |= UGEN_RA_WB_STOP;
   1416   1.84       gdt 
   1417  1.152  riastrad 	cv_signal(&sce->cv);
   1418  1.120       mrg 	mutex_exit(&sc->sc_lock);
   1419   1.97     rmind 	selnotify(&sce->rsel, 0, 0);
   1420   1.84       gdt }
   1421   1.84       gdt 
   1422   1.37  augustss Static usbd_status
   1423   1.40  augustss ugen_set_interface(struct ugen_softc *sc, int ifaceidx, int altno)
   1424    1.1  augustss {
   1425  1.133     skrll 	struct usbd_interface *iface;
   1426    1.1  augustss 	usb_endpoint_descriptor_t *ed;
   1427   1.28  augustss 	usbd_status err;
   1428    1.1  augustss 	struct ugen_endpoint *sce;
   1429  1.133     skrll 	uint8_t niface, nendpt, endptno, endpt;
   1430   1.21  augustss 	int dir;
   1431    1.1  augustss 
   1432    1.1  augustss 	DPRINTFN(15, ("ugen_set_interface %d %d\n", ifaceidx, altno));
   1433    1.1  augustss 
   1434   1.28  augustss 	err = usbd_interface_count(sc->sc_udev, &niface);
   1435   1.28  augustss 	if (err)
   1436  1.133     skrll 		return err;
   1437    1.1  augustss 	if (ifaceidx < 0 || ifaceidx >= niface)
   1438  1.133     skrll 		return USBD_INVAL;
   1439   1.59  augustss 
   1440   1.28  augustss 	err = usbd_device2interface_handle(sc->sc_udev, ifaceidx, &iface);
   1441   1.28  augustss 	if (err)
   1442  1.133     skrll 		return err;
   1443   1.28  augustss 	err = usbd_endpoint_count(iface, &nendpt);
   1444   1.28  augustss 	if (err)
   1445  1.133     skrll 		return err;
   1446    1.1  augustss 
   1447    1.1  augustss 	/* change setting */
   1448   1.28  augustss 	err = usbd_set_interface(iface, altno);
   1449   1.28  augustss 	if (err)
   1450  1.133     skrll 		return err;
   1451   1.28  augustss 
   1452   1.28  augustss 	err = usbd_endpoint_count(iface, &nendpt);
   1453   1.28  augustss 	if (err)
   1454  1.133     skrll 		return err;
   1455  1.130     skrll 
   1456  1.130     skrll 	ugen_clear_endpoints(sc);
   1457  1.130     skrll 
   1458    1.1  augustss 	for (endptno = 0; endptno < nendpt; endptno++) {
   1459    1.1  augustss 		ed = usbd_interface2endpoint_descriptor(iface,endptno);
   1460   1.81  christos 		KASSERT(ed != NULL);
   1461    1.1  augustss 		endpt = ed->bEndpointAddress;
   1462   1.21  augustss 		dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
   1463   1.21  augustss 		sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
   1464    1.1  augustss 		sce->sc = sc;
   1465    1.1  augustss 		sce->edesc = ed;
   1466    1.1  augustss 		sce->iface = iface;
   1467    1.1  augustss 	}
   1468  1.133     skrll 	return 0;
   1469    1.1  augustss }
   1470    1.1  augustss 
   1471    1.1  augustss /* Retrieve a complete descriptor for a certain device and index. */
   1472   1.37  augustss Static usb_config_descriptor_t *
   1473   1.40  augustss ugen_get_cdesc(struct ugen_softc *sc, int index, int *lenp)
   1474    1.1  augustss {
   1475    1.1  augustss 	usb_config_descriptor_t *cdesc, *tdesc, cdescr;
   1476    1.1  augustss 	int len;
   1477   1.28  augustss 	usbd_status err;
   1478    1.1  augustss 
   1479    1.1  augustss 	if (index == USB_CURRENT_CONFIG_INDEX) {
   1480    1.1  augustss 		tdesc = usbd_get_config_descriptor(sc->sc_udev);
   1481  1.148    bouyer 		if (tdesc == NULL)
   1482  1.148    bouyer 			return NULL;
   1483    1.1  augustss 		len = UGETW(tdesc->wTotalLength);
   1484    1.1  augustss 		if (lenp)
   1485    1.1  augustss 			*lenp = len;
   1486  1.133     skrll 		cdesc = kmem_alloc(len, KM_SLEEP);
   1487    1.1  augustss 		memcpy(cdesc, tdesc, len);
   1488    1.1  augustss 		DPRINTFN(5,("ugen_get_cdesc: current, len=%d\n", len));
   1489    1.1  augustss 	} else {
   1490   1.28  augustss 		err = usbd_get_config_desc(sc->sc_udev, index, &cdescr);
   1491   1.28  augustss 		if (err)
   1492  1.133     skrll 			return 0;
   1493    1.1  augustss 		len = UGETW(cdescr.wTotalLength);
   1494    1.1  augustss 		DPRINTFN(5,("ugen_get_cdesc: index=%d, len=%d\n", index, len));
   1495    1.1  augustss 		if (lenp)
   1496    1.1  augustss 			*lenp = len;
   1497  1.133     skrll 		cdesc = kmem_alloc(len, KM_SLEEP);
   1498   1.56  augustss 		err = usbd_get_config_desc_full(sc->sc_udev, index, cdesc, len);
   1499   1.28  augustss 		if (err) {
   1500  1.133     skrll 			kmem_free(cdesc, len);
   1501  1.133     skrll 			return 0;
   1502    1.1  augustss 		}
   1503    1.1  augustss 	}
   1504  1.133     skrll 	return cdesc;
   1505    1.1  augustss }
   1506    1.1  augustss 
   1507   1.37  augustss Static int
   1508   1.40  augustss ugen_get_alt_index(struct ugen_softc *sc, int ifaceidx)
   1509    1.1  augustss {
   1510  1.133     skrll 	struct usbd_interface *iface;
   1511   1.28  augustss 	usbd_status err;
   1512    1.1  augustss 
   1513   1.28  augustss 	err = usbd_device2interface_handle(sc->sc_udev, ifaceidx, &iface);
   1514   1.28  augustss 	if (err)
   1515  1.133     skrll 		return -1;
   1516  1.133     skrll 	return usbd_get_interface_altindex(iface);
   1517    1.1  augustss }
   1518    1.1  augustss 
   1519   1.37  augustss Static int
   1520   1.40  augustss ugen_do_ioctl(struct ugen_softc *sc, int endpt, u_long cmd,
   1521   1.92  christos 	      void *addr, int flag, struct lwp *l)
   1522    1.1  augustss {
   1523    1.8  augustss 	struct ugen_endpoint *sce;
   1524   1.28  augustss 	usbd_status err;
   1525  1.133     skrll 	struct usbd_interface *iface;
   1526    1.1  augustss 	struct usb_config_desc *cd;
   1527    1.1  augustss 	usb_config_descriptor_t *cdesc;
   1528    1.1  augustss 	struct usb_interface_desc *id;
   1529    1.1  augustss 	usb_interface_descriptor_t *idesc;
   1530    1.1  augustss 	struct usb_endpoint_desc *ed;
   1531    1.1  augustss 	usb_endpoint_descriptor_t *edesc;
   1532    1.1  augustss 	struct usb_alt_interface *ai;
   1533    1.2  augustss 	struct usb_string_desc *si;
   1534  1.133     skrll 	uint8_t conf, alt;
   1535  1.133     skrll 	int cdesclen;
   1536  1.133     skrll 	int error;
   1537  1.141      manu 	int dir;
   1538    1.1  augustss 
   1539  1.159  riastrad 	KASSERT(KERNEL_LOCKED_P()); /* ugen_set_config */
   1540  1.159  riastrad 
   1541    1.1  augustss 	DPRINTFN(5, ("ugenioctl: cmd=%08lx\n", cmd));
   1542    1.1  augustss 
   1543    1.2  augustss 	switch (cmd) {
   1544    1.2  augustss 	case FIONBIO:
   1545    1.2  augustss 		/* All handled in the upper FS layer. */
   1546  1.133     skrll 		return 0;
   1547    1.8  augustss 	case USB_SET_SHORT_XFER:
   1548   1.13  augustss 		if (endpt == USB_CONTROL_ENDPOINT)
   1549  1.133     skrll 			return EINVAL;
   1550   1.51  augustss 		/* This flag only affects read */
   1551    1.8  augustss 		sce = &sc->sc_endpoints[endpt][IN];
   1552   1.51  augustss 		if (sce == NULL || sce->pipeh == NULL)
   1553  1.133     skrll 			return EINVAL;
   1554    1.8  augustss 		if (*(int *)addr)
   1555    1.8  augustss 			sce->state |= UGEN_SHORT_OK;
   1556    1.8  augustss 		else
   1557    1.8  augustss 			sce->state &= ~UGEN_SHORT_OK;
   1558  1.133     skrll 		return 0;
   1559   1.17  augustss 	case USB_SET_TIMEOUT:
   1560  1.141      manu 		for (dir = OUT; dir <= IN; dir++) {
   1561  1.141      manu 			sce = &sc->sc_endpoints[endpt][dir];
   1562  1.141      manu 			if (sce == NULL)
   1563  1.141      manu 				return EINVAL;
   1564  1.141      manu 
   1565  1.141      manu 			sce->timeout = *(int *)addr;
   1566  1.141      manu 		}
   1567  1.133     skrll 		return 0;
   1568   1.84       gdt 	case USB_SET_BULK_RA:
   1569   1.84       gdt 		if (endpt == USB_CONTROL_ENDPOINT)
   1570  1.133     skrll 			return EINVAL;
   1571   1.84       gdt 		sce = &sc->sc_endpoints[endpt][IN];
   1572   1.84       gdt 		if (sce == NULL || sce->pipeh == NULL)
   1573  1.133     skrll 			return EINVAL;
   1574   1.84       gdt 		edesc = sce->edesc;
   1575   1.84       gdt 		if ((edesc->bmAttributes & UE_XFERTYPE) != UE_BULK)
   1576  1.133     skrll 			return EINVAL;
   1577   1.84       gdt 
   1578   1.84       gdt 		if (*(int *)addr) {
   1579   1.84       gdt 			/* Only turn RA on if it's currently off. */
   1580   1.84       gdt 			if (sce->state & UGEN_BULK_RA)
   1581  1.133     skrll 				return 0;
   1582   1.84       gdt 
   1583   1.84       gdt 			if (sce->ra_wb_bufsize == 0 || sce->ra_wb_reqsize == 0)
   1584   1.84       gdt 				/* shouldn't happen */
   1585  1.133     skrll 				return EINVAL;
   1586  1.133     skrll 			error = usbd_create_xfer(sce->pipeh,
   1587  1.133     skrll 			    sce->ra_wb_reqsize, 0, 0, &sce->ra_wb_xfer);
   1588  1.133     skrll 			if (error)
   1589  1.133     skrll 				return error;
   1590   1.84       gdt 			sce->ra_wb_xferlen = sce->ra_wb_reqsize;
   1591  1.133     skrll 			sce->ibuf = kmem_alloc(sce->ra_wb_bufsize, KM_SLEEP);
   1592   1.84       gdt 			sce->fill = sce->cur = sce->ibuf;
   1593   1.84       gdt 			sce->limit = sce->ibuf + sce->ra_wb_bufsize;
   1594   1.84       gdt 			sce->ra_wb_used = 0;
   1595   1.84       gdt 			sce->state |= UGEN_BULK_RA;
   1596   1.84       gdt 			sce->state &= ~UGEN_RA_WB_STOP;
   1597   1.84       gdt 			/* Now start reading. */
   1598  1.133     skrll 			usbd_setup_xfer(sce->ra_wb_xfer, sce, NULL,
   1599  1.140  riastrad 			    uimin(sce->ra_wb_xferlen, sce->ra_wb_bufsize),
   1600  1.133     skrll 			     0, USBD_NO_TIMEOUT, ugen_bulkra_intr);
   1601   1.84       gdt 			err = usbd_transfer(sce->ra_wb_xfer);
   1602   1.84       gdt 			if (err != USBD_IN_PROGRESS) {
   1603   1.84       gdt 				sce->state &= ~UGEN_BULK_RA;
   1604  1.133     skrll 				kmem_free(sce->ibuf, sce->ra_wb_bufsize);
   1605   1.84       gdt 				sce->ibuf = NULL;
   1606  1.133     skrll 				usbd_destroy_xfer(sce->ra_wb_xfer);
   1607  1.133     skrll 				return EIO;
   1608   1.84       gdt 			}
   1609   1.84       gdt 		} else {
   1610   1.84       gdt 			/* Only turn RA off if it's currently on. */
   1611   1.84       gdt 			if (!(sce->state & UGEN_BULK_RA))
   1612  1.133     skrll 				return 0;
   1613   1.84       gdt 
   1614   1.84       gdt 			sce->state &= ~UGEN_BULK_RA;
   1615   1.84       gdt 			usbd_abort_pipe(sce->pipeh);
   1616  1.133     skrll 			usbd_destroy_xfer(sce->ra_wb_xfer);
   1617   1.84       gdt 			/*
   1618   1.84       gdt 			 * XXX Discard whatever's in the buffer, but we
   1619   1.84       gdt 			 * should keep it around and drain the buffer
   1620   1.84       gdt 			 * instead.
   1621   1.84       gdt 			 */
   1622  1.133     skrll 			kmem_free(sce->ibuf, sce->ra_wb_bufsize);
   1623   1.84       gdt 			sce->ibuf = NULL;
   1624   1.84       gdt 		}
   1625  1.133     skrll 		return 0;
   1626   1.84       gdt 	case USB_SET_BULK_WB:
   1627   1.84       gdt 		if (endpt == USB_CONTROL_ENDPOINT)
   1628  1.133     skrll 			return EINVAL;
   1629   1.84       gdt 		sce = &sc->sc_endpoints[endpt][OUT];
   1630   1.84       gdt 		if (sce == NULL || sce->pipeh == NULL)
   1631  1.133     skrll 			return EINVAL;
   1632   1.84       gdt 		edesc = sce->edesc;
   1633   1.84       gdt 		if ((edesc->bmAttributes & UE_XFERTYPE) != UE_BULK)
   1634  1.133     skrll 			return EINVAL;
   1635   1.84       gdt 
   1636   1.84       gdt 		if (*(int *)addr) {
   1637   1.84       gdt 			/* Only turn WB on if it's currently off. */
   1638   1.84       gdt 			if (sce->state & UGEN_BULK_WB)
   1639  1.133     skrll 				return 0;
   1640   1.84       gdt 
   1641   1.84       gdt 			if (sce->ra_wb_bufsize == 0 || sce->ra_wb_reqsize == 0)
   1642   1.84       gdt 				/* shouldn't happen */
   1643  1.133     skrll 				return EINVAL;
   1644  1.133     skrll 			error = usbd_create_xfer(sce->pipeh, sce->ra_wb_reqsize,
   1645  1.133     skrll 			    0, 0, &sce->ra_wb_xfer);
   1646   1.84       gdt 			sce->ra_wb_xferlen = sce->ra_wb_reqsize;
   1647  1.133     skrll 			sce->ibuf = kmem_alloc(sce->ra_wb_bufsize, KM_SLEEP);
   1648   1.84       gdt 			sce->fill = sce->cur = sce->ibuf;
   1649   1.84       gdt 			sce->limit = sce->ibuf + sce->ra_wb_bufsize;
   1650   1.84       gdt 			sce->ra_wb_used = 0;
   1651   1.84       gdt 			sce->state |= UGEN_BULK_WB | UGEN_RA_WB_STOP;
   1652   1.84       gdt 		} else {
   1653   1.84       gdt 			/* Only turn WB off if it's currently on. */
   1654   1.84       gdt 			if (!(sce->state & UGEN_BULK_WB))
   1655  1.133     skrll 				return 0;
   1656   1.84       gdt 
   1657   1.84       gdt 			sce->state &= ~UGEN_BULK_WB;
   1658   1.84       gdt 			/*
   1659   1.84       gdt 			 * XXX Discard whatever's in the buffer, but we
   1660  1.122  christos 			 * should keep it around and keep writing to
   1661   1.84       gdt 			 * drain the buffer instead.
   1662   1.84       gdt 			 */
   1663   1.84       gdt 			usbd_abort_pipe(sce->pipeh);
   1664  1.133     skrll 			usbd_destroy_xfer(sce->ra_wb_xfer);
   1665  1.133     skrll 			kmem_free(sce->ibuf, sce->ra_wb_bufsize);
   1666   1.84       gdt 			sce->ibuf = NULL;
   1667   1.84       gdt 		}
   1668  1.133     skrll 		return 0;
   1669   1.84       gdt 	case USB_SET_BULK_RA_OPT:
   1670   1.84       gdt 	case USB_SET_BULK_WB_OPT:
   1671   1.84       gdt 	{
   1672   1.84       gdt 		struct usb_bulk_ra_wb_opt *opt;
   1673   1.84       gdt 
   1674   1.84       gdt 		if (endpt == USB_CONTROL_ENDPOINT)
   1675  1.133     skrll 			return EINVAL;
   1676   1.84       gdt 		opt = (struct usb_bulk_ra_wb_opt *)addr;
   1677   1.84       gdt 		if (cmd == USB_SET_BULK_RA_OPT)
   1678   1.84       gdt 			sce = &sc->sc_endpoints[endpt][IN];
   1679   1.84       gdt 		else
   1680   1.84       gdt 			sce = &sc->sc_endpoints[endpt][OUT];
   1681   1.84       gdt 		if (sce == NULL || sce->pipeh == NULL)
   1682  1.133     skrll 			return EINVAL;
   1683   1.84       gdt 		if (opt->ra_wb_buffer_size < 1 ||
   1684   1.84       gdt 		    opt->ra_wb_buffer_size > UGEN_BULK_RA_WB_BUFMAX ||
   1685   1.84       gdt 		    opt->ra_wb_request_size < 1 ||
   1686   1.84       gdt 		    opt->ra_wb_request_size > opt->ra_wb_buffer_size)
   1687  1.133     skrll 			return EINVAL;
   1688  1.122  christos 		/*
   1689   1.84       gdt 		 * XXX These changes do not take effect until the
   1690   1.84       gdt 		 * next time RA/WB mode is enabled but they ought to
   1691   1.84       gdt 		 * take effect immediately.
   1692   1.84       gdt 		 */
   1693   1.84       gdt 		sce->ra_wb_bufsize = opt->ra_wb_buffer_size;
   1694   1.84       gdt 		sce->ra_wb_reqsize = opt->ra_wb_request_size;
   1695  1.133     skrll 		return 0;
   1696   1.84       gdt 	}
   1697    1.2  augustss 	default:
   1698    1.2  augustss 		break;
   1699    1.2  augustss 	}
   1700    1.2  augustss 
   1701    1.1  augustss 	if (endpt != USB_CONTROL_ENDPOINT)
   1702  1.133     skrll 		return EINVAL;
   1703    1.1  augustss 
   1704    1.1  augustss 	switch (cmd) {
   1705   1.30  augustss #ifdef UGEN_DEBUG
   1706    1.8  augustss 	case USB_SETDEBUG:
   1707    1.8  augustss 		ugendebug = *(int *)addr;
   1708    1.8  augustss 		break;
   1709    1.8  augustss #endif
   1710    1.1  augustss 	case USB_GET_CONFIG:
   1711   1.28  augustss 		err = usbd_get_config(sc->sc_udev, &conf);
   1712   1.28  augustss 		if (err)
   1713  1.133     skrll 			return EIO;
   1714    1.1  augustss 		*(int *)addr = conf;
   1715    1.1  augustss 		break;
   1716    1.1  augustss 	case USB_SET_CONFIG:
   1717    1.2  augustss 		if (!(flag & FWRITE))
   1718  1.133     skrll 			return EPERM;
   1719  1.138        ws 		err = ugen_set_config(sc, *(int *)addr, 1);
   1720   1.53  augustss 		switch (err) {
   1721   1.53  augustss 		case USBD_NORMAL_COMPLETION:
   1722   1.53  augustss 			break;
   1723   1.53  augustss 		case USBD_IN_USE:
   1724  1.133     skrll 			return EBUSY;
   1725   1.53  augustss 		default:
   1726  1.133     skrll 			return EIO;
   1727   1.53  augustss 		}
   1728    1.1  augustss 		break;
   1729    1.1  augustss 	case USB_GET_ALTINTERFACE:
   1730    1.1  augustss 		ai = (struct usb_alt_interface *)addr;
   1731   1.55  augustss 		err = usbd_device2interface_handle(sc->sc_udev,
   1732   1.58  christos 			  ai->uai_interface_index, &iface);
   1733   1.28  augustss 		if (err)
   1734  1.133     skrll 			return EINVAL;
   1735    1.1  augustss 		idesc = usbd_get_interface_descriptor(iface);
   1736   1.28  augustss 		if (idesc == NULL)
   1737  1.133     skrll 			return EIO;
   1738   1.58  christos 		ai->uai_alt_no = idesc->bAlternateSetting;
   1739    1.1  augustss 		break;
   1740    1.1  augustss 	case USB_SET_ALTINTERFACE:
   1741    1.2  augustss 		if (!(flag & FWRITE))
   1742  1.133     skrll 			return EPERM;
   1743    1.1  augustss 		ai = (struct usb_alt_interface *)addr;
   1744   1.55  augustss 		err = usbd_device2interface_handle(sc->sc_udev,
   1745   1.58  christos 			  ai->uai_interface_index, &iface);
   1746   1.28  augustss 		if (err)
   1747  1.133     skrll 			return EINVAL;
   1748   1.58  christos 		err = ugen_set_interface(sc, ai->uai_interface_index,
   1749   1.58  christos 		    ai->uai_alt_no);
   1750   1.28  augustss 		if (err)
   1751  1.133     skrll 			return EINVAL;
   1752    1.1  augustss 		break;
   1753    1.1  augustss 	case USB_GET_NO_ALT:
   1754    1.1  augustss 		ai = (struct usb_alt_interface *)addr;
   1755  1.133     skrll 		cdesc = ugen_get_cdesc(sc, ai->uai_config_index, &cdesclen);
   1756   1.28  augustss 		if (cdesc == NULL)
   1757  1.133     skrll 			return EINVAL;
   1758   1.58  christos 		idesc = usbd_find_idesc(cdesc, ai->uai_interface_index, 0);
   1759   1.28  augustss 		if (idesc == NULL) {
   1760  1.133     skrll 			kmem_free(cdesc, cdesclen);
   1761  1.133     skrll 			return EINVAL;
   1762   1.12  augustss 		}
   1763   1.58  christos 		ai->uai_alt_no = usbd_get_no_alts(cdesc,
   1764   1.58  christos 		    idesc->bInterfaceNumber);
   1765  1.133     skrll 		kmem_free(cdesc, cdesclen);
   1766    1.1  augustss 		break;
   1767    1.1  augustss 	case USB_GET_DEVICE_DESC:
   1768    1.1  augustss 		*(usb_device_descriptor_t *)addr =
   1769    1.1  augustss 			*usbd_get_device_descriptor(sc->sc_udev);
   1770    1.1  augustss 		break;
   1771    1.1  augustss 	case USB_GET_CONFIG_DESC:
   1772    1.1  augustss 		cd = (struct usb_config_desc *)addr;
   1773  1.133     skrll 		cdesc = ugen_get_cdesc(sc, cd->ucd_config_index, &cdesclen);
   1774   1.28  augustss 		if (cdesc == NULL)
   1775  1.133     skrll 			return EINVAL;
   1776   1.58  christos 		cd->ucd_desc = *cdesc;
   1777  1.133     skrll 		kmem_free(cdesc, cdesclen);
   1778    1.1  augustss 		break;
   1779    1.1  augustss 	case USB_GET_INTERFACE_DESC:
   1780    1.1  augustss 		id = (struct usb_interface_desc *)addr;
   1781  1.133     skrll 		cdesc = ugen_get_cdesc(sc, id->uid_config_index, &cdesclen);
   1782   1.28  augustss 		if (cdesc == NULL)
   1783  1.133     skrll 			return EINVAL;
   1784   1.58  christos 		if (id->uid_config_index == USB_CURRENT_CONFIG_INDEX &&
   1785   1.58  christos 		    id->uid_alt_index == USB_CURRENT_ALT_INDEX)
   1786   1.58  christos 			alt = ugen_get_alt_index(sc, id->uid_interface_index);
   1787    1.1  augustss 		else
   1788   1.58  christos 			alt = id->uid_alt_index;
   1789   1.58  christos 		idesc = usbd_find_idesc(cdesc, id->uid_interface_index, alt);
   1790   1.28  augustss 		if (idesc == NULL) {
   1791  1.133     skrll 			kmem_free(cdesc, cdesclen);
   1792  1.133     skrll 			return EINVAL;
   1793    1.1  augustss 		}
   1794   1.58  christos 		id->uid_desc = *idesc;
   1795  1.133     skrll 		kmem_free(cdesc, cdesclen);
   1796    1.1  augustss 		break;
   1797    1.1  augustss 	case USB_GET_ENDPOINT_DESC:
   1798    1.1  augustss 		ed = (struct usb_endpoint_desc *)addr;
   1799  1.133     skrll 		cdesc = ugen_get_cdesc(sc, ed->ued_config_index, &cdesclen);
   1800   1.28  augustss 		if (cdesc == NULL)
   1801  1.133     skrll 			return EINVAL;
   1802   1.58  christos 		if (ed->ued_config_index == USB_CURRENT_CONFIG_INDEX &&
   1803   1.58  christos 		    ed->ued_alt_index == USB_CURRENT_ALT_INDEX)
   1804   1.58  christos 			alt = ugen_get_alt_index(sc, ed->ued_interface_index);
   1805    1.1  augustss 		else
   1806   1.58  christos 			alt = ed->ued_alt_index;
   1807   1.58  christos 		edesc = usbd_find_edesc(cdesc, ed->ued_interface_index,
   1808   1.58  christos 					alt, ed->ued_endpoint_index);
   1809   1.28  augustss 		if (edesc == NULL) {
   1810  1.133     skrll 			kmem_free(cdesc, cdesclen);
   1811  1.133     skrll 			return EINVAL;
   1812    1.1  augustss 		}
   1813   1.58  christos 		ed->ued_desc = *edesc;
   1814  1.133     skrll 		kmem_free(cdesc, cdesclen);
   1815    1.1  augustss 		break;
   1816    1.1  augustss 	case USB_GET_FULL_DESC:
   1817    1.1  augustss 	{
   1818    1.1  augustss 		int len;
   1819    1.1  augustss 		struct iovec iov;
   1820    1.1  augustss 		struct uio uio;
   1821    1.1  augustss 		struct usb_full_desc *fd = (struct usb_full_desc *)addr;
   1822    1.1  augustss 
   1823  1.133     skrll 		cdesc = ugen_get_cdesc(sc, fd->ufd_config_index, &cdesclen);
   1824  1.104     pooka 		if (cdesc == NULL)
   1825  1.133     skrll 			return EINVAL;
   1826  1.133     skrll 		len = cdesclen;
   1827   1.58  christos 		if (len > fd->ufd_size)
   1828   1.58  christos 			len = fd->ufd_size;
   1829   1.92  christos 		iov.iov_base = (void *)fd->ufd_data;
   1830    1.1  augustss 		iov.iov_len = len;
   1831    1.1  augustss 		uio.uio_iov = &iov;
   1832    1.1  augustss 		uio.uio_iovcnt = 1;
   1833    1.1  augustss 		uio.uio_resid = len;
   1834    1.1  augustss 		uio.uio_offset = 0;
   1835    1.1  augustss 		uio.uio_rw = UIO_READ;
   1836   1.79      yamt 		uio.uio_vmspace = l->l_proc->p_vmspace;
   1837   1.30  augustss 		error = uiomove((void *)cdesc, len, &uio);
   1838  1.133     skrll 		kmem_free(cdesc, cdesclen);
   1839  1.133     skrll 		return error;
   1840    1.1  augustss 	}
   1841   1.68   mycroft 	case USB_GET_STRING_DESC: {
   1842   1.68   mycroft 		int len;
   1843    1.2  augustss 		si = (struct usb_string_desc *)addr;
   1844   1.58  christos 		err = usbd_get_string_desc(sc->sc_udev, si->usd_string_index,
   1845   1.68   mycroft 			  si->usd_language_id, &si->usd_desc, &len);
   1846   1.28  augustss 		if (err)
   1847  1.133     skrll 			return EINVAL;
   1848    1.2  augustss 		break;
   1849   1.68   mycroft 	}
   1850    1.1  augustss 	case USB_DO_REQUEST:
   1851    1.1  augustss 	{
   1852    1.1  augustss 		struct usb_ctl_request *ur = (void *)addr;
   1853   1.58  christos 		int len = UGETW(ur->ucr_request.wLength);
   1854    1.1  augustss 		struct iovec iov;
   1855    1.1  augustss 		struct uio uio;
   1856    1.1  augustss 		void *ptr = 0;
   1857   1.74  christos 		usbd_status xerr;
   1858  1.133     skrll 
   1859  1.133     skrll 		error = 0;
   1860    1.1  augustss 
   1861    1.2  augustss 		if (!(flag & FWRITE))
   1862  1.133     skrll 			return EPERM;
   1863    1.1  augustss 		/* Avoid requests that would damage the bus integrity. */
   1864   1.58  christos 		if ((ur->ucr_request.bmRequestType == UT_WRITE_DEVICE &&
   1865   1.58  christos 		     ur->ucr_request.bRequest == UR_SET_ADDRESS) ||
   1866   1.58  christos 		    (ur->ucr_request.bmRequestType == UT_WRITE_DEVICE &&
   1867   1.58  christos 		     ur->ucr_request.bRequest == UR_SET_CONFIG) ||
   1868   1.58  christos 		    (ur->ucr_request.bmRequestType == UT_WRITE_INTERFACE &&
   1869   1.58  christos 		     ur->ucr_request.bRequest == UR_SET_INTERFACE))
   1870  1.133     skrll 			return EINVAL;
   1871    1.1  augustss 
   1872    1.1  augustss 		if (len < 0 || len > 32767)
   1873  1.133     skrll 			return EINVAL;
   1874    1.1  augustss 		if (len != 0) {
   1875   1.92  christos 			iov.iov_base = (void *)ur->ucr_data;
   1876    1.1  augustss 			iov.iov_len = len;
   1877    1.1  augustss 			uio.uio_iov = &iov;
   1878    1.1  augustss 			uio.uio_iovcnt = 1;
   1879    1.1  augustss 			uio.uio_resid = len;
   1880    1.1  augustss 			uio.uio_offset = 0;
   1881    1.1  augustss 			uio.uio_rw =
   1882   1.58  christos 				ur->ucr_request.bmRequestType & UT_READ ?
   1883    1.1  augustss 				UIO_READ : UIO_WRITE;
   1884   1.79      yamt 			uio.uio_vmspace = l->l_proc->p_vmspace;
   1885  1.133     skrll 			ptr = kmem_alloc(len, KM_SLEEP);
   1886    1.1  augustss 			if (uio.uio_rw == UIO_WRITE) {
   1887    1.1  augustss 				error = uiomove(ptr, len, &uio);
   1888    1.1  augustss 				if (error)
   1889    1.1  augustss 					goto ret;
   1890    1.1  augustss 			}
   1891    1.1  augustss 		}
   1892   1.57  augustss 		sce = &sc->sc_endpoints[endpt][IN];
   1893   1.74  christos 		xerr = usbd_do_request_flags(sc->sc_udev, &ur->ucr_request,
   1894   1.58  christos 			  ptr, ur->ucr_flags, &ur->ucr_actlen, sce->timeout);
   1895   1.74  christos 		if (xerr) {
   1896    1.1  augustss 			error = EIO;
   1897    1.1  augustss 			goto ret;
   1898    1.1  augustss 		}
   1899    1.1  augustss 		if (len != 0) {
   1900    1.1  augustss 			if (uio.uio_rw == UIO_READ) {
   1901  1.140  riastrad 				size_t alen = uimin(len, ur->ucr_actlen);
   1902  1.133     skrll 				error = uiomove(ptr, alen, &uio);
   1903    1.1  augustss 				if (error)
   1904    1.1  augustss 					goto ret;
   1905    1.1  augustss 			}
   1906    1.1  augustss 		}
   1907    1.1  augustss 	ret:
   1908    1.1  augustss 		if (ptr)
   1909  1.133     skrll 			kmem_free(ptr, len);
   1910  1.133     skrll 		return error;
   1911    1.1  augustss 	}
   1912    1.2  augustss 	case USB_GET_DEVICEINFO:
   1913    1.2  augustss 		usbd_fill_deviceinfo(sc->sc_udev,
   1914   1.91  drochner 				     (struct usb_device_info *)addr, 0);
   1915    1.2  augustss 		break;
   1916   1.89     pavel 	case USB_GET_DEVICEINFO_OLD:
   1917  1.142  pgoyette 	{
   1918  1.142  pgoyette 		int ret;
   1919  1.145  pgoyette 		MODULE_HOOK_CALL(usb_subr_fill_30_hook,
   1920  1.142  pgoyette 		    (sc->sc_udev, (struct usb_device_info_old *)addr, 0,
   1921  1.142  pgoyette 		      usbd_devinfo_vp, usbd_printBCD),
   1922  1.142  pgoyette 		    enosys(), ret);
   1923  1.142  pgoyette 		if (ret == 0)
   1924  1.142  pgoyette 			return 0;
   1925  1.142  pgoyette 		return EINVAL;
   1926  1.142  pgoyette 	}
   1927    1.1  augustss 	default:
   1928  1.133     skrll 		return EINVAL;
   1929    1.1  augustss 	}
   1930  1.133     skrll 	return 0;
   1931    1.1  augustss }
   1932    1.1  augustss 
   1933  1.147      maxv static int
   1934   1.92  christos ugenioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
   1935   1.12  augustss {
   1936   1.12  augustss 	int endpt = UGENENDPOINT(dev);
   1937   1.24  augustss 	struct ugen_softc *sc;
   1938   1.12  augustss 	int error;
   1939   1.12  augustss 
   1940  1.155  riastrad 	if ((sc = ugenif_acquire(UGENUNIT(dev))) == 0)
   1941  1.111    dyoung 		return ENXIO;
   1942  1.155  riastrad 	error = ugen_do_ioctl(sc, endpt, cmd, addr, flag, l);
   1943  1.155  riastrad 	ugenif_release(sc);
   1944   1.30  augustss 
   1945  1.133     skrll 	return error;
   1946   1.12  augustss }
   1947   1.12  augustss 
   1948  1.147      maxv static int
   1949   1.78  christos ugenpoll(dev_t dev, int events, struct lwp *l)
   1950    1.1  augustss {
   1951   1.24  augustss 	struct ugen_softc *sc;
   1952   1.84       gdt 	struct ugen_endpoint *sce_in, *sce_out;
   1953    1.1  augustss 	int revents = 0;
   1954    1.1  augustss 
   1955  1.155  riastrad 	if ((sc = ugenif_acquire(UGENUNIT(dev))) == NULL)
   1956  1.133     skrll 		return POLLHUP;
   1957    1.1  augustss 
   1958  1.155  riastrad 	if (UGENENDPOINT(dev) == USB_CONTROL_ENDPOINT) {
   1959  1.155  riastrad 		revents |= POLLERR;
   1960  1.155  riastrad 		goto out;
   1961  1.155  riastrad 	}
   1962  1.113  jakllsch 
   1963   1.84       gdt 	sce_in = &sc->sc_endpoints[UGENENDPOINT(dev)][IN];
   1964   1.84       gdt 	sce_out = &sc->sc_endpoints[UGENENDPOINT(dev)][OUT];
   1965  1.154  riastrad 	KASSERT(sce_in->edesc || sce_out->edesc);
   1966  1.154  riastrad 	KASSERT(sce_in->pipeh || sce_out->pipeh);
   1967  1.120       mrg 
   1968  1.120       mrg 	mutex_enter(&sc->sc_lock);
   1969   1.84       gdt 	if (sce_in && sce_in->pipeh && (events & (POLLIN | POLLRDNORM)))
   1970   1.84       gdt 		switch (sce_in->edesc->bmAttributes & UE_XFERTYPE) {
   1971   1.84       gdt 		case UE_INTERRUPT:
   1972   1.84       gdt 			if (sce_in->q.c_cc > 0)
   1973   1.41  augustss 				revents |= events & (POLLIN | POLLRDNORM);
   1974   1.41  augustss 			else
   1975   1.84       gdt 				selrecord(l, &sce_in->rsel);
   1976   1.84       gdt 			break;
   1977   1.84       gdt 		case UE_ISOCHRONOUS:
   1978   1.84       gdt 			if (sce_in->cur != sce_in->fill)
   1979    1.1  augustss 				revents |= events & (POLLIN | POLLRDNORM);
   1980    1.1  augustss 			else
   1981   1.84       gdt 				selrecord(l, &sce_in->rsel);
   1982   1.84       gdt 			break;
   1983   1.84       gdt 		case UE_BULK:
   1984   1.84       gdt 			if (sce_in->state & UGEN_BULK_RA) {
   1985   1.84       gdt 				if (sce_in->ra_wb_used > 0)
   1986   1.84       gdt 					revents |= events &
   1987   1.84       gdt 					    (POLLIN | POLLRDNORM);
   1988   1.84       gdt 				else
   1989   1.84       gdt 					selrecord(l, &sce_in->rsel);
   1990   1.84       gdt 				break;
   1991   1.84       gdt 			}
   1992   1.84       gdt 			/*
   1993   1.84       gdt 			 * We have no easy way of determining if a read will
   1994   1.84       gdt 			 * yield any data or a write will happen.
   1995   1.84       gdt 			 * Pretend they will.
   1996   1.84       gdt 			 */
   1997  1.155  riastrad 			revents |= events & (POLLIN | POLLRDNORM);
   1998  1.155  riastrad 			break;
   1999   1.84       gdt 		default:
   2000   1.84       gdt 			break;
   2001    1.1  augustss 		}
   2002   1.84       gdt 	if (sce_out && sce_out->pipeh && (events & (POLLOUT | POLLWRNORM)))
   2003   1.84       gdt 		switch (sce_out->edesc->bmAttributes & UE_XFERTYPE) {
   2004   1.84       gdt 		case UE_INTERRUPT:
   2005   1.84       gdt 		case UE_ISOCHRONOUS:
   2006   1.84       gdt 			/* XXX unimplemented */
   2007   1.84       gdt 			break;
   2008   1.84       gdt 		case UE_BULK:
   2009   1.84       gdt 			if (sce_out->state & UGEN_BULK_WB) {
   2010   1.84       gdt 				if (sce_out->ra_wb_used <
   2011   1.84       gdt 				    sce_out->limit - sce_out->ibuf)
   2012   1.84       gdt 					revents |= events &
   2013   1.84       gdt 					    (POLLOUT | POLLWRNORM);
   2014   1.84       gdt 				else
   2015   1.84       gdt 					selrecord(l, &sce_out->rsel);
   2016   1.84       gdt 				break;
   2017   1.84       gdt 			}
   2018   1.84       gdt 			/*
   2019   1.84       gdt 			 * We have no easy way of determining if a read will
   2020   1.84       gdt 			 * yield any data or a write will happen.
   2021   1.84       gdt 			 * Pretend they will.
   2022   1.84       gdt 			 */
   2023   1.84       gdt 			 revents |= events & (POLLOUT | POLLWRNORM);
   2024   1.84       gdt 			 break;
   2025   1.84       gdt 		default:
   2026   1.84       gdt 			break;
   2027   1.84       gdt 		}
   2028   1.84       gdt 
   2029  1.120       mrg 	mutex_exit(&sc->sc_lock);
   2030   1.84       gdt 
   2031  1.155  riastrad out:	ugenif_release(sc);
   2032  1.133     skrll 	return revents;
   2033   1.62  jdolecek }
   2034   1.62  jdolecek 
   2035   1.62  jdolecek static void
   2036   1.62  jdolecek filt_ugenrdetach(struct knote *kn)
   2037   1.62  jdolecek {
   2038   1.62  jdolecek 	struct ugen_endpoint *sce = kn->kn_hook;
   2039  1.120       mrg 	struct ugen_softc *sc = sce->sc;
   2040   1.62  jdolecek 
   2041  1.120       mrg 	mutex_enter(&sc->sc_lock);
   2042  1.158   thorpej 	selremove_knote(&sce->rsel, kn);
   2043  1.120       mrg 	mutex_exit(&sc->sc_lock);
   2044   1.62  jdolecek }
   2045   1.62  jdolecek 
   2046   1.62  jdolecek static int
   2047   1.88  christos filt_ugenread_intr(struct knote *kn, long hint)
   2048   1.62  jdolecek {
   2049   1.62  jdolecek 	struct ugen_endpoint *sce = kn->kn_hook;
   2050  1.135       mrg 	struct ugen_softc *sc = sce->sc;
   2051  1.153  riastrad 	int ret;
   2052  1.135       mrg 
   2053  1.153  riastrad 	mutex_enter(&sc->sc_lock);
   2054  1.153  riastrad 	if (sc->sc_dying) {
   2055  1.153  riastrad 		ret = 0;
   2056  1.153  riastrad 	} else {
   2057  1.153  riastrad 		kn->kn_data = sce->q.c_cc;
   2058  1.153  riastrad 		ret = kn->kn_data > 0;
   2059  1.153  riastrad 	}
   2060  1.153  riastrad 	mutex_exit(&sc->sc_lock);
   2061   1.62  jdolecek 
   2062  1.153  riastrad 	return ret;
   2063   1.62  jdolecek }
   2064   1.62  jdolecek 
   2065   1.62  jdolecek static int
   2066   1.88  christos filt_ugenread_isoc(struct knote *kn, long hint)
   2067   1.62  jdolecek {
   2068   1.62  jdolecek 	struct ugen_endpoint *sce = kn->kn_hook;
   2069  1.135       mrg 	struct ugen_softc *sc = sce->sc;
   2070  1.153  riastrad 	int ret;
   2071  1.135       mrg 
   2072  1.153  riastrad 	mutex_enter(&sc->sc_lock);
   2073  1.153  riastrad 	if (sc->sc_dying) {
   2074  1.153  riastrad 		ret = 0;
   2075  1.153  riastrad 	} else if (sce->cur == sce->fill) {
   2076  1.153  riastrad 		ret = 0;
   2077  1.153  riastrad 	} else if (sce->cur < sce->fill) {
   2078   1.62  jdolecek 		kn->kn_data = sce->fill - sce->cur;
   2079  1.153  riastrad 		ret = 1;
   2080  1.153  riastrad 	} else {
   2081   1.62  jdolecek 		kn->kn_data = (sce->limit - sce->cur) +
   2082   1.62  jdolecek 		    (sce->fill - sce->ibuf);
   2083  1.153  riastrad 		ret = 1;
   2084  1.153  riastrad 	}
   2085  1.153  riastrad 	mutex_exit(&sc->sc_lock);
   2086   1.62  jdolecek 
   2087  1.153  riastrad 	return ret;
   2088   1.62  jdolecek }
   2089   1.62  jdolecek 
   2090   1.84       gdt static int
   2091   1.84       gdt filt_ugenread_bulk(struct knote *kn, long hint)
   2092   1.84       gdt {
   2093   1.84       gdt 	struct ugen_endpoint *sce = kn->kn_hook;
   2094  1.135       mrg 	struct ugen_softc *sc = sce->sc;
   2095  1.153  riastrad 	int ret;
   2096  1.135       mrg 
   2097  1.153  riastrad 	mutex_enter(&sc->sc_lock);
   2098  1.153  riastrad 	if (sc->sc_dying) {
   2099  1.153  riastrad 		ret = 0;
   2100  1.153  riastrad 	} else if (!(sce->state & UGEN_BULK_RA)) {
   2101   1.84       gdt 		/*
   2102   1.84       gdt 		 * We have no easy way of determining if a read will
   2103   1.84       gdt 		 * yield any data or a write will happen.
   2104   1.84       gdt 		 * So, emulate "seltrue".
   2105   1.84       gdt 		 */
   2106  1.153  riastrad 		ret = filt_seltrue(kn, hint);
   2107  1.153  riastrad 	} else if (sce->ra_wb_used == 0) {
   2108  1.153  riastrad 		ret = 0;
   2109  1.153  riastrad 	} else {
   2110  1.153  riastrad 		kn->kn_data = sce->ra_wb_used;
   2111  1.153  riastrad 		ret = 1;
   2112  1.153  riastrad 	}
   2113  1.153  riastrad 	mutex_exit(&sc->sc_lock);
   2114   1.84       gdt 
   2115  1.153  riastrad 	return ret;
   2116   1.84       gdt }
   2117   1.84       gdt 
   2118   1.84       gdt static int
   2119   1.84       gdt filt_ugenwrite_bulk(struct knote *kn, long hint)
   2120   1.84       gdt {
   2121   1.84       gdt 	struct ugen_endpoint *sce = kn->kn_hook;
   2122  1.135       mrg 	struct ugen_softc *sc = sce->sc;
   2123  1.153  riastrad 	int ret;
   2124  1.135       mrg 
   2125  1.153  riastrad 	mutex_enter(&sc->sc_lock);
   2126  1.153  riastrad 	if (sc->sc_dying) {
   2127  1.153  riastrad 		ret = 0;
   2128  1.153  riastrad 	} else if (!(sce->state & UGEN_BULK_WB)) {
   2129   1.84       gdt 		/*
   2130   1.84       gdt 		 * We have no easy way of determining if a read will
   2131   1.84       gdt 		 * yield any data or a write will happen.
   2132   1.84       gdt 		 * So, emulate "seltrue".
   2133   1.84       gdt 		 */
   2134  1.153  riastrad 		ret = filt_seltrue(kn, hint);
   2135  1.153  riastrad 	} else if (sce->ra_wb_used == sce->limit - sce->ibuf) {
   2136  1.153  riastrad 		ret = 0;
   2137  1.153  riastrad 	} else {
   2138  1.153  riastrad 		kn->kn_data = (sce->limit - sce->ibuf) - sce->ra_wb_used;
   2139  1.153  riastrad 		ret = 1;
   2140  1.153  riastrad 	}
   2141  1.153  riastrad 	mutex_exit(&sc->sc_lock);
   2142   1.84       gdt 
   2143  1.153  riastrad 	return ret;
   2144   1.84       gdt }
   2145   1.84       gdt 
   2146  1.136      maya static const struct filterops ugenread_intr_filtops = {
   2147  1.136      maya 	.f_isfd = 1,
   2148  1.136      maya 	.f_attach = NULL,
   2149  1.136      maya 	.f_detach = filt_ugenrdetach,
   2150  1.136      maya 	.f_event = filt_ugenread_intr,
   2151  1.136      maya };
   2152   1.62  jdolecek 
   2153  1.136      maya static const struct filterops ugenread_isoc_filtops = {
   2154  1.136      maya 	.f_isfd = 1,
   2155  1.136      maya 	.f_attach = NULL,
   2156  1.136      maya 	.f_detach = filt_ugenrdetach,
   2157  1.136      maya 	.f_event = filt_ugenread_isoc,
   2158  1.136      maya };
   2159   1.62  jdolecek 
   2160  1.136      maya static const struct filterops ugenread_bulk_filtops = {
   2161  1.136      maya 	.f_isfd = 1,
   2162  1.136      maya 	.f_attach = NULL,
   2163  1.136      maya 	.f_detach = filt_ugenrdetach,
   2164  1.136      maya 	.f_event = filt_ugenread_bulk,
   2165  1.136      maya };
   2166   1.84       gdt 
   2167  1.136      maya static const struct filterops ugenwrite_bulk_filtops = {
   2168  1.136      maya 	.f_isfd = 1,
   2169  1.136      maya 	.f_attach = NULL,
   2170  1.136      maya 	.f_detach = filt_ugenrdetach,
   2171  1.136      maya 	.f_event = filt_ugenwrite_bulk,
   2172  1.136      maya };
   2173   1.62  jdolecek 
   2174  1.147      maxv static int
   2175   1.62  jdolecek ugenkqfilter(dev_t dev, struct knote *kn)
   2176   1.62  jdolecek {
   2177   1.62  jdolecek 	struct ugen_softc *sc;
   2178   1.62  jdolecek 	struct ugen_endpoint *sce;
   2179  1.158   thorpej 	struct selinfo *sip;
   2180  1.155  riastrad 	int error;
   2181   1.62  jdolecek 
   2182  1.155  riastrad 	if ((sc = ugenif_acquire(UGENUNIT(dev))) == NULL)
   2183  1.133     skrll 		return ENXIO;
   2184   1.62  jdolecek 
   2185  1.155  riastrad 	if (UGENENDPOINT(dev) == USB_CONTROL_ENDPOINT) {
   2186  1.155  riastrad 		error = ENODEV;
   2187  1.155  riastrad 		goto out;
   2188  1.155  riastrad 	}
   2189  1.113  jakllsch 
   2190   1.62  jdolecek 	switch (kn->kn_filter) {
   2191   1.62  jdolecek 	case EVFILT_READ:
   2192   1.84       gdt 		sce = &sc->sc_endpoints[UGENENDPOINT(dev)][IN];
   2193  1.155  riastrad 		if (sce == NULL) {
   2194  1.155  riastrad 			error = EINVAL;
   2195  1.155  riastrad 			goto out;
   2196  1.155  riastrad 		}
   2197   1.84       gdt 
   2198  1.158   thorpej 		sip = &sce->rsel;
   2199   1.62  jdolecek 		switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
   2200   1.62  jdolecek 		case UE_INTERRUPT:
   2201   1.62  jdolecek 			kn->kn_fop = &ugenread_intr_filtops;
   2202   1.62  jdolecek 			break;
   2203   1.62  jdolecek 		case UE_ISOCHRONOUS:
   2204   1.62  jdolecek 			kn->kn_fop = &ugenread_isoc_filtops;
   2205   1.62  jdolecek 			break;
   2206   1.62  jdolecek 		case UE_BULK:
   2207   1.84       gdt 			kn->kn_fop = &ugenread_bulk_filtops;
   2208   1.84       gdt 			break;
   2209   1.62  jdolecek 		default:
   2210  1.155  riastrad 			error = EINVAL;
   2211  1.155  riastrad 			goto out;
   2212   1.62  jdolecek 		}
   2213   1.62  jdolecek 		break;
   2214   1.62  jdolecek 
   2215   1.62  jdolecek 	case EVFILT_WRITE:
   2216   1.84       gdt 		sce = &sc->sc_endpoints[UGENENDPOINT(dev)][OUT];
   2217  1.155  riastrad 		if (sce == NULL) {
   2218  1.155  riastrad 			error = EINVAL;
   2219  1.155  riastrad 			goto out;
   2220  1.155  riastrad 		}
   2221   1.84       gdt 
   2222  1.158   thorpej 		sip = &sce->rsel;
   2223   1.62  jdolecek 		switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
   2224   1.62  jdolecek 		case UE_INTERRUPT:
   2225   1.62  jdolecek 		case UE_ISOCHRONOUS:
   2226   1.62  jdolecek 			/* XXX poll doesn't support this */
   2227  1.155  riastrad 			error = EINVAL;
   2228  1.155  riastrad 			goto out;
   2229   1.62  jdolecek 
   2230   1.62  jdolecek 		case UE_BULK:
   2231   1.84       gdt 			kn->kn_fop = &ugenwrite_bulk_filtops;
   2232   1.62  jdolecek 			break;
   2233   1.62  jdolecek 		default:
   2234  1.155  riastrad 			error = EINVAL;
   2235  1.155  riastrad 			goto out;
   2236   1.62  jdolecek 		}
   2237   1.62  jdolecek 		break;
   2238   1.62  jdolecek 
   2239   1.62  jdolecek 	default:
   2240  1.155  riastrad 		error = EINVAL;
   2241  1.155  riastrad 		goto out;
   2242   1.62  jdolecek 	}
   2243   1.62  jdolecek 
   2244   1.62  jdolecek 	kn->kn_hook = sce;
   2245   1.62  jdolecek 
   2246  1.120       mrg 	mutex_enter(&sc->sc_lock);
   2247  1.158   thorpej 	selrecord_knote(sip, kn);
   2248  1.120       mrg 	mutex_exit(&sc->sc_lock);
   2249   1.62  jdolecek 
   2250  1.155  riastrad 	error = 0;
   2251  1.155  riastrad 
   2252  1.155  riastrad out:	ugenif_release(sc);
   2253  1.155  riastrad 	return error;
   2254  1.155  riastrad }
   2255  1.155  riastrad 
   2256  1.155  riastrad MODULE(MODULE_CLASS_DRIVER, ugen, NULL);
   2257  1.155  riastrad 
   2258  1.155  riastrad static int
   2259  1.155  riastrad ugen_modcmd(modcmd_t cmd, void *aux)
   2260  1.155  riastrad {
   2261  1.155  riastrad 
   2262  1.155  riastrad 	switch (cmd) {
   2263  1.155  riastrad 	case MODULE_CMD_INIT:
   2264  1.155  riastrad 		mutex_init(&ugenif.lock, MUTEX_DEFAULT, IPL_NONE);
   2265  1.155  riastrad 		rb_tree_init(&ugenif.tree, &ugenif_tree_ops);
   2266  1.155  riastrad 		return 0;
   2267  1.155  riastrad 	default:
   2268  1.155  riastrad 		return ENOTTY;
   2269  1.155  riastrad 	}
   2270    1.1  augustss }
   2271