xref: /freebsd/sys/dev/usb/usb_dev.c (revision 7214348f57d62e2a206a349ff1351991e253e416)
102ac6454SAndrew Thompson /* $FreeBSD$ */
202ac6454SAndrew Thompson /*-
302ac6454SAndrew Thompson  * Copyright (c) 2006-2008 Hans Petter Selasky. All rights reserved.
402ac6454SAndrew Thompson  *
502ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
602ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
702ac6454SAndrew Thompson  * are met:
802ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
902ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
1002ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
1102ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
1202ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
1302ac6454SAndrew Thompson  *
1402ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1502ac6454SAndrew Thompson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1602ac6454SAndrew Thompson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1702ac6454SAndrew Thompson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1802ac6454SAndrew Thompson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1902ac6454SAndrew Thompson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2002ac6454SAndrew Thompson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2102ac6454SAndrew Thompson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2202ac6454SAndrew Thompson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2302ac6454SAndrew Thompson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2402ac6454SAndrew Thompson  * SUCH DAMAGE.
2502ac6454SAndrew Thompson  *
2602ac6454SAndrew Thompson  *
2702ac6454SAndrew Thompson  * usb2_dev.c - An abstraction layer for creating devices under /dev/...
2802ac6454SAndrew Thompson  */
2902ac6454SAndrew Thompson 
3002ac6454SAndrew Thompson #include <dev/usb/usb.h>
3102ac6454SAndrew Thompson #include <dev/usb/usb_ioctl.h>
3202ac6454SAndrew Thompson #include <dev/usb/usb_defs.h>
3302ac6454SAndrew Thompson #include <dev/usb/usb_mfunc.h>
3402ac6454SAndrew Thompson #include <dev/usb/usb_error.h>
3502ac6454SAndrew Thompson 
3602ac6454SAndrew Thompson #define	USB_DEBUG_VAR usb2_fifo_debug
3702ac6454SAndrew Thompson 
3802ac6454SAndrew Thompson #include <dev/usb/usb_core.h>
3902ac6454SAndrew Thompson #include <dev/usb/usb_mbuf.h>
4002ac6454SAndrew Thompson #include <dev/usb/usb_dev.h>
4102ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
4202ac6454SAndrew Thompson #include <dev/usb/usb_device.h>
4302ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
4402ac6454SAndrew Thompson #include <dev/usb/usb_busdma.h>
4502ac6454SAndrew Thompson #include <dev/usb/usb_generic.h>
4602ac6454SAndrew Thompson #include <dev/usb/usb_dynamic.h>
4702ac6454SAndrew Thompson #include <dev/usb/usb_util.h>
4802ac6454SAndrew Thompson 
4902ac6454SAndrew Thompson #include <dev/usb/usb_controller.h>
5002ac6454SAndrew Thompson #include <dev/usb/usb_bus.h>
5102ac6454SAndrew Thompson 
5202ac6454SAndrew Thompson #include <sys/filio.h>
5302ac6454SAndrew Thompson #include <sys/ttycom.h>
5402ac6454SAndrew Thompson #include <sys/syscallsubr.h>
5502ac6454SAndrew Thompson 
5602ac6454SAndrew Thompson #include <machine/stdarg.h>
5702ac6454SAndrew Thompson 
5802ac6454SAndrew Thompson #if USB_DEBUG
5902ac6454SAndrew Thompson static int usb2_fifo_debug = 0;
6002ac6454SAndrew Thompson 
6102ac6454SAndrew Thompson SYSCTL_NODE(_hw_usb2, OID_AUTO, dev, CTLFLAG_RW, 0, "USB device");
6202ac6454SAndrew Thompson SYSCTL_INT(_hw_usb2_dev, OID_AUTO, debug, CTLFLAG_RW,
6302ac6454SAndrew Thompson     &usb2_fifo_debug, 0, "Debug Level");
6402ac6454SAndrew Thompson #endif
6502ac6454SAndrew Thompson 
6602ac6454SAndrew Thompson #if ((__FreeBSD_version >= 700001) || (__FreeBSD_version == 0) || \
6702ac6454SAndrew Thompson      ((__FreeBSD_version >= 600034) && (__FreeBSD_version < 700000)))
6802ac6454SAndrew Thompson #define	USB_UCRED struct ucred *ucred,
6902ac6454SAndrew Thompson #else
7002ac6454SAndrew Thompson #define	USB_UCRED
7102ac6454SAndrew Thompson #endif
7202ac6454SAndrew Thompson 
7302ac6454SAndrew Thompson /* prototypes */
7402ac6454SAndrew Thompson 
757214348fSAndrew Thompson static int	usb2_fifo_open(struct usb2_cdev_privdata *,
767214348fSAndrew Thompson 		    struct usb2_fifo *, int);
77ee3e3ff5SAndrew Thompson static void	usb2_fifo_close(struct usb2_fifo *, int);
7802ac6454SAndrew Thompson static void	usb2_dev_init(void *);
7902ac6454SAndrew Thompson static void	usb2_dev_init_post(void *);
8002ac6454SAndrew Thompson static void	usb2_dev_uninit(void *);
8102ac6454SAndrew Thompson static int	usb2_fifo_uiomove(struct usb2_fifo *, void *, int,
8202ac6454SAndrew Thompson 		    struct uio *);
8302ac6454SAndrew Thompson static void	usb2_fifo_check_methods(struct usb2_fifo_methods *);
8402ac6454SAndrew Thompson static struct	usb2_fifo *usb2_fifo_alloc(void);
8502ac6454SAndrew Thompson static struct	usb2_pipe *usb2_dev_get_pipe(struct usb2_device *, uint8_t,
86f5f145baSAndrew Thompson 		    uint8_t);
87ee3e3ff5SAndrew Thompson static void	usb2_loc_fill(struct usb2_fs_privdata *,
88ee3e3ff5SAndrew Thompson 		    struct usb2_cdev_privdata *);
89ee3e3ff5SAndrew Thompson static void	usb2_close(void *);
90ee3e3ff5SAndrew Thompson static usb2_error_t usb2_ref_device(struct usb2_cdev_privdata *, int);
91ee3e3ff5SAndrew Thompson static usb2_error_t usb2_uref_location(struct usb2_cdev_privdata *);
92ee3e3ff5SAndrew Thompson static void	usb2_unref_device(struct usb2_cdev_privdata *);
9302ac6454SAndrew Thompson 
94ee3e3ff5SAndrew Thompson static d_open_t usb2_open;
9502ac6454SAndrew Thompson static d_ioctl_t usb2_ioctl;
96ee3e3ff5SAndrew Thompson static d_read_t usb2_read;
97ee3e3ff5SAndrew Thompson static d_write_t usb2_write;
98ee3e3ff5SAndrew Thompson static d_poll_t usb2_poll;
9902ac6454SAndrew Thompson 
100ee3e3ff5SAndrew Thompson static d_ioctl_t usb2_static_ioctl;
10102ac6454SAndrew Thompson 
10202ac6454SAndrew Thompson static usb2_fifo_open_t usb2_fifo_dummy_open;
10302ac6454SAndrew Thompson static usb2_fifo_close_t usb2_fifo_dummy_close;
10402ac6454SAndrew Thompson static usb2_fifo_ioctl_t usb2_fifo_dummy_ioctl;
10502ac6454SAndrew Thompson static usb2_fifo_cmd_t usb2_fifo_dummy_cmd;
10602ac6454SAndrew Thompson 
107ee3e3ff5SAndrew Thompson /* character device structure used for devices (/dev/ugenX.Y and /dev/uXXX) */
108ee3e3ff5SAndrew Thompson struct cdevsw usb2_devsw = {
10902ac6454SAndrew Thompson 	.d_version = D_VERSION,
110ee3e3ff5SAndrew Thompson 	.d_open = usb2_open,
11102ac6454SAndrew Thompson 	.d_ioctl = usb2_ioctl,
112ee3e3ff5SAndrew Thompson 	.d_name = "usbdev",
11302ac6454SAndrew Thompson 	.d_flags = D_TRACKCLOSE,
114ee3e3ff5SAndrew Thompson 	.d_read = usb2_read,
115ee3e3ff5SAndrew Thompson 	.d_write = usb2_write,
116ee3e3ff5SAndrew Thompson 	.d_poll = usb2_poll
11702ac6454SAndrew Thompson };
11802ac6454SAndrew Thompson 
119ee3e3ff5SAndrew Thompson static struct cdev* usb2_dev = NULL;
120ee3e3ff5SAndrew Thompson 
121ee3e3ff5SAndrew Thompson /* character device structure used for /dev/usb */
122ee3e3ff5SAndrew Thompson struct cdevsw usb2_static_devsw = {
123ee3e3ff5SAndrew Thompson 	.d_version = D_VERSION,
124ee3e3ff5SAndrew Thompson 	.d_ioctl = usb2_static_ioctl,
125ee3e3ff5SAndrew Thompson 	.d_name = "usb"
12602ac6454SAndrew Thompson };
12702ac6454SAndrew Thompson 
12802ac6454SAndrew Thompson static TAILQ_HEAD(, usb2_symlink) usb2_sym_head;
12902ac6454SAndrew Thompson static struct sx usb2_sym_lock;
13002ac6454SAndrew Thompson 
13102ac6454SAndrew Thompson struct mtx usb2_ref_lock;
13202ac6454SAndrew Thompson 
13302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
134ee3e3ff5SAndrew Thompson  *	usb2_loc_fill
13502ac6454SAndrew Thompson  *
136ee3e3ff5SAndrew Thompson  * This is used to fill out a usb2_cdev_privdata structure based on the
137ee3e3ff5SAndrew Thompson  * device's address as contained in usb2_fs_privdata.
13802ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
139ee3e3ff5SAndrew Thompson static void
140ee3e3ff5SAndrew Thompson usb2_loc_fill(struct usb2_fs_privdata* pd, struct usb2_cdev_privdata *cpd)
14102ac6454SAndrew Thompson {
142ee3e3ff5SAndrew Thompson 	cpd->bus_index = pd->bus_index;
143ee3e3ff5SAndrew Thompson 	cpd->dev_index = pd->dev_index;
144ee3e3ff5SAndrew Thompson 	cpd->ep_addr = pd->ep_addr;
145ee3e3ff5SAndrew Thompson 	cpd->fifo_index = pd->fifo_index;
14602ac6454SAndrew Thompson }
14702ac6454SAndrew Thompson 
14802ac6454SAndrew Thompson /*------------------------------------------------------------------------*
14902ac6454SAndrew Thompson  *	usb2_ref_device
15002ac6454SAndrew Thompson  *
15102ac6454SAndrew Thompson  * This function is used to atomically refer an USB device by its
15202ac6454SAndrew Thompson  * device location. If this function returns success the USB device
15302ac6454SAndrew Thompson  * will not dissappear until the USB device is unreferenced.
15402ac6454SAndrew Thompson  *
15502ac6454SAndrew Thompson  * Return values:
15602ac6454SAndrew Thompson  *  0: Success, refcount incremented on the given USB device.
15702ac6454SAndrew Thompson  *  Else: Failure.
15802ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
15902ac6454SAndrew Thompson usb2_error_t
160ee3e3ff5SAndrew Thompson usb2_ref_device(struct usb2_cdev_privdata* cpd, int need_uref)
16102ac6454SAndrew Thompson {
16202ac6454SAndrew Thompson 	struct usb2_fifo **ppf;
16302ac6454SAndrew Thompson 	struct usb2_fifo *f;
16402ac6454SAndrew Thompson 
165ee3e3ff5SAndrew Thompson 	DPRINTFN(2, "usb2_ref_device, cpd=%p need uref=%d\n", cpd, need_uref);
16602ac6454SAndrew Thompson 
16702ac6454SAndrew Thompson 	mtx_lock(&usb2_ref_lock);
168ee3e3ff5SAndrew Thompson 	cpd->bus = devclass_get_softc(usb2_devclass_ptr, cpd->bus_index);
169ee3e3ff5SAndrew Thompson 	if (cpd->bus == NULL) {
170ee3e3ff5SAndrew Thompson 		DPRINTFN(2, "no bus at %u\n", cpd->bus_index);
17102ac6454SAndrew Thompson 		goto error;
17202ac6454SAndrew Thompson 	}
173ee3e3ff5SAndrew Thompson 	cpd->udev = cpd->bus->devices[cpd->dev_index];
174ee3e3ff5SAndrew Thompson 	if (cpd->udev == NULL) {
175ee3e3ff5SAndrew Thompson 		DPRINTFN(2, "no device at %u\n", cpd->dev_index);
17602ac6454SAndrew Thompson 		goto error;
17702ac6454SAndrew Thompson 	}
178ee3e3ff5SAndrew Thompson 	if (cpd->udev->refcount == USB_DEV_REF_MAX) {
17902ac6454SAndrew Thompson 		DPRINTFN(2, "no dev ref\n");
18002ac6454SAndrew Thompson 		goto error;
18102ac6454SAndrew Thompson 	}
18202ac6454SAndrew Thompson 	/* check if we are doing an open */
183ee3e3ff5SAndrew Thompson 	if (cpd->fflags == 0) {
18402ac6454SAndrew Thompson 		/* set defaults */
185ee3e3ff5SAndrew Thompson 		cpd->txfifo = NULL;
186ee3e3ff5SAndrew Thompson 		cpd->rxfifo = NULL;
187ee3e3ff5SAndrew Thompson 		cpd->is_write = 0;
188ee3e3ff5SAndrew Thompson 		cpd->is_read = 0;
189ee3e3ff5SAndrew Thompson 		cpd->is_usbfs = 0;
19002ac6454SAndrew Thompson 	} else {
19102ac6454SAndrew Thompson 		/* initialise "is_usbfs" flag */
192ee3e3ff5SAndrew Thompson 		cpd->is_usbfs = 0;
19302ac6454SAndrew Thompson 
19402ac6454SAndrew Thompson 		/* check for write */
195ee3e3ff5SAndrew Thompson 		if (cpd->fflags & FWRITE) {
196ee3e3ff5SAndrew Thompson 			ppf = cpd->udev->fifo;
197ee3e3ff5SAndrew Thompson 			f = ppf[cpd->fifo_index + USB_FIFO_TX];
198ee3e3ff5SAndrew Thompson 			cpd->txfifo = f;
199ee3e3ff5SAndrew Thompson 			cpd->is_write = 1;	/* ref */
200ee3e3ff5SAndrew Thompson 			if (f == NULL || f->refcount == USB_FIFO_REF_MAX)
20102ac6454SAndrew Thompson 				goto error;
2027214348fSAndrew Thompson 			if (f->curr_cpd != cpd)
2037214348fSAndrew Thompson 				goto error;
20402ac6454SAndrew Thompson 			/* check if USB-FS is active */
20502ac6454SAndrew Thompson 			if (f->fs_ep_max != 0) {
206ee3e3ff5SAndrew Thompson 				cpd->is_usbfs = 1;
20702ac6454SAndrew Thompson 			}
20802ac6454SAndrew Thompson 		} else {
209ee3e3ff5SAndrew Thompson 			cpd->txfifo = NULL;
210ee3e3ff5SAndrew Thompson 			cpd->is_write = 0;	/* no ref */
21102ac6454SAndrew Thompson 		}
21202ac6454SAndrew Thompson 
21302ac6454SAndrew Thompson 		/* check for read */
214ee3e3ff5SAndrew Thompson 		if (cpd->fflags & FREAD) {
215ee3e3ff5SAndrew Thompson 			ppf = cpd->udev->fifo;
216ee3e3ff5SAndrew Thompson 			f = ppf[cpd->fifo_index + USB_FIFO_RX];
217ee3e3ff5SAndrew Thompson 			cpd->rxfifo = f;
218ee3e3ff5SAndrew Thompson 			cpd->is_read = 1;	/* ref */
219ee3e3ff5SAndrew Thompson 			if (f == NULL || f->refcount == USB_FIFO_REF_MAX)
22002ac6454SAndrew Thompson 				goto error;
2217214348fSAndrew Thompson 			if (f->curr_cpd != cpd)
2227214348fSAndrew Thompson 				goto error;
22302ac6454SAndrew Thompson 			/* check if USB-FS is active */
22402ac6454SAndrew Thompson 			if (f->fs_ep_max != 0) {
225ee3e3ff5SAndrew Thompson 				cpd->is_usbfs = 1;
22602ac6454SAndrew Thompson 			}
22702ac6454SAndrew Thompson 		} else {
228ee3e3ff5SAndrew Thompson 			cpd->rxfifo = NULL;
229ee3e3ff5SAndrew Thompson 			cpd->is_read = 0;	/* no ref */
23002ac6454SAndrew Thompson 		}
23102ac6454SAndrew Thompson 	}
23202ac6454SAndrew Thompson 
23302ac6454SAndrew Thompson 	/* when everything is OK we increment the refcounts */
234ee3e3ff5SAndrew Thompson 	if (cpd->is_write) {
23502ac6454SAndrew Thompson 		DPRINTFN(2, "ref write\n");
236ee3e3ff5SAndrew Thompson 		cpd->txfifo->refcount++;
23702ac6454SAndrew Thompson 	}
238ee3e3ff5SAndrew Thompson 	if (cpd->is_read) {
23902ac6454SAndrew Thompson 		DPRINTFN(2, "ref read\n");
240ee3e3ff5SAndrew Thompson 		cpd->rxfifo->refcount++;
24102ac6454SAndrew Thompson 	}
242ee3e3ff5SAndrew Thompson 	if (need_uref) {
24302ac6454SAndrew Thompson 		DPRINTFN(2, "ref udev - needed\n");
244ee3e3ff5SAndrew Thompson 		cpd->udev->refcount++;
245ee3e3ff5SAndrew Thompson 		cpd->is_uref = 1;
24602ac6454SAndrew Thompson 	}
24702ac6454SAndrew Thompson 	mtx_unlock(&usb2_ref_lock);
24802ac6454SAndrew Thompson 
249ee3e3ff5SAndrew Thompson 	if (cpd->is_uref) {
25002ac6454SAndrew Thompson 		/*
25102ac6454SAndrew Thompson 		 * We are about to alter the bus-state. Apply the
25202ac6454SAndrew Thompson 		 * required locks.
25302ac6454SAndrew Thompson 		 */
254ee3e3ff5SAndrew Thompson 		sx_xlock(cpd->udev->default_sx + 1);
25502ac6454SAndrew Thompson 		mtx_lock(&Giant);	/* XXX */
25602ac6454SAndrew Thompson 	}
25702ac6454SAndrew Thompson 	return (0);
25802ac6454SAndrew Thompson 
25902ac6454SAndrew Thompson error:
26002ac6454SAndrew Thompson 	mtx_unlock(&usb2_ref_lock);
26102ac6454SAndrew Thompson 	DPRINTFN(2, "fail\n");
26202ac6454SAndrew Thompson 	return (USB_ERR_INVAL);
26302ac6454SAndrew Thompson }
26402ac6454SAndrew Thompson 
26502ac6454SAndrew Thompson /*------------------------------------------------------------------------*
26602ac6454SAndrew Thompson  *	usb2_uref_location
26702ac6454SAndrew Thompson  *
26802ac6454SAndrew Thompson  * This function is used to upgrade an USB reference to include the
26902ac6454SAndrew Thompson  * USB device reference on a USB location.
27002ac6454SAndrew Thompson  *
27102ac6454SAndrew Thompson  * Return values:
27202ac6454SAndrew Thompson  *  0: Success, refcount incremented on the given USB device.
27302ac6454SAndrew Thompson  *  Else: Failure.
27402ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
27502ac6454SAndrew Thompson static usb2_error_t
276ee3e3ff5SAndrew Thompson usb2_uref_location(struct usb2_cdev_privdata *cpd)
27702ac6454SAndrew Thompson {
27802ac6454SAndrew Thompson 	/*
27902ac6454SAndrew Thompson 	 * Check if we already got an USB reference on this location:
28002ac6454SAndrew Thompson 	 */
281ee3e3ff5SAndrew Thompson 	if (cpd->is_uref) {
28202ac6454SAndrew Thompson 		return (0);		/* success */
28302ac6454SAndrew Thompson 	}
28402ac6454SAndrew Thompson 	mtx_lock(&usb2_ref_lock);
285ee3e3ff5SAndrew Thompson 	if (cpd->bus != devclass_get_softc(usb2_devclass_ptr, cpd->bus_index)) {
286ee3e3ff5SAndrew Thompson 		DPRINTFN(2, "bus changed at %u\n", cpd->bus_index);
28702ac6454SAndrew Thompson 		goto error;
28802ac6454SAndrew Thompson 	}
289ee3e3ff5SAndrew Thompson 	if (cpd->udev != cpd->bus->devices[cpd->dev_index]) {
290ee3e3ff5SAndrew Thompson 		DPRINTFN(2, "device changed at %u\n", cpd->dev_index);
29102ac6454SAndrew Thompson 		goto error;
29202ac6454SAndrew Thompson 	}
293ee3e3ff5SAndrew Thompson 	if (cpd->udev->refcount == USB_DEV_REF_MAX) {
29402ac6454SAndrew Thompson 		DPRINTFN(2, "no dev ref\n");
29502ac6454SAndrew Thompson 		goto error;
29602ac6454SAndrew Thompson 	}
29702ac6454SAndrew Thompson 	DPRINTFN(2, "ref udev\n");
298ee3e3ff5SAndrew Thompson 	cpd->udev->refcount++;
29902ac6454SAndrew Thompson 	mtx_unlock(&usb2_ref_lock);
30002ac6454SAndrew Thompson 
30102ac6454SAndrew Thompson 	/* set "uref" */
302ee3e3ff5SAndrew Thompson 	cpd->is_uref = 1;
30302ac6454SAndrew Thompson 
30402ac6454SAndrew Thompson 	/*
30502ac6454SAndrew Thompson 	 * We are about to alter the bus-state. Apply the
30602ac6454SAndrew Thompson 	 * required locks.
30702ac6454SAndrew Thompson 	 */
308ee3e3ff5SAndrew Thompson 	sx_xlock(cpd->udev->default_sx + 1);
30902ac6454SAndrew Thompson 	mtx_lock(&Giant);		/* XXX */
31002ac6454SAndrew Thompson 	return (0);
31102ac6454SAndrew Thompson 
31202ac6454SAndrew Thompson error:
31302ac6454SAndrew Thompson 	mtx_unlock(&usb2_ref_lock);
31402ac6454SAndrew Thompson 	DPRINTFN(2, "fail\n");
31502ac6454SAndrew Thompson 	return (USB_ERR_INVAL);
31602ac6454SAndrew Thompson }
31702ac6454SAndrew Thompson 
31802ac6454SAndrew Thompson /*------------------------------------------------------------------------*
31902ac6454SAndrew Thompson  *	usb2_unref_device
32002ac6454SAndrew Thompson  *
32102ac6454SAndrew Thompson  * This function will release the reference count by one unit for the
32202ac6454SAndrew Thompson  * given USB device.
32302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
32402ac6454SAndrew Thompson void
325ee3e3ff5SAndrew Thompson usb2_unref_device(struct usb2_cdev_privdata *cpd)
32602ac6454SAndrew Thompson {
327ee3e3ff5SAndrew Thompson 	if (cpd->is_uref) {
32802ac6454SAndrew Thompson 		mtx_unlock(&Giant);	/* XXX */
329ee3e3ff5SAndrew Thompson 		sx_unlock(cpd->udev->default_sx + 1);
33002ac6454SAndrew Thompson 	}
33102ac6454SAndrew Thompson 	mtx_lock(&usb2_ref_lock);
332ee3e3ff5SAndrew Thompson 	if (cpd->is_read) {
333ee3e3ff5SAndrew Thompson 		if (--(cpd->rxfifo->refcount) == 0) {
334ee3e3ff5SAndrew Thompson 			usb2_cv_signal(&cpd->rxfifo->cv_drain);
33502ac6454SAndrew Thompson 		}
336ee3e3ff5SAndrew Thompson 		cpd->is_read = 0;
33702ac6454SAndrew Thompson 	}
338ee3e3ff5SAndrew Thompson 	if (cpd->is_write) {
339ee3e3ff5SAndrew Thompson 		if (--(cpd->txfifo->refcount) == 0) {
340ee3e3ff5SAndrew Thompson 			usb2_cv_signal(&cpd->txfifo->cv_drain);
34102ac6454SAndrew Thompson 		}
342ee3e3ff5SAndrew Thompson 		cpd->is_write = 0;
34302ac6454SAndrew Thompson 	}
344ee3e3ff5SAndrew Thompson 	if (cpd->is_uref) {
345ee3e3ff5SAndrew Thompson 		if (--(cpd->udev->refcount) == 0) {
346ee3e3ff5SAndrew Thompson 			usb2_cv_signal(cpd->udev->default_cv + 1);
34702ac6454SAndrew Thompson 		}
348ee3e3ff5SAndrew Thompson 		cpd->is_uref = 0;
34902ac6454SAndrew Thompson 	}
35002ac6454SAndrew Thompson 	mtx_unlock(&usb2_ref_lock);
35102ac6454SAndrew Thompson }
35202ac6454SAndrew Thompson 
35302ac6454SAndrew Thompson static struct usb2_fifo *
35402ac6454SAndrew Thompson usb2_fifo_alloc(void)
35502ac6454SAndrew Thompson {
35602ac6454SAndrew Thompson 	struct usb2_fifo *f;
35702ac6454SAndrew Thompson 
35802ac6454SAndrew Thompson 	f = malloc(sizeof(*f), M_USBDEV, M_WAITOK | M_ZERO);
35902ac6454SAndrew Thompson 	if (f) {
36002ac6454SAndrew Thompson 		usb2_cv_init(&f->cv_io, "FIFO-IO");
36102ac6454SAndrew Thompson 		usb2_cv_init(&f->cv_drain, "FIFO-DRAIN");
36202ac6454SAndrew Thompson 		f->refcount = 1;
36302ac6454SAndrew Thompson 	}
36402ac6454SAndrew Thompson 	return (f);
36502ac6454SAndrew Thompson }
36602ac6454SAndrew Thompson 
36702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
36802ac6454SAndrew Thompson  *	usb2_fifo_create
36902ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
37002ac6454SAndrew Thompson static int
371ee3e3ff5SAndrew Thompson usb2_fifo_create(struct usb2_cdev_privdata *cpd)
37202ac6454SAndrew Thompson {
373ee3e3ff5SAndrew Thompson 	struct usb2_device *udev = cpd->udev;
37402ac6454SAndrew Thompson 	struct usb2_fifo *f;
37502ac6454SAndrew Thompson 	struct usb2_pipe *pipe;
37602ac6454SAndrew Thompson 	uint8_t n;
37702ac6454SAndrew Thompson 	uint8_t is_tx;
37802ac6454SAndrew Thompson 	uint8_t is_rx;
37902ac6454SAndrew Thompson 	uint8_t no_null;
38002ac6454SAndrew Thompson 	uint8_t is_busy;
381ee3e3ff5SAndrew Thompson 	int ep = cpd->ep_addr;
38202ac6454SAndrew Thompson 
383ee3e3ff5SAndrew Thompson 	is_tx = (cpd->fflags & FWRITE) ? 1 : 0;
384ee3e3ff5SAndrew Thompson 	is_rx = (cpd->fflags & FREAD) ? 1 : 0;
38502ac6454SAndrew Thompson 	no_null = 1;
38602ac6454SAndrew Thompson 	is_busy = 0;
38702ac6454SAndrew Thompson 
388ee3e3ff5SAndrew Thompson 	/* Preallocated FIFO */
389ee3e3ff5SAndrew Thompson 	if (ep < 0) {
390ee3e3ff5SAndrew Thompson 		DPRINTFN(5, "Preallocated FIFO\n");
391ee3e3ff5SAndrew Thompson 		if (is_tx) {
392ee3e3ff5SAndrew Thompson 			f = udev->fifo[cpd->fifo_index + USB_FIFO_TX];
393ee3e3ff5SAndrew Thompson 			if (f == NULL)
394ee3e3ff5SAndrew Thompson 				return (EINVAL);
395ee3e3ff5SAndrew Thompson 			cpd->txfifo = f;
396ee3e3ff5SAndrew Thompson 		}
397ee3e3ff5SAndrew Thompson 		if (is_rx) {
398ee3e3ff5SAndrew Thompson 			f = udev->fifo[cpd->fifo_index + USB_FIFO_RX];
399ee3e3ff5SAndrew Thompson 			if (f == NULL)
400ee3e3ff5SAndrew Thompson 				return (EINVAL);
401ee3e3ff5SAndrew Thompson 			cpd->rxfifo = f;
402ee3e3ff5SAndrew Thompson 		}
403ee3e3ff5SAndrew Thompson 		return (0);
404ee3e3ff5SAndrew Thompson 	}
40502ac6454SAndrew Thompson 
406ee3e3ff5SAndrew Thompson 	KASSERT(ep >= 0 && ep <= 15, ("endpoint %d out of range", ep));
407ee3e3ff5SAndrew Thompson 
408ee3e3ff5SAndrew Thompson 	/* search for a free FIFO slot */
409ee3e3ff5SAndrew Thompson 	DPRINTFN(5, "Endpoint device, searching for 0x%02x\n", ep);
41002ac6454SAndrew Thompson 	for (n = 0;; n += 2) {
41102ac6454SAndrew Thompson 
41202ac6454SAndrew Thompson 		if (n == USB_FIFO_MAX) {
41302ac6454SAndrew Thompson 			if (no_null) {
41402ac6454SAndrew Thompson 				no_null = 0;
41502ac6454SAndrew Thompson 				n = 0;
41602ac6454SAndrew Thompson 			} else {
41702ac6454SAndrew Thompson 				/* end of FIFOs reached */
418ee3e3ff5SAndrew Thompson 				DPRINTFN(5, "out of FIFOs\n");
41902ac6454SAndrew Thompson 				return (ENOMEM);
42002ac6454SAndrew Thompson 			}
42102ac6454SAndrew Thompson 		}
42202ac6454SAndrew Thompson 		/* Check for TX FIFO */
42302ac6454SAndrew Thompson 		if (is_tx) {
42402ac6454SAndrew Thompson 			f = udev->fifo[n + USB_FIFO_TX];
42502ac6454SAndrew Thompson 			if (f != NULL) {
426ee3e3ff5SAndrew Thompson 				if (f->dev_ep_index != ep) {
42702ac6454SAndrew Thompson 					/* wrong endpoint index */
42802ac6454SAndrew Thompson 					continue;
42902ac6454SAndrew Thompson 				}
4307214348fSAndrew Thompson 				if (f->curr_cpd != NULL) {
43102ac6454SAndrew Thompson 					/* FIFO is opened */
43202ac6454SAndrew Thompson 					is_busy = 1;
43302ac6454SAndrew Thompson 					continue;
43402ac6454SAndrew Thompson 				}
43502ac6454SAndrew Thompson 			} else if (no_null) {
43602ac6454SAndrew Thompson 				continue;
43702ac6454SAndrew Thompson 			}
43802ac6454SAndrew Thompson 		}
43902ac6454SAndrew Thompson 		/* Check for RX FIFO */
44002ac6454SAndrew Thompson 		if (is_rx) {
44102ac6454SAndrew Thompson 			f = udev->fifo[n + USB_FIFO_RX];
44202ac6454SAndrew Thompson 			if (f != NULL) {
443ee3e3ff5SAndrew Thompson 				if (f->dev_ep_index != ep) {
44402ac6454SAndrew Thompson 					/* wrong endpoint index */
44502ac6454SAndrew Thompson 					continue;
44602ac6454SAndrew Thompson 				}
4477214348fSAndrew Thompson 				if (f->curr_cpd != NULL) {
44802ac6454SAndrew Thompson 					/* FIFO is opened */
44902ac6454SAndrew Thompson 					is_busy = 1;
45002ac6454SAndrew Thompson 					continue;
45102ac6454SAndrew Thompson 				}
45202ac6454SAndrew Thompson 			} else if (no_null) {
45302ac6454SAndrew Thompson 				continue;
45402ac6454SAndrew Thompson 			}
45502ac6454SAndrew Thompson 		}
45602ac6454SAndrew Thompson 		break;
45702ac6454SAndrew Thompson 	}
45802ac6454SAndrew Thompson 
45902ac6454SAndrew Thompson 	if (no_null == 0) {
460ee3e3ff5SAndrew Thompson 		if (ep >= (USB_EP_MAX / 2)) {
46102ac6454SAndrew Thompson 			/* we don't create any endpoints in this range */
4627214348fSAndrew Thompson 			DPRINTFN(5, "ep out of range\n");
46302ac6454SAndrew Thompson 			return (is_busy ? EBUSY : EINVAL);
46402ac6454SAndrew Thompson 		}
46502ac6454SAndrew Thompson 	}
4667214348fSAndrew Thompson 
4677214348fSAndrew Thompson 	if ((ep != 0) && is_busy) {
4687214348fSAndrew Thompson 		/*
4697214348fSAndrew Thompson 		 * Only the default control endpoint is allowed to be
4707214348fSAndrew Thompson 		 * opened multiple times!
4717214348fSAndrew Thompson 		 */
4727214348fSAndrew Thompson 		DPRINTFN(5, "busy\n");
4737214348fSAndrew Thompson 		return (EBUSY);
4747214348fSAndrew Thompson 	}
4757214348fSAndrew Thompson 
47602ac6454SAndrew Thompson 	/* Check TX FIFO */
47702ac6454SAndrew Thompson 	if (is_tx &&
47802ac6454SAndrew Thompson 	    (udev->fifo[n + USB_FIFO_TX] == NULL)) {
479f35aaff0SAndrew Thompson 		pipe = usb2_dev_get_pipe(udev, ep, USB_FIFO_TX);
480f5f145baSAndrew Thompson 		DPRINTFN(5, "dev_get_pipe(%d, 0x%x)\n", ep, USB_FIFO_TX);
48102ac6454SAndrew Thompson 		if (pipe == NULL) {
482ee3e3ff5SAndrew Thompson 			DPRINTFN(5, "dev_get_pipe returned NULL\n");
48302ac6454SAndrew Thompson 			return (EINVAL);
48402ac6454SAndrew Thompson 		}
48502ac6454SAndrew Thompson 		f = usb2_fifo_alloc();
48602ac6454SAndrew Thompson 		if (f == NULL) {
487ee3e3ff5SAndrew Thompson 			DPRINTFN(5, "could not alloc tx fifo\n");
48802ac6454SAndrew Thompson 			return (ENOMEM);
48902ac6454SAndrew Thompson 		}
49002ac6454SAndrew Thompson 		/* update some fields */
49102ac6454SAndrew Thompson 		f->fifo_index = n + USB_FIFO_TX;
492ee3e3ff5SAndrew Thompson 		f->dev_ep_index = ep;
49302ac6454SAndrew Thompson 		f->priv_mtx = udev->default_mtx;
49402ac6454SAndrew Thompson 		f->priv_sc0 = pipe;
49502ac6454SAndrew Thompson 		f->methods = &usb2_ugen_methods;
496f5f145baSAndrew Thompson 		f->iface_index = pipe->iface_index;
49702ac6454SAndrew Thompson 		f->udev = udev;
49802ac6454SAndrew Thompson 		mtx_lock(&usb2_ref_lock);
49902ac6454SAndrew Thompson 		udev->fifo[n + USB_FIFO_TX] = f;
50002ac6454SAndrew Thompson 		mtx_unlock(&usb2_ref_lock);
50102ac6454SAndrew Thompson 	}
50202ac6454SAndrew Thompson 	/* Check RX FIFO */
50302ac6454SAndrew Thompson 	if (is_rx &&
50402ac6454SAndrew Thompson 	    (udev->fifo[n + USB_FIFO_RX] == NULL)) {
50502ac6454SAndrew Thompson 
506f35aaff0SAndrew Thompson 		pipe = usb2_dev_get_pipe(udev, ep, USB_FIFO_RX);
507f5f145baSAndrew Thompson 		DPRINTFN(5, "dev_get_pipe(%d, 0x%x)\n", ep, USB_FIFO_RX);
50802ac6454SAndrew Thompson 		if (pipe == NULL) {
509ee3e3ff5SAndrew Thompson 			DPRINTFN(5, "dev_get_pipe returned NULL\n");
51002ac6454SAndrew Thompson 			return (EINVAL);
51102ac6454SAndrew Thompson 		}
51202ac6454SAndrew Thompson 		f = usb2_fifo_alloc();
51302ac6454SAndrew Thompson 		if (f == NULL) {
514ee3e3ff5SAndrew Thompson 			DPRINTFN(5, "could not alloc rx fifo\n");
51502ac6454SAndrew Thompson 			return (ENOMEM);
51602ac6454SAndrew Thompson 		}
51702ac6454SAndrew Thompson 		/* update some fields */
51802ac6454SAndrew Thompson 		f->fifo_index = n + USB_FIFO_RX;
519ee3e3ff5SAndrew Thompson 		f->dev_ep_index = ep;
52002ac6454SAndrew Thompson 		f->priv_mtx = udev->default_mtx;
52102ac6454SAndrew Thompson 		f->priv_sc0 = pipe;
52202ac6454SAndrew Thompson 		f->methods = &usb2_ugen_methods;
523f5f145baSAndrew Thompson 		f->iface_index = pipe->iface_index;
52402ac6454SAndrew Thompson 		f->udev = udev;
52502ac6454SAndrew Thompson 		mtx_lock(&usb2_ref_lock);
52602ac6454SAndrew Thompson 		udev->fifo[n + USB_FIFO_RX] = f;
52702ac6454SAndrew Thompson 		mtx_unlock(&usb2_ref_lock);
52802ac6454SAndrew Thompson 	}
52902ac6454SAndrew Thompson 	if (is_tx) {
530ee3e3ff5SAndrew Thompson 		cpd->txfifo = udev->fifo[n + USB_FIFO_TX];
53102ac6454SAndrew Thompson 	}
53202ac6454SAndrew Thompson 	if (is_rx) {
533ee3e3ff5SAndrew Thompson 		cpd->rxfifo = udev->fifo[n + USB_FIFO_RX];
53402ac6454SAndrew Thompson 	}
535ee3e3ff5SAndrew Thompson 	/* fill out fifo index */
536ee3e3ff5SAndrew Thompson 	DPRINTFN(5, "fifo index = %d\n", n);
537ee3e3ff5SAndrew Thompson 	cpd->fifo_index = n;
53802ac6454SAndrew Thompson 
53902ac6454SAndrew Thompson 	/* complete */
54002ac6454SAndrew Thompson 
54102ac6454SAndrew Thompson 	return (0);
54202ac6454SAndrew Thompson }
54302ac6454SAndrew Thompson 
54402ac6454SAndrew Thompson void
54502ac6454SAndrew Thompson usb2_fifo_free(struct usb2_fifo *f)
54602ac6454SAndrew Thompson {
54702ac6454SAndrew Thompson 	uint8_t n;
54802ac6454SAndrew Thompson 
54902ac6454SAndrew Thompson 	if (f == NULL) {
55002ac6454SAndrew Thompson 		/* be NULL safe */
55102ac6454SAndrew Thompson 		return;
55202ac6454SAndrew Thompson 	}
55302ac6454SAndrew Thompson 	/* destroy symlink devices, if any */
55402ac6454SAndrew Thompson 	for (n = 0; n != 2; n++) {
55502ac6454SAndrew Thompson 		if (f->symlink[n]) {
55602ac6454SAndrew Thompson 			usb2_free_symlink(f->symlink[n]);
55702ac6454SAndrew Thompson 			f->symlink[n] = NULL;
55802ac6454SAndrew Thompson 		}
55902ac6454SAndrew Thompson 	}
56002ac6454SAndrew Thompson 	mtx_lock(&usb2_ref_lock);
56102ac6454SAndrew Thompson 
56202ac6454SAndrew Thompson 	/* delink ourselves to stop calls from userland */
56302ac6454SAndrew Thompson 	if ((f->fifo_index < USB_FIFO_MAX) &&
56402ac6454SAndrew Thompson 	    (f->udev != NULL) &&
56502ac6454SAndrew Thompson 	    (f->udev->fifo[f->fifo_index] == f)) {
56602ac6454SAndrew Thompson 		f->udev->fifo[f->fifo_index] = NULL;
56702ac6454SAndrew Thompson 	} else {
56802ac6454SAndrew Thompson 		DPRINTFN(0, "USB FIFO %p has not been linked!\n", f);
56902ac6454SAndrew Thompson 	}
57002ac6454SAndrew Thompson 
57102ac6454SAndrew Thompson 	/* decrease refcount */
57202ac6454SAndrew Thompson 	f->refcount--;
57302ac6454SAndrew Thompson 	/* prevent any write flush */
57402ac6454SAndrew Thompson 	f->flag_iserror = 1;
57502ac6454SAndrew Thompson 	/* need to wait until all callers have exited */
57602ac6454SAndrew Thompson 	while (f->refcount != 0) {
57702ac6454SAndrew Thompson 		mtx_unlock(&usb2_ref_lock);	/* avoid LOR */
57802ac6454SAndrew Thompson 		mtx_lock(f->priv_mtx);
57902ac6454SAndrew Thompson 		/* get I/O thread out of any sleep state */
58002ac6454SAndrew Thompson 		if (f->flag_sleeping) {
58102ac6454SAndrew Thompson 			f->flag_sleeping = 0;
58202ac6454SAndrew Thompson 			usb2_cv_broadcast(&f->cv_io);
58302ac6454SAndrew Thompson 		}
58402ac6454SAndrew Thompson 		mtx_unlock(f->priv_mtx);
58502ac6454SAndrew Thompson 		mtx_lock(&usb2_ref_lock);
58602ac6454SAndrew Thompson 
58702ac6454SAndrew Thompson 		/* wait for sync */
58802ac6454SAndrew Thompson 		usb2_cv_wait(&f->cv_drain, &usb2_ref_lock);
58902ac6454SAndrew Thompson 	}
59002ac6454SAndrew Thompson 	mtx_unlock(&usb2_ref_lock);
59102ac6454SAndrew Thompson 
59202ac6454SAndrew Thompson 	/* take care of closing the device here, if any */
593ee3e3ff5SAndrew Thompson 	usb2_fifo_close(f, 0);
59402ac6454SAndrew Thompson 
59502ac6454SAndrew Thompson 	usb2_cv_destroy(&f->cv_io);
59602ac6454SAndrew Thompson 	usb2_cv_destroy(&f->cv_drain);
59702ac6454SAndrew Thompson 
59802ac6454SAndrew Thompson 	free(f, M_USBDEV);
59902ac6454SAndrew Thompson }
60002ac6454SAndrew Thompson 
60102ac6454SAndrew Thompson static struct usb2_pipe *
602f35aaff0SAndrew Thompson usb2_dev_get_pipe(struct usb2_device *udev, uint8_t ep_index, uint8_t dir)
60302ac6454SAndrew Thompson {
60402ac6454SAndrew Thompson 	struct usb2_pipe *pipe;
60502ac6454SAndrew Thompson 	uint8_t ep_dir;
60602ac6454SAndrew Thompson 
60702ac6454SAndrew Thompson 	if (ep_index == 0) {
60802ac6454SAndrew Thompson 		pipe = &udev->default_pipe;
60902ac6454SAndrew Thompson 	} else {
61002ac6454SAndrew Thompson 		if (dir == USB_FIFO_RX) {
61102ac6454SAndrew Thompson 			if (udev->flags.usb2_mode == USB_MODE_HOST) {
61202ac6454SAndrew Thompson 				ep_dir = UE_DIR_IN;
61302ac6454SAndrew Thompson 			} else {
61402ac6454SAndrew Thompson 				ep_dir = UE_DIR_OUT;
61502ac6454SAndrew Thompson 			}
61602ac6454SAndrew Thompson 		} else {
61702ac6454SAndrew Thompson 			if (udev->flags.usb2_mode == USB_MODE_HOST) {
61802ac6454SAndrew Thompson 				ep_dir = UE_DIR_OUT;
61902ac6454SAndrew Thompson 			} else {
62002ac6454SAndrew Thompson 				ep_dir = UE_DIR_IN;
62102ac6454SAndrew Thompson 			}
62202ac6454SAndrew Thompson 		}
62302ac6454SAndrew Thompson 		pipe = usb2_get_pipe_by_addr(udev, ep_index | ep_dir);
62402ac6454SAndrew Thompson 	}
62502ac6454SAndrew Thompson 
62602ac6454SAndrew Thompson 	if (pipe == NULL) {
62702ac6454SAndrew Thompson 		/* if the pipe does not exist then return */
62802ac6454SAndrew Thompson 		return (NULL);
62902ac6454SAndrew Thompson 	}
63002ac6454SAndrew Thompson 	if (pipe->edesc == NULL) {
63102ac6454SAndrew Thompson 		/* invalid pipe */
63202ac6454SAndrew Thompson 		return (NULL);
63302ac6454SAndrew Thompson 	}
63402ac6454SAndrew Thompson 	return (pipe);			/* success */
63502ac6454SAndrew Thompson }
63602ac6454SAndrew Thompson 
63702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
63802ac6454SAndrew Thompson  *	usb2_fifo_open
63902ac6454SAndrew Thompson  *
64002ac6454SAndrew Thompson  * Returns:
64102ac6454SAndrew Thompson  * 0: Success
64202ac6454SAndrew Thompson  * Else: Failure
64302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
64402ac6454SAndrew Thompson static int
6457214348fSAndrew Thompson usb2_fifo_open(struct usb2_cdev_privdata *cpd,
6467214348fSAndrew Thompson     struct usb2_fifo *f, int fflags)
64702ac6454SAndrew Thompson {
64802ac6454SAndrew Thompson 	int err;
64902ac6454SAndrew Thompson 
65002ac6454SAndrew Thompson 	if (f == NULL) {
65102ac6454SAndrew Thompson 		/* no FIFO there */
65202ac6454SAndrew Thompson 		DPRINTFN(2, "no FIFO\n");
65302ac6454SAndrew Thompson 		return (ENXIO);
65402ac6454SAndrew Thompson 	}
65502ac6454SAndrew Thompson 	/* remove FWRITE and FREAD flags */
65602ac6454SAndrew Thompson 	fflags &= ~(FWRITE | FREAD);
65702ac6454SAndrew Thompson 
65802ac6454SAndrew Thompson 	/* set correct file flags */
65902ac6454SAndrew Thompson 	if ((f->fifo_index & 1) == USB_FIFO_TX) {
66002ac6454SAndrew Thompson 		fflags |= FWRITE;
66102ac6454SAndrew Thompson 	} else {
66202ac6454SAndrew Thompson 		fflags |= FREAD;
66302ac6454SAndrew Thompson 	}
66402ac6454SAndrew Thompson 
66502ac6454SAndrew Thompson 	/* check if we are already opened */
66602ac6454SAndrew Thompson 	/* we don't need any locks when checking this variable */
6677214348fSAndrew Thompson 	if (f->curr_cpd != NULL) {
66802ac6454SAndrew Thompson 		err = EBUSY;
66902ac6454SAndrew Thompson 		goto done;
67002ac6454SAndrew Thompson 	}
671ee3e3ff5SAndrew Thompson 
6727214348fSAndrew Thompson 	/* reset short flag before open */
6737214348fSAndrew Thompson 	f->flag_short = 0;
6747214348fSAndrew Thompson 
67502ac6454SAndrew Thompson 	/* call open method */
676ee3e3ff5SAndrew Thompson 	err = (f->methods->f_open) (f, fflags);
67702ac6454SAndrew Thompson 	if (err) {
67802ac6454SAndrew Thompson 		goto done;
67902ac6454SAndrew Thompson 	}
68002ac6454SAndrew Thompson 	mtx_lock(f->priv_mtx);
68102ac6454SAndrew Thompson 
68202ac6454SAndrew Thompson 	/* reset sleep flag */
68302ac6454SAndrew Thompson 	f->flag_sleeping = 0;
68402ac6454SAndrew Thompson 
68502ac6454SAndrew Thompson 	/* reset error flag */
68602ac6454SAndrew Thompson 	f->flag_iserror = 0;
68702ac6454SAndrew Thompson 
68802ac6454SAndrew Thompson 	/* reset complete flag */
68902ac6454SAndrew Thompson 	f->flag_iscomplete = 0;
69002ac6454SAndrew Thompson 
69102ac6454SAndrew Thompson 	/* reset select flag */
69202ac6454SAndrew Thompson 	f->flag_isselect = 0;
69302ac6454SAndrew Thompson 
69402ac6454SAndrew Thompson 	/* reset flushing flag */
69502ac6454SAndrew Thompson 	f->flag_flushing = 0;
69602ac6454SAndrew Thompson 
69702ac6454SAndrew Thompson 	/* reset ASYNC proc flag */
69802ac6454SAndrew Thompson 	f->async_p = NULL;
69902ac6454SAndrew Thompson 
70002ac6454SAndrew Thompson 	mtx_lock(&usb2_ref_lock);
7017214348fSAndrew Thompson 	/* flag the fifo as opened to prevent others */
7027214348fSAndrew Thompson 	f->curr_cpd = cpd;
70302ac6454SAndrew Thompson 	mtx_unlock(&usb2_ref_lock);
70402ac6454SAndrew Thompson 
70502ac6454SAndrew Thompson 	/* reset queue */
70602ac6454SAndrew Thompson 	usb2_fifo_reset(f);
70702ac6454SAndrew Thompson 
70802ac6454SAndrew Thompson 	mtx_unlock(f->priv_mtx);
70902ac6454SAndrew Thompson done:
71002ac6454SAndrew Thompson 	return (err);
71102ac6454SAndrew Thompson }
71202ac6454SAndrew Thompson 
71302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
71402ac6454SAndrew Thompson  *	usb2_fifo_reset
71502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
71602ac6454SAndrew Thompson void
71702ac6454SAndrew Thompson usb2_fifo_reset(struct usb2_fifo *f)
71802ac6454SAndrew Thompson {
71902ac6454SAndrew Thompson 	struct usb2_mbuf *m;
72002ac6454SAndrew Thompson 
72102ac6454SAndrew Thompson 	if (f == NULL) {
72202ac6454SAndrew Thompson 		return;
72302ac6454SAndrew Thompson 	}
72402ac6454SAndrew Thompson 	while (1) {
72502ac6454SAndrew Thompson 		USB_IF_DEQUEUE(&f->used_q, m);
72602ac6454SAndrew Thompson 		if (m) {
72702ac6454SAndrew Thompson 			USB_IF_ENQUEUE(&f->free_q, m);
72802ac6454SAndrew Thompson 		} else {
72902ac6454SAndrew Thompson 			break;
73002ac6454SAndrew Thompson 		}
73102ac6454SAndrew Thompson 	}
73202ac6454SAndrew Thompson }
73302ac6454SAndrew Thompson 
73402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
73502ac6454SAndrew Thompson  *	usb2_fifo_close
73602ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
73702ac6454SAndrew Thompson static void
738ee3e3ff5SAndrew Thompson usb2_fifo_close(struct usb2_fifo *f, int fflags)
73902ac6454SAndrew Thompson {
74002ac6454SAndrew Thompson 	int err;
74102ac6454SAndrew Thompson 
74202ac6454SAndrew Thompson 	/* check if we are not opened */
7437214348fSAndrew Thompson 	if (f->curr_cpd == NULL) {
74402ac6454SAndrew Thompson 		/* nothing to do - already closed */
74502ac6454SAndrew Thompson 		return;
74602ac6454SAndrew Thompson 	}
74702ac6454SAndrew Thompson 	mtx_lock(f->priv_mtx);
74802ac6454SAndrew Thompson 
7497214348fSAndrew Thompson 	/* clear current cdev private data pointer */
7507214348fSAndrew Thompson 	f->curr_cpd = NULL;
75102ac6454SAndrew Thompson 
75202ac6454SAndrew Thompson 	/* check if we are selected */
75302ac6454SAndrew Thompson 	if (f->flag_isselect) {
75402ac6454SAndrew Thompson 		selwakeup(&f->selinfo);
75502ac6454SAndrew Thompson 		f->flag_isselect = 0;
75602ac6454SAndrew Thompson 	}
75702ac6454SAndrew Thompson 	/* check if a thread wants SIGIO */
75802ac6454SAndrew Thompson 	if (f->async_p != NULL) {
75902ac6454SAndrew Thompson 		PROC_LOCK(f->async_p);
76002ac6454SAndrew Thompson 		psignal(f->async_p, SIGIO);
76102ac6454SAndrew Thompson 		PROC_UNLOCK(f->async_p);
76202ac6454SAndrew Thompson 		f->async_p = NULL;
76302ac6454SAndrew Thompson 	}
76402ac6454SAndrew Thompson 	/* remove FWRITE and FREAD flags */
76502ac6454SAndrew Thompson 	fflags &= ~(FWRITE | FREAD);
76602ac6454SAndrew Thompson 
76702ac6454SAndrew Thompson 	/* flush written data, if any */
76802ac6454SAndrew Thompson 	if ((f->fifo_index & 1) == USB_FIFO_TX) {
76902ac6454SAndrew Thompson 
77002ac6454SAndrew Thompson 		if (!f->flag_iserror) {
77102ac6454SAndrew Thompson 
77202ac6454SAndrew Thompson 			/* set flushing flag */
77302ac6454SAndrew Thompson 			f->flag_flushing = 1;
77402ac6454SAndrew Thompson 
77502ac6454SAndrew Thompson 			/* start write transfer, if not already started */
77602ac6454SAndrew Thompson 			(f->methods->f_start_write) (f);
77702ac6454SAndrew Thompson 
77802ac6454SAndrew Thompson 			/* check if flushed already */
77902ac6454SAndrew Thompson 			while (f->flag_flushing &&
78002ac6454SAndrew Thompson 			    (!f->flag_iserror)) {
78102ac6454SAndrew Thompson 				/* wait until all data has been written */
78202ac6454SAndrew Thompson 				f->flag_sleeping = 1;
78302ac6454SAndrew Thompson 				err = usb2_cv_wait_sig(&f->cv_io, f->priv_mtx);
78402ac6454SAndrew Thompson 				if (err) {
78502ac6454SAndrew Thompson 					DPRINTF("signal received\n");
78602ac6454SAndrew Thompson 					break;
78702ac6454SAndrew Thompson 				}
78802ac6454SAndrew Thompson 			}
78902ac6454SAndrew Thompson 		}
79002ac6454SAndrew Thompson 		fflags |= FWRITE;
79102ac6454SAndrew Thompson 
79202ac6454SAndrew Thompson 		/* stop write transfer, if not already stopped */
79302ac6454SAndrew Thompson 		(f->methods->f_stop_write) (f);
79402ac6454SAndrew Thompson 	} else {
79502ac6454SAndrew Thompson 		fflags |= FREAD;
79602ac6454SAndrew Thompson 
79702ac6454SAndrew Thompson 		/* stop write transfer, if not already stopped */
79802ac6454SAndrew Thompson 		(f->methods->f_stop_read) (f);
79902ac6454SAndrew Thompson 	}
80002ac6454SAndrew Thompson 
80102ac6454SAndrew Thompson 	/* check if we are sleeping */
80202ac6454SAndrew Thompson 	if (f->flag_sleeping) {
80302ac6454SAndrew Thompson 		DPRINTFN(2, "Sleeping at close!\n");
80402ac6454SAndrew Thompson 	}
80502ac6454SAndrew Thompson 	mtx_unlock(f->priv_mtx);
80602ac6454SAndrew Thompson 
80702ac6454SAndrew Thompson 	/* call close method */
808ee3e3ff5SAndrew Thompson 	(f->methods->f_close) (f, fflags);
80902ac6454SAndrew Thompson 
81002ac6454SAndrew Thompson 	DPRINTF("closed\n");
81102ac6454SAndrew Thompson }
81202ac6454SAndrew Thompson 
81302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
814ee3e3ff5SAndrew Thompson  *	usb2_open - cdev callback
81502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
81602ac6454SAndrew Thompson static int
817ee3e3ff5SAndrew Thompson usb2_open(struct cdev *dev, int fflags, int devtype, struct thread *td)
81802ac6454SAndrew Thompson {
819ee3e3ff5SAndrew Thompson 	struct usb2_fs_privdata* pd = (struct usb2_fs_privdata*)dev->si_drv1;
820ee3e3ff5SAndrew Thompson 	struct usb2_cdev_privdata *cpd;
821ee3e3ff5SAndrew Thompson 	int err, ep;
82202ac6454SAndrew Thompson 
82302ac6454SAndrew Thompson 	DPRINTFN(2, "fflags=0x%08x\n", fflags);
82402ac6454SAndrew Thompson 
825ee3e3ff5SAndrew Thompson 	KASSERT(fflags & (FREAD|FWRITE), ("invalid open flags"));
826ee3e3ff5SAndrew Thompson 	if (((fflags & FREAD) && !(pd->mode & FREAD)) ||
827ee3e3ff5SAndrew Thompson 	    ((fflags & FWRITE) && !(pd->mode & FWRITE))) {
828ee3e3ff5SAndrew Thompson 		DPRINTFN(2, "access mode not supported\n");
82902ac6454SAndrew Thompson 		return (EPERM);
83002ac6454SAndrew Thompson 	}
831ee3e3ff5SAndrew Thompson 
832ee3e3ff5SAndrew Thompson 	cpd = malloc(sizeof(*cpd), M_USBDEV, M_WAITOK | M_ZERO);
833ee3e3ff5SAndrew Thompson 	ep = cpd->ep_addr = pd->ep_addr;
834ee3e3ff5SAndrew Thompson 
835ee3e3ff5SAndrew Thompson 	usb2_loc_fill(pd, cpd);
836ee3e3ff5SAndrew Thompson 	err = usb2_ref_device(cpd, 1);
83702ac6454SAndrew Thompson 	if (err) {
83802ac6454SAndrew Thompson 		DPRINTFN(2, "cannot ref device\n");
839ee3e3ff5SAndrew Thompson 		free(cpd, M_USBDEV);
84002ac6454SAndrew Thompson 		return (ENXIO);
84102ac6454SAndrew Thompson 	}
842ee3e3ff5SAndrew Thompson 	cpd->fflags = fflags;	/* access mode for open lifetime */
84302ac6454SAndrew Thompson 
84402ac6454SAndrew Thompson 	/* create FIFOs, if any */
845ee3e3ff5SAndrew Thompson 	err = usb2_fifo_create(cpd);
84602ac6454SAndrew Thompson 	/* check for error */
84702ac6454SAndrew Thompson 	if (err) {
848ee3e3ff5SAndrew Thompson 		DPRINTFN(2, "cannot create fifo\n");
849ee3e3ff5SAndrew Thompson 		usb2_unref_device(cpd);
850ee3e3ff5SAndrew Thompson 		free(cpd, M_USBDEV);
85102ac6454SAndrew Thompson 		return (err);
85202ac6454SAndrew Thompson 	}
85302ac6454SAndrew Thompson 	if (fflags & FREAD) {
8547214348fSAndrew Thompson 		err = usb2_fifo_open(cpd, cpd->rxfifo, fflags);
85502ac6454SAndrew Thompson 		if (err) {
85602ac6454SAndrew Thompson 			DPRINTFN(2, "read open failed\n");
857ee3e3ff5SAndrew Thompson 			usb2_unref_device(cpd);
858ee3e3ff5SAndrew Thompson 			free(cpd, M_USBDEV);
85902ac6454SAndrew Thompson 			return (err);
86002ac6454SAndrew Thompson 		}
86102ac6454SAndrew Thompson 	}
86202ac6454SAndrew Thompson 	if (fflags & FWRITE) {
8637214348fSAndrew Thompson 		err = usb2_fifo_open(cpd, cpd->txfifo, fflags);
86402ac6454SAndrew Thompson 		if (err) {
86502ac6454SAndrew Thompson 			DPRINTFN(2, "write open failed\n");
86602ac6454SAndrew Thompson 			if (fflags & FREAD) {
867ee3e3ff5SAndrew Thompson 				usb2_fifo_close(cpd->rxfifo, fflags);
86802ac6454SAndrew Thompson 			}
869ee3e3ff5SAndrew Thompson 			usb2_unref_device(cpd);
870ee3e3ff5SAndrew Thompson 			free(cpd, M_USBDEV);
87102ac6454SAndrew Thompson 			return (err);
87202ac6454SAndrew Thompson 		}
87302ac6454SAndrew Thompson 	}
874ee3e3ff5SAndrew Thompson 	usb2_unref_device(cpd);
875ee3e3ff5SAndrew Thompson 	devfs_set_cdevpriv(cpd, usb2_close);
87602ac6454SAndrew Thompson 
877ee3e3ff5SAndrew Thompson 	return (0);
87802ac6454SAndrew Thompson }
87902ac6454SAndrew Thompson 
88002ac6454SAndrew Thompson /*------------------------------------------------------------------------*
88102ac6454SAndrew Thompson  *	usb2_close - cdev callback
88202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
88302ac6454SAndrew Thompson static void
884ee3e3ff5SAndrew Thompson usb2_close(void *arg)
88502ac6454SAndrew Thompson {
886ee3e3ff5SAndrew Thompson 	struct usb2_cdev_privdata *cpd = arg;
887ee3e3ff5SAndrew Thompson 	int err;
88802ac6454SAndrew Thompson 
8897214348fSAndrew Thompson 	DPRINTFN(2, "cpd=%p\n", cpd);
890ee3e3ff5SAndrew Thompson 
891ee3e3ff5SAndrew Thompson 	err = usb2_ref_device(cpd, 1);
892ee3e3ff5SAndrew Thompson 	if (err) {
893ee3e3ff5SAndrew Thompson 		free(cpd, M_USBDEV);
89402ac6454SAndrew Thompson 		return;
89502ac6454SAndrew Thompson 	}
896ee3e3ff5SAndrew Thompson 	if (cpd->fflags & FREAD) {
897ee3e3ff5SAndrew Thompson 		usb2_fifo_close(cpd->rxfifo, cpd->fflags);
89802ac6454SAndrew Thompson 	}
899ee3e3ff5SAndrew Thompson 	if (cpd->fflags & FWRITE) {
900ee3e3ff5SAndrew Thompson 		usb2_fifo_close(cpd->txfifo, cpd->fflags);
90102ac6454SAndrew Thompson 	}
902ee3e3ff5SAndrew Thompson 
903ee3e3ff5SAndrew Thompson 	usb2_unref_device(cpd);
904ee3e3ff5SAndrew Thompson 	free(cpd, M_USBDEV);
90502ac6454SAndrew Thompson 	return;
90602ac6454SAndrew Thompson }
90702ac6454SAndrew Thompson 
90802ac6454SAndrew Thompson static void
90902ac6454SAndrew Thompson usb2_dev_init(void *arg)
91002ac6454SAndrew Thompson {
91102ac6454SAndrew Thompson 	mtx_init(&usb2_ref_lock, "USB ref mutex", NULL, MTX_DEF);
91202ac6454SAndrew Thompson 	sx_init(&usb2_sym_lock, "USB sym mutex");
91302ac6454SAndrew Thompson 	TAILQ_INIT(&usb2_sym_head);
91402ac6454SAndrew Thompson 
91502ac6454SAndrew Thompson 	/* check the UGEN methods */
91602ac6454SAndrew Thompson 	usb2_fifo_check_methods(&usb2_ugen_methods);
91702ac6454SAndrew Thompson }
91802ac6454SAndrew Thompson 
91902ac6454SAndrew Thompson SYSINIT(usb2_dev_init, SI_SUB_KLD, SI_ORDER_FIRST, usb2_dev_init, NULL);
92002ac6454SAndrew Thompson 
92102ac6454SAndrew Thompson static void
92202ac6454SAndrew Thompson usb2_dev_init_post(void *arg)
92302ac6454SAndrew Thompson {
92402ac6454SAndrew Thompson 	/*
925ee3e3ff5SAndrew Thompson 	 * Create /dev/usb - this is needed for usbconfig(8), which
926ee3e3ff5SAndrew Thompson 	 * needs a well-known device name to access.
92702ac6454SAndrew Thompson 	 */
928ee3e3ff5SAndrew Thompson 	usb2_dev = make_dev(&usb2_static_devsw, 0, UID_ROOT, GID_OPERATOR,
929ee3e3ff5SAndrew Thompson 	    0644, USB_DEVICE_NAME);
93002ac6454SAndrew Thompson 	if (usb2_dev == NULL) {
93102ac6454SAndrew Thompson 		DPRINTFN(0, "Could not create usb bus device!\n");
93202ac6454SAndrew Thompson 	}
93302ac6454SAndrew Thompson }
93402ac6454SAndrew Thompson 
93502ac6454SAndrew Thompson SYSINIT(usb2_dev_init_post, SI_SUB_KICK_SCHEDULER, SI_ORDER_FIRST, usb2_dev_init_post, NULL);
93602ac6454SAndrew Thompson 
93702ac6454SAndrew Thompson static void
93802ac6454SAndrew Thompson usb2_dev_uninit(void *arg)
93902ac6454SAndrew Thompson {
940ee3e3ff5SAndrew Thompson 	if (usb2_dev != NULL) {
94102ac6454SAndrew Thompson 		destroy_dev(usb2_dev);
94202ac6454SAndrew Thompson 		usb2_dev = NULL;
943ee3e3ff5SAndrew Thompson 
94402ac6454SAndrew Thompson 	}
94502ac6454SAndrew Thompson 	mtx_destroy(&usb2_ref_lock);
94602ac6454SAndrew Thompson 	sx_destroy(&usb2_sym_lock);
94702ac6454SAndrew Thompson }
94802ac6454SAndrew Thompson 
94902ac6454SAndrew Thompson SYSUNINIT(usb2_dev_uninit, SI_SUB_KICK_SCHEDULER, SI_ORDER_ANY, usb2_dev_uninit, NULL);
95002ac6454SAndrew Thompson 
95102ac6454SAndrew Thompson static int
95202ac6454SAndrew Thompson usb2_ioctl_f_sub(struct usb2_fifo *f, u_long cmd, void *addr,
95302ac6454SAndrew Thompson     struct thread *td)
95402ac6454SAndrew Thompson {
95502ac6454SAndrew Thompson 	int error = 0;
95602ac6454SAndrew Thompson 
95702ac6454SAndrew Thompson 	switch (cmd) {
95802ac6454SAndrew Thompson 	case FIODTYPE:
95902ac6454SAndrew Thompson 		*(int *)addr = 0;	/* character device */
96002ac6454SAndrew Thompson 		break;
96102ac6454SAndrew Thompson 
96202ac6454SAndrew Thompson 	case FIONBIO:
96302ac6454SAndrew Thompson 		/* handled by upper FS layer */
96402ac6454SAndrew Thompson 		break;
96502ac6454SAndrew Thompson 
96602ac6454SAndrew Thompson 	case FIOASYNC:
96702ac6454SAndrew Thompson 		if (*(int *)addr) {
96802ac6454SAndrew Thompson 			if (f->async_p != NULL) {
96902ac6454SAndrew Thompson 				error = EBUSY;
97002ac6454SAndrew Thompson 				break;
97102ac6454SAndrew Thompson 			}
97202ac6454SAndrew Thompson 			f->async_p = USB_TD_GET_PROC(td);
97302ac6454SAndrew Thompson 		} else {
97402ac6454SAndrew Thompson 			f->async_p = NULL;
97502ac6454SAndrew Thompson 		}
97602ac6454SAndrew Thompson 		break;
97702ac6454SAndrew Thompson 
97802ac6454SAndrew Thompson 		/* XXX this is not the most general solution */
97902ac6454SAndrew Thompson 	case TIOCSPGRP:
98002ac6454SAndrew Thompson 		if (f->async_p == NULL) {
98102ac6454SAndrew Thompson 			error = EINVAL;
98202ac6454SAndrew Thompson 			break;
98302ac6454SAndrew Thompson 		}
98402ac6454SAndrew Thompson 		if (*(int *)addr != USB_PROC_GET_GID(f->async_p)) {
98502ac6454SAndrew Thompson 			error = EPERM;
98602ac6454SAndrew Thompson 			break;
98702ac6454SAndrew Thompson 		}
98802ac6454SAndrew Thompson 		break;
98902ac6454SAndrew Thompson 	default:
99002ac6454SAndrew Thompson 		return (ENOIOCTL);
99102ac6454SAndrew Thompson 	}
99202ac6454SAndrew Thompson 	return (error);
99302ac6454SAndrew Thompson }
99402ac6454SAndrew Thompson 
995ee3e3ff5SAndrew Thompson /*------------------------------------------------------------------------*
996ee3e3ff5SAndrew Thompson  *	usb2_ioctl - cdev callback
997ee3e3ff5SAndrew Thompson  *------------------------------------------------------------------------*/
99802ac6454SAndrew Thompson static int
999ee3e3ff5SAndrew Thompson usb2_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int fflag, struct thread* td)
100002ac6454SAndrew Thompson {
1001ee3e3ff5SAndrew Thompson 	struct usb2_cdev_privdata* cpd;
100202ac6454SAndrew Thompson 	struct usb2_fifo *f;
100302ac6454SAndrew Thompson 	int fflags;
100402ac6454SAndrew Thompson 	int err;
100502ac6454SAndrew Thompson 
1006ee3e3ff5SAndrew Thompson 	err = devfs_get_cdevpriv((void **)&cpd);
1007ee3e3ff5SAndrew Thompson 	if (err != 0)
1008ee3e3ff5SAndrew Thompson 		return (err);
1009ee3e3ff5SAndrew Thompson 
1010f5f145baSAndrew Thompson 	/*
1011f5f145baSAndrew Thompson 	 * Performance optimistaion: We try to check for IOCTL's that
1012f5f145baSAndrew Thompson 	 * don't need the USB reference first. Then we grab the USB
1013f5f145baSAndrew Thompson 	 * reference if we need it!
1014f5f145baSAndrew Thompson 	 */
1015f5f145baSAndrew Thompson 	err = usb2_ref_device(cpd, 0 /* no uref */ );
101602ac6454SAndrew Thompson 	if (err) {
101702ac6454SAndrew Thompson 		return (ENXIO);
101802ac6454SAndrew Thompson 	}
1019ee3e3ff5SAndrew Thompson 	fflags = cpd->fflags;
102002ac6454SAndrew Thompson 
102102ac6454SAndrew Thompson 	DPRINTFN(2, "fflags=%u, cmd=0x%lx\n", fflags, cmd);
102202ac6454SAndrew Thompson 
102302ac6454SAndrew Thompson 	f = NULL;			/* set default value */
102402ac6454SAndrew Thompson 	err = ENOIOCTL;			/* set default value */
102502ac6454SAndrew Thompson 
102602ac6454SAndrew Thompson 	if (fflags & FWRITE) {
1027ee3e3ff5SAndrew Thompson 		f = cpd->txfifo;
102802ac6454SAndrew Thompson 		err = usb2_ioctl_f_sub(f, cmd, addr, td);
102902ac6454SAndrew Thompson 	}
103002ac6454SAndrew Thompson 	if (fflags & FREAD) {
1031ee3e3ff5SAndrew Thompson 		f = cpd->rxfifo;
103202ac6454SAndrew Thompson 		err = usb2_ioctl_f_sub(f, cmd, addr, td);
103302ac6454SAndrew Thompson 	}
1034ee3e3ff5SAndrew Thompson 	KASSERT(f != NULL, ("fifo not found"));
103502ac6454SAndrew Thompson 	if (err == ENOIOCTL) {
1036ee3e3ff5SAndrew Thompson 		err = (f->methods->f_ioctl) (f, cmd, addr, fflags);
103702ac6454SAndrew Thompson 		if (err == ENOIOCTL) {
1038ee3e3ff5SAndrew Thompson 			if (usb2_uref_location(cpd)) {
103902ac6454SAndrew Thompson 				err = ENXIO;
104002ac6454SAndrew Thompson 				goto done;
104102ac6454SAndrew Thompson 			}
1042ee3e3ff5SAndrew Thompson 			err = (f->methods->f_ioctl_post) (f, cmd, addr, fflags);
104302ac6454SAndrew Thompson 		}
104402ac6454SAndrew Thompson 	}
104502ac6454SAndrew Thompson 	if (err == ENOIOCTL) {
104602ac6454SAndrew Thompson 		err = ENOTTY;
104702ac6454SAndrew Thompson 	}
104802ac6454SAndrew Thompson done:
1049ee3e3ff5SAndrew Thompson 	usb2_unref_device(cpd);
105002ac6454SAndrew Thompson 	return (err);
105102ac6454SAndrew Thompson }
105202ac6454SAndrew Thompson 
105302ac6454SAndrew Thompson /* ARGSUSED */
105402ac6454SAndrew Thompson static int
1055ee3e3ff5SAndrew Thompson usb2_poll(struct cdev* dev, int events, struct thread* td)
105602ac6454SAndrew Thompson {
1057ee3e3ff5SAndrew Thompson 	struct usb2_cdev_privdata* cpd;
105802ac6454SAndrew Thompson 	struct usb2_fifo *f;
105902ac6454SAndrew Thompson 	struct usb2_mbuf *m;
10608a93629eSAndrew Thompson 	int fflags, revents;
106102ac6454SAndrew Thompson 
10628a93629eSAndrew Thompson 	if (devfs_get_cdevpriv((void **)&cpd) != 0 ||
10638a93629eSAndrew Thompson 	    usb2_ref_device(cpd, 0) != 0)
10648a93629eSAndrew Thompson 		return (events &
10658a93629eSAndrew Thompson 		    (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM));
1066ee3e3ff5SAndrew Thompson 
1067ee3e3ff5SAndrew Thompson 	fflags = cpd->fflags;
106802ac6454SAndrew Thompson 
106902ac6454SAndrew Thompson 	/* Figure out who needs service */
1070ee3e3ff5SAndrew Thompson 	revents = 0;
107102ac6454SAndrew Thompson 	if ((events & (POLLOUT | POLLWRNORM)) &&
107202ac6454SAndrew Thompson 	    (fflags & FWRITE)) {
107302ac6454SAndrew Thompson 
1074ee3e3ff5SAndrew Thompson 		f = cpd->txfifo;
107502ac6454SAndrew Thompson 
107602ac6454SAndrew Thompson 		mtx_lock(f->priv_mtx);
107702ac6454SAndrew Thompson 
1078ee3e3ff5SAndrew Thompson 		if (!cpd->is_usbfs) {
107902ac6454SAndrew Thompson 			if (f->flag_iserror) {
108002ac6454SAndrew Thompson 				/* we got an error */
108102ac6454SAndrew Thompson 				m = (void *)1;
108202ac6454SAndrew Thompson 			} else {
108302ac6454SAndrew Thompson 				if (f->queue_data == NULL) {
108402ac6454SAndrew Thompson 					/*
108502ac6454SAndrew Thompson 					 * start write transfer, if not
108602ac6454SAndrew Thompson 					 * already started
108702ac6454SAndrew Thompson 					 */
108802ac6454SAndrew Thompson 					(f->methods->f_start_write) (f);
108902ac6454SAndrew Thompson 				}
109002ac6454SAndrew Thompson 				/* check if any packets are available */
109102ac6454SAndrew Thompson 				USB_IF_POLL(&f->free_q, m);
109202ac6454SAndrew Thompson 			}
109302ac6454SAndrew Thompson 		} else {
109402ac6454SAndrew Thompson 			if (f->flag_iscomplete) {
109502ac6454SAndrew Thompson 				m = (void *)1;
109602ac6454SAndrew Thompson 			} else {
109702ac6454SAndrew Thompson 				m = NULL;
109802ac6454SAndrew Thompson 			}
109902ac6454SAndrew Thompson 		}
110002ac6454SAndrew Thompson 
110102ac6454SAndrew Thompson 		if (m) {
110202ac6454SAndrew Thompson 			revents |= events & (POLLOUT | POLLWRNORM);
110302ac6454SAndrew Thompson 		} else {
110402ac6454SAndrew Thompson 			f->flag_isselect = 1;
110502ac6454SAndrew Thompson 			selrecord(td, &f->selinfo);
110602ac6454SAndrew Thompson 		}
110702ac6454SAndrew Thompson 
110802ac6454SAndrew Thompson 		mtx_unlock(f->priv_mtx);
110902ac6454SAndrew Thompson 	}
111002ac6454SAndrew Thompson 	if ((events & (POLLIN | POLLRDNORM)) &&
111102ac6454SAndrew Thompson 	    (fflags & FREAD)) {
111202ac6454SAndrew Thompson 
1113ee3e3ff5SAndrew Thompson 		f = cpd->rxfifo;
111402ac6454SAndrew Thompson 
111502ac6454SAndrew Thompson 		mtx_lock(f->priv_mtx);
111602ac6454SAndrew Thompson 
1117ee3e3ff5SAndrew Thompson 		if (!cpd->is_usbfs) {
111802ac6454SAndrew Thompson 			if (f->flag_iserror) {
111902ac6454SAndrew Thompson 				/* we have and error */
112002ac6454SAndrew Thompson 				m = (void *)1;
112102ac6454SAndrew Thompson 			} else {
112202ac6454SAndrew Thompson 				if (f->queue_data == NULL) {
112302ac6454SAndrew Thompson 					/*
112402ac6454SAndrew Thompson 					 * start read transfer, if not
112502ac6454SAndrew Thompson 					 * already started
112602ac6454SAndrew Thompson 					 */
112702ac6454SAndrew Thompson 					(f->methods->f_start_read) (f);
112802ac6454SAndrew Thompson 				}
112902ac6454SAndrew Thompson 				/* check if any packets are available */
113002ac6454SAndrew Thompson 				USB_IF_POLL(&f->used_q, m);
113102ac6454SAndrew Thompson 			}
113202ac6454SAndrew Thompson 		} else {
113302ac6454SAndrew Thompson 			if (f->flag_iscomplete) {
113402ac6454SAndrew Thompson 				m = (void *)1;
113502ac6454SAndrew Thompson 			} else {
113602ac6454SAndrew Thompson 				m = NULL;
113702ac6454SAndrew Thompson 			}
113802ac6454SAndrew Thompson 		}
113902ac6454SAndrew Thompson 
114002ac6454SAndrew Thompson 		if (m) {
114102ac6454SAndrew Thompson 			revents |= events & (POLLIN | POLLRDNORM);
114202ac6454SAndrew Thompson 		} else {
114302ac6454SAndrew Thompson 			f->flag_isselect = 1;
114402ac6454SAndrew Thompson 			selrecord(td, &f->selinfo);
114502ac6454SAndrew Thompson 
1146ee3e3ff5SAndrew Thompson 			if (!cpd->is_usbfs) {
114702ac6454SAndrew Thompson 				/* start reading data */
114802ac6454SAndrew Thompson 				(f->methods->f_start_read) (f);
114902ac6454SAndrew Thompson 			}
115002ac6454SAndrew Thompson 		}
115102ac6454SAndrew Thompson 
115202ac6454SAndrew Thompson 		mtx_unlock(f->priv_mtx);
115302ac6454SAndrew Thompson 	}
1154ee3e3ff5SAndrew Thompson 	usb2_unref_device(cpd);
115502ac6454SAndrew Thompson 	return (revents);
115602ac6454SAndrew Thompson }
115702ac6454SAndrew Thompson 
115802ac6454SAndrew Thompson static int
1159ee3e3ff5SAndrew Thompson usb2_read(struct cdev *dev, struct uio *uio, int ioflag)
116002ac6454SAndrew Thompson {
1161ee3e3ff5SAndrew Thompson 	struct usb2_cdev_privdata* cpd;
116202ac6454SAndrew Thompson 	struct usb2_fifo *f;
116302ac6454SAndrew Thompson 	struct usb2_mbuf *m;
116402ac6454SAndrew Thompson 	int fflags;
116502ac6454SAndrew Thompson 	int resid;
116602ac6454SAndrew Thompson 	int io_len;
116702ac6454SAndrew Thompson 	int err;
116802ac6454SAndrew Thompson 	uint8_t tr_data = 0;
116902ac6454SAndrew Thompson 
1170ee3e3ff5SAndrew Thompson 	err = devfs_get_cdevpriv((void **)&cpd);
1171ee3e3ff5SAndrew Thompson 	if (err != 0)
1172ee3e3ff5SAndrew Thompson 		return (err);
117302ac6454SAndrew Thompson 
1174ee3e3ff5SAndrew Thompson 	err = usb2_ref_device(cpd, 0 /* no uref */ );
117502ac6454SAndrew Thompson 	if (err) {
117602ac6454SAndrew Thompson 		return (ENXIO);
117702ac6454SAndrew Thompson 	}
1178ee3e3ff5SAndrew Thompson 	fflags = cpd->fflags;
1179ee3e3ff5SAndrew Thompson 
1180ee3e3ff5SAndrew Thompson 	f = cpd->rxfifo;
118102ac6454SAndrew Thompson 	if (f == NULL) {
118202ac6454SAndrew Thompson 		/* should not happen */
118302ac6454SAndrew Thompson 		return (EPERM);
118402ac6454SAndrew Thompson 	}
118502ac6454SAndrew Thompson 
1186ee3e3ff5SAndrew Thompson 	resid = uio->uio_resid;
118702ac6454SAndrew Thompson 
118802ac6454SAndrew Thompson 	mtx_lock(f->priv_mtx);
118902ac6454SAndrew Thompson 
119002ac6454SAndrew Thompson 	/* check for permanent read error */
119102ac6454SAndrew Thompson 	if (f->flag_iserror) {
119202ac6454SAndrew Thompson 		err = EIO;
119302ac6454SAndrew Thompson 		goto done;
119402ac6454SAndrew Thompson 	}
119502ac6454SAndrew Thompson 	/* check if USB-FS interface is active */
1196ee3e3ff5SAndrew Thompson 	if (cpd->is_usbfs) {
119702ac6454SAndrew Thompson 		/*
119802ac6454SAndrew Thompson 		 * The queue is used for events that should be
119902ac6454SAndrew Thompson 		 * retrieved using the "USB_FS_COMPLETE" ioctl.
120002ac6454SAndrew Thompson 		 */
120102ac6454SAndrew Thompson 		err = EINVAL;
120202ac6454SAndrew Thompson 		goto done;
120302ac6454SAndrew Thompson 	}
120402ac6454SAndrew Thompson 	while (uio->uio_resid > 0) {
120502ac6454SAndrew Thompson 
120602ac6454SAndrew Thompson 		USB_IF_DEQUEUE(&f->used_q, m);
120702ac6454SAndrew Thompson 
120802ac6454SAndrew Thompson 		if (m == NULL) {
120902ac6454SAndrew Thompson 
121002ac6454SAndrew Thompson 			/* start read transfer, if not already started */
121102ac6454SAndrew Thompson 
121202ac6454SAndrew Thompson 			(f->methods->f_start_read) (f);
121302ac6454SAndrew Thompson 
1214ee3e3ff5SAndrew Thompson 			if (fflags & IO_NDELAY) {
121502ac6454SAndrew Thompson 				if (tr_data) {
121602ac6454SAndrew Thompson 					/* return length before error */
121702ac6454SAndrew Thompson 					break;
121802ac6454SAndrew Thompson 				}
121902ac6454SAndrew Thompson 				err = EWOULDBLOCK;
122002ac6454SAndrew Thompson 				break;
122102ac6454SAndrew Thompson 			}
122202ac6454SAndrew Thompson 			DPRINTF("sleeping\n");
122302ac6454SAndrew Thompson 
122402ac6454SAndrew Thompson 			err = usb2_fifo_wait(f);
122502ac6454SAndrew Thompson 			if (err) {
122602ac6454SAndrew Thompson 				break;
122702ac6454SAndrew Thompson 			}
122802ac6454SAndrew Thompson 			continue;
122902ac6454SAndrew Thompson 		}
123002ac6454SAndrew Thompson 		if (f->methods->f_filter_read) {
123102ac6454SAndrew Thompson 			/*
123202ac6454SAndrew Thompson 			 * Sometimes it is convenient to process data at the
123302ac6454SAndrew Thompson 			 * expense of a userland process instead of a kernel
123402ac6454SAndrew Thompson 			 * process.
123502ac6454SAndrew Thompson 			 */
123602ac6454SAndrew Thompson 			(f->methods->f_filter_read) (f, m);
123702ac6454SAndrew Thompson 		}
123802ac6454SAndrew Thompson 		tr_data = 1;
123902ac6454SAndrew Thompson 
124002ac6454SAndrew Thompson 		io_len = MIN(m->cur_data_len, uio->uio_resid);
124102ac6454SAndrew Thompson 
124202ac6454SAndrew Thompson 		DPRINTFN(2, "transfer %d bytes from %p\n",
124302ac6454SAndrew Thompson 		    io_len, m->cur_data_ptr);
124402ac6454SAndrew Thompson 
124502ac6454SAndrew Thompson 		err = usb2_fifo_uiomove(f,
124602ac6454SAndrew Thompson 		    m->cur_data_ptr, io_len, uio);
124702ac6454SAndrew Thompson 
124802ac6454SAndrew Thompson 		m->cur_data_len -= io_len;
124902ac6454SAndrew Thompson 		m->cur_data_ptr += io_len;
125002ac6454SAndrew Thompson 
125102ac6454SAndrew Thompson 		if (m->cur_data_len == 0) {
125202ac6454SAndrew Thompson 
125302ac6454SAndrew Thompson 			uint8_t last_packet;
125402ac6454SAndrew Thompson 
125502ac6454SAndrew Thompson 			last_packet = m->last_packet;
125602ac6454SAndrew Thompson 
125702ac6454SAndrew Thompson 			USB_IF_ENQUEUE(&f->free_q, m);
125802ac6454SAndrew Thompson 
125902ac6454SAndrew Thompson 			if (last_packet) {
126002ac6454SAndrew Thompson 				/* keep framing */
126102ac6454SAndrew Thompson 				break;
126202ac6454SAndrew Thompson 			}
126302ac6454SAndrew Thompson 		} else {
126402ac6454SAndrew Thompson 			USB_IF_PREPEND(&f->used_q, m);
126502ac6454SAndrew Thompson 		}
126602ac6454SAndrew Thompson 
126702ac6454SAndrew Thompson 		if (err) {
126802ac6454SAndrew Thompson 			break;
126902ac6454SAndrew Thompson 		}
127002ac6454SAndrew Thompson 	}
127102ac6454SAndrew Thompson done:
127202ac6454SAndrew Thompson 	mtx_unlock(f->priv_mtx);
127302ac6454SAndrew Thompson 
1274ee3e3ff5SAndrew Thompson 	usb2_unref_device(cpd);
127502ac6454SAndrew Thompson 
127602ac6454SAndrew Thompson 	return (err);
127702ac6454SAndrew Thompson }
127802ac6454SAndrew Thompson 
127902ac6454SAndrew Thompson static int
1280ee3e3ff5SAndrew Thompson usb2_write(struct cdev *dev, struct uio *uio, int ioflag)
128102ac6454SAndrew Thompson {
1282ee3e3ff5SAndrew Thompson 	struct usb2_cdev_privdata* cpd;
128302ac6454SAndrew Thompson 	struct usb2_fifo *f;
128402ac6454SAndrew Thompson 	struct usb2_mbuf *m;
128502ac6454SAndrew Thompson 	int fflags;
128602ac6454SAndrew Thompson 	int resid;
128702ac6454SAndrew Thompson 	int io_len;
128802ac6454SAndrew Thompson 	int err;
128902ac6454SAndrew Thompson 	uint8_t tr_data = 0;
129002ac6454SAndrew Thompson 
129102ac6454SAndrew Thompson 	DPRINTFN(2, "\n");
129202ac6454SAndrew Thompson 
1293ee3e3ff5SAndrew Thompson 	err = devfs_get_cdevpriv((void **)&cpd);
1294ee3e3ff5SAndrew Thompson 	if (err != 0)
1295ee3e3ff5SAndrew Thompson 		return (err);
129602ac6454SAndrew Thompson 
1297ee3e3ff5SAndrew Thompson 	err = usb2_ref_device(cpd, 0 /* no uref */ );
129802ac6454SAndrew Thompson 	if (err) {
129902ac6454SAndrew Thompson 		return (ENXIO);
130002ac6454SAndrew Thompson 	}
1301ee3e3ff5SAndrew Thompson 	fflags = cpd->fflags;
1302ee3e3ff5SAndrew Thompson 
1303ee3e3ff5SAndrew Thompson 	f = cpd->txfifo;
130402ac6454SAndrew Thompson 	if (f == NULL) {
130502ac6454SAndrew Thompson 		/* should not happen */
1306ee3e3ff5SAndrew Thompson 		usb2_unref_device(cpd);
130702ac6454SAndrew Thompson 		return (EPERM);
130802ac6454SAndrew Thompson 	}
130902ac6454SAndrew Thompson 	resid = uio->uio_resid;
131002ac6454SAndrew Thompson 
131102ac6454SAndrew Thompson 	mtx_lock(f->priv_mtx);
131202ac6454SAndrew Thompson 
131302ac6454SAndrew Thompson 	/* check for permanent write error */
131402ac6454SAndrew Thompson 	if (f->flag_iserror) {
131502ac6454SAndrew Thompson 		err = EIO;
131602ac6454SAndrew Thompson 		goto done;
131702ac6454SAndrew Thompson 	}
131802ac6454SAndrew Thompson 	/* check if USB-FS interface is active */
1319ee3e3ff5SAndrew Thompson 	if (cpd->is_usbfs) {
132002ac6454SAndrew Thompson 		/*
132102ac6454SAndrew Thompson 		 * The queue is used for events that should be
132202ac6454SAndrew Thompson 		 * retrieved using the "USB_FS_COMPLETE" ioctl.
132302ac6454SAndrew Thompson 		 */
132402ac6454SAndrew Thompson 		err = EINVAL;
132502ac6454SAndrew Thompson 		goto done;
132602ac6454SAndrew Thompson 	}
132702ac6454SAndrew Thompson 	if (f->queue_data == NULL) {
132802ac6454SAndrew Thompson 		/* start write transfer, if not already started */
132902ac6454SAndrew Thompson 		(f->methods->f_start_write) (f);
133002ac6454SAndrew Thompson 	}
133102ac6454SAndrew Thompson 	/* we allow writing zero length data */
133202ac6454SAndrew Thompson 	do {
133302ac6454SAndrew Thompson 		USB_IF_DEQUEUE(&f->free_q, m);
133402ac6454SAndrew Thompson 
133502ac6454SAndrew Thompson 		if (m == NULL) {
133602ac6454SAndrew Thompson 
1337ee3e3ff5SAndrew Thompson 			if (fflags & IO_NDELAY) {
133802ac6454SAndrew Thompson 				if (tr_data) {
133902ac6454SAndrew Thompson 					/* return length before error */
134002ac6454SAndrew Thompson 					break;
134102ac6454SAndrew Thompson 				}
134202ac6454SAndrew Thompson 				err = EWOULDBLOCK;
134302ac6454SAndrew Thompson 				break;
134402ac6454SAndrew Thompson 			}
134502ac6454SAndrew Thompson 			DPRINTF("sleeping\n");
134602ac6454SAndrew Thompson 
134702ac6454SAndrew Thompson 			err = usb2_fifo_wait(f);
134802ac6454SAndrew Thompson 			if (err) {
134902ac6454SAndrew Thompson 				break;
135002ac6454SAndrew Thompson 			}
135102ac6454SAndrew Thompson 			continue;
135202ac6454SAndrew Thompson 		}
135302ac6454SAndrew Thompson 		tr_data = 1;
135402ac6454SAndrew Thompson 
135502ac6454SAndrew Thompson 		USB_MBUF_RESET(m);
135602ac6454SAndrew Thompson 
135702ac6454SAndrew Thompson 		io_len = MIN(m->cur_data_len, uio->uio_resid);
135802ac6454SAndrew Thompson 
135902ac6454SAndrew Thompson 		m->cur_data_len = io_len;
136002ac6454SAndrew Thompson 
136102ac6454SAndrew Thompson 		DPRINTFN(2, "transfer %d bytes to %p\n",
136202ac6454SAndrew Thompson 		    io_len, m->cur_data_ptr);
136302ac6454SAndrew Thompson 
136402ac6454SAndrew Thompson 		err = usb2_fifo_uiomove(f,
136502ac6454SAndrew Thompson 		    m->cur_data_ptr, io_len, uio);
136602ac6454SAndrew Thompson 
136702ac6454SAndrew Thompson 		if (err) {
136802ac6454SAndrew Thompson 			USB_IF_ENQUEUE(&f->free_q, m);
136902ac6454SAndrew Thompson 			break;
137002ac6454SAndrew Thompson 		}
137102ac6454SAndrew Thompson 		if (f->methods->f_filter_write) {
137202ac6454SAndrew Thompson 			/*
137302ac6454SAndrew Thompson 			 * Sometimes it is convenient to process data at the
137402ac6454SAndrew Thompson 			 * expense of a userland process instead of a kernel
137502ac6454SAndrew Thompson 			 * process.
137602ac6454SAndrew Thompson 			 */
137702ac6454SAndrew Thompson 			(f->methods->f_filter_write) (f, m);
137802ac6454SAndrew Thompson 		}
137902ac6454SAndrew Thompson 		USB_IF_ENQUEUE(&f->used_q, m);
138002ac6454SAndrew Thompson 
138102ac6454SAndrew Thompson 		(f->methods->f_start_write) (f);
138202ac6454SAndrew Thompson 
138302ac6454SAndrew Thompson 	} while (uio->uio_resid > 0);
138402ac6454SAndrew Thompson done:
138502ac6454SAndrew Thompson 	mtx_unlock(f->priv_mtx);
138602ac6454SAndrew Thompson 
1387ee3e3ff5SAndrew Thompson 	usb2_unref_device(cpd);
138802ac6454SAndrew Thompson 
1389ee3e3ff5SAndrew Thompson 	return (err);
1390ee3e3ff5SAndrew Thompson }
139102ac6454SAndrew Thompson 
1392ee3e3ff5SAndrew Thompson int
1393ee3e3ff5SAndrew Thompson usb2_static_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
1394ee3e3ff5SAndrew Thompson     struct thread *td)
1395ee3e3ff5SAndrew Thompson {
1396ee3e3ff5SAndrew Thompson 	union {
1397ee3e3ff5SAndrew Thompson 		struct usb2_read_dir *urd;
1398ee3e3ff5SAndrew Thompson 		void* data;
1399ee3e3ff5SAndrew Thompson 	} u;
1400ee3e3ff5SAndrew Thompson 	int err = ENOTTY;
1401ee3e3ff5SAndrew Thompson 
1402ee3e3ff5SAndrew Thompson 	u.data = data;
1403ee3e3ff5SAndrew Thompson 	switch (cmd) {
1404ee3e3ff5SAndrew Thompson 		case USB_READ_DIR:
1405ee3e3ff5SAndrew Thompson 			err = usb2_read_symlink(u.urd->urd_data,
1406ee3e3ff5SAndrew Thompson 			    u.urd->urd_startentry, u.urd->urd_maxlen);
1407ee3e3ff5SAndrew Thompson 			break;
1408ee3e3ff5SAndrew Thompson 		case USB_DEV_QUIRK_GET:
1409ee3e3ff5SAndrew Thompson 		case USB_QUIRK_NAME_GET:
1410ee3e3ff5SAndrew Thompson 		case USB_DEV_QUIRK_ADD:
1411ee3e3ff5SAndrew Thompson 		case USB_DEV_QUIRK_REMOVE:
1412ee3e3ff5SAndrew Thompson 			err = usb2_quirk_ioctl_p(cmd, data, fflag, td);
1413ee3e3ff5SAndrew Thompson 			break;
1414ee3e3ff5SAndrew Thompson 		case USB_GET_TEMPLATE:
1415ee3e3ff5SAndrew Thompson 			*(int *)data = usb2_template;
1416ee3e3ff5SAndrew Thompson 			break;
1417ee3e3ff5SAndrew Thompson 		case USB_SET_TEMPLATE:
141850230f98SAndrew Thompson 			err = priv_check(curthread, PRIV_DRIVER);
1419ee3e3ff5SAndrew Thompson 			if (err)
1420ee3e3ff5SAndrew Thompson 				break;
1421ee3e3ff5SAndrew Thompson 			usb2_template = *(int *)data;
1422ee3e3ff5SAndrew Thompson 			break;
1423ee3e3ff5SAndrew Thompson 	}
142402ac6454SAndrew Thompson 	return (err);
142502ac6454SAndrew Thompson }
142602ac6454SAndrew Thompson 
142702ac6454SAndrew Thompson static int
142802ac6454SAndrew Thompson usb2_fifo_uiomove(struct usb2_fifo *f, void *cp,
142902ac6454SAndrew Thompson     int n, struct uio *uio)
143002ac6454SAndrew Thompson {
143102ac6454SAndrew Thompson 	int error;
143202ac6454SAndrew Thompson 
143302ac6454SAndrew Thompson 	mtx_unlock(f->priv_mtx);
143402ac6454SAndrew Thompson 
143502ac6454SAndrew Thompson 	/*
143602ac6454SAndrew Thompson 	 * "uiomove()" can sleep so one needs to make a wrapper,
143702ac6454SAndrew Thompson 	 * exiting the mutex and checking things:
143802ac6454SAndrew Thompson 	 */
143902ac6454SAndrew Thompson 	error = uiomove(cp, n, uio);
144002ac6454SAndrew Thompson 
144102ac6454SAndrew Thompson 	mtx_lock(f->priv_mtx);
144202ac6454SAndrew Thompson 
144302ac6454SAndrew Thompson 	return (error);
144402ac6454SAndrew Thompson }
144502ac6454SAndrew Thompson 
144602ac6454SAndrew Thompson int
144702ac6454SAndrew Thompson usb2_fifo_wait(struct usb2_fifo *f)
144802ac6454SAndrew Thompson {
144902ac6454SAndrew Thompson 	int err;
145002ac6454SAndrew Thompson 
145102ac6454SAndrew Thompson 	mtx_assert(f->priv_mtx, MA_OWNED);
145202ac6454SAndrew Thompson 
145302ac6454SAndrew Thompson 	if (f->flag_iserror) {
145402ac6454SAndrew Thompson 		/* we are gone */
145502ac6454SAndrew Thompson 		return (EIO);
145602ac6454SAndrew Thompson 	}
145702ac6454SAndrew Thompson 	f->flag_sleeping = 1;
145802ac6454SAndrew Thompson 
145902ac6454SAndrew Thompson 	err = usb2_cv_wait_sig(&f->cv_io, f->priv_mtx);
146002ac6454SAndrew Thompson 
146102ac6454SAndrew Thompson 	if (f->flag_iserror) {
146202ac6454SAndrew Thompson 		/* we are gone */
146302ac6454SAndrew Thompson 		err = EIO;
146402ac6454SAndrew Thompson 	}
146502ac6454SAndrew Thompson 	return (err);
146602ac6454SAndrew Thompson }
146702ac6454SAndrew Thompson 
146802ac6454SAndrew Thompson void
146902ac6454SAndrew Thompson usb2_fifo_signal(struct usb2_fifo *f)
147002ac6454SAndrew Thompson {
147102ac6454SAndrew Thompson 	if (f->flag_sleeping) {
147202ac6454SAndrew Thompson 		f->flag_sleeping = 0;
147302ac6454SAndrew Thompson 		usb2_cv_broadcast(&f->cv_io);
147402ac6454SAndrew Thompson 	}
147502ac6454SAndrew Thompson }
147602ac6454SAndrew Thompson 
147702ac6454SAndrew Thompson void
147802ac6454SAndrew Thompson usb2_fifo_wakeup(struct usb2_fifo *f)
147902ac6454SAndrew Thompson {
148002ac6454SAndrew Thompson 	usb2_fifo_signal(f);
148102ac6454SAndrew Thompson 
148202ac6454SAndrew Thompson 	if (f->flag_isselect) {
148302ac6454SAndrew Thompson 		selwakeup(&f->selinfo);
148402ac6454SAndrew Thompson 		f->flag_isselect = 0;
148502ac6454SAndrew Thompson 	}
148602ac6454SAndrew Thompson 	if (f->async_p != NULL) {
148702ac6454SAndrew Thompson 		PROC_LOCK(f->async_p);
148802ac6454SAndrew Thompson 		psignal(f->async_p, SIGIO);
148902ac6454SAndrew Thompson 		PROC_UNLOCK(f->async_p);
149002ac6454SAndrew Thompson 	}
149102ac6454SAndrew Thompson }
149202ac6454SAndrew Thompson 
149302ac6454SAndrew Thompson static int
1494ee3e3ff5SAndrew Thompson usb2_fifo_dummy_open(struct usb2_fifo *fifo, int fflags)
149502ac6454SAndrew Thompson {
149602ac6454SAndrew Thompson 	return (0);
149702ac6454SAndrew Thompson }
149802ac6454SAndrew Thompson 
149902ac6454SAndrew Thompson static void
1500ee3e3ff5SAndrew Thompson usb2_fifo_dummy_close(struct usb2_fifo *fifo, int fflags)
150102ac6454SAndrew Thompson {
150202ac6454SAndrew Thompson 	return;
150302ac6454SAndrew Thompson }
150402ac6454SAndrew Thompson 
150502ac6454SAndrew Thompson static int
1506ee3e3ff5SAndrew Thompson usb2_fifo_dummy_ioctl(struct usb2_fifo *fifo, u_long cmd, void *addr, int fflags)
150702ac6454SAndrew Thompson {
150802ac6454SAndrew Thompson 	return (ENOIOCTL);
150902ac6454SAndrew Thompson }
151002ac6454SAndrew Thompson 
151102ac6454SAndrew Thompson static void
151202ac6454SAndrew Thompson usb2_fifo_dummy_cmd(struct usb2_fifo *fifo)
151302ac6454SAndrew Thompson {
151402ac6454SAndrew Thompson 	fifo->flag_flushing = 0;	/* not flushing */
151502ac6454SAndrew Thompson }
151602ac6454SAndrew Thompson 
151702ac6454SAndrew Thompson static void
151802ac6454SAndrew Thompson usb2_fifo_check_methods(struct usb2_fifo_methods *pm)
151902ac6454SAndrew Thompson {
152002ac6454SAndrew Thompson 	/* check that all callback functions are OK */
152102ac6454SAndrew Thompson 
152202ac6454SAndrew Thompson 	if (pm->f_open == NULL)
152302ac6454SAndrew Thompson 		pm->f_open = &usb2_fifo_dummy_open;
152402ac6454SAndrew Thompson 
152502ac6454SAndrew Thompson 	if (pm->f_close == NULL)
152602ac6454SAndrew Thompson 		pm->f_close = &usb2_fifo_dummy_close;
152702ac6454SAndrew Thompson 
152802ac6454SAndrew Thompson 	if (pm->f_ioctl == NULL)
152902ac6454SAndrew Thompson 		pm->f_ioctl = &usb2_fifo_dummy_ioctl;
153002ac6454SAndrew Thompson 
153102ac6454SAndrew Thompson 	if (pm->f_ioctl_post == NULL)
153202ac6454SAndrew Thompson 		pm->f_ioctl_post = &usb2_fifo_dummy_ioctl;
153302ac6454SAndrew Thompson 
153402ac6454SAndrew Thompson 	if (pm->f_start_read == NULL)
153502ac6454SAndrew Thompson 		pm->f_start_read = &usb2_fifo_dummy_cmd;
153602ac6454SAndrew Thompson 
153702ac6454SAndrew Thompson 	if (pm->f_stop_read == NULL)
153802ac6454SAndrew Thompson 		pm->f_stop_read = &usb2_fifo_dummy_cmd;
153902ac6454SAndrew Thompson 
154002ac6454SAndrew Thompson 	if (pm->f_start_write == NULL)
154102ac6454SAndrew Thompson 		pm->f_start_write = &usb2_fifo_dummy_cmd;
154202ac6454SAndrew Thompson 
154302ac6454SAndrew Thompson 	if (pm->f_stop_write == NULL)
154402ac6454SAndrew Thompson 		pm->f_stop_write = &usb2_fifo_dummy_cmd;
154502ac6454SAndrew Thompson }
154602ac6454SAndrew Thompson 
154702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
154802ac6454SAndrew Thompson  *	usb2_fifo_attach
154902ac6454SAndrew Thompson  *
155002ac6454SAndrew Thompson  * The following function will create a duplex FIFO.
155102ac6454SAndrew Thompson  *
155202ac6454SAndrew Thompson  * Return values:
155302ac6454SAndrew Thompson  * 0: Success.
155402ac6454SAndrew Thompson  * Else: Failure.
155502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
155602ac6454SAndrew Thompson int
155702ac6454SAndrew Thompson usb2_fifo_attach(struct usb2_device *udev, void *priv_sc,
155802ac6454SAndrew Thompson     struct mtx *priv_mtx, struct usb2_fifo_methods *pm,
155902ac6454SAndrew Thompson     struct usb2_fifo_sc *f_sc, uint16_t unit, uint16_t subunit,
1560ee3e3ff5SAndrew Thompson     uint8_t iface_index, uid_t uid, gid_t gid, int mode)
156102ac6454SAndrew Thompson {
156202ac6454SAndrew Thompson 	struct usb2_fifo *f_tx;
156302ac6454SAndrew Thompson 	struct usb2_fifo *f_rx;
1564ee3e3ff5SAndrew Thompson 	char devname[32];
156502ac6454SAndrew Thompson 	uint8_t n;
1566ee3e3ff5SAndrew Thompson 	struct usb2_fs_privdata* pd;
156702ac6454SAndrew Thompson 
156802ac6454SAndrew Thompson 	f_sc->fp[USB_FIFO_TX] = NULL;
156902ac6454SAndrew Thompson 	f_sc->fp[USB_FIFO_RX] = NULL;
157002ac6454SAndrew Thompson 
157102ac6454SAndrew Thompson 	if (pm == NULL)
157202ac6454SAndrew Thompson 		return (EINVAL);
157302ac6454SAndrew Thompson 
157402ac6454SAndrew Thompson 	/* check the methods */
157502ac6454SAndrew Thompson 	usb2_fifo_check_methods(pm);
157602ac6454SAndrew Thompson 
157702ac6454SAndrew Thompson 	if (priv_mtx == NULL)
157802ac6454SAndrew Thompson 		priv_mtx = &Giant;
157902ac6454SAndrew Thompson 
158002ac6454SAndrew Thompson 	/* search for a free FIFO slot */
158102ac6454SAndrew Thompson 	for (n = 0;; n += 2) {
158202ac6454SAndrew Thompson 
158302ac6454SAndrew Thompson 		if (n == USB_FIFO_MAX) {
158402ac6454SAndrew Thompson 			/* end of FIFOs reached */
158502ac6454SAndrew Thompson 			return (ENOMEM);
158602ac6454SAndrew Thompson 		}
158702ac6454SAndrew Thompson 		/* Check for TX FIFO */
158802ac6454SAndrew Thompson 		if (udev->fifo[n + USB_FIFO_TX] != NULL) {
158902ac6454SAndrew Thompson 			continue;
159002ac6454SAndrew Thompson 		}
159102ac6454SAndrew Thompson 		/* Check for RX FIFO */
159202ac6454SAndrew Thompson 		if (udev->fifo[n + USB_FIFO_RX] != NULL) {
159302ac6454SAndrew Thompson 			continue;
159402ac6454SAndrew Thompson 		}
159502ac6454SAndrew Thompson 		break;
159602ac6454SAndrew Thompson 	}
159702ac6454SAndrew Thompson 
159802ac6454SAndrew Thompson 	f_tx = usb2_fifo_alloc();
159902ac6454SAndrew Thompson 	f_rx = usb2_fifo_alloc();
160002ac6454SAndrew Thompson 
160102ac6454SAndrew Thompson 	if ((f_tx == NULL) || (f_rx == NULL)) {
160202ac6454SAndrew Thompson 		usb2_fifo_free(f_tx);
160302ac6454SAndrew Thompson 		usb2_fifo_free(f_rx);
160402ac6454SAndrew Thompson 		return (ENOMEM);
160502ac6454SAndrew Thompson 	}
160602ac6454SAndrew Thompson 	/* initialise FIFO structures */
160702ac6454SAndrew Thompson 
160802ac6454SAndrew Thompson 	f_tx->fifo_index = n + USB_FIFO_TX;
160902ac6454SAndrew Thompson 	f_tx->priv_mtx = priv_mtx;
161002ac6454SAndrew Thompson 	f_tx->priv_sc0 = priv_sc;
161102ac6454SAndrew Thompson 	f_tx->methods = pm;
161202ac6454SAndrew Thompson 	f_tx->iface_index = iface_index;
161302ac6454SAndrew Thompson 	f_tx->udev = udev;
161402ac6454SAndrew Thompson 
161502ac6454SAndrew Thompson 	f_rx->fifo_index = n + USB_FIFO_RX;
161602ac6454SAndrew Thompson 	f_rx->priv_mtx = priv_mtx;
161702ac6454SAndrew Thompson 	f_rx->priv_sc0 = priv_sc;
161802ac6454SAndrew Thompson 	f_rx->methods = pm;
161902ac6454SAndrew Thompson 	f_rx->iface_index = iface_index;
162002ac6454SAndrew Thompson 	f_rx->udev = udev;
162102ac6454SAndrew Thompson 
162202ac6454SAndrew Thompson 	f_sc->fp[USB_FIFO_TX] = f_tx;
162302ac6454SAndrew Thompson 	f_sc->fp[USB_FIFO_RX] = f_rx;
162402ac6454SAndrew Thompson 
162502ac6454SAndrew Thompson 	mtx_lock(&usb2_ref_lock);
162602ac6454SAndrew Thompson 	udev->fifo[f_tx->fifo_index] = f_tx;
162702ac6454SAndrew Thompson 	udev->fifo[f_rx->fifo_index] = f_rx;
162802ac6454SAndrew Thompson 	mtx_unlock(&usb2_ref_lock);
162902ac6454SAndrew Thompson 
163002ac6454SAndrew Thompson 	for (n = 0; n != 4; n++) {
163102ac6454SAndrew Thompson 
163202ac6454SAndrew Thompson 		if (pm->basename[n] == NULL) {
163302ac6454SAndrew Thompson 			continue;
163402ac6454SAndrew Thompson 		}
163502ac6454SAndrew Thompson 		if (subunit == 0xFFFF) {
1636ee3e3ff5SAndrew Thompson 			if (snprintf(devname, sizeof(devname),
163702ac6454SAndrew Thompson 			    "%s%u%s", pm->basename[n],
163802ac6454SAndrew Thompson 			    unit, pm->postfix[n] ?
163902ac6454SAndrew Thompson 			    pm->postfix[n] : "")) {
164002ac6454SAndrew Thompson 				/* ignore */
164102ac6454SAndrew Thompson 			}
164202ac6454SAndrew Thompson 		} else {
1643ee3e3ff5SAndrew Thompson 			if (snprintf(devname, sizeof(devname),
164402ac6454SAndrew Thompson 			    "%s%u.%u%s", pm->basename[n],
164502ac6454SAndrew Thompson 			    unit, subunit, pm->postfix[n] ?
164602ac6454SAndrew Thompson 			    pm->postfix[n] : "")) {
164702ac6454SAndrew Thompson 				/* ignore */
164802ac6454SAndrew Thompson 			}
164902ac6454SAndrew Thompson 		}
165002ac6454SAndrew Thompson 
165102ac6454SAndrew Thompson 		/*
165202ac6454SAndrew Thompson 		 * Distribute the symbolic links into two FIFO structures:
165302ac6454SAndrew Thompson 		 */
165402ac6454SAndrew Thompson 		if (n & 1) {
165502ac6454SAndrew Thompson 			f_rx->symlink[n / 2] =
1656ee3e3ff5SAndrew Thompson 			    usb2_alloc_symlink(devname);
165702ac6454SAndrew Thompson 		} else {
165802ac6454SAndrew Thompson 			f_tx->symlink[n / 2] =
1659ee3e3ff5SAndrew Thompson 			    usb2_alloc_symlink(devname);
166002ac6454SAndrew Thompson 		}
1661ee3e3ff5SAndrew Thompson 
1662ee3e3ff5SAndrew Thompson 		/*
1663ee3e3ff5SAndrew Thompson 		 * Initialize device private data - this is used to find the
1664ee3e3ff5SAndrew Thompson 		 * actual USB device itself.
1665ee3e3ff5SAndrew Thompson 		 */
1666ee3e3ff5SAndrew Thompson 		pd = malloc(sizeof(struct usb2_fs_privdata), M_USBDEV, M_WAITOK | M_ZERO);
1667ee3e3ff5SAndrew Thompson 		pd->bus_index = device_get_unit(udev->bus->bdev);
1668ee3e3ff5SAndrew Thompson 		pd->dev_index = udev->device_index;
1669ee3e3ff5SAndrew Thompson 		pd->ep_addr = -1;	/* not an endpoint */
16707214348fSAndrew Thompson 		pd->fifo_index = f_tx->fifo_index & f_rx->fifo_index;
1671ee3e3ff5SAndrew Thompson 		pd->mode = FREAD|FWRITE;
1672ee3e3ff5SAndrew Thompson 
1673ee3e3ff5SAndrew Thompson 		/* Now, create the device itself */
1674ee3e3ff5SAndrew Thompson 		f_sc->dev = make_dev(&usb2_devsw, 0, uid, gid, mode,
1675ee3e3ff5SAndrew Thompson 		    devname);
16767214348fSAndrew Thompson 		/* XXX setting si_drv1 and creating the device is not atomic! */
1677ee3e3ff5SAndrew Thompson 		f_sc->dev->si_drv1 = pd;
167802ac6454SAndrew Thompson 	}
167902ac6454SAndrew Thompson 
168002ac6454SAndrew Thompson 	DPRINTFN(2, "attached %p/%p\n", f_tx, f_rx);
168102ac6454SAndrew Thompson 	return (0);
168202ac6454SAndrew Thompson }
168302ac6454SAndrew Thompson 
168402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
168502ac6454SAndrew Thompson  *	usb2_fifo_alloc_buffer
168602ac6454SAndrew Thompson  *
168702ac6454SAndrew Thompson  * Return values:
168802ac6454SAndrew Thompson  * 0: Success
168902ac6454SAndrew Thompson  * Else failure
169002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
169102ac6454SAndrew Thompson int
169202ac6454SAndrew Thompson usb2_fifo_alloc_buffer(struct usb2_fifo *f, uint32_t bufsize,
169302ac6454SAndrew Thompson     uint16_t nbuf)
169402ac6454SAndrew Thompson {
169502ac6454SAndrew Thompson 	usb2_fifo_free_buffer(f);
169602ac6454SAndrew Thompson 
169702ac6454SAndrew Thompson 	/* allocate an endpoint */
169802ac6454SAndrew Thompson 	f->free_q.ifq_maxlen = nbuf;
169902ac6454SAndrew Thompson 	f->used_q.ifq_maxlen = nbuf;
170002ac6454SAndrew Thompson 
170102ac6454SAndrew Thompson 	f->queue_data = usb2_alloc_mbufs(
170202ac6454SAndrew Thompson 	    M_USBDEV, &f->free_q, bufsize, nbuf);
170302ac6454SAndrew Thompson 
170402ac6454SAndrew Thompson 	if ((f->queue_data == NULL) && bufsize && nbuf) {
170502ac6454SAndrew Thompson 		return (ENOMEM);
170602ac6454SAndrew Thompson 	}
170702ac6454SAndrew Thompson 	return (0);			/* success */
170802ac6454SAndrew Thompson }
170902ac6454SAndrew Thompson 
171002ac6454SAndrew Thompson /*------------------------------------------------------------------------*
171102ac6454SAndrew Thompson  *	usb2_fifo_free_buffer
171202ac6454SAndrew Thompson  *
171302ac6454SAndrew Thompson  * This function will free the buffers associated with a FIFO. This
171402ac6454SAndrew Thompson  * function can be called multiple times in a row.
171502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
171602ac6454SAndrew Thompson void
171702ac6454SAndrew Thompson usb2_fifo_free_buffer(struct usb2_fifo *f)
171802ac6454SAndrew Thompson {
171902ac6454SAndrew Thompson 	if (f->queue_data) {
172002ac6454SAndrew Thompson 		/* free old buffer */
172102ac6454SAndrew Thompson 		free(f->queue_data, M_USBDEV);
172202ac6454SAndrew Thompson 		f->queue_data = NULL;
172302ac6454SAndrew Thompson 	}
172402ac6454SAndrew Thompson 	/* reset queues */
172502ac6454SAndrew Thompson 
172602ac6454SAndrew Thompson 	bzero(&f->free_q, sizeof(f->free_q));
172702ac6454SAndrew Thompson 	bzero(&f->used_q, sizeof(f->used_q));
172802ac6454SAndrew Thompson }
172902ac6454SAndrew Thompson 
1730ee3e3ff5SAndrew Thompson static void
1731ee3e3ff5SAndrew Thompson usb2_fifo_cleanup(void* ptr)
1732ee3e3ff5SAndrew Thompson {
1733ee3e3ff5SAndrew Thompson 	free(ptr, M_USBDEV);
1734ee3e3ff5SAndrew Thompson }
1735ee3e3ff5SAndrew Thompson 
173602ac6454SAndrew Thompson void
173702ac6454SAndrew Thompson usb2_fifo_detach(struct usb2_fifo_sc *f_sc)
173802ac6454SAndrew Thompson {
173902ac6454SAndrew Thompson 	if (f_sc == NULL) {
174002ac6454SAndrew Thompson 		return;
174102ac6454SAndrew Thompson 	}
174202ac6454SAndrew Thompson 	usb2_fifo_free(f_sc->fp[USB_FIFO_TX]);
174302ac6454SAndrew Thompson 	usb2_fifo_free(f_sc->fp[USB_FIFO_RX]);
174402ac6454SAndrew Thompson 
174502ac6454SAndrew Thompson 	f_sc->fp[USB_FIFO_TX] = NULL;
174602ac6454SAndrew Thompson 	f_sc->fp[USB_FIFO_RX] = NULL;
174702ac6454SAndrew Thompson 
1748ee3e3ff5SAndrew Thompson 	if (f_sc->dev != NULL) {
17497214348fSAndrew Thompson 		destroy_dev_sched_cb(f_sc->dev,
17507214348fSAndrew Thompson 		    usb2_fifo_cleanup, f_sc->dev->si_drv1);
17517214348fSAndrew Thompson 		f_sc->dev = NULL;
1752ee3e3ff5SAndrew Thompson 	}
1753ee3e3ff5SAndrew Thompson 
175402ac6454SAndrew Thompson 	DPRINTFN(2, "detached %p\n", f_sc);
175502ac6454SAndrew Thompson }
175602ac6454SAndrew Thompson 
175702ac6454SAndrew Thompson uint32_t
175802ac6454SAndrew Thompson usb2_fifo_put_bytes_max(struct usb2_fifo *f)
175902ac6454SAndrew Thompson {
176002ac6454SAndrew Thompson 	struct usb2_mbuf *m;
176102ac6454SAndrew Thompson 	uint32_t len;
176202ac6454SAndrew Thompson 
176302ac6454SAndrew Thompson 	USB_IF_POLL(&f->free_q, m);
176402ac6454SAndrew Thompson 
176502ac6454SAndrew Thompson 	if (m) {
176602ac6454SAndrew Thompson 		len = m->max_data_len;
176702ac6454SAndrew Thompson 	} else {
176802ac6454SAndrew Thompson 		len = 0;
176902ac6454SAndrew Thompson 	}
177002ac6454SAndrew Thompson 	return (len);
177102ac6454SAndrew Thompson }
177202ac6454SAndrew Thompson 
177302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
177402ac6454SAndrew Thompson  *	usb2_fifo_put_data
177502ac6454SAndrew Thompson  *
177602ac6454SAndrew Thompson  * what:
177702ac6454SAndrew Thompson  *  0 - normal operation
177802ac6454SAndrew Thompson  *  1 - set last packet flag to enforce framing
177902ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
178002ac6454SAndrew Thompson void
178102ac6454SAndrew Thompson usb2_fifo_put_data(struct usb2_fifo *f, struct usb2_page_cache *pc,
178202ac6454SAndrew Thompson     uint32_t offset, uint32_t len, uint8_t what)
178302ac6454SAndrew Thompson {
178402ac6454SAndrew Thompson 	struct usb2_mbuf *m;
178502ac6454SAndrew Thompson 	uint32_t io_len;
178602ac6454SAndrew Thompson 
178702ac6454SAndrew Thompson 	while (len || (what == 1)) {
178802ac6454SAndrew Thompson 
178902ac6454SAndrew Thompson 		USB_IF_DEQUEUE(&f->free_q, m);
179002ac6454SAndrew Thompson 
179102ac6454SAndrew Thompson 		if (m) {
179202ac6454SAndrew Thompson 			USB_MBUF_RESET(m);
179302ac6454SAndrew Thompson 
179402ac6454SAndrew Thompson 			io_len = MIN(len, m->cur_data_len);
179502ac6454SAndrew Thompson 
179602ac6454SAndrew Thompson 			usb2_copy_out(pc, offset, m->cur_data_ptr, io_len);
179702ac6454SAndrew Thompson 
179802ac6454SAndrew Thompson 			m->cur_data_len = io_len;
179902ac6454SAndrew Thompson 			offset += io_len;
180002ac6454SAndrew Thompson 			len -= io_len;
180102ac6454SAndrew Thompson 
180202ac6454SAndrew Thompson 			if ((len == 0) && (what == 1)) {
180302ac6454SAndrew Thompson 				m->last_packet = 1;
180402ac6454SAndrew Thompson 			}
180502ac6454SAndrew Thompson 			USB_IF_ENQUEUE(&f->used_q, m);
180602ac6454SAndrew Thompson 
180702ac6454SAndrew Thompson 			usb2_fifo_wakeup(f);
180802ac6454SAndrew Thompson 
180902ac6454SAndrew Thompson 			if ((len == 0) || (what == 1)) {
181002ac6454SAndrew Thompson 				break;
181102ac6454SAndrew Thompson 			}
181202ac6454SAndrew Thompson 		} else {
181302ac6454SAndrew Thompson 			break;
181402ac6454SAndrew Thompson 		}
181502ac6454SAndrew Thompson 	}
181602ac6454SAndrew Thompson }
181702ac6454SAndrew Thompson 
181802ac6454SAndrew Thompson void
181902ac6454SAndrew Thompson usb2_fifo_put_data_linear(struct usb2_fifo *f, void *ptr,
182002ac6454SAndrew Thompson     uint32_t len, uint8_t what)
182102ac6454SAndrew Thompson {
182202ac6454SAndrew Thompson 	struct usb2_mbuf *m;
182302ac6454SAndrew Thompson 	uint32_t io_len;
182402ac6454SAndrew Thompson 
182502ac6454SAndrew Thompson 	while (len || (what == 1)) {
182602ac6454SAndrew Thompson 
182702ac6454SAndrew Thompson 		USB_IF_DEQUEUE(&f->free_q, m);
182802ac6454SAndrew Thompson 
182902ac6454SAndrew Thompson 		if (m) {
183002ac6454SAndrew Thompson 			USB_MBUF_RESET(m);
183102ac6454SAndrew Thompson 
183202ac6454SAndrew Thompson 			io_len = MIN(len, m->cur_data_len);
183302ac6454SAndrew Thompson 
183402ac6454SAndrew Thompson 			bcopy(ptr, m->cur_data_ptr, io_len);
183502ac6454SAndrew Thompson 
183602ac6454SAndrew Thompson 			m->cur_data_len = io_len;
183702ac6454SAndrew Thompson 			ptr = USB_ADD_BYTES(ptr, io_len);
183802ac6454SAndrew Thompson 			len -= io_len;
183902ac6454SAndrew Thompson 
184002ac6454SAndrew Thompson 			if ((len == 0) && (what == 1)) {
184102ac6454SAndrew Thompson 				m->last_packet = 1;
184202ac6454SAndrew Thompson 			}
184302ac6454SAndrew Thompson 			USB_IF_ENQUEUE(&f->used_q, m);
184402ac6454SAndrew Thompson 
184502ac6454SAndrew Thompson 			usb2_fifo_wakeup(f);
184602ac6454SAndrew Thompson 
184702ac6454SAndrew Thompson 			if ((len == 0) || (what == 1)) {
184802ac6454SAndrew Thompson 				break;
184902ac6454SAndrew Thompson 			}
185002ac6454SAndrew Thompson 		} else {
185102ac6454SAndrew Thompson 			break;
185202ac6454SAndrew Thompson 		}
185302ac6454SAndrew Thompson 	}
185402ac6454SAndrew Thompson }
185502ac6454SAndrew Thompson 
185602ac6454SAndrew Thompson uint8_t
185702ac6454SAndrew Thompson usb2_fifo_put_data_buffer(struct usb2_fifo *f, void *ptr, uint32_t len)
185802ac6454SAndrew Thompson {
185902ac6454SAndrew Thompson 	struct usb2_mbuf *m;
186002ac6454SAndrew Thompson 
186102ac6454SAndrew Thompson 	USB_IF_DEQUEUE(&f->free_q, m);
186202ac6454SAndrew Thompson 
186302ac6454SAndrew Thompson 	if (m) {
186402ac6454SAndrew Thompson 		m->cur_data_len = len;
186502ac6454SAndrew Thompson 		m->cur_data_ptr = ptr;
186602ac6454SAndrew Thompson 		USB_IF_ENQUEUE(&f->used_q, m);
186702ac6454SAndrew Thompson 		usb2_fifo_wakeup(f);
186802ac6454SAndrew Thompson 		return (1);
186902ac6454SAndrew Thompson 	}
187002ac6454SAndrew Thompson 	return (0);
187102ac6454SAndrew Thompson }
187202ac6454SAndrew Thompson 
187302ac6454SAndrew Thompson void
187402ac6454SAndrew Thompson usb2_fifo_put_data_error(struct usb2_fifo *f)
187502ac6454SAndrew Thompson {
187602ac6454SAndrew Thompson 	f->flag_iserror = 1;
187702ac6454SAndrew Thompson 	usb2_fifo_wakeup(f);
187802ac6454SAndrew Thompson }
187902ac6454SAndrew Thompson 
188002ac6454SAndrew Thompson /*------------------------------------------------------------------------*
188102ac6454SAndrew Thompson  *	usb2_fifo_get_data
188202ac6454SAndrew Thompson  *
188302ac6454SAndrew Thompson  * what:
188402ac6454SAndrew Thompson  *  0 - normal operation
188502ac6454SAndrew Thompson  *  1 - only get one "usb2_mbuf"
188602ac6454SAndrew Thompson  *
188702ac6454SAndrew Thompson  * returns:
188802ac6454SAndrew Thompson  *  0 - no more data
188902ac6454SAndrew Thompson  *  1 - data in buffer
189002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
189102ac6454SAndrew Thompson uint8_t
189202ac6454SAndrew Thompson usb2_fifo_get_data(struct usb2_fifo *f, struct usb2_page_cache *pc,
189302ac6454SAndrew Thompson     uint32_t offset, uint32_t len, uint32_t *actlen,
189402ac6454SAndrew Thompson     uint8_t what)
189502ac6454SAndrew Thompson {
189602ac6454SAndrew Thompson 	struct usb2_mbuf *m;
189702ac6454SAndrew Thompson 	uint32_t io_len;
189802ac6454SAndrew Thompson 	uint8_t tr_data = 0;
189902ac6454SAndrew Thompson 
190002ac6454SAndrew Thompson 	actlen[0] = 0;
190102ac6454SAndrew Thompson 
190202ac6454SAndrew Thompson 	while (1) {
190302ac6454SAndrew Thompson 
190402ac6454SAndrew Thompson 		USB_IF_DEQUEUE(&f->used_q, m);
190502ac6454SAndrew Thompson 
190602ac6454SAndrew Thompson 		if (m) {
190702ac6454SAndrew Thompson 
190802ac6454SAndrew Thompson 			tr_data = 1;
190902ac6454SAndrew Thompson 
191002ac6454SAndrew Thompson 			io_len = MIN(len, m->cur_data_len);
191102ac6454SAndrew Thompson 
191202ac6454SAndrew Thompson 			usb2_copy_in(pc, offset, m->cur_data_ptr, io_len);
191302ac6454SAndrew Thompson 
191402ac6454SAndrew Thompson 			len -= io_len;
191502ac6454SAndrew Thompson 			offset += io_len;
191602ac6454SAndrew Thompson 			actlen[0] += io_len;
191702ac6454SAndrew Thompson 			m->cur_data_ptr += io_len;
191802ac6454SAndrew Thompson 			m->cur_data_len -= io_len;
191902ac6454SAndrew Thompson 
192002ac6454SAndrew Thompson 			if ((m->cur_data_len == 0) || (what == 1)) {
192102ac6454SAndrew Thompson 				USB_IF_ENQUEUE(&f->free_q, m);
192202ac6454SAndrew Thompson 
192302ac6454SAndrew Thompson 				usb2_fifo_wakeup(f);
192402ac6454SAndrew Thompson 
192502ac6454SAndrew Thompson 				if (what == 1) {
192602ac6454SAndrew Thompson 					break;
192702ac6454SAndrew Thompson 				}
192802ac6454SAndrew Thompson 			} else {
192902ac6454SAndrew Thompson 				USB_IF_PREPEND(&f->used_q, m);
193002ac6454SAndrew Thompson 			}
193102ac6454SAndrew Thompson 		} else {
193202ac6454SAndrew Thompson 
193302ac6454SAndrew Thompson 			if (tr_data) {
193402ac6454SAndrew Thompson 				/* wait for data to be written out */
193502ac6454SAndrew Thompson 				break;
193602ac6454SAndrew Thompson 			}
193702ac6454SAndrew Thompson 			if (f->flag_flushing) {
19387214348fSAndrew Thompson 				/* check if we should send a short packet */
19397214348fSAndrew Thompson 				if (f->flag_short != 0) {
19407214348fSAndrew Thompson 					f->flag_short = 0;
19417214348fSAndrew Thompson 					tr_data = 1;
19427214348fSAndrew Thompson 					break;
19437214348fSAndrew Thompson 				}
19447214348fSAndrew Thompson 				/* flushing complete */
194502ac6454SAndrew Thompson 				f->flag_flushing = 0;
194602ac6454SAndrew Thompson 				usb2_fifo_wakeup(f);
194702ac6454SAndrew Thompson 			}
194802ac6454SAndrew Thompson 			break;
194902ac6454SAndrew Thompson 		}
195002ac6454SAndrew Thompson 		if (len == 0) {
195102ac6454SAndrew Thompson 			break;
195202ac6454SAndrew Thompson 		}
195302ac6454SAndrew Thompson 	}
195402ac6454SAndrew Thompson 	return (tr_data);
195502ac6454SAndrew Thompson }
195602ac6454SAndrew Thompson 
195702ac6454SAndrew Thompson uint8_t
195802ac6454SAndrew Thompson usb2_fifo_get_data_linear(struct usb2_fifo *f, void *ptr,
195902ac6454SAndrew Thompson     uint32_t len, uint32_t *actlen, uint8_t what)
196002ac6454SAndrew Thompson {
196102ac6454SAndrew Thompson 	struct usb2_mbuf *m;
196202ac6454SAndrew Thompson 	uint32_t io_len;
196302ac6454SAndrew Thompson 	uint8_t tr_data = 0;
196402ac6454SAndrew Thompson 
196502ac6454SAndrew Thompson 	actlen[0] = 0;
196602ac6454SAndrew Thompson 
196702ac6454SAndrew Thompson 	while (1) {
196802ac6454SAndrew Thompson 
196902ac6454SAndrew Thompson 		USB_IF_DEQUEUE(&f->used_q, m);
197002ac6454SAndrew Thompson 
197102ac6454SAndrew Thompson 		if (m) {
197202ac6454SAndrew Thompson 
197302ac6454SAndrew Thompson 			tr_data = 1;
197402ac6454SAndrew Thompson 
197502ac6454SAndrew Thompson 			io_len = MIN(len, m->cur_data_len);
197602ac6454SAndrew Thompson 
197702ac6454SAndrew Thompson 			bcopy(m->cur_data_ptr, ptr, io_len);
197802ac6454SAndrew Thompson 
197902ac6454SAndrew Thompson 			len -= io_len;
198002ac6454SAndrew Thompson 			ptr = USB_ADD_BYTES(ptr, io_len);
198102ac6454SAndrew Thompson 			actlen[0] += io_len;
198202ac6454SAndrew Thompson 			m->cur_data_ptr += io_len;
198302ac6454SAndrew Thompson 			m->cur_data_len -= io_len;
198402ac6454SAndrew Thompson 
198502ac6454SAndrew Thompson 			if ((m->cur_data_len == 0) || (what == 1)) {
198602ac6454SAndrew Thompson 				USB_IF_ENQUEUE(&f->free_q, m);
198702ac6454SAndrew Thompson 
198802ac6454SAndrew Thompson 				usb2_fifo_wakeup(f);
198902ac6454SAndrew Thompson 
199002ac6454SAndrew Thompson 				if (what == 1) {
199102ac6454SAndrew Thompson 					break;
199202ac6454SAndrew Thompson 				}
199302ac6454SAndrew Thompson 			} else {
199402ac6454SAndrew Thompson 				USB_IF_PREPEND(&f->used_q, m);
199502ac6454SAndrew Thompson 			}
199602ac6454SAndrew Thompson 		} else {
199702ac6454SAndrew Thompson 
199802ac6454SAndrew Thompson 			if (tr_data) {
199902ac6454SAndrew Thompson 				/* wait for data to be written out */
200002ac6454SAndrew Thompson 				break;
200102ac6454SAndrew Thompson 			}
200202ac6454SAndrew Thompson 			if (f->flag_flushing) {
20037214348fSAndrew Thompson 				/* check if we should send a short packet */
20047214348fSAndrew Thompson 				if (f->flag_short != 0) {
20057214348fSAndrew Thompson 					f->flag_short = 0;
20067214348fSAndrew Thompson 					tr_data = 1;
20077214348fSAndrew Thompson 					break;
20087214348fSAndrew Thompson 				}
20097214348fSAndrew Thompson 				/* flushing complete */
201002ac6454SAndrew Thompson 				f->flag_flushing = 0;
201102ac6454SAndrew Thompson 				usb2_fifo_wakeup(f);
201202ac6454SAndrew Thompson 			}
201302ac6454SAndrew Thompson 			break;
201402ac6454SAndrew Thompson 		}
201502ac6454SAndrew Thompson 		if (len == 0) {
201602ac6454SAndrew Thompson 			break;
201702ac6454SAndrew Thompson 		}
201802ac6454SAndrew Thompson 	}
201902ac6454SAndrew Thompson 	return (tr_data);
202002ac6454SAndrew Thompson }
202102ac6454SAndrew Thompson 
202202ac6454SAndrew Thompson uint8_t
202302ac6454SAndrew Thompson usb2_fifo_get_data_buffer(struct usb2_fifo *f, void **pptr, uint32_t *plen)
202402ac6454SAndrew Thompson {
202502ac6454SAndrew Thompson 	struct usb2_mbuf *m;
202602ac6454SAndrew Thompson 
202702ac6454SAndrew Thompson 	USB_IF_POLL(&f->used_q, m);
202802ac6454SAndrew Thompson 
202902ac6454SAndrew Thompson 	if (m) {
203002ac6454SAndrew Thompson 		*plen = m->cur_data_len;
203102ac6454SAndrew Thompson 		*pptr = m->cur_data_ptr;
203202ac6454SAndrew Thompson 
203302ac6454SAndrew Thompson 		return (1);
203402ac6454SAndrew Thompson 	}
203502ac6454SAndrew Thompson 	return (0);
203602ac6454SAndrew Thompson }
203702ac6454SAndrew Thompson 
203802ac6454SAndrew Thompson void
203902ac6454SAndrew Thompson usb2_fifo_get_data_error(struct usb2_fifo *f)
204002ac6454SAndrew Thompson {
204102ac6454SAndrew Thompson 	f->flag_iserror = 1;
204202ac6454SAndrew Thompson 	usb2_fifo_wakeup(f);
204302ac6454SAndrew Thompson }
204402ac6454SAndrew Thompson 
204502ac6454SAndrew Thompson /*------------------------------------------------------------------------*
204602ac6454SAndrew Thompson  *	usb2_alloc_symlink
204702ac6454SAndrew Thompson  *
204802ac6454SAndrew Thompson  * Return values:
204902ac6454SAndrew Thompson  * NULL: Failure
205002ac6454SAndrew Thompson  * Else: Pointer to symlink entry
205102ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
205202ac6454SAndrew Thompson struct usb2_symlink *
2053ee3e3ff5SAndrew Thompson usb2_alloc_symlink(const char *target)
205402ac6454SAndrew Thompson {
205502ac6454SAndrew Thompson 	struct usb2_symlink *ps;
205602ac6454SAndrew Thompson 
205702ac6454SAndrew Thompson 	ps = malloc(sizeof(*ps), M_USBDEV, M_WAITOK);
205802ac6454SAndrew Thompson 	if (ps == NULL) {
205902ac6454SAndrew Thompson 		return (ps);
206002ac6454SAndrew Thompson 	}
2061ee3e3ff5SAndrew Thompson 	/* XXX no longer needed */
2062ee3e3ff5SAndrew Thompson 	strlcpy(ps->src_path, target, sizeof(ps->src_path));
2063ee3e3ff5SAndrew Thompson 	ps->src_len = strlen(ps->src_path);
206402ac6454SAndrew Thompson 	strlcpy(ps->dst_path, target, sizeof(ps->dst_path));
206502ac6454SAndrew Thompson 	ps->dst_len = strlen(ps->dst_path);
206602ac6454SAndrew Thompson 
206702ac6454SAndrew Thompson 	sx_xlock(&usb2_sym_lock);
206802ac6454SAndrew Thompson 	TAILQ_INSERT_TAIL(&usb2_sym_head, ps, sym_entry);
206902ac6454SAndrew Thompson 	sx_unlock(&usb2_sym_lock);
207002ac6454SAndrew Thompson 	return (ps);
207102ac6454SAndrew Thompson }
207202ac6454SAndrew Thompson 
207302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
207402ac6454SAndrew Thompson  *	usb2_free_symlink
207502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
207602ac6454SAndrew Thompson void
207702ac6454SAndrew Thompson usb2_free_symlink(struct usb2_symlink *ps)
207802ac6454SAndrew Thompson {
207902ac6454SAndrew Thompson 	if (ps == NULL) {
208002ac6454SAndrew Thompson 		return;
208102ac6454SAndrew Thompson 	}
208202ac6454SAndrew Thompson 	sx_xlock(&usb2_sym_lock);
208302ac6454SAndrew Thompson 	TAILQ_REMOVE(&usb2_sym_head, ps, sym_entry);
208402ac6454SAndrew Thompson 	sx_unlock(&usb2_sym_lock);
208502ac6454SAndrew Thompson 
208602ac6454SAndrew Thompson 	free(ps, M_USBDEV);
208702ac6454SAndrew Thompson }
208802ac6454SAndrew Thompson 
208902ac6454SAndrew Thompson /*------------------------------------------------------------------------*
209002ac6454SAndrew Thompson  *	usb2_read_symlink
209102ac6454SAndrew Thompson  *
209202ac6454SAndrew Thompson  * Return value:
209302ac6454SAndrew Thompson  * 0: Success
209402ac6454SAndrew Thompson  * Else: Failure
209502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
209602ac6454SAndrew Thompson int
209702ac6454SAndrew Thompson usb2_read_symlink(uint8_t *user_ptr, uint32_t startentry, uint32_t user_len)
209802ac6454SAndrew Thompson {
209902ac6454SAndrew Thompson 	struct usb2_symlink *ps;
210002ac6454SAndrew Thompson 	uint32_t temp;
210102ac6454SAndrew Thompson 	uint32_t delta = 0;
210202ac6454SAndrew Thompson 	uint8_t len;
210302ac6454SAndrew Thompson 	int error = 0;
210402ac6454SAndrew Thompson 
210502ac6454SAndrew Thompson 	sx_xlock(&usb2_sym_lock);
210602ac6454SAndrew Thompson 
210702ac6454SAndrew Thompson 	TAILQ_FOREACH(ps, &usb2_sym_head, sym_entry) {
210802ac6454SAndrew Thompson 
210902ac6454SAndrew Thompson 		/*
211002ac6454SAndrew Thompson 		 * Compute total length of source and destination symlink
211102ac6454SAndrew Thompson 		 * strings pluss one length byte and two NUL bytes:
211202ac6454SAndrew Thompson 		 */
211302ac6454SAndrew Thompson 		temp = ps->src_len + ps->dst_len + 3;
211402ac6454SAndrew Thompson 
211502ac6454SAndrew Thompson 		if (temp > 255) {
211602ac6454SAndrew Thompson 			/*
211702ac6454SAndrew Thompson 			 * Skip entry because this length cannot fit
211802ac6454SAndrew Thompson 			 * into one byte:
211902ac6454SAndrew Thompson 			 */
212002ac6454SAndrew Thompson 			continue;
212102ac6454SAndrew Thompson 		}
212202ac6454SAndrew Thompson 		if (startentry != 0) {
212302ac6454SAndrew Thompson 			/* decrement read offset */
212402ac6454SAndrew Thompson 			startentry--;
212502ac6454SAndrew Thompson 			continue;
212602ac6454SAndrew Thompson 		}
212702ac6454SAndrew Thompson 		if (temp > user_len) {
212802ac6454SAndrew Thompson 			/* out of buffer space */
212902ac6454SAndrew Thompson 			break;
213002ac6454SAndrew Thompson 		}
213102ac6454SAndrew Thompson 		len = temp;
213202ac6454SAndrew Thompson 
213302ac6454SAndrew Thompson 		/* copy out total length */
213402ac6454SAndrew Thompson 
213502ac6454SAndrew Thompson 		error = copyout(&len,
213602ac6454SAndrew Thompson 		    USB_ADD_BYTES(user_ptr, delta), 1);
213702ac6454SAndrew Thompson 		if (error) {
213802ac6454SAndrew Thompson 			break;
213902ac6454SAndrew Thompson 		}
214002ac6454SAndrew Thompson 		delta += 1;
214102ac6454SAndrew Thompson 
214202ac6454SAndrew Thompson 		/* copy out source string */
214302ac6454SAndrew Thompson 
214402ac6454SAndrew Thompson 		error = copyout(ps->src_path,
214502ac6454SAndrew Thompson 		    USB_ADD_BYTES(user_ptr, delta), ps->src_len);
214602ac6454SAndrew Thompson 		if (error) {
214702ac6454SAndrew Thompson 			break;
214802ac6454SAndrew Thompson 		}
214902ac6454SAndrew Thompson 		len = 0;
215002ac6454SAndrew Thompson 		delta += ps->src_len;
215102ac6454SAndrew Thompson 		error = copyout(&len,
215202ac6454SAndrew Thompson 		    USB_ADD_BYTES(user_ptr, delta), 1);
215302ac6454SAndrew Thompson 		if (error) {
215402ac6454SAndrew Thompson 			break;
215502ac6454SAndrew Thompson 		}
215602ac6454SAndrew Thompson 		delta += 1;
215702ac6454SAndrew Thompson 
215802ac6454SAndrew Thompson 		/* copy out destination string */
215902ac6454SAndrew Thompson 
216002ac6454SAndrew Thompson 		error = copyout(ps->dst_path,
216102ac6454SAndrew Thompson 		    USB_ADD_BYTES(user_ptr, delta), ps->dst_len);
216202ac6454SAndrew Thompson 		if (error) {
216302ac6454SAndrew Thompson 			break;
216402ac6454SAndrew Thompson 		}
216502ac6454SAndrew Thompson 		len = 0;
216602ac6454SAndrew Thompson 		delta += ps->dst_len;
216702ac6454SAndrew Thompson 		error = copyout(&len,
216802ac6454SAndrew Thompson 		    USB_ADD_BYTES(user_ptr, delta), 1);
216902ac6454SAndrew Thompson 		if (error) {
217002ac6454SAndrew Thompson 			break;
217102ac6454SAndrew Thompson 		}
217202ac6454SAndrew Thompson 		delta += 1;
217302ac6454SAndrew Thompson 
217402ac6454SAndrew Thompson 		user_len -= temp;
217502ac6454SAndrew Thompson 	}
217602ac6454SAndrew Thompson 
217702ac6454SAndrew Thompson 	/* a zero length entry indicates the end */
217802ac6454SAndrew Thompson 
217902ac6454SAndrew Thompson 	if ((user_len != 0) && (error == 0)) {
218002ac6454SAndrew Thompson 
218102ac6454SAndrew Thompson 		len = 0;
218202ac6454SAndrew Thompson 
218302ac6454SAndrew Thompson 		error = copyout(&len,
218402ac6454SAndrew Thompson 		    USB_ADD_BYTES(user_ptr, delta), 1);
218502ac6454SAndrew Thompson 	}
218602ac6454SAndrew Thompson 	sx_unlock(&usb2_sym_lock);
218702ac6454SAndrew Thompson 	return (error);
218802ac6454SAndrew Thompson }
21897214348fSAndrew Thompson 
21907214348fSAndrew Thompson void
21917214348fSAndrew Thompson usb2_fifo_set_close_zlp(struct usb2_fifo *f, uint8_t onoff)
21927214348fSAndrew Thompson {
21937214348fSAndrew Thompson 	if (f == NULL)
21947214348fSAndrew Thompson 		return;
21957214348fSAndrew Thompson 
21967214348fSAndrew Thompson 	/* send a Zero Length Packet, ZLP, before close */
21977214348fSAndrew Thompson 	f->flag_short = onoff;
21987214348fSAndrew Thompson }
2199