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