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