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 #include <sys/stdint.h>
36 #include <sys/stddef.h>
37 #include <sys/param.h>
38 #include <sys/queue.h>
39 #include <sys/types.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
43 #include <sys/module.h>
45 #include <sys/mutex.h>
46 #include <sys/condvar.h>
47 #include <sys/sysctl.h>
49 #include <sys/unistd.h>
50 #include <sys/callout.h>
51 #include <sys/malloc.h>
53 #include <sys/vnode.h>
55 #include <sys/fcntl.h>
57 #include <dev/usb/usb.h>
58 #include <dev/usb/usb_ioctl.h>
59 #include <dev/usb/usbdi.h>
60 #include <dev/usb/usbdi_util.h>
62 #define USB_DEBUG_VAR usb_fifo_debug
64 #include <dev/usb/usb_core.h>
65 #include <dev/usb/usb_dev.h>
66 #include <dev/usb/usb_mbuf.h>
67 #include <dev/usb/usb_process.h>
68 #include <dev/usb/usb_device.h>
69 #include <dev/usb/usb_debug.h>
70 #include <dev/usb/usb_busdma.h>
71 #include <dev/usb/usb_generic.h>
72 #include <dev/usb/usb_dynamic.h>
73 #include <dev/usb/usb_util.h>
75 #include <dev/usb/usb_controller.h>
76 #include <dev/usb/usb_bus.h>
78 #include <sys/filio.h>
79 #include <sys/ttycom.h>
80 #include <sys/syscallsubr.h>
82 #include <machine/stdarg.h>
83 #endif /* USB_GLOBAL_INCLUDE_FILE */
88 static int usb_fifo_debug = 0;
90 static SYSCTL_NODE(_hw_usb, OID_AUTO, dev, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
92 SYSCTL_INT(_hw_usb_dev, OID_AUTO, debug, CTLFLAG_RWTUN,
93 &usb_fifo_debug, 0, "Debug Level");
96 #if ((__FreeBSD_version >= 700001) || (__FreeBSD_version == 0) || \
97 ((__FreeBSD_version >= 600034) && (__FreeBSD_version < 700000)))
98 #define USB_UCRED struct ucred *ucred,
105 static int usb_fifo_open(struct usb_cdev_privdata *,
106 struct usb_fifo *, int);
107 static void usb_fifo_close(struct usb_fifo *, int);
108 static void usb_dev_init(void *);
109 static void usb_dev_init_post(void *);
110 static void usb_dev_uninit(void *);
111 static int usb_fifo_uiomove(struct usb_fifo *, void *, int,
113 static void usb_fifo_check_methods(struct usb_fifo_methods *);
114 static struct usb_fifo *usb_fifo_alloc(struct mtx *);
115 static struct usb_endpoint *usb_dev_get_ep(struct usb_device *, uint8_t,
117 static void usb_loc_fill(struct usb_fs_privdata *,
118 struct usb_cdev_privdata *);
119 static void usb_close(void *);
120 static usb_error_t usb_ref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *, int);
121 static usb_error_t usb_usb_ref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *);
122 static void usb_unref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *);
124 static d_open_t usb_open;
125 static d_ioctl_t usb_ioctl;
126 static d_read_t usb_read;
127 static d_write_t usb_write;
128 static d_poll_t usb_poll;
129 static d_kqfilter_t usb_kqfilter;
131 static d_ioctl_t usb_static_ioctl;
133 static usb_fifo_open_t usb_fifo_dummy_open;
134 static usb_fifo_close_t usb_fifo_dummy_close;
135 static usb_fifo_ioctl_t usb_fifo_dummy_ioctl;
136 static usb_fifo_cmd_t usb_fifo_dummy_cmd;
138 /* character device structure used for devices (/dev/ugenX.Y and /dev/uXXX) */
139 struct cdevsw usb_devsw = {
140 .d_version = D_VERSION,
142 .d_ioctl = usb_ioctl,
144 .d_flags = D_TRACKCLOSE,
146 .d_write = usb_write,
148 .d_kqfilter = usb_kqfilter,
151 static struct cdev* usb_dev = NULL;
153 /* character device structure used for /dev/usb */
154 static struct cdevsw usb_static_devsw = {
155 .d_version = D_VERSION,
156 .d_ioctl = usb_static_ioctl,
160 static TAILQ_HEAD(, usb_symlink) usb_sym_head;
161 static struct sx usb_sym_lock;
163 struct mtx usb_ref_lock;
165 /*------------------------------------------------------------------------*
168 * This is used to fill out a usb_cdev_privdata structure based on the
169 * device's address as contained in usb_fs_privdata.
170 *------------------------------------------------------------------------*/
172 usb_loc_fill(struct usb_fs_privdata* pd, struct usb_cdev_privdata *cpd)
174 cpd->bus_index = pd->bus_index;
175 cpd->dev_index = pd->dev_index;
176 cpd->ep_addr = pd->ep_addr;
177 cpd->fifo_index = pd->fifo_index;
180 /*------------------------------------------------------------------------*
183 * This function is used to atomically refer an USB device by its
184 * device location. If this function returns success the USB device
185 * will not disappear until the USB device is unreferenced.
188 * 0: Success, refcount incremented on the given USB device.
190 *------------------------------------------------------------------------*/
192 usb_ref_device(struct usb_cdev_privdata *cpd,
193 struct usb_cdev_refdata *crd, int need_uref)
195 struct usb_fifo **ppf;
198 DPRINTFN(2, "cpd=%p need uref=%d\n", cpd, need_uref);
201 memset(crd, 0, sizeof(*crd));
203 mtx_lock(&usb_ref_lock);
204 cpd->bus = devclass_get_softc(usb_devclass_ptr, cpd->bus_index);
205 if (cpd->bus == NULL) {
206 DPRINTFN(2, "no bus at %u\n", cpd->bus_index);
209 cpd->udev = cpd->bus->devices[cpd->dev_index];
210 if (cpd->udev == NULL) {
211 DPRINTFN(2, "no device at %u\n", cpd->dev_index);
214 if (cpd->udev->state == USB_STATE_DETACHED &&
216 DPRINTFN(2, "device is detached\n");
220 DPRINTFN(2, "ref udev - needed\n");
222 if (cpd->udev->refcount == USB_DEV_REF_MAX) {
223 DPRINTFN(2, "no dev ref\n");
226 cpd->udev->refcount++;
228 mtx_unlock(&usb_ref_lock);
231 * We need to grab the enumeration SX-lock before
232 * grabbing the FIFO refs to avoid deadlock at detach!
234 crd->do_unlock = usbd_enum_lock_sig(cpd->udev);
236 mtx_lock(&usb_ref_lock);
239 * Set "is_uref" after grabbing the default SX lock
243 /* check for signal */
244 if (crd->do_unlock > 1) {
250 /* check if we are doing an open */
251 if (cpd->fflags == 0) {
252 /* use zero defaults */
254 /* check for write */
255 if (cpd->fflags & FWRITE) {
256 ppf = cpd->udev->fifo;
257 f = ppf[cpd->fifo_index + USB_FIFO_TX];
259 crd->is_write = 1; /* ref */
260 if (f == NULL || f->refcount == USB_FIFO_REF_MAX)
262 if (f->curr_cpd != cpd)
264 /* check if USB-FS is active */
265 if (f->fs_ep_max != 0) {
271 if (cpd->fflags & FREAD) {
272 ppf = cpd->udev->fifo;
273 f = ppf[cpd->fifo_index + USB_FIFO_RX];
275 crd->is_read = 1; /* ref */
276 if (f == NULL || f->refcount == USB_FIFO_REF_MAX)
278 if (f->curr_cpd != cpd)
280 /* check if USB-FS is active */
281 if (f->fs_ep_max != 0) {
287 /* when everything is OK we increment the refcounts */
289 DPRINTFN(2, "ref write\n");
290 crd->txfifo->refcount++;
293 DPRINTFN(2, "ref read\n");
294 crd->rxfifo->refcount++;
296 mtx_unlock(&usb_ref_lock);
302 usbd_enum_unlock(cpd->udev);
305 if (--(cpd->udev->refcount) == 0)
306 cv_broadcast(&cpd->udev->ref_cv);
308 mtx_unlock(&usb_ref_lock);
309 DPRINTFN(2, "fail\n");
312 memset(crd, 0, sizeof(*crd));
314 return (USB_ERR_INVAL);
317 /*------------------------------------------------------------------------*
320 * This function is used to upgrade an USB reference to include the
321 * USB device reference on a USB location.
324 * 0: Success, refcount incremented on the given USB device.
326 *------------------------------------------------------------------------*/
328 usb_usb_ref_device(struct usb_cdev_privdata *cpd,
329 struct usb_cdev_refdata *crd)
332 * Check if we already got an USB reference on this location:
335 return (0); /* success */
338 * To avoid deadlock at detach we need to drop the FIFO ref
339 * and re-acquire a new ref!
341 usb_unref_device(cpd, crd);
343 return (usb_ref_device(cpd, crd, 1 /* need uref */));
346 /*------------------------------------------------------------------------*
349 * This function will release the reference count by one unit for the
351 *------------------------------------------------------------------------*/
353 usb_unref_device(struct usb_cdev_privdata *cpd,
354 struct usb_cdev_refdata *crd)
357 DPRINTFN(2, "cpd=%p is_uref=%d\n", cpd, crd->is_uref);
360 usbd_enum_unlock(cpd->udev);
362 mtx_lock(&usb_ref_lock);
364 if (--(crd->rxfifo->refcount) == 0) {
365 cv_signal(&crd->rxfifo->cv_drain);
370 if (--(crd->txfifo->refcount) == 0) {
371 cv_signal(&crd->txfifo->cv_drain);
377 if (--(cpd->udev->refcount) == 0)
378 cv_broadcast(&cpd->udev->ref_cv);
380 mtx_unlock(&usb_ref_lock);
383 static struct usb_fifo *
384 usb_fifo_alloc(struct mtx *mtx)
388 f = malloc(sizeof(*f), M_USBDEV, M_WAITOK | M_ZERO);
390 cv_init(&f->cv_io, "FIFO-IO");
391 cv_init(&f->cv_drain, "FIFO-DRAIN");
394 knlist_init_mtx(&f->selinfo.si_note, mtx);
399 /*------------------------------------------------------------------------*
401 *------------------------------------------------------------------------*/
403 usb_fifo_create(struct usb_cdev_privdata *cpd,
404 struct usb_cdev_refdata *crd)
406 struct usb_device *udev = cpd->udev;
408 struct usb_endpoint *ep;
414 int e = cpd->ep_addr;
416 is_tx = (cpd->fflags & FWRITE) ? 1 : 0;
417 is_rx = (cpd->fflags & FREAD) ? 1 : 0;
421 /* Preallocated FIFO */
423 DPRINTFN(5, "Preallocated FIFO\n");
425 f = udev->fifo[cpd->fifo_index + USB_FIFO_TX];
431 f = udev->fifo[cpd->fifo_index + USB_FIFO_RX];
439 KASSERT(e >= 0 && e <= 15, ("endpoint %d out of range", e));
441 /* search for a free FIFO slot */
442 DPRINTFN(5, "Endpoint device, searching for 0x%02x\n", e);
443 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)) {
538 ep = usb_dev_get_ep(udev, e, USB_FIFO_RX);
539 DPRINTFN(5, "dev_get_endpoint(%d, 0x%x)\n", e, USB_FIFO_RX);
541 DPRINTFN(5, "dev_get_endpoint returned NULL\n");
544 f = usb_fifo_alloc(&udev->device_mtx);
546 DPRINTFN(5, "could not alloc rx fifo\n");
549 /* update some fields */
550 f->fifo_index = n + USB_FIFO_RX;
553 f->methods = &usb_ugen_methods;
554 f->iface_index = ep->iface_index;
556 mtx_lock(&usb_ref_lock);
557 udev->fifo[n + USB_FIFO_RX] = f;
558 mtx_unlock(&usb_ref_lock);
561 crd->txfifo = udev->fifo[n + USB_FIFO_TX];
564 crd->rxfifo = udev->fifo[n + USB_FIFO_RX];
566 /* fill out fifo index */
567 DPRINTFN(5, "fifo index = %d\n", n);
576 usb_fifo_free(struct usb_fifo *f)
584 /* destroy symlink devices, if any */
585 for (n = 0; n != 2; n++) {
587 usb_free_symlink(f->symlink[n]);
588 f->symlink[n] = NULL;
591 mtx_lock(&usb_ref_lock);
593 /* delink ourselves to stop calls from userland */
594 if ((f->fifo_index < USB_FIFO_MAX) &&
596 (f->udev->fifo[f->fifo_index] == f)) {
597 f->udev->fifo[f->fifo_index] = NULL;
599 DPRINTFN(0, "USB FIFO %p has not been linked\n", f);
602 /* decrease refcount */
604 /* need to wait until all callers have exited */
605 while (f->refcount != 0) {
606 mtx_unlock(&usb_ref_lock); /* avoid LOR */
607 mtx_lock(f->priv_mtx);
608 /* prevent write flush, if any */
610 /* get I/O thread out of any sleep state */
611 if (f->flag_sleeping) {
612 f->flag_sleeping = 0;
613 cv_broadcast(&f->cv_io);
615 mtx_unlock(f->priv_mtx);
616 mtx_lock(&usb_ref_lock);
619 * Check if the "f->refcount" variable reached zero
620 * during the unlocked time before entering wait:
622 if (f->refcount == 0)
626 cv_wait(&f->cv_drain, &usb_ref_lock);
628 mtx_unlock(&usb_ref_lock);
630 /* take care of closing the device here, if any */
631 usb_fifo_close(f, 0);
633 cv_destroy(&f->cv_io);
634 cv_destroy(&f->cv_drain);
636 knlist_clear(&f->selinfo.si_note, 0);
637 seldrain(&f->selinfo);
638 knlist_destroy(&f->selinfo.si_note);
643 static struct usb_endpoint *
644 usb_dev_get_ep(struct usb_device *udev, uint8_t ep_index, uint8_t dir)
646 struct usb_endpoint *ep;
652 if (dir == USB_FIFO_RX) {
653 if (udev->flags.usb_mode == USB_MODE_HOST) {
659 if (udev->flags.usb_mode == USB_MODE_HOST) {
665 ep = usbd_get_ep_by_addr(udev, ep_index | ep_dir);
669 /* if the endpoint does not exist then return */
672 if (ep->edesc == NULL) {
673 /* invalid endpoint */
676 return (ep); /* success */
679 /*------------------------------------------------------------------------*
685 *------------------------------------------------------------------------*/
687 usb_fifo_open(struct usb_cdev_privdata *cpd,
688 struct usb_fifo *f, int fflags)
694 DPRINTFN(2, "no FIFO\n");
697 /* remove FWRITE and FREAD flags */
698 fflags &= ~(FWRITE | FREAD);
700 /* set correct file flags */
701 if ((f->fifo_index & 1) == USB_FIFO_TX) {
707 /* check if we are already opened */
708 /* we don't need any locks when checking this variable */
709 if (f->curr_cpd != NULL) {
714 /* reset short flag before open */
717 /* call open method */
718 err = (f->methods->f_open) (f, fflags);
722 mtx_lock(f->priv_mtx);
724 /* reset sleep flag */
725 f->flag_sleeping = 0;
727 /* reset error flag */
730 /* reset complete flag */
731 f->flag_iscomplete = 0;
733 /* reset select flag */
734 f->flag_isselect = 0;
736 /* reset flushing flag */
737 f->flag_flushing = 0;
739 /* reset ASYNC proc flag */
742 mtx_lock(&usb_ref_lock);
743 /* flag the fifo as opened to prevent others */
745 mtx_unlock(&usb_ref_lock);
750 mtx_unlock(f->priv_mtx);
755 /*------------------------------------------------------------------------*
757 *------------------------------------------------------------------------*/
759 usb_fifo_reset(struct usb_fifo *f)
767 USB_IF_DEQUEUE(&f->used_q, m);
769 USB_IF_ENQUEUE(&f->free_q, m);
774 /* reset have fragment flag */
775 f->flag_have_fragment = 0;
778 /*------------------------------------------------------------------------*
780 *------------------------------------------------------------------------*/
782 usb_fifo_close(struct usb_fifo *f, int fflags)
786 /* check if we are not opened */
787 if (f->curr_cpd == NULL) {
788 /* nothing to do - already closed */
791 mtx_lock(f->priv_mtx);
793 /* clear current cdev private data pointer */
794 mtx_lock(&usb_ref_lock);
796 mtx_unlock(&usb_ref_lock);
798 /* check if we are watched by kevent */
799 KNOTE_LOCKED(&f->selinfo.si_note, 0);
801 /* check if we are selected */
802 if (f->flag_isselect) {
803 selwakeup(&f->selinfo);
804 f->flag_isselect = 0;
806 /* check if a thread wants SIGIO */
807 if (f->async_p != NULL) {
808 PROC_LOCK(f->async_p);
809 kern_psignal(f->async_p, SIGIO);
810 PROC_UNLOCK(f->async_p);
813 /* remove FWRITE and FREAD flags */
814 fflags &= ~(FWRITE | FREAD);
816 /* flush written data, if any */
817 if ((f->fifo_index & 1) == USB_FIFO_TX) {
819 if (!f->flag_iserror) {
821 /* set flushing flag */
822 f->flag_flushing = 1;
824 /* get the last packet in */
825 if (f->flag_have_fragment) {
827 f->flag_have_fragment = 0;
828 USB_IF_DEQUEUE(&f->free_q, m);
830 USB_IF_ENQUEUE(&f->used_q, m);
834 /* start write transfer, if not already started */
835 (f->methods->f_start_write) (f);
837 /* check if flushed already */
838 while (f->flag_flushing &&
839 (!f->flag_iserror)) {
840 /* wait until all data has been written */
841 f->flag_sleeping = 1;
842 err = cv_timedwait_sig(&f->cv_io, f->priv_mtx,
843 USB_MS_TO_TICKS(USB_DEFAULT_TIMEOUT));
845 DPRINTF("signal received\n");
852 /* stop write transfer, if not already stopped */
853 (f->methods->f_stop_write) (f);
857 /* stop write transfer, if not already stopped */
858 (f->methods->f_stop_read) (f);
861 /* check if we are sleeping */
862 if (f->flag_sleeping) {
863 DPRINTFN(2, "Sleeping at close!\n");
865 mtx_unlock(f->priv_mtx);
867 /* call close method */
868 (f->methods->f_close) (f, fflags);
873 /*------------------------------------------------------------------------*
874 * usb_open - cdev callback
875 *------------------------------------------------------------------------*/
877 usb_open(struct cdev *dev, int fflags, int devtype, struct thread *td)
879 struct usb_fs_privdata* pd = (struct usb_fs_privdata*)dev->si_drv1;
880 struct usb_cdev_refdata refs;
881 struct usb_cdev_privdata *cpd;
884 DPRINTFN(2, "%s fflags=0x%08x\n", devtoname(dev), fflags);
886 KASSERT(fflags & (FREAD|FWRITE), ("invalid open flags"));
887 if (((fflags & FREAD) && !(pd->mode & FREAD)) ||
888 ((fflags & FWRITE) && !(pd->mode & FWRITE))) {
889 DPRINTFN(2, "access mode not supported\n");
893 cpd = malloc(sizeof(*cpd), M_USBDEV, M_WAITOK | M_ZERO);
895 usb_loc_fill(pd, cpd);
896 err = usb_ref_device(cpd, &refs, 1);
898 DPRINTFN(2, "cannot ref device\n");
902 cpd->fflags = fflags; /* access mode for open lifetime */
904 /* create FIFOs, if any */
905 err = usb_fifo_create(cpd, &refs);
906 /* check for error */
908 DPRINTFN(2, "cannot create fifo\n");
909 usb_unref_device(cpd, &refs);
913 if (fflags & FREAD) {
914 err = usb_fifo_open(cpd, refs.rxfifo, fflags);
916 DPRINTFN(2, "read open failed\n");
917 usb_unref_device(cpd, &refs);
922 if (fflags & FWRITE) {
923 err = usb_fifo_open(cpd, refs.txfifo, fflags);
925 DPRINTFN(2, "write open failed\n");
926 if (fflags & FREAD) {
927 usb_fifo_close(refs.rxfifo, fflags);
929 usb_unref_device(cpd, &refs);
934 usb_unref_device(cpd, &refs);
935 devfs_set_cdevpriv(cpd, usb_close);
940 /*------------------------------------------------------------------------*
941 * usb_close - cdev callback
942 *------------------------------------------------------------------------*/
946 struct usb_cdev_refdata refs;
947 struct usb_cdev_privdata *cpd = arg;
950 DPRINTFN(2, "cpd=%p\n", cpd);
952 err = usb_ref_device(cpd, &refs,
953 2 /* uref and allow detached state */);
955 DPRINTFN(2, "Cannot grab USB reference when "
956 "closing USB file handle\n");
959 if (cpd->fflags & FREAD) {
960 usb_fifo_close(refs.rxfifo, cpd->fflags);
962 if (cpd->fflags & FWRITE) {
963 usb_fifo_close(refs.txfifo, cpd->fflags);
965 usb_unref_device(cpd, &refs);
971 usb_dev_init(void *arg)
973 mtx_init(&usb_ref_lock, "USB ref mutex", NULL, MTX_DEF);
974 sx_init(&usb_sym_lock, "USB sym mutex");
975 TAILQ_INIT(&usb_sym_head);
977 /* check the UGEN methods */
978 usb_fifo_check_methods(&usb_ugen_methods);
981 SYSINIT(usb_dev_init, SI_SUB_KLD, SI_ORDER_FIRST, usb_dev_init, NULL);
984 usb_dev_init_post(void *arg)
987 * Create /dev/usb - this is needed for usbconfig(8), which
988 * needs a well-known device name to access.
990 usb_dev = make_dev(&usb_static_devsw, 0, UID_ROOT, GID_OPERATOR,
991 0644, USB_DEVICE_NAME);
992 if (usb_dev == NULL) {
993 DPRINTFN(0, "Could not create usb bus device\n");
997 SYSINIT(usb_dev_init_post, SI_SUB_KICK_SCHEDULER, SI_ORDER_FIRST, usb_dev_init_post, NULL);
1000 usb_dev_uninit(void *arg)
1002 if (usb_dev != NULL) {
1003 destroy_dev(usb_dev);
1006 mtx_destroy(&usb_ref_lock);
1007 sx_destroy(&usb_sym_lock);
1010 SYSUNINIT(usb_dev_uninit, SI_SUB_KICK_SCHEDULER, SI_ORDER_ANY, usb_dev_uninit, NULL);
1013 usb_ioctl_f_sub(struct usb_fifo *f, u_long cmd, void *addr,
1020 *(int *)addr = 0; /* character device */
1024 /* handled by upper FS layer */
1029 if (f->async_p != NULL) {
1033 f->async_p = USB_TD_GET_PROC(td);
1039 /* XXX this is not the most general solution */
1041 if (f->async_p == NULL) {
1045 if (*(int *)addr != USB_PROC_GET_GID(f->async_p)) {
1053 DPRINTFN(3, "cmd 0x%lx = %d\n", cmd, error);
1057 /*------------------------------------------------------------------------*
1058 * usb_ioctl - cdev callback
1059 *------------------------------------------------------------------------*/
1061 usb_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int fflag, struct thread* td)
1063 struct usb_cdev_refdata refs;
1064 struct usb_cdev_privdata* cpd;
1069 DPRINTFN(2, "cmd=0x%lx\n", cmd);
1071 err = devfs_get_cdevpriv((void **)&cpd);
1076 * Performance optimisation: We try to check for IOCTL's that
1077 * don't need the USB reference first. Then we grab the USB
1078 * reference if we need it!
1080 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1084 fflags = cpd->fflags;
1086 f = NULL; /* set default value */
1087 err = ENOIOCTL; /* set default value */
1089 if (fflags & FWRITE) {
1091 err = usb_ioctl_f_sub(f, cmd, addr, td);
1093 if (fflags & FREAD) {
1095 err = usb_ioctl_f_sub(f, cmd, addr, td);
1097 KASSERT(f != NULL, ("fifo not found"));
1098 if (err != ENOIOCTL)
1101 err = (f->methods->f_ioctl) (f, cmd, addr, fflags);
1103 DPRINTFN(2, "f_ioctl cmd 0x%lx = %d\n", cmd, err);
1105 if (err != ENOIOCTL)
1108 if (usb_usb_ref_device(cpd, &refs)) {
1109 /* we lost the reference */
1113 err = (f->methods->f_ioctl_post) (f, cmd, addr, fflags);
1115 DPRINTFN(2, "f_ioctl_post cmd 0x%lx = %d\n", cmd, err);
1117 if (err == ENOIOCTL)
1123 /* Wait for re-enumeration, if any */
1125 while (f->udev->re_enumerate_wait != USB_RE_ENUM_DONE) {
1127 usb_unref_device(cpd, &refs);
1129 usb_pause_mtx(NULL, hz / 128);
1131 while (usb_ref_device(cpd, &refs, 1 /* need uref */)) {
1132 if (usb_ref_device(cpd, &refs, 0)) {
1133 /* device no longer exists */
1136 usb_unref_device(cpd, &refs);
1137 usb_pause_mtx(NULL, hz / 128);
1142 usb_unref_device(cpd, &refs);
1147 usb_filter_detach(struct knote *kn)
1149 struct usb_fifo *f = kn->kn_hook;
1150 knlist_remove(&f->selinfo.si_note, kn, 0);
1154 usb_filter_write(struct knote *kn, long hint)
1156 struct usb_cdev_privdata* cpd;
1164 USB_MTX_ASSERT(f->priv_mtx, MA_OWNED);
1169 } else if (f->fs_ep_max == 0) {
1170 if (f->flag_iserror) {
1171 /* we got an error */
1174 if (f->queue_data == NULL) {
1176 * start write transfer, if not
1179 (f->methods->f_start_write) (f);
1181 /* check if any packets are available */
1182 USB_IF_POLL(&f->free_q, m);
1185 if (f->flag_iscomplete) {
1195 usb_filter_read(struct knote *kn, long hint)
1197 struct usb_cdev_privdata* cpd;
1205 USB_MTX_ASSERT(f->priv_mtx, MA_OWNED);
1210 } else if (f->fs_ep_max == 0) {
1211 if (f->flag_iserror) {
1212 /* we have an error */
1215 if (f->queue_data == NULL) {
1217 * start read transfer, if not
1220 (f->methods->f_start_read) (f);
1222 /* check if any packets are available */
1223 USB_IF_POLL(&f->used_q, m);
1225 /* start reading data, if any */
1227 (f->methods->f_start_read) (f);
1230 if (f->flag_iscomplete) {
1239 static struct filterops usb_filtops_write = {
1241 .f_detach = usb_filter_detach,
1242 .f_event = usb_filter_write,
1245 static struct filterops usb_filtops_read = {
1247 .f_detach = usb_filter_detach,
1248 .f_event = usb_filter_read,
1254 usb_kqfilter(struct cdev* dev, struct knote *kn)
1256 struct usb_cdev_refdata refs;
1257 struct usb_cdev_privdata* cpd;
1264 if (devfs_get_cdevpriv((void **)&cpd) != 0 ||
1265 usb_ref_device(cpd, &refs, 0) != 0)
1268 fflags = cpd->fflags;
1270 /* Figure out who needs service */
1271 switch (kn->kn_filter) {
1273 if (fflags & FWRITE) {
1275 kn->kn_fop = &usb_filtops_write;
1280 if (fflags & FREAD) {
1282 kn->kn_fop = &usb_filtops_read;
1293 mtx_lock(f->priv_mtx);
1294 knlist_add(&f->selinfo.si_note, kn, 1);
1295 mtx_unlock(f->priv_mtx);
1298 usb_unref_device(cpd, &refs);
1304 usb_poll(struct cdev* dev, int events, struct thread* td)
1306 struct usb_cdev_refdata refs;
1307 struct usb_cdev_privdata* cpd;
1310 int fflags, revents;
1312 if (devfs_get_cdevpriv((void **)&cpd) != 0 ||
1313 usb_ref_device(cpd, &refs, 0) != 0)
1315 (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM));
1317 fflags = cpd->fflags;
1319 /* Figure out who needs service */
1321 if ((events & (POLLOUT | POLLWRNORM)) &&
1322 (fflags & FWRITE)) {
1326 mtx_lock(f->priv_mtx);
1328 if (!refs.is_usbfs) {
1329 if (f->flag_iserror) {
1330 /* we got an error */
1333 if (f->queue_data == NULL) {
1335 * start write transfer, if not
1338 (f->methods->f_start_write) (f);
1340 /* check if any packets are available */
1341 USB_IF_POLL(&f->free_q, m);
1344 if (f->flag_iscomplete) {
1352 revents |= events & (POLLOUT | POLLWRNORM);
1354 f->flag_isselect = 1;
1355 selrecord(td, &f->selinfo);
1358 mtx_unlock(f->priv_mtx);
1360 if ((events & (POLLIN | POLLRDNORM)) &&
1365 mtx_lock(f->priv_mtx);
1367 if (!refs.is_usbfs) {
1368 if (f->flag_iserror) {
1369 /* we have an error */
1372 if (f->queue_data == NULL) {
1374 * start read transfer, if not
1377 (f->methods->f_start_read) (f);
1379 /* check if any packets are available */
1380 USB_IF_POLL(&f->used_q, m);
1383 if (f->flag_iscomplete) {
1391 revents |= events & (POLLIN | POLLRDNORM);
1393 f->flag_isselect = 1;
1394 selrecord(td, &f->selinfo);
1396 if (!refs.is_usbfs) {
1397 /* start reading data */
1398 (f->methods->f_start_read) (f);
1402 mtx_unlock(f->priv_mtx);
1404 usb_unref_device(cpd, &refs);
1409 usb_read(struct cdev *dev, struct uio *uio, int ioflag)
1411 struct usb_cdev_refdata refs;
1412 struct usb_cdev_privdata* cpd;
1417 uint8_t tr_data = 0;
1419 err = devfs_get_cdevpriv((void **)&cpd);
1423 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1429 /* should not happen */
1430 usb_unref_device(cpd, &refs);
1434 mtx_lock(f->priv_mtx);
1436 /* check for permanent read error */
1437 if (f->flag_iserror) {
1441 /* check if USB-FS interface is active */
1442 if (refs.is_usbfs) {
1444 * The queue is used for events that should be
1445 * retrieved using the "USB_FS_COMPLETE" ioctl.
1450 while (uio->uio_resid > 0) {
1452 USB_IF_DEQUEUE(&f->used_q, m);
1456 /* start read transfer, if not already started */
1458 (f->methods->f_start_read) (f);
1460 if (ioflag & IO_NDELAY) {
1462 /* return length before error */
1468 DPRINTF("sleeping\n");
1470 err = usb_fifo_wait(f);
1476 if (f->methods->f_filter_read) {
1478 * Sometimes it is convenient to process data at the
1479 * expense of a userland process instead of a kernel
1482 (f->methods->f_filter_read) (f, m);
1486 io_len = MIN(m->cur_data_len, uio->uio_resid);
1488 DPRINTFN(2, "transfer %d bytes from %p\n",
1489 io_len, m->cur_data_ptr);
1491 err = usb_fifo_uiomove(f,
1492 m->cur_data_ptr, io_len, uio);
1494 m->cur_data_len -= io_len;
1495 m->cur_data_ptr += io_len;
1497 if (m->cur_data_len == 0) {
1499 uint8_t last_packet;
1501 last_packet = m->last_packet;
1503 USB_IF_ENQUEUE(&f->free_q, m);
1510 USB_IF_PREPEND(&f->used_q, m);
1518 mtx_unlock(f->priv_mtx);
1520 usb_unref_device(cpd, &refs);
1526 usb_write(struct cdev *dev, struct uio *uio, int ioflag)
1528 struct usb_cdev_refdata refs;
1529 struct usb_cdev_privdata* cpd;
1535 uint8_t tr_data = 0;
1539 err = devfs_get_cdevpriv((void **)&cpd);
1543 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1549 /* should not happen */
1550 usb_unref_device(cpd, &refs);
1554 mtx_lock(f->priv_mtx);
1556 /* check for permanent write error */
1557 if (f->flag_iserror) {
1561 /* check if USB-FS interface is active */
1562 if (refs.is_usbfs) {
1564 * The queue is used for events that should be
1565 * retrieved using the "USB_FS_COMPLETE" ioctl.
1570 if (f->queue_data == NULL) {
1571 /* start write transfer, if not already started */
1572 (f->methods->f_start_write) (f);
1574 /* we allow writing zero length data */
1576 USB_IF_DEQUEUE(&f->free_q, m);
1580 if (ioflag & IO_NDELAY) {
1582 /* return length before error */
1588 DPRINTF("sleeping\n");
1590 err = usb_fifo_wait(f);
1598 if (f->flag_have_fragment == 0) {
1600 io_len = m->cur_data_len;
1601 pdata = m->cur_data_ptr;
1602 if (io_len > uio->uio_resid)
1603 io_len = uio->uio_resid;
1604 m->cur_data_len = io_len;
1606 io_len = m->max_data_len - m->cur_data_len;
1607 pdata = m->cur_data_ptr + m->cur_data_len;
1608 if (io_len > uio->uio_resid)
1609 io_len = uio->uio_resid;
1610 m->cur_data_len += io_len;
1613 DPRINTFN(2, "transfer %d bytes to %p\n",
1616 err = usb_fifo_uiomove(f, pdata, io_len, uio);
1619 f->flag_have_fragment = 0;
1620 USB_IF_ENQUEUE(&f->free_q, m);
1624 /* check if the buffer is ready to be transmitted */
1626 if ((f->flag_write_defrag == 0) ||
1627 (m->cur_data_len == m->max_data_len)) {
1628 f->flag_have_fragment = 0;
1631 * Check for write filter:
1633 * Sometimes it is convenient to process data
1634 * at the expense of a userland process
1635 * instead of a kernel process.
1637 if (f->methods->f_filter_write) {
1638 (f->methods->f_filter_write) (f, m);
1641 /* Put USB mbuf in the used queue */
1642 USB_IF_ENQUEUE(&f->used_q, m);
1644 /* Start writing data, if not already started */
1645 (f->methods->f_start_write) (f);
1647 /* Wait for more data or close */
1648 f->flag_have_fragment = 1;
1649 USB_IF_PREPEND(&f->free_q, m);
1652 } while (uio->uio_resid > 0);
1654 mtx_unlock(f->priv_mtx);
1656 usb_unref_device(cpd, &refs);
1662 usb_static_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
1666 struct usb_read_dir *urd;
1674 err = usb_read_symlink(u.urd->urd_data,
1675 u.urd->urd_startentry, u.urd->urd_maxlen);
1677 case USB_DEV_QUIRK_GET:
1678 case USB_QUIRK_NAME_GET:
1679 case USB_DEV_QUIRK_ADD:
1680 case USB_DEV_QUIRK_REMOVE:
1681 err = usb_quirk_ioctl_p(cmd, data, fflag, td);
1683 case USB_GET_TEMPLATE:
1684 *(int *)data = usb_template;
1687 case USB_SET_TEMPLATE:
1688 err = priv_check(curthread, PRIV_DRIVER);
1691 usb_template = *(int *)data;
1701 usb_fifo_uiomove(struct usb_fifo *f, void *cp,
1702 int n, struct uio *uio)
1706 mtx_unlock(f->priv_mtx);
1709 * "uiomove()" can sleep so one needs to make a wrapper,
1710 * exiting the mutex and checking things:
1712 error = uiomove(cp, n, uio);
1714 mtx_lock(f->priv_mtx);
1720 usb_fifo_wait(struct usb_fifo *f)
1724 USB_MTX_ASSERT(f->priv_mtx, MA_OWNED);
1726 if (f->flag_iserror) {
1730 f->flag_sleeping = 1;
1732 err = cv_wait_sig(&f->cv_io, f->priv_mtx);
1734 if (f->flag_iserror) {
1742 usb_fifo_signal(struct usb_fifo *f)
1744 if (f->flag_sleeping) {
1745 f->flag_sleeping = 0;
1746 cv_broadcast(&f->cv_io);
1751 usb_fifo_wakeup(struct usb_fifo *f)
1755 KNOTE_LOCKED(&f->selinfo.si_note, 0);
1757 if (f->flag_isselect) {
1758 selwakeup(&f->selinfo);
1759 f->flag_isselect = 0;
1761 if (f->async_p != NULL) {
1762 PROC_LOCK(f->async_p);
1763 kern_psignal(f->async_p, SIGIO);
1764 PROC_UNLOCK(f->async_p);
1769 usb_fifo_dummy_open(struct usb_fifo *fifo, int fflags)
1775 usb_fifo_dummy_close(struct usb_fifo *fifo, int fflags)
1781 usb_fifo_dummy_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
1787 usb_fifo_dummy_cmd(struct usb_fifo *fifo)
1789 fifo->flag_flushing = 0; /* not flushing */
1793 usb_fifo_check_methods(struct usb_fifo_methods *pm)
1795 /* check that all callback functions are OK */
1797 if (pm->f_open == NULL)
1798 pm->f_open = &usb_fifo_dummy_open;
1800 if (pm->f_close == NULL)
1801 pm->f_close = &usb_fifo_dummy_close;
1803 if (pm->f_ioctl == NULL)
1804 pm->f_ioctl = &usb_fifo_dummy_ioctl;
1806 if (pm->f_ioctl_post == NULL)
1807 pm->f_ioctl_post = &usb_fifo_dummy_ioctl;
1809 if (pm->f_start_read == NULL)
1810 pm->f_start_read = &usb_fifo_dummy_cmd;
1812 if (pm->f_stop_read == NULL)
1813 pm->f_stop_read = &usb_fifo_dummy_cmd;
1815 if (pm->f_start_write == NULL)
1816 pm->f_start_write = &usb_fifo_dummy_cmd;
1818 if (pm->f_stop_write == NULL)
1819 pm->f_stop_write = &usb_fifo_dummy_cmd;
1822 /*------------------------------------------------------------------------*
1825 * The following function will create a duplex FIFO.
1830 *------------------------------------------------------------------------*/
1832 usb_fifo_attach(struct usb_device *udev, void *priv_sc,
1833 struct mtx *priv_mtx, struct usb_fifo_methods *pm,
1834 struct usb_fifo_sc *f_sc, uint16_t unit, int16_t subunit,
1835 uint8_t iface_index, uid_t uid, gid_t gid, int mode)
1837 struct usb_fifo *f_tx;
1838 struct usb_fifo *f_rx;
1842 f_sc->fp[USB_FIFO_TX] = NULL;
1843 f_sc->fp[USB_FIFO_RX] = NULL;
1848 /* check the methods */
1849 usb_fifo_check_methods(pm);
1851 if (priv_mtx == NULL)
1854 /* search for a free FIFO slot */
1855 for (n = 0;; n += 2) {
1857 if (n == USB_FIFO_MAX) {
1858 /* end of FIFOs reached */
1861 /* Check for TX FIFO */
1862 if (udev->fifo[n + USB_FIFO_TX] != NULL) {
1865 /* Check for RX FIFO */
1866 if (udev->fifo[n + USB_FIFO_RX] != NULL) {
1872 f_tx = usb_fifo_alloc(priv_mtx);
1873 f_rx = usb_fifo_alloc(priv_mtx);
1875 if ((f_tx == NULL) || (f_rx == NULL)) {
1876 usb_fifo_free(f_tx);
1877 usb_fifo_free(f_rx);
1880 /* initialise FIFO structures */
1882 f_tx->fifo_index = n + USB_FIFO_TX;
1883 f_tx->dev_ep_index = -1;
1884 f_tx->priv_sc0 = priv_sc;
1886 f_tx->iface_index = iface_index;
1889 f_rx->fifo_index = n + USB_FIFO_RX;
1890 f_rx->dev_ep_index = -1;
1891 f_rx->priv_sc0 = priv_sc;
1893 f_rx->iface_index = iface_index;
1896 f_sc->fp[USB_FIFO_TX] = f_tx;
1897 f_sc->fp[USB_FIFO_RX] = f_rx;
1899 mtx_lock(&usb_ref_lock);
1900 udev->fifo[f_tx->fifo_index] = f_tx;
1901 udev->fifo[f_rx->fifo_index] = f_rx;
1902 mtx_unlock(&usb_ref_lock);
1904 for (n = 0; n != 4; n++) {
1906 if (pm->basename[n] == NULL) {
1910 if (snprintf(devname, sizeof(devname),
1911 "%s%u%s", pm->basename[n],
1912 unit, pm->postfix[n] ?
1913 pm->postfix[n] : "")) {
1917 if (snprintf(devname, sizeof(devname),
1918 "%s%u.%d%s", pm->basename[n],
1919 unit, subunit, pm->postfix[n] ?
1920 pm->postfix[n] : "")) {
1926 * Distribute the symbolic links into two FIFO structures:
1929 f_rx->symlink[n / 2] =
1930 usb_alloc_symlink(devname);
1932 f_tx->symlink[n / 2] =
1933 usb_alloc_symlink(devname);
1936 /* Create the device */
1937 f_sc->dev = usb_make_dev(udev, devname, -1,
1938 f_tx->fifo_index & f_rx->fifo_index,
1939 FREAD|FWRITE, uid, gid, mode);
1942 DPRINTFN(2, "attached %p/%p\n", f_tx, f_rx);
1946 /*------------------------------------------------------------------------*
1947 * usb_fifo_alloc_buffer
1952 *------------------------------------------------------------------------*/
1954 usb_fifo_alloc_buffer(struct usb_fifo *f, usb_size_t bufsize,
1957 usb_fifo_free_buffer(f);
1959 /* allocate an endpoint */
1960 f->free_q.ifq_maxlen = nbuf;
1961 f->used_q.ifq_maxlen = nbuf;
1963 f->queue_data = usb_alloc_mbufs(
1964 M_USBDEV, &f->free_q, bufsize, nbuf);
1966 if ((f->queue_data == NULL) && bufsize && nbuf) {
1969 return (0); /* success */
1972 /*------------------------------------------------------------------------*
1973 * usb_fifo_free_buffer
1975 * This function will free the buffers associated with a FIFO. This
1976 * function can be called multiple times in a row.
1977 *------------------------------------------------------------------------*/
1979 usb_fifo_free_buffer(struct usb_fifo *f)
1981 if (f->queue_data) {
1982 /* free old buffer */
1983 free(f->queue_data, M_USBDEV);
1984 f->queue_data = NULL;
1988 memset(&f->free_q, 0, sizeof(f->free_q));
1989 memset(&f->used_q, 0, sizeof(f->used_q));
1993 usb_fifo_detach(struct usb_fifo_sc *f_sc)
1998 usb_fifo_free(f_sc->fp[USB_FIFO_TX]);
1999 usb_fifo_free(f_sc->fp[USB_FIFO_RX]);
2001 f_sc->fp[USB_FIFO_TX] = NULL;
2002 f_sc->fp[USB_FIFO_RX] = NULL;
2004 usb_destroy_dev(f_sc->dev);
2008 DPRINTFN(2, "detached %p\n", f_sc);
2012 usb_fifo_put_bytes_max(struct usb_fifo *f)
2017 USB_IF_POLL(&f->free_q, m);
2020 len = m->max_data_len;
2027 /*------------------------------------------------------------------------*
2031 * 0 - normal operation
2032 * 1 - set last packet flag to enforce framing
2033 *------------------------------------------------------------------------*/
2035 usb_fifo_put_data(struct usb_fifo *f, struct usb_page_cache *pc,
2036 usb_frlength_t offset, usb_frlength_t len, uint8_t what)
2039 usb_frlength_t io_len;
2041 while (len || (what == 1)) {
2043 USB_IF_DEQUEUE(&f->free_q, m);
2048 io_len = MIN(len, m->cur_data_len);
2050 usbd_copy_out(pc, offset, m->cur_data_ptr, io_len);
2052 m->cur_data_len = io_len;
2056 if ((len == 0) && (what == 1)) {
2059 USB_IF_ENQUEUE(&f->used_q, m);
2063 if ((len == 0) || (what == 1)) {
2073 usb_fifo_put_data_linear(struct usb_fifo *f, void *ptr,
2074 usb_size_t len, uint8_t what)
2079 while (len || (what == 1)) {
2081 USB_IF_DEQUEUE(&f->free_q, m);
2086 io_len = MIN(len, m->cur_data_len);
2088 memcpy(m->cur_data_ptr, ptr, io_len);
2090 m->cur_data_len = io_len;
2091 ptr = USB_ADD_BYTES(ptr, io_len);
2094 if ((len == 0) && (what == 1)) {
2097 USB_IF_ENQUEUE(&f->used_q, m);
2101 if ((len == 0) || (what == 1)) {
2111 usb_fifo_put_data_buffer(struct usb_fifo *f, void *ptr, usb_size_t len)
2115 USB_IF_DEQUEUE(&f->free_q, m);
2118 m->cur_data_len = len;
2119 m->cur_data_ptr = ptr;
2120 USB_IF_ENQUEUE(&f->used_q, m);
2128 usb_fifo_put_data_error(struct usb_fifo *f)
2130 f->flag_iserror = 1;
2134 /*------------------------------------------------------------------------*
2138 * 0 - normal operation
2139 * 1 - only get one "usb_mbuf"
2143 * 1 - data in buffer
2144 *------------------------------------------------------------------------*/
2146 usb_fifo_get_data(struct usb_fifo *f, struct usb_page_cache *pc,
2147 usb_frlength_t offset, usb_frlength_t len, usb_frlength_t *actlen,
2151 usb_frlength_t io_len;
2152 uint8_t tr_data = 0;
2158 USB_IF_DEQUEUE(&f->used_q, m);
2164 io_len = MIN(len, m->cur_data_len);
2166 usbd_copy_in(pc, offset, m->cur_data_ptr, io_len);
2170 actlen[0] += io_len;
2171 m->cur_data_ptr += io_len;
2172 m->cur_data_len -= io_len;
2174 if ((m->cur_data_len == 0) || (what == 1)) {
2175 USB_IF_ENQUEUE(&f->free_q, m);
2183 USB_IF_PREPEND(&f->used_q, m);
2188 /* wait for data to be written out */
2191 if (f->flag_flushing) {
2192 /* check if we should send a short packet */
2193 if (f->flag_short != 0) {
2198 /* flushing complete */
2199 f->flag_flushing = 0;
2212 usb_fifo_get_data_linear(struct usb_fifo *f, void *ptr,
2213 usb_size_t len, usb_size_t *actlen, uint8_t what)
2217 uint8_t tr_data = 0;
2223 USB_IF_DEQUEUE(&f->used_q, m);
2229 io_len = MIN(len, m->cur_data_len);
2231 memcpy(ptr, m->cur_data_ptr, io_len);
2234 ptr = USB_ADD_BYTES(ptr, io_len);
2235 actlen[0] += io_len;
2236 m->cur_data_ptr += io_len;
2237 m->cur_data_len -= io_len;
2239 if ((m->cur_data_len == 0) || (what == 1)) {
2240 USB_IF_ENQUEUE(&f->free_q, m);
2248 USB_IF_PREPEND(&f->used_q, m);
2253 /* wait for data to be written out */
2256 if (f->flag_flushing) {
2257 /* check if we should send a short packet */
2258 if (f->flag_short != 0) {
2263 /* flushing complete */
2264 f->flag_flushing = 0;
2277 usb_fifo_get_data_buffer(struct usb_fifo *f, void **pptr, usb_size_t *plen)
2281 USB_IF_POLL(&f->used_q, m);
2284 *plen = m->cur_data_len;
2285 *pptr = m->cur_data_ptr;
2293 usb_fifo_get_data_error(struct usb_fifo *f)
2295 f->flag_iserror = 1;
2299 /*------------------------------------------------------------------------*
2304 * Else: Pointer to symlink entry
2305 *------------------------------------------------------------------------*/
2306 struct usb_symlink *
2307 usb_alloc_symlink(const char *target)
2309 struct usb_symlink *ps;
2311 ps = malloc(sizeof(*ps), M_USBDEV, M_WAITOK);
2315 /* XXX no longer needed */
2316 strlcpy(ps->src_path, target, sizeof(ps->src_path));
2317 ps->src_len = strlen(ps->src_path);
2318 strlcpy(ps->dst_path, target, sizeof(ps->dst_path));
2319 ps->dst_len = strlen(ps->dst_path);
2321 sx_xlock(&usb_sym_lock);
2322 TAILQ_INSERT_TAIL(&usb_sym_head, ps, sym_entry);
2323 sx_unlock(&usb_sym_lock);
2327 /*------------------------------------------------------------------------*
2329 *------------------------------------------------------------------------*/
2331 usb_free_symlink(struct usb_symlink *ps)
2336 sx_xlock(&usb_sym_lock);
2337 TAILQ_REMOVE(&usb_sym_head, ps, sym_entry);
2338 sx_unlock(&usb_sym_lock);
2343 /*------------------------------------------------------------------------*
2349 *------------------------------------------------------------------------*/
2351 usb_read_symlink(uint8_t *user_ptr, uint32_t startentry, uint32_t user_len)
2353 struct usb_symlink *ps;
2359 sx_xlock(&usb_sym_lock);
2361 TAILQ_FOREACH(ps, &usb_sym_head, sym_entry) {
2364 * Compute total length of source and destination symlink
2365 * strings pluss one length byte and two NUL bytes:
2367 temp = ps->src_len + ps->dst_len + 3;
2371 * Skip entry because this length cannot fit
2376 if (startentry != 0) {
2377 /* decrement read offset */
2381 if (temp > user_len) {
2382 /* out of buffer space */
2387 /* copy out total length */
2389 error = copyout(&len,
2390 USB_ADD_BYTES(user_ptr, delta), 1);
2396 /* copy out source string */
2398 error = copyout(ps->src_path,
2399 USB_ADD_BYTES(user_ptr, delta), ps->src_len);
2404 delta += ps->src_len;
2405 error = copyout(&len,
2406 USB_ADD_BYTES(user_ptr, delta), 1);
2412 /* copy out destination string */
2414 error = copyout(ps->dst_path,
2415 USB_ADD_BYTES(user_ptr, delta), ps->dst_len);
2420 delta += ps->dst_len;
2421 error = copyout(&len,
2422 USB_ADD_BYTES(user_ptr, delta), 1);
2431 /* a zero length entry indicates the end */
2433 if ((user_len != 0) && (error == 0)) {
2437 error = copyout(&len,
2438 USB_ADD_BYTES(user_ptr, delta), 1);
2440 sx_unlock(&usb_sym_lock);
2445 usb_fifo_set_close_zlp(struct usb_fifo *f, uint8_t onoff)
2450 /* send a Zero Length Packet, ZLP, before close */
2451 f->flag_short = onoff;
2455 usb_fifo_set_write_defrag(struct usb_fifo *f, uint8_t onoff)
2460 /* defrag written data */
2461 f->flag_write_defrag = onoff;
2462 /* reset defrag state */
2463 f->flag_have_fragment = 0;
2467 usb_fifo_softc(struct usb_fifo *f)
2469 return (f->priv_sc0);
2471 #endif /* USB_HAVE_UGEN */