Home | History | Annotate | Line # | Download | only in usb
ugen.c revision 1.45.2.10
      1   1.45.2.6   nathanw /*	$NetBSD: ugen.c,v 1.45.2.10 2002/12/11 06:38:51 thorpej Exp $	*/
      2       1.30  augustss /*	$FreeBSD: src/sys/dev/usb/ugen.c,v 1.26 1999/11/17 22:33:41 n_hibma Exp $	*/
      3        1.1  augustss 
      4        1.1  augustss /*
      5        1.1  augustss  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      6        1.1  augustss  * All rights reserved.
      7        1.1  augustss  *
      8        1.1  augustss  * This code is derived from software contributed to The NetBSD Foundation
      9       1.38  augustss  * by Lennart Augustsson (lennart (at) augustsson.net) at
     10        1.1  augustss  * Carlstedt Research & Technology.
     11        1.1  augustss  *
     12        1.1  augustss  * Redistribution and use in source and binary forms, with or without
     13        1.1  augustss  * modification, are permitted provided that the following conditions
     14        1.1  augustss  * are met:
     15        1.1  augustss  * 1. Redistributions of source code must retain the above copyright
     16        1.1  augustss  *    notice, this list of conditions and the following disclaimer.
     17        1.1  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     18        1.1  augustss  *    notice, this list of conditions and the following disclaimer in the
     19        1.1  augustss  *    documentation and/or other materials provided with the distribution.
     20        1.1  augustss  * 3. All advertising materials mentioning features or use of this software
     21        1.1  augustss  *    must display the following acknowledgement:
     22   1.45.2.4   nathanw  *	This product includes software developed by the NetBSD
     23   1.45.2.4   nathanw  *	Foundation, Inc. and its contributors.
     24        1.1  augustss  * 4. Neither the name of The NetBSD Foundation nor the names of its
     25        1.1  augustss  *    contributors may be used to endorse or promote products derived
     26        1.1  augustss  *    from this software without specific prior written permission.
     27        1.1  augustss  *
     28        1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     29        1.1  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     30        1.1  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     31        1.1  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     32        1.1  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     33        1.1  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     34        1.1  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     35        1.1  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     36        1.1  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     37        1.1  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     38        1.1  augustss  * POSSIBILITY OF SUCH DAMAGE.
     39        1.1  augustss  */
     40        1.1  augustss 
     41        1.1  augustss 
     42   1.45.2.2   nathanw #include <sys/cdefs.h>
     43   1.45.2.6   nathanw __KERNEL_RCSID(0, "$NetBSD: ugen.c,v 1.45.2.10 2002/12/11 06:38:51 thorpej Exp $");
     44   1.45.2.2   nathanw 
     45        1.1  augustss #include <sys/param.h>
     46        1.1  augustss #include <sys/systm.h>
     47        1.1  augustss #include <sys/kernel.h>
     48        1.1  augustss #include <sys/malloc.h>
     49       1.15  augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
     50        1.1  augustss #include <sys/device.h>
     51        1.1  augustss #include <sys/ioctl.h>
     52       1.11  augustss #elif defined(__FreeBSD__)
     53       1.11  augustss #include <sys/module.h>
     54       1.11  augustss #include <sys/bus.h>
     55       1.11  augustss #include <sys/ioccom.h>
     56       1.11  augustss #include <sys/conf.h>
     57       1.11  augustss #include <sys/fcntl.h>
     58       1.11  augustss #include <sys/filio.h>
     59       1.11  augustss #endif
     60       1.12  augustss #include <sys/conf.h>
     61        1.1  augustss #include <sys/tty.h>
     62        1.1  augustss #include <sys/file.h>
     63        1.1  augustss #include <sys/select.h>
     64        1.1  augustss #include <sys/proc.h>
     65        1.1  augustss #include <sys/vnode.h>
     66        1.1  augustss #include <sys/poll.h>
     67        1.1  augustss 
     68        1.1  augustss #include <dev/usb/usb.h>
     69        1.1  augustss #include <dev/usb/usbdi.h>
     70        1.1  augustss #include <dev/usb/usbdi_util.h>
     71        1.1  augustss 
     72       1.25  augustss #ifdef UGEN_DEBUG
     73       1.14  augustss #define DPRINTF(x)	if (ugendebug) logprintf x
     74       1.14  augustss #define DPRINTFN(n,x)	if (ugendebug>(n)) logprintf x
     75        1.2  augustss int	ugendebug = 0;
     76        1.1  augustss #else
     77        1.1  augustss #define DPRINTF(x)
     78        1.1  augustss #define DPRINTFN(n,x)
     79        1.1  augustss #endif
     80        1.1  augustss 
     81       1.41  augustss #define	UGEN_CHUNK	128	/* chunk size for read */
     82       1.41  augustss #define	UGEN_IBSIZE	1020	/* buffer size */
     83       1.41  augustss #define	UGEN_BBSIZE	1024
     84       1.41  augustss 
     85       1.41  augustss #define	UGEN_NISOFRAMES	500	/* 0.5 seconds worth */
     86       1.41  augustss #define UGEN_NISOREQS	6	/* number of outstanding xfer requests */
     87       1.41  augustss #define UGEN_NISORFRMS	4	/* number of frames (miliseconds) per req */
     88       1.41  augustss 
     89        1.1  augustss struct ugen_endpoint {
     90        1.1  augustss 	struct ugen_softc *sc;
     91        1.1  augustss 	usb_endpoint_descriptor_t *edesc;
     92        1.1  augustss 	usbd_interface_handle iface;
     93        1.1  augustss 	int state;
     94        1.1  augustss #define	UGEN_ASLP	0x02	/* waiting for data */
     95        1.8  augustss #define UGEN_SHORT_OK	0x04	/* short xfers are OK */
     96        1.1  augustss 	usbd_pipe_handle pipeh;
     97        1.1  augustss 	struct clist q;
     98        1.1  augustss 	struct selinfo rsel;
     99       1.41  augustss 	u_char *ibuf;		/* start of buffer (circular for isoc) */
    100       1.41  augustss 	u_char *fill;		/* location for input (isoc) */
    101       1.41  augustss 	u_char *limit;		/* end of circular buffer (isoc) */
    102       1.41  augustss 	u_char *cur;		/* current read location (isoc) */
    103       1.17  augustss 	u_int32_t timeout;
    104       1.41  augustss 	struct isoreq {
    105       1.41  augustss 		struct ugen_endpoint *sce;
    106       1.41  augustss 		usbd_xfer_handle xfer;
    107       1.41  augustss 		void *dmabuf;
    108       1.41  augustss 		u_int16_t sizes[UGEN_NISORFRMS];
    109       1.41  augustss 	} isoreqs[UGEN_NISOREQS];
    110        1.1  augustss };
    111        1.1  augustss 
    112        1.1  augustss struct ugen_softc {
    113       1.22  augustss 	USBBASEDEVICE sc_dev;		/* base device */
    114       1.19  augustss 	usbd_device_handle sc_udev;
    115        1.1  augustss 
    116       1.13  augustss 	char sc_is_open[USB_MAX_ENDPOINTS];
    117        1.1  augustss 	struct ugen_endpoint sc_endpoints[USB_MAX_ENDPOINTS][2];
    118       1.21  augustss #define OUT 0
    119       1.21  augustss #define IN  1
    120        1.1  augustss 
    121       1.12  augustss 	int sc_refcnt;
    122       1.12  augustss 	u_char sc_dying;
    123        1.1  augustss };
    124        1.1  augustss 
    125   1.45.2.7   nathanw #if defined(__NetBSD__)
    126   1.45.2.7   nathanw dev_type_open(ugenopen);
    127   1.45.2.7   nathanw dev_type_close(ugenclose);
    128   1.45.2.7   nathanw dev_type_read(ugenread);
    129   1.45.2.7   nathanw dev_type_write(ugenwrite);
    130   1.45.2.7   nathanw dev_type_ioctl(ugenioctl);
    131   1.45.2.7   nathanw dev_type_poll(ugenpoll);
    132   1.45.2.9   nathanw dev_type_kqfilter(ugenkqfilter);
    133   1.45.2.7   nathanw 
    134   1.45.2.7   nathanw const struct cdevsw ugen_cdevsw = {
    135   1.45.2.7   nathanw 	ugenopen, ugenclose, ugenread, ugenwrite, ugenioctl,
    136   1.45.2.9   nathanw 	nostop, notty, ugenpoll, nommap, ugenkqfilter,
    137   1.45.2.7   nathanw };
    138   1.45.2.7   nathanw #elif defined(__OpenBSD__)
    139       1.14  augustss cdev_decl(ugen);
    140       1.14  augustss #elif defined(__FreeBSD__)
    141       1.14  augustss d_open_t  ugenopen;
    142       1.14  augustss d_close_t ugenclose;
    143       1.14  augustss d_read_t  ugenread;
    144       1.14  augustss d_write_t ugenwrite;
    145       1.14  augustss d_ioctl_t ugenioctl;
    146       1.14  augustss d_poll_t  ugenpoll;
    147       1.14  augustss 
    148       1.14  augustss #define UGEN_CDEV_MAJOR	114
    149       1.14  augustss 
    150       1.37  augustss Static struct cdevsw ugen_cdevsw = {
    151       1.14  augustss 	/* open */	ugenopen,
    152       1.14  augustss 	/* close */	ugenclose,
    153       1.14  augustss 	/* read */	ugenread,
    154       1.14  augustss 	/* write */	ugenwrite,
    155       1.14  augustss 	/* ioctl */	ugenioctl,
    156       1.14  augustss 	/* poll */	ugenpoll,
    157       1.14  augustss 	/* mmap */	nommap,
    158       1.14  augustss 	/* strategy */	nostrategy,
    159       1.14  augustss 	/* name */	"ugen",
    160       1.14  augustss 	/* maj */	UGEN_CDEV_MAJOR,
    161       1.14  augustss 	/* dump */	nodump,
    162       1.14  augustss 	/* psize */	nopsize,
    163       1.14  augustss 	/* flags */	0,
    164       1.14  augustss 	/* bmaj */	-1
    165       1.14  augustss };
    166       1.14  augustss #endif
    167       1.14  augustss 
    168   1.45.2.3   nathanw Static void ugenintr(usbd_xfer_handle xfer, usbd_private_handle addr,
    169       1.40  augustss 		     usbd_status status);
    170       1.42  augustss Static void ugen_isoc_rintr(usbd_xfer_handle xfer, usbd_private_handle addr,
    171       1.42  augustss 			    usbd_status status);
    172       1.40  augustss Static int ugen_do_read(struct ugen_softc *, int, struct uio *, int);
    173       1.40  augustss Static int ugen_do_write(struct ugen_softc *, int, struct uio *, int);
    174   1.45.2.3   nathanw Static int ugen_do_ioctl(struct ugen_softc *, int, u_long,
    175   1.45.2.3   nathanw 			 caddr_t, int, usb_proc_ptr);
    176       1.40  augustss Static int ugen_set_config(struct ugen_softc *sc, int configno);
    177   1.45.2.3   nathanw Static usb_config_descriptor_t *ugen_get_cdesc(struct ugen_softc *sc,
    178       1.40  augustss 					       int index, int *lenp);
    179       1.40  augustss Static usbd_status ugen_set_interface(struct ugen_softc *, int, int);
    180       1.40  augustss Static int ugen_get_alt_index(struct ugen_softc *sc, int ifaceidx);
    181        1.1  augustss 
    182       1.12  augustss #define UGENUNIT(n) ((minor(n) >> 4) & 0xf)
    183       1.12  augustss #define UGENENDPOINT(n) (minor(n) & 0xf)
    184       1.12  augustss #define UGENDEV(u, e) (makedev(0, ((u) << 4) | (e)))
    185        1.1  augustss 
    186        1.5  augustss USB_DECLARE_DRIVER(ugen);
    187        1.1  augustss 
    188        1.5  augustss USB_MATCH(ugen)
    189        1.1  augustss {
    190        1.5  augustss 	USB_MATCH_START(ugen, uaa);
    191        1.1  augustss 
    192   1.45.2.2   nathanw #if 0
    193   1.45.2.2   nathanw 	if (uaa->matchlvl)
    194   1.45.2.2   nathanw 		return (uaa->matchlvl);
    195   1.45.2.2   nathanw #endif
    196        1.1  augustss 	if (uaa->usegeneric)
    197        1.1  augustss 		return (UMATCH_GENERIC);
    198        1.1  augustss 	else
    199        1.1  augustss 		return (UMATCH_NONE);
    200        1.1  augustss }
    201        1.1  augustss 
    202        1.5  augustss USB_ATTACH(ugen)
    203        1.1  augustss {
    204        1.5  augustss 	USB_ATTACH_START(ugen, sc, uaa);
    205       1.31  augustss 	usbd_device_handle udev;
    206        1.1  augustss 	char devinfo[1024];
    207       1.28  augustss 	usbd_status err;
    208        1.7  augustss 	int conf;
    209   1.45.2.6   nathanw 
    210        1.1  augustss 	usbd_devinfo(uaa->device, 0, devinfo);
    211        1.5  augustss 	USB_ATTACH_SETUP;
    212        1.5  augustss 	printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
    213        1.1  augustss 
    214       1.31  augustss 	sc->sc_udev = udev = uaa->device;
    215       1.31  augustss 
    216       1.31  augustss 	/* First set configuration index 0, the default one for ugen. */
    217       1.31  augustss 	err = usbd_set_config_index(udev, 0, 0);
    218       1.31  augustss 	if (err) {
    219   1.45.2.3   nathanw 		printf("%s: setting configuration index 0 failed\n",
    220       1.31  augustss 		       USBDEVNAME(sc->sc_dev));
    221       1.31  augustss 		sc->sc_dying = 1;
    222       1.31  augustss 		USB_ATTACH_ERROR_RETURN;
    223       1.31  augustss 	}
    224       1.31  augustss 	conf = usbd_get_config_descriptor(udev)->bConfigurationValue;
    225       1.31  augustss 
    226       1.31  augustss 	/* Set up all the local state for this configuration. */
    227       1.28  augustss 	err = ugen_set_config(sc, conf);
    228       1.28  augustss 	if (err) {
    229   1.45.2.3   nathanw 		printf("%s: setting configuration %d failed\n",
    230        1.7  augustss 		       USBDEVNAME(sc->sc_dev), conf);
    231       1.12  augustss 		sc->sc_dying = 1;
    232        1.5  augustss 		USB_ATTACH_ERROR_RETURN;
    233        1.1  augustss 	}
    234       1.30  augustss 
    235       1.30  augustss #ifdef __FreeBSD__
    236       1.30  augustss 	{
    237       1.30  augustss 		static int global_init_done = 0;
    238       1.30  augustss 		if (!global_init_done) {
    239       1.30  augustss 			cdevsw_add(&ugen_cdevsw);
    240       1.30  augustss 			global_init_done = 1;
    241       1.30  augustss 		}
    242       1.30  augustss 	}
    243       1.30  augustss #endif
    244       1.30  augustss 
    245       1.33  augustss 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    246       1.33  augustss 			   USBDEV(sc->sc_dev));
    247       1.33  augustss 
    248        1.5  augustss 	USB_ATTACH_SUCCESS_RETURN;
    249        1.1  augustss }
    250        1.1  augustss 
    251       1.37  augustss Static int
    252       1.40  augustss ugen_set_config(struct ugen_softc *sc, int configno)
    253        1.1  augustss {
    254        1.1  augustss 	usbd_device_handle dev = sc->sc_udev;
    255        1.1  augustss 	usbd_interface_handle iface;
    256        1.1  augustss 	usb_endpoint_descriptor_t *ed;
    257        1.1  augustss 	struct ugen_endpoint *sce;
    258        1.1  augustss 	u_int8_t niface, nendpt;
    259        1.1  augustss 	int ifaceno, endptno, endpt;
    260       1.28  augustss 	usbd_status err;
    261       1.21  augustss 	int dir;
    262        1.1  augustss 
    263        1.1  augustss 	DPRINTFN(1,("ugen_set_config: %s to configno %d, sc=%p\n",
    264        1.5  augustss 		    USBDEVNAME(sc->sc_dev), configno, sc));
    265   1.45.2.3   nathanw 
    266   1.45.2.3   nathanw 	/*
    267   1.45.2.3   nathanw 	 * We start at 1, not 0, because we don't care whether the
    268   1.45.2.3   nathanw 	 * control endpoint is open or not. It is always present.
    269   1.45.2.3   nathanw 	 */
    270   1.45.2.3   nathanw 	for (endptno = 1; endptno < USB_MAX_ENDPOINTS; endptno++)
    271   1.45.2.3   nathanw 		if (sc->sc_is_open[endptno]) {
    272   1.45.2.3   nathanw 			DPRINTFN(1,
    273   1.45.2.3   nathanw 			     ("ugen_set_config: %s - endpoint %d is open\n",
    274   1.45.2.3   nathanw 			      USBDEVNAME(sc->sc_dev), endptno));
    275   1.45.2.3   nathanw 			return (USBD_IN_USE);
    276   1.45.2.3   nathanw 		}
    277   1.45.2.3   nathanw 
    278       1.31  augustss 	/* Avoid setting the current value. */
    279        1.7  augustss 	if (usbd_get_config_descriptor(dev)->bConfigurationValue != configno) {
    280       1.43  augustss 		err = usbd_set_config_no(dev, configno, 1);
    281       1.28  augustss 		if (err)
    282       1.28  augustss 			return (err);
    283        1.7  augustss 	}
    284        1.1  augustss 
    285       1.28  augustss 	err = usbd_interface_count(dev, &niface);
    286       1.28  augustss 	if (err)
    287       1.28  augustss 		return (err);
    288        1.1  augustss 	memset(sc->sc_endpoints, 0, sizeof sc->sc_endpoints);
    289        1.1  augustss 	for (ifaceno = 0; ifaceno < niface; ifaceno++) {
    290        1.1  augustss 		DPRINTFN(1,("ugen_set_config: ifaceno %d\n", ifaceno));
    291       1.28  augustss 		err = usbd_device2interface_handle(dev, ifaceno, &iface);
    292       1.28  augustss 		if (err)
    293       1.28  augustss 			return (err);
    294       1.28  augustss 		err = usbd_endpoint_count(iface, &nendpt);
    295       1.28  augustss 		if (err)
    296       1.28  augustss 			return (err);
    297        1.1  augustss 		for (endptno = 0; endptno < nendpt; endptno++) {
    298        1.1  augustss 			ed = usbd_interface2endpoint_descriptor(iface,endptno);
    299        1.1  augustss 			endpt = ed->bEndpointAddress;
    300       1.21  augustss 			dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
    301       1.21  augustss 			sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
    302        1.3  augustss 			DPRINTFN(1,("ugen_set_config: endptno %d, endpt=0x%02x"
    303   1.45.2.3   nathanw 				    "(%d,%d), sce=%p\n",
    304        1.1  augustss 				    endptno, endpt, UE_GET_ADDR(endpt),
    305       1.21  augustss 				    UE_GET_DIR(endpt), sce));
    306        1.1  augustss 			sce->sc = sc;
    307        1.1  augustss 			sce->edesc = ed;
    308        1.1  augustss 			sce->iface = iface;
    309        1.1  augustss 		}
    310        1.1  augustss 	}
    311        1.1  augustss 	return (USBD_NORMAL_COMPLETION);
    312        1.1  augustss }
    313        1.1  augustss 
    314        1.1  augustss int
    315   1.45.2.3   nathanw ugenopen(dev_t dev, int flag, int mode, usb_proc_ptr p)
    316        1.1  augustss {
    317       1.24  augustss 	struct ugen_softc *sc;
    318        1.1  augustss 	int unit = UGENUNIT(dev);
    319        1.1  augustss 	int endpt = UGENENDPOINT(dev);
    320        1.1  augustss 	usb_endpoint_descriptor_t *edesc;
    321        1.1  augustss 	struct ugen_endpoint *sce;
    322        1.1  augustss 	int dir, isize;
    323       1.28  augustss 	usbd_status err;
    324       1.41  augustss 	usbd_xfer_handle xfer;
    325       1.41  augustss 	void *buf;
    326       1.41  augustss 	int i, j;
    327        1.1  augustss 
    328       1.11  augustss 	USB_GET_SC_OPEN(ugen, unit, sc);
    329       1.30  augustss 
    330   1.45.2.3   nathanw 	DPRINTFN(5, ("ugenopen: flag=%d, mode=%d, unit=%d endpt=%d\n",
    331        1.9  augustss 		     flag, mode, unit, endpt));
    332        1.1  augustss 
    333       1.30  augustss 	if (sc == NULL || sc->sc_dying)
    334       1.12  augustss 		return (ENXIO);
    335        1.1  augustss 
    336       1.13  augustss 	if (sc->sc_is_open[endpt])
    337       1.13  augustss 		return (EBUSY);
    338       1.13  augustss 
    339        1.1  augustss 	if (endpt == USB_CONTROL_ENDPOINT) {
    340       1.13  augustss 		sc->sc_is_open[USB_CONTROL_ENDPOINT] = 1;
    341       1.13  augustss 		return (0);
    342       1.13  augustss 	}
    343       1.35  augustss 
    344       1.13  augustss 	/* Make sure there are pipes for all directions. */
    345       1.13  augustss 	for (dir = OUT; dir <= IN; dir++) {
    346       1.13  augustss 		if (flag & (dir == OUT ? FWRITE : FREAD)) {
    347       1.13  augustss 			sce = &sc->sc_endpoints[endpt][dir];
    348       1.13  augustss 			if (sce == 0 || sce->edesc == 0)
    349       1.13  augustss 				return (ENXIO);
    350        1.1  augustss 		}
    351       1.13  augustss 	}
    352       1.13  augustss 
    353       1.13  augustss 	/* Actually open the pipes. */
    354       1.13  augustss 	/* XXX Should back out properly if it fails. */
    355       1.13  augustss 	for (dir = OUT; dir <= IN; dir++) {
    356       1.13  augustss 		if (!(flag & (dir == OUT ? FWRITE : FREAD)))
    357       1.13  augustss 			continue;
    358        1.1  augustss 		sce = &sc->sc_endpoints[endpt][dir];
    359       1.13  augustss 		sce->state = 0;
    360       1.17  augustss 		sce->timeout = USBD_NO_TIMEOUT;
    361   1.45.2.3   nathanw 		DPRINTFN(5, ("ugenopen: sc=%p, endpt=%d, dir=%d, sce=%p\n",
    362        1.3  augustss 			     sc, endpt, dir, sce));
    363        1.1  augustss 		edesc = sce->edesc;
    364        1.1  augustss 		switch (edesc->bmAttributes & UE_XFERTYPE) {
    365        1.1  augustss 		case UE_INTERRUPT:
    366        1.1  augustss 			isize = UGETW(edesc->wMaxPacketSize);
    367        1.1  augustss 			if (isize == 0)	/* shouldn't happen */
    368        1.1  augustss 				return (EINVAL);
    369       1.12  augustss 			sce->ibuf = malloc(isize, M_USBDEV, M_WAITOK);
    370   1.45.2.3   nathanw 			DPRINTFN(5, ("ugenopen: intr endpt=%d,isize=%d\n",
    371        1.1  augustss 				     endpt, isize));
    372   1.45.2.4   nathanw 			if (clalloc(&sce->q, UGEN_IBSIZE, 0) == -1)
    373   1.45.2.4   nathanw 				return (ENOMEM);
    374   1.45.2.3   nathanw 			err = usbd_open_pipe_intr(sce->iface,
    375   1.45.2.3   nathanw 				  edesc->bEndpointAddress,
    376   1.45.2.3   nathanw 				  USBD_SHORT_XFER_OK, &sce->pipeh, sce,
    377   1.45.2.3   nathanw 				  sce->ibuf, isize, ugenintr,
    378       1.32  augustss 				  USBD_DEFAULT_INTERVAL);
    379       1.28  augustss 			if (err) {
    380       1.12  augustss 				free(sce->ibuf, M_USBDEV);
    381        1.1  augustss 				clfree(&sce->q);
    382        1.1  augustss 				return (EIO);
    383        1.1  augustss 			}
    384        1.1  augustss 			DPRINTFN(5, ("ugenopen: interrupt open done\n"));
    385        1.1  augustss 			break;
    386        1.1  augustss 		case UE_BULK:
    387   1.45.2.3   nathanw 			err = usbd_open_pipe(sce->iface,
    388       1.28  augustss 				  edesc->bEndpointAddress, 0, &sce->pipeh);
    389       1.28  augustss 			if (err)
    390        1.1  augustss 				return (EIO);
    391        1.1  augustss 			break;
    392       1.41  augustss 		case UE_ISOCHRONOUS:
    393       1.41  augustss 			if (dir == OUT)
    394       1.41  augustss 				return (EINVAL);
    395       1.41  augustss 			isize = UGETW(edesc->wMaxPacketSize);
    396       1.41  augustss 			if (isize == 0)	/* shouldn't happen */
    397       1.41  augustss 				return (EINVAL);
    398       1.41  augustss 			sce->ibuf = malloc(isize * UGEN_NISOFRAMES,
    399       1.41  augustss 				M_USBDEV, M_WAITOK);
    400       1.41  augustss 			sce->cur = sce->fill = sce->ibuf;
    401       1.41  augustss 			sce->limit = sce->ibuf + isize * UGEN_NISOFRAMES;
    402   1.45.2.3   nathanw 			DPRINTFN(5, ("ugenopen: isoc endpt=%d, isize=%d\n",
    403       1.41  augustss 				     endpt, isize));
    404       1.41  augustss 			err = usbd_open_pipe(sce->iface,
    405       1.41  augustss 				  edesc->bEndpointAddress, 0, &sce->pipeh);
    406       1.41  augustss 			if (err) {
    407       1.41  augustss 				free(sce->ibuf, M_USBDEV);
    408       1.41  augustss 				return (EIO);
    409       1.41  augustss 			}
    410       1.41  augustss 			for(i = 0; i < UGEN_NISOREQS; ++i) {
    411       1.41  augustss 				sce->isoreqs[i].sce = sce;
    412       1.41  augustss 				xfer = usbd_alloc_xfer(sc->sc_udev);
    413       1.41  augustss 				if (xfer == 0)
    414       1.41  augustss 					goto bad;
    415       1.41  augustss 				sce->isoreqs[i].xfer = xfer;
    416       1.41  augustss 				buf = usbd_alloc_buffer
    417       1.41  augustss 					(xfer, isize * UGEN_NISORFRMS);
    418       1.41  augustss 				if (buf == 0) {
    419       1.41  augustss 					i++;
    420       1.41  augustss 					goto bad;
    421       1.41  augustss 				}
    422       1.41  augustss 				sce->isoreqs[i].dmabuf = buf;
    423       1.41  augustss 				for(j = 0; j < UGEN_NISORFRMS; ++j)
    424       1.41  augustss 					sce->isoreqs[i].sizes[j] = isize;
    425       1.41  augustss 				usbd_setup_isoc_xfer
    426       1.41  augustss 					(xfer, sce->pipeh, &sce->isoreqs[i],
    427       1.41  augustss 					 sce->isoreqs[i].sizes,
    428       1.41  augustss 					 UGEN_NISORFRMS, USBD_NO_COPY,
    429       1.41  augustss 					 ugen_isoc_rintr);
    430       1.41  augustss 				(void)usbd_transfer(xfer);
    431       1.41  augustss 			}
    432       1.41  augustss 			DPRINTFN(5, ("ugenopen: isoc open done\n"));
    433       1.41  augustss 			break;
    434       1.41  augustss 		bad:
    435       1.41  augustss 			while (--i >= 0) /* implicit buffer free */
    436       1.41  augustss 				usbd_free_xfer(sce->isoreqs[i].xfer);
    437       1.41  augustss 			return (ENOMEM);
    438        1.1  augustss 		case UE_CONTROL:
    439   1.45.2.5   nathanw 			sce->timeout = USBD_DEFAULT_TIMEOUT;
    440        1.1  augustss 			return (EINVAL);
    441        1.1  augustss 		}
    442        1.1  augustss 	}
    443       1.13  augustss 	sc->sc_is_open[endpt] = 1;
    444        1.1  augustss 	return (0);
    445        1.1  augustss }
    446        1.1  augustss 
    447        1.1  augustss int
    448   1.45.2.3   nathanw ugenclose(dev_t dev, int flag, int mode, usb_proc_ptr p)
    449        1.1  augustss {
    450        1.1  augustss 	int endpt = UGENENDPOINT(dev);
    451       1.24  augustss 	struct ugen_softc *sc;
    452        1.1  augustss 	struct ugen_endpoint *sce;
    453        1.1  augustss 	int dir;
    454       1.41  augustss 	int i;
    455        1.1  augustss 
    456       1.24  augustss 	USB_GET_SC(ugen, UGENUNIT(dev), sc);
    457       1.30  augustss 
    458       1.13  augustss 	DPRINTFN(5, ("ugenclose: flag=%d, mode=%d, unit=%d, endpt=%d\n",
    459       1.13  augustss 		     flag, mode, UGENUNIT(dev), endpt));
    460       1.13  augustss 
    461       1.13  augustss #ifdef DIAGNOSTIC
    462       1.13  augustss 	if (!sc->sc_is_open[endpt]) {
    463       1.13  augustss 		printf("ugenclose: not open\n");
    464       1.13  augustss 		return (EINVAL);
    465       1.13  augustss 	}
    466       1.13  augustss #endif
    467        1.1  augustss 
    468        1.1  augustss 	if (endpt == USB_CONTROL_ENDPOINT) {
    469        1.9  augustss 		DPRINTFN(5, ("ugenclose: close control\n"));
    470       1.13  augustss 		sc->sc_is_open[endpt] = 0;
    471        1.1  augustss 		return (0);
    472        1.1  augustss 	}
    473        1.9  augustss 
    474        1.9  augustss 	for (dir = OUT; dir <= IN; dir++) {
    475       1.13  augustss 		if (!(flag & (dir == OUT ? FWRITE : FREAD)))
    476       1.13  augustss 			continue;
    477       1.13  augustss 		sce = &sc->sc_endpoints[endpt][dir];
    478       1.30  augustss 		if (sce == NULL || sce->pipeh == NULL)
    479       1.13  augustss 			continue;
    480   1.45.2.3   nathanw 		DPRINTFN(5, ("ugenclose: endpt=%d dir=%d sce=%p\n",
    481       1.13  augustss 			     endpt, dir, sce));
    482   1.45.2.6   nathanw 
    483       1.13  augustss 		usbd_abort_pipe(sce->pipeh);
    484       1.13  augustss 		usbd_close_pipe(sce->pipeh);
    485       1.28  augustss 		sce->pipeh = NULL;
    486       1.41  augustss 
    487   1.45.2.4   nathanw 		switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
    488   1.45.2.4   nathanw 		case UE_INTERRUPT:
    489   1.45.2.4   nathanw 			ndflush(&sce->q, sce->q.c_cc);
    490   1.45.2.4   nathanw 			clfree(&sce->q);
    491   1.45.2.4   nathanw 			break;
    492   1.45.2.4   nathanw 		case UE_ISOCHRONOUS:
    493   1.45.2.4   nathanw 			for (i = 0; i < UGEN_NISOREQS; ++i)
    494   1.45.2.4   nathanw 				usbd_free_xfer(sce->isoreqs[i].xfer);
    495       1.41  augustss 
    496   1.45.2.4   nathanw 		default:
    497   1.45.2.4   nathanw 			break;
    498       1.41  augustss 		}
    499       1.41  augustss 
    500       1.28  augustss 		if (sce->ibuf != NULL) {
    501       1.13  augustss 			free(sce->ibuf, M_USBDEV);
    502       1.30  augustss 			sce->ibuf = NULL;
    503       1.13  augustss 			clfree(&sce->q);
    504        1.9  augustss 		}
    505        1.1  augustss 	}
    506       1.13  augustss 	sc->sc_is_open[endpt] = 0;
    507        1.1  augustss 
    508        1.1  augustss 	return (0);
    509        1.1  augustss }
    510        1.1  augustss 
    511       1.37  augustss Static int
    512       1.40  augustss ugen_do_read(struct ugen_softc *sc, int endpt, struct uio *uio, int flag)
    513        1.1  augustss {
    514        1.1  augustss 	struct ugen_endpoint *sce = &sc->sc_endpoints[endpt][IN];
    515        1.9  augustss 	u_int32_t n, tn;
    516        1.1  augustss 	char buf[UGEN_BBSIZE];
    517       1.28  augustss 	usbd_xfer_handle xfer;
    518       1.28  augustss 	usbd_status err;
    519        1.1  augustss 	int s;
    520        1.1  augustss 	int error = 0;
    521        1.1  augustss 	u_char buffer[UGEN_CHUNK];
    522        1.1  augustss 
    523       1.44  augustss 	DPRINTFN(5, ("%s: ugenread: %d\n", USBDEVNAME(sc->sc_dev), endpt));
    524       1.26  augustss 
    525       1.12  augustss 	if (sc->sc_dying)
    526        1.1  augustss 		return (EIO);
    527        1.1  augustss 
    528       1.26  augustss 	if (endpt == USB_CONTROL_ENDPOINT)
    529       1.26  augustss 		return (ENODEV);
    530       1.26  augustss 
    531        1.1  augustss #ifdef DIAGNOSTIC
    532       1.28  augustss 	if (sce->edesc == NULL) {
    533        1.1  augustss 		printf("ugenread: no edesc\n");
    534        1.1  augustss 		return (EIO);
    535        1.1  augustss 	}
    536       1.28  augustss 	if (sce->pipeh == NULL) {
    537        1.9  augustss 		printf("ugenread: no pipe\n");
    538        1.9  augustss 		return (EIO);
    539        1.9  augustss 	}
    540        1.1  augustss #endif
    541        1.1  augustss 
    542        1.1  augustss 	switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
    543        1.1  augustss 	case UE_INTERRUPT:
    544   1.45.2.1   nathanw 		/* Block until activity occurred. */
    545        1.2  augustss 		s = splusb();
    546        1.1  augustss 		while (sce->q.c_cc == 0) {
    547        1.1  augustss 			if (flag & IO_NDELAY) {
    548        1.1  augustss 				splx(s);
    549        1.8  augustss 				return (EWOULDBLOCK);
    550        1.1  augustss 			}
    551        1.1  augustss 			sce->state |= UGEN_ASLP;
    552   1.45.2.1   nathanw 			DPRINTFN(5, ("ugenread: sleep on %p\n", sce));
    553       1.12  augustss 			error = tsleep(sce, PZERO | PCATCH, "ugenri", 0);
    554        1.1  augustss 			DPRINTFN(5, ("ugenread: woke, error=%d\n", error));
    555       1.12  augustss 			if (sc->sc_dying)
    556       1.12  augustss 				error = EIO;
    557        1.1  augustss 			if (error) {
    558        1.1  augustss 				sce->state &= ~UGEN_ASLP;
    559       1.12  augustss 				break;
    560        1.1  augustss 			}
    561        1.1  augustss 		}
    562        1.1  augustss 		splx(s);
    563        1.1  augustss 
    564        1.1  augustss 		/* Transfer as many chunks as possible. */
    565       1.12  augustss 		while (sce->q.c_cc > 0 && uio->uio_resid > 0 && !error) {
    566        1.1  augustss 			n = min(sce->q.c_cc, uio->uio_resid);
    567        1.1  augustss 			if (n > sizeof(buffer))
    568        1.1  augustss 				n = sizeof(buffer);
    569        1.1  augustss 
    570        1.1  augustss 			/* Remove a small chunk from the input queue. */
    571        1.1  augustss 			q_to_b(&sce->q, buffer, n);
    572        1.1  augustss 			DPRINTFN(5, ("ugenread: got %d chars\n", n));
    573        1.1  augustss 
    574        1.1  augustss 			/* Copy the data to the user process. */
    575        1.1  augustss 			error = uiomove(buffer, n, uio);
    576        1.1  augustss 			if (error)
    577        1.1  augustss 				break;
    578        1.1  augustss 		}
    579        1.1  augustss 		break;
    580        1.1  augustss 	case UE_BULK:
    581       1.29  augustss 		xfer = usbd_alloc_xfer(sc->sc_udev);
    582       1.28  augustss 		if (xfer == 0)
    583        1.1  augustss 			return (ENOMEM);
    584        1.1  augustss 		while ((n = min(UGEN_BBSIZE, uio->uio_resid)) != 0) {
    585       1.10  augustss 			DPRINTFN(1, ("ugenread: start transfer %d bytes\n",n));
    586       1.10  augustss 			tn = n;
    587       1.28  augustss 			err = usbd_bulk_transfer(
    588       1.28  augustss 				  xfer, sce->pipeh,
    589   1.45.2.3   nathanw 				  sce->state & UGEN_SHORT_OK ?
    590   1.45.2.3   nathanw 				      USBD_SHORT_XFER_OK : 0,
    591       1.28  augustss 				  sce->timeout, buf, &tn, "ugenrb");
    592       1.30  augustss 			if (err) {
    593       1.28  augustss 				if (err == USBD_INTERRUPTED)
    594        1.8  augustss 					error = EINTR;
    595       1.28  augustss 				else if (err == USBD_TIMEOUT)
    596       1.17  augustss 					error = ETIMEDOUT;
    597        1.8  augustss 				else
    598        1.8  augustss 					error = EIO;
    599        1.1  augustss 				break;
    600        1.1  augustss 			}
    601       1.10  augustss 			DPRINTFN(1, ("ugenread: got %d bytes\n", tn));
    602        1.9  augustss 			error = uiomove(buf, tn, uio);
    603        1.9  augustss 			if (error || tn < n)
    604        1.1  augustss 				break;
    605        1.1  augustss 		}
    606       1.29  augustss 		usbd_free_xfer(xfer);
    607        1.1  augustss 		break;
    608       1.41  augustss 	case UE_ISOCHRONOUS:
    609       1.41  augustss 		s = splusb();
    610       1.41  augustss 		while (sce->cur == sce->fill) {
    611       1.41  augustss 			if (flag & IO_NDELAY) {
    612       1.41  augustss 				splx(s);
    613       1.41  augustss 				return (EWOULDBLOCK);
    614       1.41  augustss 			}
    615       1.41  augustss 			sce->state |= UGEN_ASLP;
    616   1.45.2.1   nathanw 			DPRINTFN(5, ("ugenread: sleep on %p\n", sce));
    617       1.41  augustss 			error = tsleep(sce, PZERO | PCATCH, "ugenri", 0);
    618       1.41  augustss 			DPRINTFN(5, ("ugenread: woke, error=%d\n", error));
    619       1.41  augustss 			if (sc->sc_dying)
    620       1.41  augustss 				error = EIO;
    621       1.41  augustss 			if (error) {
    622       1.41  augustss 				sce->state &= ~UGEN_ASLP;
    623       1.41  augustss 				break;
    624       1.41  augustss 			}
    625       1.41  augustss 		}
    626       1.41  augustss 
    627       1.41  augustss 		while (sce->cur != sce->fill && uio->uio_resid > 0 && !error) {
    628       1.41  augustss 			if(sce->fill > sce->cur)
    629       1.41  augustss 				n = min(sce->fill - sce->cur, uio->uio_resid);
    630       1.41  augustss 			else
    631       1.41  augustss 				n = min(sce->limit - sce->cur, uio->uio_resid);
    632       1.41  augustss 
    633       1.41  augustss 			DPRINTFN(5, ("ugenread: isoc got %d chars\n", n));
    634       1.41  augustss 
    635       1.41  augustss 			/* Copy the data to the user process. */
    636       1.41  augustss 			error = uiomove(sce->cur, n, uio);
    637       1.41  augustss 			if (error)
    638       1.41  augustss 				break;
    639       1.41  augustss 			sce->cur += n;
    640       1.41  augustss 			if(sce->cur >= sce->limit)
    641       1.41  augustss 				sce->cur = sce->ibuf;
    642       1.41  augustss 		}
    643       1.41  augustss 		splx(s);
    644       1.41  augustss 		break;
    645       1.41  augustss 
    646   1.45.2.6   nathanw 
    647        1.1  augustss 	default:
    648        1.1  augustss 		return (ENXIO);
    649        1.1  augustss 	}
    650        1.1  augustss 	return (error);
    651        1.1  augustss }
    652        1.1  augustss 
    653        1.1  augustss int
    654       1.40  augustss ugenread(dev_t dev, struct uio *uio, int flag)
    655        1.1  augustss {
    656        1.1  augustss 	int endpt = UGENENDPOINT(dev);
    657       1.24  augustss 	struct ugen_softc *sc;
    658       1.12  augustss 	int error;
    659       1.12  augustss 
    660       1.24  augustss 	USB_GET_SC(ugen, UGENUNIT(dev), sc);
    661       1.30  augustss 
    662       1.12  augustss 	sc->sc_refcnt++;
    663       1.12  augustss 	error = ugen_do_read(sc, endpt, uio, flag);
    664       1.12  augustss 	if (--sc->sc_refcnt < 0)
    665       1.22  augustss 		usb_detach_wakeup(USBDEV(sc->sc_dev));
    666       1.12  augustss 	return (error);
    667       1.12  augustss }
    668       1.12  augustss 
    669       1.37  augustss Static int
    670       1.40  augustss ugen_do_write(struct ugen_softc *sc, int endpt, struct uio *uio, int flag)
    671       1.12  augustss {
    672       1.10  augustss 	struct ugen_endpoint *sce = &sc->sc_endpoints[endpt][OUT];
    673       1.16  augustss 	u_int32_t n;
    674        1.1  augustss 	int error = 0;
    675        1.1  augustss 	char buf[UGEN_BBSIZE];
    676       1.28  augustss 	usbd_xfer_handle xfer;
    677       1.28  augustss 	usbd_status err;
    678        1.1  augustss 
    679       1.30  augustss 	DPRINTFN(5, ("%s: ugenwrite: %d\n", USBDEVNAME(sc->sc_dev), endpt));
    680       1.26  augustss 
    681       1.12  augustss 	if (sc->sc_dying)
    682        1.1  augustss 		return (EIO);
    683        1.1  augustss 
    684       1.26  augustss 	if (endpt == USB_CONTROL_ENDPOINT)
    685       1.26  augustss 		return (ENODEV);
    686       1.26  augustss 
    687        1.1  augustss #ifdef DIAGNOSTIC
    688       1.28  augustss 	if (sce->edesc == NULL) {
    689        1.1  augustss 		printf("ugenwrite: no edesc\n");
    690        1.1  augustss 		return (EIO);
    691        1.1  augustss 	}
    692       1.28  augustss 	if (sce->pipeh == NULL) {
    693        1.9  augustss 		printf("ugenwrite: no pipe\n");
    694        1.9  augustss 		return (EIO);
    695        1.9  augustss 	}
    696        1.1  augustss #endif
    697        1.1  augustss 
    698        1.1  augustss 	switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
    699        1.1  augustss 	case UE_BULK:
    700       1.29  augustss 		xfer = usbd_alloc_xfer(sc->sc_udev);
    701       1.28  augustss 		if (xfer == 0)
    702        1.1  augustss 			return (EIO);
    703        1.1  augustss 		while ((n = min(UGEN_BBSIZE, uio->uio_resid)) != 0) {
    704        1.1  augustss 			error = uiomove(buf, n, uio);
    705        1.1  augustss 			if (error)
    706        1.1  augustss 				break;
    707        1.1  augustss 			DPRINTFN(1, ("ugenwrite: transfer %d bytes\n", n));
    708   1.45.2.3   nathanw 			err = usbd_bulk_transfer(xfer, sce->pipeh, 0,
    709       1.28  augustss 				  sce->timeout, buf, &n,"ugenwb");
    710       1.28  augustss 			if (err) {
    711       1.28  augustss 				if (err == USBD_INTERRUPTED)
    712        1.8  augustss 					error = EINTR;
    713       1.36  augustss 				else if (err == USBD_TIMEOUT)
    714       1.36  augustss 					error = ETIMEDOUT;
    715        1.8  augustss 				else
    716        1.8  augustss 					error = EIO;
    717        1.1  augustss 				break;
    718        1.1  augustss 			}
    719        1.1  augustss 		}
    720       1.29  augustss 		usbd_free_xfer(xfer);
    721        1.1  augustss 		break;
    722        1.1  augustss 	default:
    723        1.1  augustss 		return (ENXIO);
    724        1.1  augustss 	}
    725        1.1  augustss 	return (error);
    726        1.1  augustss }
    727        1.1  augustss 
    728       1.12  augustss int
    729       1.40  augustss ugenwrite(dev_t dev, struct uio *uio, int flag)
    730       1.12  augustss {
    731       1.12  augustss 	int endpt = UGENENDPOINT(dev);
    732       1.24  augustss 	struct ugen_softc *sc;
    733       1.12  augustss 	int error;
    734       1.12  augustss 
    735       1.24  augustss 	USB_GET_SC(ugen, UGENUNIT(dev), sc);
    736       1.30  augustss 
    737       1.12  augustss 	sc->sc_refcnt++;
    738       1.12  augustss 	error = ugen_do_write(sc, endpt, uio, flag);
    739       1.12  augustss 	if (--sc->sc_refcnt < 0)
    740       1.22  augustss 		usb_detach_wakeup(USBDEV(sc->sc_dev));
    741       1.12  augustss 	return (error);
    742       1.12  augustss }
    743       1.12  augustss 
    744       1.25  augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
    745       1.12  augustss int
    746       1.40  augustss ugen_activate(device_ptr_t self, enum devact act)
    747       1.12  augustss {
    748       1.18  augustss 	struct ugen_softc *sc = (struct ugen_softc *)self;
    749       1.18  augustss 
    750       1.18  augustss 	switch (act) {
    751       1.18  augustss 	case DVACT_ACTIVATE:
    752       1.18  augustss 		return (EOPNOTSUPP);
    753       1.18  augustss 
    754       1.18  augustss 	case DVACT_DEACTIVATE:
    755       1.18  augustss 		sc->sc_dying = 1;
    756       1.18  augustss 		break;
    757       1.18  augustss 	}
    758       1.12  augustss 	return (0);
    759       1.12  augustss }
    760       1.25  augustss #endif
    761       1.12  augustss 
    762       1.25  augustss USB_DETACH(ugen)
    763       1.12  augustss {
    764       1.25  augustss 	USB_DETACH_START(ugen, sc);
    765       1.12  augustss 	struct ugen_endpoint *sce;
    766       1.12  augustss 	int i, dir;
    767       1.12  augustss 	int s;
    768       1.25  augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
    769       1.25  augustss 	int maj, mn;
    770       1.12  augustss 
    771       1.12  augustss 	DPRINTF(("ugen_detach: sc=%p flags=%d\n", sc, flags));
    772       1.25  augustss #elif defined(__FreeBSD__)
    773       1.25  augustss 	DPRINTF(("ugen_detach: sc=%p\n", sc));
    774       1.25  augustss #endif
    775       1.12  augustss 
    776       1.12  augustss 	sc->sc_dying = 1;
    777       1.12  augustss 	/* Abort all pipes.  Causes processes waiting for transfer to wake. */
    778       1.12  augustss 	for (i = 0; i < USB_MAX_ENDPOINTS; i++) {
    779       1.12  augustss 		for (dir = OUT; dir <= IN; dir++) {
    780       1.12  augustss 			sce = &sc->sc_endpoints[i][dir];
    781       1.12  augustss 			if (sce && sce->pipeh)
    782       1.12  augustss 				usbd_abort_pipe(sce->pipeh);
    783       1.12  augustss 		}
    784       1.12  augustss 	}
    785       1.12  augustss 
    786       1.12  augustss 	s = splusb();
    787       1.12  augustss 	if (--sc->sc_refcnt >= 0) {
    788       1.12  augustss 		/* Wake everyone */
    789       1.12  augustss 		for (i = 0; i < USB_MAX_ENDPOINTS; i++)
    790       1.12  augustss 			wakeup(&sc->sc_endpoints[i][IN]);
    791       1.12  augustss 		/* Wait for processes to go away. */
    792       1.22  augustss 		usb_detach_wait(USBDEV(sc->sc_dev));
    793       1.12  augustss 	}
    794       1.12  augustss 	splx(s);
    795       1.12  augustss 
    796       1.25  augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
    797       1.12  augustss 	/* locate the major number */
    798   1.45.2.7   nathanw #if defined(__NetBSD__)
    799   1.45.2.7   nathanw 	maj = cdevsw_lookup_major(&ugen_cdevsw);
    800   1.45.2.7   nathanw #elif defined(__OpenBSD__)
    801       1.12  augustss 	for (maj = 0; maj < nchrdev; maj++)
    802       1.12  augustss 		if (cdevsw[maj].d_open == ugenopen)
    803       1.12  augustss 			break;
    804   1.45.2.7   nathanw #endif
    805       1.12  augustss 
    806       1.12  augustss 	/* Nuke the vnodes for any open instances (calls close). */
    807       1.12  augustss 	mn = self->dv_unit * USB_MAX_ENDPOINTS;
    808       1.12  augustss 	vdevgone(maj, mn, mn + USB_MAX_ENDPOINTS - 1, VCHR);
    809       1.25  augustss #elif defined(__FreeBSD__)
    810       1.25  augustss 	/* XXX not implemented yet */
    811       1.25  augustss #endif
    812       1.33  augustss 
    813       1.33  augustss 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    814       1.33  augustss 			   USBDEV(sc->sc_dev));
    815       1.12  augustss 
    816       1.12  augustss 	return (0);
    817       1.12  augustss }
    818        1.1  augustss 
    819       1.37  augustss Static void
    820       1.40  augustss ugenintr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
    821        1.1  augustss {
    822        1.1  augustss 	struct ugen_endpoint *sce = addr;
    823        1.1  augustss 	/*struct ugen_softc *sc = sce->sc;*/
    824        1.8  augustss 	u_int32_t count;
    825        1.1  augustss 	u_char *ibuf;
    826        1.1  augustss 
    827        1.1  augustss 	if (status == USBD_CANCELLED)
    828        1.1  augustss 		return;
    829        1.1  augustss 
    830        1.1  augustss 	if (status != USBD_NORMAL_COMPLETION) {
    831        1.1  augustss 		DPRINTF(("ugenintr: status=%d\n", status));
    832   1.45.2.3   nathanw 		if (status == USBD_STALLED)
    833   1.45.2.3   nathanw 		    usbd_clear_endpoint_stall_async(sce->pipeh);
    834        1.1  augustss 		return;
    835        1.1  augustss 	}
    836        1.1  augustss 
    837       1.34  augustss 	usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
    838        1.1  augustss 	ibuf = sce->ibuf;
    839        1.1  augustss 
    840   1.45.2.3   nathanw 	DPRINTFN(5, ("ugenintr: xfer=%p status=%d count=%d\n",
    841       1.28  augustss 		     xfer, status, count));
    842        1.1  augustss 	DPRINTFN(5, ("          data = %02x %02x %02x\n",
    843        1.8  augustss 		     ibuf[0], ibuf[1], ibuf[2]));
    844        1.1  augustss 
    845        1.8  augustss 	(void)b_to_q(ibuf, count, &sce->q);
    846   1.45.2.6   nathanw 
    847        1.1  augustss 	if (sce->state & UGEN_ASLP) {
    848        1.1  augustss 		sce->state &= ~UGEN_ASLP;
    849        1.1  augustss 		DPRINTFN(5, ("ugen_intr: waking %p\n", sce));
    850       1.12  augustss 		wakeup(sce);
    851        1.1  augustss 	}
    852   1.45.2.9   nathanw 	selnotify(&sce->rsel, 0);
    853        1.1  augustss }
    854        1.1  augustss 
    855       1.41  augustss Static void
    856   1.45.2.3   nathanw ugen_isoc_rintr(usbd_xfer_handle xfer, usbd_private_handle addr,
    857       1.42  augustss 		usbd_status status)
    858       1.41  augustss {
    859       1.41  augustss 	struct isoreq *req = addr;
    860       1.41  augustss 	struct ugen_endpoint *sce = req->sce;
    861       1.41  augustss 	u_int32_t count, n;
    862   1.45.2.1   nathanw 	int i, isize;
    863       1.41  augustss 
    864       1.41  augustss 	/* Return if we are aborting. */
    865       1.41  augustss 	if (status == USBD_CANCELLED)
    866       1.41  augustss 		return;
    867       1.41  augustss 
    868       1.41  augustss 	usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
    869       1.41  augustss 	DPRINTFN(5,("ugen_isoc_rintr: xfer %d, count=%d\n", req - sce->isoreqs,
    870       1.41  augustss 		    count));
    871       1.41  augustss 
    872       1.41  augustss 	/* throw away oldest input if the buffer is full */
    873       1.41  augustss 	if(sce->fill < sce->cur && sce->cur <= sce->fill + count) {
    874       1.41  augustss 		sce->cur += count;
    875       1.41  augustss 		if(sce->cur >= sce->limit)
    876       1.41  augustss 			sce->cur = sce->ibuf + (sce->limit - sce->cur);
    877       1.41  augustss 		DPRINTFN(5, ("ugen_isoc_rintr: throwing away %d bytes\n",
    878       1.41  augustss 			     count));
    879       1.41  augustss 	}
    880       1.41  augustss 
    881   1.45.2.1   nathanw 	isize = UGETW(sce->edesc->wMaxPacketSize);
    882   1.45.2.1   nathanw 	for (i = 0; i < UGEN_NISORFRMS; i++) {
    883   1.45.2.1   nathanw 		u_int32_t actlen = req->sizes[i];
    884   1.45.2.1   nathanw 		char const *buf = (char const *)req->dmabuf + isize * i;
    885   1.45.2.1   nathanw 
    886   1.45.2.1   nathanw 		/* copy data to buffer */
    887   1.45.2.1   nathanw 		while (actlen > 0) {
    888   1.45.2.1   nathanw 			n = min(actlen, sce->limit - sce->fill);
    889   1.45.2.1   nathanw 			memcpy(sce->fill, buf, n);
    890   1.45.2.1   nathanw 
    891   1.45.2.1   nathanw 			buf += n;
    892   1.45.2.1   nathanw 			actlen -= n;
    893   1.45.2.1   nathanw 			sce->fill += n;
    894   1.45.2.1   nathanw 			if(sce->fill == sce->limit)
    895   1.45.2.1   nathanw 				sce->fill = sce->ibuf;
    896   1.45.2.1   nathanw 		}
    897   1.45.2.1   nathanw 
    898   1.45.2.1   nathanw 		/* setup size for next transfer */
    899   1.45.2.1   nathanw 		req->sizes[i] = isize;
    900       1.41  augustss 	}
    901       1.41  augustss 
    902       1.41  augustss 	usbd_setup_isoc_xfer(xfer, sce->pipeh, req, req->sizes, UGEN_NISORFRMS,
    903       1.41  augustss 			     USBD_NO_COPY, ugen_isoc_rintr);
    904       1.41  augustss 	(void)usbd_transfer(xfer);
    905       1.41  augustss 
    906       1.41  augustss 	if (sce->state & UGEN_ASLP) {
    907       1.41  augustss 		sce->state &= ~UGEN_ASLP;
    908       1.41  augustss 		DPRINTFN(5, ("ugen_isoc_rintr: waking %p\n", sce));
    909       1.41  augustss 		wakeup(sce);
    910       1.41  augustss 	}
    911   1.45.2.9   nathanw 	selnotify(&sce->rsel, 0);
    912       1.41  augustss }
    913       1.41  augustss 
    914       1.37  augustss Static usbd_status
    915       1.40  augustss ugen_set_interface(struct ugen_softc *sc, int ifaceidx, int altno)
    916        1.1  augustss {
    917        1.1  augustss 	usbd_interface_handle iface;
    918        1.1  augustss 	usb_endpoint_descriptor_t *ed;
    919       1.28  augustss 	usbd_status err;
    920        1.1  augustss 	struct ugen_endpoint *sce;
    921        1.1  augustss 	u_int8_t niface, nendpt, endptno, endpt;
    922       1.21  augustss 	int dir;
    923        1.1  augustss 
    924        1.1  augustss 	DPRINTFN(15, ("ugen_set_interface %d %d\n", ifaceidx, altno));
    925        1.1  augustss 
    926       1.28  augustss 	err = usbd_interface_count(sc->sc_udev, &niface);
    927       1.28  augustss 	if (err)
    928       1.28  augustss 		return (err);
    929        1.1  augustss 	if (ifaceidx < 0 || ifaceidx >= niface)
    930        1.1  augustss 		return (USBD_INVAL);
    931   1.45.2.6   nathanw 
    932       1.28  augustss 	err = usbd_device2interface_handle(sc->sc_udev, ifaceidx, &iface);
    933       1.28  augustss 	if (err)
    934       1.28  augustss 		return (err);
    935       1.28  augustss 	err = usbd_endpoint_count(iface, &nendpt);
    936       1.28  augustss 	if (err)
    937       1.28  augustss 		return (err);
    938       1.39  augustss 	/* XXX should only do this after setting new altno has succeeded */
    939        1.1  augustss 	for (endptno = 0; endptno < nendpt; endptno++) {
    940        1.1  augustss 		ed = usbd_interface2endpoint_descriptor(iface,endptno);
    941        1.1  augustss 		endpt = ed->bEndpointAddress;
    942       1.21  augustss 		dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
    943       1.21  augustss 		sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
    944        1.1  augustss 		sce->sc = 0;
    945        1.1  augustss 		sce->edesc = 0;
    946        1.1  augustss 		sce->iface = 0;
    947        1.1  augustss 	}
    948        1.1  augustss 
    949        1.1  augustss 	/* change setting */
    950       1.28  augustss 	err = usbd_set_interface(iface, altno);
    951       1.28  augustss 	if (err)
    952       1.28  augustss 		return (err);
    953       1.28  augustss 
    954       1.28  augustss 	err = usbd_endpoint_count(iface, &nendpt);
    955       1.28  augustss 	if (err)
    956       1.28  augustss 		return (err);
    957        1.1  augustss 	for (endptno = 0; endptno < nendpt; endptno++) {
    958        1.1  augustss 		ed = usbd_interface2endpoint_descriptor(iface,endptno);
    959        1.1  augustss 		endpt = ed->bEndpointAddress;
    960       1.21  augustss 		dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
    961       1.21  augustss 		sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
    962        1.1  augustss 		sce->sc = sc;
    963        1.1  augustss 		sce->edesc = ed;
    964        1.1  augustss 		sce->iface = iface;
    965        1.1  augustss 	}
    966        1.1  augustss 	return (0);
    967        1.1  augustss }
    968        1.1  augustss 
    969        1.1  augustss /* Retrieve a complete descriptor for a certain device and index. */
    970       1.37  augustss Static usb_config_descriptor_t *
    971       1.40  augustss ugen_get_cdesc(struct ugen_softc *sc, int index, int *lenp)
    972        1.1  augustss {
    973        1.1  augustss 	usb_config_descriptor_t *cdesc, *tdesc, cdescr;
    974        1.1  augustss 	int len;
    975       1.28  augustss 	usbd_status err;
    976        1.1  augustss 
    977        1.1  augustss 	if (index == USB_CURRENT_CONFIG_INDEX) {
    978        1.1  augustss 		tdesc = usbd_get_config_descriptor(sc->sc_udev);
    979        1.1  augustss 		len = UGETW(tdesc->wTotalLength);
    980        1.1  augustss 		if (lenp)
    981        1.1  augustss 			*lenp = len;
    982        1.1  augustss 		cdesc = malloc(len, M_TEMP, M_WAITOK);
    983        1.1  augustss 		memcpy(cdesc, tdesc, len);
    984        1.1  augustss 		DPRINTFN(5,("ugen_get_cdesc: current, len=%d\n", len));
    985        1.1  augustss 	} else {
    986       1.28  augustss 		err = usbd_get_config_desc(sc->sc_udev, index, &cdescr);
    987       1.28  augustss 		if (err)
    988        1.1  augustss 			return (0);
    989        1.1  augustss 		len = UGETW(cdescr.wTotalLength);
    990        1.1  augustss 		DPRINTFN(5,("ugen_get_cdesc: index=%d, len=%d\n", index, len));
    991        1.1  augustss 		if (lenp)
    992        1.1  augustss 			*lenp = len;
    993        1.1  augustss 		cdesc = malloc(len, M_TEMP, M_WAITOK);
    994   1.45.2.4   nathanw 		err = usbd_get_config_desc_full(sc->sc_udev, index, cdesc, len);
    995       1.28  augustss 		if (err) {
    996        1.1  augustss 			free(cdesc, M_TEMP);
    997        1.1  augustss 			return (0);
    998        1.1  augustss 		}
    999        1.1  augustss 	}
   1000        1.1  augustss 	return (cdesc);
   1001        1.1  augustss }
   1002        1.1  augustss 
   1003       1.37  augustss Static int
   1004       1.40  augustss ugen_get_alt_index(struct ugen_softc *sc, int ifaceidx)
   1005        1.1  augustss {
   1006        1.1  augustss 	usbd_interface_handle iface;
   1007       1.28  augustss 	usbd_status err;
   1008        1.1  augustss 
   1009       1.28  augustss 	err = usbd_device2interface_handle(sc->sc_udev, ifaceidx, &iface);
   1010       1.28  augustss 	if (err)
   1011       1.39  augustss 		return (-1);
   1012        1.2  augustss 	return (usbd_get_interface_altindex(iface));
   1013        1.1  augustss }
   1014        1.1  augustss 
   1015       1.37  augustss Static int
   1016       1.40  augustss ugen_do_ioctl(struct ugen_softc *sc, int endpt, u_long cmd,
   1017   1.45.2.3   nathanw 	      caddr_t addr, int flag, usb_proc_ptr p)
   1018        1.1  augustss {
   1019        1.8  augustss 	struct ugen_endpoint *sce;
   1020       1.28  augustss 	usbd_status err;
   1021        1.1  augustss 	usbd_interface_handle iface;
   1022        1.1  augustss 	struct usb_config_desc *cd;
   1023        1.1  augustss 	usb_config_descriptor_t *cdesc;
   1024        1.1  augustss 	struct usb_interface_desc *id;
   1025        1.1  augustss 	usb_interface_descriptor_t *idesc;
   1026        1.1  augustss 	struct usb_endpoint_desc *ed;
   1027        1.1  augustss 	usb_endpoint_descriptor_t *edesc;
   1028        1.1  augustss 	struct usb_alt_interface *ai;
   1029        1.2  augustss 	struct usb_string_desc *si;
   1030        1.1  augustss 	u_int8_t conf, alt;
   1031        1.1  augustss 
   1032        1.1  augustss 	DPRINTFN(5, ("ugenioctl: cmd=%08lx\n", cmd));
   1033       1.12  augustss 	if (sc->sc_dying)
   1034        1.1  augustss 		return (EIO);
   1035        1.1  augustss 
   1036        1.2  augustss 	switch (cmd) {
   1037        1.2  augustss 	case FIONBIO:
   1038        1.2  augustss 		/* All handled in the upper FS layer. */
   1039        1.2  augustss 		return (0);
   1040        1.8  augustss 	case USB_SET_SHORT_XFER:
   1041       1.13  augustss 		if (endpt == USB_CONTROL_ENDPOINT)
   1042       1.13  augustss 			return (EINVAL);
   1043   1.45.2.2   nathanw 		/* This flag only affects read */
   1044        1.8  augustss 		sce = &sc->sc_endpoints[endpt][IN];
   1045   1.45.2.2   nathanw 		if (sce == NULL || sce->pipeh == NULL)
   1046       1.27  augustss 			return (EINVAL);
   1047        1.8  augustss 		if (*(int *)addr)
   1048        1.8  augustss 			sce->state |= UGEN_SHORT_OK;
   1049        1.8  augustss 		else
   1050        1.8  augustss 			sce->state &= ~UGEN_SHORT_OK;
   1051       1.17  augustss 		return (0);
   1052       1.17  augustss 	case USB_SET_TIMEOUT:
   1053   1.45.2.2   nathanw 		sce = &sc->sc_endpoints[endpt][IN];
   1054   1.45.2.6   nathanw 		if (sce == NULL
   1055   1.45.2.5   nathanw 		    /* XXX this shouldn't happen, but the distinction between
   1056   1.45.2.5   nathanw 		       input and output pipes isn't clear enough.
   1057   1.45.2.5   nathanw 		       || sce->pipeh == NULL */
   1058   1.45.2.5   nathanw 			)
   1059   1.45.2.2   nathanw 			return (EINVAL);
   1060       1.17  augustss 		sce->timeout = *(int *)addr;
   1061        1.8  augustss 		return (0);
   1062        1.2  augustss 	default:
   1063        1.2  augustss 		break;
   1064        1.2  augustss 	}
   1065        1.2  augustss 
   1066        1.1  augustss 	if (endpt != USB_CONTROL_ENDPOINT)
   1067        1.1  augustss 		return (EINVAL);
   1068        1.1  augustss 
   1069        1.1  augustss 	switch (cmd) {
   1070       1.30  augustss #ifdef UGEN_DEBUG
   1071        1.8  augustss 	case USB_SETDEBUG:
   1072        1.8  augustss 		ugendebug = *(int *)addr;
   1073        1.8  augustss 		break;
   1074        1.8  augustss #endif
   1075        1.1  augustss 	case USB_GET_CONFIG:
   1076       1.28  augustss 		err = usbd_get_config(sc->sc_udev, &conf);
   1077       1.28  augustss 		if (err)
   1078        1.1  augustss 			return (EIO);
   1079        1.1  augustss 		*(int *)addr = conf;
   1080        1.1  augustss 		break;
   1081        1.1  augustss 	case USB_SET_CONFIG:
   1082        1.2  augustss 		if (!(flag & FWRITE))
   1083        1.2  augustss 			return (EPERM);
   1084       1.28  augustss 		err = ugen_set_config(sc, *(int *)addr);
   1085   1.45.2.3   nathanw 		switch (err) {
   1086   1.45.2.3   nathanw 		case USBD_NORMAL_COMPLETION:
   1087   1.45.2.3   nathanw 			break;
   1088   1.45.2.3   nathanw 		case USBD_IN_USE:
   1089   1.45.2.3   nathanw 			return (EBUSY);
   1090   1.45.2.3   nathanw 		default:
   1091        1.1  augustss 			return (EIO);
   1092   1.45.2.3   nathanw 		}
   1093        1.1  augustss 		break;
   1094        1.1  augustss 	case USB_GET_ALTINTERFACE:
   1095        1.1  augustss 		ai = (struct usb_alt_interface *)addr;
   1096   1.45.2.3   nathanw 		err = usbd_device2interface_handle(sc->sc_udev,
   1097   1.45.2.5   nathanw 			  ai->uai_interface_index, &iface);
   1098       1.28  augustss 		if (err)
   1099        1.1  augustss 			return (EINVAL);
   1100        1.1  augustss 		idesc = usbd_get_interface_descriptor(iface);
   1101       1.28  augustss 		if (idesc == NULL)
   1102        1.2  augustss 			return (EIO);
   1103   1.45.2.5   nathanw 		ai->uai_alt_no = idesc->bAlternateSetting;
   1104        1.1  augustss 		break;
   1105        1.1  augustss 	case USB_SET_ALTINTERFACE:
   1106        1.2  augustss 		if (!(flag & FWRITE))
   1107        1.2  augustss 			return (EPERM);
   1108        1.1  augustss 		ai = (struct usb_alt_interface *)addr;
   1109   1.45.2.3   nathanw 		err = usbd_device2interface_handle(sc->sc_udev,
   1110   1.45.2.5   nathanw 			  ai->uai_interface_index, &iface);
   1111       1.28  augustss 		if (err)
   1112        1.1  augustss 			return (EINVAL);
   1113   1.45.2.5   nathanw 		err = ugen_set_interface(sc, ai->uai_interface_index,
   1114   1.45.2.5   nathanw 		    ai->uai_alt_no);
   1115       1.28  augustss 		if (err)
   1116        1.1  augustss 			return (EINVAL);
   1117        1.1  augustss 		break;
   1118        1.1  augustss 	case USB_GET_NO_ALT:
   1119        1.1  augustss 		ai = (struct usb_alt_interface *)addr;
   1120   1.45.2.5   nathanw 		cdesc = ugen_get_cdesc(sc, ai->uai_config_index, 0);
   1121       1.28  augustss 		if (cdesc == NULL)
   1122        1.2  augustss 			return (EINVAL);
   1123   1.45.2.5   nathanw 		idesc = usbd_find_idesc(cdesc, ai->uai_interface_index, 0);
   1124       1.28  augustss 		if (idesc == NULL) {
   1125       1.12  augustss 			free(cdesc, M_TEMP);
   1126        1.1  augustss 			return (EINVAL);
   1127       1.12  augustss 		}
   1128   1.45.2.5   nathanw 		ai->uai_alt_no = usbd_get_no_alts(cdesc,
   1129   1.45.2.5   nathanw 		    idesc->bInterfaceNumber);
   1130       1.12  augustss 		free(cdesc, M_TEMP);
   1131        1.1  augustss 		break;
   1132        1.1  augustss 	case USB_GET_DEVICE_DESC:
   1133        1.1  augustss 		*(usb_device_descriptor_t *)addr =
   1134        1.1  augustss 			*usbd_get_device_descriptor(sc->sc_udev);
   1135        1.1  augustss 		break;
   1136        1.1  augustss 	case USB_GET_CONFIG_DESC:
   1137        1.1  augustss 		cd = (struct usb_config_desc *)addr;
   1138   1.45.2.5   nathanw 		cdesc = ugen_get_cdesc(sc, cd->ucd_config_index, 0);
   1139       1.28  augustss 		if (cdesc == NULL)
   1140        1.1  augustss 			return (EINVAL);
   1141   1.45.2.5   nathanw 		cd->ucd_desc = *cdesc;
   1142        1.1  augustss 		free(cdesc, M_TEMP);
   1143        1.1  augustss 		break;
   1144        1.1  augustss 	case USB_GET_INTERFACE_DESC:
   1145        1.1  augustss 		id = (struct usb_interface_desc *)addr;
   1146   1.45.2.5   nathanw 		cdesc = ugen_get_cdesc(sc, id->uid_config_index, 0);
   1147       1.28  augustss 		if (cdesc == NULL)
   1148        1.1  augustss 			return (EINVAL);
   1149   1.45.2.5   nathanw 		if (id->uid_config_index == USB_CURRENT_CONFIG_INDEX &&
   1150   1.45.2.5   nathanw 		    id->uid_alt_index == USB_CURRENT_ALT_INDEX)
   1151   1.45.2.5   nathanw 			alt = ugen_get_alt_index(sc, id->uid_interface_index);
   1152        1.1  augustss 		else
   1153   1.45.2.5   nathanw 			alt = id->uid_alt_index;
   1154   1.45.2.5   nathanw 		idesc = usbd_find_idesc(cdesc, id->uid_interface_index, alt);
   1155       1.28  augustss 		if (idesc == NULL) {
   1156        1.1  augustss 			free(cdesc, M_TEMP);
   1157        1.1  augustss 			return (EINVAL);
   1158        1.1  augustss 		}
   1159   1.45.2.5   nathanw 		id->uid_desc = *idesc;
   1160        1.1  augustss 		free(cdesc, M_TEMP);
   1161        1.1  augustss 		break;
   1162        1.1  augustss 	case USB_GET_ENDPOINT_DESC:
   1163        1.1  augustss 		ed = (struct usb_endpoint_desc *)addr;
   1164   1.45.2.5   nathanw 		cdesc = ugen_get_cdesc(sc, ed->ued_config_index, 0);
   1165       1.28  augustss 		if (cdesc == NULL)
   1166        1.1  augustss 			return (EINVAL);
   1167   1.45.2.5   nathanw 		if (ed->ued_config_index == USB_CURRENT_CONFIG_INDEX &&
   1168   1.45.2.5   nathanw 		    ed->ued_alt_index == USB_CURRENT_ALT_INDEX)
   1169   1.45.2.5   nathanw 			alt = ugen_get_alt_index(sc, ed->ued_interface_index);
   1170        1.1  augustss 		else
   1171   1.45.2.5   nathanw 			alt = ed->ued_alt_index;
   1172   1.45.2.5   nathanw 		edesc = usbd_find_edesc(cdesc, ed->ued_interface_index,
   1173   1.45.2.5   nathanw 					alt, ed->ued_endpoint_index);
   1174       1.28  augustss 		if (edesc == NULL) {
   1175        1.1  augustss 			free(cdesc, M_TEMP);
   1176        1.1  augustss 			return (EINVAL);
   1177        1.1  augustss 		}
   1178   1.45.2.5   nathanw 		ed->ued_desc = *edesc;
   1179        1.1  augustss 		free(cdesc, M_TEMP);
   1180        1.1  augustss 		break;
   1181        1.1  augustss 	case USB_GET_FULL_DESC:
   1182        1.1  augustss 	{
   1183        1.1  augustss 		int len;
   1184        1.1  augustss 		struct iovec iov;
   1185        1.1  augustss 		struct uio uio;
   1186        1.1  augustss 		struct usb_full_desc *fd = (struct usb_full_desc *)addr;
   1187        1.1  augustss 		int error;
   1188        1.1  augustss 
   1189   1.45.2.5   nathanw 		cdesc = ugen_get_cdesc(sc, fd->ufd_config_index, &len);
   1190   1.45.2.5   nathanw 		if (len > fd->ufd_size)
   1191   1.45.2.5   nathanw 			len = fd->ufd_size;
   1192   1.45.2.5   nathanw 		iov.iov_base = (caddr_t)fd->ufd_data;
   1193        1.1  augustss 		iov.iov_len = len;
   1194        1.1  augustss 		uio.uio_iov = &iov;
   1195        1.1  augustss 		uio.uio_iovcnt = 1;
   1196        1.1  augustss 		uio.uio_resid = len;
   1197        1.1  augustss 		uio.uio_offset = 0;
   1198        1.1  augustss 		uio.uio_segflg = UIO_USERSPACE;
   1199        1.1  augustss 		uio.uio_rw = UIO_READ;
   1200        1.1  augustss 		uio.uio_procp = p;
   1201       1.30  augustss 		error = uiomove((void *)cdesc, len, &uio);
   1202        1.1  augustss 		free(cdesc, M_TEMP);
   1203        1.1  augustss 		return (error);
   1204        1.1  augustss 	}
   1205        1.2  augustss 	case USB_GET_STRING_DESC:
   1206        1.2  augustss 		si = (struct usb_string_desc *)addr;
   1207   1.45.2.5   nathanw 		err = usbd_get_string_desc(sc->sc_udev, si->usd_string_index,
   1208   1.45.2.5   nathanw 			  si->usd_language_id, &si->usd_desc);
   1209       1.28  augustss 		if (err)
   1210        1.2  augustss 			return (EINVAL);
   1211        1.2  augustss 		break;
   1212        1.1  augustss 	case USB_DO_REQUEST:
   1213        1.1  augustss 	{
   1214        1.1  augustss 		struct usb_ctl_request *ur = (void *)addr;
   1215   1.45.2.5   nathanw 		int len = UGETW(ur->ucr_request.wLength);
   1216        1.1  augustss 		struct iovec iov;
   1217        1.1  augustss 		struct uio uio;
   1218        1.1  augustss 		void *ptr = 0;
   1219       1.28  augustss 		usbd_status err;
   1220        1.1  augustss 		int error = 0;
   1221        1.1  augustss 
   1222        1.2  augustss 		if (!(flag & FWRITE))
   1223        1.2  augustss 			return (EPERM);
   1224        1.1  augustss 		/* Avoid requests that would damage the bus integrity. */
   1225   1.45.2.5   nathanw 		if ((ur->ucr_request.bmRequestType == UT_WRITE_DEVICE &&
   1226   1.45.2.5   nathanw 		     ur->ucr_request.bRequest == UR_SET_ADDRESS) ||
   1227   1.45.2.5   nathanw 		    (ur->ucr_request.bmRequestType == UT_WRITE_DEVICE &&
   1228   1.45.2.5   nathanw 		     ur->ucr_request.bRequest == UR_SET_CONFIG) ||
   1229   1.45.2.5   nathanw 		    (ur->ucr_request.bmRequestType == UT_WRITE_INTERFACE &&
   1230   1.45.2.5   nathanw 		     ur->ucr_request.bRequest == UR_SET_INTERFACE))
   1231        1.1  augustss 			return (EINVAL);
   1232        1.1  augustss 
   1233        1.1  augustss 		if (len < 0 || len > 32767)
   1234        1.1  augustss 			return (EINVAL);
   1235        1.1  augustss 		if (len != 0) {
   1236   1.45.2.5   nathanw 			iov.iov_base = (caddr_t)ur->ucr_data;
   1237        1.1  augustss 			iov.iov_len = len;
   1238        1.1  augustss 			uio.uio_iov = &iov;
   1239        1.1  augustss 			uio.uio_iovcnt = 1;
   1240        1.1  augustss 			uio.uio_resid = len;
   1241        1.1  augustss 			uio.uio_offset = 0;
   1242        1.1  augustss 			uio.uio_segflg = UIO_USERSPACE;
   1243        1.1  augustss 			uio.uio_rw =
   1244   1.45.2.5   nathanw 				ur->ucr_request.bmRequestType & UT_READ ?
   1245        1.1  augustss 				UIO_READ : UIO_WRITE;
   1246        1.1  augustss 			uio.uio_procp = p;
   1247        1.1  augustss 			ptr = malloc(len, M_TEMP, M_WAITOK);
   1248        1.1  augustss 			if (uio.uio_rw == UIO_WRITE) {
   1249        1.1  augustss 				error = uiomove(ptr, len, &uio);
   1250        1.1  augustss 				if (error)
   1251        1.1  augustss 					goto ret;
   1252        1.1  augustss 			}
   1253        1.1  augustss 		}
   1254   1.45.2.5   nathanw 		sce = &sc->sc_endpoints[endpt][IN];
   1255   1.45.2.5   nathanw 		err = usbd_do_request_flags(sc->sc_udev, &ur->ucr_request,
   1256   1.45.2.5   nathanw 			  ptr, ur->ucr_flags, &ur->ucr_actlen, sce->timeout);
   1257       1.28  augustss 		if (err) {
   1258        1.1  augustss 			error = EIO;
   1259        1.1  augustss 			goto ret;
   1260        1.1  augustss 		}
   1261        1.1  augustss 		if (len != 0) {
   1262        1.1  augustss 			if (uio.uio_rw == UIO_READ) {
   1263        1.1  augustss 				error = uiomove(ptr, len, &uio);
   1264        1.1  augustss 				if (error)
   1265        1.1  augustss 					goto ret;
   1266        1.1  augustss 			}
   1267        1.1  augustss 		}
   1268        1.1  augustss 	ret:
   1269        1.1  augustss 		if (ptr)
   1270        1.1  augustss 			free(ptr, M_TEMP);
   1271        1.1  augustss 		return (error);
   1272        1.1  augustss 	}
   1273        1.2  augustss 	case USB_GET_DEVICEINFO:
   1274        1.2  augustss 		usbd_fill_deviceinfo(sc->sc_udev,
   1275       1.45  augustss 				     (struct usb_device_info *)addr, 1);
   1276        1.2  augustss 		break;
   1277        1.1  augustss 	default:
   1278        1.1  augustss 		return (EINVAL);
   1279        1.1  augustss 	}
   1280        1.1  augustss 	return (0);
   1281        1.1  augustss }
   1282        1.1  augustss 
   1283        1.1  augustss int
   1284   1.45.2.3   nathanw ugenioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, usb_proc_ptr p)
   1285       1.12  augustss {
   1286       1.12  augustss 	int endpt = UGENENDPOINT(dev);
   1287       1.24  augustss 	struct ugen_softc *sc;
   1288       1.12  augustss 	int error;
   1289       1.12  augustss 
   1290       1.24  augustss 	USB_GET_SC(ugen, UGENUNIT(dev), sc);
   1291       1.30  augustss 
   1292       1.12  augustss 	sc->sc_refcnt++;
   1293       1.12  augustss 	error = ugen_do_ioctl(sc, endpt, cmd, addr, flag, p);
   1294       1.12  augustss 	if (--sc->sc_refcnt < 0)
   1295       1.22  augustss 		usb_detach_wakeup(USBDEV(sc->sc_dev));
   1296       1.12  augustss 	return (error);
   1297       1.12  augustss }
   1298       1.12  augustss 
   1299       1.12  augustss int
   1300   1.45.2.3   nathanw ugenpoll(dev_t dev, int events, usb_proc_ptr p)
   1301        1.1  augustss {
   1302       1.24  augustss 	struct ugen_softc *sc;
   1303        1.2  augustss 	struct ugen_endpoint *sce;
   1304        1.1  augustss 	int revents = 0;
   1305        1.1  augustss 	int s;
   1306        1.1  augustss 
   1307       1.24  augustss 	USB_GET_SC(ugen, UGENUNIT(dev), sc);
   1308       1.30  augustss 
   1309       1.12  augustss 	if (sc->sc_dying)
   1310        1.1  augustss 		return (EIO);
   1311        1.1  augustss 
   1312       1.13  augustss 	/* XXX always IN */
   1313        1.4  augustss 	sce = &sc->sc_endpoints[UGENENDPOINT(dev)][IN];
   1314       1.27  augustss 	if (sce == NULL)
   1315       1.27  augustss 		return (EINVAL);
   1316        1.9  augustss #ifdef DIAGNOSTIC
   1317        1.9  augustss 	if (!sce->edesc) {
   1318       1.36  augustss 		printf("ugenpoll: no edesc\n");
   1319        1.9  augustss 		return (EIO);
   1320        1.9  augustss 	}
   1321        1.9  augustss 	if (!sce->pipeh) {
   1322        1.9  augustss 		printf("ugenpoll: no pipe\n");
   1323        1.9  augustss 		return (EIO);
   1324        1.9  augustss 	}
   1325        1.9  augustss #endif
   1326        1.2  augustss 	s = splusb();
   1327        1.2  augustss 	switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
   1328        1.1  augustss 	case UE_INTERRUPT:
   1329        1.1  augustss 		if (events & (POLLIN | POLLRDNORM)) {
   1330        1.1  augustss 			if (sce->q.c_cc > 0)
   1331       1.41  augustss 				revents |= events & (POLLIN | POLLRDNORM);
   1332       1.41  augustss 			else
   1333       1.41  augustss 				selrecord(p, &sce->rsel);
   1334       1.41  augustss 		}
   1335       1.41  augustss 		break;
   1336       1.41  augustss 	case UE_ISOCHRONOUS:
   1337       1.41  augustss 		if (events & (POLLIN | POLLRDNORM)) {
   1338       1.41  augustss 			if (sce->cur != sce->fill)
   1339        1.1  augustss 				revents |= events & (POLLIN | POLLRDNORM);
   1340        1.1  augustss 			else
   1341        1.1  augustss 				selrecord(p, &sce->rsel);
   1342        1.1  augustss 		}
   1343        1.2  augustss 		break;
   1344        1.2  augustss 	case UE_BULK:
   1345   1.45.2.3   nathanw 		/*
   1346        1.2  augustss 		 * We have no easy way of determining if a read will
   1347        1.2  augustss 		 * yield any data or a write will happen.
   1348        1.2  augustss 		 * Pretend they will.
   1349        1.2  augustss 		 */
   1350   1.45.2.3   nathanw 		revents |= events &
   1351        1.2  augustss 			   (POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM);
   1352        1.2  augustss 		break;
   1353        1.2  augustss 	default:
   1354        1.1  augustss 		break;
   1355        1.1  augustss 	}
   1356        1.1  augustss 	splx(s);
   1357        1.1  augustss 	return (revents);
   1358   1.45.2.9   nathanw }
   1359   1.45.2.9   nathanw 
   1360   1.45.2.9   nathanw static void
   1361   1.45.2.9   nathanw filt_ugenrdetach(struct knote *kn)
   1362   1.45.2.9   nathanw {
   1363   1.45.2.9   nathanw 	struct ugen_endpoint *sce = kn->kn_hook;
   1364   1.45.2.9   nathanw 	int s;
   1365   1.45.2.9   nathanw 
   1366   1.45.2.9   nathanw 	s = splusb();
   1367  1.45.2.10   thorpej 	SLIST_REMOVE(&sce->rsel.sel_klist, kn, knote, kn_selnext);
   1368   1.45.2.9   nathanw 	splx(s);
   1369   1.45.2.9   nathanw }
   1370   1.45.2.9   nathanw 
   1371   1.45.2.9   nathanw static int
   1372   1.45.2.9   nathanw filt_ugenread_intr(struct knote *kn, long hint)
   1373   1.45.2.9   nathanw {
   1374   1.45.2.9   nathanw 	struct ugen_endpoint *sce = kn->kn_hook;
   1375   1.45.2.9   nathanw 
   1376   1.45.2.9   nathanw 	kn->kn_data = sce->q.c_cc;
   1377   1.45.2.9   nathanw 	return (kn->kn_data > 0);
   1378   1.45.2.9   nathanw }
   1379   1.45.2.9   nathanw 
   1380   1.45.2.9   nathanw static int
   1381   1.45.2.9   nathanw filt_ugenread_isoc(struct knote *kn, long hint)
   1382   1.45.2.9   nathanw {
   1383   1.45.2.9   nathanw 	struct ugen_endpoint *sce = kn->kn_hook;
   1384   1.45.2.9   nathanw 
   1385   1.45.2.9   nathanw 	if (sce->cur == sce->fill)
   1386   1.45.2.9   nathanw 		return (0);
   1387   1.45.2.9   nathanw 
   1388   1.45.2.9   nathanw 	if (sce->cur < sce->fill)
   1389   1.45.2.9   nathanw 		kn->kn_data = sce->fill - sce->cur;
   1390   1.45.2.9   nathanw 	else
   1391   1.45.2.9   nathanw 		kn->kn_data = (sce->limit - sce->cur) +
   1392   1.45.2.9   nathanw 		    (sce->fill - sce->ibuf);
   1393   1.45.2.9   nathanw 
   1394   1.45.2.9   nathanw 	return (1);
   1395   1.45.2.9   nathanw }
   1396   1.45.2.9   nathanw 
   1397   1.45.2.9   nathanw static const struct filterops ugenread_intr_filtops =
   1398   1.45.2.9   nathanw 	{ 1, NULL, filt_ugenrdetach, filt_ugenread_intr };
   1399   1.45.2.9   nathanw 
   1400   1.45.2.9   nathanw static const struct filterops ugenread_isoc_filtops =
   1401   1.45.2.9   nathanw 	{ 1, NULL, filt_ugenrdetach, filt_ugenread_isoc };
   1402   1.45.2.9   nathanw 
   1403   1.45.2.9   nathanw static const struct filterops ugen_seltrue_filtops =
   1404   1.45.2.9   nathanw 	{ 1, NULL, filt_ugenrdetach, filt_seltrue };
   1405   1.45.2.9   nathanw 
   1406   1.45.2.9   nathanw int
   1407   1.45.2.9   nathanw ugenkqfilter(dev_t dev, struct knote *kn)
   1408   1.45.2.9   nathanw {
   1409   1.45.2.9   nathanw 	struct ugen_softc *sc;
   1410   1.45.2.9   nathanw 	struct ugen_endpoint *sce;
   1411   1.45.2.9   nathanw 	struct klist *klist;
   1412   1.45.2.9   nathanw 	int s;
   1413   1.45.2.9   nathanw 
   1414   1.45.2.9   nathanw 	USB_GET_SC(ugen, UGENUNIT(dev), sc);
   1415   1.45.2.9   nathanw 
   1416   1.45.2.9   nathanw 	if (sc->sc_dying)
   1417   1.45.2.9   nathanw 		return (1);
   1418   1.45.2.9   nathanw 
   1419   1.45.2.9   nathanw 	/* XXX always IN */
   1420   1.45.2.9   nathanw 	sce = &sc->sc_endpoints[UGENENDPOINT(dev)][IN];
   1421   1.45.2.9   nathanw 	if (sce == NULL)
   1422   1.45.2.9   nathanw 		return (1);
   1423   1.45.2.9   nathanw 
   1424   1.45.2.9   nathanw 	switch (kn->kn_filter) {
   1425   1.45.2.9   nathanw 	case EVFILT_READ:
   1426  1.45.2.10   thorpej 		klist = &sce->rsel.sel_klist;
   1427   1.45.2.9   nathanw 		switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
   1428   1.45.2.9   nathanw 		case UE_INTERRUPT:
   1429   1.45.2.9   nathanw 			kn->kn_fop = &ugenread_intr_filtops;
   1430   1.45.2.9   nathanw 			break;
   1431   1.45.2.9   nathanw 		case UE_ISOCHRONOUS:
   1432   1.45.2.9   nathanw 			kn->kn_fop = &ugenread_isoc_filtops;
   1433   1.45.2.9   nathanw 			break;
   1434   1.45.2.9   nathanw 		case UE_BULK:
   1435   1.45.2.9   nathanw 			/*
   1436   1.45.2.9   nathanw 			 * We have no easy way of determining if a read will
   1437   1.45.2.9   nathanw 			 * yield any data or a write will happen.
   1438   1.45.2.9   nathanw 			 * So, emulate "seltrue".
   1439   1.45.2.9   nathanw 			 */
   1440   1.45.2.9   nathanw 			kn->kn_fop = &ugen_seltrue_filtops;
   1441   1.45.2.9   nathanw 			break;
   1442   1.45.2.9   nathanw 		default:
   1443   1.45.2.9   nathanw 			return (1);
   1444   1.45.2.9   nathanw 		}
   1445   1.45.2.9   nathanw 		break;
   1446   1.45.2.9   nathanw 
   1447   1.45.2.9   nathanw 	case EVFILT_WRITE:
   1448  1.45.2.10   thorpej 		klist = &sce->rsel.sel_klist;
   1449   1.45.2.9   nathanw 		switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
   1450   1.45.2.9   nathanw 		case UE_INTERRUPT:
   1451   1.45.2.9   nathanw 		case UE_ISOCHRONOUS:
   1452   1.45.2.9   nathanw 			/* XXX poll doesn't support this */
   1453   1.45.2.9   nathanw 			return (1);
   1454   1.45.2.9   nathanw 
   1455   1.45.2.9   nathanw 		case UE_BULK:
   1456   1.45.2.9   nathanw 			/*
   1457   1.45.2.9   nathanw 			 * We have no easy way of determining if a read will
   1458   1.45.2.9   nathanw 			 * yield any data or a write will happen.
   1459   1.45.2.9   nathanw 			 * So, emulate "seltrue".
   1460   1.45.2.9   nathanw 			 */
   1461   1.45.2.9   nathanw 			kn->kn_fop = &ugen_seltrue_filtops;
   1462   1.45.2.9   nathanw 			break;
   1463   1.45.2.9   nathanw 		default:
   1464   1.45.2.9   nathanw 			return (1);
   1465   1.45.2.9   nathanw 		}
   1466   1.45.2.9   nathanw 		break;
   1467   1.45.2.9   nathanw 
   1468   1.45.2.9   nathanw 	default:
   1469   1.45.2.9   nathanw 		return (1);
   1470   1.45.2.9   nathanw 	}
   1471   1.45.2.9   nathanw 
   1472   1.45.2.9   nathanw 	kn->kn_hook = sce;
   1473   1.45.2.9   nathanw 
   1474   1.45.2.9   nathanw 	s = splusb();
   1475   1.45.2.9   nathanw 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
   1476   1.45.2.9   nathanw 	splx(s);
   1477   1.45.2.9   nathanw 
   1478   1.45.2.9   nathanw 	return (0);
   1479        1.1  augustss }
   1480        1.5  augustss 
   1481        1.5  augustss #if defined(__FreeBSD__)
   1482       1.30  augustss DRIVER_MODULE(ugen, uhub, ugen_driver, ugen_devclass, usbd_driver_load, 0);
   1483        1.5  augustss #endif
   1484