xref: /freebsd/sys/dev/usb/usb_dev.c (revision 908e960ea6343acd9515d89d5d5696f9d8bf090c)
1 /* $FreeBSD$ */
2 /*-
3  * Copyright (c) 2006-2008 Hans Petter Selasky. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  *
27  * usb2_dev.c - An abstraction layer for creating devices under /dev/...
28  */
29 
30 #include <dev/usb/usb.h>
31 #include <dev/usb/usb_ioctl.h>
32 #include <dev/usb/usb_mfunc.h>
33 #include <dev/usb/usb_error.h>
34 
35 #define	USB_DEBUG_VAR usb2_fifo_debug
36 
37 #include <dev/usb/usb_core.h>
38 #include <dev/usb/usb_mbuf.h>
39 #include <dev/usb/usb_dev.h>
40 #include <dev/usb/usb_process.h>
41 #include <dev/usb/usb_device.h>
42 #include <dev/usb/usb_debug.h>
43 #include <dev/usb/usb_busdma.h>
44 #include <dev/usb/usb_generic.h>
45 #include <dev/usb/usb_dynamic.h>
46 #include <dev/usb/usb_util.h>
47 
48 #include <dev/usb/usb_controller.h>
49 #include <dev/usb/usb_bus.h>
50 
51 #include <sys/filio.h>
52 #include <sys/ttycom.h>
53 #include <sys/syscallsubr.h>
54 
55 #include <machine/stdarg.h>
56 
57 #if USB_HAVE_UGEN
58 
59 #if USB_DEBUG
60 static int usb2_fifo_debug = 0;
61 
62 SYSCTL_NODE(_hw_usb, OID_AUTO, dev, CTLFLAG_RW, 0, "USB device");
63 SYSCTL_INT(_hw_usb_dev, OID_AUTO, debug, CTLFLAG_RW,
64     &usb2_fifo_debug, 0, "Debug Level");
65 #endif
66 
67 #if ((__FreeBSD_version >= 700001) || (__FreeBSD_version == 0) || \
68      ((__FreeBSD_version >= 600034) && (__FreeBSD_version < 700000)))
69 #define	USB_UCRED struct ucred *ucred,
70 #else
71 #define	USB_UCRED
72 #endif
73 
74 /* prototypes */
75 
76 static int	usb2_fifo_open(struct usb_cdev_privdata *,
77 		    struct usb_fifo *, int);
78 static void	usb2_fifo_close(struct usb_fifo *, int);
79 static void	usb2_dev_init(void *);
80 static void	usb2_dev_init_post(void *);
81 static void	usb2_dev_uninit(void *);
82 static int	usb2_fifo_uiomove(struct usb_fifo *, void *, int,
83 		    struct uio *);
84 static void	usb2_fifo_check_methods(struct usb_fifo_methods *);
85 static struct	usb_fifo *usb2_fifo_alloc(void);
86 static struct	usb_pipe *usb2_dev_get_pipe(struct usb_device *, uint8_t,
87 		    uint8_t);
88 static void	usb2_loc_fill(struct usb_fs_privdata *,
89 		    struct usb_cdev_privdata *);
90 static void	usb2_close(void *);
91 static usb_error_t usb2_ref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *, int);
92 static usb_error_t usb2_usb_ref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *);
93 static void	usb2_unref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *);
94 
95 static d_open_t usb2_open;
96 static d_ioctl_t usb2_ioctl;
97 static d_read_t usb2_read;
98 static d_write_t usb2_write;
99 static d_poll_t usb2_poll;
100 
101 static d_ioctl_t usb2_static_ioctl;
102 
103 static usb_fifo_open_t usb2_fifo_dummy_open;
104 static usb_fifo_close_t usb2_fifo_dummy_close;
105 static usb_fifo_ioctl_t usb2_fifo_dummy_ioctl;
106 static usb_fifo_cmd_t usb2_fifo_dummy_cmd;
107 
108 /* character device structure used for devices (/dev/ugenX.Y and /dev/uXXX) */
109 struct cdevsw usb2_devsw = {
110 	.d_version = D_VERSION,
111 	.d_open = usb2_open,
112 	.d_ioctl = usb2_ioctl,
113 	.d_name = "usbdev",
114 	.d_flags = D_TRACKCLOSE,
115 	.d_read = usb2_read,
116 	.d_write = usb2_write,
117 	.d_poll = usb2_poll
118 };
119 
120 static struct cdev* usb2_dev = NULL;
121 
122 /* character device structure used for /dev/usb */
123 struct cdevsw usb2_static_devsw = {
124 	.d_version = D_VERSION,
125 	.d_ioctl = usb2_static_ioctl,
126 	.d_name = "usb"
127 };
128 
129 static TAILQ_HEAD(, usb_symlink) usb2_sym_head;
130 static struct sx usb2_sym_lock;
131 
132 struct mtx usb2_ref_lock;
133 
134 /*------------------------------------------------------------------------*
135  *	usb2_loc_fill
136  *
137  * This is used to fill out a usb_cdev_privdata structure based on the
138  * device's address as contained in usb_fs_privdata.
139  *------------------------------------------------------------------------*/
140 static void
141 usb2_loc_fill(struct usb_fs_privdata* pd, struct usb_cdev_privdata *cpd)
142 {
143 	cpd->bus_index = pd->bus_index;
144 	cpd->dev_index = pd->dev_index;
145 	cpd->ep_addr = pd->ep_addr;
146 	cpd->fifo_index = pd->fifo_index;
147 }
148 
149 /*------------------------------------------------------------------------*
150  *	usb2_ref_device
151  *
152  * This function is used to atomically refer an USB device by its
153  * device location. If this function returns success the USB device
154  * will not dissappear until the USB device is unreferenced.
155  *
156  * Return values:
157  *  0: Success, refcount incremented on the given USB device.
158  *  Else: Failure.
159  *------------------------------------------------------------------------*/
160 usb_error_t
161 usb2_ref_device(struct usb_cdev_privdata *cpd,
162     struct usb_cdev_refdata *crd, int need_uref)
163 {
164 	struct usb_fifo **ppf;
165 	struct usb_fifo *f;
166 
167 	DPRINTFN(2, "cpd=%p need uref=%d\n", cpd, need_uref);
168 
169 	/* clear all refs */
170 	memset(crd, 0, sizeof(*crd));
171 
172 	mtx_lock(&usb2_ref_lock);
173 	cpd->bus = devclass_get_softc(usb2_devclass_ptr, cpd->bus_index);
174 	if (cpd->bus == NULL) {
175 		DPRINTFN(2, "no bus at %u\n", cpd->bus_index);
176 		goto error;
177 	}
178 	cpd->udev = cpd->bus->devices[cpd->dev_index];
179 	if (cpd->udev == NULL) {
180 		DPRINTFN(2, "no device at %u\n", cpd->dev_index);
181 		goto error;
182 	}
183 	if (cpd->udev->refcount == USB_DEV_REF_MAX) {
184 		DPRINTFN(2, "no dev ref\n");
185 		goto error;
186 	}
187 	if (need_uref) {
188 		DPRINTFN(2, "ref udev - needed\n");
189 		cpd->udev->refcount++;
190 
191 		mtx_unlock(&usb2_ref_lock);
192 
193 		/*
194 		 * We need to grab the sx-lock before grabbing the
195 		 * FIFO refs to avoid deadlock at detach!
196 		 */
197 		sx_xlock(cpd->udev->default_sx + 1);
198 
199 		mtx_lock(&usb2_ref_lock);
200 
201 		/*
202 		 * Set "is_uref" after grabbing the default SX lock
203 		 */
204 		crd->is_uref = 1;
205 	}
206 
207 	/* check if we are doing an open */
208 	if (cpd->fflags == 0) {
209 		/* use zero defaults */
210 	} else {
211 		/* check for write */
212 		if (cpd->fflags & FWRITE) {
213 			ppf = cpd->udev->fifo;
214 			f = ppf[cpd->fifo_index + USB_FIFO_TX];
215 			crd->txfifo = f;
216 			crd->is_write = 1;	/* ref */
217 			if (f == NULL || f->refcount == USB_FIFO_REF_MAX)
218 				goto error;
219 			if (f->curr_cpd != cpd)
220 				goto error;
221 			/* check if USB-FS is active */
222 			if (f->fs_ep_max != 0) {
223 				crd->is_usbfs = 1;
224 			}
225 		}
226 
227 		/* check for read */
228 		if (cpd->fflags & FREAD) {
229 			ppf = cpd->udev->fifo;
230 			f = ppf[cpd->fifo_index + USB_FIFO_RX];
231 			crd->rxfifo = f;
232 			crd->is_read = 1;	/* ref */
233 			if (f == NULL || f->refcount == USB_FIFO_REF_MAX)
234 				goto error;
235 			if (f->curr_cpd != cpd)
236 				goto error;
237 			/* check if USB-FS is active */
238 			if (f->fs_ep_max != 0) {
239 				crd->is_usbfs = 1;
240 			}
241 		}
242 	}
243 
244 	/* when everything is OK we increment the refcounts */
245 	if (crd->is_write) {
246 		DPRINTFN(2, "ref write\n");
247 		crd->txfifo->refcount++;
248 	}
249 	if (crd->is_read) {
250 		DPRINTFN(2, "ref read\n");
251 		crd->rxfifo->refcount++;
252 	}
253 	mtx_unlock(&usb2_ref_lock);
254 
255 	if (crd->is_uref) {
256 		mtx_lock(&Giant);	/* XXX */
257 	}
258 	return (0);
259 
260 error:
261 	if (crd->is_uref) {
262 		sx_unlock(cpd->udev->default_sx + 1);
263 		if (--(cpd->udev->refcount) == 0) {
264 			usb2_cv_signal(cpd->udev->default_cv + 1);
265 		}
266 	}
267 	mtx_unlock(&usb2_ref_lock);
268 	DPRINTFN(2, "fail\n");
269 	return (USB_ERR_INVAL);
270 }
271 
272 /*------------------------------------------------------------------------*
273  *	usb2_usb_ref_device
274  *
275  * This function is used to upgrade an USB reference to include the
276  * USB device reference on a USB location.
277  *
278  * Return values:
279  *  0: Success, refcount incremented on the given USB device.
280  *  Else: Failure.
281  *------------------------------------------------------------------------*/
282 static usb_error_t
283 usb2_usb_ref_device(struct usb_cdev_privdata *cpd,
284     struct usb_cdev_refdata *crd)
285 {
286 	/*
287 	 * Check if we already got an USB reference on this location:
288 	 */
289 	if (crd->is_uref)
290 		return (0);		/* success */
291 
292 	/*
293 	 * To avoid deadlock at detach we need to drop the FIFO ref
294 	 * and re-acquire a new ref!
295 	 */
296 	usb2_unref_device(cpd, crd);
297 
298 	return (usb2_ref_device(cpd, crd, 1 /* need uref */));
299 }
300 
301 /*------------------------------------------------------------------------*
302  *	usb2_unref_device
303  *
304  * This function will release the reference count by one unit for the
305  * given USB device.
306  *------------------------------------------------------------------------*/
307 void
308 usb2_unref_device(struct usb_cdev_privdata *cpd,
309     struct usb_cdev_refdata *crd)
310 {
311 
312 	DPRINTFN(2, "cpd=%p is_uref=%d\n", cpd, crd->is_uref);
313 
314 	if (crd->is_uref) {
315 		mtx_unlock(&Giant);	/* XXX */
316 		sx_unlock(cpd->udev->default_sx + 1);
317 	}
318 	mtx_lock(&usb2_ref_lock);
319 	if (crd->is_read) {
320 		if (--(crd->rxfifo->refcount) == 0) {
321 			usb2_cv_signal(&crd->rxfifo->cv_drain);
322 		}
323 		crd->is_read = 0;
324 	}
325 	if (crd->is_write) {
326 		if (--(crd->txfifo->refcount) == 0) {
327 			usb2_cv_signal(&crd->txfifo->cv_drain);
328 		}
329 		crd->is_write = 0;
330 	}
331 	if (crd->is_uref) {
332 		if (--(cpd->udev->refcount) == 0) {
333 			usb2_cv_signal(cpd->udev->default_cv + 1);
334 		}
335 		crd->is_uref = 0;
336 	}
337 	mtx_unlock(&usb2_ref_lock);
338 }
339 
340 static struct usb_fifo *
341 usb2_fifo_alloc(void)
342 {
343 	struct usb_fifo *f;
344 
345 	f = malloc(sizeof(*f), M_USBDEV, M_WAITOK | M_ZERO);
346 	if (f) {
347 		usb2_cv_init(&f->cv_io, "FIFO-IO");
348 		usb2_cv_init(&f->cv_drain, "FIFO-DRAIN");
349 		f->refcount = 1;
350 	}
351 	return (f);
352 }
353 
354 /*------------------------------------------------------------------------*
355  *	usb2_fifo_create
356  *------------------------------------------------------------------------*/
357 static int
358 usb2_fifo_create(struct usb_cdev_privdata *cpd,
359     struct usb_cdev_refdata *crd)
360 {
361 	struct usb_device *udev = cpd->udev;
362 	struct usb_fifo *f;
363 	struct usb_pipe *pipe;
364 	uint8_t n;
365 	uint8_t is_tx;
366 	uint8_t is_rx;
367 	uint8_t no_null;
368 	uint8_t is_busy;
369 	int ep = cpd->ep_addr;
370 
371 	is_tx = (cpd->fflags & FWRITE) ? 1 : 0;
372 	is_rx = (cpd->fflags & FREAD) ? 1 : 0;
373 	no_null = 1;
374 	is_busy = 0;
375 
376 	/* Preallocated FIFO */
377 	if (ep < 0) {
378 		DPRINTFN(5, "Preallocated FIFO\n");
379 		if (is_tx) {
380 			f = udev->fifo[cpd->fifo_index + USB_FIFO_TX];
381 			if (f == NULL)
382 				return (EINVAL);
383 			crd->txfifo = f;
384 		}
385 		if (is_rx) {
386 			f = udev->fifo[cpd->fifo_index + USB_FIFO_RX];
387 			if (f == NULL)
388 				return (EINVAL);
389 			crd->rxfifo = f;
390 		}
391 		return (0);
392 	}
393 
394 	KASSERT(ep >= 0 && ep <= 15, ("endpoint %d out of range", ep));
395 
396 	/* search for a free FIFO slot */
397 	DPRINTFN(5, "Endpoint device, searching for 0x%02x\n", ep);
398 	for (n = 0;; n += 2) {
399 
400 		if (n == USB_FIFO_MAX) {
401 			if (no_null) {
402 				no_null = 0;
403 				n = 0;
404 			} else {
405 				/* end of FIFOs reached */
406 				DPRINTFN(5, "out of FIFOs\n");
407 				return (ENOMEM);
408 			}
409 		}
410 		/* Check for TX FIFO */
411 		if (is_tx) {
412 			f = udev->fifo[n + USB_FIFO_TX];
413 			if (f != NULL) {
414 				if (f->dev_ep_index != ep) {
415 					/* wrong endpoint index */
416 					continue;
417 				}
418 				if (f->curr_cpd != NULL) {
419 					/* FIFO is opened */
420 					is_busy = 1;
421 					continue;
422 				}
423 			} else if (no_null) {
424 				continue;
425 			}
426 		}
427 		/* Check for RX FIFO */
428 		if (is_rx) {
429 			f = udev->fifo[n + USB_FIFO_RX];
430 			if (f != NULL) {
431 				if (f->dev_ep_index != ep) {
432 					/* wrong endpoint index */
433 					continue;
434 				}
435 				if (f->curr_cpd != NULL) {
436 					/* FIFO is opened */
437 					is_busy = 1;
438 					continue;
439 				}
440 			} else if (no_null) {
441 				continue;
442 			}
443 		}
444 		break;
445 	}
446 
447 	if (no_null == 0) {
448 		if (ep >= (USB_EP_MAX / 2)) {
449 			/* we don't create any endpoints in this range */
450 			DPRINTFN(5, "ep out of range\n");
451 			return (is_busy ? EBUSY : EINVAL);
452 		}
453 	}
454 
455 	if ((ep != 0) && is_busy) {
456 		/*
457 		 * Only the default control endpoint is allowed to be
458 		 * opened multiple times!
459 		 */
460 		DPRINTFN(5, "busy\n");
461 		return (EBUSY);
462 	}
463 
464 	/* Check TX FIFO */
465 	if (is_tx &&
466 	    (udev->fifo[n + USB_FIFO_TX] == NULL)) {
467 		pipe = usb2_dev_get_pipe(udev, ep, USB_FIFO_TX);
468 		DPRINTFN(5, "dev_get_pipe(%d, 0x%x)\n", ep, USB_FIFO_TX);
469 		if (pipe == NULL) {
470 			DPRINTFN(5, "dev_get_pipe returned NULL\n");
471 			return (EINVAL);
472 		}
473 		f = usb2_fifo_alloc();
474 		if (f == NULL) {
475 			DPRINTFN(5, "could not alloc tx fifo\n");
476 			return (ENOMEM);
477 		}
478 		/* update some fields */
479 		f->fifo_index = n + USB_FIFO_TX;
480 		f->dev_ep_index = ep;
481 		f->priv_mtx = udev->default_mtx;
482 		f->priv_sc0 = pipe;
483 		f->methods = &usb2_ugen_methods;
484 		f->iface_index = pipe->iface_index;
485 		f->udev = udev;
486 		mtx_lock(&usb2_ref_lock);
487 		udev->fifo[n + USB_FIFO_TX] = f;
488 		mtx_unlock(&usb2_ref_lock);
489 	}
490 	/* Check RX FIFO */
491 	if (is_rx &&
492 	    (udev->fifo[n + USB_FIFO_RX] == NULL)) {
493 
494 		pipe = usb2_dev_get_pipe(udev, ep, USB_FIFO_RX);
495 		DPRINTFN(5, "dev_get_pipe(%d, 0x%x)\n", ep, USB_FIFO_RX);
496 		if (pipe == NULL) {
497 			DPRINTFN(5, "dev_get_pipe returned NULL\n");
498 			return (EINVAL);
499 		}
500 		f = usb2_fifo_alloc();
501 		if (f == NULL) {
502 			DPRINTFN(5, "could not alloc rx fifo\n");
503 			return (ENOMEM);
504 		}
505 		/* update some fields */
506 		f->fifo_index = n + USB_FIFO_RX;
507 		f->dev_ep_index = ep;
508 		f->priv_mtx = udev->default_mtx;
509 		f->priv_sc0 = pipe;
510 		f->methods = &usb2_ugen_methods;
511 		f->iface_index = pipe->iface_index;
512 		f->udev = udev;
513 		mtx_lock(&usb2_ref_lock);
514 		udev->fifo[n + USB_FIFO_RX] = f;
515 		mtx_unlock(&usb2_ref_lock);
516 	}
517 	if (is_tx) {
518 		crd->txfifo = udev->fifo[n + USB_FIFO_TX];
519 	}
520 	if (is_rx) {
521 		crd->rxfifo = udev->fifo[n + USB_FIFO_RX];
522 	}
523 	/* fill out fifo index */
524 	DPRINTFN(5, "fifo index = %d\n", n);
525 	cpd->fifo_index = n;
526 
527 	/* complete */
528 
529 	return (0);
530 }
531 
532 void
533 usb2_fifo_free(struct usb_fifo *f)
534 {
535 	uint8_t n;
536 
537 	if (f == NULL) {
538 		/* be NULL safe */
539 		return;
540 	}
541 	/* destroy symlink devices, if any */
542 	for (n = 0; n != 2; n++) {
543 		if (f->symlink[n]) {
544 			usb2_free_symlink(f->symlink[n]);
545 			f->symlink[n] = NULL;
546 		}
547 	}
548 	mtx_lock(&usb2_ref_lock);
549 
550 	/* delink ourselves to stop calls from userland */
551 	if ((f->fifo_index < USB_FIFO_MAX) &&
552 	    (f->udev != NULL) &&
553 	    (f->udev->fifo[f->fifo_index] == f)) {
554 		f->udev->fifo[f->fifo_index] = NULL;
555 	} else {
556 		DPRINTFN(0, "USB FIFO %p has not been linked!\n", f);
557 	}
558 
559 	/* decrease refcount */
560 	f->refcount--;
561 	/* prevent any write flush */
562 	f->flag_iserror = 1;
563 	/* need to wait until all callers have exited */
564 	while (f->refcount != 0) {
565 		mtx_unlock(&usb2_ref_lock);	/* avoid LOR */
566 		mtx_lock(f->priv_mtx);
567 		/* get I/O thread out of any sleep state */
568 		if (f->flag_sleeping) {
569 			f->flag_sleeping = 0;
570 			usb2_cv_broadcast(&f->cv_io);
571 		}
572 		mtx_unlock(f->priv_mtx);
573 		mtx_lock(&usb2_ref_lock);
574 
575 		/* wait for sync */
576 		usb2_cv_wait(&f->cv_drain, &usb2_ref_lock);
577 	}
578 	mtx_unlock(&usb2_ref_lock);
579 
580 	/* take care of closing the device here, if any */
581 	usb2_fifo_close(f, 0);
582 
583 	usb2_cv_destroy(&f->cv_io);
584 	usb2_cv_destroy(&f->cv_drain);
585 
586 	free(f, M_USBDEV);
587 }
588 
589 static struct usb_pipe *
590 usb2_dev_get_pipe(struct usb_device *udev, uint8_t ep_index, uint8_t dir)
591 {
592 	struct usb_pipe *pipe;
593 	uint8_t ep_dir;
594 
595 	if (ep_index == 0) {
596 		pipe = &udev->default_pipe;
597 	} else {
598 		if (dir == USB_FIFO_RX) {
599 			if (udev->flags.usb_mode == USB_MODE_HOST) {
600 				ep_dir = UE_DIR_IN;
601 			} else {
602 				ep_dir = UE_DIR_OUT;
603 			}
604 		} else {
605 			if (udev->flags.usb_mode == USB_MODE_HOST) {
606 				ep_dir = UE_DIR_OUT;
607 			} else {
608 				ep_dir = UE_DIR_IN;
609 			}
610 		}
611 		pipe = usb2_get_pipe_by_addr(udev, ep_index | ep_dir);
612 	}
613 
614 	if (pipe == NULL) {
615 		/* if the pipe does not exist then return */
616 		return (NULL);
617 	}
618 	if (pipe->edesc == NULL) {
619 		/* invalid pipe */
620 		return (NULL);
621 	}
622 	return (pipe);			/* success */
623 }
624 
625 /*------------------------------------------------------------------------*
626  *	usb2_fifo_open
627  *
628  * Returns:
629  * 0: Success
630  * Else: Failure
631  *------------------------------------------------------------------------*/
632 static int
633 usb2_fifo_open(struct usb_cdev_privdata *cpd,
634     struct usb_fifo *f, int fflags)
635 {
636 	int err;
637 
638 	if (f == NULL) {
639 		/* no FIFO there */
640 		DPRINTFN(2, "no FIFO\n");
641 		return (ENXIO);
642 	}
643 	/* remove FWRITE and FREAD flags */
644 	fflags &= ~(FWRITE | FREAD);
645 
646 	/* set correct file flags */
647 	if ((f->fifo_index & 1) == USB_FIFO_TX) {
648 		fflags |= FWRITE;
649 	} else {
650 		fflags |= FREAD;
651 	}
652 
653 	/* check if we are already opened */
654 	/* we don't need any locks when checking this variable */
655 	if (f->curr_cpd != NULL) {
656 		err = EBUSY;
657 		goto done;
658 	}
659 
660 	/* reset short flag before open */
661 	f->flag_short = 0;
662 
663 	/* call open method */
664 	err = (f->methods->f_open) (f, fflags);
665 	if (err) {
666 		goto done;
667 	}
668 	mtx_lock(f->priv_mtx);
669 
670 	/* reset sleep flag */
671 	f->flag_sleeping = 0;
672 
673 	/* reset error flag */
674 	f->flag_iserror = 0;
675 
676 	/* reset complete flag */
677 	f->flag_iscomplete = 0;
678 
679 	/* reset select flag */
680 	f->flag_isselect = 0;
681 
682 	/* reset flushing flag */
683 	f->flag_flushing = 0;
684 
685 	/* reset ASYNC proc flag */
686 	f->async_p = NULL;
687 
688 	mtx_lock(&usb2_ref_lock);
689 	/* flag the fifo as opened to prevent others */
690 	f->curr_cpd = cpd;
691 	mtx_unlock(&usb2_ref_lock);
692 
693 	/* reset queue */
694 	usb2_fifo_reset(f);
695 
696 	mtx_unlock(f->priv_mtx);
697 done:
698 	return (err);
699 }
700 
701 /*------------------------------------------------------------------------*
702  *	usb2_fifo_reset
703  *------------------------------------------------------------------------*/
704 void
705 usb2_fifo_reset(struct usb_fifo *f)
706 {
707 	struct usb_mbuf *m;
708 
709 	if (f == NULL) {
710 		return;
711 	}
712 	while (1) {
713 		USB_IF_DEQUEUE(&f->used_q, m);
714 		if (m) {
715 			USB_IF_ENQUEUE(&f->free_q, m);
716 		} else {
717 			break;
718 		}
719 	}
720 }
721 
722 /*------------------------------------------------------------------------*
723  *	usb2_fifo_close
724  *------------------------------------------------------------------------*/
725 static void
726 usb2_fifo_close(struct usb_fifo *f, int fflags)
727 {
728 	int err;
729 
730 	/* check if we are not opened */
731 	if (f->curr_cpd == NULL) {
732 		/* nothing to do - already closed */
733 		return;
734 	}
735 	mtx_lock(f->priv_mtx);
736 
737 	/* clear current cdev private data pointer */
738 	f->curr_cpd = NULL;
739 
740 	/* check if we are selected */
741 	if (f->flag_isselect) {
742 		selwakeup(&f->selinfo);
743 		f->flag_isselect = 0;
744 	}
745 	/* check if a thread wants SIGIO */
746 	if (f->async_p != NULL) {
747 		PROC_LOCK(f->async_p);
748 		psignal(f->async_p, SIGIO);
749 		PROC_UNLOCK(f->async_p);
750 		f->async_p = NULL;
751 	}
752 	/* remove FWRITE and FREAD flags */
753 	fflags &= ~(FWRITE | FREAD);
754 
755 	/* flush written data, if any */
756 	if ((f->fifo_index & 1) == USB_FIFO_TX) {
757 
758 		if (!f->flag_iserror) {
759 
760 			/* set flushing flag */
761 			f->flag_flushing = 1;
762 
763 			/* start write transfer, if not already started */
764 			(f->methods->f_start_write) (f);
765 
766 			/* check if flushed already */
767 			while (f->flag_flushing &&
768 			    (!f->flag_iserror)) {
769 				/* wait until all data has been written */
770 				f->flag_sleeping = 1;
771 				err = usb2_cv_wait_sig(&f->cv_io, f->priv_mtx);
772 				if (err) {
773 					DPRINTF("signal received\n");
774 					break;
775 				}
776 			}
777 		}
778 		fflags |= FWRITE;
779 
780 		/* stop write transfer, if not already stopped */
781 		(f->methods->f_stop_write) (f);
782 	} else {
783 		fflags |= FREAD;
784 
785 		/* stop write transfer, if not already stopped */
786 		(f->methods->f_stop_read) (f);
787 	}
788 
789 	/* check if we are sleeping */
790 	if (f->flag_sleeping) {
791 		DPRINTFN(2, "Sleeping at close!\n");
792 	}
793 	mtx_unlock(f->priv_mtx);
794 
795 	/* call close method */
796 	(f->methods->f_close) (f, fflags);
797 
798 	DPRINTF("closed\n");
799 }
800 
801 /*------------------------------------------------------------------------*
802  *	usb2_open - cdev callback
803  *------------------------------------------------------------------------*/
804 static int
805 usb2_open(struct cdev *dev, int fflags, int devtype, struct thread *td)
806 {
807 	struct usb_fs_privdata* pd = (struct usb_fs_privdata*)dev->si_drv1;
808 	struct usb_cdev_refdata refs;
809 	struct usb_cdev_privdata *cpd;
810 	int err, ep;
811 
812 	DPRINTFN(2, "%s fflags=0x%08x\n", dev->si_name, fflags);
813 
814 	KASSERT(fflags & (FREAD|FWRITE), ("invalid open flags"));
815 	if (((fflags & FREAD) && !(pd->mode & FREAD)) ||
816 	    ((fflags & FWRITE) && !(pd->mode & FWRITE))) {
817 		DPRINTFN(2, "access mode not supported\n");
818 		return (EPERM);
819 	}
820 
821 	cpd = malloc(sizeof(*cpd), M_USBDEV, M_WAITOK | M_ZERO);
822 	ep = cpd->ep_addr = pd->ep_addr;
823 
824 	usb2_loc_fill(pd, cpd);
825 	err = usb2_ref_device(cpd, &refs, 1);
826 	if (err) {
827 		DPRINTFN(2, "cannot ref device\n");
828 		free(cpd, M_USBDEV);
829 		return (ENXIO);
830 	}
831 	cpd->fflags = fflags;	/* access mode for open lifetime */
832 
833 	/* create FIFOs, if any */
834 	err = usb2_fifo_create(cpd, &refs);
835 	/* check for error */
836 	if (err) {
837 		DPRINTFN(2, "cannot create fifo\n");
838 		usb2_unref_device(cpd, &refs);
839 		free(cpd, M_USBDEV);
840 		return (err);
841 	}
842 	if (fflags & FREAD) {
843 		err = usb2_fifo_open(cpd, refs.rxfifo, fflags);
844 		if (err) {
845 			DPRINTFN(2, "read open failed\n");
846 			usb2_unref_device(cpd, &refs);
847 			free(cpd, M_USBDEV);
848 			return (err);
849 		}
850 	}
851 	if (fflags & FWRITE) {
852 		err = usb2_fifo_open(cpd, refs.txfifo, fflags);
853 		if (err) {
854 			DPRINTFN(2, "write open failed\n");
855 			if (fflags & FREAD) {
856 				usb2_fifo_close(refs.rxfifo, fflags);
857 			}
858 			usb2_unref_device(cpd, &refs);
859 			free(cpd, M_USBDEV);
860 			return (err);
861 		}
862 	}
863 	usb2_unref_device(cpd, &refs);
864 	devfs_set_cdevpriv(cpd, usb2_close);
865 
866 	return (0);
867 }
868 
869 /*------------------------------------------------------------------------*
870  *	usb2_close - cdev callback
871  *------------------------------------------------------------------------*/
872 static void
873 usb2_close(void *arg)
874 {
875 	struct usb_cdev_refdata refs;
876 	struct usb_cdev_privdata *cpd = arg;
877 	int err;
878 
879 	DPRINTFN(2, "cpd=%p\n", cpd);
880 
881 	err = usb2_ref_device(cpd, &refs, 1);
882 	if (err) {
883 		free(cpd, M_USBDEV);
884 		return;
885 	}
886 	if (cpd->fflags & FREAD) {
887 		usb2_fifo_close(refs.rxfifo, cpd->fflags);
888 	}
889 	if (cpd->fflags & FWRITE) {
890 		usb2_fifo_close(refs.txfifo, cpd->fflags);
891 	}
892 
893 	usb2_unref_device(cpd, &refs);
894 	free(cpd, M_USBDEV);
895 	return;
896 }
897 
898 static void
899 usb2_dev_init(void *arg)
900 {
901 	mtx_init(&usb2_ref_lock, "USB ref mutex", NULL, MTX_DEF);
902 	sx_init(&usb2_sym_lock, "USB sym mutex");
903 	TAILQ_INIT(&usb2_sym_head);
904 
905 	/* check the UGEN methods */
906 	usb2_fifo_check_methods(&usb2_ugen_methods);
907 }
908 
909 SYSINIT(usb2_dev_init, SI_SUB_KLD, SI_ORDER_FIRST, usb2_dev_init, NULL);
910 
911 static void
912 usb2_dev_init_post(void *arg)
913 {
914 	/*
915 	 * Create /dev/usb - this is needed for usbconfig(8), which
916 	 * needs a well-known device name to access.
917 	 */
918 	usb2_dev = make_dev(&usb2_static_devsw, 0, UID_ROOT, GID_OPERATOR,
919 	    0644, USB_DEVICE_NAME);
920 	if (usb2_dev == NULL) {
921 		DPRINTFN(0, "Could not create usb bus device!\n");
922 	}
923 }
924 
925 SYSINIT(usb2_dev_init_post, SI_SUB_KICK_SCHEDULER, SI_ORDER_FIRST, usb2_dev_init_post, NULL);
926 
927 static void
928 usb2_dev_uninit(void *arg)
929 {
930 	if (usb2_dev != NULL) {
931 		destroy_dev(usb2_dev);
932 		usb2_dev = NULL;
933 
934 	}
935 	mtx_destroy(&usb2_ref_lock);
936 	sx_destroy(&usb2_sym_lock);
937 }
938 
939 SYSUNINIT(usb2_dev_uninit, SI_SUB_KICK_SCHEDULER, SI_ORDER_ANY, usb2_dev_uninit, NULL);
940 
941 static int
942 usb2_ioctl_f_sub(struct usb_fifo *f, u_long cmd, void *addr,
943     struct thread *td)
944 {
945 	int error = 0;
946 
947 	switch (cmd) {
948 	case FIODTYPE:
949 		*(int *)addr = 0;	/* character device */
950 		break;
951 
952 	case FIONBIO:
953 		/* handled by upper FS layer */
954 		break;
955 
956 	case FIOASYNC:
957 		if (*(int *)addr) {
958 			if (f->async_p != NULL) {
959 				error = EBUSY;
960 				break;
961 			}
962 			f->async_p = USB_TD_GET_PROC(td);
963 		} else {
964 			f->async_p = NULL;
965 		}
966 		break;
967 
968 		/* XXX this is not the most general solution */
969 	case TIOCSPGRP:
970 		if (f->async_p == NULL) {
971 			error = EINVAL;
972 			break;
973 		}
974 		if (*(int *)addr != USB_PROC_GET_GID(f->async_p)) {
975 			error = EPERM;
976 			break;
977 		}
978 		break;
979 	default:
980 		return (ENOIOCTL);
981 	}
982 	DPRINTFN(3, "cmd 0x%lx = %d\n", cmd, error);
983 	return (error);
984 }
985 
986 /*------------------------------------------------------------------------*
987  *	usb2_ioctl - cdev callback
988  *------------------------------------------------------------------------*/
989 static int
990 usb2_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int fflag, struct thread* td)
991 {
992 	struct usb_cdev_refdata refs;
993 	struct usb_cdev_privdata* cpd;
994 	struct usb_fifo *f;
995 	int fflags;
996 	int err;
997 
998 	DPRINTFN(2, "cmd=0x%lx\n", cmd);
999 
1000 	err = devfs_get_cdevpriv((void **)&cpd);
1001 	if (err != 0)
1002 		return (err);
1003 
1004 	/*
1005 	 * Performance optimisation: We try to check for IOCTL's that
1006 	 * don't need the USB reference first. Then we grab the USB
1007 	 * reference if we need it!
1008 	 */
1009 	err = usb2_ref_device(cpd, &refs, 0 /* no uref */ );
1010 	if (err) {
1011 		return (ENXIO);
1012 	}
1013 	fflags = cpd->fflags;
1014 
1015 	f = NULL;			/* set default value */
1016 	err = ENOIOCTL;			/* set default value */
1017 
1018 	if (fflags & FWRITE) {
1019 		f = refs.txfifo;
1020 		err = usb2_ioctl_f_sub(f, cmd, addr, td);
1021 	}
1022 	if (fflags & FREAD) {
1023 		f = refs.rxfifo;
1024 		err = usb2_ioctl_f_sub(f, cmd, addr, td);
1025 	}
1026 	KASSERT(f != NULL, ("fifo not found"));
1027 	if (err == ENOIOCTL) {
1028 		err = (f->methods->f_ioctl) (f, cmd, addr, fflags);
1029 		DPRINTFN(2, "f_ioctl cmd 0x%lx = %d\n", cmd, err);
1030 		if (err == ENOIOCTL) {
1031 			if (usb2_usb_ref_device(cpd, &refs)) {
1032 				err = ENXIO;
1033 				goto done;
1034 			}
1035 			err = (f->methods->f_ioctl_post) (f, cmd, addr, fflags);
1036 			DPRINTFN(2, "f_ioctl_post cmd 0x%lx = %d\n", cmd, err);
1037 		}
1038 	}
1039 	if (err == ENOIOCTL) {
1040 		err = ENOTTY;
1041 	}
1042 done:
1043 	usb2_unref_device(cpd, &refs);
1044 	return (err);
1045 }
1046 
1047 /* ARGSUSED */
1048 static int
1049 usb2_poll(struct cdev* dev, int events, struct thread* td)
1050 {
1051 	struct usb_cdev_refdata refs;
1052 	struct usb_cdev_privdata* cpd;
1053 	struct usb_fifo *f;
1054 	struct usb_mbuf *m;
1055 	int fflags, revents;
1056 
1057 	if (devfs_get_cdevpriv((void **)&cpd) != 0 ||
1058 	    usb2_ref_device(cpd, &refs, 0) != 0)
1059 		return (events &
1060 		    (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM));
1061 
1062 	fflags = cpd->fflags;
1063 
1064 	/* Figure out who needs service */
1065 	revents = 0;
1066 	if ((events & (POLLOUT | POLLWRNORM)) &&
1067 	    (fflags & FWRITE)) {
1068 
1069 		f = refs.txfifo;
1070 
1071 		mtx_lock(f->priv_mtx);
1072 
1073 		if (!refs.is_usbfs) {
1074 			if (f->flag_iserror) {
1075 				/* we got an error */
1076 				m = (void *)1;
1077 			} else {
1078 				if (f->queue_data == NULL) {
1079 					/*
1080 					 * start write transfer, if not
1081 					 * already started
1082 					 */
1083 					(f->methods->f_start_write) (f);
1084 				}
1085 				/* check if any packets are available */
1086 				USB_IF_POLL(&f->free_q, m);
1087 			}
1088 		} else {
1089 			if (f->flag_iscomplete) {
1090 				m = (void *)1;
1091 			} else {
1092 				m = NULL;
1093 			}
1094 		}
1095 
1096 		if (m) {
1097 			revents |= events & (POLLOUT | POLLWRNORM);
1098 		} else {
1099 			f->flag_isselect = 1;
1100 			selrecord(td, &f->selinfo);
1101 		}
1102 
1103 		mtx_unlock(f->priv_mtx);
1104 	}
1105 	if ((events & (POLLIN | POLLRDNORM)) &&
1106 	    (fflags & FREAD)) {
1107 
1108 		f = refs.rxfifo;
1109 
1110 		mtx_lock(f->priv_mtx);
1111 
1112 		if (!refs.is_usbfs) {
1113 			if (f->flag_iserror) {
1114 				/* we have and error */
1115 				m = (void *)1;
1116 			} else {
1117 				if (f->queue_data == NULL) {
1118 					/*
1119 					 * start read transfer, if not
1120 					 * already started
1121 					 */
1122 					(f->methods->f_start_read) (f);
1123 				}
1124 				/* check if any packets are available */
1125 				USB_IF_POLL(&f->used_q, m);
1126 			}
1127 		} else {
1128 			if (f->flag_iscomplete) {
1129 				m = (void *)1;
1130 			} else {
1131 				m = NULL;
1132 			}
1133 		}
1134 
1135 		if (m) {
1136 			revents |= events & (POLLIN | POLLRDNORM);
1137 		} else {
1138 			f->flag_isselect = 1;
1139 			selrecord(td, &f->selinfo);
1140 
1141 			if (!refs.is_usbfs) {
1142 				/* start reading data */
1143 				(f->methods->f_start_read) (f);
1144 			}
1145 		}
1146 
1147 		mtx_unlock(f->priv_mtx);
1148 	}
1149 	usb2_unref_device(cpd, &refs);
1150 	return (revents);
1151 }
1152 
1153 static int
1154 usb2_read(struct cdev *dev, struct uio *uio, int ioflag)
1155 {
1156 	struct usb_cdev_refdata refs;
1157 	struct usb_cdev_privdata* cpd;
1158 	struct usb_fifo *f;
1159 	struct usb_mbuf *m;
1160 	int fflags;
1161 	int resid;
1162 	int io_len;
1163 	int err;
1164 	uint8_t tr_data = 0;
1165 
1166 	err = devfs_get_cdevpriv((void **)&cpd);
1167 	if (err != 0)
1168 		return (err);
1169 
1170 	err = usb2_ref_device(cpd, &refs, 0 /* no uref */ );
1171 	if (err) {
1172 		return (ENXIO);
1173 	}
1174 	fflags = cpd->fflags;
1175 
1176 	f = refs.rxfifo;
1177 	if (f == NULL) {
1178 		/* should not happen */
1179 		usb2_unref_device(cpd, &refs);
1180 		return (EPERM);
1181 	}
1182 
1183 	resid = uio->uio_resid;
1184 
1185 	mtx_lock(f->priv_mtx);
1186 
1187 	/* check for permanent read error */
1188 	if (f->flag_iserror) {
1189 		err = EIO;
1190 		goto done;
1191 	}
1192 	/* check if USB-FS interface is active */
1193 	if (refs.is_usbfs) {
1194 		/*
1195 		 * The queue is used for events that should be
1196 		 * retrieved using the "USB_FS_COMPLETE" ioctl.
1197 		 */
1198 		err = EINVAL;
1199 		goto done;
1200 	}
1201 	while (uio->uio_resid > 0) {
1202 
1203 		USB_IF_DEQUEUE(&f->used_q, m);
1204 
1205 		if (m == NULL) {
1206 
1207 			/* start read transfer, if not already started */
1208 
1209 			(f->methods->f_start_read) (f);
1210 
1211 			if (ioflag & IO_NDELAY) {
1212 				if (tr_data) {
1213 					/* return length before error */
1214 					break;
1215 				}
1216 				err = EWOULDBLOCK;
1217 				break;
1218 			}
1219 			DPRINTF("sleeping\n");
1220 
1221 			err = usb2_fifo_wait(f);
1222 			if (err) {
1223 				break;
1224 			}
1225 			continue;
1226 		}
1227 		if (f->methods->f_filter_read) {
1228 			/*
1229 			 * Sometimes it is convenient to process data at the
1230 			 * expense of a userland process instead of a kernel
1231 			 * process.
1232 			 */
1233 			(f->methods->f_filter_read) (f, m);
1234 		}
1235 		tr_data = 1;
1236 
1237 		io_len = MIN(m->cur_data_len, uio->uio_resid);
1238 
1239 		DPRINTFN(2, "transfer %d bytes from %p\n",
1240 		    io_len, m->cur_data_ptr);
1241 
1242 		err = usb2_fifo_uiomove(f,
1243 		    m->cur_data_ptr, io_len, uio);
1244 
1245 		m->cur_data_len -= io_len;
1246 		m->cur_data_ptr += io_len;
1247 
1248 		if (m->cur_data_len == 0) {
1249 
1250 			uint8_t last_packet;
1251 
1252 			last_packet = m->last_packet;
1253 
1254 			USB_IF_ENQUEUE(&f->free_q, m);
1255 
1256 			if (last_packet) {
1257 				/* keep framing */
1258 				break;
1259 			}
1260 		} else {
1261 			USB_IF_PREPEND(&f->used_q, m);
1262 		}
1263 
1264 		if (err) {
1265 			break;
1266 		}
1267 	}
1268 done:
1269 	mtx_unlock(f->priv_mtx);
1270 
1271 	usb2_unref_device(cpd, &refs);
1272 
1273 	return (err);
1274 }
1275 
1276 static int
1277 usb2_write(struct cdev *dev, struct uio *uio, int ioflag)
1278 {
1279 	struct usb_cdev_refdata refs;
1280 	struct usb_cdev_privdata* cpd;
1281 	struct usb_fifo *f;
1282 	struct usb_mbuf *m;
1283 	int fflags;
1284 	int resid;
1285 	int io_len;
1286 	int err;
1287 	uint8_t tr_data = 0;
1288 
1289 	DPRINTFN(2, "\n");
1290 
1291 	err = devfs_get_cdevpriv((void **)&cpd);
1292 	if (err != 0)
1293 		return (err);
1294 
1295 	err = usb2_ref_device(cpd, &refs, 0 /* no uref */ );
1296 	if (err) {
1297 		return (ENXIO);
1298 	}
1299 	fflags = cpd->fflags;
1300 
1301 	f = refs.txfifo;
1302 	if (f == NULL) {
1303 		/* should not happen */
1304 		usb2_unref_device(cpd, &refs);
1305 		return (EPERM);
1306 	}
1307 	resid = uio->uio_resid;
1308 
1309 	mtx_lock(f->priv_mtx);
1310 
1311 	/* check for permanent write error */
1312 	if (f->flag_iserror) {
1313 		err = EIO;
1314 		goto done;
1315 	}
1316 	/* check if USB-FS interface is active */
1317 	if (refs.is_usbfs) {
1318 		/*
1319 		 * The queue is used for events that should be
1320 		 * retrieved using the "USB_FS_COMPLETE" ioctl.
1321 		 */
1322 		err = EINVAL;
1323 		goto done;
1324 	}
1325 	if (f->queue_data == NULL) {
1326 		/* start write transfer, if not already started */
1327 		(f->methods->f_start_write) (f);
1328 	}
1329 	/* we allow writing zero length data */
1330 	do {
1331 		USB_IF_DEQUEUE(&f->free_q, m);
1332 
1333 		if (m == NULL) {
1334 
1335 			if (ioflag & IO_NDELAY) {
1336 				if (tr_data) {
1337 					/* return length before error */
1338 					break;
1339 				}
1340 				err = EWOULDBLOCK;
1341 				break;
1342 			}
1343 			DPRINTF("sleeping\n");
1344 
1345 			err = usb2_fifo_wait(f);
1346 			if (err) {
1347 				break;
1348 			}
1349 			continue;
1350 		}
1351 		tr_data = 1;
1352 
1353 		USB_MBUF_RESET(m);
1354 
1355 		io_len = MIN(m->cur_data_len, uio->uio_resid);
1356 
1357 		m->cur_data_len = io_len;
1358 
1359 		DPRINTFN(2, "transfer %d bytes to %p\n",
1360 		    io_len, m->cur_data_ptr);
1361 
1362 		err = usb2_fifo_uiomove(f,
1363 		    m->cur_data_ptr, io_len, uio);
1364 
1365 		if (err) {
1366 			USB_IF_ENQUEUE(&f->free_q, m);
1367 			break;
1368 		}
1369 		if (f->methods->f_filter_write) {
1370 			/*
1371 			 * Sometimes it is convenient to process data at the
1372 			 * expense of a userland process instead of a kernel
1373 			 * process.
1374 			 */
1375 			(f->methods->f_filter_write) (f, m);
1376 		}
1377 		USB_IF_ENQUEUE(&f->used_q, m);
1378 
1379 		(f->methods->f_start_write) (f);
1380 
1381 	} while (uio->uio_resid > 0);
1382 done:
1383 	mtx_unlock(f->priv_mtx);
1384 
1385 	usb2_unref_device(cpd, &refs);
1386 
1387 	return (err);
1388 }
1389 
1390 int
1391 usb2_static_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
1392     struct thread *td)
1393 {
1394 	union {
1395 		struct usb_read_dir *urd;
1396 		void* data;
1397 	} u;
1398 	int err = ENOTTY;
1399 
1400 	u.data = data;
1401 	switch (cmd) {
1402 		case USB_READ_DIR:
1403 			err = usb2_read_symlink(u.urd->urd_data,
1404 			    u.urd->urd_startentry, u.urd->urd_maxlen);
1405 			break;
1406 		case USB_DEV_QUIRK_GET:
1407 		case USB_QUIRK_NAME_GET:
1408 		case USB_DEV_QUIRK_ADD:
1409 		case USB_DEV_QUIRK_REMOVE:
1410 			err = usb2_quirk_ioctl_p(cmd, data, fflag, td);
1411 			break;
1412 		case USB_GET_TEMPLATE:
1413 			*(int *)data = usb2_template;
1414 			break;
1415 		case USB_SET_TEMPLATE:
1416 			err = priv_check(curthread, PRIV_DRIVER);
1417 			if (err)
1418 				break;
1419 			usb2_template = *(int *)data;
1420 			break;
1421 	}
1422 	return (err);
1423 }
1424 
1425 static int
1426 usb2_fifo_uiomove(struct usb_fifo *f, void *cp,
1427     int n, struct uio *uio)
1428 {
1429 	int error;
1430 
1431 	mtx_unlock(f->priv_mtx);
1432 
1433 	/*
1434 	 * "uiomove()" can sleep so one needs to make a wrapper,
1435 	 * exiting the mutex and checking things:
1436 	 */
1437 	error = uiomove(cp, n, uio);
1438 
1439 	mtx_lock(f->priv_mtx);
1440 
1441 	return (error);
1442 }
1443 
1444 int
1445 usb2_fifo_wait(struct usb_fifo *f)
1446 {
1447 	int err;
1448 
1449 	mtx_assert(f->priv_mtx, MA_OWNED);
1450 
1451 	if (f->flag_iserror) {
1452 		/* we are gone */
1453 		return (EIO);
1454 	}
1455 	f->flag_sleeping = 1;
1456 
1457 	err = usb2_cv_wait_sig(&f->cv_io, f->priv_mtx);
1458 
1459 	if (f->flag_iserror) {
1460 		/* we are gone */
1461 		err = EIO;
1462 	}
1463 	return (err);
1464 }
1465 
1466 void
1467 usb2_fifo_signal(struct usb_fifo *f)
1468 {
1469 	if (f->flag_sleeping) {
1470 		f->flag_sleeping = 0;
1471 		usb2_cv_broadcast(&f->cv_io);
1472 	}
1473 }
1474 
1475 void
1476 usb2_fifo_wakeup(struct usb_fifo *f)
1477 {
1478 	usb2_fifo_signal(f);
1479 
1480 	if (f->flag_isselect) {
1481 		selwakeup(&f->selinfo);
1482 		f->flag_isselect = 0;
1483 	}
1484 	if (f->async_p != NULL) {
1485 		PROC_LOCK(f->async_p);
1486 		psignal(f->async_p, SIGIO);
1487 		PROC_UNLOCK(f->async_p);
1488 	}
1489 }
1490 
1491 static int
1492 usb2_fifo_dummy_open(struct usb_fifo *fifo, int fflags)
1493 {
1494 	return (0);
1495 }
1496 
1497 static void
1498 usb2_fifo_dummy_close(struct usb_fifo *fifo, int fflags)
1499 {
1500 	return;
1501 }
1502 
1503 static int
1504 usb2_fifo_dummy_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
1505 {
1506 	return (ENOIOCTL);
1507 }
1508 
1509 static void
1510 usb2_fifo_dummy_cmd(struct usb_fifo *fifo)
1511 {
1512 	fifo->flag_flushing = 0;	/* not flushing */
1513 }
1514 
1515 static void
1516 usb2_fifo_check_methods(struct usb_fifo_methods *pm)
1517 {
1518 	/* check that all callback functions are OK */
1519 
1520 	if (pm->f_open == NULL)
1521 		pm->f_open = &usb2_fifo_dummy_open;
1522 
1523 	if (pm->f_close == NULL)
1524 		pm->f_close = &usb2_fifo_dummy_close;
1525 
1526 	if (pm->f_ioctl == NULL)
1527 		pm->f_ioctl = &usb2_fifo_dummy_ioctl;
1528 
1529 	if (pm->f_ioctl_post == NULL)
1530 		pm->f_ioctl_post = &usb2_fifo_dummy_ioctl;
1531 
1532 	if (pm->f_start_read == NULL)
1533 		pm->f_start_read = &usb2_fifo_dummy_cmd;
1534 
1535 	if (pm->f_stop_read == NULL)
1536 		pm->f_stop_read = &usb2_fifo_dummy_cmd;
1537 
1538 	if (pm->f_start_write == NULL)
1539 		pm->f_start_write = &usb2_fifo_dummy_cmd;
1540 
1541 	if (pm->f_stop_write == NULL)
1542 		pm->f_stop_write = &usb2_fifo_dummy_cmd;
1543 }
1544 
1545 /*------------------------------------------------------------------------*
1546  *	usb2_fifo_attach
1547  *
1548  * The following function will create a duplex FIFO.
1549  *
1550  * Return values:
1551  * 0: Success.
1552  * Else: Failure.
1553  *------------------------------------------------------------------------*/
1554 int
1555 usb2_fifo_attach(struct usb_device *udev, void *priv_sc,
1556     struct mtx *priv_mtx, struct usb_fifo_methods *pm,
1557     struct usb_fifo_sc *f_sc, uint16_t unit, uint16_t subunit,
1558     uint8_t iface_index, uid_t uid, gid_t gid, int mode)
1559 {
1560 	struct usb_fifo *f_tx;
1561 	struct usb_fifo *f_rx;
1562 	char devname[32];
1563 	uint8_t n;
1564 	struct usb_fs_privdata* pd;
1565 
1566 	f_sc->fp[USB_FIFO_TX] = NULL;
1567 	f_sc->fp[USB_FIFO_RX] = NULL;
1568 
1569 	if (pm == NULL)
1570 		return (EINVAL);
1571 
1572 	/* check the methods */
1573 	usb2_fifo_check_methods(pm);
1574 
1575 	if (priv_mtx == NULL)
1576 		priv_mtx = &Giant;
1577 
1578 	/* search for a free FIFO slot */
1579 	for (n = 0;; n += 2) {
1580 
1581 		if (n == USB_FIFO_MAX) {
1582 			/* end of FIFOs reached */
1583 			return (ENOMEM);
1584 		}
1585 		/* Check for TX FIFO */
1586 		if (udev->fifo[n + USB_FIFO_TX] != NULL) {
1587 			continue;
1588 		}
1589 		/* Check for RX FIFO */
1590 		if (udev->fifo[n + USB_FIFO_RX] != NULL) {
1591 			continue;
1592 		}
1593 		break;
1594 	}
1595 
1596 	f_tx = usb2_fifo_alloc();
1597 	f_rx = usb2_fifo_alloc();
1598 
1599 	if ((f_tx == NULL) || (f_rx == NULL)) {
1600 		usb2_fifo_free(f_tx);
1601 		usb2_fifo_free(f_rx);
1602 		return (ENOMEM);
1603 	}
1604 	/* initialise FIFO structures */
1605 
1606 	f_tx->fifo_index = n + USB_FIFO_TX;
1607 	f_tx->dev_ep_index = -1;
1608 	f_tx->priv_mtx = priv_mtx;
1609 	f_tx->priv_sc0 = priv_sc;
1610 	f_tx->methods = pm;
1611 	f_tx->iface_index = iface_index;
1612 	f_tx->udev = udev;
1613 
1614 	f_rx->fifo_index = n + USB_FIFO_RX;
1615 	f_rx->dev_ep_index = -1;
1616 	f_rx->priv_mtx = priv_mtx;
1617 	f_rx->priv_sc0 = priv_sc;
1618 	f_rx->methods = pm;
1619 	f_rx->iface_index = iface_index;
1620 	f_rx->udev = udev;
1621 
1622 	f_sc->fp[USB_FIFO_TX] = f_tx;
1623 	f_sc->fp[USB_FIFO_RX] = f_rx;
1624 
1625 	mtx_lock(&usb2_ref_lock);
1626 	udev->fifo[f_tx->fifo_index] = f_tx;
1627 	udev->fifo[f_rx->fifo_index] = f_rx;
1628 	mtx_unlock(&usb2_ref_lock);
1629 
1630 	for (n = 0; n != 4; n++) {
1631 
1632 		if (pm->basename[n] == NULL) {
1633 			continue;
1634 		}
1635 		if (subunit == 0xFFFF) {
1636 			if (snprintf(devname, sizeof(devname),
1637 			    "%s%u%s", pm->basename[n],
1638 			    unit, pm->postfix[n] ?
1639 			    pm->postfix[n] : "")) {
1640 				/* ignore */
1641 			}
1642 		} else {
1643 			if (snprintf(devname, sizeof(devname),
1644 			    "%s%u.%u%s", pm->basename[n],
1645 			    unit, subunit, pm->postfix[n] ?
1646 			    pm->postfix[n] : "")) {
1647 				/* ignore */
1648 			}
1649 		}
1650 
1651 		/*
1652 		 * Distribute the symbolic links into two FIFO structures:
1653 		 */
1654 		if (n & 1) {
1655 			f_rx->symlink[n / 2] =
1656 			    usb2_alloc_symlink(devname);
1657 		} else {
1658 			f_tx->symlink[n / 2] =
1659 			    usb2_alloc_symlink(devname);
1660 		}
1661 
1662 		/*
1663 		 * Initialize device private data - this is used to find the
1664 		 * actual USB device itself.
1665 		 */
1666 		pd = malloc(sizeof(struct usb_fs_privdata), M_USBDEV, M_WAITOK | M_ZERO);
1667 		pd->bus_index = device_get_unit(udev->bus->bdev);
1668 		pd->dev_index = udev->device_index;
1669 		pd->ep_addr = -1;	/* not an endpoint */
1670 		pd->fifo_index = f_tx->fifo_index & f_rx->fifo_index;
1671 		pd->mode = FREAD|FWRITE;
1672 
1673 		/* Now, create the device itself */
1674 		f_sc->dev = make_dev(&usb2_devsw, 0, uid, gid, mode,
1675 		    devname);
1676 		/* XXX setting si_drv1 and creating the device is not atomic! */
1677 		f_sc->dev->si_drv1 = pd;
1678 	}
1679 
1680 	DPRINTFN(2, "attached %p/%p\n", f_tx, f_rx);
1681 	return (0);
1682 }
1683 
1684 /*------------------------------------------------------------------------*
1685  *	usb2_fifo_alloc_buffer
1686  *
1687  * Return values:
1688  * 0: Success
1689  * Else failure
1690  *------------------------------------------------------------------------*/
1691 int
1692 usb2_fifo_alloc_buffer(struct usb_fifo *f, usb_size_t bufsize,
1693     uint16_t nbuf)
1694 {
1695 	usb2_fifo_free_buffer(f);
1696 
1697 	/* allocate an endpoint */
1698 	f->free_q.ifq_maxlen = nbuf;
1699 	f->used_q.ifq_maxlen = nbuf;
1700 
1701 	f->queue_data = usb2_alloc_mbufs(
1702 	    M_USBDEV, &f->free_q, bufsize, nbuf);
1703 
1704 	if ((f->queue_data == NULL) && bufsize && nbuf) {
1705 		return (ENOMEM);
1706 	}
1707 	return (0);			/* success */
1708 }
1709 
1710 /*------------------------------------------------------------------------*
1711  *	usb2_fifo_free_buffer
1712  *
1713  * This function will free the buffers associated with a FIFO. This
1714  * function can be called multiple times in a row.
1715  *------------------------------------------------------------------------*/
1716 void
1717 usb2_fifo_free_buffer(struct usb_fifo *f)
1718 {
1719 	if (f->queue_data) {
1720 		/* free old buffer */
1721 		free(f->queue_data, M_USBDEV);
1722 		f->queue_data = NULL;
1723 	}
1724 	/* reset queues */
1725 
1726 	bzero(&f->free_q, sizeof(f->free_q));
1727 	bzero(&f->used_q, sizeof(f->used_q));
1728 }
1729 
1730 static void
1731 usb2_fifo_cleanup(void* ptr)
1732 {
1733 	free(ptr, M_USBDEV);
1734 }
1735 
1736 void
1737 usb2_fifo_detach(struct usb_fifo_sc *f_sc)
1738 {
1739 	if (f_sc == NULL) {
1740 		return;
1741 	}
1742 	usb2_fifo_free(f_sc->fp[USB_FIFO_TX]);
1743 	usb2_fifo_free(f_sc->fp[USB_FIFO_RX]);
1744 
1745 	f_sc->fp[USB_FIFO_TX] = NULL;
1746 	f_sc->fp[USB_FIFO_RX] = NULL;
1747 
1748 	if (f_sc->dev != NULL) {
1749 		destroy_dev_sched_cb(f_sc->dev,
1750 		    usb2_fifo_cleanup, f_sc->dev->si_drv1);
1751 		f_sc->dev = NULL;
1752 	}
1753 
1754 	DPRINTFN(2, "detached %p\n", f_sc);
1755 }
1756 
1757 usb_size_t
1758 usb2_fifo_put_bytes_max(struct usb_fifo *f)
1759 {
1760 	struct usb_mbuf *m;
1761 	usb_size_t len;
1762 
1763 	USB_IF_POLL(&f->free_q, m);
1764 
1765 	if (m) {
1766 		len = m->max_data_len;
1767 	} else {
1768 		len = 0;
1769 	}
1770 	return (len);
1771 }
1772 
1773 /*------------------------------------------------------------------------*
1774  *	usb2_fifo_put_data
1775  *
1776  * what:
1777  *  0 - normal operation
1778  *  1 - set last packet flag to enforce framing
1779  *------------------------------------------------------------------------*/
1780 void
1781 usb2_fifo_put_data(struct usb_fifo *f, struct usb_page_cache *pc,
1782     usb_frlength_t offset, usb_frlength_t len, uint8_t what)
1783 {
1784 	struct usb_mbuf *m;
1785 	usb_frlength_t io_len;
1786 
1787 	while (len || (what == 1)) {
1788 
1789 		USB_IF_DEQUEUE(&f->free_q, m);
1790 
1791 		if (m) {
1792 			USB_MBUF_RESET(m);
1793 
1794 			io_len = MIN(len, m->cur_data_len);
1795 
1796 			usb2_copy_out(pc, offset, m->cur_data_ptr, io_len);
1797 
1798 			m->cur_data_len = io_len;
1799 			offset += io_len;
1800 			len -= io_len;
1801 
1802 			if ((len == 0) && (what == 1)) {
1803 				m->last_packet = 1;
1804 			}
1805 			USB_IF_ENQUEUE(&f->used_q, m);
1806 
1807 			usb2_fifo_wakeup(f);
1808 
1809 			if ((len == 0) || (what == 1)) {
1810 				break;
1811 			}
1812 		} else {
1813 			break;
1814 		}
1815 	}
1816 }
1817 
1818 void
1819 usb2_fifo_put_data_linear(struct usb_fifo *f, void *ptr,
1820     usb_size_t len, uint8_t what)
1821 {
1822 	struct usb_mbuf *m;
1823 	usb_size_t io_len;
1824 
1825 	while (len || (what == 1)) {
1826 
1827 		USB_IF_DEQUEUE(&f->free_q, m);
1828 
1829 		if (m) {
1830 			USB_MBUF_RESET(m);
1831 
1832 			io_len = MIN(len, m->cur_data_len);
1833 
1834 			bcopy(ptr, m->cur_data_ptr, io_len);
1835 
1836 			m->cur_data_len = io_len;
1837 			ptr = USB_ADD_BYTES(ptr, io_len);
1838 			len -= io_len;
1839 
1840 			if ((len == 0) && (what == 1)) {
1841 				m->last_packet = 1;
1842 			}
1843 			USB_IF_ENQUEUE(&f->used_q, m);
1844 
1845 			usb2_fifo_wakeup(f);
1846 
1847 			if ((len == 0) || (what == 1)) {
1848 				break;
1849 			}
1850 		} else {
1851 			break;
1852 		}
1853 	}
1854 }
1855 
1856 uint8_t
1857 usb2_fifo_put_data_buffer(struct usb_fifo *f, void *ptr, usb_size_t len)
1858 {
1859 	struct usb_mbuf *m;
1860 
1861 	USB_IF_DEQUEUE(&f->free_q, m);
1862 
1863 	if (m) {
1864 		m->cur_data_len = len;
1865 		m->cur_data_ptr = ptr;
1866 		USB_IF_ENQUEUE(&f->used_q, m);
1867 		usb2_fifo_wakeup(f);
1868 		return (1);
1869 	}
1870 	return (0);
1871 }
1872 
1873 void
1874 usb2_fifo_put_data_error(struct usb_fifo *f)
1875 {
1876 	f->flag_iserror = 1;
1877 	usb2_fifo_wakeup(f);
1878 }
1879 
1880 /*------------------------------------------------------------------------*
1881  *	usb2_fifo_get_data
1882  *
1883  * what:
1884  *  0 - normal operation
1885  *  1 - only get one "usb_mbuf"
1886  *
1887  * returns:
1888  *  0 - no more data
1889  *  1 - data in buffer
1890  *------------------------------------------------------------------------*/
1891 uint8_t
1892 usb2_fifo_get_data(struct usb_fifo *f, struct usb_page_cache *pc,
1893     usb_frlength_t offset, usb_frlength_t len, usb_frlength_t *actlen,
1894     uint8_t what)
1895 {
1896 	struct usb_mbuf *m;
1897 	usb_frlength_t io_len;
1898 	uint8_t tr_data = 0;
1899 
1900 	actlen[0] = 0;
1901 
1902 	while (1) {
1903 
1904 		USB_IF_DEQUEUE(&f->used_q, m);
1905 
1906 		if (m) {
1907 
1908 			tr_data = 1;
1909 
1910 			io_len = MIN(len, m->cur_data_len);
1911 
1912 			usb2_copy_in(pc, offset, m->cur_data_ptr, io_len);
1913 
1914 			len -= io_len;
1915 			offset += io_len;
1916 			actlen[0] += io_len;
1917 			m->cur_data_ptr += io_len;
1918 			m->cur_data_len -= io_len;
1919 
1920 			if ((m->cur_data_len == 0) || (what == 1)) {
1921 				USB_IF_ENQUEUE(&f->free_q, m);
1922 
1923 				usb2_fifo_wakeup(f);
1924 
1925 				if (what == 1) {
1926 					break;
1927 				}
1928 			} else {
1929 				USB_IF_PREPEND(&f->used_q, m);
1930 			}
1931 		} else {
1932 
1933 			if (tr_data) {
1934 				/* wait for data to be written out */
1935 				break;
1936 			}
1937 			if (f->flag_flushing) {
1938 				/* check if we should send a short packet */
1939 				if (f->flag_short != 0) {
1940 					f->flag_short = 0;
1941 					tr_data = 1;
1942 					break;
1943 				}
1944 				/* flushing complete */
1945 				f->flag_flushing = 0;
1946 				usb2_fifo_wakeup(f);
1947 			}
1948 			break;
1949 		}
1950 		if (len == 0) {
1951 			break;
1952 		}
1953 	}
1954 	return (tr_data);
1955 }
1956 
1957 uint8_t
1958 usb2_fifo_get_data_linear(struct usb_fifo *f, void *ptr,
1959     usb_size_t len, usb_size_t *actlen, uint8_t what)
1960 {
1961 	struct usb_mbuf *m;
1962 	usb_size_t io_len;
1963 	uint8_t tr_data = 0;
1964 
1965 	actlen[0] = 0;
1966 
1967 	while (1) {
1968 
1969 		USB_IF_DEQUEUE(&f->used_q, m);
1970 
1971 		if (m) {
1972 
1973 			tr_data = 1;
1974 
1975 			io_len = MIN(len, m->cur_data_len);
1976 
1977 			bcopy(m->cur_data_ptr, ptr, io_len);
1978 
1979 			len -= io_len;
1980 			ptr = USB_ADD_BYTES(ptr, io_len);
1981 			actlen[0] += io_len;
1982 			m->cur_data_ptr += io_len;
1983 			m->cur_data_len -= io_len;
1984 
1985 			if ((m->cur_data_len == 0) || (what == 1)) {
1986 				USB_IF_ENQUEUE(&f->free_q, m);
1987 
1988 				usb2_fifo_wakeup(f);
1989 
1990 				if (what == 1) {
1991 					break;
1992 				}
1993 			} else {
1994 				USB_IF_PREPEND(&f->used_q, m);
1995 			}
1996 		} else {
1997 
1998 			if (tr_data) {
1999 				/* wait for data to be written out */
2000 				break;
2001 			}
2002 			if (f->flag_flushing) {
2003 				/* check if we should send a short packet */
2004 				if (f->flag_short != 0) {
2005 					f->flag_short = 0;
2006 					tr_data = 1;
2007 					break;
2008 				}
2009 				/* flushing complete */
2010 				f->flag_flushing = 0;
2011 				usb2_fifo_wakeup(f);
2012 			}
2013 			break;
2014 		}
2015 		if (len == 0) {
2016 			break;
2017 		}
2018 	}
2019 	return (tr_data);
2020 }
2021 
2022 uint8_t
2023 usb2_fifo_get_data_buffer(struct usb_fifo *f, void **pptr, usb_size_t *plen)
2024 {
2025 	struct usb_mbuf *m;
2026 
2027 	USB_IF_POLL(&f->used_q, m);
2028 
2029 	if (m) {
2030 		*plen = m->cur_data_len;
2031 		*pptr = m->cur_data_ptr;
2032 
2033 		return (1);
2034 	}
2035 	return (0);
2036 }
2037 
2038 void
2039 usb2_fifo_get_data_error(struct usb_fifo *f)
2040 {
2041 	f->flag_iserror = 1;
2042 	usb2_fifo_wakeup(f);
2043 }
2044 
2045 /*------------------------------------------------------------------------*
2046  *	usb2_alloc_symlink
2047  *
2048  * Return values:
2049  * NULL: Failure
2050  * Else: Pointer to symlink entry
2051  *------------------------------------------------------------------------*/
2052 struct usb_symlink *
2053 usb2_alloc_symlink(const char *target)
2054 {
2055 	struct usb_symlink *ps;
2056 
2057 	ps = malloc(sizeof(*ps), M_USBDEV, M_WAITOK);
2058 	if (ps == NULL) {
2059 		return (ps);
2060 	}
2061 	/* XXX no longer needed */
2062 	strlcpy(ps->src_path, target, sizeof(ps->src_path));
2063 	ps->src_len = strlen(ps->src_path);
2064 	strlcpy(ps->dst_path, target, sizeof(ps->dst_path));
2065 	ps->dst_len = strlen(ps->dst_path);
2066 
2067 	sx_xlock(&usb2_sym_lock);
2068 	TAILQ_INSERT_TAIL(&usb2_sym_head, ps, sym_entry);
2069 	sx_unlock(&usb2_sym_lock);
2070 	return (ps);
2071 }
2072 
2073 /*------------------------------------------------------------------------*
2074  *	usb2_free_symlink
2075  *------------------------------------------------------------------------*/
2076 void
2077 usb2_free_symlink(struct usb_symlink *ps)
2078 {
2079 	if (ps == NULL) {
2080 		return;
2081 	}
2082 	sx_xlock(&usb2_sym_lock);
2083 	TAILQ_REMOVE(&usb2_sym_head, ps, sym_entry);
2084 	sx_unlock(&usb2_sym_lock);
2085 
2086 	free(ps, M_USBDEV);
2087 }
2088 
2089 /*------------------------------------------------------------------------*
2090  *	usb2_read_symlink
2091  *
2092  * Return value:
2093  * 0: Success
2094  * Else: Failure
2095  *------------------------------------------------------------------------*/
2096 int
2097 usb2_read_symlink(uint8_t *user_ptr, uint32_t startentry, uint32_t user_len)
2098 {
2099 	struct usb_symlink *ps;
2100 	uint32_t temp;
2101 	uint32_t delta = 0;
2102 	uint8_t len;
2103 	int error = 0;
2104 
2105 	sx_xlock(&usb2_sym_lock);
2106 
2107 	TAILQ_FOREACH(ps, &usb2_sym_head, sym_entry) {
2108 
2109 		/*
2110 		 * Compute total length of source and destination symlink
2111 		 * strings pluss one length byte and two NUL bytes:
2112 		 */
2113 		temp = ps->src_len + ps->dst_len + 3;
2114 
2115 		if (temp > 255) {
2116 			/*
2117 			 * Skip entry because this length cannot fit
2118 			 * into one byte:
2119 			 */
2120 			continue;
2121 		}
2122 		if (startentry != 0) {
2123 			/* decrement read offset */
2124 			startentry--;
2125 			continue;
2126 		}
2127 		if (temp > user_len) {
2128 			/* out of buffer space */
2129 			break;
2130 		}
2131 		len = temp;
2132 
2133 		/* copy out total length */
2134 
2135 		error = copyout(&len,
2136 		    USB_ADD_BYTES(user_ptr, delta), 1);
2137 		if (error) {
2138 			break;
2139 		}
2140 		delta += 1;
2141 
2142 		/* copy out source string */
2143 
2144 		error = copyout(ps->src_path,
2145 		    USB_ADD_BYTES(user_ptr, delta), ps->src_len);
2146 		if (error) {
2147 			break;
2148 		}
2149 		len = 0;
2150 		delta += ps->src_len;
2151 		error = copyout(&len,
2152 		    USB_ADD_BYTES(user_ptr, delta), 1);
2153 		if (error) {
2154 			break;
2155 		}
2156 		delta += 1;
2157 
2158 		/* copy out destination string */
2159 
2160 		error = copyout(ps->dst_path,
2161 		    USB_ADD_BYTES(user_ptr, delta), ps->dst_len);
2162 		if (error) {
2163 			break;
2164 		}
2165 		len = 0;
2166 		delta += ps->dst_len;
2167 		error = copyout(&len,
2168 		    USB_ADD_BYTES(user_ptr, delta), 1);
2169 		if (error) {
2170 			break;
2171 		}
2172 		delta += 1;
2173 
2174 		user_len -= temp;
2175 	}
2176 
2177 	/* a zero length entry indicates the end */
2178 
2179 	if ((user_len != 0) && (error == 0)) {
2180 
2181 		len = 0;
2182 
2183 		error = copyout(&len,
2184 		    USB_ADD_BYTES(user_ptr, delta), 1);
2185 	}
2186 	sx_unlock(&usb2_sym_lock);
2187 	return (error);
2188 }
2189 
2190 void
2191 usb2_fifo_set_close_zlp(struct usb_fifo *f, uint8_t onoff)
2192 {
2193 	if (f == NULL)
2194 		return;
2195 
2196 	/* send a Zero Length Packet, ZLP, before close */
2197 	f->flag_short = onoff;
2198 }
2199 #endif	/* USB_HAVE_UGEN */
2200