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