1 /* $FreeBSD: head/sys/dev/usb/usb_dev.c 272480 2014-10-03 16:09:46Z hselasky $ */
3 * Copyright (c) 2006-2008 Hans Petter Selasky. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * usb_dev.c - An abstraction layer for creating devices under /dev/...
30 #include <sys/stdint.h>
31 #include <sys/param.h>
32 #include <sys/queue.h>
33 #include <sys/types.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
37 #include <sys/module.h>
39 #include <sys/condvar.h>
40 #include <sys/sysctl.h>
41 #include <sys/unistd.h>
42 #include <sys/callout.h>
43 #include <sys/malloc.h>
45 #include <sys/vnode.h>
47 #include <sys/fcntl.h>
48 #include <sys/devfs.h>
50 #include <bus/u4b/usb.h>
51 #include <bus/u4b/usb_ioctl.h>
52 #include <bus/u4b/usbdi.h>
53 #include <bus/u4b/usbdi_util.h>
55 #define USB_DEBUG_VAR usb_fifo_debug
57 #include <bus/u4b/usb_core.h>
58 #include <bus/u4b/usb_dev.h>
59 #include <bus/u4b/usb_mbuf.h>
60 #include <bus/u4b/usb_process.h>
61 #include <bus/u4b/usb_device.h>
62 #include <bus/u4b/usb_debug.h>
63 #include <bus/u4b/usb_busdma.h>
64 #include <bus/u4b/usb_generic.h>
65 #include <bus/u4b/usb_dynamic.h>
66 #include <bus/u4b/usb_util.h>
68 #include <bus/u4b/usb_controller.h>
69 #include <bus/u4b/usb_bus.h>
71 #include <sys/filio.h>
72 #include <sys/ttycom.h>
73 #include <sys/kern_syscall.h>
75 #include <machine/stdarg.h>
80 static int usb_fifo_debug
= 0;
82 static SYSCTL_NODE(_hw_usb
, OID_AUTO
, dev
, CTLFLAG_RW
, 0, "USB device");
83 SYSCTL_INT(_hw_usb_dev
, OID_AUTO
, debug
, CTLFLAG_RW
,
84 &usb_fifo_debug
, 0, "Debug Level");
86 TUNABLE_INT("hw.usb.dev.debug", &usb_fifo_debug
);
89 #define USB_UCRED struct ucred *ucred,
93 static int usb_fifo_open(struct usb_cdev_privdata
*,
94 struct usb_fifo
*, int);
95 static void usb_fifo_close(struct usb_fifo
*, int);
96 static void usb_dev_init(void *);
97 static void usb_dev_init_post(void *);
98 static void usb_dev_uninit(void *);
99 static int usb_fifo_uiomove(struct usb_fifo
*, void *, int,
101 static void usb_fifo_check_methods(struct usb_fifo_methods
*);
102 static struct usb_fifo
*usb_fifo_alloc(struct lock
*lock
);
103 static struct usb_endpoint
*usb_dev_get_ep(struct usb_device
*, uint8_t,
105 static void usb_loc_fill(struct usb_fs_privdata
*,
106 struct usb_cdev_privdata
*);
107 static usb_error_t
usb_ref_device(struct usb_cdev_privdata
*, struct usb_cdev_refdata
*, int);
108 static usb_error_t
usb_usb_ref_device(struct usb_cdev_privdata
*, struct usb_cdev_refdata
*);
109 static void usb_unref_device(struct usb_cdev_privdata
*, struct usb_cdev_refdata
*);
111 static void usb_filter_detach(struct knote
*kn
);
112 static int usb_filter_read(struct knote
*kn
, long hint
);
113 static int usb_filter_write(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_priv_dtor_t usb_cdevpriv_dtor
;
123 static d_ioctl_t usb_static_ioctl
;
124 static d_open_t usb_static_open
;
125 static d_close_t usb_static_close
;
127 static usb_fifo_open_t usb_fifo_dummy_open
;
128 static usb_fifo_close_t usb_fifo_dummy_close
;
129 static usb_fifo_ioctl_t usb_fifo_dummy_ioctl
;
130 static usb_fifo_cmd_t usb_fifo_dummy_cmd
;
132 /* character device structure used for devices (/dev/ugenX.Y and /dev/uXXX) */
133 struct dev_ops usb_ops
= {
134 { "usbdev", 0, D_MPSAFE
| D_MEM
},
136 .d_close
= usb_close
,
137 .d_ioctl
= usb_ioctl
,
139 .d_write
= usb_write
,
140 .d_kqfilter
= usb_kqfilter
143 static struct cdev
* usb_dev
= NULL
;
145 /* character device structure used for /bus/u4b */
146 static struct dev_ops usb_static_ops
= {
147 { "usb", 0, D_MPSAFE
| D_MEM
},
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
;
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
->state
== USB_STATE_DETACHED
&&
213 DPRINTFN(2, "device is detached\n");
217 DPRINTFN(2, "ref udev - needed\n");
219 if (cpd
->udev
->refcount
== USB_DEV_REF_MAX
) {
220 DPRINTFN(2, "no dev ref\n");
223 cpd
->udev
->refcount
++;
225 lockmgr(&usb_ref_lock
, LK_RELEASE
);
228 * We need to grab the sx-lock before grabbing the
229 * FIFO refs to avoid deadlock at detach!
231 crd
->do_unlock
= usbd_enum_lock(cpd
->udev
);
233 lockmgr(&usb_ref_lock
, LK_EXCLUSIVE
);
236 * Set "is_uref" after grabbing the default SX lock
241 /* check if we are doing an open */
242 if (cpd
->fflags
== 0) {
243 /* use zero defaults */
245 /* check for write */
246 if (cpd
->fflags
& FWRITE
) {
247 ppf
= cpd
->udev
->fifo
;
248 f
= ppf
[cpd
->fifo_index
+ USB_FIFO_TX
];
250 crd
->is_write
= 1; /* ref */
251 if (f
== NULL
|| f
->refcount
== USB_FIFO_REF_MAX
)
253 if (f
->curr_cpd
!= cpd
)
255 /* check if USB-FS is active */
256 if (f
->fs_ep_max
!= 0) {
262 if (cpd
->fflags
& FREAD
) {
263 ppf
= cpd
->udev
->fifo
;
264 f
= ppf
[cpd
->fifo_index
+ USB_FIFO_RX
];
266 crd
->is_read
= 1; /* ref */
267 if (f
== NULL
|| f
->refcount
== USB_FIFO_REF_MAX
)
269 if (f
->curr_cpd
!= cpd
)
271 /* check if USB-FS is active */
272 if (f
->fs_ep_max
!= 0) {
278 /* when everything is OK we increment the refcounts */
280 DPRINTFN(2, "ref write\n");
281 crd
->txfifo
->refcount
++;
284 DPRINTFN(2, "ref read\n");
285 crd
->rxfifo
->refcount
++;
287 lockmgr(&usb_ref_lock
, LK_RELEASE
);
293 usbd_enum_unlock(cpd
->udev
);
296 if (--(cpd
->udev
->refcount
) == 0)
297 cv_broadcast(&cpd
->udev
->ref_cv
);
299 lockmgr(&usb_ref_lock
, LK_RELEASE
);
300 DPRINTFN(2, "fail\n");
303 memset(crd
, 0, sizeof(*crd
));
305 return (USB_ERR_INVAL
);
308 /*------------------------------------------------------------------------*
311 * This function is used to upgrade an USB reference to include the
312 * USB device reference on a USB location.
315 * 0: Success, refcount incremented on the given USB device.
317 *------------------------------------------------------------------------*/
319 usb_usb_ref_device(struct usb_cdev_privdata
*cpd
,
320 struct usb_cdev_refdata
*crd
)
323 * Check if we already got an USB reference on this location:
326 return (0); /* success */
329 * To avoid deadlock at detach we need to drop the FIFO ref
330 * and re-acquire a new ref!
332 usb_unref_device(cpd
, crd
);
334 return (usb_ref_device(cpd
, crd
, 1 /* need uref */));
337 /*------------------------------------------------------------------------*
340 * This function will release the reference count by one unit for the
342 *------------------------------------------------------------------------*/
344 usb_unref_device(struct usb_cdev_privdata
*cpd
,
345 struct usb_cdev_refdata
*crd
)
348 DPRINTFN(2, "cpd=%p is_uref=%d\n", cpd
, crd
->is_uref
);
351 usbd_enum_unlock(cpd
->udev
);
353 lockmgr(&usb_ref_lock
, LK_EXCLUSIVE
);
355 if (--(crd
->rxfifo
->refcount
) == 0) {
356 cv_signal(&crd
->rxfifo
->cv_drain
);
361 if (--(crd
->txfifo
->refcount
) == 0) {
362 cv_signal(&crd
->txfifo
->cv_drain
);
368 if (--(cpd
->udev
->refcount
) == 0)
369 cv_broadcast(&cpd
->udev
->ref_cv
);
371 lockmgr(&usb_ref_lock
, LK_RELEASE
);
374 static struct usb_fifo
*
375 usb_fifo_alloc(struct lock
*lock
)
379 f
= kmalloc(sizeof(*f
), M_USBDEV
, M_WAITOK
| M_ZERO
);
381 cv_init(&f
->cv_io
, "FIFO-IO");
382 cv_init(&f
->cv_drain
, "FIFO-DRAIN");
385 /* mpf: knlist_init_mtx? the lock is used here in free */
390 /*------------------------------------------------------------------------*
392 *------------------------------------------------------------------------*/
394 usb_fifo_create(struct usb_cdev_privdata
*cpd
,
395 struct usb_cdev_refdata
*crd
)
397 struct usb_device
*udev
= cpd
->udev
;
399 struct usb_endpoint
*ep
;
405 int e
= cpd
->ep_addr
;
407 is_tx
= (cpd
->fflags
& FWRITE
) ? 1 : 0;
408 is_rx
= (cpd
->fflags
& FREAD
) ? 1 : 0;
412 /* Preallocated FIFO */
414 DPRINTFN(5, "Preallocated FIFO\n");
416 f
= udev
->fifo
[cpd
->fifo_index
+ USB_FIFO_TX
];
422 f
= udev
->fifo
[cpd
->fifo_index
+ USB_FIFO_RX
];
430 KASSERT(e
>= 0 && e
<= 15, ("endpoint %d out of range", e
));
432 /* search for a free FIFO slot */
433 DPRINTFN(5, "Endpoint device, searching for 0x%02x\n", e
);
434 for (n
= 0;; n
+= 2) {
436 if (n
== USB_FIFO_MAX
) {
441 /* end of FIFOs reached */
442 DPRINTFN(5, "out of FIFOs\n");
446 /* Check for TX FIFO */
448 f
= udev
->fifo
[n
+ USB_FIFO_TX
];
450 if (f
->dev_ep_index
!= e
) {
451 /* wrong endpoint index */
454 if (f
->curr_cpd
!= NULL
) {
459 } else if (no_null
) {
463 /* Check for RX FIFO */
465 f
= udev
->fifo
[n
+ USB_FIFO_RX
];
467 if (f
->dev_ep_index
!= e
) {
468 /* wrong endpoint index */
471 if (f
->curr_cpd
!= NULL
) {
476 } else if (no_null
) {
484 if (e
>= (USB_EP_MAX
/ 2)) {
485 /* we don't create any endpoints in this range */
486 DPRINTFN(5, "ep out of range\n");
487 return (is_busy
? EBUSY
: EINVAL
);
491 if ((e
!= 0) && is_busy
) {
493 * Only the default control endpoint is allowed to be
494 * opened multiple times!
496 DPRINTFN(5, "busy\n");
502 (udev
->fifo
[n
+ USB_FIFO_TX
] == NULL
)) {
503 ep
= usb_dev_get_ep(udev
, e
, USB_FIFO_TX
);
504 DPRINTFN(5, "dev_get_endpoint(%d, 0x%x)\n", e
, USB_FIFO_TX
);
506 DPRINTFN(5, "dev_get_endpoint returned NULL\n");
509 f
= usb_fifo_alloc(&udev
->device_lock
);
511 DPRINTFN(5, "could not alloc tx fifo\n");
514 /* update some fields */
515 f
->fifo_index
= n
+ USB_FIFO_TX
;
518 f
->methods
= &usb_ugen_methods
;
519 f
->iface_index
= ep
->iface_index
;
521 lockmgr(&usb_ref_lock
, LK_EXCLUSIVE
);
522 udev
->fifo
[n
+ USB_FIFO_TX
] = f
;
523 lockmgr(&usb_ref_lock
, LK_RELEASE
);
527 (udev
->fifo
[n
+ USB_FIFO_RX
] == NULL
)) {
529 ep
= usb_dev_get_ep(udev
, e
, USB_FIFO_RX
);
530 DPRINTFN(5, "dev_get_endpoint(%d, 0x%x)\n", e
, USB_FIFO_RX
);
532 DPRINTFN(5, "dev_get_endpoint returned NULL\n");
535 f
= usb_fifo_alloc(&udev
->device_lock
);
537 DPRINTFN(5, "could not alloc rx fifo\n");
540 /* update some fields */
541 f
->fifo_index
= n
+ USB_FIFO_RX
;
544 f
->methods
= &usb_ugen_methods
;
545 f
->iface_index
= ep
->iface_index
;
547 lockmgr(&usb_ref_lock
, LK_EXCLUSIVE
);
548 udev
->fifo
[n
+ USB_FIFO_RX
] = f
;
549 lockmgr(&usb_ref_lock
, LK_RELEASE
);
552 crd
->txfifo
= udev
->fifo
[n
+ USB_FIFO_TX
];
555 crd
->rxfifo
= udev
->fifo
[n
+ USB_FIFO_RX
];
557 /* fill out fifo index */
558 DPRINTFN(5, "fifo index = %d\n", n
);
567 usb_fifo_free(struct usb_fifo
*f
)
575 /* destroy symlink devices, if any */
576 for (n
= 0; n
!= 2; n
++) {
578 usb_free_symlink(f
->symlink
[n
]);
579 f
->symlink
[n
] = NULL
;
582 lockmgr(&usb_ref_lock
, LK_EXCLUSIVE
);
584 /* delink ourselves to stop calls from userland */
585 if ((f
->fifo_index
< USB_FIFO_MAX
) &&
587 (f
->udev
->fifo
[f
->fifo_index
] == f
)) {
588 f
->udev
->fifo
[f
->fifo_index
] = NULL
;
590 DPRINTFN(0, "USB FIFO %p has not been linked\n", f
);
593 /* decrease refcount */
595 /* need to wait until all callers have exited */
596 while (f
->refcount
!= 0) {
597 lockmgr(&usb_ref_lock
, LK_RELEASE
); /* avoid LOR */
598 lockmgr(f
->priv_lock
, LK_EXCLUSIVE
);
599 /* prevent write flush, if any */
601 /* get I/O thread out of any sleep state */
602 if (f
->flag_sleeping
) {
603 f
->flag_sleeping
= 0;
604 cv_broadcast(&f
->cv_io
);
606 lockmgr(f
->priv_lock
, LK_RELEASE
);
607 lockmgr(&usb_ref_lock
, LK_EXCLUSIVE
);
610 * Check if the "f->refcount" variable reached zero
611 * during the unlocked time before entering wait:
613 if (f
->refcount
== 0)
617 cv_wait(&f
->cv_drain
, &usb_ref_lock
);
619 lockmgr(&usb_ref_lock
, LK_RELEASE
);
621 /* take care of closing the device here, if any */
622 usb_fifo_close(f
, 0);
624 cv_destroy(&f
->cv_io
);
625 cv_destroy(&f
->cv_drain
);
628 knlist_clear(&f
->selinfo
.si_note
, 0);
629 seldrain(&f
->selinfo
);
630 knlist_destroy(&f
->selinfo
.si_note
);
635 static struct usb_endpoint
*
636 usb_dev_get_ep(struct usb_device
*udev
, uint8_t ep_index
, uint8_t dir
)
638 struct usb_endpoint
*ep
;
644 if (dir
== USB_FIFO_RX
) {
645 if (udev
->flags
.usb_mode
== USB_MODE_HOST
) {
651 if (udev
->flags
.usb_mode
== USB_MODE_HOST
) {
657 ep
= usbd_get_ep_by_addr(udev
, ep_index
| ep_dir
);
661 /* if the endpoint does not exist then return */
664 if (ep
->edesc
== NULL
) {
665 /* invalid endpoint */
668 return (ep
); /* success */
671 /*------------------------------------------------------------------------*
677 *------------------------------------------------------------------------*/
679 usb_fifo_open(struct usb_cdev_privdata
*cpd
,
680 struct usb_fifo
*f
, int fflags
)
686 DPRINTFN(2, "no FIFO\n");
689 /* remove FWRITE and FREAD flags */
690 fflags
&= ~(FWRITE
| FREAD
);
692 /* set correct file flags */
693 if ((f
->fifo_index
& 1) == USB_FIFO_TX
) {
699 /* check if we are already opened */
700 /* we don't need any locks when checking this variable */
701 if (f
->curr_cpd
!= NULL
) {
706 /* reset short flag before open */
709 /* call open method */
710 err
= (f
->methods
->f_open
) (f
, fflags
);
714 lockmgr(f
->priv_lock
, LK_EXCLUSIVE
);
716 /* reset sleep flag */
717 f
->flag_sleeping
= 0;
719 /* reset error flag */
722 /* reset complete flag */
723 f
->flag_iscomplete
= 0;
725 /* reset select flag */
726 f
->flag_isselect
= 0;
728 /* reset flushing flag */
729 f
->flag_flushing
= 0;
731 /* reset ASYNC proc flag */
734 lockmgr(&usb_ref_lock
, LK_EXCLUSIVE
);
735 /* flag the fifo as opened to prevent others */
737 lockmgr(&usb_ref_lock
, LK_RELEASE
);
742 lockmgr(f
->priv_lock
, LK_RELEASE
);
747 /*------------------------------------------------------------------------*
749 *------------------------------------------------------------------------*/
751 usb_fifo_reset(struct usb_fifo
*f
)
759 USB_IF_DEQUEUE(&f
->used_q
, m
);
761 USB_IF_ENQUEUE(&f
->free_q
, m
);
766 /* reset have fragment flag */
767 f
->flag_have_fragment
= 0;
770 /*------------------------------------------------------------------------*
772 *------------------------------------------------------------------------*/
774 usb_fifo_close(struct usb_fifo
*f
, int fflags
)
778 /* check if we are not opened */
779 if (f
->curr_cpd
== NULL
) {
780 /* nothing to do - already closed */
783 lockmgr(f
->priv_lock
, LK_EXCLUSIVE
);
785 /* clear current cdev private data pointer */
786 lockmgr(&usb_ref_lock
, LK_EXCLUSIVE
);
788 lockmgr(&usb_ref_lock
, LK_RELEASE
);
790 /* check if we are selected */
791 if (f
->flag_isselect
) {
792 KNOTE(&f
->selinfo
.ki_note
, 0);
793 wakeup(&f
->selinfo
.ki_note
);
795 f
->flag_isselect
= 0;
797 /* check if a thread wants SIGIO */
798 if (f
->async_p
!= NULL
&& lwkt_trytoken(&f
->async_p
->p_token
)) {
799 ksignal(f
->async_p
, SIGIO
);
800 lwkt_reltoken(&f
->async_p
->p_token
);
803 /* remove FWRITE and FREAD flags */
804 fflags
&= ~(FWRITE
| FREAD
);
806 /* flush written data, if any */
807 if ((f
->fifo_index
& 1) == USB_FIFO_TX
) {
809 if (!f
->flag_iserror
) {
811 /* set flushing flag */
812 f
->flag_flushing
= 1;
814 /* get the last packet in */
815 if (f
->flag_have_fragment
) {
817 f
->flag_have_fragment
= 0;
818 USB_IF_DEQUEUE(&f
->free_q
, m
);
820 USB_IF_ENQUEUE(&f
->used_q
, m
);
824 /* start write transfer, if not already started */
825 (f
->methods
->f_start_write
) (f
);
827 /* check if flushed already */
828 while (f
->flag_flushing
&&
829 (!f
->flag_iserror
)) {
830 /* wait until all data has been written */
831 f
->flag_sleeping
= 1;
832 err
= cv_wait_sig(&f
->cv_io
, f
->priv_lock
);
834 DPRINTF("signal received\n");
841 /* stop write transfer, if not already stopped */
842 (f
->methods
->f_stop_write
) (f
);
846 /* stop write transfer, if not already stopped */
847 (f
->methods
->f_stop_read
) (f
);
850 /* check if we are sleeping */
851 if (f
->flag_sleeping
) {
852 DPRINTFN(2, "Sleeping at close!\n");
854 lockmgr(f
->priv_lock
, LK_RELEASE
);
856 /* call close method */
857 (f
->methods
->f_close
) (f
, fflags
);
862 /*------------------------------------------------------------------------*
863 * usb_open - cdev callback
864 *------------------------------------------------------------------------*/
866 usb_open(struct dev_open_args
*ap
)
868 struct cdev
*dev
= ap
->a_head
.a_dev
;
869 int fflags
= ap
->a_oflags
;
870 struct usb_fs_privdata
* pd
= (struct usb_fs_privdata
*)dev
->si_drv1
;
871 struct usb_cdev_refdata refs
;
872 struct usb_cdev_privdata
*cpd
;
876 DPRINTFN(2, "%s fflags=0x%08x\n", devtoname(dev
), fflags
);
878 KASSERT(fflags
& (FREAD
|FWRITE
), ("invalid open flags"));
879 if (((fflags
& FREAD
) && !(pd
->mode
& FREAD
)) ||
880 ((fflags
& FWRITE
) && !(pd
->mode
& FWRITE
))) {
881 DPRINTFN(2, "access mode not supported\n");
885 cpd
= kmalloc(sizeof(*cpd
), M_USBDEV
, M_WAITOK
| M_ZERO
);
886 ep
= cpd
->ep_addr
= pd
->ep_addr
;
888 usb_loc_fill(pd
, cpd
);
889 err
= usb_ref_device(cpd
, &refs
, 1);
891 DPRINTFN(2, "cannot ref device\n");
892 kfree(cpd
, M_USBDEV
);
895 cpd
->fflags
= fflags
; /* access mode for open lifetime */
897 /* create FIFOs, if any */
898 err
= usb_fifo_create(cpd
, &refs
);
899 /* check for error */
901 DPRINTFN(2, "cannot create fifo\n");
902 usb_unref_device(cpd
, &refs
);
903 kfree(cpd
, M_USBDEV
);
906 if (fflags
& FREAD
) {
907 err
= usb_fifo_open(cpd
, refs
.rxfifo
, fflags
);
909 DPRINTFN(2, "read open failed\n");
910 usb_unref_device(cpd
, &refs
);
911 kfree(cpd
, M_USBDEV
);
915 if (fflags
& FWRITE
) {
916 err
= usb_fifo_open(cpd
, refs
.txfifo
, fflags
);
918 DPRINTFN(2, "write open failed\n");
919 if (fflags
& FREAD
) {
920 usb_fifo_close(refs
.rxfifo
, fflags
);
922 usb_unref_device(cpd
, &refs
);
923 kfree(cpd
, M_USBDEV
);
927 usb_unref_device(cpd
, &refs
);
928 fp
= ap
->a_fpp
? *ap
->a_fpp
: NULL
;
929 err
= devfs_set_cdevpriv(fp
, cpd
, &usb_cdevpriv_dtor
);
930 DPRINTFN(2, "fp=%p cpd=%p\n", (ap
->a_fpp
? *ap
->a_fpp
: NULL
), cpd
);
932 DPRINTFN(2, "devfs_set_cdevpriv failed in %s\n", __func__
);
933 kfree(cpd
, M_USBDEV
);
944 usb_close(struct dev_close_args
*ap
)
946 DPRINTFN(2, "usb_close called\n");
950 /*------------------------------------------------------------------------*
951 * usb_close - cdev callback
952 *------------------------------------------------------------------------*/
954 usb_cdevpriv_dtor(void *cd
)
956 struct usb_cdev_privdata
*cpd
= (struct usb_cdev_privdata
*)cd
;
957 struct usb_cdev_refdata refs
;
960 DPRINTF("dtor called on %p\n", cpd
);
962 err
= usb_ref_device(cpd
, &refs
, 2);
964 DPRINTFN(0, "Cannot grab USB reference when "
965 "closing USB file handle\n");
968 if (cpd
->fflags
& FREAD
) {
969 usb_fifo_close(refs
.rxfifo
, cpd
->fflags
);
971 if (cpd
->fflags
& FWRITE
) {
972 usb_fifo_close(refs
.txfifo
, cpd
->fflags
);
974 usb_unref_device(cpd
, &refs
);
976 kfree(cpd
, M_USBDEV
);
980 usb_dev_init(void *arg
)
982 lockinit(&usb_ref_lock
, "USB ref mutex", 0, 0);
983 lockinit(&usb_sym_lock
, "USB sym mutex", 0, 0);
984 TAILQ_INIT(&usb_sym_head
);
986 /* check the UGEN methods */
987 usb_fifo_check_methods(&usb_ugen_methods
);
990 /* XXX SI_SUB_KLD? */
991 SYSINIT(usb_dev_init
, SI_SUB_PRE_DRIVERS
, SI_ORDER_FIRST
, usb_dev_init
, NULL
);
994 usb_dev_init_post(void *arg
)
997 * Create /dev/usb - this is needed for usbconfig(8), which
998 * needs a well-known device name to access.
1000 usb_dev
= make_dev(&usb_static_ops
, 0, UID_ROOT
, GID_OPERATOR
,
1001 0644, USB_DEVICE_NAME
);
1002 if (usb_dev
== NULL
) {
1003 DPRINTFN(0, "Could not create usb bus device\n");
1007 SYSINIT(usb_dev_init_post
, SI_SUB_DRIVERS
, SI_ORDER_FIRST
, usb_dev_init_post
,
1011 usb_dev_uninit(void *arg
)
1013 if (usb_dev
!= NULL
) {
1014 destroy_dev(usb_dev
);
1017 lockuninit(&usb_ref_lock
);
1018 lockuninit(&usb_sym_lock
);
1021 SYSUNINIT(usb_dev_uninit
, SI_SUB_KICK_SCHEDULER
, SI_ORDER_ANY
, usb_dev_uninit
, NULL
);
1024 usb_ioctl_f_sub(struct usb_fifo
*f
, u_long cmd
, void *addr
,
1031 *(int *)addr
= 0; /* character device */
1035 /* handled by upper FS layer */
1040 if (f
->async_p
!= NULL
) {
1044 f
->async_p
= USB_TD_GET_PROC(td
);
1050 /* XXX this is not the most general solution */
1052 if (f
->async_p
== NULL
) {
1056 if (*(int *)addr
!= USB_PROC_GET_GID(f
->async_p
)) {
1064 DPRINTFN(3, "cmd 0x%lx = %d\n", cmd
, error
);
1068 /*------------------------------------------------------------------------*
1069 * usb_ioctl - cdev callback
1070 *------------------------------------------------------------------------*/
1072 usb_ioctl(struct dev_ioctl_args
*ap
)
1074 u_long cmd
= ap
->a_cmd
;
1075 caddr_t addr
= ap
->a_data
;
1076 struct thread
*td
= curthread
;
1077 struct usb_cdev_refdata refs
;
1078 struct usb_cdev_privdata
* cpd
;
1083 DPRINTFN(2, "cmd=0x%lx\n", cmd
);
1085 err
= devfs_get_cdevpriv(ap
->a_fp
, (void **)&cpd
);
1090 * Performance optimisation: We try to check for IOCTL's that
1091 * don't need the USB reference first. Then we grab the USB
1092 * reference if we need it!
1094 err
= usb_ref_device(cpd
, &refs
, 0 /* no uref */ );
1098 fflags
= cpd
->fflags
;
1100 f
= NULL
; /* set default value */
1101 err
= ENOIOCTL
; /* set default value */
1103 if (fflags
& FWRITE
) {
1105 err
= usb_ioctl_f_sub(f
, cmd
, addr
, td
);
1107 if (fflags
& FREAD
) {
1109 err
= usb_ioctl_f_sub(f
, cmd
, addr
, td
);
1111 KASSERT(f
!= NULL
, ("fifo not found"));
1112 if (err
!= ENOIOCTL
)
1115 err
= (f
->methods
->f_ioctl
) (f
, cmd
, addr
, fflags
);
1117 DPRINTFN(2, "f_ioctl cmd 0x%lx = %d\n", cmd
, err
);
1119 if (err
!= ENOIOCTL
)
1122 if (usb_usb_ref_device(cpd
, &refs
)) {
1127 err
= (f
->methods
->f_ioctl_post
) (f
, cmd
, addr
, fflags
);
1129 DPRINTFN(2, "f_ioctl_post cmd 0x%lx = %d\n", cmd
, err
);
1131 if (err
== ENOIOCTL
)
1137 /* Wait for re-enumeration, if any */
1139 while (f
->udev
->re_enumerate_wait
!= USB_RE_ENUM_DONE
) {
1141 usb_unref_device(cpd
, &refs
);
1143 usb_pause_mtx(NULL
, hz
/ 128);
1145 if (usb_ref_device(cpd
, &refs
, 1 /* need uref */)) {
1152 usb_unref_device(cpd
, &refs
);
1156 static struct filterops usb_filtops_read
=
1157 { FILTEROP_ISFD
| FILTEROP_MPSAFE
, NULL
, usb_filter_detach
, usb_filter_read
};
1159 static struct filterops usb_filtops_write
=
1160 { FILTEROP_ISFD
| FILTEROP_MPSAFE
, NULL
, usb_filter_detach
, usb_filter_write
};
1163 usb_kqfilter(struct dev_kqfilter_args
*ap
)
1165 struct knote
*kn
= ap
->a_kn
;
1166 struct klist
*klist
;
1168 struct usb_cdev_refdata refs
;
1169 struct usb_cdev_privdata
* cpd
;
1172 err
= devfs_get_cdevpriv(ap
->a_fp
, (void **)&cpd
);
1175 err
= usb_ref_device(cpd
, &refs
, 0 /* no uref */ );
1180 fflags
= cpd
->fflags
;
1182 switch(kn
->kn_filter
) {
1185 if(fflags
& FREAD
) {
1186 lockmgr(f
->priv_lock
, LK_EXCLUSIVE
);
1187 f
->flag_isselect
= 1;
1188 lockmgr(f
->priv_lock
, LK_RELEASE
);
1189 kn
->kn_fop
= &usb_filtops_read
;
1191 ap
->a_result
= EOPNOTSUPP
;
1197 if(fflags
& FWRITE
) {
1198 lockmgr(f
->priv_lock
, LK_EXCLUSIVE
);
1199 f
->flag_isselect
= 1;
1200 lockmgr(f
->priv_lock
, LK_RELEASE
);
1201 kn
->kn_fop
= &usb_filtops_write
;
1203 ap
->a_result
= EOPNOTSUPP
;
1208 DPRINTF("unsupported kqfilter requested\n");
1209 ap
->a_result
= EOPNOTSUPP
;
1210 usb_unref_device(cpd
, &refs
);
1214 kn
->kn_hook
= (caddr_t
)cpd
;
1215 klist
= &f
->selinfo
.ki_note
;
1216 knote_insert(klist
, kn
);
1218 usb_unref_device(cpd
, &refs
);
1223 usb_filter_detach(struct knote
*kn
)
1226 struct usb_cdev_privdata
* cpd
= (struct usb_cdev_privdata
*)kn
->kn_hook
;
1227 struct usb_cdev_refdata refs
;
1228 struct klist
*klist
;
1233 * The associated cpd has vanished.
1239 err
= usb_ref_device(cpd
, &refs
, 0 /* no uref */ );
1244 switch(kn
->kn_filter
) {
1252 /* Better safe than sorry? (mpf) */
1253 panic("Trying to detach unknown filter");
1257 lockmgr(f
->priv_lock
, LK_EXCLUSIVE
);
1259 /* removed check for f->flag_isselect, because
1260 it is racing completion in the filter leading
1261 to invalid data in the fifo knote list */
1262 klist
= &f
->selinfo
.ki_note
;
1263 knote_remove(klist
, kn
);
1264 f
->flag_isselect
= 0;
1266 lockmgr(f
->priv_lock
, LK_RELEASE
);
1268 usb_unref_device(cpd
, &refs
);
1272 usb_filter_read(struct knote
*kn
, long hint
)
1275 struct usb_cdev_privdata
*cpd
= (struct usb_cdev_privdata
*)kn
->kn_hook
;
1276 struct usb_cdev_refdata refs
;
1278 int err
,locked
,ready
= 0;
1283 * The associated file has been closed.
1286 kn
->kn_flags
|= EV_ERROR
;
1290 err
= usb_ref_device(cpd
, &refs
, 0 /* no uref */ );
1292 kn
->kn_flags
|= EV_ERROR
;
1296 For some reason this function is called both
1297 with the priv_lock held and with the priv_lock
1298 not held. We need to find out from where and
1302 locked
= lockowned(f
->priv_lock
);
1304 lockmgr(f
->priv_lock
, LK_EXCLUSIVE
);
1306 if (!refs
.is_usbfs
) {
1307 if (f
->flag_iserror
) {
1308 /* we got an error */
1309 kn
->kn_flags
|= EV_ERROR
;
1312 /* start read if not running */
1313 (f
->methods
->f_start_read
)(f
);
1314 /* check if any packets are available */
1315 USB_IF_POLL(&f
->used_q
, m
);
1321 if (f
->flag_iscomplete
) {
1329 lockmgr(f
->priv_lock
, LK_RELEASE
);
1331 usb_unref_device(cpd
, &refs
);
1333 DPRINTFN(3,"ready %d\n", ready
);
1338 usb_filter_write(struct knote
*kn
, long hint
)
1341 struct usb_cdev_privdata
*cpd
= (struct usb_cdev_privdata
*)kn
->kn_hook
;
1342 struct usb_cdev_refdata refs
;
1344 int err
,locked
,ready
= 0;
1349 * The associated file has been closed.
1352 kn
->kn_flags
|= EV_ERROR
;
1356 err
= usb_ref_device(cpd
, &refs
, 0 /* no uref */ );
1358 kn
->kn_flags
|= EV_ERROR
;
1362 For some reason this function is called both
1363 with the priv_lock held and with the priv_lock
1364 not held. We need to find out from where and
1368 locked
= lockowned(f
->priv_lock
);
1370 lockmgr(f
->priv_lock
, LK_EXCLUSIVE
);
1372 if (!refs
.is_usbfs
) {
1373 if (f
->flag_iserror
) {
1374 /* we got an error */
1375 kn
->kn_flags
|= EV_ERROR
;
1378 if (f
->queue_data
== NULL
) {
1380 * start write transfer, if not
1383 (f
->methods
->f_start_write
) (f
);
1385 /* check if any packets are available */
1386 USB_IF_POLL(&f
->free_q
, m
);
1391 if (f
->flag_iscomplete
) {
1399 lockmgr(f
->priv_lock
, LK_RELEASE
);
1401 usb_unref_device(cpd
, &refs
);
1403 DPRINTFN(3,"ready %d\n", ready
);
1408 /* This is implemented above using kqfilter */
1411 usb_poll(struct cdev
* dev
, int events
, struct thread
* td
)
1413 struct usb_cdev_refdata refs
;
1414 struct usb_cdev_privdata
* cpd
;
1417 int fflags
, revents
;
1419 if (devfs_get_cdevpriv((void **)&cpd
) != 0 ||
1420 usb_ref_device(cpd
, &refs
, 0) != 0)
1422 (POLLHUP
|POLLIN
|POLLRDNORM
|POLLOUT
|POLLWRNORM
));
1424 fflags
= cpd
->fflags
;
1426 /* Figure out who needs service */
1428 if ((events
& (POLLOUT
| POLLWRNORM
)) &&
1429 (fflags
& FWRITE
)) {
1433 lockmgr(f
->priv_lock
, LK_EXCLUSIVE
);
1435 if (!refs
.is_usbfs
) {
1436 if (f
->flag_iserror
) {
1437 /* we got an error */
1440 if (f
->queue_data
== NULL
) {
1442 * start write transfer, if not
1445 (f
->methods
->f_start_write
) (f
);
1447 /* check if any packets are available */
1448 USB_IF_POLL(&f
->free_q
, m
);
1451 if (f
->flag_iscomplete
) {
1459 revents
|= events
& (POLLOUT
| POLLWRNORM
);
1461 f
->flag_isselect
= 1;
1462 selrecord(td
, &f
->selinfo
);
1465 lockmgr(f
->priv_lock
);
1467 if ((events
& (POLLIN
| POLLRDNORM
)) &&
1472 lockmgr(f
->priv_lock
, LK_EXCLUSIVE
);
1474 if (!refs
.is_usbfs
) {
1475 if (f
->flag_iserror
) {
1476 /* we have and error */
1479 if (f
->queue_data
== NULL
) {
1481 * start read transfer, if not
1484 (f
->methods
->f_start_read
) (f
);
1486 /* check if any packets are available */
1487 USB_IF_POLL(&f
->used_q
, m
);
1490 if (f
->flag_iscomplete
) {
1498 revents
|= events
& (POLLIN
| POLLRDNORM
);
1500 f
->flag_isselect
= 1;
1501 selrecord(td
, &f
->selinfo
);
1503 if (!refs
.is_usbfs
) {
1504 /* start reading data */
1505 (f
->methods
->f_start_read
) (f
);
1509 lockmgr(f
->priv_lock
, LK_RELEASE
);
1511 usb_unref_device(cpd
, &refs
);
1517 usb_read(struct dev_read_args
*ap
)
1519 struct uio
*uio
= ap
->a_uio
;
1520 int ioflag
= ap
->a_ioflag
;
1521 struct usb_cdev_refdata refs
;
1522 struct usb_cdev_privdata
* cpd
;
1529 uint8_t tr_data
= 0;
1531 err
= devfs_get_cdevpriv(ap
->a_fp
, (void **)&cpd
);
1534 err
= usb_ref_device(cpd
, &refs
, 0 /* no uref */ );
1538 fflags
= cpd
->fflags
;
1542 /* should not happen */
1543 usb_unref_device(cpd
, &refs
);
1547 resid
= uio
->uio_resid
;
1549 lockmgr(f
->priv_lock
, LK_EXCLUSIVE
);
1551 /* check for permanent read error */
1552 if (f
->flag_iserror
) {
1556 /* check if USB-FS interface is active */
1557 if (refs
.is_usbfs
) {
1559 * The queue is used for events that should be
1560 * retrieved using the "USB_FS_COMPLETE" ioctl.
1565 while (uio
->uio_resid
> 0) {
1567 USB_IF_DEQUEUE(&f
->used_q
, m
);
1571 /* start read transfer, if not already started */
1573 (f
->methods
->f_start_read
) (f
);
1575 if (ioflag
& IO_NDELAY
) {
1577 /* return length before error */
1583 DPRINTF("sleeping\n");
1585 err
= usb_fifo_wait(f
);
1591 if (f
->methods
->f_filter_read
) {
1593 * Sometimes it is convenient to process data at the
1594 * expense of a userland process instead of a kernel
1597 (f
->methods
->f_filter_read
) (f
, m
);
1601 io_len
= MIN(m
->cur_data_len
, uio
->uio_resid
);
1603 DPRINTFN(2, "transfer %d bytes from %p\n",
1604 io_len
, m
->cur_data_ptr
);
1606 err
= usb_fifo_uiomove(f
,
1607 m
->cur_data_ptr
, io_len
, uio
);
1609 m
->cur_data_len
-= io_len
;
1610 m
->cur_data_ptr
+= io_len
;
1612 if (m
->cur_data_len
== 0) {
1614 uint8_t last_packet
;
1616 last_packet
= m
->last_packet
;
1618 USB_IF_ENQUEUE(&f
->free_q
, m
);
1625 USB_IF_PREPEND(&f
->used_q
, m
);
1634 lockmgr(f
->priv_lock
, LK_RELEASE
);
1636 usb_unref_device(cpd
, &refs
);
1642 usb_write(struct dev_write_args
*ap
)
1644 struct uio
*uio
= ap
->a_uio
;
1645 int ioflag
= ap
->a_ioflag
;
1646 struct usb_cdev_refdata refs
;
1647 struct usb_cdev_privdata
* cpd
;
1655 uint8_t tr_data
= 0;
1659 err
= devfs_get_cdevpriv(ap
->a_fp
, (void **)&cpd
);
1662 err
= usb_ref_device(cpd
, &refs
, 0 /* no uref */ );
1666 fflags
= cpd
->fflags
;
1670 /* should not happen */
1671 usb_unref_device(cpd
, &refs
);
1674 resid
= uio
->uio_resid
;
1676 lockmgr(f
->priv_lock
, LK_EXCLUSIVE
);
1678 /* check for permanent write error */
1679 if (f
->flag_iserror
) {
1683 /* check if USB-FS interface is active */
1684 if (refs
.is_usbfs
) {
1686 * The queue is used for events that should be
1687 * retrieved using the "USB_FS_COMPLETE" ioctl.
1692 if (f
->queue_data
== NULL
) {
1693 /* start write transfer, if not already started */
1694 (f
->methods
->f_start_write
) (f
);
1696 /* we allow writing zero length data */
1698 USB_IF_DEQUEUE(&f
->free_q
, m
);
1702 if (ioflag
& IO_NDELAY
) {
1704 /* return length before error */
1710 DPRINTF("sleeping\n");
1712 err
= usb_fifo_wait(f
);
1720 if (f
->flag_have_fragment
== 0) {
1722 io_len
= m
->cur_data_len
;
1723 pdata
= m
->cur_data_ptr
;
1724 if (io_len
> uio
->uio_resid
)
1725 io_len
= uio
->uio_resid
;
1726 m
->cur_data_len
= io_len
;
1728 io_len
= m
->max_data_len
- m
->cur_data_len
;
1729 pdata
= m
->cur_data_ptr
+ m
->cur_data_len
;
1730 if (io_len
> uio
->uio_resid
)
1731 io_len
= uio
->uio_resid
;
1732 m
->cur_data_len
+= io_len
;
1735 DPRINTFN(2, "transfer %d bytes to %p\n",
1738 err
= usb_fifo_uiomove(f
, pdata
, io_len
, uio
);
1741 f
->flag_have_fragment
= 0;
1742 USB_IF_ENQUEUE(&f
->free_q
, m
);
1746 /* check if the buffer is ready to be transmitted */
1748 if ((f
->flag_write_defrag
== 0) ||
1749 (m
->cur_data_len
== m
->max_data_len
)) {
1750 f
->flag_have_fragment
= 0;
1753 * Check for write filter:
1755 * Sometimes it is convenient to process data
1756 * at the expense of a userland process
1757 * instead of a kernel process.
1759 if (f
->methods
->f_filter_write
) {
1760 (f
->methods
->f_filter_write
) (f
, m
);
1763 /* Put USB mbuf in the used queue */
1764 USB_IF_ENQUEUE(&f
->used_q
, m
);
1766 /* Start writing data, if not already started */
1767 (f
->methods
->f_start_write
) (f
);
1769 /* Wait for more data or close */
1770 f
->flag_have_fragment
= 1;
1771 USB_IF_PREPEND(&f
->free_q
, m
);
1774 } while (uio
->uio_resid
> 0);
1776 lockmgr(f
->priv_lock
, LK_RELEASE
);
1778 usb_unref_device(cpd
, &refs
);
1784 usb_static_open(struct dev_open_args
*ap
)
1790 usb_static_close(struct dev_close_args
*ap
)
1796 usb_static_ioctl(struct dev_ioctl_args
*ap
)
1798 u_long cmd
= ap
->a_cmd
;
1799 caddr_t data
= ap
->a_data
;
1800 struct thread
*td
= curthread
; /* XXX: curthread the correct choice? */
1801 int fflag
= ap
->a_fflag
;
1803 struct usb_read_dir
*urd
;
1811 err
= usb_read_symlink(u
.urd
->urd_data
,
1812 u
.urd
->urd_startentry
, u
.urd
->urd_maxlen
);
1814 case USB_DEV_QUIRK_GET
:
1815 case USB_QUIRK_NAME_GET
:
1816 case USB_DEV_QUIRK_ADD
:
1817 case USB_DEV_QUIRK_REMOVE
:
1818 err
= usb_quirk_ioctl_p(cmd
, data
, fflag
, td
);
1820 case USB_GET_TEMPLATE
:
1821 *(int *)data
= usb_template
;
1824 case USB_SET_TEMPLATE
:
1825 err
= caps_priv_check_self(SYSCAP_NODRIVER
);
1828 usb_template
= *(int *)data
;
1838 usb_fifo_uiomove(struct usb_fifo
*f
, void *cp
,
1839 int n
, struct uio
*uio
)
1843 lockmgr(f
->priv_lock
, LK_RELEASE
);
1846 * "uiomove()" can sleep so one needs to make a wrapper,
1847 * exiting the mutex and checking things:
1849 error
= uiomove(cp
, n
, uio
);
1851 lockmgr(f
->priv_lock
, LK_EXCLUSIVE
);
1857 usb_fifo_wait(struct usb_fifo
*f
)
1861 KKASSERT(lockowned(f
->priv_lock
));
1863 if (f
->flag_iserror
) {
1867 f
->flag_sleeping
= 1;
1869 err
= cv_wait_sig(&f
->cv_io
, f
->priv_lock
);
1871 if (f
->flag_iserror
) {
1879 usb_fifo_signal(struct usb_fifo
*f
)
1881 if (f
->flag_sleeping
) {
1882 f
->flag_sleeping
= 0;
1883 cv_broadcast(&f
->cv_io
);
1888 usb_fifo_wakeup(struct usb_fifo
*f
)
1892 KNOTE(&f
->selinfo
.ki_note
, 0);
1894 if (f
->flag_isselect
) {
1895 wakeup(&f
->selinfo
.ki_note
);
1897 if (f
->async_p
!= NULL
&& lwkt_trytoken(&f
->async_p
->p_token
)) {
1898 ksignal(f
->async_p
, SIGIO
);
1899 lwkt_reltoken(&f
->async_p
->p_token
);
1904 usb_fifo_dummy_open(struct usb_fifo
*fifo
, int fflags
)
1910 usb_fifo_dummy_close(struct usb_fifo
*fifo
, int fflags
)
1916 usb_fifo_dummy_ioctl(struct usb_fifo
*fifo
, u_long cmd
, void *addr
, int fflags
)
1922 usb_fifo_dummy_cmd(struct usb_fifo
*fifo
)
1924 fifo
->flag_flushing
= 0; /* not flushing */
1928 usb_fifo_check_methods(struct usb_fifo_methods
*pm
)
1930 /* check that all callback functions are OK */
1932 if (pm
->f_open
== NULL
)
1933 pm
->f_open
= &usb_fifo_dummy_open
;
1935 if (pm
->f_close
== NULL
)
1936 pm
->f_close
= &usb_fifo_dummy_close
;
1938 if (pm
->f_ioctl
== NULL
)
1939 pm
->f_ioctl
= &usb_fifo_dummy_ioctl
;
1941 if (pm
->f_ioctl_post
== NULL
)
1942 pm
->f_ioctl_post
= &usb_fifo_dummy_ioctl
;
1944 if (pm
->f_start_read
== NULL
)
1945 pm
->f_start_read
= &usb_fifo_dummy_cmd
;
1947 if (pm
->f_stop_read
== NULL
)
1948 pm
->f_stop_read
= &usb_fifo_dummy_cmd
;
1950 if (pm
->f_start_write
== NULL
)
1951 pm
->f_start_write
= &usb_fifo_dummy_cmd
;
1953 if (pm
->f_stop_write
== NULL
)
1954 pm
->f_stop_write
= &usb_fifo_dummy_cmd
;
1957 /*------------------------------------------------------------------------*
1960 * The following function will create a duplex FIFO.
1965 *------------------------------------------------------------------------*/
1967 usb_fifo_attach(struct usb_device
*udev
, void *priv_sc
,
1968 struct lock
*priv_lock
, struct usb_fifo_methods
*pm
,
1969 struct usb_fifo_sc
*f_sc
, uint16_t unit
, int16_t subunit
,
1970 uint8_t iface_index
, uid_t uid
, gid_t gid
, int mode
)
1972 struct usb_fifo
*f_tx
;
1973 struct usb_fifo
*f_rx
;
1977 f_sc
->fp
[USB_FIFO_TX
] = NULL
;
1978 f_sc
->fp
[USB_FIFO_RX
] = NULL
;
1983 /* check the methods */
1984 usb_fifo_check_methods(pm
);
1986 if (priv_lock
== NULL
) {
1987 DPRINTF("null priv_lock set\n");
1990 /* search for a free FIFO slot */
1991 for (n
= 0;; n
+= 2) {
1993 if (n
== USB_FIFO_MAX
) {
1994 /* end of FIFOs reached */
1997 /* Check for TX FIFO */
1998 if (udev
->fifo
[n
+ USB_FIFO_TX
] != NULL
) {
2001 /* Check for RX FIFO */
2002 if (udev
->fifo
[n
+ USB_FIFO_RX
] != NULL
) {
2008 f_tx
= usb_fifo_alloc(priv_lock
);
2009 f_rx
= usb_fifo_alloc(priv_lock
);
2011 if ((f_tx
== NULL
) || (f_rx
== NULL
)) {
2012 usb_fifo_free(f_tx
);
2013 usb_fifo_free(f_rx
);
2016 /* initialise FIFO structures */
2018 f_tx
->fifo_index
= n
+ USB_FIFO_TX
;
2019 f_tx
->dev_ep_index
= -1;
2020 f_tx
->priv_lock
= priv_lock
;
2021 f_tx
->priv_sc0
= priv_sc
;
2023 f_tx
->iface_index
= iface_index
;
2026 f_rx
->fifo_index
= n
+ USB_FIFO_RX
;
2027 f_rx
->dev_ep_index
= -1;
2028 f_rx
->priv_lock
= priv_lock
;
2029 f_rx
->priv_sc0
= priv_sc
;
2031 f_rx
->iface_index
= iface_index
;
2034 f_sc
->fp
[USB_FIFO_TX
] = f_tx
;
2035 f_sc
->fp
[USB_FIFO_RX
] = f_rx
;
2037 lockmgr(&usb_ref_lock
, LK_EXCLUSIVE
);
2038 udev
->fifo
[f_tx
->fifo_index
] = f_tx
;
2039 udev
->fifo
[f_rx
->fifo_index
] = f_rx
;
2040 lockmgr(&usb_ref_lock
, LK_RELEASE
);
2042 for (n
= 0; n
!= 4; n
++) {
2044 if (pm
->basename
[n
] == NULL
) {
2048 if (ksnprintf(devname
, sizeof(devname
),
2049 "%s%u%s", pm
->basename
[n
],
2050 unit
, pm
->postfix
[n
] ?
2051 pm
->postfix
[n
] : "")) {
2055 if (ksnprintf(devname
, sizeof(devname
),
2056 "%s%u.%u%s", pm
->basename
[n
],
2057 unit
, subunit
, pm
->postfix
[n
] ?
2058 pm
->postfix
[n
] : "")) {
2064 * Distribute the symbolic links into two FIFO structures:
2067 f_rx
->symlink
[n
/ 2] =
2068 usb_alloc_symlink(devname
);
2070 f_tx
->symlink
[n
/ 2] =
2071 usb_alloc_symlink(devname
);
2074 /* Create the device */
2075 f_sc
->dev
= usb_make_dev(udev
, devname
, -1,
2076 f_tx
->fifo_index
& f_rx
->fifo_index
,
2077 FREAD
|FWRITE
, uid
, gid
, mode
);
2080 DPRINTFN(2, "attached %p/%p\n", f_tx
, f_rx
);
2084 /*------------------------------------------------------------------------*
2085 * usb_fifo_alloc_buffer
2090 *------------------------------------------------------------------------*/
2092 usb_fifo_alloc_buffer(struct usb_fifo
*f
, usb_size_t bufsize
,
2095 usb_fifo_free_buffer(f
);
2097 /* allocate an endpoint */
2098 f
->free_q
.ifq_maxlen
= nbuf
;
2099 f
->used_q
.ifq_maxlen
= nbuf
;
2101 f
->queue_data
= usb_alloc_mbufs(
2102 M_USBDEV
, &f
->free_q
, bufsize
, nbuf
);
2104 if ((f
->queue_data
== NULL
) && bufsize
&& nbuf
) {
2107 return (0); /* success */
2110 /*------------------------------------------------------------------------*
2111 * usb_fifo_free_buffer
2113 * This function will free the buffers associated with a FIFO. This
2114 * function can be called multiple times in a row.
2115 *------------------------------------------------------------------------*/
2117 usb_fifo_free_buffer(struct usb_fifo
*f
)
2119 if (f
->queue_data
) {
2120 /* free old buffer */
2121 kfree(f
->queue_data
, M_USBDEV
);
2122 f
->queue_data
= NULL
;
2126 memset(&f
->free_q
, 0, sizeof(f
->free_q
));
2127 memset(&f
->used_q
, 0, sizeof(f
->used_q
));
2131 usb_fifo_detach(struct usb_fifo_sc
*f_sc
)
2136 usb_fifo_free(f_sc
->fp
[USB_FIFO_TX
]);
2137 usb_fifo_free(f_sc
->fp
[USB_FIFO_RX
]);
2139 f_sc
->fp
[USB_FIFO_TX
] = NULL
;
2140 f_sc
->fp
[USB_FIFO_RX
] = NULL
;
2142 usb_destroy_dev(f_sc
->dev
);
2146 DPRINTFN(2, "detached %p\n", f_sc
);
2150 usb_fifo_put_bytes_max(struct usb_fifo
*f
)
2155 USB_IF_POLL(&f
->free_q
, m
);
2158 len
= m
->max_data_len
;
2165 /*------------------------------------------------------------------------*
2169 * 0 - normal operation
2170 * 1 - set last packet flag to enforce framing
2171 *------------------------------------------------------------------------*/
2173 usb_fifo_put_data(struct usb_fifo
*f
, struct usb_page_cache
*pc
,
2174 usb_frlength_t offset
, usb_frlength_t len
, uint8_t what
)
2177 usb_frlength_t io_len
;
2179 while (len
|| (what
== 1)) {
2181 USB_IF_DEQUEUE(&f
->free_q
, m
);
2186 io_len
= MIN(len
, m
->cur_data_len
);
2188 usbd_copy_out(pc
, offset
, m
->cur_data_ptr
, io_len
);
2190 m
->cur_data_len
= io_len
;
2194 if ((len
== 0) && (what
== 1)) {
2197 USB_IF_ENQUEUE(&f
->used_q
, m
);
2201 if ((len
== 0) || (what
== 1)) {
2211 usb_fifo_put_data_linear(struct usb_fifo
*f
, void *ptr
,
2212 usb_size_t len
, uint8_t what
)
2217 while (len
|| (what
== 1)) {
2219 USB_IF_DEQUEUE(&f
->free_q
, m
);
2224 io_len
= MIN(len
, m
->cur_data_len
);
2226 memcpy(m
->cur_data_ptr
, ptr
, io_len
);
2228 m
->cur_data_len
= io_len
;
2229 ptr
= USB_ADD_BYTES(ptr
, io_len
);
2232 if ((len
== 0) && (what
== 1)) {
2235 USB_IF_ENQUEUE(&f
->used_q
, m
);
2239 if ((len
== 0) || (what
== 1)) {
2249 usb_fifo_put_data_buffer(struct usb_fifo
*f
, void *ptr
, usb_size_t len
)
2253 USB_IF_DEQUEUE(&f
->free_q
, m
);
2256 m
->cur_data_len
= len
;
2257 m
->cur_data_ptr
= ptr
;
2258 USB_IF_ENQUEUE(&f
->used_q
, m
);
2266 usb_fifo_put_data_error(struct usb_fifo
*f
)
2268 f
->flag_iserror
= 1;
2272 /*------------------------------------------------------------------------*
2276 * 0 - normal operation
2277 * 1 - only get one "usb_mbuf"
2281 * 1 - data in buffer
2282 *------------------------------------------------------------------------*/
2284 usb_fifo_get_data(struct usb_fifo
*f
, struct usb_page_cache
*pc
,
2285 usb_frlength_t offset
, usb_frlength_t len
, usb_frlength_t
*actlen
,
2289 usb_frlength_t io_len
;
2290 uint8_t tr_data
= 0;
2296 USB_IF_DEQUEUE(&f
->used_q
, m
);
2302 io_len
= MIN(len
, m
->cur_data_len
);
2304 usbd_copy_in(pc
, offset
, m
->cur_data_ptr
, io_len
);
2308 actlen
[0] += io_len
;
2309 m
->cur_data_ptr
+= io_len
;
2310 m
->cur_data_len
-= io_len
;
2312 if ((m
->cur_data_len
== 0) || (what
== 1)) {
2313 USB_IF_ENQUEUE(&f
->free_q
, m
);
2321 USB_IF_PREPEND(&f
->used_q
, m
);
2327 /* wait for data to be written out */
2330 if (f
->flag_flushing
) {
2331 /* check if we should send a short packet */
2332 if (f
->flag_short
!= 0) {
2337 /* flushing complete */
2338 f
->flag_flushing
= 0;
2351 usb_fifo_get_data_linear(struct usb_fifo
*f
, void *ptr
,
2352 usb_size_t len
, usb_size_t
*actlen
, uint8_t what
)
2356 uint8_t tr_data
= 0;
2362 USB_IF_DEQUEUE(&f
->used_q
, m
);
2368 io_len
= MIN(len
, m
->cur_data_len
);
2370 memcpy(ptr
, m
->cur_data_ptr
, io_len
);
2373 ptr
= USB_ADD_BYTES(ptr
, io_len
);
2374 actlen
[0] += io_len
;
2375 m
->cur_data_ptr
+= io_len
;
2376 m
->cur_data_len
-= io_len
;
2378 if ((m
->cur_data_len
== 0) || (what
== 1)) {
2379 USB_IF_ENQUEUE(&f
->free_q
, m
);
2387 USB_IF_PREPEND(&f
->used_q
, m
);
2393 /* wait for data to be written out */
2396 if (f
->flag_flushing
) {
2397 /* check if we should send a short packet */
2398 if (f
->flag_short
!= 0) {
2403 /* flushing complete */
2404 f
->flag_flushing
= 0;
2417 usb_fifo_get_data_buffer(struct usb_fifo
*f
, void **pptr
, usb_size_t
*plen
)
2421 USB_IF_POLL(&f
->used_q
, m
);
2424 *plen
= m
->cur_data_len
;
2425 *pptr
= m
->cur_data_ptr
;
2433 usb_fifo_get_data_error(struct usb_fifo
*f
)
2435 f
->flag_iserror
= 1;
2439 /*------------------------------------------------------------------------*
2444 * Else: Pointer to symlink entry
2445 *------------------------------------------------------------------------*/
2446 struct usb_symlink
*
2447 usb_alloc_symlink(const char *target
)
2449 struct usb_symlink
*ps
;
2451 ps
= kmalloc(sizeof(*ps
), M_USBDEV
, M_WAITOK
);
2455 /* XXX no longer needed */
2456 strlcpy(ps
->src_path
, target
, sizeof(ps
->src_path
));
2457 ps
->src_len
= strlen(ps
->src_path
);
2458 strlcpy(ps
->dst_path
, target
, sizeof(ps
->dst_path
));
2459 ps
->dst_len
= strlen(ps
->dst_path
);
2461 lockmgr(&usb_sym_lock
, LK_EXCLUSIVE
);
2462 TAILQ_INSERT_TAIL(&usb_sym_head
, ps
, sym_entry
);
2463 lockmgr(&usb_sym_lock
, LK_RELEASE
);
2467 /*------------------------------------------------------------------------*
2469 *------------------------------------------------------------------------*/
2471 usb_free_symlink(struct usb_symlink
*ps
)
2476 lockmgr(&usb_sym_lock
, LK_EXCLUSIVE
);
2477 TAILQ_REMOVE(&usb_sym_head
, ps
, sym_entry
);
2478 lockmgr(&usb_sym_lock
, LK_RELEASE
);
2480 kfree(ps
, M_USBDEV
);
2483 /*------------------------------------------------------------------------*
2489 *------------------------------------------------------------------------*/
2491 usb_read_symlink(uint8_t *user_ptr
, uint32_t startentry
, uint32_t user_len
)
2493 struct usb_symlink
*ps
;
2499 lockmgr(&usb_sym_lock
, LK_EXCLUSIVE
);
2501 TAILQ_FOREACH(ps
, &usb_sym_head
, sym_entry
) {
2504 * Compute total length of source and destination symlink
2505 * strings pluss one length byte and two NUL bytes:
2507 temp
= ps
->src_len
+ ps
->dst_len
+ 3;
2511 * Skip entry because this length cannot fit
2516 if (startentry
!= 0) {
2517 /* decrement read offset */
2521 if (temp
> user_len
) {
2522 /* out of buffer space */
2527 /* copy out total length */
2529 error
= copyout(&len
,
2530 USB_ADD_BYTES(user_ptr
, delta
), 1);
2536 /* copy out source string */
2538 error
= copyout(ps
->src_path
,
2539 USB_ADD_BYTES(user_ptr
, delta
), ps
->src_len
);
2544 delta
+= ps
->src_len
;
2545 error
= copyout(&len
,
2546 USB_ADD_BYTES(user_ptr
, delta
), 1);
2552 /* copy out destination string */
2554 error
= copyout(ps
->dst_path
,
2555 USB_ADD_BYTES(user_ptr
, delta
), ps
->dst_len
);
2560 delta
+= ps
->dst_len
;
2561 error
= copyout(&len
,
2562 USB_ADD_BYTES(user_ptr
, delta
), 1);
2571 /* a zero length entry indicates the end */
2573 if ((user_len
!= 0) && (error
== 0)) {
2577 error
= copyout(&len
,
2578 USB_ADD_BYTES(user_ptr
, delta
), 1);
2580 lockmgr(&usb_sym_lock
, LK_RELEASE
);
2585 usb_fifo_set_close_zlp(struct usb_fifo
*f
, uint8_t onoff
)
2590 /* send a Zero Length Packet, ZLP, before close */
2591 f
->flag_short
= onoff
;
2595 usb_fifo_set_write_defrag(struct usb_fifo
*f
, uint8_t onoff
)
2600 /* defrag written data */
2601 f
->flag_write_defrag
= onoff
;
2602 /* reset defrag state */
2603 f
->flag_have_fragment
= 0;
2607 usb_fifo_softc(struct usb_fifo
*f
)
2609 return (f
->priv_sc0
);
2611 #endif /* USB_HAVE_UGEN */