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