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>
36 #include <sys/thread2.h>
38 #include <sys/module.h>
40 #include <sys/mutex.h>
41 #include <sys/condvar.h>
42 #include <sys/sysctl.h>
43 #include <sys/unistd.h>
44 #include <sys/callout.h>
45 #include <sys/malloc.h>
47 #include <sys/vnode.h>
49 #include <sys/fcntl.h>
50 #include <sys/devfs.h>
52 #include <bus/u4b/usb.h>
53 #include <bus/u4b/usb_ioctl.h>
54 #include <bus/u4b/usbdi.h>
55 #include <bus/u4b/usbdi_util.h>
57 #define USB_DEBUG_VAR usb_fifo_debug
59 #include <bus/u4b/usb_core.h>
60 #include <bus/u4b/usb_dev.h>
61 #include <bus/u4b/usb_mbuf.h>
62 #include <bus/u4b/usb_process.h>
63 #include <bus/u4b/usb_device.h>
64 #include <bus/u4b/usb_debug.h>
65 #include <bus/u4b/usb_busdma.h>
66 #include <bus/u4b/usb_generic.h>
67 #include <bus/u4b/usb_dynamic.h>
68 #include <bus/u4b/usb_util.h>
70 #include <bus/u4b/usb_controller.h>
71 #include <bus/u4b/usb_bus.h>
73 #include <sys/filio.h>
74 #include <sys/ttycom.h>
75 #include <sys/kern_syscall.h>
77 #include <machine/stdarg.h>
82 static int usb_fifo_debug = 0;
84 static SYSCTL_NODE(_hw_usb, OID_AUTO, dev, CTLFLAG_RW, 0, "USB device");
85 SYSCTL_INT(_hw_usb_dev, OID_AUTO, debug, CTLFLAG_RW,
86 &usb_fifo_debug, 0, "Debug Level");
88 TUNABLE_INT("hw.usb.dev.debug", &usb_fifo_debug);
91 #define USB_UCRED struct ucred *ucred,
95 static int usb_fifo_open(struct usb_cdev_privdata *,
96 struct usb_fifo *, int);
97 static void usb_fifo_close(struct usb_fifo *, int);
98 static void usb_dev_init(void *);
99 static void usb_dev_init_post(void *);
100 static void usb_dev_uninit(void *);
101 static int usb_fifo_uiomove(struct usb_fifo *, void *, int,
103 static void usb_fifo_check_methods(struct usb_fifo_methods *);
104 static struct usb_fifo *usb_fifo_alloc(void);
105 static struct usb_endpoint *usb_dev_get_ep(struct usb_device *, uint8_t,
107 static void usb_loc_fill(struct usb_fs_privdata *,
108 struct usb_cdev_privdata *);
109 static usb_error_t usb_ref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *, int);
110 static usb_error_t usb_usb_ref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *);
111 static void usb_unref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *);
113 static void usb_filter_detach(struct knote *kn);
114 static int usb_filter_read(struct knote *kn, long hint);
115 static int usb_filter_write(struct knote *kn, long hint);
117 static d_open_t usb_open;
118 static d_close_t usb_close;
119 static d_ioctl_t usb_ioctl;
120 static d_read_t usb_read;
121 static d_write_t usb_write;
122 static d_kqfilter_t usb_kqfilter;
124 static d_ioctl_t usb_static_ioctl;
125 static d_open_t usb_static_open;
126 static d_close_t usb_static_close;
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 dev_ops usb_ops = {
135 { "usbdev", 0, D_MEM },
137 .d_close = usb_close,
138 .d_ioctl = usb_ioctl,
140 .d_write = usb_write,
141 .d_kqfilter = usb_kqfilter
144 static struct cdev* usb_dev = NULL;
146 /* character device structure used for /bus/u4b */
147 static struct dev_ops usb_static_ops = {
149 .d_open = usb_static_open,
150 .d_close = usb_static_close,
151 .d_ioctl = usb_static_ioctl,
154 static TAILQ_HEAD(, usb_symlink) usb_sym_head;
155 static struct lock usb_sym_lock;
157 struct lock usb_ref_lock;
159 static struct kqinfo usb_kqevent;
162 usb_cdevpriv_dtor(void *cpd)
166 /*------------------------------------------------------------------------*
169 * This is used to fill out a usb_cdev_privdata structure based on the
170 * device's address as contained in usb_fs_privdata.
171 *------------------------------------------------------------------------*/
173 usb_loc_fill(struct usb_fs_privdata* pd, struct usb_cdev_privdata *cpd)
175 cpd->bus_index = pd->bus_index;
176 cpd->dev_index = pd->dev_index;
177 cpd->ep_addr = pd->ep_addr;
178 cpd->fifo_index = pd->fifo_index;
181 /*------------------------------------------------------------------------*
184 * This function is used to atomically refer an USB device by its
185 * device location. If this function returns success the USB device
186 * will not dissappear until the USB device is unreferenced.
189 * 0: Success, refcount incremented on the given USB device.
191 *------------------------------------------------------------------------*/
193 usb_ref_device(struct usb_cdev_privdata *cpd,
194 struct usb_cdev_refdata *crd, int need_uref)
196 struct usb_fifo **ppf;
199 DPRINTFN(2, "cpd=%p need uref=%d\n", cpd, need_uref);
202 memset(crd, 0, sizeof(*crd));
204 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
205 cpd->bus = devclass_get_softc(usb_devclass_ptr, cpd->bus_index);
206 if (cpd->bus == NULL) {
207 DPRINTFN(2, "no bus at %u\n", cpd->bus_index);
210 cpd->udev = cpd->bus->devices[cpd->dev_index];
211 if (cpd->udev == NULL) {
212 DPRINTFN(2, "no device at %u\n", cpd->dev_index);
215 if (cpd->udev->refcount == USB_DEV_REF_MAX) {
216 DPRINTFN(2, "no dev ref\n");
220 DPRINTFN(2, "ref udev - needed\n");
221 cpd->udev->refcount++;
223 lockmgr(&usb_ref_lock, LK_RELEASE);
226 * We need to grab the sx-lock before grabbing the
227 * FIFO refs to avoid deadlock at detach!
229 crd->do_unlock = usbd_enum_lock(cpd->udev);
231 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
234 * Set "is_uref" after grabbing the default SX lock
239 /* check if we are doing an open */
240 if (cpd->fflags == 0) {
241 /* use zero defaults */
243 /* check for write */
244 if (cpd->fflags & FWRITE) {
245 ppf = cpd->udev->fifo;
246 f = ppf[cpd->fifo_index + USB_FIFO_TX];
248 crd->is_write = 1; /* ref */
249 if (f == NULL || f->refcount == USB_FIFO_REF_MAX)
251 if (f->curr_cpd != cpd)
253 /* check if USB-FS is active */
254 if (f->fs_ep_max != 0) {
260 if (cpd->fflags & FREAD) {
261 ppf = cpd->udev->fifo;
262 f = ppf[cpd->fifo_index + USB_FIFO_RX];
264 crd->is_read = 1; /* ref */
265 if (f == NULL || f->refcount == USB_FIFO_REF_MAX)
267 if (f->curr_cpd != cpd)
269 /* check if USB-FS is active */
270 if (f->fs_ep_max != 0) {
276 /* when everything is OK we increment the refcounts */
278 DPRINTFN(2, "ref write\n");
279 crd->txfifo->refcount++;
282 DPRINTFN(2, "ref read\n");
283 crd->rxfifo->refcount++;
285 lockmgr(&usb_ref_lock, LK_RELEASE);
291 usbd_enum_unlock(cpd->udev);
294 if (--(cpd->udev->refcount) == 0) {
295 cv_signal(&cpd->udev->ref_cv);
298 lockmgr(&usb_ref_lock, LK_RELEASE);
299 DPRINTFN(2, "fail\n");
300 return (USB_ERR_INVAL);
303 /*------------------------------------------------------------------------*
306 * This function is used to upgrade an USB reference to include the
307 * USB device reference on a USB location.
310 * 0: Success, refcount incremented on the given USB device.
312 *------------------------------------------------------------------------*/
314 usb_usb_ref_device(struct usb_cdev_privdata *cpd,
315 struct usb_cdev_refdata *crd)
318 * Check if we already got an USB reference on this location:
321 return (0); /* success */
324 * To avoid deadlock at detach we need to drop the FIFO ref
325 * and re-acquire a new ref!
327 usb_unref_device(cpd, crd);
329 return (usb_ref_device(cpd, crd, 1 /* need uref */));
332 /*------------------------------------------------------------------------*
335 * This function will release the reference count by one unit for the
337 *------------------------------------------------------------------------*/
339 usb_unref_device(struct usb_cdev_privdata *cpd,
340 struct usb_cdev_refdata *crd)
343 DPRINTFN(2, "cpd=%p is_uref=%d\n", cpd, crd->is_uref);
346 usbd_enum_unlock(cpd->udev);
348 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
350 if (--(crd->rxfifo->refcount) == 0) {
351 cv_signal(&crd->rxfifo->cv_drain);
356 if (--(crd->txfifo->refcount) == 0) {
357 cv_signal(&crd->txfifo->cv_drain);
362 if (--(cpd->udev->refcount) == 0) {
363 cv_signal(&cpd->udev->ref_cv);
367 lockmgr(&usb_ref_lock, LK_RELEASE);
370 static struct usb_fifo *
375 f = kmalloc(sizeof(*f), M_USBDEV, M_WAITOK | M_ZERO);
377 cv_init(&f->cv_io, "FIFO-IO");
378 cv_init(&f->cv_drain, "FIFO-DRAIN");
384 /*------------------------------------------------------------------------*
386 *------------------------------------------------------------------------*/
388 usb_fifo_create(struct usb_cdev_privdata *cpd,
389 struct usb_cdev_refdata *crd)
391 struct usb_device *udev = cpd->udev;
393 struct usb_endpoint *ep;
399 int e = cpd->ep_addr;
401 is_tx = (cpd->fflags & FWRITE) ? 1 : 0;
402 is_rx = (cpd->fflags & FREAD) ? 1 : 0;
406 /* Preallocated FIFO */
408 DPRINTFN(5, "Preallocated FIFO\n");
410 f = udev->fifo[cpd->fifo_index + USB_FIFO_TX];
416 f = udev->fifo[cpd->fifo_index + USB_FIFO_RX];
424 KASSERT(e >= 0 && e <= 15, ("endpoint %d out of range", e));
426 /* search for a free FIFO slot */
427 DPRINTFN(5, "Endpoint device, searching for 0x%02x\n", e);
428 for (n = 0;; n += 2) {
430 if (n == USB_FIFO_MAX) {
435 /* end of FIFOs reached */
436 DPRINTFN(5, "out of FIFOs\n");
440 /* Check for TX FIFO */
442 f = udev->fifo[n + USB_FIFO_TX];
444 if (f->dev_ep_index != e) {
445 /* wrong endpoint index */
448 if (f->curr_cpd != NULL) {
453 } else if (no_null) {
457 /* Check for RX FIFO */
459 f = udev->fifo[n + USB_FIFO_RX];
461 if (f->dev_ep_index != e) {
462 /* wrong endpoint index */
465 if (f->curr_cpd != NULL) {
470 } else if (no_null) {
478 if (e >= (USB_EP_MAX / 2)) {
479 /* we don't create any endpoints in this range */
480 DPRINTFN(5, "ep out of range\n");
481 return (is_busy ? EBUSY : EINVAL);
485 if ((e != 0) && is_busy) {
487 * Only the default control endpoint is allowed to be
488 * opened multiple times!
490 DPRINTFN(5, "busy\n");
496 (udev->fifo[n + USB_FIFO_TX] == NULL)) {
497 ep = usb_dev_get_ep(udev, e, USB_FIFO_TX);
498 DPRINTFN(5, "dev_get_endpoint(%d, 0x%x)\n", e, USB_FIFO_TX);
500 DPRINTFN(5, "dev_get_endpoint returned NULL\n");
503 f = usb_fifo_alloc();
505 DPRINTFN(5, "could not alloc tx fifo\n");
508 /* update some fields */
509 f->fifo_index = n + USB_FIFO_TX;
511 f->priv_lock = &udev->device_lock;
513 f->methods = &usb_ugen_methods;
514 f->iface_index = ep->iface_index;
516 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
517 udev->fifo[n + USB_FIFO_TX] = f;
518 lockmgr(&usb_ref_lock, LK_RELEASE);
522 (udev->fifo[n + USB_FIFO_RX] == NULL)) {
524 ep = usb_dev_get_ep(udev, e, USB_FIFO_RX);
525 DPRINTFN(5, "dev_get_endpoint(%d, 0x%x)\n", e, USB_FIFO_RX);
527 DPRINTFN(5, "dev_get_endpoint returned NULL\n");
530 f = usb_fifo_alloc();
532 DPRINTFN(5, "could not alloc rx fifo\n");
535 /* update some fields */
536 f->fifo_index = n + USB_FIFO_RX;
538 f->priv_lock = &udev->device_lock;
540 f->methods = &usb_ugen_methods;
541 f->iface_index = ep->iface_index;
543 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
544 udev->fifo[n + USB_FIFO_RX] = f;
545 lockmgr(&usb_ref_lock, LK_RELEASE);
548 crd->txfifo = udev->fifo[n + USB_FIFO_TX];
551 crd->rxfifo = udev->fifo[n + USB_FIFO_RX];
553 /* fill out fifo index */
554 DPRINTFN(5, "fifo index = %d\n", n);
563 usb_fifo_free(struct usb_fifo *f)
571 /* destroy symlink devices, if any */
572 for (n = 0; n != 2; n++) {
574 usb_free_symlink(f->symlink[n]);
575 f->symlink[n] = NULL;
578 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
580 /* delink ourselves to stop calls from userland */
581 if ((f->fifo_index < USB_FIFO_MAX) &&
583 (f->udev->fifo[f->fifo_index] == f)) {
584 f->udev->fifo[f->fifo_index] = NULL;
586 DPRINTFN(0, "USB FIFO %p has not been linked\n", f);
589 /* decrease refcount */
591 /* prevent any write flush */
593 /* need to wait until all callers have exited */
594 while (f->refcount != 0) {
595 lockmgr(&usb_ref_lock, LK_RELEASE); /* avoid LOR */
596 lockmgr(f->priv_lock, LK_EXCLUSIVE);
597 /* get I/O thread out of any sleep state */
598 if (f->flag_sleeping) {
599 f->flag_sleeping = 0;
600 cv_broadcast(&f->cv_io);
602 lockmgr(f->priv_lock, LK_RELEASE);
603 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
606 cv_wait(&f->cv_drain, &usb_ref_lock);
608 lockmgr(&usb_ref_lock, LK_RELEASE);
610 /* take care of closing the device here, if any */
611 usb_fifo_close(f, 0);
613 cv_destroy(&f->cv_io);
614 cv_destroy(&f->cv_drain);
619 static struct usb_endpoint *
620 usb_dev_get_ep(struct usb_device *udev, uint8_t ep_index, uint8_t dir)
622 struct usb_endpoint *ep;
628 if (dir == USB_FIFO_RX) {
629 if (udev->flags.usb_mode == USB_MODE_HOST) {
635 if (udev->flags.usb_mode == USB_MODE_HOST) {
641 ep = usbd_get_ep_by_addr(udev, ep_index | ep_dir);
645 /* if the endpoint does not exist then return */
648 if (ep->edesc == NULL) {
649 /* invalid endpoint */
652 return (ep); /* success */
655 /*------------------------------------------------------------------------*
661 *------------------------------------------------------------------------*/
663 usb_fifo_open(struct usb_cdev_privdata *cpd,
664 struct usb_fifo *f, int fflags)
670 DPRINTFN(2, "no FIFO\n");
673 /* remove FWRITE and FREAD flags */
674 fflags &= ~(FWRITE | FREAD);
676 /* set correct file flags */
677 if ((f->fifo_index & 1) == USB_FIFO_TX) {
683 /* check if we are already opened */
684 /* we don't need any locks when checking this variable */
685 if (f->curr_cpd != NULL) {
690 /* reset short flag before open */
693 /* call open method */
694 err = (f->methods->f_open) (f, fflags);
698 lockmgr(f->priv_lock, LK_EXCLUSIVE);
700 /* reset sleep flag */
701 f->flag_sleeping = 0;
703 /* reset error flag */
706 /* reset complete flag */
707 f->flag_iscomplete = 0;
709 /* reset select flag */
710 f->flag_isselect = 0;
712 /* reset flushing flag */
713 f->flag_flushing = 0;
715 /* reset ASYNC proc flag */
718 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
719 /* flag the fifo as opened to prevent others */
721 lockmgr(&usb_ref_lock, LK_RELEASE);
726 lockmgr(f->priv_lock, LK_RELEASE);
731 /*------------------------------------------------------------------------*
733 *------------------------------------------------------------------------*/
735 usb_fifo_reset(struct usb_fifo *f)
743 USB_IF_DEQUEUE(&f->used_q, m);
745 USB_IF_ENQUEUE(&f->free_q, m);
750 /* reset have fragment flag */
751 f->flag_have_fragment = 0;
754 /*------------------------------------------------------------------------*
756 *------------------------------------------------------------------------*/
758 usb_fifo_close(struct usb_fifo *f, int fflags)
762 /* check if we are not opened */
763 if (f->curr_cpd == NULL) {
764 /* nothing to do - already closed */
767 lockmgr(f->priv_lock, LK_EXCLUSIVE);
769 /* clear current cdev private data pointer */
772 /* check if we are selected */
773 if (f->flag_isselect) {
775 selwakeup(&f->selinfo);
777 f->flag_isselect = 0;
779 /* check if a thread wants SIGIO */
780 if (f->async_p != NULL && lwkt_trytoken(&f->async_p->p_token)) {
781 ksignal(f->async_p, SIGIO);
782 lwkt_reltoken(&f->async_p->p_token);
785 /* remove FWRITE and FREAD flags */
786 fflags &= ~(FWRITE | FREAD);
788 /* flush written data, if any */
789 if ((f->fifo_index & 1) == USB_FIFO_TX) {
791 if (!f->flag_iserror) {
793 /* set flushing flag */
794 f->flag_flushing = 1;
796 /* get the last packet in */
797 if (f->flag_have_fragment) {
799 f->flag_have_fragment = 0;
800 USB_IF_DEQUEUE(&f->free_q, m);
802 USB_IF_ENQUEUE(&f->used_q, m);
806 /* start write transfer, if not already started */
807 (f->methods->f_start_write) (f);
809 /* check if flushed already */
810 while (f->flag_flushing &&
811 (!f->flag_iserror)) {
812 /* wait until all data has been written */
813 f->flag_sleeping = 1;
814 err = cv_wait_sig(&f->cv_io, f->priv_lock);
816 DPRINTF("signal received\n");
823 /* stop write transfer, if not already stopped */
824 (f->methods->f_stop_write) (f);
828 /* stop write transfer, if not already stopped */
829 (f->methods->f_stop_read) (f);
832 /* check if we are sleeping */
833 if (f->flag_sleeping) {
834 DPRINTFN(2, "Sleeping at close!\n");
836 lockmgr(f->priv_lock, LK_RELEASE);
838 /* call close method */
839 (f->methods->f_close) (f, fflags);
844 /*------------------------------------------------------------------------*
845 * usb_open - cdev callback
846 *------------------------------------------------------------------------*/
848 usb_open(struct dev_open_args *ap)
850 struct cdev *dev = ap->a_head.a_dev;
851 int fflags = ap->a_oflags;
852 struct usb_fs_privdata* pd = (struct usb_fs_privdata*)dev->si_drv1;
853 struct usb_cdev_refdata refs;
854 struct usb_cdev_privdata *cpd;
857 DPRINTFN(2, "%s fflags=0x%08x\n", devtoname(dev), fflags);
859 KASSERT(fflags & (FREAD|FWRITE), ("invalid open flags"));
860 if (((fflags & FREAD) && !(pd->mode & FREAD)) ||
861 ((fflags & FWRITE) && !(pd->mode & FWRITE))) {
862 DPRINTFN(2, "access mode not supported\n");
866 cpd = kmalloc(sizeof(*cpd), M_USBDEV, M_WAITOK | M_ZERO);
867 ep = cpd->ep_addr = pd->ep_addr;
869 usb_loc_fill(pd, cpd);
870 err = usb_ref_device(cpd, &refs, 1);
872 DPRINTFN(2, "cannot ref device\n");
873 kfree(cpd, M_USBDEV);
876 cpd->fflags = fflags; /* access mode for open lifetime */
878 /* create FIFOs, if any */
879 err = usb_fifo_create(cpd, &refs);
880 /* check for error */
882 DPRINTFN(2, "cannot create fifo\n");
883 usb_unref_device(cpd, &refs);
884 kfree(cpd, M_USBDEV);
887 if (fflags & FREAD) {
888 err = usb_fifo_open(cpd, refs.rxfifo, fflags);
890 DPRINTFN(2, "read open failed\n");
891 usb_unref_device(cpd, &refs);
892 kfree(cpd, M_USBDEV);
896 if (fflags & FWRITE) {
897 err = usb_fifo_open(cpd, refs.txfifo, fflags);
899 DPRINTFN(2, "write open failed\n");
900 if (fflags & FREAD) {
901 usb_fifo_close(refs.rxfifo, fflags);
903 usb_unref_device(cpd, &refs);
904 kfree(cpd, M_USBDEV);
908 usb_unref_device(cpd, &refs);
909 err = devfs_set_cdevpriv(ap->a_fp, cpd, usb_cdevpriv_dtor);
911 DPRINTFN(2, "devfs_set_cdevpriv failed in %s\n", __func__);
912 kfree(cpd, M_USBDEV);
918 /*------------------------------------------------------------------------*
919 * usb_close - cdev callback
920 *------------------------------------------------------------------------*/
922 usb_close(struct dev_close_args *ap)
924 struct usb_cdev_refdata refs;
925 struct usb_cdev_privdata *cpd;
928 err = devfs_get_cdevpriv(ap->a_fp, (void **)&cpd);
932 DPRINTFN(2, "cpd=%p\n", cpd);
934 err = usb_ref_device(cpd, &refs, 0);
939 * If this function is not called directly from the root HUB
940 * thread, there is usually a need to lock the enumeration
943 if (!usbd_enum_is_locked(cpd->udev)) {
945 DPRINTFN(2, "Locking enumeration\n");
947 /* reference device */
948 err = usb_usb_ref_device(cpd, &refs);
952 if (cpd->fflags & FREAD) {
953 usb_fifo_close(refs.rxfifo, cpd->fflags);
955 if (cpd->fflags & FWRITE) {
956 usb_fifo_close(refs.txfifo, cpd->fflags);
958 usb_unref_device(cpd, &refs);
960 kfree(cpd, M_USBDEV);
965 usb_dev_init(void *arg)
967 lockinit(&usb_ref_lock, "USB ref mutex", 0, 0);
968 lockinit(&usb_sym_lock, "USB sym mutex", 0, 0);
969 TAILQ_INIT(&usb_sym_head);
971 /* check the UGEN methods */
972 usb_fifo_check_methods(&usb_ugen_methods);
975 /* XXX SI_SUB_KLD? */
976 SYSINIT(usb_dev_init, SI_SUB_PRE_DRIVERS, SI_ORDER_FIRST, usb_dev_init, NULL);
979 usb_dev_init_post(void *arg)
982 * Create /dev/usb - this is needed for usbconfig(8), which
983 * needs a well-known device name to access.
985 usb_dev = make_dev(&usb_static_ops, 0, UID_ROOT, GID_OPERATOR,
986 0644, USB_DEVICE_NAME);
987 if (usb_dev == NULL) {
988 DPRINTFN(0, "Could not create usb bus device\n");
992 SYSINIT(usb_dev_init_post, SI_SUB_DRIVERS, SI_ORDER_FIRST, usb_dev_init_post,
996 usb_dev_uninit(void *arg)
998 if (usb_dev != NULL) {
999 destroy_dev(usb_dev);
1002 lockuninit(&usb_ref_lock);
1003 lockuninit(&usb_sym_lock);
1006 SYSUNINIT(usb_dev_uninit, SI_SUB_KICK_SCHEDULER, SI_ORDER_ANY, usb_dev_uninit, NULL);
1009 usb_ioctl_f_sub(struct usb_fifo *f, u_long cmd, void *addr,
1016 *(int *)addr = 0; /* character device */
1020 /* handled by upper FS layer */
1025 if (f->async_p != NULL) {
1029 f->async_p = USB_TD_GET_PROC(td);
1035 /* XXX this is not the most general solution */
1037 if (f->async_p == NULL) {
1041 if (*(int *)addr != USB_PROC_GET_GID(f->async_p)) {
1049 DPRINTFN(3, "cmd 0x%lx = %d\n", cmd, error);
1053 /*------------------------------------------------------------------------*
1054 * usb_ioctl - cdev callback
1055 *------------------------------------------------------------------------*/
1057 usb_ioctl(struct dev_ioctl_args *ap)
1059 u_long cmd = ap->a_cmd;
1060 caddr_t addr = ap->a_data;
1061 struct thread *td = curthread;
1062 struct usb_cdev_refdata refs;
1063 struct usb_cdev_privdata* cpd;
1068 DPRINTFN(2, "cmd=0x%lx\n", cmd);
1070 err = devfs_get_cdevpriv(ap->a_fp, (void **)&cpd);
1075 * Performance optimisation: We try to check for IOCTL's that
1076 * don't need the USB reference first. Then we grab the USB
1077 * reference if we need it!
1079 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1083 fflags = cpd->fflags;
1085 f = NULL; /* set default value */
1086 err = ENOIOCTL; /* set default value */
1088 if (fflags & FWRITE) {
1090 err = usb_ioctl_f_sub(f, cmd, addr, td);
1092 if (fflags & FREAD) {
1094 err = usb_ioctl_f_sub(f, cmd, addr, td);
1096 KASSERT(f != NULL, ("fifo not found"));
1097 if (err != ENOIOCTL)
1100 err = (f->methods->f_ioctl) (f, cmd, addr, fflags);
1102 DPRINTFN(2, "f_ioctl cmd 0x%lx = %d\n", cmd, err);
1104 if (err != ENOIOCTL)
1107 if (usb_usb_ref_device(cpd, &refs)) {
1112 err = (f->methods->f_ioctl_post) (f, cmd, addr, fflags);
1114 DPRINTFN(2, "f_ioctl_post cmd 0x%lx = %d\n", cmd, err);
1116 if (err == ENOIOCTL)
1122 /* Wait for re-enumeration, if any */
1124 while (f->udev->re_enumerate_wait != 0) {
1126 usb_unref_device(cpd, &refs);
1128 usb_pause_mtx(NULL, hz / 128);
1130 if (usb_ref_device(cpd, &refs, 1 /* need uref */)) {
1137 usb_unref_device(cpd, &refs);
1141 static struct filterops usb_filtops_read =
1142 { FILTEROP_ISFD, NULL, usb_filter_detach, usb_filter_read };
1144 static struct filterops usb_filtops_write =
1145 { FILTEROP_ISFD, NULL, usb_filter_detach, usb_filter_write };
1148 usb_kqfilter(struct dev_kqfilter_args *ap)
1150 cdev_t dev = ap->a_head.a_dev;
1151 struct knote *kn = ap->a_kn;
1152 struct klist *klist;
1156 switch(kn->kn_filter) {
1158 kn->kn_fop = &usb_filtops_read;
1159 kn->kn_hook = (caddr_t)dev;
1162 kn->kn_fop = &usb_filtops_write;
1163 kn->kn_hook = (caddr_t)dev;
1166 ap->a_result = EOPNOTSUPP;
1170 klist = &usb_kqevent.ki_note;
1171 knote_insert(klist, kn);
1177 usb_filter_detach(struct knote *kn)
1179 struct klist *klist;
1181 klist = &usb_kqevent.ki_note;
1182 knote_remove(klist, kn);
1186 usb_filter_read(struct knote *kn, long hint)
1188 /*cdev_t dev = (cdev_t)kn->kn_hook;
1196 usb_filter_write(struct knote *kn, long hint)
1198 /*cdev_t dev = (cdev_t)kn->kn_hook;
1205 #if 0 /* XXX implement using kqfilter */
1208 usb_poll(struct cdev* dev, int events, struct thread* td)
1210 struct usb_cdev_refdata refs;
1211 struct usb_cdev_privdata* cpd;
1214 int fflags, revents;
1216 if (devfs_get_cdevpriv((void **)&cpd) != 0 ||
1217 usb_ref_device(cpd, &refs, 0) != 0)
1219 (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM));
1221 fflags = cpd->fflags;
1223 /* Figure out who needs service */
1225 if ((events & (POLLOUT | POLLWRNORM)) &&
1226 (fflags & FWRITE)) {
1230 lockmgr(f->priv_lock, LK_EXCLUSIVE);
1232 if (!refs.is_usbfs) {
1233 if (f->flag_iserror) {
1234 /* we got an error */
1237 if (f->queue_data == NULL) {
1239 * start write transfer, if not
1242 (f->methods->f_start_write) (f);
1244 /* check if any packets are available */
1245 USB_IF_POLL(&f->free_q, m);
1248 if (f->flag_iscomplete) {
1256 revents |= events & (POLLOUT | POLLWRNORM);
1258 f->flag_isselect = 1;
1259 selrecord(td, &f->selinfo);
1262 lockmgr(f->priv_lock);
1264 if ((events & (POLLIN | POLLRDNORM)) &&
1269 lockmgr(f->priv_lock, LK_EXCLUSIVE);
1271 if (!refs.is_usbfs) {
1272 if (f->flag_iserror) {
1273 /* we have and error */
1276 if (f->queue_data == NULL) {
1278 * start read transfer, if not
1281 (f->methods->f_start_read) (f);
1283 /* check if any packets are available */
1284 USB_IF_POLL(&f->used_q, m);
1287 if (f->flag_iscomplete) {
1295 revents |= events & (POLLIN | POLLRDNORM);
1297 f->flag_isselect = 1;
1298 selrecord(td, &f->selinfo);
1300 if (!refs.is_usbfs) {
1301 /* start reading data */
1302 (f->methods->f_start_read) (f);
1306 lockmgr(f->priv_lock, LK_RELEASE);
1308 usb_unref_device(cpd, &refs);
1314 usb_read(struct dev_read_args *ap)
1316 struct uio *uio = ap->a_uio;
1317 int ioflag = ap->a_ioflag;
1318 struct usb_cdev_refdata refs;
1319 struct usb_cdev_privdata* cpd;
1326 uint8_t tr_data = 0;
1328 err = devfs_get_cdevpriv(ap->a_fp, (void **)&cpd);
1331 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1335 fflags = cpd->fflags;
1339 /* should not happen */
1340 usb_unref_device(cpd, &refs);
1344 resid = uio->uio_resid;
1346 lockmgr(f->priv_lock, LK_EXCLUSIVE);
1348 /* check for permanent read error */
1349 if (f->flag_iserror) {
1353 /* check if USB-FS interface is active */
1354 if (refs.is_usbfs) {
1356 * The queue is used for events that should be
1357 * retrieved using the "USB_FS_COMPLETE" ioctl.
1362 while (uio->uio_resid > 0) {
1364 USB_IF_DEQUEUE(&f->used_q, m);
1368 /* start read transfer, if not already started */
1370 (f->methods->f_start_read) (f);
1372 if (ioflag & IO_NDELAY) {
1374 /* return length before error */
1380 DPRINTF("sleeping\n");
1382 err = usb_fifo_wait(f);
1388 if (f->methods->f_filter_read) {
1390 * Sometimes it is convenient to process data at the
1391 * expense of a userland process instead of a kernel
1394 (f->methods->f_filter_read) (f, m);
1398 io_len = MIN(m->cur_data_len, uio->uio_resid);
1400 DPRINTFN(2, "transfer %d bytes from %p\n",
1401 io_len, m->cur_data_ptr);
1403 err = usb_fifo_uiomove(f,
1404 m->cur_data_ptr, io_len, uio);
1406 m->cur_data_len -= io_len;
1407 m->cur_data_ptr += io_len;
1409 if (m->cur_data_len == 0) {
1411 uint8_t last_packet;
1413 last_packet = m->last_packet;
1415 USB_IF_ENQUEUE(&f->free_q, m);
1422 USB_IF_PREPEND(&f->used_q, m);
1430 lockmgr(f->priv_lock, LK_RELEASE);
1432 usb_unref_device(cpd, &refs);
1438 usb_write(struct dev_write_args *ap)
1440 struct uio *uio = ap->a_uio;
1441 int ioflag = ap->a_ioflag;
1442 struct usb_cdev_refdata refs;
1443 struct usb_cdev_privdata* cpd;
1451 uint8_t tr_data = 0;
1455 err = devfs_get_cdevpriv(ap->a_fp, (void **)&cpd);
1458 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1462 fflags = cpd->fflags;
1466 /* should not happen */
1467 usb_unref_device(cpd, &refs);
1470 resid = uio->uio_resid;
1472 lockmgr(f->priv_lock, LK_EXCLUSIVE);
1474 /* check for permanent write error */
1475 if (f->flag_iserror) {
1479 /* check if USB-FS interface is active */
1480 if (refs.is_usbfs) {
1482 * The queue is used for events that should be
1483 * retrieved using the "USB_FS_COMPLETE" ioctl.
1488 if (f->queue_data == NULL) {
1489 /* start write transfer, if not already started */
1490 (f->methods->f_start_write) (f);
1492 /* we allow writing zero length data */
1494 USB_IF_DEQUEUE(&f->free_q, m);
1498 if (ioflag & IO_NDELAY) {
1500 /* return length before error */
1506 DPRINTF("sleeping\n");
1508 err = usb_fifo_wait(f);
1516 if (f->flag_have_fragment == 0) {
1518 io_len = m->cur_data_len;
1519 pdata = m->cur_data_ptr;
1520 if (io_len > uio->uio_resid)
1521 io_len = uio->uio_resid;
1522 m->cur_data_len = io_len;
1524 io_len = m->max_data_len - m->cur_data_len;
1525 pdata = m->cur_data_ptr + m->cur_data_len;
1526 if (io_len > uio->uio_resid)
1527 io_len = uio->uio_resid;
1528 m->cur_data_len += io_len;
1531 DPRINTFN(2, "transfer %d bytes to %p\n",
1534 err = usb_fifo_uiomove(f, pdata, io_len, uio);
1537 f->flag_have_fragment = 0;
1538 USB_IF_ENQUEUE(&f->free_q, m);
1542 /* check if the buffer is ready to be transmitted */
1544 if ((f->flag_write_defrag == 0) ||
1545 (m->cur_data_len == m->max_data_len)) {
1546 f->flag_have_fragment = 0;
1549 * Check for write filter:
1551 * Sometimes it is convenient to process data
1552 * at the expense of a userland process
1553 * instead of a kernel process.
1555 if (f->methods->f_filter_write) {
1556 (f->methods->f_filter_write) (f, m);
1559 /* Put USB mbuf in the used queue */
1560 USB_IF_ENQUEUE(&f->used_q, m);
1562 /* Start writing data, if not already started */
1563 (f->methods->f_start_write) (f);
1565 /* Wait for more data or close */
1566 f->flag_have_fragment = 1;
1567 USB_IF_PREPEND(&f->free_q, m);
1570 } while (uio->uio_resid > 0);
1572 lockmgr(f->priv_lock, LK_RELEASE);
1574 usb_unref_device(cpd, &refs);
1580 usb_static_open(struct dev_open_args *ap)
1586 usb_static_close(struct dev_close_args *ap)
1592 usb_static_ioctl(struct dev_ioctl_args *ap)
1594 u_long cmd = ap->a_cmd;
1595 caddr_t data = ap->a_data;
1596 struct thread *td = curthread; /* XXX: curthread the correct choice? */
1597 int fflag = ap->a_fflag;
1599 struct usb_read_dir *urd;
1607 err = usb_read_symlink(u.urd->urd_data,
1608 u.urd->urd_startentry, u.urd->urd_maxlen);
1610 case USB_DEV_QUIRK_GET:
1611 case USB_QUIRK_NAME_GET:
1612 case USB_DEV_QUIRK_ADD:
1613 case USB_DEV_QUIRK_REMOVE:
1614 err = usb_quirk_ioctl_p(cmd, data, fflag, td);
1616 case USB_GET_TEMPLATE:
1617 *(int *)data = usb_template;
1620 case USB_SET_TEMPLATE:
1621 err = priv_check(curthread, PRIV_DRIVER);
1624 usb_template = *(int *)data;
1634 usb_fifo_uiomove(struct usb_fifo *f, void *cp,
1635 int n, struct uio *uio)
1639 lockmgr(f->priv_lock, LK_RELEASE);
1642 * "uiomove()" can sleep so one needs to make a wrapper,
1643 * exiting the mutex and checking things:
1645 error = uiomove(cp, n, uio);
1647 lockmgr(f->priv_lock, LK_EXCLUSIVE);
1653 usb_fifo_wait(struct usb_fifo *f)
1657 KKASSERT(lockowned(f->priv_lock));
1659 if (f->flag_iserror) {
1663 f->flag_sleeping = 1;
1665 err = cv_wait_sig(&f->cv_io, f->priv_lock);
1667 if (f->flag_iserror) {
1675 usb_fifo_signal(struct usb_fifo *f)
1677 if (f->flag_sleeping) {
1678 f->flag_sleeping = 0;
1679 cv_broadcast(&f->cv_io);
1684 usb_fifo_wakeup(struct usb_fifo *f)
1688 if (f->flag_isselect) {
1690 selwakeup(&f->selinfo);
1692 f->flag_isselect = 0;
1694 if (f->async_p != NULL && lwkt_trytoken(&f->async_p->p_token)) {
1695 ksignal(f->async_p, SIGIO);
1696 lwkt_reltoken(&f->async_p->p_token);
1701 usb_fifo_dummy_open(struct usb_fifo *fifo, int fflags)
1707 usb_fifo_dummy_close(struct usb_fifo *fifo, int fflags)
1713 usb_fifo_dummy_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
1719 usb_fifo_dummy_cmd(struct usb_fifo *fifo)
1721 fifo->flag_flushing = 0; /* not flushing */
1725 usb_fifo_check_methods(struct usb_fifo_methods *pm)
1727 /* check that all callback functions are OK */
1729 if (pm->f_open == NULL)
1730 pm->f_open = &usb_fifo_dummy_open;
1732 if (pm->f_close == NULL)
1733 pm->f_close = &usb_fifo_dummy_close;
1735 if (pm->f_ioctl == NULL)
1736 pm->f_ioctl = &usb_fifo_dummy_ioctl;
1738 if (pm->f_ioctl_post == NULL)
1739 pm->f_ioctl_post = &usb_fifo_dummy_ioctl;
1741 if (pm->f_start_read == NULL)
1742 pm->f_start_read = &usb_fifo_dummy_cmd;
1744 if (pm->f_stop_read == NULL)
1745 pm->f_stop_read = &usb_fifo_dummy_cmd;
1747 if (pm->f_start_write == NULL)
1748 pm->f_start_write = &usb_fifo_dummy_cmd;
1750 if (pm->f_stop_write == NULL)
1751 pm->f_stop_write = &usb_fifo_dummy_cmd;
1754 /*------------------------------------------------------------------------*
1757 * The following function will create a duplex FIFO.
1762 *------------------------------------------------------------------------*/
1764 usb_fifo_attach(struct usb_device *udev, void *priv_sc,
1765 struct lock *priv_lock, struct usb_fifo_methods *pm,
1766 struct usb_fifo_sc *f_sc, uint16_t unit, int16_t subunit,
1767 uint8_t iface_index, uid_t uid, gid_t gid, int mode)
1769 struct usb_fifo *f_tx;
1770 struct usb_fifo *f_rx;
1774 f_sc->fp[USB_FIFO_TX] = NULL;
1775 f_sc->fp[USB_FIFO_RX] = NULL;
1780 /* check the methods */
1781 usb_fifo_check_methods(pm);
1783 /* search for a free FIFO slot */
1784 for (n = 0;; n += 2) {
1786 if (n == USB_FIFO_MAX) {
1787 /* end of FIFOs reached */
1790 /* Check for TX FIFO */
1791 if (udev->fifo[n + USB_FIFO_TX] != NULL) {
1794 /* Check for RX FIFO */
1795 if (udev->fifo[n + USB_FIFO_RX] != NULL) {
1801 f_tx = usb_fifo_alloc();
1802 f_rx = usb_fifo_alloc();
1804 if ((f_tx == NULL) || (f_rx == NULL)) {
1805 usb_fifo_free(f_tx);
1806 usb_fifo_free(f_rx);
1809 /* initialise FIFO structures */
1811 f_tx->fifo_index = n + USB_FIFO_TX;
1812 f_tx->dev_ep_index = -1;
1813 f_tx->priv_lock = priv_lock;
1814 f_tx->priv_sc0 = priv_sc;
1816 f_tx->iface_index = iface_index;
1819 f_rx->fifo_index = n + USB_FIFO_RX;
1820 f_rx->dev_ep_index = -1;
1821 f_rx->priv_lock = priv_lock;
1822 f_rx->priv_sc0 = priv_sc;
1824 f_rx->iface_index = iface_index;
1827 f_sc->fp[USB_FIFO_TX] = f_tx;
1828 f_sc->fp[USB_FIFO_RX] = f_rx;
1830 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
1831 udev->fifo[f_tx->fifo_index] = f_tx;
1832 udev->fifo[f_rx->fifo_index] = f_rx;
1833 lockmgr(&usb_ref_lock, LK_RELEASE);
1835 for (n = 0; n != 4; n++) {
1837 if (pm->basename[n] == NULL) {
1840 if (subunit == 0xFFFF) {
1841 if (ksnprintf(devname, sizeof(devname),
1842 "%s%u%s", pm->basename[n],
1843 unit, pm->postfix[n] ?
1844 pm->postfix[n] : "")) {
1848 if (ksnprintf(devname, sizeof(devname),
1849 "%s%u.%u%s", pm->basename[n],
1850 unit, subunit, pm->postfix[n] ?
1851 pm->postfix[n] : "")) {
1857 * Distribute the symbolic links into two FIFO structures:
1860 f_rx->symlink[n / 2] =
1861 usb_alloc_symlink(devname);
1863 f_tx->symlink[n / 2] =
1864 usb_alloc_symlink(devname);
1867 /* Create the device */
1868 f_sc->dev = usb_make_dev(udev, devname, -1,
1869 f_tx->fifo_index & f_rx->fifo_index,
1870 FREAD|FWRITE, uid, gid, mode);
1873 DPRINTFN(2, "attached %p/%p\n", f_tx, f_rx);
1877 /*------------------------------------------------------------------------*
1878 * usb_fifo_alloc_buffer
1883 *------------------------------------------------------------------------*/
1885 usb_fifo_alloc_buffer(struct usb_fifo *f, usb_size_t bufsize,
1888 usb_fifo_free_buffer(f);
1890 /* allocate an endpoint */
1891 f->free_q.ifq_maxlen = nbuf;
1892 f->used_q.ifq_maxlen = nbuf;
1894 f->queue_data = usb_alloc_mbufs(
1895 M_USBDEV, &f->free_q, bufsize, nbuf);
1897 if ((f->queue_data == NULL) && bufsize && nbuf) {
1900 return (0); /* success */
1903 /*------------------------------------------------------------------------*
1904 * usb_fifo_free_buffer
1906 * This function will free the buffers associated with a FIFO. This
1907 * function can be called multiple times in a row.
1908 *------------------------------------------------------------------------*/
1910 usb_fifo_free_buffer(struct usb_fifo *f)
1912 if (f->queue_data) {
1913 /* free old buffer */
1914 kfree(f->queue_data, M_USBDEV);
1915 f->queue_data = NULL;
1919 memset(&f->free_q, 0, sizeof(f->free_q));
1920 memset(&f->used_q, 0, sizeof(f->used_q));
1924 usb_fifo_detach(struct usb_fifo_sc *f_sc)
1929 usb_fifo_free(f_sc->fp[USB_FIFO_TX]);
1930 usb_fifo_free(f_sc->fp[USB_FIFO_RX]);
1932 f_sc->fp[USB_FIFO_TX] = NULL;
1933 f_sc->fp[USB_FIFO_RX] = NULL;
1935 usb_destroy_dev(f_sc->dev);
1939 DPRINTFN(2, "detached %p\n", f_sc);
1943 usb_fifo_put_bytes_max(struct usb_fifo *f)
1948 USB_IF_POLL(&f->free_q, m);
1951 len = m->max_data_len;
1958 /*------------------------------------------------------------------------*
1962 * 0 - normal operation
1963 * 1 - set last packet flag to enforce framing
1964 *------------------------------------------------------------------------*/
1966 usb_fifo_put_data(struct usb_fifo *f, struct usb_page_cache *pc,
1967 usb_frlength_t offset, usb_frlength_t len, uint8_t what)
1970 usb_frlength_t io_len;
1972 while (len || (what == 1)) {
1974 USB_IF_DEQUEUE(&f->free_q, m);
1979 io_len = MIN(len, m->cur_data_len);
1981 usbd_copy_out(pc, offset, m->cur_data_ptr, io_len);
1983 m->cur_data_len = io_len;
1987 if ((len == 0) && (what == 1)) {
1990 USB_IF_ENQUEUE(&f->used_q, m);
1994 if ((len == 0) || (what == 1)) {
2004 usb_fifo_put_data_linear(struct usb_fifo *f, void *ptr,
2005 usb_size_t len, uint8_t what)
2010 while (len || (what == 1)) {
2012 USB_IF_DEQUEUE(&f->free_q, m);
2017 io_len = MIN(len, m->cur_data_len);
2019 memcpy(m->cur_data_ptr, ptr, io_len);
2021 m->cur_data_len = io_len;
2022 ptr = USB_ADD_BYTES(ptr, io_len);
2025 if ((len == 0) && (what == 1)) {
2028 USB_IF_ENQUEUE(&f->used_q, m);
2032 if ((len == 0) || (what == 1)) {
2042 usb_fifo_put_data_buffer(struct usb_fifo *f, void *ptr, usb_size_t len)
2046 USB_IF_DEQUEUE(&f->free_q, m);
2049 m->cur_data_len = len;
2050 m->cur_data_ptr = ptr;
2051 USB_IF_ENQUEUE(&f->used_q, m);
2059 usb_fifo_put_data_error(struct usb_fifo *f)
2061 f->flag_iserror = 1;
2065 /*------------------------------------------------------------------------*
2069 * 0 - normal operation
2070 * 1 - only get one "usb_mbuf"
2074 * 1 - data in buffer
2075 *------------------------------------------------------------------------*/
2077 usb_fifo_get_data(struct usb_fifo *f, struct usb_page_cache *pc,
2078 usb_frlength_t offset, usb_frlength_t len, usb_frlength_t *actlen,
2082 usb_frlength_t io_len;
2083 uint8_t tr_data = 0;
2089 USB_IF_DEQUEUE(&f->used_q, m);
2095 io_len = MIN(len, m->cur_data_len);
2097 usbd_copy_in(pc, offset, m->cur_data_ptr, io_len);
2101 actlen[0] += io_len;
2102 m->cur_data_ptr += io_len;
2103 m->cur_data_len -= io_len;
2105 if ((m->cur_data_len == 0) || (what == 1)) {
2106 USB_IF_ENQUEUE(&f->free_q, m);
2114 USB_IF_PREPEND(&f->used_q, m);
2119 /* wait for data to be written out */
2122 if (f->flag_flushing) {
2123 /* check if we should send a short packet */
2124 if (f->flag_short != 0) {
2129 /* flushing complete */
2130 f->flag_flushing = 0;
2143 usb_fifo_get_data_linear(struct usb_fifo *f, void *ptr,
2144 usb_size_t len, usb_size_t *actlen, uint8_t what)
2148 uint8_t tr_data = 0;
2154 USB_IF_DEQUEUE(&f->used_q, m);
2160 io_len = MIN(len, m->cur_data_len);
2162 memcpy(ptr, m->cur_data_ptr, io_len);
2165 ptr = USB_ADD_BYTES(ptr, io_len);
2166 actlen[0] += io_len;
2167 m->cur_data_ptr += io_len;
2168 m->cur_data_len -= io_len;
2170 if ((m->cur_data_len == 0) || (what == 1)) {
2171 USB_IF_ENQUEUE(&f->free_q, m);
2179 USB_IF_PREPEND(&f->used_q, m);
2184 /* wait for data to be written out */
2187 if (f->flag_flushing) {
2188 /* check if we should send a short packet */
2189 if (f->flag_short != 0) {
2194 /* flushing complete */
2195 f->flag_flushing = 0;
2208 usb_fifo_get_data_buffer(struct usb_fifo *f, void **pptr, usb_size_t *plen)
2212 USB_IF_POLL(&f->used_q, m);
2215 *plen = m->cur_data_len;
2216 *pptr = m->cur_data_ptr;
2224 usb_fifo_get_data_error(struct usb_fifo *f)
2226 f->flag_iserror = 1;
2230 /*------------------------------------------------------------------------*
2235 * Else: Pointer to symlink entry
2236 *------------------------------------------------------------------------*/
2237 struct usb_symlink *
2238 usb_alloc_symlink(const char *target)
2240 struct usb_symlink *ps;
2242 ps = kmalloc(sizeof(*ps), M_USBDEV, M_WAITOK);
2246 /* XXX no longer needed */
2247 strlcpy(ps->src_path, target, sizeof(ps->src_path));
2248 ps->src_len = strlen(ps->src_path);
2249 strlcpy(ps->dst_path, target, sizeof(ps->dst_path));
2250 ps->dst_len = strlen(ps->dst_path);
2252 lockmgr(&usb_sym_lock, LK_EXCLUSIVE);
2253 TAILQ_INSERT_TAIL(&usb_sym_head, ps, sym_entry);
2254 lockmgr(&usb_sym_lock, LK_RELEASE);
2258 /*------------------------------------------------------------------------*
2260 *------------------------------------------------------------------------*/
2262 usb_free_symlink(struct usb_symlink *ps)
2267 lockmgr(&usb_sym_lock, LK_EXCLUSIVE);
2268 TAILQ_REMOVE(&usb_sym_head, ps, sym_entry);
2269 lockmgr(&usb_sym_lock, LK_RELEASE);
2271 kfree(ps, M_USBDEV);
2274 /*------------------------------------------------------------------------*
2280 *------------------------------------------------------------------------*/
2282 usb_read_symlink(uint8_t *user_ptr, uint32_t startentry, uint32_t user_len)
2284 struct usb_symlink *ps;
2290 lockmgr(&usb_sym_lock, LK_EXCLUSIVE);
2292 TAILQ_FOREACH(ps, &usb_sym_head, sym_entry) {
2295 * Compute total length of source and destination symlink
2296 * strings pluss one length byte and two NUL bytes:
2298 temp = ps->src_len + ps->dst_len + 3;
2302 * Skip entry because this length cannot fit
2307 if (startentry != 0) {
2308 /* decrement read offset */
2312 if (temp > user_len) {
2313 /* out of buffer space */
2318 /* copy out total length */
2320 error = copyout(&len,
2321 USB_ADD_BYTES(user_ptr, delta), 1);
2327 /* copy out source string */
2329 error = copyout(ps->src_path,
2330 USB_ADD_BYTES(user_ptr, delta), ps->src_len);
2335 delta += ps->src_len;
2336 error = copyout(&len,
2337 USB_ADD_BYTES(user_ptr, delta), 1);
2343 /* copy out destination string */
2345 error = copyout(ps->dst_path,
2346 USB_ADD_BYTES(user_ptr, delta), ps->dst_len);
2351 delta += ps->dst_len;
2352 error = copyout(&len,
2353 USB_ADD_BYTES(user_ptr, delta), 1);
2362 /* a zero length entry indicates the end */
2364 if ((user_len != 0) && (error == 0)) {
2368 error = copyout(&len,
2369 USB_ADD_BYTES(user_ptr, delta), 1);
2371 lockmgr(&usb_sym_lock, LK_RELEASE);
2376 usb_fifo_set_close_zlp(struct usb_fifo *f, uint8_t onoff)
2381 /* send a Zero Length Packet, ZLP, before close */
2382 f->flag_short = onoff;
2386 usb_fifo_set_write_defrag(struct usb_fifo *f, uint8_t onoff)
2391 /* defrag written data */
2392 f->flag_write_defrag = onoff;
2393 /* reset defrag state */
2394 f->flag_have_fragment = 0;
2398 usb_fifo_softc(struct usb_fifo *f)
2400 return (f->priv_sc0);
2402 #endif /* USB_HAVE_UGEN */