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