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_sig(cpd->udev);
230 mtx_lock(&usb_ref_lock);
233 * Set "is_uref" after grabbing the default SX lock
237 /* check for signal */
238 if (crd->do_unlock > 1) {
244 /* check if we are doing an open */
245 if (cpd->fflags == 0) {
246 /* use zero defaults */
248 /* check for write */
249 if (cpd->fflags & FWRITE) {
250 ppf = cpd->udev->fifo;
251 f = ppf[cpd->fifo_index + USB_FIFO_TX];
253 crd->is_write = 1; /* ref */
254 if (f == NULL || f->refcount == USB_FIFO_REF_MAX)
256 if (f->curr_cpd != cpd)
258 /* check if USB-FS is active */
259 if (f->fs_ep_max != 0) {
265 if (cpd->fflags & FREAD) {
266 ppf = cpd->udev->fifo;
267 f = ppf[cpd->fifo_index + USB_FIFO_RX];
269 crd->is_read = 1; /* ref */
270 if (f == NULL || f->refcount == USB_FIFO_REF_MAX)
272 if (f->curr_cpd != cpd)
274 /* check if USB-FS is active */
275 if (f->fs_ep_max != 0) {
281 /* when everything is OK we increment the refcounts */
283 DPRINTFN(2, "ref write\n");
284 crd->txfifo->refcount++;
287 DPRINTFN(2, "ref read\n");
288 crd->rxfifo->refcount++;
290 mtx_unlock(&usb_ref_lock);
296 usbd_enum_unlock(cpd->udev);
299 if (--(cpd->udev->refcount) == 0)
300 cv_broadcast(&cpd->udev->ref_cv);
302 mtx_unlock(&usb_ref_lock);
303 DPRINTFN(2, "fail\n");
306 memset(crd, 0, sizeof(*crd));
308 return (USB_ERR_INVAL);
311 /*------------------------------------------------------------------------*
314 * This function is used to upgrade an USB reference to include the
315 * USB device reference on a USB location.
318 * 0: Success, refcount incremented on the given USB device.
320 *------------------------------------------------------------------------*/
322 usb_usb_ref_device(struct usb_cdev_privdata *cpd,
323 struct usb_cdev_refdata *crd)
326 * Check if we already got an USB reference on this location:
329 return (0); /* success */
332 * To avoid deadlock at detach we need to drop the FIFO ref
333 * and re-acquire a new ref!
335 usb_unref_device(cpd, crd);
337 return (usb_ref_device(cpd, crd, 1 /* need uref */));
340 /*------------------------------------------------------------------------*
343 * This function will release the reference count by one unit for the
345 *------------------------------------------------------------------------*/
347 usb_unref_device(struct usb_cdev_privdata *cpd,
348 struct usb_cdev_refdata *crd)
351 DPRINTFN(2, "cpd=%p is_uref=%d\n", cpd, crd->is_uref);
354 usbd_enum_unlock(cpd->udev);
356 mtx_lock(&usb_ref_lock);
358 if (--(crd->rxfifo->refcount) == 0) {
359 cv_signal(&crd->rxfifo->cv_drain);
364 if (--(crd->txfifo->refcount) == 0) {
365 cv_signal(&crd->txfifo->cv_drain);
371 if (--(cpd->udev->refcount) == 0)
372 cv_broadcast(&cpd->udev->ref_cv);
374 mtx_unlock(&usb_ref_lock);
377 static struct usb_fifo *
378 usb_fifo_alloc(struct mtx *mtx)
382 f = malloc(sizeof(*f), M_USBDEV, M_WAITOK | M_ZERO);
384 cv_init(&f->cv_io, "FIFO-IO");
385 cv_init(&f->cv_drain, "FIFO-DRAIN");
388 knlist_init_mtx(&f->selinfo.si_note, mtx);
393 /*------------------------------------------------------------------------*
395 *------------------------------------------------------------------------*/
397 usb_fifo_create(struct usb_cdev_privdata *cpd,
398 struct usb_cdev_refdata *crd)
400 struct usb_device *udev = cpd->udev;
402 struct usb_endpoint *ep;
408 int e = cpd->ep_addr;
410 is_tx = (cpd->fflags & FWRITE) ? 1 : 0;
411 is_rx = (cpd->fflags & FREAD) ? 1 : 0;
415 /* Preallocated FIFO */
417 DPRINTFN(5, "Preallocated FIFO\n");
419 f = udev->fifo[cpd->fifo_index + USB_FIFO_TX];
425 f = udev->fifo[cpd->fifo_index + USB_FIFO_RX];
433 KASSERT(e >= 0 && e <= 15, ("endpoint %d out of range", e));
435 /* search for a free FIFO slot */
436 DPRINTFN(5, "Endpoint device, searching for 0x%02x\n", e);
437 for (n = 0;; n += 2) {
439 if (n == USB_FIFO_MAX) {
444 /* end of FIFOs reached */
445 DPRINTFN(5, "out of FIFOs\n");
449 /* Check for TX FIFO */
451 f = udev->fifo[n + USB_FIFO_TX];
453 if (f->dev_ep_index != e) {
454 /* wrong endpoint index */
457 if (f->curr_cpd != NULL) {
462 } else if (no_null) {
466 /* Check for RX FIFO */
468 f = udev->fifo[n + USB_FIFO_RX];
470 if (f->dev_ep_index != e) {
471 /* wrong endpoint index */
474 if (f->curr_cpd != NULL) {
479 } else if (no_null) {
487 if (e >= (USB_EP_MAX / 2)) {
488 /* we don't create any endpoints in this range */
489 DPRINTFN(5, "ep out of range\n");
490 return (is_busy ? EBUSY : EINVAL);
494 if ((e != 0) && is_busy) {
496 * Only the default control endpoint is allowed to be
497 * opened multiple times!
499 DPRINTFN(5, "busy\n");
505 (udev->fifo[n + USB_FIFO_TX] == NULL)) {
506 ep = usb_dev_get_ep(udev, e, USB_FIFO_TX);
507 DPRINTFN(5, "dev_get_endpoint(%d, 0x%x)\n", e, USB_FIFO_TX);
509 DPRINTFN(5, "dev_get_endpoint returned NULL\n");
512 f = usb_fifo_alloc(&udev->device_mtx);
514 DPRINTFN(5, "could not alloc tx fifo\n");
517 /* update some fields */
518 f->fifo_index = n + USB_FIFO_TX;
521 f->methods = &usb_ugen_methods;
522 f->iface_index = ep->iface_index;
524 mtx_lock(&usb_ref_lock);
525 udev->fifo[n + USB_FIFO_TX] = f;
526 mtx_unlock(&usb_ref_lock);
530 (udev->fifo[n + USB_FIFO_RX] == NULL)) {
532 ep = usb_dev_get_ep(udev, e, USB_FIFO_RX);
533 DPRINTFN(5, "dev_get_endpoint(%d, 0x%x)\n", e, USB_FIFO_RX);
535 DPRINTFN(5, "dev_get_endpoint returned NULL\n");
538 f = usb_fifo_alloc(&udev->device_mtx);
540 DPRINTFN(5, "could not alloc rx fifo\n");
543 /* update some fields */
544 f->fifo_index = n + USB_FIFO_RX;
547 f->methods = &usb_ugen_methods;
548 f->iface_index = ep->iface_index;
550 mtx_lock(&usb_ref_lock);
551 udev->fifo[n + USB_FIFO_RX] = f;
552 mtx_unlock(&usb_ref_lock);
555 crd->txfifo = udev->fifo[n + USB_FIFO_TX];
558 crd->rxfifo = udev->fifo[n + USB_FIFO_RX];
560 /* fill out fifo index */
561 DPRINTFN(5, "fifo index = %d\n", n);
570 usb_fifo_free(struct usb_fifo *f)
578 /* destroy symlink devices, if any */
579 for (n = 0; n != 2; n++) {
581 usb_free_symlink(f->symlink[n]);
582 f->symlink[n] = NULL;
585 mtx_lock(&usb_ref_lock);
587 /* delink ourselves to stop calls from userland */
588 if ((f->fifo_index < USB_FIFO_MAX) &&
590 (f->udev->fifo[f->fifo_index] == f)) {
591 f->udev->fifo[f->fifo_index] = NULL;
593 DPRINTFN(0, "USB FIFO %p has not been linked\n", f);
596 /* decrease refcount */
598 /* need to wait until all callers have exited */
599 while (f->refcount != 0) {
600 mtx_unlock(&usb_ref_lock); /* avoid LOR */
601 mtx_lock(f->priv_mtx);
602 /* prevent write flush, if any */
604 /* get I/O thread out of any sleep state */
605 if (f->flag_sleeping) {
606 f->flag_sleeping = 0;
607 cv_broadcast(&f->cv_io);
609 mtx_unlock(f->priv_mtx);
610 mtx_lock(&usb_ref_lock);
613 * Check if the "f->refcount" variable reached zero
614 * during the unlocked time before entering wait:
616 if (f->refcount == 0)
620 cv_wait(&f->cv_drain, &usb_ref_lock);
622 mtx_unlock(&usb_ref_lock);
624 /* take care of closing the device here, if any */
625 usb_fifo_close(f, 0);
627 cv_destroy(&f->cv_io);
628 cv_destroy(&f->cv_drain);
630 knlist_clear(&f->selinfo.si_note, 0);
631 seldrain(&f->selinfo);
632 knlist_destroy(&f->selinfo.si_note);
637 static struct usb_endpoint *
638 usb_dev_get_ep(struct usb_device *udev, uint8_t ep_index, uint8_t dir)
640 struct usb_endpoint *ep;
646 if (dir == USB_FIFO_RX) {
647 if (udev->flags.usb_mode == USB_MODE_HOST) {
653 if (udev->flags.usb_mode == USB_MODE_HOST) {
659 ep = usbd_get_ep_by_addr(udev, ep_index | ep_dir);
663 /* if the endpoint does not exist then return */
666 if (ep->edesc == NULL) {
667 /* invalid endpoint */
670 return (ep); /* success */
673 /*------------------------------------------------------------------------*
679 *------------------------------------------------------------------------*/
681 usb_fifo_open(struct usb_cdev_privdata *cpd,
682 struct usb_fifo *f, int fflags)
688 DPRINTFN(2, "no FIFO\n");
691 /* remove FWRITE and FREAD flags */
692 fflags &= ~(FWRITE | FREAD);
694 /* set correct file flags */
695 if ((f->fifo_index & 1) == USB_FIFO_TX) {
701 /* check if we are already opened */
702 /* we don't need any locks when checking this variable */
703 if (f->curr_cpd != NULL) {
708 /* reset short flag before open */
711 /* call open method */
712 err = (f->methods->f_open) (f, fflags);
716 mtx_lock(f->priv_mtx);
718 /* reset sleep flag */
719 f->flag_sleeping = 0;
721 /* reset error flag */
724 /* reset complete flag */
725 f->flag_iscomplete = 0;
727 /* reset select flag */
728 f->flag_isselect = 0;
730 /* reset flushing flag */
731 f->flag_flushing = 0;
733 /* reset ASYNC proc flag */
736 mtx_lock(&usb_ref_lock);
737 /* flag the fifo as opened to prevent others */
739 mtx_unlock(&usb_ref_lock);
744 mtx_unlock(f->priv_mtx);
749 /*------------------------------------------------------------------------*
751 *------------------------------------------------------------------------*/
753 usb_fifo_reset(struct usb_fifo *f)
761 USB_IF_DEQUEUE(&f->used_q, m);
763 USB_IF_ENQUEUE(&f->free_q, m);
768 /* reset have fragment flag */
769 f->flag_have_fragment = 0;
772 /*------------------------------------------------------------------------*
774 *------------------------------------------------------------------------*/
776 usb_fifo_close(struct usb_fifo *f, int fflags)
780 /* check if we are not opened */
781 if (f->curr_cpd == NULL) {
782 /* nothing to do - already closed */
785 mtx_lock(f->priv_mtx);
787 /* clear current cdev private data pointer */
788 mtx_lock(&usb_ref_lock);
790 mtx_unlock(&usb_ref_lock);
792 /* check if we are watched by kevent */
793 KNOTE_LOCKED(&f->selinfo.si_note, 0);
795 /* check if we are selected */
796 if (f->flag_isselect) {
797 selwakeup(&f->selinfo);
798 f->flag_isselect = 0;
800 /* check if a thread wants SIGIO */
801 if (f->async_p != NULL) {
802 PROC_LOCK(f->async_p);
803 kern_psignal(f->async_p, SIGIO);
804 PROC_UNLOCK(f->async_p);
807 /* remove FWRITE and FREAD flags */
808 fflags &= ~(FWRITE | FREAD);
810 /* flush written data, if any */
811 if ((f->fifo_index & 1) == USB_FIFO_TX) {
813 if (!f->flag_iserror) {
815 /* set flushing flag */
816 f->flag_flushing = 1;
818 /* get the last packet in */
819 if (f->flag_have_fragment) {
821 f->flag_have_fragment = 0;
822 USB_IF_DEQUEUE(&f->free_q, m);
824 USB_IF_ENQUEUE(&f->used_q, m);
828 /* start write transfer, if not already started */
829 (f->methods->f_start_write) (f);
831 /* check if flushed already */
832 while (f->flag_flushing &&
833 (!f->flag_iserror)) {
834 /* wait until all data has been written */
835 f->flag_sleeping = 1;
836 err = cv_timedwait_sig(&f->cv_io, f->priv_mtx,
837 USB_MS_TO_TICKS(USB_DEFAULT_TIMEOUT));
839 DPRINTF("signal received\n");
846 /* stop write transfer, if not already stopped */
847 (f->methods->f_stop_write) (f);
851 /* stop write transfer, if not already stopped */
852 (f->methods->f_stop_read) (f);
855 /* check if we are sleeping */
856 if (f->flag_sleeping) {
857 DPRINTFN(2, "Sleeping at close!\n");
859 mtx_unlock(f->priv_mtx);
861 /* call close method */
862 (f->methods->f_close) (f, fflags);
867 /*------------------------------------------------------------------------*
868 * usb_open - cdev callback
869 *------------------------------------------------------------------------*/
871 usb_open(struct cdev *dev, int fflags, int devtype, struct thread *td)
873 struct usb_fs_privdata* pd = (struct usb_fs_privdata*)dev->si_drv1;
874 struct usb_cdev_refdata refs;
875 struct usb_cdev_privdata *cpd;
878 DPRINTFN(2, "%s fflags=0x%08x\n", devtoname(dev), fflags);
880 KASSERT(fflags & (FREAD|FWRITE), ("invalid open flags"));
881 if (((fflags & FREAD) && !(pd->mode & FREAD)) ||
882 ((fflags & FWRITE) && !(pd->mode & FWRITE))) {
883 DPRINTFN(2, "access mode not supported\n");
887 cpd = malloc(sizeof(*cpd), M_USBDEV, M_WAITOK | M_ZERO);
888 ep = cpd->ep_addr = pd->ep_addr;
890 usb_loc_fill(pd, cpd);
891 err = usb_ref_device(cpd, &refs, 1);
893 DPRINTFN(2, "cannot ref device\n");
897 cpd->fflags = fflags; /* access mode for open lifetime */
899 /* create FIFOs, if any */
900 err = usb_fifo_create(cpd, &refs);
901 /* check for error */
903 DPRINTFN(2, "cannot create fifo\n");
904 usb_unref_device(cpd, &refs);
908 if (fflags & FREAD) {
909 err = usb_fifo_open(cpd, refs.rxfifo, fflags);
911 DPRINTFN(2, "read open failed\n");
912 usb_unref_device(cpd, &refs);
917 if (fflags & FWRITE) {
918 err = usb_fifo_open(cpd, refs.txfifo, fflags);
920 DPRINTFN(2, "write open failed\n");
921 if (fflags & FREAD) {
922 usb_fifo_close(refs.rxfifo, fflags);
924 usb_unref_device(cpd, &refs);
929 usb_unref_device(cpd, &refs);
930 devfs_set_cdevpriv(cpd, usb_close);
935 /*------------------------------------------------------------------------*
936 * usb_close - cdev callback
937 *------------------------------------------------------------------------*/
941 struct usb_cdev_refdata refs;
942 struct usb_cdev_privdata *cpd = arg;
945 DPRINTFN(2, "cpd=%p\n", cpd);
947 err = usb_ref_device(cpd, &refs,
948 2 /* uref and allow detached state */);
950 DPRINTFN(2, "Cannot grab USB reference when "
951 "closing USB file handle\n");
954 if (cpd->fflags & FREAD) {
955 usb_fifo_close(refs.rxfifo, cpd->fflags);
957 if (cpd->fflags & FWRITE) {
958 usb_fifo_close(refs.txfifo, cpd->fflags);
960 usb_unref_device(cpd, &refs);
966 usb_dev_init(void *arg)
968 mtx_init(&usb_ref_lock, "USB ref mutex", NULL, MTX_DEF);
969 sx_init(&usb_sym_lock, "USB sym mutex");
970 TAILQ_INIT(&usb_sym_head);
972 /* check the UGEN methods */
973 usb_fifo_check_methods(&usb_ugen_methods);
976 SYSINIT(usb_dev_init, SI_SUB_KLD, SI_ORDER_FIRST, usb_dev_init, NULL);
979 usb_dev_init_post(void *arg)
982 * Create /dev/usb - this is needed for usbconfig(8), which
983 * needs a well-known device name to access.
985 usb_dev = make_dev(&usb_static_devsw, 0, UID_ROOT, GID_OPERATOR,
986 0644, USB_DEVICE_NAME);
987 if (usb_dev == NULL) {
988 DPRINTFN(0, "Could not create usb bus device\n");
992 SYSINIT(usb_dev_init_post, SI_SUB_KICK_SCHEDULER, SI_ORDER_FIRST, usb_dev_init_post, NULL);
995 usb_dev_uninit(void *arg)
997 if (usb_dev != NULL) {
998 destroy_dev(usb_dev);
1001 mtx_destroy(&usb_ref_lock);
1002 sx_destroy(&usb_sym_lock);
1005 SYSUNINIT(usb_dev_uninit, SI_SUB_KICK_SCHEDULER, SI_ORDER_ANY, usb_dev_uninit, NULL);
1008 usb_ioctl_f_sub(struct usb_fifo *f, u_long cmd, void *addr,
1015 *(int *)addr = 0; /* character device */
1019 /* handled by upper FS layer */
1024 if (f->async_p != NULL) {
1028 f->async_p = USB_TD_GET_PROC(td);
1034 /* XXX this is not the most general solution */
1036 if (f->async_p == NULL) {
1040 if (*(int *)addr != USB_PROC_GET_GID(f->async_p)) {
1048 DPRINTFN(3, "cmd 0x%lx = %d\n", cmd, error);
1052 /*------------------------------------------------------------------------*
1053 * usb_ioctl - cdev callback
1054 *------------------------------------------------------------------------*/
1056 usb_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int fflag, struct thread* td)
1058 struct usb_cdev_refdata refs;
1059 struct usb_cdev_privdata* cpd;
1064 DPRINTFN(2, "cmd=0x%lx\n", cmd);
1066 err = devfs_get_cdevpriv((void **)&cpd);
1071 * Performance optimisation: We try to check for IOCTL's that
1072 * don't need the USB reference first. Then we grab the USB
1073 * reference if we need it!
1075 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1079 fflags = cpd->fflags;
1081 f = NULL; /* set default value */
1082 err = ENOIOCTL; /* set default value */
1084 if (fflags & FWRITE) {
1086 err = usb_ioctl_f_sub(f, cmd, addr, td);
1088 if (fflags & FREAD) {
1090 err = usb_ioctl_f_sub(f, cmd, addr, td);
1092 KASSERT(f != NULL, ("fifo not found"));
1093 if (err != ENOIOCTL)
1096 err = (f->methods->f_ioctl) (f, cmd, addr, fflags);
1098 DPRINTFN(2, "f_ioctl cmd 0x%lx = %d\n", cmd, err);
1100 if (err != ENOIOCTL)
1103 if (usb_usb_ref_device(cpd, &refs)) {
1104 /* we lost the reference */
1108 err = (f->methods->f_ioctl_post) (f, cmd, addr, fflags);
1110 DPRINTFN(2, "f_ioctl_post cmd 0x%lx = %d\n", cmd, err);
1112 if (err == ENOIOCTL)
1118 /* Wait for re-enumeration, if any */
1120 while (f->udev->re_enumerate_wait != USB_RE_ENUM_DONE) {
1122 usb_unref_device(cpd, &refs);
1124 usb_pause_mtx(NULL, hz / 128);
1126 while (usb_ref_device(cpd, &refs, 1 /* need uref */)) {
1127 if (usb_ref_device(cpd, &refs, 0)) {
1128 /* device no longer exists */
1131 usb_unref_device(cpd, &refs);
1132 usb_pause_mtx(NULL, hz / 128);
1137 usb_unref_device(cpd, &refs);
1142 usb_filter_detach(struct knote *kn)
1144 struct usb_fifo *f = kn->kn_hook;
1145 knlist_remove(&f->selinfo.si_note, kn, 0);
1149 usb_filter_write(struct knote *kn, long hint)
1151 struct usb_cdev_privdata* cpd;
1159 mtx_assert(f->priv_mtx, MA_OWNED);
1164 } else if (f->fs_ep_max == 0) {
1165 if (f->flag_iserror) {
1166 /* we got an error */
1169 if (f->queue_data == NULL) {
1171 * start write transfer, if not
1174 (f->methods->f_start_write) (f);
1176 /* check if any packets are available */
1177 USB_IF_POLL(&f->free_q, m);
1180 if (f->flag_iscomplete) {
1190 usb_filter_read(struct knote *kn, long hint)
1192 struct usb_cdev_privdata* cpd;
1200 mtx_assert(f->priv_mtx, MA_OWNED);
1205 } else if (f->fs_ep_max == 0) {
1206 if (f->flag_iserror) {
1207 /* we have an error */
1210 if (f->queue_data == NULL) {
1212 * start read transfer, if not
1215 (f->methods->f_start_read) (f);
1217 /* check if any packets are available */
1218 USB_IF_POLL(&f->used_q, m);
1220 /* start reading data, if any */
1222 (f->methods->f_start_read) (f);
1225 if (f->flag_iscomplete) {
1234 static struct filterops usb_filtops_write = {
1236 .f_detach = usb_filter_detach,
1237 .f_event = usb_filter_write,
1240 static struct filterops usb_filtops_read = {
1242 .f_detach = usb_filter_detach,
1243 .f_event = usb_filter_read,
1249 usb_kqfilter(struct cdev* dev, struct knote *kn)
1251 struct usb_cdev_refdata refs;
1252 struct usb_cdev_privdata* cpd;
1259 if (devfs_get_cdevpriv((void **)&cpd) != 0 ||
1260 usb_ref_device(cpd, &refs, 0) != 0)
1263 fflags = cpd->fflags;
1265 /* Figure out who needs service */
1266 switch (kn->kn_filter) {
1268 if (fflags & FWRITE) {
1270 kn->kn_fop = &usb_filtops_write;
1275 if (fflags & FREAD) {
1277 kn->kn_fop = &usb_filtops_read;
1288 mtx_lock(f->priv_mtx);
1289 knlist_add(&f->selinfo.si_note, kn, 1);
1290 mtx_unlock(f->priv_mtx);
1293 usb_unref_device(cpd, &refs);
1299 usb_poll(struct cdev* dev, int events, struct thread* td)
1301 struct usb_cdev_refdata refs;
1302 struct usb_cdev_privdata* cpd;
1305 int fflags, revents;
1307 if (devfs_get_cdevpriv((void **)&cpd) != 0 ||
1308 usb_ref_device(cpd, &refs, 0) != 0)
1310 (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM));
1312 fflags = cpd->fflags;
1314 /* Figure out who needs service */
1316 if ((events & (POLLOUT | POLLWRNORM)) &&
1317 (fflags & FWRITE)) {
1321 mtx_lock(f->priv_mtx);
1323 if (!refs.is_usbfs) {
1324 if (f->flag_iserror) {
1325 /* we got an error */
1328 if (f->queue_data == NULL) {
1330 * start write transfer, if not
1333 (f->methods->f_start_write) (f);
1335 /* check if any packets are available */
1336 USB_IF_POLL(&f->free_q, m);
1339 if (f->flag_iscomplete) {
1347 revents |= events & (POLLOUT | POLLWRNORM);
1349 f->flag_isselect = 1;
1350 selrecord(td, &f->selinfo);
1353 mtx_unlock(f->priv_mtx);
1355 if ((events & (POLLIN | POLLRDNORM)) &&
1360 mtx_lock(f->priv_mtx);
1362 if (!refs.is_usbfs) {
1363 if (f->flag_iserror) {
1364 /* we have an error */
1367 if (f->queue_data == NULL) {
1369 * start read transfer, if not
1372 (f->methods->f_start_read) (f);
1374 /* check if any packets are available */
1375 USB_IF_POLL(&f->used_q, m);
1378 if (f->flag_iscomplete) {
1386 revents |= events & (POLLIN | POLLRDNORM);
1388 f->flag_isselect = 1;
1389 selrecord(td, &f->selinfo);
1391 if (!refs.is_usbfs) {
1392 /* start reading data */
1393 (f->methods->f_start_read) (f);
1397 mtx_unlock(f->priv_mtx);
1399 usb_unref_device(cpd, &refs);
1404 usb_read(struct cdev *dev, struct uio *uio, int ioflag)
1406 struct usb_cdev_refdata refs;
1407 struct usb_cdev_privdata* cpd;
1414 uint8_t tr_data = 0;
1416 err = devfs_get_cdevpriv((void **)&cpd);
1420 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1424 fflags = cpd->fflags;
1428 /* should not happen */
1429 usb_unref_device(cpd, &refs);
1433 resid = uio->uio_resid;
1435 mtx_lock(f->priv_mtx);
1437 /* check for permanent read error */
1438 if (f->flag_iserror) {
1442 /* check if USB-FS interface is active */
1443 if (refs.is_usbfs) {
1445 * The queue is used for events that should be
1446 * retrieved using the "USB_FS_COMPLETE" ioctl.
1451 while (uio->uio_resid > 0) {
1453 USB_IF_DEQUEUE(&f->used_q, m);
1457 /* start read transfer, if not already started */
1459 (f->methods->f_start_read) (f);
1461 if (ioflag & IO_NDELAY) {
1463 /* return length before error */
1469 DPRINTF("sleeping\n");
1471 err = usb_fifo_wait(f);
1477 if (f->methods->f_filter_read) {
1479 * Sometimes it is convenient to process data at the
1480 * expense of a userland process instead of a kernel
1483 (f->methods->f_filter_read) (f, m);
1487 io_len = MIN(m->cur_data_len, uio->uio_resid);
1489 DPRINTFN(2, "transfer %d bytes from %p\n",
1490 io_len, m->cur_data_ptr);
1492 err = usb_fifo_uiomove(f,
1493 m->cur_data_ptr, io_len, uio);
1495 m->cur_data_len -= io_len;
1496 m->cur_data_ptr += io_len;
1498 if (m->cur_data_len == 0) {
1500 uint8_t last_packet;
1502 last_packet = m->last_packet;
1504 USB_IF_ENQUEUE(&f->free_q, m);
1511 USB_IF_PREPEND(&f->used_q, m);
1519 mtx_unlock(f->priv_mtx);
1521 usb_unref_device(cpd, &refs);
1527 usb_write(struct cdev *dev, struct uio *uio, int ioflag)
1529 struct usb_cdev_refdata refs;
1530 struct usb_cdev_privdata* cpd;
1538 uint8_t tr_data = 0;
1542 err = devfs_get_cdevpriv((void **)&cpd);
1546 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1550 fflags = cpd->fflags;
1554 /* should not happen */
1555 usb_unref_device(cpd, &refs);
1558 resid = uio->uio_resid;
1560 mtx_lock(f->priv_mtx);
1562 /* check for permanent write error */
1563 if (f->flag_iserror) {
1567 /* check if USB-FS interface is active */
1568 if (refs.is_usbfs) {
1570 * The queue is used for events that should be
1571 * retrieved using the "USB_FS_COMPLETE" ioctl.
1576 if (f->queue_data == NULL) {
1577 /* start write transfer, if not already started */
1578 (f->methods->f_start_write) (f);
1580 /* we allow writing zero length data */
1582 USB_IF_DEQUEUE(&f->free_q, m);
1586 if (ioflag & IO_NDELAY) {
1588 /* return length before error */
1594 DPRINTF("sleeping\n");
1596 err = usb_fifo_wait(f);
1604 if (f->flag_have_fragment == 0) {
1606 io_len = m->cur_data_len;
1607 pdata = m->cur_data_ptr;
1608 if (io_len > uio->uio_resid)
1609 io_len = uio->uio_resid;
1610 m->cur_data_len = io_len;
1612 io_len = m->max_data_len - m->cur_data_len;
1613 pdata = m->cur_data_ptr + m->cur_data_len;
1614 if (io_len > uio->uio_resid)
1615 io_len = uio->uio_resid;
1616 m->cur_data_len += io_len;
1619 DPRINTFN(2, "transfer %d bytes to %p\n",
1622 err = usb_fifo_uiomove(f, pdata, io_len, uio);
1625 f->flag_have_fragment = 0;
1626 USB_IF_ENQUEUE(&f->free_q, m);
1630 /* check if the buffer is ready to be transmitted */
1632 if ((f->flag_write_defrag == 0) ||
1633 (m->cur_data_len == m->max_data_len)) {
1634 f->flag_have_fragment = 0;
1637 * Check for write filter:
1639 * Sometimes it is convenient to process data
1640 * at the expense of a userland process
1641 * instead of a kernel process.
1643 if (f->methods->f_filter_write) {
1644 (f->methods->f_filter_write) (f, m);
1647 /* Put USB mbuf in the used queue */
1648 USB_IF_ENQUEUE(&f->used_q, m);
1650 /* Start writing data, if not already started */
1651 (f->methods->f_start_write) (f);
1653 /* Wait for more data or close */
1654 f->flag_have_fragment = 1;
1655 USB_IF_PREPEND(&f->free_q, m);
1658 } while (uio->uio_resid > 0);
1660 mtx_unlock(f->priv_mtx);
1662 usb_unref_device(cpd, &refs);
1668 usb_static_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
1672 struct usb_read_dir *urd;
1680 err = usb_read_symlink(u.urd->urd_data,
1681 u.urd->urd_startentry, u.urd->urd_maxlen);
1683 case USB_DEV_QUIRK_GET:
1684 case USB_QUIRK_NAME_GET:
1685 case USB_DEV_QUIRK_ADD:
1686 case USB_DEV_QUIRK_REMOVE:
1687 err = usb_quirk_ioctl_p(cmd, data, fflag, td);
1689 case USB_GET_TEMPLATE:
1690 *(int *)data = usb_template;
1693 case USB_SET_TEMPLATE:
1694 err = priv_check(curthread, PRIV_DRIVER);
1697 usb_template = *(int *)data;
1707 usb_fifo_uiomove(struct usb_fifo *f, void *cp,
1708 int n, struct uio *uio)
1712 mtx_unlock(f->priv_mtx);
1715 * "uiomove()" can sleep so one needs to make a wrapper,
1716 * exiting the mutex and checking things:
1718 error = uiomove(cp, n, uio);
1720 mtx_lock(f->priv_mtx);
1726 usb_fifo_wait(struct usb_fifo *f)
1730 mtx_assert(f->priv_mtx, MA_OWNED);
1732 if (f->flag_iserror) {
1736 f->flag_sleeping = 1;
1738 err = cv_wait_sig(&f->cv_io, f->priv_mtx);
1740 if (f->flag_iserror) {
1748 usb_fifo_signal(struct usb_fifo *f)
1750 if (f->flag_sleeping) {
1751 f->flag_sleeping = 0;
1752 cv_broadcast(&f->cv_io);
1757 usb_fifo_wakeup(struct usb_fifo *f)
1761 KNOTE_LOCKED(&f->selinfo.si_note, 0);
1763 if (f->flag_isselect) {
1764 selwakeup(&f->selinfo);
1765 f->flag_isselect = 0;
1767 if (f->async_p != NULL) {
1768 PROC_LOCK(f->async_p);
1769 kern_psignal(f->async_p, SIGIO);
1770 PROC_UNLOCK(f->async_p);
1775 usb_fifo_dummy_open(struct usb_fifo *fifo, int fflags)
1781 usb_fifo_dummy_close(struct usb_fifo *fifo, int fflags)
1787 usb_fifo_dummy_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
1793 usb_fifo_dummy_cmd(struct usb_fifo *fifo)
1795 fifo->flag_flushing = 0; /* not flushing */
1799 usb_fifo_check_methods(struct usb_fifo_methods *pm)
1801 /* check that all callback functions are OK */
1803 if (pm->f_open == NULL)
1804 pm->f_open = &usb_fifo_dummy_open;
1806 if (pm->f_close == NULL)
1807 pm->f_close = &usb_fifo_dummy_close;
1809 if (pm->f_ioctl == NULL)
1810 pm->f_ioctl = &usb_fifo_dummy_ioctl;
1812 if (pm->f_ioctl_post == NULL)
1813 pm->f_ioctl_post = &usb_fifo_dummy_ioctl;
1815 if (pm->f_start_read == NULL)
1816 pm->f_start_read = &usb_fifo_dummy_cmd;
1818 if (pm->f_stop_read == NULL)
1819 pm->f_stop_read = &usb_fifo_dummy_cmd;
1821 if (pm->f_start_write == NULL)
1822 pm->f_start_write = &usb_fifo_dummy_cmd;
1824 if (pm->f_stop_write == NULL)
1825 pm->f_stop_write = &usb_fifo_dummy_cmd;
1828 /*------------------------------------------------------------------------*
1831 * The following function will create a duplex FIFO.
1836 *------------------------------------------------------------------------*/
1838 usb_fifo_attach(struct usb_device *udev, void *priv_sc,
1839 struct mtx *priv_mtx, struct usb_fifo_methods *pm,
1840 struct usb_fifo_sc *f_sc, uint16_t unit, int16_t subunit,
1841 uint8_t iface_index, uid_t uid, gid_t gid, int mode)
1843 struct usb_fifo *f_tx;
1844 struct usb_fifo *f_rx;
1848 f_sc->fp[USB_FIFO_TX] = NULL;
1849 f_sc->fp[USB_FIFO_RX] = NULL;
1854 /* check the methods */
1855 usb_fifo_check_methods(pm);
1857 if (priv_mtx == NULL)
1860 /* search for a free FIFO slot */
1861 for (n = 0;; n += 2) {
1863 if (n == USB_FIFO_MAX) {
1864 /* end of FIFOs reached */
1867 /* Check for TX FIFO */
1868 if (udev->fifo[n + USB_FIFO_TX] != NULL) {
1871 /* Check for RX FIFO */
1872 if (udev->fifo[n + USB_FIFO_RX] != NULL) {
1878 f_tx = usb_fifo_alloc(priv_mtx);
1879 f_rx = usb_fifo_alloc(priv_mtx);
1881 if ((f_tx == NULL) || (f_rx == NULL)) {
1882 usb_fifo_free(f_tx);
1883 usb_fifo_free(f_rx);
1886 /* initialise FIFO structures */
1888 f_tx->fifo_index = n + USB_FIFO_TX;
1889 f_tx->dev_ep_index = -1;
1890 f_tx->priv_sc0 = priv_sc;
1892 f_tx->iface_index = iface_index;
1895 f_rx->fifo_index = n + USB_FIFO_RX;
1896 f_rx->dev_ep_index = -1;
1897 f_rx->priv_sc0 = priv_sc;
1899 f_rx->iface_index = iface_index;
1902 f_sc->fp[USB_FIFO_TX] = f_tx;
1903 f_sc->fp[USB_FIFO_RX] = f_rx;
1905 mtx_lock(&usb_ref_lock);
1906 udev->fifo[f_tx->fifo_index] = f_tx;
1907 udev->fifo[f_rx->fifo_index] = f_rx;
1908 mtx_unlock(&usb_ref_lock);
1910 for (n = 0; n != 4; n++) {
1912 if (pm->basename[n] == NULL) {
1916 if (snprintf(devname, sizeof(devname),
1917 "%s%u%s", pm->basename[n],
1918 unit, pm->postfix[n] ?
1919 pm->postfix[n] : "")) {
1923 if (snprintf(devname, sizeof(devname),
1924 "%s%u.%d%s", pm->basename[n],
1925 unit, subunit, pm->postfix[n] ?
1926 pm->postfix[n] : "")) {
1932 * Distribute the symbolic links into two FIFO structures:
1935 f_rx->symlink[n / 2] =
1936 usb_alloc_symlink(devname);
1938 f_tx->symlink[n / 2] =
1939 usb_alloc_symlink(devname);
1942 /* Create the device */
1943 f_sc->dev = usb_make_dev(udev, devname, -1,
1944 f_tx->fifo_index & f_rx->fifo_index,
1945 FREAD|FWRITE, uid, gid, mode);
1948 DPRINTFN(2, "attached %p/%p\n", f_tx, f_rx);
1952 /*------------------------------------------------------------------------*
1953 * usb_fifo_alloc_buffer
1958 *------------------------------------------------------------------------*/
1960 usb_fifo_alloc_buffer(struct usb_fifo *f, usb_size_t bufsize,
1963 usb_fifo_free_buffer(f);
1965 /* allocate an endpoint */
1966 f->free_q.ifq_maxlen = nbuf;
1967 f->used_q.ifq_maxlen = nbuf;
1969 f->queue_data = usb_alloc_mbufs(
1970 M_USBDEV, &f->free_q, bufsize, nbuf);
1972 if ((f->queue_data == NULL) && bufsize && nbuf) {
1975 return (0); /* success */
1978 /*------------------------------------------------------------------------*
1979 * usb_fifo_free_buffer
1981 * This function will free the buffers associated with a FIFO. This
1982 * function can be called multiple times in a row.
1983 *------------------------------------------------------------------------*/
1985 usb_fifo_free_buffer(struct usb_fifo *f)
1987 if (f->queue_data) {
1988 /* free old buffer */
1989 free(f->queue_data, M_USBDEV);
1990 f->queue_data = NULL;
1994 memset(&f->free_q, 0, sizeof(f->free_q));
1995 memset(&f->used_q, 0, sizeof(f->used_q));
1999 usb_fifo_detach(struct usb_fifo_sc *f_sc)
2004 usb_fifo_free(f_sc->fp[USB_FIFO_TX]);
2005 usb_fifo_free(f_sc->fp[USB_FIFO_RX]);
2007 f_sc->fp[USB_FIFO_TX] = NULL;
2008 f_sc->fp[USB_FIFO_RX] = NULL;
2010 usb_destroy_dev(f_sc->dev);
2014 DPRINTFN(2, "detached %p\n", f_sc);
2018 usb_fifo_put_bytes_max(struct usb_fifo *f)
2023 USB_IF_POLL(&f->free_q, m);
2026 len = m->max_data_len;
2033 /*------------------------------------------------------------------------*
2037 * 0 - normal operation
2038 * 1 - set last packet flag to enforce framing
2039 *------------------------------------------------------------------------*/
2041 usb_fifo_put_data(struct usb_fifo *f, struct usb_page_cache *pc,
2042 usb_frlength_t offset, usb_frlength_t len, uint8_t what)
2045 usb_frlength_t io_len;
2047 while (len || (what == 1)) {
2049 USB_IF_DEQUEUE(&f->free_q, m);
2054 io_len = MIN(len, m->cur_data_len);
2056 usbd_copy_out(pc, offset, m->cur_data_ptr, io_len);
2058 m->cur_data_len = io_len;
2062 if ((len == 0) && (what == 1)) {
2065 USB_IF_ENQUEUE(&f->used_q, m);
2069 if ((len == 0) || (what == 1)) {
2079 usb_fifo_put_data_linear(struct usb_fifo *f, void *ptr,
2080 usb_size_t len, uint8_t what)
2085 while (len || (what == 1)) {
2087 USB_IF_DEQUEUE(&f->free_q, m);
2092 io_len = MIN(len, m->cur_data_len);
2094 memcpy(m->cur_data_ptr, ptr, io_len);
2096 m->cur_data_len = io_len;
2097 ptr = USB_ADD_BYTES(ptr, io_len);
2100 if ((len == 0) && (what == 1)) {
2103 USB_IF_ENQUEUE(&f->used_q, m);
2107 if ((len == 0) || (what == 1)) {
2117 usb_fifo_put_data_buffer(struct usb_fifo *f, void *ptr, usb_size_t len)
2121 USB_IF_DEQUEUE(&f->free_q, m);
2124 m->cur_data_len = len;
2125 m->cur_data_ptr = ptr;
2126 USB_IF_ENQUEUE(&f->used_q, m);
2134 usb_fifo_put_data_error(struct usb_fifo *f)
2136 f->flag_iserror = 1;
2140 /*------------------------------------------------------------------------*
2144 * 0 - normal operation
2145 * 1 - only get one "usb_mbuf"
2149 * 1 - data in buffer
2150 *------------------------------------------------------------------------*/
2152 usb_fifo_get_data(struct usb_fifo *f, struct usb_page_cache *pc,
2153 usb_frlength_t offset, usb_frlength_t len, usb_frlength_t *actlen,
2157 usb_frlength_t io_len;
2158 uint8_t tr_data = 0;
2164 USB_IF_DEQUEUE(&f->used_q, m);
2170 io_len = MIN(len, m->cur_data_len);
2172 usbd_copy_in(pc, offset, m->cur_data_ptr, io_len);
2176 actlen[0] += io_len;
2177 m->cur_data_ptr += io_len;
2178 m->cur_data_len -= io_len;
2180 if ((m->cur_data_len == 0) || (what == 1)) {
2181 USB_IF_ENQUEUE(&f->free_q, m);
2189 USB_IF_PREPEND(&f->used_q, m);
2194 /* wait for data to be written out */
2197 if (f->flag_flushing) {
2198 /* check if we should send a short packet */
2199 if (f->flag_short != 0) {
2204 /* flushing complete */
2205 f->flag_flushing = 0;
2218 usb_fifo_get_data_linear(struct usb_fifo *f, void *ptr,
2219 usb_size_t len, usb_size_t *actlen, uint8_t what)
2223 uint8_t tr_data = 0;
2229 USB_IF_DEQUEUE(&f->used_q, m);
2235 io_len = MIN(len, m->cur_data_len);
2237 memcpy(ptr, m->cur_data_ptr, io_len);
2240 ptr = USB_ADD_BYTES(ptr, io_len);
2241 actlen[0] += io_len;
2242 m->cur_data_ptr += io_len;
2243 m->cur_data_len -= io_len;
2245 if ((m->cur_data_len == 0) || (what == 1)) {
2246 USB_IF_ENQUEUE(&f->free_q, m);
2254 USB_IF_PREPEND(&f->used_q, m);
2259 /* wait for data to be written out */
2262 if (f->flag_flushing) {
2263 /* check if we should send a short packet */
2264 if (f->flag_short != 0) {
2269 /* flushing complete */
2270 f->flag_flushing = 0;
2283 usb_fifo_get_data_buffer(struct usb_fifo *f, void **pptr, usb_size_t *plen)
2287 USB_IF_POLL(&f->used_q, m);
2290 *plen = m->cur_data_len;
2291 *pptr = m->cur_data_ptr;
2299 usb_fifo_get_data_error(struct usb_fifo *f)
2301 f->flag_iserror = 1;
2305 /*------------------------------------------------------------------------*
2310 * Else: Pointer to symlink entry
2311 *------------------------------------------------------------------------*/
2312 struct usb_symlink *
2313 usb_alloc_symlink(const char *target)
2315 struct usb_symlink *ps;
2317 ps = malloc(sizeof(*ps), M_USBDEV, M_WAITOK);
2321 /* XXX no longer needed */
2322 strlcpy(ps->src_path, target, sizeof(ps->src_path));
2323 ps->src_len = strlen(ps->src_path);
2324 strlcpy(ps->dst_path, target, sizeof(ps->dst_path));
2325 ps->dst_len = strlen(ps->dst_path);
2327 sx_xlock(&usb_sym_lock);
2328 TAILQ_INSERT_TAIL(&usb_sym_head, ps, sym_entry);
2329 sx_unlock(&usb_sym_lock);
2333 /*------------------------------------------------------------------------*
2335 *------------------------------------------------------------------------*/
2337 usb_free_symlink(struct usb_symlink *ps)
2342 sx_xlock(&usb_sym_lock);
2343 TAILQ_REMOVE(&usb_sym_head, ps, sym_entry);
2344 sx_unlock(&usb_sym_lock);
2349 /*------------------------------------------------------------------------*
2355 *------------------------------------------------------------------------*/
2357 usb_read_symlink(uint8_t *user_ptr, uint32_t startentry, uint32_t user_len)
2359 struct usb_symlink *ps;
2365 sx_xlock(&usb_sym_lock);
2367 TAILQ_FOREACH(ps, &usb_sym_head, sym_entry) {
2370 * Compute total length of source and destination symlink
2371 * strings pluss one length byte and two NUL bytes:
2373 temp = ps->src_len + ps->dst_len + 3;
2377 * Skip entry because this length cannot fit
2382 if (startentry != 0) {
2383 /* decrement read offset */
2387 if (temp > user_len) {
2388 /* out of buffer space */
2393 /* copy out total length */
2395 error = copyout(&len,
2396 USB_ADD_BYTES(user_ptr, delta), 1);
2402 /* copy out source string */
2404 error = copyout(ps->src_path,
2405 USB_ADD_BYTES(user_ptr, delta), ps->src_len);
2410 delta += ps->src_len;
2411 error = copyout(&len,
2412 USB_ADD_BYTES(user_ptr, delta), 1);
2418 /* copy out destination string */
2420 error = copyout(ps->dst_path,
2421 USB_ADD_BYTES(user_ptr, delta), ps->dst_len);
2426 delta += ps->dst_len;
2427 error = copyout(&len,
2428 USB_ADD_BYTES(user_ptr, delta), 1);
2437 /* a zero length entry indicates the end */
2439 if ((user_len != 0) && (error == 0)) {
2443 error = copyout(&len,
2444 USB_ADD_BYTES(user_ptr, delta), 1);
2446 sx_unlock(&usb_sym_lock);
2451 usb_fifo_set_close_zlp(struct usb_fifo *f, uint8_t onoff)
2456 /* send a Zero Length Packet, ZLP, before close */
2457 f->flag_short = onoff;
2461 usb_fifo_set_write_defrag(struct usb_fifo *f, uint8_t onoff)
2466 /* defrag written data */
2467 f->flag_write_defrag = onoff;
2468 /* reset defrag state */
2469 f->flag_have_fragment = 0;
2473 usb_fifo_softc(struct usb_fifo *f)
2475 return (f->priv_sc0);
2477 #endif /* USB_HAVE_UGEN */