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/usb/usb.h>
52 #include <bus/usb/usb_ioctl.h>
53 #include <bus/usb/usbdi.h>
54 #include <bus/usb/usbdi_util.h>
56 #define USB_DEBUG_VAR usb_fifo_debug
58 #include <bus/usb/usb_core.h>
59 #include <bus/usb/usb_dev.h>
60 #include <bus/usb/usb_mbuf.h>
61 #include <bus/usb/usb_process.h>
62 #include <bus/usb/usb_device.h>
63 #include <bus/usb/usb_debug.h>
64 #include <bus/usb/usb_busdma.h>
65 #include <bus/usb/usb_generic.h>
66 #include <bus/usb/usb_dynamic.h>
67 #include <bus/usb/usb_util.h>
69 #include <bus/usb/usb_controller.h>
70 #include <bus/usb/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);
115 static d_open_t usb_open;
116 static d_close_t usb_close;
117 static d_ioctl_t usb_ioctl;
118 static d_read_t usb_read;
119 static d_write_t usb_write;
120 static d_kqfilter_t usb_kqfilter;
121 static d_open_t usb_static_open;
122 static d_close_t usb_static_close;
123 static d_ioctl_t usb_static_ioctl;
125 static usb_fifo_open_t usb_fifo_dummy_open;
126 static usb_fifo_close_t usb_fifo_dummy_close;
127 static usb_fifo_ioctl_t usb_fifo_dummy_ioctl;
128 static usb_fifo_cmd_t usb_fifo_dummy_cmd;
130 /* character device structure used for devices (/dev/ugenX.Y and /dev/uXXX) */
131 struct dev_ops usb_devsw = {
132 /* .d_version = D_VERSION, */
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/usb */
145 static struct dev_ops usb_static_devsw = {
147 /* .d_version = D_VERSION, */
148 .d_open = usb_static_open,
149 .d_close = usb_static_close,
150 .d_ioctl = usb_static_ioctl,
153 static TAILQ_HEAD(, usb_symlink) usb_sym_head;
154 static struct lock usb_sym_lock;
156 struct lock usb_ref_lock;
158 static int usb_nevents = 0;
159 static struct kqinfo usb_kqevent;
161 /*------------------------------------------------------------------------*
164 * This is used to fill out a usb_cdev_privdata structure based on the
165 * device's address as contained in usb_fs_privdata.
166 *------------------------------------------------------------------------*/
168 usb_loc_fill(struct usb_fs_privdata* pd, struct usb_cdev_privdata *cpd)
170 cpd->bus_index = pd->bus_index;
171 cpd->dev_index = pd->dev_index;
172 cpd->ep_addr = pd->ep_addr;
173 cpd->fifo_index = pd->fifo_index;
176 /*------------------------------------------------------------------------*
179 * This function is used to atomically refer an USB device by its
180 * device location. If this function returns success the USB device
181 * will not dissappear until the USB device is unreferenced.
184 * 0: Success, refcount incremented on the given USB device.
186 *------------------------------------------------------------------------*/
188 usb_ref_device(struct usb_cdev_privdata *cpd,
189 struct usb_cdev_refdata *crd, int need_uref)
191 struct usb_fifo **ppf;
194 DPRINTFN(2, "cpd=%p need uref=%d\n", cpd, need_uref);
197 memset(crd, 0, sizeof(*crd));
199 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
200 cpd->bus = devclass_get_softc(usb_devclass_ptr, cpd->bus_index);
201 if (cpd->bus == NULL) {
202 DPRINTFN(2, "no bus at %u\n", cpd->bus_index);
205 cpd->udev = cpd->bus->devices[cpd->dev_index];
206 if (cpd->udev == NULL) {
207 DPRINTFN(2, "no device at %u\n", cpd->dev_index);
210 if (cpd->udev->refcount == USB_DEV_REF_MAX) {
211 DPRINTFN(2, "no dev ref\n");
215 DPRINTFN(2, "ref udev - needed\n");
216 cpd->udev->refcount++;
218 lockmgr(&usb_ref_lock, LK_RELEASE);
221 * We need to grab the sx-lock before grabbing the
222 * FIFO refs to avoid deadlock at detach!
224 usbd_enum_lock(cpd->udev);
226 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
229 * Set "is_uref" after grabbing the default SX lock
234 /* check if we are doing an open */
235 if (cpd->fflags == 0) {
236 /* use zero defaults */
238 /* check for write */
239 if (cpd->fflags & FWRITE) {
240 ppf = cpd->udev->fifo;
241 f = ppf[cpd->fifo_index + USB_FIFO_TX];
243 crd->is_write = 1; /* ref */
244 if (f == NULL || f->refcount == USB_FIFO_REF_MAX)
246 if (f->curr_cpd != cpd)
248 /* check if USB-FS is active */
249 if (f->fs_ep_max != 0) {
255 if (cpd->fflags & FREAD) {
256 ppf = cpd->udev->fifo;
257 f = ppf[cpd->fifo_index + USB_FIFO_RX];
259 crd->is_read = 1; /* ref */
260 if (f == NULL || f->refcount == USB_FIFO_REF_MAX)
262 if (f->curr_cpd != cpd)
264 /* check if USB-FS is active */
265 if (f->fs_ep_max != 0) {
271 /* when everything is OK we increment the refcounts */
273 DPRINTFN(2, "ref write\n");
274 crd->txfifo->refcount++;
277 DPRINTFN(2, "ref read\n");
278 crd->rxfifo->refcount++;
280 lockmgr(&usb_ref_lock, LK_RELEASE);
286 usbd_enum_unlock(cpd->udev);
288 if (--(cpd->udev->refcount) == 0) {
289 cv_signal(&cpd->udev->ref_cv);
292 lockmgr(&usb_ref_lock, LK_RELEASE);
293 DPRINTFN(2, "fail\n");
294 return (USB_ERR_INVAL);
297 /*------------------------------------------------------------------------*
300 * This function is used to upgrade an USB reference to include the
301 * USB device reference on a USB location.
304 * 0: Success, refcount incremented on the given USB device.
306 *------------------------------------------------------------------------*/
308 usb_usb_ref_device(struct usb_cdev_privdata *cpd,
309 struct usb_cdev_refdata *crd)
312 * Check if we already got an USB reference on this location:
315 return (0); /* success */
318 * To avoid deadlock at detach we need to drop the FIFO ref
319 * and re-acquire a new ref!
321 usb_unref_device(cpd, crd);
323 return (usb_ref_device(cpd, crd, 1 /* need uref */));
326 /*------------------------------------------------------------------------*
329 * This function will release the reference count by one unit for the
331 *------------------------------------------------------------------------*/
333 usb_unref_device(struct usb_cdev_privdata *cpd,
334 struct usb_cdev_refdata *crd)
337 DPRINTFN(2, "cpd=%p is_uref=%d\n", cpd, crd->is_uref);
340 usbd_enum_unlock(cpd->udev);
342 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
344 if (--(crd->rxfifo->refcount) == 0) {
345 cv_signal(&crd->rxfifo->cv_drain);
350 if (--(crd->txfifo->refcount) == 0) {
351 cv_signal(&crd->txfifo->cv_drain);
356 if (--(cpd->udev->refcount) == 0) {
357 cv_signal(&cpd->udev->ref_cv);
361 lockmgr(&usb_ref_lock, LK_RELEASE);
364 static struct usb_fifo *
369 f = kmalloc(sizeof(*f), M_USBDEV, M_WAITOK | M_ZERO);
371 cv_init(&f->cv_io, "FIFO-IO");
372 cv_init(&f->cv_drain, "FIFO-DRAIN");
378 /*------------------------------------------------------------------------*
380 *------------------------------------------------------------------------*/
382 usb_fifo_create(struct usb_cdev_privdata *cpd,
383 struct usb_cdev_refdata *crd)
385 struct usb_device *udev = cpd->udev;
387 struct usb_endpoint *ep;
393 int e = cpd->ep_addr;
395 is_tx = (cpd->fflags & FWRITE) ? 1 : 0;
396 is_rx = (cpd->fflags & FREAD) ? 1 : 0;
400 /* Preallocated FIFO */
402 DPRINTFN(5, "Preallocated FIFO\n");
404 f = udev->fifo[cpd->fifo_index + USB_FIFO_TX];
410 f = udev->fifo[cpd->fifo_index + USB_FIFO_RX];
418 KASSERT(e >= 0 && e <= 15, ("endpoint %d out of range", e));
420 /* search for a free FIFO slot */
421 DPRINTFN(5, "Endpoint device, searching for 0x%02x\n", e);
422 for (n = 0;; n += 2) {
424 if (n == USB_FIFO_MAX) {
429 /* end of FIFOs reached */
430 DPRINTFN(5, "out of FIFOs\n");
434 /* Check for TX FIFO */
436 f = udev->fifo[n + USB_FIFO_TX];
438 if (f->dev_ep_index != e) {
439 /* wrong endpoint index */
442 if (f->curr_cpd != NULL) {
447 } else if (no_null) {
451 /* Check for RX FIFO */
453 f = udev->fifo[n + USB_FIFO_RX];
455 if (f->dev_ep_index != e) {
456 /* wrong endpoint index */
459 if (f->curr_cpd != NULL) {
464 } else if (no_null) {
472 if (e >= (USB_EP_MAX / 2)) {
473 /* we don't create any endpoints in this range */
474 DPRINTFN(5, "ep out of range\n");
475 return (is_busy ? EBUSY : EINVAL);
479 if ((e != 0) && is_busy) {
481 * Only the default control endpoint is allowed to be
482 * opened multiple times!
484 DPRINTFN(5, "busy\n");
490 (udev->fifo[n + USB_FIFO_TX] == NULL)) {
491 ep = usb_dev_get_ep(udev, e, USB_FIFO_TX);
492 DPRINTFN(5, "dev_get_endpoint(%d, 0x%x)\n", e, USB_FIFO_TX);
494 DPRINTFN(5, "dev_get_endpoint returned NULL\n");
497 f = usb_fifo_alloc();
499 DPRINTFN(5, "could not alloc tx fifo\n");
502 /* update some fields */
503 f->fifo_index = n + USB_FIFO_TX;
505 f->priv_lock = &udev->device_lock;
507 f->methods = &usb_ugen_methods;
508 f->iface_index = ep->iface_index;
510 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
511 udev->fifo[n + USB_FIFO_TX] = f;
512 lockmgr(&usb_ref_lock, LK_RELEASE);
516 (udev->fifo[n + USB_FIFO_RX] == NULL)) {
518 ep = usb_dev_get_ep(udev, e, USB_FIFO_RX);
519 DPRINTFN(5, "dev_get_endpoint(%d, 0x%x)\n", e, USB_FIFO_RX);
521 DPRINTFN(5, "dev_get_endpoint returned NULL\n");
524 f = usb_fifo_alloc();
526 DPRINTFN(5, "could not alloc rx fifo\n");
529 /* update some fields */
530 f->fifo_index = n + USB_FIFO_RX;
532 f->priv_lock = &udev->device_lock;
534 f->methods = &usb_ugen_methods;
535 f->iface_index = ep->iface_index;
537 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
538 udev->fifo[n + USB_FIFO_RX] = f;
539 lockmgr(&usb_ref_lock, LK_RELEASE);
542 crd->txfifo = udev->fifo[n + USB_FIFO_TX];
545 crd->rxfifo = udev->fifo[n + USB_FIFO_RX];
547 /* fill out fifo index */
548 DPRINTFN(5, "fifo index = %d\n", n);
557 usb_fifo_free(struct usb_fifo *f)
565 /* destroy symlink devices, if any */
566 for (n = 0; n != 2; n++) {
568 usb_free_symlink(f->symlink[n]);
569 f->symlink[n] = NULL;
572 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
574 /* delink ourselves to stop calls from userland */
575 if ((f->fifo_index < USB_FIFO_MAX) &&
577 (f->udev->fifo[f->fifo_index] == f)) {
578 f->udev->fifo[f->fifo_index] = NULL;
580 DPRINTFN(0, "USB FIFO %p has not been linked\n", f);
583 /* decrease refcount */
585 /* prevent any write flush */
587 /* need to wait until all callers have exited */
588 while (f->refcount != 0) {
589 lockmgr(&usb_ref_lock, LK_RELEASE); /* avoid LOR */
590 lockmgr(f->priv_lock, LK_EXCLUSIVE);
591 /* get I/O thread out of any sleep state */
592 if (f->flag_sleeping) {
593 f->flag_sleeping = 0;
594 cv_broadcast(&f->cv_io);
596 lockmgr(f->priv_lock, LK_RELEASE);
597 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
600 cv_wait(&f->cv_drain, &usb_ref_lock);
602 lockmgr(&usb_ref_lock, LK_RELEASE);
604 /* take care of closing the device here, if any */
605 usb_fifo_close(f, 0);
607 cv_destroy(&f->cv_io);
608 cv_destroy(&f->cv_drain);
613 static struct usb_endpoint *
614 usb_dev_get_ep(struct usb_device *udev, uint8_t ep_index, uint8_t dir)
616 struct usb_endpoint *ep;
622 if (dir == USB_FIFO_RX) {
623 if (udev->flags.usb_mode == USB_MODE_HOST) {
629 if (udev->flags.usb_mode == USB_MODE_HOST) {
635 ep = usbd_get_ep_by_addr(udev, ep_index | ep_dir);
639 /* if the endpoint does not exist then return */
642 if (ep->edesc == NULL) {
643 /* invalid endpoint */
646 return (ep); /* success */
649 /*------------------------------------------------------------------------*
655 *------------------------------------------------------------------------*/
657 usb_fifo_open(struct usb_cdev_privdata *cpd,
658 struct usb_fifo *f, int fflags)
664 DPRINTFN(2, "no FIFO\n");
667 /* remove FWRITE and FREAD flags */
668 fflags &= ~(FWRITE | FREAD);
670 /* set correct file flags */
671 if ((f->fifo_index & 1) == USB_FIFO_TX) {
677 /* check if we are already opened */
678 /* we don't need any locks when checking this variable */
679 if (f->curr_cpd != NULL) {
684 /* reset short flag before open */
687 /* call open method */
688 err = (f->methods->f_open) (f, fflags);
692 lockmgr(f->priv_lock, LK_EXCLUSIVE);
694 /* reset sleep flag */
695 f->flag_sleeping = 0;
697 /* reset error flag */
700 /* reset complete flag */
701 f->flag_iscomplete = 0;
703 /* reset select flag */
704 f->flag_isselect = 0;
706 /* reset flushing flag */
707 f->flag_flushing = 0;
709 /* reset ASYNC proc flag */
712 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
713 /* flag the fifo as opened to prevent others */
715 lockmgr(&usb_ref_lock, LK_RELEASE);
720 lockmgr(f->priv_lock, LK_RELEASE);
725 /*------------------------------------------------------------------------*
727 *------------------------------------------------------------------------*/
729 usb_fifo_reset(struct usb_fifo *f)
737 USB_IF_DEQUEUE(&f->used_q, m);
739 USB_IF_ENQUEUE(&f->free_q, m);
744 /* reset have fragment flag */
745 f->flag_have_fragment = 0;
748 /*------------------------------------------------------------------------*
750 *------------------------------------------------------------------------*/
752 usb_fifo_close(struct usb_fifo *f, int fflags)
756 /* check if we are not opened */
757 if (f->curr_cpd == NULL) {
758 /* nothing to do - already closed */
761 lockmgr(f->priv_lock, LK_EXCLUSIVE);
763 /* clear current cdev private data pointer */
766 /* check if we are selected */
767 if (f->flag_isselect) {
769 selwakeup(&f->selinfo);
771 f->flag_isselect = 0;
773 /* check if a thread wants SIGIO */
774 if (f->async_p != NULL && lwkt_trytoken(&f->async_p->p_token)) {
775 ksignal(f->async_p, SIGIO);
776 lwkt_reltoken(&f->async_p->p_token);
779 /* remove FWRITE and FREAD flags */
780 fflags &= ~(FWRITE | FREAD);
782 /* flush written data, if any */
783 if ((f->fifo_index & 1) == USB_FIFO_TX) {
785 if (!f->flag_iserror) {
787 /* set flushing flag */
788 f->flag_flushing = 1;
790 /* get the last packet in */
791 if (f->flag_have_fragment) {
793 f->flag_have_fragment = 0;
794 USB_IF_DEQUEUE(&f->free_q, m);
796 USB_IF_ENQUEUE(&f->used_q, m);
800 /* start write transfer, if not already started */
801 (f->methods->f_start_write) (f);
803 /* check if flushed already */
804 while (f->flag_flushing &&
805 (!f->flag_iserror)) {
806 /* wait until all data has been written */
807 f->flag_sleeping = 1;
808 err = cv_wait_sig(&f->cv_io, f->priv_lock);
810 DPRINTF("signal received\n");
817 /* stop write transfer, if not already stopped */
818 (f->methods->f_stop_write) (f);
822 /* stop write transfer, if not already stopped */
823 (f->methods->f_stop_read) (f);
826 /* check if we are sleeping */
827 if (f->flag_sleeping) {
828 DPRINTFN(2, "Sleeping at close!\n");
830 lockmgr(f->priv_lock, LK_RELEASE);
832 /* call close method */
833 (f->methods->f_close) (f, fflags);
838 /*------------------------------------------------------------------------*
839 * usb_open - cdev callback
840 *------------------------------------------------------------------------*/
842 usb_open(struct dev_open_args *ap)
843 /*struct cdev *dev, int fflags, int devtype, struct thread *td)
846 struct cdev *dev = ap->a_head.a_dev;
847 int fflags = ap->a_oflags;
848 struct usb_fs_privdata* pd = (struct usb_fs_privdata*)dev->si_drv1;
849 struct usb_cdev_refdata refs;
850 struct usb_cdev_privdata *cpd;
853 DPRINTFN(2, "%s fflags=0x%08x\n", devtoname(dev), fflags);
855 KASSERT(fflags & (FREAD|FWRITE), ("invalid open flags"));
856 if (((fflags & FREAD) && !(pd->mode & FREAD)) ||
857 ((fflags & FWRITE) && !(pd->mode & FWRITE))) {
858 DPRINTFN(2, "access mode not supported\n");
862 cpd = kmalloc(sizeof(*cpd), M_USBDEV, M_WAITOK | M_ZERO);
863 ep = cpd->ep_addr = pd->ep_addr;
865 usb_loc_fill(pd, cpd);
866 err = usb_ref_device(cpd, &refs, 1);
868 DPRINTFN(2, "cannot ref device\n");
869 kfree(cpd, M_USBDEV);
872 cpd->fflags = fflags; /* access mode for open lifetime */
874 /* create FIFOs, if any */
875 err = usb_fifo_create(cpd, &refs);
876 /* check for error */
878 DPRINTFN(2, "cannot create fifo\n");
879 usb_unref_device(cpd, &refs);
880 kfree(cpd, M_USBDEV);
883 if (fflags & FREAD) {
884 err = usb_fifo_open(cpd, refs.rxfifo, fflags);
886 DPRINTFN(2, "read open failed\n");
887 usb_unref_device(cpd, &refs);
888 kfree(cpd, M_USBDEV);
892 if (fflags & FWRITE) {
893 err = usb_fifo_open(cpd, refs.txfifo, fflags);
895 DPRINTFN(2, "write open failed\n");
896 if (fflags & FREAD) {
897 usb_fifo_close(refs.rxfifo, fflags);
899 usb_unref_device(cpd, &refs);
900 kfree(cpd, M_USBDEV);
904 usb_unref_device(cpd, &refs);
905 /* XXX: markusp: which privs?
906 devfs_set_cdevpriv(cpd, usb_close);
908 /* XXX: This might not work as I expect! */
909 dev->si_drv2 = (void *)cpd;
913 /*------------------------------------------------------------------------*
914 * usb_close - cdev callback
915 *------------------------------------------------------------------------*/
917 usb_close(struct dev_close_args *ap)
919 struct cdev *dev = ap->a_head.a_dev;
920 struct usb_cdev_refdata refs;
921 struct usb_cdev_privdata *cpd = (struct usb_cdev_privdata *)dev->si_drv2;
924 DPRINTFN(2, "cpd=%p\n", cpd);
926 err = usb_ref_device(cpd, &refs, 0);
931 * If this function is not called directly from the root HUB
932 * thread, there is usually a need to lock the enumeration
935 if (!usbd_enum_is_locked(cpd->udev)) {
937 DPRINTFN(2, "Locking enumeration\n");
939 /* reference device */
940 err = usb_usb_ref_device(cpd, &refs);
944 if (cpd->fflags & FREAD) {
945 usb_fifo_close(refs.rxfifo, cpd->fflags);
947 if (cpd->fflags & FWRITE) {
948 usb_fifo_close(refs.txfifo, cpd->fflags);
950 usb_unref_device(cpd, &refs);
952 kfree(cpd, M_USBDEV);
958 usb_dev_init(void *arg)
960 lockinit(&usb_ref_lock, "USB ref mutex", 0, 0);
961 lockinit(&usb_sym_lock, "USB sym mutex", 0, 0);
962 TAILQ_INIT(&usb_sym_head);
964 /* check the UGEN methods */
965 usb_fifo_check_methods(&usb_ugen_methods);
968 /* XXX SI_SUB_KLD? */
969 SYSINIT(usb_dev_init, SI_SUB_PRE_DRIVERS, SI_ORDER_FIRST, usb_dev_init, NULL);
972 usb_dev_init_post(void *arg)
975 * Create /bus/usb - this is needed for usbconfig(8), which
976 * needs a well-known device name to access.
978 usb_dev = make_dev(&usb_static_devsw, 0, UID_ROOT, GID_OPERATOR,
979 0644, USB_DEVICE_NAME);
980 if (usb_dev == NULL) {
981 DPRINTFN(0, "Could not create usb bus device\n");
985 SYSINIT(usb_dev_init_post, SI_SUB_DRIVERS, SI_ORDER_FIRST, usb_dev_init_post, NULL);
988 usb_dev_uninit(void *arg)
990 if (usb_dev != NULL) {
991 destroy_dev(usb_dev);
994 lockuninit(&usb_ref_lock);
995 lockuninit(&usb_sym_lock);
998 SYSUNINIT(usb_dev_uninit, SI_SUB_KICK_SCHEDULER, SI_ORDER_ANY, usb_dev_uninit, NULL);
1001 usb_ioctl_f_sub(struct usb_fifo *f, u_long cmd, void *addr, struct thread *td)
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)
1049 /*usb_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int fflag, struct thread* td)
1052 struct cdev *dev = ap->a_head.a_dev;
1053 u_long cmd = ap->a_cmd;
1054 caddr_t addr = ap->a_data;
1055 /* XXX: What is this thread and where is it supposed to come from */
1056 struct thread *td = curthread;
1057 struct usb_cdev_refdata refs;
1058 struct usb_cdev_privdata* cpd;
1063 DPRINTFN(2, "cmd=0x%lx\n", cmd);
1066 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 */
1072 if(dev->si_drv2 == NULL)
1075 cpd = (struct usb_cdev_privdata *)dev->si_drv2;
1078 * Performance optimisation: We try to check for IOCTL's that
1079 * don't need the USB reference first. Then we grab the USB
1080 * reference if we need it!
1082 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1086 fflags = cpd->fflags;
1088 f = NULL; /* set default value */
1089 err = ENOIOCTL; /* set default value */
1091 if (fflags & FWRITE) {
1093 err = usb_ioctl_f_sub(f, cmd, addr, td);
1095 if (fflags & FREAD) {
1097 err = usb_ioctl_f_sub(f, cmd, addr, td);
1099 KASSERT(f != NULL, ("fifo not found"));
1100 if (err != ENOIOCTL)
1103 err = (f->methods->f_ioctl) (f, cmd, addr, fflags);
1105 DPRINTFN(2, "f_ioctl cmd 0x%lx = %d\n", cmd, err);
1107 if (err != ENOIOCTL)
1110 if (usb_usb_ref_device(cpd, &refs)) {
1115 err = (f->methods->f_ioctl_post) (f, cmd, addr, fflags);
1117 DPRINTFN(2, "f_ioctl_post cmd 0x%lx = %d\n", cmd, err);
1119 if (err == ENOIOCTL)
1125 /* Wait for re-enumeration, if any */
1127 while (f->udev->re_enumerate_wait != 0) {
1129 usb_unref_device(cpd, &refs);
1131 usb_pause_mtx(NULL, hz / 128);
1133 if (usb_ref_device(cpd, &refs, 1 /* need uref */)) {
1140 usb_unref_device(cpd, &refs);
1144 static struct filterops usb_filtops_read =
1145 { FILTEROP_ISFD, NULL, usb_filter_detach, usb_filter_read };
1147 static struct filterops usb_filtops_write =
1148 { FILTEROP_ISFD, NULL, usb_filter_detach, usb_filter_write };
1151 usb_kqfilter(struct dev_kqfilter_args *ap)
1153 cdev_t dev = ap->a_head.a_dev;
1154 struct knote *kn = ap->a_kn;
1158 switch(kn->kn_filter) {
1160 kn->kn_fop = &usb_filtops_read;
1161 kn->kn_hook = (caddr_t)dev;
1164 kn->kn_fop = &usb_filtops_write;
1165 kn->kn_hook = (caddr_t)dev;
1168 ap->a_result = EOPNOTSUPP;
1172 klist = &usb_kqevent.ki_note;
1173 knote_insert(klist, kn);
1179 usb_filter_detach(struct knote *kn)
1181 struct klist *klist;
1183 klist = &usb_kqevent.ki_note;
1184 knote_remove(klist, kn);
1188 usb_filter_read(struct knote *kn, long hint)
1190 /* struct usb_softc *sc;
1192 sc = (struc usb_softc *)kn->kn_hook;
1202 usb_filter_write(struct knote *kn, long hint)
1209 /* XXX implement using kqfilter */
1213 usb_poll(struct cdev* dev, int events, struct thread* td)
1216 struct usb_cdev_refdata refs;
1217 struct usb_cdev_privdata* cpd;
1220 int fflags, revents;
1222 if (devfs_get_cdevpriv((void **)&cpd) != 0 ||
1223 usb_ref_device(cpd, &refs, 0) != 0)
1225 (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM));
1227 fflags = cpd->fflags;
1229 /* Figure out who needs service */
1231 if ((events & (POLLOUT | POLLWRNORM)) &&
1232 (fflags & FWRITE)) {
1236 lockmgr(f->priv_lock, LK_EXCLUSIVE);
1238 if (!refs.is_usbfs) {
1239 if (f->flag_iserror) {
1240 /* we got an error */
1243 if (f->queue_data == NULL) {
1245 * start write transfer, if not
1248 (f->methods->f_start_write) (f);
1250 /* check if any packets are available */
1251 USB_IF_POLL(&f->free_q, m);
1254 if (f->flag_iscomplete) {
1262 revents |= events & (POLLOUT | POLLWRNORM);
1264 f->flag_isselect = 1;
1265 selrecord(td, &f->selinfo);
1268 lockmgr(f->priv_lock);
1270 if ((events & (POLLIN | POLLRDNORM)) &&
1275 lockmgr(f->priv_lock, LK_EXCLUSIVE);
1277 if (!refs.is_usbfs) {
1278 if (f->flag_iserror) {
1279 /* we have and error */
1282 if (f->queue_data == NULL) {
1284 * start read transfer, if not
1287 (f->methods->f_start_read) (f);
1289 /* check if any packets are available */
1290 USB_IF_POLL(&f->used_q, m);
1293 if (f->flag_iscomplete) {
1301 revents |= events & (POLLIN | POLLRDNORM);
1303 f->flag_isselect = 1;
1304 selrecord(td, &f->selinfo);
1306 if (!refs.is_usbfs) {
1307 /* start reading data */
1308 (f->methods->f_start_read) (f);
1312 lockmgr(f->priv_lock, LK_RELEASE);
1314 usb_unref_device(cpd, &refs);
1319 /*usb_read(struct cdev *dev, struct uio *uio, int ioflag)*/
1320 usb_read(struct dev_read_args *ap)
1322 struct cdev *dev = ap->a_head.a_dev;
1323 struct uio *uio = ap->a_uio;
1324 int ioflag = ap->a_ioflag;
1325 struct usb_cdev_refdata refs;
1326 struct usb_cdev_privdata* cpd;
1333 uint8_t tr_data = 0;
1336 err = devfs_get_cdevpriv((void **)&cpd);
1341 if(dev->si_drv2 == NULL)
1344 cpd = (struct usb_cdev_privdata *)dev->si_drv2;
1346 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1350 fflags = cpd->fflags;
1354 /* should not happen */
1355 usb_unref_device(cpd, &refs);
1359 resid = uio->uio_resid;
1361 lockmgr(f->priv_lock, LK_EXCLUSIVE);
1363 /* check for permanent read error */
1364 if (f->flag_iserror) {
1368 /* check if USB-FS interface is active */
1369 if (refs.is_usbfs) {
1371 * The queue is used for events that should be
1372 * retrieved using the "USB_FS_COMPLETE" ioctl.
1377 while (uio->uio_resid > 0) {
1379 USB_IF_DEQUEUE(&f->used_q, m);
1383 /* start read transfer, if not already started */
1385 (f->methods->f_start_read) (f);
1387 if (ioflag & IO_NDELAY) {
1389 /* return length before error */
1395 DPRINTF("sleeping\n");
1397 err = usb_fifo_wait(f);
1403 if (f->methods->f_filter_read) {
1405 * Sometimes it is convenient to process data at the
1406 * expense of a userland process instead of a kernel
1409 (f->methods->f_filter_read) (f, m);
1413 io_len = MIN(m->cur_data_len, uio->uio_resid);
1415 DPRINTFN(2, "transfer %d bytes from %p\n",
1416 io_len, m->cur_data_ptr);
1418 err = usb_fifo_uiomove(f,
1419 m->cur_data_ptr, io_len, uio);
1421 m->cur_data_len -= io_len;
1422 m->cur_data_ptr += io_len;
1424 if (m->cur_data_len == 0) {
1426 uint8_t last_packet;
1428 last_packet = m->last_packet;
1430 USB_IF_ENQUEUE(&f->free_q, m);
1437 USB_IF_PREPEND(&f->used_q, m);
1445 lockmgr(f->priv_lock, LK_RELEASE);
1447 usb_unref_device(cpd, &refs);
1453 /* usb_write(struct cdev *dev, struct uio *uio, int ioflag) */
1454 usb_write(struct dev_write_args *ap)
1456 struct cdev *dev = ap->a_head.a_dev;
1457 struct uio *uio = ap->a_uio;
1458 int ioflag = ap->a_ioflag;
1459 struct usb_cdev_refdata refs;
1460 struct usb_cdev_privdata* cpd;
1468 uint8_t tr_data = 0;
1473 err = devfs_get_cdevpriv((void **)&cpd);
1477 if(dev->si_drv2 == NULL)
1480 cpd = (struct usb_cdev_privdata *)dev->si_drv2;
1482 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1486 fflags = cpd->fflags;
1490 /* should not happen */
1491 usb_unref_device(cpd, &refs);
1494 resid = uio->uio_resid;
1496 lockmgr(f->priv_lock, LK_EXCLUSIVE);
1498 /* check for permanent write error */
1499 if (f->flag_iserror) {
1503 /* check if USB-FS interface is active */
1504 if (refs.is_usbfs) {
1506 * The queue is used for events that should be
1507 * retrieved using the "USB_FS_COMPLETE" ioctl.
1512 if (f->queue_data == NULL) {
1513 /* start write transfer, if not already started */
1514 (f->methods->f_start_write) (f);
1516 /* we allow writing zero length data */
1518 USB_IF_DEQUEUE(&f->free_q, m);
1522 if (ioflag & IO_NDELAY) {
1524 /* return length before error */
1530 DPRINTF("sleeping\n");
1532 err = usb_fifo_wait(f);
1540 if (f->flag_have_fragment == 0) {
1542 io_len = m->cur_data_len;
1543 pdata = m->cur_data_ptr;
1544 if (io_len > uio->uio_resid)
1545 io_len = uio->uio_resid;
1546 m->cur_data_len = io_len;
1548 io_len = m->max_data_len - m->cur_data_len;
1549 pdata = m->cur_data_ptr + m->cur_data_len;
1550 if (io_len > uio->uio_resid)
1551 io_len = uio->uio_resid;
1552 m->cur_data_len += io_len;
1555 DPRINTFN(2, "transfer %d bytes to %p\n",
1558 err = usb_fifo_uiomove(f, pdata, io_len, uio);
1561 f->flag_have_fragment = 0;
1562 USB_IF_ENQUEUE(&f->free_q, m);
1566 /* check if the buffer is ready to be transmitted */
1568 if ((f->flag_write_defrag == 0) ||
1569 (m->cur_data_len == m->max_data_len)) {
1570 f->flag_have_fragment = 0;
1573 * Check for write filter:
1575 * Sometimes it is convenient to process data
1576 * at the expense of a userland process
1577 * instead of a kernel process.
1579 if (f->methods->f_filter_write) {
1580 (f->methods->f_filter_write) (f, m);
1583 /* Put USB mbuf in the used queue */
1584 USB_IF_ENQUEUE(&f->used_q, m);
1586 /* Start writing data, if not already started */
1587 (f->methods->f_start_write) (f);
1589 /* Wait for more data or close */
1590 f->flag_have_fragment = 1;
1591 USB_IF_PREPEND(&f->free_q, m);
1594 } while (uio->uio_resid > 0);
1596 lockmgr(f->priv_lock, LK_RELEASE);
1598 usb_unref_device(cpd, &refs);
1605 usb_static_open(struct dev_open_args *ap)
1606 /*struct cdev *dev, int fflags, int devtype, struct thread *td)
1613 usb_static_close(struct dev_close_args *ap)
1614 /*struct cdev *dev, int fflags, int devtype, struct thread *td)
1621 usb_static_ioctl(struct dev_ioctl_args *ap)
1623 usb_static_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
1627 u_long cmd = ap->a_cmd;
1628 caddr_t data = ap->a_data;
1629 /* XXX: curthread the correct choice?
1631 struct thread *td = curthread;
1632 int fflag = ap->a_fflag;
1634 struct usb_read_dir *urd;
1642 err = usb_read_symlink(u.urd->urd_data,
1643 u.urd->urd_startentry, u.urd->urd_maxlen);
1645 case USB_DEV_QUIRK_GET:
1646 case USB_QUIRK_NAME_GET:
1647 case USB_DEV_QUIRK_ADD:
1648 case USB_DEV_QUIRK_REMOVE:
1649 err = usb_quirk_ioctl_p(cmd, data, fflag, td);
1651 case USB_GET_TEMPLATE:
1652 *(int *)data = usb_template;
1655 case USB_SET_TEMPLATE:
1656 err = priv_check(curthread, PRIV_DRIVER);
1659 usb_template = *(int *)data;
1669 usb_fifo_uiomove(struct usb_fifo *f, void *cp,
1670 int n, struct uio *uio)
1674 lockmgr(f->priv_lock, LK_RELEASE);
1677 * "uiomove()" can sleep so one needs to make a wrapper,
1678 * exiting the mutex and checking things:
1680 error = uiomove(cp, n, uio);
1682 lockmgr(f->priv_lock, LK_EXCLUSIVE);
1688 usb_fifo_wait(struct usb_fifo *f)
1692 KKASSERT(lockowned(f->priv_lock));
1694 if (f->flag_iserror) {
1698 f->flag_sleeping = 1;
1700 err = cv_wait_sig(&f->cv_io, f->priv_lock);
1702 if (f->flag_iserror) {
1710 usb_fifo_signal(struct usb_fifo *f)
1712 if (f->flag_sleeping) {
1713 f->flag_sleeping = 0;
1714 cv_broadcast(&f->cv_io);
1719 usb_fifo_wakeup(struct usb_fifo *f)
1723 if (f->flag_isselect) {
1725 selwakeup(&f->selinfo);
1727 f->flag_isselect = 0;
1729 if (f->async_p != NULL && lwkt_trytoken(&f->async_p->p_token)) {
1730 ksignal(f->async_p, SIGIO);
1731 lwkt_reltoken(&f->async_p->p_token);
1736 usb_fifo_dummy_open(struct usb_fifo *fifo, int fflags)
1742 usb_fifo_dummy_close(struct usb_fifo *fifo, int fflags)
1748 usb_fifo_dummy_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
1754 usb_fifo_dummy_cmd(struct usb_fifo *fifo)
1756 fifo->flag_flushing = 0; /* not flushing */
1760 usb_fifo_check_methods(struct usb_fifo_methods *pm)
1762 /* check that all callback functions are OK */
1764 if (pm->f_open == NULL)
1765 pm->f_open = &usb_fifo_dummy_open;
1767 if (pm->f_close == NULL)
1768 pm->f_close = &usb_fifo_dummy_close;
1770 if (pm->f_ioctl == NULL)
1771 pm->f_ioctl = &usb_fifo_dummy_ioctl;
1773 if (pm->f_ioctl_post == NULL)
1774 pm->f_ioctl_post = &usb_fifo_dummy_ioctl;
1776 if (pm->f_start_read == NULL)
1777 pm->f_start_read = &usb_fifo_dummy_cmd;
1779 if (pm->f_stop_read == NULL)
1780 pm->f_stop_read = &usb_fifo_dummy_cmd;
1782 if (pm->f_start_write == NULL)
1783 pm->f_start_write = &usb_fifo_dummy_cmd;
1785 if (pm->f_stop_write == NULL)
1786 pm->f_stop_write = &usb_fifo_dummy_cmd;
1789 /*------------------------------------------------------------------------*
1792 * The following function will create a duplex FIFO.
1797 *------------------------------------------------------------------------*/
1799 usb_fifo_attach(struct usb_device *udev, void *priv_sc,
1800 struct lock *priv_lock, struct usb_fifo_methods *pm,
1801 struct usb_fifo_sc *f_sc, uint16_t unit, uint16_t subunit,
1802 uint8_t iface_index, uid_t uid, gid_t gid, int mode)
1804 struct usb_fifo *f_tx;
1805 struct usb_fifo *f_rx;
1809 f_sc->fp[USB_FIFO_TX] = NULL;
1810 f_sc->fp[USB_FIFO_RX] = NULL;
1815 /* check the methods */
1816 usb_fifo_check_methods(pm);
1818 /* search for a free FIFO slot */
1819 for (n = 0;; n += 2) {
1821 if (n == USB_FIFO_MAX) {
1822 /* end of FIFOs reached */
1825 /* Check for TX FIFO */
1826 if (udev->fifo[n + USB_FIFO_TX] != NULL) {
1829 /* Check for RX FIFO */
1830 if (udev->fifo[n + USB_FIFO_RX] != NULL) {
1836 f_tx = usb_fifo_alloc();
1837 f_rx = usb_fifo_alloc();
1839 if ((f_tx == NULL) || (f_rx == NULL)) {
1840 usb_fifo_free(f_tx);
1841 usb_fifo_free(f_rx);
1844 /* initialise FIFO structures */
1846 f_tx->fifo_index = n + USB_FIFO_TX;
1847 f_tx->dev_ep_index = -1;
1848 f_tx->priv_lock = priv_lock;
1849 f_tx->priv_sc0 = priv_sc;
1851 f_tx->iface_index = iface_index;
1854 f_rx->fifo_index = n + USB_FIFO_RX;
1855 f_rx->dev_ep_index = -1;
1856 f_rx->priv_lock = priv_lock;
1857 f_rx->priv_sc0 = priv_sc;
1859 f_rx->iface_index = iface_index;
1862 f_sc->fp[USB_FIFO_TX] = f_tx;
1863 f_sc->fp[USB_FIFO_RX] = f_rx;
1865 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
1866 udev->fifo[f_tx->fifo_index] = f_tx;
1867 udev->fifo[f_rx->fifo_index] = f_rx;
1868 lockmgr(&usb_ref_lock, LK_RELEASE);
1870 for (n = 0; n != 4; n++) {
1872 if (pm->basename[n] == NULL) {
1875 if (subunit == 0xFFFF) {
1876 if (ksnprintf(devname, sizeof(devname),
1877 "%s%u%s", pm->basename[n],
1878 unit, pm->postfix[n] ?
1879 pm->postfix[n] : "")) {
1883 if (ksnprintf(devname, sizeof(devname),
1884 "%s%u.%u%s", pm->basename[n],
1885 unit, subunit, pm->postfix[n] ?
1886 pm->postfix[n] : "")) {
1892 * Distribute the symbolic links into two FIFO structures:
1895 f_rx->symlink[n / 2] =
1896 usb_alloc_symlink(devname);
1898 f_tx->symlink[n / 2] =
1899 usb_alloc_symlink(devname);
1902 /* Create the device */
1903 f_sc->dev = usb_make_dev(udev, devname, -1,
1904 f_tx->fifo_index & f_rx->fifo_index,
1905 FREAD|FWRITE, uid, gid, mode);
1908 DPRINTFN(2, "attached %p/%p\n", f_tx, f_rx);
1912 /*------------------------------------------------------------------------*
1913 * usb_fifo_alloc_buffer
1918 *------------------------------------------------------------------------*/
1920 usb_fifo_alloc_buffer(struct usb_fifo *f, usb_size_t bufsize,
1923 usb_fifo_free_buffer(f);
1925 /* allocate an endpoint */
1926 f->free_q.ifq_maxlen = nbuf;
1927 f->used_q.ifq_maxlen = nbuf;
1929 f->queue_data = usb_alloc_mbufs(
1930 M_USBDEV, &f->free_q, bufsize, nbuf);
1932 if ((f->queue_data == NULL) && bufsize && nbuf) {
1935 return (0); /* success */
1938 /*------------------------------------------------------------------------*
1939 * usb_fifo_free_buffer
1941 * This function will free the buffers associated with a FIFO. This
1942 * function can be called multiple times in a row.
1943 *------------------------------------------------------------------------*/
1945 usb_fifo_free_buffer(struct usb_fifo *f)
1947 if (f->queue_data) {
1948 /* free old buffer */
1949 kfree(f->queue_data, M_USBDEV);
1950 f->queue_data = NULL;
1954 memset(&f->free_q, 0, sizeof(f->free_q));
1955 memset(&f->used_q, 0, sizeof(f->used_q));
1959 usb_fifo_detach(struct usb_fifo_sc *f_sc)
1964 usb_fifo_free(f_sc->fp[USB_FIFO_TX]);
1965 usb_fifo_free(f_sc->fp[USB_FIFO_RX]);
1967 f_sc->fp[USB_FIFO_TX] = NULL;
1968 f_sc->fp[USB_FIFO_RX] = NULL;
1970 usb_destroy_dev(f_sc->dev);
1974 DPRINTFN(2, "detached %p\n", f_sc);
1978 usb_fifo_put_bytes_max(struct usb_fifo *f)
1983 USB_IF_POLL(&f->free_q, m);
1986 len = m->max_data_len;
1993 /*------------------------------------------------------------------------*
1997 * 0 - normal operation
1998 * 1 - set last packet flag to enforce framing
1999 *------------------------------------------------------------------------*/
2001 usb_fifo_put_data(struct usb_fifo *f, struct usb_page_cache *pc,
2002 usb_frlength_t offset, usb_frlength_t len, uint8_t what)
2005 usb_frlength_t io_len;
2007 while (len || (what == 1)) {
2009 USB_IF_DEQUEUE(&f->free_q, m);
2014 io_len = MIN(len, m->cur_data_len);
2016 usbd_copy_out(pc, offset, m->cur_data_ptr, io_len);
2018 m->cur_data_len = io_len;
2022 if ((len == 0) && (what == 1)) {
2025 USB_IF_ENQUEUE(&f->used_q, m);
2029 if ((len == 0) || (what == 1)) {
2039 usb_fifo_put_data_linear(struct usb_fifo *f, void *ptr,
2040 usb_size_t len, uint8_t what)
2045 while (len || (what == 1)) {
2047 USB_IF_DEQUEUE(&f->free_q, m);
2052 io_len = MIN(len, m->cur_data_len);
2054 memcpy(m->cur_data_ptr, ptr, io_len);
2056 m->cur_data_len = io_len;
2057 ptr = USB_ADD_BYTES(ptr, io_len);
2060 if ((len == 0) && (what == 1)) {
2063 USB_IF_ENQUEUE(&f->used_q, m);
2067 if ((len == 0) || (what == 1)) {
2077 usb_fifo_put_data_buffer(struct usb_fifo *f, void *ptr, usb_size_t len)
2081 USB_IF_DEQUEUE(&f->free_q, m);
2084 m->cur_data_len = len;
2085 m->cur_data_ptr = ptr;
2086 USB_IF_ENQUEUE(&f->used_q, m);
2094 usb_fifo_put_data_error(struct usb_fifo *f)
2096 f->flag_iserror = 1;
2100 /*------------------------------------------------------------------------*
2104 * 0 - normal operation
2105 * 1 - only get one "usb_mbuf"
2109 * 1 - data in buffer
2110 *------------------------------------------------------------------------*/
2112 usb_fifo_get_data(struct usb_fifo *f, struct usb_page_cache *pc,
2113 usb_frlength_t offset, usb_frlength_t len, usb_frlength_t *actlen,
2117 usb_frlength_t io_len;
2118 uint8_t tr_data = 0;
2124 USB_IF_DEQUEUE(&f->used_q, m);
2130 io_len = MIN(len, m->cur_data_len);
2132 usbd_copy_in(pc, offset, m->cur_data_ptr, io_len);
2136 actlen[0] += io_len;
2137 m->cur_data_ptr += io_len;
2138 m->cur_data_len -= io_len;
2140 if ((m->cur_data_len == 0) || (what == 1)) {
2141 USB_IF_ENQUEUE(&f->free_q, m);
2149 USB_IF_PREPEND(&f->used_q, m);
2154 /* wait for data to be written out */
2157 if (f->flag_flushing) {
2158 /* check if we should send a short packet */
2159 if (f->flag_short != 0) {
2164 /* flushing complete */
2165 f->flag_flushing = 0;
2178 usb_fifo_get_data_linear(struct usb_fifo *f, void *ptr,
2179 usb_size_t len, usb_size_t *actlen, uint8_t what)
2183 uint8_t tr_data = 0;
2189 USB_IF_DEQUEUE(&f->used_q, m);
2195 io_len = MIN(len, m->cur_data_len);
2197 memcpy(ptr, m->cur_data_ptr, io_len);
2200 ptr = USB_ADD_BYTES(ptr, io_len);
2201 actlen[0] += io_len;
2202 m->cur_data_ptr += io_len;
2203 m->cur_data_len -= io_len;
2205 if ((m->cur_data_len == 0) || (what == 1)) {
2206 USB_IF_ENQUEUE(&f->free_q, m);
2214 USB_IF_PREPEND(&f->used_q, m);
2219 /* wait for data to be written out */
2222 if (f->flag_flushing) {
2223 /* check if we should send a short packet */
2224 if (f->flag_short != 0) {
2229 /* flushing complete */
2230 f->flag_flushing = 0;
2243 usb_fifo_get_data_buffer(struct usb_fifo *f, void **pptr, usb_size_t *plen)
2247 USB_IF_POLL(&f->used_q, m);
2250 *plen = m->cur_data_len;
2251 *pptr = m->cur_data_ptr;
2259 usb_fifo_get_data_error(struct usb_fifo *f)
2261 f->flag_iserror = 1;
2265 /*------------------------------------------------------------------------*
2270 * Else: Pointer to symlink entry
2271 *------------------------------------------------------------------------*/
2272 struct usb_symlink *
2273 usb_alloc_symlink(const char *target)
2275 struct usb_symlink *ps;
2277 ps = kmalloc(sizeof(*ps), M_USBDEV, M_WAITOK);
2281 /* XXX no longer needed */
2282 strlcpy(ps->src_path, target, sizeof(ps->src_path));
2283 ps->src_len = strlen(ps->src_path);
2284 strlcpy(ps->dst_path, target, sizeof(ps->dst_path));
2285 ps->dst_len = strlen(ps->dst_path);
2287 lockmgr(&usb_sym_lock, LK_EXCLUSIVE);
2288 TAILQ_INSERT_TAIL(&usb_sym_head, ps, sym_entry);
2289 lockmgr(&usb_sym_lock, LK_RELEASE);
2293 /*------------------------------------------------------------------------*
2295 *------------------------------------------------------------------------*/
2297 usb_free_symlink(struct usb_symlink *ps)
2302 lockmgr(&usb_sym_lock, LK_EXCLUSIVE);
2303 TAILQ_REMOVE(&usb_sym_head, ps, sym_entry);
2304 lockmgr(&usb_sym_lock, LK_RELEASE);
2306 kfree(ps, M_USBDEV);
2309 /*------------------------------------------------------------------------*
2315 *------------------------------------------------------------------------*/
2317 usb_read_symlink(uint8_t *user_ptr, uint32_t startentry, uint32_t user_len)
2319 struct usb_symlink *ps;
2325 lockmgr(&usb_sym_lock, LK_EXCLUSIVE);
2327 TAILQ_FOREACH(ps, &usb_sym_head, sym_entry) {
2330 * Compute total length of source and destination symlink
2331 * strings pluss one length byte and two NUL bytes:
2333 temp = ps->src_len + ps->dst_len + 3;
2337 * Skip entry because this length cannot fit
2342 if (startentry != 0) {
2343 /* decrement read offset */
2347 if (temp > user_len) {
2348 /* out of buffer space */
2353 /* copy out total length */
2355 error = copyout(&len,
2356 USB_ADD_BYTES(user_ptr, delta), 1);
2362 /* copy out source string */
2364 error = copyout(ps->src_path,
2365 USB_ADD_BYTES(user_ptr, delta), ps->src_len);
2370 delta += ps->src_len;
2371 error = copyout(&len,
2372 USB_ADD_BYTES(user_ptr, delta), 1);
2378 /* copy out destination string */
2380 error = copyout(ps->dst_path,
2381 USB_ADD_BYTES(user_ptr, delta), ps->dst_len);
2386 delta += ps->dst_len;
2387 error = copyout(&len,
2388 USB_ADD_BYTES(user_ptr, delta), 1);
2397 /* a zero length entry indicates the end */
2399 if ((user_len != 0) && (error == 0)) {
2403 error = copyout(&len,
2404 USB_ADD_BYTES(user_ptr, delta), 1);
2406 lockmgr(&usb_sym_lock, LK_RELEASE);
2411 usb_fifo_set_close_zlp(struct usb_fifo *f, uint8_t onoff)
2416 /* send a Zero Length Packet, ZLP, before close */
2417 f->flag_short = onoff;
2421 usb_fifo_set_write_defrag(struct usb_fifo *f, uint8_t onoff)
2426 /* defrag written data */
2427 f->flag_write_defrag = onoff;
2428 /* reset defrag state */
2429 f->flag_have_fragment = 0;
2433 usb_fifo_softc(struct usb_fifo *f)
2435 return (f->priv_sc0);
2437 #endif /* USB_HAVE_UGEN */