xref: /freebsd/sys/dev/usb/usb_generic.c (revision f4b37ed0f8b307b1f3f0f630ca725d68f1dff30d)
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 total level of ports */
1853 	dpp->udp_port_level = nlevel;
1854 
1855 	/* store port index array */
1856 	next = udev;
1857 	while (next->parent_hub != NULL) {
1858 		dpp->udp_port_no[--nlevel] = next->port_no;
1859 		next = next->parent_hub;
1860 	}
1861 	return (0);	/* success */
1862 
1863 error:
1864 	return (EINVAL);	/* failure */
1865 }
1866 
1867 static int
1868 ugen_get_power_usage(struct usb_fifo *f)
1869 {
1870 	struct usb_device *udev = f->udev;
1871 
1872 	if (udev == NULL)
1873 		return (0);
1874 
1875 	return (udev->power);
1876 }
1877 
1878 static int
1879 ugen_do_port_feature(struct usb_fifo *f, uint8_t port_no,
1880     uint8_t set, uint16_t feature)
1881 {
1882 	struct usb_device *udev = f->udev;
1883 	struct usb_hub *hub;
1884 	int err;
1885 
1886 	err = priv_check(curthread, PRIV_DRIVER);
1887 	if (err) {
1888 		return (err);
1889 	}
1890 	if (port_no == 0) {
1891 		return (EINVAL);
1892 	}
1893 	if ((udev == NULL) ||
1894 	    (udev->hub == NULL)) {
1895 		return (EINVAL);
1896 	}
1897 	hub = udev->hub;
1898 
1899 	if (port_no > hub->nports) {
1900 		return (EINVAL);
1901 	}
1902 	if (set)
1903 		err = usbd_req_set_port_feature(udev,
1904 		    NULL, port_no, feature);
1905 	else
1906 		err = usbd_req_clear_port_feature(udev,
1907 		    NULL, port_no, feature);
1908 
1909 	if (err)
1910 		return (ENXIO);		/* failure */
1911 
1912 	return (0);			/* success */
1913 }
1914 
1915 static int
1916 ugen_iface_ioctl(struct usb_fifo *f, u_long cmd, void *addr, int fflags)
1917 {
1918 	struct usb_fifo *f_rx;
1919 	struct usb_fifo *f_tx;
1920 	int error = 0;
1921 
1922 	f_rx = f->udev->fifo[(f->fifo_index & ~1) + USB_FIFO_RX];
1923 	f_tx = f->udev->fifo[(f->fifo_index & ~1) + USB_FIFO_TX];
1924 
1925 	switch (cmd) {
1926 	case USB_SET_RX_SHORT_XFER:
1927 		if (fflags & FREAD) {
1928 			error = ugen_set_short_xfer(f_rx, addr);
1929 		} else {
1930 			error = EINVAL;
1931 		}
1932 		break;
1933 
1934 	case USB_SET_TX_FORCE_SHORT:
1935 		if (fflags & FWRITE) {
1936 			error = ugen_set_short_xfer(f_tx, addr);
1937 		} else {
1938 			error = EINVAL;
1939 		}
1940 		break;
1941 
1942 	case USB_SET_RX_TIMEOUT:
1943 		if (fflags & FREAD) {
1944 			error = ugen_set_timeout(f_rx, addr);
1945 		} else {
1946 			error = EINVAL;
1947 		}
1948 		break;
1949 
1950 	case USB_SET_TX_TIMEOUT:
1951 		if (fflags & FWRITE) {
1952 			error = ugen_set_timeout(f_tx, addr);
1953 		} else {
1954 			error = EINVAL;
1955 		}
1956 		break;
1957 
1958 	case USB_GET_RX_FRAME_SIZE:
1959 		if (fflags & FREAD) {
1960 			error = ugen_get_frame_size(f_rx, addr);
1961 		} else {
1962 			error = EINVAL;
1963 		}
1964 		break;
1965 
1966 	case USB_GET_TX_FRAME_SIZE:
1967 		if (fflags & FWRITE) {
1968 			error = ugen_get_frame_size(f_tx, addr);
1969 		} else {
1970 			error = EINVAL;
1971 		}
1972 		break;
1973 
1974 	case USB_SET_RX_BUFFER_SIZE:
1975 		if (fflags & FREAD) {
1976 			error = ugen_set_buffer_size(f_rx, addr);
1977 		} else {
1978 			error = EINVAL;
1979 		}
1980 		break;
1981 
1982 	case USB_SET_TX_BUFFER_SIZE:
1983 		if (fflags & FWRITE) {
1984 			error = ugen_set_buffer_size(f_tx, addr);
1985 		} else {
1986 			error = EINVAL;
1987 		}
1988 		break;
1989 
1990 	case USB_GET_RX_BUFFER_SIZE:
1991 		if (fflags & FREAD) {
1992 			error = ugen_get_buffer_size(f_rx, addr);
1993 		} else {
1994 			error = EINVAL;
1995 		}
1996 		break;
1997 
1998 	case USB_GET_TX_BUFFER_SIZE:
1999 		if (fflags & FWRITE) {
2000 			error = ugen_get_buffer_size(f_tx, addr);
2001 		} else {
2002 			error = EINVAL;
2003 		}
2004 		break;
2005 
2006 	case USB_GET_RX_INTERFACE_DESC:
2007 		if (fflags & FREAD) {
2008 			error = ugen_get_iface_desc(f_rx, addr);
2009 		} else {
2010 			error = EINVAL;
2011 		}
2012 		break;
2013 
2014 	case USB_GET_TX_INTERFACE_DESC:
2015 		if (fflags & FWRITE) {
2016 			error = ugen_get_iface_desc(f_tx, addr);
2017 		} else {
2018 			error = EINVAL;
2019 		}
2020 		break;
2021 
2022 	case USB_GET_RX_ENDPOINT_DESC:
2023 		if (fflags & FREAD) {
2024 			error = ugen_get_endpoint_desc(f_rx, addr);
2025 		} else {
2026 			error = EINVAL;
2027 		}
2028 		break;
2029 
2030 	case USB_GET_TX_ENDPOINT_DESC:
2031 		if (fflags & FWRITE) {
2032 			error = ugen_get_endpoint_desc(f_tx, addr);
2033 		} else {
2034 			error = EINVAL;
2035 		}
2036 		break;
2037 
2038 	case USB_SET_RX_STALL_FLAG:
2039 		if ((fflags & FREAD) && (*(int *)addr)) {
2040 			f_rx->flag_stall = 1;
2041 		}
2042 		break;
2043 
2044 	case USB_SET_TX_STALL_FLAG:
2045 		if ((fflags & FWRITE) && (*(int *)addr)) {
2046 			f_tx->flag_stall = 1;
2047 		}
2048 		break;
2049 
2050 	default:
2051 		error = ENOIOCTL;
2052 		break;
2053 	}
2054 	return (error);
2055 }
2056 
2057 static int
2058 ugen_ioctl_post(struct usb_fifo *f, u_long cmd, void *addr, int fflags)
2059 {
2060 	union {
2061 		struct usb_interface_descriptor *idesc;
2062 		struct usb_alt_interface *ai;
2063 		struct usb_device_descriptor *ddesc;
2064 		struct usb_config_descriptor *cdesc;
2065 		struct usb_device_stats *stat;
2066 		struct usb_fs_init *pinit;
2067 		struct usb_fs_uninit *puninit;
2068 		struct usb_device_port_path *dpp;
2069 		uint32_t *ptime;
2070 		void   *addr;
2071 		int    *pint;
2072 	}     u;
2073 	struct usb_device_descriptor *dtemp;
2074 	struct usb_config_descriptor *ctemp;
2075 	struct usb_interface *iface;
2076 	int error = 0;
2077 	uint8_t n;
2078 
2079 	u.addr = addr;
2080 
2081 	DPRINTFN(6, "cmd=0x%08lx\n", cmd);
2082 
2083 	switch (cmd) {
2084 	case USB_DISCOVER:
2085 		usb_needs_explore_all();
2086 		break;
2087 
2088 	case USB_SETDEBUG:
2089 		if (!(fflags & FWRITE)) {
2090 			error = EPERM;
2091 			break;
2092 		}
2093 		usb_debug = *(int *)addr;
2094 		break;
2095 
2096 	case USB_GET_CONFIG:
2097 		*(int *)addr = f->udev->curr_config_index;
2098 		break;
2099 
2100 	case USB_SET_CONFIG:
2101 		if (!(fflags & FWRITE)) {
2102 			error = EPERM;
2103 			break;
2104 		}
2105 		error = ugen_set_config(f, *(int *)addr);
2106 		break;
2107 
2108 	case USB_GET_ALTINTERFACE:
2109 		iface = usbd_get_iface(f->udev,
2110 		    u.ai->uai_interface_index);
2111 		if (iface && iface->idesc) {
2112 			u.ai->uai_alt_index = iface->alt_index;
2113 		} else {
2114 			error = EINVAL;
2115 		}
2116 		break;
2117 
2118 	case USB_SET_ALTINTERFACE:
2119 		if (!(fflags & FWRITE)) {
2120 			error = EPERM;
2121 			break;
2122 		}
2123 		error = ugen_set_interface(f,
2124 		    u.ai->uai_interface_index, u.ai->uai_alt_index);
2125 		break;
2126 
2127 	case USB_GET_DEVICE_DESC:
2128 		dtemp = usbd_get_device_descriptor(f->udev);
2129 		if (!dtemp) {
2130 			error = EIO;
2131 			break;
2132 		}
2133 		*u.ddesc = *dtemp;
2134 		break;
2135 
2136 	case USB_GET_CONFIG_DESC:
2137 		ctemp = usbd_get_config_descriptor(f->udev);
2138 		if (!ctemp) {
2139 			error = EIO;
2140 			break;
2141 		}
2142 		*u.cdesc = *ctemp;
2143 		break;
2144 
2145 	case USB_GET_FULL_DESC:
2146 		error = ugen_get_cdesc(f, addr);
2147 		break;
2148 
2149 	case USB_GET_STRING_DESC:
2150 		error = ugen_get_sdesc(f, addr);
2151 		break;
2152 
2153 	case USB_GET_IFACE_DRIVER:
2154 		error = ugen_get_iface_driver(f, addr);
2155 		break;
2156 
2157 	case USB_REQUEST:
2158 	case USB_DO_REQUEST:
2159 		if (!(fflags & FWRITE)) {
2160 			error = EPERM;
2161 			break;
2162 		}
2163 		error = ugen_do_request(f, addr);
2164 		break;
2165 
2166 	case USB_DEVICEINFO:
2167 	case USB_GET_DEVICEINFO:
2168 		error = usb_gen_fill_deviceinfo(f, addr);
2169 		break;
2170 
2171 	case USB_DEVICESTATS:
2172 		for (n = 0; n != 4; n++) {
2173 
2174 			u.stat->uds_requests_fail[n] =
2175 			    f->udev->bus->stats_err.uds_requests[n];
2176 
2177 			u.stat->uds_requests_ok[n] =
2178 			    f->udev->bus->stats_ok.uds_requests[n];
2179 		}
2180 		break;
2181 
2182 	case USB_DEVICEENUMERATE:
2183 		error = ugen_re_enumerate(f);
2184 		break;
2185 
2186 	case USB_GET_PLUGTIME:
2187 		*u.ptime = f->udev->plugtime;
2188 		break;
2189 
2190 	case USB_CLAIM_INTERFACE:
2191 	case USB_RELEASE_INTERFACE:
2192 		/* TODO */
2193 		break;
2194 
2195 	case USB_IFACE_DRIVER_ACTIVE:
2196 
2197 		n = *u.pint & 0xFF;
2198 
2199 		iface = usbd_get_iface(f->udev, n);
2200 
2201 		if (iface && iface->subdev)
2202 			error = 0;
2203 		else
2204 			error = ENXIO;
2205 		break;
2206 
2207 	case USB_IFACE_DRIVER_DETACH:
2208 
2209 		error = priv_check(curthread, PRIV_DRIVER);
2210 
2211 		if (error)
2212 			break;
2213 
2214 		n = *u.pint & 0xFF;
2215 
2216 		if (n == USB_IFACE_INDEX_ANY) {
2217 			error = EINVAL;
2218 			break;
2219 		}
2220 
2221 		/*
2222 		 * Detach the currently attached driver.
2223 		 */
2224 		usb_detach_device(f->udev, n, 0);
2225 
2226 		/*
2227 		 * Set parent to self, this should keep attach away
2228 		 * until the next set configuration event.
2229 		 */
2230 		usbd_set_parent_iface(f->udev, n, n);
2231 		break;
2232 
2233 	case USB_SET_POWER_MODE:
2234 		error = ugen_set_power_mode(f, *u.pint);
2235 		break;
2236 
2237 	case USB_GET_POWER_MODE:
2238 		*u.pint = ugen_get_power_mode(f);
2239 		break;
2240 
2241 	case USB_GET_DEV_PORT_PATH:
2242 		error = ugen_get_port_path(f, u.dpp);
2243 		break;
2244 
2245 	case USB_GET_POWER_USAGE:
2246 		*u.pint = ugen_get_power_usage(f);
2247 		break;
2248 
2249 	case USB_SET_PORT_ENABLE:
2250 		error = ugen_do_port_feature(f,
2251 		    *u.pint, 1, UHF_PORT_ENABLE);
2252 		break;
2253 
2254 	case USB_SET_PORT_DISABLE:
2255 		error = ugen_do_port_feature(f,
2256 		    *u.pint, 0, UHF_PORT_ENABLE);
2257 		break;
2258 
2259 	case USB_FS_INIT:
2260 		/* verify input parameters */
2261 		if (u.pinit->pEndpoints == NULL) {
2262 			error = EINVAL;
2263 			break;
2264 		}
2265 		if (u.pinit->ep_index_max > 127) {
2266 			error = EINVAL;
2267 			break;
2268 		}
2269 		if (u.pinit->ep_index_max == 0) {
2270 			error = EINVAL;
2271 			break;
2272 		}
2273 		if (f->fs_xfer != NULL) {
2274 			error = EBUSY;
2275 			break;
2276 		}
2277 		if (f->dev_ep_index != 0) {
2278 			error = EINVAL;
2279 			break;
2280 		}
2281 		if (ugen_fifo_in_use(f, fflags)) {
2282 			error = EBUSY;
2283 			break;
2284 		}
2285 		error = usb_fifo_alloc_buffer(f, 1, u.pinit->ep_index_max);
2286 		if (error) {
2287 			break;
2288 		}
2289 		f->fs_xfer = malloc(sizeof(f->fs_xfer[0]) *
2290 		    u.pinit->ep_index_max, M_USB, M_WAITOK | M_ZERO);
2291 		if (f->fs_xfer == NULL) {
2292 			usb_fifo_free_buffer(f);
2293 			error = ENOMEM;
2294 			break;
2295 		}
2296 		f->fs_ep_max = u.pinit->ep_index_max;
2297 		f->fs_ep_ptr = u.pinit->pEndpoints;
2298 		break;
2299 
2300 	case USB_FS_UNINIT:
2301 		if (u.puninit->dummy != 0) {
2302 			error = EINVAL;
2303 			break;
2304 		}
2305 		error = ugen_fs_uninit(f);
2306 		break;
2307 
2308 	default:
2309 		mtx_lock(f->priv_mtx);
2310 		error = ugen_iface_ioctl(f, cmd, addr, fflags);
2311 		mtx_unlock(f->priv_mtx);
2312 		break;
2313 	}
2314 	DPRINTFN(6, "error=%d\n", error);
2315 	return (error);
2316 }
2317 
2318 static void
2319 ugen_ctrl_fs_callback(struct usb_xfer *xfer, usb_error_t error)
2320 {
2321 	;				/* workaround for a bug in "indent" */
2322 
2323 	DPRINTF("st=%u alen=%u aframes=%u\n",
2324 	    USB_GET_STATE(xfer), xfer->actlen, xfer->aframes);
2325 
2326 	switch (USB_GET_STATE(xfer)) {
2327 	case USB_ST_SETUP:
2328 		usbd_transfer_submit(xfer);
2329 		break;
2330 	default:
2331 		ugen_fs_set_complete(xfer->priv_sc, USB_P2U(xfer->priv_fifo));
2332 		break;
2333 	}
2334 }
2335 #endif	/* USB_HAVE_UGEN */
2336