1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * u_fs.h
4 *
5 * Utility definitions for the FunctionFS
6 *
7 * Copyright (c) 2013 Samsung Electronics Co., Ltd.
8 * http://www.samsung.com
9 *
10 * Author: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
11 */
12
13 #ifndef U_GENERIC_H
14 #define U_GENERIC_H
15
16 #include <linux/usb/composite.h>
17 #include <linux/list.h>
18 #include <linux/mutex.h>
19 #include <linux/workqueue.h>
20 #include <linux/refcount.h>
21 #include <linux/cdev.h>
22 #ifdef VERBOSE_DEBUG
23 #ifndef pr_vdebug
24 # define pr_vdebug pr_debug
25 #endif /* pr_vdebug */
26 # define ffs_dump_mem(prefix, ptr, len) \
27 print_hex_dump_bytes(pr_fmt(prefix ": "), DUMP_PREFIX_NONE, ptr, len)
28 #else
29 #ifndef pr_vdebug
30 # define pr_vdebug(...) do { } while (0)
31 #endif /* pr_vdebug */
32 # define ffs_dump_mem(prefix, ptr, len) do { } while (0)
33 #endif /* VERBOSE_DEBUG */
34
35 #define ENTER() pr_vdebug("%s()\n", __func__)
36
37 #define MAX_REQUEST 64
38 #define MAX_NAMELEN 64
39 #define FUNCTION_GENERIC "f_generic"
40
41 struct FuncNew {
42 uint32_t nameLen;
43 char name[MAX_NAMELEN];
44 };
45
46 struct IoData {
47 uint32_t aio;
48 uint32_t read;
49 uint32_t len;
50 uint32_t timeout;
51 uint64_t buf;
52 };
53
54 struct UsbFnReqEvent {
55 uint64_t buf;
56 uint32_t actual;
57 int status;
58 };
59
60 struct ffs_memory{
61 uint64_t mem;
62 uint64_t vm_start;
63 uint32_t size;
64 struct list_head memlist;
65 };
66
67 struct generic_memory{
68 uint32_t size;
69 uint64_t buf;
70 };
71
72
73 #define FUNCTIONFS_NEWFN _IOW('g', 60, struct FuncNew)
74 #define FUNCTIONFS_DELFN _IOW('g', 61, struct FuncNew)
75 #define FUNCTIONFS_ENDPOINT_GET_REQ_STATUS _IOW('g', 48, struct IoData)
76 #define FUNCTIONFS_ENDPOINT_WRITE _IOW('g', 49, struct IoData)
77 #define FUNCTIONFS_ENDPOINT_READ _IOW('g', 50, struct IoData)
78 #define FUNCTIONFS_ENDPOINT_RW_CANCEL _IOW('g', 51, struct IoData)
79 #define FUNCTIONFS_ENDPOINT_QUEUE_INIT _IO('g', 52)
80 #define FUNCTIONFS_ENDPOINT_QUEUE_DEL _IO('g', 53)
81 #define FUNCTIONFS_ENDPOINT_RELEASE_BUF _IOR('g', 54, struct generic_memory)
82 #define FUNCTIONFS_ENDPOINT_GET_EP0_EVENT _IOR('g', 56, struct UsbFnReqEvent)
83
84 struct f_fs_opts;
85
86 struct ffs_dev {
87 struct ffs_data *ffs_data;
88 struct f_fs_opts *opts;
89 struct list_head entry;
90
91 char name[MAX_NAMELEN];
92
93 bool mounted;
94 bool desc_ready;
95 bool single;
96
97 int (*ffs_ready_callback)(struct ffs_data *ffs);
98 void (*ffs_closed_callback)(struct ffs_data *ffs);
99 void *(*ffs_acquire_dev_callback)(struct ffs_dev *dev);
100 void (*ffs_release_dev_callback)(struct ffs_dev *dev);
101 };
102
103 extern struct mutex ffs_lock_adapter;
104
ffs_dev_lock(void)105 static inline void ffs_dev_lock(void)
106 {
107 mutex_lock(&ffs_lock_adapter);
108 }
109
ffs_dev_unlock(void)110 static inline void ffs_dev_unlock(void)
111 {
112 mutex_unlock(&ffs_lock_adapter);
113 }
114
115 int ffs_name_dev_adapter(struct ffs_dev *dev, const char *name);
116 int ffs_single_dev_adapter(struct ffs_dev *dev);
117
118 struct ffs_epfile;
119 struct ffs_function;
120
121 enum ffs_state {
122 /*
123 * Waiting for descriptors and strings.
124 *
125 * In this state no open(2), read(2) or write(2) on epfiles
126 * may succeed (which should not be the problem as there
127 * should be no such files opened in the first place).
128 */
129 FFS_READ_DESCRIPTORS,
130 FFS_READ_STRINGS,
131
132 /*
133 * We've got descriptors and strings. We are or have called
134 * functionfs_ready_callback(). functionfs_bind() may have
135 * been called but we don't know.
136 *
137 * This is the only state in which operations on epfiles may
138 * succeed.
139 */
140 FFS_ACTIVE,
141
142 /*
143 * Function is visible to host, but it's not functional. All
144 * setup requests are stalled and transfers on another endpoints
145 * are refused. All epfiles, except ep0, are deleted so there
146 * is no way to perform any operations on them.
147 *
148 * This state is set after closing all functionfs files, when
149 * mount parameter "no_disconnect=1" has been set. Function will
150 * remain in deactivated state until filesystem is umounted or
151 * ep0 is opened again. In the second case functionfs state will
152 * be reset, and it will be ready for descriptors and strings
153 * writing.
154 *
155 * This is useful only when functionfs is composed to gadget
156 * with another function which can perform some critical
157 * operations, and it's strongly desired to have this operations
158 * completed, even after functionfs files closure.
159 */
160 FFS_DEACTIVATED,
161
162 /*
163 * All endpoints have been closed. This state is also set if
164 * we encounter an unrecoverable error. The only
165 * unrecoverable error is situation when after reading strings
166 * from user space we fail to initialise epfiles or
167 * functionfs_ready_callback() returns with error (<0).
168 *
169 * In this state no open(2), read(2) or write(2) (both on ep0
170 * as well as epfile) may succeed (at this point epfiles are
171 * unlinked and all closed so this is not a problem; ep0 is
172 * also closed but ep0 file exists and so open(2) on ep0 must
173 * fail).
174 */
175 FFS_CLOSING
176 };
177
178 enum ffs_setup_state {
179 /* There is no setup request pending. */
180 FFS_NO_SETUP,
181 /*
182 * User has read events and there was a setup request event
183 * there. The next read/write on ep0 will handle the
184 * request.
185 */
186 FFS_SETUP_PENDING,
187 /*
188 * There was event pending but before user space handled it
189 * some other event was introduced which canceled existing
190 * setup. If this state is set read/write on ep0 return
191 * -EIDRM. This state is only set when adding event.
192 */
193 FFS_SETUP_CANCELLED
194 };
195
196 struct ffs_data {
197 struct usb_gadget *gadget;
198 struct list_head entry;
199 struct list_head memory_list;
200 /*
201 * Protect access read/write operations, only one read/write
202 * at a time. As a consequence protects ep0req and company.
203 * While setup request is being processed (queued) this is
204 * held.
205 */
206 struct mutex mutex;
207
208 /*
209 * Protect access to endpoint related structures (basically
210 * usb_ep_queue(), usb_ep_dequeue(), etc. calls) except for
211 * endpoint zero.
212 */
213 spinlock_t eps_lock;
214 spinlock_t mem_lock;
215
216 /*
217 * XXX REVISIT do we need our own request? Since we are not
218 * handling setup requests immediately user space may be so
219 * slow that another setup will be sent to the gadget but this
220 * time not to us but another function and then there could be
221 * a race. Is that the case? Or maybe we can use cdev->req
222 * after all, maybe we just need some spinlock for that?
223 */
224 struct usb_request *ep0req; /* P: mutex */
225 struct completion ep0req_completion; /* P: mutex */
226
227 /* reference counter */
228 refcount_t ref;
229 /* how many files are opened (EP0 and others) */
230 atomic_t opened;
231
232 /* EP0 state */
233 enum ffs_state state;
234
235 /*
236 * Possible transitions:
237 * + FFS_NO_SETUP -> FFS_SETUP_PENDING -- P: ev.waitq.lock
238 * happens only in ep0 read which is P: mutex
239 * + FFS_SETUP_PENDING -> FFS_NO_SETUP -- P: ev.waitq.lock
240 * happens only in ep0 i/o which is P: mutex
241 * + FFS_SETUP_PENDING -> FFS_SETUP_CANCELLED -- P: ev.waitq.lock
242 * + FFS_SETUP_CANCELLED -> FFS_NO_SETUP -- cmpxchg
243 *
244 * This field should never be accessed directly and instead
245 * ffs_setup_state_clear_cancelled function should be used.
246 */
247 enum ffs_setup_state setup_state;
248
249 /* Events & such. */
250 struct {
251 u8 types[4];
252 unsigned short count;
253 /* XXX REVISIT need to update it in some places, or do we? */
254 unsigned short can_stall;
255 struct usb_ctrlrequest setup;
256
257 wait_queue_head_t waitq;
258 } ev; /* the whole structure, P: ev.waitq.lock */
259
260 /* Flags */
261 unsigned long flags;
262 #define FFS_FL_CALL_CLOSED_CALLBACK 0
263 #define FFS_FL_BOUND 1
264
265 /* For waking up blocked threads when function is enabled. */
266 wait_queue_head_t wait;
267
268 /* Active function */
269 struct ffs_function *func;
270
271
272 char dev_name[MAX_NAMELEN];
273 struct cdev cdev;
274 dev_t devno;
275 struct device *fn_device;
276
277 struct kfifo reqEventFifo;
278 wait_queue_head_t wait_que;
279 /* Private data for our user (ie. gadget). Managed by user. */
280 void *private_data;
281 /* filled by __ffs_data_got_descs() */
282 /*
283 * raw_descs is what you kfree, real_descs points inside of raw_descs,
284 * where full speed, high speed and super speed descriptors start.
285 * real_descs_length is the length of all those descriptors.
286 */
287 const void *raw_descs_data;
288 const void *raw_descs;
289 unsigned raw_descs_length;
290 unsigned fs_descs_count;
291 unsigned hs_descs_count;
292 unsigned ss_descs_count;
293 unsigned ms_os_descs_count;
294 unsigned ms_os_descs_ext_prop_count;
295 unsigned ms_os_descs_ext_prop_name_len;
296 unsigned ms_os_descs_ext_prop_data_len;
297 void *ms_os_descs_ext_prop_avail;
298 void *ms_os_descs_ext_prop_name_avail;
299 void *ms_os_descs_ext_prop_data_avail;
300
301 unsigned user_flags;
302
303 #define FFS_MAX_EPS_COUNT 31
304 u8 eps_addrmap[FFS_MAX_EPS_COUNT];
305
306 unsigned short strings_count;
307 unsigned short interfaces_count;
308 unsigned short eps_count;
309 unsigned short _pad1;
310
311 /* filled by __ffs_data_got_strings() */
312 /* ids in stringtabs are set in functionfs_bind() */
313 const void *raw_strings;
314 struct usb_gadget_strings **stringtabs;
315
316 /*
317 * File system's super block, write once when file system is
318 * mounted.
319 */
320 struct super_block *sb;
321
322 /* File permissions, written once when fs is mounted */
323 struct ffs_file_perms {
324 umode_t mode;
325 kuid_t uid;
326 kgid_t gid;
327 } file_perms;
328
329 struct eventfd_ctx *ffs_eventfd;
330 struct workqueue_struct *io_completion_wq;
331 bool no_disconnect;
332 struct work_struct reset_work;
333
334 /*
335 * The endpoint files, filled by ffs_epfiles_create(),
336 * destroyed by ffs_epfiles_destroy().
337 */
338 struct ffs_epfile *epfiles;
339 struct ffs_ep *eps;
340 enum usb_device_speed speed;
341 };
342
343
344 struct f_fs_opts {
345 struct usb_function_instance func_inst;
346 struct ffs_dev *dev;
347 unsigned refcnt;
348 bool no_configfs;
349 };
350
to_f_fs_opts(struct usb_function_instance * fi)351 static inline struct f_fs_opts *to_f_fs_opts(struct usb_function_instance *fi)
352 {
353 return container_of(fi, struct f_fs_opts, func_inst);
354 }
355
356 #endif /* U_GENERIC_H */