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