xref: /freebsd/sys/dev/usb/usb_dev.h (revision d5fc25e5d6c52b306312784663ccad85923a9c76)
1 /* $FreeBSD$ */
2 /*-
3  * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26 
27 #ifndef _USB2_DEV_H_
28 #define	_USB2_DEV_H_
29 
30 #include <sys/file.h>
31 #include <sys/vnode.h>
32 #include <sys/poll.h>
33 #include <sys/signalvar.h>
34 #include <sys/conf.h>
35 #include <sys/fcntl.h>
36 #include <sys/proc.h>
37 
38 #define	USB_FIFO_TX 0
39 #define	USB_FIFO_RX 1
40 
41 struct usb_fifo;
42 struct usb_mbuf;
43 
44 typedef int (usb_fifo_open_t)(struct usb_fifo *fifo, int fflags);
45 typedef void (usb_fifo_close_t)(struct usb_fifo *fifo, int fflags);
46 typedef int (usb_fifo_ioctl_t)(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags);
47 typedef void (usb_fifo_cmd_t)(struct usb_fifo *fifo);
48 typedef void (usb_fifo_filter_t)(struct usb_fifo *fifo, struct usb_mbuf *m);
49 
50 struct usb_symlink {
51 	TAILQ_ENTRY(usb_symlink) sym_entry;
52 	char	src_path[32];		/* Source path - including terminating
53 					 * zero */
54 	char	dst_path[32];		/* Destination path - including
55 					 * terminating zero */
56 	uint8_t	src_len;		/* String length */
57 	uint8_t	dst_len;		/* String length */
58 };
59 
60 /*
61  * Locking note for the following functions.  All the
62  * "usb_fifo_cmd_t" and "usb_fifo_filter_t" functions are called
63  * locked. The others are called unlocked.
64  */
65 struct usb_fifo_methods {
66 	usb_fifo_open_t *f_open;
67 	usb_fifo_close_t *f_close;
68 	usb_fifo_ioctl_t *f_ioctl;
69 	/*
70 	 * NOTE: The post-ioctl callback is called after the USB reference
71 	 * gets locked in the IOCTL handler:
72 	 */
73 	usb_fifo_ioctl_t *f_ioctl_post;
74 	usb_fifo_cmd_t *f_start_read;
75 	usb_fifo_cmd_t *f_stop_read;
76 	usb_fifo_cmd_t *f_start_write;
77 	usb_fifo_cmd_t *f_stop_write;
78 	usb_fifo_filter_t *f_filter_read;
79 	usb_fifo_filter_t *f_filter_write;
80 	const char *basename[4];
81 	const char *postfix[4];
82 };
83 
84 /*
85  * Private per-device information.
86  */
87 struct usb_cdev_privdata {
88 	struct usb_bus		*bus;
89 	struct usb_device	*udev;
90 	struct usb_interface	*iface;
91 	int			bus_index;	/* bus index */
92 	int			dev_index;	/* device index */
93 	int			ep_addr;	/* endpoint address */
94 	int			fflags;
95 	uint8_t			fifo_index;	/* FIFO index */
96 };
97 
98 /*
99  * Private per-device and per-thread reference information
100  */
101 struct usb_cdev_refdata {
102 	struct usb_fifo		*rxfifo;
103 	struct usb_fifo		*txfifo;
104 	uint8_t			is_read;	/* location has read access */
105 	uint8_t			is_write;	/* location has write access */
106 	uint8_t			is_uref;	/* USB refcount decr. needed */
107 	uint8_t			is_usbfs;	/* USB-FS is active */
108 };
109 
110 struct usb_fs_privdata {
111 	int bus_index;
112 	int dev_index;
113 	int ep_addr;
114 	int mode;
115 	int fifo_index;
116 	struct cdev *cdev;
117 
118 	LIST_ENTRY(usb_fs_privdata) pd_next;
119 };
120 
121 /*
122  * Most of the fields in the "usb_fifo" structure are used by the
123  * generic USB access layer.
124  */
125 struct usb_fifo {
126 	struct usb_ifqueue free_q;
127 	struct usb_ifqueue used_q;
128 	struct selinfo selinfo;
129 	struct cv cv_io;
130 	struct cv cv_drain;
131 	struct usb_fifo_methods *methods;
132 	struct usb_symlink *symlink[2];/* our symlinks */
133 	struct proc *async_p;		/* process that wants SIGIO */
134 	struct usb_fs_endpoint *fs_ep_ptr;
135 	struct usb_device *udev;
136 	struct usb_xfer *xfer[2];
137 	struct usb_xfer **fs_xfer;
138 	struct mtx *priv_mtx;		/* client data */
139 	/* set if FIFO is opened by a FILE: */
140 	struct usb_cdev_privdata *curr_cpd;
141 	void   *priv_sc0;		/* client data */
142 	void   *priv_sc1;		/* client data */
143 	void   *queue_data;
144 	usb_timeout_t timeout;		/* timeout in milliseconds */
145 	usb_frlength_t bufsize;		/* BULK and INTERRUPT buffer size */
146 	usb_frcount_t nframes;		/* for isochronous mode */
147 	uint16_t dev_ep_index;		/* our device endpoint index */
148 	uint8_t	flag_sleeping;		/* set if FIFO is sleeping */
149 	uint8_t	flag_iscomplete;	/* set if a USB transfer is complete */
150 	uint8_t	flag_iserror;		/* set if FIFO error happened */
151 	uint8_t	flag_isselect;		/* set if FIFO is selected */
152 	uint8_t	flag_flushing;		/* set if FIFO is flushing data */
153 	uint8_t	flag_short;		/* set if short_ok or force_short
154 					 * transfer flags should be set */
155 	uint8_t	flag_stall;		/* set if clear stall should be run */
156 	uint8_t	iface_index;		/* set to the interface we belong to */
157 	uint8_t	fifo_index;		/* set to the FIFO index in "struct
158 					 * usb_device" */
159 	uint8_t	fs_ep_max;
160 	uint8_t	fifo_zlp;		/* zero length packet count */
161 	uint8_t	refcount;
162 #define	USB_FIFO_REF_MAX 0xFF
163 };
164 
165 struct usb_fifo_sc {
166 	struct usb_fifo *fp[2];
167 	struct cdev* dev;
168 };
169 
170 extern struct cdevsw usb2_devsw;
171 
172 int	usb2_fifo_wait(struct usb_fifo *fifo);
173 void	usb2_fifo_signal(struct usb_fifo *fifo);
174 int	usb2_fifo_alloc_buffer(struct usb_fifo *f, uint32_t bufsize,
175 	    uint16_t nbuf);
176 void	usb2_fifo_free_buffer(struct usb_fifo *f);
177 int	usb2_fifo_attach(struct usb_device *udev, void *priv_sc,
178 	    struct mtx *priv_mtx, struct usb_fifo_methods *pm,
179 	    struct usb_fifo_sc *f_sc, uint16_t unit, uint16_t subunit,
180 	    uint8_t iface_index, uid_t uid, gid_t gid, int mode);
181 void	usb2_fifo_detach(struct usb_fifo_sc *f_sc);
182 uint32_t usb2_fifo_put_bytes_max(struct usb_fifo *fifo);
183 void	usb2_fifo_put_data(struct usb_fifo *fifo, struct usb_page_cache *pc,
184 	    usb_frlength_t offset, usb_frlength_t len, uint8_t what);
185 void	usb2_fifo_put_data_linear(struct usb_fifo *fifo, void *ptr,
186 	    usb_size_t len, uint8_t what);
187 uint8_t	usb2_fifo_put_data_buffer(struct usb_fifo *f, void *ptr, usb_size_t len);
188 void	usb2_fifo_put_data_error(struct usb_fifo *fifo);
189 uint8_t	usb2_fifo_get_data(struct usb_fifo *fifo, struct usb_page_cache *pc,
190 	    usb_frlength_t offset, usb_frlength_t len, usb_frlength_t *actlen,
191 	    uint8_t what);
192 uint8_t	usb2_fifo_get_data_linear(struct usb_fifo *fifo, void *ptr,
193 	    usb_size_t len, usb_size_t *actlen, uint8_t what);
194 uint8_t	usb2_fifo_get_data_buffer(struct usb_fifo *f, void **pptr,
195 	    usb_size_t *plen);
196 void	usb2_fifo_get_data_error(struct usb_fifo *fifo);
197 uint8_t	usb2_fifo_opened(struct usb_fifo *fifo);
198 void	usb2_fifo_free(struct usb_fifo *f);
199 void	usb2_fifo_reset(struct usb_fifo *f);
200 void	usb2_fifo_wakeup(struct usb_fifo *f);
201 struct usb_symlink *usb2_alloc_symlink(const char *target);
202 void	usb2_free_symlink(struct usb_symlink *ps);
203 int	usb2_read_symlink(uint8_t *user_ptr, uint32_t startentry,
204 	    uint32_t user_len);
205 void	usb2_fifo_set_close_zlp(struct usb_fifo *, uint8_t);
206 
207 #endif					/* _USB2_DEV_H_ */
208