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