xref: /freebsd/sys/dev/usb/usb_dev.h (revision c0020399a650364d0134f79f3fa319f84064372d)
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 usb2_fifo;
42 struct usb2_mbuf;
43 
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);
49 
50 struct usb2_symlink {
51 	TAILQ_ENTRY(usb2_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  * "usb2_fifo_cmd_t" and "usb2_fifo_filter_t" functions are called
63  * locked. The others are called unlocked.
64  */
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;
69 	/*
70 	 * NOTE: The post-ioctl callback is called after the USB reference
71 	 * gets locked in the IOCTL handler:
72 	 */
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];
82 };
83 
84 /*
85  * Private per-device information.
86  */
87 struct usb2_cdev_privdata {
88 	struct usb2_bus		*bus;
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			ep_addr;	/* endpoint address */
96 	int			fflags;
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 */
102 };
103 
104 struct usb2_fs_privdata {
105 	int bus_index;
106 	int dev_index;
107 	int ep_addr;
108 	int mode;
109 	int fifo_index;
110 	struct cdev *cdev;
111 
112 	LIST_ENTRY(usb2_fs_privdata) pd_next;
113 };
114 
115 /*
116  * Most of the fields in the "usb2_fifo" structure are used by the
117  * generic USB access layer.
118  */
119 struct usb2_fifo {
120 	struct usb2_ifqueue free_q;
121 	struct usb2_ifqueue used_q;
122 	struct selinfo selinfo;
123 	struct cv cv_io;
124 	struct cv cv_drain;
125 	struct usb2_fifo_methods *methods;
126 	struct usb2_symlink *symlink[2];/* our symlinks */
127 	struct proc *async_p;		/* process that wants SIGIO */
128 	struct usb2_fs_endpoint *fs_ep_ptr;
129 	struct usb2_device *udev;
130 	struct usb2_xfer *xfer[2];
131 	struct usb2_xfer **fs_xfer;
132 	struct mtx *priv_mtx;		/* client data */
133 	/* set if FIFO is opened by a FILE: */
134 	struct usb2_cdev_privdata *curr_cpd;
135 	void   *priv_sc0;		/* client data */
136 	void   *priv_sc1;		/* client data */
137 	void   *queue_data;
138 	usb2_timeout_t timeout;		/* timeout in milliseconds */
139 	usb2_frlength_t bufsize;		/* BULK and INTERRUPT buffer size */
140 	usb2_frcount_t nframes;		/* for isochronous mode */
141 	uint16_t dev_ep_index;		/* our device endpoint index */
142 	uint8_t	flag_sleeping;		/* set if FIFO is sleeping */
143 	uint8_t	flag_iscomplete;	/* set if a USB transfer is complete */
144 	uint8_t	flag_iserror;		/* set if FIFO error happened */
145 	uint8_t	flag_isselect;		/* set if FIFO is selected */
146 	uint8_t	flag_flushing;		/* set if FIFO is flushing data */
147 	uint8_t	flag_short;		/* set if short_ok or force_short
148 					 * transfer flags should be set */
149 	uint8_t	flag_stall;		/* set if clear stall should be run */
150 	uint8_t	iface_index;		/* set to the interface we belong to */
151 	uint8_t	fifo_index;		/* set to the FIFO index in "struct
152 					 * usb2_device" */
153 	uint8_t	fs_ep_max;
154 	uint8_t	fifo_zlp;		/* zero length packet count */
155 	uint8_t	refcount;
156 #define	USB_FIFO_REF_MAX 0xFF
157 };
158 
159 struct usb2_fifo_sc {
160 	struct usb2_fifo *fp[2];
161 	struct cdev* dev;
162 };
163 
164 extern struct cdevsw usb2_devsw;
165 
166 int	usb2_fifo_wait(struct usb2_fifo *fifo);
167 void	usb2_fifo_signal(struct usb2_fifo *fifo);
168 int	usb2_fifo_alloc_buffer(struct usb2_fifo *f, uint32_t bufsize,
169 	    uint16_t nbuf);
170 void	usb2_fifo_free_buffer(struct usb2_fifo *f);
171 int	usb2_fifo_attach(struct usb2_device *udev, void *priv_sc,
172 	    struct mtx *priv_mtx, struct usb2_fifo_methods *pm,
173 	    struct usb2_fifo_sc *f_sc, uint16_t unit, uint16_t subunit,
174 	    uint8_t iface_index, uid_t uid, gid_t gid, int mode);
175 void	usb2_fifo_detach(struct usb2_fifo_sc *f_sc);
176 uint32_t usb2_fifo_put_bytes_max(struct usb2_fifo *fifo);
177 void	usb2_fifo_put_data(struct usb2_fifo *fifo, struct usb2_page_cache *pc,
178 	    usb2_frlength_t offset, usb2_frlength_t len, uint8_t what);
179 void	usb2_fifo_put_data_linear(struct usb2_fifo *fifo, void *ptr,
180 	    usb2_size_t len, uint8_t what);
181 uint8_t	usb2_fifo_put_data_buffer(struct usb2_fifo *f, void *ptr, usb2_size_t len);
182 void	usb2_fifo_put_data_error(struct usb2_fifo *fifo);
183 uint8_t	usb2_fifo_get_data(struct usb2_fifo *fifo, struct usb2_page_cache *pc,
184 	    usb2_frlength_t offset, usb2_frlength_t len, usb2_frlength_t *actlen,
185 	    uint8_t what);
186 uint8_t	usb2_fifo_get_data_linear(struct usb2_fifo *fifo, void *ptr,
187 	    usb2_size_t len, usb2_size_t *actlen, uint8_t what);
188 uint8_t	usb2_fifo_get_data_buffer(struct usb2_fifo *f, void **pptr,
189 	    usb2_size_t *plen);
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,
198 	    uint32_t user_len);
199 void	usb2_fifo_set_close_zlp(struct usb2_fifo *, uint8_t);
200 
201 #endif					/* _USB2_DEV_H_ */
202