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