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