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