xref: /linux/drivers/usb/gadget/function/u_fs.h (revision 3932b9ca55b0be314a36d3e84faff3e823c081f5)
1 /*
2  * u_fs.h
3  *
4  * Utility definitions for the FunctionFS
5  *
6  * Copyright (c) 2013 Samsung Electronics Co., Ltd.
7  *		http://www.samsung.com
8  *
9  * Author: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License version 2 as
13  * published by the Free Software Foundation.
14  */
15 
16 #ifndef U_FFS_H
17 #define U_FFS_H
18 
19 #include <linux/usb/composite.h>
20 #include <linux/list.h>
21 #include <linux/mutex.h>
22 
23 #ifdef VERBOSE_DEBUG
24 #ifndef pr_vdebug
25 #  define pr_vdebug pr_debug
26 #endif /* pr_vdebug */
27 #  define ffs_dump_mem(prefix, ptr, len) \
28 	print_hex_dump_bytes(pr_fmt(prefix ": "), DUMP_PREFIX_NONE, ptr, len)
29 #else
30 #ifndef pr_vdebug
31 #  define pr_vdebug(...)                 do { } while (0)
32 #endif /* pr_vdebug */
33 #  define ffs_dump_mem(prefix, ptr, len) do { } while (0)
34 #endif /* VERBOSE_DEBUG */
35 
36 #define ENTER()    pr_vdebug("%s()\n", __func__)
37 
38 struct f_fs_opts;
39 
40 struct ffs_dev {
41 	const char *name;
42 	bool name_allocated;
43 	bool mounted;
44 	bool desc_ready;
45 	bool single;
46 	struct ffs_data *ffs_data;
47 	struct f_fs_opts *opts;
48 	struct list_head entry;
49 
50 	int (*ffs_ready_callback)(struct ffs_data *ffs);
51 	void (*ffs_closed_callback)(struct ffs_data *ffs);
52 	void *(*ffs_acquire_dev_callback)(struct ffs_dev *dev);
53 	void (*ffs_release_dev_callback)(struct ffs_dev *dev);
54 };
55 
56 extern struct mutex ffs_lock;
57 
58 static inline void ffs_dev_lock(void)
59 {
60 	mutex_lock(&ffs_lock);
61 }
62 
63 static inline void ffs_dev_unlock(void)
64 {
65 	mutex_unlock(&ffs_lock);
66 }
67 
68 int ffs_name_dev(struct ffs_dev *dev, const char *name);
69 int ffs_single_dev(struct ffs_dev *dev);
70 
71 struct ffs_epfile;
72 struct ffs_function;
73 
74 enum ffs_state {
75 	/*
76 	 * Waiting for descriptors and strings.
77 	 *
78 	 * In this state no open(2), read(2) or write(2) on epfiles
79 	 * may succeed (which should not be the problem as there
80 	 * should be no such files opened in the first place).
81 	 */
82 	FFS_READ_DESCRIPTORS,
83 	FFS_READ_STRINGS,
84 
85 	/*
86 	 * We've got descriptors and strings.  We are or have called
87 	 * functionfs_ready_callback().  functionfs_bind() may have
88 	 * been called but we don't know.
89 	 *
90 	 * This is the only state in which operations on epfiles may
91 	 * succeed.
92 	 */
93 	FFS_ACTIVE,
94 
95 	/*
96 	 * All endpoints have been closed.  This state is also set if
97 	 * we encounter an unrecoverable error.  The only
98 	 * unrecoverable error is situation when after reading strings
99 	 * from user space we fail to initialise epfiles or
100 	 * functionfs_ready_callback() returns with error (<0).
101 	 *
102 	 * In this state no open(2), read(2) or write(2) (both on ep0
103 	 * as well as epfile) may succeed (at this point epfiles are
104 	 * unlinked and all closed so this is not a problem; ep0 is
105 	 * also closed but ep0 file exists and so open(2) on ep0 must
106 	 * fail).
107 	 */
108 	FFS_CLOSING
109 };
110 
111 enum ffs_setup_state {
112 	/* There is no setup request pending. */
113 	FFS_NO_SETUP,
114 	/*
115 	 * User has read events and there was a setup request event
116 	 * there.  The next read/write on ep0 will handle the
117 	 * request.
118 	 */
119 	FFS_SETUP_PENDING,
120 	/*
121 	 * There was event pending but before user space handled it
122 	 * some other event was introduced which canceled existing
123 	 * setup.  If this state is set read/write on ep0 return
124 	 * -EIDRM.  This state is only set when adding event.
125 	 */
126 	FFS_SETUP_CANCELLED
127 };
128 
129 struct ffs_data {
130 	struct usb_gadget		*gadget;
131 
132 	/*
133 	 * Protect access read/write operations, only one read/write
134 	 * at a time.  As a consequence protects ep0req and company.
135 	 * While setup request is being processed (queued) this is
136 	 * held.
137 	 */
138 	struct mutex			mutex;
139 
140 	/*
141 	 * Protect access to endpoint related structures (basically
142 	 * usb_ep_queue(), usb_ep_dequeue(), etc. calls) except for
143 	 * endpoint zero.
144 	 */
145 	spinlock_t			eps_lock;
146 
147 	/*
148 	 * XXX REVISIT do we need our own request? Since we are not
149 	 * handling setup requests immediately user space may be so
150 	 * slow that another setup will be sent to the gadget but this
151 	 * time not to us but another function and then there could be
152 	 * a race.  Is that the case? Or maybe we can use cdev->req
153 	 * after all, maybe we just need some spinlock for that?
154 	 */
155 	struct usb_request		*ep0req;		/* P: mutex */
156 	struct completion		ep0req_completion;	/* P: mutex */
157 
158 	/* reference counter */
159 	atomic_t			ref;
160 	/* how many files are opened (EP0 and others) */
161 	atomic_t			opened;
162 
163 	/* EP0 state */
164 	enum ffs_state			state;
165 
166 	/*
167 	 * Possible transitions:
168 	 * + FFS_NO_SETUP        -> FFS_SETUP_PENDING  -- P: ev.waitq.lock
169 	 *               happens only in ep0 read which is P: mutex
170 	 * + FFS_SETUP_PENDING   -> FFS_NO_SETUP       -- P: ev.waitq.lock
171 	 *               happens only in ep0 i/o  which is P: mutex
172 	 * + FFS_SETUP_PENDING   -> FFS_SETUP_CANCELLED -- P: ev.waitq.lock
173 	 * + FFS_SETUP_CANCELLED -> FFS_NO_SETUP        -- cmpxchg
174 	 *
175 	 * This field should never be accessed directly and instead
176 	 * ffs_setup_state_clear_cancelled function should be used.
177 	 */
178 	enum ffs_setup_state		setup_state;
179 
180 	/* Events & such. */
181 	struct {
182 		u8				types[4];
183 		unsigned short			count;
184 		/* XXX REVISIT need to update it in some places, or do we? */
185 		unsigned short			can_stall;
186 		struct usb_ctrlrequest		setup;
187 
188 		wait_queue_head_t		waitq;
189 	} ev; /* the whole structure, P: ev.waitq.lock */
190 
191 	/* Flags */
192 	unsigned long			flags;
193 #define FFS_FL_CALL_CLOSED_CALLBACK 0
194 #define FFS_FL_BOUND                1
195 
196 	/* Active function */
197 	struct ffs_function		*func;
198 
199 	/*
200 	 * Device name, write once when file system is mounted.
201 	 * Intended for user to read if she wants.
202 	 */
203 	const char			*dev_name;
204 	/* Private data for our user (ie. gadget).  Managed by user. */
205 	void				*private_data;
206 
207 	/* filled by __ffs_data_got_descs() */
208 	/*
209 	 * raw_descs is what you kfree, real_descs points inside of raw_descs,
210 	 * where full speed, high speed and super speed descriptors start.
211 	 * real_descs_length is the length of all those descriptors.
212 	 */
213 	const void			*raw_descs_data;
214 	const void			*raw_descs;
215 	unsigned			raw_descs_length;
216 	unsigned			fs_descs_count;
217 	unsigned			hs_descs_count;
218 	unsigned			ss_descs_count;
219 	unsigned			ms_os_descs_count;
220 	unsigned			ms_os_descs_ext_prop_count;
221 	unsigned			ms_os_descs_ext_prop_name_len;
222 	unsigned			ms_os_descs_ext_prop_data_len;
223 	void				*ms_os_descs_ext_prop_avail;
224 	void				*ms_os_descs_ext_prop_name_avail;
225 	void				*ms_os_descs_ext_prop_data_avail;
226 
227 	u8				eps_addrmap[15];
228 
229 	unsigned short			strings_count;
230 	unsigned short			interfaces_count;
231 	unsigned short			eps_count;
232 	unsigned short			_pad1;
233 
234 	/* filled by __ffs_data_got_strings() */
235 	/* ids in stringtabs are set in functionfs_bind() */
236 	const void			*raw_strings;
237 	struct usb_gadget_strings	**stringtabs;
238 
239 	/*
240 	 * File system's super block, write once when file system is
241 	 * mounted.
242 	 */
243 	struct super_block		*sb;
244 
245 	/* File permissions, written once when fs is mounted */
246 	struct ffs_file_perms {
247 		umode_t				mode;
248 		kuid_t				uid;
249 		kgid_t				gid;
250 	}				file_perms;
251 
252 	/*
253 	 * The endpoint files, filled by ffs_epfiles_create(),
254 	 * destroyed by ffs_epfiles_destroy().
255 	 */
256 	struct ffs_epfile		*epfiles;
257 };
258 
259 
260 struct f_fs_opts {
261 	struct usb_function_instance	func_inst;
262 	struct ffs_dev			*dev;
263 	unsigned			refcnt;
264 	bool				no_configfs;
265 };
266 
267 static inline struct f_fs_opts *to_f_fs_opts(struct usb_function_instance *fi)
268 {
269 	return container_of(fi, struct f_fs_opts, func_inst);
270 }
271 
272 #endif /* U_FFS_H */
273