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 cv_wait(&f->cv_drain, &usb_ref_lock);
603 mtx_unlock(&usb_ref_lock);
605 /* take care of closing the device here, if any */
606 usb_fifo_close(f, 0);
608 cv_destroy(&f->cv_io);
609 cv_destroy(&f->cv_drain);
614 static struct usb_endpoint *
615 usb_dev_get_ep(struct usb_device *udev, uint8_t ep_index, uint8_t dir)
617 struct usb_endpoint *ep;
623 if (dir == USB_FIFO_RX) {
624 if (udev->flags.usb_mode == USB_MODE_HOST) {
630 if (udev->flags.usb_mode == USB_MODE_HOST) {
636 ep = usbd_get_ep_by_addr(udev, ep_index | ep_dir);
640 /* if the endpoint does not exist then return */
643 if (ep->edesc == NULL) {
644 /* invalid endpoint */
647 return (ep); /* success */
650 /*------------------------------------------------------------------------*
656 *------------------------------------------------------------------------*/
658 usb_fifo_open(struct usb_cdev_privdata *cpd,
659 struct usb_fifo *f, int fflags)
665 DPRINTFN(2, "no FIFO\n");
668 /* remove FWRITE and FREAD flags */
669 fflags &= ~(FWRITE | FREAD);
671 /* set correct file flags */
672 if ((f->fifo_index & 1) == USB_FIFO_TX) {
678 /* check if we are already opened */
679 /* we don't need any locks when checking this variable */
680 if (f->curr_cpd != NULL) {
685 /* reset short flag before open */
688 /* call open method */
689 err = (f->methods->f_open) (f, fflags);
693 mtx_lock(f->priv_mtx);
695 /* reset sleep flag */
696 f->flag_sleeping = 0;
698 /* reset error flag */
701 /* reset complete flag */
702 f->flag_iscomplete = 0;
704 /* reset select flag */
705 f->flag_isselect = 0;
707 /* reset flushing flag */
708 f->flag_flushing = 0;
710 /* reset ASYNC proc flag */
713 mtx_lock(&usb_ref_lock);
714 /* flag the fifo as opened to prevent others */
716 mtx_unlock(&usb_ref_lock);
721 mtx_unlock(f->priv_mtx);
726 /*------------------------------------------------------------------------*
728 *------------------------------------------------------------------------*/
730 usb_fifo_reset(struct usb_fifo *f)
738 USB_IF_DEQUEUE(&f->used_q, m);
740 USB_IF_ENQUEUE(&f->free_q, m);
745 /* reset have fragment flag */
746 f->flag_have_fragment = 0;
749 /*------------------------------------------------------------------------*
751 *------------------------------------------------------------------------*/
753 usb_fifo_close(struct usb_fifo *f, int fflags)
757 /* check if we are not opened */
758 if (f->curr_cpd == NULL) {
759 /* nothing to do - already closed */
762 mtx_lock(f->priv_mtx);
764 /* clear current cdev private data pointer */
767 /* check if we are selected */
768 if (f->flag_isselect) {
769 selwakeup(&f->selinfo);
770 f->flag_isselect = 0;
772 /* check if a thread wants SIGIO */
773 if (f->async_p != NULL) {
774 PROC_LOCK(f->async_p);
775 kern_psignal(f->async_p, SIGIO);
776 PROC_UNLOCK(f->async_p);
779 /* remove FWRITE and FREAD flags */
780 fflags &= ~(FWRITE | FREAD);
782 /* flush written data, if any */
783 if ((f->fifo_index & 1) == USB_FIFO_TX) {
785 if (!f->flag_iserror) {
787 /* set flushing flag */
788 f->flag_flushing = 1;
790 /* get the last packet in */
791 if (f->flag_have_fragment) {
793 f->flag_have_fragment = 0;
794 USB_IF_DEQUEUE(&f->free_q, m);
796 USB_IF_ENQUEUE(&f->used_q, m);
800 /* start write transfer, if not already started */
801 (f->methods->f_start_write) (f);
803 /* check if flushed already */
804 while (f->flag_flushing &&
805 (!f->flag_iserror)) {
806 /* wait until all data has been written */
807 f->flag_sleeping = 1;
808 err = cv_wait_sig(&f->cv_io, f->priv_mtx);
810 DPRINTF("signal received\n");
817 /* stop write transfer, if not already stopped */
818 (f->methods->f_stop_write) (f);
822 /* stop write transfer, if not already stopped */
823 (f->methods->f_stop_read) (f);
826 /* check if we are sleeping */
827 if (f->flag_sleeping) {
828 DPRINTFN(2, "Sleeping at close!\n");
830 mtx_unlock(f->priv_mtx);
832 /* call close method */
833 (f->methods->f_close) (f, fflags);
838 /*------------------------------------------------------------------------*
839 * usb_open - cdev callback
840 *------------------------------------------------------------------------*/
842 usb_open(struct cdev *dev, int fflags, int devtype, struct thread *td)
844 struct usb_fs_privdata* pd = (struct usb_fs_privdata*)dev->si_drv1;
845 struct usb_cdev_refdata refs;
846 struct usb_cdev_privdata *cpd;
849 DPRINTFN(2, "%s fflags=0x%08x\n", devtoname(dev), fflags);
851 KASSERT(fflags & (FREAD|FWRITE), ("invalid open flags"));
852 if (((fflags & FREAD) && !(pd->mode & FREAD)) ||
853 ((fflags & FWRITE) && !(pd->mode & FWRITE))) {
854 DPRINTFN(2, "access mode not supported\n");
858 cpd = malloc(sizeof(*cpd), M_USBDEV, M_WAITOK | M_ZERO);
859 ep = cpd->ep_addr = pd->ep_addr;
861 usb_loc_fill(pd, cpd);
862 err = usb_ref_device(cpd, &refs, 1);
864 DPRINTFN(2, "cannot ref device\n");
868 cpd->fflags = fflags; /* access mode for open lifetime */
870 /* create FIFOs, if any */
871 err = usb_fifo_create(cpd, &refs);
872 /* check for error */
874 DPRINTFN(2, "cannot create fifo\n");
875 usb_unref_device(cpd, &refs);
879 if (fflags & FREAD) {
880 err = usb_fifo_open(cpd, refs.rxfifo, fflags);
882 DPRINTFN(2, "read open failed\n");
883 usb_unref_device(cpd, &refs);
888 if (fflags & FWRITE) {
889 err = usb_fifo_open(cpd, refs.txfifo, fflags);
891 DPRINTFN(2, "write open failed\n");
892 if (fflags & FREAD) {
893 usb_fifo_close(refs.rxfifo, fflags);
895 usb_unref_device(cpd, &refs);
900 usb_unref_device(cpd, &refs);
901 devfs_set_cdevpriv(cpd, usb_close);
906 /*------------------------------------------------------------------------*
907 * usb_close - cdev callback
908 *------------------------------------------------------------------------*/
912 struct usb_cdev_refdata refs;
913 struct usb_cdev_privdata *cpd = arg;
916 DPRINTFN(2, "cpd=%p\n", cpd);
918 err = usb_ref_device(cpd, &refs, 0);
923 * If this function is not called directly from the root HUB
924 * thread, there is usually a need to lock the enumeration
927 if (!usbd_enum_is_locked(cpd->udev)) {
929 DPRINTFN(2, "Locking enumeration\n");
931 /* reference device */
932 err = usb_usb_ref_device(cpd, &refs);
936 if (cpd->fflags & FREAD) {
937 usb_fifo_close(refs.rxfifo, cpd->fflags);
939 if (cpd->fflags & FWRITE) {
940 usb_fifo_close(refs.txfifo, cpd->fflags);
942 usb_unref_device(cpd, &refs);
948 usb_dev_init(void *arg)
950 mtx_init(&usb_ref_lock, "USB ref mutex", NULL, MTX_DEF);
951 sx_init(&usb_sym_lock, "USB sym mutex");
952 TAILQ_INIT(&usb_sym_head);
954 /* check the UGEN methods */
955 usb_fifo_check_methods(&usb_ugen_methods);
958 SYSINIT(usb_dev_init, SI_SUB_KLD, SI_ORDER_FIRST, usb_dev_init, NULL);
961 usb_dev_init_post(void *arg)
964 * Create /dev/usb - this is needed for usbconfig(8), which
965 * needs a well-known device name to access.
967 usb_dev = make_dev(&usb_static_devsw, 0, UID_ROOT, GID_OPERATOR,
968 0644, USB_DEVICE_NAME);
969 if (usb_dev == NULL) {
970 DPRINTFN(0, "Could not create usb bus device\n");
974 SYSINIT(usb_dev_init_post, SI_SUB_KICK_SCHEDULER, SI_ORDER_FIRST, usb_dev_init_post, NULL);
977 usb_dev_uninit(void *arg)
979 if (usb_dev != NULL) {
980 destroy_dev(usb_dev);
983 mtx_destroy(&usb_ref_lock);
984 sx_destroy(&usb_sym_lock);
987 SYSUNINIT(usb_dev_uninit, SI_SUB_KICK_SCHEDULER, SI_ORDER_ANY, usb_dev_uninit, NULL);
990 usb_ioctl_f_sub(struct usb_fifo *f, u_long cmd, void *addr,
997 *(int *)addr = 0; /* character device */
1001 /* handled by upper FS layer */
1006 if (f->async_p != NULL) {
1010 f->async_p = USB_TD_GET_PROC(td);
1016 /* XXX this is not the most general solution */
1018 if (f->async_p == NULL) {
1022 if (*(int *)addr != USB_PROC_GET_GID(f->async_p)) {
1030 DPRINTFN(3, "cmd 0x%lx = %d\n", cmd, error);
1034 /*------------------------------------------------------------------------*
1035 * usb_ioctl - cdev callback
1036 *------------------------------------------------------------------------*/
1038 usb_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int fflag, struct thread* td)
1040 struct usb_cdev_refdata refs;
1041 struct usb_cdev_privdata* cpd;
1046 DPRINTFN(2, "cmd=0x%lx\n", cmd);
1048 err = devfs_get_cdevpriv((void **)&cpd);
1053 * Performance optimisation: We try to check for IOCTL's that
1054 * don't need the USB reference first. Then we grab the USB
1055 * reference if we need it!
1057 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1061 fflags = cpd->fflags;
1063 f = NULL; /* set default value */
1064 err = ENOIOCTL; /* set default value */
1066 if (fflags & FWRITE) {
1068 err = usb_ioctl_f_sub(f, cmd, addr, td);
1070 if (fflags & FREAD) {
1072 err = usb_ioctl_f_sub(f, cmd, addr, td);
1074 KASSERT(f != NULL, ("fifo not found"));
1075 if (err != ENOIOCTL)
1078 err = (f->methods->f_ioctl) (f, cmd, addr, fflags);
1080 DPRINTFN(2, "f_ioctl cmd 0x%lx = %d\n", cmd, err);
1082 if (err != ENOIOCTL)
1085 if (usb_usb_ref_device(cpd, &refs)) {
1090 err = (f->methods->f_ioctl_post) (f, cmd, addr, fflags);
1092 DPRINTFN(2, "f_ioctl_post cmd 0x%lx = %d\n", cmd, err);
1094 if (err == ENOIOCTL)
1100 /* Wait for re-enumeration, if any */
1102 while (f->udev->re_enumerate_wait != USB_RE_ENUM_DONE) {
1104 usb_unref_device(cpd, &refs);
1106 usb_pause_mtx(NULL, hz / 128);
1108 if (usb_ref_device(cpd, &refs, 1 /* need uref */)) {
1115 usb_unref_device(cpd, &refs);
1121 usb_poll(struct cdev* dev, int events, struct thread* td)
1123 struct usb_cdev_refdata refs;
1124 struct usb_cdev_privdata* cpd;
1127 int fflags, revents;
1129 if (devfs_get_cdevpriv((void **)&cpd) != 0 ||
1130 usb_ref_device(cpd, &refs, 0) != 0)
1132 (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM));
1134 fflags = cpd->fflags;
1136 /* Figure out who needs service */
1138 if ((events & (POLLOUT | POLLWRNORM)) &&
1139 (fflags & FWRITE)) {
1143 mtx_lock(f->priv_mtx);
1145 if (!refs.is_usbfs) {
1146 if (f->flag_iserror) {
1147 /* we got an error */
1150 if (f->queue_data == NULL) {
1152 * start write transfer, if not
1155 (f->methods->f_start_write) (f);
1157 /* check if any packets are available */
1158 USB_IF_POLL(&f->free_q, m);
1161 if (f->flag_iscomplete) {
1169 revents |= events & (POLLOUT | POLLWRNORM);
1171 f->flag_isselect = 1;
1172 selrecord(td, &f->selinfo);
1175 mtx_unlock(f->priv_mtx);
1177 if ((events & (POLLIN | POLLRDNORM)) &&
1182 mtx_lock(f->priv_mtx);
1184 if (!refs.is_usbfs) {
1185 if (f->flag_iserror) {
1186 /* we have and error */
1189 if (f->queue_data == NULL) {
1191 * start read transfer, if not
1194 (f->methods->f_start_read) (f);
1196 /* check if any packets are available */
1197 USB_IF_POLL(&f->used_q, m);
1200 if (f->flag_iscomplete) {
1208 revents |= events & (POLLIN | POLLRDNORM);
1210 f->flag_isselect = 1;
1211 selrecord(td, &f->selinfo);
1213 if (!refs.is_usbfs) {
1214 /* start reading data */
1215 (f->methods->f_start_read) (f);
1219 mtx_unlock(f->priv_mtx);
1221 usb_unref_device(cpd, &refs);
1226 usb_read(struct cdev *dev, struct uio *uio, int ioflag)
1228 struct usb_cdev_refdata refs;
1229 struct usb_cdev_privdata* cpd;
1236 uint8_t tr_data = 0;
1238 err = devfs_get_cdevpriv((void **)&cpd);
1242 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1246 fflags = cpd->fflags;
1250 /* should not happen */
1251 usb_unref_device(cpd, &refs);
1255 resid = uio->uio_resid;
1257 mtx_lock(f->priv_mtx);
1259 /* check for permanent read error */
1260 if (f->flag_iserror) {
1264 /* check if USB-FS interface is active */
1265 if (refs.is_usbfs) {
1267 * The queue is used for events that should be
1268 * retrieved using the "USB_FS_COMPLETE" ioctl.
1273 while (uio->uio_resid > 0) {
1275 USB_IF_DEQUEUE(&f->used_q, m);
1279 /* start read transfer, if not already started */
1281 (f->methods->f_start_read) (f);
1283 if (ioflag & IO_NDELAY) {
1285 /* return length before error */
1291 DPRINTF("sleeping\n");
1293 err = usb_fifo_wait(f);
1299 if (f->methods->f_filter_read) {
1301 * Sometimes it is convenient to process data at the
1302 * expense of a userland process instead of a kernel
1305 (f->methods->f_filter_read) (f, m);
1309 io_len = MIN(m->cur_data_len, uio->uio_resid);
1311 DPRINTFN(2, "transfer %d bytes from %p\n",
1312 io_len, m->cur_data_ptr);
1314 err = usb_fifo_uiomove(f,
1315 m->cur_data_ptr, io_len, uio);
1317 m->cur_data_len -= io_len;
1318 m->cur_data_ptr += io_len;
1320 if (m->cur_data_len == 0) {
1322 uint8_t last_packet;
1324 last_packet = m->last_packet;
1326 USB_IF_ENQUEUE(&f->free_q, m);
1333 USB_IF_PREPEND(&f->used_q, m);
1341 mtx_unlock(f->priv_mtx);
1343 usb_unref_device(cpd, &refs);
1349 usb_write(struct cdev *dev, struct uio *uio, int ioflag)
1351 struct usb_cdev_refdata refs;
1352 struct usb_cdev_privdata* cpd;
1360 uint8_t tr_data = 0;
1364 err = devfs_get_cdevpriv((void **)&cpd);
1368 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1372 fflags = cpd->fflags;
1376 /* should not happen */
1377 usb_unref_device(cpd, &refs);
1380 resid = uio->uio_resid;
1382 mtx_lock(f->priv_mtx);
1384 /* check for permanent write error */
1385 if (f->flag_iserror) {
1389 /* check if USB-FS interface is active */
1390 if (refs.is_usbfs) {
1392 * The queue is used for events that should be
1393 * retrieved using the "USB_FS_COMPLETE" ioctl.
1398 if (f->queue_data == NULL) {
1399 /* start write transfer, if not already started */
1400 (f->methods->f_start_write) (f);
1402 /* we allow writing zero length data */
1404 USB_IF_DEQUEUE(&f->free_q, m);
1408 if (ioflag & IO_NDELAY) {
1410 /* return length before error */
1416 DPRINTF("sleeping\n");
1418 err = usb_fifo_wait(f);
1426 if (f->flag_have_fragment == 0) {
1428 io_len = m->cur_data_len;
1429 pdata = m->cur_data_ptr;
1430 if (io_len > uio->uio_resid)
1431 io_len = uio->uio_resid;
1432 m->cur_data_len = io_len;
1434 io_len = m->max_data_len - m->cur_data_len;
1435 pdata = m->cur_data_ptr + m->cur_data_len;
1436 if (io_len > uio->uio_resid)
1437 io_len = uio->uio_resid;
1438 m->cur_data_len += io_len;
1441 DPRINTFN(2, "transfer %d bytes to %p\n",
1444 err = usb_fifo_uiomove(f, pdata, io_len, uio);
1447 f->flag_have_fragment = 0;
1448 USB_IF_ENQUEUE(&f->free_q, m);
1452 /* check if the buffer is ready to be transmitted */
1454 if ((f->flag_write_defrag == 0) ||
1455 (m->cur_data_len == m->max_data_len)) {
1456 f->flag_have_fragment = 0;
1459 * Check for write filter:
1461 * Sometimes it is convenient to process data
1462 * at the expense of a userland process
1463 * instead of a kernel process.
1465 if (f->methods->f_filter_write) {
1466 (f->methods->f_filter_write) (f, m);
1469 /* Put USB mbuf in the used queue */
1470 USB_IF_ENQUEUE(&f->used_q, m);
1472 /* Start writing data, if not already started */
1473 (f->methods->f_start_write) (f);
1475 /* Wait for more data or close */
1476 f->flag_have_fragment = 1;
1477 USB_IF_PREPEND(&f->free_q, m);
1480 } while (uio->uio_resid > 0);
1482 mtx_unlock(f->priv_mtx);
1484 usb_unref_device(cpd, &refs);
1490 usb_static_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
1494 struct usb_read_dir *urd;
1502 err = usb_read_symlink(u.urd->urd_data,
1503 u.urd->urd_startentry, u.urd->urd_maxlen);
1505 case USB_DEV_QUIRK_GET:
1506 case USB_QUIRK_NAME_GET:
1507 case USB_DEV_QUIRK_ADD:
1508 case USB_DEV_QUIRK_REMOVE:
1509 err = usb_quirk_ioctl_p(cmd, data, fflag, td);
1511 case USB_GET_TEMPLATE:
1512 *(int *)data = usb_template;
1515 case USB_SET_TEMPLATE:
1516 err = priv_check(curthread, PRIV_DRIVER);
1519 usb_template = *(int *)data;
1529 usb_fifo_uiomove(struct usb_fifo *f, void *cp,
1530 int n, struct uio *uio)
1534 mtx_unlock(f->priv_mtx);
1537 * "uiomove()" can sleep so one needs to make a wrapper,
1538 * exiting the mutex and checking things:
1540 error = uiomove(cp, n, uio);
1542 mtx_lock(f->priv_mtx);
1548 usb_fifo_wait(struct usb_fifo *f)
1552 mtx_assert(f->priv_mtx, MA_OWNED);
1554 if (f->flag_iserror) {
1558 f->flag_sleeping = 1;
1560 err = cv_wait_sig(&f->cv_io, f->priv_mtx);
1562 if (f->flag_iserror) {
1570 usb_fifo_signal(struct usb_fifo *f)
1572 if (f->flag_sleeping) {
1573 f->flag_sleeping = 0;
1574 cv_broadcast(&f->cv_io);
1579 usb_fifo_wakeup(struct usb_fifo *f)
1583 if (f->flag_isselect) {
1584 selwakeup(&f->selinfo);
1585 f->flag_isselect = 0;
1587 if (f->async_p != NULL) {
1588 PROC_LOCK(f->async_p);
1589 kern_psignal(f->async_p, SIGIO);
1590 PROC_UNLOCK(f->async_p);
1595 usb_fifo_dummy_open(struct usb_fifo *fifo, int fflags)
1601 usb_fifo_dummy_close(struct usb_fifo *fifo, int fflags)
1607 usb_fifo_dummy_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
1613 usb_fifo_dummy_cmd(struct usb_fifo *fifo)
1615 fifo->flag_flushing = 0; /* not flushing */
1619 usb_fifo_check_methods(struct usb_fifo_methods *pm)
1621 /* check that all callback functions are OK */
1623 if (pm->f_open == NULL)
1624 pm->f_open = &usb_fifo_dummy_open;
1626 if (pm->f_close == NULL)
1627 pm->f_close = &usb_fifo_dummy_close;
1629 if (pm->f_ioctl == NULL)
1630 pm->f_ioctl = &usb_fifo_dummy_ioctl;
1632 if (pm->f_ioctl_post == NULL)
1633 pm->f_ioctl_post = &usb_fifo_dummy_ioctl;
1635 if (pm->f_start_read == NULL)
1636 pm->f_start_read = &usb_fifo_dummy_cmd;
1638 if (pm->f_stop_read == NULL)
1639 pm->f_stop_read = &usb_fifo_dummy_cmd;
1641 if (pm->f_start_write == NULL)
1642 pm->f_start_write = &usb_fifo_dummy_cmd;
1644 if (pm->f_stop_write == NULL)
1645 pm->f_stop_write = &usb_fifo_dummy_cmd;
1648 /*------------------------------------------------------------------------*
1651 * The following function will create a duplex FIFO.
1656 *------------------------------------------------------------------------*/
1658 usb_fifo_attach(struct usb_device *udev, void *priv_sc,
1659 struct mtx *priv_mtx, struct usb_fifo_methods *pm,
1660 struct usb_fifo_sc *f_sc, uint16_t unit, int16_t subunit,
1661 uint8_t iface_index, uid_t uid, gid_t gid, int mode)
1663 struct usb_fifo *f_tx;
1664 struct usb_fifo *f_rx;
1668 f_sc->fp[USB_FIFO_TX] = NULL;
1669 f_sc->fp[USB_FIFO_RX] = NULL;
1674 /* check the methods */
1675 usb_fifo_check_methods(pm);
1677 if (priv_mtx == NULL)
1680 /* search for a free FIFO slot */
1681 for (n = 0;; n += 2) {
1683 if (n == USB_FIFO_MAX) {
1684 /* end of FIFOs reached */
1687 /* Check for TX FIFO */
1688 if (udev->fifo[n + USB_FIFO_TX] != NULL) {
1691 /* Check for RX FIFO */
1692 if (udev->fifo[n + USB_FIFO_RX] != NULL) {
1698 f_tx = usb_fifo_alloc();
1699 f_rx = usb_fifo_alloc();
1701 if ((f_tx == NULL) || (f_rx == NULL)) {
1702 usb_fifo_free(f_tx);
1703 usb_fifo_free(f_rx);
1706 /* initialise FIFO structures */
1708 f_tx->fifo_index = n + USB_FIFO_TX;
1709 f_tx->dev_ep_index = -1;
1710 f_tx->priv_mtx = priv_mtx;
1711 f_tx->priv_sc0 = priv_sc;
1713 f_tx->iface_index = iface_index;
1716 f_rx->fifo_index = n + USB_FIFO_RX;
1717 f_rx->dev_ep_index = -1;
1718 f_rx->priv_mtx = priv_mtx;
1719 f_rx->priv_sc0 = priv_sc;
1721 f_rx->iface_index = iface_index;
1724 f_sc->fp[USB_FIFO_TX] = f_tx;
1725 f_sc->fp[USB_FIFO_RX] = f_rx;
1727 mtx_lock(&usb_ref_lock);
1728 udev->fifo[f_tx->fifo_index] = f_tx;
1729 udev->fifo[f_rx->fifo_index] = f_rx;
1730 mtx_unlock(&usb_ref_lock);
1732 for (n = 0; n != 4; n++) {
1734 if (pm->basename[n] == NULL) {
1738 if (snprintf(devname, sizeof(devname),
1739 "%s%u%s", pm->basename[n],
1740 unit, pm->postfix[n] ?
1741 pm->postfix[n] : "")) {
1745 if (snprintf(devname, sizeof(devname),
1746 "%s%u.%d%s", pm->basename[n],
1747 unit, subunit, pm->postfix[n] ?
1748 pm->postfix[n] : "")) {
1754 * Distribute the symbolic links into two FIFO structures:
1757 f_rx->symlink[n / 2] =
1758 usb_alloc_symlink(devname);
1760 f_tx->symlink[n / 2] =
1761 usb_alloc_symlink(devname);
1764 /* Create the device */
1765 f_sc->dev = usb_make_dev(udev, devname, -1,
1766 f_tx->fifo_index & f_rx->fifo_index,
1767 FREAD|FWRITE, uid, gid, mode);
1770 DPRINTFN(2, "attached %p/%p\n", f_tx, f_rx);
1774 /*------------------------------------------------------------------------*
1775 * usb_fifo_alloc_buffer
1780 *------------------------------------------------------------------------*/
1782 usb_fifo_alloc_buffer(struct usb_fifo *f, usb_size_t bufsize,
1785 usb_fifo_free_buffer(f);
1787 /* allocate an endpoint */
1788 f->free_q.ifq_maxlen = nbuf;
1789 f->used_q.ifq_maxlen = nbuf;
1791 f->queue_data = usb_alloc_mbufs(
1792 M_USBDEV, &f->free_q, bufsize, nbuf);
1794 if ((f->queue_data == NULL) && bufsize && nbuf) {
1797 return (0); /* success */
1800 /*------------------------------------------------------------------------*
1801 * usb_fifo_free_buffer
1803 * This function will free the buffers associated with a FIFO. This
1804 * function can be called multiple times in a row.
1805 *------------------------------------------------------------------------*/
1807 usb_fifo_free_buffer(struct usb_fifo *f)
1809 if (f->queue_data) {
1810 /* free old buffer */
1811 free(f->queue_data, M_USBDEV);
1812 f->queue_data = NULL;
1816 memset(&f->free_q, 0, sizeof(f->free_q));
1817 memset(&f->used_q, 0, sizeof(f->used_q));
1821 usb_fifo_detach(struct usb_fifo_sc *f_sc)
1826 usb_fifo_free(f_sc->fp[USB_FIFO_TX]);
1827 usb_fifo_free(f_sc->fp[USB_FIFO_RX]);
1829 f_sc->fp[USB_FIFO_TX] = NULL;
1830 f_sc->fp[USB_FIFO_RX] = NULL;
1832 usb_destroy_dev(f_sc->dev);
1836 DPRINTFN(2, "detached %p\n", f_sc);
1840 usb_fifo_put_bytes_max(struct usb_fifo *f)
1845 USB_IF_POLL(&f->free_q, m);
1848 len = m->max_data_len;
1855 /*------------------------------------------------------------------------*
1859 * 0 - normal operation
1860 * 1 - set last packet flag to enforce framing
1861 *------------------------------------------------------------------------*/
1863 usb_fifo_put_data(struct usb_fifo *f, struct usb_page_cache *pc,
1864 usb_frlength_t offset, usb_frlength_t len, uint8_t what)
1867 usb_frlength_t io_len;
1869 while (len || (what == 1)) {
1871 USB_IF_DEQUEUE(&f->free_q, m);
1876 io_len = MIN(len, m->cur_data_len);
1878 usbd_copy_out(pc, offset, m->cur_data_ptr, io_len);
1880 m->cur_data_len = io_len;
1884 if ((len == 0) && (what == 1)) {
1887 USB_IF_ENQUEUE(&f->used_q, m);
1891 if ((len == 0) || (what == 1)) {
1901 usb_fifo_put_data_linear(struct usb_fifo *f, void *ptr,
1902 usb_size_t len, uint8_t what)
1907 while (len || (what == 1)) {
1909 USB_IF_DEQUEUE(&f->free_q, m);
1914 io_len = MIN(len, m->cur_data_len);
1916 memcpy(m->cur_data_ptr, ptr, io_len);
1918 m->cur_data_len = io_len;
1919 ptr = USB_ADD_BYTES(ptr, io_len);
1922 if ((len == 0) && (what == 1)) {
1925 USB_IF_ENQUEUE(&f->used_q, m);
1929 if ((len == 0) || (what == 1)) {
1939 usb_fifo_put_data_buffer(struct usb_fifo *f, void *ptr, usb_size_t len)
1943 USB_IF_DEQUEUE(&f->free_q, m);
1946 m->cur_data_len = len;
1947 m->cur_data_ptr = ptr;
1948 USB_IF_ENQUEUE(&f->used_q, m);
1956 usb_fifo_put_data_error(struct usb_fifo *f)
1958 f->flag_iserror = 1;
1962 /*------------------------------------------------------------------------*
1966 * 0 - normal operation
1967 * 1 - only get one "usb_mbuf"
1971 * 1 - data in buffer
1972 *------------------------------------------------------------------------*/
1974 usb_fifo_get_data(struct usb_fifo *f, struct usb_page_cache *pc,
1975 usb_frlength_t offset, usb_frlength_t len, usb_frlength_t *actlen,
1979 usb_frlength_t io_len;
1980 uint8_t tr_data = 0;
1986 USB_IF_DEQUEUE(&f->used_q, m);
1992 io_len = MIN(len, m->cur_data_len);
1994 usbd_copy_in(pc, offset, m->cur_data_ptr, io_len);
1998 actlen[0] += io_len;
1999 m->cur_data_ptr += io_len;
2000 m->cur_data_len -= io_len;
2002 if ((m->cur_data_len == 0) || (what == 1)) {
2003 USB_IF_ENQUEUE(&f->free_q, m);
2011 USB_IF_PREPEND(&f->used_q, m);
2016 /* wait for data to be written out */
2019 if (f->flag_flushing) {
2020 /* check if we should send a short packet */
2021 if (f->flag_short != 0) {
2026 /* flushing complete */
2027 f->flag_flushing = 0;
2040 usb_fifo_get_data_linear(struct usb_fifo *f, void *ptr,
2041 usb_size_t len, usb_size_t *actlen, uint8_t what)
2045 uint8_t tr_data = 0;
2051 USB_IF_DEQUEUE(&f->used_q, m);
2057 io_len = MIN(len, m->cur_data_len);
2059 memcpy(ptr, m->cur_data_ptr, io_len);
2062 ptr = USB_ADD_BYTES(ptr, io_len);
2063 actlen[0] += io_len;
2064 m->cur_data_ptr += io_len;
2065 m->cur_data_len -= io_len;
2067 if ((m->cur_data_len == 0) || (what == 1)) {
2068 USB_IF_ENQUEUE(&f->free_q, m);
2076 USB_IF_PREPEND(&f->used_q, m);
2081 /* wait for data to be written out */
2084 if (f->flag_flushing) {
2085 /* check if we should send a short packet */
2086 if (f->flag_short != 0) {
2091 /* flushing complete */
2092 f->flag_flushing = 0;
2105 usb_fifo_get_data_buffer(struct usb_fifo *f, void **pptr, usb_size_t *plen)
2109 USB_IF_POLL(&f->used_q, m);
2112 *plen = m->cur_data_len;
2113 *pptr = m->cur_data_ptr;
2121 usb_fifo_get_data_error(struct usb_fifo *f)
2123 f->flag_iserror = 1;
2127 /*------------------------------------------------------------------------*
2132 * Else: Pointer to symlink entry
2133 *------------------------------------------------------------------------*/
2134 struct usb_symlink *
2135 usb_alloc_symlink(const char *target)
2137 struct usb_symlink *ps;
2139 ps = malloc(sizeof(*ps), M_USBDEV, M_WAITOK);
2143 /* XXX no longer needed */
2144 strlcpy(ps->src_path, target, sizeof(ps->src_path));
2145 ps->src_len = strlen(ps->src_path);
2146 strlcpy(ps->dst_path, target, sizeof(ps->dst_path));
2147 ps->dst_len = strlen(ps->dst_path);
2149 sx_xlock(&usb_sym_lock);
2150 TAILQ_INSERT_TAIL(&usb_sym_head, ps, sym_entry);
2151 sx_unlock(&usb_sym_lock);
2155 /*------------------------------------------------------------------------*
2157 *------------------------------------------------------------------------*/
2159 usb_free_symlink(struct usb_symlink *ps)
2164 sx_xlock(&usb_sym_lock);
2165 TAILQ_REMOVE(&usb_sym_head, ps, sym_entry);
2166 sx_unlock(&usb_sym_lock);
2171 /*------------------------------------------------------------------------*
2177 *------------------------------------------------------------------------*/
2179 usb_read_symlink(uint8_t *user_ptr, uint32_t startentry, uint32_t user_len)
2181 struct usb_symlink *ps;
2187 sx_xlock(&usb_sym_lock);
2189 TAILQ_FOREACH(ps, &usb_sym_head, sym_entry) {
2192 * Compute total length of source and destination symlink
2193 * strings pluss one length byte and two NUL bytes:
2195 temp = ps->src_len + ps->dst_len + 3;
2199 * Skip entry because this length cannot fit
2204 if (startentry != 0) {
2205 /* decrement read offset */
2209 if (temp > user_len) {
2210 /* out of buffer space */
2215 /* copy out total length */
2217 error = copyout(&len,
2218 USB_ADD_BYTES(user_ptr, delta), 1);
2224 /* copy out source string */
2226 error = copyout(ps->src_path,
2227 USB_ADD_BYTES(user_ptr, delta), ps->src_len);
2232 delta += ps->src_len;
2233 error = copyout(&len,
2234 USB_ADD_BYTES(user_ptr, delta), 1);
2240 /* copy out destination string */
2242 error = copyout(ps->dst_path,
2243 USB_ADD_BYTES(user_ptr, delta), ps->dst_len);
2248 delta += ps->dst_len;
2249 error = copyout(&len,
2250 USB_ADD_BYTES(user_ptr, delta), 1);
2259 /* a zero length entry indicates the end */
2261 if ((user_len != 0) && (error == 0)) {
2265 error = copyout(&len,
2266 USB_ADD_BYTES(user_ptr, delta), 1);
2268 sx_unlock(&usb_sym_lock);
2273 usb_fifo_set_close_zlp(struct usb_fifo *f, uint8_t onoff)
2278 /* send a Zero Length Packet, ZLP, before close */
2279 f->flag_short = onoff;
2283 usb_fifo_set_write_defrag(struct usb_fifo *f, uint8_t onoff)
2288 /* defrag written data */
2289 f->flag_write_defrag = onoff;
2290 /* reset defrag state */
2291 f->flag_have_fragment = 0;
2295 usb_fifo_softc(struct usb_fifo *f)
2297 return (f->priv_sc0);
2299 #endif /* USB_HAVE_UGEN */