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