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);
893 ep = cpd->ep_addr = pd->ep_addr;
895 usb_loc_fill(pd, cpd);
896 err = usb_ref_device(cpd, &refs, 1);
898 DPRINTFN(2, "cannot ref device\n");
902 cpd->fflags = fflags; /* access mode for open lifetime */
904 /* create FIFOs, if any */
905 err = usb_fifo_create(cpd, &refs);
906 /* check for error */
908 DPRINTFN(2, "cannot create fifo\n");
909 usb_unref_device(cpd, &refs);
913 if (fflags & FREAD) {
914 err = usb_fifo_open(cpd, refs.rxfifo, fflags);
916 DPRINTFN(2, "read open failed\n");
917 usb_unref_device(cpd, &refs);
922 if (fflags & FWRITE) {
923 err = usb_fifo_open(cpd, refs.txfifo, fflags);
925 DPRINTFN(2, "write open failed\n");
926 if (fflags & FREAD) {
927 usb_fifo_close(refs.rxfifo, fflags);
929 usb_unref_device(cpd, &refs);
934 usb_unref_device(cpd, &refs);
935 devfs_set_cdevpriv(cpd, usb_close);
940 /*------------------------------------------------------------------------*
941 * usb_close - cdev callback
942 *------------------------------------------------------------------------*/
946 struct usb_cdev_refdata refs;
947 struct usb_cdev_privdata *cpd = arg;
950 DPRINTFN(2, "cpd=%p\n", cpd);
952 err = usb_ref_device(cpd, &refs,
953 2 /* uref and allow detached state */);
955 DPRINTFN(2, "Cannot grab USB reference when "
956 "closing USB file handle\n");
959 if (cpd->fflags & FREAD) {
960 usb_fifo_close(refs.rxfifo, cpd->fflags);
962 if (cpd->fflags & FWRITE) {
963 usb_fifo_close(refs.txfifo, cpd->fflags);
965 usb_unref_device(cpd, &refs);
971 usb_dev_init(void *arg)
973 mtx_init(&usb_ref_lock, "USB ref mutex", NULL, MTX_DEF);
974 sx_init(&usb_sym_lock, "USB sym mutex");
975 TAILQ_INIT(&usb_sym_head);
977 /* check the UGEN methods */
978 usb_fifo_check_methods(&usb_ugen_methods);
981 SYSINIT(usb_dev_init, SI_SUB_KLD, SI_ORDER_FIRST, usb_dev_init, NULL);
984 usb_dev_init_post(void *arg)
987 * Create /dev/usb - this is needed for usbconfig(8), which
988 * needs a well-known device name to access.
990 usb_dev = make_dev(&usb_static_devsw, 0, UID_ROOT, GID_OPERATOR,
991 0644, USB_DEVICE_NAME);
992 if (usb_dev == NULL) {
993 DPRINTFN(0, "Could not create usb bus device\n");
997 SYSINIT(usb_dev_init_post, SI_SUB_KICK_SCHEDULER, SI_ORDER_FIRST, usb_dev_init_post, NULL);
1000 usb_dev_uninit(void *arg)
1002 if (usb_dev != NULL) {
1003 destroy_dev(usb_dev);
1006 mtx_destroy(&usb_ref_lock);
1007 sx_destroy(&usb_sym_lock);
1010 SYSUNINIT(usb_dev_uninit, SI_SUB_KICK_SCHEDULER, SI_ORDER_ANY, usb_dev_uninit, NULL);
1013 usb_ioctl_f_sub(struct usb_fifo *f, u_long cmd, void *addr,
1020 *(int *)addr = 0; /* character device */
1024 /* handled by upper FS layer */
1029 if (f->async_p != NULL) {
1033 f->async_p = USB_TD_GET_PROC(td);
1039 /* XXX this is not the most general solution */
1041 if (f->async_p == NULL) {
1045 if (*(int *)addr != USB_PROC_GET_GID(f->async_p)) {
1053 DPRINTFN(3, "cmd 0x%lx = %d\n", cmd, error);
1057 /*------------------------------------------------------------------------*
1058 * usb_ioctl - cdev callback
1059 *------------------------------------------------------------------------*/
1061 usb_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int fflag, struct thread* td)
1063 struct usb_cdev_refdata refs;
1064 struct usb_cdev_privdata* cpd;
1069 DPRINTFN(2, "cmd=0x%lx\n", cmd);
1071 err = devfs_get_cdevpriv((void **)&cpd);
1076 * Performance optimisation: We try to check for IOCTL's that
1077 * don't need the USB reference first. Then we grab the USB
1078 * reference if we need it!
1080 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1084 fflags = cpd->fflags;
1086 f = NULL; /* set default value */
1087 err = ENOIOCTL; /* set default value */
1089 if (fflags & FWRITE) {
1091 err = usb_ioctl_f_sub(f, cmd, addr, td);
1093 if (fflags & FREAD) {
1095 err = usb_ioctl_f_sub(f, cmd, addr, td);
1097 KASSERT(f != NULL, ("fifo not found"));
1098 if (err != ENOIOCTL)
1101 err = (f->methods->f_ioctl) (f, cmd, addr, fflags);
1103 DPRINTFN(2, "f_ioctl cmd 0x%lx = %d\n", cmd, err);
1105 if (err != ENOIOCTL)
1108 if (usb_usb_ref_device(cpd, &refs)) {
1109 /* we lost the reference */
1113 err = (f->methods->f_ioctl_post) (f, cmd, addr, fflags);
1115 DPRINTFN(2, "f_ioctl_post cmd 0x%lx = %d\n", cmd, err);
1117 if (err == ENOIOCTL)
1123 /* Wait for re-enumeration, if any */
1125 while (f->udev->re_enumerate_wait != USB_RE_ENUM_DONE) {
1127 usb_unref_device(cpd, &refs);
1129 usb_pause_mtx(NULL, hz / 128);
1131 while (usb_ref_device(cpd, &refs, 1 /* need uref */)) {
1132 if (usb_ref_device(cpd, &refs, 0)) {
1133 /* device no longer exists */
1136 usb_unref_device(cpd, &refs);
1137 usb_pause_mtx(NULL, hz / 128);
1142 usb_unref_device(cpd, &refs);
1147 usb_filter_detach(struct knote *kn)
1149 struct usb_fifo *f = kn->kn_hook;
1150 knlist_remove(&f->selinfo.si_note, kn, 0);
1154 usb_filter_write(struct knote *kn, long hint)
1156 struct usb_cdev_privdata* cpd;
1164 USB_MTX_ASSERT(f->priv_mtx, MA_OWNED);
1169 } else if (f->fs_ep_max == 0) {
1170 if (f->flag_iserror) {
1171 /* we got an error */
1174 if (f->queue_data == NULL) {
1176 * start write transfer, if not
1179 (f->methods->f_start_write) (f);
1181 /* check if any packets are available */
1182 USB_IF_POLL(&f->free_q, m);
1185 if (f->flag_iscomplete) {
1195 usb_filter_read(struct knote *kn, long hint)
1197 struct usb_cdev_privdata* cpd;
1205 USB_MTX_ASSERT(f->priv_mtx, MA_OWNED);
1210 } else if (f->fs_ep_max == 0) {
1211 if (f->flag_iserror) {
1212 /* we have an error */
1215 if (f->queue_data == NULL) {
1217 * start read transfer, if not
1220 (f->methods->f_start_read) (f);
1222 /* check if any packets are available */
1223 USB_IF_POLL(&f->used_q, m);
1225 /* start reading data, if any */
1227 (f->methods->f_start_read) (f);
1230 if (f->flag_iscomplete) {
1239 static struct filterops usb_filtops_write = {
1241 .f_detach = usb_filter_detach,
1242 .f_event = usb_filter_write,
1245 static struct filterops usb_filtops_read = {
1247 .f_detach = usb_filter_detach,
1248 .f_event = usb_filter_read,
1254 usb_kqfilter(struct cdev* dev, struct knote *kn)
1256 struct usb_cdev_refdata refs;
1257 struct usb_cdev_privdata* cpd;
1264 if (devfs_get_cdevpriv((void **)&cpd) != 0 ||
1265 usb_ref_device(cpd, &refs, 0) != 0)
1268 fflags = cpd->fflags;
1270 /* Figure out who needs service */
1271 switch (kn->kn_filter) {
1273 if (fflags & FWRITE) {
1275 kn->kn_fop = &usb_filtops_write;
1280 if (fflags & FREAD) {
1282 kn->kn_fop = &usb_filtops_read;
1293 mtx_lock(f->priv_mtx);
1294 knlist_add(&f->selinfo.si_note, kn, 1);
1295 mtx_unlock(f->priv_mtx);
1298 usb_unref_device(cpd, &refs);
1304 usb_poll(struct cdev* dev, int events, struct thread* td)
1306 struct usb_cdev_refdata refs;
1307 struct usb_cdev_privdata* cpd;
1310 int fflags, revents;
1312 if (devfs_get_cdevpriv((void **)&cpd) != 0 ||
1313 usb_ref_device(cpd, &refs, 0) != 0)
1315 (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM));
1317 fflags = cpd->fflags;
1319 /* Figure out who needs service */
1321 if ((events & (POLLOUT | POLLWRNORM)) &&
1322 (fflags & FWRITE)) {
1326 mtx_lock(f->priv_mtx);
1328 if (!refs.is_usbfs) {
1329 if (f->flag_iserror) {
1330 /* we got an error */
1333 if (f->queue_data == NULL) {
1335 * start write transfer, if not
1338 (f->methods->f_start_write) (f);
1340 /* check if any packets are available */
1341 USB_IF_POLL(&f->free_q, m);
1344 if (f->flag_iscomplete) {
1352 revents |= events & (POLLOUT | POLLWRNORM);
1354 f->flag_isselect = 1;
1355 selrecord(td, &f->selinfo);
1358 mtx_unlock(f->priv_mtx);
1360 if ((events & (POLLIN | POLLRDNORM)) &&
1365 mtx_lock(f->priv_mtx);
1367 if (!refs.is_usbfs) {
1368 if (f->flag_iserror) {
1369 /* we have an error */
1372 if (f->queue_data == NULL) {
1374 * start read transfer, if not
1377 (f->methods->f_start_read) (f);
1379 /* check if any packets are available */
1380 USB_IF_POLL(&f->used_q, m);
1383 if (f->flag_iscomplete) {
1391 revents |= events & (POLLIN | POLLRDNORM);
1393 f->flag_isselect = 1;
1394 selrecord(td, &f->selinfo);
1396 if (!refs.is_usbfs) {
1397 /* start reading data */
1398 (f->methods->f_start_read) (f);
1402 mtx_unlock(f->priv_mtx);
1404 usb_unref_device(cpd, &refs);
1409 usb_read(struct cdev *dev, struct uio *uio, int ioflag)
1411 struct usb_cdev_refdata refs;
1412 struct usb_cdev_privdata* cpd;
1419 uint8_t tr_data = 0;
1421 err = devfs_get_cdevpriv((void **)&cpd);
1425 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1429 fflags = cpd->fflags;
1433 /* should not happen */
1434 usb_unref_device(cpd, &refs);
1438 resid = uio->uio_resid;
1440 mtx_lock(f->priv_mtx);
1442 /* check for permanent read error */
1443 if (f->flag_iserror) {
1447 /* check if USB-FS interface is active */
1448 if (refs.is_usbfs) {
1450 * The queue is used for events that should be
1451 * retrieved using the "USB_FS_COMPLETE" ioctl.
1456 while (uio->uio_resid > 0) {
1458 USB_IF_DEQUEUE(&f->used_q, m);
1462 /* start read transfer, if not already started */
1464 (f->methods->f_start_read) (f);
1466 if (ioflag & IO_NDELAY) {
1468 /* return length before error */
1474 DPRINTF("sleeping\n");
1476 err = usb_fifo_wait(f);
1482 if (f->methods->f_filter_read) {
1484 * Sometimes it is convenient to process data at the
1485 * expense of a userland process instead of a kernel
1488 (f->methods->f_filter_read) (f, m);
1492 io_len = MIN(m->cur_data_len, uio->uio_resid);
1494 DPRINTFN(2, "transfer %d bytes from %p\n",
1495 io_len, m->cur_data_ptr);
1497 err = usb_fifo_uiomove(f,
1498 m->cur_data_ptr, io_len, uio);
1500 m->cur_data_len -= io_len;
1501 m->cur_data_ptr += io_len;
1503 if (m->cur_data_len == 0) {
1505 uint8_t last_packet;
1507 last_packet = m->last_packet;
1509 USB_IF_ENQUEUE(&f->free_q, m);
1516 USB_IF_PREPEND(&f->used_q, m);
1524 mtx_unlock(f->priv_mtx);
1526 usb_unref_device(cpd, &refs);
1532 usb_write(struct cdev *dev, struct uio *uio, int ioflag)
1534 struct usb_cdev_refdata refs;
1535 struct usb_cdev_privdata* cpd;
1543 uint8_t tr_data = 0;
1547 err = devfs_get_cdevpriv((void **)&cpd);
1551 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1555 fflags = cpd->fflags;
1559 /* should not happen */
1560 usb_unref_device(cpd, &refs);
1563 resid = uio->uio_resid;
1565 mtx_lock(f->priv_mtx);
1567 /* check for permanent write error */
1568 if (f->flag_iserror) {
1572 /* check if USB-FS interface is active */
1573 if (refs.is_usbfs) {
1575 * The queue is used for events that should be
1576 * retrieved using the "USB_FS_COMPLETE" ioctl.
1581 if (f->queue_data == NULL) {
1582 /* start write transfer, if not already started */
1583 (f->methods->f_start_write) (f);
1585 /* we allow writing zero length data */
1587 USB_IF_DEQUEUE(&f->free_q, m);
1591 if (ioflag & IO_NDELAY) {
1593 /* return length before error */
1599 DPRINTF("sleeping\n");
1601 err = usb_fifo_wait(f);
1609 if (f->flag_have_fragment == 0) {
1611 io_len = m->cur_data_len;
1612 pdata = m->cur_data_ptr;
1613 if (io_len > uio->uio_resid)
1614 io_len = uio->uio_resid;
1615 m->cur_data_len = io_len;
1617 io_len = m->max_data_len - m->cur_data_len;
1618 pdata = m->cur_data_ptr + m->cur_data_len;
1619 if (io_len > uio->uio_resid)
1620 io_len = uio->uio_resid;
1621 m->cur_data_len += io_len;
1624 DPRINTFN(2, "transfer %d bytes to %p\n",
1627 err = usb_fifo_uiomove(f, pdata, io_len, uio);
1630 f->flag_have_fragment = 0;
1631 USB_IF_ENQUEUE(&f->free_q, m);
1635 /* check if the buffer is ready to be transmitted */
1637 if ((f->flag_write_defrag == 0) ||
1638 (m->cur_data_len == m->max_data_len)) {
1639 f->flag_have_fragment = 0;
1642 * Check for write filter:
1644 * Sometimes it is convenient to process data
1645 * at the expense of a userland process
1646 * instead of a kernel process.
1648 if (f->methods->f_filter_write) {
1649 (f->methods->f_filter_write) (f, m);
1652 /* Put USB mbuf in the used queue */
1653 USB_IF_ENQUEUE(&f->used_q, m);
1655 /* Start writing data, if not already started */
1656 (f->methods->f_start_write) (f);
1658 /* Wait for more data or close */
1659 f->flag_have_fragment = 1;
1660 USB_IF_PREPEND(&f->free_q, m);
1663 } while (uio->uio_resid > 0);
1665 mtx_unlock(f->priv_mtx);
1667 usb_unref_device(cpd, &refs);
1673 usb_static_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
1677 struct usb_read_dir *urd;
1685 err = usb_read_symlink(u.urd->urd_data,
1686 u.urd->urd_startentry, u.urd->urd_maxlen);
1688 case USB_DEV_QUIRK_GET:
1689 case USB_QUIRK_NAME_GET:
1690 case USB_DEV_QUIRK_ADD:
1691 case USB_DEV_QUIRK_REMOVE:
1692 err = usb_quirk_ioctl_p(cmd, data, fflag, td);
1694 case USB_GET_TEMPLATE:
1695 *(int *)data = usb_template;
1698 case USB_SET_TEMPLATE:
1699 err = priv_check(curthread, PRIV_DRIVER);
1702 usb_template = *(int *)data;
1712 usb_fifo_uiomove(struct usb_fifo *f, void *cp,
1713 int n, struct uio *uio)
1717 mtx_unlock(f->priv_mtx);
1720 * "uiomove()" can sleep so one needs to make a wrapper,
1721 * exiting the mutex and checking things:
1723 error = uiomove(cp, n, uio);
1725 mtx_lock(f->priv_mtx);
1731 usb_fifo_wait(struct usb_fifo *f)
1735 USB_MTX_ASSERT(f->priv_mtx, MA_OWNED);
1737 if (f->flag_iserror) {
1741 f->flag_sleeping = 1;
1743 err = cv_wait_sig(&f->cv_io, f->priv_mtx);
1745 if (f->flag_iserror) {
1753 usb_fifo_signal(struct usb_fifo *f)
1755 if (f->flag_sleeping) {
1756 f->flag_sleeping = 0;
1757 cv_broadcast(&f->cv_io);
1762 usb_fifo_wakeup(struct usb_fifo *f)
1766 KNOTE_LOCKED(&f->selinfo.si_note, 0);
1768 if (f->flag_isselect) {
1769 selwakeup(&f->selinfo);
1770 f->flag_isselect = 0;
1772 if (f->async_p != NULL) {
1773 PROC_LOCK(f->async_p);
1774 kern_psignal(f->async_p, SIGIO);
1775 PROC_UNLOCK(f->async_p);
1780 usb_fifo_dummy_open(struct usb_fifo *fifo, int fflags)
1786 usb_fifo_dummy_close(struct usb_fifo *fifo, int fflags)
1792 usb_fifo_dummy_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
1798 usb_fifo_dummy_cmd(struct usb_fifo *fifo)
1800 fifo->flag_flushing = 0; /* not flushing */
1804 usb_fifo_check_methods(struct usb_fifo_methods *pm)
1806 /* check that all callback functions are OK */
1808 if (pm->f_open == NULL)
1809 pm->f_open = &usb_fifo_dummy_open;
1811 if (pm->f_close == NULL)
1812 pm->f_close = &usb_fifo_dummy_close;
1814 if (pm->f_ioctl == NULL)
1815 pm->f_ioctl = &usb_fifo_dummy_ioctl;
1817 if (pm->f_ioctl_post == NULL)
1818 pm->f_ioctl_post = &usb_fifo_dummy_ioctl;
1820 if (pm->f_start_read == NULL)
1821 pm->f_start_read = &usb_fifo_dummy_cmd;
1823 if (pm->f_stop_read == NULL)
1824 pm->f_stop_read = &usb_fifo_dummy_cmd;
1826 if (pm->f_start_write == NULL)
1827 pm->f_start_write = &usb_fifo_dummy_cmd;
1829 if (pm->f_stop_write == NULL)
1830 pm->f_stop_write = &usb_fifo_dummy_cmd;
1833 /*------------------------------------------------------------------------*
1836 * The following function will create a duplex FIFO.
1841 *------------------------------------------------------------------------*/
1843 usb_fifo_attach(struct usb_device *udev, void *priv_sc,
1844 struct mtx *priv_mtx, struct usb_fifo_methods *pm,
1845 struct usb_fifo_sc *f_sc, uint16_t unit, int16_t subunit,
1846 uint8_t iface_index, uid_t uid, gid_t gid, int mode)
1848 struct usb_fifo *f_tx;
1849 struct usb_fifo *f_rx;
1853 f_sc->fp[USB_FIFO_TX] = NULL;
1854 f_sc->fp[USB_FIFO_RX] = NULL;
1859 /* check the methods */
1860 usb_fifo_check_methods(pm);
1862 if (priv_mtx == NULL)
1865 /* search for a free FIFO slot */
1866 for (n = 0;; n += 2) {
1868 if (n == USB_FIFO_MAX) {
1869 /* end of FIFOs reached */
1872 /* Check for TX FIFO */
1873 if (udev->fifo[n + USB_FIFO_TX] != NULL) {
1876 /* Check for RX FIFO */
1877 if (udev->fifo[n + USB_FIFO_RX] != NULL) {
1883 f_tx = usb_fifo_alloc(priv_mtx);
1884 f_rx = usb_fifo_alloc(priv_mtx);
1886 if ((f_tx == NULL) || (f_rx == NULL)) {
1887 usb_fifo_free(f_tx);
1888 usb_fifo_free(f_rx);
1891 /* initialise FIFO structures */
1893 f_tx->fifo_index = n + USB_FIFO_TX;
1894 f_tx->dev_ep_index = -1;
1895 f_tx->priv_sc0 = priv_sc;
1897 f_tx->iface_index = iface_index;
1900 f_rx->fifo_index = n + USB_FIFO_RX;
1901 f_rx->dev_ep_index = -1;
1902 f_rx->priv_sc0 = priv_sc;
1904 f_rx->iface_index = iface_index;
1907 f_sc->fp[USB_FIFO_TX] = f_tx;
1908 f_sc->fp[USB_FIFO_RX] = f_rx;
1910 mtx_lock(&usb_ref_lock);
1911 udev->fifo[f_tx->fifo_index] = f_tx;
1912 udev->fifo[f_rx->fifo_index] = f_rx;
1913 mtx_unlock(&usb_ref_lock);
1915 for (n = 0; n != 4; n++) {
1917 if (pm->basename[n] == NULL) {
1921 if (snprintf(devname, sizeof(devname),
1922 "%s%u%s", pm->basename[n],
1923 unit, pm->postfix[n] ?
1924 pm->postfix[n] : "")) {
1928 if (snprintf(devname, sizeof(devname),
1929 "%s%u.%d%s", pm->basename[n],
1930 unit, subunit, pm->postfix[n] ?
1931 pm->postfix[n] : "")) {
1937 * Distribute the symbolic links into two FIFO structures:
1940 f_rx->symlink[n / 2] =
1941 usb_alloc_symlink(devname);
1943 f_tx->symlink[n / 2] =
1944 usb_alloc_symlink(devname);
1947 /* Create the device */
1948 f_sc->dev = usb_make_dev(udev, devname, -1,
1949 f_tx->fifo_index & f_rx->fifo_index,
1950 FREAD|FWRITE, uid, gid, mode);
1953 DPRINTFN(2, "attached %p/%p\n", f_tx, f_rx);
1957 /*------------------------------------------------------------------------*
1958 * usb_fifo_alloc_buffer
1963 *------------------------------------------------------------------------*/
1965 usb_fifo_alloc_buffer(struct usb_fifo *f, usb_size_t bufsize,
1968 usb_fifo_free_buffer(f);
1970 /* allocate an endpoint */
1971 f->free_q.ifq_maxlen = nbuf;
1972 f->used_q.ifq_maxlen = nbuf;
1974 f->queue_data = usb_alloc_mbufs(
1975 M_USBDEV, &f->free_q, bufsize, nbuf);
1977 if ((f->queue_data == NULL) && bufsize && nbuf) {
1980 return (0); /* success */
1983 /*------------------------------------------------------------------------*
1984 * usb_fifo_free_buffer
1986 * This function will free the buffers associated with a FIFO. This
1987 * function can be called multiple times in a row.
1988 *------------------------------------------------------------------------*/
1990 usb_fifo_free_buffer(struct usb_fifo *f)
1992 if (f->queue_data) {
1993 /* free old buffer */
1994 free(f->queue_data, M_USBDEV);
1995 f->queue_data = NULL;
1999 memset(&f->free_q, 0, sizeof(f->free_q));
2000 memset(&f->used_q, 0, sizeof(f->used_q));
2004 usb_fifo_detach(struct usb_fifo_sc *f_sc)
2009 usb_fifo_free(f_sc->fp[USB_FIFO_TX]);
2010 usb_fifo_free(f_sc->fp[USB_FIFO_RX]);
2012 f_sc->fp[USB_FIFO_TX] = NULL;
2013 f_sc->fp[USB_FIFO_RX] = NULL;
2015 usb_destroy_dev(f_sc->dev);
2019 DPRINTFN(2, "detached %p\n", f_sc);
2023 usb_fifo_put_bytes_max(struct usb_fifo *f)
2028 USB_IF_POLL(&f->free_q, m);
2031 len = m->max_data_len;
2038 /*------------------------------------------------------------------------*
2042 * 0 - normal operation
2043 * 1 - set last packet flag to enforce framing
2044 *------------------------------------------------------------------------*/
2046 usb_fifo_put_data(struct usb_fifo *f, struct usb_page_cache *pc,
2047 usb_frlength_t offset, usb_frlength_t len, uint8_t what)
2050 usb_frlength_t io_len;
2052 while (len || (what == 1)) {
2054 USB_IF_DEQUEUE(&f->free_q, m);
2059 io_len = MIN(len, m->cur_data_len);
2061 usbd_copy_out(pc, offset, m->cur_data_ptr, io_len);
2063 m->cur_data_len = io_len;
2067 if ((len == 0) && (what == 1)) {
2070 USB_IF_ENQUEUE(&f->used_q, m);
2074 if ((len == 0) || (what == 1)) {
2084 usb_fifo_put_data_linear(struct usb_fifo *f, void *ptr,
2085 usb_size_t len, uint8_t what)
2090 while (len || (what == 1)) {
2092 USB_IF_DEQUEUE(&f->free_q, m);
2097 io_len = MIN(len, m->cur_data_len);
2099 memcpy(m->cur_data_ptr, ptr, io_len);
2101 m->cur_data_len = io_len;
2102 ptr = USB_ADD_BYTES(ptr, io_len);
2105 if ((len == 0) && (what == 1)) {
2108 USB_IF_ENQUEUE(&f->used_q, m);
2112 if ((len == 0) || (what == 1)) {
2122 usb_fifo_put_data_buffer(struct usb_fifo *f, void *ptr, usb_size_t len)
2126 USB_IF_DEQUEUE(&f->free_q, m);
2129 m->cur_data_len = len;
2130 m->cur_data_ptr = ptr;
2131 USB_IF_ENQUEUE(&f->used_q, m);
2139 usb_fifo_put_data_error(struct usb_fifo *f)
2141 f->flag_iserror = 1;
2145 /*------------------------------------------------------------------------*
2149 * 0 - normal operation
2150 * 1 - only get one "usb_mbuf"
2154 * 1 - data in buffer
2155 *------------------------------------------------------------------------*/
2157 usb_fifo_get_data(struct usb_fifo *f, struct usb_page_cache *pc,
2158 usb_frlength_t offset, usb_frlength_t len, usb_frlength_t *actlen,
2162 usb_frlength_t io_len;
2163 uint8_t tr_data = 0;
2169 USB_IF_DEQUEUE(&f->used_q, m);
2175 io_len = MIN(len, m->cur_data_len);
2177 usbd_copy_in(pc, offset, m->cur_data_ptr, io_len);
2181 actlen[0] += io_len;
2182 m->cur_data_ptr += io_len;
2183 m->cur_data_len -= io_len;
2185 if ((m->cur_data_len == 0) || (what == 1)) {
2186 USB_IF_ENQUEUE(&f->free_q, m);
2194 USB_IF_PREPEND(&f->used_q, m);
2199 /* wait for data to be written out */
2202 if (f->flag_flushing) {
2203 /* check if we should send a short packet */
2204 if (f->flag_short != 0) {
2209 /* flushing complete */
2210 f->flag_flushing = 0;
2223 usb_fifo_get_data_linear(struct usb_fifo *f, void *ptr,
2224 usb_size_t len, usb_size_t *actlen, uint8_t what)
2228 uint8_t tr_data = 0;
2234 USB_IF_DEQUEUE(&f->used_q, m);
2240 io_len = MIN(len, m->cur_data_len);
2242 memcpy(ptr, m->cur_data_ptr, io_len);
2245 ptr = USB_ADD_BYTES(ptr, io_len);
2246 actlen[0] += io_len;
2247 m->cur_data_ptr += io_len;
2248 m->cur_data_len -= io_len;
2250 if ((m->cur_data_len == 0) || (what == 1)) {
2251 USB_IF_ENQUEUE(&f->free_q, m);
2259 USB_IF_PREPEND(&f->used_q, m);
2264 /* wait for data to be written out */
2267 if (f->flag_flushing) {
2268 /* check if we should send a short packet */
2269 if (f->flag_short != 0) {
2274 /* flushing complete */
2275 f->flag_flushing = 0;
2288 usb_fifo_get_data_buffer(struct usb_fifo *f, void **pptr, usb_size_t *plen)
2292 USB_IF_POLL(&f->used_q, m);
2295 *plen = m->cur_data_len;
2296 *pptr = m->cur_data_ptr;
2304 usb_fifo_get_data_error(struct usb_fifo *f)
2306 f->flag_iserror = 1;
2310 /*------------------------------------------------------------------------*
2315 * Else: Pointer to symlink entry
2316 *------------------------------------------------------------------------*/
2317 struct usb_symlink *
2318 usb_alloc_symlink(const char *target)
2320 struct usb_symlink *ps;
2322 ps = malloc(sizeof(*ps), M_USBDEV, M_WAITOK);
2326 /* XXX no longer needed */
2327 strlcpy(ps->src_path, target, sizeof(ps->src_path));
2328 ps->src_len = strlen(ps->src_path);
2329 strlcpy(ps->dst_path, target, sizeof(ps->dst_path));
2330 ps->dst_len = strlen(ps->dst_path);
2332 sx_xlock(&usb_sym_lock);
2333 TAILQ_INSERT_TAIL(&usb_sym_head, ps, sym_entry);
2334 sx_unlock(&usb_sym_lock);
2338 /*------------------------------------------------------------------------*
2340 *------------------------------------------------------------------------*/
2342 usb_free_symlink(struct usb_symlink *ps)
2347 sx_xlock(&usb_sym_lock);
2348 TAILQ_REMOVE(&usb_sym_head, ps, sym_entry);
2349 sx_unlock(&usb_sym_lock);
2354 /*------------------------------------------------------------------------*
2360 *------------------------------------------------------------------------*/
2362 usb_read_symlink(uint8_t *user_ptr, uint32_t startentry, uint32_t user_len)
2364 struct usb_symlink *ps;
2370 sx_xlock(&usb_sym_lock);
2372 TAILQ_FOREACH(ps, &usb_sym_head, sym_entry) {
2375 * Compute total length of source and destination symlink
2376 * strings pluss one length byte and two NUL bytes:
2378 temp = ps->src_len + ps->dst_len + 3;
2382 * Skip entry because this length cannot fit
2387 if (startentry != 0) {
2388 /* decrement read offset */
2392 if (temp > user_len) {
2393 /* out of buffer space */
2398 /* copy out total length */
2400 error = copyout(&len,
2401 USB_ADD_BYTES(user_ptr, delta), 1);
2407 /* copy out source string */
2409 error = copyout(ps->src_path,
2410 USB_ADD_BYTES(user_ptr, delta), ps->src_len);
2415 delta += ps->src_len;
2416 error = copyout(&len,
2417 USB_ADD_BYTES(user_ptr, delta), 1);
2423 /* copy out destination string */
2425 error = copyout(ps->dst_path,
2426 USB_ADD_BYTES(user_ptr, delta), ps->dst_len);
2431 delta += ps->dst_len;
2432 error = copyout(&len,
2433 USB_ADD_BYTES(user_ptr, delta), 1);
2442 /* a zero length entry indicates the end */
2444 if ((user_len != 0) && (error == 0)) {
2448 error = copyout(&len,
2449 USB_ADD_BYTES(user_ptr, delta), 1);
2451 sx_unlock(&usb_sym_lock);
2456 usb_fifo_set_close_zlp(struct usb_fifo *f, uint8_t onoff)
2461 /* send a Zero Length Packet, ZLP, before close */
2462 f->flag_short = onoff;
2466 usb_fifo_set_write_defrag(struct usb_fifo *f, uint8_t onoff)
2471 /* defrag written data */
2472 f->flag_write_defrag = onoff;
2473 /* reset defrag state */
2474 f->flag_have_fragment = 0;
2478 usb_fifo_softc(struct usb_fifo *f)
2480 return (f->priv_sc0);
2482 #endif /* USB_HAVE_UGEN */