aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/gadget/f_fs.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/usb/gadget/f_fs.c')
-rw-r--r--drivers/usb/gadget/f_fs.c422
1 files changed, 213 insertions, 209 deletions
diff --git a/drivers/usb/gadget/f_fs.c b/drivers/usb/gadget/f_fs.c
index d17930d9a843..8d318eaaaf20 100644
--- a/drivers/usb/gadget/f_fs.c
+++ b/drivers/usb/gadget/f_fs.c
@@ -22,6 +22,7 @@
22#include <linux/pagemap.h> 22#include <linux/pagemap.h>
23#include <linux/export.h> 23#include <linux/export.h>
24#include <linux/hid.h> 24#include <linux/hid.h>
25#include <linux/module.h>
25#include <asm/unaligned.h> 26#include <asm/unaligned.h>
26 27
27#include <linux/usb/composite.h> 28#include <linux/usb/composite.h>
@@ -57,210 +58,6 @@
57#define vla_ptr(ptr, groupname, name) \ 58#define vla_ptr(ptr, groupname, name) \
58 ((void *) ((char *)ptr + groupname##_##name##__offset)) 59 ((void *) ((char *)ptr + groupname##_##name##__offset))
59 60
60/* Debugging ****************************************************************/
61
62#ifdef VERBOSE_DEBUG
63#ifndef pr_vdebug
64# define pr_vdebug pr_debug
65#endif /* pr_vdebug */
66# define ffs_dump_mem(prefix, ptr, len) \
67 print_hex_dump_bytes(pr_fmt(prefix ": "), DUMP_PREFIX_NONE, ptr, len)
68#else
69#ifndef pr_vdebug
70# define pr_vdebug(...) do { } while (0)
71#endif /* pr_vdebug */
72# define ffs_dump_mem(prefix, ptr, len) do { } while (0)
73#endif /* VERBOSE_DEBUG */
74
75#define ENTER() pr_vdebug("%s()\n", __func__)
76
77
78/* The data structure and setup file ****************************************/
79
80enum ffs_state {
81 /*
82 * Waiting for descriptors and strings.
83 *
84 * In this state no open(2), read(2) or write(2) on epfiles
85 * may succeed (which should not be the problem as there
86 * should be no such files opened in the first place).
87 */
88 FFS_READ_DESCRIPTORS,
89 FFS_READ_STRINGS,
90
91 /*
92 * We've got descriptors and strings. We are or have called
93 * ffs_ready(). functionfs_bind() may have
94 * been called but we don't know.
95 *
96 * This is the only state in which operations on epfiles may
97 * succeed.
98 */
99 FFS_ACTIVE,
100
101 /*
102 * All endpoints have been closed. This state is also set if
103 * we encounter an unrecoverable error. The only
104 * unrecoverable error is situation when after reading strings
105 * from user space we fail to initialise epfiles or
106 * ffs_ready() returns with error (<0).
107 *
108 * In this state no open(2), read(2) or write(2) (both on ep0
109 * as well as epfile) may succeed (at this point epfiles are
110 * unlinked and all closed so this is not a problem; ep0 is
111 * also closed but ep0 file exists and so open(2) on ep0 must
112 * fail).
113 */
114 FFS_CLOSING
115};
116
117
118enum ffs_setup_state {
119 /* There is no setup request pending. */
120 FFS_NO_SETUP,
121 /*
122 * User has read events and there was a setup request event
123 * there. The next read/write on ep0 will handle the
124 * request.
125 */
126 FFS_SETUP_PENDING,
127 /*
128 * There was event pending but before user space handled it
129 * some other event was introduced which canceled existing
130 * setup. If this state is set read/write on ep0 return
131 * -EIDRM. This state is only set when adding event.
132 */
133 FFS_SETUP_CANCELED
134};
135
136
137
138struct ffs_epfile;
139struct ffs_function;
140
141struct ffs_data {
142 struct usb_gadget *gadget;
143
144 /*
145 * Protect access read/write operations, only one read/write
146 * at a time. As a consequence protects ep0req and company.
147 * While setup request is being processed (queued) this is
148 * held.
149 */
150 struct mutex mutex;
151
152 /*
153 * Protect access to endpoint related structures (basically
154 * usb_ep_queue(), usb_ep_dequeue(), etc. calls) except for
155 * endpoint zero.
156 */
157 spinlock_t eps_lock;
158
159 /*
160 * XXX REVISIT do we need our own request? Since we are not
161 * handling setup requests immediately user space may be so
162 * slow that another setup will be sent to the gadget but this
163 * time not to us but another function and then there could be
164 * a race. Is that the case? Or maybe we can use cdev->req
165 * after all, maybe we just need some spinlock for that?
166 */
167 struct usb_request *ep0req; /* P: mutex */
168 struct completion ep0req_completion; /* P: mutex */
169 int ep0req_status; /* P: mutex */
170
171 /* reference counter */
172 atomic_t ref;
173 /* how many files are opened (EP0 and others) */
174 atomic_t opened;
175
176 /* EP0 state */
177 enum ffs_state state;
178
179 /*
180 * Possible transitions:
181 * + FFS_NO_SETUP -> FFS_SETUP_PENDING -- P: ev.waitq.lock
182 * happens only in ep0 read which is P: mutex
183 * + FFS_SETUP_PENDING -> FFS_NO_SETUP -- P: ev.waitq.lock
184 * happens only in ep0 i/o which is P: mutex
185 * + FFS_SETUP_PENDING -> FFS_SETUP_CANCELED -- P: ev.waitq.lock
186 * + FFS_SETUP_CANCELED -> FFS_NO_SETUP -- cmpxchg
187 */
188 enum ffs_setup_state setup_state;
189
190#define FFS_SETUP_STATE(ffs) \
191 ((enum ffs_setup_state)cmpxchg(&(ffs)->setup_state, \
192 FFS_SETUP_CANCELED, FFS_NO_SETUP))
193
194 /* Events & such. */
195 struct {
196 u8 types[4];
197 unsigned short count;
198 /* XXX REVISIT need to update it in some places, or do we? */
199 unsigned short can_stall;
200 struct usb_ctrlrequest setup;
201
202 wait_queue_head_t waitq;
203 } ev; /* the whole structure, P: ev.waitq.lock */
204
205 /* Flags */
206 unsigned long flags;
207#define FFS_FL_CALL_CLOSED_CALLBACK 0
208#define FFS_FL_BOUND 1
209
210 /* Active function */
211 struct ffs_function *func;
212
213 /*
214 * Device name, write once when file system is mounted.
215 * Intended for user to read if she wants.
216 */
217 const char *dev_name;
218 /* Private data for our user (ie. gadget). Managed by user. */
219 void *private_data;
220
221 /* filled by __ffs_data_got_descs() */
222 /*
223 * Real descriptors are 16 bytes after raw_descs (so you need
224 * to skip 16 bytes (ie. ffs->raw_descs + 16) to get to the
225 * first full speed descriptor). raw_descs_length and
226 * raw_fs_descs_length do not have those 16 bytes added.
227 */
228 const void *raw_descs;
229 unsigned raw_descs_length;
230 unsigned raw_fs_descs_length;
231 unsigned fs_descs_count;
232 unsigned hs_descs_count;
233
234 unsigned short strings_count;
235 unsigned short interfaces_count;
236 unsigned short eps_count;
237 unsigned short _pad1;
238
239 /* filled by __ffs_data_got_strings() */
240 /* ids in stringtabs are set in functionfs_bind() */
241 const void *raw_strings;
242 struct usb_gadget_strings **stringtabs;
243
244 /*
245 * File system's super block, write once when file system is
246 * mounted.
247 */
248 struct super_block *sb;
249
250 /* File permissions, written once when fs is mounted */
251 struct ffs_file_perms {
252 umode_t mode;
253 kuid_t uid;
254 kgid_t gid;
255 } file_perms;
256
257 /*
258 * The endpoint files, filled by ffs_epfiles_create(),
259 * destroyed by ffs_epfiles_destroy().
260 */
261 struct ffs_epfile *epfiles;
262};
263
264/* Reference counter handling */ 61/* Reference counter handling */
265static void ffs_data_get(struct ffs_data *ffs); 62static void ffs_data_get(struct ffs_data *ffs);
266static void ffs_data_put(struct ffs_data *ffs); 63static void ffs_data_put(struct ffs_data *ffs);
@@ -300,15 +97,19 @@ static struct ffs_function *ffs_func_from_usb(struct usb_function *f)
300 return container_of(f, struct ffs_function, function); 97 return container_of(f, struct ffs_function, function);
301} 98}
302 99
100#ifdef USB_FFS_INCLUDED
303static void ffs_func_free(struct ffs_function *func); 101static void ffs_func_free(struct ffs_function *func);
102#endif
304 103
305static void ffs_func_eps_disable(struct ffs_function *func); 104static void ffs_func_eps_disable(struct ffs_function *func);
306static int __must_check ffs_func_eps_enable(struct ffs_function *func); 105static int __must_check ffs_func_eps_enable(struct ffs_function *func);
307 106
308static int ffs_func_bind(struct usb_configuration *, 107static int ffs_func_bind(struct usb_configuration *,
309 struct usb_function *); 108 struct usb_function *);
310static void ffs_func_unbind(struct usb_configuration *, 109#ifdef USB_FFS_INCLUDED
110static void old_ffs_func_unbind(struct usb_configuration *,
311 struct usb_function *); 111 struct usb_function *);
112#endif
312static int ffs_func_set_alt(struct usb_function *, unsigned, unsigned); 113static int ffs_func_set_alt(struct usb_function *, unsigned, unsigned);
313static void ffs_func_disable(struct usb_function *); 114static void ffs_func_disable(struct usb_function *);
314static int ffs_func_setup(struct usb_function *, 115static int ffs_func_setup(struct usb_function *,
@@ -364,6 +165,9 @@ ffs_sb_create_file(struct super_block *sb, const char *name, void *data,
364/* Devices management *******************************************************/ 165/* Devices management *******************************************************/
365 166
366DEFINE_MUTEX(ffs_lock); 167DEFINE_MUTEX(ffs_lock);
168#ifndef USB_FFS_INCLUDED
169EXPORT_SYMBOL(ffs_lock);
170#endif
367 171
368static struct ffs_dev *ffs_find_dev(const char *name); 172static struct ffs_dev *ffs_find_dev(const char *name);
369static void *ffs_acquire_dev(const char *dev_name); 173static void *ffs_acquire_dev(const char *dev_name);
@@ -1499,6 +1303,8 @@ static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count)
1499 kfree(epfiles); 1303 kfree(epfiles);
1500} 1304}
1501 1305
1306#ifdef USB_FFS_INCLUDED
1307
1502static int functionfs_bind_config(struct usb_composite_dev *cdev, 1308static int functionfs_bind_config(struct usb_composite_dev *cdev,
1503 struct usb_configuration *c, 1309 struct usb_configuration *c,
1504 struct ffs_data *ffs) 1310 struct ffs_data *ffs)
@@ -1516,7 +1322,7 @@ static int functionfs_bind_config(struct usb_composite_dev *cdev,
1516 func->function.strings = ffs->stringtabs; 1322 func->function.strings = ffs->stringtabs;
1517 1323
1518 func->function.bind = ffs_func_bind; 1324 func->function.bind = ffs_func_bind;
1519 func->function.unbind = ffs_func_unbind; 1325 func->function.unbind = old_ffs_func_unbind;
1520 func->function.set_alt = ffs_func_set_alt; 1326 func->function.set_alt = ffs_func_set_alt;
1521 func->function.disable = ffs_func_disable; 1327 func->function.disable = ffs_func_disable;
1522 func->function.setup = ffs_func_setup; 1328 func->function.setup = ffs_func_setup;
@@ -1565,6 +1371,8 @@ static void ffs_func_free(struct ffs_function *func)
1565 kfree(func); 1371 kfree(func);
1566} 1372}
1567 1373
1374#endif
1375
1568static void ffs_func_eps_disable(struct ffs_function *func) 1376static void ffs_func_eps_disable(struct ffs_function *func)
1569{ 1377{
1570 struct ffs_ep *ep = func->eps; 1378 struct ffs_ep *ep = func->eps;
@@ -2227,8 +2035,59 @@ static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep,
2227 return 0; 2035 return 0;
2228} 2036}
2229 2037
2230static int ffs_func_bind(struct usb_configuration *c, 2038#ifndef USB_FFS_INCLUDED
2231 struct usb_function *f) 2039static inline struct f_fs_opts *ffs_do_functionfs_bind(struct usb_function *f,
2040 struct usb_configuration *c)
2041{
2042 struct ffs_function *func = ffs_func_from_usb(f);
2043 struct f_fs_opts *ffs_opts =
2044 container_of(f->fi, struct f_fs_opts, func_inst);
2045 int ret;
2046
2047 ENTER();
2048
2049 /*
2050 * Legacy gadget triggers binding in functionfs_ready_callback,
2051 * which already uses locking; taking the same lock here would
2052 * cause a deadlock.
2053 *
2054 * Configfs-enabled gadgets however do need ffs_dev_lock.
2055 */
2056 if (!ffs_opts->no_configfs)
2057 ffs_dev_lock();
2058 ret = ffs_opts->dev->desc_ready ? 0 : -ENODEV;
2059 func->ffs = ffs_opts->dev->ffs_data;
2060 if (!ffs_opts->no_configfs)
2061 ffs_dev_unlock();
2062 if (ret)
2063 return ERR_PTR(ret);
2064
2065 func->conf = c;
2066 func->gadget = c->cdev->gadget;
2067
2068 ffs_data_get(func->ffs);
2069
2070 /*
2071 * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
2072 * configurations are bound in sequence with list_for_each_entry,
2073 * in each configuration its functions are bound in sequence
2074 * with list_for_each_entry, so we assume no race condition
2075 * with regard to ffs_opts->bound access
2076 */
2077 if (!ffs_opts->refcnt) {
2078 ret = functionfs_bind(func->ffs, c->cdev);
2079 if (ret)
2080 return ERR_PTR(ret);
2081 }
2082 ffs_opts->refcnt++;
2083 func->function.strings = func->ffs->stringtabs;
2084
2085 return ffs_opts;
2086}
2087#endif
2088
2089static int _ffs_func_bind(struct usb_configuration *c,
2090 struct usb_function *f)
2232{ 2091{
2233 struct ffs_function *func = ffs_func_from_usb(f); 2092 struct ffs_function *func = ffs_func_from_usb(f);
2234 struct ffs_data *ffs = func->ffs; 2093 struct ffs_data *ffs = func->ffs;
@@ -2328,10 +2187,25 @@ error:
2328 return ret; 2187 return ret;
2329} 2188}
2330 2189
2190static int ffs_func_bind(struct usb_configuration *c,
2191 struct usb_function *f)
2192{
2193#ifndef USB_FFS_INCLUDED
2194 struct f_fs_opts *ffs_opts = ffs_do_functionfs_bind(f, c);
2195
2196 if (IS_ERR(ffs_opts))
2197 return PTR_ERR(ffs_opts);
2198#endif
2199
2200 return _ffs_func_bind(c, f);
2201}
2202
2331 2203
2332/* Other USB function hooks *************************************************/ 2204/* Other USB function hooks *************************************************/
2333 2205
2334static void ffs_func_unbind(struct usb_configuration *c, 2206#ifdef USB_FFS_INCLUDED
2207
2208static void old_ffs_func_unbind(struct usb_configuration *c,
2335 struct usb_function *f) 2209 struct usb_function *f)
2336{ 2210{
2337 struct ffs_function *func = ffs_func_from_usb(f); 2211 struct ffs_function *func = ffs_func_from_usb(f);
@@ -2349,6 +2223,8 @@ static void ffs_func_unbind(struct usb_configuration *c,
2349 ffs_func_free(func); 2223 ffs_func_free(func);
2350} 2224}
2351 2225
2226#endif
2227
2352static int ffs_func_set_alt(struct usb_function *f, 2228static int ffs_func_set_alt(struct usb_function *f,
2353 unsigned interface, unsigned alt) 2229 unsigned interface, unsigned alt)
2354{ 2230{
@@ -2523,6 +2399,116 @@ static struct ffs_dev *ffs_find_dev(const char *name)
2523 return _ffs_find_dev(name); 2399 return _ffs_find_dev(name);
2524} 2400}
2525 2401
2402/* Function registration interface ******************************************/
2403
2404#ifndef USB_FFS_INCLUDED
2405
2406static void ffs_free_inst(struct usb_function_instance *f)
2407{
2408 struct f_fs_opts *opts;
2409
2410 opts = to_f_fs_opts(f);
2411 ffs_dev_lock();
2412 ffs_free_dev(opts->dev);
2413 ffs_dev_unlock();
2414 kfree(opts);
2415}
2416
2417static struct usb_function_instance *ffs_alloc_inst(void)
2418{
2419 struct f_fs_opts *opts;
2420 struct ffs_dev *dev;
2421
2422 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
2423 if (!opts)
2424 return ERR_PTR(-ENOMEM);
2425
2426 opts->func_inst.free_func_inst = ffs_free_inst;
2427 ffs_dev_lock();
2428 dev = ffs_alloc_dev();
2429 ffs_dev_unlock();
2430 if (IS_ERR(dev)) {
2431 kfree(opts);
2432 return ERR_CAST(dev);
2433 }
2434 opts->dev = dev;
2435
2436 return &opts->func_inst;
2437}
2438
2439static void ffs_free(struct usb_function *f)
2440{
2441 kfree(ffs_func_from_usb(f));
2442}
2443
2444static void ffs_func_unbind(struct usb_configuration *c,
2445 struct usb_function *f)
2446{
2447 struct ffs_function *func = ffs_func_from_usb(f);
2448 struct ffs_data *ffs = func->ffs;
2449 struct f_fs_opts *opts =
2450 container_of(f->fi, struct f_fs_opts, func_inst);
2451 struct ffs_ep *ep = func->eps;
2452 unsigned count = ffs->eps_count;
2453 unsigned long flags;
2454
2455 ENTER();
2456 if (ffs->func == func) {
2457 ffs_func_eps_disable(func);
2458 ffs->func = NULL;
2459 }
2460
2461 if (!--opts->refcnt)
2462 functionfs_unbind(ffs);
2463
2464 /* cleanup after autoconfig */
2465 spin_lock_irqsave(&func->ffs->eps_lock, flags);
2466 do {
2467 if (ep->ep && ep->req)
2468 usb_ep_free_request(ep->ep, ep->req);
2469 ep->req = NULL;
2470 ++ep;
2471 } while (--count);
2472 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
2473 kfree(func->eps);
2474 func->eps = NULL;
2475 /*
2476 * eps, descriptors and interfaces_nums are allocated in the
2477 * same chunk so only one free is required.
2478 */
2479 func->function.fs_descriptors = NULL;
2480 func->function.hs_descriptors = NULL;
2481 func->interfaces_nums = NULL;
2482
2483 ffs_event_add(ffs, FUNCTIONFS_UNBIND);
2484}
2485
2486static struct usb_function *ffs_alloc(struct usb_function_instance *fi)
2487{
2488 struct ffs_function *func;
2489
2490 ENTER();
2491
2492 func = kzalloc(sizeof(*func), GFP_KERNEL);
2493 if (unlikely(!func))
2494 return ERR_PTR(-ENOMEM);
2495
2496 func->function.name = "Function FS Gadget";
2497
2498 func->function.bind = ffs_func_bind;
2499 func->function.unbind = ffs_func_unbind;
2500 func->function.set_alt = ffs_func_set_alt;
2501 func->function.disable = ffs_func_disable;
2502 func->function.setup = ffs_func_setup;
2503 func->function.suspend = ffs_func_suspend;
2504 func->function.resume = ffs_func_resume;
2505 func->function.free_func = ffs_free;
2506
2507 return &func->function;
2508}
2509
2510#endif
2511
2526/* 2512/*
2527 * ffs_lock must be taken by the caller of this function 2513 * ffs_lock must be taken by the caller of this function
2528 */ 2514 */
@@ -2581,6 +2567,9 @@ int ffs_name_dev(struct ffs_dev *dev, const char *name)
2581 2567
2582 return ret; 2568 return ret;
2583} 2569}
2570#ifndef USB_FFS_INCLUDED
2571EXPORT_SYMBOL(ffs_name_dev);
2572#endif
2584 2573
2585int ffs_single_dev(struct ffs_dev *dev) 2574int ffs_single_dev(struct ffs_dev *dev)
2586{ 2575{
@@ -2597,6 +2586,9 @@ int ffs_single_dev(struct ffs_dev *dev)
2597 ffs_dev_unlock(); 2586 ffs_dev_unlock();
2598 return ret; 2587 return ret;
2599} 2588}
2589#ifndef USB_FFS_INCLUDED
2590EXPORT_SYMBOL(ffs_single_dev);
2591#endif
2600 2592
2601/* 2593/*
2602 * ffs_lock must be taken by the caller of this function 2594 * ffs_lock must be taken by the caller of this function
@@ -2621,6 +2613,9 @@ static void *ffs_acquire_dev(const char *dev_name)
2621 ffs_dev = ERR_PTR(-ENODEV); 2613 ffs_dev = ERR_PTR(-ENODEV);
2622 else if (ffs_dev->mounted) 2614 else if (ffs_dev->mounted)
2623 ffs_dev = ERR_PTR(-EBUSY); 2615 ffs_dev = ERR_PTR(-EBUSY);
2616 else if (ffs_dev->ffs_acquire_dev_callback &&
2617 ffs_dev->ffs_acquire_dev_callback(ffs_dev))
2618 ffs_dev = ERR_PTR(-ENODEV);
2624 else 2619 else
2625 ffs_dev->mounted = true; 2620 ffs_dev->mounted = true;
2626 2621
@@ -2638,6 +2633,9 @@ static void ffs_release_dev(struct ffs_data *ffs_data)
2638 ffs_dev = ffs_data->private_data; 2633 ffs_dev = ffs_data->private_data;
2639 if (ffs_dev) 2634 if (ffs_dev)
2640 ffs_dev->mounted = false; 2635 ffs_dev->mounted = false;
2636
2637 if (ffs_dev->ffs_release_dev_callback)
2638 ffs_dev->ffs_release_dev_callback(ffs_dev);
2641 2639
2642 ffs_dev_unlock(); 2640 ffs_dev_unlock();
2643} 2641}
@@ -2720,3 +2718,9 @@ static char *ffs_prepare_buffer(const char __user *buf, size_t len)
2720 2718
2721 return data; 2719 return data;
2722} 2720}
2721
2722#ifndef USB_FFS_INCLUDED
2723DECLARE_USB_FUNCTION_INIT(ffs, ffs_alloc_inst, ffs_alloc);
2724MODULE_LICENSE("GPL");
2725MODULE_AUTHOR("Michal Nazarewicz");
2726#endif