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>
51 #include <bus/u4b/usb.h>
52 #include <bus/u4b/usb_ioctl.h>
53 #include <bus/u4b/usbdi.h>
54 #include <bus/u4b/usbdi_util.h>
56 #define USB_DEBUG_VAR usb_fifo_debug
58 #include <bus/u4b/usb_core.h>
59 #include <bus/u4b/usb_dev.h>
60 #include <bus/u4b/usb_mbuf.h>
61 #include <bus/u4b/usb_process.h>
62 #include <bus/u4b/usb_device.h>
63 #include <bus/u4b/usb_debug.h>
64 #include <bus/u4b/usb_busdma.h>
65 #include <bus/u4b/usb_generic.h>
66 #include <bus/u4b/usb_dynamic.h>
67 #include <bus/u4b/usb_util.h>
69 #include <bus/u4b/usb_controller.h>
70 #include <bus/u4b/usb_bus.h>
72 #include <sys/filio.h>
73 #include <sys/ttycom.h>
74 #include <sys/kern_syscall.h>
76 #include <machine/stdarg.h>
81 static int usb_fifo_debug = 0;
83 static SYSCTL_NODE(_hw_usb, OID_AUTO, dev, CTLFLAG_RW, 0, "USB device");
84 SYSCTL_INT(_hw_usb_dev, OID_AUTO, debug, CTLFLAG_RW,
85 &usb_fifo_debug, 0, "Debug Level");
87 TUNABLE_INT("hw.usb.dev.debug", &usb_fifo_debug);
90 #define USB_UCRED struct ucred *ucred,
94 static int usb_fifo_open(struct usb_cdev_privdata *,
95 struct usb_fifo *, int);
96 static void usb_fifo_close(struct usb_fifo *, int);
97 static void usb_dev_init(void *);
98 static void usb_dev_init_post(void *);
99 static void usb_dev_uninit(void *);
100 static int usb_fifo_uiomove(struct usb_fifo *, void *, int,
102 static void usb_fifo_check_methods(struct usb_fifo_methods *);
103 static struct usb_fifo *usb_fifo_alloc(void);
104 static struct usb_endpoint *usb_dev_get_ep(struct usb_device *, uint8_t,
106 static void usb_loc_fill(struct usb_fs_privdata *,
107 struct usb_cdev_privdata *);
108 static usb_error_t usb_ref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *, int);
109 static usb_error_t usb_usb_ref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *);
110 static void usb_unref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *);
112 static void usb_filter_detach(struct knote *kn);
113 static int usb_filter_read(struct knote *kn, long hint);
114 static int usb_filter_write(struct knote *kn, long hint);
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_open_t usb_static_open;
123 static d_close_t usb_static_close;
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 dev_ops usb_ops = {
133 { "usbdev", 0, D_MEM },
135 .d_close = usb_close,
136 .d_ioctl = usb_ioctl,
138 .d_write = usb_write,
139 .d_kqfilter = usb_kqfilter
142 static struct cdev* usb_dev = NULL;
144 /* character device structure used for /bus/u4b */
145 static struct dev_ops usb_static_ops = {
147 .d_open = usb_static_open,
148 .d_close = usb_static_close,
149 .d_ioctl = usb_static_ioctl,
152 static TAILQ_HEAD(, usb_symlink) usb_sym_head;
153 static struct lock usb_sym_lock;
155 struct lock usb_ref_lock;
158 static int usb_nevents = 0;
159 static struct kqinfo usb_kqevent;
162 /*------------------------------------------------------------------------*
165 * This is used to fill out a usb_cdev_privdata structure based on the
166 * device's address as contained in usb_fs_privdata.
167 *------------------------------------------------------------------------*/
169 usb_loc_fill(struct usb_fs_privdata* pd, struct usb_cdev_privdata *cpd)
171 cpd->bus_index = pd->bus_index;
172 cpd->dev_index = pd->dev_index;
173 cpd->ep_addr = pd->ep_addr;
174 cpd->fifo_index = pd->fifo_index;
177 /*------------------------------------------------------------------------*
180 * This function is used to atomically refer an USB device by its
181 * device location. If this function returns success the USB device
182 * will not dissappear until the USB device is unreferenced.
185 * 0: Success, refcount incremented on the given USB device.
187 *------------------------------------------------------------------------*/
189 usb_ref_device(struct usb_cdev_privdata *cpd,
190 struct usb_cdev_refdata *crd, int need_uref)
192 struct usb_fifo **ppf;
195 DPRINTFN(2, "cpd=%p need uref=%d\n", cpd, need_uref);
198 memset(crd, 0, sizeof(*crd));
200 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
201 cpd->bus = devclass_get_softc(usb_devclass_ptr, cpd->bus_index);
202 if (cpd->bus == NULL) {
203 DPRINTFN(2, "no bus at %u\n", cpd->bus_index);
206 cpd->udev = cpd->bus->devices[cpd->dev_index];
207 if (cpd->udev == NULL) {
208 DPRINTFN(2, "no device at %u\n", cpd->dev_index);
211 if (cpd->udev->refcount == USB_DEV_REF_MAX) {
212 DPRINTFN(2, "no dev ref\n");
216 DPRINTFN(2, "ref udev - needed\n");
217 cpd->udev->refcount++;
219 lockmgr(&usb_ref_lock, LK_RELEASE);
222 * We need to grab the sx-lock before grabbing the
223 * FIFO refs to avoid deadlock at detach!
225 usbd_enum_lock(cpd->udev);
227 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
230 * Set "is_uref" after grabbing the default SX lock
235 /* check if we are doing an open */
236 if (cpd->fflags == 0) {
237 /* use zero defaults */
239 /* check for write */
240 if (cpd->fflags & FWRITE) {
241 ppf = cpd->udev->fifo;
242 f = ppf[cpd->fifo_index + USB_FIFO_TX];
244 crd->is_write = 1; /* ref */
245 if (f == NULL || f->refcount == USB_FIFO_REF_MAX)
247 if (f->curr_cpd != cpd)
249 /* check if USB-FS is active */
250 if (f->fs_ep_max != 0) {
256 if (cpd->fflags & FREAD) {
257 ppf = cpd->udev->fifo;
258 f = ppf[cpd->fifo_index + USB_FIFO_RX];
260 crd->is_read = 1; /* ref */
261 if (f == NULL || f->refcount == USB_FIFO_REF_MAX)
263 if (f->curr_cpd != cpd)
265 /* check if USB-FS is active */
266 if (f->fs_ep_max != 0) {
272 /* when everything is OK we increment the refcounts */
274 DPRINTFN(2, "ref write\n");
275 crd->txfifo->refcount++;
278 DPRINTFN(2, "ref read\n");
279 crd->rxfifo->refcount++;
281 lockmgr(&usb_ref_lock, LK_RELEASE);
287 usbd_enum_unlock(cpd->udev);
289 if (--(cpd->udev->refcount) == 0) {
290 cv_signal(&cpd->udev->ref_cv);
293 lockmgr(&usb_ref_lock, LK_RELEASE);
294 DPRINTFN(2, "fail\n");
295 return (USB_ERR_INVAL);
298 /*------------------------------------------------------------------------*
301 * This function is used to upgrade an USB reference to include the
302 * USB device reference on a USB location.
305 * 0: Success, refcount incremented on the given USB device.
307 *------------------------------------------------------------------------*/
309 usb_usb_ref_device(struct usb_cdev_privdata *cpd,
310 struct usb_cdev_refdata *crd)
313 * Check if we already got an USB reference on this location:
316 return (0); /* success */
319 * To avoid deadlock at detach we need to drop the FIFO ref
320 * and re-acquire a new ref!
322 usb_unref_device(cpd, crd);
324 return (usb_ref_device(cpd, crd, 1 /* need uref */));
327 /*------------------------------------------------------------------------*
330 * This function will release the reference count by one unit for the
332 *------------------------------------------------------------------------*/
334 usb_unref_device(struct usb_cdev_privdata *cpd,
335 struct usb_cdev_refdata *crd)
338 DPRINTFN(2, "cpd=%p is_uref=%d\n", cpd, crd->is_uref);
341 usbd_enum_unlock(cpd->udev);
343 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
345 if (--(crd->rxfifo->refcount) == 0) {
346 cv_signal(&crd->rxfifo->cv_drain);
351 if (--(crd->txfifo->refcount) == 0) {
352 cv_signal(&crd->txfifo->cv_drain);
357 if (--(cpd->udev->refcount) == 0) {
358 cv_signal(&cpd->udev->ref_cv);
362 lockmgr(&usb_ref_lock, LK_RELEASE);
365 static struct usb_fifo *
370 f = kmalloc(sizeof(*f), M_USBDEV, M_WAITOK | M_ZERO);
372 cv_init(&f->cv_io, "FIFO-IO");
373 cv_init(&f->cv_drain, "FIFO-DRAIN");
379 /*------------------------------------------------------------------------*
381 *------------------------------------------------------------------------*/
383 usb_fifo_create(struct usb_cdev_privdata *cpd,
384 struct usb_cdev_refdata *crd)
386 struct usb_device *udev = cpd->udev;
388 struct usb_endpoint *ep;
394 int e = cpd->ep_addr;
396 is_tx = (cpd->fflags & FWRITE) ? 1 : 0;
397 is_rx = (cpd->fflags & FREAD) ? 1 : 0;
401 /* Preallocated FIFO */
403 DPRINTFN(5, "Preallocated FIFO\n");
405 f = udev->fifo[cpd->fifo_index + USB_FIFO_TX];
411 f = udev->fifo[cpd->fifo_index + USB_FIFO_RX];
419 KASSERT(e >= 0 && e <= 15, ("endpoint %d out of range", e));
421 /* search for a free FIFO slot */
422 DPRINTFN(5, "Endpoint device, searching for 0x%02x\n", e);
423 for (n = 0;; n += 2) {
425 if (n == USB_FIFO_MAX) {
430 /* end of FIFOs reached */
431 DPRINTFN(5, "out of FIFOs\n");
435 /* Check for TX FIFO */
437 f = udev->fifo[n + USB_FIFO_TX];
439 if (f->dev_ep_index != e) {
440 /* wrong endpoint index */
443 if (f->curr_cpd != NULL) {
448 } else if (no_null) {
452 /* Check for RX FIFO */
454 f = udev->fifo[n + USB_FIFO_RX];
456 if (f->dev_ep_index != e) {
457 /* wrong endpoint index */
460 if (f->curr_cpd != NULL) {
465 } else if (no_null) {
473 if (e >= (USB_EP_MAX / 2)) {
474 /* we don't create any endpoints in this range */
475 DPRINTFN(5, "ep out of range\n");
476 return (is_busy ? EBUSY : EINVAL);
480 if ((e != 0) && is_busy) {
482 * Only the default control endpoint is allowed to be
483 * opened multiple times!
485 DPRINTFN(5, "busy\n");
491 (udev->fifo[n + USB_FIFO_TX] == NULL)) {
492 ep = usb_dev_get_ep(udev, e, USB_FIFO_TX);
493 DPRINTFN(5, "dev_get_endpoint(%d, 0x%x)\n", e, USB_FIFO_TX);
495 DPRINTFN(5, "dev_get_endpoint returned NULL\n");
498 f = usb_fifo_alloc();
500 DPRINTFN(5, "could not alloc tx fifo\n");
503 /* update some fields */
504 f->fifo_index = n + USB_FIFO_TX;
506 f->priv_lock = &udev->device_lock;
508 f->methods = &usb_ugen_methods;
509 f->iface_index = ep->iface_index;
511 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
512 udev->fifo[n + USB_FIFO_TX] = f;
513 lockmgr(&usb_ref_lock, LK_RELEASE);
517 (udev->fifo[n + USB_FIFO_RX] == NULL)) {
519 ep = usb_dev_get_ep(udev, e, USB_FIFO_RX);
520 DPRINTFN(5, "dev_get_endpoint(%d, 0x%x)\n", e, USB_FIFO_RX);
522 DPRINTFN(5, "dev_get_endpoint returned NULL\n");
525 f = usb_fifo_alloc();
527 DPRINTFN(5, "could not alloc rx fifo\n");
530 /* update some fields */
531 f->fifo_index = n + USB_FIFO_RX;
533 f->priv_lock = &udev->device_lock;
535 f->methods = &usb_ugen_methods;
536 f->iface_index = ep->iface_index;
538 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
539 udev->fifo[n + USB_FIFO_RX] = f;
540 lockmgr(&usb_ref_lock, LK_RELEASE);
543 crd->txfifo = udev->fifo[n + USB_FIFO_TX];
546 crd->rxfifo = udev->fifo[n + USB_FIFO_RX];
548 /* fill out fifo index */
549 DPRINTFN(5, "fifo index = %d\n", n);
558 usb_fifo_free(struct usb_fifo *f)
566 /* destroy symlink devices, if any */
567 for (n = 0; n != 2; n++) {
569 usb_free_symlink(f->symlink[n]);
570 f->symlink[n] = NULL;
573 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
575 /* delink ourselves to stop calls from userland */
576 if ((f->fifo_index < USB_FIFO_MAX) &&
578 (f->udev->fifo[f->fifo_index] == f)) {
579 f->udev->fifo[f->fifo_index] = NULL;
581 DPRINTFN(0, "USB FIFO %p has not been linked\n", f);
584 /* decrease refcount */
586 /* prevent any write flush */
588 /* need to wait until all callers have exited */
589 while (f->refcount != 0) {
590 lockmgr(&usb_ref_lock, LK_RELEASE); /* avoid LOR */
591 lockmgr(f->priv_lock, LK_EXCLUSIVE);
592 /* get I/O thread out of any sleep state */
593 if (f->flag_sleeping) {
594 f->flag_sleeping = 0;
595 cv_broadcast(&f->cv_io);
597 lockmgr(f->priv_lock, LK_RELEASE);
598 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
601 cv_wait(&f->cv_drain, &usb_ref_lock);
603 lockmgr(&usb_ref_lock, LK_RELEASE);
605 /* take care of closing the device here, if any */
606 usb_fifo_close(f, 0);
608 cv_destroy(&f->cv_io);
609 cv_destroy(&f->cv_drain);
614 static struct usb_endpoint *
615 usb_dev_get_ep(struct usb_device *udev, uint8_t ep_index, uint8_t dir)
617 struct usb_endpoint *ep;
623 if (dir == USB_FIFO_RX) {
624 if (udev->flags.usb_mode == USB_MODE_HOST) {
630 if (udev->flags.usb_mode == USB_MODE_HOST) {
636 ep = usbd_get_ep_by_addr(udev, ep_index | ep_dir);
640 /* if the endpoint does not exist then return */
643 if (ep->edesc == NULL) {
644 /* invalid endpoint */
647 return (ep); /* success */
650 /*------------------------------------------------------------------------*
656 *------------------------------------------------------------------------*/
658 usb_fifo_open(struct usb_cdev_privdata *cpd,
659 struct usb_fifo *f, int fflags)
665 DPRINTFN(2, "no FIFO\n");
668 /* remove FWRITE and FREAD flags */
669 fflags &= ~(FWRITE | FREAD);
671 /* set correct file flags */
672 if ((f->fifo_index & 1) == USB_FIFO_TX) {
678 /* check if we are already opened */
679 /* we don't need any locks when checking this variable */
680 if (f->curr_cpd != NULL) {
685 /* reset short flag before open */
688 /* call open method */
689 err = (f->methods->f_open) (f, fflags);
693 lockmgr(f->priv_lock, LK_EXCLUSIVE);
695 /* reset sleep flag */
696 f->flag_sleeping = 0;
698 /* reset error flag */
701 /* reset complete flag */
702 f->flag_iscomplete = 0;
704 /* reset select flag */
705 f->flag_isselect = 0;
707 /* reset flushing flag */
708 f->flag_flushing = 0;
710 /* reset ASYNC proc flag */
713 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
714 /* flag the fifo as opened to prevent others */
716 lockmgr(&usb_ref_lock, LK_RELEASE);
721 lockmgr(f->priv_lock, LK_RELEASE);
726 /*------------------------------------------------------------------------*
728 *------------------------------------------------------------------------*/
730 usb_fifo_reset(struct usb_fifo *f)
738 USB_IF_DEQUEUE(&f->used_q, m);
740 USB_IF_ENQUEUE(&f->free_q, m);
745 /* reset have fragment flag */
746 f->flag_have_fragment = 0;
749 /*------------------------------------------------------------------------*
751 *------------------------------------------------------------------------*/
753 usb_fifo_close(struct usb_fifo *f, int fflags)
757 /* check if we are not opened */
758 if (f->curr_cpd == NULL) {
759 /* nothing to do - already closed */
762 lockmgr(f->priv_lock, LK_EXCLUSIVE);
764 /* clear current cdev private data pointer */
767 /* check if we are selected */
768 if (f->flag_isselect) {
770 selwakeup(&f->selinfo);
772 f->flag_isselect = 0;
774 /* check if a thread wants SIGIO */
775 if (f->async_p != NULL && lwkt_trytoken(&f->async_p->p_token)) {
776 ksignal(f->async_p, SIGIO);
777 lwkt_reltoken(&f->async_p->p_token);
780 /* remove FWRITE and FREAD flags */
781 fflags &= ~(FWRITE | FREAD);
783 /* flush written data, if any */
784 if ((f->fifo_index & 1) == USB_FIFO_TX) {
786 if (!f->flag_iserror) {
788 /* set flushing flag */
789 f->flag_flushing = 1;
791 /* get the last packet in */
792 if (f->flag_have_fragment) {
794 f->flag_have_fragment = 0;
795 USB_IF_DEQUEUE(&f->free_q, m);
797 USB_IF_ENQUEUE(&f->used_q, m);
801 /* start write transfer, if not already started */
802 (f->methods->f_start_write) (f);
804 /* check if flushed already */
805 while (f->flag_flushing &&
806 (!f->flag_iserror)) {
807 /* wait until all data has been written */
808 f->flag_sleeping = 1;
809 err = cv_wait_sig(&f->cv_io, f->priv_lock);
811 DPRINTF("signal received\n");
818 /* stop write transfer, if not already stopped */
819 (f->methods->f_stop_write) (f);
823 /* stop write transfer, if not already stopped */
824 (f->methods->f_stop_read) (f);
827 /* check if we are sleeping */
828 if (f->flag_sleeping) {
829 DPRINTFN(2, "Sleeping at close!\n");
831 lockmgr(f->priv_lock, LK_RELEASE);
833 /* call close method */
834 (f->methods->f_close) (f, fflags);
839 /*------------------------------------------------------------------------*
840 * usb_open - cdev callback
841 *------------------------------------------------------------------------*/
843 usb_open(struct dev_open_args *ap)
845 struct cdev *dev = ap->a_head.a_dev;
846 int fflags = ap->a_oflags;
847 struct usb_fs_privdata* pd = (struct usb_fs_privdata*)dev->si_drv1;
848 struct usb_cdev_refdata refs;
849 struct usb_cdev_privdata *cpd;
852 DPRINTFN(2, "%s fflags=0x%08x\n", devtoname(dev), fflags);
854 KASSERT(fflags & (FREAD|FWRITE), ("invalid open flags"));
855 if (((fflags & FREAD) && !(pd->mode & FREAD)) ||
856 ((fflags & FWRITE) && !(pd->mode & FWRITE))) {
857 DPRINTFN(2, "access mode not supported\n");
861 cpd = kmalloc(sizeof(*cpd), M_USBDEV, M_WAITOK | M_ZERO);
862 ep = cpd->ep_addr = pd->ep_addr;
864 usb_loc_fill(pd, cpd);
865 err = usb_ref_device(cpd, &refs, 1);
867 DPRINTFN(2, "cannot ref device\n");
868 kfree(cpd, M_USBDEV);
871 cpd->fflags = fflags; /* access mode for open lifetime */
873 /* create FIFOs, if any */
874 err = usb_fifo_create(cpd, &refs);
875 /* check for error */
877 DPRINTFN(2, "cannot create fifo\n");
878 usb_unref_device(cpd, &refs);
879 kfree(cpd, M_USBDEV);
882 if (fflags & FREAD) {
883 err = usb_fifo_open(cpd, refs.rxfifo, fflags);
885 DPRINTFN(2, "read open failed\n");
886 usb_unref_device(cpd, &refs);
887 kfree(cpd, M_USBDEV);
891 if (fflags & FWRITE) {
892 err = usb_fifo_open(cpd, refs.txfifo, fflags);
894 DPRINTFN(2, "write open failed\n");
895 if (fflags & FREAD) {
896 usb_fifo_close(refs.rxfifo, fflags);
898 usb_unref_device(cpd, &refs);
899 kfree(cpd, M_USBDEV);
903 usb_unref_device(cpd, &refs);
904 #if 0 /* XXX: markusp: which privs? */
905 devfs_set_cdevpriv(cpd, usb_close);
907 /* XXX: This might not work as I expect! */
908 dev->si_drv2 = (void *)cpd;
912 /*------------------------------------------------------------------------*
913 * usb_close - cdev callback
914 *------------------------------------------------------------------------*/
916 usb_close(struct dev_close_args *ap)
918 struct cdev *dev = ap->a_head.a_dev;
919 struct usb_cdev_refdata refs;
920 struct usb_cdev_privdata *cpd = (struct usb_cdev_privdata *)dev->si_drv2;
923 DPRINTFN(2, "cpd=%p\n", cpd);
925 err = usb_ref_device(cpd, &refs, 0);
930 * If this function is not called directly from the root HUB
931 * thread, there is usually a need to lock the enumeration
934 if (!usbd_enum_is_locked(cpd->udev)) {
936 DPRINTFN(2, "Locking enumeration\n");
938 /* reference device */
939 err = usb_usb_ref_device(cpd, &refs);
943 if (cpd->fflags & FREAD) {
944 usb_fifo_close(refs.rxfifo, cpd->fflags);
946 if (cpd->fflags & FWRITE) {
947 usb_fifo_close(refs.txfifo, cpd->fflags);
949 usb_unref_device(cpd, &refs);
951 kfree(cpd, M_USBDEV);
956 usb_dev_init(void *arg)
958 lockinit(&usb_ref_lock, "USB ref mutex", 0, 0);
959 lockinit(&usb_sym_lock, "USB sym mutex", 0, 0);
960 TAILQ_INIT(&usb_sym_head);
962 /* check the UGEN methods */
963 usb_fifo_check_methods(&usb_ugen_methods);
966 /* XXX SI_SUB_KLD? */
967 SYSINIT(usb_dev_init, SI_SUB_PRE_DRIVERS, SI_ORDER_FIRST, usb_dev_init, NULL);
970 usb_dev_init_post(void *arg)
973 * Create /dev/usb - this is needed for usbconfig(8), which
974 * needs a well-known device name to access.
976 usb_dev = make_dev(&usb_static_ops, 0, UID_ROOT, GID_OPERATOR,
977 0644, USB_DEVICE_NAME);
978 if (usb_dev == NULL) {
979 DPRINTFN(0, "Could not create usb bus device\n");
983 SYSINIT(usb_dev_init_post, SI_SUB_DRIVERS, SI_ORDER_FIRST, usb_dev_init_post,
987 usb_dev_uninit(void *arg)
989 if (usb_dev != NULL) {
990 destroy_dev(usb_dev);
993 lockuninit(&usb_ref_lock);
994 lockuninit(&usb_sym_lock);
997 SYSUNINIT(usb_dev_uninit, SI_SUB_KICK_SCHEDULER, SI_ORDER_ANY, usb_dev_uninit, NULL);
1000 usb_ioctl_f_sub(struct usb_fifo *f, u_long cmd, void *addr,
1007 *(int *)addr = 0; /* character device */
1011 /* handled by upper FS layer */
1016 if (f->async_p != NULL) {
1020 f->async_p = USB_TD_GET_PROC(td);
1026 /* XXX this is not the most general solution */
1028 if (f->async_p == NULL) {
1032 if (*(int *)addr != USB_PROC_GET_GID(f->async_p)) {
1040 DPRINTFN(3, "cmd 0x%lx = %d\n", cmd, error);
1044 /*------------------------------------------------------------------------*
1045 * usb_ioctl - cdev callback
1046 *------------------------------------------------------------------------*/
1048 usb_ioctl(struct dev_ioctl_args *ap)
1050 struct cdev *dev = ap->a_head.a_dev;
1051 u_long cmd = ap->a_cmd;
1052 caddr_t addr = ap->a_data;
1053 /* XXX: What is this thread and where is it supposed to come from */
1054 struct thread *td = curthread;
1055 struct usb_cdev_refdata refs;
1056 struct usb_cdev_privdata* cpd;
1061 DPRINTFN(2, "cmd=0x%lx\n", cmd);
1063 #if 0 /* XXX: cdev? */
1064 err = devfs_get_cdevpriv((void **)&cpd);
1070 * XXX: This might not work as I would like it to
1071 * also I need a proper return value if it does
1073 if(dev->si_drv2 == NULL)
1076 cpd = (struct usb_cdev_privdata *)dev->si_drv2;
1079 * Performance optimisation: We try to check for IOCTL's that
1080 * don't need the USB reference first. Then we grab the USB
1081 * reference if we need it!
1083 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1087 fflags = cpd->fflags;
1089 f = NULL; /* set default value */
1090 err = ENOIOCTL; /* set default value */
1092 if (fflags & FWRITE) {
1094 err = usb_ioctl_f_sub(f, cmd, addr, td);
1096 if (fflags & FREAD) {
1098 err = usb_ioctl_f_sub(f, cmd, addr, td);
1100 KASSERT(f != NULL, ("fifo not found"));
1101 if (err != ENOIOCTL)
1104 err = (f->methods->f_ioctl) (f, cmd, addr, fflags);
1106 DPRINTFN(2, "f_ioctl cmd 0x%lx = %d\n", cmd, err);
1108 if (err != ENOIOCTL)
1111 if (usb_usb_ref_device(cpd, &refs)) {
1116 err = (f->methods->f_ioctl_post) (f, cmd, addr, fflags);
1118 DPRINTFN(2, "f_ioctl_post cmd 0x%lx = %d\n", cmd, err);
1120 if (err == ENOIOCTL)
1126 /* Wait for re-enumeration, if any */
1128 while (f->udev->re_enumerate_wait != 0) {
1130 usb_unref_device(cpd, &refs);
1132 usb_pause_mtx(NULL, hz / 128);
1134 if (usb_ref_device(cpd, &refs, 1 /* need uref */)) {
1141 usb_unref_device(cpd, &refs);
1145 static struct filterops usb_filtops_read =
1146 { FILTEROP_ISFD, NULL, usb_filter_detach, usb_filter_read };
1148 static struct filterops usb_filtops_write =
1149 { FILTEROP_ISFD, NULL, usb_filter_detach, usb_filter_write };
1152 usb_kqfilter(struct dev_kqfilter_args *ap)
1154 cdev_t dev = ap->a_head.a_dev;
1155 struct knote *kn = ap->a_kn;
1159 switch(kn->kn_filter) {
1161 kn->kn_fop = &usb_filtops_read;
1162 kn->kn_hook = (caddr_t)dev;
1165 kn->kn_fop = &usb_filtops_write;
1166 kn->kn_hook = (caddr_t)dev;
1169 ap->a_result = EOPNOTSUPP;
1177 usb_filter_detach(struct knote *kn)
1180 struct klist *klist;
1182 klist = &usb_kqevent.ki_note;
1183 knote_remove(klist, kn);
1188 usb_filter_read(struct knote *kn, long hint)
1194 usb_filter_write(struct knote *kn, long hint)
1199 #if 0 /* XXX implement using kqfilter */
1202 usb_poll(struct cdev* dev, int events, struct thread* td)
1204 struct usb_cdev_refdata refs;
1205 struct usb_cdev_privdata* cpd;
1208 int fflags, revents;
1210 if (devfs_get_cdevpriv((void **)&cpd) != 0 ||
1211 usb_ref_device(cpd, &refs, 0) != 0)
1213 (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM));
1215 fflags = cpd->fflags;
1217 /* Figure out who needs service */
1219 if ((events & (POLLOUT | POLLWRNORM)) &&
1220 (fflags & FWRITE)) {
1224 lockmgr(f->priv_lock, LK_EXCLUSIVE);
1226 if (!refs.is_usbfs) {
1227 if (f->flag_iserror) {
1228 /* we got an error */
1231 if (f->queue_data == NULL) {
1233 * start write transfer, if not
1236 (f->methods->f_start_write) (f);
1238 /* check if any packets are available */
1239 USB_IF_POLL(&f->free_q, m);
1242 if (f->flag_iscomplete) {
1250 revents |= events & (POLLOUT | POLLWRNORM);
1252 f->flag_isselect = 1;
1253 selrecord(td, &f->selinfo);
1256 lockmgr(f->priv_lock);
1258 if ((events & (POLLIN | POLLRDNORM)) &&
1263 lockmgr(f->priv_lock, LK_EXCLUSIVE);
1265 if (!refs.is_usbfs) {
1266 if (f->flag_iserror) {
1267 /* we have and error */
1270 if (f->queue_data == NULL) {
1272 * start read transfer, if not
1275 (f->methods->f_start_read) (f);
1277 /* check if any packets are available */
1278 USB_IF_POLL(&f->used_q, m);
1281 if (f->flag_iscomplete) {
1289 revents |= events & (POLLIN | POLLRDNORM);
1291 f->flag_isselect = 1;
1292 selrecord(td, &f->selinfo);
1294 if (!refs.is_usbfs) {
1295 /* start reading data */
1296 (f->methods->f_start_read) (f);
1300 lockmgr(f->priv_lock, LK_RELEASE);
1302 usb_unref_device(cpd, &refs);
1308 usb_read(struct dev_read_args *ap)
1310 struct cdev *dev = ap->a_head.a_dev;
1311 struct uio *uio = ap->a_uio;
1312 int ioflag = ap->a_ioflag;
1313 struct usb_cdev_refdata refs;
1314 struct usb_cdev_privdata* cpd;
1321 uint8_t tr_data = 0;
1324 err = devfs_get_cdevpriv((void **)&cpd);
1329 if(dev->si_drv2 == NULL)
1332 cpd = (struct usb_cdev_privdata *)dev->si_drv2;
1334 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1338 fflags = cpd->fflags;
1342 /* should not happen */
1343 usb_unref_device(cpd, &refs);
1347 resid = uio->uio_resid;
1349 lockmgr(f->priv_lock, LK_EXCLUSIVE);
1351 /* check for permanent read error */
1352 if (f->flag_iserror) {
1356 /* check if USB-FS interface is active */
1357 if (refs.is_usbfs) {
1359 * The queue is used for events that should be
1360 * retrieved using the "USB_FS_COMPLETE" ioctl.
1365 while (uio->uio_resid > 0) {
1367 USB_IF_DEQUEUE(&f->used_q, m);
1371 /* start read transfer, if not already started */
1373 (f->methods->f_start_read) (f);
1375 if (ioflag & IO_NDELAY) {
1377 /* return length before error */
1383 DPRINTF("sleeping\n");
1385 err = usb_fifo_wait(f);
1391 if (f->methods->f_filter_read) {
1393 * Sometimes it is convenient to process data at the
1394 * expense of a userland process instead of a kernel
1397 (f->methods->f_filter_read) (f, m);
1401 io_len = MIN(m->cur_data_len, uio->uio_resid);
1403 DPRINTFN(2, "transfer %d bytes from %p\n",
1404 io_len, m->cur_data_ptr);
1406 err = usb_fifo_uiomove(f,
1407 m->cur_data_ptr, io_len, uio);
1409 m->cur_data_len -= io_len;
1410 m->cur_data_ptr += io_len;
1412 if (m->cur_data_len == 0) {
1414 uint8_t last_packet;
1416 last_packet = m->last_packet;
1418 USB_IF_ENQUEUE(&f->free_q, m);
1425 USB_IF_PREPEND(&f->used_q, m);
1433 lockmgr(f->priv_lock, LK_RELEASE);
1435 usb_unref_device(cpd, &refs);
1441 usb_write(struct dev_write_args *ap)
1443 struct cdev *dev = ap->a_head.a_dev;
1444 struct uio *uio = ap->a_uio;
1445 int ioflag = ap->a_ioflag;
1446 struct usb_cdev_refdata refs;
1447 struct usb_cdev_privdata* cpd;
1455 uint8_t tr_data = 0;
1460 err = devfs_get_cdevpriv((void **)&cpd);
1465 if(dev->si_drv2 == NULL)
1468 cpd = (struct usb_cdev_privdata *)dev->si_drv2;
1470 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1474 fflags = cpd->fflags;
1478 /* should not happen */
1479 usb_unref_device(cpd, &refs);
1482 resid = uio->uio_resid;
1484 lockmgr(f->priv_lock, LK_EXCLUSIVE);
1486 /* check for permanent write error */
1487 if (f->flag_iserror) {
1491 /* check if USB-FS interface is active */
1492 if (refs.is_usbfs) {
1494 * The queue is used for events that should be
1495 * retrieved using the "USB_FS_COMPLETE" ioctl.
1500 if (f->queue_data == NULL) {
1501 /* start write transfer, if not already started */
1502 (f->methods->f_start_write) (f);
1504 /* we allow writing zero length data */
1506 USB_IF_DEQUEUE(&f->free_q, m);
1510 if (ioflag & IO_NDELAY) {
1512 /* return length before error */
1518 DPRINTF("sleeping\n");
1520 err = usb_fifo_wait(f);
1528 if (f->flag_have_fragment == 0) {
1530 io_len = m->cur_data_len;
1531 pdata = m->cur_data_ptr;
1532 if (io_len > uio->uio_resid)
1533 io_len = uio->uio_resid;
1534 m->cur_data_len = io_len;
1536 io_len = m->max_data_len - m->cur_data_len;
1537 pdata = m->cur_data_ptr + m->cur_data_len;
1538 if (io_len > uio->uio_resid)
1539 io_len = uio->uio_resid;
1540 m->cur_data_len += io_len;
1543 DPRINTFN(2, "transfer %d bytes to %p\n",
1546 err = usb_fifo_uiomove(f, pdata, io_len, uio);
1549 f->flag_have_fragment = 0;
1550 USB_IF_ENQUEUE(&f->free_q, m);
1554 /* check if the buffer is ready to be transmitted */
1556 if ((f->flag_write_defrag == 0) ||
1557 (m->cur_data_len == m->max_data_len)) {
1558 f->flag_have_fragment = 0;
1561 * Check for write filter:
1563 * Sometimes it is convenient to process data
1564 * at the expense of a userland process
1565 * instead of a kernel process.
1567 if (f->methods->f_filter_write) {
1568 (f->methods->f_filter_write) (f, m);
1571 /* Put USB mbuf in the used queue */
1572 USB_IF_ENQUEUE(&f->used_q, m);
1574 /* Start writing data, if not already started */
1575 (f->methods->f_start_write) (f);
1577 /* Wait for more data or close */
1578 f->flag_have_fragment = 1;
1579 USB_IF_PREPEND(&f->free_q, m);
1582 } while (uio->uio_resid > 0);
1584 lockmgr(f->priv_lock, LK_RELEASE);
1586 usb_unref_device(cpd, &refs);
1593 usb_static_open(struct dev_open_args *ap)
1599 usb_static_close(struct dev_close_args *ap)
1605 usb_static_ioctl(struct dev_ioctl_args *ap)
1607 u_long cmd = ap->a_cmd;
1608 caddr_t data = ap->a_data;
1609 struct thread *td = curthread; /* XXX: curthread the correct choice? */
1610 int fflag = ap->a_fflag;
1612 struct usb_read_dir *urd;
1620 err = usb_read_symlink(u.urd->urd_data,
1621 u.urd->urd_startentry, u.urd->urd_maxlen);
1623 case USB_DEV_QUIRK_GET:
1624 case USB_QUIRK_NAME_GET:
1625 case USB_DEV_QUIRK_ADD:
1626 case USB_DEV_QUIRK_REMOVE:
1627 err = usb_quirk_ioctl_p(cmd, data, fflag, td);
1629 case USB_GET_TEMPLATE:
1630 *(int *)data = usb_template;
1633 case USB_SET_TEMPLATE:
1634 err = priv_check(curthread, PRIV_DRIVER);
1637 usb_template = *(int *)data;
1647 usb_fifo_uiomove(struct usb_fifo *f, void *cp,
1648 int n, struct uio *uio)
1652 lockmgr(f->priv_lock, LK_RELEASE);
1655 * "uiomove()" can sleep so one needs to make a wrapper,
1656 * exiting the mutex and checking things:
1658 error = uiomove(cp, n, uio);
1660 lockmgr(f->priv_lock, LK_EXCLUSIVE);
1666 usb_fifo_wait(struct usb_fifo *f)
1670 KKASSERT(lockowned(f->priv_lock));
1672 if (f->flag_iserror) {
1676 f->flag_sleeping = 1;
1678 err = cv_wait_sig(&f->cv_io, f->priv_lock);
1680 if (f->flag_iserror) {
1688 usb_fifo_signal(struct usb_fifo *f)
1690 if (f->flag_sleeping) {
1691 f->flag_sleeping = 0;
1692 cv_broadcast(&f->cv_io);
1697 usb_fifo_wakeup(struct usb_fifo *f)
1701 if (f->flag_isselect) {
1703 selwakeup(&f->selinfo);
1705 f->flag_isselect = 0;
1707 if (f->async_p != NULL && lwkt_trytoken(&f->async_p->p_token)) {
1708 ksignal(f->async_p, SIGIO);
1709 lwkt_reltoken(&f->async_p->p_token);
1714 usb_fifo_dummy_open(struct usb_fifo *fifo, int fflags)
1720 usb_fifo_dummy_close(struct usb_fifo *fifo, int fflags)
1726 usb_fifo_dummy_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
1732 usb_fifo_dummy_cmd(struct usb_fifo *fifo)
1734 fifo->flag_flushing = 0; /* not flushing */
1738 usb_fifo_check_methods(struct usb_fifo_methods *pm)
1740 /* check that all callback functions are OK */
1742 if (pm->f_open == NULL)
1743 pm->f_open = &usb_fifo_dummy_open;
1745 if (pm->f_close == NULL)
1746 pm->f_close = &usb_fifo_dummy_close;
1748 if (pm->f_ioctl == NULL)
1749 pm->f_ioctl = &usb_fifo_dummy_ioctl;
1751 if (pm->f_ioctl_post == NULL)
1752 pm->f_ioctl_post = &usb_fifo_dummy_ioctl;
1754 if (pm->f_start_read == NULL)
1755 pm->f_start_read = &usb_fifo_dummy_cmd;
1757 if (pm->f_stop_read == NULL)
1758 pm->f_stop_read = &usb_fifo_dummy_cmd;
1760 if (pm->f_start_write == NULL)
1761 pm->f_start_write = &usb_fifo_dummy_cmd;
1763 if (pm->f_stop_write == NULL)
1764 pm->f_stop_write = &usb_fifo_dummy_cmd;
1767 /*------------------------------------------------------------------------*
1770 * The following function will create a duplex FIFO.
1775 *------------------------------------------------------------------------*/
1777 usb_fifo_attach(struct usb_device *udev, void *priv_sc,
1778 struct lock *priv_lock, struct usb_fifo_methods *pm,
1779 struct usb_fifo_sc *f_sc, uint16_t unit, uint16_t subunit,
1780 uint8_t iface_index, uid_t uid, gid_t gid, int mode)
1782 struct usb_fifo *f_tx;
1783 struct usb_fifo *f_rx;
1787 f_sc->fp[USB_FIFO_TX] = NULL;
1788 f_sc->fp[USB_FIFO_RX] = NULL;
1793 /* check the methods */
1794 usb_fifo_check_methods(pm);
1796 /* search for a free FIFO slot */
1797 for (n = 0;; n += 2) {
1799 if (n == USB_FIFO_MAX) {
1800 /* end of FIFOs reached */
1803 /* Check for TX FIFO */
1804 if (udev->fifo[n + USB_FIFO_TX] != NULL) {
1807 /* Check for RX FIFO */
1808 if (udev->fifo[n + USB_FIFO_RX] != NULL) {
1814 f_tx = usb_fifo_alloc();
1815 f_rx = usb_fifo_alloc();
1817 if ((f_tx == NULL) || (f_rx == NULL)) {
1818 usb_fifo_free(f_tx);
1819 usb_fifo_free(f_rx);
1822 /* initialise FIFO structures */
1824 f_tx->fifo_index = n + USB_FIFO_TX;
1825 f_tx->dev_ep_index = -1;
1826 f_tx->priv_lock = priv_lock;
1827 f_tx->priv_sc0 = priv_sc;
1829 f_tx->iface_index = iface_index;
1832 f_rx->fifo_index = n + USB_FIFO_RX;
1833 f_rx->dev_ep_index = -1;
1834 f_rx->priv_lock = priv_lock;
1835 f_rx->priv_sc0 = priv_sc;
1837 f_rx->iface_index = iface_index;
1840 f_sc->fp[USB_FIFO_TX] = f_tx;
1841 f_sc->fp[USB_FIFO_RX] = f_rx;
1843 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
1844 udev->fifo[f_tx->fifo_index] = f_tx;
1845 udev->fifo[f_rx->fifo_index] = f_rx;
1846 lockmgr(&usb_ref_lock, LK_RELEASE);
1848 for (n = 0; n != 4; n++) {
1850 if (pm->basename[n] == NULL) {
1853 if (subunit == 0xFFFF) {
1854 if (ksnprintf(devname, sizeof(devname),
1855 "%s%u%s", pm->basename[n],
1856 unit, pm->postfix[n] ?
1857 pm->postfix[n] : "")) {
1861 if (ksnprintf(devname, sizeof(devname),
1862 "%s%u.%u%s", pm->basename[n],
1863 unit, subunit, pm->postfix[n] ?
1864 pm->postfix[n] : "")) {
1870 * Distribute the symbolic links into two FIFO structures:
1873 f_rx->symlink[n / 2] =
1874 usb_alloc_symlink(devname);
1876 f_tx->symlink[n / 2] =
1877 usb_alloc_symlink(devname);
1880 /* Create the device */
1881 f_sc->dev = usb_make_dev(udev, devname, -1,
1882 f_tx->fifo_index & f_rx->fifo_index,
1883 FREAD|FWRITE, uid, gid, mode);
1886 DPRINTFN(2, "attached %p/%p\n", f_tx, f_rx);
1890 /*------------------------------------------------------------------------*
1891 * usb_fifo_alloc_buffer
1896 *------------------------------------------------------------------------*/
1898 usb_fifo_alloc_buffer(struct usb_fifo *f, usb_size_t bufsize,
1901 usb_fifo_free_buffer(f);
1903 /* allocate an endpoint */
1904 f->free_q.ifq_maxlen = nbuf;
1905 f->used_q.ifq_maxlen = nbuf;
1907 f->queue_data = usb_alloc_mbufs(
1908 M_USBDEV, &f->free_q, bufsize, nbuf);
1910 if ((f->queue_data == NULL) && bufsize && nbuf) {
1913 return (0); /* success */
1916 /*------------------------------------------------------------------------*
1917 * usb_fifo_free_buffer
1919 * This function will free the buffers associated with a FIFO. This
1920 * function can be called multiple times in a row.
1921 *------------------------------------------------------------------------*/
1923 usb_fifo_free_buffer(struct usb_fifo *f)
1925 if (f->queue_data) {
1926 /* free old buffer */
1927 kfree(f->queue_data, M_USBDEV);
1928 f->queue_data = NULL;
1932 memset(&f->free_q, 0, sizeof(f->free_q));
1933 memset(&f->used_q, 0, sizeof(f->used_q));
1937 usb_fifo_detach(struct usb_fifo_sc *f_sc)
1942 usb_fifo_free(f_sc->fp[USB_FIFO_TX]);
1943 usb_fifo_free(f_sc->fp[USB_FIFO_RX]);
1945 f_sc->fp[USB_FIFO_TX] = NULL;
1946 f_sc->fp[USB_FIFO_RX] = NULL;
1948 usb_destroy_dev(f_sc->dev);
1952 DPRINTFN(2, "detached %p\n", f_sc);
1956 usb_fifo_put_bytes_max(struct usb_fifo *f)
1961 USB_IF_POLL(&f->free_q, m);
1964 len = m->max_data_len;
1971 /*------------------------------------------------------------------------*
1975 * 0 - normal operation
1976 * 1 - set last packet flag to enforce framing
1977 *------------------------------------------------------------------------*/
1979 usb_fifo_put_data(struct usb_fifo *f, struct usb_page_cache *pc,
1980 usb_frlength_t offset, usb_frlength_t len, uint8_t what)
1983 usb_frlength_t io_len;
1985 while (len || (what == 1)) {
1987 USB_IF_DEQUEUE(&f->free_q, m);
1992 io_len = MIN(len, m->cur_data_len);
1994 usbd_copy_out(pc, offset, m->cur_data_ptr, io_len);
1996 m->cur_data_len = io_len;
2000 if ((len == 0) && (what == 1)) {
2003 USB_IF_ENQUEUE(&f->used_q, m);
2007 if ((len == 0) || (what == 1)) {
2017 usb_fifo_put_data_linear(struct usb_fifo *f, void *ptr,
2018 usb_size_t len, uint8_t what)
2023 while (len || (what == 1)) {
2025 USB_IF_DEQUEUE(&f->free_q, m);
2030 io_len = MIN(len, m->cur_data_len);
2032 memcpy(m->cur_data_ptr, ptr, io_len);
2034 m->cur_data_len = io_len;
2035 ptr = USB_ADD_BYTES(ptr, io_len);
2038 if ((len == 0) && (what == 1)) {
2041 USB_IF_ENQUEUE(&f->used_q, m);
2045 if ((len == 0) || (what == 1)) {
2055 usb_fifo_put_data_buffer(struct usb_fifo *f, void *ptr, usb_size_t len)
2059 USB_IF_DEQUEUE(&f->free_q, m);
2062 m->cur_data_len = len;
2063 m->cur_data_ptr = ptr;
2064 USB_IF_ENQUEUE(&f->used_q, m);
2072 usb_fifo_put_data_error(struct usb_fifo *f)
2074 f->flag_iserror = 1;
2078 /*------------------------------------------------------------------------*
2082 * 0 - normal operation
2083 * 1 - only get one "usb_mbuf"
2087 * 1 - data in buffer
2088 *------------------------------------------------------------------------*/
2090 usb_fifo_get_data(struct usb_fifo *f, struct usb_page_cache *pc,
2091 usb_frlength_t offset, usb_frlength_t len, usb_frlength_t *actlen,
2095 usb_frlength_t io_len;
2096 uint8_t tr_data = 0;
2102 USB_IF_DEQUEUE(&f->used_q, m);
2108 io_len = MIN(len, m->cur_data_len);
2110 usbd_copy_in(pc, offset, m->cur_data_ptr, io_len);
2114 actlen[0] += io_len;
2115 m->cur_data_ptr += io_len;
2116 m->cur_data_len -= io_len;
2118 if ((m->cur_data_len == 0) || (what == 1)) {
2119 USB_IF_ENQUEUE(&f->free_q, m);
2127 USB_IF_PREPEND(&f->used_q, m);
2132 /* wait for data to be written out */
2135 if (f->flag_flushing) {
2136 /* check if we should send a short packet */
2137 if (f->flag_short != 0) {
2142 /* flushing complete */
2143 f->flag_flushing = 0;
2156 usb_fifo_get_data_linear(struct usb_fifo *f, void *ptr,
2157 usb_size_t len, usb_size_t *actlen, uint8_t what)
2161 uint8_t tr_data = 0;
2167 USB_IF_DEQUEUE(&f->used_q, m);
2173 io_len = MIN(len, m->cur_data_len);
2175 memcpy(ptr, m->cur_data_ptr, io_len);
2178 ptr = USB_ADD_BYTES(ptr, io_len);
2179 actlen[0] += io_len;
2180 m->cur_data_ptr += io_len;
2181 m->cur_data_len -= io_len;
2183 if ((m->cur_data_len == 0) || (what == 1)) {
2184 USB_IF_ENQUEUE(&f->free_q, m);
2192 USB_IF_PREPEND(&f->used_q, m);
2197 /* wait for data to be written out */
2200 if (f->flag_flushing) {
2201 /* check if we should send a short packet */
2202 if (f->flag_short != 0) {
2207 /* flushing complete */
2208 f->flag_flushing = 0;
2221 usb_fifo_get_data_buffer(struct usb_fifo *f, void **pptr, usb_size_t *plen)
2225 USB_IF_POLL(&f->used_q, m);
2228 *plen = m->cur_data_len;
2229 *pptr = m->cur_data_ptr;
2237 usb_fifo_get_data_error(struct usb_fifo *f)
2239 f->flag_iserror = 1;
2243 /*------------------------------------------------------------------------*
2248 * Else: Pointer to symlink entry
2249 *------------------------------------------------------------------------*/
2250 struct usb_symlink *
2251 usb_alloc_symlink(const char *target)
2253 struct usb_symlink *ps;
2255 ps = kmalloc(sizeof(*ps), M_USBDEV, M_WAITOK);
2256 /* XXX no longer needed */
2257 strlcpy(ps->src_path, target, sizeof(ps->src_path));
2258 ps->src_len = strlen(ps->src_path);
2259 strlcpy(ps->dst_path, target, sizeof(ps->dst_path));
2260 ps->dst_len = strlen(ps->dst_path);
2262 lockmgr(&usb_sym_lock, LK_EXCLUSIVE);
2263 TAILQ_INSERT_TAIL(&usb_sym_head, ps, sym_entry);
2264 lockmgr(&usb_sym_lock, LK_RELEASE);
2268 /*------------------------------------------------------------------------*
2270 *------------------------------------------------------------------------*/
2272 usb_free_symlink(struct usb_symlink *ps)
2277 lockmgr(&usb_sym_lock, LK_EXCLUSIVE);
2278 TAILQ_REMOVE(&usb_sym_head, ps, sym_entry);
2279 lockmgr(&usb_sym_lock, LK_RELEASE);
2281 kfree(ps, M_USBDEV);
2284 /*------------------------------------------------------------------------*
2290 *------------------------------------------------------------------------*/
2292 usb_read_symlink(uint8_t *user_ptr, uint32_t startentry, uint32_t user_len)
2294 struct usb_symlink *ps;
2300 lockmgr(&usb_sym_lock, LK_EXCLUSIVE);
2302 TAILQ_FOREACH(ps, &usb_sym_head, sym_entry) {
2305 * Compute total length of source and destination symlink
2306 * strings pluss one length byte and two NUL bytes:
2308 temp = ps->src_len + ps->dst_len + 3;
2312 * Skip entry because this length cannot fit
2317 if (startentry != 0) {
2318 /* decrement read offset */
2322 if (temp > user_len) {
2323 /* out of buffer space */
2328 /* copy out total length */
2330 error = copyout(&len,
2331 USB_ADD_BYTES(user_ptr, delta), 1);
2337 /* copy out source string */
2339 error = copyout(ps->src_path,
2340 USB_ADD_BYTES(user_ptr, delta), ps->src_len);
2345 delta += ps->src_len;
2346 error = copyout(&len,
2347 USB_ADD_BYTES(user_ptr, delta), 1);
2353 /* copy out destination string */
2355 error = copyout(ps->dst_path,
2356 USB_ADD_BYTES(user_ptr, delta), ps->dst_len);
2361 delta += ps->dst_len;
2362 error = copyout(&len,
2363 USB_ADD_BYTES(user_ptr, delta), 1);
2372 /* a zero length entry indicates the end */
2374 if ((user_len != 0) && (error == 0)) {
2378 error = copyout(&len,
2379 USB_ADD_BYTES(user_ptr, delta), 1);
2381 lockmgr(&usb_sym_lock, LK_RELEASE);
2386 usb_fifo_set_close_zlp(struct usb_fifo *f, uint8_t onoff)
2391 /* send a Zero Length Packet, ZLP, before close */
2392 f->flag_short = onoff;
2396 usb_fifo_set_write_defrag(struct usb_fifo *f, uint8_t onoff)
2401 /* defrag written data */
2402 f->flag_write_defrag = onoff;
2403 /* reset defrag state */
2404 f->flag_have_fragment = 0;
2408 usb_fifo_softc(struct usb_fifo *f)
2410 return (f->priv_sc0);
2412 #endif /* USB_HAVE_UGEN */