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_cdevpriv_dtor(void *cd);
115 static void usb_filter_detach(struct knote *kn);
116 static int usb_filter_read(struct knote *kn, long hint);
117 static int usb_filter_write(struct knote *kn, long hint);
119 static d_open_t usb_open;
120 static d_close_t usb_close;
121 static d_ioctl_t usb_ioctl;
122 static d_read_t usb_read;
123 static d_write_t usb_write;
124 static d_kqfilter_t usb_kqfilter;
126 static d_ioctl_t usb_static_ioctl;
127 static d_open_t usb_static_open;
128 static d_close_t usb_static_close;
130 static usb_fifo_open_t usb_fifo_dummy_open;
131 static usb_fifo_close_t usb_fifo_dummy_close;
132 static usb_fifo_ioctl_t usb_fifo_dummy_ioctl;
133 static usb_fifo_cmd_t usb_fifo_dummy_cmd;
135 /* character device structure used for devices (/dev/ugenX.Y and /dev/uXXX) */
136 struct dev_ops usb_ops = {
137 { "usbdev", 0, D_MPSAFE | D_MEM },
139 .d_close = usb_close,
140 .d_ioctl = usb_ioctl,
142 .d_write = usb_write,
143 .d_kqfilter = usb_kqfilter
146 static struct cdev* usb_dev = NULL;
148 /* character device structure used for /bus/u4b */
149 static struct dev_ops usb_static_ops = {
150 { "usb", 0, D_MPSAFE | D_MEM },
151 .d_open = usb_static_open,
152 .d_close = usb_static_close,
153 .d_ioctl = usb_static_ioctl,
156 static TAILQ_HEAD(, usb_symlink) usb_sym_head;
157 static struct lock usb_sym_lock;
159 struct lock usb_ref_lock;
161 /*static struct kqinfo usb_kqevent;
164 /*------------------------------------------------------------------------*
167 * This is used to fill out a usb_cdev_privdata structure based on the
168 * device's address as contained in usb_fs_privdata.
169 *------------------------------------------------------------------------*/
171 usb_loc_fill(struct usb_fs_privdata* pd, struct usb_cdev_privdata *cpd)
173 cpd->bus_index = pd->bus_index;
174 cpd->dev_index = pd->dev_index;
175 cpd->ep_addr = pd->ep_addr;
176 cpd->fifo_index = pd->fifo_index;
179 /*------------------------------------------------------------------------*
182 * This function is used to atomically refer an USB device by its
183 * device location. If this function returns success the USB device
184 * will not dissappear until the USB device is unreferenced.
187 * 0: Success, refcount incremented on the given USB device.
189 *------------------------------------------------------------------------*/
191 usb_ref_device(struct usb_cdev_privdata *cpd,
192 struct usb_cdev_refdata *crd, int need_uref)
194 struct usb_fifo **ppf;
197 DPRINTFN(2, "cpd=%p need uref=%d\n", cpd, need_uref);
200 memset(crd, 0, sizeof(*crd));
202 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
203 cpd->bus = devclass_get_softc(usb_devclass_ptr, cpd->bus_index);
204 if (cpd->bus == NULL) {
205 DPRINTFN(2, "no bus at %u\n", cpd->bus_index);
208 cpd->udev = cpd->bus->devices[cpd->dev_index];
209 if (cpd->udev == NULL) {
210 DPRINTFN(2, "no device at %u\n", cpd->dev_index);
213 if (cpd->udev->state == USB_STATE_DETACHED &&
215 DPRINTFN(2, "device is detached\n");
218 if (cpd->udev->refcount == USB_DEV_REF_MAX) {
219 DPRINTFN(2, "no dev ref\n");
223 DPRINTFN(2, "ref udev - needed\n");
224 cpd->udev->refcount++;
226 lockmgr(&usb_ref_lock, LK_RELEASE);
229 * We need to grab the sx-lock before grabbing the
230 * FIFO refs to avoid deadlock at detach!
232 crd->do_unlock = usbd_enum_lock(cpd->udev);
234 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
237 * Set "is_uref" after grabbing the default SX lock
242 /* check if we are doing an open */
243 if (cpd->fflags == 0) {
244 /* use zero defaults */
246 /* check for write */
247 if (cpd->fflags & FWRITE) {
248 ppf = cpd->udev->fifo;
249 f = ppf[cpd->fifo_index + USB_FIFO_TX];
251 crd->is_write = 1; /* ref */
252 if (f == NULL || f->refcount == USB_FIFO_REF_MAX)
254 if (f->curr_cpd != cpd)
256 /* check if USB-FS is active */
257 if (f->fs_ep_max != 0) {
263 if (cpd->fflags & FREAD) {
264 ppf = cpd->udev->fifo;
265 f = ppf[cpd->fifo_index + USB_FIFO_RX];
267 crd->is_read = 1; /* ref */
268 if (f == NULL || f->refcount == USB_FIFO_REF_MAX)
270 if (f->curr_cpd != cpd)
272 /* check if USB-FS is active */
273 if (f->fs_ep_max != 0) {
279 /* when everything is OK we increment the refcounts */
281 DPRINTFN(2, "ref write\n");
282 crd->txfifo->refcount++;
285 DPRINTFN(2, "ref read\n");
286 crd->rxfifo->refcount++;
288 lockmgr(&usb_ref_lock, LK_RELEASE);
294 usbd_enum_unlock(cpd->udev);
297 if (--(cpd->udev->refcount) == 0) {
298 cv_signal(&cpd->udev->ref_cv);
301 lockmgr(&usb_ref_lock, LK_RELEASE);
302 DPRINTFN(2, "fail\n");
303 return (USB_ERR_INVAL);
306 /*------------------------------------------------------------------------*
309 * This function is used to upgrade an USB reference to include the
310 * USB device reference on a USB location.
313 * 0: Success, refcount incremented on the given USB device.
315 *------------------------------------------------------------------------*/
317 usb_usb_ref_device(struct usb_cdev_privdata *cpd,
318 struct usb_cdev_refdata *crd)
321 * Check if we already got an USB reference on this location:
324 return (0); /* success */
327 * To avoid deadlock at detach we need to drop the FIFO ref
328 * and re-acquire a new ref!
330 usb_unref_device(cpd, crd);
332 return (usb_ref_device(cpd, crd, 1 /* need uref */));
335 /*------------------------------------------------------------------------*
338 * This function will release the reference count by one unit for the
340 *------------------------------------------------------------------------*/
342 usb_unref_device(struct usb_cdev_privdata *cpd,
343 struct usb_cdev_refdata *crd)
346 DPRINTFN(2, "cpd=%p is_uref=%d\n", cpd, crd->is_uref);
349 usbd_enum_unlock(cpd->udev);
351 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
353 if (--(crd->rxfifo->refcount) == 0) {
354 cv_signal(&crd->rxfifo->cv_drain);
359 if (--(crd->txfifo->refcount) == 0) {
360 cv_signal(&crd->txfifo->cv_drain);
365 if (--(cpd->udev->refcount) == 0) {
366 cv_signal(&cpd->udev->ref_cv);
370 lockmgr(&usb_ref_lock, LK_RELEASE);
373 static struct usb_fifo *
378 f = kmalloc(sizeof(*f), M_USBDEV, M_WAITOK | M_ZERO);
380 cv_init(&f->cv_io, "FIFO-IO");
381 cv_init(&f->cv_drain, "FIFO-DRAIN");
387 /*------------------------------------------------------------------------*
389 *------------------------------------------------------------------------*/
391 usb_fifo_create(struct usb_cdev_privdata *cpd,
392 struct usb_cdev_refdata *crd)
394 struct usb_device *udev = cpd->udev;
396 struct usb_endpoint *ep;
402 int e = cpd->ep_addr;
404 is_tx = (cpd->fflags & FWRITE) ? 1 : 0;
405 is_rx = (cpd->fflags & FREAD) ? 1 : 0;
409 /* Preallocated FIFO */
411 DPRINTFN(5, "Preallocated FIFO\n");
413 f = udev->fifo[cpd->fifo_index + USB_FIFO_TX];
419 f = udev->fifo[cpd->fifo_index + USB_FIFO_RX];
427 KASSERT(e >= 0 && e <= 15, ("endpoint %d out of range", e));
429 /* search for a free FIFO slot */
430 DPRINTFN(5, "Endpoint device, searching for 0x%02x\n", e);
431 for (n = 0;; n += 2) {
433 if (n == USB_FIFO_MAX) {
438 /* end of FIFOs reached */
439 DPRINTFN(5, "out of FIFOs\n");
443 /* Check for TX FIFO */
445 f = udev->fifo[n + USB_FIFO_TX];
447 if (f->dev_ep_index != e) {
448 /* wrong endpoint index */
451 if (f->curr_cpd != NULL) {
456 } else if (no_null) {
460 /* Check for RX FIFO */
462 f = udev->fifo[n + USB_FIFO_RX];
464 if (f->dev_ep_index != e) {
465 /* wrong endpoint index */
468 if (f->curr_cpd != NULL) {
473 } else if (no_null) {
481 if (e >= (USB_EP_MAX / 2)) {
482 /* we don't create any endpoints in this range */
483 DPRINTFN(5, "ep out of range\n");
484 return (is_busy ? EBUSY : EINVAL);
488 if ((e != 0) && is_busy) {
490 * Only the default control endpoint is allowed to be
491 * opened multiple times!
493 DPRINTFN(5, "busy\n");
499 (udev->fifo[n + USB_FIFO_TX] == NULL)) {
500 ep = usb_dev_get_ep(udev, e, USB_FIFO_TX);
501 DPRINTFN(5, "dev_get_endpoint(%d, 0x%x)\n", e, USB_FIFO_TX);
503 DPRINTFN(5, "dev_get_endpoint returned NULL\n");
506 f = usb_fifo_alloc();
508 DPRINTFN(5, "could not alloc tx fifo\n");
511 /* update some fields */
512 f->fifo_index = n + USB_FIFO_TX;
514 f->priv_lock = &udev->device_lock;
516 f->methods = &usb_ugen_methods;
517 f->iface_index = ep->iface_index;
519 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
520 udev->fifo[n + USB_FIFO_TX] = f;
521 lockmgr(&usb_ref_lock, LK_RELEASE);
525 (udev->fifo[n + USB_FIFO_RX] == NULL)) {
527 ep = usb_dev_get_ep(udev, e, USB_FIFO_RX);
528 DPRINTFN(5, "dev_get_endpoint(%d, 0x%x)\n", e, USB_FIFO_RX);
530 DPRINTFN(5, "dev_get_endpoint returned NULL\n");
533 f = usb_fifo_alloc();
535 DPRINTFN(5, "could not alloc rx fifo\n");
538 /* update some fields */
539 f->fifo_index = n + USB_FIFO_RX;
541 f->priv_lock = &udev->device_lock;
543 f->methods = &usb_ugen_methods;
544 f->iface_index = ep->iface_index;
546 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
547 udev->fifo[n + USB_FIFO_RX] = f;
548 lockmgr(&usb_ref_lock, LK_RELEASE);
551 crd->txfifo = udev->fifo[n + USB_FIFO_TX];
554 crd->rxfifo = udev->fifo[n + USB_FIFO_RX];
556 /* fill out fifo index */
557 DPRINTFN(5, "fifo index = %d\n", n);
566 usb_fifo_free(struct usb_fifo *f)
574 /* destroy symlink devices, if any */
575 for (n = 0; n != 2; n++) {
577 usb_free_symlink(f->symlink[n]);
578 f->symlink[n] = NULL;
581 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
583 /* delink ourselves to stop calls from userland */
584 if ((f->fifo_index < USB_FIFO_MAX) &&
586 (f->udev->fifo[f->fifo_index] == f)) {
587 f->udev->fifo[f->fifo_index] = NULL;
589 DPRINTFN(0, "USB FIFO %p has not been linked\n", f);
592 /* decrease refcount */
594 /* prevent any write flush */
596 /* need to wait until all callers have exited */
597 while (f->refcount != 0) {
598 lockmgr(&usb_ref_lock, LK_RELEASE); /* avoid LOR */
599 lockmgr(f->priv_lock, LK_EXCLUSIVE);
600 /* get I/O thread out of any sleep state */
601 if (f->flag_sleeping) {
602 f->flag_sleeping = 0;
603 cv_broadcast(&f->cv_io);
605 lockmgr(f->priv_lock, LK_RELEASE);
606 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
609 * Check if the "f->refcount" variable reached zero
610 * during the unlocked time before entering wait:
612 if (f->refcount == 0)
616 cv_wait(&f->cv_drain, &usb_ref_lock);
618 lockmgr(&usb_ref_lock, LK_RELEASE);
620 /* take care of closing the device here, if any */
621 usb_fifo_close(f, 0);
623 cv_destroy(&f->cv_io);
624 cv_destroy(&f->cv_drain);
629 static struct usb_endpoint *
630 usb_dev_get_ep(struct usb_device *udev, uint8_t ep_index, uint8_t dir)
632 struct usb_endpoint *ep;
638 if (dir == USB_FIFO_RX) {
639 if (udev->flags.usb_mode == USB_MODE_HOST) {
645 if (udev->flags.usb_mode == USB_MODE_HOST) {
651 ep = usbd_get_ep_by_addr(udev, ep_index | ep_dir);
655 /* if the endpoint does not exist then return */
658 if (ep->edesc == NULL) {
659 /* invalid endpoint */
662 return (ep); /* success */
665 /*------------------------------------------------------------------------*
671 *------------------------------------------------------------------------*/
673 usb_fifo_open(struct usb_cdev_privdata *cpd,
674 struct usb_fifo *f, int fflags)
680 DPRINTFN(2, "no FIFO\n");
683 /* remove FWRITE and FREAD flags */
684 fflags &= ~(FWRITE | FREAD);
686 /* set correct file flags */
687 if ((f->fifo_index & 1) == USB_FIFO_TX) {
693 /* check if we are already opened */
694 /* we don't need any locks when checking this variable */
695 if (f->curr_cpd != NULL) {
700 /* reset short flag before open */
703 /* call open method */
704 err = (f->methods->f_open) (f, fflags);
708 lockmgr(f->priv_lock, LK_EXCLUSIVE);
710 /* reset sleep flag */
711 f->flag_sleeping = 0;
713 /* reset error flag */
716 /* reset complete flag */
717 f->flag_iscomplete = 0;
719 /* reset select flag */
720 f->flag_isselect = 0;
722 /* reset flushing flag */
723 f->flag_flushing = 0;
725 /* reset ASYNC proc flag */
728 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
729 /* flag the fifo as opened to prevent others */
731 lockmgr(&usb_ref_lock, LK_RELEASE);
736 lockmgr(f->priv_lock, LK_RELEASE);
741 /*------------------------------------------------------------------------*
743 *------------------------------------------------------------------------*/
745 usb_fifo_reset(struct usb_fifo *f)
753 USB_IF_DEQUEUE(&f->used_q, m);
755 USB_IF_ENQUEUE(&f->free_q, m);
760 /* reset have fragment flag */
761 f->flag_have_fragment = 0;
764 /*------------------------------------------------------------------------*
766 *------------------------------------------------------------------------*/
768 usb_fifo_close(struct usb_fifo *f, int fflags)
772 /* check if we are not opened */
773 if (f->curr_cpd == NULL) {
774 /* nothing to do - already closed */
777 lockmgr(f->priv_lock, LK_EXCLUSIVE);
779 /* clear current cdev private data pointer */
782 /* check if we are selected */
783 if (f->flag_isselect) {
784 KNOTE(&f->selinfo.ki_note, 0);
785 wakeup(&f->selinfo.ki_note);
787 f->flag_isselect = 0;
789 /* check if a thread wants SIGIO */
790 if (f->async_p != NULL && lwkt_trytoken(&f->async_p->p_token)) {
791 ksignal(f->async_p, SIGIO);
792 lwkt_reltoken(&f->async_p->p_token);
795 /* remove FWRITE and FREAD flags */
796 fflags &= ~(FWRITE | FREAD);
798 /* flush written data, if any */
799 if ((f->fifo_index & 1) == USB_FIFO_TX) {
801 if (!f->flag_iserror) {
803 /* set flushing flag */
804 f->flag_flushing = 1;
806 /* get the last packet in */
807 if (f->flag_have_fragment) {
809 f->flag_have_fragment = 0;
810 USB_IF_DEQUEUE(&f->free_q, m);
812 USB_IF_ENQUEUE(&f->used_q, m);
816 /* start write transfer, if not already started */
817 (f->methods->f_start_write) (f);
819 /* check if flushed already */
820 while (f->flag_flushing &&
821 (!f->flag_iserror)) {
822 /* wait until all data has been written */
823 f->flag_sleeping = 1;
824 err = cv_wait_sig(&f->cv_io, f->priv_lock);
826 DPRINTF("signal received\n");
833 /* stop write transfer, if not already stopped */
834 (f->methods->f_stop_write) (f);
838 /* stop write transfer, if not already stopped */
839 (f->methods->f_stop_read) (f);
842 /* check if we are sleeping */
843 if (f->flag_sleeping) {
844 DPRINTFN(2, "Sleeping at close!\n");
846 lockmgr(f->priv_lock, LK_RELEASE);
848 /* call close method */
849 (f->methods->f_close) (f, fflags);
854 /*------------------------------------------------------------------------*
855 * usb_open - cdev callback
856 *------------------------------------------------------------------------*/
858 usb_open(struct dev_open_args *ap)
860 struct cdev *dev = ap->a_head.a_dev;
861 int fflags = ap->a_oflags;
862 struct usb_fs_privdata* pd = (struct usb_fs_privdata*)dev->si_drv1;
863 struct usb_cdev_refdata refs;
864 struct usb_cdev_privdata *cpd;
867 DPRINTFN(2, "%s fflags=0x%08x\n", devtoname(dev), fflags);
869 KASSERT(fflags & (FREAD|FWRITE), ("invalid open flags"));
870 if (((fflags & FREAD) && !(pd->mode & FREAD)) ||
871 ((fflags & FWRITE) && !(pd->mode & FWRITE))) {
872 DPRINTFN(2, "access mode not supported\n");
876 cpd = kmalloc(sizeof(*cpd), M_USBDEV, M_WAITOK | M_ZERO);
877 ep = cpd->ep_addr = pd->ep_addr;
879 usb_loc_fill(pd, cpd);
880 err = usb_ref_device(cpd, &refs, 1);
882 DPRINTFN(2, "cannot ref device\n");
883 kfree(cpd, M_USBDEV);
886 cpd->fflags = fflags; /* access mode for open lifetime */
888 /* create FIFOs, if any */
889 err = usb_fifo_create(cpd, &refs);
890 /* check for error */
892 DPRINTFN(2, "cannot create fifo\n");
893 usb_unref_device(cpd, &refs);
894 kfree(cpd, M_USBDEV);
897 if (fflags & FREAD) {
898 err = usb_fifo_open(cpd, refs.rxfifo, fflags);
900 DPRINTFN(2, "read open failed\n");
901 usb_unref_device(cpd, &refs);
902 kfree(cpd, M_USBDEV);
906 if (fflags & FWRITE) {
907 err = usb_fifo_open(cpd, refs.txfifo, fflags);
909 DPRINTFN(2, "write open failed\n");
910 if (fflags & FREAD) {
911 usb_fifo_close(refs.rxfifo, fflags);
913 usb_unref_device(cpd, &refs);
914 kfree(cpd, M_USBDEV);
918 usb_unref_device(cpd, &refs);
919 err = devfs_set_cdevpriv(ap->a_fp, cpd, &usb_cdevpriv_dtor);
920 DPRINTFN(2, "fp=%p cpd=%p\n", ap->a_fp, cpd);
922 DPRINTFN(2, "devfs_set_cdevpriv failed in %s\n", __func__);
923 kfree(cpd, M_USBDEV);
933 usb_close(struct dev_close_args *ap)
935 DPRINTFN(2, "usb_close called\n");
939 /*------------------------------------------------------------------------*
940 * usb_close - cdev callback
941 *------------------------------------------------------------------------*/
943 usb_cdevpriv_dtor(void *cd)
945 struct usb_cdev_privdata *cpd = (struct usb_cdev_privdata *)cd;
946 struct usb_cdev_refdata refs;
949 DPRINTF("dtor called on %p\n", cpd);
951 err = usb_ref_device(cpd, &refs, 2);
953 DPRINTFN(0, "Cannot grab USB reference when "
954 "closing USB file handle\n");
957 if (cpd->fflags & FREAD) {
958 usb_fifo_close(refs.rxfifo, cpd->fflags);
960 if (cpd->fflags & FWRITE) {
961 usb_fifo_close(refs.txfifo, cpd->fflags);
963 usb_unref_device(cpd, &refs);
965 kfree(cpd, M_USBDEV);
969 usb_dev_init(void *arg)
971 lockinit(&usb_ref_lock, "USB ref mutex", 0, 0);
972 lockinit(&usb_sym_lock, "USB sym mutex", 0, 0);
973 TAILQ_INIT(&usb_sym_head);
975 /* check the UGEN methods */
976 usb_fifo_check_methods(&usb_ugen_methods);
979 /* XXX SI_SUB_KLD? */
980 SYSINIT(usb_dev_init, SI_SUB_PRE_DRIVERS, SI_ORDER_FIRST, usb_dev_init, NULL);
983 usb_dev_init_post(void *arg)
986 * Create /dev/usb - this is needed for usbconfig(8), which
987 * needs a well-known device name to access.
989 usb_dev = make_dev(&usb_static_ops, 0, UID_ROOT, GID_OPERATOR,
990 0644, USB_DEVICE_NAME);
991 if (usb_dev == NULL) {
992 DPRINTFN(0, "Could not create usb bus device\n");
996 SYSINIT(usb_dev_init_post, SI_SUB_DRIVERS, SI_ORDER_FIRST, usb_dev_init_post,
1000 usb_dev_uninit(void *arg)
1002 if (usb_dev != NULL) {
1003 destroy_dev(usb_dev);
1006 lockuninit(&usb_ref_lock);
1007 lockuninit(&usb_sym_lock);
1010 SYSUNINIT(usb_dev_uninit, SI_SUB_KICK_SCHEDULER, SI_ORDER_ANY, usb_dev_uninit, NULL);
1013 usb_ioctl_f_sub(struct usb_fifo *f, u_long cmd, void *addr,
1020 *(int *)addr = 0; /* character device */
1024 /* handled by upper FS layer */
1029 if (f->async_p != NULL) {
1033 f->async_p = USB_TD_GET_PROC(td);
1039 /* XXX this is not the most general solution */
1041 if (f->async_p == NULL) {
1045 if (*(int *)addr != USB_PROC_GET_GID(f->async_p)) {
1053 DPRINTFN(3, "cmd 0x%lx = %d\n", cmd, error);
1057 /*------------------------------------------------------------------------*
1058 * usb_ioctl - cdev callback
1059 *------------------------------------------------------------------------*/
1061 usb_ioctl(struct dev_ioctl_args *ap)
1063 u_long cmd = ap->a_cmd;
1064 caddr_t addr = ap->a_data;
1065 struct thread *td = curthread;
1066 struct usb_cdev_refdata refs;
1067 struct usb_cdev_privdata* cpd;
1072 DPRINTFN(2, "cmd=0x%lx\n", cmd);
1074 err = devfs_get_cdevpriv(ap->a_fp, (void **)&cpd);
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 != USB_RE_ENUM_DONE) {
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 struct knote *kn = ap->a_kn;
1155 struct klist *klist;
1157 struct usb_cdev_refdata refs;
1158 struct usb_cdev_privdata* cpd;
1161 err = devfs_get_cdevpriv(ap->a_fp, (void **)&cpd);
1164 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1169 fflags = cpd->fflags;
1171 switch(kn->kn_filter) {
1174 if(fflags & FREAD) {
1175 lockmgr(f->priv_lock, LK_EXCLUSIVE);
1176 f->flag_isselect = 1;
1177 lockmgr(f->priv_lock, LK_RELEASE);
1178 kn->kn_fop = &usb_filtops_read;
1180 ap->a_result = EOPNOTSUPP;
1186 if(fflags & FWRITE) {
1187 lockmgr(f->priv_lock, LK_EXCLUSIVE);
1188 f->flag_isselect = 1;
1189 lockmgr(f->priv_lock, LK_RELEASE);
1190 kn->kn_fop = &usb_filtops_write;
1192 ap->a_result = EOPNOTSUPP;
1197 DPRINTF("unsupported kqfilter requested\n");
1198 ap->a_result = EOPNOTSUPP;
1199 usb_unref_device(cpd, &refs);
1203 kn->kn_hook = (caddr_t)f;
1204 klist = &f->selinfo.ki_note;
1205 knote_insert(klist, kn);
1207 usb_unref_device(cpd, &refs);
1212 usb_filter_detach(struct knote *kn)
1214 struct usb_fifo *f = (struct usb_fifo *)kn->kn_hook;
1215 struct usb_cdev_privdata* cpd = f->curr_cpd;
1216 struct usb_cdev_refdata refs;
1217 struct klist *klist;
1222 * The associated cpd has vanished.
1228 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1233 lockmgr(f->priv_lock, LK_EXCLUSIVE);
1234 if(f->flag_isselect) {
1235 klist = &f->selinfo.ki_note;
1236 knote_remove(klist, kn);
1237 f->flag_isselect = 0;
1239 lockmgr(f->priv_lock, LK_RELEASE);
1241 usb_unref_device(cpd, &refs);
1245 usb_filter_read(struct knote *kn, long hint)
1247 struct usb_fifo *f = (struct usb_fifo *)kn->kn_hook;
1248 struct usb_cdev_privdata* cpd = f->curr_cpd;
1249 struct usb_cdev_refdata refs;
1251 int err,locked,ready = 0;
1255 * The associated file has been closed.
1258 kn->kn_flags |= EV_ERROR;
1262 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1264 kn->kn_flags |= EV_ERROR;
1268 For some reason this function is called both
1269 with the priv_lock held and with the priv_lock
1270 not held. We need to find out from where and
1272 locked = lockowned(f->priv_lock);
1274 lockmgr(f->priv_lock, LK_EXCLUSIVE);
1276 if (!refs.is_usbfs) {
1277 if (f->flag_iserror) {
1278 /* we got an error */
1279 kn->kn_flags |= EV_ERROR;
1282 /* start read if not running */
1283 (f->methods->f_start_read)(f);
1284 /* check if any packets are available */
1285 USB_IF_POLL(&f->used_q, m);
1291 if (f->flag_iscomplete) {
1299 lockmgr(f->priv_lock, LK_RELEASE);
1301 usb_unref_device(cpd, &refs);
1303 DPRINTFN(3,"ready %d\n", ready);
1308 usb_filter_write(struct knote *kn, long hint)
1315 This seems to work just fine. Not sure whether we
1316 need the whole kerfuffle done in usb_poll.
1317 It also does not work, probably since if there
1318 wasn't a write transfer queued before, why
1319 should it have completed.
1323 struct usb_fifo *f = (struct usb_fifo *)kn->kn_hook;
1324 struct usb_cdev_privdata* cpd = f->curr_cpd;
1325 struct usb_cdev_refdata refs;
1327 int locked, err,ready = 0;
1332 * The associated file has been closed.
1335 kn->kn_flags |= EV_ERROR;
1339 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1341 kn->kn_flags |= EV_ERROR;
1345 locked = lockowned(f->priv_lock);
1347 lockmgr(f->priv_lock, LK_EXCLUSIVE);
1349 if (!refs.is_usbfs) {
1350 if (f->flag_iserror) {
1351 /* we got an error */
1352 kn->kn_flags |= EV_ERROR;
1355 if (f->queue_data == NULL) {
1357 * start write transfer, if not
1360 (f->methods->f_start_write) (f);
1362 /* check if any packets are available */
1363 USB_IF_POLL(&f->free_q, m);
1369 if (f->flag_iscomplete) {
1377 lockmgr(f->priv_lock, LK_RELEASE);
1379 usb_unref_device(cpd, &refs);
1385 /* This is implemented above using kqfilter */
1388 usb_poll(struct cdev* dev, int events, struct thread* td)
1390 struct usb_cdev_refdata refs;
1391 struct usb_cdev_privdata* cpd;
1394 int fflags, revents;
1396 if (devfs_get_cdevpriv((void **)&cpd) != 0 ||
1397 usb_ref_device(cpd, &refs, 0) != 0)
1399 (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM));
1401 fflags = cpd->fflags;
1403 /* Figure out who needs service */
1405 if ((events & (POLLOUT | POLLWRNORM)) &&
1406 (fflags & FWRITE)) {
1410 lockmgr(f->priv_lock, LK_EXCLUSIVE);
1412 if (!refs.is_usbfs) {
1413 if (f->flag_iserror) {
1414 /* we got an error */
1417 if (f->queue_data == NULL) {
1419 * start write transfer, if not
1422 (f->methods->f_start_write) (f);
1424 /* check if any packets are available */
1425 USB_IF_POLL(&f->free_q, m);
1428 if (f->flag_iscomplete) {
1436 revents |= events & (POLLOUT | POLLWRNORM);
1438 f->flag_isselect = 1;
1439 selrecord(td, &f->selinfo);
1442 lockmgr(f->priv_lock);
1444 if ((events & (POLLIN | POLLRDNORM)) &&
1449 lockmgr(f->priv_lock, LK_EXCLUSIVE);
1451 if (!refs.is_usbfs) {
1452 if (f->flag_iserror) {
1453 /* we have and error */
1456 if (f->queue_data == NULL) {
1458 * start read transfer, if not
1461 (f->methods->f_start_read) (f);
1463 /* check if any packets are available */
1464 USB_IF_POLL(&f->used_q, m);
1467 if (f->flag_iscomplete) {
1475 revents |= events & (POLLIN | POLLRDNORM);
1477 f->flag_isselect = 1;
1478 selrecord(td, &f->selinfo);
1480 if (!refs.is_usbfs) {
1481 /* start reading data */
1482 (f->methods->f_start_read) (f);
1486 lockmgr(f->priv_lock, LK_RELEASE);
1488 usb_unref_device(cpd, &refs);
1494 usb_read(struct dev_read_args *ap)
1496 struct uio *uio = ap->a_uio;
1497 int ioflag = ap->a_ioflag;
1498 struct usb_cdev_refdata refs;
1499 struct usb_cdev_privdata* cpd;
1506 uint8_t tr_data = 0;
1508 err = devfs_get_cdevpriv(ap->a_fp, (void **)&cpd);
1511 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1515 fflags = cpd->fflags;
1519 /* should not happen */
1520 usb_unref_device(cpd, &refs);
1524 resid = uio->uio_resid;
1526 lockmgr(f->priv_lock, LK_EXCLUSIVE);
1528 /* check for permanent read error */
1529 if (f->flag_iserror) {
1533 /* check if USB-FS interface is active */
1534 if (refs.is_usbfs) {
1536 * The queue is used for events that should be
1537 * retrieved using the "USB_FS_COMPLETE" ioctl.
1542 while (uio->uio_resid > 0) {
1544 USB_IF_DEQUEUE(&f->used_q, m);
1548 /* start read transfer, if not already started */
1550 (f->methods->f_start_read) (f);
1552 if (ioflag & IO_NDELAY) {
1554 /* return length before error */
1560 DPRINTF("sleeping\n");
1562 err = usb_fifo_wait(f);
1568 if (f->methods->f_filter_read) {
1570 * Sometimes it is convenient to process data at the
1571 * expense of a userland process instead of a kernel
1574 (f->methods->f_filter_read) (f, m);
1578 io_len = MIN(m->cur_data_len, uio->uio_resid);
1580 DPRINTFN(2, "transfer %d bytes from %p\n",
1581 io_len, m->cur_data_ptr);
1583 err = usb_fifo_uiomove(f,
1584 m->cur_data_ptr, io_len, uio);
1586 m->cur_data_len -= io_len;
1587 m->cur_data_ptr += io_len;
1589 if (m->cur_data_len == 0) {
1591 uint8_t last_packet;
1593 last_packet = m->last_packet;
1595 USB_IF_ENQUEUE(&f->free_q, m);
1602 USB_IF_PREPEND(&f->used_q, m);
1611 lockmgr(f->priv_lock, LK_RELEASE);
1613 usb_unref_device(cpd, &refs);
1619 usb_write(struct dev_write_args *ap)
1621 struct uio *uio = ap->a_uio;
1622 int ioflag = ap->a_ioflag;
1623 struct usb_cdev_refdata refs;
1624 struct usb_cdev_privdata* cpd;
1632 uint8_t tr_data = 0;
1636 err = devfs_get_cdevpriv(ap->a_fp, (void **)&cpd);
1639 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1643 fflags = cpd->fflags;
1647 /* should not happen */
1648 usb_unref_device(cpd, &refs);
1651 resid = uio->uio_resid;
1653 lockmgr(f->priv_lock, LK_EXCLUSIVE);
1655 /* check for permanent write error */
1656 if (f->flag_iserror) {
1660 /* check if USB-FS interface is active */
1661 if (refs.is_usbfs) {
1663 * The queue is used for events that should be
1664 * retrieved using the "USB_FS_COMPLETE" ioctl.
1669 if (f->queue_data == NULL) {
1670 /* start write transfer, if not already started */
1671 (f->methods->f_start_write) (f);
1673 /* we allow writing zero length data */
1675 USB_IF_DEQUEUE(&f->free_q, m);
1679 if (ioflag & IO_NDELAY) {
1681 /* return length before error */
1687 DPRINTF("sleeping\n");
1689 err = usb_fifo_wait(f);
1697 if (f->flag_have_fragment == 0) {
1699 io_len = m->cur_data_len;
1700 pdata = m->cur_data_ptr;
1701 if (io_len > uio->uio_resid)
1702 io_len = uio->uio_resid;
1703 m->cur_data_len = io_len;
1705 io_len = m->max_data_len - m->cur_data_len;
1706 pdata = m->cur_data_ptr + m->cur_data_len;
1707 if (io_len > uio->uio_resid)
1708 io_len = uio->uio_resid;
1709 m->cur_data_len += io_len;
1712 DPRINTFN(2, "transfer %d bytes to %p\n",
1715 err = usb_fifo_uiomove(f, pdata, io_len, uio);
1718 f->flag_have_fragment = 0;
1719 USB_IF_ENQUEUE(&f->free_q, m);
1723 /* check if the buffer is ready to be transmitted */
1725 if ((f->flag_write_defrag == 0) ||
1726 (m->cur_data_len == m->max_data_len)) {
1727 f->flag_have_fragment = 0;
1730 * Check for write filter:
1732 * Sometimes it is convenient to process data
1733 * at the expense of a userland process
1734 * instead of a kernel process.
1736 if (f->methods->f_filter_write) {
1737 (f->methods->f_filter_write) (f, m);
1740 /* Put USB mbuf in the used queue */
1741 USB_IF_ENQUEUE(&f->used_q, m);
1743 /* Start writing data, if not already started */
1744 (f->methods->f_start_write) (f);
1746 /* Wait for more data or close */
1747 f->flag_have_fragment = 1;
1748 USB_IF_PREPEND(&f->free_q, m);
1751 } while (uio->uio_resid > 0);
1753 lockmgr(f->priv_lock, LK_RELEASE);
1755 usb_unref_device(cpd, &refs);
1761 usb_static_open(struct dev_open_args *ap)
1767 usb_static_close(struct dev_close_args *ap)
1773 usb_static_ioctl(struct dev_ioctl_args *ap)
1775 u_long cmd = ap->a_cmd;
1776 caddr_t data = ap->a_data;
1777 struct thread *td = curthread; /* XXX: curthread the correct choice? */
1778 int fflag = ap->a_fflag;
1780 struct usb_read_dir *urd;
1788 err = usb_read_symlink(u.urd->urd_data,
1789 u.urd->urd_startentry, u.urd->urd_maxlen);
1791 case USB_DEV_QUIRK_GET:
1792 case USB_QUIRK_NAME_GET:
1793 case USB_DEV_QUIRK_ADD:
1794 case USB_DEV_QUIRK_REMOVE:
1795 err = usb_quirk_ioctl_p(cmd, data, fflag, td);
1797 case USB_GET_TEMPLATE:
1798 *(int *)data = usb_template;
1801 case USB_SET_TEMPLATE:
1802 err = priv_check(curthread, PRIV_DRIVER);
1805 usb_template = *(int *)data;
1815 usb_fifo_uiomove(struct usb_fifo *f, void *cp,
1816 int n, struct uio *uio)
1820 lockmgr(f->priv_lock, LK_RELEASE);
1823 * "uiomove()" can sleep so one needs to make a wrapper,
1824 * exiting the mutex and checking things:
1826 error = uiomove(cp, n, uio);
1828 lockmgr(f->priv_lock, LK_EXCLUSIVE);
1834 usb_fifo_wait(struct usb_fifo *f)
1838 KKASSERT(lockowned(f->priv_lock));
1840 if (f->flag_iserror) {
1844 f->flag_sleeping = 1;
1846 err = cv_wait_sig(&f->cv_io, f->priv_lock);
1848 if (f->flag_iserror) {
1856 usb_fifo_signal(struct usb_fifo *f)
1858 if (f->flag_sleeping) {
1859 f->flag_sleeping = 0;
1860 cv_broadcast(&f->cv_io);
1865 usb_fifo_wakeup(struct usb_fifo *f)
1869 if (f->flag_isselect) {
1870 KNOTE(&f->selinfo.ki_note, 0);
1871 wakeup(&f->selinfo.ki_note);
1873 if (f->async_p != NULL && lwkt_trytoken(&f->async_p->p_token)) {
1874 ksignal(f->async_p, SIGIO);
1875 lwkt_reltoken(&f->async_p->p_token);
1880 usb_fifo_dummy_open(struct usb_fifo *fifo, int fflags)
1886 usb_fifo_dummy_close(struct usb_fifo *fifo, int fflags)
1892 usb_fifo_dummy_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
1898 usb_fifo_dummy_cmd(struct usb_fifo *fifo)
1900 fifo->flag_flushing = 0; /* not flushing */
1904 usb_fifo_check_methods(struct usb_fifo_methods *pm)
1906 /* check that all callback functions are OK */
1908 if (pm->f_open == NULL)
1909 pm->f_open = &usb_fifo_dummy_open;
1911 if (pm->f_close == NULL)
1912 pm->f_close = &usb_fifo_dummy_close;
1914 if (pm->f_ioctl == NULL)
1915 pm->f_ioctl = &usb_fifo_dummy_ioctl;
1917 if (pm->f_ioctl_post == NULL)
1918 pm->f_ioctl_post = &usb_fifo_dummy_ioctl;
1920 if (pm->f_start_read == NULL)
1921 pm->f_start_read = &usb_fifo_dummy_cmd;
1923 if (pm->f_stop_read == NULL)
1924 pm->f_stop_read = &usb_fifo_dummy_cmd;
1926 if (pm->f_start_write == NULL)
1927 pm->f_start_write = &usb_fifo_dummy_cmd;
1929 if (pm->f_stop_write == NULL)
1930 pm->f_stop_write = &usb_fifo_dummy_cmd;
1933 /*------------------------------------------------------------------------*
1936 * The following function will create a duplex FIFO.
1941 *------------------------------------------------------------------------*/
1943 usb_fifo_attach(struct usb_device *udev, void *priv_sc,
1944 struct lock *priv_lock, struct usb_fifo_methods *pm,
1945 struct usb_fifo_sc *f_sc, uint16_t unit, int16_t subunit,
1946 uint8_t iface_index, uid_t uid, gid_t gid, int mode)
1948 struct usb_fifo *f_tx;
1949 struct usb_fifo *f_rx;
1953 f_sc->fp[USB_FIFO_TX] = NULL;
1954 f_sc->fp[USB_FIFO_RX] = NULL;
1959 /* check the methods */
1960 usb_fifo_check_methods(pm);
1962 if (priv_lock == NULL) {
1963 DPRINTF("null priv_lock set\n");
1966 /* search for a free FIFO slot */
1967 for (n = 0;; n += 2) {
1969 if (n == USB_FIFO_MAX) {
1970 /* end of FIFOs reached */
1973 /* Check for TX FIFO */
1974 if (udev->fifo[n + USB_FIFO_TX] != NULL) {
1977 /* Check for RX FIFO */
1978 if (udev->fifo[n + USB_FIFO_RX] != NULL) {
1984 f_tx = usb_fifo_alloc();
1985 f_rx = usb_fifo_alloc();
1987 if ((f_tx == NULL) || (f_rx == NULL)) {
1988 usb_fifo_free(f_tx);
1989 usb_fifo_free(f_rx);
1992 /* initialise FIFO structures */
1994 f_tx->fifo_index = n + USB_FIFO_TX;
1995 f_tx->dev_ep_index = -1;
1996 f_tx->priv_lock = priv_lock;
1997 f_tx->priv_sc0 = priv_sc;
1999 f_tx->iface_index = iface_index;
2002 f_rx->fifo_index = n + USB_FIFO_RX;
2003 f_rx->dev_ep_index = -1;
2004 f_rx->priv_lock = priv_lock;
2005 f_rx->priv_sc0 = priv_sc;
2007 f_rx->iface_index = iface_index;
2010 f_sc->fp[USB_FIFO_TX] = f_tx;
2011 f_sc->fp[USB_FIFO_RX] = f_rx;
2013 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
2014 udev->fifo[f_tx->fifo_index] = f_tx;
2015 udev->fifo[f_rx->fifo_index] = f_rx;
2016 lockmgr(&usb_ref_lock, LK_RELEASE);
2018 for (n = 0; n != 4; n++) {
2020 if (pm->basename[n] == NULL) {
2024 if (ksnprintf(devname, sizeof(devname),
2025 "%s%u%s", pm->basename[n],
2026 unit, pm->postfix[n] ?
2027 pm->postfix[n] : "")) {
2031 if (ksnprintf(devname, sizeof(devname),
2032 "%s%u.%u%s", pm->basename[n],
2033 unit, subunit, pm->postfix[n] ?
2034 pm->postfix[n] : "")) {
2040 * Distribute the symbolic links into two FIFO structures:
2043 f_rx->symlink[n / 2] =
2044 usb_alloc_symlink(devname);
2046 f_tx->symlink[n / 2] =
2047 usb_alloc_symlink(devname);
2050 /* Create the device */
2051 f_sc->dev = usb_make_dev(udev, devname, -1,
2052 f_tx->fifo_index & f_rx->fifo_index,
2053 FREAD|FWRITE, uid, gid, mode);
2056 DPRINTFN(2, "attached %p/%p\n", f_tx, f_rx);
2060 /*------------------------------------------------------------------------*
2061 * usb_fifo_alloc_buffer
2066 *------------------------------------------------------------------------*/
2068 usb_fifo_alloc_buffer(struct usb_fifo *f, usb_size_t bufsize,
2071 usb_fifo_free_buffer(f);
2073 /* allocate an endpoint */
2074 f->free_q.ifq_maxlen = nbuf;
2075 f->used_q.ifq_maxlen = nbuf;
2077 f->queue_data = usb_alloc_mbufs(
2078 M_USBDEV, &f->free_q, bufsize, nbuf);
2080 if ((f->queue_data == NULL) && bufsize && nbuf) {
2083 return (0); /* success */
2086 /*------------------------------------------------------------------------*
2087 * usb_fifo_free_buffer
2089 * This function will free the buffers associated with a FIFO. This
2090 * function can be called multiple times in a row.
2091 *------------------------------------------------------------------------*/
2093 usb_fifo_free_buffer(struct usb_fifo *f)
2095 if (f->queue_data) {
2096 /* free old buffer */
2097 kfree(f->queue_data, M_USBDEV);
2098 f->queue_data = NULL;
2102 memset(&f->free_q, 0, sizeof(f->free_q));
2103 memset(&f->used_q, 0, sizeof(f->used_q));
2107 usb_fifo_detach(struct usb_fifo_sc *f_sc)
2112 usb_fifo_free(f_sc->fp[USB_FIFO_TX]);
2113 usb_fifo_free(f_sc->fp[USB_FIFO_RX]);
2115 f_sc->fp[USB_FIFO_TX] = NULL;
2116 f_sc->fp[USB_FIFO_RX] = NULL;
2118 usb_destroy_dev(f_sc->dev);
2122 DPRINTFN(2, "detached %p\n", f_sc);
2126 usb_fifo_put_bytes_max(struct usb_fifo *f)
2131 USB_IF_POLL(&f->free_q, m);
2134 len = m->max_data_len;
2141 /*------------------------------------------------------------------------*
2145 * 0 - normal operation
2146 * 1 - set last packet flag to enforce framing
2147 *------------------------------------------------------------------------*/
2149 usb_fifo_put_data(struct usb_fifo *f, struct usb_page_cache *pc,
2150 usb_frlength_t offset, usb_frlength_t len, uint8_t what)
2153 usb_frlength_t io_len;
2155 while (len || (what == 1)) {
2157 USB_IF_DEQUEUE(&f->free_q, m);
2162 io_len = MIN(len, m->cur_data_len);
2164 usbd_copy_out(pc, offset, m->cur_data_ptr, io_len);
2166 m->cur_data_len = io_len;
2170 if ((len == 0) && (what == 1)) {
2173 USB_IF_ENQUEUE(&f->used_q, m);
2176 if ((len == 0) || (what == 1)) {
2186 usb_fifo_put_data_linear(struct usb_fifo *f, void *ptr,
2187 usb_size_t len, uint8_t what)
2192 while (len || (what == 1)) {
2194 USB_IF_DEQUEUE(&f->free_q, m);
2199 io_len = MIN(len, m->cur_data_len);
2201 memcpy(m->cur_data_ptr, ptr, io_len);
2203 m->cur_data_len = io_len;
2204 ptr = USB_ADD_BYTES(ptr, io_len);
2207 if ((len == 0) && (what == 1)) {
2210 USB_IF_ENQUEUE(&f->used_q, m);
2213 if ((len == 0) || (what == 1)) {
2223 usb_fifo_put_data_buffer(struct usb_fifo *f, void *ptr, usb_size_t len)
2227 USB_IF_DEQUEUE(&f->free_q, m);
2230 m->cur_data_len = len;
2231 m->cur_data_ptr = ptr;
2232 USB_IF_ENQUEUE(&f->used_q, m);
2240 usb_fifo_put_data_error(struct usb_fifo *f)
2242 f->flag_iserror = 1;
2246 /*------------------------------------------------------------------------*
2250 * 0 - normal operation
2251 * 1 - only get one "usb_mbuf"
2255 * 1 - data in buffer
2256 *------------------------------------------------------------------------*/
2258 usb_fifo_get_data(struct usb_fifo *f, struct usb_page_cache *pc,
2259 usb_frlength_t offset, usb_frlength_t len, usb_frlength_t *actlen,
2263 usb_frlength_t io_len;
2264 uint8_t tr_data = 0;
2270 USB_IF_DEQUEUE(&f->used_q, m);
2276 io_len = MIN(len, m->cur_data_len);
2278 usbd_copy_in(pc, offset, m->cur_data_ptr, io_len);
2282 actlen[0] += io_len;
2283 m->cur_data_ptr += io_len;
2284 m->cur_data_len -= io_len;
2286 if ((m->cur_data_len == 0) || (what == 1)) {
2287 USB_IF_ENQUEUE(&f->free_q, m);
2295 USB_IF_PREPEND(&f->used_q, m);
2301 /* wait for data to be written out */
2304 if (f->flag_flushing) {
2305 /* check if we should send a short packet */
2306 if (f->flag_short != 0) {
2311 /* flushing complete */
2312 f->flag_flushing = 0;
2325 usb_fifo_get_data_linear(struct usb_fifo *f, void *ptr,
2326 usb_size_t len, usb_size_t *actlen, uint8_t what)
2330 uint8_t tr_data = 0;
2336 USB_IF_DEQUEUE(&f->used_q, m);
2342 io_len = MIN(len, m->cur_data_len);
2344 memcpy(ptr, m->cur_data_ptr, io_len);
2347 ptr = USB_ADD_BYTES(ptr, io_len);
2348 actlen[0] += io_len;
2349 m->cur_data_ptr += io_len;
2350 m->cur_data_len -= io_len;
2352 if ((m->cur_data_len == 0) || (what == 1)) {
2353 USB_IF_ENQUEUE(&f->free_q, m);
2361 USB_IF_PREPEND(&f->used_q, m);
2367 /* wait for data to be written out */
2370 if (f->flag_flushing) {
2371 /* check if we should send a short packet */
2372 if (f->flag_short != 0) {
2377 /* flushing complete */
2378 f->flag_flushing = 0;
2391 usb_fifo_get_data_buffer(struct usb_fifo *f, void **pptr, usb_size_t *plen)
2395 USB_IF_POLL(&f->used_q, m);
2398 *plen = m->cur_data_len;
2399 *pptr = m->cur_data_ptr;
2407 usb_fifo_get_data_error(struct usb_fifo *f)
2409 f->flag_iserror = 1;
2413 /*------------------------------------------------------------------------*
2418 * Else: Pointer to symlink entry
2419 *------------------------------------------------------------------------*/
2420 struct usb_symlink *
2421 usb_alloc_symlink(const char *target)
2423 struct usb_symlink *ps;
2425 ps = kmalloc(sizeof(*ps), M_USBDEV, M_WAITOK);
2429 /* XXX no longer needed */
2430 strlcpy(ps->src_path, target, sizeof(ps->src_path));
2431 ps->src_len = strlen(ps->src_path);
2432 strlcpy(ps->dst_path, target, sizeof(ps->dst_path));
2433 ps->dst_len = strlen(ps->dst_path);
2435 lockmgr(&usb_sym_lock, LK_EXCLUSIVE);
2436 TAILQ_INSERT_TAIL(&usb_sym_head, ps, sym_entry);
2437 lockmgr(&usb_sym_lock, LK_RELEASE);
2441 /*------------------------------------------------------------------------*
2443 *------------------------------------------------------------------------*/
2445 usb_free_symlink(struct usb_symlink *ps)
2450 lockmgr(&usb_sym_lock, LK_EXCLUSIVE);
2451 TAILQ_REMOVE(&usb_sym_head, ps, sym_entry);
2452 lockmgr(&usb_sym_lock, LK_RELEASE);
2454 kfree(ps, M_USBDEV);
2457 /*------------------------------------------------------------------------*
2463 *------------------------------------------------------------------------*/
2465 usb_read_symlink(uint8_t *user_ptr, uint32_t startentry, uint32_t user_len)
2467 struct usb_symlink *ps;
2473 lockmgr(&usb_sym_lock, LK_EXCLUSIVE);
2475 TAILQ_FOREACH(ps, &usb_sym_head, sym_entry) {
2478 * Compute total length of source and destination symlink
2479 * strings pluss one length byte and two NUL bytes:
2481 temp = ps->src_len + ps->dst_len + 3;
2485 * Skip entry because this length cannot fit
2490 if (startentry != 0) {
2491 /* decrement read offset */
2495 if (temp > user_len) {
2496 /* out of buffer space */
2501 /* copy out total length */
2503 error = copyout(&len,
2504 USB_ADD_BYTES(user_ptr, delta), 1);
2510 /* copy out source string */
2512 error = copyout(ps->src_path,
2513 USB_ADD_BYTES(user_ptr, delta), ps->src_len);
2518 delta += ps->src_len;
2519 error = copyout(&len,
2520 USB_ADD_BYTES(user_ptr, delta), 1);
2526 /* copy out destination string */
2528 error = copyout(ps->dst_path,
2529 USB_ADD_BYTES(user_ptr, delta), ps->dst_len);
2534 delta += ps->dst_len;
2535 error = copyout(&len,
2536 USB_ADD_BYTES(user_ptr, delta), 1);
2545 /* a zero length entry indicates the end */
2547 if ((user_len != 0) && (error == 0)) {
2551 error = copyout(&len,
2552 USB_ADD_BYTES(user_ptr, delta), 1);
2554 lockmgr(&usb_sym_lock, LK_RELEASE);
2559 usb_fifo_set_close_zlp(struct usb_fifo *f, uint8_t onoff)
2564 /* send a Zero Length Packet, ZLP, before close */
2565 f->flag_short = onoff;
2569 usb_fifo_set_write_defrag(struct usb_fifo *f, uint8_t onoff)
2574 /* defrag written data */
2575 f->flag_write_defrag = onoff;
2576 /* reset defrag state */
2577 f->flag_have_fragment = 0;
2581 usb_fifo_softc(struct usb_fifo *f)
2583 return (f->priv_sc0);
2585 #endif /* USB_HAVE_UGEN */