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/param.h>
32 #include <sys/queue.h>
33 #include <sys/types.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
37 #include <sys/module.h>
39 #include <sys/mutex.h>
40 #include <sys/condvar.h>
41 #include <sys/sysctl.h>
42 #include <sys/unistd.h>
43 #include <sys/callout.h>
44 #include <sys/malloc.h>
46 #include <sys/vnode.h>
48 #include <sys/fcntl.h>
50 #include <bus/u4b/usb.h>
51 #include <bus/u4b/usb_ioctl.h>
52 #include <bus/u4b/usbdi.h>
53 #include <bus/u4b/usbdi_util.h>
55 #define USB_DEBUG_VAR usb_fifo_debug
57 #include <bus/u4b/usb_core.h>
58 #include <bus/u4b/usb_dev.h>
59 #include <bus/u4b/usb_mbuf.h>
60 #include <bus/u4b/usb_process.h>
61 #include <bus/u4b/usb_device.h>
62 #include <bus/u4b/usb_debug.h>
63 #include <bus/u4b/usb_busdma.h>
64 #include <bus/u4b/usb_generic.h>
65 #include <bus/u4b/usb_dynamic.h>
66 #include <bus/u4b/usb_util.h>
68 #include <bus/u4b/usb_controller.h>
69 #include <bus/u4b/usb_bus.h>
71 #include <sys/filio.h>
72 #include <sys/ttycom.h>
73 #include <sys/kern_syscall.h>
75 #include <machine/stdarg.h>
80 static int usb_fifo_debug = 0;
82 static SYSCTL_NODE(_hw_usb, OID_AUTO, dev, CTLFLAG_RW, 0, "USB device");
83 SYSCTL_INT(_hw_usb_dev, OID_AUTO, debug, CTLFLAG_RW,
84 &usb_fifo_debug, 0, "Debug Level");
86 TUNABLE_INT("hw.usb.dev.debug", &usb_fifo_debug);
89 #if ((__FreeBSD_version >= 700001) || (__FreeBSD_version == 0) || \
90 ((__FreeBSD_version >= 600034) && (__FreeBSD_version < 700000)))
91 #define USB_UCRED struct ucred *ucred,
98 static int usb_fifo_open(struct usb_cdev_privdata *,
99 struct usb_fifo *, int);
100 static void usb_fifo_close(struct usb_fifo *, int);
101 static void usb_dev_init(void *);
102 static void usb_dev_init_post(void *);
103 static void usb_dev_uninit(void *);
104 static int usb_fifo_uiomove(struct usb_fifo *, void *, int,
106 static void usb_fifo_check_methods(struct usb_fifo_methods *);
107 static struct usb_fifo *usb_fifo_alloc(void);
108 static struct usb_endpoint *usb_dev_get_ep(struct usb_device *, uint8_t,
110 static void usb_loc_fill(struct usb_fs_privdata *,
111 struct usb_cdev_privdata *);
112 static usb_error_t usb_ref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *, int);
113 static usb_error_t usb_usb_ref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *);
114 static void usb_unref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *);
116 static d_open_t usb_open;
117 static d_close_t usb_close;
118 static d_ioctl_t usb_ioctl;
119 static d_read_t usb_read;
120 static d_write_t usb_write;
121 static d_kqfilter_t usb_kqfilter;
122 static d_ioctl_t usb_static_ioctl;
124 static usb_fifo_open_t usb_fifo_dummy_open;
125 static usb_fifo_close_t usb_fifo_dummy_close;
126 static usb_fifo_ioctl_t usb_fifo_dummy_ioctl;
127 static usb_fifo_cmd_t usb_fifo_dummy_cmd;
129 /* character device structure used for devices (/dev/ugenX.Y and /dev/uXXX) */
130 struct dev_ops usb_devsw = {
131 /* .d_version = D_VERSION, */
132 { "usbdev", 0, D_MEM },
134 .d_close = usb_close,
135 .d_ioctl = usb_ioctl,
137 .d_write = usb_write,
138 .d_kqfilter = usb_kqfilter
141 static struct cdev* usb_dev = NULL;
143 /* character device structure used for /bus/u4b */
144 static struct dev_ops usb_static_devsw = {
146 /* .d_version = D_VERSION, */
147 .d_ioctl = usb_static_ioctl,
150 static TAILQ_HEAD(, usb_symlink) usb_sym_head;
151 static struct lock usb_sym_lock;
153 struct lock usb_ref_lock;
155 /*------------------------------------------------------------------------*
158 * This is used to fill out a usb_cdev_privdata structure based on the
159 * device's address as contained in usb_fs_privdata.
160 *------------------------------------------------------------------------*/
162 usb_loc_fill(struct usb_fs_privdata* pd, struct usb_cdev_privdata *cpd)
164 cpd->bus_index = pd->bus_index;
165 cpd->dev_index = pd->dev_index;
166 cpd->ep_addr = pd->ep_addr;
167 cpd->fifo_index = pd->fifo_index;
170 /*------------------------------------------------------------------------*
173 * This function is used to atomically refer an USB device by its
174 * device location. If this function returns success the USB device
175 * will not dissappear until the USB device is unreferenced.
178 * 0: Success, refcount incremented on the given USB device.
180 *------------------------------------------------------------------------*/
182 usb_ref_device(struct usb_cdev_privdata *cpd,
183 struct usb_cdev_refdata *crd, int need_uref)
185 struct usb_fifo **ppf;
188 DPRINTFN(2, "cpd=%p need uref=%d\n", cpd, need_uref);
191 memset(crd, 0, sizeof(*crd));
193 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
194 cpd->bus = devclass_get_softc(usb_devclass_ptr, cpd->bus_index);
195 if (cpd->bus == NULL) {
196 DPRINTFN(2, "no bus at %u\n", cpd->bus_index);
199 cpd->udev = cpd->bus->devices[cpd->dev_index];
200 if (cpd->udev == NULL) {
201 DPRINTFN(2, "no device at %u\n", cpd->dev_index);
204 if (cpd->udev->refcount == USB_DEV_REF_MAX) {
205 DPRINTFN(2, "no dev ref\n");
209 DPRINTFN(2, "ref udev - needed\n");
210 cpd->udev->refcount++;
212 lockmgr(&usb_ref_lock, LK_RELEASE);
215 * We need to grab the sx-lock before grabbing the
216 * FIFO refs to avoid deadlock at detach!
218 usbd_enum_lock(cpd->udev);
220 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
223 * Set "is_uref" after grabbing the default SX lock
228 /* check if we are doing an open */
229 if (cpd->fflags == 0) {
230 /* use zero defaults */
232 /* check for write */
233 if (cpd->fflags & FWRITE) {
234 ppf = cpd->udev->fifo;
235 f = ppf[cpd->fifo_index + USB_FIFO_TX];
237 crd->is_write = 1; /* ref */
238 if (f == NULL || f->refcount == USB_FIFO_REF_MAX)
240 if (f->curr_cpd != cpd)
242 /* check if USB-FS is active */
243 if (f->fs_ep_max != 0) {
249 if (cpd->fflags & FREAD) {
250 ppf = cpd->udev->fifo;
251 f = ppf[cpd->fifo_index + USB_FIFO_RX];
253 crd->is_read = 1; /* ref */
254 if (f == NULL || f->refcount == USB_FIFO_REF_MAX)
256 if (f->curr_cpd != cpd)
258 /* check if USB-FS is active */
259 if (f->fs_ep_max != 0) {
265 /* when everything is OK we increment the refcounts */
267 DPRINTFN(2, "ref write\n");
268 crd->txfifo->refcount++;
271 DPRINTFN(2, "ref read\n");
272 crd->rxfifo->refcount++;
274 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
280 usbd_enum_unlock(cpd->udev);
282 if (--(cpd->udev->refcount) == 0) {
283 cv_signal(&cpd->udev->ref_cv);
286 lockmgr(&usb_ref_lock, LK_RELEASE);
287 DPRINTFN(2, "fail\n");
288 return (USB_ERR_INVAL);
291 /*------------------------------------------------------------------------*
294 * This function is used to upgrade an USB reference to include the
295 * USB device reference on a USB location.
298 * 0: Success, refcount incremented on the given USB device.
300 *------------------------------------------------------------------------*/
302 usb_usb_ref_device(struct usb_cdev_privdata *cpd,
303 struct usb_cdev_refdata *crd)
306 * Check if we already got an USB reference on this location:
309 return (0); /* success */
312 * To avoid deadlock at detach we need to drop the FIFO ref
313 * and re-acquire a new ref!
315 usb_unref_device(cpd, crd);
317 return (usb_ref_device(cpd, crd, 1 /* need uref */));
320 /*------------------------------------------------------------------------*
323 * This function will release the reference count by one unit for the
325 *------------------------------------------------------------------------*/
327 usb_unref_device(struct usb_cdev_privdata *cpd,
328 struct usb_cdev_refdata *crd)
331 DPRINTFN(2, "cpd=%p is_uref=%d\n", cpd, crd->is_uref);
334 usbd_enum_unlock(cpd->udev);
336 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
338 if (--(crd->rxfifo->refcount) == 0) {
339 cv_signal(&crd->rxfifo->cv_drain);
344 if (--(crd->txfifo->refcount) == 0) {
345 cv_signal(&crd->txfifo->cv_drain);
350 if (--(cpd->udev->refcount) == 0) {
351 cv_signal(&cpd->udev->ref_cv);
355 lockmgr(&usb_ref_lock, LK_RELEASE);
358 static struct usb_fifo *
363 f = kmalloc(sizeof(*f), M_USBDEV, M_WAITOK | M_ZERO);
365 cv_init(&f->cv_io, "FIFO-IO");
366 cv_init(&f->cv_drain, "FIFO-DRAIN");
372 /*------------------------------------------------------------------------*
374 *------------------------------------------------------------------------*/
376 usb_fifo_create(struct usb_cdev_privdata *cpd,
377 struct usb_cdev_refdata *crd)
379 struct usb_device *udev = cpd->udev;
381 struct usb_endpoint *ep;
387 int e = cpd->ep_addr;
389 is_tx = (cpd->fflags & FWRITE) ? 1 : 0;
390 is_rx = (cpd->fflags & FREAD) ? 1 : 0;
394 /* Preallocated FIFO */
396 DPRINTFN(5, "Preallocated FIFO\n");
398 f = udev->fifo[cpd->fifo_index + USB_FIFO_TX];
404 f = udev->fifo[cpd->fifo_index + USB_FIFO_RX];
412 KASSERT(e >= 0 && e <= 15, ("endpoint %d out of range", e));
414 /* search for a free FIFO slot */
415 DPRINTFN(5, "Endpoint device, searching for 0x%02x\n", e);
416 for (n = 0;; n += 2) {
418 if (n == USB_FIFO_MAX) {
423 /* end of FIFOs reached */
424 DPRINTFN(5, "out of FIFOs\n");
428 /* Check for TX FIFO */
430 f = udev->fifo[n + USB_FIFO_TX];
432 if (f->dev_ep_index != e) {
433 /* wrong endpoint index */
436 if (f->curr_cpd != NULL) {
441 } else if (no_null) {
445 /* Check for RX FIFO */
447 f = udev->fifo[n + USB_FIFO_RX];
449 if (f->dev_ep_index != e) {
450 /* wrong endpoint index */
453 if (f->curr_cpd != NULL) {
458 } else if (no_null) {
466 if (e >= (USB_EP_MAX / 2)) {
467 /* we don't create any endpoints in this range */
468 DPRINTFN(5, "ep out of range\n");
469 return (is_busy ? EBUSY : EINVAL);
473 if ((e != 0) && is_busy) {
475 * Only the default control endpoint is allowed to be
476 * opened multiple times!
478 DPRINTFN(5, "busy\n");
484 (udev->fifo[n + USB_FIFO_TX] == NULL)) {
485 ep = usb_dev_get_ep(udev, e, USB_FIFO_TX);
486 DPRINTFN(5, "dev_get_endpoint(%d, 0x%x)\n", e, USB_FIFO_TX);
488 DPRINTFN(5, "dev_get_endpoint returned NULL\n");
491 f = usb_fifo_alloc();
493 DPRINTFN(5, "could not alloc tx fifo\n");
496 /* update some fields */
497 f->fifo_index = n + USB_FIFO_TX;
499 f->priv_lock = &udev->device_lock;
501 f->methods = &usb_ugen_methods;
502 f->iface_index = ep->iface_index;
504 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
505 udev->fifo[n + USB_FIFO_TX] = f;
506 lockmgr(&usb_ref_lock, LK_RELEASE);
510 (udev->fifo[n + USB_FIFO_RX] == NULL)) {
512 ep = usb_dev_get_ep(udev, e, USB_FIFO_RX);
513 DPRINTFN(5, "dev_get_endpoint(%d, 0x%x)\n", e, USB_FIFO_RX);
515 DPRINTFN(5, "dev_get_endpoint returned NULL\n");
518 f = usb_fifo_alloc();
520 DPRINTFN(5, "could not alloc rx fifo\n");
523 /* update some fields */
524 f->fifo_index = n + USB_FIFO_RX;
526 f->priv_lock = &udev->device_lock;
528 f->methods = &usb_ugen_methods;
529 f->iface_index = ep->iface_index;
531 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
532 udev->fifo[n + USB_FIFO_RX] = f;
533 lockmgr(&usb_ref_lock, LK_RELEASE);
536 crd->txfifo = udev->fifo[n + USB_FIFO_TX];
539 crd->rxfifo = udev->fifo[n + USB_FIFO_RX];
541 /* fill out fifo index */
542 DPRINTFN(5, "fifo index = %d\n", n);
551 usb_fifo_free(struct usb_fifo *f)
559 /* destroy symlink devices, if any */
560 for (n = 0; n != 2; n++) {
562 usb_free_symlink(f->symlink[n]);
563 f->symlink[n] = NULL;
566 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
568 /* delink ourselves to stop calls from userland */
569 if ((f->fifo_index < USB_FIFO_MAX) &&
571 (f->udev->fifo[f->fifo_index] == f)) {
572 f->udev->fifo[f->fifo_index] = NULL;
574 DPRINTFN(0, "USB FIFO %p has not been linked\n", f);
577 /* decrease refcount */
579 /* prevent any write flush */
581 /* need to wait until all callers have exited */
582 while (f->refcount != 0) {
583 lockmgr(&usb_ref_lock, LK_RELEASE); /* avoid LOR */
584 lockmgr(f->priv_lock, LK_EXCLUSIVE);
585 /* get I/O thread out of any sleep state */
586 if (f->flag_sleeping) {
587 f->flag_sleeping = 0;
588 cv_broadcast(&f->cv_io);
590 lockmgr(f->priv_lock, LK_RELEASE);
591 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
594 cv_wait(&f->cv_drain, &usb_ref_lock);
596 lockmgr(&usb_ref_lock, LK_RELEASE);
598 /* take care of closing the device here, if any */
599 usb_fifo_close(f, 0);
601 cv_destroy(&f->cv_io);
602 cv_destroy(&f->cv_drain);
607 static struct usb_endpoint *
608 usb_dev_get_ep(struct usb_device *udev, uint8_t ep_index, uint8_t dir)
610 struct usb_endpoint *ep;
616 if (dir == USB_FIFO_RX) {
617 if (udev->flags.usb_mode == USB_MODE_HOST) {
623 if (udev->flags.usb_mode == USB_MODE_HOST) {
629 ep = usbd_get_ep_by_addr(udev, ep_index | ep_dir);
633 /* if the endpoint does not exist then return */
636 if (ep->edesc == NULL) {
637 /* invalid endpoint */
640 return (ep); /* success */
643 /*------------------------------------------------------------------------*
649 *------------------------------------------------------------------------*/
651 usb_fifo_open(struct usb_cdev_privdata *cpd,
652 struct usb_fifo *f, int fflags)
658 DPRINTFN(2, "no FIFO\n");
661 /* remove FWRITE and FREAD flags */
662 fflags &= ~(FWRITE | FREAD);
664 /* set correct file flags */
665 if ((f->fifo_index & 1) == USB_FIFO_TX) {
671 /* check if we are already opened */
672 /* we don't need any locks when checking this variable */
673 if (f->curr_cpd != NULL) {
678 /* reset short flag before open */
681 /* call open method */
682 err = (f->methods->f_open) (f, fflags);
686 lockmgr(f->priv_lock, LK_EXCLUSIVE);
688 /* reset sleep flag */
689 f->flag_sleeping = 0;
691 /* reset error flag */
694 /* reset complete flag */
695 f->flag_iscomplete = 0;
697 /* reset select flag */
698 f->flag_isselect = 0;
700 /* reset flushing flag */
701 f->flag_flushing = 0;
703 /* reset ASYNC proc flag */
706 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
707 /* flag the fifo as opened to prevent others */
709 lockmgr(&usb_ref_lock, LK_RELEASE);
714 lockmgr(f->priv_lock, LK_RELEASE);
719 /*------------------------------------------------------------------------*
721 *------------------------------------------------------------------------*/
723 usb_fifo_reset(struct usb_fifo *f)
731 USB_IF_DEQUEUE(&f->used_q, m);
733 USB_IF_ENQUEUE(&f->free_q, m);
738 /* reset have fragment flag */
739 f->flag_have_fragment = 0;
742 /*------------------------------------------------------------------------*
744 *------------------------------------------------------------------------*/
746 usb_fifo_close(struct usb_fifo *f, int fflags)
750 /* check if we are not opened */
751 if (f->curr_cpd == NULL) {
752 /* nothing to do - already closed */
755 lockmgr(f->priv_lock, LK_EXCLUSIVE);
757 /* clear current cdev private data pointer */
760 /* check if we are selected */
761 if (f->flag_isselect) {
763 selwakeup(&f->selinfo);
765 f->flag_isselect = 0;
767 /* check if a thread wants SIGIO */
768 if (f->async_p != NULL && lwkt_trytoken(&f->async_p->p_token)) {
769 ksignal(f->async_p, SIGIO);
770 lwkt_reltoken(&f->async_p->p_token);
773 /* remove FWRITE and FREAD flags */
774 fflags &= ~(FWRITE | FREAD);
776 /* flush written data, if any */
777 if ((f->fifo_index & 1) == USB_FIFO_TX) {
779 if (!f->flag_iserror) {
781 /* set flushing flag */
782 f->flag_flushing = 1;
784 /* get the last packet in */
785 if (f->flag_have_fragment) {
787 f->flag_have_fragment = 0;
788 USB_IF_DEQUEUE(&f->free_q, m);
790 USB_IF_ENQUEUE(&f->used_q, m);
794 /* start write transfer, if not already started */
795 (f->methods->f_start_write) (f);
797 /* check if flushed already */
798 while (f->flag_flushing &&
799 (!f->flag_iserror)) {
800 /* wait until all data has been written */
801 f->flag_sleeping = 1;
802 err = cv_wait_sig(&f->cv_io, f->priv_lock);
804 DPRINTF("signal received\n");
811 /* stop write transfer, if not already stopped */
812 (f->methods->f_stop_write) (f);
816 /* stop write transfer, if not already stopped */
817 (f->methods->f_stop_read) (f);
820 /* check if we are sleeping */
821 if (f->flag_sleeping) {
822 DPRINTFN(2, "Sleeping at close!\n");
824 lockmgr(f->priv_lock, LK_RELEASE);
826 /* call close method */
827 (f->methods->f_close) (f, fflags);
832 /*------------------------------------------------------------------------*
833 * usb_open - cdev callback
834 *------------------------------------------------------------------------*/
836 usb_open(struct dev_open_args *ap)
837 /*struct cdev *dev, int fflags, int devtype, struct thread *td)
840 struct cdev *dev = ap->a_head.a_dev;
841 int fflags = ap->a_oflags;
842 struct usb_fs_privdata* pd = (struct usb_fs_privdata*)dev->si_drv1;
843 struct usb_cdev_refdata refs;
844 struct usb_cdev_privdata *cpd;
847 DPRINTFN(2, "%s fflags=0x%08x\n", devtoname(dev), fflags);
849 KASSERT(fflags & (FREAD|FWRITE), ("invalid open flags"));
850 if (((fflags & FREAD) && !(pd->mode & FREAD)) ||
851 ((fflags & FWRITE) && !(pd->mode & FWRITE))) {
852 DPRINTFN(2, "access mode not supported\n");
856 cpd = kmalloc(sizeof(*cpd), M_USBDEV, M_WAITOK | M_ZERO);
857 ep = cpd->ep_addr = pd->ep_addr;
859 usb_loc_fill(pd, cpd);
860 err = usb_ref_device(cpd, &refs, 1);
862 DPRINTFN(2, "cannot ref device\n");
863 kfree(cpd, M_USBDEV);
866 cpd->fflags = fflags; /* access mode for open lifetime */
868 /* create FIFOs, if any */
869 err = usb_fifo_create(cpd, &refs);
870 /* check for error */
872 DPRINTFN(2, "cannot create fifo\n");
873 usb_unref_device(cpd, &refs);
874 kfree(cpd, M_USBDEV);
877 if (fflags & FREAD) {
878 err = usb_fifo_open(cpd, refs.rxfifo, fflags);
880 DPRINTFN(2, "read open failed\n");
881 usb_unref_device(cpd, &refs);
882 kfree(cpd, M_USBDEV);
886 if (fflags & FWRITE) {
887 err = usb_fifo_open(cpd, refs.txfifo, fflags);
889 DPRINTFN(2, "write open failed\n");
890 if (fflags & FREAD) {
891 usb_fifo_close(refs.rxfifo, fflags);
893 usb_unref_device(cpd, &refs);
894 kfree(cpd, M_USBDEV);
898 usb_unref_device(cpd, &refs);
899 /* XXX: markusp: which privs?
900 devfs_set_cdevpriv(cpd, usb_close);
902 /* XXX: This might not work as I expect! */
903 dev->si_drv2 = (void *)cpd;
907 /*------------------------------------------------------------------------*
908 * usb_close - cdev callback
909 *------------------------------------------------------------------------*/
911 usb_close(struct dev_close_args *ap)
913 struct cdev *dev = ap->a_head.a_dev;
914 struct usb_cdev_refdata refs;
915 struct usb_cdev_privdata *cpd = (struct usb_cdev_privdata *)dev->si_drv2;
918 DPRINTFN(2, "cpd=%p\n", cpd);
920 err = usb_ref_device(cpd, &refs, 0);
925 * If this function is not called directly from the root HUB
926 * thread, there is usually a need to lock the enumeration
929 if (!usbd_enum_is_locked(cpd->udev)) {
931 DPRINTFN(2, "Locking enumeration\n");
933 /* reference device */
934 err = usb_usb_ref_device(cpd, &refs);
938 if (cpd->fflags & FREAD) {
939 usb_fifo_close(refs.rxfifo, cpd->fflags);
941 if (cpd->fflags & FWRITE) {
942 usb_fifo_close(refs.txfifo, cpd->fflags);
944 usb_unref_device(cpd, &refs);
946 kfree(cpd, M_USBDEV);
951 usb_dev_init(void *arg)
953 lockinit(&usb_ref_lock, "USB ref mutex", 0, 0);
954 lockinit(&usb_sym_lock, "USB sym mutex", 0, 0);
955 TAILQ_INIT(&usb_sym_head);
957 /* check the UGEN methods */
958 usb_fifo_check_methods(&usb_ugen_methods);
961 /* XXX SI_SUB_KLD? */
962 SYSINIT(usb_dev_init, SI_SUB_CONFIGURE, SI_ORDER_FIRST, usb_dev_init, NULL);
965 usb_dev_init_post(void *arg)
968 * Create /bus/u4b - this is needed for usbconfig(8), which
969 * needs a well-known device name to access.
971 usb_dev = make_dev(&usb_static_devsw, 0, UID_ROOT, GID_OPERATOR,
972 0644, USB_DEVICE_NAME);
973 if (usb_dev == NULL) {
974 DPRINTFN(0, "Could not create usb bus device\n");
978 SYSINIT(usb_dev_init_post, SI_SUB_KICK_SCHEDULER, SI_ORDER_FIRST, usb_dev_init_post, NULL);
981 usb_dev_uninit(void *arg)
983 if (usb_dev != NULL) {
984 destroy_dev(usb_dev);
987 lockuninit(&usb_ref_lock);
988 lockuninit(&usb_sym_lock);
991 SYSUNINIT(usb_dev_uninit, SI_SUB_KICK_SCHEDULER, SI_ORDER_ANY, usb_dev_uninit, NULL);
994 usb_ioctl_f_sub(struct usb_fifo *f, u_long cmd, void *addr, struct thread *td)
1000 *(int *)addr = 0; /* character device */
1004 /* handled by upper FS layer */
1009 if (f->async_p != NULL) {
1013 f->async_p = USB_TD_GET_PROC(td);
1019 /* XXX this is not the most general solution */
1021 if (f->async_p == NULL) {
1025 if (*(int *)addr != USB_PROC_GET_GID(f->async_p)) {
1033 DPRINTFN(3, "cmd 0x%lx = %d\n", cmd, error);
1037 /*------------------------------------------------------------------------*
1038 * usb_ioctl - cdev callback
1039 *------------------------------------------------------------------------*/
1041 usb_ioctl(struct dev_ioctl_args *ap)
1042 /*usb_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int fflag, struct thread* td)
1045 struct cdev *dev = ap->a_head.a_dev;
1046 u_long cmd = ap->a_cmd;
1047 caddr_t addr = ap->a_data;
1048 /* XXX: What is this thread and where is it supposed to come from */
1049 struct thread *td = curthread;
1050 struct usb_cdev_refdata refs;
1051 struct usb_cdev_privdata* cpd;
1056 DPRINTFN(2, "cmd=0x%lx\n", cmd);
1059 err = devfs_get_cdevpriv((void **)&cpd);
1063 /* XXX: This might not work as I would like it to
1064 * also I need a proper return value if it does */
1065 if(dev->si_drv2 == NULL)
1068 cpd = (struct usb_cdev_privdata *)dev->si_drv2;
1071 * Performance optimisation: We try to check for IOCTL's that
1072 * don't need the USB reference first. Then we grab the USB
1073 * reference if we need it!
1075 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1079 fflags = cpd->fflags;
1081 f = NULL; /* set default value */
1082 err = ENOIOCTL; /* set default value */
1084 if (fflags & FWRITE) {
1086 err = usb_ioctl_f_sub(f, cmd, addr, td);
1088 if (fflags & FREAD) {
1090 err = usb_ioctl_f_sub(f, cmd, addr, td);
1092 KASSERT(f != NULL, ("fifo not found"));
1093 if (err != ENOIOCTL)
1096 err = (f->methods->f_ioctl) (f, cmd, addr, fflags);
1098 DPRINTFN(2, "f_ioctl cmd 0x%lx = %d\n", cmd, err);
1100 if (err != ENOIOCTL)
1103 if (usb_usb_ref_device(cpd, &refs)) {
1108 err = (f->methods->f_ioctl_post) (f, cmd, addr, fflags);
1110 DPRINTFN(2, "f_ioctl_post cmd 0x%lx = %d\n", cmd, err);
1112 if (err == ENOIOCTL)
1118 /* Wait for re-enumeration, if any */
1120 while (f->udev->re_enumerate_wait != 0) {
1122 usb_unref_device(cpd, &refs);
1124 usb_pause_mtx(NULL, hz / 128);
1126 if (usb_ref_device(cpd, &refs, 1 /* need uref */)) {
1133 usb_unref_device(cpd, &refs);
1139 usb_kqfilter(struct dev_kqfilter_args *ap)
1145 /* XXX implement using kqfilter */
1149 usb_poll(struct cdev* dev, int events, struct thread* td)
1152 struct usb_cdev_refdata refs;
1153 struct usb_cdev_privdata* cpd;
1156 int fflags, revents;
1158 if (devfs_get_cdevpriv((void **)&cpd) != 0 ||
1159 usb_ref_device(cpd, &refs, 0) != 0)
1161 (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM));
1163 fflags = cpd->fflags;
1165 /* Figure out who needs service */
1167 if ((events & (POLLOUT | POLLWRNORM)) &&
1168 (fflags & FWRITE)) {
1172 lockmgr(f->priv_lock, LK_EXCLUSIVE);
1174 if (!refs.is_usbfs) {
1175 if (f->flag_iserror) {
1176 /* we got an error */
1179 if (f->queue_data == NULL) {
1181 * start write transfer, if not
1184 (f->methods->f_start_write) (f);
1186 /* check if any packets are available */
1187 USB_IF_POLL(&f->free_q, m);
1190 if (f->flag_iscomplete) {
1198 revents |= events & (POLLOUT | POLLWRNORM);
1200 f->flag_isselect = 1;
1201 selrecord(td, &f->selinfo);
1204 lockmgr(f->priv_lock);
1206 if ((events & (POLLIN | POLLRDNORM)) &&
1211 lockmgr(f->priv_lock, LK_EXCLUSIVE);
1213 if (!refs.is_usbfs) {
1214 if (f->flag_iserror) {
1215 /* we have and error */
1218 if (f->queue_data == NULL) {
1220 * start read transfer, if not
1223 (f->methods->f_start_read) (f);
1225 /* check if any packets are available */
1226 USB_IF_POLL(&f->used_q, m);
1229 if (f->flag_iscomplete) {
1237 revents |= events & (POLLIN | POLLRDNORM);
1239 f->flag_isselect = 1;
1240 selrecord(td, &f->selinfo);
1242 if (!refs.is_usbfs) {
1243 /* start reading data */
1244 (f->methods->f_start_read) (f);
1248 lockmgr(f->priv_lock, LK_RELEASE);
1250 usb_unref_device(cpd, &refs);
1255 /*usb_read(struct cdev *dev, struct uio *uio, int ioflag)*/
1256 usb_read(struct dev_read_args *ap)
1258 struct cdev *dev = ap->a_head.a_dev;
1259 struct uio *uio = ap->a_uio;
1260 int ioflag = ap->a_ioflag;
1261 struct usb_cdev_refdata refs;
1262 struct usb_cdev_privdata* cpd;
1269 uint8_t tr_data = 0;
1272 err = devfs_get_cdevpriv((void **)&cpd);
1277 if(dev->si_drv2 == NULL)
1280 cpd = (struct usb_cdev_privdata *)dev->si_drv2;
1282 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1286 fflags = cpd->fflags;
1290 /* should not happen */
1291 usb_unref_device(cpd, &refs);
1295 resid = uio->uio_resid;
1297 lockmgr(f->priv_lock, LK_EXCLUSIVE);
1299 /* check for permanent read error */
1300 if (f->flag_iserror) {
1304 /* check if USB-FS interface is active */
1305 if (refs.is_usbfs) {
1307 * The queue is used for events that should be
1308 * retrieved using the "USB_FS_COMPLETE" ioctl.
1313 while (uio->uio_resid > 0) {
1315 USB_IF_DEQUEUE(&f->used_q, m);
1319 /* start read transfer, if not already started */
1321 (f->methods->f_start_read) (f);
1323 if (ioflag & IO_NDELAY) {
1325 /* return length before error */
1331 DPRINTF("sleeping\n");
1333 err = usb_fifo_wait(f);
1339 if (f->methods->f_filter_read) {
1341 * Sometimes it is convenient to process data at the
1342 * expense of a userland process instead of a kernel
1345 (f->methods->f_filter_read) (f, m);
1349 io_len = MIN(m->cur_data_len, uio->uio_resid);
1351 DPRINTFN(2, "transfer %d bytes from %p\n",
1352 io_len, m->cur_data_ptr);
1354 err = usb_fifo_uiomove(f,
1355 m->cur_data_ptr, io_len, uio);
1357 m->cur_data_len -= io_len;
1358 m->cur_data_ptr += io_len;
1360 if (m->cur_data_len == 0) {
1362 uint8_t last_packet;
1364 last_packet = m->last_packet;
1366 USB_IF_ENQUEUE(&f->free_q, m);
1373 USB_IF_PREPEND(&f->used_q, m);
1381 lockmgr(f->priv_lock, LK_RELEASE);
1383 usb_unref_device(cpd, &refs);
1389 /* usb_write(struct cdev *dev, struct uio *uio, int ioflag) */
1390 usb_write(struct dev_write_args *ap)
1392 struct cdev *dev = ap->a_head.a_dev;
1393 struct uio *uio = ap->a_uio;
1394 int ioflag = ap->a_ioflag;
1395 struct usb_cdev_refdata refs;
1396 struct usb_cdev_privdata* cpd;
1404 uint8_t tr_data = 0;
1409 err = devfs_get_cdevpriv((void **)&cpd);
1413 if(dev->si_drv2 == NULL)
1416 cpd = (struct usb_cdev_privdata *)dev->si_drv2;
1418 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1422 fflags = cpd->fflags;
1426 /* should not happen */
1427 usb_unref_device(cpd, &refs);
1430 resid = uio->uio_resid;
1432 lockmgr(f->priv_lock, LK_EXCLUSIVE);
1434 /* check for permanent write error */
1435 if (f->flag_iserror) {
1439 /* check if USB-FS interface is active */
1440 if (refs.is_usbfs) {
1442 * The queue is used for events that should be
1443 * retrieved using the "USB_FS_COMPLETE" ioctl.
1448 if (f->queue_data == NULL) {
1449 /* start write transfer, if not already started */
1450 (f->methods->f_start_write) (f);
1452 /* we allow writing zero length data */
1454 USB_IF_DEQUEUE(&f->free_q, m);
1458 if (ioflag & IO_NDELAY) {
1460 /* return length before error */
1466 DPRINTF("sleeping\n");
1468 err = usb_fifo_wait(f);
1476 if (f->flag_have_fragment == 0) {
1478 io_len = m->cur_data_len;
1479 pdata = m->cur_data_ptr;
1480 if (io_len > uio->uio_resid)
1481 io_len = uio->uio_resid;
1482 m->cur_data_len = io_len;
1484 io_len = m->max_data_len - m->cur_data_len;
1485 pdata = m->cur_data_ptr + m->cur_data_len;
1486 if (io_len > uio->uio_resid)
1487 io_len = uio->uio_resid;
1488 m->cur_data_len += io_len;
1491 DPRINTFN(2, "transfer %d bytes to %p\n",
1494 err = usb_fifo_uiomove(f, pdata, io_len, uio);
1497 f->flag_have_fragment = 0;
1498 USB_IF_ENQUEUE(&f->free_q, m);
1502 /* check if the buffer is ready to be transmitted */
1504 if ((f->flag_write_defrag == 0) ||
1505 (m->cur_data_len == m->max_data_len)) {
1506 f->flag_have_fragment = 0;
1509 * Check for write filter:
1511 * Sometimes it is convenient to process data
1512 * at the expense of a userland process
1513 * instead of a kernel process.
1515 if (f->methods->f_filter_write) {
1516 (f->methods->f_filter_write) (f, m);
1519 /* Put USB mbuf in the used queue */
1520 USB_IF_ENQUEUE(&f->used_q, m);
1522 /* Start writing data, if not already started */
1523 (f->methods->f_start_write) (f);
1525 /* Wait for more data or close */
1526 f->flag_have_fragment = 1;
1527 USB_IF_PREPEND(&f->free_q, m);
1530 } while (uio->uio_resid > 0);
1532 lockmgr(f->priv_lock, LK_RELEASE);
1534 usb_unref_device(cpd, &refs);
1540 usb_static_ioctl(struct dev_ioctl_args *ap)
1542 usb_static_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
1546 u_long cmd = ap->a_cmd;
1547 caddr_t data = ap->a_data;
1548 /* XXX: what is this thread descriptor and where is it
1549 * supposed to come from?
1551 struct thread *td = NULL;
1552 int fflag = ap->a_fflag;
1554 struct usb_read_dir *urd;
1562 err = usb_read_symlink(u.urd->urd_data,
1563 u.urd->urd_startentry, u.urd->urd_maxlen);
1565 case USB_DEV_QUIRK_GET:
1566 case USB_QUIRK_NAME_GET:
1567 case USB_DEV_QUIRK_ADD:
1568 case USB_DEV_QUIRK_REMOVE:
1569 err = usb_quirk_ioctl_p(cmd, data, fflag, td);
1571 case USB_GET_TEMPLATE:
1572 *(int *)data = usb_template;
1575 case USB_SET_TEMPLATE:
1576 err = priv_check(curthread, PRIV_DRIVER);
1579 usb_template = *(int *)data;
1589 usb_fifo_uiomove(struct usb_fifo *f, void *cp,
1590 int n, struct uio *uio)
1594 lockmgr(f->priv_lock, LK_RELEASE);
1597 * "uiomove()" can sleep so one needs to make a wrapper,
1598 * exiting the mutex and checking things:
1600 error = uiomove(cp, n, uio);
1602 lockmgr(f->priv_lock, LK_EXCLUSIVE);
1608 usb_fifo_wait(struct usb_fifo *f)
1612 KKASSERT(lockstatus(f->priv_lock, curthread) != 0);
1614 if (f->flag_iserror) {
1618 f->flag_sleeping = 1;
1620 err = cv_wait_sig(&f->cv_io, f->priv_lock);
1622 if (f->flag_iserror) {
1630 usb_fifo_signal(struct usb_fifo *f)
1632 if (f->flag_sleeping) {
1633 f->flag_sleeping = 0;
1634 cv_broadcast(&f->cv_io);
1639 usb_fifo_wakeup(struct usb_fifo *f)
1643 if (f->flag_isselect) {
1645 selwakeup(&f->selinfo);
1647 f->flag_isselect = 0;
1649 if (f->async_p != NULL && lwkt_trytoken(&f->async_p->p_token)) {
1650 ksignal(f->async_p, SIGIO);
1651 lwkt_reltoken(&f->async_p->p_token);
1656 usb_fifo_dummy_open(struct usb_fifo *fifo, int fflags)
1662 usb_fifo_dummy_close(struct usb_fifo *fifo, int fflags)
1668 usb_fifo_dummy_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
1674 usb_fifo_dummy_cmd(struct usb_fifo *fifo)
1676 fifo->flag_flushing = 0; /* not flushing */
1680 usb_fifo_check_methods(struct usb_fifo_methods *pm)
1682 /* check that all callback functions are OK */
1684 if (pm->f_open == NULL)
1685 pm->f_open = &usb_fifo_dummy_open;
1687 if (pm->f_close == NULL)
1688 pm->f_close = &usb_fifo_dummy_close;
1690 if (pm->f_ioctl == NULL)
1691 pm->f_ioctl = &usb_fifo_dummy_ioctl;
1693 if (pm->f_ioctl_post == NULL)
1694 pm->f_ioctl_post = &usb_fifo_dummy_ioctl;
1696 if (pm->f_start_read == NULL)
1697 pm->f_start_read = &usb_fifo_dummy_cmd;
1699 if (pm->f_stop_read == NULL)
1700 pm->f_stop_read = &usb_fifo_dummy_cmd;
1702 if (pm->f_start_write == NULL)
1703 pm->f_start_write = &usb_fifo_dummy_cmd;
1705 if (pm->f_stop_write == NULL)
1706 pm->f_stop_write = &usb_fifo_dummy_cmd;
1709 /*------------------------------------------------------------------------*
1712 * The following function will create a duplex FIFO.
1717 *------------------------------------------------------------------------*/
1719 usb_fifo_attach(struct usb_device *udev, void *priv_sc,
1720 struct lock *priv_lock, struct usb_fifo_methods *pm,
1721 struct usb_fifo_sc *f_sc, uint16_t unit, uint16_t subunit,
1722 uint8_t iface_index, uid_t uid, gid_t gid, int mode)
1724 struct usb_fifo *f_tx;
1725 struct usb_fifo *f_rx;
1729 f_sc->fp[USB_FIFO_TX] = NULL;
1730 f_sc->fp[USB_FIFO_RX] = NULL;
1735 /* check the methods */
1736 usb_fifo_check_methods(pm);
1738 /* search for a free FIFO slot */
1739 for (n = 0;; n += 2) {
1741 if (n == USB_FIFO_MAX) {
1742 /* end of FIFOs reached */
1745 /* Check for TX FIFO */
1746 if (udev->fifo[n + USB_FIFO_TX] != NULL) {
1749 /* Check for RX FIFO */
1750 if (udev->fifo[n + USB_FIFO_RX] != NULL) {
1756 f_tx = usb_fifo_alloc();
1757 f_rx = usb_fifo_alloc();
1759 if ((f_tx == NULL) || (f_rx == NULL)) {
1760 usb_fifo_free(f_tx);
1761 usb_fifo_free(f_rx);
1764 /* initialise FIFO structures */
1766 f_tx->fifo_index = n + USB_FIFO_TX;
1767 f_tx->dev_ep_index = -1;
1768 f_tx->priv_lock = priv_lock;
1769 f_tx->priv_sc0 = priv_sc;
1771 f_tx->iface_index = iface_index;
1774 f_rx->fifo_index = n + USB_FIFO_RX;
1775 f_rx->dev_ep_index = -1;
1776 f_rx->priv_lock = priv_lock;
1777 f_rx->priv_sc0 = priv_sc;
1779 f_rx->iface_index = iface_index;
1782 f_sc->fp[USB_FIFO_TX] = f_tx;
1783 f_sc->fp[USB_FIFO_RX] = f_rx;
1785 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
1786 udev->fifo[f_tx->fifo_index] = f_tx;
1787 udev->fifo[f_rx->fifo_index] = f_rx;
1788 lockmgr(&usb_ref_lock, LK_RELEASE);
1790 for (n = 0; n != 4; n++) {
1792 if (pm->basename[n] == NULL) {
1795 if (subunit == 0xFFFF) {
1796 if (ksnprintf(devname, sizeof(devname),
1797 "%s%u%s", pm->basename[n],
1798 unit, pm->postfix[n] ?
1799 pm->postfix[n] : "")) {
1803 if (ksnprintf(devname, sizeof(devname),
1804 "%s%u.%u%s", pm->basename[n],
1805 unit, subunit, pm->postfix[n] ?
1806 pm->postfix[n] : "")) {
1812 * Distribute the symbolic links into two FIFO structures:
1815 f_rx->symlink[n / 2] =
1816 usb_alloc_symlink(devname);
1818 f_tx->symlink[n / 2] =
1819 usb_alloc_symlink(devname);
1822 /* Create the device */
1823 f_sc->dev = usb_make_dev(udev, devname, -1,
1824 f_tx->fifo_index & f_rx->fifo_index,
1825 FREAD|FWRITE, uid, gid, mode);
1828 DPRINTFN(2, "attached %p/%p\n", f_tx, f_rx);
1832 /*------------------------------------------------------------------------*
1833 * usb_fifo_alloc_buffer
1838 *------------------------------------------------------------------------*/
1840 usb_fifo_alloc_buffer(struct usb_fifo *f, usb_size_t bufsize,
1843 usb_fifo_free_buffer(f);
1845 /* allocate an endpoint */
1846 f->free_q.ifq_maxlen = nbuf;
1847 f->used_q.ifq_maxlen = nbuf;
1849 f->queue_data = usb_alloc_mbufs(
1850 M_USBDEV, &f->free_q, bufsize, nbuf);
1852 if ((f->queue_data == NULL) && bufsize && nbuf) {
1855 return (0); /* success */
1858 /*------------------------------------------------------------------------*
1859 * usb_fifo_free_buffer
1861 * This function will free the buffers associated with a FIFO. This
1862 * function can be called multiple times in a row.
1863 *------------------------------------------------------------------------*/
1865 usb_fifo_free_buffer(struct usb_fifo *f)
1867 if (f->queue_data) {
1868 /* free old buffer */
1869 kfree(f->queue_data, M_USBDEV);
1870 f->queue_data = NULL;
1874 memset(&f->free_q, 0, sizeof(f->free_q));
1875 memset(&f->used_q, 0, sizeof(f->used_q));
1879 usb_fifo_detach(struct usb_fifo_sc *f_sc)
1884 usb_fifo_free(f_sc->fp[USB_FIFO_TX]);
1885 usb_fifo_free(f_sc->fp[USB_FIFO_RX]);
1887 f_sc->fp[USB_FIFO_TX] = NULL;
1888 f_sc->fp[USB_FIFO_RX] = NULL;
1890 usb_destroy_dev(f_sc->dev);
1894 DPRINTFN(2, "detached %p\n", f_sc);
1898 usb_fifo_put_bytes_max(struct usb_fifo *f)
1903 USB_IF_POLL(&f->free_q, m);
1906 len = m->max_data_len;
1913 /*------------------------------------------------------------------------*
1917 * 0 - normal operation
1918 * 1 - set last packet flag to enforce framing
1919 *------------------------------------------------------------------------*/
1921 usb_fifo_put_data(struct usb_fifo *f, struct usb_page_cache *pc,
1922 usb_frlength_t offset, usb_frlength_t len, uint8_t what)
1925 usb_frlength_t io_len;
1927 while (len || (what == 1)) {
1929 USB_IF_DEQUEUE(&f->free_q, m);
1934 io_len = MIN(len, m->cur_data_len);
1936 usbd_copy_out(pc, offset, m->cur_data_ptr, io_len);
1938 m->cur_data_len = io_len;
1942 if ((len == 0) && (what == 1)) {
1945 USB_IF_ENQUEUE(&f->used_q, m);
1949 if ((len == 0) || (what == 1)) {
1959 usb_fifo_put_data_linear(struct usb_fifo *f, void *ptr,
1960 usb_size_t len, uint8_t what)
1965 while (len || (what == 1)) {
1967 USB_IF_DEQUEUE(&f->free_q, m);
1972 io_len = MIN(len, m->cur_data_len);
1974 memcpy(m->cur_data_ptr, ptr, io_len);
1976 m->cur_data_len = io_len;
1977 ptr = USB_ADD_BYTES(ptr, io_len);
1980 if ((len == 0) && (what == 1)) {
1983 USB_IF_ENQUEUE(&f->used_q, m);
1987 if ((len == 0) || (what == 1)) {
1997 usb_fifo_put_data_buffer(struct usb_fifo *f, void *ptr, usb_size_t len)
2001 USB_IF_DEQUEUE(&f->free_q, m);
2004 m->cur_data_len = len;
2005 m->cur_data_ptr = ptr;
2006 USB_IF_ENQUEUE(&f->used_q, m);
2014 usb_fifo_put_data_error(struct usb_fifo *f)
2016 f->flag_iserror = 1;
2020 /*------------------------------------------------------------------------*
2024 * 0 - normal operation
2025 * 1 - only get one "usb_mbuf"
2029 * 1 - data in buffer
2030 *------------------------------------------------------------------------*/
2032 usb_fifo_get_data(struct usb_fifo *f, struct usb_page_cache *pc,
2033 usb_frlength_t offset, usb_frlength_t len, usb_frlength_t *actlen,
2037 usb_frlength_t io_len;
2038 uint8_t tr_data = 0;
2044 USB_IF_DEQUEUE(&f->used_q, m);
2050 io_len = MIN(len, m->cur_data_len);
2052 usbd_copy_in(pc, offset, m->cur_data_ptr, io_len);
2056 actlen[0] += io_len;
2057 m->cur_data_ptr += io_len;
2058 m->cur_data_len -= io_len;
2060 if ((m->cur_data_len == 0) || (what == 1)) {
2061 USB_IF_ENQUEUE(&f->free_q, m);
2069 USB_IF_PREPEND(&f->used_q, m);
2074 /* wait for data to be written out */
2077 if (f->flag_flushing) {
2078 /* check if we should send a short packet */
2079 if (f->flag_short != 0) {
2084 /* flushing complete */
2085 f->flag_flushing = 0;
2098 usb_fifo_get_data_linear(struct usb_fifo *f, void *ptr,
2099 usb_size_t len, usb_size_t *actlen, uint8_t what)
2103 uint8_t tr_data = 0;
2109 USB_IF_DEQUEUE(&f->used_q, m);
2115 io_len = MIN(len, m->cur_data_len);
2117 memcpy(ptr, m->cur_data_ptr, io_len);
2120 ptr = USB_ADD_BYTES(ptr, io_len);
2121 actlen[0] += io_len;
2122 m->cur_data_ptr += io_len;
2123 m->cur_data_len -= io_len;
2125 if ((m->cur_data_len == 0) || (what == 1)) {
2126 USB_IF_ENQUEUE(&f->free_q, m);
2134 USB_IF_PREPEND(&f->used_q, m);
2139 /* wait for data to be written out */
2142 if (f->flag_flushing) {
2143 /* check if we should send a short packet */
2144 if (f->flag_short != 0) {
2149 /* flushing complete */
2150 f->flag_flushing = 0;
2163 usb_fifo_get_data_buffer(struct usb_fifo *f, void **pptr, usb_size_t *plen)
2167 USB_IF_POLL(&f->used_q, m);
2170 *plen = m->cur_data_len;
2171 *pptr = m->cur_data_ptr;
2179 usb_fifo_get_data_error(struct usb_fifo *f)
2181 f->flag_iserror = 1;
2185 /*------------------------------------------------------------------------*
2190 * Else: Pointer to symlink entry
2191 *------------------------------------------------------------------------*/
2192 struct usb_symlink *
2193 usb_alloc_symlink(const char *target)
2195 struct usb_symlink *ps;
2197 ps = kmalloc(sizeof(*ps), M_USBDEV, M_WAITOK);
2201 /* XXX no longer needed */
2202 strlcpy(ps->src_path, target, sizeof(ps->src_path));
2203 ps->src_len = strlen(ps->src_path);
2204 strlcpy(ps->dst_path, target, sizeof(ps->dst_path));
2205 ps->dst_len = strlen(ps->dst_path);
2207 lockmgr(&usb_sym_lock, LK_EXCLUSIVE);
2208 TAILQ_INSERT_TAIL(&usb_sym_head, ps, sym_entry);
2209 lockmgr(&usb_sym_lock, LK_RELEASE);
2213 /*------------------------------------------------------------------------*
2215 *------------------------------------------------------------------------*/
2217 usb_free_symlink(struct usb_symlink *ps)
2222 lockmgr(&usb_sym_lock, LK_EXCLUSIVE);
2223 TAILQ_REMOVE(&usb_sym_head, ps, sym_entry);
2224 lockmgr(&usb_sym_lock, LK_RELEASE);
2226 kfree(ps, M_USBDEV);
2229 /*------------------------------------------------------------------------*
2235 *------------------------------------------------------------------------*/
2237 usb_read_symlink(uint8_t *user_ptr, uint32_t startentry, uint32_t user_len)
2239 struct usb_symlink *ps;
2245 lockmgr(&usb_sym_lock, LK_EXCLUSIVE);
2247 TAILQ_FOREACH(ps, &usb_sym_head, sym_entry) {
2250 * Compute total length of source and destination symlink
2251 * strings pluss one length byte and two NUL bytes:
2253 temp = ps->src_len + ps->dst_len + 3;
2257 * Skip entry because this length cannot fit
2262 if (startentry != 0) {
2263 /* decrement read offset */
2267 if (temp > user_len) {
2268 /* out of buffer space */
2273 /* copy out total length */
2275 error = copyout(&len,
2276 USB_ADD_BYTES(user_ptr, delta), 1);
2282 /* copy out source string */
2284 error = copyout(ps->src_path,
2285 USB_ADD_BYTES(user_ptr, delta), ps->src_len);
2290 delta += ps->src_len;
2291 error = copyout(&len,
2292 USB_ADD_BYTES(user_ptr, delta), 1);
2298 /* copy out destination string */
2300 error = copyout(ps->dst_path,
2301 USB_ADD_BYTES(user_ptr, delta), ps->dst_len);
2306 delta += ps->dst_len;
2307 error = copyout(&len,
2308 USB_ADD_BYTES(user_ptr, delta), 1);
2317 /* a zero length entry indicates the end */
2319 if ((user_len != 0) && (error == 0)) {
2323 error = copyout(&len,
2324 USB_ADD_BYTES(user_ptr, delta), 1);
2326 lockmgr(&usb_sym_lock, LK_RELEASE);
2331 usb_fifo_set_close_zlp(struct usb_fifo *f, uint8_t onoff)
2336 /* send a Zero Length Packet, ZLP, before close */
2337 f->flag_short = onoff;
2341 usb_fifo_set_write_defrag(struct usb_fifo *f, uint8_t onoff)
2346 /* defrag written data */
2347 f->flag_write_defrag = onoff;
2348 /* reset defrag state */
2349 f->flag_have_fragment = 0;
2353 usb_fifo_softc(struct usb_fifo *f)
2355 return (f->priv_sc0);
2357 #endif /* USB_HAVE_UGEN */