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->state == USB_STATE_DETACHED &&
212 DPRINTFN(2, "device is detached\n");
215 if (cpd->udev->refcount == USB_DEV_REF_MAX) {
216 DPRINTFN(2, "no dev ref\n");
220 DPRINTFN(2, "ref udev - needed\n");
221 cpd->udev->refcount++;
223 mtx_unlock(&usb_ref_lock);
226 * We need to grab the enumeration SX-lock before
227 * grabbing the FIFO refs to avoid deadlock at detach!
229 crd->do_unlock = usbd_enum_lock(cpd->udev);
231 mtx_lock(&usb_ref_lock);
234 * Set "is_uref" after grabbing the default SX lock
239 /* check if we are doing an open */
240 if (cpd->fflags == 0) {
241 /* use zero defaults */
243 /* check for write */
244 if (cpd->fflags & FWRITE) {
245 ppf = cpd->udev->fifo;
246 f = ppf[cpd->fifo_index + USB_FIFO_TX];
248 crd->is_write = 1; /* ref */
249 if (f == NULL || f->refcount == USB_FIFO_REF_MAX)
251 if (f->curr_cpd != cpd)
253 /* check if USB-FS is active */
254 if (f->fs_ep_max != 0) {
260 if (cpd->fflags & FREAD) {
261 ppf = cpd->udev->fifo;
262 f = ppf[cpd->fifo_index + USB_FIFO_RX];
264 crd->is_read = 1; /* ref */
265 if (f == NULL || f->refcount == USB_FIFO_REF_MAX)
267 if (f->curr_cpd != cpd)
269 /* check if USB-FS is active */
270 if (f->fs_ep_max != 0) {
276 /* when everything is OK we increment the refcounts */
278 DPRINTFN(2, "ref write\n");
279 crd->txfifo->refcount++;
282 DPRINTFN(2, "ref read\n");
283 crd->rxfifo->refcount++;
285 mtx_unlock(&usb_ref_lock);
291 usbd_enum_unlock(cpd->udev);
294 if (--(cpd->udev->refcount) == 0) {
295 cv_signal(&cpd->udev->ref_cv);
298 mtx_unlock(&usb_ref_lock);
299 DPRINTFN(2, "fail\n");
300 return (USB_ERR_INVAL);
303 /*------------------------------------------------------------------------*
306 * This function is used to upgrade an USB reference to include the
307 * USB device reference on a USB location.
310 * 0: Success, refcount incremented on the given USB device.
312 *------------------------------------------------------------------------*/
314 usb_usb_ref_device(struct usb_cdev_privdata *cpd,
315 struct usb_cdev_refdata *crd)
318 * Check if we already got an USB reference on this location:
321 return (0); /* success */
324 * To avoid deadlock at detach we need to drop the FIFO ref
325 * and re-acquire a new ref!
327 usb_unref_device(cpd, crd);
329 return (usb_ref_device(cpd, crd, 1 /* need uref */));
332 /*------------------------------------------------------------------------*
335 * This function will release the reference count by one unit for the
337 *------------------------------------------------------------------------*/
339 usb_unref_device(struct usb_cdev_privdata *cpd,
340 struct usb_cdev_refdata *crd)
343 DPRINTFN(2, "cpd=%p is_uref=%d\n", cpd, crd->is_uref);
346 usbd_enum_unlock(cpd->udev);
348 mtx_lock(&usb_ref_lock);
350 if (--(crd->rxfifo->refcount) == 0) {
351 cv_signal(&crd->rxfifo->cv_drain);
356 if (--(crd->txfifo->refcount) == 0) {
357 cv_signal(&crd->txfifo->cv_drain);
362 if (--(cpd->udev->refcount) == 0) {
363 cv_signal(&cpd->udev->ref_cv);
367 mtx_unlock(&usb_ref_lock);
370 static struct usb_fifo *
375 f = malloc(sizeof(*f), M_USBDEV, M_WAITOK | M_ZERO);
377 cv_init(&f->cv_io, "FIFO-IO");
378 cv_init(&f->cv_drain, "FIFO-DRAIN");
384 /*------------------------------------------------------------------------*
386 *------------------------------------------------------------------------*/
388 usb_fifo_create(struct usb_cdev_privdata *cpd,
389 struct usb_cdev_refdata *crd)
391 struct usb_device *udev = cpd->udev;
393 struct usb_endpoint *ep;
399 int e = cpd->ep_addr;
401 is_tx = (cpd->fflags & FWRITE) ? 1 : 0;
402 is_rx = (cpd->fflags & FREAD) ? 1 : 0;
406 /* Preallocated FIFO */
408 DPRINTFN(5, "Preallocated FIFO\n");
410 f = udev->fifo[cpd->fifo_index + USB_FIFO_TX];
416 f = udev->fifo[cpd->fifo_index + USB_FIFO_RX];
424 KASSERT(e >= 0 && e <= 15, ("endpoint %d out of range", e));
426 /* search for a free FIFO slot */
427 DPRINTFN(5, "Endpoint device, searching for 0x%02x\n", e);
428 for (n = 0;; n += 2) {
430 if (n == USB_FIFO_MAX) {
435 /* end of FIFOs reached */
436 DPRINTFN(5, "out of FIFOs\n");
440 /* Check for TX FIFO */
442 f = udev->fifo[n + USB_FIFO_TX];
444 if (f->dev_ep_index != e) {
445 /* wrong endpoint index */
448 if (f->curr_cpd != NULL) {
453 } else if (no_null) {
457 /* Check for RX FIFO */
459 f = udev->fifo[n + USB_FIFO_RX];
461 if (f->dev_ep_index != e) {
462 /* wrong endpoint index */
465 if (f->curr_cpd != NULL) {
470 } else if (no_null) {
478 if (e >= (USB_EP_MAX / 2)) {
479 /* we don't create any endpoints in this range */
480 DPRINTFN(5, "ep out of range\n");
481 return (is_busy ? EBUSY : EINVAL);
485 if ((e != 0) && is_busy) {
487 * Only the default control endpoint is allowed to be
488 * opened multiple times!
490 DPRINTFN(5, "busy\n");
496 (udev->fifo[n + USB_FIFO_TX] == NULL)) {
497 ep = usb_dev_get_ep(udev, e, USB_FIFO_TX);
498 DPRINTFN(5, "dev_get_endpoint(%d, 0x%x)\n", e, USB_FIFO_TX);
500 DPRINTFN(5, "dev_get_endpoint returned NULL\n");
503 f = usb_fifo_alloc();
505 DPRINTFN(5, "could not alloc tx fifo\n");
508 /* update some fields */
509 f->fifo_index = n + USB_FIFO_TX;
511 f->priv_mtx = &udev->device_mtx;
513 f->methods = &usb_ugen_methods;
514 f->iface_index = ep->iface_index;
516 mtx_lock(&usb_ref_lock);
517 udev->fifo[n + USB_FIFO_TX] = f;
518 mtx_unlock(&usb_ref_lock);
522 (udev->fifo[n + USB_FIFO_RX] == NULL)) {
524 ep = usb_dev_get_ep(udev, e, USB_FIFO_RX);
525 DPRINTFN(5, "dev_get_endpoint(%d, 0x%x)\n", e, USB_FIFO_RX);
527 DPRINTFN(5, "dev_get_endpoint returned NULL\n");
530 f = usb_fifo_alloc();
532 DPRINTFN(5, "could not alloc rx fifo\n");
535 /* update some fields */
536 f->fifo_index = n + USB_FIFO_RX;
538 f->priv_mtx = &udev->device_mtx;
540 f->methods = &usb_ugen_methods;
541 f->iface_index = ep->iface_index;
543 mtx_lock(&usb_ref_lock);
544 udev->fifo[n + USB_FIFO_RX] = f;
545 mtx_unlock(&usb_ref_lock);
548 crd->txfifo = udev->fifo[n + USB_FIFO_TX];
551 crd->rxfifo = udev->fifo[n + USB_FIFO_RX];
553 /* fill out fifo index */
554 DPRINTFN(5, "fifo index = %d\n", n);
563 usb_fifo_free(struct usb_fifo *f)
571 /* destroy symlink devices, if any */
572 for (n = 0; n != 2; n++) {
574 usb_free_symlink(f->symlink[n]);
575 f->symlink[n] = NULL;
578 mtx_lock(&usb_ref_lock);
580 /* delink ourselves to stop calls from userland */
581 if ((f->fifo_index < USB_FIFO_MAX) &&
583 (f->udev->fifo[f->fifo_index] == f)) {
584 f->udev->fifo[f->fifo_index] = NULL;
586 DPRINTFN(0, "USB FIFO %p has not been linked\n", f);
589 /* decrease refcount */
591 /* prevent any write flush */
593 /* need to wait until all callers have exited */
594 while (f->refcount != 0) {
595 mtx_unlock(&usb_ref_lock); /* avoid LOR */
596 mtx_lock(f->priv_mtx);
597 /* get I/O thread out of any sleep state */
598 if (f->flag_sleeping) {
599 f->flag_sleeping = 0;
600 cv_broadcast(&f->cv_io);
602 mtx_unlock(f->priv_mtx);
603 mtx_lock(&usb_ref_lock);
606 * Check if the "f->refcount" variable reached zero
607 * during the unlocked time before entering wait:
609 if (f->refcount == 0)
613 cv_wait(&f->cv_drain, &usb_ref_lock);
615 mtx_unlock(&usb_ref_lock);
617 /* take care of closing the device here, if any */
618 usb_fifo_close(f, 0);
620 cv_destroy(&f->cv_io);
621 cv_destroy(&f->cv_drain);
626 static struct usb_endpoint *
627 usb_dev_get_ep(struct usb_device *udev, uint8_t ep_index, uint8_t dir)
629 struct usb_endpoint *ep;
635 if (dir == USB_FIFO_RX) {
636 if (udev->flags.usb_mode == USB_MODE_HOST) {
642 if (udev->flags.usb_mode == USB_MODE_HOST) {
648 ep = usbd_get_ep_by_addr(udev, ep_index | ep_dir);
652 /* if the endpoint does not exist then return */
655 if (ep->edesc == NULL) {
656 /* invalid endpoint */
659 return (ep); /* success */
662 /*------------------------------------------------------------------------*
668 *------------------------------------------------------------------------*/
670 usb_fifo_open(struct usb_cdev_privdata *cpd,
671 struct usb_fifo *f, int fflags)
677 DPRINTFN(2, "no FIFO\n");
680 /* remove FWRITE and FREAD flags */
681 fflags &= ~(FWRITE | FREAD);
683 /* set correct file flags */
684 if ((f->fifo_index & 1) == USB_FIFO_TX) {
690 /* check if we are already opened */
691 /* we don't need any locks when checking this variable */
692 if (f->curr_cpd != NULL) {
697 /* reset short flag before open */
700 /* call open method */
701 err = (f->methods->f_open) (f, fflags);
705 mtx_lock(f->priv_mtx);
707 /* reset sleep flag */
708 f->flag_sleeping = 0;
710 /* reset error flag */
713 /* reset complete flag */
714 f->flag_iscomplete = 0;
716 /* reset select flag */
717 f->flag_isselect = 0;
719 /* reset flushing flag */
720 f->flag_flushing = 0;
722 /* reset ASYNC proc flag */
725 mtx_lock(&usb_ref_lock);
726 /* flag the fifo as opened to prevent others */
728 mtx_unlock(&usb_ref_lock);
733 mtx_unlock(f->priv_mtx);
738 /*------------------------------------------------------------------------*
740 *------------------------------------------------------------------------*/
742 usb_fifo_reset(struct usb_fifo *f)
750 USB_IF_DEQUEUE(&f->used_q, m);
752 USB_IF_ENQUEUE(&f->free_q, m);
757 /* reset have fragment flag */
758 f->flag_have_fragment = 0;
761 /*------------------------------------------------------------------------*
763 *------------------------------------------------------------------------*/
765 usb_fifo_close(struct usb_fifo *f, int fflags)
769 /* check if we are not opened */
770 if (f->curr_cpd == NULL) {
771 /* nothing to do - already closed */
774 mtx_lock(f->priv_mtx);
776 /* clear current cdev private data pointer */
779 /* check if we are selected */
780 if (f->flag_isselect) {
781 selwakeup(&f->selinfo);
782 f->flag_isselect = 0;
784 /* check if a thread wants SIGIO */
785 if (f->async_p != NULL) {
786 PROC_LOCK(f->async_p);
787 kern_psignal(f->async_p, SIGIO);
788 PROC_UNLOCK(f->async_p);
791 /* remove FWRITE and FREAD flags */
792 fflags &= ~(FWRITE | FREAD);
794 /* flush written data, if any */
795 if ((f->fifo_index & 1) == USB_FIFO_TX) {
797 if (!f->flag_iserror) {
799 /* set flushing flag */
800 f->flag_flushing = 1;
802 /* get the last packet in */
803 if (f->flag_have_fragment) {
805 f->flag_have_fragment = 0;
806 USB_IF_DEQUEUE(&f->free_q, m);
808 USB_IF_ENQUEUE(&f->used_q, m);
812 /* start write transfer, if not already started */
813 (f->methods->f_start_write) (f);
815 /* check if flushed already */
816 while (f->flag_flushing &&
817 (!f->flag_iserror)) {
818 /* wait until all data has been written */
819 f->flag_sleeping = 1;
820 err = cv_wait_sig(&f->cv_io, f->priv_mtx);
822 DPRINTF("signal received\n");
829 /* stop write transfer, if not already stopped */
830 (f->methods->f_stop_write) (f);
834 /* stop write transfer, if not already stopped */
835 (f->methods->f_stop_read) (f);
838 /* check if we are sleeping */
839 if (f->flag_sleeping) {
840 DPRINTFN(2, "Sleeping at close!\n");
842 mtx_unlock(f->priv_mtx);
844 /* call close method */
845 (f->methods->f_close) (f, fflags);
850 /*------------------------------------------------------------------------*
851 * usb_open - cdev callback
852 *------------------------------------------------------------------------*/
854 usb_open(struct cdev *dev, int fflags, int devtype, struct thread *td)
856 struct usb_fs_privdata* pd = (struct usb_fs_privdata*)dev->si_drv1;
857 struct usb_cdev_refdata refs;
858 struct usb_cdev_privdata *cpd;
861 DPRINTFN(2, "%s fflags=0x%08x\n", devtoname(dev), fflags);
863 KASSERT(fflags & (FREAD|FWRITE), ("invalid open flags"));
864 if (((fflags & FREAD) && !(pd->mode & FREAD)) ||
865 ((fflags & FWRITE) && !(pd->mode & FWRITE))) {
866 DPRINTFN(2, "access mode not supported\n");
870 cpd = malloc(sizeof(*cpd), M_USBDEV, M_WAITOK | M_ZERO);
871 ep = cpd->ep_addr = pd->ep_addr;
873 usb_loc_fill(pd, cpd);
874 err = usb_ref_device(cpd, &refs, 1);
876 DPRINTFN(2, "cannot ref device\n");
880 cpd->fflags = fflags; /* access mode for open lifetime */
882 /* create FIFOs, if any */
883 err = usb_fifo_create(cpd, &refs);
884 /* check for error */
886 DPRINTFN(2, "cannot create fifo\n");
887 usb_unref_device(cpd, &refs);
891 if (fflags & FREAD) {
892 err = usb_fifo_open(cpd, refs.rxfifo, fflags);
894 DPRINTFN(2, "read open failed\n");
895 usb_unref_device(cpd, &refs);
900 if (fflags & FWRITE) {
901 err = usb_fifo_open(cpd, refs.txfifo, fflags);
903 DPRINTFN(2, "write open failed\n");
904 if (fflags & FREAD) {
905 usb_fifo_close(refs.rxfifo, fflags);
907 usb_unref_device(cpd, &refs);
912 usb_unref_device(cpd, &refs);
913 devfs_set_cdevpriv(cpd, usb_close);
918 /*------------------------------------------------------------------------*
919 * usb_close - cdev callback
920 *------------------------------------------------------------------------*/
924 struct usb_cdev_refdata refs;
925 struct usb_cdev_privdata *cpd = arg;
928 DPRINTFN(2, "cpd=%p\n", cpd);
930 err = usb_ref_device(cpd, &refs,
931 2 /* uref and allow detached state */);
933 DPRINTFN(2, "Cannot grab USB reference when "
934 "closing USB file handle\n");
937 if (cpd->fflags & FREAD) {
938 usb_fifo_close(refs.rxfifo, cpd->fflags);
940 if (cpd->fflags & FWRITE) {
941 usb_fifo_close(refs.txfifo, cpd->fflags);
943 usb_unref_device(cpd, &refs);
949 usb_dev_init(void *arg)
951 mtx_init(&usb_ref_lock, "USB ref mutex", NULL, MTX_DEF);
952 sx_init(&usb_sym_lock, "USB sym mutex");
953 TAILQ_INIT(&usb_sym_head);
955 /* check the UGEN methods */
956 usb_fifo_check_methods(&usb_ugen_methods);
959 SYSINIT(usb_dev_init, SI_SUB_KLD, SI_ORDER_FIRST, usb_dev_init, NULL);
962 usb_dev_init_post(void *arg)
965 * Create /dev/usb - this is needed for usbconfig(8), which
966 * needs a well-known device name to access.
968 usb_dev = make_dev(&usb_static_devsw, 0, UID_ROOT, GID_OPERATOR,
969 0644, USB_DEVICE_NAME);
970 if (usb_dev == NULL) {
971 DPRINTFN(0, "Could not create usb bus device\n");
975 SYSINIT(usb_dev_init_post, SI_SUB_KICK_SCHEDULER, SI_ORDER_FIRST, usb_dev_init_post, NULL);
978 usb_dev_uninit(void *arg)
980 if (usb_dev != NULL) {
981 destroy_dev(usb_dev);
984 mtx_destroy(&usb_ref_lock);
985 sx_destroy(&usb_sym_lock);
988 SYSUNINIT(usb_dev_uninit, SI_SUB_KICK_SCHEDULER, SI_ORDER_ANY, usb_dev_uninit, NULL);
991 usb_ioctl_f_sub(struct usb_fifo *f, u_long cmd, void *addr,
998 *(int *)addr = 0; /* character device */
1002 /* handled by upper FS layer */
1007 if (f->async_p != NULL) {
1011 f->async_p = USB_TD_GET_PROC(td);
1017 /* XXX this is not the most general solution */
1019 if (f->async_p == NULL) {
1023 if (*(int *)addr != USB_PROC_GET_GID(f->async_p)) {
1031 DPRINTFN(3, "cmd 0x%lx = %d\n", cmd, error);
1035 /*------------------------------------------------------------------------*
1036 * usb_ioctl - cdev callback
1037 *------------------------------------------------------------------------*/
1039 usb_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int fflag, struct thread* td)
1041 struct usb_cdev_refdata refs;
1042 struct usb_cdev_privdata* cpd;
1047 DPRINTFN(2, "cmd=0x%lx\n", cmd);
1049 err = devfs_get_cdevpriv((void **)&cpd);
1054 * Performance optimisation: We try to check for IOCTL's that
1055 * don't need the USB reference first. Then we grab the USB
1056 * reference if we need it!
1058 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1062 fflags = cpd->fflags;
1064 f = NULL; /* set default value */
1065 err = ENOIOCTL; /* set default value */
1067 if (fflags & FWRITE) {
1069 err = usb_ioctl_f_sub(f, cmd, addr, td);
1071 if (fflags & FREAD) {
1073 err = usb_ioctl_f_sub(f, cmd, addr, td);
1075 KASSERT(f != NULL, ("fifo not found"));
1076 if (err != ENOIOCTL)
1079 err = (f->methods->f_ioctl) (f, cmd, addr, fflags);
1081 DPRINTFN(2, "f_ioctl cmd 0x%lx = %d\n", cmd, err);
1083 if (err != ENOIOCTL)
1086 if (usb_usb_ref_device(cpd, &refs)) {
1091 err = (f->methods->f_ioctl_post) (f, cmd, addr, fflags);
1093 DPRINTFN(2, "f_ioctl_post cmd 0x%lx = %d\n", cmd, err);
1095 if (err == ENOIOCTL)
1101 /* Wait for re-enumeration, if any */
1103 while (f->udev->re_enumerate_wait != USB_RE_ENUM_DONE) {
1105 usb_unref_device(cpd, &refs);
1107 usb_pause_mtx(NULL, hz / 128);
1109 if (usb_ref_device(cpd, &refs, 1 /* need uref */)) {
1116 usb_unref_device(cpd, &refs);
1122 usb_poll(struct cdev* dev, int events, struct thread* td)
1124 struct usb_cdev_refdata refs;
1125 struct usb_cdev_privdata* cpd;
1128 int fflags, revents;
1130 if (devfs_get_cdevpriv((void **)&cpd) != 0 ||
1131 usb_ref_device(cpd, &refs, 0) != 0)
1133 (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM));
1135 fflags = cpd->fflags;
1137 /* Figure out who needs service */
1139 if ((events & (POLLOUT | POLLWRNORM)) &&
1140 (fflags & FWRITE)) {
1144 mtx_lock(f->priv_mtx);
1146 if (!refs.is_usbfs) {
1147 if (f->flag_iserror) {
1148 /* we got an error */
1151 if (f->queue_data == NULL) {
1153 * start write transfer, if not
1156 (f->methods->f_start_write) (f);
1158 /* check if any packets are available */
1159 USB_IF_POLL(&f->free_q, m);
1162 if (f->flag_iscomplete) {
1170 revents |= events & (POLLOUT | POLLWRNORM);
1172 f->flag_isselect = 1;
1173 selrecord(td, &f->selinfo);
1176 mtx_unlock(f->priv_mtx);
1178 if ((events & (POLLIN | POLLRDNORM)) &&
1183 mtx_lock(f->priv_mtx);
1185 if (!refs.is_usbfs) {
1186 if (f->flag_iserror) {
1187 /* we have and error */
1190 if (f->queue_data == NULL) {
1192 * start read transfer, if not
1195 (f->methods->f_start_read) (f);
1197 /* check if any packets are available */
1198 USB_IF_POLL(&f->used_q, m);
1201 if (f->flag_iscomplete) {
1209 revents |= events & (POLLIN | POLLRDNORM);
1211 f->flag_isselect = 1;
1212 selrecord(td, &f->selinfo);
1214 if (!refs.is_usbfs) {
1215 /* start reading data */
1216 (f->methods->f_start_read) (f);
1220 mtx_unlock(f->priv_mtx);
1222 usb_unref_device(cpd, &refs);
1227 usb_read(struct cdev *dev, struct uio *uio, int ioflag)
1229 struct usb_cdev_refdata refs;
1230 struct usb_cdev_privdata* cpd;
1237 uint8_t tr_data = 0;
1239 err = devfs_get_cdevpriv((void **)&cpd);
1243 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1247 fflags = cpd->fflags;
1251 /* should not happen */
1252 usb_unref_device(cpd, &refs);
1256 resid = uio->uio_resid;
1258 mtx_lock(f->priv_mtx);
1260 /* check for permanent read error */
1261 if (f->flag_iserror) {
1265 /* check if USB-FS interface is active */
1266 if (refs.is_usbfs) {
1268 * The queue is used for events that should be
1269 * retrieved using the "USB_FS_COMPLETE" ioctl.
1274 while (uio->uio_resid > 0) {
1276 USB_IF_DEQUEUE(&f->used_q, m);
1280 /* start read transfer, if not already started */
1282 (f->methods->f_start_read) (f);
1284 if (ioflag & IO_NDELAY) {
1286 /* return length before error */
1292 DPRINTF("sleeping\n");
1294 err = usb_fifo_wait(f);
1300 if (f->methods->f_filter_read) {
1302 * Sometimes it is convenient to process data at the
1303 * expense of a userland process instead of a kernel
1306 (f->methods->f_filter_read) (f, m);
1310 io_len = MIN(m->cur_data_len, uio->uio_resid);
1312 DPRINTFN(2, "transfer %d bytes from %p\n",
1313 io_len, m->cur_data_ptr);
1315 err = usb_fifo_uiomove(f,
1316 m->cur_data_ptr, io_len, uio);
1318 m->cur_data_len -= io_len;
1319 m->cur_data_ptr += io_len;
1321 if (m->cur_data_len == 0) {
1323 uint8_t last_packet;
1325 last_packet = m->last_packet;
1327 USB_IF_ENQUEUE(&f->free_q, m);
1334 USB_IF_PREPEND(&f->used_q, m);
1342 mtx_unlock(f->priv_mtx);
1344 usb_unref_device(cpd, &refs);
1350 usb_write(struct cdev *dev, struct uio *uio, int ioflag)
1352 struct usb_cdev_refdata refs;
1353 struct usb_cdev_privdata* cpd;
1361 uint8_t tr_data = 0;
1365 err = devfs_get_cdevpriv((void **)&cpd);
1369 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1373 fflags = cpd->fflags;
1377 /* should not happen */
1378 usb_unref_device(cpd, &refs);
1381 resid = uio->uio_resid;
1383 mtx_lock(f->priv_mtx);
1385 /* check for permanent write error */
1386 if (f->flag_iserror) {
1390 /* check if USB-FS interface is active */
1391 if (refs.is_usbfs) {
1393 * The queue is used for events that should be
1394 * retrieved using the "USB_FS_COMPLETE" ioctl.
1399 if (f->queue_data == NULL) {
1400 /* start write transfer, if not already started */
1401 (f->methods->f_start_write) (f);
1403 /* we allow writing zero length data */
1405 USB_IF_DEQUEUE(&f->free_q, m);
1409 if (ioflag & IO_NDELAY) {
1411 /* return length before error */
1417 DPRINTF("sleeping\n");
1419 err = usb_fifo_wait(f);
1427 if (f->flag_have_fragment == 0) {
1429 io_len = m->cur_data_len;
1430 pdata = m->cur_data_ptr;
1431 if (io_len > uio->uio_resid)
1432 io_len = uio->uio_resid;
1433 m->cur_data_len = io_len;
1435 io_len = m->max_data_len - m->cur_data_len;
1436 pdata = m->cur_data_ptr + m->cur_data_len;
1437 if (io_len > uio->uio_resid)
1438 io_len = uio->uio_resid;
1439 m->cur_data_len += io_len;
1442 DPRINTFN(2, "transfer %d bytes to %p\n",
1445 err = usb_fifo_uiomove(f, pdata, io_len, uio);
1448 f->flag_have_fragment = 0;
1449 USB_IF_ENQUEUE(&f->free_q, m);
1453 /* check if the buffer is ready to be transmitted */
1455 if ((f->flag_write_defrag == 0) ||
1456 (m->cur_data_len == m->max_data_len)) {
1457 f->flag_have_fragment = 0;
1460 * Check for write filter:
1462 * Sometimes it is convenient to process data
1463 * at the expense of a userland process
1464 * instead of a kernel process.
1466 if (f->methods->f_filter_write) {
1467 (f->methods->f_filter_write) (f, m);
1470 /* Put USB mbuf in the used queue */
1471 USB_IF_ENQUEUE(&f->used_q, m);
1473 /* Start writing data, if not already started */
1474 (f->methods->f_start_write) (f);
1476 /* Wait for more data or close */
1477 f->flag_have_fragment = 1;
1478 USB_IF_PREPEND(&f->free_q, m);
1481 } while (uio->uio_resid > 0);
1483 mtx_unlock(f->priv_mtx);
1485 usb_unref_device(cpd, &refs);
1491 usb_static_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
1495 struct usb_read_dir *urd;
1503 err = usb_read_symlink(u.urd->urd_data,
1504 u.urd->urd_startentry, u.urd->urd_maxlen);
1506 case USB_DEV_QUIRK_GET:
1507 case USB_QUIRK_NAME_GET:
1508 case USB_DEV_QUIRK_ADD:
1509 case USB_DEV_QUIRK_REMOVE:
1510 err = usb_quirk_ioctl_p(cmd, data, fflag, td);
1512 case USB_GET_TEMPLATE:
1513 *(int *)data = usb_template;
1516 case USB_SET_TEMPLATE:
1517 err = priv_check(curthread, PRIV_DRIVER);
1520 usb_template = *(int *)data;
1530 usb_fifo_uiomove(struct usb_fifo *f, void *cp,
1531 int n, struct uio *uio)
1535 mtx_unlock(f->priv_mtx);
1538 * "uiomove()" can sleep so one needs to make a wrapper,
1539 * exiting the mutex and checking things:
1541 error = uiomove(cp, n, uio);
1543 mtx_lock(f->priv_mtx);
1549 usb_fifo_wait(struct usb_fifo *f)
1553 mtx_assert(f->priv_mtx, MA_OWNED);
1555 if (f->flag_iserror) {
1559 f->flag_sleeping = 1;
1561 err = cv_wait_sig(&f->cv_io, f->priv_mtx);
1563 if (f->flag_iserror) {
1571 usb_fifo_signal(struct usb_fifo *f)
1573 if (f->flag_sleeping) {
1574 f->flag_sleeping = 0;
1575 cv_broadcast(&f->cv_io);
1580 usb_fifo_wakeup(struct usb_fifo *f)
1584 if (f->flag_isselect) {
1585 selwakeup(&f->selinfo);
1586 f->flag_isselect = 0;
1588 if (f->async_p != NULL) {
1589 PROC_LOCK(f->async_p);
1590 kern_psignal(f->async_p, SIGIO);
1591 PROC_UNLOCK(f->async_p);
1596 usb_fifo_dummy_open(struct usb_fifo *fifo, int fflags)
1602 usb_fifo_dummy_close(struct usb_fifo *fifo, int fflags)
1608 usb_fifo_dummy_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
1614 usb_fifo_dummy_cmd(struct usb_fifo *fifo)
1616 fifo->flag_flushing = 0; /* not flushing */
1620 usb_fifo_check_methods(struct usb_fifo_methods *pm)
1622 /* check that all callback functions are OK */
1624 if (pm->f_open == NULL)
1625 pm->f_open = &usb_fifo_dummy_open;
1627 if (pm->f_close == NULL)
1628 pm->f_close = &usb_fifo_dummy_close;
1630 if (pm->f_ioctl == NULL)
1631 pm->f_ioctl = &usb_fifo_dummy_ioctl;
1633 if (pm->f_ioctl_post == NULL)
1634 pm->f_ioctl_post = &usb_fifo_dummy_ioctl;
1636 if (pm->f_start_read == NULL)
1637 pm->f_start_read = &usb_fifo_dummy_cmd;
1639 if (pm->f_stop_read == NULL)
1640 pm->f_stop_read = &usb_fifo_dummy_cmd;
1642 if (pm->f_start_write == NULL)
1643 pm->f_start_write = &usb_fifo_dummy_cmd;
1645 if (pm->f_stop_write == NULL)
1646 pm->f_stop_write = &usb_fifo_dummy_cmd;
1649 /*------------------------------------------------------------------------*
1652 * The following function will create a duplex FIFO.
1657 *------------------------------------------------------------------------*/
1659 usb_fifo_attach(struct usb_device *udev, void *priv_sc,
1660 struct mtx *priv_mtx, struct usb_fifo_methods *pm,
1661 struct usb_fifo_sc *f_sc, uint16_t unit, int16_t subunit,
1662 uint8_t iface_index, uid_t uid, gid_t gid, int mode)
1664 struct usb_fifo *f_tx;
1665 struct usb_fifo *f_rx;
1669 f_sc->fp[USB_FIFO_TX] = NULL;
1670 f_sc->fp[USB_FIFO_RX] = NULL;
1675 /* check the methods */
1676 usb_fifo_check_methods(pm);
1678 if (priv_mtx == NULL)
1681 /* search for a free FIFO slot */
1682 for (n = 0;; n += 2) {
1684 if (n == USB_FIFO_MAX) {
1685 /* end of FIFOs reached */
1688 /* Check for TX FIFO */
1689 if (udev->fifo[n + USB_FIFO_TX] != NULL) {
1692 /* Check for RX FIFO */
1693 if (udev->fifo[n + USB_FIFO_RX] != NULL) {
1699 f_tx = usb_fifo_alloc();
1700 f_rx = usb_fifo_alloc();
1702 if ((f_tx == NULL) || (f_rx == NULL)) {
1703 usb_fifo_free(f_tx);
1704 usb_fifo_free(f_rx);
1707 /* initialise FIFO structures */
1709 f_tx->fifo_index = n + USB_FIFO_TX;
1710 f_tx->dev_ep_index = -1;
1711 f_tx->priv_mtx = priv_mtx;
1712 f_tx->priv_sc0 = priv_sc;
1714 f_tx->iface_index = iface_index;
1717 f_rx->fifo_index = n + USB_FIFO_RX;
1718 f_rx->dev_ep_index = -1;
1719 f_rx->priv_mtx = priv_mtx;
1720 f_rx->priv_sc0 = priv_sc;
1722 f_rx->iface_index = iface_index;
1725 f_sc->fp[USB_FIFO_TX] = f_tx;
1726 f_sc->fp[USB_FIFO_RX] = f_rx;
1728 mtx_lock(&usb_ref_lock);
1729 udev->fifo[f_tx->fifo_index] = f_tx;
1730 udev->fifo[f_rx->fifo_index] = f_rx;
1731 mtx_unlock(&usb_ref_lock);
1733 for (n = 0; n != 4; n++) {
1735 if (pm->basename[n] == NULL) {
1739 if (snprintf(devname, sizeof(devname),
1740 "%s%u%s", pm->basename[n],
1741 unit, pm->postfix[n] ?
1742 pm->postfix[n] : "")) {
1746 if (snprintf(devname, sizeof(devname),
1747 "%s%u.%d%s", pm->basename[n],
1748 unit, subunit, pm->postfix[n] ?
1749 pm->postfix[n] : "")) {
1755 * Distribute the symbolic links into two FIFO structures:
1758 f_rx->symlink[n / 2] =
1759 usb_alloc_symlink(devname);
1761 f_tx->symlink[n / 2] =
1762 usb_alloc_symlink(devname);
1765 /* Create the device */
1766 f_sc->dev = usb_make_dev(udev, devname, -1,
1767 f_tx->fifo_index & f_rx->fifo_index,
1768 FREAD|FWRITE, uid, gid, mode);
1771 DPRINTFN(2, "attached %p/%p\n", f_tx, f_rx);
1775 /*------------------------------------------------------------------------*
1776 * usb_fifo_alloc_buffer
1781 *------------------------------------------------------------------------*/
1783 usb_fifo_alloc_buffer(struct usb_fifo *f, usb_size_t bufsize,
1786 usb_fifo_free_buffer(f);
1788 /* allocate an endpoint */
1789 f->free_q.ifq_maxlen = nbuf;
1790 f->used_q.ifq_maxlen = nbuf;
1792 f->queue_data = usb_alloc_mbufs(
1793 M_USBDEV, &f->free_q, bufsize, nbuf);
1795 if ((f->queue_data == NULL) && bufsize && nbuf) {
1798 return (0); /* success */
1801 /*------------------------------------------------------------------------*
1802 * usb_fifo_free_buffer
1804 * This function will free the buffers associated with a FIFO. This
1805 * function can be called multiple times in a row.
1806 *------------------------------------------------------------------------*/
1808 usb_fifo_free_buffer(struct usb_fifo *f)
1810 if (f->queue_data) {
1811 /* free old buffer */
1812 free(f->queue_data, M_USBDEV);
1813 f->queue_data = NULL;
1817 memset(&f->free_q, 0, sizeof(f->free_q));
1818 memset(&f->used_q, 0, sizeof(f->used_q));
1822 usb_fifo_detach(struct usb_fifo_sc *f_sc)
1827 usb_fifo_free(f_sc->fp[USB_FIFO_TX]);
1828 usb_fifo_free(f_sc->fp[USB_FIFO_RX]);
1830 f_sc->fp[USB_FIFO_TX] = NULL;
1831 f_sc->fp[USB_FIFO_RX] = NULL;
1833 usb_destroy_dev(f_sc->dev);
1837 DPRINTFN(2, "detached %p\n", f_sc);
1841 usb_fifo_put_bytes_max(struct usb_fifo *f)
1846 USB_IF_POLL(&f->free_q, m);
1849 len = m->max_data_len;
1856 /*------------------------------------------------------------------------*
1860 * 0 - normal operation
1861 * 1 - set last packet flag to enforce framing
1862 *------------------------------------------------------------------------*/
1864 usb_fifo_put_data(struct usb_fifo *f, struct usb_page_cache *pc,
1865 usb_frlength_t offset, usb_frlength_t len, uint8_t what)
1868 usb_frlength_t io_len;
1870 while (len || (what == 1)) {
1872 USB_IF_DEQUEUE(&f->free_q, m);
1877 io_len = MIN(len, m->cur_data_len);
1879 usbd_copy_out(pc, offset, m->cur_data_ptr, io_len);
1881 m->cur_data_len = io_len;
1885 if ((len == 0) && (what == 1)) {
1888 USB_IF_ENQUEUE(&f->used_q, m);
1892 if ((len == 0) || (what == 1)) {
1902 usb_fifo_put_data_linear(struct usb_fifo *f, void *ptr,
1903 usb_size_t len, uint8_t what)
1908 while (len || (what == 1)) {
1910 USB_IF_DEQUEUE(&f->free_q, m);
1915 io_len = MIN(len, m->cur_data_len);
1917 memcpy(m->cur_data_ptr, ptr, io_len);
1919 m->cur_data_len = io_len;
1920 ptr = USB_ADD_BYTES(ptr, io_len);
1923 if ((len == 0) && (what == 1)) {
1926 USB_IF_ENQUEUE(&f->used_q, m);
1930 if ((len == 0) || (what == 1)) {
1940 usb_fifo_put_data_buffer(struct usb_fifo *f, void *ptr, usb_size_t len)
1944 USB_IF_DEQUEUE(&f->free_q, m);
1947 m->cur_data_len = len;
1948 m->cur_data_ptr = ptr;
1949 USB_IF_ENQUEUE(&f->used_q, m);
1957 usb_fifo_put_data_error(struct usb_fifo *f)
1959 f->flag_iserror = 1;
1963 /*------------------------------------------------------------------------*
1967 * 0 - normal operation
1968 * 1 - only get one "usb_mbuf"
1972 * 1 - data in buffer
1973 *------------------------------------------------------------------------*/
1975 usb_fifo_get_data(struct usb_fifo *f, struct usb_page_cache *pc,
1976 usb_frlength_t offset, usb_frlength_t len, usb_frlength_t *actlen,
1980 usb_frlength_t io_len;
1981 uint8_t tr_data = 0;
1987 USB_IF_DEQUEUE(&f->used_q, m);
1993 io_len = MIN(len, m->cur_data_len);
1995 usbd_copy_in(pc, offset, m->cur_data_ptr, io_len);
1999 actlen[0] += io_len;
2000 m->cur_data_ptr += io_len;
2001 m->cur_data_len -= io_len;
2003 if ((m->cur_data_len == 0) || (what == 1)) {
2004 USB_IF_ENQUEUE(&f->free_q, m);
2012 USB_IF_PREPEND(&f->used_q, m);
2017 /* wait for data to be written out */
2020 if (f->flag_flushing) {
2021 /* check if we should send a short packet */
2022 if (f->flag_short != 0) {
2027 /* flushing complete */
2028 f->flag_flushing = 0;
2041 usb_fifo_get_data_linear(struct usb_fifo *f, void *ptr,
2042 usb_size_t len, usb_size_t *actlen, uint8_t what)
2046 uint8_t tr_data = 0;
2052 USB_IF_DEQUEUE(&f->used_q, m);
2058 io_len = MIN(len, m->cur_data_len);
2060 memcpy(ptr, m->cur_data_ptr, io_len);
2063 ptr = USB_ADD_BYTES(ptr, io_len);
2064 actlen[0] += io_len;
2065 m->cur_data_ptr += io_len;
2066 m->cur_data_len -= io_len;
2068 if ((m->cur_data_len == 0) || (what == 1)) {
2069 USB_IF_ENQUEUE(&f->free_q, m);
2077 USB_IF_PREPEND(&f->used_q, m);
2082 /* wait for data to be written out */
2085 if (f->flag_flushing) {
2086 /* check if we should send a short packet */
2087 if (f->flag_short != 0) {
2092 /* flushing complete */
2093 f->flag_flushing = 0;
2106 usb_fifo_get_data_buffer(struct usb_fifo *f, void **pptr, usb_size_t *plen)
2110 USB_IF_POLL(&f->used_q, m);
2113 *plen = m->cur_data_len;
2114 *pptr = m->cur_data_ptr;
2122 usb_fifo_get_data_error(struct usb_fifo *f)
2124 f->flag_iserror = 1;
2128 /*------------------------------------------------------------------------*
2133 * Else: Pointer to symlink entry
2134 *------------------------------------------------------------------------*/
2135 struct usb_symlink *
2136 usb_alloc_symlink(const char *target)
2138 struct usb_symlink *ps;
2140 ps = malloc(sizeof(*ps), M_USBDEV, M_WAITOK);
2144 /* XXX no longer needed */
2145 strlcpy(ps->src_path, target, sizeof(ps->src_path));
2146 ps->src_len = strlen(ps->src_path);
2147 strlcpy(ps->dst_path, target, sizeof(ps->dst_path));
2148 ps->dst_len = strlen(ps->dst_path);
2150 sx_xlock(&usb_sym_lock);
2151 TAILQ_INSERT_TAIL(&usb_sym_head, ps, sym_entry);
2152 sx_unlock(&usb_sym_lock);
2156 /*------------------------------------------------------------------------*
2158 *------------------------------------------------------------------------*/
2160 usb_free_symlink(struct usb_symlink *ps)
2165 sx_xlock(&usb_sym_lock);
2166 TAILQ_REMOVE(&usb_sym_head, ps, sym_entry);
2167 sx_unlock(&usb_sym_lock);
2172 /*------------------------------------------------------------------------*
2178 *------------------------------------------------------------------------*/
2180 usb_read_symlink(uint8_t *user_ptr, uint32_t startentry, uint32_t user_len)
2182 struct usb_symlink *ps;
2188 sx_xlock(&usb_sym_lock);
2190 TAILQ_FOREACH(ps, &usb_sym_head, sym_entry) {
2193 * Compute total length of source and destination symlink
2194 * strings pluss one length byte and two NUL bytes:
2196 temp = ps->src_len + ps->dst_len + 3;
2200 * Skip entry because this length cannot fit
2205 if (startentry != 0) {
2206 /* decrement read offset */
2210 if (temp > user_len) {
2211 /* out of buffer space */
2216 /* copy out total length */
2218 error = copyout(&len,
2219 USB_ADD_BYTES(user_ptr, delta), 1);
2225 /* copy out source string */
2227 error = copyout(ps->src_path,
2228 USB_ADD_BYTES(user_ptr, delta), ps->src_len);
2233 delta += ps->src_len;
2234 error = copyout(&len,
2235 USB_ADD_BYTES(user_ptr, delta), 1);
2241 /* copy out destination string */
2243 error = copyout(ps->dst_path,
2244 USB_ADD_BYTES(user_ptr, delta), ps->dst_len);
2249 delta += ps->dst_len;
2250 error = copyout(&len,
2251 USB_ADD_BYTES(user_ptr, delta), 1);
2260 /* a zero length entry indicates the end */
2262 if ((user_len != 0) && (error == 0)) {
2266 error = copyout(&len,
2267 USB_ADD_BYTES(user_ptr, delta), 1);
2269 sx_unlock(&usb_sym_lock);
2274 usb_fifo_set_close_zlp(struct usb_fifo *f, uint8_t onoff)
2279 /* send a Zero Length Packet, ZLP, before close */
2280 f->flag_short = onoff;
2284 usb_fifo_set_write_defrag(struct usb_fifo *f, uint8_t onoff)
2289 /* defrag written data */
2290 f->flag_write_defrag = onoff;
2291 /* reset defrag state */
2292 f->flag_have_fragment = 0;
2296 usb_fifo_softc(struct usb_fifo *f)
2298 return (f->priv_sc0);
2300 #endif /* USB_HAVE_UGEN */