xref: /freebsd/sys/dev/usb/usb_generic.c (revision 40a8ac8f62b535d30349faf28cf47106b7041b83)
1 /* $FreeBSD$ */
2 /*-
3  * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #ifdef USB_GLOBAL_INCLUDE_FILE
28 #include USB_GLOBAL_INCLUDE_FILE
29 #else
30 #include <sys/stdint.h>
31 #include <sys/stddef.h>
32 #include <sys/param.h>
33 #include <sys/queue.h>
34 #include <sys/types.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/bus.h>
38 #include <sys/module.h>
39 #include <sys/lock.h>
40 #include <sys/mutex.h>
41 #include <sys/condvar.h>
42 #include <sys/sysctl.h>
43 #include <sys/sx.h>
44 #include <sys/unistd.h>
45 #include <sys/callout.h>
46 #include <sys/malloc.h>
47 #include <sys/priv.h>
48 #include <sys/conf.h>
49 #include <sys/fcntl.h>
50 
51 #include <dev/usb/usb.h>
52 #include <dev/usb/usb_ioctl.h>
53 #include <dev/usb/usbdi.h>
54 #include <dev/usb/usbdi_util.h>
55 
56 #define	USB_DEBUG_VAR ugen_debug
57 
58 #include <dev/usb/usb_core.h>
59 #include <dev/usb/usb_dev.h>
60 #include <dev/usb/usb_mbuf.h>
61 #include <dev/usb/usb_process.h>
62 #include <dev/usb/usb_device.h>
63 #include <dev/usb/usb_debug.h>
64 #include <dev/usb/usb_request.h>
65 #include <dev/usb/usb_busdma.h>
66 #include <dev/usb/usb_util.h>
67 #include <dev/usb/usb_hub.h>
68 #include <dev/usb/usb_generic.h>
69 #include <dev/usb/usb_transfer.h>
70 
71 #include <dev/usb/usb_controller.h>
72 #include <dev/usb/usb_bus.h>
73 #endif			/* USB_GLOBAL_INCLUDE_FILE */
74 
75 #if USB_HAVE_UGEN
76 
77 /* defines */
78 
79 #define	UGEN_BULK_FS_BUFFER_SIZE	(64*32)	/* bytes */
80 #define	UGEN_BULK_HS_BUFFER_SIZE	(1024*32)	/* bytes */
81 #define	UGEN_HW_FRAMES	50		/* number of milliseconds per transfer */
82 
83 /* function prototypes */
84 
85 static usb_callback_t ugen_read_clear_stall_callback;
86 static usb_callback_t ugen_write_clear_stall_callback;
87 static usb_callback_t ugen_ctrl_read_callback;
88 static usb_callback_t ugen_ctrl_write_callback;
89 static usb_callback_t ugen_isoc_read_callback;
90 static usb_callback_t ugen_isoc_write_callback;
91 static usb_callback_t ugen_ctrl_fs_callback;
92 
93 static usb_fifo_open_t ugen_open;
94 static usb_fifo_close_t ugen_close;
95 static usb_fifo_ioctl_t ugen_ioctl;
96 static usb_fifo_ioctl_t ugen_ioctl_post;
97 static usb_fifo_cmd_t ugen_start_read;
98 static usb_fifo_cmd_t ugen_start_write;
99 static usb_fifo_cmd_t ugen_stop_io;
100 
101 static int	ugen_transfer_setup(struct usb_fifo *,
102 		     const struct usb_config *, uint8_t);
103 static int	ugen_open_pipe_write(struct usb_fifo *);
104 static int	ugen_open_pipe_read(struct usb_fifo *);
105 static int	ugen_set_config(struct usb_fifo *, uint8_t);
106 static int	ugen_set_interface(struct usb_fifo *, uint8_t, uint8_t);
107 static int	ugen_get_cdesc(struct usb_fifo *, struct usb_gen_descriptor *);
108 static int	ugen_get_sdesc(struct usb_fifo *, struct usb_gen_descriptor *);
109 static int	ugen_get_iface_driver(struct usb_fifo *f, struct usb_gen_descriptor *ugd);
110 static int	usb_gen_fill_deviceinfo(struct usb_fifo *,
111 		    struct usb_device_info *);
112 static int	ugen_re_enumerate(struct usb_fifo *);
113 static int	ugen_iface_ioctl(struct usb_fifo *, u_long, void *, int);
114 static uint8_t	ugen_fs_get_complete(struct usb_fifo *, uint8_t *);
115 static int	ugen_fs_uninit(struct usb_fifo *f);
116 
117 /* structures */
118 
119 struct usb_fifo_methods usb_ugen_methods = {
120 	.f_open = &ugen_open,
121 	.f_close = &ugen_close,
122 	.f_ioctl = &ugen_ioctl,
123 	.f_ioctl_post = &ugen_ioctl_post,
124 	.f_start_read = &ugen_start_read,
125 	.f_stop_read = &ugen_stop_io,
126 	.f_start_write = &ugen_start_write,
127 	.f_stop_write = &ugen_stop_io,
128 };
129 
130 #ifdef USB_DEBUG
131 static int ugen_debug = 0;
132 
133 static SYSCTL_NODE(_hw_usb, OID_AUTO, ugen, CTLFLAG_RW, 0, "USB generic");
134 SYSCTL_INT(_hw_usb_ugen, OID_AUTO, debug, CTLFLAG_RWTUN, &ugen_debug,
135     0, "Debug level");
136 #endif
137 
138 
139 /* prototypes */
140 
141 static int
142 ugen_transfer_setup(struct usb_fifo *f,
143     const struct usb_config *setup, uint8_t n_setup)
144 {
145 	struct usb_endpoint *ep = usb_fifo_softc(f);
146 	struct usb_device *udev = f->udev;
147 	uint8_t iface_index = ep->iface_index;
148 	int error;
149 
150 	mtx_unlock(f->priv_mtx);
151 
152 	/*
153 	 * "usbd_transfer_setup()" can sleep so one needs to make a wrapper,
154 	 * exiting the mutex and checking things
155 	 */
156 	error = usbd_transfer_setup(udev, &iface_index, f->xfer,
157 	    setup, n_setup, f, f->priv_mtx);
158 	if (error == 0) {
159 
160 		if (f->xfer[0]->nframes == 1) {
161 			error = usb_fifo_alloc_buffer(f,
162 			    f->xfer[0]->max_data_length, 2);
163 		} else {
164 			error = usb_fifo_alloc_buffer(f,
165 			    f->xfer[0]->max_frame_size,
166 			    2 * f->xfer[0]->nframes);
167 		}
168 		if (error) {
169 			usbd_transfer_unsetup(f->xfer, n_setup);
170 		}
171 	}
172 	mtx_lock(f->priv_mtx);
173 
174 	return (error);
175 }
176 
177 static int
178 ugen_open(struct usb_fifo *f, int fflags)
179 {
180 	struct usb_endpoint *ep = usb_fifo_softc(f);
181 	struct usb_endpoint_descriptor *ed = ep->edesc;
182 	uint8_t type;
183 
184 	DPRINTFN(6, "flag=0x%x\n", fflags);
185 
186 	mtx_lock(f->priv_mtx);
187 	switch (usbd_get_speed(f->udev)) {
188 	case USB_SPEED_LOW:
189 	case USB_SPEED_FULL:
190 		f->nframes = UGEN_HW_FRAMES;
191 		f->bufsize = UGEN_BULK_FS_BUFFER_SIZE;
192 		break;
193 	default:
194 		f->nframes = UGEN_HW_FRAMES * 8;
195 		f->bufsize = UGEN_BULK_HS_BUFFER_SIZE;
196 		break;
197 	}
198 
199 	type = ed->bmAttributes & UE_XFERTYPE;
200 	if (type == UE_INTERRUPT) {
201 		f->bufsize = 0;		/* use "wMaxPacketSize" */
202 	}
203 	f->timeout = USB_NO_TIMEOUT;
204 	f->flag_short = 0;
205 	f->fifo_zlp = 0;
206 	mtx_unlock(f->priv_mtx);
207 
208 	return (0);
209 }
210 
211 static void
212 ugen_close(struct usb_fifo *f, int fflags)
213 {
214 	DPRINTFN(6, "flag=0x%x\n", fflags);
215 
216 	/* cleanup */
217 
218 	mtx_lock(f->priv_mtx);
219 	usbd_transfer_stop(f->xfer[0]);
220 	usbd_transfer_stop(f->xfer[1]);
221 	mtx_unlock(f->priv_mtx);
222 
223 	usbd_transfer_unsetup(f->xfer, 2);
224 	usb_fifo_free_buffer(f);
225 
226 	if (ugen_fs_uninit(f)) {
227 		/* ignore any errors - we are closing */
228 		DPRINTFN(6, "no FIFOs\n");
229 	}
230 }
231 
232 static int
233 ugen_open_pipe_write(struct usb_fifo *f)
234 {
235 	struct usb_config usb_config[2];
236 	struct usb_endpoint *ep = usb_fifo_softc(f);
237 	struct usb_endpoint_descriptor *ed = ep->edesc;
238 
239 	mtx_assert(f->priv_mtx, MA_OWNED);
240 
241 	if (f->xfer[0] || f->xfer[1]) {
242 		/* transfers are already opened */
243 		return (0);
244 	}
245 	memset(usb_config, 0, sizeof(usb_config));
246 
247 	usb_config[1].type = UE_CONTROL;
248 	usb_config[1].endpoint = 0;
249 	usb_config[1].direction = UE_DIR_ANY;
250 	usb_config[1].timeout = 1000;	/* 1 second */
251 	usb_config[1].interval = 50;/* 50 milliseconds */
252 	usb_config[1].bufsize = sizeof(struct usb_device_request);
253 	usb_config[1].callback = &ugen_write_clear_stall_callback;
254 	usb_config[1].usb_mode = USB_MODE_HOST;
255 
256 	usb_config[0].type = ed->bmAttributes & UE_XFERTYPE;
257 	usb_config[0].endpoint = ed->bEndpointAddress & UE_ADDR;
258 	usb_config[0].stream_id = 0;	/* XXX support more stream ID's */
259 	usb_config[0].direction = UE_DIR_TX;
260 	usb_config[0].interval = USB_DEFAULT_INTERVAL;
261 	usb_config[0].flags.proxy_buffer = 1;
262 	usb_config[0].usb_mode = USB_MODE_DUAL;	/* both modes */
263 
264 	switch (ed->bmAttributes & UE_XFERTYPE) {
265 	case UE_INTERRUPT:
266 	case UE_BULK:
267 		if (f->flag_short) {
268 			usb_config[0].flags.force_short_xfer = 1;
269 		}
270 		usb_config[0].callback = &ugen_ctrl_write_callback;
271 		usb_config[0].timeout = f->timeout;
272 		usb_config[0].frames = 1;
273 		usb_config[0].bufsize = f->bufsize;
274 		if (ugen_transfer_setup(f, usb_config, 2)) {
275 			return (EIO);
276 		}
277 		/* first transfer does not clear stall */
278 		f->flag_stall = 0;
279 		break;
280 
281 	case UE_ISOCHRONOUS:
282 		usb_config[0].flags.short_xfer_ok = 1;
283 		usb_config[0].bufsize = 0;	/* use default */
284 		usb_config[0].frames = f->nframes;
285 		usb_config[0].callback = &ugen_isoc_write_callback;
286 		usb_config[0].timeout = 0;
287 
288 		/* clone configuration */
289 		usb_config[1] = usb_config[0];
290 
291 		if (ugen_transfer_setup(f, usb_config, 2)) {
292 			return (EIO);
293 		}
294 		break;
295 	default:
296 		return (EINVAL);
297 	}
298 	return (0);
299 }
300 
301 static int
302 ugen_open_pipe_read(struct usb_fifo *f)
303 {
304 	struct usb_config usb_config[2];
305 	struct usb_endpoint *ep = usb_fifo_softc(f);
306 	struct usb_endpoint_descriptor *ed = ep->edesc;
307 
308 	mtx_assert(f->priv_mtx, MA_OWNED);
309 
310 	if (f->xfer[0] || f->xfer[1]) {
311 		/* transfers are already opened */
312 		return (0);
313 	}
314 	memset(usb_config, 0, sizeof(usb_config));
315 
316 	usb_config[1].type = UE_CONTROL;
317 	usb_config[1].endpoint = 0;
318 	usb_config[1].direction = UE_DIR_ANY;
319 	usb_config[1].timeout = 1000;	/* 1 second */
320 	usb_config[1].interval = 50;/* 50 milliseconds */
321 	usb_config[1].bufsize = sizeof(struct usb_device_request);
322 	usb_config[1].callback = &ugen_read_clear_stall_callback;
323 	usb_config[1].usb_mode = USB_MODE_HOST;
324 
325 	usb_config[0].type = ed->bmAttributes & UE_XFERTYPE;
326 	usb_config[0].endpoint = ed->bEndpointAddress & UE_ADDR;
327 	usb_config[0].stream_id = 0;	/* XXX support more stream ID's */
328 	usb_config[0].direction = UE_DIR_RX;
329 	usb_config[0].interval = USB_DEFAULT_INTERVAL;
330 	usb_config[0].flags.proxy_buffer = 1;
331 	usb_config[0].usb_mode = USB_MODE_DUAL;	/* both modes */
332 
333 	switch (ed->bmAttributes & UE_XFERTYPE) {
334 	case UE_INTERRUPT:
335 	case UE_BULK:
336 		if (f->flag_short) {
337 			usb_config[0].flags.short_xfer_ok = 1;
338 		}
339 		usb_config[0].timeout = f->timeout;
340 		usb_config[0].frames = 1;
341 		usb_config[0].callback = &ugen_ctrl_read_callback;
342 		usb_config[0].bufsize = f->bufsize;
343 
344 		if (ugen_transfer_setup(f, usb_config, 2)) {
345 			return (EIO);
346 		}
347 		/* first transfer does not clear stall */
348 		f->flag_stall = 0;
349 		break;
350 
351 	case UE_ISOCHRONOUS:
352 		usb_config[0].flags.short_xfer_ok = 1;
353 		usb_config[0].bufsize = 0;	/* use default */
354 		usb_config[0].frames = f->nframes;
355 		usb_config[0].callback = &ugen_isoc_read_callback;
356 		usb_config[0].timeout = 0;
357 
358 		/* clone configuration */
359 		usb_config[1] = usb_config[0];
360 
361 		if (ugen_transfer_setup(f, usb_config, 2)) {
362 			return (EIO);
363 		}
364 		break;
365 
366 	default:
367 		return (EINVAL);
368 	}
369 	return (0);
370 }
371 
372 static void
373 ugen_start_read(struct usb_fifo *f)
374 {
375 	/* check that pipes are open */
376 	if (ugen_open_pipe_read(f)) {
377 		/* signal error */
378 		usb_fifo_put_data_error(f);
379 	}
380 	/* start transfers */
381 	usbd_transfer_start(f->xfer[0]);
382 	usbd_transfer_start(f->xfer[1]);
383 }
384 
385 static void
386 ugen_start_write(struct usb_fifo *f)
387 {
388 	/* check that pipes are open */
389 	if (ugen_open_pipe_write(f)) {
390 		/* signal error */
391 		usb_fifo_get_data_error(f);
392 	}
393 	/* start transfers */
394 	usbd_transfer_start(f->xfer[0]);
395 	usbd_transfer_start(f->xfer[1]);
396 }
397 
398 static void
399 ugen_stop_io(struct usb_fifo *f)
400 {
401 	/* stop transfers */
402 	usbd_transfer_stop(f->xfer[0]);
403 	usbd_transfer_stop(f->xfer[1]);
404 }
405 
406 static void
407 ugen_ctrl_read_callback(struct usb_xfer *xfer, usb_error_t error)
408 {
409 	struct usb_fifo *f = usbd_xfer_softc(xfer);
410 	struct usb_mbuf *m;
411 
412 	DPRINTFN(4, "actlen=%u, aframes=%u\n", xfer->actlen, xfer->aframes);
413 
414 	switch (USB_GET_STATE(xfer)) {
415 	case USB_ST_TRANSFERRED:
416 		if (xfer->actlen == 0) {
417 			if (f->fifo_zlp != 4) {
418 				f->fifo_zlp++;
419 			} else {
420 				/*
421 				 * Throttle a little bit we have multiple ZLPs
422 				 * in a row!
423 				 */
424 				xfer->interval = 64;	/* ms */
425 			}
426 		} else {
427 			/* clear throttle */
428 			xfer->interval = 0;
429 			f->fifo_zlp = 0;
430 		}
431 		usb_fifo_put_data(f, xfer->frbuffers, 0,
432 		    xfer->actlen, 1);
433 
434 	case USB_ST_SETUP:
435 		if (f->flag_stall) {
436 			usbd_transfer_start(f->xfer[1]);
437 			break;
438 		}
439 		USB_IF_POLL(&f->free_q, m);
440 		if (m) {
441 			usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
442 			usbd_transfer_submit(xfer);
443 		}
444 		break;
445 
446 	default:			/* Error */
447 		if (xfer->error != USB_ERR_CANCELLED) {
448 			/* send a zero length packet to userland */
449 			usb_fifo_put_data(f, xfer->frbuffers, 0, 0, 1);
450 			f->flag_stall = 1;
451 			f->fifo_zlp = 0;
452 			usbd_transfer_start(f->xfer[1]);
453 		}
454 		break;
455 	}
456 }
457 
458 static void
459 ugen_ctrl_write_callback(struct usb_xfer *xfer, usb_error_t error)
460 {
461 	struct usb_fifo *f = usbd_xfer_softc(xfer);
462 	usb_frlength_t actlen;
463 
464 	DPRINTFN(4, "actlen=%u, aframes=%u\n", xfer->actlen, xfer->aframes);
465 
466 	switch (USB_GET_STATE(xfer)) {
467 	case USB_ST_SETUP:
468 	case USB_ST_TRANSFERRED:
469 		/*
470 		 * If writing is in stall, just jump to clear stall
471 		 * callback and solve the situation.
472 		 */
473 		if (f->flag_stall) {
474 			usbd_transfer_start(f->xfer[1]);
475 			break;
476 		}
477 		/*
478 		 * Write data, setup and perform hardware transfer.
479 		 */
480 		if (usb_fifo_get_data(f, xfer->frbuffers, 0,
481 		    xfer->max_data_length, &actlen, 0)) {
482 			usbd_xfer_set_frame_len(xfer, 0, actlen);
483 			usbd_transfer_submit(xfer);
484 		}
485 		break;
486 
487 	default:			/* Error */
488 		if (xfer->error != USB_ERR_CANCELLED) {
489 			f->flag_stall = 1;
490 			usbd_transfer_start(f->xfer[1]);
491 		}
492 		break;
493 	}
494 }
495 
496 static void
497 ugen_read_clear_stall_callback(struct usb_xfer *xfer, usb_error_t error)
498 {
499 	struct usb_fifo *f = usbd_xfer_softc(xfer);
500 	struct usb_xfer *xfer_other = f->xfer[0];
501 
502 	if (f->flag_stall == 0) {
503 		/* nothing to do */
504 		return;
505 	}
506 	if (usbd_clear_stall_callback(xfer, xfer_other)) {
507 		DPRINTFN(5, "f=%p: stall cleared\n", f);
508 		f->flag_stall = 0;
509 		usbd_transfer_start(xfer_other);
510 	}
511 }
512 
513 static void
514 ugen_write_clear_stall_callback(struct usb_xfer *xfer, usb_error_t error)
515 {
516 	struct usb_fifo *f = usbd_xfer_softc(xfer);
517 	struct usb_xfer *xfer_other = f->xfer[0];
518 
519 	if (f->flag_stall == 0) {
520 		/* nothing to do */
521 		return;
522 	}
523 	if (usbd_clear_stall_callback(xfer, xfer_other)) {
524 		DPRINTFN(5, "f=%p: stall cleared\n", f);
525 		f->flag_stall = 0;
526 		usbd_transfer_start(xfer_other);
527 	}
528 }
529 
530 static void
531 ugen_isoc_read_callback(struct usb_xfer *xfer, usb_error_t error)
532 {
533 	struct usb_fifo *f = usbd_xfer_softc(xfer);
534 	usb_frlength_t offset;
535 	usb_frcount_t n;
536 
537 	DPRINTFN(4, "actlen=%u, aframes=%u\n", xfer->actlen, xfer->aframes);
538 
539 	switch (USB_GET_STATE(xfer)) {
540 	case USB_ST_TRANSFERRED:
541 
542 		DPRINTFN(6, "actlen=%d\n", xfer->actlen);
543 
544 		offset = 0;
545 
546 		for (n = 0; n != xfer->aframes; n++) {
547 			usb_fifo_put_data(f, xfer->frbuffers, offset,
548 			    xfer->frlengths[n], 1);
549 			offset += xfer->max_frame_size;
550 		}
551 
552 	case USB_ST_SETUP:
553 tr_setup:
554 		for (n = 0; n != xfer->nframes; n++) {
555 			/* setup size for next transfer */
556 			usbd_xfer_set_frame_len(xfer, n, xfer->max_frame_size);
557 		}
558 		usbd_transfer_submit(xfer);
559 		break;
560 
561 	default:			/* Error */
562 		if (xfer->error == USB_ERR_CANCELLED) {
563 			break;
564 		}
565 		goto tr_setup;
566 	}
567 }
568 
569 static void
570 ugen_isoc_write_callback(struct usb_xfer *xfer, usb_error_t error)
571 {
572 	struct usb_fifo *f = usbd_xfer_softc(xfer);
573 	usb_frlength_t actlen;
574 	usb_frlength_t offset;
575 	usb_frcount_t n;
576 
577 	DPRINTFN(4, "actlen=%u, aframes=%u\n", xfer->actlen, xfer->aframes);
578 
579 	switch (USB_GET_STATE(xfer)) {
580 	case USB_ST_TRANSFERRED:
581 	case USB_ST_SETUP:
582 tr_setup:
583 		offset = 0;
584 		for (n = 0; n != xfer->nframes; n++) {
585 			if (usb_fifo_get_data(f, xfer->frbuffers, offset,
586 			    xfer->max_frame_size, &actlen, 1)) {
587 				usbd_xfer_set_frame_len(xfer, n, actlen);
588 				offset += actlen;
589 			} else {
590 				break;
591 			}
592 		}
593 
594 		for (; n != xfer->nframes; n++) {
595 			/* fill in zero frames */
596 			usbd_xfer_set_frame_len(xfer, n, 0);
597 		}
598 		usbd_transfer_submit(xfer);
599 		break;
600 
601 	default:			/* Error */
602 		if (xfer->error == USB_ERR_CANCELLED) {
603 			break;
604 		}
605 		goto tr_setup;
606 	}
607 }
608 
609 static int
610 ugen_set_config(struct usb_fifo *f, uint8_t index)
611 {
612 	DPRINTFN(2, "index %u\n", index);
613 
614 	if (f->udev->flags.usb_mode != USB_MODE_HOST) {
615 		/* not possible in device side mode */
616 		return (ENOTTY);
617 	}
618 
619 	/* make sure all FIFO's are gone */
620 	/* else there can be a deadlock */
621 	if (ugen_fs_uninit(f)) {
622 		/* ignore any errors */
623 		DPRINTFN(6, "no FIFOs\n");
624 	}
625 
626 	if (usbd_start_set_config(f->udev, index) != 0)
627 		return (EIO);
628 
629 	return (0);
630 }
631 
632 static int
633 ugen_set_interface(struct usb_fifo *f,
634     uint8_t iface_index, uint8_t alt_index)
635 {
636 	DPRINTFN(2, "%u, %u\n", iface_index, alt_index);
637 
638 	if (f->udev->flags.usb_mode != USB_MODE_HOST) {
639 		/* not possible in device side mode */
640 		return (ENOTTY);
641 	}
642 	/* make sure all FIFO's are gone */
643 	/* else there can be a deadlock */
644 	if (ugen_fs_uninit(f)) {
645 		/* ignore any errors */
646 		DPRINTFN(6, "no FIFOs\n");
647 	}
648 	/* change setting - will free generic FIFOs, if any */
649 	if (usbd_set_alt_interface_index(f->udev, iface_index, alt_index)) {
650 		return (EIO);
651 	}
652 	/* probe and attach */
653 	if (usb_probe_and_attach(f->udev, iface_index)) {
654 		return (EIO);
655 	}
656 	return (0);
657 }
658 
659 /*------------------------------------------------------------------------*
660  *	ugen_get_cdesc
661  *
662  * This function will retrieve the complete configuration descriptor
663  * at the given index.
664  *------------------------------------------------------------------------*/
665 static int
666 ugen_get_cdesc(struct usb_fifo *f, struct usb_gen_descriptor *ugd)
667 {
668 	struct usb_config_descriptor *cdesc;
669 	struct usb_device *udev = f->udev;
670 	int error;
671 	uint16_t len;
672 	uint8_t free_data;
673 
674 	DPRINTFN(6, "\n");
675 
676 	if (ugd->ugd_data == NULL) {
677 		/* userland pointer should not be zero */
678 		return (EINVAL);
679 	}
680 	if ((ugd->ugd_config_index == USB_UNCONFIG_INDEX) ||
681 	    (ugd->ugd_config_index == udev->curr_config_index)) {
682 		cdesc = usbd_get_config_descriptor(udev);
683 		if (cdesc == NULL)
684 			return (ENXIO);
685 		free_data = 0;
686 
687 	} else {
688 #if (USB_HAVE_FIXED_CONFIG == 0)
689 		if (usbd_req_get_config_desc_full(udev,
690 		    NULL, &cdesc, ugd->ugd_config_index)) {
691 			return (ENXIO);
692 		}
693 		free_data = 1;
694 #else
695 		/* configuration descriptor data is shared */
696 		return (EINVAL);
697 #endif
698 	}
699 
700 	len = UGETW(cdesc->wTotalLength);
701 	if (len > ugd->ugd_maxlen) {
702 		len = ugd->ugd_maxlen;
703 	}
704 	DPRINTFN(6, "len=%u\n", len);
705 
706 	ugd->ugd_actlen = len;
707 	ugd->ugd_offset = 0;
708 
709 	error = copyout(cdesc, ugd->ugd_data, len);
710 
711 	if (free_data)
712 		usbd_free_config_desc(udev, cdesc);
713 
714 	return (error);
715 }
716 
717 /*
718  * This function is called having the enumeration SX locked which
719  * protects the scratch area used.
720  */
721 static int
722 ugen_get_sdesc(struct usb_fifo *f, struct usb_gen_descriptor *ugd)
723 {
724 	void *ptr;
725 	uint16_t size;
726 	int error;
727 
728 	ptr = f->udev->scratch.data;
729 	size = sizeof(f->udev->scratch.data);
730 
731 	if (usbd_req_get_string_desc(f->udev, NULL, ptr,
732 	    size, ugd->ugd_lang_id, ugd->ugd_string_index)) {
733 		error = EINVAL;
734 	} else {
735 
736 		if (size > ((uint8_t *)ptr)[0]) {
737 			size = ((uint8_t *)ptr)[0];
738 		}
739 		if (size > ugd->ugd_maxlen) {
740 			size = ugd->ugd_maxlen;
741 		}
742 		ugd->ugd_actlen = size;
743 		ugd->ugd_offset = 0;
744 
745 		error = copyout(ptr, ugd->ugd_data, size);
746 	}
747 	return (error);
748 }
749 
750 /*------------------------------------------------------------------------*
751  *	ugen_get_iface_driver
752  *
753  * This function generates an USB interface description for userland.
754  *
755  * Returns:
756  *    0: Success
757  * Else: Failure
758  *------------------------------------------------------------------------*/
759 static int
760 ugen_get_iface_driver(struct usb_fifo *f, struct usb_gen_descriptor *ugd)
761 {
762 	struct usb_device *udev = f->udev;
763 	struct usb_interface *iface;
764 	const char *ptr;
765 	const char *desc;
766 	unsigned int len;
767 	unsigned int maxlen;
768 	char buf[128];
769 	int error;
770 
771 	DPRINTFN(6, "\n");
772 
773 	if ((ugd->ugd_data == NULL) || (ugd->ugd_maxlen == 0)) {
774 		/* userland pointer should not be zero */
775 		return (EINVAL);
776 	}
777 
778 	iface = usbd_get_iface(udev, ugd->ugd_iface_index);
779 	if ((iface == NULL) || (iface->idesc == NULL)) {
780 		/* invalid interface index */
781 		return (EINVAL);
782 	}
783 
784 	/* read out device nameunit string, if any */
785 	if ((iface->subdev != NULL) &&
786 	    device_is_attached(iface->subdev) &&
787 	    (ptr = device_get_nameunit(iface->subdev)) &&
788 	    (desc = device_get_desc(iface->subdev))) {
789 
790 		/* print description */
791 		snprintf(buf, sizeof(buf), "%s: <%s>", ptr, desc);
792 
793 		/* range checks */
794 		maxlen = ugd->ugd_maxlen - 1;
795 		len = strlen(buf);
796 		if (len > maxlen)
797 			len = maxlen;
798 
799 		/* update actual length, including terminating zero */
800 		ugd->ugd_actlen = len + 1;
801 
802 		/* copy out interface description */
803 		error = copyout(buf, ugd->ugd_data, ugd->ugd_actlen);
804 	} else {
805 		/* zero length string is default */
806 		error = copyout("", ugd->ugd_data, 1);
807 	}
808 	return (error);
809 }
810 
811 /*------------------------------------------------------------------------*
812  *	usb_gen_fill_deviceinfo
813  *
814  * This function dumps information about an USB device to the
815  * structure pointed to by the "di" argument.
816  *
817  * Returns:
818  *    0: Success
819  * Else: Failure
820  *------------------------------------------------------------------------*/
821 static int
822 usb_gen_fill_deviceinfo(struct usb_fifo *f, struct usb_device_info *di)
823 {
824 	struct usb_device *udev;
825 	struct usb_device *hub;
826 
827 	udev = f->udev;
828 
829 	bzero(di, sizeof(di[0]));
830 
831 	di->udi_bus = device_get_unit(udev->bus->bdev);
832 	di->udi_addr = udev->address;
833 	di->udi_index = udev->device_index;
834 	strlcpy(di->udi_serial, usb_get_serial(udev), sizeof(di->udi_serial));
835 	strlcpy(di->udi_vendor, usb_get_manufacturer(udev), sizeof(di->udi_vendor));
836 	strlcpy(di->udi_product, usb_get_product(udev), sizeof(di->udi_product));
837 	usb_printbcd(di->udi_release, sizeof(di->udi_release),
838 	    UGETW(udev->ddesc.bcdDevice));
839 	di->udi_vendorNo = UGETW(udev->ddesc.idVendor);
840 	di->udi_productNo = UGETW(udev->ddesc.idProduct);
841 	di->udi_releaseNo = UGETW(udev->ddesc.bcdDevice);
842 	di->udi_class = udev->ddesc.bDeviceClass;
843 	di->udi_subclass = udev->ddesc.bDeviceSubClass;
844 	di->udi_protocol = udev->ddesc.bDeviceProtocol;
845 	di->udi_config_no = udev->curr_config_no;
846 	di->udi_config_index = udev->curr_config_index;
847 	di->udi_power = udev->flags.self_powered ? 0 : udev->power;
848 	di->udi_speed = udev->speed;
849 	di->udi_mode = udev->flags.usb_mode;
850 	di->udi_power_mode = udev->power_mode;
851 	di->udi_suspended = udev->flags.peer_suspended;
852 
853 	hub = udev->parent_hub;
854 	if (hub) {
855 		di->udi_hubaddr = hub->address;
856 		di->udi_hubindex = hub->device_index;
857 		di->udi_hubport = udev->port_no;
858 	}
859 	return (0);
860 }
861 
862 /*------------------------------------------------------------------------*
863  *	ugen_check_request
864  *
865  * Return values:
866  * 0: Access allowed
867  * Else: No access
868  *------------------------------------------------------------------------*/
869 static int
870 ugen_check_request(struct usb_device *udev, struct usb_device_request *req)
871 {
872 	struct usb_endpoint *ep;
873 	int error;
874 
875 	/*
876 	 * Avoid requests that would damage the bus integrity:
877 	 */
878 	if (((req->bmRequestType == UT_WRITE_DEVICE) &&
879 	    (req->bRequest == UR_SET_ADDRESS)) ||
880 	    ((req->bmRequestType == UT_WRITE_DEVICE) &&
881 	    (req->bRequest == UR_SET_CONFIG)) ||
882 	    ((req->bmRequestType == UT_WRITE_INTERFACE) &&
883 	    (req->bRequest == UR_SET_INTERFACE))) {
884 		/*
885 		 * These requests can be useful for testing USB drivers.
886 		 */
887 		error = priv_check(curthread, PRIV_DRIVER);
888 		if (error) {
889 			return (error);
890 		}
891 	}
892 	/*
893 	 * Special case - handle clearing of stall
894 	 */
895 	if (req->bmRequestType == UT_WRITE_ENDPOINT) {
896 
897 		ep = usbd_get_ep_by_addr(udev, req->wIndex[0]);
898 		if (ep == NULL) {
899 			return (EINVAL);
900 		}
901 		if ((req->bRequest == UR_CLEAR_FEATURE) &&
902 		    (UGETW(req->wValue) == UF_ENDPOINT_HALT)) {
903 			usbd_clear_data_toggle(udev, ep);
904 		}
905 	}
906 	/* TODO: add more checks to verify the interface index */
907 
908 	return (0);
909 }
910 
911 int
912 ugen_do_request(struct usb_fifo *f, struct usb_ctl_request *ur)
913 {
914 	int error;
915 	uint16_t len;
916 	uint16_t actlen;
917 
918 	if (ugen_check_request(f->udev, &ur->ucr_request)) {
919 		return (EPERM);
920 	}
921 	len = UGETW(ur->ucr_request.wLength);
922 
923 	/* check if "ucr_data" is valid */
924 	if (len != 0) {
925 		if (ur->ucr_data == NULL) {
926 			return (EFAULT);
927 		}
928 	}
929 	/* do the USB request */
930 	error = usbd_do_request_flags
931 	    (f->udev, NULL, &ur->ucr_request, ur->ucr_data,
932 	    (ur->ucr_flags & USB_SHORT_XFER_OK) |
933 	    USB_USER_DATA_PTR, &actlen,
934 	    USB_DEFAULT_TIMEOUT);
935 
936 	ur->ucr_actlen = actlen;
937 
938 	if (error) {
939 		error = EIO;
940 	}
941 	return (error);
942 }
943 
944 /*------------------------------------------------------------------------
945  *	ugen_re_enumerate
946  *------------------------------------------------------------------------*/
947 static int
948 ugen_re_enumerate(struct usb_fifo *f)
949 {
950 	struct usb_device *udev = f->udev;
951 	int error;
952 
953 	/*
954 	 * This request can be useful for testing USB drivers:
955 	 */
956 	error = priv_check(curthread, PRIV_DRIVER);
957 	if (error) {
958 		return (error);
959 	}
960 	if (udev->flags.usb_mode != USB_MODE_HOST) {
961 		/* not possible in device side mode */
962 		DPRINTFN(6, "device mode\n");
963 		return (ENOTTY);
964 	}
965 	/* make sure all FIFO's are gone */
966 	/* else there can be a deadlock */
967 	if (ugen_fs_uninit(f)) {
968 		/* ignore any errors */
969 		DPRINTFN(6, "no FIFOs\n");
970 	}
971 	/* start re-enumeration of device */
972 	usbd_start_re_enumerate(udev);
973 	return (0);
974 }
975 
976 int
977 ugen_fs_uninit(struct usb_fifo *f)
978 {
979 	if (f->fs_xfer == NULL) {
980 		return (EINVAL);
981 	}
982 	usbd_transfer_unsetup(f->fs_xfer, f->fs_ep_max);
983 	free(f->fs_xfer, M_USB);
984 	f->fs_xfer = NULL;
985 	f->fs_ep_max = 0;
986 	f->fs_ep_ptr = NULL;
987 	f->flag_iscomplete = 0;
988 	usb_fifo_free_buffer(f);
989 	return (0);
990 }
991 
992 static uint8_t
993 ugen_fs_get_complete(struct usb_fifo *f, uint8_t *pindex)
994 {
995 	struct usb_mbuf *m;
996 
997 	USB_IF_DEQUEUE(&f->used_q, m);
998 
999 	if (m) {
1000 		*pindex = *((uint8_t *)(m->cur_data_ptr));
1001 
1002 		USB_IF_ENQUEUE(&f->free_q, m);
1003 
1004 		return (0);		/* success */
1005 	} else {
1006 
1007 		*pindex = 0;		/* fix compiler warning */
1008 
1009 		f->flag_iscomplete = 0;
1010 	}
1011 	return (1);			/* failure */
1012 }
1013 
1014 static void
1015 ugen_fs_set_complete(struct usb_fifo *f, uint8_t index)
1016 {
1017 	struct usb_mbuf *m;
1018 
1019 	USB_IF_DEQUEUE(&f->free_q, m);
1020 
1021 	if (m == NULL) {
1022 		/* can happen during close */
1023 		DPRINTF("out of buffers\n");
1024 		return;
1025 	}
1026 	USB_MBUF_RESET(m);
1027 
1028 	*((uint8_t *)(m->cur_data_ptr)) = index;
1029 
1030 	USB_IF_ENQUEUE(&f->used_q, m);
1031 
1032 	f->flag_iscomplete = 1;
1033 
1034 	usb_fifo_wakeup(f);
1035 }
1036 
1037 static int
1038 ugen_fs_copy_in(struct usb_fifo *f, uint8_t ep_index)
1039 {
1040 	struct usb_device_request *req;
1041 	struct usb_xfer *xfer;
1042 	struct usb_fs_endpoint fs_ep;
1043 	void *uaddr;			/* userland pointer */
1044 	void *kaddr;
1045 	usb_frlength_t offset;
1046 	usb_frlength_t rem;
1047 	usb_frcount_t n;
1048 	uint32_t length;
1049 	int error;
1050 	uint8_t isread;
1051 
1052 	if (ep_index >= f->fs_ep_max) {
1053 		return (EINVAL);
1054 	}
1055 	xfer = f->fs_xfer[ep_index];
1056 	if (xfer == NULL) {
1057 		return (EINVAL);
1058 	}
1059 	mtx_lock(f->priv_mtx);
1060 	if (usbd_transfer_pending(xfer)) {
1061 		mtx_unlock(f->priv_mtx);
1062 		return (EBUSY);		/* should not happen */
1063 	}
1064 	mtx_unlock(f->priv_mtx);
1065 
1066 	error = copyin(f->fs_ep_ptr +
1067 	    ep_index, &fs_ep, sizeof(fs_ep));
1068 	if (error) {
1069 		return (error);
1070 	}
1071 	/* security checks */
1072 
1073 	if (fs_ep.nFrames > xfer->max_frame_count) {
1074 		xfer->error = USB_ERR_INVAL;
1075 		goto complete;
1076 	}
1077 	if (fs_ep.nFrames == 0) {
1078 		xfer->error = USB_ERR_INVAL;
1079 		goto complete;
1080 	}
1081 	error = copyin(fs_ep.ppBuffer,
1082 	    &uaddr, sizeof(uaddr));
1083 	if (error) {
1084 		return (error);
1085 	}
1086 	/* reset first frame */
1087 	usbd_xfer_set_frame_offset(xfer, 0, 0);
1088 
1089 	if (xfer->flags_int.control_xfr) {
1090 
1091 		req = xfer->frbuffers[0].buffer;
1092 
1093 		error = copyin(fs_ep.pLength,
1094 		    &length, sizeof(length));
1095 		if (error) {
1096 			return (error);
1097 		}
1098 		if (length != sizeof(*req)) {
1099 			xfer->error = USB_ERR_INVAL;
1100 			goto complete;
1101 		}
1102 		if (length != 0) {
1103 			error = copyin(uaddr, req, length);
1104 			if (error) {
1105 				return (error);
1106 			}
1107 		}
1108 		if (ugen_check_request(f->udev, req)) {
1109 			xfer->error = USB_ERR_INVAL;
1110 			goto complete;
1111 		}
1112 		usbd_xfer_set_frame_len(xfer, 0, length);
1113 
1114 		/* Host mode only ! */
1115 		if ((req->bmRequestType &
1116 		    (UT_READ | UT_WRITE)) == UT_READ) {
1117 			isread = 1;
1118 		} else {
1119 			isread = 0;
1120 		}
1121 		n = 1;
1122 		offset = sizeof(*req);
1123 
1124 	} else {
1125 		/* Device and Host mode */
1126 		if (USB_GET_DATA_ISREAD(xfer)) {
1127 			isread = 1;
1128 		} else {
1129 			isread = 0;
1130 		}
1131 		n = 0;
1132 		offset = 0;
1133 	}
1134 
1135 	rem = usbd_xfer_max_len(xfer);
1136 	xfer->nframes = fs_ep.nFrames;
1137 	xfer->timeout = fs_ep.timeout;
1138 	if (xfer->timeout > 65535) {
1139 		xfer->timeout = 65535;
1140 	}
1141 	if (fs_ep.flags & USB_FS_FLAG_SINGLE_SHORT_OK)
1142 		xfer->flags.short_xfer_ok = 1;
1143 	else
1144 		xfer->flags.short_xfer_ok = 0;
1145 
1146 	if (fs_ep.flags & USB_FS_FLAG_MULTI_SHORT_OK)
1147 		xfer->flags.short_frames_ok = 1;
1148 	else
1149 		xfer->flags.short_frames_ok = 0;
1150 
1151 	if (fs_ep.flags & USB_FS_FLAG_FORCE_SHORT)
1152 		xfer->flags.force_short_xfer = 1;
1153 	else
1154 		xfer->flags.force_short_xfer = 0;
1155 
1156 	if (fs_ep.flags & USB_FS_FLAG_CLEAR_STALL)
1157 		usbd_xfer_set_stall(xfer);
1158 	else
1159 		xfer->flags.stall_pipe = 0;
1160 
1161 	for (; n != xfer->nframes; n++) {
1162 
1163 		error = copyin(fs_ep.pLength + n,
1164 		    &length, sizeof(length));
1165 		if (error) {
1166 			break;
1167 		}
1168 		usbd_xfer_set_frame_len(xfer, n, length);
1169 
1170 		if (length > rem) {
1171 			xfer->error = USB_ERR_INVAL;
1172 			goto complete;
1173 		}
1174 		rem -= length;
1175 
1176 		if (!isread) {
1177 
1178 			/* we need to know the source buffer */
1179 			error = copyin(fs_ep.ppBuffer + n,
1180 			    &uaddr, sizeof(uaddr));
1181 			if (error) {
1182 				break;
1183 			}
1184 			if (xfer->flags_int.isochronous_xfr) {
1185 				/* get kernel buffer address */
1186 				kaddr = xfer->frbuffers[0].buffer;
1187 				kaddr = USB_ADD_BYTES(kaddr, offset);
1188 			} else {
1189 				/* set current frame offset */
1190 				usbd_xfer_set_frame_offset(xfer, offset, n);
1191 
1192 				/* get kernel buffer address */
1193 				kaddr = xfer->frbuffers[n].buffer;
1194 			}
1195 
1196 			/* move data */
1197 			error = copyin(uaddr, kaddr, length);
1198 			if (error) {
1199 				break;
1200 			}
1201 		}
1202 		offset += length;
1203 	}
1204 	return (error);
1205 
1206 complete:
1207 	mtx_lock(f->priv_mtx);
1208 	ugen_fs_set_complete(f, ep_index);
1209 	mtx_unlock(f->priv_mtx);
1210 	return (0);
1211 }
1212 
1213 static int
1214 ugen_fs_copy_out(struct usb_fifo *f, uint8_t ep_index)
1215 {
1216 	struct usb_device_request *req;
1217 	struct usb_xfer *xfer;
1218 	struct usb_fs_endpoint fs_ep;
1219 	struct usb_fs_endpoint *fs_ep_uptr;	/* userland ptr */
1220 	void *uaddr;			/* userland ptr */
1221 	void *kaddr;
1222 	usb_frlength_t offset;
1223 	usb_frlength_t rem;
1224 	usb_frcount_t n;
1225 	uint32_t length;
1226 	uint32_t temp;
1227 	int error;
1228 	uint8_t isread;
1229 
1230 	if (ep_index >= f->fs_ep_max)
1231 		return (EINVAL);
1232 
1233 	xfer = f->fs_xfer[ep_index];
1234 	if (xfer == NULL)
1235 		return (EINVAL);
1236 
1237 	mtx_lock(f->priv_mtx);
1238 	if (usbd_transfer_pending(xfer)) {
1239 		mtx_unlock(f->priv_mtx);
1240 		return (EBUSY);		/* should not happen */
1241 	}
1242 	mtx_unlock(f->priv_mtx);
1243 
1244 	fs_ep_uptr = f->fs_ep_ptr + ep_index;
1245 	error = copyin(fs_ep_uptr, &fs_ep, sizeof(fs_ep));
1246 	if (error) {
1247 		return (error);
1248 	}
1249 	fs_ep.status = xfer->error;
1250 	fs_ep.aFrames = xfer->aframes;
1251 	fs_ep.isoc_time_complete = xfer->isoc_time_complete;
1252 	if (xfer->error) {
1253 		goto complete;
1254 	}
1255 	if (xfer->flags_int.control_xfr) {
1256 		req = xfer->frbuffers[0].buffer;
1257 
1258 		/* Host mode only ! */
1259 		if ((req->bmRequestType & (UT_READ | UT_WRITE)) == UT_READ) {
1260 			isread = 1;
1261 		} else {
1262 			isread = 0;
1263 		}
1264 		if (xfer->nframes == 0)
1265 			n = 0;		/* should never happen */
1266 		else
1267 			n = 1;
1268 	} else {
1269 		/* Device and Host mode */
1270 		if (USB_GET_DATA_ISREAD(xfer)) {
1271 			isread = 1;
1272 		} else {
1273 			isread = 0;
1274 		}
1275 		n = 0;
1276 	}
1277 
1278 	/* Update lengths and copy out data */
1279 
1280 	rem = usbd_xfer_max_len(xfer);
1281 	offset = 0;
1282 
1283 	for (; n != xfer->nframes; n++) {
1284 
1285 		/* get initial length into "temp" */
1286 		error = copyin(fs_ep.pLength + n,
1287 		    &temp, sizeof(temp));
1288 		if (error) {
1289 			return (error);
1290 		}
1291 		if (temp > rem) {
1292 			/* the userland length has been corrupted */
1293 			DPRINTF("corrupt userland length "
1294 			    "%u > %u\n", temp, rem);
1295 			fs_ep.status = USB_ERR_INVAL;
1296 			goto complete;
1297 		}
1298 		rem -= temp;
1299 
1300 		/* get actual transfer length */
1301 		length = xfer->frlengths[n];
1302 		if (length > temp) {
1303 			/* data overflow */
1304 			fs_ep.status = USB_ERR_INVAL;
1305 			DPRINTF("data overflow %u > %u\n",
1306 			    length, temp);
1307 			goto complete;
1308 		}
1309 		if (isread) {
1310 
1311 			/* we need to know the destination buffer */
1312 			error = copyin(fs_ep.ppBuffer + n,
1313 			    &uaddr, sizeof(uaddr));
1314 			if (error) {
1315 				return (error);
1316 			}
1317 			if (xfer->flags_int.isochronous_xfr) {
1318 				/* only one frame buffer */
1319 				kaddr = USB_ADD_BYTES(
1320 				    xfer->frbuffers[0].buffer, offset);
1321 			} else {
1322 				/* multiple frame buffers */
1323 				kaddr = xfer->frbuffers[n].buffer;
1324 			}
1325 
1326 			/* move data */
1327 			error = copyout(kaddr, uaddr, length);
1328 			if (error) {
1329 				return (error);
1330 			}
1331 		}
1332 		/*
1333 		 * Update offset according to initial length, which is
1334 		 * needed by isochronous transfers!
1335 		 */
1336 		offset += temp;
1337 
1338 		/* update length */
1339 		error = copyout(&length,
1340 		    fs_ep.pLength + n, sizeof(length));
1341 		if (error) {
1342 			return (error);
1343 		}
1344 	}
1345 
1346 complete:
1347 	/* update "aFrames" */
1348 	error = copyout(&fs_ep.aFrames, &fs_ep_uptr->aFrames,
1349 	    sizeof(fs_ep.aFrames));
1350 	if (error)
1351 		goto done;
1352 
1353 	/* update "isoc_time_complete" */
1354 	error = copyout(&fs_ep.isoc_time_complete,
1355 	    &fs_ep_uptr->isoc_time_complete,
1356 	    sizeof(fs_ep.isoc_time_complete));
1357 	if (error)
1358 		goto done;
1359 	/* update "status" */
1360 	error = copyout(&fs_ep.status, &fs_ep_uptr->status,
1361 	    sizeof(fs_ep.status));
1362 done:
1363 	return (error);
1364 }
1365 
1366 static uint8_t
1367 ugen_fifo_in_use(struct usb_fifo *f, int fflags)
1368 {
1369 	struct usb_fifo *f_rx;
1370 	struct usb_fifo *f_tx;
1371 
1372 	f_rx = f->udev->fifo[(f->fifo_index & ~1) + USB_FIFO_RX];
1373 	f_tx = f->udev->fifo[(f->fifo_index & ~1) + USB_FIFO_TX];
1374 
1375 	if ((fflags & FREAD) && f_rx &&
1376 	    (f_rx->xfer[0] || f_rx->xfer[1])) {
1377 		return (1);		/* RX FIFO in use */
1378 	}
1379 	if ((fflags & FWRITE) && f_tx &&
1380 	    (f_tx->xfer[0] || f_tx->xfer[1])) {
1381 		return (1);		/* TX FIFO in use */
1382 	}
1383 	return (0);			/* not in use */
1384 }
1385 
1386 static int
1387 ugen_ioctl(struct usb_fifo *f, u_long cmd, void *addr, int fflags)
1388 {
1389 	struct usb_config usb_config[1];
1390 	struct usb_device_request req;
1391 	union {
1392 		struct usb_fs_complete *pcomp;
1393 		struct usb_fs_start *pstart;
1394 		struct usb_fs_stop *pstop;
1395 		struct usb_fs_open *popen;
1396 		struct usb_fs_open_stream *popen_stream;
1397 		struct usb_fs_close *pclose;
1398 		struct usb_fs_clear_stall_sync *pstall;
1399 		void   *addr;
1400 	}     u;
1401 	struct usb_endpoint *ep;
1402 	struct usb_endpoint_descriptor *ed;
1403 	struct usb_xfer *xfer;
1404 	int error = 0;
1405 	uint8_t iface_index;
1406 	uint8_t isread;
1407 	uint8_t ep_index;
1408 	uint8_t pre_scale;
1409 
1410 	u.addr = addr;
1411 
1412 	DPRINTFN(6, "cmd=0x%08lx\n", cmd);
1413 
1414 	switch (cmd) {
1415 	case USB_FS_COMPLETE:
1416 		mtx_lock(f->priv_mtx);
1417 		error = ugen_fs_get_complete(f, &ep_index);
1418 		mtx_unlock(f->priv_mtx);
1419 
1420 		if (error) {
1421 			error = EBUSY;
1422 			break;
1423 		}
1424 		u.pcomp->ep_index = ep_index;
1425 		error = ugen_fs_copy_out(f, u.pcomp->ep_index);
1426 		break;
1427 
1428 	case USB_FS_START:
1429 		error = ugen_fs_copy_in(f, u.pstart->ep_index);
1430 		if (error)
1431 			break;
1432 		mtx_lock(f->priv_mtx);
1433 		xfer = f->fs_xfer[u.pstart->ep_index];
1434 		usbd_transfer_start(xfer);
1435 		mtx_unlock(f->priv_mtx);
1436 		break;
1437 
1438 	case USB_FS_STOP:
1439 		if (u.pstop->ep_index >= f->fs_ep_max) {
1440 			error = EINVAL;
1441 			break;
1442 		}
1443 		mtx_lock(f->priv_mtx);
1444 		xfer = f->fs_xfer[u.pstart->ep_index];
1445 		if (usbd_transfer_pending(xfer)) {
1446 			usbd_transfer_stop(xfer);
1447 			/*
1448 			 * Check if the USB transfer was stopped
1449 			 * before it was even started. Else a cancel
1450 			 * callback will be pending.
1451 			 */
1452 			if (!xfer->flags_int.transferring) {
1453 				ugen_fs_set_complete(xfer->priv_sc,
1454 				    USB_P2U(xfer->priv_fifo));
1455 			}
1456 		}
1457 		mtx_unlock(f->priv_mtx);
1458 		break;
1459 
1460 	case USB_FS_OPEN:
1461 	case USB_FS_OPEN_STREAM:
1462 		if (u.popen->ep_index >= f->fs_ep_max) {
1463 			error = EINVAL;
1464 			break;
1465 		}
1466 		if (f->fs_xfer[u.popen->ep_index] != NULL) {
1467 			error = EBUSY;
1468 			break;
1469 		}
1470 		if (u.popen->max_bufsize > USB_FS_MAX_BUFSIZE) {
1471 			u.popen->max_bufsize = USB_FS_MAX_BUFSIZE;
1472 		}
1473 		if (u.popen->max_frames & USB_FS_MAX_FRAMES_PRE_SCALE) {
1474 			pre_scale = 1;
1475 			u.popen->max_frames &= ~USB_FS_MAX_FRAMES_PRE_SCALE;
1476 		} else {
1477 			pre_scale = 0;
1478 		}
1479 		if (u.popen->max_frames > USB_FS_MAX_FRAMES) {
1480 			u.popen->max_frames = USB_FS_MAX_FRAMES;
1481 			break;
1482 		}
1483 		if (u.popen->max_frames == 0) {
1484 			error = EINVAL;
1485 			break;
1486 		}
1487 		ep = usbd_get_ep_by_addr(f->udev, u.popen->ep_no);
1488 		if (ep == NULL) {
1489 			error = EINVAL;
1490 			break;
1491 		}
1492 		ed = ep->edesc;
1493 		if (ed == NULL) {
1494 			error = ENXIO;
1495 			break;
1496 		}
1497 		iface_index = ep->iface_index;
1498 
1499 		memset(usb_config, 0, sizeof(usb_config));
1500 
1501 		usb_config[0].type = ed->bmAttributes & UE_XFERTYPE;
1502 		usb_config[0].endpoint = ed->bEndpointAddress & UE_ADDR;
1503 		usb_config[0].direction = ed->bEndpointAddress & (UE_DIR_OUT | UE_DIR_IN);
1504 		usb_config[0].interval = USB_DEFAULT_INTERVAL;
1505 		usb_config[0].flags.proxy_buffer = 1;
1506 		if (pre_scale != 0)
1507 			usb_config[0].flags.pre_scale_frames = 1;
1508 		usb_config[0].callback = &ugen_ctrl_fs_callback;
1509 		usb_config[0].timeout = 0;	/* no timeout */
1510 		usb_config[0].frames = u.popen->max_frames;
1511 		usb_config[0].bufsize = u.popen->max_bufsize;
1512 		usb_config[0].usb_mode = USB_MODE_DUAL;	/* both modes */
1513 		if (cmd == USB_FS_OPEN_STREAM)
1514 			usb_config[0].stream_id = u.popen_stream->stream_id;
1515 
1516 		if (usb_config[0].type == UE_CONTROL) {
1517 			if (f->udev->flags.usb_mode != USB_MODE_HOST) {
1518 				error = EINVAL;
1519 				break;
1520 			}
1521 		} else {
1522 
1523 			isread = ((usb_config[0].endpoint &
1524 			    (UE_DIR_IN | UE_DIR_OUT)) == UE_DIR_IN);
1525 
1526 			if (f->udev->flags.usb_mode != USB_MODE_HOST) {
1527 				isread = !isread;
1528 			}
1529 			/* check permissions */
1530 			if (isread) {
1531 				if (!(fflags & FREAD)) {
1532 					error = EPERM;
1533 					break;
1534 				}
1535 			} else {
1536 				if (!(fflags & FWRITE)) {
1537 					error = EPERM;
1538 					break;
1539 				}
1540 			}
1541 		}
1542 		error = usbd_transfer_setup(f->udev, &iface_index,
1543 		    f->fs_xfer + u.popen->ep_index, usb_config, 1,
1544 		    f, f->priv_mtx);
1545 		if (error == 0) {
1546 			/* update maximums */
1547 			u.popen->max_packet_length =
1548 			    f->fs_xfer[u.popen->ep_index]->max_frame_size;
1549 			u.popen->max_bufsize =
1550 			    f->fs_xfer[u.popen->ep_index]->max_data_length;
1551 			/* update number of frames */
1552 			u.popen->max_frames =
1553 			    f->fs_xfer[u.popen->ep_index]->nframes;
1554 			/* store index of endpoint */
1555 			f->fs_xfer[u.popen->ep_index]->priv_fifo =
1556 			    ((uint8_t *)0) + u.popen->ep_index;
1557 		} else {
1558 			error = ENOMEM;
1559 		}
1560 		break;
1561 
1562 	case USB_FS_CLOSE:
1563 		if (u.pclose->ep_index >= f->fs_ep_max) {
1564 			error = EINVAL;
1565 			break;
1566 		}
1567 		if (f->fs_xfer[u.pclose->ep_index] == NULL) {
1568 			error = EINVAL;
1569 			break;
1570 		}
1571 		usbd_transfer_unsetup(f->fs_xfer + u.pclose->ep_index, 1);
1572 		break;
1573 
1574 	case USB_FS_CLEAR_STALL_SYNC:
1575 		if (u.pstall->ep_index >= f->fs_ep_max) {
1576 			error = EINVAL;
1577 			break;
1578 		}
1579 		if (f->fs_xfer[u.pstall->ep_index] == NULL) {
1580 			error = EINVAL;
1581 			break;
1582 		}
1583 		if (f->udev->flags.usb_mode != USB_MODE_HOST) {
1584 			error = EINVAL;
1585 			break;
1586 		}
1587 		mtx_lock(f->priv_mtx);
1588 		error = usbd_transfer_pending(f->fs_xfer[u.pstall->ep_index]);
1589 		mtx_unlock(f->priv_mtx);
1590 
1591 		if (error) {
1592 			return (EBUSY);
1593 		}
1594 		ep = f->fs_xfer[u.pstall->ep_index]->endpoint;
1595 
1596 		/* setup a clear-stall packet */
1597 		req.bmRequestType = UT_WRITE_ENDPOINT;
1598 		req.bRequest = UR_CLEAR_FEATURE;
1599 		USETW(req.wValue, UF_ENDPOINT_HALT);
1600 		req.wIndex[0] = ep->edesc->bEndpointAddress;
1601 		req.wIndex[1] = 0;
1602 		USETW(req.wLength, 0);
1603 
1604 		error = usbd_do_request(f->udev, NULL, &req, NULL);
1605 		if (error == 0) {
1606 			usbd_clear_data_toggle(f->udev, ep);
1607 		} else {
1608 			error = ENXIO;
1609 		}
1610 		break;
1611 
1612 	default:
1613 		error = ENOIOCTL;
1614 		break;
1615 	}
1616 
1617 	DPRINTFN(6, "error=%d\n", error);
1618 
1619 	return (error);
1620 }
1621 
1622 static int
1623 ugen_set_short_xfer(struct usb_fifo *f, void *addr)
1624 {
1625 	uint8_t t;
1626 
1627 	if (*(int *)addr)
1628 		t = 1;
1629 	else
1630 		t = 0;
1631 
1632 	if (f->flag_short == t) {
1633 		/* same value like before - accept */
1634 		return (0);
1635 	}
1636 	if (f->xfer[0] || f->xfer[1]) {
1637 		/* cannot change this during transfer */
1638 		return (EBUSY);
1639 	}
1640 	f->flag_short = t;
1641 	return (0);
1642 }
1643 
1644 static int
1645 ugen_set_timeout(struct usb_fifo *f, void *addr)
1646 {
1647 	f->timeout = *(int *)addr;
1648 	if (f->timeout > 65535) {
1649 		/* limit user input */
1650 		f->timeout = 65535;
1651 	}
1652 	return (0);
1653 }
1654 
1655 static int
1656 ugen_get_frame_size(struct usb_fifo *f, void *addr)
1657 {
1658 	if (f->xfer[0]) {
1659 		*(int *)addr = f->xfer[0]->max_frame_size;
1660 	} else {
1661 		return (EINVAL);
1662 	}
1663 	return (0);
1664 }
1665 
1666 static int
1667 ugen_set_buffer_size(struct usb_fifo *f, void *addr)
1668 {
1669 	usb_frlength_t t;
1670 
1671 	if (*(int *)addr < 0)
1672 		t = 0;		/* use "wMaxPacketSize" */
1673 	else if (*(int *)addr < (256 * 1024))
1674 		t = *(int *)addr;
1675 	else
1676 		t = 256 * 1024;
1677 
1678 	if (f->bufsize == t) {
1679 		/* same value like before - accept */
1680 		return (0);
1681 	}
1682 	if (f->xfer[0] || f->xfer[1]) {
1683 		/* cannot change this during transfer */
1684 		return (EBUSY);
1685 	}
1686 	f->bufsize = t;
1687 	return (0);
1688 }
1689 
1690 static int
1691 ugen_get_buffer_size(struct usb_fifo *f, void *addr)
1692 {
1693 	*(int *)addr = f->bufsize;
1694 	return (0);
1695 }
1696 
1697 static int
1698 ugen_get_iface_desc(struct usb_fifo *f,
1699     struct usb_interface_descriptor *idesc)
1700 {
1701 	struct usb_interface *iface;
1702 
1703 	iface = usbd_get_iface(f->udev, f->iface_index);
1704 	if (iface && iface->idesc) {
1705 		*idesc = *(iface->idesc);
1706 	} else {
1707 		return (EIO);
1708 	}
1709 	return (0);
1710 }
1711 
1712 static int
1713 ugen_get_endpoint_desc(struct usb_fifo *f,
1714     struct usb_endpoint_descriptor *ed)
1715 {
1716 	struct usb_endpoint *ep;
1717 
1718 	ep = usb_fifo_softc(f);
1719 
1720 	if (ep && ep->edesc) {
1721 		*ed = *ep->edesc;
1722 	} else {
1723 		return (EINVAL);
1724 	}
1725 	return (0);
1726 }
1727 
1728 static int
1729 ugen_set_power_mode(struct usb_fifo *f, int mode)
1730 {
1731 	struct usb_device *udev = f->udev;
1732 	int err;
1733 	uint8_t old_mode;
1734 
1735 	if ((udev == NULL) ||
1736 	    (udev->parent_hub == NULL)) {
1737 		return (EINVAL);
1738 	}
1739 	err = priv_check(curthread, PRIV_DRIVER);
1740 	if (err)
1741 		return (err);
1742 
1743 	/* get old power mode */
1744 	old_mode = udev->power_mode;
1745 
1746 	/* if no change, then just return */
1747 	if (old_mode == mode)
1748 		return (0);
1749 
1750 	switch (mode) {
1751 	case USB_POWER_MODE_OFF:
1752 		if (udev->flags.usb_mode == USB_MODE_HOST &&
1753 		    udev->re_enumerate_wait == USB_RE_ENUM_DONE) {
1754 			udev->re_enumerate_wait = USB_RE_ENUM_PWR_OFF;
1755 		}
1756 		/* set power mode will wake up the explore thread */
1757 		break;
1758 
1759 	case USB_POWER_MODE_ON:
1760 	case USB_POWER_MODE_SAVE:
1761 		break;
1762 
1763 	case USB_POWER_MODE_RESUME:
1764 #if USB_HAVE_POWERD
1765 		/* let USB-powerd handle resume */
1766 		USB_BUS_LOCK(udev->bus);
1767 		udev->pwr_save.write_refs++;
1768 		udev->pwr_save.last_xfer_time = ticks;
1769 		USB_BUS_UNLOCK(udev->bus);
1770 
1771 		/* set new power mode */
1772 		usbd_set_power_mode(udev, USB_POWER_MODE_SAVE);
1773 
1774 		/* wait for resume to complete */
1775 		usb_pause_mtx(NULL, hz / 4);
1776 
1777 		/* clear write reference */
1778 		USB_BUS_LOCK(udev->bus);
1779 		udev->pwr_save.write_refs--;
1780 		USB_BUS_UNLOCK(udev->bus);
1781 #endif
1782 		mode = USB_POWER_MODE_SAVE;
1783 		break;
1784 
1785 	case USB_POWER_MODE_SUSPEND:
1786 #if USB_HAVE_POWERD
1787 		/* let USB-powerd handle suspend */
1788 		USB_BUS_LOCK(udev->bus);
1789 		udev->pwr_save.last_xfer_time = ticks - (256 * hz);
1790 		USB_BUS_UNLOCK(udev->bus);
1791 #endif
1792 		mode = USB_POWER_MODE_SAVE;
1793 		break;
1794 
1795 	default:
1796 		return (EINVAL);
1797 	}
1798 
1799 	if (err)
1800 		return (ENXIO);		/* I/O failure */
1801 
1802 	/* if we are powered off we need to re-enumerate first */
1803 	if (old_mode == USB_POWER_MODE_OFF) {
1804 		if (udev->flags.usb_mode == USB_MODE_HOST &&
1805 		    udev->re_enumerate_wait == USB_RE_ENUM_DONE) {
1806 			udev->re_enumerate_wait = USB_RE_ENUM_START;
1807 		}
1808 		/* set power mode will wake up the explore thread */
1809 	}
1810 
1811 	/* set new power mode */
1812 	usbd_set_power_mode(udev, mode);
1813 
1814 	return (0);			/* success */
1815 }
1816 
1817 static int
1818 ugen_get_power_mode(struct usb_fifo *f)
1819 {
1820 	struct usb_device *udev = f->udev;
1821 
1822 	if (udev == NULL)
1823 		return (USB_POWER_MODE_ON);
1824 
1825 	return (udev->power_mode);
1826 }
1827 
1828 static int
1829 ugen_get_port_path(struct usb_fifo *f, struct usb_device_port_path *dpp)
1830 {
1831 	struct usb_device *udev = f->udev;
1832 	struct usb_device *next;
1833 	unsigned int nlevel = 0;
1834 
1835 	if (udev == NULL)
1836 		goto error;
1837 
1838 	dpp->udp_bus = device_get_unit(udev->bus->bdev);
1839 	dpp->udp_index = udev->device_index;
1840 
1841 	/* count port levels */
1842 	next = udev;
1843 	while (next->parent_hub != NULL) {
1844 		nlevel++;
1845 		next = next->parent_hub;
1846 	}
1847 
1848 	/* check if too many levels */
1849 	if (nlevel > USB_DEVICE_PORT_PATH_MAX)
1850 		goto error;
1851 
1852 	/* store port index array */
1853 	next = udev;
1854 	while (next->parent_hub != NULL) {
1855 		nlevel--;
1856 
1857 		dpp->udp_port_no[nlevel] = next->port_no;
1858 		dpp->udp_port_level = nlevel;
1859 
1860 		next = next->parent_hub;
1861 	}
1862 	return (0);	/* success */
1863 
1864 error:
1865 	return (EINVAL);	/* failure */
1866 }
1867 
1868 static int
1869 ugen_get_power_usage(struct usb_fifo *f)
1870 {
1871 	struct usb_device *udev = f->udev;
1872 
1873 	if (udev == NULL)
1874 		return (0);
1875 
1876 	return (udev->power);
1877 }
1878 
1879 static int
1880 ugen_do_port_feature(struct usb_fifo *f, uint8_t port_no,
1881     uint8_t set, uint16_t feature)
1882 {
1883 	struct usb_device *udev = f->udev;
1884 	struct usb_hub *hub;
1885 	int err;
1886 
1887 	err = priv_check(curthread, PRIV_DRIVER);
1888 	if (err) {
1889 		return (err);
1890 	}
1891 	if (port_no == 0) {
1892 		return (EINVAL);
1893 	}
1894 	if ((udev == NULL) ||
1895 	    (udev->hub == NULL)) {
1896 		return (EINVAL);
1897 	}
1898 	hub = udev->hub;
1899 
1900 	if (port_no > hub->nports) {
1901 		return (EINVAL);
1902 	}
1903 	if (set)
1904 		err = usbd_req_set_port_feature(udev,
1905 		    NULL, port_no, feature);
1906 	else
1907 		err = usbd_req_clear_port_feature(udev,
1908 		    NULL, port_no, feature);
1909 
1910 	if (err)
1911 		return (ENXIO);		/* failure */
1912 
1913 	return (0);			/* success */
1914 }
1915 
1916 static int
1917 ugen_iface_ioctl(struct usb_fifo *f, u_long cmd, void *addr, int fflags)
1918 {
1919 	struct usb_fifo *f_rx;
1920 	struct usb_fifo *f_tx;
1921 	int error = 0;
1922 
1923 	f_rx = f->udev->fifo[(f->fifo_index & ~1) + USB_FIFO_RX];
1924 	f_tx = f->udev->fifo[(f->fifo_index & ~1) + USB_FIFO_TX];
1925 
1926 	switch (cmd) {
1927 	case USB_SET_RX_SHORT_XFER:
1928 		if (fflags & FREAD) {
1929 			error = ugen_set_short_xfer(f_rx, addr);
1930 		} else {
1931 			error = EINVAL;
1932 		}
1933 		break;
1934 
1935 	case USB_SET_TX_FORCE_SHORT:
1936 		if (fflags & FWRITE) {
1937 			error = ugen_set_short_xfer(f_tx, addr);
1938 		} else {
1939 			error = EINVAL;
1940 		}
1941 		break;
1942 
1943 	case USB_SET_RX_TIMEOUT:
1944 		if (fflags & FREAD) {
1945 			error = ugen_set_timeout(f_rx, addr);
1946 		} else {
1947 			error = EINVAL;
1948 		}
1949 		break;
1950 
1951 	case USB_SET_TX_TIMEOUT:
1952 		if (fflags & FWRITE) {
1953 			error = ugen_set_timeout(f_tx, addr);
1954 		} else {
1955 			error = EINVAL;
1956 		}
1957 		break;
1958 
1959 	case USB_GET_RX_FRAME_SIZE:
1960 		if (fflags & FREAD) {
1961 			error = ugen_get_frame_size(f_rx, addr);
1962 		} else {
1963 			error = EINVAL;
1964 		}
1965 		break;
1966 
1967 	case USB_GET_TX_FRAME_SIZE:
1968 		if (fflags & FWRITE) {
1969 			error = ugen_get_frame_size(f_tx, addr);
1970 		} else {
1971 			error = EINVAL;
1972 		}
1973 		break;
1974 
1975 	case USB_SET_RX_BUFFER_SIZE:
1976 		if (fflags & FREAD) {
1977 			error = ugen_set_buffer_size(f_rx, addr);
1978 		} else {
1979 			error = EINVAL;
1980 		}
1981 		break;
1982 
1983 	case USB_SET_TX_BUFFER_SIZE:
1984 		if (fflags & FWRITE) {
1985 			error = ugen_set_buffer_size(f_tx, addr);
1986 		} else {
1987 			error = EINVAL;
1988 		}
1989 		break;
1990 
1991 	case USB_GET_RX_BUFFER_SIZE:
1992 		if (fflags & FREAD) {
1993 			error = ugen_get_buffer_size(f_rx, addr);
1994 		} else {
1995 			error = EINVAL;
1996 		}
1997 		break;
1998 
1999 	case USB_GET_TX_BUFFER_SIZE:
2000 		if (fflags & FWRITE) {
2001 			error = ugen_get_buffer_size(f_tx, addr);
2002 		} else {
2003 			error = EINVAL;
2004 		}
2005 		break;
2006 
2007 	case USB_GET_RX_INTERFACE_DESC:
2008 		if (fflags & FREAD) {
2009 			error = ugen_get_iface_desc(f_rx, addr);
2010 		} else {
2011 			error = EINVAL;
2012 		}
2013 		break;
2014 
2015 	case USB_GET_TX_INTERFACE_DESC:
2016 		if (fflags & FWRITE) {
2017 			error = ugen_get_iface_desc(f_tx, addr);
2018 		} else {
2019 			error = EINVAL;
2020 		}
2021 		break;
2022 
2023 	case USB_GET_RX_ENDPOINT_DESC:
2024 		if (fflags & FREAD) {
2025 			error = ugen_get_endpoint_desc(f_rx, addr);
2026 		} else {
2027 			error = EINVAL;
2028 		}
2029 		break;
2030 
2031 	case USB_GET_TX_ENDPOINT_DESC:
2032 		if (fflags & FWRITE) {
2033 			error = ugen_get_endpoint_desc(f_tx, addr);
2034 		} else {
2035 			error = EINVAL;
2036 		}
2037 		break;
2038 
2039 	case USB_SET_RX_STALL_FLAG:
2040 		if ((fflags & FREAD) && (*(int *)addr)) {
2041 			f_rx->flag_stall = 1;
2042 		}
2043 		break;
2044 
2045 	case USB_SET_TX_STALL_FLAG:
2046 		if ((fflags & FWRITE) && (*(int *)addr)) {
2047 			f_tx->flag_stall = 1;
2048 		}
2049 		break;
2050 
2051 	default:
2052 		error = ENOIOCTL;
2053 		break;
2054 	}
2055 	return (error);
2056 }
2057 
2058 static int
2059 ugen_ioctl_post(struct usb_fifo *f, u_long cmd, void *addr, int fflags)
2060 {
2061 	union {
2062 		struct usb_interface_descriptor *idesc;
2063 		struct usb_alt_interface *ai;
2064 		struct usb_device_descriptor *ddesc;
2065 		struct usb_config_descriptor *cdesc;
2066 		struct usb_device_stats *stat;
2067 		struct usb_fs_init *pinit;
2068 		struct usb_fs_uninit *puninit;
2069 		struct usb_device_port_path *dpp;
2070 		uint32_t *ptime;
2071 		void   *addr;
2072 		int    *pint;
2073 	}     u;
2074 	struct usb_device_descriptor *dtemp;
2075 	struct usb_config_descriptor *ctemp;
2076 	struct usb_interface *iface;
2077 	int error = 0;
2078 	uint8_t n;
2079 
2080 	u.addr = addr;
2081 
2082 	DPRINTFN(6, "cmd=0x%08lx\n", cmd);
2083 
2084 	switch (cmd) {
2085 	case USB_DISCOVER:
2086 		usb_needs_explore_all();
2087 		break;
2088 
2089 	case USB_SETDEBUG:
2090 		if (!(fflags & FWRITE)) {
2091 			error = EPERM;
2092 			break;
2093 		}
2094 		usb_debug = *(int *)addr;
2095 		break;
2096 
2097 	case USB_GET_CONFIG:
2098 		*(int *)addr = f->udev->curr_config_index;
2099 		break;
2100 
2101 	case USB_SET_CONFIG:
2102 		if (!(fflags & FWRITE)) {
2103 			error = EPERM;
2104 			break;
2105 		}
2106 		error = ugen_set_config(f, *(int *)addr);
2107 		break;
2108 
2109 	case USB_GET_ALTINTERFACE:
2110 		iface = usbd_get_iface(f->udev,
2111 		    u.ai->uai_interface_index);
2112 		if (iface && iface->idesc) {
2113 			u.ai->uai_alt_index = iface->alt_index;
2114 		} else {
2115 			error = EINVAL;
2116 		}
2117 		break;
2118 
2119 	case USB_SET_ALTINTERFACE:
2120 		if (!(fflags & FWRITE)) {
2121 			error = EPERM;
2122 			break;
2123 		}
2124 		error = ugen_set_interface(f,
2125 		    u.ai->uai_interface_index, u.ai->uai_alt_index);
2126 		break;
2127 
2128 	case USB_GET_DEVICE_DESC:
2129 		dtemp = usbd_get_device_descriptor(f->udev);
2130 		if (!dtemp) {
2131 			error = EIO;
2132 			break;
2133 		}
2134 		*u.ddesc = *dtemp;
2135 		break;
2136 
2137 	case USB_GET_CONFIG_DESC:
2138 		ctemp = usbd_get_config_descriptor(f->udev);
2139 		if (!ctemp) {
2140 			error = EIO;
2141 			break;
2142 		}
2143 		*u.cdesc = *ctemp;
2144 		break;
2145 
2146 	case USB_GET_FULL_DESC:
2147 		error = ugen_get_cdesc(f, addr);
2148 		break;
2149 
2150 	case USB_GET_STRING_DESC:
2151 		error = ugen_get_sdesc(f, addr);
2152 		break;
2153 
2154 	case USB_GET_IFACE_DRIVER:
2155 		error = ugen_get_iface_driver(f, addr);
2156 		break;
2157 
2158 	case USB_REQUEST:
2159 	case USB_DO_REQUEST:
2160 		if (!(fflags & FWRITE)) {
2161 			error = EPERM;
2162 			break;
2163 		}
2164 		error = ugen_do_request(f, addr);
2165 		break;
2166 
2167 	case USB_DEVICEINFO:
2168 	case USB_GET_DEVICEINFO:
2169 		error = usb_gen_fill_deviceinfo(f, addr);
2170 		break;
2171 
2172 	case USB_DEVICESTATS:
2173 		for (n = 0; n != 4; n++) {
2174 
2175 			u.stat->uds_requests_fail[n] =
2176 			    f->udev->bus->stats_err.uds_requests[n];
2177 
2178 			u.stat->uds_requests_ok[n] =
2179 			    f->udev->bus->stats_ok.uds_requests[n];
2180 		}
2181 		break;
2182 
2183 	case USB_DEVICEENUMERATE:
2184 		error = ugen_re_enumerate(f);
2185 		break;
2186 
2187 	case USB_GET_PLUGTIME:
2188 		*u.ptime = f->udev->plugtime;
2189 		break;
2190 
2191 	case USB_CLAIM_INTERFACE:
2192 	case USB_RELEASE_INTERFACE:
2193 		/* TODO */
2194 		break;
2195 
2196 	case USB_IFACE_DRIVER_ACTIVE:
2197 
2198 		n = *u.pint & 0xFF;
2199 
2200 		iface = usbd_get_iface(f->udev, n);
2201 
2202 		if (iface && iface->subdev)
2203 			error = 0;
2204 		else
2205 			error = ENXIO;
2206 		break;
2207 
2208 	case USB_IFACE_DRIVER_DETACH:
2209 
2210 		error = priv_check(curthread, PRIV_DRIVER);
2211 
2212 		if (error)
2213 			break;
2214 
2215 		n = *u.pint & 0xFF;
2216 
2217 		if (n == USB_IFACE_INDEX_ANY) {
2218 			error = EINVAL;
2219 			break;
2220 		}
2221 
2222 		/*
2223 		 * Detach the currently attached driver.
2224 		 */
2225 		usb_detach_device(f->udev, n, 0);
2226 
2227 		/*
2228 		 * Set parent to self, this should keep attach away
2229 		 * until the next set configuration event.
2230 		 */
2231 		usbd_set_parent_iface(f->udev, n, n);
2232 		break;
2233 
2234 	case USB_SET_POWER_MODE:
2235 		error = ugen_set_power_mode(f, *u.pint);
2236 		break;
2237 
2238 	case USB_GET_POWER_MODE:
2239 		*u.pint = ugen_get_power_mode(f);
2240 		break;
2241 
2242 	case USB_GET_DEV_PORT_PATH:
2243 		error = ugen_get_port_path(f, u.dpp);
2244 		break;
2245 
2246 	case USB_GET_POWER_USAGE:
2247 		*u.pint = ugen_get_power_usage(f);
2248 		break;
2249 
2250 	case USB_SET_PORT_ENABLE:
2251 		error = ugen_do_port_feature(f,
2252 		    *u.pint, 1, UHF_PORT_ENABLE);
2253 		break;
2254 
2255 	case USB_SET_PORT_DISABLE:
2256 		error = ugen_do_port_feature(f,
2257 		    *u.pint, 0, UHF_PORT_ENABLE);
2258 		break;
2259 
2260 	case USB_FS_INIT:
2261 		/* verify input parameters */
2262 		if (u.pinit->pEndpoints == NULL) {
2263 			error = EINVAL;
2264 			break;
2265 		}
2266 		if (u.pinit->ep_index_max > 127) {
2267 			error = EINVAL;
2268 			break;
2269 		}
2270 		if (u.pinit->ep_index_max == 0) {
2271 			error = EINVAL;
2272 			break;
2273 		}
2274 		if (f->fs_xfer != NULL) {
2275 			error = EBUSY;
2276 			break;
2277 		}
2278 		if (f->dev_ep_index != 0) {
2279 			error = EINVAL;
2280 			break;
2281 		}
2282 		if (ugen_fifo_in_use(f, fflags)) {
2283 			error = EBUSY;
2284 			break;
2285 		}
2286 		error = usb_fifo_alloc_buffer(f, 1, u.pinit->ep_index_max);
2287 		if (error) {
2288 			break;
2289 		}
2290 		f->fs_xfer = malloc(sizeof(f->fs_xfer[0]) *
2291 		    u.pinit->ep_index_max, M_USB, M_WAITOK | M_ZERO);
2292 		if (f->fs_xfer == NULL) {
2293 			usb_fifo_free_buffer(f);
2294 			error = ENOMEM;
2295 			break;
2296 		}
2297 		f->fs_ep_max = u.pinit->ep_index_max;
2298 		f->fs_ep_ptr = u.pinit->pEndpoints;
2299 		break;
2300 
2301 	case USB_FS_UNINIT:
2302 		if (u.puninit->dummy != 0) {
2303 			error = EINVAL;
2304 			break;
2305 		}
2306 		error = ugen_fs_uninit(f);
2307 		break;
2308 
2309 	default:
2310 		mtx_lock(f->priv_mtx);
2311 		error = ugen_iface_ioctl(f, cmd, addr, fflags);
2312 		mtx_unlock(f->priv_mtx);
2313 		break;
2314 	}
2315 	DPRINTFN(6, "error=%d\n", error);
2316 	return (error);
2317 }
2318 
2319 static void
2320 ugen_ctrl_fs_callback(struct usb_xfer *xfer, usb_error_t error)
2321 {
2322 	;				/* workaround for a bug in "indent" */
2323 
2324 	DPRINTF("st=%u alen=%u aframes=%u\n",
2325 	    USB_GET_STATE(xfer), xfer->actlen, xfer->aframes);
2326 
2327 	switch (USB_GET_STATE(xfer)) {
2328 	case USB_ST_SETUP:
2329 		usbd_transfer_submit(xfer);
2330 		break;
2331 	default:
2332 		ugen_fs_set_complete(xfer->priv_sc, USB_P2U(xfer->priv_fifo));
2333 		break;
2334 	}
2335 }
2336 #endif	/* USB_HAVE_UGEN */
2337