3 * Copyright (c) 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
31 #include <sys/vnode.h>
33 #include <sys/signalvar.h>
35 #include <sys/fcntl.h>
44 typedef int (usb2_fifo_open_t)(struct usb2_fifo *fifo, int fflags);
45 typedef void (usb2_fifo_close_t)(struct usb2_fifo *fifo, int fflags);
46 typedef int (usb2_fifo_ioctl_t)(struct usb2_fifo *fifo, u_long cmd, void *addr, int fflags);
47 typedef void (usb2_fifo_cmd_t)(struct usb2_fifo *fifo);
48 typedef void (usb2_fifo_filter_t)(struct usb2_fifo *fifo, struct usb2_mbuf *m);
51 TAILQ_ENTRY(usb2_symlink) sym_entry;
52 char src_path[32]; /* Source path - including terminating
54 char dst_path[32]; /* Destination path - including
56 uint8_t src_len; /* String length */
57 uint8_t dst_len; /* String length */
61 * Locking note for the following functions. All the
62 * "usb2_fifo_cmd_t" and "usb2_fifo_filter_t" functions are called
63 * locked. The others are called unlocked.
65 struct usb2_fifo_methods {
66 usb2_fifo_open_t *f_open;
67 usb2_fifo_close_t *f_close;
68 usb2_fifo_ioctl_t *f_ioctl;
70 * NOTE: The post-ioctl callback is called after the USB reference
71 * gets locked in the IOCTL handler:
73 usb2_fifo_ioctl_t *f_ioctl_post;
74 usb2_fifo_cmd_t *f_start_read;
75 usb2_fifo_cmd_t *f_stop_read;
76 usb2_fifo_cmd_t *f_start_write;
77 usb2_fifo_cmd_t *f_stop_write;
78 usb2_fifo_filter_t *f_filter_read;
79 usb2_fifo_filter_t *f_filter_write;
80 const char *basename[4];
81 const char *postfix[4];
85 * Private per-device information.
87 struct usb2_cdev_privdata {
89 struct usb2_device *udev;
90 struct usb2_interface *iface;
91 struct usb2_fifo *rxfifo;
92 struct usb2_fifo *txfifo;
93 int bus_index; /* bus index */
94 int dev_index; /* device index */
95 int iface_index; /* interface index */
96 int ep_addr; /* endpoint address */
97 uint8_t fifo_index; /* FIFO index */
98 uint8_t is_read; /* location has read access */
99 uint8_t is_write; /* location has write access */
100 uint8_t is_uref; /* USB refcount decr. needed */
101 uint8_t is_usbfs; /* USB-FS is active */
105 struct usb2_fs_privdata {
114 LIST_ENTRY(usb2_fs_privdata) pd_next;
118 * Most of the fields in the "usb2_fifo" structure are used by the
119 * generic USB access layer.
122 struct usb2_ifqueue free_q;
123 struct usb2_ifqueue used_q;
124 struct selinfo selinfo;
127 struct usb2_fifo_methods *methods;
128 struct usb2_symlink *symlink[2];/* our symlinks */
129 struct proc *async_p; /* process that wants SIGIO */
130 struct usb2_fs_endpoint *fs_ep_ptr;
131 struct usb2_device *udev;
132 struct usb2_xfer *xfer[2];
133 struct usb2_xfer **fs_xfer;
134 struct mtx *priv_mtx; /* client data */
135 int opened; /* set if FIFO is opened by a FILE */
136 void *priv_sc0; /* client data */
137 void *priv_sc1; /* client data */
139 uint32_t timeout; /* timeout in milliseconds */
140 uint32_t bufsize; /* BULK and INTERRUPT buffer size */
141 uint16_t nframes; /* for isochronous mode */
142 uint16_t dev_ep_index; /* our device endpoint index */
143 uint8_t flag_sleeping; /* set if FIFO is sleeping */
144 uint8_t flag_iscomplete; /* set if a USB transfer is complete */
145 uint8_t flag_iserror; /* set if FIFO error happened */
146 uint8_t flag_isselect; /* set if FIFO is selected */
147 uint8_t flag_flushing; /* set if FIFO is flushing data */
148 uint8_t flag_short; /* set if short_ok or force_short
149 * transfer flags should be set */
150 uint8_t flag_stall; /* set if clear stall should be run */
151 uint8_t iface_index; /* set to the interface we belong to */
152 uint8_t fifo_index; /* set to the FIFO index in "struct
155 uint8_t fifo_zlp; /* zero length packet count */
157 #define USB_FIFO_REF_MAX 0xFF
160 struct usb2_fifo_sc {
161 struct usb2_fifo *fp[2];
165 extern struct cdevsw usb2_devsw;
167 int usb2_fifo_wait(struct usb2_fifo *fifo);
168 void usb2_fifo_signal(struct usb2_fifo *fifo);
169 int usb2_fifo_alloc_buffer(struct usb2_fifo *f, uint32_t bufsize,
171 void usb2_fifo_free_buffer(struct usb2_fifo *f);
172 int usb2_fifo_attach(struct usb2_device *udev, void *priv_sc,
173 struct mtx *priv_mtx, struct usb2_fifo_methods *pm,
174 struct usb2_fifo_sc *f_sc, uint16_t unit, uint16_t subunit,
175 uint8_t iface_index, uid_t uid, gid_t gid, int mode);
176 void usb2_fifo_detach(struct usb2_fifo_sc *f_sc);
177 uint32_t usb2_fifo_put_bytes_max(struct usb2_fifo *fifo);
178 void usb2_fifo_put_data(struct usb2_fifo *fifo, struct usb2_page_cache *pc,
179 uint32_t offset, uint32_t len, uint8_t what);
180 void usb2_fifo_put_data_linear(struct usb2_fifo *fifo, void *ptr,
181 uint32_t len, uint8_t what);
182 uint8_t usb2_fifo_put_data_buffer(struct usb2_fifo *f, void *ptr, uint32_t len);
183 void usb2_fifo_put_data_error(struct usb2_fifo *fifo);
184 uint8_t usb2_fifo_get_data(struct usb2_fifo *fifo, struct usb2_page_cache *pc,
185 uint32_t offset, uint32_t len, uint32_t *actlen, uint8_t what);
186 uint8_t usb2_fifo_get_data_linear(struct usb2_fifo *fifo, void *ptr,
187 uint32_t len, uint32_t *actlen, uint8_t what);
188 uint8_t usb2_fifo_get_data_buffer(struct usb2_fifo *f, void **pptr,
190 void usb2_fifo_get_data_error(struct usb2_fifo *fifo);
191 uint8_t usb2_fifo_opened(struct usb2_fifo *fifo);
192 void usb2_fifo_free(struct usb2_fifo *f);
193 void usb2_fifo_reset(struct usb2_fifo *f);
194 void usb2_fifo_wakeup(struct usb2_fifo *f);
195 struct usb2_symlink *usb2_alloc_symlink(const char *target);
196 void usb2_free_symlink(struct usb2_symlink *ps);
197 int usb2_read_symlink(uint8_t *user_ptr, uint32_t startentry,
200 #endif /* _USB2_DEV_H_ */