3 * Copyright (c) 2006-2008 Hans Petter Selasky. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * usb_dev.c - An abstraction layer for creating devices under /dev/...
30 #include <sys/stdint.h>
31 #include <sys/stddef.h>
32 #include <sys/param.h>
33 #include <sys/queue.h>
34 #include <sys/types.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/module.h>
40 #include <sys/mutex.h>
41 #include <sys/condvar.h>
42 #include <sys/sysctl.h>
44 #include <sys/unistd.h>
45 #include <sys/callout.h>
46 #include <sys/malloc.h>
48 #include <sys/vnode.h>
50 #include <sys/fcntl.h>
52 #include <dev/usb/usb.h>
53 #include <dev/usb/usb_ioctl.h>
54 #include <dev/usb/usbdi.h>
55 #include <dev/usb/usbdi_util.h>
57 #define USB_DEBUG_VAR usb_fifo_debug
59 #include <dev/usb/usb_core.h>
60 #include <dev/usb/usb_dev.h>
61 #include <dev/usb/usb_mbuf.h>
62 #include <dev/usb/usb_process.h>
63 #include <dev/usb/usb_device.h>
64 #include <dev/usb/usb_debug.h>
65 #include <dev/usb/usb_busdma.h>
66 #include <dev/usb/usb_generic.h>
67 #include <dev/usb/usb_dynamic.h>
68 #include <dev/usb/usb_util.h>
70 #include <dev/usb/usb_controller.h>
71 #include <dev/usb/usb_bus.h>
73 #include <sys/filio.h>
74 #include <sys/ttycom.h>
75 #include <sys/syscallsubr.h>
77 #include <machine/stdarg.h>
82 static int usb_fifo_debug = 0;
84 SYSCTL_NODE(_hw_usb, OID_AUTO, dev, CTLFLAG_RW, 0, "USB device");
85 SYSCTL_INT(_hw_usb_dev, OID_AUTO, debug, CTLFLAG_RW | CTLFLAG_TUN,
86 &usb_fifo_debug, 0, "Debug Level");
87 TUNABLE_INT("hw.usb.dev.debug", &usb_fifo_debug);
90 #if ((__FreeBSD_version >= 700001) || (__FreeBSD_version == 0) || \
91 ((__FreeBSD_version >= 600034) && (__FreeBSD_version < 700000)))
92 #define USB_UCRED struct ucred *ucred,
99 static int usb_fifo_open(struct usb_cdev_privdata *,
100 struct usb_fifo *, int);
101 static void usb_fifo_close(struct usb_fifo *, int);
102 static void usb_dev_init(void *);
103 static void usb_dev_init_post(void *);
104 static void usb_dev_uninit(void *);
105 static int usb_fifo_uiomove(struct usb_fifo *, void *, int,
107 static void usb_fifo_check_methods(struct usb_fifo_methods *);
108 static struct usb_fifo *usb_fifo_alloc(void);
109 static struct usb_endpoint *usb_dev_get_ep(struct usb_device *, uint8_t,
111 static void usb_loc_fill(struct usb_fs_privdata *,
112 struct usb_cdev_privdata *);
113 static void usb_close(void *);
114 static usb_error_t usb_ref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *, int);
115 static usb_error_t usb_usb_ref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *);
116 static void usb_unref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *);
118 static d_open_t usb_open;
119 static d_ioctl_t usb_ioctl;
120 static d_read_t usb_read;
121 static d_write_t usb_write;
122 static d_poll_t usb_poll;
124 static d_ioctl_t usb_static_ioctl;
126 static usb_fifo_open_t usb_fifo_dummy_open;
127 static usb_fifo_close_t usb_fifo_dummy_close;
128 static usb_fifo_ioctl_t usb_fifo_dummy_ioctl;
129 static usb_fifo_cmd_t usb_fifo_dummy_cmd;
131 /* character device structure used for devices (/dev/ugenX.Y and /dev/uXXX) */
132 struct cdevsw usb_devsw = {
133 .d_version = D_VERSION,
135 .d_ioctl = usb_ioctl,
137 .d_flags = D_TRACKCLOSE,
139 .d_write = usb_write,
143 static struct cdev* usb_dev = NULL;
145 /* character device structure used for /dev/usb */
146 static struct cdevsw usb_static_devsw = {
147 .d_version = D_VERSION,
148 .d_ioctl = usb_static_ioctl,
152 static TAILQ_HEAD(, usb_symlink) usb_sym_head;
153 static struct sx usb_sym_lock;
155 struct mtx usb_ref_lock;
157 /*------------------------------------------------------------------------*
160 * This is used to fill out a usb_cdev_privdata structure based on the
161 * device's address as contained in usb_fs_privdata.
162 *------------------------------------------------------------------------*/
164 usb_loc_fill(struct usb_fs_privdata* pd, struct usb_cdev_privdata *cpd)
166 cpd->bus_index = pd->bus_index;
167 cpd->dev_index = pd->dev_index;
168 cpd->ep_addr = pd->ep_addr;
169 cpd->fifo_index = pd->fifo_index;
172 /*------------------------------------------------------------------------*
175 * This function is used to atomically refer an USB device by its
176 * device location. If this function returns success the USB device
177 * will not dissappear until the USB device is unreferenced.
180 * 0: Success, refcount incremented on the given USB device.
182 *------------------------------------------------------------------------*/
184 usb_ref_device(struct usb_cdev_privdata *cpd,
185 struct usb_cdev_refdata *crd, int need_uref)
187 struct usb_fifo **ppf;
190 DPRINTFN(2, "cpd=%p need uref=%d\n", cpd, need_uref);
193 memset(crd, 0, sizeof(*crd));
195 mtx_lock(&usb_ref_lock);
196 cpd->bus = devclass_get_softc(usb_devclass_ptr, cpd->bus_index);
197 if (cpd->bus == NULL) {
198 DPRINTFN(2, "no bus at %u\n", cpd->bus_index);
201 cpd->udev = cpd->bus->devices[cpd->dev_index];
202 if (cpd->udev == NULL) {
203 DPRINTFN(2, "no device at %u\n", cpd->dev_index);
206 if (cpd->udev->state == USB_STATE_DETACHED &&
208 DPRINTFN(2, "device is detached\n");
212 DPRINTFN(2, "ref udev - needed\n");
214 if (cpd->udev->refcount == USB_DEV_REF_MAX) {
215 DPRINTFN(2, "no dev ref\n");
218 cpd->udev->refcount++;
220 mtx_unlock(&usb_ref_lock);
223 * We need to grab the enumeration SX-lock before
224 * grabbing the FIFO refs to avoid deadlock at detach!
226 crd->do_unlock = usbd_enum_lock_sig(cpd->udev);
228 mtx_lock(&usb_ref_lock);
231 * Set "is_uref" after grabbing the default SX lock
235 /* check for signal */
236 if (crd->do_unlock > 1) {
242 /* check if we are doing an open */
243 if (cpd->fflags == 0) {
244 /* use zero defaults */
246 /* check for write */
247 if (cpd->fflags & FWRITE) {
248 ppf = cpd->udev->fifo;
249 f = ppf[cpd->fifo_index + USB_FIFO_TX];
251 crd->is_write = 1; /* ref */
252 if (f == NULL || f->refcount == USB_FIFO_REF_MAX)
254 if (f->curr_cpd != cpd)
256 /* check if USB-FS is active */
257 if (f->fs_ep_max != 0) {
263 if (cpd->fflags & FREAD) {
264 ppf = cpd->udev->fifo;
265 f = ppf[cpd->fifo_index + USB_FIFO_RX];
267 crd->is_read = 1; /* ref */
268 if (f == NULL || f->refcount == USB_FIFO_REF_MAX)
270 if (f->curr_cpd != cpd)
272 /* check if USB-FS is active */
273 if (f->fs_ep_max != 0) {
279 /* when everything is OK we increment the refcounts */
281 DPRINTFN(2, "ref write\n");
282 crd->txfifo->refcount++;
285 DPRINTFN(2, "ref read\n");
286 crd->rxfifo->refcount++;
288 mtx_unlock(&usb_ref_lock);
294 usbd_enum_unlock(cpd->udev);
297 if (--(cpd->udev->refcount) == 0)
298 cv_broadcast(&cpd->udev->ref_cv);
300 mtx_unlock(&usb_ref_lock);
301 DPRINTFN(2, "fail\n");
304 memset(crd, 0, sizeof(*crd));
306 return (USB_ERR_INVAL);
309 /*------------------------------------------------------------------------*
312 * This function is used to upgrade an USB reference to include the
313 * USB device reference on a USB location.
316 * 0: Success, refcount incremented on the given USB device.
318 *------------------------------------------------------------------------*/
320 usb_usb_ref_device(struct usb_cdev_privdata *cpd,
321 struct usb_cdev_refdata *crd)
324 * Check if we already got an USB reference on this location:
327 return (0); /* success */
330 * To avoid deadlock at detach we need to drop the FIFO ref
331 * and re-acquire a new ref!
333 usb_unref_device(cpd, crd);
335 return (usb_ref_device(cpd, crd, 1 /* need uref */));
338 /*------------------------------------------------------------------------*
341 * This function will release the reference count by one unit for the
343 *------------------------------------------------------------------------*/
345 usb_unref_device(struct usb_cdev_privdata *cpd,
346 struct usb_cdev_refdata *crd)
349 DPRINTFN(2, "cpd=%p is_uref=%d\n", cpd, crd->is_uref);
352 usbd_enum_unlock(cpd->udev);
354 mtx_lock(&usb_ref_lock);
356 if (--(crd->rxfifo->refcount) == 0) {
357 cv_signal(&crd->rxfifo->cv_drain);
362 if (--(crd->txfifo->refcount) == 0) {
363 cv_signal(&crd->txfifo->cv_drain);
369 if (--(cpd->udev->refcount) == 0)
370 cv_broadcast(&cpd->udev->ref_cv);
372 mtx_unlock(&usb_ref_lock);
375 static struct usb_fifo *
380 f = malloc(sizeof(*f), M_USBDEV, M_WAITOK | M_ZERO);
382 cv_init(&f->cv_io, "FIFO-IO");
383 cv_init(&f->cv_drain, "FIFO-DRAIN");
389 /*------------------------------------------------------------------------*
391 *------------------------------------------------------------------------*/
393 usb_fifo_create(struct usb_cdev_privdata *cpd,
394 struct usb_cdev_refdata *crd)
396 struct usb_device *udev = cpd->udev;
398 struct usb_endpoint *ep;
404 int e = cpd->ep_addr;
406 is_tx = (cpd->fflags & FWRITE) ? 1 : 0;
407 is_rx = (cpd->fflags & FREAD) ? 1 : 0;
411 /* Preallocated FIFO */
413 DPRINTFN(5, "Preallocated FIFO\n");
415 f = udev->fifo[cpd->fifo_index + USB_FIFO_TX];
421 f = udev->fifo[cpd->fifo_index + USB_FIFO_RX];
429 KASSERT(e >= 0 && e <= 15, ("endpoint %d out of range", e));
431 /* search for a free FIFO slot */
432 DPRINTFN(5, "Endpoint device, searching for 0x%02x\n", e);
433 for (n = 0;; n += 2) {
435 if (n == USB_FIFO_MAX) {
440 /* end of FIFOs reached */
441 DPRINTFN(5, "out of FIFOs\n");
445 /* Check for TX FIFO */
447 f = udev->fifo[n + USB_FIFO_TX];
449 if (f->dev_ep_index != e) {
450 /* wrong endpoint index */
453 if (f->curr_cpd != NULL) {
458 } else if (no_null) {
462 /* Check for RX FIFO */
464 f = udev->fifo[n + USB_FIFO_RX];
466 if (f->dev_ep_index != e) {
467 /* wrong endpoint index */
470 if (f->curr_cpd != NULL) {
475 } else if (no_null) {
483 if (e >= (USB_EP_MAX / 2)) {
484 /* we don't create any endpoints in this range */
485 DPRINTFN(5, "ep out of range\n");
486 return (is_busy ? EBUSY : EINVAL);
490 if ((e != 0) && is_busy) {
492 * Only the default control endpoint is allowed to be
493 * opened multiple times!
495 DPRINTFN(5, "busy\n");
501 (udev->fifo[n + USB_FIFO_TX] == NULL)) {
502 ep = usb_dev_get_ep(udev, e, USB_FIFO_TX);
503 DPRINTFN(5, "dev_get_endpoint(%d, 0x%x)\n", e, USB_FIFO_TX);
505 DPRINTFN(5, "dev_get_endpoint returned NULL\n");
508 f = usb_fifo_alloc();
510 DPRINTFN(5, "could not alloc tx fifo\n");
513 /* update some fields */
514 f->fifo_index = n + USB_FIFO_TX;
516 f->priv_mtx = &udev->device_mtx;
518 f->methods = &usb_ugen_methods;
519 f->iface_index = ep->iface_index;
521 mtx_lock(&usb_ref_lock);
522 udev->fifo[n + USB_FIFO_TX] = f;
523 mtx_unlock(&usb_ref_lock);
527 (udev->fifo[n + USB_FIFO_RX] == NULL)) {
529 ep = usb_dev_get_ep(udev, e, USB_FIFO_RX);
530 DPRINTFN(5, "dev_get_endpoint(%d, 0x%x)\n", e, USB_FIFO_RX);
532 DPRINTFN(5, "dev_get_endpoint returned NULL\n");
535 f = usb_fifo_alloc();
537 DPRINTFN(5, "could not alloc rx fifo\n");
540 /* update some fields */
541 f->fifo_index = n + USB_FIFO_RX;
543 f->priv_mtx = &udev->device_mtx;
545 f->methods = &usb_ugen_methods;
546 f->iface_index = ep->iface_index;
548 mtx_lock(&usb_ref_lock);
549 udev->fifo[n + USB_FIFO_RX] = f;
550 mtx_unlock(&usb_ref_lock);
553 crd->txfifo = udev->fifo[n + USB_FIFO_TX];
556 crd->rxfifo = udev->fifo[n + USB_FIFO_RX];
558 /* fill out fifo index */
559 DPRINTFN(5, "fifo index = %d\n", n);
568 usb_fifo_free(struct usb_fifo *f)
576 /* destroy symlink devices, if any */
577 for (n = 0; n != 2; n++) {
579 usb_free_symlink(f->symlink[n]);
580 f->symlink[n] = NULL;
583 mtx_lock(&usb_ref_lock);
585 /* delink ourselves to stop calls from userland */
586 if ((f->fifo_index < USB_FIFO_MAX) &&
588 (f->udev->fifo[f->fifo_index] == f)) {
589 f->udev->fifo[f->fifo_index] = NULL;
591 DPRINTFN(0, "USB FIFO %p has not been linked\n", f);
594 /* decrease refcount */
596 /* need to wait until all callers have exited */
597 while (f->refcount != 0) {
598 mtx_unlock(&usb_ref_lock); /* avoid LOR */
599 mtx_lock(f->priv_mtx);
600 /* prevent write flush, if any */
602 /* get I/O thread out of any sleep state */
603 if (f->flag_sleeping) {
604 f->flag_sleeping = 0;
605 cv_broadcast(&f->cv_io);
607 mtx_unlock(f->priv_mtx);
608 mtx_lock(&usb_ref_lock);
611 * Check if the "f->refcount" variable reached zero
612 * during the unlocked time before entering wait:
614 if (f->refcount == 0)
618 cv_wait(&f->cv_drain, &usb_ref_lock);
620 mtx_unlock(&usb_ref_lock);
622 /* take care of closing the device here, if any */
623 usb_fifo_close(f, 0);
625 cv_destroy(&f->cv_io);
626 cv_destroy(&f->cv_drain);
631 static struct usb_endpoint *
632 usb_dev_get_ep(struct usb_device *udev, uint8_t ep_index, uint8_t dir)
634 struct usb_endpoint *ep;
640 if (dir == USB_FIFO_RX) {
641 if (udev->flags.usb_mode == USB_MODE_HOST) {
647 if (udev->flags.usb_mode == USB_MODE_HOST) {
653 ep = usbd_get_ep_by_addr(udev, ep_index | ep_dir);
657 /* if the endpoint does not exist then return */
660 if (ep->edesc == NULL) {
661 /* invalid endpoint */
664 return (ep); /* success */
667 /*------------------------------------------------------------------------*
673 *------------------------------------------------------------------------*/
675 usb_fifo_open(struct usb_cdev_privdata *cpd,
676 struct usb_fifo *f, int fflags)
682 DPRINTFN(2, "no FIFO\n");
685 /* remove FWRITE and FREAD flags */
686 fflags &= ~(FWRITE | FREAD);
688 /* set correct file flags */
689 if ((f->fifo_index & 1) == USB_FIFO_TX) {
695 /* check if we are already opened */
696 /* we don't need any locks when checking this variable */
697 if (f->curr_cpd != NULL) {
702 /* reset short flag before open */
705 /* call open method */
706 err = (f->methods->f_open) (f, fflags);
710 mtx_lock(f->priv_mtx);
712 /* reset sleep flag */
713 f->flag_sleeping = 0;
715 /* reset error flag */
718 /* reset complete flag */
719 f->flag_iscomplete = 0;
721 /* reset select flag */
722 f->flag_isselect = 0;
724 /* reset flushing flag */
725 f->flag_flushing = 0;
727 /* reset ASYNC proc flag */
730 mtx_lock(&usb_ref_lock);
731 /* flag the fifo as opened to prevent others */
733 mtx_unlock(&usb_ref_lock);
738 mtx_unlock(f->priv_mtx);
743 /*------------------------------------------------------------------------*
745 *------------------------------------------------------------------------*/
747 usb_fifo_reset(struct usb_fifo *f)
755 USB_IF_DEQUEUE(&f->used_q, m);
757 USB_IF_ENQUEUE(&f->free_q, m);
762 /* reset have fragment flag */
763 f->flag_have_fragment = 0;
766 /*------------------------------------------------------------------------*
768 *------------------------------------------------------------------------*/
770 usb_fifo_close(struct usb_fifo *f, int fflags)
774 /* check if we are not opened */
775 if (f->curr_cpd == NULL) {
776 /* nothing to do - already closed */
779 mtx_lock(f->priv_mtx);
781 /* clear current cdev private data pointer */
784 /* check if we are selected */
785 if (f->flag_isselect) {
786 selwakeup(&f->selinfo);
787 f->flag_isselect = 0;
789 /* check if a thread wants SIGIO */
790 if (f->async_p != NULL) {
791 PROC_LOCK(f->async_p);
792 psignal(f->async_p, SIGIO);
793 PROC_UNLOCK(f->async_p);
796 /* remove FWRITE and FREAD flags */
797 fflags &= ~(FWRITE | FREAD);
799 /* flush written data, if any */
800 if ((f->fifo_index & 1) == USB_FIFO_TX) {
802 if (!f->flag_iserror) {
804 /* set flushing flag */
805 f->flag_flushing = 1;
807 /* get the last packet in */
808 if (f->flag_have_fragment) {
810 f->flag_have_fragment = 0;
811 USB_IF_DEQUEUE(&f->free_q, m);
813 USB_IF_ENQUEUE(&f->used_q, m);
817 /* start write transfer, if not already started */
818 (f->methods->f_start_write) (f);
820 /* check if flushed already */
821 while (f->flag_flushing &&
822 (!f->flag_iserror)) {
823 /* wait until all data has been written */
824 f->flag_sleeping = 1;
825 err = cv_timedwait_sig(&f->cv_io, f->priv_mtx,
826 USB_MS_TO_TICKS(USB_DEFAULT_TIMEOUT));
828 DPRINTF("signal received\n");
835 /* stop write transfer, if not already stopped */
836 (f->methods->f_stop_write) (f);
840 /* stop write transfer, if not already stopped */
841 (f->methods->f_stop_read) (f);
844 /* check if we are sleeping */
845 if (f->flag_sleeping) {
846 DPRINTFN(2, "Sleeping at close!\n");
848 mtx_unlock(f->priv_mtx);
850 /* call close method */
851 (f->methods->f_close) (f, fflags);
856 /*------------------------------------------------------------------------*
857 * usb_open - cdev callback
858 *------------------------------------------------------------------------*/
860 usb_open(struct cdev *dev, int fflags, int devtype, struct thread *td)
862 struct usb_fs_privdata* pd = (struct usb_fs_privdata*)dev->si_drv1;
863 struct usb_cdev_refdata refs;
864 struct usb_cdev_privdata *cpd;
867 DPRINTFN(2, "%s fflags=0x%08x\n", dev->si_name, fflags);
869 KASSERT(fflags & (FREAD|FWRITE), ("invalid open flags"));
870 if (((fflags & FREAD) && !(pd->mode & FREAD)) ||
871 ((fflags & FWRITE) && !(pd->mode & FWRITE))) {
872 DPRINTFN(2, "access mode not supported\n");
876 cpd = malloc(sizeof(*cpd), M_USBDEV, M_WAITOK | M_ZERO);
877 ep = cpd->ep_addr = pd->ep_addr;
879 usb_loc_fill(pd, cpd);
880 err = usb_ref_device(cpd, &refs, 1);
882 DPRINTFN(2, "cannot ref device\n");
886 cpd->fflags = fflags; /* access mode for open lifetime */
888 /* create FIFOs, if any */
889 err = usb_fifo_create(cpd, &refs);
890 /* check for error */
892 DPRINTFN(2, "cannot create fifo\n");
893 usb_unref_device(cpd, &refs);
897 if (fflags & FREAD) {
898 err = usb_fifo_open(cpd, refs.rxfifo, fflags);
900 DPRINTFN(2, "read open failed\n");
901 usb_unref_device(cpd, &refs);
906 if (fflags & FWRITE) {
907 err = usb_fifo_open(cpd, refs.txfifo, fflags);
909 DPRINTFN(2, "write open failed\n");
910 if (fflags & FREAD) {
911 usb_fifo_close(refs.rxfifo, fflags);
913 usb_unref_device(cpd, &refs);
918 usb_unref_device(cpd, &refs);
919 devfs_set_cdevpriv(cpd, usb_close);
924 /*------------------------------------------------------------------------*
925 * usb_close - cdev callback
926 *------------------------------------------------------------------------*/
930 struct usb_cdev_refdata refs;
931 struct usb_cdev_privdata *cpd = arg;
934 DPRINTFN(2, "cpd=%p\n", cpd);
936 err = usb_ref_device(cpd, &refs,
937 2 /* uref and allow detached state */);
939 DPRINTFN(2, "Cannot grab USB reference when "
940 "closing USB file handle\n");
943 if (cpd->fflags & FREAD) {
944 usb_fifo_close(refs.rxfifo, cpd->fflags);
946 if (cpd->fflags & FWRITE) {
947 usb_fifo_close(refs.txfifo, cpd->fflags);
949 usb_unref_device(cpd, &refs);
955 usb_dev_init(void *arg)
957 mtx_init(&usb_ref_lock, "USB ref mutex", NULL, MTX_DEF);
958 sx_init(&usb_sym_lock, "USB sym mutex");
959 TAILQ_INIT(&usb_sym_head);
961 /* check the UGEN methods */
962 usb_fifo_check_methods(&usb_ugen_methods);
965 SYSINIT(usb_dev_init, SI_SUB_KLD, SI_ORDER_FIRST, usb_dev_init, NULL);
968 usb_dev_init_post(void *arg)
971 * Create /dev/usb - this is needed for usbconfig(8), which
972 * needs a well-known device name to access.
974 usb_dev = make_dev(&usb_static_devsw, 0, UID_ROOT, GID_OPERATOR,
975 0644, USB_DEVICE_NAME);
976 if (usb_dev == NULL) {
977 DPRINTFN(0, "Could not create usb bus device\n");
981 SYSINIT(usb_dev_init_post, SI_SUB_KICK_SCHEDULER, SI_ORDER_FIRST, usb_dev_init_post, NULL);
984 usb_dev_uninit(void *arg)
986 if (usb_dev != NULL) {
987 destroy_dev(usb_dev);
990 mtx_destroy(&usb_ref_lock);
991 sx_destroy(&usb_sym_lock);
994 SYSUNINIT(usb_dev_uninit, SI_SUB_KICK_SCHEDULER, SI_ORDER_ANY, usb_dev_uninit, NULL);
997 usb_ioctl_f_sub(struct usb_fifo *f, u_long cmd, void *addr,
1004 *(int *)addr = 0; /* character device */
1008 /* handled by upper FS layer */
1013 if (f->async_p != NULL) {
1017 f->async_p = USB_TD_GET_PROC(td);
1023 /* XXX this is not the most general solution */
1025 if (f->async_p == NULL) {
1029 if (*(int *)addr != USB_PROC_GET_GID(f->async_p)) {
1037 DPRINTFN(3, "cmd 0x%lx = %d\n", cmd, error);
1041 /*------------------------------------------------------------------------*
1042 * usb_ioctl - cdev callback
1043 *------------------------------------------------------------------------*/
1045 usb_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int fflag, struct thread* td)
1047 struct usb_cdev_refdata refs;
1048 struct usb_cdev_privdata* cpd;
1053 DPRINTFN(2, "cmd=0x%lx\n", cmd);
1055 err = devfs_get_cdevpriv((void **)&cpd);
1060 * Performance optimisation: We try to check for IOCTL's that
1061 * don't need the USB reference first. Then we grab the USB
1062 * reference if we need it!
1064 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1068 fflags = cpd->fflags;
1070 f = NULL; /* set default value */
1071 err = ENOIOCTL; /* set default value */
1073 if (fflags & FWRITE) {
1075 err = usb_ioctl_f_sub(f, cmd, addr, td);
1077 if (fflags & FREAD) {
1079 err = usb_ioctl_f_sub(f, cmd, addr, td);
1081 KASSERT(f != NULL, ("fifo not found"));
1082 if (err != ENOIOCTL)
1085 err = (f->methods->f_ioctl) (f, cmd, addr, fflags);
1087 DPRINTFN(2, "f_ioctl cmd 0x%lx = %d\n", cmd, err);
1089 if (err != ENOIOCTL)
1092 if (usb_usb_ref_device(cpd, &refs)) {
1093 /* we lost the reference */
1097 err = (f->methods->f_ioctl_post) (f, cmd, addr, fflags);
1099 DPRINTFN(2, "f_ioctl_post cmd 0x%lx = %d\n", cmd, err);
1101 if (err == ENOIOCTL)
1107 /* Wait for re-enumeration, if any */
1109 while (f->udev->re_enumerate_wait != USB_RE_ENUM_DONE) {
1111 usb_unref_device(cpd, &refs);
1113 usb_pause_mtx(NULL, hz / 128);
1115 while (usb_ref_device(cpd, &refs, 1 /* need uref */)) {
1116 if (usb_ref_device(cpd, &refs, 0)) {
1117 /* device no longer exists */
1120 usb_unref_device(cpd, &refs);
1121 usb_pause_mtx(NULL, hz / 128);
1126 usb_unref_device(cpd, &refs);
1132 usb_poll(struct cdev* dev, int events, struct thread* td)
1134 struct usb_cdev_refdata refs;
1135 struct usb_cdev_privdata* cpd;
1138 int fflags, revents;
1140 if (devfs_get_cdevpriv((void **)&cpd) != 0 ||
1141 usb_ref_device(cpd, &refs, 0) != 0)
1143 (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM));
1145 fflags = cpd->fflags;
1147 /* Figure out who needs service */
1149 if ((events & (POLLOUT | POLLWRNORM)) &&
1150 (fflags & FWRITE)) {
1154 mtx_lock(f->priv_mtx);
1156 if (!refs.is_usbfs) {
1157 if (f->flag_iserror) {
1158 /* we got an error */
1161 if (f->queue_data == NULL) {
1163 * start write transfer, if not
1166 (f->methods->f_start_write) (f);
1168 /* check if any packets are available */
1169 USB_IF_POLL(&f->free_q, m);
1172 if (f->flag_iscomplete) {
1180 revents |= events & (POLLOUT | POLLWRNORM);
1182 f->flag_isselect = 1;
1183 selrecord(td, &f->selinfo);
1186 mtx_unlock(f->priv_mtx);
1188 if ((events & (POLLIN | POLLRDNORM)) &&
1193 mtx_lock(f->priv_mtx);
1195 if (!refs.is_usbfs) {
1196 if (f->flag_iserror) {
1197 /* we have and error */
1200 if (f->queue_data == NULL) {
1202 * start read transfer, if not
1205 (f->methods->f_start_read) (f);
1207 /* check if any packets are available */
1208 USB_IF_POLL(&f->used_q, m);
1211 if (f->flag_iscomplete) {
1219 revents |= events & (POLLIN | POLLRDNORM);
1221 f->flag_isselect = 1;
1222 selrecord(td, &f->selinfo);
1224 if (!refs.is_usbfs) {
1225 /* start reading data */
1226 (f->methods->f_start_read) (f);
1230 mtx_unlock(f->priv_mtx);
1232 usb_unref_device(cpd, &refs);
1237 usb_read(struct cdev *dev, struct uio *uio, int ioflag)
1239 struct usb_cdev_refdata refs;
1240 struct usb_cdev_privdata* cpd;
1247 uint8_t tr_data = 0;
1249 err = devfs_get_cdevpriv((void **)&cpd);
1253 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1257 fflags = cpd->fflags;
1261 /* should not happen */
1262 usb_unref_device(cpd, &refs);
1266 resid = uio->uio_resid;
1268 mtx_lock(f->priv_mtx);
1270 /* check for permanent read error */
1271 if (f->flag_iserror) {
1275 /* check if USB-FS interface is active */
1276 if (refs.is_usbfs) {
1278 * The queue is used for events that should be
1279 * retrieved using the "USB_FS_COMPLETE" ioctl.
1284 while (uio->uio_resid > 0) {
1286 USB_IF_DEQUEUE(&f->used_q, m);
1290 /* start read transfer, if not already started */
1292 (f->methods->f_start_read) (f);
1294 if (ioflag & IO_NDELAY) {
1296 /* return length before error */
1302 DPRINTF("sleeping\n");
1304 err = usb_fifo_wait(f);
1310 if (f->methods->f_filter_read) {
1312 * Sometimes it is convenient to process data at the
1313 * expense of a userland process instead of a kernel
1316 (f->methods->f_filter_read) (f, m);
1320 io_len = MIN(m->cur_data_len, uio->uio_resid);
1322 DPRINTFN(2, "transfer %d bytes from %p\n",
1323 io_len, m->cur_data_ptr);
1325 err = usb_fifo_uiomove(f,
1326 m->cur_data_ptr, io_len, uio);
1328 m->cur_data_len -= io_len;
1329 m->cur_data_ptr += io_len;
1331 if (m->cur_data_len == 0) {
1333 uint8_t last_packet;
1335 last_packet = m->last_packet;
1337 USB_IF_ENQUEUE(&f->free_q, m);
1344 USB_IF_PREPEND(&f->used_q, m);
1352 mtx_unlock(f->priv_mtx);
1354 usb_unref_device(cpd, &refs);
1360 usb_write(struct cdev *dev, struct uio *uio, int ioflag)
1362 struct usb_cdev_refdata refs;
1363 struct usb_cdev_privdata* cpd;
1371 uint8_t tr_data = 0;
1375 err = devfs_get_cdevpriv((void **)&cpd);
1379 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1383 fflags = cpd->fflags;
1387 /* should not happen */
1388 usb_unref_device(cpd, &refs);
1391 resid = uio->uio_resid;
1393 mtx_lock(f->priv_mtx);
1395 /* check for permanent write error */
1396 if (f->flag_iserror) {
1400 /* check if USB-FS interface is active */
1401 if (refs.is_usbfs) {
1403 * The queue is used for events that should be
1404 * retrieved using the "USB_FS_COMPLETE" ioctl.
1409 if (f->queue_data == NULL) {
1410 /* start write transfer, if not already started */
1411 (f->methods->f_start_write) (f);
1413 /* we allow writing zero length data */
1415 USB_IF_DEQUEUE(&f->free_q, m);
1419 if (ioflag & IO_NDELAY) {
1421 /* return length before error */
1427 DPRINTF("sleeping\n");
1429 err = usb_fifo_wait(f);
1437 if (f->flag_have_fragment == 0) {
1439 io_len = m->cur_data_len;
1440 pdata = m->cur_data_ptr;
1441 if (io_len > uio->uio_resid)
1442 io_len = uio->uio_resid;
1443 m->cur_data_len = io_len;
1445 io_len = m->max_data_len - m->cur_data_len;
1446 pdata = m->cur_data_ptr + m->cur_data_len;
1447 if (io_len > uio->uio_resid)
1448 io_len = uio->uio_resid;
1449 m->cur_data_len += io_len;
1452 DPRINTFN(2, "transfer %d bytes to %p\n",
1455 err = usb_fifo_uiomove(f, pdata, io_len, uio);
1458 f->flag_have_fragment = 0;
1459 USB_IF_ENQUEUE(&f->free_q, m);
1463 /* check if the buffer is ready to be transmitted */
1465 if ((f->flag_write_defrag == 0) ||
1466 (m->cur_data_len == m->max_data_len)) {
1467 f->flag_have_fragment = 0;
1470 * Check for write filter:
1472 * Sometimes it is convenient to process data
1473 * at the expense of a userland process
1474 * instead of a kernel process.
1476 if (f->methods->f_filter_write) {
1477 (f->methods->f_filter_write) (f, m);
1480 /* Put USB mbuf in the used queue */
1481 USB_IF_ENQUEUE(&f->used_q, m);
1483 /* Start writing data, if not already started */
1484 (f->methods->f_start_write) (f);
1486 /* Wait for more data or close */
1487 f->flag_have_fragment = 1;
1488 USB_IF_PREPEND(&f->free_q, m);
1491 } while (uio->uio_resid > 0);
1493 mtx_unlock(f->priv_mtx);
1495 usb_unref_device(cpd, &refs);
1501 usb_static_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
1505 struct usb_read_dir *urd;
1513 err = usb_read_symlink(u.urd->urd_data,
1514 u.urd->urd_startentry, u.urd->urd_maxlen);
1516 case USB_DEV_QUIRK_GET:
1517 case USB_QUIRK_NAME_GET:
1518 case USB_DEV_QUIRK_ADD:
1519 case USB_DEV_QUIRK_REMOVE:
1520 err = usb_quirk_ioctl_p(cmd, data, fflag, td);
1522 case USB_GET_TEMPLATE:
1523 *(int *)data = usb_template;
1526 case USB_SET_TEMPLATE:
1527 err = priv_check(curthread, PRIV_DRIVER);
1530 usb_template = *(int *)data;
1540 usb_fifo_uiomove(struct usb_fifo *f, void *cp,
1541 int n, struct uio *uio)
1545 mtx_unlock(f->priv_mtx);
1548 * "uiomove()" can sleep so one needs to make a wrapper,
1549 * exiting the mutex and checking things:
1551 error = uiomove(cp, n, uio);
1553 mtx_lock(f->priv_mtx);
1559 usb_fifo_wait(struct usb_fifo *f)
1563 mtx_assert(f->priv_mtx, MA_OWNED);
1565 if (f->flag_iserror) {
1569 f->flag_sleeping = 1;
1571 err = cv_wait_sig(&f->cv_io, f->priv_mtx);
1573 if (f->flag_iserror) {
1581 usb_fifo_signal(struct usb_fifo *f)
1583 if (f->flag_sleeping) {
1584 f->flag_sleeping = 0;
1585 cv_broadcast(&f->cv_io);
1590 usb_fifo_wakeup(struct usb_fifo *f)
1594 if (f->flag_isselect) {
1595 selwakeup(&f->selinfo);
1596 f->flag_isselect = 0;
1598 if (f->async_p != NULL) {
1599 PROC_LOCK(f->async_p);
1600 psignal(f->async_p, SIGIO);
1601 PROC_UNLOCK(f->async_p);
1606 usb_fifo_dummy_open(struct usb_fifo *fifo, int fflags)
1612 usb_fifo_dummy_close(struct usb_fifo *fifo, int fflags)
1618 usb_fifo_dummy_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
1624 usb_fifo_dummy_cmd(struct usb_fifo *fifo)
1626 fifo->flag_flushing = 0; /* not flushing */
1630 usb_fifo_check_methods(struct usb_fifo_methods *pm)
1632 /* check that all callback functions are OK */
1634 if (pm->f_open == NULL)
1635 pm->f_open = &usb_fifo_dummy_open;
1637 if (pm->f_close == NULL)
1638 pm->f_close = &usb_fifo_dummy_close;
1640 if (pm->f_ioctl == NULL)
1641 pm->f_ioctl = &usb_fifo_dummy_ioctl;
1643 if (pm->f_ioctl_post == NULL)
1644 pm->f_ioctl_post = &usb_fifo_dummy_ioctl;
1646 if (pm->f_start_read == NULL)
1647 pm->f_start_read = &usb_fifo_dummy_cmd;
1649 if (pm->f_stop_read == NULL)
1650 pm->f_stop_read = &usb_fifo_dummy_cmd;
1652 if (pm->f_start_write == NULL)
1653 pm->f_start_write = &usb_fifo_dummy_cmd;
1655 if (pm->f_stop_write == NULL)
1656 pm->f_stop_write = &usb_fifo_dummy_cmd;
1659 /*------------------------------------------------------------------------*
1662 * The following function will create a duplex FIFO.
1667 *------------------------------------------------------------------------*/
1669 usb_fifo_attach(struct usb_device *udev, void *priv_sc,
1670 struct mtx *priv_mtx, struct usb_fifo_methods *pm,
1671 struct usb_fifo_sc *f_sc, uint16_t unit, int16_t subunit,
1672 uint8_t iface_index, uid_t uid, gid_t gid, int mode)
1674 struct usb_fifo *f_tx;
1675 struct usb_fifo *f_rx;
1679 f_sc->fp[USB_FIFO_TX] = NULL;
1680 f_sc->fp[USB_FIFO_RX] = NULL;
1685 /* check the methods */
1686 usb_fifo_check_methods(pm);
1688 if (priv_mtx == NULL)
1691 /* search for a free FIFO slot */
1692 for (n = 0;; n += 2) {
1694 if (n == USB_FIFO_MAX) {
1695 /* end of FIFOs reached */
1698 /* Check for TX FIFO */
1699 if (udev->fifo[n + USB_FIFO_TX] != NULL) {
1702 /* Check for RX FIFO */
1703 if (udev->fifo[n + USB_FIFO_RX] != NULL) {
1709 f_tx = usb_fifo_alloc();
1710 f_rx = usb_fifo_alloc();
1712 if ((f_tx == NULL) || (f_rx == NULL)) {
1713 usb_fifo_free(f_tx);
1714 usb_fifo_free(f_rx);
1717 /* initialise FIFO structures */
1719 f_tx->fifo_index = n + USB_FIFO_TX;
1720 f_tx->dev_ep_index = -1;
1721 f_tx->priv_mtx = priv_mtx;
1722 f_tx->priv_sc0 = priv_sc;
1724 f_tx->iface_index = iface_index;
1727 f_rx->fifo_index = n + USB_FIFO_RX;
1728 f_rx->dev_ep_index = -1;
1729 f_rx->priv_mtx = priv_mtx;
1730 f_rx->priv_sc0 = priv_sc;
1732 f_rx->iface_index = iface_index;
1735 f_sc->fp[USB_FIFO_TX] = f_tx;
1736 f_sc->fp[USB_FIFO_RX] = f_rx;
1738 mtx_lock(&usb_ref_lock);
1739 udev->fifo[f_tx->fifo_index] = f_tx;
1740 udev->fifo[f_rx->fifo_index] = f_rx;
1741 mtx_unlock(&usb_ref_lock);
1743 for (n = 0; n != 4; n++) {
1745 if (pm->basename[n] == NULL) {
1749 if (snprintf(devname, sizeof(devname),
1750 "%s%u%s", pm->basename[n],
1751 unit, pm->postfix[n] ?
1752 pm->postfix[n] : "")) {
1756 if (snprintf(devname, sizeof(devname),
1757 "%s%u.%d%s", pm->basename[n],
1758 unit, subunit, pm->postfix[n] ?
1759 pm->postfix[n] : "")) {
1765 * Distribute the symbolic links into two FIFO structures:
1768 f_rx->symlink[n / 2] =
1769 usb_alloc_symlink(devname);
1771 f_tx->symlink[n / 2] =
1772 usb_alloc_symlink(devname);
1775 /* Create the device */
1776 f_sc->dev = usb_make_dev(udev, devname, -1,
1777 f_tx->fifo_index & f_rx->fifo_index,
1778 FREAD|FWRITE, uid, gid, mode);
1781 DPRINTFN(2, "attached %p/%p\n", f_tx, f_rx);
1785 /*------------------------------------------------------------------------*
1786 * usb_fifo_alloc_buffer
1791 *------------------------------------------------------------------------*/
1793 usb_fifo_alloc_buffer(struct usb_fifo *f, usb_size_t bufsize,
1796 usb_fifo_free_buffer(f);
1798 /* allocate an endpoint */
1799 f->free_q.ifq_maxlen = nbuf;
1800 f->used_q.ifq_maxlen = nbuf;
1802 f->queue_data = usb_alloc_mbufs(
1803 M_USBDEV, &f->free_q, bufsize, nbuf);
1805 if ((f->queue_data == NULL) && bufsize && nbuf) {
1808 return (0); /* success */
1811 /*------------------------------------------------------------------------*
1812 * usb_fifo_free_buffer
1814 * This function will free the buffers associated with a FIFO. This
1815 * function can be called multiple times in a row.
1816 *------------------------------------------------------------------------*/
1818 usb_fifo_free_buffer(struct usb_fifo *f)
1820 if (f->queue_data) {
1821 /* free old buffer */
1822 free(f->queue_data, M_USBDEV);
1823 f->queue_data = NULL;
1827 memset(&f->free_q, 0, sizeof(f->free_q));
1828 memset(&f->used_q, 0, sizeof(f->used_q));
1832 usb_fifo_detach(struct usb_fifo_sc *f_sc)
1837 usb_fifo_free(f_sc->fp[USB_FIFO_TX]);
1838 usb_fifo_free(f_sc->fp[USB_FIFO_RX]);
1840 f_sc->fp[USB_FIFO_TX] = NULL;
1841 f_sc->fp[USB_FIFO_RX] = NULL;
1843 usb_destroy_dev(f_sc->dev);
1847 DPRINTFN(2, "detached %p\n", f_sc);
1851 usb_fifo_put_bytes_max(struct usb_fifo *f)
1856 USB_IF_POLL(&f->free_q, m);
1859 len = m->max_data_len;
1866 /*------------------------------------------------------------------------*
1870 * 0 - normal operation
1871 * 1 - set last packet flag to enforce framing
1872 *------------------------------------------------------------------------*/
1874 usb_fifo_put_data(struct usb_fifo *f, struct usb_page_cache *pc,
1875 usb_frlength_t offset, usb_frlength_t len, uint8_t what)
1878 usb_frlength_t io_len;
1880 while (len || (what == 1)) {
1882 USB_IF_DEQUEUE(&f->free_q, m);
1887 io_len = MIN(len, m->cur_data_len);
1889 usbd_copy_out(pc, offset, m->cur_data_ptr, io_len);
1891 m->cur_data_len = io_len;
1895 if ((len == 0) && (what == 1)) {
1898 USB_IF_ENQUEUE(&f->used_q, m);
1902 if ((len == 0) || (what == 1)) {
1912 usb_fifo_put_data_linear(struct usb_fifo *f, void *ptr,
1913 usb_size_t len, uint8_t what)
1918 while (len || (what == 1)) {
1920 USB_IF_DEQUEUE(&f->free_q, m);
1925 io_len = MIN(len, m->cur_data_len);
1927 memcpy(m->cur_data_ptr, ptr, io_len);
1929 m->cur_data_len = io_len;
1930 ptr = USB_ADD_BYTES(ptr, io_len);
1933 if ((len == 0) && (what == 1)) {
1936 USB_IF_ENQUEUE(&f->used_q, m);
1940 if ((len == 0) || (what == 1)) {
1950 usb_fifo_put_data_buffer(struct usb_fifo *f, void *ptr, usb_size_t len)
1954 USB_IF_DEQUEUE(&f->free_q, m);
1957 m->cur_data_len = len;
1958 m->cur_data_ptr = ptr;
1959 USB_IF_ENQUEUE(&f->used_q, m);
1967 usb_fifo_put_data_error(struct usb_fifo *f)
1969 f->flag_iserror = 1;
1973 /*------------------------------------------------------------------------*
1977 * 0 - normal operation
1978 * 1 - only get one "usb_mbuf"
1982 * 1 - data in buffer
1983 *------------------------------------------------------------------------*/
1985 usb_fifo_get_data(struct usb_fifo *f, struct usb_page_cache *pc,
1986 usb_frlength_t offset, usb_frlength_t len, usb_frlength_t *actlen,
1990 usb_frlength_t io_len;
1991 uint8_t tr_data = 0;
1997 USB_IF_DEQUEUE(&f->used_q, m);
2003 io_len = MIN(len, m->cur_data_len);
2005 usbd_copy_in(pc, offset, m->cur_data_ptr, io_len);
2009 actlen[0] += io_len;
2010 m->cur_data_ptr += io_len;
2011 m->cur_data_len -= io_len;
2013 if ((m->cur_data_len == 0) || (what == 1)) {
2014 USB_IF_ENQUEUE(&f->free_q, m);
2022 USB_IF_PREPEND(&f->used_q, m);
2027 /* wait for data to be written out */
2030 if (f->flag_flushing) {
2031 /* check if we should send a short packet */
2032 if (f->flag_short != 0) {
2037 /* flushing complete */
2038 f->flag_flushing = 0;
2051 usb_fifo_get_data_linear(struct usb_fifo *f, void *ptr,
2052 usb_size_t len, usb_size_t *actlen, uint8_t what)
2056 uint8_t tr_data = 0;
2062 USB_IF_DEQUEUE(&f->used_q, m);
2068 io_len = MIN(len, m->cur_data_len);
2070 memcpy(ptr, m->cur_data_ptr, io_len);
2073 ptr = USB_ADD_BYTES(ptr, io_len);
2074 actlen[0] += io_len;
2075 m->cur_data_ptr += io_len;
2076 m->cur_data_len -= io_len;
2078 if ((m->cur_data_len == 0) || (what == 1)) {
2079 USB_IF_ENQUEUE(&f->free_q, m);
2087 USB_IF_PREPEND(&f->used_q, m);
2092 /* wait for data to be written out */
2095 if (f->flag_flushing) {
2096 /* check if we should send a short packet */
2097 if (f->flag_short != 0) {
2102 /* flushing complete */
2103 f->flag_flushing = 0;
2116 usb_fifo_get_data_buffer(struct usb_fifo *f, void **pptr, usb_size_t *plen)
2120 USB_IF_POLL(&f->used_q, m);
2123 *plen = m->cur_data_len;
2124 *pptr = m->cur_data_ptr;
2132 usb_fifo_get_data_error(struct usb_fifo *f)
2134 f->flag_iserror = 1;
2138 /*------------------------------------------------------------------------*
2143 * Else: Pointer to symlink entry
2144 *------------------------------------------------------------------------*/
2145 struct usb_symlink *
2146 usb_alloc_symlink(const char *target)
2148 struct usb_symlink *ps;
2150 ps = malloc(sizeof(*ps), M_USBDEV, M_WAITOK);
2154 /* XXX no longer needed */
2155 strlcpy(ps->src_path, target, sizeof(ps->src_path));
2156 ps->src_len = strlen(ps->src_path);
2157 strlcpy(ps->dst_path, target, sizeof(ps->dst_path));
2158 ps->dst_len = strlen(ps->dst_path);
2160 sx_xlock(&usb_sym_lock);
2161 TAILQ_INSERT_TAIL(&usb_sym_head, ps, sym_entry);
2162 sx_unlock(&usb_sym_lock);
2166 /*------------------------------------------------------------------------*
2168 *------------------------------------------------------------------------*/
2170 usb_free_symlink(struct usb_symlink *ps)
2175 sx_xlock(&usb_sym_lock);
2176 TAILQ_REMOVE(&usb_sym_head, ps, sym_entry);
2177 sx_unlock(&usb_sym_lock);
2182 /*------------------------------------------------------------------------*
2188 *------------------------------------------------------------------------*/
2190 usb_read_symlink(uint8_t *user_ptr, uint32_t startentry, uint32_t user_len)
2192 struct usb_symlink *ps;
2198 sx_xlock(&usb_sym_lock);
2200 TAILQ_FOREACH(ps, &usb_sym_head, sym_entry) {
2203 * Compute total length of source and destination symlink
2204 * strings pluss one length byte and two NUL bytes:
2206 temp = ps->src_len + ps->dst_len + 3;
2210 * Skip entry because this length cannot fit
2215 if (startentry != 0) {
2216 /* decrement read offset */
2220 if (temp > user_len) {
2221 /* out of buffer space */
2226 /* copy out total length */
2228 error = copyout(&len,
2229 USB_ADD_BYTES(user_ptr, delta), 1);
2235 /* copy out source string */
2237 error = copyout(ps->src_path,
2238 USB_ADD_BYTES(user_ptr, delta), ps->src_len);
2243 delta += ps->src_len;
2244 error = copyout(&len,
2245 USB_ADD_BYTES(user_ptr, delta), 1);
2251 /* copy out destination string */
2253 error = copyout(ps->dst_path,
2254 USB_ADD_BYTES(user_ptr, delta), ps->dst_len);
2259 delta += ps->dst_len;
2260 error = copyout(&len,
2261 USB_ADD_BYTES(user_ptr, delta), 1);
2270 /* a zero length entry indicates the end */
2272 if ((user_len != 0) && (error == 0)) {
2276 error = copyout(&len,
2277 USB_ADD_BYTES(user_ptr, delta), 1);
2279 sx_unlock(&usb_sym_lock);
2284 usb_fifo_set_close_zlp(struct usb_fifo *f, uint8_t onoff)
2289 /* send a Zero Length Packet, ZLP, before close */
2290 f->flag_short = onoff;
2294 usb_fifo_set_write_defrag(struct usb_fifo *f, uint8_t onoff)
2299 /* defrag written data */
2300 f->flag_write_defrag = onoff;
2301 /* reset defrag state */
2302 f->flag_have_fragment = 0;
2306 usb_fifo_softc(struct usb_fifo *f)
2308 return (f->priv_sc0);
2310 #endif /* USB_HAVE_UGEN */