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);
389 cv_init(&f->cv_io, "FIFO-IO");
390 cv_init(&f->cv_drain, "FIFO-DRAIN");
393 knlist_init_mtx(&f->selinfo.si_note, mtx);
398 /*------------------------------------------------------------------------*
400 *------------------------------------------------------------------------*/
402 usb_fifo_create(struct usb_cdev_privdata *cpd,
403 struct usb_cdev_refdata *crd)
405 struct usb_device *udev = cpd->udev;
407 struct usb_endpoint *ep;
413 int e = cpd->ep_addr;
415 is_tx = (cpd->fflags & FWRITE) ? 1 : 0;
416 is_rx = (cpd->fflags & FREAD) ? 1 : 0;
420 /* Preallocated FIFO */
422 DPRINTFN(5, "Preallocated FIFO\n");
424 f = udev->fifo[cpd->fifo_index + USB_FIFO_TX];
430 f = udev->fifo[cpd->fifo_index + USB_FIFO_RX];
438 KASSERT(e >= 0 && e <= 15, ("endpoint %d out of range", e));
440 /* search for a free FIFO slot */
441 DPRINTFN(5, "Endpoint device, searching for 0x%02x\n", e);
442 for (n = 0;; n += 2) {
444 if (n == USB_FIFO_MAX) {
449 /* end of FIFOs reached */
450 DPRINTFN(5, "out of FIFOs\n");
454 /* Check for TX FIFO */
456 f = udev->fifo[n + USB_FIFO_TX];
458 if (f->dev_ep_index != e) {
459 /* wrong endpoint index */
462 if (f->curr_cpd != NULL) {
467 } else if (no_null) {
471 /* Check for RX FIFO */
473 f = udev->fifo[n + USB_FIFO_RX];
475 if (f->dev_ep_index != e) {
476 /* wrong endpoint index */
479 if (f->curr_cpd != NULL) {
484 } else if (no_null) {
492 if (e >= (USB_EP_MAX / 2)) {
493 /* we don't create any endpoints in this range */
494 DPRINTFN(5, "ep out of range\n");
495 return (is_busy ? EBUSY : EINVAL);
499 if ((e != 0) && is_busy) {
501 * Only the default control endpoint is allowed to be
502 * opened multiple times!
504 DPRINTFN(5, "busy\n");
510 (udev->fifo[n + USB_FIFO_TX] == NULL)) {
511 ep = usb_dev_get_ep(udev, e, USB_FIFO_TX);
512 DPRINTFN(5, "dev_get_endpoint(%d, 0x%x)\n", e, USB_FIFO_TX);
514 DPRINTFN(5, "dev_get_endpoint returned NULL\n");
517 f = usb_fifo_alloc(&udev->device_mtx);
519 DPRINTFN(5, "could not alloc tx fifo\n");
522 /* update some fields */
523 f->fifo_index = n + USB_FIFO_TX;
526 f->methods = &usb_ugen_methods;
527 f->iface_index = ep->iface_index;
529 mtx_lock(&usb_ref_lock);
530 udev->fifo[n + USB_FIFO_TX] = f;
531 mtx_unlock(&usb_ref_lock);
535 (udev->fifo[n + USB_FIFO_RX] == NULL)) {
537 ep = usb_dev_get_ep(udev, e, USB_FIFO_RX);
538 DPRINTFN(5, "dev_get_endpoint(%d, 0x%x)\n", e, USB_FIFO_RX);
540 DPRINTFN(5, "dev_get_endpoint returned NULL\n");
543 f = usb_fifo_alloc(&udev->device_mtx);
545 DPRINTFN(5, "could not alloc rx fifo\n");
548 /* update some fields */
549 f->fifo_index = n + USB_FIFO_RX;
552 f->methods = &usb_ugen_methods;
553 f->iface_index = ep->iface_index;
555 mtx_lock(&usb_ref_lock);
556 udev->fifo[n + USB_FIFO_RX] = f;
557 mtx_unlock(&usb_ref_lock);
560 crd->txfifo = udev->fifo[n + USB_FIFO_TX];
563 crd->rxfifo = udev->fifo[n + USB_FIFO_RX];
565 /* fill out fifo index */
566 DPRINTFN(5, "fifo index = %d\n", n);
575 usb_fifo_free(struct usb_fifo *f)
583 /* destroy symlink devices, if any */
584 for (n = 0; n != 2; n++) {
586 usb_free_symlink(f->symlink[n]);
587 f->symlink[n] = NULL;
590 mtx_lock(&usb_ref_lock);
592 /* delink ourselves to stop calls from userland */
593 if ((f->fifo_index < USB_FIFO_MAX) &&
595 (f->udev->fifo[f->fifo_index] == f)) {
596 f->udev->fifo[f->fifo_index] = NULL;
598 DPRINTFN(0, "USB FIFO %p has not been linked\n", f);
601 /* decrease refcount */
603 /* need to wait until all callers have exited */
604 while (f->refcount != 0) {
605 mtx_unlock(&usb_ref_lock); /* avoid LOR */
606 mtx_lock(f->priv_mtx);
607 /* prevent write flush, if any */
609 /* get I/O thread out of any sleep state */
610 if (f->flag_sleeping) {
611 f->flag_sleeping = 0;
612 cv_broadcast(&f->cv_io);
614 mtx_unlock(f->priv_mtx);
615 mtx_lock(&usb_ref_lock);
618 * Check if the "f->refcount" variable reached zero
619 * during the unlocked time before entering wait:
621 if (f->refcount == 0)
625 cv_wait(&f->cv_drain, &usb_ref_lock);
627 mtx_unlock(&usb_ref_lock);
629 /* take care of closing the device here, if any */
630 usb_fifo_close(f, 0);
632 cv_destroy(&f->cv_io);
633 cv_destroy(&f->cv_drain);
635 knlist_clear(&f->selinfo.si_note, 0);
636 seldrain(&f->selinfo);
637 knlist_destroy(&f->selinfo.si_note);
642 static struct usb_endpoint *
643 usb_dev_get_ep(struct usb_device *udev, uint8_t ep_index, uint8_t dir)
645 struct usb_endpoint *ep;
651 if (dir == USB_FIFO_RX) {
652 if (udev->flags.usb_mode == USB_MODE_HOST) {
658 if (udev->flags.usb_mode == USB_MODE_HOST) {
664 ep = usbd_get_ep_by_addr(udev, ep_index | ep_dir);
668 /* if the endpoint does not exist then return */
671 if (ep->edesc == NULL) {
672 /* invalid endpoint */
675 return (ep); /* success */
678 /*------------------------------------------------------------------------*
684 *------------------------------------------------------------------------*/
686 usb_fifo_open(struct usb_cdev_privdata *cpd,
687 struct usb_fifo *f, int fflags)
693 DPRINTFN(2, "no FIFO\n");
696 /* remove FWRITE and FREAD flags */
697 fflags &= ~(FWRITE | FREAD);
699 /* set correct file flags */
700 if ((f->fifo_index & 1) == USB_FIFO_TX) {
706 /* check if we are already opened */
707 /* we don't need any locks when checking this variable */
708 if (f->curr_cpd != NULL) {
713 /* reset short flag before open */
716 /* call open method */
717 err = (f->methods->f_open) (f, fflags);
721 mtx_lock(f->priv_mtx);
723 /* reset sleep flag */
724 f->flag_sleeping = 0;
726 /* reset error flag */
729 /* reset complete flag */
730 f->flag_iscomplete = 0;
732 /* reset select flag */
733 f->flag_isselect = 0;
735 /* reset flushing flag */
736 f->flag_flushing = 0;
738 /* reset ASYNC proc flag */
741 mtx_lock(&usb_ref_lock);
742 /* flag the fifo as opened to prevent others */
744 mtx_unlock(&usb_ref_lock);
749 mtx_unlock(f->priv_mtx);
754 /*------------------------------------------------------------------------*
756 *------------------------------------------------------------------------*/
758 usb_fifo_reset(struct usb_fifo *f)
766 USB_IF_DEQUEUE(&f->used_q, m);
768 USB_IF_ENQUEUE(&f->free_q, m);
773 /* reset have fragment flag */
774 f->flag_have_fragment = 0;
777 /*------------------------------------------------------------------------*
779 *------------------------------------------------------------------------*/
781 usb_fifo_close(struct usb_fifo *f, int fflags)
785 /* check if we are not opened */
786 if (f->curr_cpd == NULL) {
787 /* nothing to do - already closed */
790 mtx_lock(f->priv_mtx);
792 /* clear current cdev private data pointer */
793 mtx_lock(&usb_ref_lock);
795 mtx_unlock(&usb_ref_lock);
797 /* check if we are watched by kevent */
798 KNOTE_LOCKED(&f->selinfo.si_note, 0);
800 /* check if we are selected */
801 if (f->flag_isselect) {
802 selwakeup(&f->selinfo);
803 f->flag_isselect = 0;
805 /* check if a thread wants SIGIO */
806 if (f->async_p != NULL) {
807 PROC_LOCK(f->async_p);
808 kern_psignal(f->async_p, SIGIO);
809 PROC_UNLOCK(f->async_p);
812 /* remove FWRITE and FREAD flags */
813 fflags &= ~(FWRITE | FREAD);
815 /* flush written data, if any */
816 if ((f->fifo_index & 1) == USB_FIFO_TX) {
818 if (!f->flag_iserror) {
820 /* set flushing flag */
821 f->flag_flushing = 1;
823 /* get the last packet in */
824 if (f->flag_have_fragment) {
826 f->flag_have_fragment = 0;
827 USB_IF_DEQUEUE(&f->free_q, m);
829 USB_IF_ENQUEUE(&f->used_q, m);
833 /* start write transfer, if not already started */
834 (f->methods->f_start_write) (f);
836 /* check if flushed already */
837 while (f->flag_flushing &&
838 (!f->flag_iserror)) {
839 /* wait until all data has been written */
840 f->flag_sleeping = 1;
841 err = cv_timedwait_sig(&f->cv_io, f->priv_mtx,
842 USB_MS_TO_TICKS(USB_DEFAULT_TIMEOUT));
844 DPRINTF("signal received\n");
851 /* stop write transfer, if not already stopped */
852 (f->methods->f_stop_write) (f);
856 /* stop write transfer, if not already stopped */
857 (f->methods->f_stop_read) (f);
860 /* check if we are sleeping */
861 if (f->flag_sleeping) {
862 DPRINTFN(2, "Sleeping at close!\n");
864 mtx_unlock(f->priv_mtx);
866 /* call close method */
867 (f->methods->f_close) (f, fflags);
872 /*------------------------------------------------------------------------*
873 * usb_open - cdev callback
874 *------------------------------------------------------------------------*/
876 usb_open(struct cdev *dev, int fflags, int devtype, struct thread *td)
878 struct usb_fs_privdata* pd = (struct usb_fs_privdata*)dev->si_drv1;
879 struct usb_cdev_refdata refs;
880 struct usb_cdev_privdata *cpd;
883 DPRINTFN(2, "%s fflags=0x%08x\n", devtoname(dev), fflags);
885 KASSERT(fflags & (FREAD|FWRITE), ("invalid open flags"));
886 if (((fflags & FREAD) && !(pd->mode & FREAD)) ||
887 ((fflags & FWRITE) && !(pd->mode & FWRITE))) {
888 DPRINTFN(2, "access mode not supported\n");
892 cpd = malloc(sizeof(*cpd), M_USBDEV, M_WAITOK | M_ZERO);
894 usb_loc_fill(pd, cpd);
895 err = usb_ref_device(cpd, &refs, 1);
897 DPRINTFN(2, "cannot ref device\n");
901 cpd->fflags = fflags; /* access mode for open lifetime */
903 /* create FIFOs, if any */
904 err = usb_fifo_create(cpd, &refs);
905 /* check for error */
907 DPRINTFN(2, "cannot create fifo\n");
908 usb_unref_device(cpd, &refs);
912 if (fflags & FREAD) {
913 err = usb_fifo_open(cpd, refs.rxfifo, fflags);
915 DPRINTFN(2, "read open failed\n");
916 usb_unref_device(cpd, &refs);
921 if (fflags & FWRITE) {
922 err = usb_fifo_open(cpd, refs.txfifo, fflags);
924 DPRINTFN(2, "write open failed\n");
925 if (fflags & FREAD) {
926 usb_fifo_close(refs.rxfifo, fflags);
928 usb_unref_device(cpd, &refs);
933 usb_unref_device(cpd, &refs);
934 devfs_set_cdevpriv(cpd, usb_close);
939 /*------------------------------------------------------------------------*
940 * usb_close - cdev callback
941 *------------------------------------------------------------------------*/
945 struct usb_cdev_refdata refs;
946 struct usb_cdev_privdata *cpd = arg;
949 DPRINTFN(2, "cpd=%p\n", cpd);
951 err = usb_ref_device(cpd, &refs,
952 2 /* uref and allow detached state */);
954 DPRINTFN(2, "Cannot grab USB reference when "
955 "closing USB file handle\n");
958 if (cpd->fflags & FREAD) {
959 usb_fifo_close(refs.rxfifo, cpd->fflags);
961 if (cpd->fflags & FWRITE) {
962 usb_fifo_close(refs.txfifo, cpd->fflags);
964 usb_unref_device(cpd, &refs);
970 usb_dev_init(void *arg)
972 mtx_init(&usb_ref_lock, "USB ref mutex", NULL, MTX_DEF);
973 sx_init(&usb_sym_lock, "USB sym mutex");
974 TAILQ_INIT(&usb_sym_head);
976 /* check the UGEN methods */
977 usb_fifo_check_methods(&usb_ugen_methods);
980 SYSINIT(usb_dev_init, SI_SUB_KLD, SI_ORDER_FIRST, usb_dev_init, NULL);
983 usb_dev_init_post(void *arg)
986 * Create /dev/usb - this is needed for usbconfig(8), which
987 * needs a well-known device name to access.
989 usb_dev = make_dev(&usb_static_devsw, 0, UID_ROOT, GID_OPERATOR,
990 0644, USB_DEVICE_NAME);
991 if (usb_dev == NULL) {
992 DPRINTFN(0, "Could not create usb bus device\n");
996 SYSINIT(usb_dev_init_post, SI_SUB_KICK_SCHEDULER, SI_ORDER_FIRST, usb_dev_init_post, NULL);
999 usb_dev_uninit(void *arg)
1001 if (usb_dev != NULL) {
1002 destroy_dev(usb_dev);
1005 mtx_destroy(&usb_ref_lock);
1006 sx_destroy(&usb_sym_lock);
1009 SYSUNINIT(usb_dev_uninit, SI_SUB_KICK_SCHEDULER, SI_ORDER_ANY, usb_dev_uninit, NULL);
1012 usb_ioctl_f_sub(struct usb_fifo *f, u_long cmd, void *addr,
1019 *(int *)addr = 0; /* character device */
1023 /* handled by upper FS layer */
1028 if (f->async_p != NULL) {
1032 f->async_p = USB_TD_GET_PROC(td);
1038 /* XXX this is not the most general solution */
1040 if (f->async_p == NULL) {
1044 if (*(int *)addr != USB_PROC_GET_GID(f->async_p)) {
1052 DPRINTFN(3, "cmd 0x%lx = %d\n", cmd, error);
1056 /*------------------------------------------------------------------------*
1057 * usb_ioctl - cdev callback
1058 *------------------------------------------------------------------------*/
1060 usb_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int fflag, struct thread* td)
1062 struct usb_cdev_refdata refs;
1063 struct usb_cdev_privdata* cpd;
1068 DPRINTFN(2, "cmd=0x%lx\n", cmd);
1070 err = devfs_get_cdevpriv((void **)&cpd);
1075 * Performance optimisation: We try to check for IOCTL's that
1076 * don't need the USB reference first. Then we grab the USB
1077 * reference if we need it!
1079 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1083 fflags = cpd->fflags;
1085 f = NULL; /* set default value */
1086 err = ENOIOCTL; /* set default value */
1088 if (fflags & FWRITE) {
1090 err = usb_ioctl_f_sub(f, cmd, addr, td);
1092 if (fflags & FREAD) {
1094 err = usb_ioctl_f_sub(f, cmd, addr, td);
1096 KASSERT(f != NULL, ("fifo not found"));
1097 if (err != ENOIOCTL)
1100 err = (f->methods->f_ioctl) (f, cmd, addr, fflags);
1102 DPRINTFN(2, "f_ioctl cmd 0x%lx = %d\n", cmd, err);
1104 if (err != ENOIOCTL)
1107 if (usb_usb_ref_device(cpd, &refs)) {
1108 /* we lost the reference */
1112 err = (f->methods->f_ioctl_post) (f, cmd, addr, fflags);
1114 DPRINTFN(2, "f_ioctl_post cmd 0x%lx = %d\n", cmd, err);
1116 if (err == ENOIOCTL)
1122 /* Wait for re-enumeration, if any */
1124 while (f->udev->re_enumerate_wait != USB_RE_ENUM_DONE) {
1126 usb_unref_device(cpd, &refs);
1128 usb_pause_mtx(NULL, hz / 128);
1130 while (usb_ref_device(cpd, &refs, 1 /* need uref */)) {
1131 if (usb_ref_device(cpd, &refs, 0)) {
1132 /* device no longer exists */
1135 usb_unref_device(cpd, &refs);
1136 usb_pause_mtx(NULL, hz / 128);
1141 usb_unref_device(cpd, &refs);
1146 usb_filter_detach(struct knote *kn)
1148 struct usb_fifo *f = kn->kn_hook;
1149 knlist_remove(&f->selinfo.si_note, kn, 0);
1153 usb_filter_write(struct knote *kn, long hint)
1155 struct usb_cdev_privdata* cpd;
1163 USB_MTX_ASSERT(f->priv_mtx, MA_OWNED);
1168 } else if (f->fs_ep_max == 0) {
1169 if (f->flag_iserror) {
1170 /* we got an error */
1173 if (f->queue_data == NULL) {
1175 * start write transfer, if not
1178 (f->methods->f_start_write) (f);
1180 /* check if any packets are available */
1181 USB_IF_POLL(&f->free_q, m);
1184 if (f->flag_iscomplete) {
1194 usb_filter_read(struct knote *kn, long hint)
1196 struct usb_cdev_privdata* cpd;
1204 USB_MTX_ASSERT(f->priv_mtx, MA_OWNED);
1209 } else if (f->fs_ep_max == 0) {
1210 if (f->flag_iserror) {
1211 /* we have an error */
1214 if (f->queue_data == NULL) {
1216 * start read transfer, if not
1219 (f->methods->f_start_read) (f);
1221 /* check if any packets are available */
1222 USB_IF_POLL(&f->used_q, m);
1224 /* start reading data, if any */
1226 (f->methods->f_start_read) (f);
1229 if (f->flag_iscomplete) {
1238 static struct filterops usb_filtops_write = {
1240 .f_detach = usb_filter_detach,
1241 .f_event = usb_filter_write,
1244 static struct filterops usb_filtops_read = {
1246 .f_detach = usb_filter_detach,
1247 .f_event = usb_filter_read,
1253 usb_kqfilter(struct cdev* dev, struct knote *kn)
1255 struct usb_cdev_refdata refs;
1256 struct usb_cdev_privdata* cpd;
1263 if (devfs_get_cdevpriv((void **)&cpd) != 0 ||
1264 usb_ref_device(cpd, &refs, 0) != 0)
1267 fflags = cpd->fflags;
1269 /* Figure out who needs service */
1270 switch (kn->kn_filter) {
1272 if (fflags & FWRITE) {
1274 kn->kn_fop = &usb_filtops_write;
1279 if (fflags & FREAD) {
1281 kn->kn_fop = &usb_filtops_read;
1292 mtx_lock(f->priv_mtx);
1293 knlist_add(&f->selinfo.si_note, kn, 1);
1294 mtx_unlock(f->priv_mtx);
1297 usb_unref_device(cpd, &refs);
1303 usb_poll(struct cdev* dev, int events, struct thread* td)
1305 struct usb_cdev_refdata refs;
1306 struct usb_cdev_privdata* cpd;
1309 int fflags, revents;
1311 if (devfs_get_cdevpriv((void **)&cpd) != 0 ||
1312 usb_ref_device(cpd, &refs, 0) != 0)
1314 (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM));
1316 fflags = cpd->fflags;
1318 /* Figure out who needs service */
1320 if ((events & (POLLOUT | POLLWRNORM)) &&
1321 (fflags & FWRITE)) {
1325 mtx_lock(f->priv_mtx);
1327 if (!refs.is_usbfs) {
1328 if (f->flag_iserror) {
1329 /* we got an error */
1332 if (f->queue_data == NULL) {
1334 * start write transfer, if not
1337 (f->methods->f_start_write) (f);
1339 /* check if any packets are available */
1340 USB_IF_POLL(&f->free_q, m);
1343 if (f->flag_iscomplete) {
1351 revents |= events & (POLLOUT | POLLWRNORM);
1353 f->flag_isselect = 1;
1354 selrecord(td, &f->selinfo);
1357 mtx_unlock(f->priv_mtx);
1359 if ((events & (POLLIN | POLLRDNORM)) &&
1364 mtx_lock(f->priv_mtx);
1366 if (!refs.is_usbfs) {
1367 if (f->flag_iserror) {
1368 /* we have an error */
1371 if (f->queue_data == NULL) {
1373 * start read transfer, if not
1376 (f->methods->f_start_read) (f);
1378 /* check if any packets are available */
1379 USB_IF_POLL(&f->used_q, m);
1382 if (f->flag_iscomplete) {
1390 revents |= events & (POLLIN | POLLRDNORM);
1392 f->flag_isselect = 1;
1393 selrecord(td, &f->selinfo);
1395 if (!refs.is_usbfs) {
1396 /* start reading data */
1397 (f->methods->f_start_read) (f);
1401 mtx_unlock(f->priv_mtx);
1403 usb_unref_device(cpd, &refs);
1408 usb_read(struct cdev *dev, struct uio *uio, int ioflag)
1410 struct usb_cdev_refdata refs;
1411 struct usb_cdev_privdata* cpd;
1416 uint8_t tr_data = 0;
1418 err = devfs_get_cdevpriv((void **)&cpd);
1422 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1428 /* should not happen */
1429 usb_unref_device(cpd, &refs);
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;
1534 uint8_t tr_data = 0;
1538 err = devfs_get_cdevpriv((void **)&cpd);
1542 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1548 /* should not happen */
1549 usb_unref_device(cpd, &refs);
1553 mtx_lock(f->priv_mtx);
1555 /* check for permanent write error */
1556 if (f->flag_iserror) {
1560 /* check if USB-FS interface is active */
1561 if (refs.is_usbfs) {
1563 * The queue is used for events that should be
1564 * retrieved using the "USB_FS_COMPLETE" ioctl.
1569 if (f->queue_data == NULL) {
1570 /* start write transfer, if not already started */
1571 (f->methods->f_start_write) (f);
1573 /* we allow writing zero length data */
1575 USB_IF_DEQUEUE(&f->free_q, m);
1579 if (ioflag & IO_NDELAY) {
1581 /* return length before error */
1587 DPRINTF("sleeping\n");
1589 err = usb_fifo_wait(f);
1597 if (f->flag_have_fragment == 0) {
1599 io_len = m->cur_data_len;
1600 pdata = m->cur_data_ptr;
1601 if (io_len > uio->uio_resid)
1602 io_len = uio->uio_resid;
1603 m->cur_data_len = io_len;
1605 io_len = m->max_data_len - m->cur_data_len;
1606 pdata = m->cur_data_ptr + m->cur_data_len;
1607 if (io_len > uio->uio_resid)
1608 io_len = uio->uio_resid;
1609 m->cur_data_len += io_len;
1612 DPRINTFN(2, "transfer %d bytes to %p\n",
1615 err = usb_fifo_uiomove(f, pdata, io_len, uio);
1618 f->flag_have_fragment = 0;
1619 USB_IF_ENQUEUE(&f->free_q, m);
1623 /* check if the buffer is ready to be transmitted */
1625 if ((f->flag_write_defrag == 0) ||
1626 (m->cur_data_len == m->max_data_len)) {
1627 f->flag_have_fragment = 0;
1630 * Check for write filter:
1632 * Sometimes it is convenient to process data
1633 * at the expense of a userland process
1634 * instead of a kernel process.
1636 if (f->methods->f_filter_write) {
1637 (f->methods->f_filter_write) (f, m);
1640 /* Put USB mbuf in the used queue */
1641 USB_IF_ENQUEUE(&f->used_q, m);
1643 /* Start writing data, if not already started */
1644 (f->methods->f_start_write) (f);
1646 /* Wait for more data or close */
1647 f->flag_have_fragment = 1;
1648 USB_IF_PREPEND(&f->free_q, m);
1651 } while (uio->uio_resid > 0);
1653 mtx_unlock(f->priv_mtx);
1655 usb_unref_device(cpd, &refs);
1661 usb_static_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
1665 struct usb_read_dir *urd;
1673 err = usb_read_symlink(u.urd->urd_data,
1674 u.urd->urd_startentry, u.urd->urd_maxlen);
1676 case USB_DEV_QUIRK_GET:
1677 case USB_QUIRK_NAME_GET:
1678 case USB_DEV_QUIRK_ADD:
1679 case USB_DEV_QUIRK_REMOVE:
1680 err = usb_quirk_ioctl_p(cmd, data, fflag, td);
1682 case USB_GET_TEMPLATE:
1683 *(int *)data = usb_template;
1686 case USB_SET_TEMPLATE:
1687 err = priv_check(curthread, PRIV_DRIVER);
1690 usb_template = *(int *)data;
1700 usb_fifo_uiomove(struct usb_fifo *f, void *cp,
1701 int n, struct uio *uio)
1705 mtx_unlock(f->priv_mtx);
1708 * "uiomove()" can sleep so one needs to make a wrapper,
1709 * exiting the mutex and checking things:
1711 error = uiomove(cp, n, uio);
1713 mtx_lock(f->priv_mtx);
1719 usb_fifo_wait(struct usb_fifo *f)
1723 USB_MTX_ASSERT(f->priv_mtx, MA_OWNED);
1725 if (f->flag_iserror) {
1729 f->flag_sleeping = 1;
1731 err = cv_wait_sig(&f->cv_io, f->priv_mtx);
1733 if (f->flag_iserror) {
1741 usb_fifo_signal(struct usb_fifo *f)
1743 if (f->flag_sleeping) {
1744 f->flag_sleeping = 0;
1745 cv_broadcast(&f->cv_io);
1750 usb_fifo_wakeup(struct usb_fifo *f)
1754 KNOTE_LOCKED(&f->selinfo.si_note, 0);
1756 if (f->flag_isselect) {
1757 selwakeup(&f->selinfo);
1758 f->flag_isselect = 0;
1760 if (f->async_p != NULL) {
1761 PROC_LOCK(f->async_p);
1762 kern_psignal(f->async_p, SIGIO);
1763 PROC_UNLOCK(f->async_p);
1768 usb_fifo_dummy_open(struct usb_fifo *fifo, int fflags)
1774 usb_fifo_dummy_close(struct usb_fifo *fifo, int fflags)
1780 usb_fifo_dummy_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
1786 usb_fifo_dummy_cmd(struct usb_fifo *fifo)
1788 fifo->flag_flushing = 0; /* not flushing */
1792 usb_fifo_check_methods(struct usb_fifo_methods *pm)
1794 /* check that all callback functions are OK */
1796 if (pm->f_open == NULL)
1797 pm->f_open = &usb_fifo_dummy_open;
1799 if (pm->f_close == NULL)
1800 pm->f_close = &usb_fifo_dummy_close;
1802 if (pm->f_ioctl == NULL)
1803 pm->f_ioctl = &usb_fifo_dummy_ioctl;
1805 if (pm->f_ioctl_post == NULL)
1806 pm->f_ioctl_post = &usb_fifo_dummy_ioctl;
1808 if (pm->f_start_read == NULL)
1809 pm->f_start_read = &usb_fifo_dummy_cmd;
1811 if (pm->f_stop_read == NULL)
1812 pm->f_stop_read = &usb_fifo_dummy_cmd;
1814 if (pm->f_start_write == NULL)
1815 pm->f_start_write = &usb_fifo_dummy_cmd;
1817 if (pm->f_stop_write == NULL)
1818 pm->f_stop_write = &usb_fifo_dummy_cmd;
1821 /*------------------------------------------------------------------------*
1824 * The following function will create a duplex FIFO.
1829 *------------------------------------------------------------------------*/
1831 usb_fifo_attach(struct usb_device *udev, void *priv_sc,
1832 struct mtx *priv_mtx, struct usb_fifo_methods *pm,
1833 struct usb_fifo_sc *f_sc, uint16_t unit, int16_t subunit,
1834 uint8_t iface_index, uid_t uid, gid_t gid, int mode)
1836 struct usb_fifo *f_tx;
1837 struct usb_fifo *f_rx;
1841 f_sc->fp[USB_FIFO_TX] = NULL;
1842 f_sc->fp[USB_FIFO_RX] = NULL;
1847 /* check the methods */
1848 usb_fifo_check_methods(pm);
1850 if (priv_mtx == NULL)
1853 /* search for a free FIFO slot */
1854 for (n = 0;; n += 2) {
1856 if (n == USB_FIFO_MAX) {
1857 /* end of FIFOs reached */
1860 /* Check for TX FIFO */
1861 if (udev->fifo[n + USB_FIFO_TX] != NULL) {
1864 /* Check for RX FIFO */
1865 if (udev->fifo[n + USB_FIFO_RX] != NULL) {
1871 f_tx = usb_fifo_alloc(priv_mtx);
1872 f_rx = usb_fifo_alloc(priv_mtx);
1874 if ((f_tx == NULL) || (f_rx == NULL)) {
1875 usb_fifo_free(f_tx);
1876 usb_fifo_free(f_rx);
1879 /* initialise FIFO structures */
1881 f_tx->fifo_index = n + USB_FIFO_TX;
1882 f_tx->dev_ep_index = -1;
1883 f_tx->priv_sc0 = priv_sc;
1885 f_tx->iface_index = iface_index;
1888 f_rx->fifo_index = n + USB_FIFO_RX;
1889 f_rx->dev_ep_index = -1;
1890 f_rx->priv_sc0 = priv_sc;
1892 f_rx->iface_index = iface_index;
1895 f_sc->fp[USB_FIFO_TX] = f_tx;
1896 f_sc->fp[USB_FIFO_RX] = f_rx;
1898 mtx_lock(&usb_ref_lock);
1899 udev->fifo[f_tx->fifo_index] = f_tx;
1900 udev->fifo[f_rx->fifo_index] = f_rx;
1901 mtx_unlock(&usb_ref_lock);
1903 for (n = 0; n != 4; n++) {
1905 if (pm->basename[n] == NULL) {
1909 if (snprintf(devname, sizeof(devname),
1910 "%s%u%s", pm->basename[n],
1911 unit, pm->postfix[n] ?
1912 pm->postfix[n] : "")) {
1916 if (snprintf(devname, sizeof(devname),
1917 "%s%u.%d%s", pm->basename[n],
1918 unit, subunit, pm->postfix[n] ?
1919 pm->postfix[n] : "")) {
1925 * Distribute the symbolic links into two FIFO structures:
1928 f_rx->symlink[n / 2] =
1929 usb_alloc_symlink(devname);
1931 f_tx->symlink[n / 2] =
1932 usb_alloc_symlink(devname);
1935 /* Create the device */
1936 f_sc->dev = usb_make_dev(udev, devname, -1,
1937 f_tx->fifo_index & f_rx->fifo_index,
1938 FREAD|FWRITE, uid, gid, mode);
1941 DPRINTFN(2, "attached %p/%p\n", f_tx, f_rx);
1945 /*------------------------------------------------------------------------*
1946 * usb_fifo_alloc_buffer
1951 *------------------------------------------------------------------------*/
1953 usb_fifo_alloc_buffer(struct usb_fifo *f, usb_size_t bufsize,
1956 usb_fifo_free_buffer(f);
1958 /* allocate an endpoint */
1959 f->free_q.ifq_maxlen = nbuf;
1960 f->used_q.ifq_maxlen = nbuf;
1962 f->queue_data = usb_alloc_mbufs(
1963 M_USBDEV, &f->free_q, bufsize, nbuf);
1965 if ((f->queue_data == NULL) && bufsize && nbuf) {
1968 return (0); /* success */
1971 /*------------------------------------------------------------------------*
1972 * usb_fifo_free_buffer
1974 * This function will free the buffers associated with a FIFO. This
1975 * function can be called multiple times in a row.
1976 *------------------------------------------------------------------------*/
1978 usb_fifo_free_buffer(struct usb_fifo *f)
1980 if (f->queue_data) {
1981 /* free old buffer */
1982 free(f->queue_data, M_USBDEV);
1983 f->queue_data = NULL;
1987 memset(&f->free_q, 0, sizeof(f->free_q));
1988 memset(&f->used_q, 0, sizeof(f->used_q));
1992 usb_fifo_detach(struct usb_fifo_sc *f_sc)
1997 usb_fifo_free(f_sc->fp[USB_FIFO_TX]);
1998 usb_fifo_free(f_sc->fp[USB_FIFO_RX]);
2000 f_sc->fp[USB_FIFO_TX] = NULL;
2001 f_sc->fp[USB_FIFO_RX] = NULL;
2003 usb_destroy_dev(f_sc->dev);
2007 DPRINTFN(2, "detached %p\n", f_sc);
2011 usb_fifo_put_bytes_max(struct usb_fifo *f)
2016 USB_IF_POLL(&f->free_q, m);
2019 len = m->max_data_len;
2026 /*------------------------------------------------------------------------*
2030 * 0 - normal operation
2031 * 1 - set last packet flag to enforce framing
2032 *------------------------------------------------------------------------*/
2034 usb_fifo_put_data(struct usb_fifo *f, struct usb_page_cache *pc,
2035 usb_frlength_t offset, usb_frlength_t len, uint8_t what)
2038 usb_frlength_t io_len;
2040 while (len || (what == 1)) {
2042 USB_IF_DEQUEUE(&f->free_q, m);
2047 io_len = MIN(len, m->cur_data_len);
2049 usbd_copy_out(pc, offset, m->cur_data_ptr, io_len);
2051 m->cur_data_len = io_len;
2055 if ((len == 0) && (what == 1)) {
2058 USB_IF_ENQUEUE(&f->used_q, m);
2062 if ((len == 0) || (what == 1)) {
2072 usb_fifo_put_data_linear(struct usb_fifo *f, void *ptr,
2073 usb_size_t len, uint8_t what)
2078 while (len || (what == 1)) {
2080 USB_IF_DEQUEUE(&f->free_q, m);
2085 io_len = MIN(len, m->cur_data_len);
2087 memcpy(m->cur_data_ptr, ptr, io_len);
2089 m->cur_data_len = io_len;
2090 ptr = USB_ADD_BYTES(ptr, io_len);
2093 if ((len == 0) && (what == 1)) {
2096 USB_IF_ENQUEUE(&f->used_q, m);
2100 if ((len == 0) || (what == 1)) {
2110 usb_fifo_put_data_buffer(struct usb_fifo *f, void *ptr, usb_size_t len)
2114 USB_IF_DEQUEUE(&f->free_q, m);
2117 m->cur_data_len = len;
2118 m->cur_data_ptr = ptr;
2119 USB_IF_ENQUEUE(&f->used_q, m);
2127 usb_fifo_put_data_error(struct usb_fifo *f)
2129 f->flag_iserror = 1;
2133 /*------------------------------------------------------------------------*
2137 * 0 - normal operation
2138 * 1 - only get one "usb_mbuf"
2142 * 1 - data in buffer
2143 *------------------------------------------------------------------------*/
2145 usb_fifo_get_data(struct usb_fifo *f, struct usb_page_cache *pc,
2146 usb_frlength_t offset, usb_frlength_t len, usb_frlength_t *actlen,
2150 usb_frlength_t io_len;
2151 uint8_t tr_data = 0;
2157 USB_IF_DEQUEUE(&f->used_q, m);
2163 io_len = MIN(len, m->cur_data_len);
2165 usbd_copy_in(pc, offset, m->cur_data_ptr, io_len);
2169 actlen[0] += io_len;
2170 m->cur_data_ptr += io_len;
2171 m->cur_data_len -= io_len;
2173 if ((m->cur_data_len == 0) || (what == 1)) {
2174 USB_IF_ENQUEUE(&f->free_q, m);
2182 USB_IF_PREPEND(&f->used_q, m);
2187 /* wait for data to be written out */
2190 if (f->flag_flushing) {
2191 /* check if we should send a short packet */
2192 if (f->flag_short != 0) {
2197 /* flushing complete */
2198 f->flag_flushing = 0;
2211 usb_fifo_get_data_linear(struct usb_fifo *f, void *ptr,
2212 usb_size_t len, usb_size_t *actlen, uint8_t what)
2216 uint8_t tr_data = 0;
2222 USB_IF_DEQUEUE(&f->used_q, m);
2228 io_len = MIN(len, m->cur_data_len);
2230 memcpy(ptr, m->cur_data_ptr, io_len);
2233 ptr = USB_ADD_BYTES(ptr, io_len);
2234 actlen[0] += io_len;
2235 m->cur_data_ptr += io_len;
2236 m->cur_data_len -= io_len;
2238 if ((m->cur_data_len == 0) || (what == 1)) {
2239 USB_IF_ENQUEUE(&f->free_q, m);
2247 USB_IF_PREPEND(&f->used_q, m);
2252 /* wait for data to be written out */
2255 if (f->flag_flushing) {
2256 /* check if we should send a short packet */
2257 if (f->flag_short != 0) {
2262 /* flushing complete */
2263 f->flag_flushing = 0;
2276 usb_fifo_get_data_buffer(struct usb_fifo *f, void **pptr, usb_size_t *plen)
2280 USB_IF_POLL(&f->used_q, m);
2283 *plen = m->cur_data_len;
2284 *pptr = m->cur_data_ptr;
2292 usb_fifo_get_data_error(struct usb_fifo *f)
2294 f->flag_iserror = 1;
2298 /*------------------------------------------------------------------------*
2303 * Else: Pointer to symlink entry
2304 *------------------------------------------------------------------------*/
2305 struct usb_symlink *
2306 usb_alloc_symlink(const char *target)
2308 struct usb_symlink *ps;
2310 ps = malloc(sizeof(*ps), M_USBDEV, M_WAITOK);
2314 /* XXX no longer needed */
2315 strlcpy(ps->src_path, target, sizeof(ps->src_path));
2316 ps->src_len = strlen(ps->src_path);
2317 strlcpy(ps->dst_path, target, sizeof(ps->dst_path));
2318 ps->dst_len = strlen(ps->dst_path);
2320 sx_xlock(&usb_sym_lock);
2321 TAILQ_INSERT_TAIL(&usb_sym_head, ps, sym_entry);
2322 sx_unlock(&usb_sym_lock);
2326 /*------------------------------------------------------------------------*
2328 *------------------------------------------------------------------------*/
2330 usb_free_symlink(struct usb_symlink *ps)
2335 sx_xlock(&usb_sym_lock);
2336 TAILQ_REMOVE(&usb_sym_head, ps, sym_entry);
2337 sx_unlock(&usb_sym_lock);
2342 /*------------------------------------------------------------------------*
2348 *------------------------------------------------------------------------*/
2350 usb_read_symlink(uint8_t *user_ptr, uint32_t startentry, uint32_t user_len)
2352 struct usb_symlink *ps;
2358 sx_xlock(&usb_sym_lock);
2360 TAILQ_FOREACH(ps, &usb_sym_head, sym_entry) {
2363 * Compute total length of source and destination symlink
2364 * strings pluss one length byte and two NUL bytes:
2366 temp = ps->src_len + ps->dst_len + 3;
2370 * Skip entry because this length cannot fit
2375 if (startentry != 0) {
2376 /* decrement read offset */
2380 if (temp > user_len) {
2381 /* out of buffer space */
2386 /* copy out total length */
2388 error = copyout(&len,
2389 USB_ADD_BYTES(user_ptr, delta), 1);
2395 /* copy out source string */
2397 error = copyout(ps->src_path,
2398 USB_ADD_BYTES(user_ptr, delta), ps->src_len);
2403 delta += ps->src_len;
2404 error = copyout(&len,
2405 USB_ADD_BYTES(user_ptr, delta), 1);
2411 /* copy out destination string */
2413 error = copyout(ps->dst_path,
2414 USB_ADD_BYTES(user_ptr, delta), ps->dst_len);
2419 delta += ps->dst_len;
2420 error = copyout(&len,
2421 USB_ADD_BYTES(user_ptr, delta), 1);
2430 /* a zero length entry indicates the end */
2432 if ((user_len != 0) && (error == 0)) {
2436 error = copyout(&len,
2437 USB_ADD_BYTES(user_ptr, delta), 1);
2439 sx_unlock(&usb_sym_lock);
2444 usb_fifo_set_close_zlp(struct usb_fifo *f, uint8_t onoff)
2449 /* send a Zero Length Packet, ZLP, before close */
2450 f->flag_short = onoff;
2454 usb_fifo_set_write_defrag(struct usb_fifo *f, uint8_t onoff)
2459 /* defrag written data */
2460 f->flag_write_defrag = onoff;
2461 /* reset defrag state */
2462 f->flag_have_fragment = 0;
2466 usb_fifo_softc(struct usb_fifo *f)
2468 return (f->priv_sc0);
2470 #endif /* USB_HAVE_UGEN */