3 * Copyright (c) 2006-2008 Hans Petter Selasky. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * usb_dev.c - An abstraction layer for creating devices under /dev/...
30 #include <sys/stdint.h>
31 #include <sys/stddef.h>
32 #include <sys/param.h>
33 #include <sys/queue.h>
34 #include <sys/types.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/linker_set.h>
39 #include <sys/module.h>
41 #include <sys/mutex.h>
42 #include <sys/condvar.h>
43 #include <sys/sysctl.h>
45 #include <sys/unistd.h>
46 #include <sys/callout.h>
47 #include <sys/malloc.h>
49 #include <sys/vnode.h>
51 #include <sys/fcntl.h>
53 #include <dev/usb/usb.h>
54 #include <dev/usb/usb_ioctl.h>
55 #include <dev/usb/usbdi.h>
56 #include <dev/usb/usbdi_util.h>
58 #define USB_DEBUG_VAR usb_fifo_debug
60 #include <dev/usb/usb_core.h>
61 #include <dev/usb/usb_dev.h>
62 #include <dev/usb/usb_mbuf.h>
63 #include <dev/usb/usb_process.h>
64 #include <dev/usb/usb_device.h>
65 #include <dev/usb/usb_debug.h>
66 #include <dev/usb/usb_busdma.h>
67 #include <dev/usb/usb_generic.h>
68 #include <dev/usb/usb_dynamic.h>
69 #include <dev/usb/usb_util.h>
71 #include <dev/usb/usb_controller.h>
72 #include <dev/usb/usb_bus.h>
74 #include <sys/filio.h>
75 #include <sys/ttycom.h>
76 #include <sys/syscallsubr.h>
78 #include <machine/stdarg.h>
83 static int usb_fifo_debug = 0;
85 SYSCTL_NODE(_hw_usb, OID_AUTO, dev, CTLFLAG_RW, 0, "USB device");
86 SYSCTL_INT(_hw_usb_dev, OID_AUTO, debug, CTLFLAG_RW,
87 &usb_fifo_debug, 0, "Debug Level");
89 TUNABLE_INT("hw.usb.dev.debug", &usb_fifo_debug);
92 #if ((__FreeBSD_version >= 700001) || (__FreeBSD_version == 0) || \
93 ((__FreeBSD_version >= 600034) && (__FreeBSD_version < 700000)))
94 #define USB_UCRED struct ucred *ucred,
101 static int usb_fifo_open(struct usb_cdev_privdata *,
102 struct usb_fifo *, int);
103 static void usb_fifo_close(struct usb_fifo *, int);
104 static void usb_dev_init(void *);
105 static void usb_dev_init_post(void *);
106 static void usb_dev_uninit(void *);
107 static int usb_fifo_uiomove(struct usb_fifo *, void *, int,
109 static void usb_fifo_check_methods(struct usb_fifo_methods *);
110 static struct usb_fifo *usb_fifo_alloc(void);
111 static struct usb_endpoint *usb_dev_get_ep(struct usb_device *, uint8_t,
113 static void usb_loc_fill(struct usb_fs_privdata *,
114 struct usb_cdev_privdata *);
115 static void usb_close(void *);
116 static usb_error_t usb_ref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *, int);
117 static usb_error_t usb_usb_ref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *);
118 static void usb_unref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *);
120 static d_open_t usb_open;
121 static d_ioctl_t usb_ioctl;
122 static d_read_t usb_read;
123 static d_write_t usb_write;
124 static d_poll_t usb_poll;
126 static d_ioctl_t usb_static_ioctl;
128 static usb_fifo_open_t usb_fifo_dummy_open;
129 static usb_fifo_close_t usb_fifo_dummy_close;
130 static usb_fifo_ioctl_t usb_fifo_dummy_ioctl;
131 static usb_fifo_cmd_t usb_fifo_dummy_cmd;
133 /* character device structure used for devices (/dev/ugenX.Y and /dev/uXXX) */
134 struct cdevsw usb_devsw = {
135 .d_version = D_VERSION,
137 .d_ioctl = usb_ioctl,
139 .d_flags = D_TRACKCLOSE,
141 .d_write = usb_write,
145 static struct cdev* usb_dev = NULL;
147 /* character device structure used for /dev/usb */
148 static struct cdevsw usb_static_devsw = {
149 .d_version = D_VERSION,
150 .d_ioctl = usb_static_ioctl,
154 static TAILQ_HEAD(, usb_symlink) usb_sym_head;
155 static struct sx usb_sym_lock;
157 struct mtx usb_ref_lock;
159 /*------------------------------------------------------------------------*
162 * This is used to fill out a usb_cdev_privdata structure based on the
163 * device's address as contained in usb_fs_privdata.
164 *------------------------------------------------------------------------*/
166 usb_loc_fill(struct usb_fs_privdata* pd, struct usb_cdev_privdata *cpd)
168 cpd->bus_index = pd->bus_index;
169 cpd->dev_index = pd->dev_index;
170 cpd->ep_addr = pd->ep_addr;
171 cpd->fifo_index = pd->fifo_index;
174 /*------------------------------------------------------------------------*
177 * This function is used to atomically refer an USB device by its
178 * device location. If this function returns success the USB device
179 * will not dissappear until the USB device is unreferenced.
182 * 0: Success, refcount incremented on the given USB device.
184 *------------------------------------------------------------------------*/
186 usb_ref_device(struct usb_cdev_privdata *cpd,
187 struct usb_cdev_refdata *crd, int need_uref)
189 struct usb_fifo **ppf;
192 DPRINTFN(2, "cpd=%p need uref=%d\n", cpd, need_uref);
195 memset(crd, 0, sizeof(*crd));
197 mtx_lock(&usb_ref_lock);
198 cpd->bus = devclass_get_softc(usb_devclass_ptr, cpd->bus_index);
199 if (cpd->bus == NULL) {
200 DPRINTFN(2, "no bus at %u\n", cpd->bus_index);
203 cpd->udev = cpd->bus->devices[cpd->dev_index];
204 if (cpd->udev == NULL) {
205 DPRINTFN(2, "no device at %u\n", cpd->dev_index);
208 if (cpd->udev->refcount == USB_DEV_REF_MAX) {
209 DPRINTFN(2, "no dev ref\n");
213 DPRINTFN(2, "ref udev - needed\n");
214 cpd->udev->refcount++;
216 mtx_unlock(&usb_ref_lock);
219 * We need to grab the sx-lock before grabbing the
220 * FIFO refs to avoid deadlock at detach!
222 usbd_enum_lock(cpd->udev);
224 mtx_lock(&usb_ref_lock);
227 * Set "is_uref" after grabbing the default SX lock
232 /* check if we are doing an open */
233 if (cpd->fflags == 0) {
234 /* use zero defaults */
236 /* check for write */
237 if (cpd->fflags & FWRITE) {
238 ppf = cpd->udev->fifo;
239 f = ppf[cpd->fifo_index + USB_FIFO_TX];
241 crd->is_write = 1; /* ref */
242 if (f == NULL || f->refcount == USB_FIFO_REF_MAX)
244 if (f->curr_cpd != cpd)
246 /* check if USB-FS is active */
247 if (f->fs_ep_max != 0) {
253 if (cpd->fflags & FREAD) {
254 ppf = cpd->udev->fifo;
255 f = ppf[cpd->fifo_index + USB_FIFO_RX];
257 crd->is_read = 1; /* ref */
258 if (f == NULL || f->refcount == USB_FIFO_REF_MAX)
260 if (f->curr_cpd != cpd)
262 /* check if USB-FS is active */
263 if (f->fs_ep_max != 0) {
269 /* when everything is OK we increment the refcounts */
271 DPRINTFN(2, "ref write\n");
272 crd->txfifo->refcount++;
275 DPRINTFN(2, "ref read\n");
276 crd->rxfifo->refcount++;
278 mtx_unlock(&usb_ref_lock);
284 usbd_enum_unlock(cpd->udev);
286 if (--(cpd->udev->refcount) == 0) {
287 cv_signal(&cpd->udev->ref_cv);
290 mtx_unlock(&usb_ref_lock);
291 DPRINTFN(2, "fail\n");
292 return (USB_ERR_INVAL);
295 /*------------------------------------------------------------------------*
298 * This function is used to upgrade an USB reference to include the
299 * USB device reference on a USB location.
302 * 0: Success, refcount incremented on the given USB device.
304 *------------------------------------------------------------------------*/
306 usb_usb_ref_device(struct usb_cdev_privdata *cpd,
307 struct usb_cdev_refdata *crd)
310 * Check if we already got an USB reference on this location:
313 return (0); /* success */
316 * To avoid deadlock at detach we need to drop the FIFO ref
317 * and re-acquire a new ref!
319 usb_unref_device(cpd, crd);
321 return (usb_ref_device(cpd, crd, 1 /* need uref */));
324 /*------------------------------------------------------------------------*
327 * This function will release the reference count by one unit for the
329 *------------------------------------------------------------------------*/
331 usb_unref_device(struct usb_cdev_privdata *cpd,
332 struct usb_cdev_refdata *crd)
335 DPRINTFN(2, "cpd=%p is_uref=%d\n", cpd, crd->is_uref);
338 usbd_enum_unlock(cpd->udev);
340 mtx_lock(&usb_ref_lock);
342 if (--(crd->rxfifo->refcount) == 0) {
343 cv_signal(&crd->rxfifo->cv_drain);
348 if (--(crd->txfifo->refcount) == 0) {
349 cv_signal(&crd->txfifo->cv_drain);
354 if (--(cpd->udev->refcount) == 0) {
355 cv_signal(&cpd->udev->ref_cv);
359 mtx_unlock(&usb_ref_lock);
362 static struct usb_fifo *
367 f = malloc(sizeof(*f), M_USBDEV, M_WAITOK | M_ZERO);
369 cv_init(&f->cv_io, "FIFO-IO");
370 cv_init(&f->cv_drain, "FIFO-DRAIN");
376 /*------------------------------------------------------------------------*
378 *------------------------------------------------------------------------*/
380 usb_fifo_create(struct usb_cdev_privdata *cpd,
381 struct usb_cdev_refdata *crd)
383 struct usb_device *udev = cpd->udev;
385 struct usb_endpoint *ep;
391 int e = cpd->ep_addr;
393 is_tx = (cpd->fflags & FWRITE) ? 1 : 0;
394 is_rx = (cpd->fflags & FREAD) ? 1 : 0;
398 /* Preallocated FIFO */
400 DPRINTFN(5, "Preallocated FIFO\n");
402 f = udev->fifo[cpd->fifo_index + USB_FIFO_TX];
408 f = udev->fifo[cpd->fifo_index + USB_FIFO_RX];
416 KASSERT(e >= 0 && e <= 15, ("endpoint %d out of range", e));
418 /* search for a free FIFO slot */
419 DPRINTFN(5, "Endpoint device, searching for 0x%02x\n", e);
420 for (n = 0;; n += 2) {
422 if (n == USB_FIFO_MAX) {
427 /* end of FIFOs reached */
428 DPRINTFN(5, "out of FIFOs\n");
432 /* Check for TX FIFO */
434 f = udev->fifo[n + USB_FIFO_TX];
436 if (f->dev_ep_index != e) {
437 /* wrong endpoint index */
440 if (f->curr_cpd != NULL) {
445 } else if (no_null) {
449 /* Check for RX FIFO */
451 f = udev->fifo[n + USB_FIFO_RX];
453 if (f->dev_ep_index != e) {
454 /* wrong endpoint index */
457 if (f->curr_cpd != NULL) {
462 } else if (no_null) {
470 if (e >= (USB_EP_MAX / 2)) {
471 /* we don't create any endpoints in this range */
472 DPRINTFN(5, "ep out of range\n");
473 return (is_busy ? EBUSY : EINVAL);
477 if ((e != 0) && is_busy) {
479 * Only the default control endpoint is allowed to be
480 * opened multiple times!
482 DPRINTFN(5, "busy\n");
488 (udev->fifo[n + USB_FIFO_TX] == NULL)) {
489 ep = usb_dev_get_ep(udev, e, USB_FIFO_TX);
490 DPRINTFN(5, "dev_get_endpoint(%d, 0x%x)\n", e, USB_FIFO_TX);
492 DPRINTFN(5, "dev_get_endpoint returned NULL\n");
495 f = usb_fifo_alloc();
497 DPRINTFN(5, "could not alloc tx fifo\n");
500 /* update some fields */
501 f->fifo_index = n + USB_FIFO_TX;
503 f->priv_mtx = &udev->device_mtx;
505 f->methods = &usb_ugen_methods;
506 f->iface_index = ep->iface_index;
508 mtx_lock(&usb_ref_lock);
509 udev->fifo[n + USB_FIFO_TX] = f;
510 mtx_unlock(&usb_ref_lock);
514 (udev->fifo[n + USB_FIFO_RX] == NULL)) {
516 ep = usb_dev_get_ep(udev, e, USB_FIFO_RX);
517 DPRINTFN(5, "dev_get_endpoint(%d, 0x%x)\n", e, USB_FIFO_RX);
519 DPRINTFN(5, "dev_get_endpoint returned NULL\n");
522 f = usb_fifo_alloc();
524 DPRINTFN(5, "could not alloc rx fifo\n");
527 /* update some fields */
528 f->fifo_index = n + USB_FIFO_RX;
530 f->priv_mtx = &udev->device_mtx;
532 f->methods = &usb_ugen_methods;
533 f->iface_index = ep->iface_index;
535 mtx_lock(&usb_ref_lock);
536 udev->fifo[n + USB_FIFO_RX] = f;
537 mtx_unlock(&usb_ref_lock);
540 crd->txfifo = udev->fifo[n + USB_FIFO_TX];
543 crd->rxfifo = udev->fifo[n + USB_FIFO_RX];
545 /* fill out fifo index */
546 DPRINTFN(5, "fifo index = %d\n", n);
555 usb_fifo_free(struct usb_fifo *f)
563 /* destroy symlink devices, if any */
564 for (n = 0; n != 2; n++) {
566 usb_free_symlink(f->symlink[n]);
567 f->symlink[n] = NULL;
570 mtx_lock(&usb_ref_lock);
572 /* delink ourselves to stop calls from userland */
573 if ((f->fifo_index < USB_FIFO_MAX) &&
575 (f->udev->fifo[f->fifo_index] == f)) {
576 f->udev->fifo[f->fifo_index] = NULL;
578 DPRINTFN(0, "USB FIFO %p has not been linked\n", f);
581 /* decrease refcount */
583 /* prevent any write flush */
585 /* need to wait until all callers have exited */
586 while (f->refcount != 0) {
587 mtx_unlock(&usb_ref_lock); /* avoid LOR */
588 mtx_lock(f->priv_mtx);
589 /* get I/O thread out of any sleep state */
590 if (f->flag_sleeping) {
591 f->flag_sleeping = 0;
592 cv_broadcast(&f->cv_io);
594 mtx_unlock(f->priv_mtx);
595 mtx_lock(&usb_ref_lock);
598 cv_wait(&f->cv_drain, &usb_ref_lock);
600 mtx_unlock(&usb_ref_lock);
602 /* take care of closing the device here, if any */
603 usb_fifo_close(f, 0);
605 cv_destroy(&f->cv_io);
606 cv_destroy(&f->cv_drain);
611 static struct usb_endpoint *
612 usb_dev_get_ep(struct usb_device *udev, uint8_t ep_index, uint8_t dir)
614 struct usb_endpoint *ep;
620 if (dir == USB_FIFO_RX) {
621 if (udev->flags.usb_mode == USB_MODE_HOST) {
627 if (udev->flags.usb_mode == USB_MODE_HOST) {
633 ep = usbd_get_ep_by_addr(udev, ep_index | ep_dir);
637 /* if the endpoint does not exist then return */
640 if (ep->edesc == NULL) {
641 /* invalid endpoint */
644 return (ep); /* success */
647 /*------------------------------------------------------------------------*
653 *------------------------------------------------------------------------*/
655 usb_fifo_open(struct usb_cdev_privdata *cpd,
656 struct usb_fifo *f, int fflags)
662 DPRINTFN(2, "no FIFO\n");
665 /* remove FWRITE and FREAD flags */
666 fflags &= ~(FWRITE | FREAD);
668 /* set correct file flags */
669 if ((f->fifo_index & 1) == USB_FIFO_TX) {
675 /* check if we are already opened */
676 /* we don't need any locks when checking this variable */
677 if (f->curr_cpd != NULL) {
682 /* reset short flag before open */
685 /* call open method */
686 err = (f->methods->f_open) (f, fflags);
690 mtx_lock(f->priv_mtx);
692 /* reset sleep flag */
693 f->flag_sleeping = 0;
695 /* reset error flag */
698 /* reset complete flag */
699 f->flag_iscomplete = 0;
701 /* reset select flag */
702 f->flag_isselect = 0;
704 /* reset flushing flag */
705 f->flag_flushing = 0;
707 /* reset ASYNC proc flag */
710 mtx_lock(&usb_ref_lock);
711 /* flag the fifo as opened to prevent others */
713 mtx_unlock(&usb_ref_lock);
718 mtx_unlock(f->priv_mtx);
723 /*------------------------------------------------------------------------*
725 *------------------------------------------------------------------------*/
727 usb_fifo_reset(struct usb_fifo *f)
735 USB_IF_DEQUEUE(&f->used_q, m);
737 USB_IF_ENQUEUE(&f->free_q, m);
742 /* reset have fragment flag */
743 f->flag_have_fragment = 0;
746 /*------------------------------------------------------------------------*
748 *------------------------------------------------------------------------*/
750 usb_fifo_close(struct usb_fifo *f, int fflags)
754 /* check if we are not opened */
755 if (f->curr_cpd == NULL) {
756 /* nothing to do - already closed */
759 mtx_lock(f->priv_mtx);
761 /* clear current cdev private data pointer */
764 /* check if we are selected */
765 if (f->flag_isselect) {
766 selwakeup(&f->selinfo);
767 f->flag_isselect = 0;
769 /* check if a thread wants SIGIO */
770 if (f->async_p != NULL) {
771 PROC_LOCK(f->async_p);
772 psignal(f->async_p, SIGIO);
773 PROC_UNLOCK(f->async_p);
776 /* remove FWRITE and FREAD flags */
777 fflags &= ~(FWRITE | FREAD);
779 /* flush written data, if any */
780 if ((f->fifo_index & 1) == USB_FIFO_TX) {
782 if (!f->flag_iserror) {
784 /* set flushing flag */
785 f->flag_flushing = 1;
787 /* get the last packet in */
788 if (f->flag_have_fragment) {
790 f->flag_have_fragment = 0;
791 USB_IF_DEQUEUE(&f->free_q, m);
793 USB_IF_ENQUEUE(&f->used_q, m);
797 /* start write transfer, if not already started */
798 (f->methods->f_start_write) (f);
800 /* check if flushed already */
801 while (f->flag_flushing &&
802 (!f->flag_iserror)) {
803 /* wait until all data has been written */
804 f->flag_sleeping = 1;
805 err = cv_wait_sig(&f->cv_io, f->priv_mtx);
807 DPRINTF("signal received\n");
814 /* stop write transfer, if not already stopped */
815 (f->methods->f_stop_write) (f);
819 /* stop write transfer, if not already stopped */
820 (f->methods->f_stop_read) (f);
823 /* check if we are sleeping */
824 if (f->flag_sleeping) {
825 DPRINTFN(2, "Sleeping at close!\n");
827 mtx_unlock(f->priv_mtx);
829 /* call close method */
830 (f->methods->f_close) (f, fflags);
835 /*------------------------------------------------------------------------*
836 * usb_open - cdev callback
837 *------------------------------------------------------------------------*/
839 usb_open(struct cdev *dev, int fflags, int devtype, struct thread *td)
841 struct usb_fs_privdata* pd = (struct usb_fs_privdata*)dev->si_drv1;
842 struct usb_cdev_refdata refs;
843 struct usb_cdev_privdata *cpd;
846 DPRINTFN(2, "%s fflags=0x%08x\n", dev->si_name, fflags);
848 KASSERT(fflags & (FREAD|FWRITE), ("invalid open flags"));
849 if (((fflags & FREAD) && !(pd->mode & FREAD)) ||
850 ((fflags & FWRITE) && !(pd->mode & FWRITE))) {
851 DPRINTFN(2, "access mode not supported\n");
855 cpd = malloc(sizeof(*cpd), M_USBDEV, M_WAITOK | M_ZERO);
856 ep = cpd->ep_addr = pd->ep_addr;
858 usb_loc_fill(pd, cpd);
859 err = usb_ref_device(cpd, &refs, 1);
861 DPRINTFN(2, "cannot ref device\n");
865 cpd->fflags = fflags; /* access mode for open lifetime */
867 /* create FIFOs, if any */
868 err = usb_fifo_create(cpd, &refs);
869 /* check for error */
871 DPRINTFN(2, "cannot create fifo\n");
872 usb_unref_device(cpd, &refs);
876 if (fflags & FREAD) {
877 err = usb_fifo_open(cpd, refs.rxfifo, fflags);
879 DPRINTFN(2, "read open failed\n");
880 usb_unref_device(cpd, &refs);
885 if (fflags & FWRITE) {
886 err = usb_fifo_open(cpd, refs.txfifo, fflags);
888 DPRINTFN(2, "write open failed\n");
889 if (fflags & FREAD) {
890 usb_fifo_close(refs.rxfifo, fflags);
892 usb_unref_device(cpd, &refs);
897 usb_unref_device(cpd, &refs);
898 devfs_set_cdevpriv(cpd, usb_close);
903 /*------------------------------------------------------------------------*
904 * usb_close - cdev callback
905 *------------------------------------------------------------------------*/
909 struct usb_cdev_refdata refs;
910 struct usb_cdev_privdata *cpd = arg;
913 DPRINTFN(2, "cpd=%p\n", cpd);
915 err = usb_ref_device(cpd, &refs, 1);
920 if (cpd->fflags & FREAD) {
921 usb_fifo_close(refs.rxfifo, cpd->fflags);
923 if (cpd->fflags & FWRITE) {
924 usb_fifo_close(refs.txfifo, cpd->fflags);
927 usb_unref_device(cpd, &refs);
933 usb_dev_init(void *arg)
935 mtx_init(&usb_ref_lock, "USB ref mutex", NULL, MTX_DEF);
936 sx_init(&usb_sym_lock, "USB sym mutex");
937 TAILQ_INIT(&usb_sym_head);
939 /* check the UGEN methods */
940 usb_fifo_check_methods(&usb_ugen_methods);
943 SYSINIT(usb_dev_init, SI_SUB_KLD, SI_ORDER_FIRST, usb_dev_init, NULL);
946 usb_dev_init_post(void *arg)
949 * Create /dev/usb - this is needed for usbconfig(8), which
950 * needs a well-known device name to access.
952 usb_dev = make_dev(&usb_static_devsw, 0, UID_ROOT, GID_OPERATOR,
953 0644, USB_DEVICE_NAME);
954 if (usb_dev == NULL) {
955 DPRINTFN(0, "Could not create usb bus device\n");
959 SYSINIT(usb_dev_init_post, SI_SUB_KICK_SCHEDULER, SI_ORDER_FIRST, usb_dev_init_post, NULL);
962 usb_dev_uninit(void *arg)
964 if (usb_dev != NULL) {
965 destroy_dev(usb_dev);
969 mtx_destroy(&usb_ref_lock);
970 sx_destroy(&usb_sym_lock);
973 SYSUNINIT(usb_dev_uninit, SI_SUB_KICK_SCHEDULER, SI_ORDER_ANY, usb_dev_uninit, NULL);
976 usb_ioctl_f_sub(struct usb_fifo *f, u_long cmd, void *addr,
983 *(int *)addr = 0; /* character device */
987 /* handled by upper FS layer */
992 if (f->async_p != NULL) {
996 f->async_p = USB_TD_GET_PROC(td);
1002 /* XXX this is not the most general solution */
1004 if (f->async_p == NULL) {
1008 if (*(int *)addr != USB_PROC_GET_GID(f->async_p)) {
1016 DPRINTFN(3, "cmd 0x%lx = %d\n", cmd, error);
1020 /*------------------------------------------------------------------------*
1021 * usb_ioctl - cdev callback
1022 *------------------------------------------------------------------------*/
1024 usb_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int fflag, struct thread* td)
1026 struct usb_cdev_refdata refs;
1027 struct usb_cdev_privdata* cpd;
1032 DPRINTFN(2, "cmd=0x%lx\n", cmd);
1034 err = devfs_get_cdevpriv((void **)&cpd);
1039 * Performance optimisation: We try to check for IOCTL's that
1040 * don't need the USB reference first. Then we grab the USB
1041 * reference if we need it!
1043 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1047 fflags = cpd->fflags;
1049 f = NULL; /* set default value */
1050 err = ENOIOCTL; /* set default value */
1052 if (fflags & FWRITE) {
1054 err = usb_ioctl_f_sub(f, cmd, addr, td);
1056 if (fflags & FREAD) {
1058 err = usb_ioctl_f_sub(f, cmd, addr, td);
1060 KASSERT(f != NULL, ("fifo not found"));
1061 if (err == ENOIOCTL) {
1062 err = (f->methods->f_ioctl) (f, cmd, addr, fflags);
1063 DPRINTFN(2, "f_ioctl cmd 0x%lx = %d\n", cmd, err);
1064 if (err == ENOIOCTL) {
1065 if (usb_usb_ref_device(cpd, &refs)) {
1069 err = (f->methods->f_ioctl_post) (f, cmd, addr, fflags);
1070 DPRINTFN(2, "f_ioctl_post cmd 0x%lx = %d\n", cmd, err);
1073 if (err == ENOIOCTL) {
1077 usb_unref_device(cpd, &refs);
1083 usb_poll(struct cdev* dev, int events, struct thread* td)
1085 struct usb_cdev_refdata refs;
1086 struct usb_cdev_privdata* cpd;
1089 int fflags, revents;
1091 if (devfs_get_cdevpriv((void **)&cpd) != 0 ||
1092 usb_ref_device(cpd, &refs, 0) != 0)
1094 (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM));
1096 fflags = cpd->fflags;
1098 /* Figure out who needs service */
1100 if ((events & (POLLOUT | POLLWRNORM)) &&
1101 (fflags & FWRITE)) {
1105 mtx_lock(f->priv_mtx);
1107 if (!refs.is_usbfs) {
1108 if (f->flag_iserror) {
1109 /* we got an error */
1112 if (f->queue_data == NULL) {
1114 * start write transfer, if not
1117 (f->methods->f_start_write) (f);
1119 /* check if any packets are available */
1120 USB_IF_POLL(&f->free_q, m);
1123 if (f->flag_iscomplete) {
1131 revents |= events & (POLLOUT | POLLWRNORM);
1133 f->flag_isselect = 1;
1134 selrecord(td, &f->selinfo);
1137 mtx_unlock(f->priv_mtx);
1139 if ((events & (POLLIN | POLLRDNORM)) &&
1144 mtx_lock(f->priv_mtx);
1146 if (!refs.is_usbfs) {
1147 if (f->flag_iserror) {
1148 /* we have and error */
1151 if (f->queue_data == NULL) {
1153 * start read transfer, if not
1156 (f->methods->f_start_read) (f);
1158 /* check if any packets are available */
1159 USB_IF_POLL(&f->used_q, m);
1162 if (f->flag_iscomplete) {
1170 revents |= events & (POLLIN | POLLRDNORM);
1172 f->flag_isselect = 1;
1173 selrecord(td, &f->selinfo);
1175 if (!refs.is_usbfs) {
1176 /* start reading data */
1177 (f->methods->f_start_read) (f);
1181 mtx_unlock(f->priv_mtx);
1183 usb_unref_device(cpd, &refs);
1188 usb_read(struct cdev *dev, struct uio *uio, int ioflag)
1190 struct usb_cdev_refdata refs;
1191 struct usb_cdev_privdata* cpd;
1198 uint8_t tr_data = 0;
1200 err = devfs_get_cdevpriv((void **)&cpd);
1204 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1208 fflags = cpd->fflags;
1212 /* should not happen */
1213 usb_unref_device(cpd, &refs);
1217 resid = uio->uio_resid;
1219 mtx_lock(f->priv_mtx);
1221 /* check for permanent read error */
1222 if (f->flag_iserror) {
1226 /* check if USB-FS interface is active */
1227 if (refs.is_usbfs) {
1229 * The queue is used for events that should be
1230 * retrieved using the "USB_FS_COMPLETE" ioctl.
1235 while (uio->uio_resid > 0) {
1237 USB_IF_DEQUEUE(&f->used_q, m);
1241 /* start read transfer, if not already started */
1243 (f->methods->f_start_read) (f);
1245 if (ioflag & IO_NDELAY) {
1247 /* return length before error */
1253 DPRINTF("sleeping\n");
1255 err = usb_fifo_wait(f);
1261 if (f->methods->f_filter_read) {
1263 * Sometimes it is convenient to process data at the
1264 * expense of a userland process instead of a kernel
1267 (f->methods->f_filter_read) (f, m);
1271 io_len = MIN(m->cur_data_len, uio->uio_resid);
1273 DPRINTFN(2, "transfer %d bytes from %p\n",
1274 io_len, m->cur_data_ptr);
1276 err = usb_fifo_uiomove(f,
1277 m->cur_data_ptr, io_len, uio);
1279 m->cur_data_len -= io_len;
1280 m->cur_data_ptr += io_len;
1282 if (m->cur_data_len == 0) {
1284 uint8_t last_packet;
1286 last_packet = m->last_packet;
1288 USB_IF_ENQUEUE(&f->free_q, m);
1295 USB_IF_PREPEND(&f->used_q, m);
1303 mtx_unlock(f->priv_mtx);
1305 usb_unref_device(cpd, &refs);
1311 usb_write(struct cdev *dev, struct uio *uio, int ioflag)
1313 struct usb_cdev_refdata refs;
1314 struct usb_cdev_privdata* cpd;
1322 uint8_t tr_data = 0;
1326 err = devfs_get_cdevpriv((void **)&cpd);
1330 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1334 fflags = cpd->fflags;
1338 /* should not happen */
1339 usb_unref_device(cpd, &refs);
1342 resid = uio->uio_resid;
1344 mtx_lock(f->priv_mtx);
1346 /* check for permanent write error */
1347 if (f->flag_iserror) {
1351 /* check if USB-FS interface is active */
1352 if (refs.is_usbfs) {
1354 * The queue is used for events that should be
1355 * retrieved using the "USB_FS_COMPLETE" ioctl.
1360 if (f->queue_data == NULL) {
1361 /* start write transfer, if not already started */
1362 (f->methods->f_start_write) (f);
1364 /* we allow writing zero length data */
1366 USB_IF_DEQUEUE(&f->free_q, m);
1370 if (ioflag & IO_NDELAY) {
1372 /* return length before error */
1378 DPRINTF("sleeping\n");
1380 err = usb_fifo_wait(f);
1388 if (f->flag_have_fragment == 0) {
1390 io_len = m->cur_data_len;
1391 pdata = m->cur_data_ptr;
1392 if (io_len > uio->uio_resid)
1393 io_len = uio->uio_resid;
1394 m->cur_data_len = io_len;
1396 io_len = m->max_data_len - m->cur_data_len;
1397 pdata = m->cur_data_ptr + m->cur_data_len;
1398 if (io_len > uio->uio_resid)
1399 io_len = uio->uio_resid;
1400 m->cur_data_len += io_len;
1403 DPRINTFN(2, "transfer %d bytes to %p\n",
1406 err = usb_fifo_uiomove(f, pdata, io_len, uio);
1409 f->flag_have_fragment = 0;
1410 USB_IF_ENQUEUE(&f->free_q, m);
1414 /* check if the buffer is ready to be transmitted */
1416 if ((f->flag_write_defrag == 0) ||
1417 (m->cur_data_len == m->max_data_len)) {
1418 f->flag_have_fragment = 0;
1421 * Check for write filter:
1423 * Sometimes it is convenient to process data
1424 * at the expense of a userland process
1425 * instead of a kernel process.
1427 if (f->methods->f_filter_write) {
1428 (f->methods->f_filter_write) (f, m);
1431 /* Put USB mbuf in the used queue */
1432 USB_IF_ENQUEUE(&f->used_q, m);
1434 /* Start writing data, if not already started */
1435 (f->methods->f_start_write) (f);
1437 /* Wait for more data or close */
1438 f->flag_have_fragment = 1;
1439 USB_IF_PREPEND(&f->free_q, m);
1442 } while (uio->uio_resid > 0);
1444 mtx_unlock(f->priv_mtx);
1446 usb_unref_device(cpd, &refs);
1452 usb_static_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
1456 struct usb_read_dir *urd;
1464 err = usb_read_symlink(u.urd->urd_data,
1465 u.urd->urd_startentry, u.urd->urd_maxlen);
1467 case USB_DEV_QUIRK_GET:
1468 case USB_QUIRK_NAME_GET:
1469 case USB_DEV_QUIRK_ADD:
1470 case USB_DEV_QUIRK_REMOVE:
1471 err = usb_quirk_ioctl_p(cmd, data, fflag, td);
1473 case USB_GET_TEMPLATE:
1474 *(int *)data = usb_template;
1476 case USB_SET_TEMPLATE:
1477 err = priv_check(curthread, PRIV_DRIVER);
1480 usb_template = *(int *)data;
1487 usb_fifo_uiomove(struct usb_fifo *f, void *cp,
1488 int n, struct uio *uio)
1492 mtx_unlock(f->priv_mtx);
1495 * "uiomove()" can sleep so one needs to make a wrapper,
1496 * exiting the mutex and checking things:
1498 error = uiomove(cp, n, uio);
1500 mtx_lock(f->priv_mtx);
1506 usb_fifo_wait(struct usb_fifo *f)
1510 mtx_assert(f->priv_mtx, MA_OWNED);
1512 if (f->flag_iserror) {
1516 f->flag_sleeping = 1;
1518 err = cv_wait_sig(&f->cv_io, f->priv_mtx);
1520 if (f->flag_iserror) {
1528 usb_fifo_signal(struct usb_fifo *f)
1530 if (f->flag_sleeping) {
1531 f->flag_sleeping = 0;
1532 cv_broadcast(&f->cv_io);
1537 usb_fifo_wakeup(struct usb_fifo *f)
1541 if (f->flag_isselect) {
1542 selwakeup(&f->selinfo);
1543 f->flag_isselect = 0;
1545 if (f->async_p != NULL) {
1546 PROC_LOCK(f->async_p);
1547 psignal(f->async_p, SIGIO);
1548 PROC_UNLOCK(f->async_p);
1553 usb_fifo_dummy_open(struct usb_fifo *fifo, int fflags)
1559 usb_fifo_dummy_close(struct usb_fifo *fifo, int fflags)
1565 usb_fifo_dummy_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
1571 usb_fifo_dummy_cmd(struct usb_fifo *fifo)
1573 fifo->flag_flushing = 0; /* not flushing */
1577 usb_fifo_check_methods(struct usb_fifo_methods *pm)
1579 /* check that all callback functions are OK */
1581 if (pm->f_open == NULL)
1582 pm->f_open = &usb_fifo_dummy_open;
1584 if (pm->f_close == NULL)
1585 pm->f_close = &usb_fifo_dummy_close;
1587 if (pm->f_ioctl == NULL)
1588 pm->f_ioctl = &usb_fifo_dummy_ioctl;
1590 if (pm->f_ioctl_post == NULL)
1591 pm->f_ioctl_post = &usb_fifo_dummy_ioctl;
1593 if (pm->f_start_read == NULL)
1594 pm->f_start_read = &usb_fifo_dummy_cmd;
1596 if (pm->f_stop_read == NULL)
1597 pm->f_stop_read = &usb_fifo_dummy_cmd;
1599 if (pm->f_start_write == NULL)
1600 pm->f_start_write = &usb_fifo_dummy_cmd;
1602 if (pm->f_stop_write == NULL)
1603 pm->f_stop_write = &usb_fifo_dummy_cmd;
1606 /*------------------------------------------------------------------------*
1609 * The following function will create a duplex FIFO.
1614 *------------------------------------------------------------------------*/
1616 usb_fifo_attach(struct usb_device *udev, void *priv_sc,
1617 struct mtx *priv_mtx, struct usb_fifo_methods *pm,
1618 struct usb_fifo_sc *f_sc, uint16_t unit, uint16_t subunit,
1619 uint8_t iface_index, uid_t uid, gid_t gid, int mode)
1621 struct usb_fifo *f_tx;
1622 struct usb_fifo *f_rx;
1625 struct usb_fs_privdata* pd;
1627 f_sc->fp[USB_FIFO_TX] = NULL;
1628 f_sc->fp[USB_FIFO_RX] = NULL;
1633 /* check the methods */
1634 usb_fifo_check_methods(pm);
1636 if (priv_mtx == NULL)
1639 /* search for a free FIFO slot */
1640 for (n = 0;; n += 2) {
1642 if (n == USB_FIFO_MAX) {
1643 /* end of FIFOs reached */
1646 /* Check for TX FIFO */
1647 if (udev->fifo[n + USB_FIFO_TX] != NULL) {
1650 /* Check for RX FIFO */
1651 if (udev->fifo[n + USB_FIFO_RX] != NULL) {
1657 f_tx = usb_fifo_alloc();
1658 f_rx = usb_fifo_alloc();
1660 if ((f_tx == NULL) || (f_rx == NULL)) {
1661 usb_fifo_free(f_tx);
1662 usb_fifo_free(f_rx);
1665 /* initialise FIFO structures */
1667 f_tx->fifo_index = n + USB_FIFO_TX;
1668 f_tx->dev_ep_index = -1;
1669 f_tx->priv_mtx = priv_mtx;
1670 f_tx->priv_sc0 = priv_sc;
1672 f_tx->iface_index = iface_index;
1675 f_rx->fifo_index = n + USB_FIFO_RX;
1676 f_rx->dev_ep_index = -1;
1677 f_rx->priv_mtx = priv_mtx;
1678 f_rx->priv_sc0 = priv_sc;
1680 f_rx->iface_index = iface_index;
1683 f_sc->fp[USB_FIFO_TX] = f_tx;
1684 f_sc->fp[USB_FIFO_RX] = f_rx;
1686 mtx_lock(&usb_ref_lock);
1687 udev->fifo[f_tx->fifo_index] = f_tx;
1688 udev->fifo[f_rx->fifo_index] = f_rx;
1689 mtx_unlock(&usb_ref_lock);
1691 for (n = 0; n != 4; n++) {
1693 if (pm->basename[n] == NULL) {
1696 if (subunit == 0xFFFF) {
1697 if (snprintf(devname, sizeof(devname),
1698 "%s%u%s", pm->basename[n],
1699 unit, pm->postfix[n] ?
1700 pm->postfix[n] : "")) {
1704 if (snprintf(devname, sizeof(devname),
1705 "%s%u.%u%s", pm->basename[n],
1706 unit, subunit, pm->postfix[n] ?
1707 pm->postfix[n] : "")) {
1713 * Distribute the symbolic links into two FIFO structures:
1716 f_rx->symlink[n / 2] =
1717 usb_alloc_symlink(devname);
1719 f_tx->symlink[n / 2] =
1720 usb_alloc_symlink(devname);
1724 * Initialize device private data - this is used to find the
1725 * actual USB device itself.
1727 pd = malloc(sizeof(struct usb_fs_privdata), M_USBDEV, M_WAITOK | M_ZERO);
1728 pd->bus_index = device_get_unit(udev->bus->bdev);
1729 pd->dev_index = udev->device_index;
1730 pd->ep_addr = -1; /* not an endpoint */
1731 pd->fifo_index = f_tx->fifo_index & f_rx->fifo_index;
1732 pd->mode = FREAD|FWRITE;
1734 /* Now, create the device itself */
1735 f_sc->dev = make_dev(&usb_devsw, 0, uid, gid, mode,
1737 /* XXX setting si_drv1 and creating the device is not atomic! */
1738 f_sc->dev->si_drv1 = pd;
1741 DPRINTFN(2, "attached %p/%p\n", f_tx, f_rx);
1745 /*------------------------------------------------------------------------*
1746 * usb_fifo_alloc_buffer
1751 *------------------------------------------------------------------------*/
1753 usb_fifo_alloc_buffer(struct usb_fifo *f, usb_size_t bufsize,
1756 usb_fifo_free_buffer(f);
1758 /* allocate an endpoint */
1759 f->free_q.ifq_maxlen = nbuf;
1760 f->used_q.ifq_maxlen = nbuf;
1762 f->queue_data = usb_alloc_mbufs(
1763 M_USBDEV, &f->free_q, bufsize, nbuf);
1765 if ((f->queue_data == NULL) && bufsize && nbuf) {
1768 return (0); /* success */
1771 /*------------------------------------------------------------------------*
1772 * usb_fifo_free_buffer
1774 * This function will free the buffers associated with a FIFO. This
1775 * function can be called multiple times in a row.
1776 *------------------------------------------------------------------------*/
1778 usb_fifo_free_buffer(struct usb_fifo *f)
1780 if (f->queue_data) {
1781 /* free old buffer */
1782 free(f->queue_data, M_USBDEV);
1783 f->queue_data = NULL;
1787 bzero(&f->free_q, sizeof(f->free_q));
1788 bzero(&f->used_q, sizeof(f->used_q));
1792 usb_fifo_cleanup(void* ptr)
1794 free(ptr, M_USBDEV);
1798 usb_fifo_detach(struct usb_fifo_sc *f_sc)
1803 usb_fifo_free(f_sc->fp[USB_FIFO_TX]);
1804 usb_fifo_free(f_sc->fp[USB_FIFO_RX]);
1806 f_sc->fp[USB_FIFO_TX] = NULL;
1807 f_sc->fp[USB_FIFO_RX] = NULL;
1809 if (f_sc->dev != NULL) {
1810 destroy_dev_sched_cb(f_sc->dev,
1811 usb_fifo_cleanup, f_sc->dev->si_drv1);
1815 DPRINTFN(2, "detached %p\n", f_sc);
1819 usb_fifo_put_bytes_max(struct usb_fifo *f)
1824 USB_IF_POLL(&f->free_q, m);
1827 len = m->max_data_len;
1834 /*------------------------------------------------------------------------*
1838 * 0 - normal operation
1839 * 1 - set last packet flag to enforce framing
1840 *------------------------------------------------------------------------*/
1842 usb_fifo_put_data(struct usb_fifo *f, struct usb_page_cache *pc,
1843 usb_frlength_t offset, usb_frlength_t len, uint8_t what)
1846 usb_frlength_t io_len;
1848 while (len || (what == 1)) {
1850 USB_IF_DEQUEUE(&f->free_q, m);
1855 io_len = MIN(len, m->cur_data_len);
1857 usbd_copy_out(pc, offset, m->cur_data_ptr, io_len);
1859 m->cur_data_len = io_len;
1863 if ((len == 0) && (what == 1)) {
1866 USB_IF_ENQUEUE(&f->used_q, m);
1870 if ((len == 0) || (what == 1)) {
1880 usb_fifo_put_data_linear(struct usb_fifo *f, void *ptr,
1881 usb_size_t len, uint8_t what)
1886 while (len || (what == 1)) {
1888 USB_IF_DEQUEUE(&f->free_q, m);
1893 io_len = MIN(len, m->cur_data_len);
1895 bcopy(ptr, m->cur_data_ptr, io_len);
1897 m->cur_data_len = io_len;
1898 ptr = USB_ADD_BYTES(ptr, io_len);
1901 if ((len == 0) && (what == 1)) {
1904 USB_IF_ENQUEUE(&f->used_q, m);
1908 if ((len == 0) || (what == 1)) {
1918 usb_fifo_put_data_buffer(struct usb_fifo *f, void *ptr, usb_size_t len)
1922 USB_IF_DEQUEUE(&f->free_q, m);
1925 m->cur_data_len = len;
1926 m->cur_data_ptr = ptr;
1927 USB_IF_ENQUEUE(&f->used_q, m);
1935 usb_fifo_put_data_error(struct usb_fifo *f)
1937 f->flag_iserror = 1;
1941 /*------------------------------------------------------------------------*
1945 * 0 - normal operation
1946 * 1 - only get one "usb_mbuf"
1950 * 1 - data in buffer
1951 *------------------------------------------------------------------------*/
1953 usb_fifo_get_data(struct usb_fifo *f, struct usb_page_cache *pc,
1954 usb_frlength_t offset, usb_frlength_t len, usb_frlength_t *actlen,
1958 usb_frlength_t io_len;
1959 uint8_t tr_data = 0;
1965 USB_IF_DEQUEUE(&f->used_q, m);
1971 io_len = MIN(len, m->cur_data_len);
1973 usbd_copy_in(pc, offset, m->cur_data_ptr, io_len);
1977 actlen[0] += io_len;
1978 m->cur_data_ptr += io_len;
1979 m->cur_data_len -= io_len;
1981 if ((m->cur_data_len == 0) || (what == 1)) {
1982 USB_IF_ENQUEUE(&f->free_q, m);
1990 USB_IF_PREPEND(&f->used_q, m);
1995 /* wait for data to be written out */
1998 if (f->flag_flushing) {
1999 /* check if we should send a short packet */
2000 if (f->flag_short != 0) {
2005 /* flushing complete */
2006 f->flag_flushing = 0;
2019 usb_fifo_get_data_linear(struct usb_fifo *f, void *ptr,
2020 usb_size_t len, usb_size_t *actlen, uint8_t what)
2024 uint8_t tr_data = 0;
2030 USB_IF_DEQUEUE(&f->used_q, m);
2036 io_len = MIN(len, m->cur_data_len);
2038 bcopy(m->cur_data_ptr, ptr, io_len);
2041 ptr = USB_ADD_BYTES(ptr, io_len);
2042 actlen[0] += io_len;
2043 m->cur_data_ptr += io_len;
2044 m->cur_data_len -= io_len;
2046 if ((m->cur_data_len == 0) || (what == 1)) {
2047 USB_IF_ENQUEUE(&f->free_q, m);
2055 USB_IF_PREPEND(&f->used_q, m);
2060 /* wait for data to be written out */
2063 if (f->flag_flushing) {
2064 /* check if we should send a short packet */
2065 if (f->flag_short != 0) {
2070 /* flushing complete */
2071 f->flag_flushing = 0;
2084 usb_fifo_get_data_buffer(struct usb_fifo *f, void **pptr, usb_size_t *plen)
2088 USB_IF_POLL(&f->used_q, m);
2091 *plen = m->cur_data_len;
2092 *pptr = m->cur_data_ptr;
2100 usb_fifo_get_data_error(struct usb_fifo *f)
2102 f->flag_iserror = 1;
2106 /*------------------------------------------------------------------------*
2111 * Else: Pointer to symlink entry
2112 *------------------------------------------------------------------------*/
2113 struct usb_symlink *
2114 usb_alloc_symlink(const char *target)
2116 struct usb_symlink *ps;
2118 ps = malloc(sizeof(*ps), M_USBDEV, M_WAITOK);
2122 /* XXX no longer needed */
2123 strlcpy(ps->src_path, target, sizeof(ps->src_path));
2124 ps->src_len = strlen(ps->src_path);
2125 strlcpy(ps->dst_path, target, sizeof(ps->dst_path));
2126 ps->dst_len = strlen(ps->dst_path);
2128 sx_xlock(&usb_sym_lock);
2129 TAILQ_INSERT_TAIL(&usb_sym_head, ps, sym_entry);
2130 sx_unlock(&usb_sym_lock);
2134 /*------------------------------------------------------------------------*
2136 *------------------------------------------------------------------------*/
2138 usb_free_symlink(struct usb_symlink *ps)
2143 sx_xlock(&usb_sym_lock);
2144 TAILQ_REMOVE(&usb_sym_head, ps, sym_entry);
2145 sx_unlock(&usb_sym_lock);
2150 /*------------------------------------------------------------------------*
2156 *------------------------------------------------------------------------*/
2158 usb_read_symlink(uint8_t *user_ptr, uint32_t startentry, uint32_t user_len)
2160 struct usb_symlink *ps;
2166 sx_xlock(&usb_sym_lock);
2168 TAILQ_FOREACH(ps, &usb_sym_head, sym_entry) {
2171 * Compute total length of source and destination symlink
2172 * strings pluss one length byte and two NUL bytes:
2174 temp = ps->src_len + ps->dst_len + 3;
2178 * Skip entry because this length cannot fit
2183 if (startentry != 0) {
2184 /* decrement read offset */
2188 if (temp > user_len) {
2189 /* out of buffer space */
2194 /* copy out total length */
2196 error = copyout(&len,
2197 USB_ADD_BYTES(user_ptr, delta), 1);
2203 /* copy out source string */
2205 error = copyout(ps->src_path,
2206 USB_ADD_BYTES(user_ptr, delta), ps->src_len);
2211 delta += ps->src_len;
2212 error = copyout(&len,
2213 USB_ADD_BYTES(user_ptr, delta), 1);
2219 /* copy out destination string */
2221 error = copyout(ps->dst_path,
2222 USB_ADD_BYTES(user_ptr, delta), ps->dst_len);
2227 delta += ps->dst_len;
2228 error = copyout(&len,
2229 USB_ADD_BYTES(user_ptr, delta), 1);
2238 /* a zero length entry indicates the end */
2240 if ((user_len != 0) && (error == 0)) {
2244 error = copyout(&len,
2245 USB_ADD_BYTES(user_ptr, delta), 1);
2247 sx_unlock(&usb_sym_lock);
2252 usb_fifo_set_close_zlp(struct usb_fifo *f, uint8_t onoff)
2257 /* send a Zero Length Packet, ZLP, before close */
2258 f->flag_short = onoff;
2262 usb_fifo_set_write_defrag(struct usb_fifo *f, uint8_t onoff)
2267 /* defrag written data */
2268 f->flag_write_defrag = onoff;
2269 /* reset defrag state */
2270 f->flag_have_fragment = 0;
2274 usb_fifo_softc(struct usb_fifo *f)
2276 return (f->priv_sc0);
2278 #endif /* USB_HAVE_UGEN */