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