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>
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(struct lock
*lock
);
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
;
162 static struct kqinfo usb_kqevent
;
165 /*------------------------------------------------------------------------*
168 * This is used to fill out a usb_cdev_privdata structure based on the
169 * device's address as contained in usb_fs_privdata.
170 *------------------------------------------------------------------------*/
172 usb_loc_fill(struct usb_fs_privdata
* pd
, struct usb_cdev_privdata
*cpd
)
174 cpd
->bus_index
= pd
->bus_index
;
175 cpd
->dev_index
= pd
->dev_index
;
176 cpd
->ep_addr
= pd
->ep_addr
;
177 cpd
->fifo_index
= pd
->fifo_index
;
180 /*------------------------------------------------------------------------*
183 * This function is used to atomically refer an USB device by its
184 * device location. If this function returns success the USB device
185 * will not dissappear until the USB device is unreferenced.
188 * 0: Success, refcount incremented on the given USB device.
190 *------------------------------------------------------------------------*/
192 usb_ref_device(struct usb_cdev_privdata
*cpd
,
193 struct usb_cdev_refdata
*crd
, int need_uref
)
195 struct usb_fifo
**ppf
;
198 DPRINTFN(2, "cpd=%p need uref=%d\n", cpd
, need_uref
);
201 memset(crd
, 0, sizeof(*crd
));
203 lockmgr(&usb_ref_lock
, LK_EXCLUSIVE
);
204 cpd
->bus
= devclass_get_softc(usb_devclass_ptr
, cpd
->bus_index
);
205 if (cpd
->bus
== NULL
) {
206 DPRINTFN(2, "no bus at %u\n", cpd
->bus_index
);
209 cpd
->udev
= cpd
->bus
->devices
[cpd
->dev_index
];
210 if (cpd
->udev
== NULL
) {
211 DPRINTFN(2, "no device at %u\n", cpd
->dev_index
);
214 if (cpd
->udev
->state
== USB_STATE_DETACHED
&&
216 DPRINTFN(2, "device is detached\n");
220 DPRINTFN(2, "ref udev - needed\n");
222 if (cpd
->udev
->refcount
== USB_DEV_REF_MAX
) {
223 DPRINTFN(2, "no dev ref\n");
226 cpd
->udev
->refcount
++;
228 lockmgr(&usb_ref_lock
, LK_RELEASE
);
231 * We need to grab the sx-lock before grabbing the
232 * FIFO refs to avoid deadlock at detach!
234 crd
->do_unlock
= usbd_enum_lock(cpd
->udev
);
236 lockmgr(&usb_ref_lock
, LK_EXCLUSIVE
);
239 * Set "is_uref" after grabbing the default SX lock
244 /* check if we are doing an open */
245 if (cpd
->fflags
== 0) {
246 /* use zero defaults */
248 /* check for write */
249 if (cpd
->fflags
& FWRITE
) {
250 ppf
= cpd
->udev
->fifo
;
251 f
= ppf
[cpd
->fifo_index
+ USB_FIFO_TX
];
253 crd
->is_write
= 1; /* ref */
254 if (f
== NULL
|| f
->refcount
== USB_FIFO_REF_MAX
)
256 if (f
->curr_cpd
!= cpd
)
258 /* check if USB-FS is active */
259 if (f
->fs_ep_max
!= 0) {
265 if (cpd
->fflags
& FREAD
) {
266 ppf
= cpd
->udev
->fifo
;
267 f
= ppf
[cpd
->fifo_index
+ USB_FIFO_RX
];
269 crd
->is_read
= 1; /* ref */
270 if (f
== NULL
|| f
->refcount
== USB_FIFO_REF_MAX
)
272 if (f
->curr_cpd
!= cpd
)
274 /* check if USB-FS is active */
275 if (f
->fs_ep_max
!= 0) {
281 /* when everything is OK we increment the refcounts */
283 DPRINTFN(2, "ref write\n");
284 crd
->txfifo
->refcount
++;
287 DPRINTFN(2, "ref read\n");
288 crd
->rxfifo
->refcount
++;
290 lockmgr(&usb_ref_lock
, LK_RELEASE
);
296 usbd_enum_unlock(cpd
->udev
);
299 if (--(cpd
->udev
->refcount
) == 0)
300 cv_broadcast(&cpd
->udev
->ref_cv
);
302 lockmgr(&usb_ref_lock
, LK_RELEASE
);
303 DPRINTFN(2, "fail\n");
306 memset(crd
, 0, sizeof(*crd
));
308 return (USB_ERR_INVAL
);
311 /*------------------------------------------------------------------------*
314 * This function is used to upgrade an USB reference to include the
315 * USB device reference on a USB location.
318 * 0: Success, refcount incremented on the given USB device.
320 *------------------------------------------------------------------------*/
322 usb_usb_ref_device(struct usb_cdev_privdata
*cpd
,
323 struct usb_cdev_refdata
*crd
)
326 * Check if we already got an USB reference on this location:
329 return (0); /* success */
332 * To avoid deadlock at detach we need to drop the FIFO ref
333 * and re-acquire a new ref!
335 usb_unref_device(cpd
, crd
);
337 return (usb_ref_device(cpd
, crd
, 1 /* need uref */));
340 /*------------------------------------------------------------------------*
343 * This function will release the reference count by one unit for the
345 *------------------------------------------------------------------------*/
347 usb_unref_device(struct usb_cdev_privdata
*cpd
,
348 struct usb_cdev_refdata
*crd
)
351 DPRINTFN(2, "cpd=%p is_uref=%d\n", cpd
, crd
->is_uref
);
354 usbd_enum_unlock(cpd
->udev
);
356 lockmgr(&usb_ref_lock
, LK_EXCLUSIVE
);
358 if (--(crd
->rxfifo
->refcount
) == 0) {
359 cv_signal(&crd
->rxfifo
->cv_drain
);
364 if (--(crd
->txfifo
->refcount
) == 0) {
365 cv_signal(&crd
->txfifo
->cv_drain
);
371 if (--(cpd
->udev
->refcount
) == 0)
372 cv_broadcast(&cpd
->udev
->ref_cv
);
374 lockmgr(&usb_ref_lock
, LK_RELEASE
);
377 static struct usb_fifo
*
378 usb_fifo_alloc(struct lock
*lock
)
382 f
= kmalloc(sizeof(*f
), M_USBDEV
, M_WAITOK
| M_ZERO
);
384 cv_init(&f
->cv_io
, "FIFO-IO");
385 cv_init(&f
->cv_drain
, "FIFO-DRAIN");
388 /* mpf: knlist_init_mtx? the lock is used here in free */
393 /*------------------------------------------------------------------------*
395 *------------------------------------------------------------------------*/
397 usb_fifo_create(struct usb_cdev_privdata
*cpd
,
398 struct usb_cdev_refdata
*crd
)
400 struct usb_device
*udev
= cpd
->udev
;
402 struct usb_endpoint
*ep
;
408 int e
= cpd
->ep_addr
;
410 is_tx
= (cpd
->fflags
& FWRITE
) ? 1 : 0;
411 is_rx
= (cpd
->fflags
& FREAD
) ? 1 : 0;
415 /* Preallocated FIFO */
417 DPRINTFN(5, "Preallocated FIFO\n");
419 f
= udev
->fifo
[cpd
->fifo_index
+ USB_FIFO_TX
];
425 f
= udev
->fifo
[cpd
->fifo_index
+ USB_FIFO_RX
];
433 KASSERT(e
>= 0 && e
<= 15, ("endpoint %d out of range", e
));
435 /* search for a free FIFO slot */
436 DPRINTFN(5, "Endpoint device, searching for 0x%02x\n", e
);
437 for (n
= 0;; n
+= 2) {
439 if (n
== USB_FIFO_MAX
) {
444 /* end of FIFOs reached */
445 DPRINTFN(5, "out of FIFOs\n");
449 /* Check for TX FIFO */
451 f
= udev
->fifo
[n
+ USB_FIFO_TX
];
453 if (f
->dev_ep_index
!= e
) {
454 /* wrong endpoint index */
457 if (f
->curr_cpd
!= NULL
) {
462 } else if (no_null
) {
466 /* Check for RX FIFO */
468 f
= udev
->fifo
[n
+ USB_FIFO_RX
];
470 if (f
->dev_ep_index
!= e
) {
471 /* wrong endpoint index */
474 if (f
->curr_cpd
!= NULL
) {
479 } else if (no_null
) {
487 if (e
>= (USB_EP_MAX
/ 2)) {
488 /* we don't create any endpoints in this range */
489 DPRINTFN(5, "ep out of range\n");
490 return (is_busy
? EBUSY
: EINVAL
);
494 if ((e
!= 0) && is_busy
) {
496 * Only the default control endpoint is allowed to be
497 * opened multiple times!
499 DPRINTFN(5, "busy\n");
505 (udev
->fifo
[n
+ USB_FIFO_TX
] == NULL
)) {
506 ep
= usb_dev_get_ep(udev
, e
, USB_FIFO_TX
);
507 DPRINTFN(5, "dev_get_endpoint(%d, 0x%x)\n", e
, USB_FIFO_TX
);
509 DPRINTFN(5, "dev_get_endpoint returned NULL\n");
512 f
= usb_fifo_alloc(&udev
->device_lock
);
514 DPRINTFN(5, "could not alloc tx fifo\n");
517 /* update some fields */
518 f
->fifo_index
= n
+ USB_FIFO_TX
;
521 f
->methods
= &usb_ugen_methods
;
522 f
->iface_index
= ep
->iface_index
;
524 lockmgr(&usb_ref_lock
, LK_EXCLUSIVE
);
525 udev
->fifo
[n
+ USB_FIFO_TX
] = f
;
526 lockmgr(&usb_ref_lock
, LK_RELEASE
);
530 (udev
->fifo
[n
+ USB_FIFO_RX
] == NULL
)) {
532 ep
= usb_dev_get_ep(udev
, e
, USB_FIFO_RX
);
533 DPRINTFN(5, "dev_get_endpoint(%d, 0x%x)\n", e
, USB_FIFO_RX
);
535 DPRINTFN(5, "dev_get_endpoint returned NULL\n");
538 f
= usb_fifo_alloc(&udev
->device_lock
);
540 DPRINTFN(5, "could not alloc rx fifo\n");
543 /* update some fields */
544 f
->fifo_index
= n
+ USB_FIFO_RX
;
547 f
->methods
= &usb_ugen_methods
;
548 f
->iface_index
= ep
->iface_index
;
550 lockmgr(&usb_ref_lock
, LK_EXCLUSIVE
);
551 udev
->fifo
[n
+ USB_FIFO_RX
] = f
;
552 lockmgr(&usb_ref_lock
, LK_RELEASE
);
555 crd
->txfifo
= udev
->fifo
[n
+ USB_FIFO_TX
];
558 crd
->rxfifo
= udev
->fifo
[n
+ USB_FIFO_RX
];
560 /* fill out fifo index */
561 DPRINTFN(5, "fifo index = %d\n", n
);
570 usb_fifo_free(struct usb_fifo
*f
)
578 /* destroy symlink devices, if any */
579 for (n
= 0; n
!= 2; n
++) {
581 usb_free_symlink(f
->symlink
[n
]);
582 f
->symlink
[n
] = NULL
;
585 lockmgr(&usb_ref_lock
, LK_EXCLUSIVE
);
587 /* delink ourselves to stop calls from userland */
588 if ((f
->fifo_index
< USB_FIFO_MAX
) &&
590 (f
->udev
->fifo
[f
->fifo_index
] == f
)) {
591 f
->udev
->fifo
[f
->fifo_index
] = NULL
;
593 DPRINTFN(0, "USB FIFO %p has not been linked\n", f
);
596 /* decrease refcount */
598 /* need to wait until all callers have exited */
599 while (f
->refcount
!= 0) {
600 lockmgr(&usb_ref_lock
, LK_RELEASE
); /* avoid LOR */
601 lockmgr(f
->priv_lock
, LK_EXCLUSIVE
);
602 /* prevent write flush, if any */
604 /* get I/O thread out of any sleep state */
605 if (f
->flag_sleeping
) {
606 f
->flag_sleeping
= 0;
607 cv_broadcast(&f
->cv_io
);
609 lockmgr(f
->priv_lock
, LK_RELEASE
);
610 lockmgr(&usb_ref_lock
, LK_EXCLUSIVE
);
613 * Check if the "f->refcount" variable reached zero
614 * during the unlocked time before entering wait:
616 if (f
->refcount
== 0)
620 cv_wait(&f
->cv_drain
, &usb_ref_lock
);
622 lockmgr(&usb_ref_lock
, LK_RELEASE
);
624 /* take care of closing the device here, if any */
625 usb_fifo_close(f
, 0);
627 cv_destroy(&f
->cv_io
);
628 cv_destroy(&f
->cv_drain
);
631 knlist_clear(&f
->selinfo
.si_note
, 0);
632 seldrain(&f
->selinfo
);
633 knlist_destroy(&f
->selinfo
.si_note
);
638 static struct usb_endpoint
*
639 usb_dev_get_ep(struct usb_device
*udev
, uint8_t ep_index
, uint8_t dir
)
641 struct usb_endpoint
*ep
;
647 if (dir
== USB_FIFO_RX
) {
648 if (udev
->flags
.usb_mode
== USB_MODE_HOST
) {
654 if (udev
->flags
.usb_mode
== USB_MODE_HOST
) {
660 ep
= usbd_get_ep_by_addr(udev
, ep_index
| ep_dir
);
664 /* if the endpoint does not exist then return */
667 if (ep
->edesc
== NULL
) {
668 /* invalid endpoint */
671 return (ep
); /* success */
674 /*------------------------------------------------------------------------*
680 *------------------------------------------------------------------------*/
682 usb_fifo_open(struct usb_cdev_privdata
*cpd
,
683 struct usb_fifo
*f
, int fflags
)
689 DPRINTFN(2, "no FIFO\n");
692 /* remove FWRITE and FREAD flags */
693 fflags
&= ~(FWRITE
| FREAD
);
695 /* set correct file flags */
696 if ((f
->fifo_index
& 1) == USB_FIFO_TX
) {
702 /* check if we are already opened */
703 /* we don't need any locks when checking this variable */
704 if (f
->curr_cpd
!= NULL
) {
709 /* reset short flag before open */
712 /* call open method */
713 err
= (f
->methods
->f_open
) (f
, fflags
);
717 lockmgr(f
->priv_lock
, LK_EXCLUSIVE
);
719 /* reset sleep flag */
720 f
->flag_sleeping
= 0;
722 /* reset error flag */
725 /* reset complete flag */
726 f
->flag_iscomplete
= 0;
728 /* reset select flag */
729 f
->flag_isselect
= 0;
731 /* reset flushing flag */
732 f
->flag_flushing
= 0;
734 /* reset ASYNC proc flag */
737 lockmgr(&usb_ref_lock
, LK_EXCLUSIVE
);
738 /* flag the fifo as opened to prevent others */
740 lockmgr(&usb_ref_lock
, LK_RELEASE
);
745 lockmgr(f
->priv_lock
, LK_RELEASE
);
750 /*------------------------------------------------------------------------*
752 *------------------------------------------------------------------------*/
754 usb_fifo_reset(struct usb_fifo
*f
)
762 USB_IF_DEQUEUE(&f
->used_q
, m
);
764 USB_IF_ENQUEUE(&f
->free_q
, m
);
769 /* reset have fragment flag */
770 f
->flag_have_fragment
= 0;
773 /*------------------------------------------------------------------------*
775 *------------------------------------------------------------------------*/
777 usb_fifo_close(struct usb_fifo
*f
, int fflags
)
781 /* check if we are not opened */
782 if (f
->curr_cpd
== NULL
) {
783 /* nothing to do - already closed */
786 lockmgr(f
->priv_lock
, LK_EXCLUSIVE
);
788 /* clear current cdev private data pointer */
789 lockmgr(&usb_ref_lock
, LK_EXCLUSIVE
);
791 lockmgr(&usb_ref_lock
, LK_RELEASE
);
793 /* check if we are selected */
794 if (f
->flag_isselect
) {
795 KNOTE(&f
->selinfo
.ki_note
, 0);
796 wakeup(&f
->selinfo
.ki_note
);
798 f
->flag_isselect
= 0;
800 /* check if a thread wants SIGIO */
801 if (f
->async_p
!= NULL
&& lwkt_trytoken(&f
->async_p
->p_token
)) {
802 ksignal(f
->async_p
, SIGIO
);
803 lwkt_reltoken(&f
->async_p
->p_token
);
806 /* remove FWRITE and FREAD flags */
807 fflags
&= ~(FWRITE
| FREAD
);
809 /* flush written data, if any */
810 if ((f
->fifo_index
& 1) == USB_FIFO_TX
) {
812 if (!f
->flag_iserror
) {
814 /* set flushing flag */
815 f
->flag_flushing
= 1;
817 /* get the last packet in */
818 if (f
->flag_have_fragment
) {
820 f
->flag_have_fragment
= 0;
821 USB_IF_DEQUEUE(&f
->free_q
, m
);
823 USB_IF_ENQUEUE(&f
->used_q
, m
);
827 /* start write transfer, if not already started */
828 (f
->methods
->f_start_write
) (f
);
830 /* check if flushed already */
831 while (f
->flag_flushing
&&
832 (!f
->flag_iserror
)) {
833 /* wait until all data has been written */
834 f
->flag_sleeping
= 1;
835 err
= cv_wait_sig(&f
->cv_io
, f
->priv_lock
);
837 DPRINTF("signal received\n");
844 /* stop write transfer, if not already stopped */
845 (f
->methods
->f_stop_write
) (f
);
849 /* stop write transfer, if not already stopped */
850 (f
->methods
->f_stop_read
) (f
);
853 /* check if we are sleeping */
854 if (f
->flag_sleeping
) {
855 DPRINTFN(2, "Sleeping at close!\n");
857 lockmgr(f
->priv_lock
, LK_RELEASE
);
859 /* call close method */
860 (f
->methods
->f_close
) (f
, fflags
);
865 /*------------------------------------------------------------------------*
866 * usb_open - cdev callback
867 *------------------------------------------------------------------------*/
869 usb_open(struct dev_open_args
*ap
)
871 struct cdev
*dev
= ap
->a_head
.a_dev
;
872 int fflags
= ap
->a_oflags
;
873 struct usb_fs_privdata
* pd
= (struct usb_fs_privdata
*)dev
->si_drv1
;
874 struct usb_cdev_refdata refs
;
875 struct usb_cdev_privdata
*cpd
;
878 DPRINTFN(2, "%s fflags=0x%08x\n", devtoname(dev
), fflags
);
880 KASSERT(fflags
& (FREAD
|FWRITE
), ("invalid open flags"));
881 if (((fflags
& FREAD
) && !(pd
->mode
& FREAD
)) ||
882 ((fflags
& FWRITE
) && !(pd
->mode
& FWRITE
))) {
883 DPRINTFN(2, "access mode not supported\n");
887 cpd
= kmalloc(sizeof(*cpd
), M_USBDEV
, M_WAITOK
| M_ZERO
);
888 ep
= cpd
->ep_addr
= pd
->ep_addr
;
890 usb_loc_fill(pd
, cpd
);
891 err
= usb_ref_device(cpd
, &refs
, 1);
893 DPRINTFN(2, "cannot ref device\n");
894 kfree(cpd
, M_USBDEV
);
897 cpd
->fflags
= fflags
; /* access mode for open lifetime */
899 /* create FIFOs, if any */
900 err
= usb_fifo_create(cpd
, &refs
);
901 /* check for error */
903 DPRINTFN(2, "cannot create fifo\n");
904 usb_unref_device(cpd
, &refs
);
905 kfree(cpd
, M_USBDEV
);
908 if (fflags
& FREAD
) {
909 err
= usb_fifo_open(cpd
, refs
.rxfifo
, fflags
);
911 DPRINTFN(2, "read open failed\n");
912 usb_unref_device(cpd
, &refs
);
913 kfree(cpd
, M_USBDEV
);
917 if (fflags
& FWRITE
) {
918 err
= usb_fifo_open(cpd
, refs
.txfifo
, fflags
);
920 DPRINTFN(2, "write open failed\n");
921 if (fflags
& FREAD
) {
922 usb_fifo_close(refs
.rxfifo
, fflags
);
924 usb_unref_device(cpd
, &refs
);
925 kfree(cpd
, M_USBDEV
);
929 usb_unref_device(cpd
, &refs
);
930 err
= devfs_set_cdevpriv(ap
->a_fp
, cpd
, &usb_cdevpriv_dtor
);
931 DPRINTFN(2, "fp=%p cpd=%p\n", ap
->a_fp
, cpd
);
933 DPRINTFN(2, "devfs_set_cdevpriv failed in %s\n", __func__
);
934 kfree(cpd
, M_USBDEV
);
945 usb_close(struct dev_close_args
*ap
)
947 DPRINTFN(2, "usb_close called\n");
951 /*------------------------------------------------------------------------*
952 * usb_close - cdev callback
953 *------------------------------------------------------------------------*/
955 usb_cdevpriv_dtor(void *cd
)
957 struct usb_cdev_privdata
*cpd
= (struct usb_cdev_privdata
*)cd
;
958 struct usb_cdev_refdata refs
;
961 DPRINTF("dtor called on %p\n", cpd
);
963 err
= usb_ref_device(cpd
, &refs
, 2);
965 DPRINTFN(0, "Cannot grab USB reference when "
966 "closing USB file handle\n");
969 if (cpd
->fflags
& FREAD
) {
970 usb_fifo_close(refs
.rxfifo
, cpd
->fflags
);
972 if (cpd
->fflags
& FWRITE
) {
973 usb_fifo_close(refs
.txfifo
, cpd
->fflags
);
975 usb_unref_device(cpd
, &refs
);
977 kfree(cpd
, M_USBDEV
);
981 usb_dev_init(void *arg
)
983 lockinit(&usb_ref_lock
, "USB ref mutex", 0, 0);
984 lockinit(&usb_sym_lock
, "USB sym mutex", 0, 0);
985 TAILQ_INIT(&usb_sym_head
);
987 /* check the UGEN methods */
988 usb_fifo_check_methods(&usb_ugen_methods
);
991 /* XXX SI_SUB_KLD? */
992 SYSINIT(usb_dev_init
, SI_SUB_PRE_DRIVERS
, SI_ORDER_FIRST
, usb_dev_init
, NULL
);
995 usb_dev_init_post(void *arg
)
998 * Create /dev/usb - this is needed for usbconfig(8), which
999 * needs a well-known device name to access.
1001 usb_dev
= make_dev(&usb_static_ops
, 0, UID_ROOT
, GID_OPERATOR
,
1002 0644, USB_DEVICE_NAME
);
1003 if (usb_dev
== NULL
) {
1004 DPRINTFN(0, "Could not create usb bus device\n");
1008 SYSINIT(usb_dev_init_post
, SI_SUB_DRIVERS
, SI_ORDER_FIRST
, usb_dev_init_post
,
1012 usb_dev_uninit(void *arg
)
1014 if (usb_dev
!= NULL
) {
1015 destroy_dev(usb_dev
);
1018 lockuninit(&usb_ref_lock
);
1019 lockuninit(&usb_sym_lock
);
1022 SYSUNINIT(usb_dev_uninit
, SI_SUB_KICK_SCHEDULER
, SI_ORDER_ANY
, usb_dev_uninit
, NULL
);
1025 usb_ioctl_f_sub(struct usb_fifo
*f
, u_long cmd
, void *addr
,
1032 *(int *)addr
= 0; /* character device */
1036 /* handled by upper FS layer */
1041 if (f
->async_p
!= NULL
) {
1045 f
->async_p
= USB_TD_GET_PROC(td
);
1051 /* XXX this is not the most general solution */
1053 if (f
->async_p
== NULL
) {
1057 if (*(int *)addr
!= USB_PROC_GET_GID(f
->async_p
)) {
1065 DPRINTFN(3, "cmd 0x%lx = %d\n", cmd
, error
);
1069 /*------------------------------------------------------------------------*
1070 * usb_ioctl - cdev callback
1071 *------------------------------------------------------------------------*/
1073 usb_ioctl(struct dev_ioctl_args
*ap
)
1075 u_long cmd
= ap
->a_cmd
;
1076 caddr_t addr
= ap
->a_data
;
1077 struct thread
*td
= curthread
;
1078 struct usb_cdev_refdata refs
;
1079 struct usb_cdev_privdata
* cpd
;
1084 DPRINTFN(2, "cmd=0x%lx\n", cmd
);
1086 err
= devfs_get_cdevpriv(ap
->a_fp
, (void **)&cpd
);
1091 * Performance optimisation: We try to check for IOCTL's that
1092 * don't need the USB reference first. Then we grab the USB
1093 * reference if we need it!
1095 err
= usb_ref_device(cpd
, &refs
, 0 /* no uref */ );
1099 fflags
= cpd
->fflags
;
1101 f
= NULL
; /* set default value */
1102 err
= ENOIOCTL
; /* set default value */
1104 if (fflags
& FWRITE
) {
1106 err
= usb_ioctl_f_sub(f
, cmd
, addr
, td
);
1108 if (fflags
& FREAD
) {
1110 err
= usb_ioctl_f_sub(f
, cmd
, addr
, td
);
1112 KASSERT(f
!= NULL
, ("fifo not found"));
1113 if (err
!= ENOIOCTL
)
1116 err
= (f
->methods
->f_ioctl
) (f
, cmd
, addr
, fflags
);
1118 DPRINTFN(2, "f_ioctl cmd 0x%lx = %d\n", cmd
, err
);
1120 if (err
!= ENOIOCTL
)
1123 if (usb_usb_ref_device(cpd
, &refs
)) {
1128 err
= (f
->methods
->f_ioctl_post
) (f
, cmd
, addr
, fflags
);
1130 DPRINTFN(2, "f_ioctl_post cmd 0x%lx = %d\n", cmd
, err
);
1132 if (err
== ENOIOCTL
)
1138 /* Wait for re-enumeration, if any */
1140 while (f
->udev
->re_enumerate_wait
!= USB_RE_ENUM_DONE
) {
1142 usb_unref_device(cpd
, &refs
);
1144 usb_pause_mtx(NULL
, hz
/ 128);
1146 if (usb_ref_device(cpd
, &refs
, 1 /* need uref */)) {
1153 usb_unref_device(cpd
, &refs
);
1157 static struct filterops usb_filtops_read
=
1158 { FILTEROP_ISFD
| FILTEROP_MPSAFE
, NULL
, usb_filter_detach
, usb_filter_read
};
1160 static struct filterops usb_filtops_write
=
1161 { FILTEROP_ISFD
| FILTEROP_MPSAFE
, NULL
, usb_filter_detach
, usb_filter_write
};
1164 usb_kqfilter(struct dev_kqfilter_args
*ap
)
1166 struct knote
*kn
= ap
->a_kn
;
1167 struct klist
*klist
;
1169 struct usb_cdev_refdata refs
;
1170 struct usb_cdev_privdata
* cpd
;
1173 err
= devfs_get_cdevpriv(ap
->a_fp
, (void **)&cpd
);
1176 err
= usb_ref_device(cpd
, &refs
, 0 /* no uref */ );
1181 fflags
= cpd
->fflags
;
1183 switch(kn
->kn_filter
) {
1186 if(fflags
& FREAD
) {
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_read
;
1192 ap
->a_result
= EOPNOTSUPP
;
1198 if(fflags
& FWRITE
) {
1199 lockmgr(f
->priv_lock
, LK_EXCLUSIVE
);
1200 f
->flag_isselect
= 1;
1201 lockmgr(f
->priv_lock
, LK_RELEASE
);
1202 kn
->kn_fop
= &usb_filtops_write
;
1204 ap
->a_result
= EOPNOTSUPP
;
1209 DPRINTF("unsupported kqfilter requested\n");
1210 ap
->a_result
= EOPNOTSUPP
;
1211 usb_unref_device(cpd
, &refs
);
1215 kn
->kn_hook
= (caddr_t
)cpd
;
1216 klist
= &f
->selinfo
.ki_note
;
1217 knote_insert(klist
, kn
);
1219 usb_unref_device(cpd
, &refs
);
1224 usb_filter_detach(struct knote
*kn
)
1227 struct usb_cdev_privdata
* cpd
= (struct usb_cdev_privdata
*)kn
->kn_hook
;
1228 struct usb_cdev_refdata refs
;
1229 struct klist
*klist
;
1234 * The associated cpd has vanished.
1240 err
= usb_ref_device(cpd
, &refs
, 0 /* no uref */ );
1245 switch(kn
->kn_filter
) {
1253 /* Better safe than sorry? (mpf) */
1254 panic("Trying to detach unknown filter");
1258 lockmgr(f
->priv_lock
, LK_EXCLUSIVE
);
1260 /* removed check for f->flag_isselect, because
1261 it is racing completion in the filter leading
1262 to invalid data in the fifo knote list */
1263 klist
= &f
->selinfo
.ki_note
;
1264 knote_remove(klist
, kn
);
1265 f
->flag_isselect
= 0;
1267 lockmgr(f
->priv_lock
, LK_RELEASE
);
1269 usb_unref_device(cpd
, &refs
);
1273 usb_filter_read(struct knote
*kn
, long hint
)
1276 struct usb_cdev_privdata
*cpd
= (struct usb_cdev_privdata
*)kn
->kn_hook
;
1277 struct usb_cdev_refdata refs
;
1279 int err
,locked
,ready
= 0;
1284 * The associated file has been closed.
1287 kn
->kn_flags
|= EV_ERROR
;
1291 err
= usb_ref_device(cpd
, &refs
, 0 /* no uref */ );
1293 kn
->kn_flags
|= EV_ERROR
;
1297 For some reason this function is called both
1298 with the priv_lock held and with the priv_lock
1299 not held. We need to find out from where and
1303 locked
= lockowned(f
->priv_lock
);
1305 lockmgr(f
->priv_lock
, LK_EXCLUSIVE
);
1307 if (!refs
.is_usbfs
) {
1308 if (f
->flag_iserror
) {
1309 /* we got an error */
1310 kn
->kn_flags
|= EV_ERROR
;
1313 /* start read if not running */
1314 (f
->methods
->f_start_read
)(f
);
1315 /* check if any packets are available */
1316 USB_IF_POLL(&f
->used_q
, m
);
1322 if (f
->flag_iscomplete
) {
1330 lockmgr(f
->priv_lock
, LK_RELEASE
);
1332 usb_unref_device(cpd
, &refs
);
1334 DPRINTFN(3,"ready %d\n", ready
);
1339 usb_filter_write(struct knote
*kn
, long hint
)
1342 struct usb_cdev_privdata
*cpd
= (struct usb_cdev_privdata
*)kn
->kn_hook
;
1343 struct usb_cdev_refdata refs
;
1345 int err
,locked
,ready
= 0;
1350 * The associated file has been closed.
1353 kn
->kn_flags
|= EV_ERROR
;
1357 err
= usb_ref_device(cpd
, &refs
, 0 /* no uref */ );
1359 kn
->kn_flags
|= EV_ERROR
;
1363 For some reason this function is called both
1364 with the priv_lock held and with the priv_lock
1365 not held. We need to find out from where and
1369 locked
= lockowned(f
->priv_lock
);
1371 lockmgr(f
->priv_lock
, LK_EXCLUSIVE
);
1373 if (!refs
.is_usbfs
) {
1374 if (f
->flag_iserror
) {
1375 /* we got an error */
1376 kn
->kn_flags
|= EV_ERROR
;
1379 if (f
->queue_data
== NULL
) {
1381 * start write transfer, if not
1384 (f
->methods
->f_start_write
) (f
);
1386 /* check if any packets are available */
1387 USB_IF_POLL(&f
->free_q
, m
);
1392 if (f
->flag_iscomplete
) {
1400 lockmgr(f
->priv_lock
, LK_RELEASE
);
1402 usb_unref_device(cpd
, &refs
);
1404 DPRINTFN(3,"ready %d\n", ready
);
1409 /* This is implemented above using kqfilter */
1412 usb_poll(struct cdev
* dev
, int events
, struct thread
* td
)
1414 struct usb_cdev_refdata refs
;
1415 struct usb_cdev_privdata
* cpd
;
1418 int fflags
, revents
;
1420 if (devfs_get_cdevpriv((void **)&cpd
) != 0 ||
1421 usb_ref_device(cpd
, &refs
, 0) != 0)
1423 (POLLHUP
|POLLIN
|POLLRDNORM
|POLLOUT
|POLLWRNORM
));
1425 fflags
= cpd
->fflags
;
1427 /* Figure out who needs service */
1429 if ((events
& (POLLOUT
| POLLWRNORM
)) &&
1430 (fflags
& FWRITE
)) {
1434 lockmgr(f
->priv_lock
, LK_EXCLUSIVE
);
1436 if (!refs
.is_usbfs
) {
1437 if (f
->flag_iserror
) {
1438 /* we got an error */
1441 if (f
->queue_data
== NULL
) {
1443 * start write transfer, if not
1446 (f
->methods
->f_start_write
) (f
);
1448 /* check if any packets are available */
1449 USB_IF_POLL(&f
->free_q
, m
);
1452 if (f
->flag_iscomplete
) {
1460 revents
|= events
& (POLLOUT
| POLLWRNORM
);
1462 f
->flag_isselect
= 1;
1463 selrecord(td
, &f
->selinfo
);
1466 lockmgr(f
->priv_lock
);
1468 if ((events
& (POLLIN
| POLLRDNORM
)) &&
1473 lockmgr(f
->priv_lock
, LK_EXCLUSIVE
);
1475 if (!refs
.is_usbfs
) {
1476 if (f
->flag_iserror
) {
1477 /* we have and error */
1480 if (f
->queue_data
== NULL
) {
1482 * start read transfer, if not
1485 (f
->methods
->f_start_read
) (f
);
1487 /* check if any packets are available */
1488 USB_IF_POLL(&f
->used_q
, m
);
1491 if (f
->flag_iscomplete
) {
1499 revents
|= events
& (POLLIN
| POLLRDNORM
);
1501 f
->flag_isselect
= 1;
1502 selrecord(td
, &f
->selinfo
);
1504 if (!refs
.is_usbfs
) {
1505 /* start reading data */
1506 (f
->methods
->f_start_read
) (f
);
1510 lockmgr(f
->priv_lock
, LK_RELEASE
);
1512 usb_unref_device(cpd
, &refs
);
1518 usb_read(struct dev_read_args
*ap
)
1520 struct uio
*uio
= ap
->a_uio
;
1521 int ioflag
= ap
->a_ioflag
;
1522 struct usb_cdev_refdata refs
;
1523 struct usb_cdev_privdata
* cpd
;
1530 uint8_t tr_data
= 0;
1532 err
= devfs_get_cdevpriv(ap
->a_fp
, (void **)&cpd
);
1535 err
= usb_ref_device(cpd
, &refs
, 0 /* no uref */ );
1539 fflags
= cpd
->fflags
;
1543 /* should not happen */
1544 usb_unref_device(cpd
, &refs
);
1548 resid
= uio
->uio_resid
;
1550 lockmgr(f
->priv_lock
, LK_EXCLUSIVE
);
1552 /* check for permanent read error */
1553 if (f
->flag_iserror
) {
1557 /* check if USB-FS interface is active */
1558 if (refs
.is_usbfs
) {
1560 * The queue is used for events that should be
1561 * retrieved using the "USB_FS_COMPLETE" ioctl.
1566 while (uio
->uio_resid
> 0) {
1568 USB_IF_DEQUEUE(&f
->used_q
, m
);
1572 /* start read transfer, if not already started */
1574 (f
->methods
->f_start_read
) (f
);
1576 if (ioflag
& IO_NDELAY
) {
1578 /* return length before error */
1584 DPRINTF("sleeping\n");
1586 err
= usb_fifo_wait(f
);
1592 if (f
->methods
->f_filter_read
) {
1594 * Sometimes it is convenient to process data at the
1595 * expense of a userland process instead of a kernel
1598 (f
->methods
->f_filter_read
) (f
, m
);
1602 io_len
= MIN(m
->cur_data_len
, uio
->uio_resid
);
1604 DPRINTFN(2, "transfer %d bytes from %p\n",
1605 io_len
, m
->cur_data_ptr
);
1607 err
= usb_fifo_uiomove(f
,
1608 m
->cur_data_ptr
, io_len
, uio
);
1610 m
->cur_data_len
-= io_len
;
1611 m
->cur_data_ptr
+= io_len
;
1613 if (m
->cur_data_len
== 0) {
1615 uint8_t last_packet
;
1617 last_packet
= m
->last_packet
;
1619 USB_IF_ENQUEUE(&f
->free_q
, m
);
1626 USB_IF_PREPEND(&f
->used_q
, m
);
1635 lockmgr(f
->priv_lock
, LK_RELEASE
);
1637 usb_unref_device(cpd
, &refs
);
1643 usb_write(struct dev_write_args
*ap
)
1645 struct uio
*uio
= ap
->a_uio
;
1646 int ioflag
= ap
->a_ioflag
;
1647 struct usb_cdev_refdata refs
;
1648 struct usb_cdev_privdata
* cpd
;
1656 uint8_t tr_data
= 0;
1660 err
= devfs_get_cdevpriv(ap
->a_fp
, (void **)&cpd
);
1663 err
= usb_ref_device(cpd
, &refs
, 0 /* no uref */ );
1667 fflags
= cpd
->fflags
;
1671 /* should not happen */
1672 usb_unref_device(cpd
, &refs
);
1675 resid
= uio
->uio_resid
;
1677 lockmgr(f
->priv_lock
, LK_EXCLUSIVE
);
1679 /* check for permanent write error */
1680 if (f
->flag_iserror
) {
1684 /* check if USB-FS interface is active */
1685 if (refs
.is_usbfs
) {
1687 * The queue is used for events that should be
1688 * retrieved using the "USB_FS_COMPLETE" ioctl.
1693 if (f
->queue_data
== NULL
) {
1694 /* start write transfer, if not already started */
1695 (f
->methods
->f_start_write
) (f
);
1697 /* we allow writing zero length data */
1699 USB_IF_DEQUEUE(&f
->free_q
, m
);
1703 if (ioflag
& IO_NDELAY
) {
1705 /* return length before error */
1711 DPRINTF("sleeping\n");
1713 err
= usb_fifo_wait(f
);
1721 if (f
->flag_have_fragment
== 0) {
1723 io_len
= m
->cur_data_len
;
1724 pdata
= m
->cur_data_ptr
;
1725 if (io_len
> uio
->uio_resid
)
1726 io_len
= uio
->uio_resid
;
1727 m
->cur_data_len
= io_len
;
1729 io_len
= m
->max_data_len
- m
->cur_data_len
;
1730 pdata
= m
->cur_data_ptr
+ m
->cur_data_len
;
1731 if (io_len
> uio
->uio_resid
)
1732 io_len
= uio
->uio_resid
;
1733 m
->cur_data_len
+= io_len
;
1736 DPRINTFN(2, "transfer %d bytes to %p\n",
1739 err
= usb_fifo_uiomove(f
, pdata
, io_len
, uio
);
1742 f
->flag_have_fragment
= 0;
1743 USB_IF_ENQUEUE(&f
->free_q
, m
);
1747 /* check if the buffer is ready to be transmitted */
1749 if ((f
->flag_write_defrag
== 0) ||
1750 (m
->cur_data_len
== m
->max_data_len
)) {
1751 f
->flag_have_fragment
= 0;
1754 * Check for write filter:
1756 * Sometimes it is convenient to process data
1757 * at the expense of a userland process
1758 * instead of a kernel process.
1760 if (f
->methods
->f_filter_write
) {
1761 (f
->methods
->f_filter_write
) (f
, m
);
1764 /* Put USB mbuf in the used queue */
1765 USB_IF_ENQUEUE(&f
->used_q
, m
);
1767 /* Start writing data, if not already started */
1768 (f
->methods
->f_start_write
) (f
);
1770 /* Wait for more data or close */
1771 f
->flag_have_fragment
= 1;
1772 USB_IF_PREPEND(&f
->free_q
, m
);
1775 } while (uio
->uio_resid
> 0);
1777 lockmgr(f
->priv_lock
, LK_RELEASE
);
1779 usb_unref_device(cpd
, &refs
);
1785 usb_static_open(struct dev_open_args
*ap
)
1791 usb_static_close(struct dev_close_args
*ap
)
1797 usb_static_ioctl(struct dev_ioctl_args
*ap
)
1799 u_long cmd
= ap
->a_cmd
;
1800 caddr_t data
= ap
->a_data
;
1801 struct thread
*td
= curthread
; /* XXX: curthread the correct choice? */
1802 int fflag
= ap
->a_fflag
;
1804 struct usb_read_dir
*urd
;
1812 err
= usb_read_symlink(u
.urd
->urd_data
,
1813 u
.urd
->urd_startentry
, u
.urd
->urd_maxlen
);
1815 case USB_DEV_QUIRK_GET
:
1816 case USB_QUIRK_NAME_GET
:
1817 case USB_DEV_QUIRK_ADD
:
1818 case USB_DEV_QUIRK_REMOVE
:
1819 err
= usb_quirk_ioctl_p(cmd
, data
, fflag
, td
);
1821 case USB_GET_TEMPLATE
:
1822 *(int *)data
= usb_template
;
1825 case USB_SET_TEMPLATE
:
1826 err
= priv_check(curthread
, PRIV_DRIVER
);
1829 usb_template
= *(int *)data
;
1839 usb_fifo_uiomove(struct usb_fifo
*f
, void *cp
,
1840 int n
, struct uio
*uio
)
1844 lockmgr(f
->priv_lock
, LK_RELEASE
);
1847 * "uiomove()" can sleep so one needs to make a wrapper,
1848 * exiting the mutex and checking things:
1850 error
= uiomove(cp
, n
, uio
);
1852 lockmgr(f
->priv_lock
, LK_EXCLUSIVE
);
1858 usb_fifo_wait(struct usb_fifo
*f
)
1862 KKASSERT(lockowned(f
->priv_lock
));
1864 if (f
->flag_iserror
) {
1868 f
->flag_sleeping
= 1;
1870 err
= cv_wait_sig(&f
->cv_io
, f
->priv_lock
);
1872 if (f
->flag_iserror
) {
1880 usb_fifo_signal(struct usb_fifo
*f
)
1882 if (f
->flag_sleeping
) {
1883 f
->flag_sleeping
= 0;
1884 cv_broadcast(&f
->cv_io
);
1889 usb_fifo_wakeup(struct usb_fifo
*f
)
1893 KNOTE(&f
->selinfo
.ki_note
, 0);
1895 if (f
->flag_isselect
) {
1896 wakeup(&f
->selinfo
.ki_note
);
1898 if (f
->async_p
!= NULL
&& lwkt_trytoken(&f
->async_p
->p_token
)) {
1899 ksignal(f
->async_p
, SIGIO
);
1900 lwkt_reltoken(&f
->async_p
->p_token
);
1905 usb_fifo_dummy_open(struct usb_fifo
*fifo
, int fflags
)
1911 usb_fifo_dummy_close(struct usb_fifo
*fifo
, int fflags
)
1917 usb_fifo_dummy_ioctl(struct usb_fifo
*fifo
, u_long cmd
, void *addr
, int fflags
)
1923 usb_fifo_dummy_cmd(struct usb_fifo
*fifo
)
1925 fifo
->flag_flushing
= 0; /* not flushing */
1929 usb_fifo_check_methods(struct usb_fifo_methods
*pm
)
1931 /* check that all callback functions are OK */
1933 if (pm
->f_open
== NULL
)
1934 pm
->f_open
= &usb_fifo_dummy_open
;
1936 if (pm
->f_close
== NULL
)
1937 pm
->f_close
= &usb_fifo_dummy_close
;
1939 if (pm
->f_ioctl
== NULL
)
1940 pm
->f_ioctl
= &usb_fifo_dummy_ioctl
;
1942 if (pm
->f_ioctl_post
== NULL
)
1943 pm
->f_ioctl_post
= &usb_fifo_dummy_ioctl
;
1945 if (pm
->f_start_read
== NULL
)
1946 pm
->f_start_read
= &usb_fifo_dummy_cmd
;
1948 if (pm
->f_stop_read
== NULL
)
1949 pm
->f_stop_read
= &usb_fifo_dummy_cmd
;
1951 if (pm
->f_start_write
== NULL
)
1952 pm
->f_start_write
= &usb_fifo_dummy_cmd
;
1954 if (pm
->f_stop_write
== NULL
)
1955 pm
->f_stop_write
= &usb_fifo_dummy_cmd
;
1958 /*------------------------------------------------------------------------*
1961 * The following function will create a duplex FIFO.
1966 *------------------------------------------------------------------------*/
1968 usb_fifo_attach(struct usb_device
*udev
, void *priv_sc
,
1969 struct lock
*priv_lock
, struct usb_fifo_methods
*pm
,
1970 struct usb_fifo_sc
*f_sc
, uint16_t unit
, int16_t subunit
,
1971 uint8_t iface_index
, uid_t uid
, gid_t gid
, int mode
)
1973 struct usb_fifo
*f_tx
;
1974 struct usb_fifo
*f_rx
;
1978 f_sc
->fp
[USB_FIFO_TX
] = NULL
;
1979 f_sc
->fp
[USB_FIFO_RX
] = NULL
;
1984 /* check the methods */
1985 usb_fifo_check_methods(pm
);
1987 if (priv_lock
== NULL
) {
1988 DPRINTF("null priv_lock set\n");
1991 /* search for a free FIFO slot */
1992 for (n
= 0;; n
+= 2) {
1994 if (n
== USB_FIFO_MAX
) {
1995 /* end of FIFOs reached */
1998 /* Check for TX FIFO */
1999 if (udev
->fifo
[n
+ USB_FIFO_TX
] != NULL
) {
2002 /* Check for RX FIFO */
2003 if (udev
->fifo
[n
+ USB_FIFO_RX
] != NULL
) {
2009 f_tx
= usb_fifo_alloc(priv_lock
);
2010 f_rx
= usb_fifo_alloc(priv_lock
);
2012 if ((f_tx
== NULL
) || (f_rx
== NULL
)) {
2013 usb_fifo_free(f_tx
);
2014 usb_fifo_free(f_rx
);
2017 /* initialise FIFO structures */
2019 f_tx
->fifo_index
= n
+ USB_FIFO_TX
;
2020 f_tx
->dev_ep_index
= -1;
2021 f_tx
->priv_lock
= priv_lock
;
2022 f_tx
->priv_sc0
= priv_sc
;
2024 f_tx
->iface_index
= iface_index
;
2027 f_rx
->fifo_index
= n
+ USB_FIFO_RX
;
2028 f_rx
->dev_ep_index
= -1;
2029 f_rx
->priv_lock
= priv_lock
;
2030 f_rx
->priv_sc0
= priv_sc
;
2032 f_rx
->iface_index
= iface_index
;
2035 f_sc
->fp
[USB_FIFO_TX
] = f_tx
;
2036 f_sc
->fp
[USB_FIFO_RX
] = f_rx
;
2038 lockmgr(&usb_ref_lock
, LK_EXCLUSIVE
);
2039 udev
->fifo
[f_tx
->fifo_index
] = f_tx
;
2040 udev
->fifo
[f_rx
->fifo_index
] = f_rx
;
2041 lockmgr(&usb_ref_lock
, LK_RELEASE
);
2043 for (n
= 0; n
!= 4; n
++) {
2045 if (pm
->basename
[n
] == NULL
) {
2049 if (ksnprintf(devname
, sizeof(devname
),
2050 "%s%u%s", pm
->basename
[n
],
2051 unit
, pm
->postfix
[n
] ?
2052 pm
->postfix
[n
] : "")) {
2056 if (ksnprintf(devname
, sizeof(devname
),
2057 "%s%u.%u%s", pm
->basename
[n
],
2058 unit
, subunit
, pm
->postfix
[n
] ?
2059 pm
->postfix
[n
] : "")) {
2065 * Distribute the symbolic links into two FIFO structures:
2068 f_rx
->symlink
[n
/ 2] =
2069 usb_alloc_symlink(devname
);
2071 f_tx
->symlink
[n
/ 2] =
2072 usb_alloc_symlink(devname
);
2075 /* Create the device */
2076 f_sc
->dev
= usb_make_dev(udev
, devname
, -1,
2077 f_tx
->fifo_index
& f_rx
->fifo_index
,
2078 FREAD
|FWRITE
, uid
, gid
, mode
);
2081 DPRINTFN(2, "attached %p/%p\n", f_tx
, f_rx
);
2085 /*------------------------------------------------------------------------*
2086 * usb_fifo_alloc_buffer
2091 *------------------------------------------------------------------------*/
2093 usb_fifo_alloc_buffer(struct usb_fifo
*f
, usb_size_t bufsize
,
2096 usb_fifo_free_buffer(f
);
2098 /* allocate an endpoint */
2099 f
->free_q
.ifq_maxlen
= nbuf
;
2100 f
->used_q
.ifq_maxlen
= nbuf
;
2102 f
->queue_data
= usb_alloc_mbufs(
2103 M_USBDEV
, &f
->free_q
, bufsize
, nbuf
);
2105 if ((f
->queue_data
== NULL
) && bufsize
&& nbuf
) {
2108 return (0); /* success */
2111 /*------------------------------------------------------------------------*
2112 * usb_fifo_free_buffer
2114 * This function will free the buffers associated with a FIFO. This
2115 * function can be called multiple times in a row.
2116 *------------------------------------------------------------------------*/
2118 usb_fifo_free_buffer(struct usb_fifo
*f
)
2120 if (f
->queue_data
) {
2121 /* free old buffer */
2122 kfree(f
->queue_data
, M_USBDEV
);
2123 f
->queue_data
= NULL
;
2127 memset(&f
->free_q
, 0, sizeof(f
->free_q
));
2128 memset(&f
->used_q
, 0, sizeof(f
->used_q
));
2132 usb_fifo_detach(struct usb_fifo_sc
*f_sc
)
2137 usb_fifo_free(f_sc
->fp
[USB_FIFO_TX
]);
2138 usb_fifo_free(f_sc
->fp
[USB_FIFO_RX
]);
2140 f_sc
->fp
[USB_FIFO_TX
] = NULL
;
2141 f_sc
->fp
[USB_FIFO_RX
] = NULL
;
2143 usb_destroy_dev(f_sc
->dev
);
2147 DPRINTFN(2, "detached %p\n", f_sc
);
2151 usb_fifo_put_bytes_max(struct usb_fifo
*f
)
2156 USB_IF_POLL(&f
->free_q
, m
);
2159 len
= m
->max_data_len
;
2166 /*------------------------------------------------------------------------*
2170 * 0 - normal operation
2171 * 1 - set last packet flag to enforce framing
2172 *------------------------------------------------------------------------*/
2174 usb_fifo_put_data(struct usb_fifo
*f
, struct usb_page_cache
*pc
,
2175 usb_frlength_t offset
, usb_frlength_t len
, uint8_t what
)
2178 usb_frlength_t io_len
;
2180 while (len
|| (what
== 1)) {
2182 USB_IF_DEQUEUE(&f
->free_q
, m
);
2187 io_len
= MIN(len
, m
->cur_data_len
);
2189 usbd_copy_out(pc
, offset
, m
->cur_data_ptr
, io_len
);
2191 m
->cur_data_len
= io_len
;
2195 if ((len
== 0) && (what
== 1)) {
2198 USB_IF_ENQUEUE(&f
->used_q
, m
);
2202 if ((len
== 0) || (what
== 1)) {
2212 usb_fifo_put_data_linear(struct usb_fifo
*f
, void *ptr
,
2213 usb_size_t len
, uint8_t what
)
2218 while (len
|| (what
== 1)) {
2220 USB_IF_DEQUEUE(&f
->free_q
, m
);
2225 io_len
= MIN(len
, m
->cur_data_len
);
2227 memcpy(m
->cur_data_ptr
, ptr
, io_len
);
2229 m
->cur_data_len
= io_len
;
2230 ptr
= USB_ADD_BYTES(ptr
, io_len
);
2233 if ((len
== 0) && (what
== 1)) {
2236 USB_IF_ENQUEUE(&f
->used_q
, m
);
2240 if ((len
== 0) || (what
== 1)) {
2250 usb_fifo_put_data_buffer(struct usb_fifo
*f
, void *ptr
, usb_size_t len
)
2254 USB_IF_DEQUEUE(&f
->free_q
, m
);
2257 m
->cur_data_len
= len
;
2258 m
->cur_data_ptr
= ptr
;
2259 USB_IF_ENQUEUE(&f
->used_q
, m
);
2267 usb_fifo_put_data_error(struct usb_fifo
*f
)
2269 f
->flag_iserror
= 1;
2273 /*------------------------------------------------------------------------*
2277 * 0 - normal operation
2278 * 1 - only get one "usb_mbuf"
2282 * 1 - data in buffer
2283 *------------------------------------------------------------------------*/
2285 usb_fifo_get_data(struct usb_fifo
*f
, struct usb_page_cache
*pc
,
2286 usb_frlength_t offset
, usb_frlength_t len
, usb_frlength_t
*actlen
,
2290 usb_frlength_t io_len
;
2291 uint8_t tr_data
= 0;
2297 USB_IF_DEQUEUE(&f
->used_q
, m
);
2303 io_len
= MIN(len
, m
->cur_data_len
);
2305 usbd_copy_in(pc
, offset
, m
->cur_data_ptr
, io_len
);
2309 actlen
[0] += io_len
;
2310 m
->cur_data_ptr
+= io_len
;
2311 m
->cur_data_len
-= io_len
;
2313 if ((m
->cur_data_len
== 0) || (what
== 1)) {
2314 USB_IF_ENQUEUE(&f
->free_q
, m
);
2322 USB_IF_PREPEND(&f
->used_q
, m
);
2328 /* wait for data to be written out */
2331 if (f
->flag_flushing
) {
2332 /* check if we should send a short packet */
2333 if (f
->flag_short
!= 0) {
2338 /* flushing complete */
2339 f
->flag_flushing
= 0;
2352 usb_fifo_get_data_linear(struct usb_fifo
*f
, void *ptr
,
2353 usb_size_t len
, usb_size_t
*actlen
, uint8_t what
)
2357 uint8_t tr_data
= 0;
2363 USB_IF_DEQUEUE(&f
->used_q
, m
);
2369 io_len
= MIN(len
, m
->cur_data_len
);
2371 memcpy(ptr
, m
->cur_data_ptr
, io_len
);
2374 ptr
= USB_ADD_BYTES(ptr
, io_len
);
2375 actlen
[0] += io_len
;
2376 m
->cur_data_ptr
+= io_len
;
2377 m
->cur_data_len
-= io_len
;
2379 if ((m
->cur_data_len
== 0) || (what
== 1)) {
2380 USB_IF_ENQUEUE(&f
->free_q
, m
);
2388 USB_IF_PREPEND(&f
->used_q
, m
);
2394 /* wait for data to be written out */
2397 if (f
->flag_flushing
) {
2398 /* check if we should send a short packet */
2399 if (f
->flag_short
!= 0) {
2404 /* flushing complete */
2405 f
->flag_flushing
= 0;
2418 usb_fifo_get_data_buffer(struct usb_fifo
*f
, void **pptr
, usb_size_t
*plen
)
2422 USB_IF_POLL(&f
->used_q
, m
);
2425 *plen
= m
->cur_data_len
;
2426 *pptr
= m
->cur_data_ptr
;
2434 usb_fifo_get_data_error(struct usb_fifo
*f
)
2436 f
->flag_iserror
= 1;
2440 /*------------------------------------------------------------------------*
2445 * Else: Pointer to symlink entry
2446 *------------------------------------------------------------------------*/
2447 struct usb_symlink
*
2448 usb_alloc_symlink(const char *target
)
2450 struct usb_symlink
*ps
;
2452 ps
= kmalloc(sizeof(*ps
), M_USBDEV
, M_WAITOK
);
2456 /* XXX no longer needed */
2457 strlcpy(ps
->src_path
, target
, sizeof(ps
->src_path
));
2458 ps
->src_len
= strlen(ps
->src_path
);
2459 strlcpy(ps
->dst_path
, target
, sizeof(ps
->dst_path
));
2460 ps
->dst_len
= strlen(ps
->dst_path
);
2462 lockmgr(&usb_sym_lock
, LK_EXCLUSIVE
);
2463 TAILQ_INSERT_TAIL(&usb_sym_head
, ps
, sym_entry
);
2464 lockmgr(&usb_sym_lock
, LK_RELEASE
);
2468 /*------------------------------------------------------------------------*
2470 *------------------------------------------------------------------------*/
2472 usb_free_symlink(struct usb_symlink
*ps
)
2477 lockmgr(&usb_sym_lock
, LK_EXCLUSIVE
);
2478 TAILQ_REMOVE(&usb_sym_head
, ps
, sym_entry
);
2479 lockmgr(&usb_sym_lock
, LK_RELEASE
);
2481 kfree(ps
, M_USBDEV
);
2484 /*------------------------------------------------------------------------*
2490 *------------------------------------------------------------------------*/
2492 usb_read_symlink(uint8_t *user_ptr
, uint32_t startentry
, uint32_t user_len
)
2494 struct usb_symlink
*ps
;
2500 lockmgr(&usb_sym_lock
, LK_EXCLUSIVE
);
2502 TAILQ_FOREACH(ps
, &usb_sym_head
, sym_entry
) {
2505 * Compute total length of source and destination symlink
2506 * strings pluss one length byte and two NUL bytes:
2508 temp
= ps
->src_len
+ ps
->dst_len
+ 3;
2512 * Skip entry because this length cannot fit
2517 if (startentry
!= 0) {
2518 /* decrement read offset */
2522 if (temp
> user_len
) {
2523 /* out of buffer space */
2528 /* copy out total length */
2530 error
= copyout(&len
,
2531 USB_ADD_BYTES(user_ptr
, delta
), 1);
2537 /* copy out source string */
2539 error
= copyout(ps
->src_path
,
2540 USB_ADD_BYTES(user_ptr
, delta
), ps
->src_len
);
2545 delta
+= ps
->src_len
;
2546 error
= copyout(&len
,
2547 USB_ADD_BYTES(user_ptr
, delta
), 1);
2553 /* copy out destination string */
2555 error
= copyout(ps
->dst_path
,
2556 USB_ADD_BYTES(user_ptr
, delta
), ps
->dst_len
);
2561 delta
+= ps
->dst_len
;
2562 error
= copyout(&len
,
2563 USB_ADD_BYTES(user_ptr
, delta
), 1);
2572 /* a zero length entry indicates the end */
2574 if ((user_len
!= 0) && (error
== 0)) {
2578 error
= copyout(&len
,
2579 USB_ADD_BYTES(user_ptr
, delta
), 1);
2581 lockmgr(&usb_sym_lock
, LK_RELEASE
);
2586 usb_fifo_set_close_zlp(struct usb_fifo
*f
, uint8_t onoff
)
2591 /* send a Zero Length Packet, ZLP, before close */
2592 f
->flag_short
= onoff
;
2596 usb_fifo_set_write_defrag(struct usb_fifo
*f
, uint8_t onoff
)
2601 /* defrag written data */
2602 f
->flag_write_defrag
= onoff
;
2603 /* reset defrag state */
2604 f
->flag_have_fragment
= 0;
2608 usb_fifo_softc(struct usb_fifo
*f
)
2610 return (f
->priv_sc0
);
2612 #endif /* USB_HAVE_UGEN */