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_RW | CTLFLAG_TUN,
90 &usb_fifo_debug, 0, "Debug Level");
91 TUNABLE_INT("hw.usb.dev.debug", &usb_fifo_debug);
94 #if ((__FreeBSD_version >= 700001) || (__FreeBSD_version == 0) || \
95 ((__FreeBSD_version >= 600034) && (__FreeBSD_version < 700000)))
96 #define USB_UCRED struct ucred *ucred,
103 static int usb_fifo_open(struct usb_cdev_privdata *,
104 struct usb_fifo *, int);
105 static void usb_fifo_close(struct usb_fifo *, int);
106 static void usb_dev_init(void *);
107 static void usb_dev_init_post(void *);
108 static void usb_dev_uninit(void *);
109 static int usb_fifo_uiomove(struct usb_fifo *, void *, int,
111 static void usb_fifo_check_methods(struct usb_fifo_methods *);
112 static struct usb_fifo *usb_fifo_alloc(struct mtx *);
113 static struct usb_endpoint *usb_dev_get_ep(struct usb_device *, uint8_t,
115 static void usb_loc_fill(struct usb_fs_privdata *,
116 struct usb_cdev_privdata *);
117 static void usb_close(void *);
118 static usb_error_t usb_ref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *, int);
119 static usb_error_t usb_usb_ref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *);
120 static void usb_unref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *);
122 static d_open_t usb_open;
123 static d_ioctl_t usb_ioctl;
124 static d_read_t usb_read;
125 static d_write_t usb_write;
126 static d_poll_t usb_poll;
127 static d_kqfilter_t usb_kqfilter;
129 static d_ioctl_t usb_static_ioctl;
131 static usb_fifo_open_t usb_fifo_dummy_open;
132 static usb_fifo_close_t usb_fifo_dummy_close;
133 static usb_fifo_ioctl_t usb_fifo_dummy_ioctl;
134 static usb_fifo_cmd_t usb_fifo_dummy_cmd;
136 /* character device structure used for devices (/dev/ugenX.Y and /dev/uXXX) */
137 struct cdevsw usb_devsw = {
138 .d_version = D_VERSION,
140 .d_ioctl = usb_ioctl,
142 .d_flags = D_TRACKCLOSE,
144 .d_write = usb_write,
146 .d_kqfilter = usb_kqfilter,
149 static struct cdev* usb_dev = NULL;
151 /* character device structure used for /dev/usb */
152 static struct cdevsw usb_static_devsw = {
153 .d_version = D_VERSION,
154 .d_ioctl = usb_static_ioctl,
158 static TAILQ_HEAD(, usb_symlink) usb_sym_head;
159 static struct sx usb_sym_lock;
161 struct mtx usb_ref_lock;
163 /*------------------------------------------------------------------------*
166 * This is used to fill out a usb_cdev_privdata structure based on the
167 * device's address as contained in usb_fs_privdata.
168 *------------------------------------------------------------------------*/
170 usb_loc_fill(struct usb_fs_privdata* pd, struct usb_cdev_privdata *cpd)
172 cpd->bus_index = pd->bus_index;
173 cpd->dev_index = pd->dev_index;
174 cpd->ep_addr = pd->ep_addr;
175 cpd->fifo_index = pd->fifo_index;
178 /*------------------------------------------------------------------------*
181 * This function is used to atomically refer an USB device by its
182 * device location. If this function returns success the USB device
183 * will not dissappear until the USB device is unreferenced.
186 * 0: Success, refcount incremented on the given USB device.
188 *------------------------------------------------------------------------*/
190 usb_ref_device(struct usb_cdev_privdata *cpd,
191 struct usb_cdev_refdata *crd, int need_uref)
193 struct usb_fifo **ppf;
196 DPRINTFN(2, "cpd=%p need uref=%d\n", cpd, need_uref);
199 memset(crd, 0, sizeof(*crd));
201 mtx_lock(&usb_ref_lock);
202 cpd->bus = devclass_get_softc(usb_devclass_ptr, cpd->bus_index);
203 if (cpd->bus == NULL) {
204 DPRINTFN(2, "no bus at %u\n", cpd->bus_index);
207 cpd->udev = cpd->bus->devices[cpd->dev_index];
208 if (cpd->udev == NULL) {
209 DPRINTFN(2, "no device at %u\n", cpd->dev_index);
212 if (cpd->udev->state == USB_STATE_DETACHED &&
214 DPRINTFN(2, "device is detached\n");
218 DPRINTFN(2, "ref udev - needed\n");
220 if (cpd->udev->refcount == USB_DEV_REF_MAX) {
221 DPRINTFN(2, "no dev ref\n");
224 cpd->udev->refcount++;
226 mtx_unlock(&usb_ref_lock);
229 * We need to grab the enumeration SX-lock before
230 * grabbing the FIFO refs to avoid deadlock at detach!
232 crd->do_unlock = usbd_enum_lock(cpd->udev);
234 mtx_lock(&usb_ref_lock);
237 * Set "is_uref" after grabbing the default SX lock
242 /* check if we are doing an open */
243 if (cpd->fflags == 0) {
244 /* use zero defaults */
246 /* check for write */
247 if (cpd->fflags & FWRITE) {
248 ppf = cpd->udev->fifo;
249 f = ppf[cpd->fifo_index + USB_FIFO_TX];
251 crd->is_write = 1; /* ref */
252 if (f == NULL || f->refcount == USB_FIFO_REF_MAX)
254 if (f->curr_cpd != cpd)
256 /* check if USB-FS is active */
257 if (f->fs_ep_max != 0) {
263 if (cpd->fflags & FREAD) {
264 ppf = cpd->udev->fifo;
265 f = ppf[cpd->fifo_index + USB_FIFO_RX];
267 crd->is_read = 1; /* ref */
268 if (f == NULL || f->refcount == USB_FIFO_REF_MAX)
270 if (f->curr_cpd != cpd)
272 /* check if USB-FS is active */
273 if (f->fs_ep_max != 0) {
279 /* when everything is OK we increment the refcounts */
281 DPRINTFN(2, "ref write\n");
282 crd->txfifo->refcount++;
285 DPRINTFN(2, "ref read\n");
286 crd->rxfifo->refcount++;
288 mtx_unlock(&usb_ref_lock);
294 usbd_enum_unlock(cpd->udev);
297 if (--(cpd->udev->refcount) == 0)
298 cv_broadcast(&cpd->udev->ref_cv);
300 mtx_unlock(&usb_ref_lock);
301 DPRINTFN(2, "fail\n");
304 memset(crd, 0, sizeof(*crd));
306 return (USB_ERR_INVAL);
309 /*------------------------------------------------------------------------*
312 * This function is used to upgrade an USB reference to include the
313 * USB device reference on a USB location.
316 * 0: Success, refcount incremented on the given USB device.
318 *------------------------------------------------------------------------*/
320 usb_usb_ref_device(struct usb_cdev_privdata *cpd,
321 struct usb_cdev_refdata *crd)
324 * Check if we already got an USB reference on this location:
327 return (0); /* success */
330 * To avoid deadlock at detach we need to drop the FIFO ref
331 * and re-acquire a new ref!
333 usb_unref_device(cpd, crd);
335 return (usb_ref_device(cpd, crd, 1 /* need uref */));
338 /*------------------------------------------------------------------------*
341 * This function will release the reference count by one unit for the
343 *------------------------------------------------------------------------*/
345 usb_unref_device(struct usb_cdev_privdata *cpd,
346 struct usb_cdev_refdata *crd)
349 DPRINTFN(2, "cpd=%p is_uref=%d\n", cpd, crd->is_uref);
352 usbd_enum_unlock(cpd->udev);
354 mtx_lock(&usb_ref_lock);
356 if (--(crd->rxfifo->refcount) == 0) {
357 cv_signal(&crd->rxfifo->cv_drain);
362 if (--(crd->txfifo->refcount) == 0) {
363 cv_signal(&crd->txfifo->cv_drain);
369 if (--(cpd->udev->refcount) == 0)
370 cv_broadcast(&cpd->udev->ref_cv);
372 mtx_unlock(&usb_ref_lock);
375 static struct usb_fifo *
376 usb_fifo_alloc(struct mtx *mtx)
380 f = malloc(sizeof(*f), M_USBDEV, M_WAITOK | M_ZERO);
382 cv_init(&f->cv_io, "FIFO-IO");
383 cv_init(&f->cv_drain, "FIFO-DRAIN");
386 knlist_init_mtx(&f->selinfo.si_note, mtx);
391 /*------------------------------------------------------------------------*
393 *------------------------------------------------------------------------*/
395 usb_fifo_create(struct usb_cdev_privdata *cpd,
396 struct usb_cdev_refdata *crd)
398 struct usb_device *udev = cpd->udev;
400 struct usb_endpoint *ep;
406 int e = cpd->ep_addr;
408 is_tx = (cpd->fflags & FWRITE) ? 1 : 0;
409 is_rx = (cpd->fflags & FREAD) ? 1 : 0;
413 /* Preallocated FIFO */
415 DPRINTFN(5, "Preallocated FIFO\n");
417 f = udev->fifo[cpd->fifo_index + USB_FIFO_TX];
423 f = udev->fifo[cpd->fifo_index + USB_FIFO_RX];
431 KASSERT(e >= 0 && e <= 15, ("endpoint %d out of range", e));
433 /* search for a free FIFO slot */
434 DPRINTFN(5, "Endpoint device, searching for 0x%02x\n", e);
435 for (n = 0;; n += 2) {
437 if (n == USB_FIFO_MAX) {
442 /* end of FIFOs reached */
443 DPRINTFN(5, "out of FIFOs\n");
447 /* Check for TX FIFO */
449 f = udev->fifo[n + USB_FIFO_TX];
451 if (f->dev_ep_index != e) {
452 /* wrong endpoint index */
455 if (f->curr_cpd != NULL) {
460 } else if (no_null) {
464 /* Check for RX FIFO */
466 f = udev->fifo[n + USB_FIFO_RX];
468 if (f->dev_ep_index != e) {
469 /* wrong endpoint index */
472 if (f->curr_cpd != NULL) {
477 } else if (no_null) {
485 if (e >= (USB_EP_MAX / 2)) {
486 /* we don't create any endpoints in this range */
487 DPRINTFN(5, "ep out of range\n");
488 return (is_busy ? EBUSY : EINVAL);
492 if ((e != 0) && is_busy) {
494 * Only the default control endpoint is allowed to be
495 * opened multiple times!
497 DPRINTFN(5, "busy\n");
503 (udev->fifo[n + USB_FIFO_TX] == NULL)) {
504 ep = usb_dev_get_ep(udev, e, USB_FIFO_TX);
505 DPRINTFN(5, "dev_get_endpoint(%d, 0x%x)\n", e, USB_FIFO_TX);
507 DPRINTFN(5, "dev_get_endpoint returned NULL\n");
510 f = usb_fifo_alloc(&udev->device_mtx);
512 DPRINTFN(5, "could not alloc tx fifo\n");
515 /* update some fields */
516 f->fifo_index = n + USB_FIFO_TX;
519 f->methods = &usb_ugen_methods;
520 f->iface_index = ep->iface_index;
522 mtx_lock(&usb_ref_lock);
523 udev->fifo[n + USB_FIFO_TX] = f;
524 mtx_unlock(&usb_ref_lock);
528 (udev->fifo[n + USB_FIFO_RX] == NULL)) {
530 ep = usb_dev_get_ep(udev, e, USB_FIFO_RX);
531 DPRINTFN(5, "dev_get_endpoint(%d, 0x%x)\n", e, USB_FIFO_RX);
533 DPRINTFN(5, "dev_get_endpoint returned NULL\n");
536 f = usb_fifo_alloc(&udev->device_mtx);
538 DPRINTFN(5, "could not alloc rx fifo\n");
541 /* update some fields */
542 f->fifo_index = n + USB_FIFO_RX;
545 f->methods = &usb_ugen_methods;
546 f->iface_index = ep->iface_index;
548 mtx_lock(&usb_ref_lock);
549 udev->fifo[n + USB_FIFO_RX] = f;
550 mtx_unlock(&usb_ref_lock);
553 crd->txfifo = udev->fifo[n + USB_FIFO_TX];
556 crd->rxfifo = udev->fifo[n + USB_FIFO_RX];
558 /* fill out fifo index */
559 DPRINTFN(5, "fifo index = %d\n", n);
568 usb_fifo_free(struct usb_fifo *f)
576 /* destroy symlink devices, if any */
577 for (n = 0; n != 2; n++) {
579 usb_free_symlink(f->symlink[n]);
580 f->symlink[n] = NULL;
583 mtx_lock(&usb_ref_lock);
585 /* delink ourselves to stop calls from userland */
586 if ((f->fifo_index < USB_FIFO_MAX) &&
588 (f->udev->fifo[f->fifo_index] == f)) {
589 f->udev->fifo[f->fifo_index] = NULL;
591 DPRINTFN(0, "USB FIFO %p has not been linked\n", f);
594 /* decrease refcount */
596 /* need to wait until all callers have exited */
597 while (f->refcount != 0) {
598 mtx_unlock(&usb_ref_lock); /* avoid LOR */
599 mtx_lock(f->priv_mtx);
600 /* prevent write flush, if any */
602 /* get I/O thread out of any sleep state */
603 if (f->flag_sleeping) {
604 f->flag_sleeping = 0;
605 cv_broadcast(&f->cv_io);
607 mtx_unlock(f->priv_mtx);
608 mtx_lock(&usb_ref_lock);
611 * Check if the "f->refcount" variable reached zero
612 * during the unlocked time before entering wait:
614 if (f->refcount == 0)
618 cv_wait(&f->cv_drain, &usb_ref_lock);
620 mtx_unlock(&usb_ref_lock);
622 /* take care of closing the device here, if any */
623 usb_fifo_close(f, 0);
625 cv_destroy(&f->cv_io);
626 cv_destroy(&f->cv_drain);
628 knlist_clear(&f->selinfo.si_note, 0);
629 seldrain(&f->selinfo);
630 knlist_destroy(&f->selinfo.si_note);
635 static struct usb_endpoint *
636 usb_dev_get_ep(struct usb_device *udev, uint8_t ep_index, uint8_t dir)
638 struct usb_endpoint *ep;
644 if (dir == USB_FIFO_RX) {
645 if (udev->flags.usb_mode == USB_MODE_HOST) {
651 if (udev->flags.usb_mode == USB_MODE_HOST) {
657 ep = usbd_get_ep_by_addr(udev, ep_index | ep_dir);
661 /* if the endpoint does not exist then return */
664 if (ep->edesc == NULL) {
665 /* invalid endpoint */
668 return (ep); /* success */
671 /*------------------------------------------------------------------------*
677 *------------------------------------------------------------------------*/
679 usb_fifo_open(struct usb_cdev_privdata *cpd,
680 struct usb_fifo *f, int fflags)
686 DPRINTFN(2, "no FIFO\n");
689 /* remove FWRITE and FREAD flags */
690 fflags &= ~(FWRITE | FREAD);
692 /* set correct file flags */
693 if ((f->fifo_index & 1) == USB_FIFO_TX) {
699 /* check if we are already opened */
700 /* we don't need any locks when checking this variable */
701 if (f->curr_cpd != NULL) {
706 /* reset short flag before open */
709 /* call open method */
710 err = (f->methods->f_open) (f, fflags);
714 mtx_lock(f->priv_mtx);
716 /* reset sleep flag */
717 f->flag_sleeping = 0;
719 /* reset error flag */
722 /* reset complete flag */
723 f->flag_iscomplete = 0;
725 /* reset select flag */
726 f->flag_isselect = 0;
728 /* reset flushing flag */
729 f->flag_flushing = 0;
731 /* reset ASYNC proc flag */
734 mtx_lock(&usb_ref_lock);
735 /* flag the fifo as opened to prevent others */
737 mtx_unlock(&usb_ref_lock);
742 mtx_unlock(f->priv_mtx);
747 /*------------------------------------------------------------------------*
749 *------------------------------------------------------------------------*/
751 usb_fifo_reset(struct usb_fifo *f)
759 USB_IF_DEQUEUE(&f->used_q, m);
761 USB_IF_ENQUEUE(&f->free_q, m);
766 /* reset have fragment flag */
767 f->flag_have_fragment = 0;
770 /*------------------------------------------------------------------------*
772 *------------------------------------------------------------------------*/
774 usb_fifo_close(struct usb_fifo *f, int fflags)
778 /* check if we are not opened */
779 if (f->curr_cpd == NULL) {
780 /* nothing to do - already closed */
783 mtx_lock(f->priv_mtx);
785 /* clear current cdev private data pointer */
786 mtx_lock(&usb_ref_lock);
788 mtx_unlock(&usb_ref_lock);
790 /* check if we are watched by kevent */
791 KNOTE_LOCKED(&f->selinfo.si_note, 0);
793 /* check if we are selected */
794 if (f->flag_isselect) {
795 selwakeup(&f->selinfo);
796 f->flag_isselect = 0;
798 /* check if a thread wants SIGIO */
799 if (f->async_p != NULL) {
800 PROC_LOCK(f->async_p);
801 kern_psignal(f->async_p, SIGIO);
802 PROC_UNLOCK(f->async_p);
805 /* remove FWRITE and FREAD flags */
806 fflags &= ~(FWRITE | FREAD);
808 /* flush written data, if any */
809 if ((f->fifo_index & 1) == USB_FIFO_TX) {
811 if (!f->flag_iserror) {
813 /* set flushing flag */
814 f->flag_flushing = 1;
816 /* get the last packet in */
817 if (f->flag_have_fragment) {
819 f->flag_have_fragment = 0;
820 USB_IF_DEQUEUE(&f->free_q, m);
822 USB_IF_ENQUEUE(&f->used_q, m);
826 /* start write transfer, if not already started */
827 (f->methods->f_start_write) (f);
829 /* check if flushed already */
830 while (f->flag_flushing &&
831 (!f->flag_iserror)) {
832 /* wait until all data has been written */
833 f->flag_sleeping = 1;
834 err = cv_timedwait_sig(&f->cv_io, f->priv_mtx,
835 USB_MS_TO_TICKS(USB_DEFAULT_TIMEOUT));
837 DPRINTF("signal received\n");
844 /* stop write transfer, if not already stopped */
845 (f->methods->f_stop_write) (f);
849 /* stop write transfer, if not already stopped */
850 (f->methods->f_stop_read) (f);
853 /* check if we are sleeping */
854 if (f->flag_sleeping) {
855 DPRINTFN(2, "Sleeping at close!\n");
857 mtx_unlock(f->priv_mtx);
859 /* call close method */
860 (f->methods->f_close) (f, fflags);
865 /*------------------------------------------------------------------------*
866 * usb_open - cdev callback
867 *------------------------------------------------------------------------*/
869 usb_open(struct cdev *dev, int fflags, int devtype, struct thread *td)
871 struct usb_fs_privdata* pd = (struct usb_fs_privdata*)dev->si_drv1;
872 struct usb_cdev_refdata refs;
873 struct usb_cdev_privdata *cpd;
876 DPRINTFN(2, "%s fflags=0x%08x\n", devtoname(dev), fflags);
878 KASSERT(fflags & (FREAD|FWRITE), ("invalid open flags"));
879 if (((fflags & FREAD) && !(pd->mode & FREAD)) ||
880 ((fflags & FWRITE) && !(pd->mode & FWRITE))) {
881 DPRINTFN(2, "access mode not supported\n");
885 cpd = malloc(sizeof(*cpd), M_USBDEV, M_WAITOK | M_ZERO);
886 ep = cpd->ep_addr = pd->ep_addr;
888 usb_loc_fill(pd, cpd);
889 err = usb_ref_device(cpd, &refs, 1);
891 DPRINTFN(2, "cannot ref device\n");
895 cpd->fflags = fflags; /* access mode for open lifetime */
897 /* create FIFOs, if any */
898 err = usb_fifo_create(cpd, &refs);
899 /* check for error */
901 DPRINTFN(2, "cannot create fifo\n");
902 usb_unref_device(cpd, &refs);
906 if (fflags & FREAD) {
907 err = usb_fifo_open(cpd, refs.rxfifo, fflags);
909 DPRINTFN(2, "read open failed\n");
910 usb_unref_device(cpd, &refs);
915 if (fflags & FWRITE) {
916 err = usb_fifo_open(cpd, refs.txfifo, fflags);
918 DPRINTFN(2, "write open failed\n");
919 if (fflags & FREAD) {
920 usb_fifo_close(refs.rxfifo, fflags);
922 usb_unref_device(cpd, &refs);
927 usb_unref_device(cpd, &refs);
928 devfs_set_cdevpriv(cpd, usb_close);
933 /*------------------------------------------------------------------------*
934 * usb_close - cdev callback
935 *------------------------------------------------------------------------*/
939 struct usb_cdev_refdata refs;
940 struct usb_cdev_privdata *cpd = arg;
943 DPRINTFN(2, "cpd=%p\n", cpd);
945 err = usb_ref_device(cpd, &refs,
946 2 /* uref and allow detached state */);
948 DPRINTFN(2, "Cannot grab USB reference when "
949 "closing USB file handle\n");
952 if (cpd->fflags & FREAD) {
953 usb_fifo_close(refs.rxfifo, cpd->fflags);
955 if (cpd->fflags & FWRITE) {
956 usb_fifo_close(refs.txfifo, cpd->fflags);
958 usb_unref_device(cpd, &refs);
964 usb_dev_init(void *arg)
966 mtx_init(&usb_ref_lock, "USB ref mutex", NULL, MTX_DEF);
967 sx_init(&usb_sym_lock, "USB sym mutex");
968 TAILQ_INIT(&usb_sym_head);
970 /* check the UGEN methods */
971 usb_fifo_check_methods(&usb_ugen_methods);
974 SYSINIT(usb_dev_init, SI_SUB_KLD, SI_ORDER_FIRST, usb_dev_init, NULL);
977 usb_dev_init_post(void *arg)
980 * Create /dev/usb - this is needed for usbconfig(8), which
981 * needs a well-known device name to access.
983 usb_dev = make_dev(&usb_static_devsw, 0, UID_ROOT, GID_OPERATOR,
984 0644, USB_DEVICE_NAME);
985 if (usb_dev == NULL) {
986 DPRINTFN(0, "Could not create usb bus device\n");
990 SYSINIT(usb_dev_init_post, SI_SUB_KICK_SCHEDULER, SI_ORDER_FIRST, usb_dev_init_post, NULL);
993 usb_dev_uninit(void *arg)
995 if (usb_dev != NULL) {
996 destroy_dev(usb_dev);
999 mtx_destroy(&usb_ref_lock);
1000 sx_destroy(&usb_sym_lock);
1003 SYSUNINIT(usb_dev_uninit, SI_SUB_KICK_SCHEDULER, SI_ORDER_ANY, usb_dev_uninit, NULL);
1006 usb_ioctl_f_sub(struct usb_fifo *f, u_long cmd, void *addr,
1013 *(int *)addr = 0; /* character device */
1017 /* handled by upper FS layer */
1022 if (f->async_p != NULL) {
1026 f->async_p = USB_TD_GET_PROC(td);
1032 /* XXX this is not the most general solution */
1034 if (f->async_p == NULL) {
1038 if (*(int *)addr != USB_PROC_GET_GID(f->async_p)) {
1046 DPRINTFN(3, "cmd 0x%lx = %d\n", cmd, error);
1050 /*------------------------------------------------------------------------*
1051 * usb_ioctl - cdev callback
1052 *------------------------------------------------------------------------*/
1054 usb_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int fflag, struct thread* td)
1056 struct usb_cdev_refdata refs;
1057 struct usb_cdev_privdata* cpd;
1062 DPRINTFN(2, "cmd=0x%lx\n", cmd);
1064 err = devfs_get_cdevpriv((void **)&cpd);
1069 * Performance optimisation: We try to check for IOCTL's that
1070 * don't need the USB reference first. Then we grab the USB
1071 * reference if we need it!
1073 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1077 fflags = cpd->fflags;
1079 f = NULL; /* set default value */
1080 err = ENOIOCTL; /* set default value */
1082 if (fflags & FWRITE) {
1084 err = usb_ioctl_f_sub(f, cmd, addr, td);
1086 if (fflags & FREAD) {
1088 err = usb_ioctl_f_sub(f, cmd, addr, td);
1090 KASSERT(f != NULL, ("fifo not found"));
1091 if (err != ENOIOCTL)
1094 err = (f->methods->f_ioctl) (f, cmd, addr, fflags);
1096 DPRINTFN(2, "f_ioctl cmd 0x%lx = %d\n", cmd, err);
1098 if (err != ENOIOCTL)
1101 if (usb_usb_ref_device(cpd, &refs)) {
1102 /* we lost the reference */
1106 err = (f->methods->f_ioctl_post) (f, cmd, addr, fflags);
1108 DPRINTFN(2, "f_ioctl_post cmd 0x%lx = %d\n", cmd, err);
1110 if (err == ENOIOCTL)
1116 /* Wait for re-enumeration, if any */
1118 while (f->udev->re_enumerate_wait != USB_RE_ENUM_DONE) {
1120 usb_unref_device(cpd, &refs);
1122 usb_pause_mtx(NULL, hz / 128);
1124 while (usb_ref_device(cpd, &refs, 1 /* need uref */)) {
1125 if (usb_ref_device(cpd, &refs, 0)) {
1126 /* device no longer exists */
1129 usb_unref_device(cpd, &refs);
1130 usb_pause_mtx(NULL, hz / 128);
1135 usb_unref_device(cpd, &refs);
1140 usb_filter_detach(struct knote *kn)
1142 struct usb_fifo *f = kn->kn_hook;
1143 knlist_remove(&f->selinfo.si_note, kn, 0);
1147 usb_filter_write(struct knote *kn, long hint)
1149 struct usb_cdev_privdata* cpd;
1157 mtx_assert(f->priv_mtx, MA_OWNED);
1162 } else if (f->fs_ep_max == 0) {
1163 if (f->flag_iserror) {
1164 /* we got an error */
1167 if (f->queue_data == NULL) {
1169 * start write transfer, if not
1172 (f->methods->f_start_write) (f);
1174 /* check if any packets are available */
1175 USB_IF_POLL(&f->free_q, m);
1178 if (f->flag_iscomplete) {
1188 usb_filter_read(struct knote *kn, long hint)
1190 struct usb_cdev_privdata* cpd;
1198 mtx_assert(f->priv_mtx, MA_OWNED);
1203 } else if (f->fs_ep_max == 0) {
1204 if (f->flag_iserror) {
1205 /* we have an error */
1208 if (f->queue_data == NULL) {
1210 * start read transfer, if not
1213 (f->methods->f_start_read) (f);
1215 /* check if any packets are available */
1216 USB_IF_POLL(&f->used_q, m);
1218 /* start reading data, if any */
1220 (f->methods->f_start_read) (f);
1223 if (f->flag_iscomplete) {
1232 static struct filterops usb_filtops_write = {
1234 .f_detach = usb_filter_detach,
1235 .f_event = usb_filter_write,
1238 static struct filterops usb_filtops_read = {
1240 .f_detach = usb_filter_detach,
1241 .f_event = usb_filter_read,
1247 usb_kqfilter(struct cdev* dev, struct knote *kn)
1249 struct usb_cdev_refdata refs;
1250 struct usb_cdev_privdata* cpd;
1257 if (devfs_get_cdevpriv((void **)&cpd) != 0 ||
1258 usb_ref_device(cpd, &refs, 0) != 0)
1261 fflags = cpd->fflags;
1263 /* Figure out who needs service */
1264 switch (kn->kn_filter) {
1266 if (fflags & FWRITE) {
1268 kn->kn_fop = &usb_filtops_write;
1273 if (fflags & FREAD) {
1275 kn->kn_fop = &usb_filtops_read;
1286 mtx_lock(f->priv_mtx);
1287 knlist_add(&f->selinfo.si_note, kn, 1);
1288 mtx_unlock(f->priv_mtx);
1291 usb_unref_device(cpd, &refs);
1297 usb_poll(struct cdev* dev, int events, struct thread* td)
1299 struct usb_cdev_refdata refs;
1300 struct usb_cdev_privdata* cpd;
1303 int fflags, revents;
1305 if (devfs_get_cdevpriv((void **)&cpd) != 0 ||
1306 usb_ref_device(cpd, &refs, 0) != 0)
1308 (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM));
1310 fflags = cpd->fflags;
1312 /* Figure out who needs service */
1314 if ((events & (POLLOUT | POLLWRNORM)) &&
1315 (fflags & FWRITE)) {
1319 mtx_lock(f->priv_mtx);
1321 if (!refs.is_usbfs) {
1322 if (f->flag_iserror) {
1323 /* we got an error */
1326 if (f->queue_data == NULL) {
1328 * start write transfer, if not
1331 (f->methods->f_start_write) (f);
1333 /* check if any packets are available */
1334 USB_IF_POLL(&f->free_q, m);
1337 if (f->flag_iscomplete) {
1345 revents |= events & (POLLOUT | POLLWRNORM);
1347 f->flag_isselect = 1;
1348 selrecord(td, &f->selinfo);
1351 mtx_unlock(f->priv_mtx);
1353 if ((events & (POLLIN | POLLRDNORM)) &&
1358 mtx_lock(f->priv_mtx);
1360 if (!refs.is_usbfs) {
1361 if (f->flag_iserror) {
1362 /* we have an error */
1365 if (f->queue_data == NULL) {
1367 * start read transfer, if not
1370 (f->methods->f_start_read) (f);
1372 /* check if any packets are available */
1373 USB_IF_POLL(&f->used_q, m);
1376 if (f->flag_iscomplete) {
1384 revents |= events & (POLLIN | POLLRDNORM);
1386 f->flag_isselect = 1;
1387 selrecord(td, &f->selinfo);
1389 if (!refs.is_usbfs) {
1390 /* start reading data */
1391 (f->methods->f_start_read) (f);
1395 mtx_unlock(f->priv_mtx);
1397 usb_unref_device(cpd, &refs);
1402 usb_read(struct cdev *dev, struct uio *uio, int ioflag)
1404 struct usb_cdev_refdata refs;
1405 struct usb_cdev_privdata* cpd;
1412 uint8_t tr_data = 0;
1414 err = devfs_get_cdevpriv((void **)&cpd);
1418 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1422 fflags = cpd->fflags;
1426 /* should not happen */
1427 usb_unref_device(cpd, &refs);
1431 resid = uio->uio_resid;
1433 mtx_lock(f->priv_mtx);
1435 /* check for permanent read error */
1436 if (f->flag_iserror) {
1440 /* check if USB-FS interface is active */
1441 if (refs.is_usbfs) {
1443 * The queue is used for events that should be
1444 * retrieved using the "USB_FS_COMPLETE" ioctl.
1449 while (uio->uio_resid > 0) {
1451 USB_IF_DEQUEUE(&f->used_q, m);
1455 /* start read transfer, if not already started */
1457 (f->methods->f_start_read) (f);
1459 if (ioflag & IO_NDELAY) {
1461 /* return length before error */
1467 DPRINTF("sleeping\n");
1469 err = usb_fifo_wait(f);
1475 if (f->methods->f_filter_read) {
1477 * Sometimes it is convenient to process data at the
1478 * expense of a userland process instead of a kernel
1481 (f->methods->f_filter_read) (f, m);
1485 io_len = MIN(m->cur_data_len, uio->uio_resid);
1487 DPRINTFN(2, "transfer %d bytes from %p\n",
1488 io_len, m->cur_data_ptr);
1490 err = usb_fifo_uiomove(f,
1491 m->cur_data_ptr, io_len, uio);
1493 m->cur_data_len -= io_len;
1494 m->cur_data_ptr += io_len;
1496 if (m->cur_data_len == 0) {
1498 uint8_t last_packet;
1500 last_packet = m->last_packet;
1502 USB_IF_ENQUEUE(&f->free_q, m);
1509 USB_IF_PREPEND(&f->used_q, m);
1517 mtx_unlock(f->priv_mtx);
1519 usb_unref_device(cpd, &refs);
1525 usb_write(struct cdev *dev, struct uio *uio, int ioflag)
1527 struct usb_cdev_refdata refs;
1528 struct usb_cdev_privdata* cpd;
1536 uint8_t tr_data = 0;
1540 err = devfs_get_cdevpriv((void **)&cpd);
1544 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1548 fflags = cpd->fflags;
1552 /* should not happen */
1553 usb_unref_device(cpd, &refs);
1556 resid = uio->uio_resid;
1558 mtx_lock(f->priv_mtx);
1560 /* check for permanent write error */
1561 if (f->flag_iserror) {
1565 /* check if USB-FS interface is active */
1566 if (refs.is_usbfs) {
1568 * The queue is used for events that should be
1569 * retrieved using the "USB_FS_COMPLETE" ioctl.
1574 if (f->queue_data == NULL) {
1575 /* start write transfer, if not already started */
1576 (f->methods->f_start_write) (f);
1578 /* we allow writing zero length data */
1580 USB_IF_DEQUEUE(&f->free_q, m);
1584 if (ioflag & IO_NDELAY) {
1586 /* return length before error */
1592 DPRINTF("sleeping\n");
1594 err = usb_fifo_wait(f);
1602 if (f->flag_have_fragment == 0) {
1604 io_len = m->cur_data_len;
1605 pdata = m->cur_data_ptr;
1606 if (io_len > uio->uio_resid)
1607 io_len = uio->uio_resid;
1608 m->cur_data_len = io_len;
1610 io_len = m->max_data_len - m->cur_data_len;
1611 pdata = m->cur_data_ptr + m->cur_data_len;
1612 if (io_len > uio->uio_resid)
1613 io_len = uio->uio_resid;
1614 m->cur_data_len += io_len;
1617 DPRINTFN(2, "transfer %d bytes to %p\n",
1620 err = usb_fifo_uiomove(f, pdata, io_len, uio);
1623 f->flag_have_fragment = 0;
1624 USB_IF_ENQUEUE(&f->free_q, m);
1628 /* check if the buffer is ready to be transmitted */
1630 if ((f->flag_write_defrag == 0) ||
1631 (m->cur_data_len == m->max_data_len)) {
1632 f->flag_have_fragment = 0;
1635 * Check for write filter:
1637 * Sometimes it is convenient to process data
1638 * at the expense of a userland process
1639 * instead of a kernel process.
1641 if (f->methods->f_filter_write) {
1642 (f->methods->f_filter_write) (f, m);
1645 /* Put USB mbuf in the used queue */
1646 USB_IF_ENQUEUE(&f->used_q, m);
1648 /* Start writing data, if not already started */
1649 (f->methods->f_start_write) (f);
1651 /* Wait for more data or close */
1652 f->flag_have_fragment = 1;
1653 USB_IF_PREPEND(&f->free_q, m);
1656 } while (uio->uio_resid > 0);
1658 mtx_unlock(f->priv_mtx);
1660 usb_unref_device(cpd, &refs);
1666 usb_static_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
1670 struct usb_read_dir *urd;
1678 err = usb_read_symlink(u.urd->urd_data,
1679 u.urd->urd_startentry, u.urd->urd_maxlen);
1681 case USB_DEV_QUIRK_GET:
1682 case USB_QUIRK_NAME_GET:
1683 case USB_DEV_QUIRK_ADD:
1684 case USB_DEV_QUIRK_REMOVE:
1685 err = usb_quirk_ioctl_p(cmd, data, fflag, td);
1687 case USB_GET_TEMPLATE:
1688 *(int *)data = usb_template;
1691 case USB_SET_TEMPLATE:
1692 err = priv_check(curthread, PRIV_DRIVER);
1695 usb_template = *(int *)data;
1705 usb_fifo_uiomove(struct usb_fifo *f, void *cp,
1706 int n, struct uio *uio)
1710 mtx_unlock(f->priv_mtx);
1713 * "uiomove()" can sleep so one needs to make a wrapper,
1714 * exiting the mutex and checking things:
1716 error = uiomove(cp, n, uio);
1718 mtx_lock(f->priv_mtx);
1724 usb_fifo_wait(struct usb_fifo *f)
1728 mtx_assert(f->priv_mtx, MA_OWNED);
1730 if (f->flag_iserror) {
1734 f->flag_sleeping = 1;
1736 err = cv_wait_sig(&f->cv_io, f->priv_mtx);
1738 if (f->flag_iserror) {
1746 usb_fifo_signal(struct usb_fifo *f)
1748 if (f->flag_sleeping) {
1749 f->flag_sleeping = 0;
1750 cv_broadcast(&f->cv_io);
1755 usb_fifo_wakeup(struct usb_fifo *f)
1759 KNOTE_LOCKED(&f->selinfo.si_note, 0);
1761 if (f->flag_isselect) {
1762 selwakeup(&f->selinfo);
1763 f->flag_isselect = 0;
1765 if (f->async_p != NULL) {
1766 PROC_LOCK(f->async_p);
1767 kern_psignal(f->async_p, SIGIO);
1768 PROC_UNLOCK(f->async_p);
1773 usb_fifo_dummy_open(struct usb_fifo *fifo, int fflags)
1779 usb_fifo_dummy_close(struct usb_fifo *fifo, int fflags)
1785 usb_fifo_dummy_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
1791 usb_fifo_dummy_cmd(struct usb_fifo *fifo)
1793 fifo->flag_flushing = 0; /* not flushing */
1797 usb_fifo_check_methods(struct usb_fifo_methods *pm)
1799 /* check that all callback functions are OK */
1801 if (pm->f_open == NULL)
1802 pm->f_open = &usb_fifo_dummy_open;
1804 if (pm->f_close == NULL)
1805 pm->f_close = &usb_fifo_dummy_close;
1807 if (pm->f_ioctl == NULL)
1808 pm->f_ioctl = &usb_fifo_dummy_ioctl;
1810 if (pm->f_ioctl_post == NULL)
1811 pm->f_ioctl_post = &usb_fifo_dummy_ioctl;
1813 if (pm->f_start_read == NULL)
1814 pm->f_start_read = &usb_fifo_dummy_cmd;
1816 if (pm->f_stop_read == NULL)
1817 pm->f_stop_read = &usb_fifo_dummy_cmd;
1819 if (pm->f_start_write == NULL)
1820 pm->f_start_write = &usb_fifo_dummy_cmd;
1822 if (pm->f_stop_write == NULL)
1823 pm->f_stop_write = &usb_fifo_dummy_cmd;
1826 /*------------------------------------------------------------------------*
1829 * The following function will create a duplex FIFO.
1834 *------------------------------------------------------------------------*/
1836 usb_fifo_attach(struct usb_device *udev, void *priv_sc,
1837 struct mtx *priv_mtx, struct usb_fifo_methods *pm,
1838 struct usb_fifo_sc *f_sc, uint16_t unit, int16_t subunit,
1839 uint8_t iface_index, uid_t uid, gid_t gid, int mode)
1841 struct usb_fifo *f_tx;
1842 struct usb_fifo *f_rx;
1846 f_sc->fp[USB_FIFO_TX] = NULL;
1847 f_sc->fp[USB_FIFO_RX] = NULL;
1852 /* check the methods */
1853 usb_fifo_check_methods(pm);
1855 if (priv_mtx == NULL)
1858 /* search for a free FIFO slot */
1859 for (n = 0;; n += 2) {
1861 if (n == USB_FIFO_MAX) {
1862 /* end of FIFOs reached */
1865 /* Check for TX FIFO */
1866 if (udev->fifo[n + USB_FIFO_TX] != NULL) {
1869 /* Check for RX FIFO */
1870 if (udev->fifo[n + USB_FIFO_RX] != NULL) {
1876 f_tx = usb_fifo_alloc(priv_mtx);
1877 f_rx = usb_fifo_alloc(priv_mtx);
1879 if ((f_tx == NULL) || (f_rx == NULL)) {
1880 usb_fifo_free(f_tx);
1881 usb_fifo_free(f_rx);
1884 /* initialise FIFO structures */
1886 f_tx->fifo_index = n + USB_FIFO_TX;
1887 f_tx->dev_ep_index = -1;
1888 f_tx->priv_sc0 = priv_sc;
1890 f_tx->iface_index = iface_index;
1893 f_rx->fifo_index = n + USB_FIFO_RX;
1894 f_rx->dev_ep_index = -1;
1895 f_rx->priv_sc0 = priv_sc;
1897 f_rx->iface_index = iface_index;
1900 f_sc->fp[USB_FIFO_TX] = f_tx;
1901 f_sc->fp[USB_FIFO_RX] = f_rx;
1903 mtx_lock(&usb_ref_lock);
1904 udev->fifo[f_tx->fifo_index] = f_tx;
1905 udev->fifo[f_rx->fifo_index] = f_rx;
1906 mtx_unlock(&usb_ref_lock);
1908 for (n = 0; n != 4; n++) {
1910 if (pm->basename[n] == NULL) {
1914 if (snprintf(devname, sizeof(devname),
1915 "%s%u%s", pm->basename[n],
1916 unit, pm->postfix[n] ?
1917 pm->postfix[n] : "")) {
1921 if (snprintf(devname, sizeof(devname),
1922 "%s%u.%d%s", pm->basename[n],
1923 unit, subunit, pm->postfix[n] ?
1924 pm->postfix[n] : "")) {
1930 * Distribute the symbolic links into two FIFO structures:
1933 f_rx->symlink[n / 2] =
1934 usb_alloc_symlink(devname);
1936 f_tx->symlink[n / 2] =
1937 usb_alloc_symlink(devname);
1940 /* Create the device */
1941 f_sc->dev = usb_make_dev(udev, devname, -1,
1942 f_tx->fifo_index & f_rx->fifo_index,
1943 FREAD|FWRITE, uid, gid, mode);
1946 DPRINTFN(2, "attached %p/%p\n", f_tx, f_rx);
1950 /*------------------------------------------------------------------------*
1951 * usb_fifo_alloc_buffer
1956 *------------------------------------------------------------------------*/
1958 usb_fifo_alloc_buffer(struct usb_fifo *f, usb_size_t bufsize,
1961 usb_fifo_free_buffer(f);
1963 /* allocate an endpoint */
1964 f->free_q.ifq_maxlen = nbuf;
1965 f->used_q.ifq_maxlen = nbuf;
1967 f->queue_data = usb_alloc_mbufs(
1968 M_USBDEV, &f->free_q, bufsize, nbuf);
1970 if ((f->queue_data == NULL) && bufsize && nbuf) {
1973 return (0); /* success */
1976 /*------------------------------------------------------------------------*
1977 * usb_fifo_free_buffer
1979 * This function will free the buffers associated with a FIFO. This
1980 * function can be called multiple times in a row.
1981 *------------------------------------------------------------------------*/
1983 usb_fifo_free_buffer(struct usb_fifo *f)
1985 if (f->queue_data) {
1986 /* free old buffer */
1987 free(f->queue_data, M_USBDEV);
1988 f->queue_data = NULL;
1992 memset(&f->free_q, 0, sizeof(f->free_q));
1993 memset(&f->used_q, 0, sizeof(f->used_q));
1997 usb_fifo_detach(struct usb_fifo_sc *f_sc)
2002 usb_fifo_free(f_sc->fp[USB_FIFO_TX]);
2003 usb_fifo_free(f_sc->fp[USB_FIFO_RX]);
2005 f_sc->fp[USB_FIFO_TX] = NULL;
2006 f_sc->fp[USB_FIFO_RX] = NULL;
2008 usb_destroy_dev(f_sc->dev);
2012 DPRINTFN(2, "detached %p\n", f_sc);
2016 usb_fifo_put_bytes_max(struct usb_fifo *f)
2021 USB_IF_POLL(&f->free_q, m);
2024 len = m->max_data_len;
2031 /*------------------------------------------------------------------------*
2035 * 0 - normal operation
2036 * 1 - set last packet flag to enforce framing
2037 *------------------------------------------------------------------------*/
2039 usb_fifo_put_data(struct usb_fifo *f, struct usb_page_cache *pc,
2040 usb_frlength_t offset, usb_frlength_t len, uint8_t what)
2043 usb_frlength_t io_len;
2045 while (len || (what == 1)) {
2047 USB_IF_DEQUEUE(&f->free_q, m);
2052 io_len = MIN(len, m->cur_data_len);
2054 usbd_copy_out(pc, offset, m->cur_data_ptr, io_len);
2056 m->cur_data_len = io_len;
2060 if ((len == 0) && (what == 1)) {
2063 USB_IF_ENQUEUE(&f->used_q, m);
2067 if ((len == 0) || (what == 1)) {
2077 usb_fifo_put_data_linear(struct usb_fifo *f, void *ptr,
2078 usb_size_t len, uint8_t what)
2083 while (len || (what == 1)) {
2085 USB_IF_DEQUEUE(&f->free_q, m);
2090 io_len = MIN(len, m->cur_data_len);
2092 memcpy(m->cur_data_ptr, ptr, io_len);
2094 m->cur_data_len = io_len;
2095 ptr = USB_ADD_BYTES(ptr, io_len);
2098 if ((len == 0) && (what == 1)) {
2101 USB_IF_ENQUEUE(&f->used_q, m);
2105 if ((len == 0) || (what == 1)) {
2115 usb_fifo_put_data_buffer(struct usb_fifo *f, void *ptr, usb_size_t len)
2119 USB_IF_DEQUEUE(&f->free_q, m);
2122 m->cur_data_len = len;
2123 m->cur_data_ptr = ptr;
2124 USB_IF_ENQUEUE(&f->used_q, m);
2132 usb_fifo_put_data_error(struct usb_fifo *f)
2134 f->flag_iserror = 1;
2138 /*------------------------------------------------------------------------*
2142 * 0 - normal operation
2143 * 1 - only get one "usb_mbuf"
2147 * 1 - data in buffer
2148 *------------------------------------------------------------------------*/
2150 usb_fifo_get_data(struct usb_fifo *f, struct usb_page_cache *pc,
2151 usb_frlength_t offset, usb_frlength_t len, usb_frlength_t *actlen,
2155 usb_frlength_t io_len;
2156 uint8_t tr_data = 0;
2162 USB_IF_DEQUEUE(&f->used_q, m);
2168 io_len = MIN(len, m->cur_data_len);
2170 usbd_copy_in(pc, offset, m->cur_data_ptr, io_len);
2174 actlen[0] += io_len;
2175 m->cur_data_ptr += io_len;
2176 m->cur_data_len -= io_len;
2178 if ((m->cur_data_len == 0) || (what == 1)) {
2179 USB_IF_ENQUEUE(&f->free_q, m);
2187 USB_IF_PREPEND(&f->used_q, m);
2192 /* wait for data to be written out */
2195 if (f->flag_flushing) {
2196 /* check if we should send a short packet */
2197 if (f->flag_short != 0) {
2202 /* flushing complete */
2203 f->flag_flushing = 0;
2216 usb_fifo_get_data_linear(struct usb_fifo *f, void *ptr,
2217 usb_size_t len, usb_size_t *actlen, uint8_t what)
2221 uint8_t tr_data = 0;
2227 USB_IF_DEQUEUE(&f->used_q, m);
2233 io_len = MIN(len, m->cur_data_len);
2235 memcpy(ptr, m->cur_data_ptr, io_len);
2238 ptr = USB_ADD_BYTES(ptr, io_len);
2239 actlen[0] += io_len;
2240 m->cur_data_ptr += io_len;
2241 m->cur_data_len -= io_len;
2243 if ((m->cur_data_len == 0) || (what == 1)) {
2244 USB_IF_ENQUEUE(&f->free_q, m);
2252 USB_IF_PREPEND(&f->used_q, m);
2257 /* wait for data to be written out */
2260 if (f->flag_flushing) {
2261 /* check if we should send a short packet */
2262 if (f->flag_short != 0) {
2267 /* flushing complete */
2268 f->flag_flushing = 0;
2281 usb_fifo_get_data_buffer(struct usb_fifo *f, void **pptr, usb_size_t *plen)
2285 USB_IF_POLL(&f->used_q, m);
2288 *plen = m->cur_data_len;
2289 *pptr = m->cur_data_ptr;
2297 usb_fifo_get_data_error(struct usb_fifo *f)
2299 f->flag_iserror = 1;
2303 /*------------------------------------------------------------------------*
2308 * Else: Pointer to symlink entry
2309 *------------------------------------------------------------------------*/
2310 struct usb_symlink *
2311 usb_alloc_symlink(const char *target)
2313 struct usb_symlink *ps;
2315 ps = malloc(sizeof(*ps), M_USBDEV, M_WAITOK);
2319 /* XXX no longer needed */
2320 strlcpy(ps->src_path, target, sizeof(ps->src_path));
2321 ps->src_len = strlen(ps->src_path);
2322 strlcpy(ps->dst_path, target, sizeof(ps->dst_path));
2323 ps->dst_len = strlen(ps->dst_path);
2325 sx_xlock(&usb_sym_lock);
2326 TAILQ_INSERT_TAIL(&usb_sym_head, ps, sym_entry);
2327 sx_unlock(&usb_sym_lock);
2331 /*------------------------------------------------------------------------*
2333 *------------------------------------------------------------------------*/
2335 usb_free_symlink(struct usb_symlink *ps)
2340 sx_xlock(&usb_sym_lock);
2341 TAILQ_REMOVE(&usb_sym_head, ps, sym_entry);
2342 sx_unlock(&usb_sym_lock);
2347 /*------------------------------------------------------------------------*
2353 *------------------------------------------------------------------------*/
2355 usb_read_symlink(uint8_t *user_ptr, uint32_t startentry, uint32_t user_len)
2357 struct usb_symlink *ps;
2363 sx_xlock(&usb_sym_lock);
2365 TAILQ_FOREACH(ps, &usb_sym_head, sym_entry) {
2368 * Compute total length of source and destination symlink
2369 * strings pluss one length byte and two NUL bytes:
2371 temp = ps->src_len + ps->dst_len + 3;
2375 * Skip entry because this length cannot fit
2380 if (startentry != 0) {
2381 /* decrement read offset */
2385 if (temp > user_len) {
2386 /* out of buffer space */
2391 /* copy out total length */
2393 error = copyout(&len,
2394 USB_ADD_BYTES(user_ptr, delta), 1);
2400 /* copy out source string */
2402 error = copyout(ps->src_path,
2403 USB_ADD_BYTES(user_ptr, delta), ps->src_len);
2408 delta += ps->src_len;
2409 error = copyout(&len,
2410 USB_ADD_BYTES(user_ptr, delta), 1);
2416 /* copy out destination string */
2418 error = copyout(ps->dst_path,
2419 USB_ADD_BYTES(user_ptr, delta), ps->dst_len);
2424 delta += ps->dst_len;
2425 error = copyout(&len,
2426 USB_ADD_BYTES(user_ptr, delta), 1);
2435 /* a zero length entry indicates the end */
2437 if ((user_len != 0) && (error == 0)) {
2441 error = copyout(&len,
2442 USB_ADD_BYTES(user_ptr, delta), 1);
2444 sx_unlock(&usb_sym_lock);
2449 usb_fifo_set_close_zlp(struct usb_fifo *f, uint8_t onoff)
2454 /* send a Zero Length Packet, ZLP, before close */
2455 f->flag_short = onoff;
2459 usb_fifo_set_write_defrag(struct usb_fifo *f, uint8_t onoff)
2464 /* defrag written data */
2465 f->flag_write_defrag = onoff;
2466 /* reset defrag state */
2467 f->flag_have_fragment = 0;
2471 usb_fifo_softc(struct usb_fifo *f)
2473 return (f->priv_sc0);
2475 #endif /* USB_HAVE_UGEN */