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