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