Home | History | Annotate | Line # | Download | only in usb
ugen.c revision 1.11.4.1
      1  1.11.4.1   thorpej /*	$NetBSD: ugen.c,v 1.11.4.1 1999/07/01 23:40:22 thorpej Exp $	*/
      2       1.1  augustss 
      3       1.1  augustss /*
      4       1.1  augustss  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5       1.1  augustss  * All rights reserved.
      6       1.1  augustss  *
      7       1.1  augustss  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1  augustss  * by Lennart Augustsson (augustss (at) carlstedt.se) at
      9       1.1  augustss  * Carlstedt Research & Technology.
     10       1.1  augustss  *
     11       1.1  augustss  * Redistribution and use in source and binary forms, with or without
     12       1.1  augustss  * modification, are permitted provided that the following conditions
     13       1.1  augustss  * are met:
     14       1.1  augustss  * 1. Redistributions of source code must retain the above copyright
     15       1.1  augustss  *    notice, this list of conditions and the following disclaimer.
     16       1.1  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     17       1.1  augustss  *    notice, this list of conditions and the following disclaimer in the
     18       1.1  augustss  *    documentation and/or other materials provided with the distribution.
     19       1.1  augustss  * 3. All advertising materials mentioning features or use of this software
     20       1.1  augustss  *    must display the following acknowledgement:
     21       1.1  augustss  *        This product includes software developed by the NetBSD
     22       1.1  augustss  *        Foundation, Inc. and its contributors.
     23       1.1  augustss  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24       1.1  augustss  *    contributors may be used to endorse or promote products derived
     25       1.1  augustss  *    from this software without specific prior written permission.
     26       1.1  augustss  *
     27       1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28       1.1  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29       1.1  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30       1.1  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31       1.1  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32       1.1  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33       1.1  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34       1.1  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35       1.1  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36       1.1  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37       1.1  augustss  * POSSIBILITY OF SUCH DAMAGE.
     38       1.1  augustss  */
     39       1.1  augustss 
     40       1.1  augustss 
     41       1.1  augustss #include <sys/param.h>
     42       1.1  augustss #include <sys/systm.h>
     43       1.1  augustss #include <sys/kernel.h>
     44       1.1  augustss #include <sys/malloc.h>
     45      1.11  augustss #if defined(__NetBSD__)
     46       1.1  augustss #include <sys/device.h>
     47       1.1  augustss #include <sys/ioctl.h>
     48      1.11  augustss #elif defined(__FreeBSD__)
     49      1.11  augustss #include <sys/module.h>
     50      1.11  augustss #include <sys/bus.h>
     51      1.11  augustss #include <sys/ioccom.h>
     52      1.11  augustss #include <sys/conf.h>
     53      1.11  augustss #include <sys/fcntl.h>
     54      1.11  augustss #include <sys/filio.h>
     55      1.11  augustss #endif
     56  1.11.4.1   thorpej #include <sys/conf.h>
     57       1.1  augustss #include <sys/tty.h>
     58       1.1  augustss #include <sys/file.h>
     59       1.1  augustss #include <sys/select.h>
     60       1.1  augustss #include <sys/proc.h>
     61       1.1  augustss #include <sys/vnode.h>
     62       1.1  augustss #include <sys/poll.h>
     63       1.1  augustss 
     64       1.1  augustss #include <dev/usb/usb.h>
     65       1.1  augustss #include <dev/usb/usbdi.h>
     66       1.1  augustss #include <dev/usb/usbdi_util.h>
     67       1.1  augustss 
     68       1.1  augustss #ifdef USB_DEBUG
     69       1.1  augustss #define DPRINTF(x)	if (ugendebug) printf x
     70       1.1  augustss #define DPRINTFN(n,x)	if (ugendebug>(n)) printf x
     71       1.2  augustss int	ugendebug = 0;
     72       1.1  augustss #else
     73       1.1  augustss #define DPRINTF(x)
     74       1.1  augustss #define DPRINTFN(n,x)
     75       1.1  augustss #endif
     76       1.1  augustss 
     77       1.1  augustss struct ugen_endpoint {
     78       1.1  augustss 	struct ugen_softc *sc;
     79       1.1  augustss 	usb_endpoint_descriptor_t *edesc;
     80       1.1  augustss 	usbd_interface_handle iface;
     81       1.1  augustss 	int state;
     82       1.1  augustss #define UGEN_OPEN	0x01	/* device is open */
     83       1.1  augustss #define	UGEN_ASLP	0x02	/* waiting for data */
     84       1.8  augustss #define UGEN_SHORT_OK	0x04	/* short xfers are OK */
     85       1.1  augustss 	usbd_pipe_handle pipeh;
     86       1.1  augustss 	struct clist q;
     87       1.1  augustss 	struct selinfo rsel;
     88       1.1  augustss 	void *ibuf;
     89       1.1  augustss };
     90       1.1  augustss 
     91       1.1  augustss #define	UGEN_CHUNK	128	/* chunk size for read */
     92       1.1  augustss #define	UGEN_IBSIZE	1020	/* buffer size */
     93       1.1  augustss #define	UGEN_BBSIZE	1024
     94       1.1  augustss 
     95       1.1  augustss struct ugen_softc {
     96      1.11  augustss 	bdevice sc_dev;		/* base device */
     97       1.1  augustss 	struct usbd_device *sc_udev;
     98       1.1  augustss 
     99       1.1  augustss 	struct ugen_endpoint sc_endpoints[USB_MAX_ENDPOINTS][2];
    100       1.1  augustss #define OUT 0			/* index order is important, from UE_OUT */
    101       1.1  augustss #define IN  1			/* from UE_IN */
    102       1.1  augustss 
    103  1.11.4.1   thorpej 	int sc_refcnt;
    104  1.11.4.1   thorpej 	u_char sc_dying;
    105       1.1  augustss };
    106       1.1  augustss 
    107       1.1  augustss int ugenopen __P((dev_t, int, int, struct proc *));
    108  1.11.4.1   thorpej int ugenclose __P((dev_t, int, int, struct proc *));
    109  1.11.4.1   thorpej int ugenread __P((dev_t, struct uio *, int));
    110  1.11.4.1   thorpej int ugenwrite __P((dev_t, struct uio *, int));
    111       1.1  augustss int ugenioctl __P((dev_t, u_long, caddr_t, int, struct proc *));
    112       1.1  augustss int ugenpoll __P((dev_t, int, struct proc *));
    113       1.3  augustss void ugenintr __P((usbd_request_handle reqh, usbd_private_handle addr,
    114       1.3  augustss 		   usbd_status status));
    115       1.1  augustss 
    116  1.11.4.1   thorpej int ugen_do_read __P((struct ugen_softc *, int, struct uio *, int));
    117  1.11.4.1   thorpej int ugen_do_write __P((struct ugen_softc *, int, struct uio *, int));
    118  1.11.4.1   thorpej int ugen_do_ioctl __P((struct ugen_softc *, int, u_long,
    119  1.11.4.1   thorpej 		       caddr_t, int, struct proc *));
    120       1.1  augustss int ugen_set_config __P((struct ugen_softc *sc, int configno));
    121       1.3  augustss usb_config_descriptor_t *ugen_get_cdesc __P((struct ugen_softc *sc, int index,
    122       1.3  augustss 					     int *lenp));
    123       1.1  augustss usbd_status ugen_set_interface __P((struct ugen_softc *, int, int));
    124       1.2  augustss int ugen_get_alt_index __P((struct ugen_softc *sc, int ifaceidx));
    125       1.1  augustss 
    126  1.11.4.1   thorpej #define UGENUNIT(n) ((minor(n) >> 4) & 0xf)
    127  1.11.4.1   thorpej #define UGENENDPOINT(n) (minor(n) & 0xf)
    128  1.11.4.1   thorpej #define UGENDEV(u, e) (makedev(0, ((u) << 4) | (e)))
    129       1.1  augustss 
    130       1.5  augustss USB_DECLARE_DRIVER(ugen);
    131       1.1  augustss 
    132       1.5  augustss USB_MATCH(ugen)
    133       1.1  augustss {
    134       1.5  augustss 	USB_MATCH_START(ugen, uaa);
    135       1.1  augustss 
    136       1.1  augustss 	if (uaa->usegeneric)
    137       1.1  augustss 		return (UMATCH_GENERIC);
    138       1.1  augustss 	else
    139       1.1  augustss 		return (UMATCH_NONE);
    140       1.1  augustss }
    141       1.1  augustss 
    142       1.5  augustss USB_ATTACH(ugen)
    143       1.1  augustss {
    144       1.5  augustss 	USB_ATTACH_START(ugen, sc, uaa);
    145       1.1  augustss 	char devinfo[1024];
    146       1.1  augustss 	usbd_status r;
    147       1.7  augustss 	int conf;
    148       1.1  augustss 
    149       1.1  augustss 	usbd_devinfo(uaa->device, 0, devinfo);
    150       1.5  augustss 	USB_ATTACH_SETUP;
    151       1.5  augustss 	printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
    152       1.1  augustss 
    153       1.1  augustss 	sc->sc_udev = uaa->device;
    154       1.7  augustss 	conf = 1;		/* XXX should not hard code 1 */
    155       1.7  augustss 	r = ugen_set_config(sc, conf);
    156       1.1  augustss 	if (r != USBD_NORMAL_COMPLETION) {
    157       1.7  augustss 		printf("%s: setting configuration %d failed\n",
    158       1.7  augustss 		       USBDEVNAME(sc->sc_dev), conf);
    159  1.11.4.1   thorpej 		sc->sc_dying = 1;
    160       1.5  augustss 		USB_ATTACH_ERROR_RETURN;
    161       1.1  augustss 	}
    162       1.5  augustss 	USB_ATTACH_SUCCESS_RETURN;
    163       1.1  augustss }
    164       1.1  augustss 
    165       1.1  augustss int
    166       1.1  augustss ugen_set_config(sc, configno)
    167       1.1  augustss 	struct ugen_softc *sc;
    168       1.1  augustss 	int configno;
    169       1.1  augustss {
    170       1.1  augustss 	usbd_device_handle dev = sc->sc_udev;
    171       1.1  augustss 	usbd_interface_handle iface;
    172       1.1  augustss 	usb_endpoint_descriptor_t *ed;
    173       1.1  augustss 	struct ugen_endpoint *sce;
    174       1.1  augustss 	u_int8_t niface, nendpt;
    175       1.1  augustss 	int ifaceno, endptno, endpt;
    176       1.1  augustss 	usbd_status r;
    177       1.1  augustss 
    178       1.1  augustss 	DPRINTFN(1,("ugen_set_config: %s to configno %d, sc=%p\n",
    179       1.5  augustss 		    USBDEVNAME(sc->sc_dev), configno, sc));
    180       1.7  augustss 	if (usbd_get_config_descriptor(dev)->bConfigurationValue != configno) {
    181       1.7  augustss 		/* Avoid setting the current value. */
    182       1.7  augustss 		r = usbd_set_config_no(dev, configno, 0);
    183       1.7  augustss 		if (r != USBD_NORMAL_COMPLETION)
    184       1.7  augustss 			return (r);
    185       1.7  augustss 	}
    186       1.1  augustss 
    187       1.1  augustss 	r = usbd_interface_count(dev, &niface);
    188       1.1  augustss 	if (r != USBD_NORMAL_COMPLETION)
    189       1.1  augustss 		return (r);
    190       1.1  augustss 	memset(sc->sc_endpoints, 0, sizeof sc->sc_endpoints);
    191       1.1  augustss 	for (ifaceno = 0; ifaceno < niface; ifaceno++) {
    192       1.1  augustss 		DPRINTFN(1,("ugen_set_config: ifaceno %d\n", ifaceno));
    193       1.1  augustss 		r = usbd_device2interface_handle(dev, ifaceno, &iface);
    194       1.1  augustss 		if (r != USBD_NORMAL_COMPLETION)
    195       1.1  augustss 			return (r);
    196       1.1  augustss 		r = usbd_endpoint_count(iface, &nendpt);
    197       1.1  augustss 		if (r != USBD_NORMAL_COMPLETION)
    198       1.1  augustss 			return (r);
    199       1.1  augustss 		for (endptno = 0; endptno < nendpt; endptno++) {
    200       1.1  augustss 			ed = usbd_interface2endpoint_descriptor(iface,endptno);
    201       1.1  augustss 			endpt = ed->bEndpointAddress;
    202       1.1  augustss 			sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)]
    203       1.1  augustss 				               [UE_GET_IN(endpt)];
    204       1.3  augustss 			DPRINTFN(1,("ugen_set_config: endptno %d, endpt=0x%02x"
    205       1.3  augustss 				    "(%d,%d), sce=%p\n",
    206       1.1  augustss 				    endptno, endpt, UE_GET_ADDR(endpt),
    207       1.1  augustss 				    UE_GET_IN(endpt), sce));
    208       1.1  augustss 			sce->sc = sc;
    209       1.1  augustss 			sce->edesc = ed;
    210       1.1  augustss 			sce->iface = iface;
    211       1.1  augustss 		}
    212       1.1  augustss 	}
    213       1.1  augustss 	return (USBD_NORMAL_COMPLETION);
    214       1.1  augustss }
    215       1.1  augustss 
    216       1.1  augustss int
    217       1.1  augustss ugenopen(dev, flag, mode, p)
    218       1.1  augustss 	dev_t dev;
    219       1.1  augustss 	int flag;
    220       1.1  augustss 	int mode;
    221       1.1  augustss 	struct proc *p;
    222       1.1  augustss {
    223       1.1  augustss 	int unit = UGENUNIT(dev);
    224       1.1  augustss 	int endpt = UGENENDPOINT(dev);
    225       1.1  augustss 	usb_endpoint_descriptor_t *edesc;
    226       1.1  augustss 	struct ugen_endpoint *sce;
    227       1.1  augustss 	int dir, isize;
    228       1.1  augustss 	usbd_status r;
    229       1.1  augustss 
    230      1.11  augustss 	USB_GET_SC_OPEN(ugen, unit, sc);
    231      1.11  augustss  	DPRINTFN(5, ("ugenopen: flag=%d, mode=%d, unit=%d endpt=%d\n",
    232       1.9  augustss 		     flag, mode, unit, endpt));
    233       1.1  augustss 
    234  1.11.4.1   thorpej 	if (sc->sc_dying)
    235  1.11.4.1   thorpej 		return (ENXIO);
    236       1.1  augustss 
    237       1.1  augustss 	if (endpt == USB_CONTROL_ENDPOINT) {
    238       1.1  augustss 		/*if ((flag & (FWRITE|FREAD)) != (FWRITE|FREAD))
    239       1.1  augustss 		  return (EACCES);*/
    240       1.1  augustss 		sce = &sc->sc_endpoints[USB_CONTROL_ENDPOINT][OUT];
    241       1.1  augustss 		if (sce->state & UGEN_OPEN)
    242       1.1  augustss 			return (EBUSY);
    243       1.1  augustss 	} else {
    244       1.1  augustss 		switch (flag & (FWRITE|FREAD)) {
    245       1.1  augustss 		case FWRITE:
    246       1.1  augustss 			dir = OUT;
    247       1.1  augustss 			break;
    248       1.1  augustss 		case FREAD:
    249       1.1  augustss 			dir = IN;
    250       1.1  augustss 			break;
    251       1.1  augustss 		default:
    252       1.1  augustss 			return (EACCES);
    253       1.1  augustss 		}
    254       1.1  augustss 		sce = &sc->sc_endpoints[endpt][dir];
    255       1.3  augustss 		DPRINTFN(5, ("ugenopen: sc=%p, endpt=%d, dir=%d, sce=%p\n",
    256       1.3  augustss 			     sc, endpt, dir, sce));
    257       1.1  augustss 		if (sce->state & UGEN_OPEN)
    258       1.1  augustss 			return (EBUSY);
    259       1.1  augustss 		edesc = sce->edesc;
    260       1.1  augustss 		if (!edesc)
    261       1.1  augustss 			return (ENXIO);
    262       1.1  augustss 		switch (edesc->bmAttributes & UE_XFERTYPE) {
    263       1.1  augustss 		case UE_INTERRUPT:
    264       1.1  augustss 			isize = UGETW(edesc->wMaxPacketSize);
    265       1.1  augustss 			if (isize == 0)	/* shouldn't happen */
    266       1.1  augustss 				return (EINVAL);
    267  1.11.4.1   thorpej 			sce->ibuf = malloc(isize, M_USBDEV, M_WAITOK);
    268       1.1  augustss 			DPRINTFN(5, ("ugenopen: intr endpt=%d,isize=%d\n",
    269       1.1  augustss 				     endpt, isize));
    270      1.11  augustss #if defined(__NetBSD__)
    271      1.11  augustss                         if (clalloc(&sce->q, UGEN_IBSIZE, 0) == -1)
    272      1.11  augustss                                 return (ENOMEM);
    273      1.11  augustss #elif defined(__FreeBSD__)
    274      1.11  augustss 			clist_alloc_cblocks(&sce->q, UGEN_IBSIZE, 0);
    275      1.11  augustss #endif
    276       1.1  augustss 			r = usbd_open_pipe_intr(sce->iface,
    277       1.1  augustss 				edesc->bEndpointAddress,
    278       1.1  augustss 				USBD_SHORT_XFER_OK, &sce->pipeh, sce,
    279       1.1  augustss 				sce->ibuf, isize, ugenintr);
    280       1.1  augustss 			if (r != USBD_NORMAL_COMPLETION) {
    281  1.11.4.1   thorpej 				free(sce->ibuf, M_USBDEV);
    282      1.11  augustss #if defined(__NetBSD__)
    283       1.1  augustss 				clfree(&sce->q);
    284      1.11  augustss #elif defined(__FreeBSD__)
    285      1.11  augustss 				clist_free_cblocks(&sce->q);
    286      1.11  augustss #endif
    287       1.1  augustss 				return (EIO);
    288       1.1  augustss 			}
    289       1.1  augustss 			DPRINTFN(5, ("ugenopen: interrupt open done\n"));
    290       1.1  augustss 			break;
    291       1.1  augustss 		case UE_BULK:
    292       1.1  augustss 			r = usbd_open_pipe(sce->iface,
    293       1.1  augustss 					   edesc->bEndpointAddress, 0,
    294       1.1  augustss 					   &sce->pipeh);
    295       1.1  augustss 			if (r != USBD_NORMAL_COMPLETION)
    296       1.1  augustss 				return (EIO);
    297       1.1  augustss 			break;
    298       1.1  augustss 		case UE_CONTROL:
    299       1.1  augustss 		case UE_ISOCHRONOUS:
    300       1.1  augustss 			return (EINVAL);
    301       1.1  augustss 		}
    302       1.1  augustss 	}
    303       1.1  augustss 	sce->state |= UGEN_OPEN;
    304       1.1  augustss 	return (0);
    305       1.1  augustss }
    306       1.1  augustss 
    307       1.1  augustss int
    308       1.1  augustss ugenclose(dev, flag, mode, p)
    309       1.1  augustss 	dev_t dev;
    310       1.1  augustss 	int flag;
    311       1.1  augustss 	int mode;
    312       1.1  augustss 	struct proc *p;
    313       1.1  augustss {
    314      1.11  augustss 	USB_GET_SC(ugen, UGENUNIT(dev), sc);
    315       1.1  augustss 	int endpt = UGENENDPOINT(dev);
    316       1.1  augustss 	struct ugen_endpoint *sce;
    317       1.1  augustss 	int dir;
    318       1.1  augustss 
    319       1.9  augustss 	DPRINTFN(5, ("ugenclose: flag=%d, mode=%d\n", flag, mode));
    320       1.1  augustss 
    321       1.1  augustss 	if (endpt == USB_CONTROL_ENDPOINT) {
    322       1.9  augustss 		DPRINTFN(5, ("ugenclose: close control\n"));
    323       1.8  augustss 		sc->sc_endpoints[endpt][OUT].state = 0;
    324       1.1  augustss 		return (0);
    325       1.1  augustss 	}
    326       1.9  augustss 
    327       1.9  augustss 	flag = FWRITE | FREAD;	/* XXX bug if generic open/close */
    328       1.9  augustss 
    329       1.9  augustss 	/* The open modes have been joined, so check for both modes. */
    330       1.9  augustss 	for (dir = OUT; dir <= IN; dir++) {
    331       1.9  augustss 		if (flag & (dir == OUT ? FWRITE : FREAD)) {
    332       1.9  augustss 			sce = &sc->sc_endpoints[endpt][dir];
    333  1.11.4.1   thorpej 			if (!sce || !sce->pipeh || sce->state == 0)
    334  1.11.4.1   thorpej 				continue;
    335       1.9  augustss 			DPRINTFN(5, ("ugenclose: endpt=%d dir=%d sce=%p\n",
    336       1.9  augustss 				     endpt, dir, sce));
    337       1.9  augustss 			sce->state = 0;
    338       1.9  augustss 
    339       1.9  augustss 			usbd_abort_pipe(sce->pipeh);
    340       1.9  augustss 			usbd_close_pipe(sce->pipeh);
    341       1.9  augustss 			sce->pipeh = 0;
    342       1.9  augustss 
    343       1.9  augustss 			if (sce->ibuf) {
    344  1.11.4.1   thorpej 				free(sce->ibuf, M_USBDEV);
    345       1.9  augustss 				sce->ibuf = 0;
    346  1.11.4.1   thorpej 				clfree(&sce->q);
    347       1.9  augustss 			}
    348       1.9  augustss 		}
    349       1.1  augustss 	}
    350       1.1  augustss 
    351       1.1  augustss 	return (0);
    352       1.1  augustss }
    353       1.1  augustss 
    354       1.1  augustss int
    355  1.11.4.1   thorpej ugen_do_read(sc, endpt, uio, flag)
    356  1.11.4.1   thorpej 	struct ugen_softc *sc;
    357  1.11.4.1   thorpej 	int endpt;
    358       1.1  augustss 	struct uio *uio;
    359       1.1  augustss 	int flag;
    360       1.1  augustss {
    361       1.1  augustss 	struct ugen_endpoint *sce = &sc->sc_endpoints[endpt][IN];
    362       1.9  augustss 	u_int32_t n, tn;
    363       1.1  augustss 	char buf[UGEN_BBSIZE];
    364       1.1  augustss 	usbd_request_handle reqh;
    365       1.1  augustss 	usbd_status r;
    366       1.1  augustss 	int s;
    367       1.1  augustss 	int error = 0;
    368       1.1  augustss 	u_char buffer[UGEN_CHUNK];
    369       1.1  augustss 
    370  1.11.4.1   thorpej 	DPRINTFN(5, ("ugenread: %d:%d\n", sc->sc_dev.dv_unit, endpt));
    371  1.11.4.1   thorpej 	if (sc->sc_dying)
    372       1.1  augustss 		return (EIO);
    373       1.1  augustss 
    374       1.1  augustss #ifdef DIAGNOSTIC
    375       1.1  augustss 	if (!sce->edesc) {
    376       1.1  augustss 		printf("ugenread: no edesc\n");
    377       1.1  augustss 		return (EIO);
    378       1.1  augustss 	}
    379       1.9  augustss 	if (!sce->pipeh) {
    380       1.9  augustss 		printf("ugenread: no pipe\n");
    381       1.9  augustss 		return (EIO);
    382       1.9  augustss 	}
    383       1.1  augustss #endif
    384       1.1  augustss 
    385       1.1  augustss 	switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
    386       1.1  augustss 	case UE_INTERRUPT:
    387       1.1  augustss 		/* Block until activity occured. */
    388       1.2  augustss 		s = splusb();
    389       1.1  augustss 		while (sce->q.c_cc == 0) {
    390       1.1  augustss 			if (flag & IO_NDELAY) {
    391       1.1  augustss 				splx(s);
    392       1.8  augustss 				return (EWOULDBLOCK);
    393       1.1  augustss 			}
    394       1.1  augustss 			sce->state |= UGEN_ASLP;
    395       1.1  augustss 			DPRINTFN(5, ("ugenread: sleep on %p\n", sc));
    396  1.11.4.1   thorpej 			error = tsleep(sce, PZERO | PCATCH, "ugenri", 0);
    397       1.1  augustss 			DPRINTFN(5, ("ugenread: woke, error=%d\n", error));
    398  1.11.4.1   thorpej 			if (sc->sc_dying)
    399  1.11.4.1   thorpej 				error = EIO;
    400       1.1  augustss 			if (error) {
    401       1.1  augustss 				sce->state &= ~UGEN_ASLP;
    402  1.11.4.1   thorpej 				break;
    403       1.1  augustss 			}
    404       1.1  augustss 		}
    405       1.1  augustss 		splx(s);
    406       1.1  augustss 
    407       1.1  augustss 		/* Transfer as many chunks as possible. */
    408  1.11.4.1   thorpej 		while (sce->q.c_cc > 0 && uio->uio_resid > 0 && !error) {
    409       1.1  augustss 			n = min(sce->q.c_cc, uio->uio_resid);
    410       1.1  augustss 			if (n > sizeof(buffer))
    411       1.1  augustss 				n = sizeof(buffer);
    412       1.1  augustss 
    413       1.1  augustss 			/* Remove a small chunk from the input queue. */
    414       1.1  augustss 			q_to_b(&sce->q, buffer, n);
    415       1.1  augustss 			DPRINTFN(5, ("ugenread: got %d chars\n", n));
    416       1.1  augustss 
    417       1.1  augustss 			/* Copy the data to the user process. */
    418       1.1  augustss 			error = uiomove(buffer, n, uio);
    419       1.1  augustss 			if (error)
    420       1.1  augustss 				break;
    421       1.1  augustss 		}
    422       1.1  augustss 		break;
    423       1.1  augustss 	case UE_BULK:
    424       1.1  augustss 		reqh = usbd_alloc_request();
    425       1.1  augustss 		if (reqh == 0)
    426       1.1  augustss 			return (ENOMEM);
    427       1.1  augustss 		while ((n = min(UGEN_BBSIZE, uio->uio_resid)) != 0) {
    428      1.10  augustss 			DPRINTFN(1, ("ugenread: start transfer %d bytes\n",n));
    429      1.10  augustss 			tn = n;
    430       1.8  augustss 			r = usbd_bulk_transfer(reqh, sce->pipeh, 0, buf,
    431       1.9  augustss 					       &tn, "ugenrb");
    432       1.1  augustss 			if (r != USBD_NORMAL_COMPLETION) {
    433       1.8  augustss 				if (r == USBD_INTERRUPTED)
    434       1.8  augustss 					error = EINTR;
    435       1.8  augustss 				else
    436       1.8  augustss 					error = EIO;
    437       1.1  augustss 				break;
    438       1.1  augustss 			}
    439      1.10  augustss 			DPRINTFN(1, ("ugenread: got %d bytes\n", tn));
    440       1.9  augustss 			error = uiomove(buf, tn, uio);
    441       1.9  augustss 			if (error || tn < n)
    442       1.1  augustss 				break;
    443       1.1  augustss 		}
    444       1.1  augustss 		usbd_free_request(reqh);
    445       1.1  augustss 		break;
    446       1.1  augustss 	default:
    447       1.1  augustss 		return (ENXIO);
    448       1.1  augustss 	}
    449       1.1  augustss 	return (error);
    450       1.1  augustss }
    451       1.1  augustss 
    452       1.1  augustss int
    453  1.11.4.1   thorpej ugenread(dev, uio, flag)
    454       1.1  augustss 	dev_t dev;
    455       1.1  augustss 	struct uio *uio;
    456       1.1  augustss 	int flag;
    457       1.1  augustss {
    458      1.11  augustss 	USB_GET_SC(ugen, UGENUNIT(dev), sc);
    459       1.1  augustss 	int endpt = UGENENDPOINT(dev);
    460  1.11.4.1   thorpej 	int error;
    461  1.11.4.1   thorpej 
    462  1.11.4.1   thorpej 	sc->sc_refcnt++;
    463  1.11.4.1   thorpej 	error = ugen_do_read(sc, endpt, uio, flag);
    464  1.11.4.1   thorpej 	if (--sc->sc_refcnt < 0)
    465  1.11.4.1   thorpej 		usb_detach_wakeup(&sc->sc_dev);
    466  1.11.4.1   thorpej 	return (error);
    467  1.11.4.1   thorpej }
    468  1.11.4.1   thorpej 
    469  1.11.4.1   thorpej int
    470  1.11.4.1   thorpej ugen_do_write(sc, endpt, uio, flag)
    471  1.11.4.1   thorpej 	struct ugen_softc *sc;
    472  1.11.4.1   thorpej 	int endpt;
    473  1.11.4.1   thorpej 	struct uio *uio;
    474  1.11.4.1   thorpej 	int flag;
    475  1.11.4.1   thorpej {
    476      1.10  augustss 	struct ugen_endpoint *sce = &sc->sc_endpoints[endpt][OUT];
    477       1.1  augustss 	size_t n;
    478       1.1  augustss 	int error = 0;
    479       1.1  augustss 	char buf[UGEN_BBSIZE];
    480       1.1  augustss 	usbd_request_handle reqh;
    481       1.1  augustss 	usbd_status r;
    482       1.1  augustss 
    483  1.11.4.1   thorpej 	if (sc->sc_dying)
    484       1.1  augustss 		return (EIO);
    485       1.1  augustss 
    486       1.1  augustss #ifdef DIAGNOSTIC
    487       1.1  augustss 	if (!sce->edesc) {
    488       1.1  augustss 		printf("ugenwrite: no edesc\n");
    489       1.1  augustss 		return (EIO);
    490       1.1  augustss 	}
    491       1.9  augustss 	if (!sce->pipeh) {
    492       1.9  augustss 		printf("ugenwrite: no pipe\n");
    493       1.9  augustss 		return (EIO);
    494       1.9  augustss 	}
    495       1.1  augustss #endif
    496       1.1  augustss 
    497       1.1  augustss 	DPRINTF(("ugenwrite\n"));
    498       1.1  augustss 	switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
    499       1.1  augustss 	case UE_BULK:
    500       1.1  augustss 		reqh = usbd_alloc_request();
    501       1.1  augustss 		if (reqh == 0)
    502       1.1  augustss 			return (EIO);
    503       1.1  augustss 		while ((n = min(UGEN_BBSIZE, uio->uio_resid)) != 0) {
    504       1.1  augustss 			error = uiomove(buf, n, uio);
    505       1.1  augustss 			if (error)
    506       1.1  augustss 				break;
    507       1.1  augustss 			DPRINTFN(1, ("ugenwrite: transfer %d bytes\n", n));
    508       1.8  augustss 			r = usbd_bulk_transfer(reqh, sce->pipeh, 0, buf,
    509       1.8  augustss 					       &n, "ugenwb");
    510       1.1  augustss 			if (r != USBD_NORMAL_COMPLETION) {
    511       1.8  augustss 				if (r == USBD_INTERRUPTED)
    512       1.8  augustss 					error = EINTR;
    513       1.8  augustss 				else
    514       1.8  augustss 					error = EIO;
    515       1.1  augustss 				break;
    516       1.1  augustss 			}
    517       1.1  augustss 		}
    518       1.1  augustss 		usbd_free_request(reqh);
    519       1.1  augustss 		break;
    520       1.1  augustss 	default:
    521       1.1  augustss 		return (ENXIO);
    522       1.1  augustss 	}
    523       1.1  augustss 	return (error);
    524       1.1  augustss }
    525       1.1  augustss 
    526  1.11.4.1   thorpej int
    527  1.11.4.1   thorpej ugenwrite(dev, uio, flag)
    528  1.11.4.1   thorpej 	dev_t dev;
    529  1.11.4.1   thorpej 	struct uio *uio;
    530  1.11.4.1   thorpej 	int flag;
    531  1.11.4.1   thorpej {
    532  1.11.4.1   thorpej 	USB_GET_SC(ugen, UGENUNIT(dev), sc);
    533  1.11.4.1   thorpej 	int endpt = UGENENDPOINT(dev);
    534  1.11.4.1   thorpej 	int error;
    535  1.11.4.1   thorpej 
    536  1.11.4.1   thorpej 	sc->sc_refcnt++;
    537  1.11.4.1   thorpej 	error = ugen_do_write(sc, endpt, uio, flag);
    538  1.11.4.1   thorpej 	if (--sc->sc_refcnt < 0)
    539  1.11.4.1   thorpej 		usb_detach_wakeup(&sc->sc_dev);
    540  1.11.4.1   thorpej 	return (error);
    541  1.11.4.1   thorpej }
    542  1.11.4.1   thorpej 
    543  1.11.4.1   thorpej int
    544  1.11.4.1   thorpej ugen_activate(self, act)
    545  1.11.4.1   thorpej 	struct device *self;
    546  1.11.4.1   thorpej 	enum devact act;
    547  1.11.4.1   thorpej {
    548  1.11.4.1   thorpej 	return (0);
    549  1.11.4.1   thorpej }
    550  1.11.4.1   thorpej 
    551  1.11.4.1   thorpej int
    552  1.11.4.1   thorpej ugen_detach(self, flags)
    553  1.11.4.1   thorpej 	struct device  *self;
    554  1.11.4.1   thorpej 	int flags;
    555  1.11.4.1   thorpej {
    556  1.11.4.1   thorpej 	struct ugen_softc *sc = (struct ugen_softc *)self;
    557  1.11.4.1   thorpej 	struct ugen_endpoint *sce;
    558  1.11.4.1   thorpej 	int maj, mn;
    559  1.11.4.1   thorpej 	int i, dir;
    560  1.11.4.1   thorpej 	int s;
    561  1.11.4.1   thorpej 
    562  1.11.4.1   thorpej 	DPRINTF(("ugen_detach: sc=%p flags=%d\n", sc, flags));
    563  1.11.4.1   thorpej 
    564  1.11.4.1   thorpej 	sc->sc_dying = 1;
    565  1.11.4.1   thorpej 	/* Abort all pipes.  Causes processes waiting for transfer to wake. */
    566  1.11.4.1   thorpej 	for (i = 0; i < USB_MAX_ENDPOINTS; i++) {
    567  1.11.4.1   thorpej 		for (dir = OUT; dir <= IN; dir++) {
    568  1.11.4.1   thorpej 			sce = &sc->sc_endpoints[i][dir];
    569  1.11.4.1   thorpej 			if (sce && sce->pipeh)
    570  1.11.4.1   thorpej 				usbd_abort_pipe(sce->pipeh);
    571  1.11.4.1   thorpej 		}
    572  1.11.4.1   thorpej 	}
    573  1.11.4.1   thorpej 
    574  1.11.4.1   thorpej 	s = splusb();
    575  1.11.4.1   thorpej 	if (--sc->sc_refcnt >= 0) {
    576  1.11.4.1   thorpej 		/* Wake everyone */
    577  1.11.4.1   thorpej 		for (i = 0; i < USB_MAX_ENDPOINTS; i++)
    578  1.11.4.1   thorpej 			wakeup(&sc->sc_endpoints[i][IN]);
    579  1.11.4.1   thorpej 		/* Wait for processes to go away. */
    580  1.11.4.1   thorpej 		usb_detach_wait(&sc->sc_dev);
    581  1.11.4.1   thorpej 	}
    582  1.11.4.1   thorpej 	splx(s);
    583  1.11.4.1   thorpej 
    584  1.11.4.1   thorpej 	/* locate the major number */
    585  1.11.4.1   thorpej 	for (maj = 0; maj < nchrdev; maj++)
    586  1.11.4.1   thorpej 		if (cdevsw[maj].d_open == ugenopen)
    587  1.11.4.1   thorpej 			break;
    588  1.11.4.1   thorpej 
    589  1.11.4.1   thorpej 	/* Nuke the vnodes for any open instances (calls close). */
    590  1.11.4.1   thorpej 	mn = self->dv_unit * USB_MAX_ENDPOINTS;
    591  1.11.4.1   thorpej 	vdevgone(maj, mn, mn + USB_MAX_ENDPOINTS - 1, VCHR);
    592  1.11.4.1   thorpej 
    593  1.11.4.1   thorpej 	return (0);
    594  1.11.4.1   thorpej }
    595       1.1  augustss 
    596       1.1  augustss void
    597       1.1  augustss ugenintr(reqh, addr, status)
    598       1.1  augustss 	usbd_request_handle reqh;
    599       1.1  augustss 	usbd_private_handle addr;
    600       1.1  augustss 	usbd_status status;
    601       1.1  augustss {
    602       1.1  augustss 	struct ugen_endpoint *sce = addr;
    603       1.1  augustss 	/*struct ugen_softc *sc = sce->sc;*/
    604       1.8  augustss 	usbd_private_handle priv;
    605       1.8  augustss 	void *buffer;
    606       1.8  augustss 	u_int32_t count;
    607       1.8  augustss 	usbd_status xstatus;
    608       1.1  augustss 	u_char *ibuf;
    609       1.1  augustss 
    610       1.1  augustss 	if (status == USBD_CANCELLED)
    611       1.1  augustss 		return;
    612       1.1  augustss 
    613       1.1  augustss 	if (status != USBD_NORMAL_COMPLETION) {
    614       1.1  augustss 		DPRINTF(("ugenintr: status=%d\n", status));
    615       1.1  augustss 		usbd_clear_endpoint_stall_async(sce->pipeh);
    616       1.1  augustss 		return;
    617       1.1  augustss 	}
    618       1.1  augustss 
    619       1.8  augustss 	(void)usbd_get_request_status(reqh, &priv, &buffer, &count, &xstatus);
    620       1.1  augustss 	ibuf = sce->ibuf;
    621       1.1  augustss 
    622       1.8  augustss 	DPRINTFN(5, ("ugenintr: reqh=%p status=%d count=%d\n",
    623       1.8  augustss 		     reqh, xstatus, count));
    624       1.1  augustss 	DPRINTFN(5, ("          data = %02x %02x %02x\n",
    625       1.8  augustss 		     ibuf[0], ibuf[1], ibuf[2]));
    626       1.1  augustss 
    627       1.8  augustss 	(void)b_to_q(ibuf, count, &sce->q);
    628       1.1  augustss 
    629       1.1  augustss 	if (sce->state & UGEN_ASLP) {
    630       1.1  augustss 		sce->state &= ~UGEN_ASLP;
    631       1.1  augustss 		DPRINTFN(5, ("ugen_intr: waking %p\n", sce));
    632  1.11.4.1   thorpej 		wakeup(sce);
    633       1.1  augustss 	}
    634       1.1  augustss 	selwakeup(&sce->rsel);
    635       1.1  augustss }
    636       1.1  augustss 
    637       1.1  augustss usbd_status
    638       1.1  augustss ugen_set_interface(sc, ifaceidx, altno)
    639       1.1  augustss 	struct ugen_softc *sc;
    640       1.1  augustss 	int ifaceidx, altno;
    641       1.1  augustss {
    642       1.1  augustss 	usbd_interface_handle iface;
    643       1.1  augustss 	usb_endpoint_descriptor_t *ed;
    644       1.1  augustss 	usbd_status r;
    645       1.1  augustss 	struct ugen_endpoint *sce;
    646       1.1  augustss 	u_int8_t niface, nendpt, endptno, endpt;
    647       1.1  augustss 
    648       1.1  augustss 	DPRINTFN(15, ("ugen_set_interface %d %d\n", ifaceidx, altno));
    649       1.1  augustss 
    650       1.1  augustss 	r = usbd_interface_count(sc->sc_udev, &niface);
    651       1.1  augustss 	if (r != USBD_NORMAL_COMPLETION)
    652       1.1  augustss 		return (r);
    653       1.1  augustss 	if (ifaceidx < 0 || ifaceidx >= niface)
    654       1.1  augustss 		return (USBD_INVAL);
    655       1.1  augustss 
    656       1.1  augustss 	r = usbd_device2interface_handle(sc->sc_udev, ifaceidx, &iface);
    657       1.1  augustss 	if (r != USBD_NORMAL_COMPLETION)
    658       1.1  augustss 		return (r);
    659       1.1  augustss 	r = usbd_endpoint_count(iface, &nendpt);
    660       1.1  augustss 	if (r != USBD_NORMAL_COMPLETION)
    661       1.1  augustss 		return (r);
    662       1.1  augustss 	for (endptno = 0; endptno < nendpt; endptno++) {
    663       1.1  augustss 		ed = usbd_interface2endpoint_descriptor(iface,endptno);
    664       1.1  augustss 		endpt = ed->bEndpointAddress;
    665       1.1  augustss 		sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][UE_GET_IN(endpt)];
    666       1.1  augustss 		sce->sc = 0;
    667       1.1  augustss 		sce->edesc = 0;
    668       1.1  augustss 		sce->iface = 0;
    669       1.1  augustss 	}
    670       1.1  augustss 
    671       1.1  augustss 	/* change setting */
    672       1.1  augustss 	r = usbd_set_interface(iface, altno);
    673       1.1  augustss 	if (r != USBD_NORMAL_COMPLETION)
    674       1.1  augustss 		return (r);
    675       1.1  augustss 
    676       1.1  augustss 	r = usbd_endpoint_count(iface, &nendpt);
    677       1.1  augustss 	if (r != USBD_NORMAL_COMPLETION)
    678       1.1  augustss 		return (r);
    679       1.1  augustss 	for (endptno = 0; endptno < nendpt; endptno++) {
    680       1.1  augustss 		ed = usbd_interface2endpoint_descriptor(iface,endptno);
    681       1.1  augustss 		endpt = ed->bEndpointAddress;
    682       1.1  augustss 		sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][UE_GET_IN(endpt)];
    683       1.1  augustss 		sce->sc = sc;
    684       1.1  augustss 		sce->edesc = ed;
    685       1.1  augustss 		sce->iface = iface;
    686       1.1  augustss 	}
    687       1.1  augustss 	return (0);
    688       1.1  augustss }
    689       1.1  augustss 
    690       1.1  augustss /* Retrieve a complete descriptor for a certain device and index. */
    691       1.1  augustss usb_config_descriptor_t *
    692       1.1  augustss ugen_get_cdesc(sc, index, lenp)
    693       1.1  augustss 	struct ugen_softc *sc;
    694       1.1  augustss 	int index;
    695       1.1  augustss 	int *lenp;
    696       1.1  augustss {
    697       1.1  augustss 	usb_config_descriptor_t *cdesc, *tdesc, cdescr;
    698       1.1  augustss 	int len;
    699       1.1  augustss 	usbd_status r;
    700       1.1  augustss 
    701       1.1  augustss 	if (index == USB_CURRENT_CONFIG_INDEX) {
    702       1.1  augustss 		tdesc = usbd_get_config_descriptor(sc->sc_udev);
    703       1.1  augustss 		len = UGETW(tdesc->wTotalLength);
    704       1.1  augustss 		if (lenp)
    705       1.1  augustss 			*lenp = len;
    706       1.1  augustss 		cdesc = malloc(len, M_TEMP, M_WAITOK);
    707       1.1  augustss 		memcpy(cdesc, tdesc, len);
    708       1.1  augustss 		DPRINTFN(5,("ugen_get_cdesc: current, len=%d\n", len));
    709       1.1  augustss 	} else {
    710       1.1  augustss 		r = usbd_get_config_desc(sc->sc_udev, index, &cdescr);
    711       1.1  augustss 		if (r != USBD_NORMAL_COMPLETION)
    712       1.1  augustss 			return (0);
    713       1.1  augustss 		len = UGETW(cdescr.wTotalLength);
    714       1.1  augustss 		DPRINTFN(5,("ugen_get_cdesc: index=%d, len=%d\n", index, len));
    715       1.1  augustss 		if (lenp)
    716       1.1  augustss 			*lenp = len;
    717       1.1  augustss 		cdesc = malloc(len, M_TEMP, M_WAITOK);
    718       1.1  augustss 		r = usbd_get_config_desc_full(sc->sc_udev, index, cdesc, len);
    719       1.1  augustss 		if (r != USBD_NORMAL_COMPLETION) {
    720       1.1  augustss 			free(cdesc, M_TEMP);
    721       1.1  augustss 			return (0);
    722       1.1  augustss 		}
    723       1.1  augustss 	}
    724       1.1  augustss 	return (cdesc);
    725       1.1  augustss }
    726       1.1  augustss 
    727       1.1  augustss int
    728       1.2  augustss ugen_get_alt_index(sc, ifaceidx)
    729       1.1  augustss 	struct ugen_softc *sc;
    730       1.1  augustss 	int ifaceidx;
    731       1.1  augustss {
    732       1.1  augustss 	usbd_interface_handle iface;
    733       1.1  augustss 	usbd_status r;
    734       1.1  augustss 
    735       1.1  augustss 	r = usbd_device2interface_handle(sc->sc_udev, ifaceidx, &iface);
    736       1.1  augustss 	if (r != USBD_NORMAL_COMPLETION)
    737       1.1  augustss 			return (-1);
    738       1.2  augustss 	return (usbd_get_interface_altindex(iface));
    739       1.1  augustss }
    740       1.1  augustss 
    741       1.1  augustss int
    742  1.11.4.1   thorpej ugen_do_ioctl(sc, endpt, cmd, addr, flag, p)
    743  1.11.4.1   thorpej 	struct ugen_softc *sc;
    744  1.11.4.1   thorpej 	int endpt;
    745       1.1  augustss 	u_long cmd;
    746       1.1  augustss 	caddr_t addr;
    747       1.1  augustss 	int flag;
    748       1.1  augustss 	struct proc *p;
    749       1.1  augustss {
    750       1.8  augustss 	struct ugen_endpoint *sce;
    751       1.1  augustss 	usbd_status r;
    752       1.1  augustss 	usbd_interface_handle iface;
    753       1.1  augustss 	struct usb_config_desc *cd;
    754       1.1  augustss 	usb_config_descriptor_t *cdesc;
    755       1.1  augustss 	struct usb_interface_desc *id;
    756       1.1  augustss 	usb_interface_descriptor_t *idesc;
    757       1.1  augustss 	struct usb_endpoint_desc *ed;
    758       1.1  augustss 	usb_endpoint_descriptor_t *edesc;
    759       1.1  augustss 	struct usb_alt_interface *ai;
    760       1.2  augustss 	struct usb_string_desc *si;
    761       1.1  augustss 	u_int8_t conf, alt;
    762       1.1  augustss 
    763       1.1  augustss 	DPRINTFN(5, ("ugenioctl: cmd=%08lx\n", cmd));
    764  1.11.4.1   thorpej 	if (sc->sc_dying)
    765       1.1  augustss 		return (EIO);
    766       1.1  augustss 
    767       1.2  augustss 	switch (cmd) {
    768       1.2  augustss 	case FIONBIO:
    769       1.2  augustss 		/* All handled in the upper FS layer. */
    770       1.2  augustss 		return (0);
    771       1.8  augustss 	case USB_SET_SHORT_XFER:
    772       1.8  augustss 		/* This flag only affects read */
    773       1.8  augustss 		sce = &sc->sc_endpoints[endpt][IN];
    774       1.9  augustss #ifdef DIAGNOSTIC
    775       1.9  augustss 		if (!sce->pipeh) {
    776       1.9  augustss 			printf("ugenioctl: no pipe\n");
    777       1.9  augustss 			return (EIO);
    778       1.9  augustss 		}
    779       1.9  augustss #endif
    780       1.8  augustss 		if (*(int *)addr)
    781       1.8  augustss 			sce->state |= UGEN_SHORT_OK;
    782       1.8  augustss 		else
    783       1.8  augustss 			sce->state &= ~UGEN_SHORT_OK;
    784       1.8  augustss 		return (0);
    785       1.2  augustss 	default:
    786       1.2  augustss 		break;
    787       1.2  augustss 	}
    788       1.2  augustss 
    789       1.1  augustss 	if (endpt != USB_CONTROL_ENDPOINT)
    790       1.1  augustss 		return (EINVAL);
    791       1.1  augustss 
    792       1.1  augustss 	switch (cmd) {
    793       1.8  augustss #ifdef USB_DEBUG
    794       1.8  augustss 	case USB_SETDEBUG:
    795       1.8  augustss 		ugendebug = *(int *)addr;
    796       1.8  augustss 		break;
    797       1.8  augustss #endif
    798       1.1  augustss 	case USB_GET_CONFIG:
    799       1.1  augustss 		r = usbd_get_config(sc->sc_udev, &conf);
    800       1.1  augustss 		if (r != USBD_NORMAL_COMPLETION)
    801       1.1  augustss 			return (EIO);
    802       1.1  augustss 		*(int *)addr = conf;
    803       1.1  augustss 		break;
    804       1.1  augustss 	case USB_SET_CONFIG:
    805       1.2  augustss 		if (!(flag & FWRITE))
    806       1.2  augustss 			return (EPERM);
    807       1.5  augustss 		r = ugen_set_config(sc, *(int *)addr);
    808       1.1  augustss 		if (r != USBD_NORMAL_COMPLETION)
    809       1.1  augustss 			return (EIO);
    810       1.1  augustss 		break;
    811       1.1  augustss 	case USB_GET_ALTINTERFACE:
    812       1.1  augustss 		ai = (struct usb_alt_interface *)addr;
    813       1.1  augustss 		r = usbd_device2interface_handle(sc->sc_udev,
    814       1.1  augustss 						 ai->interface_index, &iface);
    815       1.1  augustss 		if (r != USBD_NORMAL_COMPLETION)
    816       1.1  augustss 			return (EINVAL);
    817       1.1  augustss 		idesc = usbd_get_interface_descriptor(iface);
    818       1.2  augustss 		if (!idesc)
    819       1.2  augustss 			return (EIO);
    820       1.1  augustss 		ai->alt_no = idesc->bAlternateSetting;
    821       1.1  augustss 		break;
    822       1.1  augustss 	case USB_SET_ALTINTERFACE:
    823       1.2  augustss 		if (!(flag & FWRITE))
    824       1.2  augustss 			return (EPERM);
    825       1.1  augustss 		ai = (struct usb_alt_interface *)addr;
    826       1.1  augustss 		r = usbd_device2interface_handle(sc->sc_udev,
    827       1.1  augustss 						 ai->interface_index, &iface);
    828       1.1  augustss 		if (r != USBD_NORMAL_COMPLETION)
    829       1.1  augustss 			return (EINVAL);
    830       1.1  augustss 		r = ugen_set_interface(sc, ai->interface_index, ai->alt_no);
    831       1.1  augustss 		if (r != USBD_NORMAL_COMPLETION)
    832       1.1  augustss 			return (EINVAL);
    833       1.1  augustss 		break;
    834       1.1  augustss 	case USB_GET_NO_ALT:
    835       1.1  augustss 		ai = (struct usb_alt_interface *)addr;
    836       1.2  augustss 		cdesc = ugen_get_cdesc(sc, ai->config_index, 0);
    837       1.2  augustss 		if (!cdesc)
    838       1.2  augustss 			return (EINVAL);
    839       1.2  augustss 		idesc = usbd_find_idesc(cdesc, ai->interface_index, 0);
    840  1.11.4.1   thorpej 		if (!idesc) {
    841  1.11.4.1   thorpej 			free(cdesc, M_TEMP);
    842       1.1  augustss 			return (EINVAL);
    843  1.11.4.1   thorpej 		}
    844       1.2  augustss 		ai->alt_no = usbd_get_no_alts(cdesc, idesc->bInterfaceNumber);
    845  1.11.4.1   thorpej 		free(cdesc, M_TEMP);
    846       1.1  augustss 		break;
    847       1.1  augustss 	case USB_GET_DEVICE_DESC:
    848       1.1  augustss 		*(usb_device_descriptor_t *)addr =
    849       1.1  augustss 			*usbd_get_device_descriptor(sc->sc_udev);
    850       1.1  augustss 		break;
    851       1.1  augustss 	case USB_GET_CONFIG_DESC:
    852       1.1  augustss 		cd = (struct usb_config_desc *)addr;
    853       1.1  augustss 		cdesc = ugen_get_cdesc(sc, cd->config_index, 0);
    854       1.1  augustss 		if (!cdesc)
    855       1.1  augustss 			return (EINVAL);
    856       1.1  augustss 		cd->desc = *cdesc;
    857       1.1  augustss 		free(cdesc, M_TEMP);
    858       1.1  augustss 		break;
    859       1.1  augustss 	case USB_GET_INTERFACE_DESC:
    860       1.1  augustss 		id = (struct usb_interface_desc *)addr;
    861       1.1  augustss 		cdesc = ugen_get_cdesc(sc, id->config_index, 0);
    862       1.1  augustss 		if (!cdesc)
    863       1.1  augustss 			return (EINVAL);
    864       1.1  augustss 		if (id->config_index == USB_CURRENT_CONFIG_INDEX &&
    865       1.1  augustss 		    id->alt_index == USB_CURRENT_ALT_INDEX)
    866       1.2  augustss 			alt = ugen_get_alt_index(sc, id->interface_index);
    867       1.1  augustss 		else
    868       1.1  augustss 			alt = id->alt_index;
    869       1.1  augustss 		idesc = usbd_find_idesc(cdesc, id->interface_index, alt);
    870       1.1  augustss 		if (!idesc) {
    871       1.1  augustss 			free(cdesc, M_TEMP);
    872       1.1  augustss 			return (EINVAL);
    873       1.1  augustss 		}
    874       1.1  augustss 		id->desc = *idesc;
    875       1.1  augustss 		free(cdesc, M_TEMP);
    876       1.1  augustss 		break;
    877       1.1  augustss 	case USB_GET_ENDPOINT_DESC:
    878       1.1  augustss 		ed = (struct usb_endpoint_desc *)addr;
    879       1.1  augustss 		cdesc = ugen_get_cdesc(sc, ed->config_index, 0);
    880       1.1  augustss 		if (!cdesc)
    881       1.1  augustss 			return (EINVAL);
    882       1.1  augustss 		if (ed->config_index == USB_CURRENT_CONFIG_INDEX &&
    883       1.1  augustss 		    ed->alt_index == USB_CURRENT_ALT_INDEX)
    884       1.2  augustss 			alt = ugen_get_alt_index(sc, ed->interface_index);
    885       1.1  augustss 		else
    886       1.1  augustss 			alt = ed->alt_index;
    887       1.1  augustss 		edesc = usbd_find_edesc(cdesc, ed->interface_index,
    888       1.1  augustss 					alt, ed->endpoint_index);
    889       1.1  augustss 		if (!edesc) {
    890       1.1  augustss 			free(cdesc, M_TEMP);
    891       1.1  augustss 			return (EINVAL);
    892       1.1  augustss 		}
    893       1.1  augustss 		ed->desc = *edesc;
    894       1.1  augustss 		free(cdesc, M_TEMP);
    895       1.1  augustss 		break;
    896       1.1  augustss 	case USB_GET_FULL_DESC:
    897       1.1  augustss 	{
    898       1.1  augustss 		int len;
    899       1.1  augustss 		struct iovec iov;
    900       1.1  augustss 		struct uio uio;
    901       1.1  augustss 		struct usb_full_desc *fd = (struct usb_full_desc *)addr;
    902       1.1  augustss 		int error;
    903       1.1  augustss 
    904       1.1  augustss 		cdesc = ugen_get_cdesc(sc, fd->config_index, &len);
    905       1.1  augustss 		if (len > fd->size)
    906       1.1  augustss 			len = fd->size;
    907       1.1  augustss 		iov.iov_base = (caddr_t)fd->data;
    908       1.1  augustss 		iov.iov_len = len;
    909       1.1  augustss 		uio.uio_iov = &iov;
    910       1.1  augustss 		uio.uio_iovcnt = 1;
    911       1.1  augustss 		uio.uio_resid = len;
    912       1.1  augustss 		uio.uio_offset = 0;
    913       1.1  augustss 		uio.uio_segflg = UIO_USERSPACE;
    914       1.1  augustss 		uio.uio_rw = UIO_READ;
    915       1.1  augustss 		uio.uio_procp = p;
    916       1.1  augustss 		error = uiomove(cdesc, len, &uio);
    917       1.1  augustss 		free(cdesc, M_TEMP);
    918       1.1  augustss 		return (error);
    919       1.1  augustss 	}
    920       1.2  augustss 	case USB_GET_STRING_DESC:
    921       1.2  augustss 		si = (struct usb_string_desc *)addr;
    922       1.3  augustss 		r = usbd_get_string_desc(sc->sc_udev, si->string_index,
    923       1.3  augustss 					 si->language_id, &si->desc);
    924       1.2  augustss 		if (r != USBD_NORMAL_COMPLETION)
    925       1.2  augustss 			return (EINVAL);
    926       1.2  augustss 		break;
    927       1.1  augustss 	case USB_DO_REQUEST:
    928       1.1  augustss 	{
    929       1.1  augustss 		struct usb_ctl_request *ur = (void *)addr;
    930       1.1  augustss 		int len = UGETW(ur->request.wLength);
    931       1.1  augustss 		struct iovec iov;
    932       1.1  augustss 		struct uio uio;
    933       1.1  augustss 		void *ptr = 0;
    934       1.1  augustss 		usbd_status r;
    935       1.1  augustss 		int error = 0;
    936       1.1  augustss 
    937       1.2  augustss 		if (!(flag & FWRITE))
    938       1.2  augustss 			return (EPERM);
    939       1.1  augustss 		/* Avoid requests that would damage the bus integrity. */
    940       1.1  augustss 		if ((ur->request.bmRequestType == UT_WRITE_DEVICE &&
    941       1.1  augustss 		     ur->request.bRequest == UR_SET_ADDRESS) ||
    942       1.1  augustss 		    (ur->request.bmRequestType == UT_WRITE_DEVICE &&
    943       1.1  augustss 		     ur->request.bRequest == UR_SET_CONFIG) ||
    944       1.1  augustss 		    (ur->request.bmRequestType == UT_WRITE_INTERFACE &&
    945       1.1  augustss 		     ur->request.bRequest == UR_SET_INTERFACE))
    946       1.1  augustss 			return (EINVAL);
    947       1.1  augustss 
    948       1.1  augustss 		if (len < 0 || len > 32767)
    949       1.1  augustss 			return (EINVAL);
    950       1.1  augustss 		if (len != 0) {
    951       1.1  augustss 			iov.iov_base = (caddr_t)ur->data;
    952       1.1  augustss 			iov.iov_len = len;
    953       1.1  augustss 			uio.uio_iov = &iov;
    954       1.1  augustss 			uio.uio_iovcnt = 1;
    955       1.1  augustss 			uio.uio_resid = len;
    956       1.1  augustss 			uio.uio_offset = 0;
    957       1.1  augustss 			uio.uio_segflg = UIO_USERSPACE;
    958       1.1  augustss 			uio.uio_rw =
    959       1.1  augustss 				ur->request.bmRequestType & UT_READ ?
    960       1.1  augustss 				UIO_READ : UIO_WRITE;
    961       1.1  augustss 			uio.uio_procp = p;
    962       1.1  augustss 			ptr = malloc(len, M_TEMP, M_WAITOK);
    963       1.1  augustss 			if (uio.uio_rw == UIO_WRITE) {
    964       1.1  augustss 				error = uiomove(ptr, len, &uio);
    965       1.1  augustss 				if (error)
    966       1.1  augustss 					goto ret;
    967       1.1  augustss 			}
    968       1.1  augustss 		}
    969       1.6  augustss 		r = usbd_do_request_flags(sc->sc_udev, &ur->request,
    970       1.9  augustss 					  ptr, ur->flags, &ur->actlen);
    971  1.11.4.1   thorpej 		if (r != USBD_NORMAL_COMPLETION) {
    972       1.1  augustss 			error = EIO;
    973       1.1  augustss 			goto ret;
    974       1.1  augustss 		}
    975       1.1  augustss 		if (len != 0) {
    976       1.1  augustss 			if (uio.uio_rw == UIO_READ) {
    977       1.1  augustss 				error = uiomove(ptr, len, &uio);
    978       1.1  augustss 				if (error)
    979       1.1  augustss 					goto ret;
    980       1.1  augustss 			}
    981       1.1  augustss 		}
    982       1.1  augustss 	ret:
    983       1.1  augustss 		if (ptr)
    984       1.1  augustss 			free(ptr, M_TEMP);
    985       1.1  augustss 		return (error);
    986       1.1  augustss 	}
    987       1.2  augustss 	case USB_GET_DEVICEINFO:
    988       1.2  augustss 		usbd_fill_deviceinfo(sc->sc_udev,
    989       1.2  augustss 				     (struct usb_device_info *)addr);
    990       1.2  augustss 		break;
    991       1.1  augustss 	default:
    992       1.1  augustss 		return (EINVAL);
    993       1.1  augustss 	}
    994       1.1  augustss 	return (0);
    995       1.1  augustss }
    996       1.1  augustss 
    997       1.1  augustss int
    998  1.11.4.1   thorpej ugenioctl(dev, cmd, addr, flag, p)
    999  1.11.4.1   thorpej 	dev_t dev;
   1000  1.11.4.1   thorpej 	u_long cmd;
   1001  1.11.4.1   thorpej 	caddr_t addr;
   1002  1.11.4.1   thorpej 	int flag;
   1003  1.11.4.1   thorpej 	struct proc *p;
   1004  1.11.4.1   thorpej {
   1005  1.11.4.1   thorpej 	USB_GET_SC(ugen, UGENUNIT(dev), sc);
   1006  1.11.4.1   thorpej 	int endpt = UGENENDPOINT(dev);
   1007  1.11.4.1   thorpej 	int error;
   1008  1.11.4.1   thorpej 
   1009  1.11.4.1   thorpej 	sc->sc_refcnt++;
   1010  1.11.4.1   thorpej 	error = ugen_do_ioctl(sc, endpt, cmd, addr, flag, p);
   1011  1.11.4.1   thorpej 	if (--sc->sc_refcnt < 0)
   1012  1.11.4.1   thorpej 		usb_detach_wakeup(&sc->sc_dev);
   1013  1.11.4.1   thorpej 	return (error);
   1014  1.11.4.1   thorpej }
   1015  1.11.4.1   thorpej 
   1016  1.11.4.1   thorpej int
   1017       1.1  augustss ugenpoll(dev, events, p)
   1018       1.1  augustss 	dev_t dev;
   1019       1.1  augustss 	int events;
   1020       1.1  augustss 	struct proc *p;
   1021       1.1  augustss {
   1022      1.11  augustss 	USB_GET_SC(ugen, UGENUNIT(dev), sc);
   1023       1.2  augustss 	struct ugen_endpoint *sce;
   1024       1.1  augustss 	int revents = 0;
   1025       1.1  augustss 	int s;
   1026       1.1  augustss 
   1027  1.11.4.1   thorpej 	if (sc->sc_dying)
   1028       1.1  augustss 		return (EIO);
   1029       1.1  augustss 
   1030       1.4  augustss 	sce = &sc->sc_endpoints[UGENENDPOINT(dev)][IN];
   1031       1.9  augustss #ifdef DIAGNOSTIC
   1032       1.9  augustss 	if (!sce->edesc) {
   1033       1.9  augustss 		printf("ugenwrite: no edesc\n");
   1034       1.9  augustss 		return (EIO);
   1035       1.9  augustss 	}
   1036       1.9  augustss 	if (!sce->pipeh) {
   1037       1.9  augustss 		printf("ugenpoll: no pipe\n");
   1038       1.9  augustss 		return (EIO);
   1039       1.9  augustss 	}
   1040       1.9  augustss #endif
   1041       1.2  augustss 	s = splusb();
   1042       1.2  augustss 	switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
   1043       1.1  augustss 	case UE_INTERRUPT:
   1044       1.1  augustss 		if (events & (POLLIN | POLLRDNORM)) {
   1045       1.1  augustss 			if (sce->q.c_cc > 0)
   1046       1.1  augustss 				revents |= events & (POLLIN | POLLRDNORM);
   1047       1.1  augustss 			else
   1048       1.1  augustss 				selrecord(p, &sce->rsel);
   1049       1.1  augustss 		}
   1050       1.2  augustss 		break;
   1051       1.2  augustss 	case UE_BULK:
   1052       1.2  augustss 		/*
   1053       1.2  augustss 		 * We have no easy way of determining if a read will
   1054       1.2  augustss 		 * yield any data or a write will happen.
   1055       1.2  augustss 		 * Pretend they will.
   1056       1.2  augustss 		 */
   1057       1.2  augustss 		revents |= events &
   1058       1.2  augustss 			   (POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM);
   1059       1.2  augustss 		break;
   1060       1.2  augustss 	default:
   1061       1.1  augustss 		break;
   1062       1.1  augustss 	}
   1063       1.1  augustss 	splx(s);
   1064       1.1  augustss 	return (revents);
   1065       1.1  augustss }
   1066       1.5  augustss 
   1067       1.5  augustss #if defined(__FreeBSD__)
   1068      1.11  augustss DRIVER_MODULE(ugen, usb, ugen_driver, ugen_devclass, usbd_driver_load, 0);
   1069       1.5  augustss #endif
   1070