xref: /freebsd/sys/dev/usb/usb_dev.h (revision 39beb93c3f8bdbf72a61fda42300b5ebed7390c8)
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 	uint8_t			fifo_index;	/* FIFO index */
97 	uint8_t			is_read;	/* location has read access */
98 	uint8_t			is_write;	/* location has write access */
99 	uint8_t			is_uref;	/* USB refcount decr. needed */
100 	uint8_t			is_usbfs;	/* USB-FS is active */
101 	int			fflags;
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 	int    opened;			/* set if FIFO is opened by a FILE */
134 	void   *priv_sc0;		/* client data */
135 	void   *priv_sc1;		/* client data */
136 	void   *queue_data;
137 	uint32_t timeout;		/* timeout in milliseconds */
138 	uint32_t bufsize;		/* BULK and INTERRUPT buffer size */
139 	uint16_t nframes;		/* for isochronous mode */
140 	uint16_t dev_ep_index;		/* our device endpoint index */
141 	uint8_t	flag_sleeping;		/* set if FIFO is sleeping */
142 	uint8_t	flag_iscomplete;	/* set if a USB transfer is complete */
143 	uint8_t	flag_iserror;		/* set if FIFO error happened */
144 	uint8_t	flag_isselect;		/* set if FIFO is selected */
145 	uint8_t	flag_flushing;		/* set if FIFO is flushing data */
146 	uint8_t	flag_short;		/* set if short_ok or force_short
147 					 * transfer flags should be set */
148 	uint8_t	flag_stall;		/* set if clear stall should be run */
149 	uint8_t	iface_index;		/* set to the interface we belong to */
150 	uint8_t	fifo_index;		/* set to the FIFO index in "struct
151 					 * usb2_device" */
152 	uint8_t	fs_ep_max;
153 	uint8_t	fifo_zlp;		/* zero length packet count */
154 	uint8_t	refcount;
155 #define	USB_FIFO_REF_MAX 0xFF
156 };
157 
158 struct usb2_fifo_sc {
159 	struct usb2_fifo *fp[2];
160 	struct cdev* dev;
161 };
162 
163 extern struct cdevsw usb2_devsw;
164 
165 int	usb2_fifo_wait(struct usb2_fifo *fifo);
166 void	usb2_fifo_signal(struct usb2_fifo *fifo);
167 int	usb2_fifo_alloc_buffer(struct usb2_fifo *f, uint32_t bufsize,
168 	    uint16_t nbuf);
169 void	usb2_fifo_free_buffer(struct usb2_fifo *f);
170 int	usb2_fifo_attach(struct usb2_device *udev, void *priv_sc,
171 	    struct mtx *priv_mtx, struct usb2_fifo_methods *pm,
172 	    struct usb2_fifo_sc *f_sc, uint16_t unit, uint16_t subunit,
173 	    uint8_t iface_index, uid_t uid, gid_t gid, int mode);
174 void	usb2_fifo_detach(struct usb2_fifo_sc *f_sc);
175 uint32_t usb2_fifo_put_bytes_max(struct usb2_fifo *fifo);
176 void	usb2_fifo_put_data(struct usb2_fifo *fifo, struct usb2_page_cache *pc,
177 	    uint32_t offset, uint32_t len, uint8_t what);
178 void	usb2_fifo_put_data_linear(struct usb2_fifo *fifo, void *ptr,
179 	    uint32_t len, uint8_t what);
180 uint8_t	usb2_fifo_put_data_buffer(struct usb2_fifo *f, void *ptr, uint32_t len);
181 void	usb2_fifo_put_data_error(struct usb2_fifo *fifo);
182 uint8_t	usb2_fifo_get_data(struct usb2_fifo *fifo, struct usb2_page_cache *pc,
183 	    uint32_t offset, uint32_t len, uint32_t *actlen, uint8_t what);
184 uint8_t	usb2_fifo_get_data_linear(struct usb2_fifo *fifo, void *ptr,
185 	    uint32_t len, uint32_t *actlen, uint8_t what);
186 uint8_t	usb2_fifo_get_data_buffer(struct usb2_fifo *f, void **pptr,
187 	    uint32_t *plen);
188 void	usb2_fifo_get_data_error(struct usb2_fifo *fifo);
189 uint8_t	usb2_fifo_opened(struct usb2_fifo *fifo);
190 void	usb2_fifo_free(struct usb2_fifo *f);
191 void	usb2_fifo_reset(struct usb2_fifo *f);
192 void	usb2_fifo_wakeup(struct usb2_fifo *f);
193 struct usb2_symlink *usb2_alloc_symlink(const char *target);
194 void	usb2_free_symlink(struct usb2_symlink *ps);
195 int	usb2_read_symlink(uint8_t *user_ptr, uint32_t startentry,
196 	    uint32_t user_len);
197 
198 #endif					/* _USB2_DEV_H_ */
199