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