3 * Copyright (c) 2006-2008 Hans Petter Selasky. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * usb_dev.c - An abstraction layer for creating devices under /dev/...
30 #ifdef USB_GLOBAL_INCLUDE_FILE
31 #include USB_GLOBAL_INCLUDE_FILE
33 #include <sys/stdint.h>
34 #include <sys/stddef.h>
35 #include <sys/param.h>
36 #include <sys/queue.h>
37 #include <sys/types.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
41 #include <sys/module.h>
43 #include <sys/mutex.h>
44 #include <sys/condvar.h>
45 #include <sys/sysctl.h>
47 #include <sys/unistd.h>
48 #include <sys/callout.h>
49 #include <sys/malloc.h>
51 #include <sys/vnode.h>
53 #include <sys/fcntl.h>
55 #include <dev/usb/usb.h>
56 #include <dev/usb/usb_ioctl.h>
57 #include <dev/usb/usbdi.h>
58 #include <dev/usb/usbdi_util.h>
60 #define USB_DEBUG_VAR usb_fifo_debug
62 #include <dev/usb/usb_core.h>
63 #include <dev/usb/usb_dev.h>
64 #include <dev/usb/usb_mbuf.h>
65 #include <dev/usb/usb_process.h>
66 #include <dev/usb/usb_device.h>
67 #include <dev/usb/usb_debug.h>
68 #include <dev/usb/usb_busdma.h>
69 #include <dev/usb/usb_generic.h>
70 #include <dev/usb/usb_dynamic.h>
71 #include <dev/usb/usb_util.h>
73 #include <dev/usb/usb_controller.h>
74 #include <dev/usb/usb_bus.h>
76 #include <sys/filio.h>
77 #include <sys/ttycom.h>
78 #include <sys/syscallsubr.h>
80 #include <machine/stdarg.h>
81 #endif /* USB_GLOBAL_INCLUDE_FILE */
86 static int usb_fifo_debug = 0;
88 static SYSCTL_NODE(_hw_usb, OID_AUTO, dev, CTLFLAG_RW, 0, "USB device");
89 SYSCTL_INT(_hw_usb_dev, OID_AUTO, debug, CTLFLAG_RWTUN,
90 &usb_fifo_debug, 0, "Debug Level");
93 #if ((__FreeBSD_version >= 700001) || (__FreeBSD_version == 0) || \
94 ((__FreeBSD_version >= 600034) && (__FreeBSD_version < 700000)))
95 #define USB_UCRED struct ucred *ucred,
102 static int usb_fifo_open(struct usb_cdev_privdata *,
103 struct usb_fifo *, int);
104 static void usb_fifo_close(struct usb_fifo *, int);
105 static void usb_dev_init(void *);
106 static void usb_dev_init_post(void *);
107 static void usb_dev_uninit(void *);
108 static int usb_fifo_uiomove(struct usb_fifo *, void *, int,
110 static void usb_fifo_check_methods(struct usb_fifo_methods *);
111 static struct usb_fifo *usb_fifo_alloc(struct mtx *);
112 static struct usb_endpoint *usb_dev_get_ep(struct usb_device *, uint8_t,
114 static void usb_loc_fill(struct usb_fs_privdata *,
115 struct usb_cdev_privdata *);
116 static void usb_close(void *);
117 static usb_error_t usb_ref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *, int);
118 static usb_error_t usb_usb_ref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *);
119 static void usb_unref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *);
121 static d_open_t usb_open;
122 static d_ioctl_t usb_ioctl;
123 static d_read_t usb_read;
124 static d_write_t usb_write;
125 static d_poll_t usb_poll;
126 static d_kqfilter_t usb_kqfilter;
128 static d_ioctl_t usb_static_ioctl;
130 static usb_fifo_open_t usb_fifo_dummy_open;
131 static usb_fifo_close_t usb_fifo_dummy_close;
132 static usb_fifo_ioctl_t usb_fifo_dummy_ioctl;
133 static usb_fifo_cmd_t usb_fifo_dummy_cmd;
135 /* character device structure used for devices (/dev/ugenX.Y and /dev/uXXX) */
136 struct cdevsw usb_devsw = {
137 .d_version = D_VERSION,
139 .d_ioctl = usb_ioctl,
141 .d_flags = D_TRACKCLOSE,
143 .d_write = usb_write,
145 .d_kqfilter = usb_kqfilter,
148 static struct cdev* usb_dev = NULL;
150 /* character device structure used for /dev/usb */
151 static struct cdevsw usb_static_devsw = {
152 .d_version = D_VERSION,
153 .d_ioctl = usb_static_ioctl,
157 static TAILQ_HEAD(, usb_symlink) usb_sym_head;
158 static struct sx usb_sym_lock;
160 struct mtx usb_ref_lock;
162 /*------------------------------------------------------------------------*
165 * This is used to fill out a usb_cdev_privdata structure based on the
166 * device's address as contained in usb_fs_privdata.
167 *------------------------------------------------------------------------*/
169 usb_loc_fill(struct usb_fs_privdata* pd, struct usb_cdev_privdata *cpd)
171 cpd->bus_index = pd->bus_index;
172 cpd->dev_index = pd->dev_index;
173 cpd->ep_addr = pd->ep_addr;
174 cpd->fifo_index = pd->fifo_index;
177 /*------------------------------------------------------------------------*
180 * This function is used to atomically refer an USB device by its
181 * device location. If this function returns success the USB device
182 * will not disappear until the USB device is unreferenced.
185 * 0: Success, refcount incremented on the given USB device.
187 *------------------------------------------------------------------------*/
189 usb_ref_device(struct usb_cdev_privdata *cpd,
190 struct usb_cdev_refdata *crd, int need_uref)
192 struct usb_fifo **ppf;
195 DPRINTFN(2, "cpd=%p need uref=%d\n", cpd, need_uref);
198 memset(crd, 0, sizeof(*crd));
200 mtx_lock(&usb_ref_lock);
201 cpd->bus = devclass_get_softc(usb_devclass_ptr, cpd->bus_index);
202 if (cpd->bus == NULL) {
203 DPRINTFN(2, "no bus at %u\n", cpd->bus_index);
206 cpd->udev = cpd->bus->devices[cpd->dev_index];
207 if (cpd->udev == NULL) {
208 DPRINTFN(2, "no device at %u\n", cpd->dev_index);
211 if (cpd->udev->state == USB_STATE_DETACHED &&
213 DPRINTFN(2, "device is detached\n");
217 DPRINTFN(2, "ref udev - needed\n");
219 if (cpd->udev->refcount == USB_DEV_REF_MAX) {
220 DPRINTFN(2, "no dev ref\n");
223 cpd->udev->refcount++;
225 mtx_unlock(&usb_ref_lock);
228 * We need to grab the enumeration SX-lock before
229 * grabbing the FIFO refs to avoid deadlock at detach!
231 crd->do_unlock = usbd_enum_lock_sig(cpd->udev);
233 mtx_lock(&usb_ref_lock);
236 * Set "is_uref" after grabbing the default SX lock
240 /* check for signal */
241 if (crd->do_unlock > 1) {
247 /* check if we are doing an open */
248 if (cpd->fflags == 0) {
249 /* use zero defaults */
251 /* check for write */
252 if (cpd->fflags & FWRITE) {
253 ppf = cpd->udev->fifo;
254 f = ppf[cpd->fifo_index + USB_FIFO_TX];
256 crd->is_write = 1; /* ref */
257 if (f == NULL || f->refcount == USB_FIFO_REF_MAX)
259 if (f->curr_cpd != cpd)
261 /* check if USB-FS is active */
262 if (f->fs_ep_max != 0) {
268 if (cpd->fflags & FREAD) {
269 ppf = cpd->udev->fifo;
270 f = ppf[cpd->fifo_index + USB_FIFO_RX];
272 crd->is_read = 1; /* ref */
273 if (f == NULL || f->refcount == USB_FIFO_REF_MAX)
275 if (f->curr_cpd != cpd)
277 /* check if USB-FS is active */
278 if (f->fs_ep_max != 0) {
284 /* when everything is OK we increment the refcounts */
286 DPRINTFN(2, "ref write\n");
287 crd->txfifo->refcount++;
290 DPRINTFN(2, "ref read\n");
291 crd->rxfifo->refcount++;
293 mtx_unlock(&usb_ref_lock);
299 usbd_enum_unlock(cpd->udev);
302 if (--(cpd->udev->refcount) == 0)
303 cv_broadcast(&cpd->udev->ref_cv);
305 mtx_unlock(&usb_ref_lock);
306 DPRINTFN(2, "fail\n");
309 memset(crd, 0, sizeof(*crd));
311 return (USB_ERR_INVAL);
314 /*------------------------------------------------------------------------*
317 * This function is used to upgrade an USB reference to include the
318 * USB device reference on a USB location.
321 * 0: Success, refcount incremented on the given USB device.
323 *------------------------------------------------------------------------*/
325 usb_usb_ref_device(struct usb_cdev_privdata *cpd,
326 struct usb_cdev_refdata *crd)
329 * Check if we already got an USB reference on this location:
332 return (0); /* success */
335 * To avoid deadlock at detach we need to drop the FIFO ref
336 * and re-acquire a new ref!
338 usb_unref_device(cpd, crd);
340 return (usb_ref_device(cpd, crd, 1 /* need uref */));
343 /*------------------------------------------------------------------------*
346 * This function will release the reference count by one unit for the
348 *------------------------------------------------------------------------*/
350 usb_unref_device(struct usb_cdev_privdata *cpd,
351 struct usb_cdev_refdata *crd)
354 DPRINTFN(2, "cpd=%p is_uref=%d\n", cpd, crd->is_uref);
357 usbd_enum_unlock(cpd->udev);
359 mtx_lock(&usb_ref_lock);
361 if (--(crd->rxfifo->refcount) == 0) {
362 cv_signal(&crd->rxfifo->cv_drain);
367 if (--(crd->txfifo->refcount) == 0) {
368 cv_signal(&crd->txfifo->cv_drain);
374 if (--(cpd->udev->refcount) == 0)
375 cv_broadcast(&cpd->udev->ref_cv);
377 mtx_unlock(&usb_ref_lock);
380 static struct usb_fifo *
381 usb_fifo_alloc(struct mtx *mtx)
385 f = malloc(sizeof(*f), M_USBDEV, M_WAITOK | M_ZERO);
387 cv_init(&f->cv_io, "FIFO-IO");
388 cv_init(&f->cv_drain, "FIFO-DRAIN");
391 knlist_init_mtx(&f->selinfo.si_note, mtx);
396 /*------------------------------------------------------------------------*
398 *------------------------------------------------------------------------*/
400 usb_fifo_create(struct usb_cdev_privdata *cpd,
401 struct usb_cdev_refdata *crd)
403 struct usb_device *udev = cpd->udev;
405 struct usb_endpoint *ep;
411 int e = cpd->ep_addr;
413 is_tx = (cpd->fflags & FWRITE) ? 1 : 0;
414 is_rx = (cpd->fflags & FREAD) ? 1 : 0;
418 /* Preallocated FIFO */
420 DPRINTFN(5, "Preallocated FIFO\n");
422 f = udev->fifo[cpd->fifo_index + USB_FIFO_TX];
428 f = udev->fifo[cpd->fifo_index + USB_FIFO_RX];
436 KASSERT(e >= 0 && e <= 15, ("endpoint %d out of range", e));
438 /* search for a free FIFO slot */
439 DPRINTFN(5, "Endpoint device, searching for 0x%02x\n", e);
440 for (n = 0;; n += 2) {
442 if (n == USB_FIFO_MAX) {
447 /* end of FIFOs reached */
448 DPRINTFN(5, "out of FIFOs\n");
452 /* Check for TX FIFO */
454 f = udev->fifo[n + USB_FIFO_TX];
456 if (f->dev_ep_index != e) {
457 /* wrong endpoint index */
460 if (f->curr_cpd != NULL) {
465 } else if (no_null) {
469 /* Check for RX FIFO */
471 f = udev->fifo[n + USB_FIFO_RX];
473 if (f->dev_ep_index != e) {
474 /* wrong endpoint index */
477 if (f->curr_cpd != NULL) {
482 } else if (no_null) {
490 if (e >= (USB_EP_MAX / 2)) {
491 /* we don't create any endpoints in this range */
492 DPRINTFN(5, "ep out of range\n");
493 return (is_busy ? EBUSY : EINVAL);
497 if ((e != 0) && is_busy) {
499 * Only the default control endpoint is allowed to be
500 * opened multiple times!
502 DPRINTFN(5, "busy\n");
508 (udev->fifo[n + USB_FIFO_TX] == NULL)) {
509 ep = usb_dev_get_ep(udev, e, USB_FIFO_TX);
510 DPRINTFN(5, "dev_get_endpoint(%d, 0x%x)\n", e, USB_FIFO_TX);
512 DPRINTFN(5, "dev_get_endpoint returned NULL\n");
515 f = usb_fifo_alloc(&udev->device_mtx);
517 DPRINTFN(5, "could not alloc tx fifo\n");
520 /* update some fields */
521 f->fifo_index = n + USB_FIFO_TX;
524 f->methods = &usb_ugen_methods;
525 f->iface_index = ep->iface_index;
527 mtx_lock(&usb_ref_lock);
528 udev->fifo[n + USB_FIFO_TX] = f;
529 mtx_unlock(&usb_ref_lock);
533 (udev->fifo[n + USB_FIFO_RX] == NULL)) {
535 ep = usb_dev_get_ep(udev, e, USB_FIFO_RX);
536 DPRINTFN(5, "dev_get_endpoint(%d, 0x%x)\n", e, USB_FIFO_RX);
538 DPRINTFN(5, "dev_get_endpoint returned NULL\n");
541 f = usb_fifo_alloc(&udev->device_mtx);
543 DPRINTFN(5, "could not alloc rx fifo\n");
546 /* update some fields */
547 f->fifo_index = n + USB_FIFO_RX;
550 f->methods = &usb_ugen_methods;
551 f->iface_index = ep->iface_index;
553 mtx_lock(&usb_ref_lock);
554 udev->fifo[n + USB_FIFO_RX] = f;
555 mtx_unlock(&usb_ref_lock);
558 crd->txfifo = udev->fifo[n + USB_FIFO_TX];
561 crd->rxfifo = udev->fifo[n + USB_FIFO_RX];
563 /* fill out fifo index */
564 DPRINTFN(5, "fifo index = %d\n", n);
573 usb_fifo_free(struct usb_fifo *f)
581 /* destroy symlink devices, if any */
582 for (n = 0; n != 2; n++) {
584 usb_free_symlink(f->symlink[n]);
585 f->symlink[n] = NULL;
588 mtx_lock(&usb_ref_lock);
590 /* delink ourselves to stop calls from userland */
591 if ((f->fifo_index < USB_FIFO_MAX) &&
593 (f->udev->fifo[f->fifo_index] == f)) {
594 f->udev->fifo[f->fifo_index] = NULL;
596 DPRINTFN(0, "USB FIFO %p has not been linked\n", f);
599 /* decrease refcount */
601 /* need to wait until all callers have exited */
602 while (f->refcount != 0) {
603 mtx_unlock(&usb_ref_lock); /* avoid LOR */
604 mtx_lock(f->priv_mtx);
605 /* prevent write flush, if any */
607 /* get I/O thread out of any sleep state */
608 if (f->flag_sleeping) {
609 f->flag_sleeping = 0;
610 cv_broadcast(&f->cv_io);
612 mtx_unlock(f->priv_mtx);
613 mtx_lock(&usb_ref_lock);
616 * Check if the "f->refcount" variable reached zero
617 * during the unlocked time before entering wait:
619 if (f->refcount == 0)
623 cv_wait(&f->cv_drain, &usb_ref_lock);
625 mtx_unlock(&usb_ref_lock);
627 /* take care of closing the device here, if any */
628 usb_fifo_close(f, 0);
630 cv_destroy(&f->cv_io);
631 cv_destroy(&f->cv_drain);
633 knlist_clear(&f->selinfo.si_note, 0);
634 seldrain(&f->selinfo);
635 knlist_destroy(&f->selinfo.si_note);
640 static struct usb_endpoint *
641 usb_dev_get_ep(struct usb_device *udev, uint8_t ep_index, uint8_t dir)
643 struct usb_endpoint *ep;
649 if (dir == USB_FIFO_RX) {
650 if (udev->flags.usb_mode == USB_MODE_HOST) {
656 if (udev->flags.usb_mode == USB_MODE_HOST) {
662 ep = usbd_get_ep_by_addr(udev, ep_index | ep_dir);
666 /* if the endpoint does not exist then return */
669 if (ep->edesc == NULL) {
670 /* invalid endpoint */
673 return (ep); /* success */
676 /*------------------------------------------------------------------------*
682 *------------------------------------------------------------------------*/
684 usb_fifo_open(struct usb_cdev_privdata *cpd,
685 struct usb_fifo *f, int fflags)
691 DPRINTFN(2, "no FIFO\n");
694 /* remove FWRITE and FREAD flags */
695 fflags &= ~(FWRITE | FREAD);
697 /* set correct file flags */
698 if ((f->fifo_index & 1) == USB_FIFO_TX) {
704 /* check if we are already opened */
705 /* we don't need any locks when checking this variable */
706 if (f->curr_cpd != NULL) {
711 /* reset short flag before open */
714 /* call open method */
715 err = (f->methods->f_open) (f, fflags);
719 mtx_lock(f->priv_mtx);
721 /* reset sleep flag */
722 f->flag_sleeping = 0;
724 /* reset error flag */
727 /* reset complete flag */
728 f->flag_iscomplete = 0;
730 /* reset select flag */
731 f->flag_isselect = 0;
733 /* reset flushing flag */
734 f->flag_flushing = 0;
736 /* reset ASYNC proc flag */
739 mtx_lock(&usb_ref_lock);
740 /* flag the fifo as opened to prevent others */
742 mtx_unlock(&usb_ref_lock);
747 mtx_unlock(f->priv_mtx);
752 /*------------------------------------------------------------------------*
754 *------------------------------------------------------------------------*/
756 usb_fifo_reset(struct usb_fifo *f)
764 USB_IF_DEQUEUE(&f->used_q, m);
766 USB_IF_ENQUEUE(&f->free_q, m);
771 /* reset have fragment flag */
772 f->flag_have_fragment = 0;
775 /*------------------------------------------------------------------------*
777 *------------------------------------------------------------------------*/
779 usb_fifo_close(struct usb_fifo *f, int fflags)
783 /* check if we are not opened */
784 if (f->curr_cpd == NULL) {
785 /* nothing to do - already closed */
788 mtx_lock(f->priv_mtx);
790 /* clear current cdev private data pointer */
791 mtx_lock(&usb_ref_lock);
793 mtx_unlock(&usb_ref_lock);
795 /* check if we are watched by kevent */
796 KNOTE_LOCKED(&f->selinfo.si_note, 0);
798 /* check if we are selected */
799 if (f->flag_isselect) {
800 selwakeup(&f->selinfo);
801 f->flag_isselect = 0;
803 /* check if a thread wants SIGIO */
804 if (f->async_p != NULL) {
805 PROC_LOCK(f->async_p);
806 kern_psignal(f->async_p, SIGIO);
807 PROC_UNLOCK(f->async_p);
810 /* remove FWRITE and FREAD flags */
811 fflags &= ~(FWRITE | FREAD);
813 /* flush written data, if any */
814 if ((f->fifo_index & 1) == USB_FIFO_TX) {
816 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);
891 ep = cpd->ep_addr = pd->ep_addr;
893 usb_loc_fill(pd, cpd);
894 err = usb_ref_device(cpd, &refs, 1);
896 DPRINTFN(2, "cannot ref device\n");
900 cpd->fflags = fflags; /* access mode for open lifetime */
902 /* create FIFOs, if any */
903 err = usb_fifo_create(cpd, &refs);
904 /* check for error */
906 DPRINTFN(2, "cannot create fifo\n");
907 usb_unref_device(cpd, &refs);
911 if (fflags & FREAD) {
912 err = usb_fifo_open(cpd, refs.rxfifo, fflags);
914 DPRINTFN(2, "read open failed\n");
915 usb_unref_device(cpd, &refs);
920 if (fflags & FWRITE) {
921 err = usb_fifo_open(cpd, refs.txfifo, fflags);
923 DPRINTFN(2, "write open failed\n");
924 if (fflags & FREAD) {
925 usb_fifo_close(refs.rxfifo, fflags);
927 usb_unref_device(cpd, &refs);
932 usb_unref_device(cpd, &refs);
933 devfs_set_cdevpriv(cpd, usb_close);
938 /*------------------------------------------------------------------------*
939 * usb_close - cdev callback
940 *------------------------------------------------------------------------*/
944 struct usb_cdev_refdata refs;
945 struct usb_cdev_privdata *cpd = arg;
948 DPRINTFN(2, "cpd=%p\n", cpd);
950 err = usb_ref_device(cpd, &refs,
951 2 /* uref and allow detached state */);
953 DPRINTFN(2, "Cannot grab USB reference when "
954 "closing USB file handle\n");
957 if (cpd->fflags & FREAD) {
958 usb_fifo_close(refs.rxfifo, cpd->fflags);
960 if (cpd->fflags & FWRITE) {
961 usb_fifo_close(refs.txfifo, cpd->fflags);
963 usb_unref_device(cpd, &refs);
969 usb_dev_init(void *arg)
971 mtx_init(&usb_ref_lock, "USB ref mutex", NULL, MTX_DEF);
972 sx_init(&usb_sym_lock, "USB sym mutex");
973 TAILQ_INIT(&usb_sym_head);
975 /* check the UGEN methods */
976 usb_fifo_check_methods(&usb_ugen_methods);
979 SYSINIT(usb_dev_init, SI_SUB_KLD, SI_ORDER_FIRST, usb_dev_init, NULL);
982 usb_dev_init_post(void *arg)
985 * Create /dev/usb - this is needed for usbconfig(8), which
986 * needs a well-known device name to access.
988 usb_dev = make_dev(&usb_static_devsw, 0, UID_ROOT, GID_OPERATOR,
989 0644, USB_DEVICE_NAME);
990 if (usb_dev == NULL) {
991 DPRINTFN(0, "Could not create usb bus device\n");
995 SYSINIT(usb_dev_init_post, SI_SUB_KICK_SCHEDULER, SI_ORDER_FIRST, usb_dev_init_post, NULL);
998 usb_dev_uninit(void *arg)
1000 if (usb_dev != NULL) {
1001 destroy_dev(usb_dev);
1004 mtx_destroy(&usb_ref_lock);
1005 sx_destroy(&usb_sym_lock);
1008 SYSUNINIT(usb_dev_uninit, SI_SUB_KICK_SCHEDULER, SI_ORDER_ANY, usb_dev_uninit, NULL);
1011 usb_ioctl_f_sub(struct usb_fifo *f, u_long cmd, void *addr,
1018 *(int *)addr = 0; /* character device */
1022 /* handled by upper FS layer */
1027 if (f->async_p != NULL) {
1031 f->async_p = USB_TD_GET_PROC(td);
1037 /* XXX this is not the most general solution */
1039 if (f->async_p == NULL) {
1043 if (*(int *)addr != USB_PROC_GET_GID(f->async_p)) {
1051 DPRINTFN(3, "cmd 0x%lx = %d\n", cmd, error);
1055 /*------------------------------------------------------------------------*
1056 * usb_ioctl - cdev callback
1057 *------------------------------------------------------------------------*/
1059 usb_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int fflag, struct thread* td)
1061 struct usb_cdev_refdata refs;
1062 struct usb_cdev_privdata* cpd;
1067 DPRINTFN(2, "cmd=0x%lx\n", cmd);
1069 err = devfs_get_cdevpriv((void **)&cpd);
1074 * Performance optimisation: We try to check for IOCTL's that
1075 * don't need the USB reference first. Then we grab the USB
1076 * reference if we need it!
1078 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1082 fflags = cpd->fflags;
1084 f = NULL; /* set default value */
1085 err = ENOIOCTL; /* set default value */
1087 if (fflags & FWRITE) {
1089 err = usb_ioctl_f_sub(f, cmd, addr, td);
1091 if (fflags & FREAD) {
1093 err = usb_ioctl_f_sub(f, cmd, addr, td);
1095 KASSERT(f != NULL, ("fifo not found"));
1096 if (err != ENOIOCTL)
1099 err = (f->methods->f_ioctl) (f, cmd, addr, fflags);
1101 DPRINTFN(2, "f_ioctl cmd 0x%lx = %d\n", cmd, err);
1103 if (err != ENOIOCTL)
1106 if (usb_usb_ref_device(cpd, &refs)) {
1107 /* we lost the reference */
1111 err = (f->methods->f_ioctl_post) (f, cmd, addr, fflags);
1113 DPRINTFN(2, "f_ioctl_post cmd 0x%lx = %d\n", cmd, err);
1115 if (err == ENOIOCTL)
1121 /* Wait for re-enumeration, if any */
1123 while (f->udev->re_enumerate_wait != USB_RE_ENUM_DONE) {
1125 usb_unref_device(cpd, &refs);
1127 usb_pause_mtx(NULL, hz / 128);
1129 while (usb_ref_device(cpd, &refs, 1 /* need uref */)) {
1130 if (usb_ref_device(cpd, &refs, 0)) {
1131 /* device no longer exists */
1134 usb_unref_device(cpd, &refs);
1135 usb_pause_mtx(NULL, hz / 128);
1140 usb_unref_device(cpd, &refs);
1145 usb_filter_detach(struct knote *kn)
1147 struct usb_fifo *f = kn->kn_hook;
1148 knlist_remove(&f->selinfo.si_note, kn, 0);
1152 usb_filter_write(struct knote *kn, long hint)
1154 struct usb_cdev_privdata* cpd;
1162 mtx_assert(f->priv_mtx, MA_OWNED);
1167 } else if (f->fs_ep_max == 0) {
1168 if (f->flag_iserror) {
1169 /* we got an error */
1172 if (f->queue_data == NULL) {
1174 * start write transfer, if not
1177 (f->methods->f_start_write) (f);
1179 /* check if any packets are available */
1180 USB_IF_POLL(&f->free_q, m);
1183 if (f->flag_iscomplete) {
1193 usb_filter_read(struct knote *kn, long hint)
1195 struct usb_cdev_privdata* cpd;
1203 mtx_assert(f->priv_mtx, MA_OWNED);
1208 } else if (f->fs_ep_max == 0) {
1209 if (f->flag_iserror) {
1210 /* we have an error */
1213 if (f->queue_data == NULL) {
1215 * start read transfer, if not
1218 (f->methods->f_start_read) (f);
1220 /* check if any packets are available */
1221 USB_IF_POLL(&f->used_q, m);
1223 /* start reading data, if any */
1225 (f->methods->f_start_read) (f);
1228 if (f->flag_iscomplete) {
1237 static struct filterops usb_filtops_write = {
1239 .f_detach = usb_filter_detach,
1240 .f_event = usb_filter_write,
1243 static struct filterops usb_filtops_read = {
1245 .f_detach = usb_filter_detach,
1246 .f_event = usb_filter_read,
1252 usb_kqfilter(struct cdev* dev, struct knote *kn)
1254 struct usb_cdev_refdata refs;
1255 struct usb_cdev_privdata* cpd;
1262 if (devfs_get_cdevpriv((void **)&cpd) != 0 ||
1263 usb_ref_device(cpd, &refs, 0) != 0)
1266 fflags = cpd->fflags;
1268 /* Figure out who needs service */
1269 switch (kn->kn_filter) {
1271 if (fflags & FWRITE) {
1273 kn->kn_fop = &usb_filtops_write;
1278 if (fflags & FREAD) {
1280 kn->kn_fop = &usb_filtops_read;
1291 mtx_lock(f->priv_mtx);
1292 knlist_add(&f->selinfo.si_note, kn, 1);
1293 mtx_unlock(f->priv_mtx);
1296 usb_unref_device(cpd, &refs);
1302 usb_poll(struct cdev* dev, int events, struct thread* td)
1304 struct usb_cdev_refdata refs;
1305 struct usb_cdev_privdata* cpd;
1308 int fflags, revents;
1310 if (devfs_get_cdevpriv((void **)&cpd) != 0 ||
1311 usb_ref_device(cpd, &refs, 0) != 0)
1313 (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM));
1315 fflags = cpd->fflags;
1317 /* Figure out who needs service */
1319 if ((events & (POLLOUT | POLLWRNORM)) &&
1320 (fflags & FWRITE)) {
1324 mtx_lock(f->priv_mtx);
1326 if (!refs.is_usbfs) {
1327 if (f->flag_iserror) {
1328 /* we got an error */
1331 if (f->queue_data == NULL) {
1333 * start write transfer, if not
1336 (f->methods->f_start_write) (f);
1338 /* check if any packets are available */
1339 USB_IF_POLL(&f->free_q, m);
1342 if (f->flag_iscomplete) {
1350 revents |= events & (POLLOUT | POLLWRNORM);
1352 f->flag_isselect = 1;
1353 selrecord(td, &f->selinfo);
1356 mtx_unlock(f->priv_mtx);
1358 if ((events & (POLLIN | POLLRDNORM)) &&
1363 mtx_lock(f->priv_mtx);
1365 if (!refs.is_usbfs) {
1366 if (f->flag_iserror) {
1367 /* we have an error */
1370 if (f->queue_data == NULL) {
1372 * start read transfer, if not
1375 (f->methods->f_start_read) (f);
1377 /* check if any packets are available */
1378 USB_IF_POLL(&f->used_q, m);
1381 if (f->flag_iscomplete) {
1389 revents |= events & (POLLIN | POLLRDNORM);
1391 f->flag_isselect = 1;
1392 selrecord(td, &f->selinfo);
1394 if (!refs.is_usbfs) {
1395 /* start reading data */
1396 (f->methods->f_start_read) (f);
1400 mtx_unlock(f->priv_mtx);
1402 usb_unref_device(cpd, &refs);
1407 usb_read(struct cdev *dev, struct uio *uio, int ioflag)
1409 struct usb_cdev_refdata refs;
1410 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 */ );
1427 fflags = cpd->fflags;
1431 /* should not happen */
1432 usb_unref_device(cpd, &refs);
1436 resid = uio->uio_resid;
1438 mtx_lock(f->priv_mtx);
1440 /* check for permanent read error */
1441 if (f->flag_iserror) {
1445 /* check if USB-FS interface is active */
1446 if (refs.is_usbfs) {
1448 * The queue is used for events that should be
1449 * retrieved using the "USB_FS_COMPLETE" ioctl.
1454 while (uio->uio_resid > 0) {
1456 USB_IF_DEQUEUE(&f->used_q, m);
1460 /* start read transfer, if not already started */
1462 (f->methods->f_start_read) (f);
1464 if (ioflag & IO_NDELAY) {
1466 /* return length before error */
1472 DPRINTF("sleeping\n");
1474 err = usb_fifo_wait(f);
1480 if (f->methods->f_filter_read) {
1482 * Sometimes it is convenient to process data at the
1483 * expense of a userland process instead of a kernel
1486 (f->methods->f_filter_read) (f, m);
1490 io_len = MIN(m->cur_data_len, uio->uio_resid);
1492 DPRINTFN(2, "transfer %d bytes from %p\n",
1493 io_len, m->cur_data_ptr);
1495 err = usb_fifo_uiomove(f,
1496 m->cur_data_ptr, io_len, uio);
1498 m->cur_data_len -= io_len;
1499 m->cur_data_ptr += io_len;
1501 if (m->cur_data_len == 0) {
1503 uint8_t last_packet;
1505 last_packet = m->last_packet;
1507 USB_IF_ENQUEUE(&f->free_q, m);
1514 USB_IF_PREPEND(&f->used_q, m);
1522 mtx_unlock(f->priv_mtx);
1524 usb_unref_device(cpd, &refs);
1530 usb_write(struct cdev *dev, struct uio *uio, int ioflag)
1532 struct usb_cdev_refdata refs;
1533 struct usb_cdev_privdata* cpd;
1541 uint8_t tr_data = 0;
1545 err = devfs_get_cdevpriv((void **)&cpd);
1549 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1553 fflags = cpd->fflags;
1557 /* should not happen */
1558 usb_unref_device(cpd, &refs);
1561 resid = uio->uio_resid;
1563 mtx_lock(f->priv_mtx);
1565 /* check for permanent write error */
1566 if (f->flag_iserror) {
1570 /* check if USB-FS interface is active */
1571 if (refs.is_usbfs) {
1573 * The queue is used for events that should be
1574 * retrieved using the "USB_FS_COMPLETE" ioctl.
1579 if (f->queue_data == NULL) {
1580 /* start write transfer, if not already started */
1581 (f->methods->f_start_write) (f);
1583 /* we allow writing zero length data */
1585 USB_IF_DEQUEUE(&f->free_q, m);
1589 if (ioflag & IO_NDELAY) {
1591 /* return length before error */
1597 DPRINTF("sleeping\n");
1599 err = usb_fifo_wait(f);
1607 if (f->flag_have_fragment == 0) {
1609 io_len = m->cur_data_len;
1610 pdata = m->cur_data_ptr;
1611 if (io_len > uio->uio_resid)
1612 io_len = uio->uio_resid;
1613 m->cur_data_len = io_len;
1615 io_len = m->max_data_len - m->cur_data_len;
1616 pdata = m->cur_data_ptr + m->cur_data_len;
1617 if (io_len > uio->uio_resid)
1618 io_len = uio->uio_resid;
1619 m->cur_data_len += io_len;
1622 DPRINTFN(2, "transfer %d bytes to %p\n",
1625 err = usb_fifo_uiomove(f, pdata, io_len, uio);
1628 f->flag_have_fragment = 0;
1629 USB_IF_ENQUEUE(&f->free_q, m);
1633 /* check if the buffer is ready to be transmitted */
1635 if ((f->flag_write_defrag == 0) ||
1636 (m->cur_data_len == m->max_data_len)) {
1637 f->flag_have_fragment = 0;
1640 * Check for write filter:
1642 * Sometimes it is convenient to process data
1643 * at the expense of a userland process
1644 * instead of a kernel process.
1646 if (f->methods->f_filter_write) {
1647 (f->methods->f_filter_write) (f, m);
1650 /* Put USB mbuf in the used queue */
1651 USB_IF_ENQUEUE(&f->used_q, m);
1653 /* Start writing data, if not already started */
1654 (f->methods->f_start_write) (f);
1656 /* Wait for more data or close */
1657 f->flag_have_fragment = 1;
1658 USB_IF_PREPEND(&f->free_q, m);
1661 } while (uio->uio_resid > 0);
1663 mtx_unlock(f->priv_mtx);
1665 usb_unref_device(cpd, &refs);
1671 usb_static_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
1675 struct usb_read_dir *urd;
1683 err = usb_read_symlink(u.urd->urd_data,
1684 u.urd->urd_startentry, u.urd->urd_maxlen);
1686 case USB_DEV_QUIRK_GET:
1687 case USB_QUIRK_NAME_GET:
1688 case USB_DEV_QUIRK_ADD:
1689 case USB_DEV_QUIRK_REMOVE:
1690 err = usb_quirk_ioctl_p(cmd, data, fflag, td);
1692 case USB_GET_TEMPLATE:
1693 *(int *)data = usb_template;
1696 case USB_SET_TEMPLATE:
1697 err = priv_check(curthread, PRIV_DRIVER);
1700 usb_template = *(int *)data;
1710 usb_fifo_uiomove(struct usb_fifo *f, void *cp,
1711 int n, struct uio *uio)
1715 mtx_unlock(f->priv_mtx);
1718 * "uiomove()" can sleep so one needs to make a wrapper,
1719 * exiting the mutex and checking things:
1721 error = uiomove(cp, n, uio);
1723 mtx_lock(f->priv_mtx);
1729 usb_fifo_wait(struct usb_fifo *f)
1733 mtx_assert(f->priv_mtx, MA_OWNED);
1735 if (f->flag_iserror) {
1739 f->flag_sleeping = 1;
1741 err = cv_wait_sig(&f->cv_io, f->priv_mtx);
1743 if (f->flag_iserror) {
1751 usb_fifo_signal(struct usb_fifo *f)
1753 if (f->flag_sleeping) {
1754 f->flag_sleeping = 0;
1755 cv_broadcast(&f->cv_io);
1760 usb_fifo_wakeup(struct usb_fifo *f)
1764 KNOTE_LOCKED(&f->selinfo.si_note, 0);
1766 if (f->flag_isselect) {
1767 selwakeup(&f->selinfo);
1768 f->flag_isselect = 0;
1770 if (f->async_p != NULL) {
1771 PROC_LOCK(f->async_p);
1772 kern_psignal(f->async_p, SIGIO);
1773 PROC_UNLOCK(f->async_p);
1778 usb_fifo_dummy_open(struct usb_fifo *fifo, int fflags)
1784 usb_fifo_dummy_close(struct usb_fifo *fifo, int fflags)
1790 usb_fifo_dummy_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
1796 usb_fifo_dummy_cmd(struct usb_fifo *fifo)
1798 fifo->flag_flushing = 0; /* not flushing */
1802 usb_fifo_check_methods(struct usb_fifo_methods *pm)
1804 /* check that all callback functions are OK */
1806 if (pm->f_open == NULL)
1807 pm->f_open = &usb_fifo_dummy_open;
1809 if (pm->f_close == NULL)
1810 pm->f_close = &usb_fifo_dummy_close;
1812 if (pm->f_ioctl == NULL)
1813 pm->f_ioctl = &usb_fifo_dummy_ioctl;
1815 if (pm->f_ioctl_post == NULL)
1816 pm->f_ioctl_post = &usb_fifo_dummy_ioctl;
1818 if (pm->f_start_read == NULL)
1819 pm->f_start_read = &usb_fifo_dummy_cmd;
1821 if (pm->f_stop_read == NULL)
1822 pm->f_stop_read = &usb_fifo_dummy_cmd;
1824 if (pm->f_start_write == NULL)
1825 pm->f_start_write = &usb_fifo_dummy_cmd;
1827 if (pm->f_stop_write == NULL)
1828 pm->f_stop_write = &usb_fifo_dummy_cmd;
1831 /*------------------------------------------------------------------------*
1834 * The following function will create a duplex FIFO.
1839 *------------------------------------------------------------------------*/
1841 usb_fifo_attach(struct usb_device *udev, void *priv_sc,
1842 struct mtx *priv_mtx, struct usb_fifo_methods *pm,
1843 struct usb_fifo_sc *f_sc, uint16_t unit, int16_t subunit,
1844 uint8_t iface_index, uid_t uid, gid_t gid, int mode)
1846 struct usb_fifo *f_tx;
1847 struct usb_fifo *f_rx;
1851 f_sc->fp[USB_FIFO_TX] = NULL;
1852 f_sc->fp[USB_FIFO_RX] = NULL;
1857 /* check the methods */
1858 usb_fifo_check_methods(pm);
1860 if (priv_mtx == NULL)
1863 /* search for a free FIFO slot */
1864 for (n = 0;; n += 2) {
1866 if (n == USB_FIFO_MAX) {
1867 /* end of FIFOs reached */
1870 /* Check for TX FIFO */
1871 if (udev->fifo[n + USB_FIFO_TX] != NULL) {
1874 /* Check for RX FIFO */
1875 if (udev->fifo[n + USB_FIFO_RX] != NULL) {
1881 f_tx = usb_fifo_alloc(priv_mtx);
1882 f_rx = usb_fifo_alloc(priv_mtx);
1884 if ((f_tx == NULL) || (f_rx == NULL)) {
1885 usb_fifo_free(f_tx);
1886 usb_fifo_free(f_rx);
1889 /* initialise FIFO structures */
1891 f_tx->fifo_index = n + USB_FIFO_TX;
1892 f_tx->dev_ep_index = -1;
1893 f_tx->priv_sc0 = priv_sc;
1895 f_tx->iface_index = iface_index;
1898 f_rx->fifo_index = n + USB_FIFO_RX;
1899 f_rx->dev_ep_index = -1;
1900 f_rx->priv_sc0 = priv_sc;
1902 f_rx->iface_index = iface_index;
1905 f_sc->fp[USB_FIFO_TX] = f_tx;
1906 f_sc->fp[USB_FIFO_RX] = f_rx;
1908 mtx_lock(&usb_ref_lock);
1909 udev->fifo[f_tx->fifo_index] = f_tx;
1910 udev->fifo[f_rx->fifo_index] = f_rx;
1911 mtx_unlock(&usb_ref_lock);
1913 for (n = 0; n != 4; n++) {
1915 if (pm->basename[n] == NULL) {
1919 if (snprintf(devname, sizeof(devname),
1920 "%s%u%s", pm->basename[n],
1921 unit, pm->postfix[n] ?
1922 pm->postfix[n] : "")) {
1926 if (snprintf(devname, sizeof(devname),
1927 "%s%u.%d%s", pm->basename[n],
1928 unit, subunit, pm->postfix[n] ?
1929 pm->postfix[n] : "")) {
1935 * Distribute the symbolic links into two FIFO structures:
1938 f_rx->symlink[n / 2] =
1939 usb_alloc_symlink(devname);
1941 f_tx->symlink[n / 2] =
1942 usb_alloc_symlink(devname);
1945 /* Create the device */
1946 f_sc->dev = usb_make_dev(udev, devname, -1,
1947 f_tx->fifo_index & f_rx->fifo_index,
1948 FREAD|FWRITE, uid, gid, mode);
1951 DPRINTFN(2, "attached %p/%p\n", f_tx, f_rx);
1955 /*------------------------------------------------------------------------*
1956 * usb_fifo_alloc_buffer
1961 *------------------------------------------------------------------------*/
1963 usb_fifo_alloc_buffer(struct usb_fifo *f, usb_size_t bufsize,
1966 usb_fifo_free_buffer(f);
1968 /* allocate an endpoint */
1969 f->free_q.ifq_maxlen = nbuf;
1970 f->used_q.ifq_maxlen = nbuf;
1972 f->queue_data = usb_alloc_mbufs(
1973 M_USBDEV, &f->free_q, bufsize, nbuf);
1975 if ((f->queue_data == NULL) && bufsize && nbuf) {
1978 return (0); /* success */
1981 /*------------------------------------------------------------------------*
1982 * usb_fifo_free_buffer
1984 * This function will free the buffers associated with a FIFO. This
1985 * function can be called multiple times in a row.
1986 *------------------------------------------------------------------------*/
1988 usb_fifo_free_buffer(struct usb_fifo *f)
1990 if (f->queue_data) {
1991 /* free old buffer */
1992 free(f->queue_data, M_USBDEV);
1993 f->queue_data = NULL;
1997 memset(&f->free_q, 0, sizeof(f->free_q));
1998 memset(&f->used_q, 0, sizeof(f->used_q));
2002 usb_fifo_detach(struct usb_fifo_sc *f_sc)
2007 usb_fifo_free(f_sc->fp[USB_FIFO_TX]);
2008 usb_fifo_free(f_sc->fp[USB_FIFO_RX]);
2010 f_sc->fp[USB_FIFO_TX] = NULL;
2011 f_sc->fp[USB_FIFO_RX] = NULL;
2013 usb_destroy_dev(f_sc->dev);
2017 DPRINTFN(2, "detached %p\n", f_sc);
2021 usb_fifo_put_bytes_max(struct usb_fifo *f)
2026 USB_IF_POLL(&f->free_q, m);
2029 len = m->max_data_len;
2036 /*------------------------------------------------------------------------*
2040 * 0 - normal operation
2041 * 1 - set last packet flag to enforce framing
2042 *------------------------------------------------------------------------*/
2044 usb_fifo_put_data(struct usb_fifo *f, struct usb_page_cache *pc,
2045 usb_frlength_t offset, usb_frlength_t len, uint8_t what)
2048 usb_frlength_t io_len;
2050 while (len || (what == 1)) {
2052 USB_IF_DEQUEUE(&f->free_q, m);
2057 io_len = MIN(len, m->cur_data_len);
2059 usbd_copy_out(pc, offset, m->cur_data_ptr, io_len);
2061 m->cur_data_len = io_len;
2065 if ((len == 0) && (what == 1)) {
2068 USB_IF_ENQUEUE(&f->used_q, m);
2072 if ((len == 0) || (what == 1)) {
2082 usb_fifo_put_data_linear(struct usb_fifo *f, void *ptr,
2083 usb_size_t len, uint8_t what)
2088 while (len || (what == 1)) {
2090 USB_IF_DEQUEUE(&f->free_q, m);
2095 io_len = MIN(len, m->cur_data_len);
2097 memcpy(m->cur_data_ptr, ptr, io_len);
2099 m->cur_data_len = io_len;
2100 ptr = USB_ADD_BYTES(ptr, io_len);
2103 if ((len == 0) && (what == 1)) {
2106 USB_IF_ENQUEUE(&f->used_q, m);
2110 if ((len == 0) || (what == 1)) {
2120 usb_fifo_put_data_buffer(struct usb_fifo *f, void *ptr, usb_size_t len)
2124 USB_IF_DEQUEUE(&f->free_q, m);
2127 m->cur_data_len = len;
2128 m->cur_data_ptr = ptr;
2129 USB_IF_ENQUEUE(&f->used_q, m);
2137 usb_fifo_put_data_error(struct usb_fifo *f)
2139 f->flag_iserror = 1;
2143 /*------------------------------------------------------------------------*
2147 * 0 - normal operation
2148 * 1 - only get one "usb_mbuf"
2152 * 1 - data in buffer
2153 *------------------------------------------------------------------------*/
2155 usb_fifo_get_data(struct usb_fifo *f, struct usb_page_cache *pc,
2156 usb_frlength_t offset, usb_frlength_t len, usb_frlength_t *actlen,
2160 usb_frlength_t io_len;
2161 uint8_t tr_data = 0;
2167 USB_IF_DEQUEUE(&f->used_q, m);
2173 io_len = MIN(len, m->cur_data_len);
2175 usbd_copy_in(pc, offset, m->cur_data_ptr, io_len);
2179 actlen[0] += io_len;
2180 m->cur_data_ptr += io_len;
2181 m->cur_data_len -= io_len;
2183 if ((m->cur_data_len == 0) || (what == 1)) {
2184 USB_IF_ENQUEUE(&f->free_q, m);
2192 USB_IF_PREPEND(&f->used_q, m);
2197 /* wait for data to be written out */
2200 if (f->flag_flushing) {
2201 /* check if we should send a short packet */
2202 if (f->flag_short != 0) {
2207 /* flushing complete */
2208 f->flag_flushing = 0;
2221 usb_fifo_get_data_linear(struct usb_fifo *f, void *ptr,
2222 usb_size_t len, usb_size_t *actlen, uint8_t what)
2226 uint8_t tr_data = 0;
2232 USB_IF_DEQUEUE(&f->used_q, m);
2238 io_len = MIN(len, m->cur_data_len);
2240 memcpy(ptr, m->cur_data_ptr, io_len);
2243 ptr = USB_ADD_BYTES(ptr, io_len);
2244 actlen[0] += io_len;
2245 m->cur_data_ptr += io_len;
2246 m->cur_data_len -= io_len;
2248 if ((m->cur_data_len == 0) || (what == 1)) {
2249 USB_IF_ENQUEUE(&f->free_q, m);
2257 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);
2324 /* XXX no longer needed */
2325 strlcpy(ps->src_path, target, sizeof(ps->src_path));
2326 ps->src_len = strlen(ps->src_path);
2327 strlcpy(ps->dst_path, target, sizeof(ps->dst_path));
2328 ps->dst_len = strlen(ps->dst_path);
2330 sx_xlock(&usb_sym_lock);
2331 TAILQ_INSERT_TAIL(&usb_sym_head, ps, sym_entry);
2332 sx_unlock(&usb_sym_lock);
2336 /*------------------------------------------------------------------------*
2338 *------------------------------------------------------------------------*/
2340 usb_free_symlink(struct usb_symlink *ps)
2345 sx_xlock(&usb_sym_lock);
2346 TAILQ_REMOVE(&usb_sym_head, ps, sym_entry);
2347 sx_unlock(&usb_sym_lock);
2352 /*------------------------------------------------------------------------*
2358 *------------------------------------------------------------------------*/
2360 usb_read_symlink(uint8_t *user_ptr, uint32_t startentry, uint32_t user_len)
2362 struct usb_symlink *ps;
2368 sx_xlock(&usb_sym_lock);
2370 TAILQ_FOREACH(ps, &usb_sym_head, sym_entry) {
2373 * Compute total length of source and destination symlink
2374 * strings pluss one length byte and two NUL bytes:
2376 temp = ps->src_len + ps->dst_len + 3;
2380 * Skip entry because this length cannot fit
2385 if (startentry != 0) {
2386 /* decrement read offset */
2390 if (temp > user_len) {
2391 /* out of buffer space */
2396 /* copy out total length */
2398 error = copyout(&len,
2399 USB_ADD_BYTES(user_ptr, delta), 1);
2405 /* copy out source string */
2407 error = copyout(ps->src_path,
2408 USB_ADD_BYTES(user_ptr, delta), ps->src_len);
2413 delta += ps->src_len;
2414 error = copyout(&len,
2415 USB_ADD_BYTES(user_ptr, delta), 1);
2421 /* copy out destination string */
2423 error = copyout(ps->dst_path,
2424 USB_ADD_BYTES(user_ptr, delta), ps->dst_len);
2429 delta += ps->dst_len;
2430 error = copyout(&len,
2431 USB_ADD_BYTES(user_ptr, delta), 1);
2440 /* a zero length entry indicates the end */
2442 if ((user_len != 0) && (error == 0)) {
2446 error = copyout(&len,
2447 USB_ADD_BYTES(user_ptr, delta), 1);
2449 sx_unlock(&usb_sym_lock);
2454 usb_fifo_set_close_zlp(struct usb_fifo *f, uint8_t onoff)
2459 /* send a Zero Length Packet, ZLP, before close */
2460 f->flag_short = onoff;
2464 usb_fifo_set_write_defrag(struct usb_fifo *f, uint8_t onoff)
2469 /* defrag written data */
2470 f->flag_write_defrag = onoff;
2471 /* reset defrag state */
2472 f->flag_have_fragment = 0;
2476 usb_fifo_softc(struct usb_fifo *f)
2478 return (f->priv_sc0);
2480 #endif /* USB_HAVE_UGEN */