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 #include <sys/stdint.h>
31 #include <sys/stddef.h>
32 #include <sys/param.h>
33 #include <sys/queue.h>
34 #include <sys/types.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/module.h>
40 #include <sys/mutex.h>
41 #include <sys/condvar.h>
42 #include <sys/sysctl.h>
44 #include <sys/unistd.h>
45 #include <sys/callout.h>
46 #include <sys/malloc.h>
48 #include <sys/vnode.h>
50 #include <sys/fcntl.h>
52 #include <dev/usb/usb.h>
53 #include <dev/usb/usb_ioctl.h>
54 #include <dev/usb/usbdi.h>
55 #include <dev/usb/usbdi_util.h>
57 #define USB_DEBUG_VAR usb_fifo_debug
59 #include <dev/usb/usb_core.h>
60 #include <dev/usb/usb_dev.h>
61 #include <dev/usb/usb_mbuf.h>
62 #include <dev/usb/usb_process.h>
63 #include <dev/usb/usb_device.h>
64 #include <dev/usb/usb_debug.h>
65 #include <dev/usb/usb_busdma.h>
66 #include <dev/usb/usb_generic.h>
67 #include <dev/usb/usb_dynamic.h>
68 #include <dev/usb/usb_util.h>
70 #include <dev/usb/usb_controller.h>
71 #include <dev/usb/usb_bus.h>
73 #include <sys/filio.h>
74 #include <sys/ttycom.h>
75 #include <sys/syscallsubr.h>
77 #include <machine/stdarg.h>
82 static int usb_fifo_debug = 0;
84 static SYSCTL_NODE(_hw_usb, OID_AUTO, dev, CTLFLAG_RW, 0, "USB device");
85 SYSCTL_INT(_hw_usb_dev, OID_AUTO, debug, CTLFLAG_RW | CTLFLAG_TUN,
86 &usb_fifo_debug, 0, "Debug Level");
87 TUNABLE_INT("hw.usb.dev.debug", &usb_fifo_debug);
90 #if ((__FreeBSD_version >= 700001) || (__FreeBSD_version == 0) || \
91 ((__FreeBSD_version >= 600034) && (__FreeBSD_version < 700000)))
92 #define USB_UCRED struct ucred *ucred,
99 static int usb_fifo_open(struct usb_cdev_privdata *,
100 struct usb_fifo *, int);
101 static void usb_fifo_close(struct usb_fifo *, int);
102 static void usb_dev_init(void *);
103 static void usb_dev_init_post(void *);
104 static void usb_dev_uninit(void *);
105 static int usb_fifo_uiomove(struct usb_fifo *, void *, int,
107 static void usb_fifo_check_methods(struct usb_fifo_methods *);
108 static struct usb_fifo *usb_fifo_alloc(struct mtx *);
109 static struct usb_endpoint *usb_dev_get_ep(struct usb_device *, uint8_t,
111 static void usb_loc_fill(struct usb_fs_privdata *,
112 struct usb_cdev_privdata *);
113 static void usb_close(void *);
114 static usb_error_t usb_ref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *, int);
115 static usb_error_t usb_usb_ref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *);
116 static void usb_unref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *);
118 static d_open_t usb_open;
119 static d_ioctl_t usb_ioctl;
120 static d_read_t usb_read;
121 static d_write_t usb_write;
122 static d_poll_t usb_poll;
123 static d_kqfilter_t usb_kqfilter;
125 static d_ioctl_t usb_static_ioctl;
127 static usb_fifo_open_t usb_fifo_dummy_open;
128 static usb_fifo_close_t usb_fifo_dummy_close;
129 static usb_fifo_ioctl_t usb_fifo_dummy_ioctl;
130 static usb_fifo_cmd_t usb_fifo_dummy_cmd;
132 /* character device structure used for devices (/dev/ugenX.Y and /dev/uXXX) */
133 struct cdevsw usb_devsw = {
134 .d_version = D_VERSION,
136 .d_ioctl = usb_ioctl,
138 .d_flags = D_TRACKCLOSE,
140 .d_write = usb_write,
142 .d_kqfilter = usb_kqfilter,
145 static struct cdev* usb_dev = NULL;
147 /* character device structure used for /dev/usb */
148 static struct cdevsw usb_static_devsw = {
149 .d_version = D_VERSION,
150 .d_ioctl = usb_static_ioctl,
154 static TAILQ_HEAD(, usb_symlink) usb_sym_head;
155 static struct sx usb_sym_lock;
157 struct mtx usb_ref_lock;
159 /*------------------------------------------------------------------------*
162 * This is used to fill out a usb_cdev_privdata structure based on the
163 * device's address as contained in usb_fs_privdata.
164 *------------------------------------------------------------------------*/
166 usb_loc_fill(struct usb_fs_privdata* pd, struct usb_cdev_privdata *cpd)
168 cpd->bus_index = pd->bus_index;
169 cpd->dev_index = pd->dev_index;
170 cpd->ep_addr = pd->ep_addr;
171 cpd->fifo_index = pd->fifo_index;
174 /*------------------------------------------------------------------------*
177 * This function is used to atomically refer an USB device by its
178 * device location. If this function returns success the USB device
179 * will not dissappear until the USB device is unreferenced.
182 * 0: Success, refcount incremented on the given USB device.
184 *------------------------------------------------------------------------*/
186 usb_ref_device(struct usb_cdev_privdata *cpd,
187 struct usb_cdev_refdata *crd, int need_uref)
189 struct usb_fifo **ppf;
192 DPRINTFN(2, "cpd=%p need uref=%d\n", cpd, need_uref);
195 memset(crd, 0, sizeof(*crd));
197 mtx_lock(&usb_ref_lock);
198 cpd->bus = devclass_get_softc(usb_devclass_ptr, cpd->bus_index);
199 if (cpd->bus == NULL) {
200 DPRINTFN(2, "no bus at %u\n", cpd->bus_index);
203 cpd->udev = cpd->bus->devices[cpd->dev_index];
204 if (cpd->udev == NULL) {
205 DPRINTFN(2, "no device at %u\n", cpd->dev_index);
208 if (cpd->udev->state == USB_STATE_DETACHED &&
210 DPRINTFN(2, "device is detached\n");
214 DPRINTFN(2, "ref udev - needed\n");
216 if (cpd->udev->refcount == USB_DEV_REF_MAX) {
217 DPRINTFN(2, "no dev ref\n");
220 cpd->udev->refcount++;
222 mtx_unlock(&usb_ref_lock);
225 * We need to grab the enumeration SX-lock before
226 * grabbing the FIFO refs to avoid deadlock at detach!
228 crd->do_unlock = usbd_enum_lock(cpd->udev);
230 mtx_lock(&usb_ref_lock);
233 * Set "is_uref" after grabbing the default SX lock
238 /* check if we are doing an open */
239 if (cpd->fflags == 0) {
240 /* use zero defaults */
242 /* check for write */
243 if (cpd->fflags & FWRITE) {
244 ppf = cpd->udev->fifo;
245 f = ppf[cpd->fifo_index + USB_FIFO_TX];
247 crd->is_write = 1; /* ref */
248 if (f == NULL || f->refcount == USB_FIFO_REF_MAX)
250 if (f->curr_cpd != cpd)
252 /* check if USB-FS is active */
253 if (f->fs_ep_max != 0) {
259 if (cpd->fflags & FREAD) {
260 ppf = cpd->udev->fifo;
261 f = ppf[cpd->fifo_index + USB_FIFO_RX];
263 crd->is_read = 1; /* ref */
264 if (f == NULL || f->refcount == USB_FIFO_REF_MAX)
266 if (f->curr_cpd != cpd)
268 /* check if USB-FS is active */
269 if (f->fs_ep_max != 0) {
275 /* when everything is OK we increment the refcounts */
277 DPRINTFN(2, "ref write\n");
278 crd->txfifo->refcount++;
281 DPRINTFN(2, "ref read\n");
282 crd->rxfifo->refcount++;
284 mtx_unlock(&usb_ref_lock);
290 usbd_enum_unlock(cpd->udev);
293 cpd->udev->refcount--;
294 cv_broadcast(&cpd->udev->ref_cv);
296 mtx_unlock(&usb_ref_lock);
297 DPRINTFN(2, "fail\n");
298 return (USB_ERR_INVAL);
301 /*------------------------------------------------------------------------*
304 * This function is used to upgrade an USB reference to include the
305 * USB device reference on a USB location.
308 * 0: Success, refcount incremented on the given USB device.
310 *------------------------------------------------------------------------*/
312 usb_usb_ref_device(struct usb_cdev_privdata *cpd,
313 struct usb_cdev_refdata *crd)
316 * Check if we already got an USB reference on this location:
319 return (0); /* success */
322 * To avoid deadlock at detach we need to drop the FIFO ref
323 * and re-acquire a new ref!
325 usb_unref_device(cpd, crd);
327 return (usb_ref_device(cpd, crd, 1 /* need uref */));
330 /*------------------------------------------------------------------------*
333 * This function will release the reference count by one unit for the
335 *------------------------------------------------------------------------*/
337 usb_unref_device(struct usb_cdev_privdata *cpd,
338 struct usb_cdev_refdata *crd)
341 DPRINTFN(2, "cpd=%p is_uref=%d\n", cpd, crd->is_uref);
344 usbd_enum_unlock(cpd->udev);
346 mtx_lock(&usb_ref_lock);
348 if (--(crd->rxfifo->refcount) == 0) {
349 cv_signal(&crd->rxfifo->cv_drain);
354 if (--(crd->txfifo->refcount) == 0) {
355 cv_signal(&crd->txfifo->cv_drain);
361 cpd->udev->refcount--;
362 cv_broadcast(&cpd->udev->ref_cv);
364 mtx_unlock(&usb_ref_lock);
367 static struct usb_fifo *
368 usb_fifo_alloc(struct mtx *mtx)
372 f = malloc(sizeof(*f), M_USBDEV, M_WAITOK | M_ZERO);
374 cv_init(&f->cv_io, "FIFO-IO");
375 cv_init(&f->cv_drain, "FIFO-DRAIN");
378 knlist_init_mtx(&f->selinfo.si_note, mtx);
383 /*------------------------------------------------------------------------*
385 *------------------------------------------------------------------------*/
387 usb_fifo_create(struct usb_cdev_privdata *cpd,
388 struct usb_cdev_refdata *crd)
390 struct usb_device *udev = cpd->udev;
392 struct usb_endpoint *ep;
398 int e = cpd->ep_addr;
400 is_tx = (cpd->fflags & FWRITE) ? 1 : 0;
401 is_rx = (cpd->fflags & FREAD) ? 1 : 0;
405 /* Preallocated FIFO */
407 DPRINTFN(5, "Preallocated FIFO\n");
409 f = udev->fifo[cpd->fifo_index + USB_FIFO_TX];
415 f = udev->fifo[cpd->fifo_index + USB_FIFO_RX];
423 KASSERT(e >= 0 && e <= 15, ("endpoint %d out of range", e));
425 /* search for a free FIFO slot */
426 DPRINTFN(5, "Endpoint device, searching for 0x%02x\n", e);
427 for (n = 0;; n += 2) {
429 if (n == USB_FIFO_MAX) {
434 /* end of FIFOs reached */
435 DPRINTFN(5, "out of FIFOs\n");
439 /* Check for TX FIFO */
441 f = udev->fifo[n + USB_FIFO_TX];
443 if (f->dev_ep_index != e) {
444 /* wrong endpoint index */
447 if (f->curr_cpd != NULL) {
452 } else if (no_null) {
456 /* Check for RX FIFO */
458 f = udev->fifo[n + USB_FIFO_RX];
460 if (f->dev_ep_index != e) {
461 /* wrong endpoint index */
464 if (f->curr_cpd != NULL) {
469 } else if (no_null) {
477 if (e >= (USB_EP_MAX / 2)) {
478 /* we don't create any endpoints in this range */
479 DPRINTFN(5, "ep out of range\n");
480 return (is_busy ? EBUSY : EINVAL);
484 if ((e != 0) && is_busy) {
486 * Only the default control endpoint is allowed to be
487 * opened multiple times!
489 DPRINTFN(5, "busy\n");
495 (udev->fifo[n + USB_FIFO_TX] == NULL)) {
496 ep = usb_dev_get_ep(udev, e, USB_FIFO_TX);
497 DPRINTFN(5, "dev_get_endpoint(%d, 0x%x)\n", e, USB_FIFO_TX);
499 DPRINTFN(5, "dev_get_endpoint returned NULL\n");
502 f = usb_fifo_alloc(&udev->device_mtx);
504 DPRINTFN(5, "could not alloc tx fifo\n");
507 /* update some fields */
508 f->fifo_index = n + USB_FIFO_TX;
511 f->methods = &usb_ugen_methods;
512 f->iface_index = ep->iface_index;
514 mtx_lock(&usb_ref_lock);
515 udev->fifo[n + USB_FIFO_TX] = f;
516 mtx_unlock(&usb_ref_lock);
520 (udev->fifo[n + USB_FIFO_RX] == NULL)) {
522 ep = usb_dev_get_ep(udev, e, USB_FIFO_RX);
523 DPRINTFN(5, "dev_get_endpoint(%d, 0x%x)\n", e, USB_FIFO_RX);
525 DPRINTFN(5, "dev_get_endpoint returned NULL\n");
528 f = usb_fifo_alloc(&udev->device_mtx);
530 DPRINTFN(5, "could not alloc rx fifo\n");
533 /* update some fields */
534 f->fifo_index = n + USB_FIFO_RX;
537 f->methods = &usb_ugen_methods;
538 f->iface_index = ep->iface_index;
540 mtx_lock(&usb_ref_lock);
541 udev->fifo[n + USB_FIFO_RX] = f;
542 mtx_unlock(&usb_ref_lock);
545 crd->txfifo = udev->fifo[n + USB_FIFO_TX];
548 crd->rxfifo = udev->fifo[n + USB_FIFO_RX];
550 /* fill out fifo index */
551 DPRINTFN(5, "fifo index = %d\n", n);
560 usb_fifo_free(struct usb_fifo *f)
568 /* destroy symlink devices, if any */
569 for (n = 0; n != 2; n++) {
571 usb_free_symlink(f->symlink[n]);
572 f->symlink[n] = NULL;
575 mtx_lock(&usb_ref_lock);
577 /* delink ourselves to stop calls from userland */
578 if ((f->fifo_index < USB_FIFO_MAX) &&
580 (f->udev->fifo[f->fifo_index] == f)) {
581 f->udev->fifo[f->fifo_index] = NULL;
583 DPRINTFN(0, "USB FIFO %p has not been linked\n", f);
586 /* decrease refcount */
588 /* prevent any write flush */
590 /* need to wait until all callers have exited */
591 while (f->refcount != 0) {
592 mtx_unlock(&usb_ref_lock); /* avoid LOR */
593 mtx_lock(f->priv_mtx);
594 /* get I/O thread out of any sleep state */
595 if (f->flag_sleeping) {
596 f->flag_sleeping = 0;
597 cv_broadcast(&f->cv_io);
599 mtx_unlock(f->priv_mtx);
600 mtx_lock(&usb_ref_lock);
603 * Check if the "f->refcount" variable reached zero
604 * during the unlocked time before entering wait:
606 if (f->refcount == 0)
610 cv_wait(&f->cv_drain, &usb_ref_lock);
612 mtx_unlock(&usb_ref_lock);
614 /* take care of closing the device here, if any */
615 usb_fifo_close(f, 0);
617 cv_destroy(&f->cv_io);
618 cv_destroy(&f->cv_drain);
620 knlist_clear(&f->selinfo.si_note, 0);
621 seldrain(&f->selinfo);
622 knlist_destroy(&f->selinfo.si_note);
627 static struct usb_endpoint *
628 usb_dev_get_ep(struct usb_device *udev, uint8_t ep_index, uint8_t dir)
630 struct usb_endpoint *ep;
636 if (dir == USB_FIFO_RX) {
637 if (udev->flags.usb_mode == USB_MODE_HOST) {
643 if (udev->flags.usb_mode == USB_MODE_HOST) {
649 ep = usbd_get_ep_by_addr(udev, ep_index | ep_dir);
653 /* if the endpoint does not exist then return */
656 if (ep->edesc == NULL) {
657 /* invalid endpoint */
660 return (ep); /* success */
663 /*------------------------------------------------------------------------*
669 *------------------------------------------------------------------------*/
671 usb_fifo_open(struct usb_cdev_privdata *cpd,
672 struct usb_fifo *f, int fflags)
678 DPRINTFN(2, "no FIFO\n");
681 /* remove FWRITE and FREAD flags */
682 fflags &= ~(FWRITE | FREAD);
684 /* set correct file flags */
685 if ((f->fifo_index & 1) == USB_FIFO_TX) {
691 /* check if we are already opened */
692 /* we don't need any locks when checking this variable */
693 if (f->curr_cpd != NULL) {
698 /* reset short flag before open */
701 /* call open method */
702 err = (f->methods->f_open) (f, fflags);
706 mtx_lock(f->priv_mtx);
708 /* reset sleep flag */
709 f->flag_sleeping = 0;
711 /* reset error flag */
714 /* reset complete flag */
715 f->flag_iscomplete = 0;
717 /* reset select flag */
718 f->flag_isselect = 0;
720 /* reset flushing flag */
721 f->flag_flushing = 0;
723 /* reset ASYNC proc flag */
726 mtx_lock(&usb_ref_lock);
727 /* flag the fifo as opened to prevent others */
729 mtx_unlock(&usb_ref_lock);
734 mtx_unlock(f->priv_mtx);
739 /*------------------------------------------------------------------------*
741 *------------------------------------------------------------------------*/
743 usb_fifo_reset(struct usb_fifo *f)
751 USB_IF_DEQUEUE(&f->used_q, m);
753 USB_IF_ENQUEUE(&f->free_q, m);
758 /* reset have fragment flag */
759 f->flag_have_fragment = 0;
762 /*------------------------------------------------------------------------*
764 *------------------------------------------------------------------------*/
766 usb_fifo_close(struct usb_fifo *f, int fflags)
770 /* check if we are not opened */
771 if (f->curr_cpd == NULL) {
772 /* nothing to do - already closed */
775 mtx_lock(f->priv_mtx);
777 /* clear current cdev private data pointer */
778 mtx_lock(&usb_ref_lock);
780 mtx_unlock(&usb_ref_lock);
782 /* check if we are watched by kevent */
783 KNOTE_LOCKED(&f->selinfo.si_note, 0);
785 /* check if we are selected */
786 if (f->flag_isselect) {
787 selwakeup(&f->selinfo);
788 f->flag_isselect = 0;
790 /* check if a thread wants SIGIO */
791 if (f->async_p != NULL) {
792 PROC_LOCK(f->async_p);
793 kern_psignal(f->async_p, SIGIO);
794 PROC_UNLOCK(f->async_p);
797 /* remove FWRITE and FREAD flags */
798 fflags &= ~(FWRITE | FREAD);
800 /* flush written data, if any */
801 if ((f->fifo_index & 1) == USB_FIFO_TX) {
803 if (!f->flag_iserror) {
805 /* set flushing flag */
806 f->flag_flushing = 1;
808 /* get the last packet in */
809 if (f->flag_have_fragment) {
811 f->flag_have_fragment = 0;
812 USB_IF_DEQUEUE(&f->free_q, m);
814 USB_IF_ENQUEUE(&f->used_q, m);
818 /* start write transfer, if not already started */
819 (f->methods->f_start_write) (f);
821 /* check if flushed already */
822 while (f->flag_flushing &&
823 (!f->flag_iserror)) {
824 /* wait until all data has been written */
825 f->flag_sleeping = 1;
826 err = cv_wait_sig(&f->cv_io, f->priv_mtx);
828 DPRINTF("signal received\n");
835 /* stop write transfer, if not already stopped */
836 (f->methods->f_stop_write) (f);
840 /* stop write transfer, if not already stopped */
841 (f->methods->f_stop_read) (f);
844 /* check if we are sleeping */
845 if (f->flag_sleeping) {
846 DPRINTFN(2, "Sleeping at close!\n");
848 mtx_unlock(f->priv_mtx);
850 /* call close method */
851 (f->methods->f_close) (f, fflags);
856 /*------------------------------------------------------------------------*
857 * usb_open - cdev callback
858 *------------------------------------------------------------------------*/
860 usb_open(struct cdev *dev, int fflags, int devtype, struct thread *td)
862 struct usb_fs_privdata* pd = (struct usb_fs_privdata*)dev->si_drv1;
863 struct usb_cdev_refdata refs;
864 struct usb_cdev_privdata *cpd;
867 DPRINTFN(2, "%s fflags=0x%08x\n", devtoname(dev), fflags);
869 KASSERT(fflags & (FREAD|FWRITE), ("invalid open flags"));
870 if (((fflags & FREAD) && !(pd->mode & FREAD)) ||
871 ((fflags & FWRITE) && !(pd->mode & FWRITE))) {
872 DPRINTFN(2, "access mode not supported\n");
876 cpd = malloc(sizeof(*cpd), M_USBDEV, M_WAITOK | M_ZERO);
877 ep = cpd->ep_addr = pd->ep_addr;
879 usb_loc_fill(pd, cpd);
880 err = usb_ref_device(cpd, &refs, 1);
882 DPRINTFN(2, "cannot ref device\n");
886 cpd->fflags = fflags; /* access mode for open lifetime */
888 /* create FIFOs, if any */
889 err = usb_fifo_create(cpd, &refs);
890 /* check for error */
892 DPRINTFN(2, "cannot create fifo\n");
893 usb_unref_device(cpd, &refs);
897 if (fflags & FREAD) {
898 err = usb_fifo_open(cpd, refs.rxfifo, fflags);
900 DPRINTFN(2, "read open failed\n");
901 usb_unref_device(cpd, &refs);
906 if (fflags & FWRITE) {
907 err = usb_fifo_open(cpd, refs.txfifo, fflags);
909 DPRINTFN(2, "write open failed\n");
910 if (fflags & FREAD) {
911 usb_fifo_close(refs.rxfifo, fflags);
913 usb_unref_device(cpd, &refs);
918 usb_unref_device(cpd, &refs);
919 devfs_set_cdevpriv(cpd, usb_close);
924 /*------------------------------------------------------------------------*
925 * usb_close - cdev callback
926 *------------------------------------------------------------------------*/
930 struct usb_cdev_refdata refs;
931 struct usb_cdev_privdata *cpd = arg;
934 DPRINTFN(2, "cpd=%p\n", cpd);
936 err = usb_ref_device(cpd, &refs,
937 2 /* uref and allow detached state */);
939 DPRINTFN(2, "Cannot grab USB reference when "
940 "closing USB file handle\n");
943 if (cpd->fflags & FREAD) {
944 usb_fifo_close(refs.rxfifo, cpd->fflags);
946 if (cpd->fflags & FWRITE) {
947 usb_fifo_close(refs.txfifo, cpd->fflags);
949 usb_unref_device(cpd, &refs);
955 usb_dev_init(void *arg)
957 mtx_init(&usb_ref_lock, "USB ref mutex", NULL, MTX_DEF);
958 sx_init(&usb_sym_lock, "USB sym mutex");
959 TAILQ_INIT(&usb_sym_head);
961 /* check the UGEN methods */
962 usb_fifo_check_methods(&usb_ugen_methods);
965 SYSINIT(usb_dev_init, SI_SUB_KLD, SI_ORDER_FIRST, usb_dev_init, NULL);
968 usb_dev_init_post(void *arg)
971 * Create /dev/usb - this is needed for usbconfig(8), which
972 * needs a well-known device name to access.
974 usb_dev = make_dev(&usb_static_devsw, 0, UID_ROOT, GID_OPERATOR,
975 0644, USB_DEVICE_NAME);
976 if (usb_dev == NULL) {
977 DPRINTFN(0, "Could not create usb bus device\n");
981 SYSINIT(usb_dev_init_post, SI_SUB_KICK_SCHEDULER, SI_ORDER_FIRST, usb_dev_init_post, NULL);
984 usb_dev_uninit(void *arg)
986 if (usb_dev != NULL) {
987 destroy_dev(usb_dev);
990 mtx_destroy(&usb_ref_lock);
991 sx_destroy(&usb_sym_lock);
994 SYSUNINIT(usb_dev_uninit, SI_SUB_KICK_SCHEDULER, SI_ORDER_ANY, usb_dev_uninit, NULL);
997 usb_ioctl_f_sub(struct usb_fifo *f, u_long cmd, void *addr,
1004 *(int *)addr = 0; /* character device */
1008 /* handled by upper FS layer */
1013 if (f->async_p != NULL) {
1017 f->async_p = USB_TD_GET_PROC(td);
1023 /* XXX this is not the most general solution */
1025 if (f->async_p == NULL) {
1029 if (*(int *)addr != USB_PROC_GET_GID(f->async_p)) {
1037 DPRINTFN(3, "cmd 0x%lx = %d\n", cmd, error);
1041 /*------------------------------------------------------------------------*
1042 * usb_ioctl - cdev callback
1043 *------------------------------------------------------------------------*/
1045 usb_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int fflag, struct thread* td)
1047 struct usb_cdev_refdata refs;
1048 struct usb_cdev_privdata* cpd;
1053 DPRINTFN(2, "cmd=0x%lx\n", cmd);
1055 err = devfs_get_cdevpriv((void **)&cpd);
1060 * Performance optimisation: We try to check for IOCTL's that
1061 * don't need the USB reference first. Then we grab the USB
1062 * reference if we need it!
1064 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1068 fflags = cpd->fflags;
1070 f = NULL; /* set default value */
1071 err = ENOIOCTL; /* set default value */
1073 if (fflags & FWRITE) {
1075 err = usb_ioctl_f_sub(f, cmd, addr, td);
1077 if (fflags & FREAD) {
1079 err = usb_ioctl_f_sub(f, cmd, addr, td);
1081 KASSERT(f != NULL, ("fifo not found"));
1082 if (err != ENOIOCTL)
1085 err = (f->methods->f_ioctl) (f, cmd, addr, fflags);
1087 DPRINTFN(2, "f_ioctl cmd 0x%lx = %d\n", cmd, err);
1089 if (err != ENOIOCTL)
1092 if (usb_usb_ref_device(cpd, &refs)) {
1097 err = (f->methods->f_ioctl_post) (f, cmd, addr, fflags);
1099 DPRINTFN(2, "f_ioctl_post cmd 0x%lx = %d\n", cmd, err);
1101 if (err == ENOIOCTL)
1107 /* Wait for re-enumeration, if any */
1109 while (f->udev->re_enumerate_wait != USB_RE_ENUM_DONE) {
1111 usb_unref_device(cpd, &refs);
1113 usb_pause_mtx(NULL, hz / 128);
1115 while (usb_ref_device(cpd, &refs, 1 /* need uref */)) {
1116 if (usb_ref_device(cpd, &refs, 0)) {
1117 /* device no longer exits */
1121 usb_unref_device(cpd, &refs);
1122 usb_pause_mtx(NULL, hz / 128);
1127 usb_unref_device(cpd, &refs);
1132 usb_filter_detach(struct knote *kn)
1134 struct usb_fifo *f = kn->kn_hook;
1135 knlist_remove(&f->selinfo.si_note, kn, 0);
1139 usb_filter_write(struct knote *kn, long hint)
1141 struct usb_cdev_privdata* cpd;
1149 mtx_assert(f->priv_mtx, MA_OWNED);
1154 } else if (f->fs_ep_max == 0) {
1155 if (f->flag_iserror) {
1156 /* we got an error */
1159 if (f->queue_data == NULL) {
1161 * start write transfer, if not
1164 (f->methods->f_start_write) (f);
1166 /* check if any packets are available */
1167 USB_IF_POLL(&f->free_q, m);
1170 if (f->flag_iscomplete) {
1180 usb_filter_read(struct knote *kn, long hint)
1182 struct usb_cdev_privdata* cpd;
1190 mtx_assert(f->priv_mtx, MA_OWNED);
1195 } else if (f->fs_ep_max == 0) {
1196 if (f->flag_iserror) {
1197 /* we have an error */
1200 if (f->queue_data == NULL) {
1202 * start read transfer, if not
1205 (f->methods->f_start_read) (f);
1207 /* check if any packets are available */
1208 USB_IF_POLL(&f->used_q, m);
1210 /* start reading data, if any */
1212 (f->methods->f_start_read) (f);
1215 if (f->flag_iscomplete) {
1224 static struct filterops usb_filtops_write = {
1226 .f_detach = usb_filter_detach,
1227 .f_event = usb_filter_write,
1230 static struct filterops usb_filtops_read = {
1232 .f_detach = usb_filter_detach,
1233 .f_event = usb_filter_read,
1239 usb_kqfilter(struct cdev* dev, struct knote *kn)
1241 struct usb_cdev_refdata refs;
1242 struct usb_cdev_privdata* cpd;
1249 if (devfs_get_cdevpriv((void **)&cpd) != 0 ||
1250 usb_ref_device(cpd, &refs, 0) != 0)
1253 fflags = cpd->fflags;
1255 /* Figure out who needs service */
1256 switch (kn->kn_filter) {
1258 if (fflags & FWRITE) {
1260 kn->kn_fop = &usb_filtops_write;
1265 if (fflags & FREAD) {
1267 kn->kn_fop = &usb_filtops_read;
1278 mtx_lock(f->priv_mtx);
1279 knlist_add(&f->selinfo.si_note, kn, 1);
1280 mtx_unlock(f->priv_mtx);
1283 usb_unref_device(cpd, &refs);
1289 usb_poll(struct cdev* dev, int events, struct thread* td)
1291 struct usb_cdev_refdata refs;
1292 struct usb_cdev_privdata* cpd;
1295 int fflags, revents;
1297 if (devfs_get_cdevpriv((void **)&cpd) != 0 ||
1298 usb_ref_device(cpd, &refs, 0) != 0)
1300 (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM));
1302 fflags = cpd->fflags;
1304 /* Figure out who needs service */
1306 if ((events & (POLLOUT | POLLWRNORM)) &&
1307 (fflags & FWRITE)) {
1311 mtx_lock(f->priv_mtx);
1313 if (!refs.is_usbfs) {
1314 if (f->flag_iserror) {
1315 /* we got an error */
1318 if (f->queue_data == NULL) {
1320 * start write transfer, if not
1323 (f->methods->f_start_write) (f);
1325 /* check if any packets are available */
1326 USB_IF_POLL(&f->free_q, m);
1329 if (f->flag_iscomplete) {
1337 revents |= events & (POLLOUT | POLLWRNORM);
1339 f->flag_isselect = 1;
1340 selrecord(td, &f->selinfo);
1343 mtx_unlock(f->priv_mtx);
1345 if ((events & (POLLIN | POLLRDNORM)) &&
1350 mtx_lock(f->priv_mtx);
1352 if (!refs.is_usbfs) {
1353 if (f->flag_iserror) {
1354 /* we have an error */
1357 if (f->queue_data == NULL) {
1359 * start read transfer, if not
1362 (f->methods->f_start_read) (f);
1364 /* check if any packets are available */
1365 USB_IF_POLL(&f->used_q, m);
1368 if (f->flag_iscomplete) {
1376 revents |= events & (POLLIN | POLLRDNORM);
1378 f->flag_isselect = 1;
1379 selrecord(td, &f->selinfo);
1381 if (!refs.is_usbfs) {
1382 /* start reading data */
1383 (f->methods->f_start_read) (f);
1387 mtx_unlock(f->priv_mtx);
1389 usb_unref_device(cpd, &refs);
1394 usb_read(struct cdev *dev, struct uio *uio, int ioflag)
1396 struct usb_cdev_refdata refs;
1397 struct usb_cdev_privdata* cpd;
1404 uint8_t tr_data = 0;
1406 err = devfs_get_cdevpriv((void **)&cpd);
1410 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1414 fflags = cpd->fflags;
1418 /* should not happen */
1419 usb_unref_device(cpd, &refs);
1423 resid = uio->uio_resid;
1425 mtx_lock(f->priv_mtx);
1427 /* check for permanent read error */
1428 if (f->flag_iserror) {
1432 /* check if USB-FS interface is active */
1433 if (refs.is_usbfs) {
1435 * The queue is used for events that should be
1436 * retrieved using the "USB_FS_COMPLETE" ioctl.
1441 while (uio->uio_resid > 0) {
1443 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) {
1490 uint8_t last_packet;
1492 last_packet = m->last_packet;
1494 USB_IF_ENQUEUE(&f->free_q, m);
1501 USB_IF_PREPEND(&f->used_q, m);
1509 mtx_unlock(f->priv_mtx);
1511 usb_unref_device(cpd, &refs);
1517 usb_write(struct cdev *dev, struct uio *uio, int ioflag)
1519 struct usb_cdev_refdata refs;
1520 struct usb_cdev_privdata* cpd;
1528 uint8_t tr_data = 0;
1532 err = devfs_get_cdevpriv((void **)&cpd);
1536 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1540 fflags = cpd->fflags;
1544 /* should not happen */
1545 usb_unref_device(cpd, &refs);
1548 resid = uio->uio_resid;
1550 mtx_lock(f->priv_mtx);
1552 /* check for permanent write error */
1553 if (f->flag_iserror) {
1557 /* check if USB-FS interface is active */
1558 if (refs.is_usbfs) {
1560 * The queue is used for events that should be
1561 * retrieved using the "USB_FS_COMPLETE" ioctl.
1566 if (f->queue_data == NULL) {
1567 /* start write transfer, if not already started */
1568 (f->methods->f_start_write) (f);
1570 /* we allow writing zero length data */
1572 USB_IF_DEQUEUE(&f->free_q, m);
1576 if (ioflag & IO_NDELAY) {
1578 /* return length before error */
1584 DPRINTF("sleeping\n");
1586 err = usb_fifo_wait(f);
1594 if (f->flag_have_fragment == 0) {
1596 io_len = m->cur_data_len;
1597 pdata = m->cur_data_ptr;
1598 if (io_len > uio->uio_resid)
1599 io_len = uio->uio_resid;
1600 m->cur_data_len = io_len;
1602 io_len = m->max_data_len - m->cur_data_len;
1603 pdata = m->cur_data_ptr + m->cur_data_len;
1604 if (io_len > uio->uio_resid)
1605 io_len = uio->uio_resid;
1606 m->cur_data_len += io_len;
1609 DPRINTFN(2, "transfer %d bytes to %p\n",
1612 err = usb_fifo_uiomove(f, pdata, io_len, uio);
1615 f->flag_have_fragment = 0;
1616 USB_IF_ENQUEUE(&f->free_q, m);
1620 /* check if the buffer is ready to be transmitted */
1622 if ((f->flag_write_defrag == 0) ||
1623 (m->cur_data_len == m->max_data_len)) {
1624 f->flag_have_fragment = 0;
1627 * Check for write filter:
1629 * Sometimes it is convenient to process data
1630 * at the expense of a userland process
1631 * instead of a kernel process.
1633 if (f->methods->f_filter_write) {
1634 (f->methods->f_filter_write) (f, m);
1637 /* Put USB mbuf in the used queue */
1638 USB_IF_ENQUEUE(&f->used_q, m);
1640 /* Start writing data, if not already started */
1641 (f->methods->f_start_write) (f);
1643 /* Wait for more data or close */
1644 f->flag_have_fragment = 1;
1645 USB_IF_PREPEND(&f->free_q, m);
1648 } while (uio->uio_resid > 0);
1650 mtx_unlock(f->priv_mtx);
1652 usb_unref_device(cpd, &refs);
1658 usb_static_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
1662 struct usb_read_dir *urd;
1670 err = usb_read_symlink(u.urd->urd_data,
1671 u.urd->urd_startentry, u.urd->urd_maxlen);
1673 case USB_DEV_QUIRK_GET:
1674 case USB_QUIRK_NAME_GET:
1675 case USB_DEV_QUIRK_ADD:
1676 case USB_DEV_QUIRK_REMOVE:
1677 err = usb_quirk_ioctl_p(cmd, data, fflag, td);
1679 case USB_GET_TEMPLATE:
1680 *(int *)data = usb_template;
1683 case USB_SET_TEMPLATE:
1684 err = priv_check(curthread, PRIV_DRIVER);
1687 usb_template = *(int *)data;
1697 usb_fifo_uiomove(struct usb_fifo *f, void *cp,
1698 int n, struct uio *uio)
1702 mtx_unlock(f->priv_mtx);
1705 * "uiomove()" can sleep so one needs to make a wrapper,
1706 * exiting the mutex and checking things:
1708 error = uiomove(cp, n, uio);
1710 mtx_lock(f->priv_mtx);
1716 usb_fifo_wait(struct usb_fifo *f)
1720 mtx_assert(f->priv_mtx, MA_OWNED);
1722 if (f->flag_iserror) {
1726 f->flag_sleeping = 1;
1728 err = cv_wait_sig(&f->cv_io, f->priv_mtx);
1730 if (f->flag_iserror) {
1738 usb_fifo_signal(struct usb_fifo *f)
1740 if (f->flag_sleeping) {
1741 f->flag_sleeping = 0;
1742 cv_broadcast(&f->cv_io);
1747 usb_fifo_wakeup(struct usb_fifo *f)
1751 KNOTE_LOCKED(&f->selinfo.si_note, 0);
1753 if (f->flag_isselect) {
1754 selwakeup(&f->selinfo);
1755 f->flag_isselect = 0;
1757 if (f->async_p != NULL) {
1758 PROC_LOCK(f->async_p);
1759 kern_psignal(f->async_p, SIGIO);
1760 PROC_UNLOCK(f->async_p);
1765 usb_fifo_dummy_open(struct usb_fifo *fifo, int fflags)
1771 usb_fifo_dummy_close(struct usb_fifo *fifo, int fflags)
1777 usb_fifo_dummy_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
1783 usb_fifo_dummy_cmd(struct usb_fifo *fifo)
1785 fifo->flag_flushing = 0; /* not flushing */
1789 usb_fifo_check_methods(struct usb_fifo_methods *pm)
1791 /* check that all callback functions are OK */
1793 if (pm->f_open == NULL)
1794 pm->f_open = &usb_fifo_dummy_open;
1796 if (pm->f_close == NULL)
1797 pm->f_close = &usb_fifo_dummy_close;
1799 if (pm->f_ioctl == NULL)
1800 pm->f_ioctl = &usb_fifo_dummy_ioctl;
1802 if (pm->f_ioctl_post == NULL)
1803 pm->f_ioctl_post = &usb_fifo_dummy_ioctl;
1805 if (pm->f_start_read == NULL)
1806 pm->f_start_read = &usb_fifo_dummy_cmd;
1808 if (pm->f_stop_read == NULL)
1809 pm->f_stop_read = &usb_fifo_dummy_cmd;
1811 if (pm->f_start_write == NULL)
1812 pm->f_start_write = &usb_fifo_dummy_cmd;
1814 if (pm->f_stop_write == NULL)
1815 pm->f_stop_write = &usb_fifo_dummy_cmd;
1818 /*------------------------------------------------------------------------*
1821 * The following function will create a duplex FIFO.
1826 *------------------------------------------------------------------------*/
1828 usb_fifo_attach(struct usb_device *udev, void *priv_sc,
1829 struct mtx *priv_mtx, struct usb_fifo_methods *pm,
1830 struct usb_fifo_sc *f_sc, uint16_t unit, int16_t subunit,
1831 uint8_t iface_index, uid_t uid, gid_t gid, int mode)
1833 struct usb_fifo *f_tx;
1834 struct usb_fifo *f_rx;
1838 f_sc->fp[USB_FIFO_TX] = NULL;
1839 f_sc->fp[USB_FIFO_RX] = NULL;
1844 /* check the methods */
1845 usb_fifo_check_methods(pm);
1847 if (priv_mtx == NULL)
1850 /* search for a free FIFO slot */
1851 for (n = 0;; n += 2) {
1853 if (n == USB_FIFO_MAX) {
1854 /* end of FIFOs reached */
1857 /* Check for TX FIFO */
1858 if (udev->fifo[n + USB_FIFO_TX] != NULL) {
1861 /* Check for RX FIFO */
1862 if (udev->fifo[n + USB_FIFO_RX] != NULL) {
1868 f_tx = usb_fifo_alloc(priv_mtx);
1869 f_rx = usb_fifo_alloc(priv_mtx);
1871 if ((f_tx == NULL) || (f_rx == NULL)) {
1872 usb_fifo_free(f_tx);
1873 usb_fifo_free(f_rx);
1876 /* initialise FIFO structures */
1878 f_tx->fifo_index = n + USB_FIFO_TX;
1879 f_tx->dev_ep_index = -1;
1880 f_tx->priv_sc0 = priv_sc;
1882 f_tx->iface_index = iface_index;
1885 f_rx->fifo_index = n + USB_FIFO_RX;
1886 f_rx->dev_ep_index = -1;
1887 f_rx->priv_sc0 = priv_sc;
1889 f_rx->iface_index = iface_index;
1892 f_sc->fp[USB_FIFO_TX] = f_tx;
1893 f_sc->fp[USB_FIFO_RX] = f_rx;
1895 mtx_lock(&usb_ref_lock);
1896 udev->fifo[f_tx->fifo_index] = f_tx;
1897 udev->fifo[f_rx->fifo_index] = f_rx;
1898 mtx_unlock(&usb_ref_lock);
1900 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 usb_fifo_free_buffer(f);
1955 /* allocate an endpoint */
1956 f->free_q.ifq_maxlen = nbuf;
1957 f->used_q.ifq_maxlen = nbuf;
1959 f->queue_data = usb_alloc_mbufs(
1960 M_USBDEV, &f->free_q, bufsize, nbuf);
1962 if ((f->queue_data == NULL) && bufsize && nbuf) {
1965 return (0); /* success */
1968 /*------------------------------------------------------------------------*
1969 * usb_fifo_free_buffer
1971 * This function will free the buffers associated with a FIFO. This
1972 * function can be called multiple times in a row.
1973 *------------------------------------------------------------------------*/
1975 usb_fifo_free_buffer(struct usb_fifo *f)
1977 if (f->queue_data) {
1978 /* free old buffer */
1979 free(f->queue_data, M_USBDEV);
1980 f->queue_data = NULL;
1984 memset(&f->free_q, 0, sizeof(f->free_q));
1985 memset(&f->used_q, 0, sizeof(f->used_q));
1989 usb_fifo_detach(struct usb_fifo_sc *f_sc)
1994 usb_fifo_free(f_sc->fp[USB_FIFO_TX]);
1995 usb_fifo_free(f_sc->fp[USB_FIFO_RX]);
1997 f_sc->fp[USB_FIFO_TX] = NULL;
1998 f_sc->fp[USB_FIFO_RX] = NULL;
2000 usb_destroy_dev(f_sc->dev);
2004 DPRINTFN(2, "detached %p\n", f_sc);
2008 usb_fifo_put_bytes_max(struct usb_fifo *f)
2013 USB_IF_POLL(&f->free_q, m);
2016 len = m->max_data_len;
2023 /*------------------------------------------------------------------------*
2027 * 0 - normal operation
2028 * 1 - set last packet flag to enforce framing
2029 *------------------------------------------------------------------------*/
2031 usb_fifo_put_data(struct usb_fifo *f, struct usb_page_cache *pc,
2032 usb_frlength_t offset, usb_frlength_t len, uint8_t what)
2035 usb_frlength_t io_len;
2037 while (len || (what == 1)) {
2039 USB_IF_DEQUEUE(&f->free_q, m);
2044 io_len = MIN(len, m->cur_data_len);
2046 usbd_copy_out(pc, offset, m->cur_data_ptr, io_len);
2048 m->cur_data_len = io_len;
2052 if ((len == 0) && (what == 1)) {
2055 USB_IF_ENQUEUE(&f->used_q, m);
2059 if ((len == 0) || (what == 1)) {
2069 usb_fifo_put_data_linear(struct usb_fifo *f, void *ptr,
2070 usb_size_t len, uint8_t what)
2075 while (len || (what == 1)) {
2077 USB_IF_DEQUEUE(&f->free_q, m);
2082 io_len = MIN(len, m->cur_data_len);
2084 memcpy(m->cur_data_ptr, ptr, io_len);
2086 m->cur_data_len = io_len;
2087 ptr = USB_ADD_BYTES(ptr, io_len);
2090 if ((len == 0) && (what == 1)) {
2093 USB_IF_ENQUEUE(&f->used_q, m);
2097 if ((len == 0) || (what == 1)) {
2107 usb_fifo_put_data_buffer(struct usb_fifo *f, void *ptr, usb_size_t len)
2111 USB_IF_DEQUEUE(&f->free_q, m);
2114 m->cur_data_len = len;
2115 m->cur_data_ptr = ptr;
2116 USB_IF_ENQUEUE(&f->used_q, m);
2124 usb_fifo_put_data_error(struct usb_fifo *f)
2126 f->flag_iserror = 1;
2130 /*------------------------------------------------------------------------*
2134 * 0 - normal operation
2135 * 1 - only get one "usb_mbuf"
2139 * 1 - data in buffer
2140 *------------------------------------------------------------------------*/
2142 usb_fifo_get_data(struct usb_fifo *f, struct usb_page_cache *pc,
2143 usb_frlength_t offset, usb_frlength_t len, usb_frlength_t *actlen,
2147 usb_frlength_t io_len;
2148 uint8_t tr_data = 0;
2154 USB_IF_DEQUEUE(&f->used_q, m);
2160 io_len = MIN(len, m->cur_data_len);
2162 usbd_copy_in(pc, offset, m->cur_data_ptr, io_len);
2166 actlen[0] += io_len;
2167 m->cur_data_ptr += io_len;
2168 m->cur_data_len -= io_len;
2170 if ((m->cur_data_len == 0) || (what == 1)) {
2171 USB_IF_ENQUEUE(&f->free_q, m);
2179 USB_IF_PREPEND(&f->used_q, m);
2184 /* wait for data to be written out */
2187 if (f->flag_flushing) {
2188 /* check if we should send a short packet */
2189 if (f->flag_short != 0) {
2194 /* flushing complete */
2195 f->flag_flushing = 0;
2208 usb_fifo_get_data_linear(struct usb_fifo *f, void *ptr,
2209 usb_size_t len, usb_size_t *actlen, uint8_t what)
2213 uint8_t tr_data = 0;
2219 USB_IF_DEQUEUE(&f->used_q, m);
2225 io_len = MIN(len, m->cur_data_len);
2227 memcpy(ptr, m->cur_data_ptr, io_len);
2230 ptr = USB_ADD_BYTES(ptr, io_len);
2231 actlen[0] += io_len;
2232 m->cur_data_ptr += io_len;
2233 m->cur_data_len -= io_len;
2235 if ((m->cur_data_len == 0) || (what == 1)) {
2236 USB_IF_ENQUEUE(&f->free_q, m);
2244 USB_IF_PREPEND(&f->used_q, m);
2249 /* wait for data to be written out */
2252 if (f->flag_flushing) {
2253 /* check if we should send a short packet */
2254 if (f->flag_short != 0) {
2259 /* flushing complete */
2260 f->flag_flushing = 0;
2273 usb_fifo_get_data_buffer(struct usb_fifo *f, void **pptr, usb_size_t *plen)
2277 USB_IF_POLL(&f->used_q, m);
2280 *plen = m->cur_data_len;
2281 *pptr = m->cur_data_ptr;
2289 usb_fifo_get_data_error(struct usb_fifo *f)
2291 f->flag_iserror = 1;
2295 /*------------------------------------------------------------------------*
2300 * Else: Pointer to symlink entry
2301 *------------------------------------------------------------------------*/
2302 struct usb_symlink *
2303 usb_alloc_symlink(const char *target)
2305 struct usb_symlink *ps;
2307 ps = malloc(sizeof(*ps), M_USBDEV, M_WAITOK);
2311 /* XXX no longer needed */
2312 strlcpy(ps->src_path, target, sizeof(ps->src_path));
2313 ps->src_len = strlen(ps->src_path);
2314 strlcpy(ps->dst_path, target, sizeof(ps->dst_path));
2315 ps->dst_len = strlen(ps->dst_path);
2317 sx_xlock(&usb_sym_lock);
2318 TAILQ_INSERT_TAIL(&usb_sym_head, ps, sym_entry);
2319 sx_unlock(&usb_sym_lock);
2323 /*------------------------------------------------------------------------*
2325 *------------------------------------------------------------------------*/
2327 usb_free_symlink(struct usb_symlink *ps)
2332 sx_xlock(&usb_sym_lock);
2333 TAILQ_REMOVE(&usb_sym_head, ps, sym_entry);
2334 sx_unlock(&usb_sym_lock);
2339 /*------------------------------------------------------------------------*
2345 *------------------------------------------------------------------------*/
2347 usb_read_symlink(uint8_t *user_ptr, uint32_t startentry, uint32_t user_len)
2349 struct usb_symlink *ps;
2355 sx_xlock(&usb_sym_lock);
2357 TAILQ_FOREACH(ps, &usb_sym_head, sym_entry) {
2360 * Compute total length of source and destination symlink
2361 * strings pluss one length byte and two NUL bytes:
2363 temp = ps->src_len + ps->dst_len + 3;
2367 * Skip entry because this length cannot fit
2372 if (startentry != 0) {
2373 /* decrement read offset */
2377 if (temp > user_len) {
2378 /* out of buffer space */
2383 /* copy out total length */
2385 error = copyout(&len,
2386 USB_ADD_BYTES(user_ptr, delta), 1);
2392 /* copy out source string */
2394 error = copyout(ps->src_path,
2395 USB_ADD_BYTES(user_ptr, delta), ps->src_len);
2400 delta += ps->src_len;
2401 error = copyout(&len,
2402 USB_ADD_BYTES(user_ptr, delta), 1);
2408 /* copy out destination string */
2410 error = copyout(ps->dst_path,
2411 USB_ADD_BYTES(user_ptr, delta), ps->dst_len);
2416 delta += ps->dst_len;
2417 error = copyout(&len,
2418 USB_ADD_BYTES(user_ptr, delta), 1);
2427 /* a zero length entry indicates the end */
2429 if ((user_len != 0) && (error == 0)) {
2433 error = copyout(&len,
2434 USB_ADD_BYTES(user_ptr, delta), 1);
2436 sx_unlock(&usb_sym_lock);
2441 usb_fifo_set_close_zlp(struct usb_fifo *f, uint8_t onoff)
2446 /* send a Zero Length Packet, ZLP, before close */
2447 f->flag_short = onoff;
2451 usb_fifo_set_write_defrag(struct usb_fifo *f, uint8_t onoff)
2456 /* defrag written data */
2457 f->flag_write_defrag = onoff;
2458 /* reset defrag state */
2459 f->flag_have_fragment = 0;
2463 usb_fifo_softc(struct usb_fifo *f)
2465 return (f->priv_sc0);
2467 #endif /* USB_HAVE_UGEN */