3 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 * Copyright (c) 2006-2008 Hans Petter Selasky. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * usb_dev.c - An abstraction layer for creating devices under /dev/...
32 #ifdef USB_GLOBAL_INCLUDE_FILE
33 #include USB_GLOBAL_INCLUDE_FILE
35 #ifdef COMPAT_FREEBSD32
36 #include <sys/abi_compat.h>
38 #include <sys/stdint.h>
39 #include <sys/stddef.h>
40 #include <sys/param.h>
41 #include <sys/queue.h>
42 #include <sys/types.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
46 #include <sys/module.h>
48 #include <sys/mutex.h>
49 #include <sys/condvar.h>
50 #include <sys/sysctl.h>
52 #include <sys/unistd.h>
53 #include <sys/callout.h>
54 #include <sys/malloc.h>
56 #include <sys/vnode.h>
58 #include <sys/fcntl.h>
60 #include <dev/usb/usb.h>
61 #include <dev/usb/usb_ioctl.h>
62 #include <dev/usb/usbdi.h>
63 #include <dev/usb/usbdi_util.h>
65 #define USB_DEBUG_VAR usb_fifo_debug
67 #include <dev/usb/usb_core.h>
68 #include <dev/usb/usb_dev.h>
69 #include <dev/usb/usb_mbuf.h>
70 #include <dev/usb/usb_process.h>
71 #include <dev/usb/usb_device.h>
72 #include <dev/usb/usb_debug.h>
73 #include <dev/usb/usb_busdma.h>
74 #include <dev/usb/usb_generic.h>
75 #include <dev/usb/usb_dynamic.h>
76 #include <dev/usb/usb_util.h>
78 #include <dev/usb/usb_controller.h>
79 #include <dev/usb/usb_bus.h>
81 #include <sys/filio.h>
82 #include <sys/ttycom.h>
83 #include <sys/syscallsubr.h>
85 #include <machine/stdarg.h>
86 #endif /* USB_GLOBAL_INCLUDE_FILE */
91 static int usb_fifo_debug = 0;
93 static SYSCTL_NODE(_hw_usb, OID_AUTO, dev, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
95 SYSCTL_INT(_hw_usb_dev, OID_AUTO, debug, CTLFLAG_RWTUN,
96 &usb_fifo_debug, 0, "Debug Level");
99 #if ((__FreeBSD_version >= 700001) || (__FreeBSD_version == 0) || \
100 ((__FreeBSD_version >= 600034) && (__FreeBSD_version < 700000)))
101 #define USB_UCRED struct ucred *ucred,
108 static int usb_fifo_open(struct usb_cdev_privdata *,
109 struct usb_fifo *, int);
110 static void usb_fifo_close(struct usb_fifo *, int);
111 static void usb_dev_init(void *);
112 static void usb_dev_init_post(void *);
113 static void usb_dev_uninit(void *);
114 static int usb_fifo_uiomove(struct usb_fifo *, void *, int,
116 static void usb_fifo_check_methods(struct usb_fifo_methods *);
117 static struct usb_fifo *usb_fifo_alloc(struct mtx *);
118 static struct usb_endpoint *usb_dev_get_ep(struct usb_device *, uint8_t,
120 static void usb_loc_fill(struct usb_fs_privdata *,
121 struct usb_cdev_privdata *);
122 static void usb_close(void *);
123 static usb_error_t usb_ref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *, int);
124 static usb_error_t usb_usb_ref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *);
125 static void usb_unref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *);
127 static d_open_t usb_open;
128 static d_ioctl_t usb_ioctl;
129 static d_read_t usb_read;
130 static d_write_t usb_write;
131 static d_poll_t usb_poll;
132 static d_kqfilter_t usb_kqfilter;
134 static d_ioctl_t usb_static_ioctl;
136 static usb_fifo_open_t usb_fifo_dummy_open;
137 static usb_fifo_close_t usb_fifo_dummy_close;
138 static usb_fifo_ioctl_t usb_fifo_dummy_ioctl;
139 static usb_fifo_cmd_t usb_fifo_dummy_cmd;
141 /* character device structure used for devices (/dev/ugenX.Y and /dev/uXXX) */
142 struct cdevsw usb_devsw = {
143 .d_version = D_VERSION,
145 .d_ioctl = usb_ioctl,
147 .d_flags = D_TRACKCLOSE,
149 .d_write = usb_write,
151 .d_kqfilter = usb_kqfilter,
154 static struct cdev* usb_dev = NULL;
156 /* character device structure used for /dev/usb */
157 static struct cdevsw usb_static_devsw = {
158 .d_version = D_VERSION,
159 .d_ioctl = usb_static_ioctl,
163 static TAILQ_HEAD(, usb_symlink) usb_sym_head;
164 static struct sx usb_sym_lock;
166 struct mtx usb_ref_lock;
168 /*------------------------------------------------------------------------*
171 * This is used to fill out a usb_cdev_privdata structure based on the
172 * device's address as contained in usb_fs_privdata.
173 *------------------------------------------------------------------------*/
175 usb_loc_fill(struct usb_fs_privdata* pd, struct usb_cdev_privdata *cpd)
177 cpd->bus_index = pd->bus_index;
178 cpd->dev_index = pd->dev_index;
179 cpd->ep_addr = pd->ep_addr;
180 cpd->fifo_index = pd->fifo_index;
183 /*------------------------------------------------------------------------*
186 * This function is used to atomically refer an USB device by its
187 * device location. If this function returns success the USB device
188 * will not disappear until the USB device is unreferenced.
191 * 0: Success, refcount incremented on the given USB device.
193 *------------------------------------------------------------------------*/
195 usb_ref_device(struct usb_cdev_privdata *cpd,
196 struct usb_cdev_refdata *crd, int need_uref)
198 struct usb_fifo **ppf;
201 DPRINTFN(2, "cpd=%p need uref=%d\n", cpd, need_uref);
204 memset(crd, 0, sizeof(*crd));
206 mtx_lock(&usb_ref_lock);
207 cpd->bus = devclass_get_softc(usb_devclass_ptr, cpd->bus_index);
208 if (cpd->bus == NULL) {
209 DPRINTFN(2, "no bus at %u\n", cpd->bus_index);
212 cpd->udev = cpd->bus->devices[cpd->dev_index];
213 if (cpd->udev == NULL) {
214 DPRINTFN(2, "no device at %u\n", cpd->dev_index);
217 if (cpd->udev->state == USB_STATE_DETACHED &&
219 DPRINTFN(2, "device is detached\n");
223 DPRINTFN(2, "ref udev - needed\n");
225 if (cpd->udev->refcount == USB_DEV_REF_MAX) {
226 DPRINTFN(2, "no dev ref\n");
229 cpd->udev->refcount++;
231 mtx_unlock(&usb_ref_lock);
234 * We need to grab the enumeration SX-lock before
235 * grabbing the FIFO refs to avoid deadlock at detach!
237 crd->do_unlock = usbd_enum_lock_sig(cpd->udev);
239 mtx_lock(&usb_ref_lock);
242 * Set "is_uref" after grabbing the default SX lock
246 /* check for signal */
247 if (crd->do_unlock > 1) {
253 /* check if we are doing an open */
254 if (cpd->fflags == 0) {
255 /* use zero defaults */
257 /* check for write */
258 if (cpd->fflags & FWRITE) {
259 ppf = cpd->udev->fifo;
260 f = ppf[cpd->fifo_index + USB_FIFO_TX];
262 crd->is_write = 1; /* ref */
263 if (f == NULL || f->refcount == USB_FIFO_REF_MAX)
265 if (f->curr_cpd != cpd)
267 /* check if USB-FS is active */
268 if (f->fs_ep_max != 0) {
274 if (cpd->fflags & FREAD) {
275 ppf = cpd->udev->fifo;
276 f = ppf[cpd->fifo_index + USB_FIFO_RX];
278 crd->is_read = 1; /* ref */
279 if (f == NULL || f->refcount == USB_FIFO_REF_MAX)
281 if (f->curr_cpd != cpd)
283 /* check if USB-FS is active */
284 if (f->fs_ep_max != 0) {
290 /* when everything is OK we increment the refcounts */
292 DPRINTFN(2, "ref write\n");
293 crd->txfifo->refcount++;
296 DPRINTFN(2, "ref read\n");
297 crd->rxfifo->refcount++;
299 mtx_unlock(&usb_ref_lock);
305 usbd_enum_unlock(cpd->udev);
308 if (--(cpd->udev->refcount) == 0)
309 cv_broadcast(&cpd->udev->ref_cv);
311 mtx_unlock(&usb_ref_lock);
312 DPRINTFN(2, "fail\n");
315 memset(crd, 0, sizeof(*crd));
317 return (USB_ERR_INVAL);
320 /*------------------------------------------------------------------------*
323 * This function is used to upgrade an USB reference to include the
324 * USB device reference on a USB location.
327 * 0: Success, refcount incremented on the given USB device.
329 *------------------------------------------------------------------------*/
331 usb_usb_ref_device(struct usb_cdev_privdata *cpd,
332 struct usb_cdev_refdata *crd)
335 * Check if we already got an USB reference on this location:
338 return (0); /* success */
341 * To avoid deadlock at detach we need to drop the FIFO ref
342 * and re-acquire a new ref!
344 usb_unref_device(cpd, crd);
346 return (usb_ref_device(cpd, crd, 1 /* need uref */));
349 /*------------------------------------------------------------------------*
352 * This function will release the reference count by one unit for the
354 *------------------------------------------------------------------------*/
356 usb_unref_device(struct usb_cdev_privdata *cpd,
357 struct usb_cdev_refdata *crd)
360 DPRINTFN(2, "cpd=%p is_uref=%d\n", cpd, crd->is_uref);
363 usbd_enum_unlock(cpd->udev);
365 mtx_lock(&usb_ref_lock);
367 if (--(crd->rxfifo->refcount) == 0) {
368 cv_signal(&crd->rxfifo->cv_drain);
373 if (--(crd->txfifo->refcount) == 0) {
374 cv_signal(&crd->txfifo->cv_drain);
380 if (--(cpd->udev->refcount) == 0)
381 cv_broadcast(&cpd->udev->ref_cv);
383 mtx_unlock(&usb_ref_lock);
386 static struct usb_fifo *
387 usb_fifo_alloc(struct mtx *mtx)
391 f = malloc(sizeof(*f), M_USBDEV, M_WAITOK | M_ZERO);
392 cv_init(&f->cv_io, "FIFO-IO");
393 cv_init(&f->cv_drain, "FIFO-DRAIN");
396 knlist_init_mtx(&f->selinfo.si_note, mtx);
400 /*------------------------------------------------------------------------*
402 *------------------------------------------------------------------------*/
404 usb_fifo_create(struct usb_cdev_privdata *cpd,
405 struct usb_cdev_refdata *crd)
407 struct usb_device *udev = cpd->udev;
409 struct usb_endpoint *ep;
415 int e = cpd->ep_addr;
417 is_tx = (cpd->fflags & FWRITE) ? 1 : 0;
418 is_rx = (cpd->fflags & FREAD) ? 1 : 0;
422 /* Preallocated FIFO */
424 DPRINTFN(5, "Preallocated FIFO\n");
426 f = udev->fifo[cpd->fifo_index + USB_FIFO_TX];
432 f = udev->fifo[cpd->fifo_index + USB_FIFO_RX];
440 KASSERT(e >= 0 && e <= 15, ("endpoint %d out of range", e));
442 /* search for a free FIFO slot */
443 DPRINTFN(5, "Endpoint device, searching for 0x%02x\n", e);
444 for (n = 0;; n += 2) {
445 if (n == USB_FIFO_MAX) {
450 /* end of FIFOs reached */
451 DPRINTFN(5, "out of FIFOs\n");
455 /* Check for TX FIFO */
457 f = udev->fifo[n + USB_FIFO_TX];
459 if (f->dev_ep_index != e) {
460 /* wrong endpoint index */
463 if (f->curr_cpd != NULL) {
468 } else if (no_null) {
472 /* Check for RX FIFO */
474 f = udev->fifo[n + USB_FIFO_RX];
476 if (f->dev_ep_index != e) {
477 /* wrong endpoint index */
480 if (f->curr_cpd != NULL) {
485 } else if (no_null) {
493 if (e >= (USB_EP_MAX / 2)) {
494 /* we don't create any endpoints in this range */
495 DPRINTFN(5, "ep out of range\n");
496 return (is_busy ? EBUSY : EINVAL);
500 if ((e != 0) && is_busy) {
502 * Only the default control endpoint is allowed to be
503 * opened multiple times!
505 DPRINTFN(5, "busy\n");
511 (udev->fifo[n + USB_FIFO_TX] == NULL)) {
512 ep = usb_dev_get_ep(udev, e, USB_FIFO_TX);
513 DPRINTFN(5, "dev_get_endpoint(%d, 0x%x)\n", e, USB_FIFO_TX);
515 DPRINTFN(5, "dev_get_endpoint returned NULL\n");
518 f = usb_fifo_alloc(&udev->device_mtx);
520 DPRINTFN(5, "could not alloc tx fifo\n");
523 /* update some fields */
524 f->fifo_index = n + USB_FIFO_TX;
527 f->methods = &usb_ugen_methods;
528 f->iface_index = ep->iface_index;
530 mtx_lock(&usb_ref_lock);
531 udev->fifo[n + USB_FIFO_TX] = f;
532 mtx_unlock(&usb_ref_lock);
536 (udev->fifo[n + USB_FIFO_RX] == NULL)) {
537 ep = usb_dev_get_ep(udev, e, USB_FIFO_RX);
538 DPRINTFN(5, "dev_get_endpoint(%d, 0x%x)\n", e, USB_FIFO_RX);
540 DPRINTFN(5, "dev_get_endpoint returned NULL\n");
543 f = usb_fifo_alloc(&udev->device_mtx);
545 DPRINTFN(5, "could not alloc rx fifo\n");
548 /* update some fields */
549 f->fifo_index = n + USB_FIFO_RX;
552 f->methods = &usb_ugen_methods;
553 f->iface_index = ep->iface_index;
555 mtx_lock(&usb_ref_lock);
556 udev->fifo[n + USB_FIFO_RX] = f;
557 mtx_unlock(&usb_ref_lock);
560 crd->txfifo = udev->fifo[n + USB_FIFO_TX];
563 crd->rxfifo = udev->fifo[n + USB_FIFO_RX];
565 /* fill out fifo index */
566 DPRINTFN(5, "fifo index = %d\n", n);
575 usb_fifo_free(struct usb_fifo *f)
583 /* destroy symlink devices, if any */
584 for (n = 0; n != 2; n++) {
586 usb_free_symlink(f->symlink[n]);
587 f->symlink[n] = NULL;
590 mtx_lock(&usb_ref_lock);
592 /* delink ourselves to stop calls from userland */
593 if ((f->fifo_index < USB_FIFO_MAX) &&
595 (f->udev->fifo[f->fifo_index] == f)) {
596 f->udev->fifo[f->fifo_index] = NULL;
598 DPRINTFN(0, "USB FIFO %p has not been linked\n", f);
601 /* decrease refcount */
603 /* need to wait until all callers have exited */
604 while (f->refcount != 0) {
605 mtx_unlock(&usb_ref_lock); /* avoid LOR */
606 mtx_lock(f->priv_mtx);
607 /* prevent write flush, if any */
609 /* get I/O thread out of any sleep state */
610 if (f->flag_sleeping) {
611 f->flag_sleeping = 0;
612 cv_broadcast(&f->cv_io);
614 mtx_unlock(f->priv_mtx);
615 mtx_lock(&usb_ref_lock);
618 * Check if the "f->refcount" variable reached zero
619 * during the unlocked time before entering wait:
621 if (f->refcount == 0)
625 cv_wait(&f->cv_drain, &usb_ref_lock);
627 mtx_unlock(&usb_ref_lock);
629 /* take care of closing the device here, if any */
630 usb_fifo_close(f, 0);
632 cv_destroy(&f->cv_io);
633 cv_destroy(&f->cv_drain);
635 knlist_clear(&f->selinfo.si_note, 0);
636 seldrain(&f->selinfo);
637 knlist_destroy(&f->selinfo.si_note);
642 static struct usb_endpoint *
643 usb_dev_get_ep(struct usb_device *udev, uint8_t ep_index, uint8_t dir)
645 struct usb_endpoint *ep;
651 if (dir == USB_FIFO_RX) {
652 if (udev->flags.usb_mode == USB_MODE_HOST) {
658 if (udev->flags.usb_mode == USB_MODE_HOST) {
664 ep = usbd_get_ep_by_addr(udev, ep_index | ep_dir);
668 /* if the endpoint does not exist then return */
671 if (ep->edesc == NULL) {
672 /* invalid endpoint */
675 return (ep); /* success */
678 /*------------------------------------------------------------------------*
684 *------------------------------------------------------------------------*/
686 usb_fifo_open(struct usb_cdev_privdata *cpd,
687 struct usb_fifo *f, int fflags)
693 DPRINTFN(2, "no FIFO\n");
696 /* remove FWRITE and FREAD flags */
697 fflags &= ~(FWRITE | FREAD);
699 /* set correct file flags */
700 if ((f->fifo_index & 1) == USB_FIFO_TX) {
706 /* check if we are already opened */
707 /* we don't need any locks when checking this variable */
708 if (f->curr_cpd != NULL) {
713 /* reset short flag before open */
716 /* call open method */
717 err = (f->methods->f_open) (f, fflags);
721 mtx_lock(f->priv_mtx);
723 /* reset sleep flag */
724 f->flag_sleeping = 0;
726 /* reset error flag */
729 /* reset complete flag */
730 f->flag_iscomplete = 0;
732 /* reset select flag */
733 f->flag_isselect = 0;
735 /* reset flushing flag */
736 f->flag_flushing = 0;
738 /* reset ASYNC proc flag */
741 mtx_lock(&usb_ref_lock);
742 /* flag the fifo as opened to prevent others */
744 mtx_unlock(&usb_ref_lock);
749 mtx_unlock(f->priv_mtx);
754 /*------------------------------------------------------------------------*
756 *------------------------------------------------------------------------*/
758 usb_fifo_reset(struct usb_fifo *f)
766 USB_IF_DEQUEUE(&f->used_q, m);
768 USB_IF_ENQUEUE(&f->free_q, m);
773 /* reset have fragment flag */
774 f->flag_have_fragment = 0;
777 /*------------------------------------------------------------------------*
779 *------------------------------------------------------------------------*/
781 usb_fifo_close(struct usb_fifo *f, int fflags)
785 /* check if we are not opened */
786 if (f->curr_cpd == NULL) {
787 /* nothing to do - already closed */
790 mtx_lock(f->priv_mtx);
792 /* clear current cdev private data pointer */
793 mtx_lock(&usb_ref_lock);
795 mtx_unlock(&usb_ref_lock);
797 /* check if we are watched by kevent */
798 KNOTE_LOCKED(&f->selinfo.si_note, 0);
800 /* check if we are selected */
801 if (f->flag_isselect) {
802 selwakeup(&f->selinfo);
803 f->flag_isselect = 0;
805 /* check if a thread wants SIGIO */
806 if (f->async_p != NULL) {
807 PROC_LOCK(f->async_p);
808 kern_psignal(f->async_p, SIGIO);
809 PROC_UNLOCK(f->async_p);
812 /* remove FWRITE and FREAD flags */
813 fflags &= ~(FWRITE | FREAD);
815 /* flush written data, if any */
816 if ((f->fifo_index & 1) == USB_FIFO_TX) {
817 if (!f->flag_iserror) {
818 /* set flushing flag */
819 f->flag_flushing = 1;
821 /* get the last packet in */
822 if (f->flag_have_fragment) {
824 f->flag_have_fragment = 0;
825 USB_IF_DEQUEUE(&f->free_q, m);
827 USB_IF_ENQUEUE(&f->used_q, m);
831 /* start write transfer, if not already started */
832 (f->methods->f_start_write) (f);
834 /* check if flushed already */
835 while (f->flag_flushing &&
836 (!f->flag_iserror)) {
837 /* wait until all data has been written */
838 f->flag_sleeping = 1;
839 err = cv_timedwait_sig(&f->cv_io, f->priv_mtx,
840 USB_MS_TO_TICKS(USB_DEFAULT_TIMEOUT));
842 DPRINTF("signal received\n");
849 /* stop write transfer, if not already stopped */
850 (f->methods->f_stop_write) (f);
854 /* stop write transfer, if not already stopped */
855 (f->methods->f_stop_read) (f);
858 /* check if we are sleeping */
859 if (f->flag_sleeping) {
860 DPRINTFN(2, "Sleeping at close!\n");
862 mtx_unlock(f->priv_mtx);
864 /* call close method */
865 (f->methods->f_close) (f, fflags);
870 /*------------------------------------------------------------------------*
871 * usb_open - cdev callback
872 *------------------------------------------------------------------------*/
874 usb_open(struct cdev *dev, int fflags, int devtype, struct thread *td)
876 struct usb_fs_privdata* pd = (struct usb_fs_privdata*)dev->si_drv1;
877 struct usb_cdev_refdata refs;
878 struct usb_cdev_privdata *cpd;
881 DPRINTFN(2, "%s fflags=0x%08x\n", devtoname(dev), fflags);
883 KASSERT(fflags & (FREAD|FWRITE), ("invalid open flags"));
884 if (((fflags & FREAD) && !(pd->mode & FREAD)) ||
885 ((fflags & FWRITE) && !(pd->mode & FWRITE))) {
886 DPRINTFN(2, "access mode not supported\n");
890 cpd = malloc(sizeof(*cpd), M_USBDEV, M_WAITOK | M_ZERO);
892 usb_loc_fill(pd, cpd);
893 err = usb_ref_device(cpd, &refs, 1);
895 DPRINTFN(2, "cannot ref device\n");
899 cpd->fflags = fflags; /* access mode for open lifetime */
901 /* create FIFOs, if any */
902 err = usb_fifo_create(cpd, &refs);
903 /* check for error */
905 DPRINTFN(2, "cannot create fifo\n");
906 usb_unref_device(cpd, &refs);
910 if (fflags & FREAD) {
911 err = usb_fifo_open(cpd, refs.rxfifo, fflags);
913 DPRINTFN(2, "read open failed\n");
914 usb_unref_device(cpd, &refs);
919 if (fflags & FWRITE) {
920 err = usb_fifo_open(cpd, refs.txfifo, fflags);
922 DPRINTFN(2, "write open failed\n");
923 if (fflags & FREAD) {
924 usb_fifo_close(refs.rxfifo, fflags);
926 usb_unref_device(cpd, &refs);
931 usb_unref_device(cpd, &refs);
932 devfs_set_cdevpriv(cpd, usb_close);
937 /*------------------------------------------------------------------------*
938 * usb_close - cdev callback
939 *------------------------------------------------------------------------*/
943 struct usb_cdev_refdata refs;
944 struct usb_cdev_privdata *cpd = arg;
947 DPRINTFN(2, "cpd=%p\n", cpd);
949 err = usb_ref_device(cpd, &refs,
950 2 /* uref and allow detached state */);
952 DPRINTFN(2, "Cannot grab USB reference when "
953 "closing USB file handle\n");
956 if (cpd->fflags & FREAD) {
957 usb_fifo_close(refs.rxfifo, cpd->fflags);
959 if (cpd->fflags & FWRITE) {
960 usb_fifo_close(refs.txfifo, cpd->fflags);
962 usb_unref_device(cpd, &refs);
968 usb_dev_init(void *arg)
970 mtx_init(&usb_ref_lock, "USB ref mutex", NULL, MTX_DEF);
971 sx_init(&usb_sym_lock, "USB sym mutex");
972 TAILQ_INIT(&usb_sym_head);
974 /* check the UGEN methods */
975 usb_fifo_check_methods(&usb_ugen_methods);
978 SYSINIT(usb_dev_init, SI_SUB_KLD, SI_ORDER_FIRST, usb_dev_init, NULL);
981 usb_dev_init_post(void *arg)
984 * Create /dev/usb - this is needed for usbconfig(8), which
985 * needs a well-known device name to access.
987 usb_dev = make_dev(&usb_static_devsw, 0, UID_ROOT, GID_OPERATOR,
988 0644, USB_DEVICE_NAME);
989 if (usb_dev == NULL) {
990 DPRINTFN(0, "Could not create usb bus device\n");
994 SYSINIT(usb_dev_init_post, SI_SUB_KICK_SCHEDULER, SI_ORDER_FIRST, usb_dev_init_post, NULL);
997 usb_dev_uninit(void *arg)
999 if (usb_dev != NULL) {
1000 destroy_dev(usb_dev);
1003 mtx_destroy(&usb_ref_lock);
1004 sx_destroy(&usb_sym_lock);
1007 SYSUNINIT(usb_dev_uninit, SI_SUB_KICK_SCHEDULER, SI_ORDER_ANY, usb_dev_uninit, NULL);
1010 usb_ioctl_f_sub(struct usb_fifo *f, u_long cmd, void *addr,
1017 *(int *)addr = 0; /* character device */
1021 /* handled by upper FS layer */
1026 if (f->async_p != NULL) {
1030 f->async_p = USB_TD_GET_PROC(td);
1036 /* XXX this is not the most general solution */
1038 if (f->async_p == NULL) {
1042 if (*(int *)addr != USB_PROC_GET_GID(f->async_p)) {
1050 DPRINTFN(3, "cmd 0x%lx = %d\n", cmd, error);
1054 /*------------------------------------------------------------------------*
1055 * usb_ioctl - cdev callback
1056 *------------------------------------------------------------------------*/
1058 usb_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int fflag, struct thread* td)
1060 struct usb_cdev_refdata refs;
1061 struct usb_cdev_privdata* cpd;
1066 DPRINTFN(2, "cmd=0x%lx\n", cmd);
1068 err = devfs_get_cdevpriv((void **)&cpd);
1073 * Performance optimisation: We try to check for IOCTL's that
1074 * don't need the USB reference first. Then we grab the USB
1075 * reference if we need it!
1077 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1081 fflags = cpd->fflags;
1083 f = NULL; /* set default value */
1084 err = ENOIOCTL; /* set default value */
1086 if (fflags & FWRITE) {
1088 err = usb_ioctl_f_sub(f, cmd, addr, td);
1090 if (fflags & FREAD) {
1092 err = usb_ioctl_f_sub(f, cmd, addr, td);
1094 KASSERT(f != NULL, ("fifo not found"));
1095 if (err != ENOIOCTL)
1098 err = (f->methods->f_ioctl) (f, cmd, addr, fflags);
1100 DPRINTFN(2, "f_ioctl cmd 0x%lx = %d\n", cmd, err);
1102 if (err != ENOIOCTL)
1105 if (usb_usb_ref_device(cpd, &refs)) {
1106 /* we lost the reference */
1110 err = (f->methods->f_ioctl_post) (f, cmd, addr, fflags);
1112 DPRINTFN(2, "f_ioctl_post cmd 0x%lx = %d\n", cmd, err);
1114 if (err == ENOIOCTL)
1120 /* Wait for re-enumeration, if any */
1122 while (f->udev->re_enumerate_wait != USB_RE_ENUM_DONE) {
1123 usb_unref_device(cpd, &refs);
1125 usb_pause_mtx(NULL, hz / 128);
1127 while (usb_ref_device(cpd, &refs, 1 /* need uref */)) {
1128 if (usb_ref_device(cpd, &refs, 0)) {
1129 /* device no longer exists */
1132 usb_unref_device(cpd, &refs);
1133 usb_pause_mtx(NULL, hz / 128);
1138 usb_unref_device(cpd, &refs);
1143 usb_filter_detach(struct knote *kn)
1145 struct usb_fifo *f = kn->kn_hook;
1146 knlist_remove(&f->selinfo.si_note, kn, 0);
1150 usb_filter_write(struct knote *kn, long hint)
1152 struct usb_cdev_privdata* cpd;
1160 USB_MTX_ASSERT(f->priv_mtx, MA_OWNED);
1165 } else if (f->fs_ep_max == 0) {
1166 if (f->flag_iserror) {
1167 /* we got an error */
1170 if (f->queue_data == NULL) {
1172 * start write transfer, if not
1175 (f->methods->f_start_write) (f);
1177 /* check if any packets are available */
1178 USB_IF_POLL(&f->free_q, m);
1181 if (f->flag_iscomplete) {
1191 usb_filter_read(struct knote *kn, long hint)
1193 struct usb_cdev_privdata* cpd;
1201 USB_MTX_ASSERT(f->priv_mtx, MA_OWNED);
1206 } else if (f->fs_ep_max == 0) {
1207 if (f->flag_iserror) {
1208 /* we have an error */
1211 if (f->queue_data == NULL) {
1213 * start read transfer, if not
1216 (f->methods->f_start_read) (f);
1218 /* check if any packets are available */
1219 USB_IF_POLL(&f->used_q, m);
1221 /* start reading data, if any */
1223 (f->methods->f_start_read) (f);
1226 if (f->flag_iscomplete) {
1235 static struct filterops usb_filtops_write = {
1237 .f_detach = usb_filter_detach,
1238 .f_event = usb_filter_write,
1241 static struct filterops usb_filtops_read = {
1243 .f_detach = usb_filter_detach,
1244 .f_event = usb_filter_read,
1249 usb_kqfilter(struct cdev* dev, struct knote *kn)
1251 struct usb_cdev_refdata refs;
1252 struct usb_cdev_privdata* cpd;
1259 if (devfs_get_cdevpriv((void **)&cpd) != 0 ||
1260 usb_ref_device(cpd, &refs, 0) != 0)
1263 fflags = cpd->fflags;
1265 /* Figure out who needs service */
1266 switch (kn->kn_filter) {
1268 if (fflags & FWRITE) {
1270 kn->kn_fop = &usb_filtops_write;
1275 if (fflags & FREAD) {
1277 kn->kn_fop = &usb_filtops_read;
1288 mtx_lock(f->priv_mtx);
1289 knlist_add(&f->selinfo.si_note, kn, 1);
1290 mtx_unlock(f->priv_mtx);
1293 usb_unref_device(cpd, &refs);
1299 usb_poll(struct cdev* dev, int events, struct thread* td)
1301 struct usb_cdev_refdata refs;
1302 struct usb_cdev_privdata* cpd;
1305 int fflags, revents;
1307 if (devfs_get_cdevpriv((void **)&cpd) != 0 ||
1308 usb_ref_device(cpd, &refs, 0) != 0)
1310 (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM));
1312 fflags = cpd->fflags;
1314 /* Figure out who needs service */
1316 if ((events & (POLLOUT | POLLWRNORM)) &&
1317 (fflags & FWRITE)) {
1320 mtx_lock(f->priv_mtx);
1322 if (!refs.is_usbfs) {
1323 if (f->flag_iserror) {
1324 /* we got an error */
1327 if (f->queue_data == NULL) {
1329 * start write transfer, if not
1332 (f->methods->f_start_write) (f);
1334 /* check if any packets are available */
1335 USB_IF_POLL(&f->free_q, m);
1338 if (f->flag_iscomplete) {
1346 revents |= events & (POLLOUT | POLLWRNORM);
1348 f->flag_isselect = 1;
1349 selrecord(td, &f->selinfo);
1352 mtx_unlock(f->priv_mtx);
1354 if ((events & (POLLIN | POLLRDNORM)) &&
1358 mtx_lock(f->priv_mtx);
1360 if (!refs.is_usbfs) {
1361 if (f->flag_iserror) {
1362 /* we have an error */
1365 if (f->queue_data == NULL) {
1367 * start read transfer, if not
1370 (f->methods->f_start_read) (f);
1372 /* check if any packets are available */
1373 USB_IF_POLL(&f->used_q, m);
1376 if (f->flag_iscomplete) {
1384 revents |= events & (POLLIN | POLLRDNORM);
1386 f->flag_isselect = 1;
1387 selrecord(td, &f->selinfo);
1389 if (!refs.is_usbfs) {
1390 /* start reading data */
1391 (f->methods->f_start_read) (f);
1395 mtx_unlock(f->priv_mtx);
1397 usb_unref_device(cpd, &refs);
1402 usb_read(struct cdev *dev, struct uio *uio, int ioflag)
1404 struct usb_cdev_refdata refs;
1405 struct usb_cdev_privdata* cpd;
1410 uint8_t tr_data = 0;
1412 err = devfs_get_cdevpriv((void **)&cpd);
1416 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1422 /* should not happen */
1423 usb_unref_device(cpd, &refs);
1427 mtx_lock(f->priv_mtx);
1429 /* check for permanent read error */
1430 if (f->flag_iserror) {
1434 /* check if USB-FS interface is active */
1435 if (refs.is_usbfs) {
1437 * The queue is used for events that should be
1438 * retrieved using the "USB_FS_COMPLETE" ioctl.
1443 while (uio->uio_resid > 0) {
1444 USB_IF_DEQUEUE(&f->used_q, m);
1447 /* start read transfer, if not already started */
1449 (f->methods->f_start_read) (f);
1451 if (ioflag & IO_NDELAY) {
1453 /* return length before error */
1459 DPRINTF("sleeping\n");
1461 err = usb_fifo_wait(f);
1467 if (f->methods->f_filter_read) {
1469 * Sometimes it is convenient to process data at the
1470 * expense of a userland process instead of a kernel
1473 (f->methods->f_filter_read) (f, m);
1477 io_len = MIN(m->cur_data_len, uio->uio_resid);
1479 DPRINTFN(2, "transfer %d bytes from %p\n",
1480 io_len, m->cur_data_ptr);
1482 err = usb_fifo_uiomove(f,
1483 m->cur_data_ptr, io_len, uio);
1485 m->cur_data_len -= io_len;
1486 m->cur_data_ptr += io_len;
1488 if (m->cur_data_len == 0) {
1489 uint8_t last_packet;
1491 last_packet = m->last_packet;
1493 USB_IF_ENQUEUE(&f->free_q, m);
1500 USB_IF_PREPEND(&f->used_q, m);
1508 mtx_unlock(f->priv_mtx);
1510 usb_unref_device(cpd, &refs);
1516 usb_write(struct cdev *dev, struct uio *uio, int ioflag)
1518 struct usb_cdev_refdata refs;
1519 struct usb_cdev_privdata* cpd;
1525 uint8_t tr_data = 0;
1529 err = devfs_get_cdevpriv((void **)&cpd);
1533 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1539 /* should not happen */
1540 usb_unref_device(cpd, &refs);
1544 mtx_lock(f->priv_mtx);
1546 /* check for permanent write error */
1547 if (f->flag_iserror) {
1551 /* check if USB-FS interface is active */
1552 if (refs.is_usbfs) {
1554 * The queue is used for events that should be
1555 * retrieved using the "USB_FS_COMPLETE" ioctl.
1560 if (f->queue_data == NULL) {
1561 /* start write transfer, if not already started */
1562 (f->methods->f_start_write) (f);
1564 /* we allow writing zero length data */
1566 USB_IF_DEQUEUE(&f->free_q, m);
1569 if (ioflag & IO_NDELAY) {
1571 /* return length before error */
1577 DPRINTF("sleeping\n");
1579 err = usb_fifo_wait(f);
1587 if (f->flag_have_fragment == 0) {
1589 io_len = m->cur_data_len;
1590 pdata = m->cur_data_ptr;
1591 if (io_len > uio->uio_resid)
1592 io_len = uio->uio_resid;
1593 m->cur_data_len = io_len;
1595 io_len = m->max_data_len - m->cur_data_len;
1596 pdata = m->cur_data_ptr + m->cur_data_len;
1597 if (io_len > uio->uio_resid)
1598 io_len = uio->uio_resid;
1599 m->cur_data_len += io_len;
1602 DPRINTFN(2, "transfer %d bytes to %p\n",
1605 err = usb_fifo_uiomove(f, pdata, io_len, uio);
1608 f->flag_have_fragment = 0;
1609 USB_IF_ENQUEUE(&f->free_q, m);
1613 /* check if the buffer is ready to be transmitted */
1615 if ((f->flag_write_defrag == 0) ||
1616 (m->cur_data_len == m->max_data_len)) {
1617 f->flag_have_fragment = 0;
1620 * Check for write filter:
1622 * Sometimes it is convenient to process data
1623 * at the expense of a userland process
1624 * instead of a kernel process.
1626 if (f->methods->f_filter_write) {
1627 (f->methods->f_filter_write) (f, m);
1630 /* Put USB mbuf in the used queue */
1631 USB_IF_ENQUEUE(&f->used_q, m);
1633 /* Start writing data, if not already started */
1634 (f->methods->f_start_write) (f);
1636 /* Wait for more data or close */
1637 f->flag_have_fragment = 1;
1638 USB_IF_PREPEND(&f->free_q, m);
1641 } while (uio->uio_resid > 0);
1643 mtx_unlock(f->priv_mtx);
1645 usb_unref_device(cpd, &refs);
1651 usb_static_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
1655 struct usb_read_dir *urd;
1656 #ifdef COMPAT_FREEBSD32
1657 struct usb_read_dir32 *urd32;
1666 err = usb_read_symlink(u.urd->urd_data,
1667 u.urd->urd_startentry, u.urd->urd_maxlen);
1669 #ifdef COMPAT_FREEBSD32
1670 case USB_READ_DIR32:
1671 err = usb_read_symlink(PTRIN(u.urd32->urd_data),
1672 u.urd32->urd_startentry, u.urd32->urd_maxlen);
1675 case USB_DEV_QUIRK_GET:
1676 case USB_QUIRK_NAME_GET:
1677 case USB_DEV_QUIRK_ADD:
1678 case USB_DEV_QUIRK_REMOVE:
1679 err = usb_quirk_ioctl_p(cmd, data, fflag, td);
1681 case USB_GET_TEMPLATE:
1682 *(int *)data = usb_template;
1685 case USB_SET_TEMPLATE:
1686 err = priv_check(curthread, PRIV_DRIVER);
1689 usb_template = *(int *)data;
1699 usb_fifo_uiomove(struct usb_fifo *f, void *cp,
1700 int n, struct uio *uio)
1704 mtx_unlock(f->priv_mtx);
1707 * "uiomove()" can sleep so one needs to make a wrapper,
1708 * exiting the mutex and checking things:
1710 error = uiomove(cp, n, uio);
1712 mtx_lock(f->priv_mtx);
1718 usb_fifo_wait(struct usb_fifo *f)
1722 USB_MTX_ASSERT(f->priv_mtx, MA_OWNED);
1724 if (f->flag_iserror) {
1728 f->flag_sleeping = 1;
1730 err = cv_wait_sig(&f->cv_io, f->priv_mtx);
1732 if (f->flag_iserror) {
1740 usb_fifo_signal(struct usb_fifo *f)
1742 if (f->flag_sleeping) {
1743 f->flag_sleeping = 0;
1744 cv_broadcast(&f->cv_io);
1749 usb_fifo_wakeup(struct usb_fifo *f)
1753 KNOTE_LOCKED(&f->selinfo.si_note, 0);
1755 if (f->flag_isselect) {
1756 selwakeup(&f->selinfo);
1757 f->flag_isselect = 0;
1759 if (f->async_p != NULL) {
1760 PROC_LOCK(f->async_p);
1761 kern_psignal(f->async_p, SIGIO);
1762 PROC_UNLOCK(f->async_p);
1767 usb_fifo_dummy_open(struct usb_fifo *fifo, int fflags)
1773 usb_fifo_dummy_close(struct usb_fifo *fifo, int fflags)
1779 usb_fifo_dummy_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
1785 usb_fifo_dummy_cmd(struct usb_fifo *fifo)
1787 fifo->flag_flushing = 0; /* not flushing */
1791 usb_fifo_check_methods(struct usb_fifo_methods *pm)
1793 /* check that all callback functions are OK */
1795 if (pm->f_open == NULL)
1796 pm->f_open = &usb_fifo_dummy_open;
1798 if (pm->f_close == NULL)
1799 pm->f_close = &usb_fifo_dummy_close;
1801 if (pm->f_ioctl == NULL)
1802 pm->f_ioctl = &usb_fifo_dummy_ioctl;
1804 if (pm->f_ioctl_post == NULL)
1805 pm->f_ioctl_post = &usb_fifo_dummy_ioctl;
1807 if (pm->f_start_read == NULL)
1808 pm->f_start_read = &usb_fifo_dummy_cmd;
1810 if (pm->f_stop_read == NULL)
1811 pm->f_stop_read = &usb_fifo_dummy_cmd;
1813 if (pm->f_start_write == NULL)
1814 pm->f_start_write = &usb_fifo_dummy_cmd;
1816 if (pm->f_stop_write == NULL)
1817 pm->f_stop_write = &usb_fifo_dummy_cmd;
1820 /*------------------------------------------------------------------------*
1823 * The following function will create a duplex FIFO.
1828 *------------------------------------------------------------------------*/
1830 usb_fifo_attach(struct usb_device *udev, void *priv_sc,
1831 struct mtx *priv_mtx, struct usb_fifo_methods *pm,
1832 struct usb_fifo_sc *f_sc, uint16_t unit, int16_t subunit,
1833 uint8_t iface_index, uid_t uid, gid_t gid, int mode)
1835 struct usb_fifo *f_tx;
1836 struct usb_fifo *f_rx;
1840 f_sc->fp[USB_FIFO_TX] = NULL;
1841 f_sc->fp[USB_FIFO_RX] = NULL;
1846 /* check the methods */
1847 usb_fifo_check_methods(pm);
1849 if (priv_mtx == NULL)
1852 /* search for a free FIFO slot */
1853 for (n = 0;; n += 2) {
1854 if (n == USB_FIFO_MAX) {
1855 /* end of FIFOs reached */
1858 /* Check for TX FIFO */
1859 if (udev->fifo[n + USB_FIFO_TX] != NULL) {
1862 /* Check for RX FIFO */
1863 if (udev->fifo[n + USB_FIFO_RX] != NULL) {
1869 f_tx = usb_fifo_alloc(priv_mtx);
1870 f_rx = usb_fifo_alloc(priv_mtx);
1872 if ((f_tx == NULL) || (f_rx == NULL)) {
1873 usb_fifo_free(f_tx);
1874 usb_fifo_free(f_rx);
1877 /* initialise FIFO structures */
1879 f_tx->fifo_index = n + USB_FIFO_TX;
1880 f_tx->dev_ep_index = -1;
1881 f_tx->priv_sc0 = priv_sc;
1883 f_tx->iface_index = iface_index;
1886 f_rx->fifo_index = n + USB_FIFO_RX;
1887 f_rx->dev_ep_index = -1;
1888 f_rx->priv_sc0 = priv_sc;
1890 f_rx->iface_index = iface_index;
1893 f_sc->fp[USB_FIFO_TX] = f_tx;
1894 f_sc->fp[USB_FIFO_RX] = f_rx;
1896 mtx_lock(&usb_ref_lock);
1897 udev->fifo[f_tx->fifo_index] = f_tx;
1898 udev->fifo[f_rx->fifo_index] = f_rx;
1899 mtx_unlock(&usb_ref_lock);
1901 for (n = 0; n != 4; n++) {
1902 if (pm->basename[n] == NULL) {
1906 if (snprintf(devname, sizeof(devname),
1907 "%s%u%s", pm->basename[n],
1908 unit, pm->postfix[n] ?
1909 pm->postfix[n] : "")) {
1913 if (snprintf(devname, sizeof(devname),
1914 "%s%u.%d%s", pm->basename[n],
1915 unit, subunit, pm->postfix[n] ?
1916 pm->postfix[n] : "")) {
1922 * Distribute the symbolic links into two FIFO structures:
1925 f_rx->symlink[n / 2] =
1926 usb_alloc_symlink(devname);
1928 f_tx->symlink[n / 2] =
1929 usb_alloc_symlink(devname);
1932 /* Create the device */
1933 f_sc->dev = usb_make_dev(udev, devname, -1,
1934 f_tx->fifo_index & f_rx->fifo_index,
1935 FREAD|FWRITE, uid, gid, mode);
1938 DPRINTFN(2, "attached %p/%p\n", f_tx, f_rx);
1942 /*------------------------------------------------------------------------*
1943 * usb_fifo_alloc_buffer
1948 *------------------------------------------------------------------------*/
1950 usb_fifo_alloc_buffer(struct usb_fifo *f, usb_size_t bufsize,
1953 struct usb_ifqueue temp_q = {};
1956 usb_fifo_free_buffer(f);
1958 temp_q.ifq_maxlen = nbuf;
1960 queue_data = usb_alloc_mbufs(
1961 M_USBDEV, &temp_q, bufsize, nbuf);
1963 if (queue_data == NULL && bufsize != 0 && nbuf != 0)
1966 mtx_lock(f->priv_mtx);
1969 * Setup queues and sizes under lock to avoid early use by
1970 * concurrent FIFO access:
1973 f->used_q.ifq_maxlen = nbuf;
1974 f->queue_data = queue_data;
1975 mtx_unlock(f->priv_mtx);
1977 return (0); /* success */
1980 /*------------------------------------------------------------------------*
1981 * usb_fifo_free_buffer
1983 * This function will free the buffers associated with a FIFO. This
1984 * function can be called multiple times in a row.
1985 *------------------------------------------------------------------------*/
1987 usb_fifo_free_buffer(struct usb_fifo *f)
1991 mtx_lock(f->priv_mtx);
1993 /* Get and clear pointer to free, if any. */
1994 queue_data = f->queue_data;
1995 f->queue_data = NULL;
1998 * Reset queues under lock to avoid use of freed buffers by
1999 * concurrent FIFO activity:
2001 memset(&f->free_q, 0, sizeof(f->free_q));
2002 memset(&f->used_q, 0, sizeof(f->used_q));
2003 mtx_unlock(f->priv_mtx);
2005 /* Free old buffer, if any. */
2006 free(queue_data, M_USBDEV);
2010 usb_fifo_detach(struct usb_fifo_sc *f_sc)
2015 usb_fifo_free(f_sc->fp[USB_FIFO_TX]);
2016 usb_fifo_free(f_sc->fp[USB_FIFO_RX]);
2018 f_sc->fp[USB_FIFO_TX] = NULL;
2019 f_sc->fp[USB_FIFO_RX] = NULL;
2021 usb_destroy_dev(f_sc->dev);
2025 DPRINTFN(2, "detached %p\n", f_sc);
2029 usb_fifo_put_bytes_max(struct usb_fifo *f)
2034 USB_IF_POLL(&f->free_q, m);
2037 len = m->max_data_len;
2044 /*------------------------------------------------------------------------*
2048 * 0 - normal operation
2049 * 1 - set last packet flag to enforce framing
2050 *------------------------------------------------------------------------*/
2052 usb_fifo_put_data(struct usb_fifo *f, struct usb_page_cache *pc,
2053 usb_frlength_t offset, usb_frlength_t len, uint8_t what)
2056 usb_frlength_t io_len;
2058 while (len || (what == 1)) {
2059 USB_IF_DEQUEUE(&f->free_q, m);
2064 io_len = MIN(len, m->cur_data_len);
2066 usbd_copy_out(pc, offset, m->cur_data_ptr, io_len);
2068 m->cur_data_len = io_len;
2072 if ((len == 0) && (what == 1)) {
2075 USB_IF_ENQUEUE(&f->used_q, m);
2079 if ((len == 0) || (what == 1)) {
2089 usb_fifo_put_data_linear(struct usb_fifo *f, void *ptr,
2090 usb_size_t len, uint8_t what)
2095 while (len || (what == 1)) {
2096 USB_IF_DEQUEUE(&f->free_q, m);
2101 io_len = MIN(len, m->cur_data_len);
2103 memcpy(m->cur_data_ptr, ptr, io_len);
2105 m->cur_data_len = io_len;
2106 ptr = USB_ADD_BYTES(ptr, io_len);
2109 if ((len == 0) && (what == 1)) {
2112 USB_IF_ENQUEUE(&f->used_q, m);
2116 if ((len == 0) || (what == 1)) {
2126 usb_fifo_put_data_buffer(struct usb_fifo *f, void *ptr, usb_size_t len)
2130 USB_IF_DEQUEUE(&f->free_q, m);
2133 m->cur_data_len = len;
2134 m->cur_data_ptr = ptr;
2135 USB_IF_ENQUEUE(&f->used_q, m);
2143 usb_fifo_put_data_error(struct usb_fifo *f)
2145 f->flag_iserror = 1;
2149 /*------------------------------------------------------------------------*
2153 * 0 - normal operation
2154 * 1 - only get one "usb_mbuf"
2158 * 1 - data in buffer
2159 *------------------------------------------------------------------------*/
2161 usb_fifo_get_data(struct usb_fifo *f, struct usb_page_cache *pc,
2162 usb_frlength_t offset, usb_frlength_t len, usb_frlength_t *actlen,
2166 usb_frlength_t io_len;
2167 uint8_t tr_data = 0;
2172 USB_IF_DEQUEUE(&f->used_q, m);
2177 io_len = MIN(len, m->cur_data_len);
2179 usbd_copy_in(pc, offset, m->cur_data_ptr, io_len);
2183 actlen[0] += io_len;
2184 m->cur_data_ptr += io_len;
2185 m->cur_data_len -= io_len;
2187 if ((m->cur_data_len == 0) || (what == 1)) {
2188 USB_IF_ENQUEUE(&f->free_q, m);
2196 USB_IF_PREPEND(&f->used_q, m);
2200 /* wait for data to be written out */
2203 if (f->flag_flushing) {
2204 /* check if we should send a short packet */
2205 if (f->flag_short != 0) {
2210 /* flushing complete */
2211 f->flag_flushing = 0;
2224 usb_fifo_get_data_linear(struct usb_fifo *f, void *ptr,
2225 usb_size_t len, usb_size_t *actlen, uint8_t what)
2229 uint8_t tr_data = 0;
2234 USB_IF_DEQUEUE(&f->used_q, m);
2239 io_len = MIN(len, m->cur_data_len);
2241 memcpy(ptr, m->cur_data_ptr, io_len);
2244 ptr = USB_ADD_BYTES(ptr, io_len);
2245 actlen[0] += io_len;
2246 m->cur_data_ptr += io_len;
2247 m->cur_data_len -= io_len;
2249 if ((m->cur_data_len == 0) || (what == 1)) {
2250 USB_IF_ENQUEUE(&f->free_q, m);
2258 USB_IF_PREPEND(&f->used_q, m);
2262 /* wait for data to be written out */
2265 if (f->flag_flushing) {
2266 /* check if we should send a short packet */
2267 if (f->flag_short != 0) {
2272 /* flushing complete */
2273 f->flag_flushing = 0;
2286 usb_fifo_get_data_buffer(struct usb_fifo *f, void **pptr, usb_size_t *plen)
2290 USB_IF_POLL(&f->used_q, m);
2293 *plen = m->cur_data_len;
2294 *pptr = m->cur_data_ptr;
2302 usb_fifo_get_data_error(struct usb_fifo *f)
2304 f->flag_iserror = 1;
2308 /*------------------------------------------------------------------------*
2313 * Else: Pointer to symlink entry
2314 *------------------------------------------------------------------------*/
2315 struct usb_symlink *
2316 usb_alloc_symlink(const char *target)
2318 struct usb_symlink *ps;
2320 ps = malloc(sizeof(*ps), M_USBDEV, M_WAITOK);
2321 /* XXX no longer needed */
2322 strlcpy(ps->src_path, target, sizeof(ps->src_path));
2323 ps->src_len = strlen(ps->src_path);
2324 strlcpy(ps->dst_path, target, sizeof(ps->dst_path));
2325 ps->dst_len = strlen(ps->dst_path);
2327 sx_xlock(&usb_sym_lock);
2328 TAILQ_INSERT_TAIL(&usb_sym_head, ps, sym_entry);
2329 sx_unlock(&usb_sym_lock);
2333 /*------------------------------------------------------------------------*
2335 *------------------------------------------------------------------------*/
2337 usb_free_symlink(struct usb_symlink *ps)
2342 sx_xlock(&usb_sym_lock);
2343 TAILQ_REMOVE(&usb_sym_head, ps, sym_entry);
2344 sx_unlock(&usb_sym_lock);
2349 /*------------------------------------------------------------------------*
2355 *------------------------------------------------------------------------*/
2357 usb_read_symlink(uint8_t *user_ptr, uint32_t startentry, uint32_t user_len)
2359 struct usb_symlink *ps;
2365 sx_xlock(&usb_sym_lock);
2367 TAILQ_FOREACH(ps, &usb_sym_head, sym_entry) {
2369 * Compute total length of source and destination symlink
2370 * strings pluss one length byte and two NUL bytes:
2372 temp = ps->src_len + ps->dst_len + 3;
2376 * Skip entry because this length cannot fit
2381 if (startentry != 0) {
2382 /* decrement read offset */
2386 if (temp > user_len) {
2387 /* out of buffer space */
2392 /* copy out total length */
2394 error = copyout(&len,
2395 USB_ADD_BYTES(user_ptr, delta), 1);
2401 /* copy out source string */
2403 error = copyout(ps->src_path,
2404 USB_ADD_BYTES(user_ptr, delta), ps->src_len);
2409 delta += ps->src_len;
2410 error = copyout(&len,
2411 USB_ADD_BYTES(user_ptr, delta), 1);
2417 /* copy out destination string */
2419 error = copyout(ps->dst_path,
2420 USB_ADD_BYTES(user_ptr, delta), ps->dst_len);
2425 delta += ps->dst_len;
2426 error = copyout(&len,
2427 USB_ADD_BYTES(user_ptr, delta), 1);
2436 /* a zero length entry indicates the end */
2438 if ((user_len != 0) && (error == 0)) {
2441 error = copyout(&len,
2442 USB_ADD_BYTES(user_ptr, delta), 1);
2444 sx_unlock(&usb_sym_lock);
2449 usb_fifo_set_close_zlp(struct usb_fifo *f, uint8_t onoff)
2454 /* send a Zero Length Packet, ZLP, before close */
2455 f->flag_short = onoff;
2459 usb_fifo_set_write_defrag(struct usb_fifo *f, uint8_t onoff)
2464 /* defrag written data */
2465 f->flag_write_defrag = onoff;
2466 /* reset defrag state */
2467 f->flag_have_fragment = 0;
2471 usb_fifo_softc(struct usb_fifo *f)
2473 return (f->priv_sc0);
2475 #endif /* USB_HAVE_UGEN */