3 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 * Copyright (c) 2006-2008 Hans Petter Selasky. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * usb_dev.c - An abstraction layer for creating devices under /dev/...
32 #ifdef USB_GLOBAL_INCLUDE_FILE
33 #include USB_GLOBAL_INCLUDE_FILE
35 #include <sys/stdint.h>
36 #include <sys/stddef.h>
37 #include <sys/param.h>
38 #include <sys/queue.h>
39 #include <sys/types.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
43 #include <sys/module.h>
45 #include <sys/mutex.h>
46 #include <sys/condvar.h>
47 #include <sys/sysctl.h>
49 #include <sys/unistd.h>
50 #include <sys/callout.h>
51 #include <sys/malloc.h>
53 #include <sys/vnode.h>
55 #include <sys/fcntl.h>
57 #include <dev/usb/usb.h>
58 #include <dev/usb/usb_ioctl.h>
59 #include <dev/usb/usbdi.h>
60 #include <dev/usb/usbdi_util.h>
62 #define USB_DEBUG_VAR usb_fifo_debug
64 #include <dev/usb/usb_core.h>
65 #include <dev/usb/usb_dev.h>
66 #include <dev/usb/usb_mbuf.h>
67 #include <dev/usb/usb_process.h>
68 #include <dev/usb/usb_device.h>
69 #include <dev/usb/usb_debug.h>
70 #include <dev/usb/usb_busdma.h>
71 #include <dev/usb/usb_generic.h>
72 #include <dev/usb/usb_dynamic.h>
73 #include <dev/usb/usb_util.h>
75 #include <dev/usb/usb_controller.h>
76 #include <dev/usb/usb_bus.h>
78 #include <sys/filio.h>
79 #include <sys/ttycom.h>
80 #include <sys/syscallsubr.h>
82 #include <machine/stdarg.h>
83 #endif /* USB_GLOBAL_INCLUDE_FILE */
88 static int usb_fifo_debug = 0;
90 static SYSCTL_NODE(_hw_usb, OID_AUTO, dev, CTLFLAG_RW, 0, "USB device");
91 SYSCTL_INT(_hw_usb_dev, OID_AUTO, debug, CTLFLAG_RWTUN,
92 &usb_fifo_debug, 0, "Debug Level");
95 #if ((__FreeBSD_version >= 700001) || (__FreeBSD_version == 0) || \
96 ((__FreeBSD_version >= 600034) && (__FreeBSD_version < 700000)))
97 #define USB_UCRED struct ucred *ucred,
104 static int usb_fifo_open(struct usb_cdev_privdata *,
105 struct usb_fifo *, int);
106 static void usb_fifo_close(struct usb_fifo *, int);
107 static void usb_dev_init(void *);
108 static void usb_dev_init_post(void *);
109 static void usb_dev_uninit(void *);
110 static int usb_fifo_uiomove(struct usb_fifo *, void *, int,
112 static void usb_fifo_check_methods(struct usb_fifo_methods *);
113 static struct usb_fifo *usb_fifo_alloc(struct mtx *);
114 static struct usb_endpoint *usb_dev_get_ep(struct usb_device *, uint8_t,
116 static void usb_loc_fill(struct usb_fs_privdata *,
117 struct usb_cdev_privdata *);
118 static void usb_close(void *);
119 static usb_error_t usb_ref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *, int);
120 static usb_error_t usb_usb_ref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *);
121 static void usb_unref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *);
123 static d_open_t usb_open;
124 static d_ioctl_t usb_ioctl;
125 static d_read_t usb_read;
126 static d_write_t usb_write;
127 static d_poll_t usb_poll;
128 static d_kqfilter_t usb_kqfilter;
130 static d_ioctl_t usb_static_ioctl;
132 static usb_fifo_open_t usb_fifo_dummy_open;
133 static usb_fifo_close_t usb_fifo_dummy_close;
134 static usb_fifo_ioctl_t usb_fifo_dummy_ioctl;
135 static usb_fifo_cmd_t usb_fifo_dummy_cmd;
137 /* character device structure used for devices (/dev/ugenX.Y and /dev/uXXX) */
138 struct cdevsw usb_devsw = {
139 .d_version = D_VERSION,
141 .d_ioctl = usb_ioctl,
143 .d_flags = D_TRACKCLOSE,
145 .d_write = usb_write,
147 .d_kqfilter = usb_kqfilter,
150 static struct cdev* usb_dev = NULL;
152 /* character device structure used for /dev/usb */
153 static struct cdevsw usb_static_devsw = {
154 .d_version = D_VERSION,
155 .d_ioctl = usb_static_ioctl,
159 static TAILQ_HEAD(, usb_symlink) usb_sym_head;
160 static struct sx usb_sym_lock;
162 struct mtx usb_ref_lock;
164 /*------------------------------------------------------------------------*
167 * This is used to fill out a usb_cdev_privdata structure based on the
168 * device's address as contained in usb_fs_privdata.
169 *------------------------------------------------------------------------*/
171 usb_loc_fill(struct usb_fs_privdata* pd, struct usb_cdev_privdata *cpd)
173 cpd->bus_index = pd->bus_index;
174 cpd->dev_index = pd->dev_index;
175 cpd->ep_addr = pd->ep_addr;
176 cpd->fifo_index = pd->fifo_index;
179 /*------------------------------------------------------------------------*
182 * This function is used to atomically refer an USB device by its
183 * device location. If this function returns success the USB device
184 * will not disappear until the USB device is unreferenced.
187 * 0: Success, refcount incremented on the given USB device.
189 *------------------------------------------------------------------------*/
191 usb_ref_device(struct usb_cdev_privdata *cpd,
192 struct usb_cdev_refdata *crd, int need_uref)
194 struct usb_fifo **ppf;
197 DPRINTFN(2, "cpd=%p need uref=%d\n", cpd, need_uref);
200 memset(crd, 0, sizeof(*crd));
202 mtx_lock(&usb_ref_lock);
203 cpd->bus = devclass_get_softc(usb_devclass_ptr, cpd->bus_index);
204 if (cpd->bus == NULL) {
205 DPRINTFN(2, "no bus at %u\n", cpd->bus_index);
208 cpd->udev = cpd->bus->devices[cpd->dev_index];
209 if (cpd->udev == NULL) {
210 DPRINTFN(2, "no device at %u\n", cpd->dev_index);
213 if (cpd->udev->state == USB_STATE_DETACHED &&
215 DPRINTFN(2, "device is detached\n");
219 DPRINTFN(2, "ref udev - needed\n");
221 if (cpd->udev->refcount == USB_DEV_REF_MAX) {
222 DPRINTFN(2, "no dev ref\n");
225 cpd->udev->refcount++;
227 mtx_unlock(&usb_ref_lock);
230 * We need to grab the enumeration SX-lock before
231 * grabbing the FIFO refs to avoid deadlock at detach!
233 crd->do_unlock = usbd_enum_lock_sig(cpd->udev);
235 mtx_lock(&usb_ref_lock);
238 * Set "is_uref" after grabbing the default SX lock
242 /* check for signal */
243 if (crd->do_unlock > 1) {
249 /* check if we are doing an open */
250 if (cpd->fflags == 0) {
251 /* use zero defaults */
253 /* check for write */
254 if (cpd->fflags & FWRITE) {
255 ppf = cpd->udev->fifo;
256 f = ppf[cpd->fifo_index + USB_FIFO_TX];
258 crd->is_write = 1; /* ref */
259 if (f == NULL || f->refcount == USB_FIFO_REF_MAX)
261 if (f->curr_cpd != cpd)
263 /* check if USB-FS is active */
264 if (f->fs_ep_max != 0) {
270 if (cpd->fflags & FREAD) {
271 ppf = cpd->udev->fifo;
272 f = ppf[cpd->fifo_index + USB_FIFO_RX];
274 crd->is_read = 1; /* ref */
275 if (f == NULL || f->refcount == USB_FIFO_REF_MAX)
277 if (f->curr_cpd != cpd)
279 /* check if USB-FS is active */
280 if (f->fs_ep_max != 0) {
286 /* when everything is OK we increment the refcounts */
288 DPRINTFN(2, "ref write\n");
289 crd->txfifo->refcount++;
292 DPRINTFN(2, "ref read\n");
293 crd->rxfifo->refcount++;
295 mtx_unlock(&usb_ref_lock);
301 usbd_enum_unlock(cpd->udev);
304 if (--(cpd->udev->refcount) == 0)
305 cv_broadcast(&cpd->udev->ref_cv);
307 mtx_unlock(&usb_ref_lock);
308 DPRINTFN(2, "fail\n");
311 memset(crd, 0, sizeof(*crd));
313 return (USB_ERR_INVAL);
316 /*------------------------------------------------------------------------*
319 * This function is used to upgrade an USB reference to include the
320 * USB device reference on a USB location.
323 * 0: Success, refcount incremented on the given USB device.
325 *------------------------------------------------------------------------*/
327 usb_usb_ref_device(struct usb_cdev_privdata *cpd,
328 struct usb_cdev_refdata *crd)
331 * Check if we already got an USB reference on this location:
334 return (0); /* success */
337 * To avoid deadlock at detach we need to drop the FIFO ref
338 * and re-acquire a new ref!
340 usb_unref_device(cpd, crd);
342 return (usb_ref_device(cpd, crd, 1 /* need uref */));
345 /*------------------------------------------------------------------------*
348 * This function will release the reference count by one unit for the
350 *------------------------------------------------------------------------*/
352 usb_unref_device(struct usb_cdev_privdata *cpd,
353 struct usb_cdev_refdata *crd)
356 DPRINTFN(2, "cpd=%p is_uref=%d\n", cpd, crd->is_uref);
359 usbd_enum_unlock(cpd->udev);
361 mtx_lock(&usb_ref_lock);
363 if (--(crd->rxfifo->refcount) == 0) {
364 cv_signal(&crd->rxfifo->cv_drain);
369 if (--(crd->txfifo->refcount) == 0) {
370 cv_signal(&crd->txfifo->cv_drain);
376 if (--(cpd->udev->refcount) == 0)
377 cv_broadcast(&cpd->udev->ref_cv);
379 mtx_unlock(&usb_ref_lock);
382 static struct usb_fifo *
383 usb_fifo_alloc(struct mtx *mtx)
387 f = malloc(sizeof(*f), M_USBDEV, M_WAITOK | M_ZERO);
388 cv_init(&f->cv_io, "FIFO-IO");
389 cv_init(&f->cv_drain, "FIFO-DRAIN");
392 knlist_init_mtx(&f->selinfo.si_note, mtx);
396 /*------------------------------------------------------------------------*
398 *------------------------------------------------------------------------*/
400 usb_fifo_create(struct usb_cdev_privdata *cpd,
401 struct usb_cdev_refdata *crd)
403 struct usb_device *udev = cpd->udev;
405 struct usb_endpoint *ep;
411 int e = cpd->ep_addr;
413 is_tx = (cpd->fflags & FWRITE) ? 1 : 0;
414 is_rx = (cpd->fflags & FREAD) ? 1 : 0;
418 /* Preallocated FIFO */
420 DPRINTFN(5, "Preallocated FIFO\n");
422 f = udev->fifo[cpd->fifo_index + USB_FIFO_TX];
428 f = udev->fifo[cpd->fifo_index + USB_FIFO_RX];
436 KASSERT(e >= 0 && e <= 15, ("endpoint %d out of range", e));
438 /* search for a free FIFO slot */
439 DPRINTFN(5, "Endpoint device, searching for 0x%02x\n", e);
440 for (n = 0;; n += 2) {
442 if (n == USB_FIFO_MAX) {
447 /* end of FIFOs reached */
448 DPRINTFN(5, "out of FIFOs\n");
452 /* Check for TX FIFO */
454 f = udev->fifo[n + USB_FIFO_TX];
456 if (f->dev_ep_index != e) {
457 /* wrong endpoint index */
460 if (f->curr_cpd != NULL) {
465 } else if (no_null) {
469 /* Check for RX FIFO */
471 f = udev->fifo[n + USB_FIFO_RX];
473 if (f->dev_ep_index != e) {
474 /* wrong endpoint index */
477 if (f->curr_cpd != NULL) {
482 } else if (no_null) {
490 if (e >= (USB_EP_MAX / 2)) {
491 /* we don't create any endpoints in this range */
492 DPRINTFN(5, "ep out of range\n");
493 return (is_busy ? EBUSY : EINVAL);
497 if ((e != 0) && is_busy) {
499 * Only the default control endpoint is allowed to be
500 * opened multiple times!
502 DPRINTFN(5, "busy\n");
508 (udev->fifo[n + USB_FIFO_TX] == NULL)) {
509 ep = usb_dev_get_ep(udev, e, USB_FIFO_TX);
510 DPRINTFN(5, "dev_get_endpoint(%d, 0x%x)\n", e, USB_FIFO_TX);
512 DPRINTFN(5, "dev_get_endpoint returned NULL\n");
515 f = usb_fifo_alloc(&udev->device_mtx);
517 DPRINTFN(5, "could not alloc tx fifo\n");
520 /* update some fields */
521 f->fifo_index = n + USB_FIFO_TX;
524 f->methods = &usb_ugen_methods;
525 f->iface_index = ep->iface_index;
527 mtx_lock(&usb_ref_lock);
528 udev->fifo[n + USB_FIFO_TX] = f;
529 mtx_unlock(&usb_ref_lock);
533 (udev->fifo[n + USB_FIFO_RX] == NULL)) {
535 ep = usb_dev_get_ep(udev, e, USB_FIFO_RX);
536 DPRINTFN(5, "dev_get_endpoint(%d, 0x%x)\n", e, USB_FIFO_RX);
538 DPRINTFN(5, "dev_get_endpoint returned NULL\n");
541 f = usb_fifo_alloc(&udev->device_mtx);
543 DPRINTFN(5, "could not alloc rx fifo\n");
546 /* update some fields */
547 f->fifo_index = n + USB_FIFO_RX;
550 f->methods = &usb_ugen_methods;
551 f->iface_index = ep->iface_index;
553 mtx_lock(&usb_ref_lock);
554 udev->fifo[n + USB_FIFO_RX] = f;
555 mtx_unlock(&usb_ref_lock);
558 crd->txfifo = udev->fifo[n + USB_FIFO_TX];
561 crd->rxfifo = udev->fifo[n + USB_FIFO_RX];
563 /* fill out fifo index */
564 DPRINTFN(5, "fifo index = %d\n", n);
573 usb_fifo_free(struct usb_fifo *f)
581 /* destroy symlink devices, if any */
582 for (n = 0; n != 2; n++) {
584 usb_free_symlink(f->symlink[n]);
585 f->symlink[n] = NULL;
588 mtx_lock(&usb_ref_lock);
590 /* delink ourselves to stop calls from userland */
591 if ((f->fifo_index < USB_FIFO_MAX) &&
593 (f->udev->fifo[f->fifo_index] == f)) {
594 f->udev->fifo[f->fifo_index] = NULL;
596 DPRINTFN(0, "USB FIFO %p has not been linked\n", f);
599 /* decrease refcount */
601 /* need to wait until all callers have exited */
602 while (f->refcount != 0) {
603 mtx_unlock(&usb_ref_lock); /* avoid LOR */
604 mtx_lock(f->priv_mtx);
605 /* prevent write flush, if any */
607 /* get I/O thread out of any sleep state */
608 if (f->flag_sleeping) {
609 f->flag_sleeping = 0;
610 cv_broadcast(&f->cv_io);
612 mtx_unlock(f->priv_mtx);
613 mtx_lock(&usb_ref_lock);
616 * Check if the "f->refcount" variable reached zero
617 * during the unlocked time before entering wait:
619 if (f->refcount == 0)
623 cv_wait(&f->cv_drain, &usb_ref_lock);
625 mtx_unlock(&usb_ref_lock);
627 /* take care of closing the device here, if any */
628 usb_fifo_close(f, 0);
630 cv_destroy(&f->cv_io);
631 cv_destroy(&f->cv_drain);
633 knlist_clear(&f->selinfo.si_note, 0);
634 seldrain(&f->selinfo);
635 knlist_destroy(&f->selinfo.si_note);
640 static struct usb_endpoint *
641 usb_dev_get_ep(struct usb_device *udev, uint8_t ep_index, uint8_t dir)
643 struct usb_endpoint *ep;
649 if (dir == USB_FIFO_RX) {
650 if (udev->flags.usb_mode == USB_MODE_HOST) {
656 if (udev->flags.usb_mode == USB_MODE_HOST) {
662 ep = usbd_get_ep_by_addr(udev, ep_index | ep_dir);
666 /* if the endpoint does not exist then return */
669 if (ep->edesc == NULL) {
670 /* invalid endpoint */
673 return (ep); /* success */
676 /*------------------------------------------------------------------------*
682 *------------------------------------------------------------------------*/
684 usb_fifo_open(struct usb_cdev_privdata *cpd,
685 struct usb_fifo *f, int fflags)
691 DPRINTFN(2, "no FIFO\n");
694 /* remove FWRITE and FREAD flags */
695 fflags &= ~(FWRITE | FREAD);
697 /* set correct file flags */
698 if ((f->fifo_index & 1) == USB_FIFO_TX) {
704 /* check if we are already opened */
705 /* we don't need any locks when checking this variable */
706 if (f->curr_cpd != NULL) {
711 /* reset short flag before open */
714 /* call open method */
715 err = (f->methods->f_open) (f, fflags);
719 mtx_lock(f->priv_mtx);
721 /* reset sleep flag */
722 f->flag_sleeping = 0;
724 /* reset error flag */
727 /* reset complete flag */
728 f->flag_iscomplete = 0;
730 /* reset select flag */
731 f->flag_isselect = 0;
733 /* reset flushing flag */
734 f->flag_flushing = 0;
736 /* reset ASYNC proc flag */
739 mtx_lock(&usb_ref_lock);
740 /* flag the fifo as opened to prevent others */
742 mtx_unlock(&usb_ref_lock);
747 mtx_unlock(f->priv_mtx);
752 /*------------------------------------------------------------------------*
754 *------------------------------------------------------------------------*/
756 usb_fifo_reset(struct usb_fifo *f)
764 USB_IF_DEQUEUE(&f->used_q, m);
766 USB_IF_ENQUEUE(&f->free_q, m);
771 /* reset have fragment flag */
772 f->flag_have_fragment = 0;
775 /*------------------------------------------------------------------------*
777 *------------------------------------------------------------------------*/
779 usb_fifo_close(struct usb_fifo *f, int fflags)
783 /* check if we are not opened */
784 if (f->curr_cpd == NULL) {
785 /* nothing to do - already closed */
788 mtx_lock(f->priv_mtx);
790 /* clear current cdev private data pointer */
791 mtx_lock(&usb_ref_lock);
793 mtx_unlock(&usb_ref_lock);
795 /* check if we are watched by kevent */
796 KNOTE_LOCKED(&f->selinfo.si_note, 0);
798 /* check if we are selected */
799 if (f->flag_isselect) {
800 selwakeup(&f->selinfo);
801 f->flag_isselect = 0;
803 /* check if a thread wants SIGIO */
804 if (f->async_p != NULL) {
805 PROC_LOCK(f->async_p);
806 kern_psignal(f->async_p, SIGIO);
807 PROC_UNLOCK(f->async_p);
810 /* remove FWRITE and FREAD flags */
811 fflags &= ~(FWRITE | FREAD);
813 /* flush written data, if any */
814 if ((f->fifo_index & 1) == USB_FIFO_TX) {
816 if (!f->flag_iserror) {
818 /* set flushing flag */
819 f->flag_flushing = 1;
821 /* get the last packet in */
822 if (f->flag_have_fragment) {
824 f->flag_have_fragment = 0;
825 USB_IF_DEQUEUE(&f->free_q, m);
827 USB_IF_ENQUEUE(&f->used_q, m);
831 /* start write transfer, if not already started */
832 (f->methods->f_start_write) (f);
834 /* check if flushed already */
835 while (f->flag_flushing &&
836 (!f->flag_iserror)) {
837 /* wait until all data has been written */
838 f->flag_sleeping = 1;
839 err = cv_timedwait_sig(&f->cv_io, f->priv_mtx,
840 USB_MS_TO_TICKS(USB_DEFAULT_TIMEOUT));
842 DPRINTF("signal received\n");
849 /* stop write transfer, if not already stopped */
850 (f->methods->f_stop_write) (f);
854 /* stop write transfer, if not already stopped */
855 (f->methods->f_stop_read) (f);
858 /* check if we are sleeping */
859 if (f->flag_sleeping) {
860 DPRINTFN(2, "Sleeping at close!\n");
862 mtx_unlock(f->priv_mtx);
864 /* call close method */
865 (f->methods->f_close) (f, fflags);
870 /*------------------------------------------------------------------------*
871 * usb_open - cdev callback
872 *------------------------------------------------------------------------*/
874 usb_open(struct cdev *dev, int fflags, int devtype, struct thread *td)
876 struct usb_fs_privdata* pd = (struct usb_fs_privdata*)dev->si_drv1;
877 struct usb_cdev_refdata refs;
878 struct usb_cdev_privdata *cpd;
881 DPRINTFN(2, "%s fflags=0x%08x\n", devtoname(dev), fflags);
883 KASSERT(fflags & (FREAD|FWRITE), ("invalid open flags"));
884 if (((fflags & FREAD) && !(pd->mode & FREAD)) ||
885 ((fflags & FWRITE) && !(pd->mode & FWRITE))) {
886 DPRINTFN(2, "access mode not supported\n");
890 cpd = malloc(sizeof(*cpd), M_USBDEV, M_WAITOK | M_ZERO);
892 usb_loc_fill(pd, cpd);
893 err = usb_ref_device(cpd, &refs, 1);
895 DPRINTFN(2, "cannot ref device\n");
899 cpd->fflags = fflags; /* access mode for open lifetime */
901 /* create FIFOs, if any */
902 err = usb_fifo_create(cpd, &refs);
903 /* check for error */
905 DPRINTFN(2, "cannot create fifo\n");
906 usb_unref_device(cpd, &refs);
910 if (fflags & FREAD) {
911 err = usb_fifo_open(cpd, refs.rxfifo, fflags);
913 DPRINTFN(2, "read open failed\n");
914 usb_unref_device(cpd, &refs);
919 if (fflags & FWRITE) {
920 err = usb_fifo_open(cpd, refs.txfifo, fflags);
922 DPRINTFN(2, "write open failed\n");
923 if (fflags & FREAD) {
924 usb_fifo_close(refs.rxfifo, fflags);
926 usb_unref_device(cpd, &refs);
931 usb_unref_device(cpd, &refs);
932 devfs_set_cdevpriv(cpd, usb_close);
937 /*------------------------------------------------------------------------*
938 * usb_close - cdev callback
939 *------------------------------------------------------------------------*/
943 struct usb_cdev_refdata refs;
944 struct usb_cdev_privdata *cpd = arg;
947 DPRINTFN(2, "cpd=%p\n", cpd);
949 err = usb_ref_device(cpd, &refs,
950 2 /* uref and allow detached state */);
952 DPRINTFN(2, "Cannot grab USB reference when "
953 "closing USB file handle\n");
956 if (cpd->fflags & FREAD) {
957 usb_fifo_close(refs.rxfifo, cpd->fflags);
959 if (cpd->fflags & FWRITE) {
960 usb_fifo_close(refs.txfifo, cpd->fflags);
962 usb_unref_device(cpd, &refs);
968 usb_dev_init(void *arg)
970 mtx_init(&usb_ref_lock, "USB ref mutex", NULL, MTX_DEF);
971 sx_init(&usb_sym_lock, "USB sym mutex");
972 TAILQ_INIT(&usb_sym_head);
974 /* check the UGEN methods */
975 usb_fifo_check_methods(&usb_ugen_methods);
978 SYSINIT(usb_dev_init, SI_SUB_KLD, SI_ORDER_FIRST, usb_dev_init, NULL);
981 usb_dev_init_post(void *arg)
984 * Create /dev/usb - this is needed for usbconfig(8), which
985 * needs a well-known device name to access.
987 usb_dev = make_dev(&usb_static_devsw, 0, UID_ROOT, GID_OPERATOR,
988 0644, USB_DEVICE_NAME);
989 if (usb_dev == NULL) {
990 DPRINTFN(0, "Could not create usb bus device\n");
994 SYSINIT(usb_dev_init_post, SI_SUB_KICK_SCHEDULER, SI_ORDER_FIRST, usb_dev_init_post, NULL);
997 usb_dev_uninit(void *arg)
999 if (usb_dev != NULL) {
1000 destroy_dev(usb_dev);
1003 mtx_destroy(&usb_ref_lock);
1004 sx_destroy(&usb_sym_lock);
1007 SYSUNINIT(usb_dev_uninit, SI_SUB_KICK_SCHEDULER, SI_ORDER_ANY, usb_dev_uninit, NULL);
1010 usb_ioctl_f_sub(struct usb_fifo *f, u_long cmd, void *addr,
1017 *(int *)addr = 0; /* character device */
1021 /* handled by upper FS layer */
1026 if (f->async_p != NULL) {
1030 f->async_p = USB_TD_GET_PROC(td);
1036 /* XXX this is not the most general solution */
1038 if (f->async_p == NULL) {
1042 if (*(int *)addr != USB_PROC_GET_GID(f->async_p)) {
1050 DPRINTFN(3, "cmd 0x%lx = %d\n", cmd, error);
1054 /*------------------------------------------------------------------------*
1055 * usb_ioctl - cdev callback
1056 *------------------------------------------------------------------------*/
1058 usb_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int fflag, struct thread* td)
1060 struct usb_cdev_refdata refs;
1061 struct usb_cdev_privdata* cpd;
1066 DPRINTFN(2, "cmd=0x%lx\n", cmd);
1068 err = devfs_get_cdevpriv((void **)&cpd);
1073 * Performance optimisation: We try to check for IOCTL's that
1074 * don't need the USB reference first. Then we grab the USB
1075 * reference if we need it!
1077 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1081 fflags = cpd->fflags;
1083 f = NULL; /* set default value */
1084 err = ENOIOCTL; /* set default value */
1086 if (fflags & FWRITE) {
1088 err = usb_ioctl_f_sub(f, cmd, addr, td);
1090 if (fflags & FREAD) {
1092 err = usb_ioctl_f_sub(f, cmd, addr, td);
1094 KASSERT(f != NULL, ("fifo not found"));
1095 if (err != ENOIOCTL)
1098 err = (f->methods->f_ioctl) (f, cmd, addr, fflags);
1100 DPRINTFN(2, "f_ioctl cmd 0x%lx = %d\n", cmd, err);
1102 if (err != ENOIOCTL)
1105 if (usb_usb_ref_device(cpd, &refs)) {
1106 /* we lost the reference */
1110 err = (f->methods->f_ioctl_post) (f, cmd, addr, fflags);
1112 DPRINTFN(2, "f_ioctl_post cmd 0x%lx = %d\n", cmd, err);
1114 if (err == ENOIOCTL)
1120 /* Wait for re-enumeration, if any */
1122 while (f->udev->re_enumerate_wait != USB_RE_ENUM_DONE) {
1124 usb_unref_device(cpd, &refs);
1126 usb_pause_mtx(NULL, hz / 128);
1128 while (usb_ref_device(cpd, &refs, 1 /* need uref */)) {
1129 if (usb_ref_device(cpd, &refs, 0)) {
1130 /* device no longer exists */
1133 usb_unref_device(cpd, &refs);
1134 usb_pause_mtx(NULL, hz / 128);
1139 usb_unref_device(cpd, &refs);
1144 usb_filter_detach(struct knote *kn)
1146 struct usb_fifo *f = kn->kn_hook;
1147 knlist_remove(&f->selinfo.si_note, kn, 0);
1151 usb_filter_write(struct knote *kn, long hint)
1153 struct usb_cdev_privdata* cpd;
1161 USB_MTX_ASSERT(f->priv_mtx, MA_OWNED);
1166 } else if (f->fs_ep_max == 0) {
1167 if (f->flag_iserror) {
1168 /* we got an error */
1171 if (f->queue_data == NULL) {
1173 * start write transfer, if not
1176 (f->methods->f_start_write) (f);
1178 /* check if any packets are available */
1179 USB_IF_POLL(&f->free_q, m);
1182 if (f->flag_iscomplete) {
1192 usb_filter_read(struct knote *kn, long hint)
1194 struct usb_cdev_privdata* cpd;
1202 USB_MTX_ASSERT(f->priv_mtx, MA_OWNED);
1207 } else if (f->fs_ep_max == 0) {
1208 if (f->flag_iserror) {
1209 /* we have an error */
1212 if (f->queue_data == NULL) {
1214 * start read transfer, if not
1217 (f->methods->f_start_read) (f);
1219 /* check if any packets are available */
1220 USB_IF_POLL(&f->used_q, m);
1222 /* start reading data, if any */
1224 (f->methods->f_start_read) (f);
1227 if (f->flag_iscomplete) {
1236 static struct filterops usb_filtops_write = {
1238 .f_detach = usb_filter_detach,
1239 .f_event = usb_filter_write,
1242 static struct filterops usb_filtops_read = {
1244 .f_detach = usb_filter_detach,
1245 .f_event = usb_filter_read,
1251 usb_kqfilter(struct cdev* dev, struct knote *kn)
1253 struct usb_cdev_refdata refs;
1254 struct usb_cdev_privdata* cpd;
1261 if (devfs_get_cdevpriv((void **)&cpd) != 0 ||
1262 usb_ref_device(cpd, &refs, 0) != 0)
1265 fflags = cpd->fflags;
1267 /* Figure out who needs service */
1268 switch (kn->kn_filter) {
1270 if (fflags & FWRITE) {
1272 kn->kn_fop = &usb_filtops_write;
1277 if (fflags & FREAD) {
1279 kn->kn_fop = &usb_filtops_read;
1290 mtx_lock(f->priv_mtx);
1291 knlist_add(&f->selinfo.si_note, kn, 1);
1292 mtx_unlock(f->priv_mtx);
1295 usb_unref_device(cpd, &refs);
1301 usb_poll(struct cdev* dev, int events, struct thread* td)
1303 struct usb_cdev_refdata refs;
1304 struct usb_cdev_privdata* cpd;
1307 int fflags, revents;
1309 if (devfs_get_cdevpriv((void **)&cpd) != 0 ||
1310 usb_ref_device(cpd, &refs, 0) != 0)
1312 (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM));
1314 fflags = cpd->fflags;
1316 /* Figure out who needs service */
1318 if ((events & (POLLOUT | POLLWRNORM)) &&
1319 (fflags & FWRITE)) {
1323 mtx_lock(f->priv_mtx);
1325 if (!refs.is_usbfs) {
1326 if (f->flag_iserror) {
1327 /* we got an error */
1330 if (f->queue_data == NULL) {
1332 * start write transfer, if not
1335 (f->methods->f_start_write) (f);
1337 /* check if any packets are available */
1338 USB_IF_POLL(&f->free_q, m);
1341 if (f->flag_iscomplete) {
1349 revents |= events & (POLLOUT | POLLWRNORM);
1351 f->flag_isselect = 1;
1352 selrecord(td, &f->selinfo);
1355 mtx_unlock(f->priv_mtx);
1357 if ((events & (POLLIN | POLLRDNORM)) &&
1362 mtx_lock(f->priv_mtx);
1364 if (!refs.is_usbfs) {
1365 if (f->flag_iserror) {
1366 /* we have an error */
1369 if (f->queue_data == NULL) {
1371 * start read transfer, if not
1374 (f->methods->f_start_read) (f);
1376 /* check if any packets are available */
1377 USB_IF_POLL(&f->used_q, m);
1380 if (f->flag_iscomplete) {
1388 revents |= events & (POLLIN | POLLRDNORM);
1390 f->flag_isselect = 1;
1391 selrecord(td, &f->selinfo);
1393 if (!refs.is_usbfs) {
1394 /* start reading data */
1395 (f->methods->f_start_read) (f);
1399 mtx_unlock(f->priv_mtx);
1401 usb_unref_device(cpd, &refs);
1406 usb_read(struct cdev *dev, struct uio *uio, int ioflag)
1408 struct usb_cdev_refdata refs;
1409 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 */ );
1426 /* should not happen */
1427 usb_unref_device(cpd, &refs);
1431 mtx_lock(f->priv_mtx);
1433 /* check for permanent read error */
1434 if (f->flag_iserror) {
1438 /* check if USB-FS interface is active */
1439 if (refs.is_usbfs) {
1441 * The queue is used for events that should be
1442 * retrieved using the "USB_FS_COMPLETE" ioctl.
1447 while (uio->uio_resid > 0) {
1449 USB_IF_DEQUEUE(&f->used_q, m);
1453 /* start read transfer, if not already started */
1455 (f->methods->f_start_read) (f);
1457 if (ioflag & IO_NDELAY) {
1459 /* return length before error */
1465 DPRINTF("sleeping\n");
1467 err = usb_fifo_wait(f);
1473 if (f->methods->f_filter_read) {
1475 * Sometimes it is convenient to process data at the
1476 * expense of a userland process instead of a kernel
1479 (f->methods->f_filter_read) (f, m);
1483 io_len = MIN(m->cur_data_len, uio->uio_resid);
1485 DPRINTFN(2, "transfer %d bytes from %p\n",
1486 io_len, m->cur_data_ptr);
1488 err = usb_fifo_uiomove(f,
1489 m->cur_data_ptr, io_len, uio);
1491 m->cur_data_len -= io_len;
1492 m->cur_data_ptr += io_len;
1494 if (m->cur_data_len == 0) {
1496 uint8_t last_packet;
1498 last_packet = m->last_packet;
1500 USB_IF_ENQUEUE(&f->free_q, m);
1507 USB_IF_PREPEND(&f->used_q, m);
1515 mtx_unlock(f->priv_mtx);
1517 usb_unref_device(cpd, &refs);
1523 usb_write(struct cdev *dev, struct uio *uio, int ioflag)
1525 struct usb_cdev_refdata refs;
1526 struct usb_cdev_privdata* cpd;
1532 uint8_t tr_data = 0;
1536 err = devfs_get_cdevpriv((void **)&cpd);
1540 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1546 /* should not happen */
1547 usb_unref_device(cpd, &refs);
1551 mtx_lock(f->priv_mtx);
1553 /* check for permanent write error */
1554 if (f->flag_iserror) {
1558 /* check if USB-FS interface is active */
1559 if (refs.is_usbfs) {
1561 * The queue is used for events that should be
1562 * retrieved using the "USB_FS_COMPLETE" ioctl.
1567 if (f->queue_data == NULL) {
1568 /* start write transfer, if not already started */
1569 (f->methods->f_start_write) (f);
1571 /* we allow writing zero length data */
1573 USB_IF_DEQUEUE(&f->free_q, m);
1577 if (ioflag & IO_NDELAY) {
1579 /* return length before error */
1585 DPRINTF("sleeping\n");
1587 err = usb_fifo_wait(f);
1595 if (f->flag_have_fragment == 0) {
1597 io_len = m->cur_data_len;
1598 pdata = m->cur_data_ptr;
1599 if (io_len > uio->uio_resid)
1600 io_len = uio->uio_resid;
1601 m->cur_data_len = io_len;
1603 io_len = m->max_data_len - m->cur_data_len;
1604 pdata = m->cur_data_ptr + m->cur_data_len;
1605 if (io_len > uio->uio_resid)
1606 io_len = uio->uio_resid;
1607 m->cur_data_len += io_len;
1610 DPRINTFN(2, "transfer %d bytes to %p\n",
1613 err = usb_fifo_uiomove(f, pdata, io_len, uio);
1616 f->flag_have_fragment = 0;
1617 USB_IF_ENQUEUE(&f->free_q, m);
1621 /* check if the buffer is ready to be transmitted */
1623 if ((f->flag_write_defrag == 0) ||
1624 (m->cur_data_len == m->max_data_len)) {
1625 f->flag_have_fragment = 0;
1628 * Check for write filter:
1630 * Sometimes it is convenient to process data
1631 * at the expense of a userland process
1632 * instead of a kernel process.
1634 if (f->methods->f_filter_write) {
1635 (f->methods->f_filter_write) (f, m);
1638 /* Put USB mbuf in the used queue */
1639 USB_IF_ENQUEUE(&f->used_q, m);
1641 /* Start writing data, if not already started */
1642 (f->methods->f_start_write) (f);
1644 /* Wait for more data or close */
1645 f->flag_have_fragment = 1;
1646 USB_IF_PREPEND(&f->free_q, m);
1649 } while (uio->uio_resid > 0);
1651 mtx_unlock(f->priv_mtx);
1653 usb_unref_device(cpd, &refs);
1659 usb_static_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
1663 struct usb_read_dir *urd;
1671 err = usb_read_symlink(u.urd->urd_data,
1672 u.urd->urd_startentry, u.urd->urd_maxlen);
1674 case USB_DEV_QUIRK_GET:
1675 case USB_QUIRK_NAME_GET:
1676 case USB_DEV_QUIRK_ADD:
1677 case USB_DEV_QUIRK_REMOVE:
1678 err = usb_quirk_ioctl_p(cmd, data, fflag, td);
1680 case USB_GET_TEMPLATE:
1681 *(int *)data = usb_template;
1684 case USB_SET_TEMPLATE:
1685 err = priv_check(curthread, PRIV_DRIVER);
1688 usb_template = *(int *)data;
1698 usb_fifo_uiomove(struct usb_fifo *f, void *cp,
1699 int n, struct uio *uio)
1703 mtx_unlock(f->priv_mtx);
1706 * "uiomove()" can sleep so one needs to make a wrapper,
1707 * exiting the mutex and checking things:
1709 error = uiomove(cp, n, uio);
1711 mtx_lock(f->priv_mtx);
1717 usb_fifo_wait(struct usb_fifo *f)
1721 USB_MTX_ASSERT(f->priv_mtx, MA_OWNED);
1723 if (f->flag_iserror) {
1727 f->flag_sleeping = 1;
1729 err = cv_wait_sig(&f->cv_io, f->priv_mtx);
1731 if (f->flag_iserror) {
1739 usb_fifo_signal(struct usb_fifo *f)
1741 if (f->flag_sleeping) {
1742 f->flag_sleeping = 0;
1743 cv_broadcast(&f->cv_io);
1748 usb_fifo_wakeup(struct usb_fifo *f)
1752 KNOTE_LOCKED(&f->selinfo.si_note, 0);
1754 if (f->flag_isselect) {
1755 selwakeup(&f->selinfo);
1756 f->flag_isselect = 0;
1758 if (f->async_p != NULL) {
1759 PROC_LOCK(f->async_p);
1760 kern_psignal(f->async_p, SIGIO);
1761 PROC_UNLOCK(f->async_p);
1766 usb_fifo_dummy_open(struct usb_fifo *fifo, int fflags)
1772 usb_fifo_dummy_close(struct usb_fifo *fifo, int fflags)
1778 usb_fifo_dummy_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
1784 usb_fifo_dummy_cmd(struct usb_fifo *fifo)
1786 fifo->flag_flushing = 0; /* not flushing */
1790 usb_fifo_check_methods(struct usb_fifo_methods *pm)
1792 /* check that all callback functions are OK */
1794 if (pm->f_open == NULL)
1795 pm->f_open = &usb_fifo_dummy_open;
1797 if (pm->f_close == NULL)
1798 pm->f_close = &usb_fifo_dummy_close;
1800 if (pm->f_ioctl == NULL)
1801 pm->f_ioctl = &usb_fifo_dummy_ioctl;
1803 if (pm->f_ioctl_post == NULL)
1804 pm->f_ioctl_post = &usb_fifo_dummy_ioctl;
1806 if (pm->f_start_read == NULL)
1807 pm->f_start_read = &usb_fifo_dummy_cmd;
1809 if (pm->f_stop_read == NULL)
1810 pm->f_stop_read = &usb_fifo_dummy_cmd;
1812 if (pm->f_start_write == NULL)
1813 pm->f_start_write = &usb_fifo_dummy_cmd;
1815 if (pm->f_stop_write == NULL)
1816 pm->f_stop_write = &usb_fifo_dummy_cmd;
1819 /*------------------------------------------------------------------------*
1822 * The following function will create a duplex FIFO.
1827 *------------------------------------------------------------------------*/
1829 usb_fifo_attach(struct usb_device *udev, void *priv_sc,
1830 struct mtx *priv_mtx, struct usb_fifo_methods *pm,
1831 struct usb_fifo_sc *f_sc, uint16_t unit, int16_t subunit,
1832 uint8_t iface_index, uid_t uid, gid_t gid, int mode)
1834 struct usb_fifo *f_tx;
1835 struct usb_fifo *f_rx;
1839 f_sc->fp[USB_FIFO_TX] = NULL;
1840 f_sc->fp[USB_FIFO_RX] = NULL;
1845 /* check the methods */
1846 usb_fifo_check_methods(pm);
1848 if (priv_mtx == NULL)
1851 /* search for a free FIFO slot */
1852 for (n = 0;; n += 2) {
1854 if (n == USB_FIFO_MAX) {
1855 /* end of FIFOs reached */
1858 /* Check for TX FIFO */
1859 if (udev->fifo[n + USB_FIFO_TX] != NULL) {
1862 /* Check for RX FIFO */
1863 if (udev->fifo[n + USB_FIFO_RX] != NULL) {
1869 f_tx = usb_fifo_alloc(priv_mtx);
1870 f_rx = usb_fifo_alloc(priv_mtx);
1872 if ((f_tx == NULL) || (f_rx == NULL)) {
1873 usb_fifo_free(f_tx);
1874 usb_fifo_free(f_rx);
1877 /* initialise FIFO structures */
1879 f_tx->fifo_index = n + USB_FIFO_TX;
1880 f_tx->dev_ep_index = -1;
1881 f_tx->priv_sc0 = priv_sc;
1883 f_tx->iface_index = iface_index;
1886 f_rx->fifo_index = n + USB_FIFO_RX;
1887 f_rx->dev_ep_index = -1;
1888 f_rx->priv_sc0 = priv_sc;
1890 f_rx->iface_index = iface_index;
1893 f_sc->fp[USB_FIFO_TX] = f_tx;
1894 f_sc->fp[USB_FIFO_RX] = f_rx;
1896 mtx_lock(&usb_ref_lock);
1897 udev->fifo[f_tx->fifo_index] = f_tx;
1898 udev->fifo[f_rx->fifo_index] = f_rx;
1899 mtx_unlock(&usb_ref_lock);
1901 for (n = 0; n != 4; n++) {
1903 if (pm->basename[n] == NULL) {
1907 if (snprintf(devname, sizeof(devname),
1908 "%s%u%s", pm->basename[n],
1909 unit, pm->postfix[n] ?
1910 pm->postfix[n] : "")) {
1914 if (snprintf(devname, sizeof(devname),
1915 "%s%u.%d%s", pm->basename[n],
1916 unit, subunit, pm->postfix[n] ?
1917 pm->postfix[n] : "")) {
1923 * Distribute the symbolic links into two FIFO structures:
1926 f_rx->symlink[n / 2] =
1927 usb_alloc_symlink(devname);
1929 f_tx->symlink[n / 2] =
1930 usb_alloc_symlink(devname);
1933 /* Create the device */
1934 f_sc->dev = usb_make_dev(udev, devname, -1,
1935 f_tx->fifo_index & f_rx->fifo_index,
1936 FREAD|FWRITE, uid, gid, mode);
1939 DPRINTFN(2, "attached %p/%p\n", f_tx, f_rx);
1943 /*------------------------------------------------------------------------*
1944 * usb_fifo_alloc_buffer
1949 *------------------------------------------------------------------------*/
1951 usb_fifo_alloc_buffer(struct usb_fifo *f, usb_size_t bufsize,
1954 usb_fifo_free_buffer(f);
1956 /* allocate an endpoint */
1957 f->free_q.ifq_maxlen = nbuf;
1958 f->used_q.ifq_maxlen = nbuf;
1960 f->queue_data = usb_alloc_mbufs(
1961 M_USBDEV, &f->free_q, bufsize, nbuf);
1963 if ((f->queue_data == NULL) && bufsize && nbuf) {
1966 return (0); /* success */
1969 /*------------------------------------------------------------------------*
1970 * usb_fifo_free_buffer
1972 * This function will free the buffers associated with a FIFO. This
1973 * function can be called multiple times in a row.
1974 *------------------------------------------------------------------------*/
1976 usb_fifo_free_buffer(struct usb_fifo *f)
1978 if (f->queue_data) {
1979 /* free old buffer */
1980 free(f->queue_data, M_USBDEV);
1981 f->queue_data = NULL;
1985 memset(&f->free_q, 0, sizeof(f->free_q));
1986 memset(&f->used_q, 0, sizeof(f->used_q));
1990 usb_fifo_detach(struct usb_fifo_sc *f_sc)
1995 usb_fifo_free(f_sc->fp[USB_FIFO_TX]);
1996 usb_fifo_free(f_sc->fp[USB_FIFO_RX]);
1998 f_sc->fp[USB_FIFO_TX] = NULL;
1999 f_sc->fp[USB_FIFO_RX] = NULL;
2001 usb_destroy_dev(f_sc->dev);
2005 DPRINTFN(2, "detached %p\n", f_sc);
2009 usb_fifo_put_bytes_max(struct usb_fifo *f)
2014 USB_IF_POLL(&f->free_q, m);
2017 len = m->max_data_len;
2024 /*------------------------------------------------------------------------*
2028 * 0 - normal operation
2029 * 1 - set last packet flag to enforce framing
2030 *------------------------------------------------------------------------*/
2032 usb_fifo_put_data(struct usb_fifo *f, struct usb_page_cache *pc,
2033 usb_frlength_t offset, usb_frlength_t len, uint8_t what)
2036 usb_frlength_t io_len;
2038 while (len || (what == 1)) {
2040 USB_IF_DEQUEUE(&f->free_q, m);
2045 io_len = MIN(len, m->cur_data_len);
2047 usbd_copy_out(pc, offset, m->cur_data_ptr, io_len);
2049 m->cur_data_len = io_len;
2053 if ((len == 0) && (what == 1)) {
2056 USB_IF_ENQUEUE(&f->used_q, m);
2060 if ((len == 0) || (what == 1)) {
2070 usb_fifo_put_data_linear(struct usb_fifo *f, void *ptr,
2071 usb_size_t len, uint8_t what)
2076 while (len || (what == 1)) {
2078 USB_IF_DEQUEUE(&f->free_q, m);
2083 io_len = MIN(len, m->cur_data_len);
2085 memcpy(m->cur_data_ptr, ptr, io_len);
2087 m->cur_data_len = io_len;
2088 ptr = USB_ADD_BYTES(ptr, io_len);
2091 if ((len == 0) && (what == 1)) {
2094 USB_IF_ENQUEUE(&f->used_q, m);
2098 if ((len == 0) || (what == 1)) {
2108 usb_fifo_put_data_buffer(struct usb_fifo *f, void *ptr, usb_size_t len)
2112 USB_IF_DEQUEUE(&f->free_q, m);
2115 m->cur_data_len = len;
2116 m->cur_data_ptr = ptr;
2117 USB_IF_ENQUEUE(&f->used_q, m);
2125 usb_fifo_put_data_error(struct usb_fifo *f)
2127 f->flag_iserror = 1;
2131 /*------------------------------------------------------------------------*
2135 * 0 - normal operation
2136 * 1 - only get one "usb_mbuf"
2140 * 1 - data in buffer
2141 *------------------------------------------------------------------------*/
2143 usb_fifo_get_data(struct usb_fifo *f, struct usb_page_cache *pc,
2144 usb_frlength_t offset, usb_frlength_t len, usb_frlength_t *actlen,
2148 usb_frlength_t io_len;
2149 uint8_t tr_data = 0;
2155 USB_IF_DEQUEUE(&f->used_q, m);
2161 io_len = MIN(len, m->cur_data_len);
2163 usbd_copy_in(pc, offset, m->cur_data_ptr, io_len);
2167 actlen[0] += io_len;
2168 m->cur_data_ptr += io_len;
2169 m->cur_data_len -= io_len;
2171 if ((m->cur_data_len == 0) || (what == 1)) {
2172 USB_IF_ENQUEUE(&f->free_q, m);
2180 USB_IF_PREPEND(&f->used_q, m);
2185 /* wait for data to be written out */
2188 if (f->flag_flushing) {
2189 /* check if we should send a short packet */
2190 if (f->flag_short != 0) {
2195 /* flushing complete */
2196 f->flag_flushing = 0;
2209 usb_fifo_get_data_linear(struct usb_fifo *f, void *ptr,
2210 usb_size_t len, usb_size_t *actlen, uint8_t what)
2214 uint8_t tr_data = 0;
2220 USB_IF_DEQUEUE(&f->used_q, m);
2226 io_len = MIN(len, m->cur_data_len);
2228 memcpy(ptr, m->cur_data_ptr, io_len);
2231 ptr = USB_ADD_BYTES(ptr, io_len);
2232 actlen[0] += io_len;
2233 m->cur_data_ptr += io_len;
2234 m->cur_data_len -= io_len;
2236 if ((m->cur_data_len == 0) || (what == 1)) {
2237 USB_IF_ENQUEUE(&f->free_q, m);
2245 USB_IF_PREPEND(&f->used_q, m);
2250 /* wait for data to be written out */
2253 if (f->flag_flushing) {
2254 /* check if we should send a short packet */
2255 if (f->flag_short != 0) {
2260 /* flushing complete */
2261 f->flag_flushing = 0;
2274 usb_fifo_get_data_buffer(struct usb_fifo *f, void **pptr, usb_size_t *plen)
2278 USB_IF_POLL(&f->used_q, m);
2281 *plen = m->cur_data_len;
2282 *pptr = m->cur_data_ptr;
2290 usb_fifo_get_data_error(struct usb_fifo *f)
2292 f->flag_iserror = 1;
2296 /*------------------------------------------------------------------------*
2301 * Else: Pointer to symlink entry
2302 *------------------------------------------------------------------------*/
2303 struct usb_symlink *
2304 usb_alloc_symlink(const char *target)
2306 struct usb_symlink *ps;
2308 ps = malloc(sizeof(*ps), M_USBDEV, M_WAITOK);
2309 /* XXX no longer needed */
2310 strlcpy(ps->src_path, target, sizeof(ps->src_path));
2311 ps->src_len = strlen(ps->src_path);
2312 strlcpy(ps->dst_path, target, sizeof(ps->dst_path));
2313 ps->dst_len = strlen(ps->dst_path);
2315 sx_xlock(&usb_sym_lock);
2316 TAILQ_INSERT_TAIL(&usb_sym_head, ps, sym_entry);
2317 sx_unlock(&usb_sym_lock);
2321 /*------------------------------------------------------------------------*
2323 *------------------------------------------------------------------------*/
2325 usb_free_symlink(struct usb_symlink *ps)
2330 sx_xlock(&usb_sym_lock);
2331 TAILQ_REMOVE(&usb_sym_head, ps, sym_entry);
2332 sx_unlock(&usb_sym_lock);
2337 /*------------------------------------------------------------------------*
2343 *------------------------------------------------------------------------*/
2345 usb_read_symlink(uint8_t *user_ptr, uint32_t startentry, uint32_t user_len)
2347 struct usb_symlink *ps;
2353 sx_xlock(&usb_sym_lock);
2355 TAILQ_FOREACH(ps, &usb_sym_head, sym_entry) {
2358 * Compute total length of source and destination symlink
2359 * strings pluss one length byte and two NUL bytes:
2361 temp = ps->src_len + ps->dst_len + 3;
2365 * Skip entry because this length cannot fit
2370 if (startentry != 0) {
2371 /* decrement read offset */
2375 if (temp > user_len) {
2376 /* out of buffer space */
2381 /* copy out total length */
2383 error = copyout(&len,
2384 USB_ADD_BYTES(user_ptr, delta), 1);
2390 /* copy out source string */
2392 error = copyout(ps->src_path,
2393 USB_ADD_BYTES(user_ptr, delta), ps->src_len);
2398 delta += ps->src_len;
2399 error = copyout(&len,
2400 USB_ADD_BYTES(user_ptr, delta), 1);
2406 /* copy out destination string */
2408 error = copyout(ps->dst_path,
2409 USB_ADD_BYTES(user_ptr, delta), ps->dst_len);
2414 delta += ps->dst_len;
2415 error = copyout(&len,
2416 USB_ADD_BYTES(user_ptr, delta), 1);
2425 /* a zero length entry indicates the end */
2427 if ((user_len != 0) && (error == 0)) {
2431 error = copyout(&len,
2432 USB_ADD_BYTES(user_ptr, delta), 1);
2434 sx_unlock(&usb_sym_lock);
2439 usb_fifo_set_close_zlp(struct usb_fifo *f, uint8_t onoff)
2444 /* send a Zero Length Packet, ZLP, before close */
2445 f->flag_short = onoff;
2449 usb_fifo_set_write_defrag(struct usb_fifo *f, uint8_t onoff)
2454 /* defrag written data */
2455 f->flag_write_defrag = onoff;
2456 /* reset defrag state */
2457 f->flag_have_fragment = 0;
2461 usb_fifo_softc(struct usb_fifo *f)
2463 return (f->priv_sc0);
2465 #endif /* USB_HAVE_UGEN */