3 * Copyright (c) 2006-2008 Hans Petter Selasky. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * usb_dev.c - An abstraction layer for creating devices under /dev/...
30 #include <sys/stdint.h>
31 #include <sys/stddef.h>
32 #include <sys/param.h>
33 #include <sys/queue.h>
34 #include <sys/types.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/module.h>
40 #include <sys/mutex.h>
41 #include <sys/condvar.h>
42 #include <sys/sysctl.h>
44 #include <sys/unistd.h>
45 #include <sys/callout.h>
46 #include <sys/malloc.h>
48 #include <sys/vnode.h>
50 #include <sys/fcntl.h>
52 #include <dev/usb/usb.h>
53 #include <dev/usb/usb_ioctl.h>
54 #include <dev/usb/usbdi.h>
55 #include <dev/usb/usbdi_util.h>
57 #define USB_DEBUG_VAR usb_fifo_debug
59 #include <dev/usb/usb_core.h>
60 #include <dev/usb/usb_dev.h>
61 #include <dev/usb/usb_mbuf.h>
62 #include <dev/usb/usb_process.h>
63 #include <dev/usb/usb_device.h>
64 #include <dev/usb/usb_debug.h>
65 #include <dev/usb/usb_busdma.h>
66 #include <dev/usb/usb_generic.h>
67 #include <dev/usb/usb_dynamic.h>
68 #include <dev/usb/usb_util.h>
70 #include <dev/usb/usb_controller.h>
71 #include <dev/usb/usb_bus.h>
73 #include <sys/filio.h>
74 #include <sys/ttycom.h>
75 #include <sys/syscallsubr.h>
77 #include <machine/stdarg.h>
82 static int usb_fifo_debug = 0;
84 SYSCTL_NODE(_hw_usb, OID_AUTO, dev, CTLFLAG_RW, 0, "USB device");
85 SYSCTL_INT(_hw_usb_dev, OID_AUTO, debug, CTLFLAG_RW | CTLFLAG_TUN,
86 &usb_fifo_debug, 0, "Debug Level");
87 TUNABLE_INT("hw.usb.dev.debug", &usb_fifo_debug);
90 #if ((__FreeBSD_version >= 700001) || (__FreeBSD_version == 0) || \
91 ((__FreeBSD_version >= 600034) && (__FreeBSD_version < 700000)))
92 #define USB_UCRED struct ucred *ucred,
99 static int usb_fifo_open(struct usb_cdev_privdata *,
100 struct usb_fifo *, int);
101 static void usb_fifo_close(struct usb_fifo *, int);
102 static void usb_dev_init(void *);
103 static void usb_dev_init_post(void *);
104 static void usb_dev_uninit(void *);
105 static int usb_fifo_uiomove(struct usb_fifo *, void *, int,
107 static void usb_fifo_check_methods(struct usb_fifo_methods *);
108 static struct usb_fifo *usb_fifo_alloc(void);
109 static struct usb_endpoint *usb_dev_get_ep(struct usb_device *, uint8_t,
111 static void usb_loc_fill(struct usb_fs_privdata *,
112 struct usb_cdev_privdata *);
113 static void usb_close(void *);
114 static usb_error_t usb_ref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *, int);
115 static usb_error_t usb_usb_ref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *);
116 static void usb_unref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *);
118 static d_open_t usb_open;
119 static d_ioctl_t usb_ioctl;
120 static d_read_t usb_read;
121 static d_write_t usb_write;
122 static d_poll_t usb_poll;
124 static d_ioctl_t usb_static_ioctl;
126 static usb_fifo_open_t usb_fifo_dummy_open;
127 static usb_fifo_close_t usb_fifo_dummy_close;
128 static usb_fifo_ioctl_t usb_fifo_dummy_ioctl;
129 static usb_fifo_cmd_t usb_fifo_dummy_cmd;
131 /* character device structure used for devices (/dev/ugenX.Y and /dev/uXXX) */
132 struct cdevsw usb_devsw = {
133 .d_version = D_VERSION,
135 .d_ioctl = usb_ioctl,
137 .d_flags = D_TRACKCLOSE,
139 .d_write = usb_write,
143 static struct cdev* usb_dev = NULL;
145 /* character device structure used for /dev/usb */
146 static struct cdevsw usb_static_devsw = {
147 .d_version = D_VERSION,
148 .d_ioctl = usb_static_ioctl,
152 static TAILQ_HEAD(, usb_symlink) usb_sym_head;
153 static struct sx usb_sym_lock;
155 struct mtx usb_ref_lock;
157 /*------------------------------------------------------------------------*
160 * This is used to fill out a usb_cdev_privdata structure based on the
161 * device's address as contained in usb_fs_privdata.
162 *------------------------------------------------------------------------*/
164 usb_loc_fill(struct usb_fs_privdata* pd, struct usb_cdev_privdata *cpd)
166 cpd->bus_index = pd->bus_index;
167 cpd->dev_index = pd->dev_index;
168 cpd->ep_addr = pd->ep_addr;
169 cpd->fifo_index = pd->fifo_index;
172 /*------------------------------------------------------------------------*
175 * This function is used to atomically refer an USB device by its
176 * device location. If this function returns success the USB device
177 * will not dissappear until the USB device is unreferenced.
180 * 0: Success, refcount incremented on the given USB device.
182 *------------------------------------------------------------------------*/
184 usb_ref_device(struct usb_cdev_privdata *cpd,
185 struct usb_cdev_refdata *crd, int need_uref)
187 struct usb_fifo **ppf;
190 DPRINTFN(2, "cpd=%p need uref=%d\n", cpd, need_uref);
193 memset(crd, 0, sizeof(*crd));
195 mtx_lock(&usb_ref_lock);
196 cpd->bus = devclass_get_softc(usb_devclass_ptr, cpd->bus_index);
197 if (cpd->bus == NULL) {
198 DPRINTFN(2, "no bus at %u\n", cpd->bus_index);
201 cpd->udev = cpd->bus->devices[cpd->dev_index];
202 if (cpd->udev == NULL) {
203 DPRINTFN(2, "no device at %u\n", cpd->dev_index);
206 if (cpd->udev->state == USB_STATE_DETACHED &&
208 DPRINTFN(2, "device is detached\n");
212 DPRINTFN(2, "ref udev - needed\n");
214 if (cpd->udev->refcount == USB_DEV_REF_MAX) {
215 DPRINTFN(2, "no dev ref\n");
218 cpd->udev->refcount++;
220 mtx_unlock(&usb_ref_lock);
223 * We need to grab the enumeration SX-lock before
224 * grabbing the FIFO refs to avoid deadlock at detach!
226 crd->do_unlock = usbd_enum_lock(cpd->udev);
228 mtx_lock(&usb_ref_lock);
231 * Set "is_uref" after grabbing the default SX lock
236 /* check if we are doing an open */
237 if (cpd->fflags == 0) {
238 /* use zero defaults */
240 /* check for write */
241 if (cpd->fflags & FWRITE) {
242 ppf = cpd->udev->fifo;
243 f = ppf[cpd->fifo_index + USB_FIFO_TX];
245 crd->is_write = 1; /* ref */
246 if (f == NULL || f->refcount == USB_FIFO_REF_MAX)
248 if (f->curr_cpd != cpd)
250 /* check if USB-FS is active */
251 if (f->fs_ep_max != 0) {
257 if (cpd->fflags & FREAD) {
258 ppf = cpd->udev->fifo;
259 f = ppf[cpd->fifo_index + USB_FIFO_RX];
261 crd->is_read = 1; /* ref */
262 if (f == NULL || f->refcount == USB_FIFO_REF_MAX)
264 if (f->curr_cpd != cpd)
266 /* check if USB-FS is active */
267 if (f->fs_ep_max != 0) {
273 /* when everything is OK we increment the refcounts */
275 DPRINTFN(2, "ref write\n");
276 crd->txfifo->refcount++;
279 DPRINTFN(2, "ref read\n");
280 crd->rxfifo->refcount++;
282 mtx_unlock(&usb_ref_lock);
288 usbd_enum_unlock(cpd->udev);
291 if (--(cpd->udev->refcount) == 0)
292 cv_broadcast(&cpd->udev->ref_cv);
294 mtx_unlock(&usb_ref_lock);
295 DPRINTFN(2, "fail\n");
298 memset(crd, 0, sizeof(*crd));
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);
363 if (--(cpd->udev->refcount) == 0)
364 cv_broadcast(&cpd->udev->ref_cv);
366 mtx_unlock(&usb_ref_lock);
369 static struct usb_fifo *
374 f = malloc(sizeof(*f), M_USBDEV, M_WAITOK | M_ZERO);
376 cv_init(&f->cv_io, "FIFO-IO");
377 cv_init(&f->cv_drain, "FIFO-DRAIN");
383 /*------------------------------------------------------------------------*
385 *------------------------------------------------------------------------*/
387 usb_fifo_create(struct usb_cdev_privdata *cpd,
388 struct usb_cdev_refdata *crd)
390 struct usb_device *udev = cpd->udev;
392 struct usb_endpoint *ep;
398 int e = cpd->ep_addr;
400 is_tx = (cpd->fflags & FWRITE) ? 1 : 0;
401 is_rx = (cpd->fflags & FREAD) ? 1 : 0;
405 /* Preallocated FIFO */
407 DPRINTFN(5, "Preallocated FIFO\n");
409 f = udev->fifo[cpd->fifo_index + USB_FIFO_TX];
415 f = udev->fifo[cpd->fifo_index + USB_FIFO_RX];
423 KASSERT(e >= 0 && e <= 15, ("endpoint %d out of range", e));
425 /* search for a free FIFO slot */
426 DPRINTFN(5, "Endpoint device, searching for 0x%02x\n", e);
427 for (n = 0;; n += 2) {
429 if (n == USB_FIFO_MAX) {
434 /* end of FIFOs reached */
435 DPRINTFN(5, "out of FIFOs\n");
439 /* Check for TX FIFO */
441 f = udev->fifo[n + USB_FIFO_TX];
443 if (f->dev_ep_index != e) {
444 /* wrong endpoint index */
447 if (f->curr_cpd != NULL) {
452 } else if (no_null) {
456 /* Check for RX FIFO */
458 f = udev->fifo[n + USB_FIFO_RX];
460 if (f->dev_ep_index != e) {
461 /* wrong endpoint index */
464 if (f->curr_cpd != NULL) {
469 } else if (no_null) {
477 if (e >= (USB_EP_MAX / 2)) {
478 /* we don't create any endpoints in this range */
479 DPRINTFN(5, "ep out of range\n");
480 return (is_busy ? EBUSY : EINVAL);
484 if ((e != 0) && is_busy) {
486 * Only the default control endpoint is allowed to be
487 * opened multiple times!
489 DPRINTFN(5, "busy\n");
495 (udev->fifo[n + USB_FIFO_TX] == NULL)) {
496 ep = usb_dev_get_ep(udev, e, USB_FIFO_TX);
497 DPRINTFN(5, "dev_get_endpoint(%d, 0x%x)\n", e, USB_FIFO_TX);
499 DPRINTFN(5, "dev_get_endpoint returned NULL\n");
502 f = usb_fifo_alloc();
504 DPRINTFN(5, "could not alloc tx fifo\n");
507 /* update some fields */
508 f->fifo_index = n + USB_FIFO_TX;
510 f->priv_mtx = &udev->device_mtx;
512 f->methods = &usb_ugen_methods;
513 f->iface_index = ep->iface_index;
515 mtx_lock(&usb_ref_lock);
516 udev->fifo[n + USB_FIFO_TX] = f;
517 mtx_unlock(&usb_ref_lock);
521 (udev->fifo[n + USB_FIFO_RX] == NULL)) {
523 ep = usb_dev_get_ep(udev, e, USB_FIFO_RX);
524 DPRINTFN(5, "dev_get_endpoint(%d, 0x%x)\n", e, USB_FIFO_RX);
526 DPRINTFN(5, "dev_get_endpoint returned NULL\n");
529 f = usb_fifo_alloc();
531 DPRINTFN(5, "could not alloc rx fifo\n");
534 /* update some fields */
535 f->fifo_index = n + USB_FIFO_RX;
537 f->priv_mtx = &udev->device_mtx;
539 f->methods = &usb_ugen_methods;
540 f->iface_index = ep->iface_index;
542 mtx_lock(&usb_ref_lock);
543 udev->fifo[n + USB_FIFO_RX] = f;
544 mtx_unlock(&usb_ref_lock);
547 crd->txfifo = udev->fifo[n + USB_FIFO_TX];
550 crd->rxfifo = udev->fifo[n + USB_FIFO_RX];
552 /* fill out fifo index */
553 DPRINTFN(5, "fifo index = %d\n", n);
562 usb_fifo_free(struct usb_fifo *f)
570 /* destroy symlink devices, if any */
571 for (n = 0; n != 2; n++) {
573 usb_free_symlink(f->symlink[n]);
574 f->symlink[n] = NULL;
577 mtx_lock(&usb_ref_lock);
579 /* delink ourselves to stop calls from userland */
580 if ((f->fifo_index < USB_FIFO_MAX) &&
582 (f->udev->fifo[f->fifo_index] == f)) {
583 f->udev->fifo[f->fifo_index] = NULL;
585 DPRINTFN(0, "USB FIFO %p has not been linked\n", f);
588 /* decrease refcount */
590 /* need to wait until all callers have exited */
591 while (f->refcount != 0) {
592 mtx_unlock(&usb_ref_lock); /* avoid LOR */
593 mtx_lock(f->priv_mtx);
594 /* prevent write flush, if any */
596 /* get I/O thread out of any sleep state */
597 if (f->flag_sleeping) {
598 f->flag_sleeping = 0;
599 cv_broadcast(&f->cv_io);
601 mtx_unlock(f->priv_mtx);
602 mtx_lock(&usb_ref_lock);
605 * Check if the "f->refcount" variable reached zero
606 * during the unlocked time before entering wait:
608 if (f->refcount == 0)
612 cv_wait(&f->cv_drain, &usb_ref_lock);
614 mtx_unlock(&usb_ref_lock);
616 /* take care of closing the device here, if any */
617 usb_fifo_close(f, 0);
619 cv_destroy(&f->cv_io);
620 cv_destroy(&f->cv_drain);
625 static struct usb_endpoint *
626 usb_dev_get_ep(struct usb_device *udev, uint8_t ep_index, uint8_t dir)
628 struct usb_endpoint *ep;
634 if (dir == USB_FIFO_RX) {
635 if (udev->flags.usb_mode == USB_MODE_HOST) {
641 if (udev->flags.usb_mode == USB_MODE_HOST) {
647 ep = usbd_get_ep_by_addr(udev, ep_index | ep_dir);
651 /* if the endpoint does not exist then return */
654 if (ep->edesc == NULL) {
655 /* invalid endpoint */
658 return (ep); /* success */
661 /*------------------------------------------------------------------------*
667 *------------------------------------------------------------------------*/
669 usb_fifo_open(struct usb_cdev_privdata *cpd,
670 struct usb_fifo *f, int fflags)
676 DPRINTFN(2, "no FIFO\n");
679 /* remove FWRITE and FREAD flags */
680 fflags &= ~(FWRITE | FREAD);
682 /* set correct file flags */
683 if ((f->fifo_index & 1) == USB_FIFO_TX) {
689 /* check if we are already opened */
690 /* we don't need any locks when checking this variable */
691 if (f->curr_cpd != NULL) {
696 /* reset short flag before open */
699 /* call open method */
700 err = (f->methods->f_open) (f, fflags);
704 mtx_lock(f->priv_mtx);
706 /* reset sleep flag */
707 f->flag_sleeping = 0;
709 /* reset error flag */
712 /* reset complete flag */
713 f->flag_iscomplete = 0;
715 /* reset select flag */
716 f->flag_isselect = 0;
718 /* reset flushing flag */
719 f->flag_flushing = 0;
721 /* reset ASYNC proc flag */
724 mtx_lock(&usb_ref_lock);
725 /* flag the fifo as opened to prevent others */
727 mtx_unlock(&usb_ref_lock);
732 mtx_unlock(f->priv_mtx);
737 /*------------------------------------------------------------------------*
739 *------------------------------------------------------------------------*/
741 usb_fifo_reset(struct usb_fifo *f)
749 USB_IF_DEQUEUE(&f->used_q, m);
751 USB_IF_ENQUEUE(&f->free_q, m);
756 /* reset have fragment flag */
757 f->flag_have_fragment = 0;
760 /*------------------------------------------------------------------------*
762 *------------------------------------------------------------------------*/
764 usb_fifo_close(struct usb_fifo *f, int fflags)
768 /* check if we are not opened */
769 if (f->curr_cpd == NULL) {
770 /* nothing to do - already closed */
773 mtx_lock(f->priv_mtx);
775 /* clear current cdev private data pointer */
778 /* check if we are selected */
779 if (f->flag_isselect) {
780 selwakeup(&f->selinfo);
781 f->flag_isselect = 0;
783 /* check if a thread wants SIGIO */
784 if (f->async_p != NULL) {
785 PROC_LOCK(f->async_p);
786 psignal(f->async_p, SIGIO);
787 PROC_UNLOCK(f->async_p);
790 /* remove FWRITE and FREAD flags */
791 fflags &= ~(FWRITE | FREAD);
793 /* flush written data, if any */
794 if ((f->fifo_index & 1) == USB_FIFO_TX) {
796 if (!f->flag_iserror) {
798 /* set flushing flag */
799 f->flag_flushing = 1;
801 /* get the last packet in */
802 if (f->flag_have_fragment) {
804 f->flag_have_fragment = 0;
805 USB_IF_DEQUEUE(&f->free_q, m);
807 USB_IF_ENQUEUE(&f->used_q, m);
811 /* start write transfer, if not already started */
812 (f->methods->f_start_write) (f);
814 /* check if flushed already */
815 while (f->flag_flushing &&
816 (!f->flag_iserror)) {
817 /* wait until all data has been written */
818 f->flag_sleeping = 1;
819 err = cv_wait_sig(&f->cv_io, f->priv_mtx);
821 DPRINTF("signal received\n");
828 /* stop write transfer, if not already stopped */
829 (f->methods->f_stop_write) (f);
833 /* stop write transfer, if not already stopped */
834 (f->methods->f_stop_read) (f);
837 /* check if we are sleeping */
838 if (f->flag_sleeping) {
839 DPRINTFN(2, "Sleeping at close!\n");
841 mtx_unlock(f->priv_mtx);
843 /* call close method */
844 (f->methods->f_close) (f, fflags);
849 /*------------------------------------------------------------------------*
850 * usb_open - cdev callback
851 *------------------------------------------------------------------------*/
853 usb_open(struct cdev *dev, int fflags, int devtype, struct thread *td)
855 struct usb_fs_privdata* pd = (struct usb_fs_privdata*)dev->si_drv1;
856 struct usb_cdev_refdata refs;
857 struct usb_cdev_privdata *cpd;
860 DPRINTFN(2, "%s fflags=0x%08x\n", dev->si_name, fflags);
862 KASSERT(fflags & (FREAD|FWRITE), ("invalid open flags"));
863 if (((fflags & FREAD) && !(pd->mode & FREAD)) ||
864 ((fflags & FWRITE) && !(pd->mode & FWRITE))) {
865 DPRINTFN(2, "access mode not supported\n");
869 cpd = malloc(sizeof(*cpd), M_USBDEV, M_WAITOK | M_ZERO);
870 ep = cpd->ep_addr = pd->ep_addr;
872 usb_loc_fill(pd, cpd);
873 err = usb_ref_device(cpd, &refs, 1);
875 DPRINTFN(2, "cannot ref device\n");
879 cpd->fflags = fflags; /* access mode for open lifetime */
881 /* create FIFOs, if any */
882 err = usb_fifo_create(cpd, &refs);
883 /* check for error */
885 DPRINTFN(2, "cannot create fifo\n");
886 usb_unref_device(cpd, &refs);
890 if (fflags & FREAD) {
891 err = usb_fifo_open(cpd, refs.rxfifo, fflags);
893 DPRINTFN(2, "read open failed\n");
894 usb_unref_device(cpd, &refs);
899 if (fflags & FWRITE) {
900 err = usb_fifo_open(cpd, refs.txfifo, fflags);
902 DPRINTFN(2, "write open failed\n");
903 if (fflags & FREAD) {
904 usb_fifo_close(refs.rxfifo, fflags);
906 usb_unref_device(cpd, &refs);
911 usb_unref_device(cpd, &refs);
912 devfs_set_cdevpriv(cpd, usb_close);
917 /*------------------------------------------------------------------------*
918 * usb_close - cdev callback
919 *------------------------------------------------------------------------*/
923 struct usb_cdev_refdata refs;
924 struct usb_cdev_privdata *cpd = arg;
927 DPRINTFN(2, "cpd=%p\n", cpd);
929 err = usb_ref_device(cpd, &refs,
930 2 /* uref and allow detached state */);
932 DPRINTFN(2, "Cannot grab USB reference when "
933 "closing USB file handle\n");
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)) {
1086 /* we lost the reference */
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 while (usb_ref_device(cpd, &refs, 1 /* need uref */)) {
1109 if (usb_ref_device(cpd, &refs, 0)) {
1110 /* device no longer exists */
1113 usb_unref_device(cpd, &refs);
1114 usb_pause_mtx(NULL, hz / 128);
1119 usb_unref_device(cpd, &refs);
1125 usb_poll(struct cdev* dev, int events, struct thread* td)
1127 struct usb_cdev_refdata refs;
1128 struct usb_cdev_privdata* cpd;
1131 int fflags, revents;
1133 if (devfs_get_cdevpriv((void **)&cpd) != 0 ||
1134 usb_ref_device(cpd, &refs, 0) != 0)
1136 (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM));
1138 fflags = cpd->fflags;
1140 /* Figure out who needs service */
1142 if ((events & (POLLOUT | POLLWRNORM)) &&
1143 (fflags & FWRITE)) {
1147 mtx_lock(f->priv_mtx);
1149 if (!refs.is_usbfs) {
1150 if (f->flag_iserror) {
1151 /* we got an error */
1154 if (f->queue_data == NULL) {
1156 * start write transfer, if not
1159 (f->methods->f_start_write) (f);
1161 /* check if any packets are available */
1162 USB_IF_POLL(&f->free_q, m);
1165 if (f->flag_iscomplete) {
1173 revents |= events & (POLLOUT | POLLWRNORM);
1175 f->flag_isselect = 1;
1176 selrecord(td, &f->selinfo);
1179 mtx_unlock(f->priv_mtx);
1181 if ((events & (POLLIN | POLLRDNORM)) &&
1186 mtx_lock(f->priv_mtx);
1188 if (!refs.is_usbfs) {
1189 if (f->flag_iserror) {
1190 /* we have and error */
1193 if (f->queue_data == NULL) {
1195 * start read transfer, if not
1198 (f->methods->f_start_read) (f);
1200 /* check if any packets are available */
1201 USB_IF_POLL(&f->used_q, m);
1204 if (f->flag_iscomplete) {
1212 revents |= events & (POLLIN | POLLRDNORM);
1214 f->flag_isselect = 1;
1215 selrecord(td, &f->selinfo);
1217 if (!refs.is_usbfs) {
1218 /* start reading data */
1219 (f->methods->f_start_read) (f);
1223 mtx_unlock(f->priv_mtx);
1225 usb_unref_device(cpd, &refs);
1230 usb_read(struct cdev *dev, struct uio *uio, int ioflag)
1232 struct usb_cdev_refdata refs;
1233 struct usb_cdev_privdata* cpd;
1240 uint8_t tr_data = 0;
1242 err = devfs_get_cdevpriv((void **)&cpd);
1246 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1250 fflags = cpd->fflags;
1254 /* should not happen */
1255 usb_unref_device(cpd, &refs);
1259 resid = uio->uio_resid;
1261 mtx_lock(f->priv_mtx);
1263 /* check for permanent read error */
1264 if (f->flag_iserror) {
1268 /* check if USB-FS interface is active */
1269 if (refs.is_usbfs) {
1271 * The queue is used for events that should be
1272 * retrieved using the "USB_FS_COMPLETE" ioctl.
1277 while (uio->uio_resid > 0) {
1279 USB_IF_DEQUEUE(&f->used_q, m);
1283 /* start read transfer, if not already started */
1285 (f->methods->f_start_read) (f);
1287 if (ioflag & IO_NDELAY) {
1289 /* return length before error */
1295 DPRINTF("sleeping\n");
1297 err = usb_fifo_wait(f);
1303 if (f->methods->f_filter_read) {
1305 * Sometimes it is convenient to process data at the
1306 * expense of a userland process instead of a kernel
1309 (f->methods->f_filter_read) (f, m);
1313 io_len = MIN(m->cur_data_len, uio->uio_resid);
1315 DPRINTFN(2, "transfer %d bytes from %p\n",
1316 io_len, m->cur_data_ptr);
1318 err = usb_fifo_uiomove(f,
1319 m->cur_data_ptr, io_len, uio);
1321 m->cur_data_len -= io_len;
1322 m->cur_data_ptr += io_len;
1324 if (m->cur_data_len == 0) {
1326 uint8_t last_packet;
1328 last_packet = m->last_packet;
1330 USB_IF_ENQUEUE(&f->free_q, m);
1337 USB_IF_PREPEND(&f->used_q, m);
1345 mtx_unlock(f->priv_mtx);
1347 usb_unref_device(cpd, &refs);
1353 usb_write(struct cdev *dev, struct uio *uio, int ioflag)
1355 struct usb_cdev_refdata refs;
1356 struct usb_cdev_privdata* cpd;
1364 uint8_t tr_data = 0;
1368 err = devfs_get_cdevpriv((void **)&cpd);
1372 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1376 fflags = cpd->fflags;
1380 /* should not happen */
1381 usb_unref_device(cpd, &refs);
1384 resid = uio->uio_resid;
1386 mtx_lock(f->priv_mtx);
1388 /* check for permanent write error */
1389 if (f->flag_iserror) {
1393 /* check if USB-FS interface is active */
1394 if (refs.is_usbfs) {
1396 * The queue is used for events that should be
1397 * retrieved using the "USB_FS_COMPLETE" ioctl.
1402 if (f->queue_data == NULL) {
1403 /* start write transfer, if not already started */
1404 (f->methods->f_start_write) (f);
1406 /* we allow writing zero length data */
1408 USB_IF_DEQUEUE(&f->free_q, m);
1412 if (ioflag & IO_NDELAY) {
1414 /* return length before error */
1420 DPRINTF("sleeping\n");
1422 err = usb_fifo_wait(f);
1430 if (f->flag_have_fragment == 0) {
1432 io_len = m->cur_data_len;
1433 pdata = m->cur_data_ptr;
1434 if (io_len > uio->uio_resid)
1435 io_len = uio->uio_resid;
1436 m->cur_data_len = io_len;
1438 io_len = m->max_data_len - m->cur_data_len;
1439 pdata = m->cur_data_ptr + m->cur_data_len;
1440 if (io_len > uio->uio_resid)
1441 io_len = uio->uio_resid;
1442 m->cur_data_len += io_len;
1445 DPRINTFN(2, "transfer %d bytes to %p\n",
1448 err = usb_fifo_uiomove(f, pdata, io_len, uio);
1451 f->flag_have_fragment = 0;
1452 USB_IF_ENQUEUE(&f->free_q, m);
1456 /* check if the buffer is ready to be transmitted */
1458 if ((f->flag_write_defrag == 0) ||
1459 (m->cur_data_len == m->max_data_len)) {
1460 f->flag_have_fragment = 0;
1463 * Check for write filter:
1465 * Sometimes it is convenient to process data
1466 * at the expense of a userland process
1467 * instead of a kernel process.
1469 if (f->methods->f_filter_write) {
1470 (f->methods->f_filter_write) (f, m);
1473 /* Put USB mbuf in the used queue */
1474 USB_IF_ENQUEUE(&f->used_q, m);
1476 /* Start writing data, if not already started */
1477 (f->methods->f_start_write) (f);
1479 /* Wait for more data or close */
1480 f->flag_have_fragment = 1;
1481 USB_IF_PREPEND(&f->free_q, m);
1484 } while (uio->uio_resid > 0);
1486 mtx_unlock(f->priv_mtx);
1488 usb_unref_device(cpd, &refs);
1494 usb_static_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
1498 struct usb_read_dir *urd;
1506 err = usb_read_symlink(u.urd->urd_data,
1507 u.urd->urd_startentry, u.urd->urd_maxlen);
1509 case USB_DEV_QUIRK_GET:
1510 case USB_QUIRK_NAME_GET:
1511 case USB_DEV_QUIRK_ADD:
1512 case USB_DEV_QUIRK_REMOVE:
1513 err = usb_quirk_ioctl_p(cmd, data, fflag, td);
1515 case USB_GET_TEMPLATE:
1516 *(int *)data = usb_template;
1519 case USB_SET_TEMPLATE:
1520 err = priv_check(curthread, PRIV_DRIVER);
1523 usb_template = *(int *)data;
1533 usb_fifo_uiomove(struct usb_fifo *f, void *cp,
1534 int n, struct uio *uio)
1538 mtx_unlock(f->priv_mtx);
1541 * "uiomove()" can sleep so one needs to make a wrapper,
1542 * exiting the mutex and checking things:
1544 error = uiomove(cp, n, uio);
1546 mtx_lock(f->priv_mtx);
1552 usb_fifo_wait(struct usb_fifo *f)
1556 mtx_assert(f->priv_mtx, MA_OWNED);
1558 if (f->flag_iserror) {
1562 f->flag_sleeping = 1;
1564 err = cv_wait_sig(&f->cv_io, f->priv_mtx);
1566 if (f->flag_iserror) {
1574 usb_fifo_signal(struct usb_fifo *f)
1576 if (f->flag_sleeping) {
1577 f->flag_sleeping = 0;
1578 cv_broadcast(&f->cv_io);
1583 usb_fifo_wakeup(struct usb_fifo *f)
1587 if (f->flag_isselect) {
1588 selwakeup(&f->selinfo);
1589 f->flag_isselect = 0;
1591 if (f->async_p != NULL) {
1592 PROC_LOCK(f->async_p);
1593 psignal(f->async_p, SIGIO);
1594 PROC_UNLOCK(f->async_p);
1599 usb_fifo_dummy_open(struct usb_fifo *fifo, int fflags)
1605 usb_fifo_dummy_close(struct usb_fifo *fifo, int fflags)
1611 usb_fifo_dummy_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
1617 usb_fifo_dummy_cmd(struct usb_fifo *fifo)
1619 fifo->flag_flushing = 0; /* not flushing */
1623 usb_fifo_check_methods(struct usb_fifo_methods *pm)
1625 /* check that all callback functions are OK */
1627 if (pm->f_open == NULL)
1628 pm->f_open = &usb_fifo_dummy_open;
1630 if (pm->f_close == NULL)
1631 pm->f_close = &usb_fifo_dummy_close;
1633 if (pm->f_ioctl == NULL)
1634 pm->f_ioctl = &usb_fifo_dummy_ioctl;
1636 if (pm->f_ioctl_post == NULL)
1637 pm->f_ioctl_post = &usb_fifo_dummy_ioctl;
1639 if (pm->f_start_read == NULL)
1640 pm->f_start_read = &usb_fifo_dummy_cmd;
1642 if (pm->f_stop_read == NULL)
1643 pm->f_stop_read = &usb_fifo_dummy_cmd;
1645 if (pm->f_start_write == NULL)
1646 pm->f_start_write = &usb_fifo_dummy_cmd;
1648 if (pm->f_stop_write == NULL)
1649 pm->f_stop_write = &usb_fifo_dummy_cmd;
1652 /*------------------------------------------------------------------------*
1655 * The following function will create a duplex FIFO.
1660 *------------------------------------------------------------------------*/
1662 usb_fifo_attach(struct usb_device *udev, void *priv_sc,
1663 struct mtx *priv_mtx, struct usb_fifo_methods *pm,
1664 struct usb_fifo_sc *f_sc, uint16_t unit, int16_t subunit,
1665 uint8_t iface_index, uid_t uid, gid_t gid, int mode)
1667 struct usb_fifo *f_tx;
1668 struct usb_fifo *f_rx;
1672 f_sc->fp[USB_FIFO_TX] = NULL;
1673 f_sc->fp[USB_FIFO_RX] = NULL;
1678 /* check the methods */
1679 usb_fifo_check_methods(pm);
1681 if (priv_mtx == NULL)
1684 /* search for a free FIFO slot */
1685 for (n = 0;; n += 2) {
1687 if (n == USB_FIFO_MAX) {
1688 /* end of FIFOs reached */
1691 /* Check for TX FIFO */
1692 if (udev->fifo[n + USB_FIFO_TX] != NULL) {
1695 /* Check for RX FIFO */
1696 if (udev->fifo[n + USB_FIFO_RX] != NULL) {
1702 f_tx = usb_fifo_alloc();
1703 f_rx = usb_fifo_alloc();
1705 if ((f_tx == NULL) || (f_rx == NULL)) {
1706 usb_fifo_free(f_tx);
1707 usb_fifo_free(f_rx);
1710 /* initialise FIFO structures */
1712 f_tx->fifo_index = n + USB_FIFO_TX;
1713 f_tx->dev_ep_index = -1;
1714 f_tx->priv_mtx = priv_mtx;
1715 f_tx->priv_sc0 = priv_sc;
1717 f_tx->iface_index = iface_index;
1720 f_rx->fifo_index = n + USB_FIFO_RX;
1721 f_rx->dev_ep_index = -1;
1722 f_rx->priv_mtx = priv_mtx;
1723 f_rx->priv_sc0 = priv_sc;
1725 f_rx->iface_index = iface_index;
1728 f_sc->fp[USB_FIFO_TX] = f_tx;
1729 f_sc->fp[USB_FIFO_RX] = f_rx;
1731 mtx_lock(&usb_ref_lock);
1732 udev->fifo[f_tx->fifo_index] = f_tx;
1733 udev->fifo[f_rx->fifo_index] = f_rx;
1734 mtx_unlock(&usb_ref_lock);
1736 for (n = 0; n != 4; n++) {
1738 if (pm->basename[n] == NULL) {
1742 if (snprintf(devname, sizeof(devname),
1743 "%s%u%s", pm->basename[n],
1744 unit, pm->postfix[n] ?
1745 pm->postfix[n] : "")) {
1749 if (snprintf(devname, sizeof(devname),
1750 "%s%u.%d%s", pm->basename[n],
1751 unit, subunit, pm->postfix[n] ?
1752 pm->postfix[n] : "")) {
1758 * Distribute the symbolic links into two FIFO structures:
1761 f_rx->symlink[n / 2] =
1762 usb_alloc_symlink(devname);
1764 f_tx->symlink[n / 2] =
1765 usb_alloc_symlink(devname);
1768 /* Create the device */
1769 f_sc->dev = usb_make_dev(udev, devname, -1,
1770 f_tx->fifo_index & f_rx->fifo_index,
1771 FREAD|FWRITE, uid, gid, mode);
1774 DPRINTFN(2, "attached %p/%p\n", f_tx, f_rx);
1778 /*------------------------------------------------------------------------*
1779 * usb_fifo_alloc_buffer
1784 *------------------------------------------------------------------------*/
1786 usb_fifo_alloc_buffer(struct usb_fifo *f, usb_size_t bufsize,
1789 usb_fifo_free_buffer(f);
1791 /* allocate an endpoint */
1792 f->free_q.ifq_maxlen = nbuf;
1793 f->used_q.ifq_maxlen = nbuf;
1795 f->queue_data = usb_alloc_mbufs(
1796 M_USBDEV, &f->free_q, bufsize, nbuf);
1798 if ((f->queue_data == NULL) && bufsize && nbuf) {
1801 return (0); /* success */
1804 /*------------------------------------------------------------------------*
1805 * usb_fifo_free_buffer
1807 * This function will free the buffers associated with a FIFO. This
1808 * function can be called multiple times in a row.
1809 *------------------------------------------------------------------------*/
1811 usb_fifo_free_buffer(struct usb_fifo *f)
1813 if (f->queue_data) {
1814 /* free old buffer */
1815 free(f->queue_data, M_USBDEV);
1816 f->queue_data = NULL;
1820 memset(&f->free_q, 0, sizeof(f->free_q));
1821 memset(&f->used_q, 0, sizeof(f->used_q));
1825 usb_fifo_detach(struct usb_fifo_sc *f_sc)
1830 usb_fifo_free(f_sc->fp[USB_FIFO_TX]);
1831 usb_fifo_free(f_sc->fp[USB_FIFO_RX]);
1833 f_sc->fp[USB_FIFO_TX] = NULL;
1834 f_sc->fp[USB_FIFO_RX] = NULL;
1836 usb_destroy_dev(f_sc->dev);
1840 DPRINTFN(2, "detached %p\n", f_sc);
1844 usb_fifo_put_bytes_max(struct usb_fifo *f)
1849 USB_IF_POLL(&f->free_q, m);
1852 len = m->max_data_len;
1859 /*------------------------------------------------------------------------*
1863 * 0 - normal operation
1864 * 1 - set last packet flag to enforce framing
1865 *------------------------------------------------------------------------*/
1867 usb_fifo_put_data(struct usb_fifo *f, struct usb_page_cache *pc,
1868 usb_frlength_t offset, usb_frlength_t len, uint8_t what)
1871 usb_frlength_t io_len;
1873 while (len || (what == 1)) {
1875 USB_IF_DEQUEUE(&f->free_q, m);
1880 io_len = MIN(len, m->cur_data_len);
1882 usbd_copy_out(pc, offset, m->cur_data_ptr, io_len);
1884 m->cur_data_len = io_len;
1888 if ((len == 0) && (what == 1)) {
1891 USB_IF_ENQUEUE(&f->used_q, m);
1895 if ((len == 0) || (what == 1)) {
1905 usb_fifo_put_data_linear(struct usb_fifo *f, void *ptr,
1906 usb_size_t len, uint8_t what)
1911 while (len || (what == 1)) {
1913 USB_IF_DEQUEUE(&f->free_q, m);
1918 io_len = MIN(len, m->cur_data_len);
1920 memcpy(m->cur_data_ptr, ptr, io_len);
1922 m->cur_data_len = io_len;
1923 ptr = USB_ADD_BYTES(ptr, io_len);
1926 if ((len == 0) && (what == 1)) {
1929 USB_IF_ENQUEUE(&f->used_q, m);
1933 if ((len == 0) || (what == 1)) {
1943 usb_fifo_put_data_buffer(struct usb_fifo *f, void *ptr, usb_size_t len)
1947 USB_IF_DEQUEUE(&f->free_q, m);
1950 m->cur_data_len = len;
1951 m->cur_data_ptr = ptr;
1952 USB_IF_ENQUEUE(&f->used_q, m);
1960 usb_fifo_put_data_error(struct usb_fifo *f)
1962 f->flag_iserror = 1;
1966 /*------------------------------------------------------------------------*
1970 * 0 - normal operation
1971 * 1 - only get one "usb_mbuf"
1975 * 1 - data in buffer
1976 *------------------------------------------------------------------------*/
1978 usb_fifo_get_data(struct usb_fifo *f, struct usb_page_cache *pc,
1979 usb_frlength_t offset, usb_frlength_t len, usb_frlength_t *actlen,
1983 usb_frlength_t io_len;
1984 uint8_t tr_data = 0;
1990 USB_IF_DEQUEUE(&f->used_q, m);
1996 io_len = MIN(len, m->cur_data_len);
1998 usbd_copy_in(pc, offset, m->cur_data_ptr, io_len);
2002 actlen[0] += io_len;
2003 m->cur_data_ptr += io_len;
2004 m->cur_data_len -= io_len;
2006 if ((m->cur_data_len == 0) || (what == 1)) {
2007 USB_IF_ENQUEUE(&f->free_q, m);
2015 USB_IF_PREPEND(&f->used_q, m);
2020 /* wait for data to be written out */
2023 if (f->flag_flushing) {
2024 /* check if we should send a short packet */
2025 if (f->flag_short != 0) {
2030 /* flushing complete */
2031 f->flag_flushing = 0;
2044 usb_fifo_get_data_linear(struct usb_fifo *f, void *ptr,
2045 usb_size_t len, usb_size_t *actlen, uint8_t what)
2049 uint8_t tr_data = 0;
2055 USB_IF_DEQUEUE(&f->used_q, m);
2061 io_len = MIN(len, m->cur_data_len);
2063 memcpy(ptr, m->cur_data_ptr, io_len);
2066 ptr = USB_ADD_BYTES(ptr, io_len);
2067 actlen[0] += io_len;
2068 m->cur_data_ptr += io_len;
2069 m->cur_data_len -= io_len;
2071 if ((m->cur_data_len == 0) || (what == 1)) {
2072 USB_IF_ENQUEUE(&f->free_q, m);
2080 USB_IF_PREPEND(&f->used_q, m);
2085 /* wait for data to be written out */
2088 if (f->flag_flushing) {
2089 /* check if we should send a short packet */
2090 if (f->flag_short != 0) {
2095 /* flushing complete */
2096 f->flag_flushing = 0;
2109 usb_fifo_get_data_buffer(struct usb_fifo *f, void **pptr, usb_size_t *plen)
2113 USB_IF_POLL(&f->used_q, m);
2116 *plen = m->cur_data_len;
2117 *pptr = m->cur_data_ptr;
2125 usb_fifo_get_data_error(struct usb_fifo *f)
2127 f->flag_iserror = 1;
2131 /*------------------------------------------------------------------------*
2136 * Else: Pointer to symlink entry
2137 *------------------------------------------------------------------------*/
2138 struct usb_symlink *
2139 usb_alloc_symlink(const char *target)
2141 struct usb_symlink *ps;
2143 ps = malloc(sizeof(*ps), M_USBDEV, M_WAITOK);
2147 /* XXX no longer needed */
2148 strlcpy(ps->src_path, target, sizeof(ps->src_path));
2149 ps->src_len = strlen(ps->src_path);
2150 strlcpy(ps->dst_path, target, sizeof(ps->dst_path));
2151 ps->dst_len = strlen(ps->dst_path);
2153 sx_xlock(&usb_sym_lock);
2154 TAILQ_INSERT_TAIL(&usb_sym_head, ps, sym_entry);
2155 sx_unlock(&usb_sym_lock);
2159 /*------------------------------------------------------------------------*
2161 *------------------------------------------------------------------------*/
2163 usb_free_symlink(struct usb_symlink *ps)
2168 sx_xlock(&usb_sym_lock);
2169 TAILQ_REMOVE(&usb_sym_head, ps, sym_entry);
2170 sx_unlock(&usb_sym_lock);
2175 /*------------------------------------------------------------------------*
2181 *------------------------------------------------------------------------*/
2183 usb_read_symlink(uint8_t *user_ptr, uint32_t startentry, uint32_t user_len)
2185 struct usb_symlink *ps;
2191 sx_xlock(&usb_sym_lock);
2193 TAILQ_FOREACH(ps, &usb_sym_head, sym_entry) {
2196 * Compute total length of source and destination symlink
2197 * strings pluss one length byte and two NUL bytes:
2199 temp = ps->src_len + ps->dst_len + 3;
2203 * Skip entry because this length cannot fit
2208 if (startentry != 0) {
2209 /* decrement read offset */
2213 if (temp > user_len) {
2214 /* out of buffer space */
2219 /* copy out total length */
2221 error = copyout(&len,
2222 USB_ADD_BYTES(user_ptr, delta), 1);
2228 /* copy out source string */
2230 error = copyout(ps->src_path,
2231 USB_ADD_BYTES(user_ptr, delta), ps->src_len);
2236 delta += ps->src_len;
2237 error = copyout(&len,
2238 USB_ADD_BYTES(user_ptr, delta), 1);
2244 /* copy out destination string */
2246 error = copyout(ps->dst_path,
2247 USB_ADD_BYTES(user_ptr, delta), ps->dst_len);
2252 delta += ps->dst_len;
2253 error = copyout(&len,
2254 USB_ADD_BYTES(user_ptr, delta), 1);
2263 /* a zero length entry indicates the end */
2265 if ((user_len != 0) && (error == 0)) {
2269 error = copyout(&len,
2270 USB_ADD_BYTES(user_ptr, delta), 1);
2272 sx_unlock(&usb_sym_lock);
2277 usb_fifo_set_close_zlp(struct usb_fifo *f, uint8_t onoff)
2282 /* send a Zero Length Packet, ZLP, before close */
2283 f->flag_short = onoff;
2287 usb_fifo_set_write_defrag(struct usb_fifo *f, uint8_t onoff)
2292 /* defrag written data */
2293 f->flag_write_defrag = onoff;
2294 /* reset defrag state */
2295 f->flag_have_fragment = 0;
2299 usb_fifo_softc(struct usb_fifo *f)
2301 return (f->priv_sc0);
2303 #endif /* USB_HAVE_UGEN */