blob: 60139854e0b1b320d66f15b5b1f0d954e72e634d [file] [log] [blame]
Andrzej Pietrasiewicze72c39c2013-12-03 15:15:31 +01001/*
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>
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +010020#include <linux/list.h>
21#include <linux/mutex.h>
Robert Baldyga18d6b32f2014-12-18 09:55:10 +010022#include <linux/workqueue.h>
Andrzej Pietrasiewicze72c39c2013-12-03 15:15:31 +010023
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +010024#ifdef VERBOSE_DEBUG
25#ifndef pr_vdebug
26# define pr_vdebug pr_debug
27#endif /* pr_vdebug */
28# define ffs_dump_mem(prefix, ptr, len) \
29 print_hex_dump_bytes(pr_fmt(prefix ": "), DUMP_PREFIX_NONE, ptr, len)
30#else
31#ifndef pr_vdebug
32# define pr_vdebug(...) do { } while (0)
33#endif /* pr_vdebug */
34# define ffs_dump_mem(prefix, ptr, len) do { } while (0)
35#endif /* VERBOSE_DEBUG */
36
37#define ENTER() pr_vdebug("%s()\n", __func__)
38
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +010039struct f_fs_opts;
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +010040
Andrzej Pietrasiewicze72c39c2013-12-03 15:15:31 +010041struct ffs_dev {
42 const char *name;
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +010043 bool name_allocated;
Andrzej Pietrasiewicze72c39c2013-12-03 15:15:31 +010044 bool mounted;
45 bool desc_ready;
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +010046 bool single;
Andrzej Pietrasiewicze72c39c2013-12-03 15:15:31 +010047 struct ffs_data *ffs_data;
Andrzej Pietrasiewiczb6584992013-12-03 15:15:36 +010048 struct f_fs_opts *opts;
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +010049 struct list_head entry;
50
51 int (*ffs_ready_callback)(struct ffs_data *ffs);
52 void (*ffs_closed_callback)(struct ffs_data *ffs);
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +010053 void *(*ffs_acquire_dev_callback)(struct ffs_dev *dev);
54 void (*ffs_release_dev_callback)(struct ffs_dev *dev);
Andrzej Pietrasiewicze72c39c2013-12-03 15:15:31 +010055};
56
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +010057extern struct mutex ffs_lock;
58
59static inline void ffs_dev_lock(void)
60{
61 mutex_lock(&ffs_lock);
62}
63
64static inline void ffs_dev_unlock(void)
65{
66 mutex_unlock(&ffs_lock);
67}
68
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +010069int ffs_name_dev(struct ffs_dev *dev, const char *name);
70int ffs_single_dev(struct ffs_dev *dev);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +010071
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +010072struct ffs_epfile;
73struct ffs_function;
74
75enum ffs_state {
76 /*
77 * Waiting for descriptors and strings.
78 *
79 * In this state no open(2), read(2) or write(2) on epfiles
80 * may succeed (which should not be the problem as there
81 * should be no such files opened in the first place).
82 */
83 FFS_READ_DESCRIPTORS,
84 FFS_READ_STRINGS,
85
86 /*
87 * We've got descriptors and strings. We are or have called
88 * functionfs_ready_callback(). functionfs_bind() may have
89 * been called but we don't know.
90 *
91 * This is the only state in which operations on epfiles may
92 * succeed.
93 */
94 FFS_ACTIVE,
95
96 /*
Robert Baldyga18d6b32f2014-12-18 09:55:10 +010097 * Function is visible to host, but it's not functional. All
98 * setup requests are stalled and transfers on another endpoints
99 * are refused. All epfiles, except ep0, are deleted so there
100 * is no way to perform any operations on them.
101 *
102 * This state is set after closing all functionfs files, when
103 * mount parameter "no_disconnect=1" has been set. Function will
104 * remain in deactivated state until filesystem is umounted or
105 * ep0 is opened again. In the second case functionfs state will
106 * be reset, and it will be ready for descriptors and strings
107 * writing.
108 *
109 * This is useful only when functionfs is composed to gadget
110 * with another function which can perform some critical
111 * operations, and it's strongly desired to have this operations
112 * completed, even after functionfs files closure.
113 */
114 FFS_DEACTIVATED,
115
116 /*
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +0100117 * All endpoints have been closed. This state is also set if
118 * we encounter an unrecoverable error. The only
119 * unrecoverable error is situation when after reading strings
120 * from user space we fail to initialise epfiles or
121 * functionfs_ready_callback() returns with error (<0).
122 *
123 * In this state no open(2), read(2) or write(2) (both on ep0
124 * as well as epfile) may succeed (at this point epfiles are
125 * unlinked and all closed so this is not a problem; ep0 is
126 * also closed but ep0 file exists and so open(2) on ep0 must
127 * fail).
128 */
129 FFS_CLOSING
130};
131
132enum ffs_setup_state {
133 /* There is no setup request pending. */
134 FFS_NO_SETUP,
135 /*
136 * User has read events and there was a setup request event
137 * there. The next read/write on ep0 will handle the
138 * request.
139 */
140 FFS_SETUP_PENDING,
141 /*
142 * There was event pending but before user space handled it
143 * some other event was introduced which canceled existing
144 * setup. If this state is set read/write on ep0 return
145 * -EIDRM. This state is only set when adding event.
146 */
Michal Nazarewicze46318a2014-02-10 10:42:40 +0100147 FFS_SETUP_CANCELLED
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +0100148};
149
150struct ffs_data {
151 struct usb_gadget *gadget;
152
153 /*
154 * Protect access read/write operations, only one read/write
155 * at a time. As a consequence protects ep0req and company.
156 * While setup request is being processed (queued) this is
157 * held.
158 */
159 struct mutex mutex;
160
161 /*
162 * Protect access to endpoint related structures (basically
163 * usb_ep_queue(), usb_ep_dequeue(), etc. calls) except for
164 * endpoint zero.
165 */
166 spinlock_t eps_lock;
167
168 /*
169 * XXX REVISIT do we need our own request? Since we are not
170 * handling setup requests immediately user space may be so
171 * slow that another setup will be sent to the gadget but this
172 * time not to us but another function and then there could be
173 * a race. Is that the case? Or maybe we can use cdev->req
174 * after all, maybe we just need some spinlock for that?
175 */
176 struct usb_request *ep0req; /* P: mutex */
177 struct completion ep0req_completion; /* P: mutex */
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +0100178
179 /* reference counter */
180 atomic_t ref;
181 /* how many files are opened (EP0 and others) */
182 atomic_t opened;
183
184 /* EP0 state */
185 enum ffs_state state;
186
187 /*
188 * Possible transitions:
Michal Nazarewicze46318a2014-02-10 10:42:40 +0100189 * + FFS_NO_SETUP -> FFS_SETUP_PENDING -- P: ev.waitq.lock
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +0100190 * happens only in ep0 read which is P: mutex
Michal Nazarewicze46318a2014-02-10 10:42:40 +0100191 * + FFS_SETUP_PENDING -> FFS_NO_SETUP -- P: ev.waitq.lock
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +0100192 * happens only in ep0 i/o which is P: mutex
Michal Nazarewicze46318a2014-02-10 10:42:40 +0100193 * + FFS_SETUP_PENDING -> FFS_SETUP_CANCELLED -- P: ev.waitq.lock
194 * + FFS_SETUP_CANCELLED -> FFS_NO_SETUP -- cmpxchg
Michal Nazarewicza7ecf052014-02-10 10:42:41 +0100195 *
196 * This field should never be accessed directly and instead
197 * ffs_setup_state_clear_cancelled function should be used.
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +0100198 */
199 enum ffs_setup_state setup_state;
200
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +0100201 /* Events & such. */
202 struct {
203 u8 types[4];
204 unsigned short count;
205 /* XXX REVISIT need to update it in some places, or do we? */
206 unsigned short can_stall;
207 struct usb_ctrlrequest setup;
208
209 wait_queue_head_t waitq;
210 } ev; /* the whole structure, P: ev.waitq.lock */
211
212 /* Flags */
213 unsigned long flags;
214#define FFS_FL_CALL_CLOSED_CALLBACK 0
215#define FFS_FL_BOUND 1
216
217 /* Active function */
218 struct ffs_function *func;
219
220 /*
221 * Device name, write once when file system is mounted.
222 * Intended for user to read if she wants.
223 */
224 const char *dev_name;
225 /* Private data for our user (ie. gadget). Managed by user. */
226 void *private_data;
227
228 /* filled by __ffs_data_got_descs() */
229 /*
Michal Nazarewiczac8dde12014-02-28 16:50:23 +0530230 * raw_descs is what you kfree, real_descs points inside of raw_descs,
231 * where full speed, high speed and super speed descriptors start.
232 * real_descs_length is the length of all those descriptors.
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +0100233 */
Michal Nazarewiczac8dde12014-02-28 16:50:23 +0530234 const void *raw_descs_data;
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +0100235 const void *raw_descs;
Michal Nazarewiczac8dde12014-02-28 16:50:23 +0530236 unsigned raw_descs_length;
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +0100237 unsigned fs_descs_count;
238 unsigned hs_descs_count;
Manu Gautam8d4e8972014-02-28 16:50:22 +0530239 unsigned ss_descs_count;
Andrzej Pietrasiewiczf0175ab2014-07-09 12:20:08 +0200240 unsigned ms_os_descs_count;
241 unsigned ms_os_descs_ext_prop_count;
242 unsigned ms_os_descs_ext_prop_name_len;
243 unsigned ms_os_descs_ext_prop_data_len;
244 void *ms_os_descs_ext_prop_avail;
245 void *ms_os_descs_ext_prop_name_avail;
246 void *ms_os_descs_ext_prop_data_avail;
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +0100247
Robert Baldyga1b0bf882014-09-09 08:23:17 +0200248 unsigned user_flags;
249
Robert Baldyga6d5c1c72014-08-25 11:16:27 +0200250 u8 eps_addrmap[15];
251
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +0100252 unsigned short strings_count;
253 unsigned short interfaces_count;
254 unsigned short eps_count;
255 unsigned short _pad1;
256
257 /* filled by __ffs_data_got_strings() */
258 /* ids in stringtabs are set in functionfs_bind() */
259 const void *raw_strings;
260 struct usb_gadget_strings **stringtabs;
261
262 /*
263 * File system's super block, write once when file system is
264 * mounted.
265 */
266 struct super_block *sb;
267
268 /* File permissions, written once when fs is mounted */
269 struct ffs_file_perms {
270 umode_t mode;
271 kuid_t uid;
272 kgid_t gid;
273 } file_perms;
274
Robert Baldyga5e33f6f2015-01-23 13:41:01 +0100275 struct eventfd_ctx *ffs_eventfd;
Robert Baldyga18d6b32f2014-12-18 09:55:10 +0100276 bool no_disconnect;
277 struct work_struct reset_work;
278
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +0100279 /*
280 * The endpoint files, filled by ffs_epfiles_create(),
281 * destroyed by ffs_epfiles_destroy().
282 */
283 struct ffs_epfile *epfiles;
284};
285
286
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +0100287struct f_fs_opts {
288 struct usb_function_instance func_inst;
289 struct ffs_dev *dev;
290 unsigned refcnt;
291 bool no_configfs;
292};
293
294static inline struct f_fs_opts *to_f_fs_opts(struct usb_function_instance *fi)
295{
296 return container_of(fi, struct f_fs_opts, func_inst);
297}
Andrzej Pietrasiewicz5920cda2013-12-03 15:15:33 +0100298
Andrzej Pietrasiewicze72c39c2013-12-03 15:15:31 +0100299#endif /* U_FFS_H */