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