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