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