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 #ifdef USB_GLOBAL_INCLUDE_FILE
31 #include USB_GLOBAL_INCLUDE_FILE
33 #include <sys/stdint.h>
34 #include <sys/stddef.h>
35 #include <sys/param.h>
36 #include <sys/queue.h>
37 #include <sys/types.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
41 #include <sys/module.h>
43 #include <sys/mutex.h>
44 #include <sys/condvar.h>
45 #include <sys/sysctl.h>
47 #include <sys/unistd.h>
48 #include <sys/callout.h>
49 #include <sys/malloc.h>
51 #include <sys/vnode.h>
53 #include <sys/fcntl.h>
55 #include <dev/usb/usb.h>
56 #include <dev/usb/usb_ioctl.h>
57 #include <dev/usb/usbdi.h>
58 #include <dev/usb/usbdi_util.h>
60 #define USB_DEBUG_VAR usb_fifo_debug
62 #include <dev/usb/usb_core.h>
63 #include <dev/usb/usb_dev.h>
64 #include <dev/usb/usb_mbuf.h>
65 #include <dev/usb/usb_process.h>
66 #include <dev/usb/usb_device.h>
67 #include <dev/usb/usb_debug.h>
68 #include <dev/usb/usb_busdma.h>
69 #include <dev/usb/usb_generic.h>
70 #include <dev/usb/usb_dynamic.h>
71 #include <dev/usb/usb_util.h>
73 #include <dev/usb/usb_controller.h>
74 #include <dev/usb/usb_bus.h>
76 #include <sys/filio.h>
77 #include <sys/ttycom.h>
78 #include <sys/syscallsubr.h>
80 #include <machine/stdarg.h>
81 #endif /* USB_GLOBAL_INCLUDE_FILE */
86 static int usb_fifo_debug = 0;
88 static SYSCTL_NODE(_hw_usb, OID_AUTO, dev, CTLFLAG_RW, 0, "USB device");
89 SYSCTL_INT(_hw_usb_dev, OID_AUTO, debug, CTLFLAG_RW | CTLFLAG_TUN,
90 &usb_fifo_debug, 0, "Debug Level");
91 TUNABLE_INT("hw.usb.dev.debug", &usb_fifo_debug);
94 #if ((__FreeBSD_version >= 700001) || (__FreeBSD_version == 0) || \
95 ((__FreeBSD_version >= 600034) && (__FreeBSD_version < 700000)))
96 #define USB_UCRED struct ucred *ucred,
103 static int usb_fifo_open(struct usb_cdev_privdata *,
104 struct usb_fifo *, int);
105 static void usb_fifo_close(struct usb_fifo *, int);
106 static void usb_dev_init(void *);
107 static void usb_dev_init_post(void *);
108 static void usb_dev_uninit(void *);
109 static int usb_fifo_uiomove(struct usb_fifo *, void *, int,
111 static void usb_fifo_check_methods(struct usb_fifo_methods *);
112 static struct usb_fifo *usb_fifo_alloc(void);
113 static struct usb_endpoint *usb_dev_get_ep(struct usb_device *, uint8_t,
115 static void usb_loc_fill(struct usb_fs_privdata *,
116 struct usb_cdev_privdata *);
117 static void usb_close(void *);
118 static usb_error_t usb_ref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *, int);
119 static usb_error_t usb_usb_ref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *);
120 static void usb_unref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *);
122 static d_open_t usb_open;
123 static d_ioctl_t usb_ioctl;
124 static d_read_t usb_read;
125 static d_write_t usb_write;
126 static d_poll_t usb_poll;
128 static d_ioctl_t usb_static_ioctl;
130 static usb_fifo_open_t usb_fifo_dummy_open;
131 static usb_fifo_close_t usb_fifo_dummy_close;
132 static usb_fifo_ioctl_t usb_fifo_dummy_ioctl;
133 static usb_fifo_cmd_t usb_fifo_dummy_cmd;
135 /* character device structure used for devices (/dev/ugenX.Y and /dev/uXXX) */
136 struct cdevsw usb_devsw = {
137 .d_version = D_VERSION,
139 .d_ioctl = usb_ioctl,
141 .d_flags = D_TRACKCLOSE,
143 .d_write = usb_write,
147 static struct cdev* usb_dev = NULL;
149 /* character device structure used for /dev/usb */
150 static struct cdevsw usb_static_devsw = {
151 .d_version = D_VERSION,
152 .d_ioctl = usb_static_ioctl,
156 static TAILQ_HEAD(, usb_symlink) usb_sym_head;
157 static struct sx usb_sym_lock;
159 struct mtx usb_ref_lock;
161 /*------------------------------------------------------------------------*
164 * This is used to fill out a usb_cdev_privdata structure based on the
165 * device's address as contained in usb_fs_privdata.
166 *------------------------------------------------------------------------*/
168 usb_loc_fill(struct usb_fs_privdata* pd, struct usb_cdev_privdata *cpd)
170 cpd->bus_index = pd->bus_index;
171 cpd->dev_index = pd->dev_index;
172 cpd->ep_addr = pd->ep_addr;
173 cpd->fifo_index = pd->fifo_index;
176 /*------------------------------------------------------------------------*
179 * This function is used to atomically refer an USB device by its
180 * device location. If this function returns success the USB device
181 * will not dissappear until the USB device is unreferenced.
184 * 0: Success, refcount incremented on the given USB device.
186 *------------------------------------------------------------------------*/
188 usb_ref_device(struct usb_cdev_privdata *cpd,
189 struct usb_cdev_refdata *crd, int need_uref)
191 struct usb_fifo **ppf;
194 DPRINTFN(2, "cpd=%p need uref=%d\n", cpd, need_uref);
197 memset(crd, 0, sizeof(*crd));
199 mtx_lock(&usb_ref_lock);
200 cpd->bus = devclass_get_softc(usb_devclass_ptr, cpd->bus_index);
201 if (cpd->bus == NULL) {
202 DPRINTFN(2, "no bus at %u\n", cpd->bus_index);
205 cpd->udev = cpd->bus->devices[cpd->dev_index];
206 if (cpd->udev == NULL) {
207 DPRINTFN(2, "no device at %u\n", cpd->dev_index);
210 if (cpd->udev->refcount == USB_DEV_REF_MAX) {
211 DPRINTFN(2, "no dev ref\n");
215 DPRINTFN(2, "ref udev - needed\n");
216 cpd->udev->refcount++;
218 mtx_unlock(&usb_ref_lock);
221 * We need to grab the enumeration SX-lock before
222 * grabbing the FIFO refs to avoid deadlock at detach!
224 crd->do_unlock = usbd_enum_lock(cpd->udev);
226 mtx_lock(&usb_ref_lock);
229 * Set "is_uref" after grabbing the default SX lock
234 /* check if we are doing an open */
235 if (cpd->fflags == 0) {
236 /* use zero defaults */
238 /* check for write */
239 if (cpd->fflags & FWRITE) {
240 ppf = cpd->udev->fifo;
241 f = ppf[cpd->fifo_index + USB_FIFO_TX];
243 crd->is_write = 1; /* ref */
244 if (f == NULL || f->refcount == USB_FIFO_REF_MAX)
246 if (f->curr_cpd != cpd)
248 /* check if USB-FS is active */
249 if (f->fs_ep_max != 0) {
255 if (cpd->fflags & FREAD) {
256 ppf = cpd->udev->fifo;
257 f = ppf[cpd->fifo_index + USB_FIFO_RX];
259 crd->is_read = 1; /* ref */
260 if (f == NULL || f->refcount == USB_FIFO_REF_MAX)
262 if (f->curr_cpd != cpd)
264 /* check if USB-FS is active */
265 if (f->fs_ep_max != 0) {
271 /* when everything is OK we increment the refcounts */
273 DPRINTFN(2, "ref write\n");
274 crd->txfifo->refcount++;
277 DPRINTFN(2, "ref read\n");
278 crd->rxfifo->refcount++;
280 mtx_unlock(&usb_ref_lock);
286 usbd_enum_unlock(cpd->udev);
289 if (--(cpd->udev->refcount) == 0) {
290 cv_signal(&cpd->udev->ref_cv);
293 mtx_unlock(&usb_ref_lock);
294 DPRINTFN(2, "fail\n");
295 return (USB_ERR_INVAL);
298 /*------------------------------------------------------------------------*
301 * This function is used to upgrade an USB reference to include the
302 * USB device reference on a USB location.
305 * 0: Success, refcount incremented on the given USB device.
307 *------------------------------------------------------------------------*/
309 usb_usb_ref_device(struct usb_cdev_privdata *cpd,
310 struct usb_cdev_refdata *crd)
313 * Check if we already got an USB reference on this location:
316 return (0); /* success */
319 * To avoid deadlock at detach we need to drop the FIFO ref
320 * and re-acquire a new ref!
322 usb_unref_device(cpd, crd);
324 return (usb_ref_device(cpd, crd, 1 /* need uref */));
327 /*------------------------------------------------------------------------*
330 * This function will release the reference count by one unit for the
332 *------------------------------------------------------------------------*/
334 usb_unref_device(struct usb_cdev_privdata *cpd,
335 struct usb_cdev_refdata *crd)
338 DPRINTFN(2, "cpd=%p is_uref=%d\n", cpd, crd->is_uref);
341 usbd_enum_unlock(cpd->udev);
343 mtx_lock(&usb_ref_lock);
345 if (--(crd->rxfifo->refcount) == 0) {
346 cv_signal(&crd->rxfifo->cv_drain);
351 if (--(crd->txfifo->refcount) == 0) {
352 cv_signal(&crd->txfifo->cv_drain);
357 if (--(cpd->udev->refcount) == 0) {
358 cv_signal(&cpd->udev->ref_cv);
362 mtx_unlock(&usb_ref_lock);
365 static struct usb_fifo *
370 f = malloc(sizeof(*f), M_USBDEV, M_WAITOK | M_ZERO);
372 cv_init(&f->cv_io, "FIFO-IO");
373 cv_init(&f->cv_drain, "FIFO-DRAIN");
379 /*------------------------------------------------------------------------*
381 *------------------------------------------------------------------------*/
383 usb_fifo_create(struct usb_cdev_privdata *cpd,
384 struct usb_cdev_refdata *crd)
386 struct usb_device *udev = cpd->udev;
388 struct usb_endpoint *ep;
394 int e = cpd->ep_addr;
396 is_tx = (cpd->fflags & FWRITE) ? 1 : 0;
397 is_rx = (cpd->fflags & FREAD) ? 1 : 0;
401 /* Preallocated FIFO */
403 DPRINTFN(5, "Preallocated FIFO\n");
405 f = udev->fifo[cpd->fifo_index + USB_FIFO_TX];
411 f = udev->fifo[cpd->fifo_index + USB_FIFO_RX];
419 KASSERT(e >= 0 && e <= 15, ("endpoint %d out of range", e));
421 /* search for a free FIFO slot */
422 DPRINTFN(5, "Endpoint device, searching for 0x%02x\n", e);
423 for (n = 0;; n += 2) {
425 if (n == USB_FIFO_MAX) {
430 /* end of FIFOs reached */
431 DPRINTFN(5, "out of FIFOs\n");
435 /* Check for TX FIFO */
437 f = udev->fifo[n + USB_FIFO_TX];
439 if (f->dev_ep_index != e) {
440 /* wrong endpoint index */
443 if (f->curr_cpd != NULL) {
448 } else if (no_null) {
452 /* Check for RX FIFO */
454 f = udev->fifo[n + USB_FIFO_RX];
456 if (f->dev_ep_index != e) {
457 /* wrong endpoint index */
460 if (f->curr_cpd != NULL) {
465 } else if (no_null) {
473 if (e >= (USB_EP_MAX / 2)) {
474 /* we don't create any endpoints in this range */
475 DPRINTFN(5, "ep out of range\n");
476 return (is_busy ? EBUSY : EINVAL);
480 if ((e != 0) && is_busy) {
482 * Only the default control endpoint is allowed to be
483 * opened multiple times!
485 DPRINTFN(5, "busy\n");
491 (udev->fifo[n + USB_FIFO_TX] == NULL)) {
492 ep = usb_dev_get_ep(udev, e, USB_FIFO_TX);
493 DPRINTFN(5, "dev_get_endpoint(%d, 0x%x)\n", e, USB_FIFO_TX);
495 DPRINTFN(5, "dev_get_endpoint returned NULL\n");
498 f = usb_fifo_alloc();
500 DPRINTFN(5, "could not alloc tx fifo\n");
503 /* update some fields */
504 f->fifo_index = n + USB_FIFO_TX;
506 f->priv_mtx = &udev->device_mtx;
508 f->methods = &usb_ugen_methods;
509 f->iface_index = ep->iface_index;
511 mtx_lock(&usb_ref_lock);
512 udev->fifo[n + USB_FIFO_TX] = f;
513 mtx_unlock(&usb_ref_lock);
517 (udev->fifo[n + USB_FIFO_RX] == NULL)) {
519 ep = usb_dev_get_ep(udev, e, USB_FIFO_RX);
520 DPRINTFN(5, "dev_get_endpoint(%d, 0x%x)\n", e, USB_FIFO_RX);
522 DPRINTFN(5, "dev_get_endpoint returned NULL\n");
525 f = usb_fifo_alloc();
527 DPRINTFN(5, "could not alloc rx fifo\n");
530 /* update some fields */
531 f->fifo_index = n + USB_FIFO_RX;
533 f->priv_mtx = &udev->device_mtx;
535 f->methods = &usb_ugen_methods;
536 f->iface_index = ep->iface_index;
538 mtx_lock(&usb_ref_lock);
539 udev->fifo[n + USB_FIFO_RX] = f;
540 mtx_unlock(&usb_ref_lock);
543 crd->txfifo = udev->fifo[n + USB_FIFO_TX];
546 crd->rxfifo = udev->fifo[n + USB_FIFO_RX];
548 /* fill out fifo index */
549 DPRINTFN(5, "fifo index = %d\n", n);
558 usb_fifo_free(struct usb_fifo *f)
566 /* destroy symlink devices, if any */
567 for (n = 0; n != 2; n++) {
569 usb_free_symlink(f->symlink[n]);
570 f->symlink[n] = NULL;
573 mtx_lock(&usb_ref_lock);
575 /* delink ourselves to stop calls from userland */
576 if ((f->fifo_index < USB_FIFO_MAX) &&
578 (f->udev->fifo[f->fifo_index] == f)) {
579 f->udev->fifo[f->fifo_index] = NULL;
581 DPRINTFN(0, "USB FIFO %p has not been linked\n", f);
584 /* decrease refcount */
586 /* prevent any write flush */
588 /* need to wait until all callers have exited */
589 while (f->refcount != 0) {
590 mtx_unlock(&usb_ref_lock); /* avoid LOR */
591 mtx_lock(f->priv_mtx);
592 /* get I/O thread out of any sleep state */
593 if (f->flag_sleeping) {
594 f->flag_sleeping = 0;
595 cv_broadcast(&f->cv_io);
597 mtx_unlock(f->priv_mtx);
598 mtx_lock(&usb_ref_lock);
601 * Check if the "f->refcount" variable reached zero
602 * during the unlocked time before entering wait:
604 if (f->refcount == 0)
608 cv_wait(&f->cv_drain, &usb_ref_lock);
610 mtx_unlock(&usb_ref_lock);
612 /* take care of closing the device here, if any */
613 usb_fifo_close(f, 0);
615 cv_destroy(&f->cv_io);
616 cv_destroy(&f->cv_drain);
621 static struct usb_endpoint *
622 usb_dev_get_ep(struct usb_device *udev, uint8_t ep_index, uint8_t dir)
624 struct usb_endpoint *ep;
630 if (dir == USB_FIFO_RX) {
631 if (udev->flags.usb_mode == USB_MODE_HOST) {
637 if (udev->flags.usb_mode == USB_MODE_HOST) {
643 ep = usbd_get_ep_by_addr(udev, ep_index | ep_dir);
647 /* if the endpoint does not exist then return */
650 if (ep->edesc == NULL) {
651 /* invalid endpoint */
654 return (ep); /* success */
657 /*------------------------------------------------------------------------*
663 *------------------------------------------------------------------------*/
665 usb_fifo_open(struct usb_cdev_privdata *cpd,
666 struct usb_fifo *f, int fflags)
672 DPRINTFN(2, "no FIFO\n");
675 /* remove FWRITE and FREAD flags */
676 fflags &= ~(FWRITE | FREAD);
678 /* set correct file flags */
679 if ((f->fifo_index & 1) == USB_FIFO_TX) {
685 /* check if we are already opened */
686 /* we don't need any locks when checking this variable */
687 if (f->curr_cpd != NULL) {
692 /* reset short flag before open */
695 /* call open method */
696 err = (f->methods->f_open) (f, fflags);
700 mtx_lock(f->priv_mtx);
702 /* reset sleep flag */
703 f->flag_sleeping = 0;
705 /* reset error flag */
708 /* reset complete flag */
709 f->flag_iscomplete = 0;
711 /* reset select flag */
712 f->flag_isselect = 0;
714 /* reset flushing flag */
715 f->flag_flushing = 0;
717 /* reset ASYNC proc flag */
720 mtx_lock(&usb_ref_lock);
721 /* flag the fifo as opened to prevent others */
723 mtx_unlock(&usb_ref_lock);
728 mtx_unlock(f->priv_mtx);
733 /*------------------------------------------------------------------------*
735 *------------------------------------------------------------------------*/
737 usb_fifo_reset(struct usb_fifo *f)
745 USB_IF_DEQUEUE(&f->used_q, m);
747 USB_IF_ENQUEUE(&f->free_q, m);
752 /* reset have fragment flag */
753 f->flag_have_fragment = 0;
756 /*------------------------------------------------------------------------*
758 *------------------------------------------------------------------------*/
760 usb_fifo_close(struct usb_fifo *f, int fflags)
764 /* check if we are not opened */
765 if (f->curr_cpd == NULL) {
766 /* nothing to do - already closed */
769 mtx_lock(f->priv_mtx);
771 /* clear current cdev private data pointer */
774 /* check if we are selected */
775 if (f->flag_isselect) {
776 selwakeup(&f->selinfo);
777 f->flag_isselect = 0;
779 /* check if a thread wants SIGIO */
780 if (f->async_p != NULL) {
781 PROC_LOCK(f->async_p);
782 kern_psignal(f->async_p, SIGIO);
783 PROC_UNLOCK(f->async_p);
786 /* remove FWRITE and FREAD flags */
787 fflags &= ~(FWRITE | FREAD);
789 /* flush written data, if any */
790 if ((f->fifo_index & 1) == USB_FIFO_TX) {
792 if (!f->flag_iserror) {
794 /* set flushing flag */
795 f->flag_flushing = 1;
797 /* get the last packet in */
798 if (f->flag_have_fragment) {
800 f->flag_have_fragment = 0;
801 USB_IF_DEQUEUE(&f->free_q, m);
803 USB_IF_ENQUEUE(&f->used_q, m);
807 /* start write transfer, if not already started */
808 (f->methods->f_start_write) (f);
810 /* check if flushed already */
811 while (f->flag_flushing &&
812 (!f->flag_iserror)) {
813 /* wait until all data has been written */
814 f->flag_sleeping = 1;
815 err = cv_wait_sig(&f->cv_io, f->priv_mtx);
817 DPRINTF("signal received\n");
824 /* stop write transfer, if not already stopped */
825 (f->methods->f_stop_write) (f);
829 /* stop write transfer, if not already stopped */
830 (f->methods->f_stop_read) (f);
833 /* check if we are sleeping */
834 if (f->flag_sleeping) {
835 DPRINTFN(2, "Sleeping at close!\n");
837 mtx_unlock(f->priv_mtx);
839 /* call close method */
840 (f->methods->f_close) (f, fflags);
845 /*------------------------------------------------------------------------*
846 * usb_open - cdev callback
847 *------------------------------------------------------------------------*/
849 usb_open(struct cdev *dev, int fflags, int devtype, struct thread *td)
851 struct usb_fs_privdata* pd = (struct usb_fs_privdata*)dev->si_drv1;
852 struct usb_cdev_refdata refs;
853 struct usb_cdev_privdata *cpd;
856 DPRINTFN(2, "%s fflags=0x%08x\n", devtoname(dev), fflags);
858 KASSERT(fflags & (FREAD|FWRITE), ("invalid open flags"));
859 if (((fflags & FREAD) && !(pd->mode & FREAD)) ||
860 ((fflags & FWRITE) && !(pd->mode & FWRITE))) {
861 DPRINTFN(2, "access mode not supported\n");
865 cpd = malloc(sizeof(*cpd), M_USBDEV, M_WAITOK | M_ZERO);
866 ep = cpd->ep_addr = pd->ep_addr;
868 usb_loc_fill(pd, cpd);
869 err = usb_ref_device(cpd, &refs, 1);
871 DPRINTFN(2, "cannot ref device\n");
875 cpd->fflags = fflags; /* access mode for open lifetime */
877 /* create FIFOs, if any */
878 err = usb_fifo_create(cpd, &refs);
879 /* check for error */
881 DPRINTFN(2, "cannot create fifo\n");
882 usb_unref_device(cpd, &refs);
886 if (fflags & FREAD) {
887 err = usb_fifo_open(cpd, refs.rxfifo, fflags);
889 DPRINTFN(2, "read open failed\n");
890 usb_unref_device(cpd, &refs);
895 if (fflags & FWRITE) {
896 err = usb_fifo_open(cpd, refs.txfifo, fflags);
898 DPRINTFN(2, "write open failed\n");
899 if (fflags & FREAD) {
900 usb_fifo_close(refs.rxfifo, fflags);
902 usb_unref_device(cpd, &refs);
907 usb_unref_device(cpd, &refs);
908 devfs_set_cdevpriv(cpd, usb_close);
913 /*------------------------------------------------------------------------*
914 * usb_close - cdev callback
915 *------------------------------------------------------------------------*/
919 struct usb_cdev_refdata refs;
920 struct usb_cdev_privdata *cpd = arg;
923 DPRINTFN(2, "cpd=%p\n", cpd);
925 err = usb_ref_device(cpd, &refs, 0);
930 * If this function is not called directly from the root HUB
931 * thread, there is usually a need to lock the enumeration
934 if (!usbd_enum_is_locked(cpd->udev)) {
936 DPRINTFN(2, "Locking enumeration\n");
938 /* reference device */
939 err = usb_usb_ref_device(cpd, &refs);
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)) {
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 if (usb_ref_device(cpd, &refs, 1 /* need uref */)) {
1122 usb_unref_device(cpd, &refs);
1128 usb_poll(struct cdev* dev, int events, struct thread* td)
1130 struct usb_cdev_refdata refs;
1131 struct usb_cdev_privdata* cpd;
1134 int fflags, revents;
1136 if (devfs_get_cdevpriv((void **)&cpd) != 0 ||
1137 usb_ref_device(cpd, &refs, 0) != 0)
1139 (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM));
1141 fflags = cpd->fflags;
1143 /* Figure out who needs service */
1145 if ((events & (POLLOUT | POLLWRNORM)) &&
1146 (fflags & FWRITE)) {
1150 mtx_lock(f->priv_mtx);
1152 if (!refs.is_usbfs) {
1153 if (f->flag_iserror) {
1154 /* we got an error */
1157 if (f->queue_data == NULL) {
1159 * start write transfer, if not
1162 (f->methods->f_start_write) (f);
1164 /* check if any packets are available */
1165 USB_IF_POLL(&f->free_q, m);
1168 if (f->flag_iscomplete) {
1176 revents |= events & (POLLOUT | POLLWRNORM);
1178 f->flag_isselect = 1;
1179 selrecord(td, &f->selinfo);
1182 mtx_unlock(f->priv_mtx);
1184 if ((events & (POLLIN | POLLRDNORM)) &&
1189 mtx_lock(f->priv_mtx);
1191 if (!refs.is_usbfs) {
1192 if (f->flag_iserror) {
1193 /* we have and error */
1196 if (f->queue_data == NULL) {
1198 * start read transfer, if not
1201 (f->methods->f_start_read) (f);
1203 /* check if any packets are available */
1204 USB_IF_POLL(&f->used_q, m);
1207 if (f->flag_iscomplete) {
1215 revents |= events & (POLLIN | POLLRDNORM);
1217 f->flag_isselect = 1;
1218 selrecord(td, &f->selinfo);
1220 if (!refs.is_usbfs) {
1221 /* start reading data */
1222 (f->methods->f_start_read) (f);
1226 mtx_unlock(f->priv_mtx);
1228 usb_unref_device(cpd, &refs);
1233 usb_read(struct cdev *dev, struct uio *uio, int ioflag)
1235 struct usb_cdev_refdata refs;
1236 struct usb_cdev_privdata* cpd;
1243 uint8_t tr_data = 0;
1245 err = devfs_get_cdevpriv((void **)&cpd);
1249 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1253 fflags = cpd->fflags;
1257 /* should not happen */
1258 usb_unref_device(cpd, &refs);
1262 resid = uio->uio_resid;
1264 mtx_lock(f->priv_mtx);
1266 /* check for permanent read error */
1267 if (f->flag_iserror) {
1271 /* check if USB-FS interface is active */
1272 if (refs.is_usbfs) {
1274 * The queue is used for events that should be
1275 * retrieved using the "USB_FS_COMPLETE" ioctl.
1280 while (uio->uio_resid > 0) {
1282 USB_IF_DEQUEUE(&f->used_q, m);
1286 /* start read transfer, if not already started */
1288 (f->methods->f_start_read) (f);
1290 if (ioflag & IO_NDELAY) {
1292 /* return length before error */
1298 DPRINTF("sleeping\n");
1300 err = usb_fifo_wait(f);
1306 if (f->methods->f_filter_read) {
1308 * Sometimes it is convenient to process data at the
1309 * expense of a userland process instead of a kernel
1312 (f->methods->f_filter_read) (f, m);
1316 io_len = MIN(m->cur_data_len, uio->uio_resid);
1318 DPRINTFN(2, "transfer %d bytes from %p\n",
1319 io_len, m->cur_data_ptr);
1321 err = usb_fifo_uiomove(f,
1322 m->cur_data_ptr, io_len, uio);
1324 m->cur_data_len -= io_len;
1325 m->cur_data_ptr += io_len;
1327 if (m->cur_data_len == 0) {
1329 uint8_t last_packet;
1331 last_packet = m->last_packet;
1333 USB_IF_ENQUEUE(&f->free_q, m);
1340 USB_IF_PREPEND(&f->used_q, m);
1348 mtx_unlock(f->priv_mtx);
1350 usb_unref_device(cpd, &refs);
1356 usb_write(struct cdev *dev, struct uio *uio, int ioflag)
1358 struct usb_cdev_refdata refs;
1359 struct usb_cdev_privdata* cpd;
1367 uint8_t tr_data = 0;
1371 err = devfs_get_cdevpriv((void **)&cpd);
1375 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1379 fflags = cpd->fflags;
1383 /* should not happen */
1384 usb_unref_device(cpd, &refs);
1387 resid = uio->uio_resid;
1389 mtx_lock(f->priv_mtx);
1391 /* check for permanent write error */
1392 if (f->flag_iserror) {
1396 /* check if USB-FS interface is active */
1397 if (refs.is_usbfs) {
1399 * The queue is used for events that should be
1400 * retrieved using the "USB_FS_COMPLETE" ioctl.
1405 if (f->queue_data == NULL) {
1406 /* start write transfer, if not already started */
1407 (f->methods->f_start_write) (f);
1409 /* we allow writing zero length data */
1411 USB_IF_DEQUEUE(&f->free_q, m);
1415 if (ioflag & IO_NDELAY) {
1417 /* return length before error */
1423 DPRINTF("sleeping\n");
1425 err = usb_fifo_wait(f);
1433 if (f->flag_have_fragment == 0) {
1435 io_len = m->cur_data_len;
1436 pdata = m->cur_data_ptr;
1437 if (io_len > uio->uio_resid)
1438 io_len = uio->uio_resid;
1439 m->cur_data_len = io_len;
1441 io_len = m->max_data_len - m->cur_data_len;
1442 pdata = m->cur_data_ptr + m->cur_data_len;
1443 if (io_len > uio->uio_resid)
1444 io_len = uio->uio_resid;
1445 m->cur_data_len += io_len;
1448 DPRINTFN(2, "transfer %d bytes to %p\n",
1451 err = usb_fifo_uiomove(f, pdata, io_len, uio);
1454 f->flag_have_fragment = 0;
1455 USB_IF_ENQUEUE(&f->free_q, m);
1459 /* check if the buffer is ready to be transmitted */
1461 if ((f->flag_write_defrag == 0) ||
1462 (m->cur_data_len == m->max_data_len)) {
1463 f->flag_have_fragment = 0;
1466 * Check for write filter:
1468 * Sometimes it is convenient to process data
1469 * at the expense of a userland process
1470 * instead of a kernel process.
1472 if (f->methods->f_filter_write) {
1473 (f->methods->f_filter_write) (f, m);
1476 /* Put USB mbuf in the used queue */
1477 USB_IF_ENQUEUE(&f->used_q, m);
1479 /* Start writing data, if not already started */
1480 (f->methods->f_start_write) (f);
1482 /* Wait for more data or close */
1483 f->flag_have_fragment = 1;
1484 USB_IF_PREPEND(&f->free_q, m);
1487 } while (uio->uio_resid > 0);
1489 mtx_unlock(f->priv_mtx);
1491 usb_unref_device(cpd, &refs);
1497 usb_static_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
1501 struct usb_read_dir *urd;
1509 err = usb_read_symlink(u.urd->urd_data,
1510 u.urd->urd_startentry, u.urd->urd_maxlen);
1512 case USB_DEV_QUIRK_GET:
1513 case USB_QUIRK_NAME_GET:
1514 case USB_DEV_QUIRK_ADD:
1515 case USB_DEV_QUIRK_REMOVE:
1516 err = usb_quirk_ioctl_p(cmd, data, fflag, td);
1518 case USB_GET_TEMPLATE:
1519 *(int *)data = usb_template;
1522 case USB_SET_TEMPLATE:
1523 err = priv_check(curthread, PRIV_DRIVER);
1526 usb_template = *(int *)data;
1536 usb_fifo_uiomove(struct usb_fifo *f, void *cp,
1537 int n, struct uio *uio)
1541 mtx_unlock(f->priv_mtx);
1544 * "uiomove()" can sleep so one needs to make a wrapper,
1545 * exiting the mutex and checking things:
1547 error = uiomove(cp, n, uio);
1549 mtx_lock(f->priv_mtx);
1555 usb_fifo_wait(struct usb_fifo *f)
1559 mtx_assert(f->priv_mtx, MA_OWNED);
1561 if (f->flag_iserror) {
1565 f->flag_sleeping = 1;
1567 err = cv_wait_sig(&f->cv_io, f->priv_mtx);
1569 if (f->flag_iserror) {
1577 usb_fifo_signal(struct usb_fifo *f)
1579 if (f->flag_sleeping) {
1580 f->flag_sleeping = 0;
1581 cv_broadcast(&f->cv_io);
1586 usb_fifo_wakeup(struct usb_fifo *f)
1590 if (f->flag_isselect) {
1591 selwakeup(&f->selinfo);
1592 f->flag_isselect = 0;
1594 if (f->async_p != NULL) {
1595 PROC_LOCK(f->async_p);
1596 kern_psignal(f->async_p, SIGIO);
1597 PROC_UNLOCK(f->async_p);
1602 usb_fifo_dummy_open(struct usb_fifo *fifo, int fflags)
1608 usb_fifo_dummy_close(struct usb_fifo *fifo, int fflags)
1614 usb_fifo_dummy_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
1620 usb_fifo_dummy_cmd(struct usb_fifo *fifo)
1622 fifo->flag_flushing = 0; /* not flushing */
1626 usb_fifo_check_methods(struct usb_fifo_methods *pm)
1628 /* check that all callback functions are OK */
1630 if (pm->f_open == NULL)
1631 pm->f_open = &usb_fifo_dummy_open;
1633 if (pm->f_close == NULL)
1634 pm->f_close = &usb_fifo_dummy_close;
1636 if (pm->f_ioctl == NULL)
1637 pm->f_ioctl = &usb_fifo_dummy_ioctl;
1639 if (pm->f_ioctl_post == NULL)
1640 pm->f_ioctl_post = &usb_fifo_dummy_ioctl;
1642 if (pm->f_start_read == NULL)
1643 pm->f_start_read = &usb_fifo_dummy_cmd;
1645 if (pm->f_stop_read == NULL)
1646 pm->f_stop_read = &usb_fifo_dummy_cmd;
1648 if (pm->f_start_write == NULL)
1649 pm->f_start_write = &usb_fifo_dummy_cmd;
1651 if (pm->f_stop_write == NULL)
1652 pm->f_stop_write = &usb_fifo_dummy_cmd;
1655 /*------------------------------------------------------------------------*
1658 * The following function will create a duplex FIFO.
1663 *------------------------------------------------------------------------*/
1665 usb_fifo_attach(struct usb_device *udev, void *priv_sc,
1666 struct mtx *priv_mtx, struct usb_fifo_methods *pm,
1667 struct usb_fifo_sc *f_sc, uint16_t unit, int16_t subunit,
1668 uint8_t iface_index, uid_t uid, gid_t gid, int mode)
1670 struct usb_fifo *f_tx;
1671 struct usb_fifo *f_rx;
1675 f_sc->fp[USB_FIFO_TX] = NULL;
1676 f_sc->fp[USB_FIFO_RX] = NULL;
1681 /* check the methods */
1682 usb_fifo_check_methods(pm);
1684 if (priv_mtx == NULL)
1687 /* search for a free FIFO slot */
1688 for (n = 0;; n += 2) {
1690 if (n == USB_FIFO_MAX) {
1691 /* end of FIFOs reached */
1694 /* Check for TX FIFO */
1695 if (udev->fifo[n + USB_FIFO_TX] != NULL) {
1698 /* Check for RX FIFO */
1699 if (udev->fifo[n + USB_FIFO_RX] != NULL) {
1705 f_tx = usb_fifo_alloc();
1706 f_rx = usb_fifo_alloc();
1708 if ((f_tx == NULL) || (f_rx == NULL)) {
1709 usb_fifo_free(f_tx);
1710 usb_fifo_free(f_rx);
1713 /* initialise FIFO structures */
1715 f_tx->fifo_index = n + USB_FIFO_TX;
1716 f_tx->dev_ep_index = -1;
1717 f_tx->priv_mtx = priv_mtx;
1718 f_tx->priv_sc0 = priv_sc;
1720 f_tx->iface_index = iface_index;
1723 f_rx->fifo_index = n + USB_FIFO_RX;
1724 f_rx->dev_ep_index = -1;
1725 f_rx->priv_mtx = priv_mtx;
1726 f_rx->priv_sc0 = priv_sc;
1728 f_rx->iface_index = iface_index;
1731 f_sc->fp[USB_FIFO_TX] = f_tx;
1732 f_sc->fp[USB_FIFO_RX] = f_rx;
1734 mtx_lock(&usb_ref_lock);
1735 udev->fifo[f_tx->fifo_index] = f_tx;
1736 udev->fifo[f_rx->fifo_index] = f_rx;
1737 mtx_unlock(&usb_ref_lock);
1739 for (n = 0; n != 4; n++) {
1741 if (pm->basename[n] == NULL) {
1745 if (snprintf(devname, sizeof(devname),
1746 "%s%u%s", pm->basename[n],
1747 unit, pm->postfix[n] ?
1748 pm->postfix[n] : "")) {
1752 if (snprintf(devname, sizeof(devname),
1753 "%s%u.%d%s", pm->basename[n],
1754 unit, subunit, pm->postfix[n] ?
1755 pm->postfix[n] : "")) {
1761 * Distribute the symbolic links into two FIFO structures:
1764 f_rx->symlink[n / 2] =
1765 usb_alloc_symlink(devname);
1767 f_tx->symlink[n / 2] =
1768 usb_alloc_symlink(devname);
1771 /* Create the device */
1772 f_sc->dev = usb_make_dev(udev, devname, -1,
1773 f_tx->fifo_index & f_rx->fifo_index,
1774 FREAD|FWRITE, uid, gid, mode);
1777 DPRINTFN(2, "attached %p/%p\n", f_tx, f_rx);
1781 /*------------------------------------------------------------------------*
1782 * usb_fifo_alloc_buffer
1787 *------------------------------------------------------------------------*/
1789 usb_fifo_alloc_buffer(struct usb_fifo *f, usb_size_t bufsize,
1792 usb_fifo_free_buffer(f);
1794 /* allocate an endpoint */
1795 f->free_q.ifq_maxlen = nbuf;
1796 f->used_q.ifq_maxlen = nbuf;
1798 f->queue_data = usb_alloc_mbufs(
1799 M_USBDEV, &f->free_q, bufsize, nbuf);
1801 if ((f->queue_data == NULL) && bufsize && nbuf) {
1804 return (0); /* success */
1807 /*------------------------------------------------------------------------*
1808 * usb_fifo_free_buffer
1810 * This function will free the buffers associated with a FIFO. This
1811 * function can be called multiple times in a row.
1812 *------------------------------------------------------------------------*/
1814 usb_fifo_free_buffer(struct usb_fifo *f)
1816 if (f->queue_data) {
1817 /* free old buffer */
1818 free(f->queue_data, M_USBDEV);
1819 f->queue_data = NULL;
1823 memset(&f->free_q, 0, sizeof(f->free_q));
1824 memset(&f->used_q, 0, sizeof(f->used_q));
1828 usb_fifo_detach(struct usb_fifo_sc *f_sc)
1833 usb_fifo_free(f_sc->fp[USB_FIFO_TX]);
1834 usb_fifo_free(f_sc->fp[USB_FIFO_RX]);
1836 f_sc->fp[USB_FIFO_TX] = NULL;
1837 f_sc->fp[USB_FIFO_RX] = NULL;
1839 usb_destroy_dev(f_sc->dev);
1843 DPRINTFN(2, "detached %p\n", f_sc);
1847 usb_fifo_put_bytes_max(struct usb_fifo *f)
1852 USB_IF_POLL(&f->free_q, m);
1855 len = m->max_data_len;
1862 /*------------------------------------------------------------------------*
1866 * 0 - normal operation
1867 * 1 - set last packet flag to enforce framing
1868 *------------------------------------------------------------------------*/
1870 usb_fifo_put_data(struct usb_fifo *f, struct usb_page_cache *pc,
1871 usb_frlength_t offset, usb_frlength_t len, uint8_t what)
1874 usb_frlength_t io_len;
1876 while (len || (what == 1)) {
1878 USB_IF_DEQUEUE(&f->free_q, m);
1883 io_len = MIN(len, m->cur_data_len);
1885 usbd_copy_out(pc, offset, m->cur_data_ptr, io_len);
1887 m->cur_data_len = io_len;
1891 if ((len == 0) && (what == 1)) {
1894 USB_IF_ENQUEUE(&f->used_q, m);
1898 if ((len == 0) || (what == 1)) {
1908 usb_fifo_put_data_linear(struct usb_fifo *f, void *ptr,
1909 usb_size_t len, uint8_t what)
1914 while (len || (what == 1)) {
1916 USB_IF_DEQUEUE(&f->free_q, m);
1921 io_len = MIN(len, m->cur_data_len);
1923 memcpy(m->cur_data_ptr, ptr, io_len);
1925 m->cur_data_len = io_len;
1926 ptr = USB_ADD_BYTES(ptr, io_len);
1929 if ((len == 0) && (what == 1)) {
1932 USB_IF_ENQUEUE(&f->used_q, m);
1936 if ((len == 0) || (what == 1)) {
1946 usb_fifo_put_data_buffer(struct usb_fifo *f, void *ptr, usb_size_t len)
1950 USB_IF_DEQUEUE(&f->free_q, m);
1953 m->cur_data_len = len;
1954 m->cur_data_ptr = ptr;
1955 USB_IF_ENQUEUE(&f->used_q, m);
1963 usb_fifo_put_data_error(struct usb_fifo *f)
1965 f->flag_iserror = 1;
1969 /*------------------------------------------------------------------------*
1973 * 0 - normal operation
1974 * 1 - only get one "usb_mbuf"
1978 * 1 - data in buffer
1979 *------------------------------------------------------------------------*/
1981 usb_fifo_get_data(struct usb_fifo *f, struct usb_page_cache *pc,
1982 usb_frlength_t offset, usb_frlength_t len, usb_frlength_t *actlen,
1986 usb_frlength_t io_len;
1987 uint8_t tr_data = 0;
1993 USB_IF_DEQUEUE(&f->used_q, m);
1999 io_len = MIN(len, m->cur_data_len);
2001 usbd_copy_in(pc, offset, m->cur_data_ptr, io_len);
2005 actlen[0] += io_len;
2006 m->cur_data_ptr += io_len;
2007 m->cur_data_len -= io_len;
2009 if ((m->cur_data_len == 0) || (what == 1)) {
2010 USB_IF_ENQUEUE(&f->free_q, m);
2018 USB_IF_PREPEND(&f->used_q, m);
2023 /* wait for data to be written out */
2026 if (f->flag_flushing) {
2027 /* check if we should send a short packet */
2028 if (f->flag_short != 0) {
2033 /* flushing complete */
2034 f->flag_flushing = 0;
2047 usb_fifo_get_data_linear(struct usb_fifo *f, void *ptr,
2048 usb_size_t len, usb_size_t *actlen, uint8_t what)
2052 uint8_t tr_data = 0;
2058 USB_IF_DEQUEUE(&f->used_q, m);
2064 io_len = MIN(len, m->cur_data_len);
2066 memcpy(ptr, m->cur_data_ptr, io_len);
2069 ptr = USB_ADD_BYTES(ptr, io_len);
2070 actlen[0] += io_len;
2071 m->cur_data_ptr += io_len;
2072 m->cur_data_len -= io_len;
2074 if ((m->cur_data_len == 0) || (what == 1)) {
2075 USB_IF_ENQUEUE(&f->free_q, m);
2083 USB_IF_PREPEND(&f->used_q, m);
2088 /* wait for data to be written out */
2091 if (f->flag_flushing) {
2092 /* check if we should send a short packet */
2093 if (f->flag_short != 0) {
2098 /* flushing complete */
2099 f->flag_flushing = 0;
2112 usb_fifo_get_data_buffer(struct usb_fifo *f, void **pptr, usb_size_t *plen)
2116 USB_IF_POLL(&f->used_q, m);
2119 *plen = m->cur_data_len;
2120 *pptr = m->cur_data_ptr;
2128 usb_fifo_get_data_error(struct usb_fifo *f)
2130 f->flag_iserror = 1;
2134 /*------------------------------------------------------------------------*
2139 * Else: Pointer to symlink entry
2140 *------------------------------------------------------------------------*/
2141 struct usb_symlink *
2142 usb_alloc_symlink(const char *target)
2144 struct usb_symlink *ps;
2146 ps = malloc(sizeof(*ps), M_USBDEV, M_WAITOK);
2150 /* XXX no longer needed */
2151 strlcpy(ps->src_path, target, sizeof(ps->src_path));
2152 ps->src_len = strlen(ps->src_path);
2153 strlcpy(ps->dst_path, target, sizeof(ps->dst_path));
2154 ps->dst_len = strlen(ps->dst_path);
2156 sx_xlock(&usb_sym_lock);
2157 TAILQ_INSERT_TAIL(&usb_sym_head, ps, sym_entry);
2158 sx_unlock(&usb_sym_lock);
2162 /*------------------------------------------------------------------------*
2164 *------------------------------------------------------------------------*/
2166 usb_free_symlink(struct usb_symlink *ps)
2171 sx_xlock(&usb_sym_lock);
2172 TAILQ_REMOVE(&usb_sym_head, ps, sym_entry);
2173 sx_unlock(&usb_sym_lock);
2178 /*------------------------------------------------------------------------*
2184 *------------------------------------------------------------------------*/
2186 usb_read_symlink(uint8_t *user_ptr, uint32_t startentry, uint32_t user_len)
2188 struct usb_symlink *ps;
2194 sx_xlock(&usb_sym_lock);
2196 TAILQ_FOREACH(ps, &usb_sym_head, sym_entry) {
2199 * Compute total length of source and destination symlink
2200 * strings pluss one length byte and two NUL bytes:
2202 temp = ps->src_len + ps->dst_len + 3;
2206 * Skip entry because this length cannot fit
2211 if (startentry != 0) {
2212 /* decrement read offset */
2216 if (temp > user_len) {
2217 /* out of buffer space */
2222 /* copy out total length */
2224 error = copyout(&len,
2225 USB_ADD_BYTES(user_ptr, delta), 1);
2231 /* copy out source string */
2233 error = copyout(ps->src_path,
2234 USB_ADD_BYTES(user_ptr, delta), ps->src_len);
2239 delta += ps->src_len;
2240 error = copyout(&len,
2241 USB_ADD_BYTES(user_ptr, delta), 1);
2247 /* copy out destination string */
2249 error = copyout(ps->dst_path,
2250 USB_ADD_BYTES(user_ptr, delta), ps->dst_len);
2255 delta += ps->dst_len;
2256 error = copyout(&len,
2257 USB_ADD_BYTES(user_ptr, delta), 1);
2266 /* a zero length entry indicates the end */
2268 if ((user_len != 0) && (error == 0)) {
2272 error = copyout(&len,
2273 USB_ADD_BYTES(user_ptr, delta), 1);
2275 sx_unlock(&usb_sym_lock);
2280 usb_fifo_set_close_zlp(struct usb_fifo *f, uint8_t onoff)
2285 /* send a Zero Length Packet, ZLP, before close */
2286 f->flag_short = onoff;
2290 usb_fifo_set_write_defrag(struct usb_fifo *f, uint8_t onoff)
2295 /* defrag written data */
2296 f->flag_write_defrag = onoff;
2297 /* reset defrag state */
2298 f->flag_have_fragment = 0;
2302 usb_fifo_softc(struct usb_fifo *f)
2304 return (f->priv_sc0);
2306 #endif /* USB_HAVE_UGEN */