blob: 474f2d9f3a0f10a271b3d47825a9fd749ab1c5f4 [file] [log] [blame]
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001/*
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002 * f_fs.c -- user mode file system API for USB composite function controllers
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02003 *
4 * Copyright (C) 2010 Samsung Electronics
5 * Author: Michal Nazarewicz <m.nazarewicz@samsung.com>
6 *
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01007 * Based on inode.c (GadgetFS) which was:
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02008 * Copyright (C) 2003-2004 David Brownell
9 * Copyright (C) 2003 Agilent Technologies
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 as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25
26
27/* #define DEBUG */
28/* #define VERBOSE_DEBUG */
29
30#include <linux/blkdev.h>
Randy Dunlapb0608692010-05-10 10:51:36 -070031#include <linux/pagemap.h>
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020032#include <asm/unaligned.h>
33#include <linux/smp_lock.h>
34
35#include <linux/usb/composite.h>
36#include <linux/usb/functionfs.h>
37
38
39#define FUNCTIONFS_MAGIC 0xa647361 /* Chosen by a honest dice roll ;) */
40
41
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010042/* Debugging ****************************************************************/
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020043
44#define ffs_printk(level, fmt, args...) printk(level "f_fs: " fmt "\n", ## args)
45
46#define FERR(...) ffs_printk(KERN_ERR, __VA_ARGS__)
47#define FINFO(...) ffs_printk(KERN_INFO, __VA_ARGS__)
48
49#ifdef DEBUG
50# define FDBG(...) ffs_printk(KERN_DEBUG, __VA_ARGS__)
51#else
52# define FDBG(...) do { } while (0)
53#endif /* DEBUG */
54
55#ifdef VERBOSE_DEBUG
56# define FVDBG FDBG
57#else
58# define FVDBG(...) do { } while (0)
59#endif /* VERBOSE_DEBUG */
60
61#define ENTER() FVDBG("%s()", __func__)
62
63#ifdef VERBOSE_DEBUG
64# define ffs_dump_mem(prefix, ptr, len) \
65 print_hex_dump_bytes("f_fs" prefix ": ", DUMP_PREFIX_NONE, ptr, len)
66#else
67# define ffs_dump_mem(prefix, ptr, len) do { } while (0)
68#endif
69
70
71/* The data structure and setup file ****************************************/
72
73enum ffs_state {
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010074 /*
75 * Waiting for descriptors and strings.
76 *
77 * In this state no open(2), read(2) or write(2) on epfiles
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020078 * may succeed (which should not be the problem as there
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010079 * should be no such files opened in the first place).
80 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020081 FFS_READ_DESCRIPTORS,
82 FFS_READ_STRINGS,
83
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010084 /*
85 * We've got descriptors and strings. We are or have called
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020086 * functionfs_ready_callback(). functionfs_bind() may have
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010087 * been called but we don't know.
88 *
89 * This is the only state in which operations on epfiles may
90 * succeed.
91 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020092 FFS_ACTIVE,
93
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010094 /*
95 * All endpoints have been closed. This state is also set if
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020096 * we encounter an unrecoverable error. The only
97 * unrecoverable error is situation when after reading strings
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010098 * from user space we fail to initialise epfiles or
99 * functionfs_ready_callback() returns with error (<0).
100 *
101 * In this state no open(2), read(2) or write(2) (both on ep0
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200102 * as well as epfile) may succeed (at this point epfiles are
103 * unlinked and all closed so this is not a problem; ep0 is
104 * also closed but ep0 file exists and so open(2) on ep0 must
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100105 * fail).
106 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200107 FFS_CLOSING
108};
109
110
111enum ffs_setup_state {
112 /* There is no setup request pending. */
113 FFS_NO_SETUP,
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100114 /*
115 * User has read events and there was a setup request event
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200116 * there. The next read/write on ep0 will handle the
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100117 * request.
118 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200119 FFS_SETUP_PENDING,
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100120 /*
121 * There was event pending but before user space handled it
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200122 * some other event was introduced which canceled existing
123 * setup. If this state is set read/write on ep0 return
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100124 * -EIDRM. This state is only set when adding event.
125 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200126 FFS_SETUP_CANCELED
127};
128
129
130
131struct ffs_epfile;
132struct ffs_function;
133
134struct ffs_data {
135 struct usb_gadget *gadget;
136
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100137 /*
138 * Protect access read/write operations, only one read/write
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200139 * at a time. As a consequence protects ep0req and company.
140 * While setup request is being processed (queued) this is
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100141 * held.
142 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200143 struct mutex mutex;
144
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100145 /*
146 * Protect access to endpoint related structures (basically
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200147 * usb_ep_queue(), usb_ep_dequeue(), etc. calls) except for
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100148 * endpoint zero.
149 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200150 spinlock_t eps_lock;
151
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100152 /*
153 * XXX REVISIT do we need our own request? Since we are not
154 * handling setup requests immediately user space may be so
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200155 * slow that another setup will be sent to the gadget but this
156 * time not to us but another function and then there could be
Linus Torvaldsa4ce96a2010-07-21 09:25:42 -0700157 * a race. Is that the case? Or maybe we can use cdev->req
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100158 * after all, maybe we just need some spinlock for that?
159 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200160 struct usb_request *ep0req; /* P: mutex */
161 struct completion ep0req_completion; /* P: mutex */
162 int ep0req_status; /* P: mutex */
163
164 /* reference counter */
165 atomic_t ref;
166 /* how many files are opened (EP0 and others) */
167 atomic_t opened;
168
169 /* EP0 state */
170 enum ffs_state state;
171
172 /*
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100173 * Possible transitions:
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200174 * + FFS_NO_SETUP -> FFS_SETUP_PENDING -- P: ev.waitq.lock
175 * happens only in ep0 read which is P: mutex
176 * + FFS_SETUP_PENDING -> FFS_NO_SETUP -- P: ev.waitq.lock
177 * happens only in ep0 i/o which is P: mutex
178 * + FFS_SETUP_PENDING -> FFS_SETUP_CANCELED -- P: ev.waitq.lock
179 * + FFS_SETUP_CANCELED -> FFS_NO_SETUP -- cmpxchg
180 */
181 enum ffs_setup_state setup_state;
182
183#define FFS_SETUP_STATE(ffs) \
184 ((enum ffs_setup_state)cmpxchg(&(ffs)->setup_state, \
185 FFS_SETUP_CANCELED, FFS_NO_SETUP))
186
187 /* Events & such. */
188 struct {
189 u8 types[4];
190 unsigned short count;
191 /* XXX REVISIT need to update it in some places, or do we? */
192 unsigned short can_stall;
193 struct usb_ctrlrequest setup;
194
195 wait_queue_head_t waitq;
196 } ev; /* the whole structure, P: ev.waitq.lock */
197
198 /* Flags */
199 unsigned long flags;
200#define FFS_FL_CALL_CLOSED_CALLBACK 0
201#define FFS_FL_BOUND 1
202
203 /* Active function */
204 struct ffs_function *func;
205
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100206 /*
207 * Device name, write once when file system is mounted.
208 * Intended for user to read if she wants.
209 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200210 const char *dev_name;
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100211 /* Private data for our user (ie. gadget). Managed by user. */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200212 void *private_data;
213
214 /* filled by __ffs_data_got_descs() */
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100215 /*
216 * Real descriptors are 16 bytes after raw_descs (so you need
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200217 * to skip 16 bytes (ie. ffs->raw_descs + 16) to get to the
218 * first full speed descriptor). raw_descs_length and
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100219 * raw_fs_descs_length do not have those 16 bytes added.
220 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200221 const void *raw_descs;
222 unsigned raw_descs_length;
223 unsigned raw_fs_descs_length;
224 unsigned fs_descs_count;
225 unsigned hs_descs_count;
226
227 unsigned short strings_count;
228 unsigned short interfaces_count;
229 unsigned short eps_count;
230 unsigned short _pad1;
231
232 /* filled by __ffs_data_got_strings() */
233 /* ids in stringtabs are set in functionfs_bind() */
234 const void *raw_strings;
235 struct usb_gadget_strings **stringtabs;
236
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100237 /*
238 * File system's super block, write once when file system is
239 * mounted.
240 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200241 struct super_block *sb;
242
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100243 /* File permissions, written once when fs is mounted */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200244 struct ffs_file_perms {
245 umode_t mode;
246 uid_t uid;
247 gid_t gid;
248 } file_perms;
249
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100250 /*
251 * The endpoint files, filled by ffs_epfiles_create(),
252 * destroyed by ffs_epfiles_destroy().
253 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200254 struct ffs_epfile *epfiles;
255};
256
257/* Reference counter handling */
258static void ffs_data_get(struct ffs_data *ffs);
259static void ffs_data_put(struct ffs_data *ffs);
260/* Creates new ffs_data object. */
261static struct ffs_data *__must_check ffs_data_new(void) __attribute__((malloc));
262
263/* Opened counter handling. */
264static void ffs_data_opened(struct ffs_data *ffs);
265static void ffs_data_closed(struct ffs_data *ffs);
266
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100267/* Called with ffs->mutex held; take over ownership of data. */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200268static int __must_check
269__ffs_data_got_descs(struct ffs_data *ffs, char *data, size_t len);
270static int __must_check
271__ffs_data_got_strings(struct ffs_data *ffs, char *data, size_t len);
272
273
274/* The function structure ***************************************************/
275
276struct ffs_ep;
277
278struct ffs_function {
279 struct usb_configuration *conf;
280 struct usb_gadget *gadget;
281 struct ffs_data *ffs;
282
283 struct ffs_ep *eps;
284 u8 eps_revmap[16];
285 short *interfaces_nums;
286
287 struct usb_function function;
288};
289
290
291static struct ffs_function *ffs_func_from_usb(struct usb_function *f)
292{
293 return container_of(f, struct ffs_function, function);
294}
295
296static void ffs_func_free(struct ffs_function *func);
297
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200298static void ffs_func_eps_disable(struct ffs_function *func);
299static int __must_check ffs_func_eps_enable(struct ffs_function *func);
300
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200301static int ffs_func_bind(struct usb_configuration *,
302 struct usb_function *);
303static void ffs_func_unbind(struct usb_configuration *,
304 struct usb_function *);
305static int ffs_func_set_alt(struct usb_function *, unsigned, unsigned);
306static void ffs_func_disable(struct usb_function *);
307static int ffs_func_setup(struct usb_function *,
308 const struct usb_ctrlrequest *);
309static void ffs_func_suspend(struct usb_function *);
310static void ffs_func_resume(struct usb_function *);
311
312
313static int ffs_func_revmap_ep(struct ffs_function *func, u8 num);
314static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf);
315
316
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200317/* The endpoints structures *************************************************/
318
319struct ffs_ep {
320 struct usb_ep *ep; /* P: ffs->eps_lock */
321 struct usb_request *req; /* P: epfile->mutex */
322
323 /* [0]: full speed, [1]: high speed */
324 struct usb_endpoint_descriptor *descs[2];
325
326 u8 num;
327
328 int status; /* P: epfile->mutex */
329};
330
331struct ffs_epfile {
332 /* Protects ep->ep and ep->req. */
333 struct mutex mutex;
334 wait_queue_head_t wait;
335
336 struct ffs_data *ffs;
337 struct ffs_ep *ep; /* P: ffs->eps_lock */
338
339 struct dentry *dentry;
340
341 char name[5];
342
343 unsigned char in; /* P: ffs->eps_lock */
344 unsigned char isoc; /* P: ffs->eps_lock */
345
346 unsigned char _pad;
347};
348
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200349static int __must_check ffs_epfiles_create(struct ffs_data *ffs);
350static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count);
351
352static struct inode *__must_check
353ffs_sb_create_file(struct super_block *sb, const char *name, void *data,
354 const struct file_operations *fops,
355 struct dentry **dentry_p);
356
357
358/* Misc helper functions ****************************************************/
359
360static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
361 __attribute__((warn_unused_result, nonnull));
362static char *ffs_prepare_buffer(const char * __user buf, size_t len)
363 __attribute__((warn_unused_result, nonnull));
364
365
366/* Control file aka ep0 *****************************************************/
367
368static void ffs_ep0_complete(struct usb_ep *ep, struct usb_request *req)
369{
370 struct ffs_data *ffs = req->context;
371
372 complete_all(&ffs->ep0req_completion);
373}
374
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200375static int __ffs_ep0_queue_wait(struct ffs_data *ffs, char *data, size_t len)
376{
377 struct usb_request *req = ffs->ep0req;
378 int ret;
379
380 req->zero = len < le16_to_cpu(ffs->ev.setup.wLength);
381
382 spin_unlock_irq(&ffs->ev.waitq.lock);
383
384 req->buf = data;
385 req->length = len;
386
387 INIT_COMPLETION(ffs->ep0req_completion);
388
389 ret = usb_ep_queue(ffs->gadget->ep0, req, GFP_ATOMIC);
390 if (unlikely(ret < 0))
391 return ret;
392
393 ret = wait_for_completion_interruptible(&ffs->ep0req_completion);
394 if (unlikely(ret)) {
395 usb_ep_dequeue(ffs->gadget->ep0, req);
396 return -EINTR;
397 }
398
399 ffs->setup_state = FFS_NO_SETUP;
400 return ffs->ep0req_status;
401}
402
403static int __ffs_ep0_stall(struct ffs_data *ffs)
404{
405 if (ffs->ev.can_stall) {
406 FVDBG("ep0 stall\n");
407 usb_ep_set_halt(ffs->gadget->ep0);
408 ffs->setup_state = FFS_NO_SETUP;
409 return -EL2HLT;
410 } else {
411 FDBG("bogus ep0 stall!\n");
412 return -ESRCH;
413 }
414}
415
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200416static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
417 size_t len, loff_t *ptr)
418{
419 struct ffs_data *ffs = file->private_data;
420 ssize_t ret;
421 char *data;
422
423 ENTER();
424
425 /* Fast check if setup was canceled */
426 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED)
427 return -EIDRM;
428
429 /* Acquire mutex */
430 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
431 if (unlikely(ret < 0))
432 return ret;
433
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200434 /* Check state */
435 switch (ffs->state) {
436 case FFS_READ_DESCRIPTORS:
437 case FFS_READ_STRINGS:
438 /* Copy data */
439 if (unlikely(len < 16)) {
440 ret = -EINVAL;
441 break;
442 }
443
444 data = ffs_prepare_buffer(buf, len);
445 if (unlikely(IS_ERR(data))) {
446 ret = PTR_ERR(data);
447 break;
448 }
449
450 /* Handle data */
451 if (ffs->state == FFS_READ_DESCRIPTORS) {
452 FINFO("read descriptors");
453 ret = __ffs_data_got_descs(ffs, data, len);
454 if (unlikely(ret < 0))
455 break;
456
457 ffs->state = FFS_READ_STRINGS;
458 ret = len;
459 } else {
460 FINFO("read strings");
461 ret = __ffs_data_got_strings(ffs, data, len);
462 if (unlikely(ret < 0))
463 break;
464
465 ret = ffs_epfiles_create(ffs);
466 if (unlikely(ret)) {
467 ffs->state = FFS_CLOSING;
468 break;
469 }
470
471 ffs->state = FFS_ACTIVE;
472 mutex_unlock(&ffs->mutex);
473
474 ret = functionfs_ready_callback(ffs);
475 if (unlikely(ret < 0)) {
476 ffs->state = FFS_CLOSING;
477 return ret;
478 }
479
480 set_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags);
481 return len;
482 }
483 break;
484
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200485 case FFS_ACTIVE:
486 data = NULL;
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100487 /*
488 * We're called from user space, we can use _irq
489 * rather then _irqsave
490 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200491 spin_lock_irq(&ffs->ev.waitq.lock);
492 switch (FFS_SETUP_STATE(ffs)) {
493 case FFS_SETUP_CANCELED:
494 ret = -EIDRM;
495 goto done_spin;
496
497 case FFS_NO_SETUP:
498 ret = -ESRCH;
499 goto done_spin;
500
501 case FFS_SETUP_PENDING:
502 break;
503 }
504
505 /* FFS_SETUP_PENDING */
506 if (!(ffs->ev.setup.bRequestType & USB_DIR_IN)) {
507 spin_unlock_irq(&ffs->ev.waitq.lock);
508 ret = __ffs_ep0_stall(ffs);
509 break;
510 }
511
512 /* FFS_SETUP_PENDING and not stall */
513 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
514
515 spin_unlock_irq(&ffs->ev.waitq.lock);
516
517 data = ffs_prepare_buffer(buf, len);
518 if (unlikely(IS_ERR(data))) {
519 ret = PTR_ERR(data);
520 break;
521 }
522
523 spin_lock_irq(&ffs->ev.waitq.lock);
524
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100525 /*
526 * We are guaranteed to be still in FFS_ACTIVE state
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200527 * but the state of setup could have changed from
528 * FFS_SETUP_PENDING to FFS_SETUP_CANCELED so we need
529 * to check for that. If that happened we copied data
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100530 * from user space in vain but it's unlikely.
531 *
532 * For sure we are not in FFS_NO_SETUP since this is
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200533 * the only place FFS_SETUP_PENDING -> FFS_NO_SETUP
534 * transition can be performed and it's protected by
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100535 * mutex.
536 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200537 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) {
538 ret = -EIDRM;
539done_spin:
540 spin_unlock_irq(&ffs->ev.waitq.lock);
541 } else {
542 /* unlocks spinlock */
543 ret = __ffs_ep0_queue_wait(ffs, data, len);
544 }
545 kfree(data);
546 break;
547
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200548 default:
549 ret = -EBADFD;
550 break;
551 }
552
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200553 mutex_unlock(&ffs->mutex);
554 return ret;
555}
556
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200557static ssize_t __ffs_ep0_read_events(struct ffs_data *ffs, char __user *buf,
558 size_t n)
559{
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100560 /*
561 * We are holding ffs->ev.waitq.lock and ffs->mutex and we need
562 * to release them.
563 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200564 struct usb_functionfs_event events[n];
565 unsigned i = 0;
566
567 memset(events, 0, sizeof events);
568
569 do {
570 events[i].type = ffs->ev.types[i];
571 if (events[i].type == FUNCTIONFS_SETUP) {
572 events[i].u.setup = ffs->ev.setup;
573 ffs->setup_state = FFS_SETUP_PENDING;
574 }
575 } while (++i < n);
576
577 if (n < ffs->ev.count) {
578 ffs->ev.count -= n;
579 memmove(ffs->ev.types, ffs->ev.types + n,
580 ffs->ev.count * sizeof *ffs->ev.types);
581 } else {
582 ffs->ev.count = 0;
583 }
584
585 spin_unlock_irq(&ffs->ev.waitq.lock);
586 mutex_unlock(&ffs->mutex);
587
588 return unlikely(__copy_to_user(buf, events, sizeof events))
589 ? -EFAULT : sizeof events;
590}
591
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200592static ssize_t ffs_ep0_read(struct file *file, char __user *buf,
593 size_t len, loff_t *ptr)
594{
595 struct ffs_data *ffs = file->private_data;
596 char *data = NULL;
597 size_t n;
598 int ret;
599
600 ENTER();
601
602 /* Fast check if setup was canceled */
603 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED)
604 return -EIDRM;
605
606 /* Acquire mutex */
607 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
608 if (unlikely(ret < 0))
609 return ret;
610
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200611 /* Check state */
612 if (ffs->state != FFS_ACTIVE) {
613 ret = -EBADFD;
614 goto done_mutex;
615 }
616
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100617 /*
618 * We're called from user space, we can use _irq rather then
619 * _irqsave
620 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200621 spin_lock_irq(&ffs->ev.waitq.lock);
622
623 switch (FFS_SETUP_STATE(ffs)) {
624 case FFS_SETUP_CANCELED:
625 ret = -EIDRM;
626 break;
627
628 case FFS_NO_SETUP:
629 n = len / sizeof(struct usb_functionfs_event);
630 if (unlikely(!n)) {
631 ret = -EINVAL;
632 break;
633 }
634
635 if ((file->f_flags & O_NONBLOCK) && !ffs->ev.count) {
636 ret = -EAGAIN;
637 break;
638 }
639
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100640 if (wait_event_interruptible_exclusive_locked_irq(ffs->ev.waitq,
641 ffs->ev.count)) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200642 ret = -EINTR;
643 break;
644 }
645
646 return __ffs_ep0_read_events(ffs, buf,
647 min(n, (size_t)ffs->ev.count));
648
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200649 case FFS_SETUP_PENDING:
650 if (ffs->ev.setup.bRequestType & USB_DIR_IN) {
651 spin_unlock_irq(&ffs->ev.waitq.lock);
652 ret = __ffs_ep0_stall(ffs);
653 goto done_mutex;
654 }
655
656 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
657
658 spin_unlock_irq(&ffs->ev.waitq.lock);
659
660 if (likely(len)) {
661 data = kmalloc(len, GFP_KERNEL);
662 if (unlikely(!data)) {
663 ret = -ENOMEM;
664 goto done_mutex;
665 }
666 }
667
668 spin_lock_irq(&ffs->ev.waitq.lock);
669
670 /* See ffs_ep0_write() */
671 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) {
672 ret = -EIDRM;
673 break;
674 }
675
676 /* unlocks spinlock */
677 ret = __ffs_ep0_queue_wait(ffs, data, len);
678 if (likely(ret > 0) && unlikely(__copy_to_user(buf, data, len)))
679 ret = -EFAULT;
680 goto done_mutex;
681
682 default:
683 ret = -EBADFD;
684 break;
685 }
686
687 spin_unlock_irq(&ffs->ev.waitq.lock);
688done_mutex:
689 mutex_unlock(&ffs->mutex);
690 kfree(data);
691 return ret;
692}
693
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200694static int ffs_ep0_open(struct inode *inode, struct file *file)
695{
696 struct ffs_data *ffs = inode->i_private;
697
698 ENTER();
699
700 if (unlikely(ffs->state == FFS_CLOSING))
701 return -EBUSY;
702
703 file->private_data = ffs;
704 ffs_data_opened(ffs);
705
706 return 0;
707}
708
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200709static int ffs_ep0_release(struct inode *inode, struct file *file)
710{
711 struct ffs_data *ffs = file->private_data;
712
713 ENTER();
714
715 ffs_data_closed(ffs);
716
717 return 0;
718}
719
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200720static long ffs_ep0_ioctl(struct file *file, unsigned code, unsigned long value)
721{
722 struct ffs_data *ffs = file->private_data;
723 struct usb_gadget *gadget = ffs->gadget;
724 long ret;
725
726 ENTER();
727
728 if (code == FUNCTIONFS_INTERFACE_REVMAP) {
729 struct ffs_function *func = ffs->func;
730 ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV;
731 } else if (gadget->ops->ioctl) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200732 ret = gadget->ops->ioctl(gadget, code, value);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200733 } else {
734 ret = -ENOTTY;
735 }
736
737 return ret;
738}
739
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200740static const struct file_operations ffs_ep0_operations = {
741 .owner = THIS_MODULE,
742 .llseek = no_llseek,
743
744 .open = ffs_ep0_open,
745 .write = ffs_ep0_write,
746 .read = ffs_ep0_read,
747 .release = ffs_ep0_release,
748 .unlocked_ioctl = ffs_ep0_ioctl,
749};
750
751
752/* "Normal" endpoints operations ********************************************/
753
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200754static void ffs_epfile_io_complete(struct usb_ep *_ep, struct usb_request *req)
755{
756 ENTER();
757 if (likely(req->context)) {
758 struct ffs_ep *ep = _ep->driver_data;
759 ep->status = req->status ? req->status : req->actual;
760 complete(req->context);
761 }
762}
763
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200764static ssize_t ffs_epfile_io(struct file *file,
765 char __user *buf, size_t len, int read)
766{
767 struct ffs_epfile *epfile = file->private_data;
768 struct ffs_ep *ep;
769 char *data = NULL;
770 ssize_t ret;
771 int halt;
772
773 goto first_try;
774 do {
775 spin_unlock_irq(&epfile->ffs->eps_lock);
776 mutex_unlock(&epfile->mutex);
777
778first_try:
779 /* Are we still active? */
780 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE)) {
781 ret = -ENODEV;
782 goto error;
783 }
784
785 /* Wait for endpoint to be enabled */
786 ep = epfile->ep;
787 if (!ep) {
788 if (file->f_flags & O_NONBLOCK) {
789 ret = -EAGAIN;
790 goto error;
791 }
792
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100793 if (wait_event_interruptible(epfile->wait,
794 (ep = epfile->ep))) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200795 ret = -EINTR;
796 goto error;
797 }
798 }
799
800 /* Do we halt? */
801 halt = !read == !epfile->in;
802 if (halt && epfile->isoc) {
803 ret = -EINVAL;
804 goto error;
805 }
806
807 /* Allocate & copy */
808 if (!halt && !data) {
809 data = kzalloc(len, GFP_KERNEL);
810 if (unlikely(!data))
811 return -ENOMEM;
812
813 if (!read &&
814 unlikely(__copy_from_user(data, buf, len))) {
815 ret = -EFAULT;
816 goto error;
817 }
818 }
819
820 /* We will be using request */
821 ret = ffs_mutex_lock(&epfile->mutex,
822 file->f_flags & O_NONBLOCK);
823 if (unlikely(ret))
824 goto error;
825
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100826 /*
827 * We're called from user space, we can use _irq rather then
828 * _irqsave
829 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200830 spin_lock_irq(&epfile->ffs->eps_lock);
831
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100832 /*
833 * While we were acquiring mutex endpoint got disabled
834 * or changed?
835 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200836 } while (unlikely(epfile->ep != ep));
837
838 /* Halt */
839 if (unlikely(halt)) {
840 if (likely(epfile->ep == ep) && !WARN_ON(!ep->ep))
841 usb_ep_set_halt(ep->ep);
842 spin_unlock_irq(&epfile->ffs->eps_lock);
843 ret = -EBADMSG;
844 } else {
845 /* Fire the request */
846 DECLARE_COMPLETION_ONSTACK(done);
847
848 struct usb_request *req = ep->req;
849 req->context = &done;
850 req->complete = ffs_epfile_io_complete;
851 req->buf = data;
852 req->length = len;
853
854 ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
855
856 spin_unlock_irq(&epfile->ffs->eps_lock);
857
858 if (unlikely(ret < 0)) {
859 /* nop */
860 } else if (unlikely(wait_for_completion_interruptible(&done))) {
861 ret = -EINTR;
862 usb_ep_dequeue(ep->ep, req);
863 } else {
864 ret = ep->status;
865 if (read && ret > 0 &&
866 unlikely(copy_to_user(buf, data, ret)))
867 ret = -EFAULT;
868 }
869 }
870
871 mutex_unlock(&epfile->mutex);
872error:
873 kfree(data);
874 return ret;
875}
876
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200877static ssize_t
878ffs_epfile_write(struct file *file, const char __user *buf, size_t len,
879 loff_t *ptr)
880{
881 ENTER();
882
883 return ffs_epfile_io(file, (char __user *)buf, len, 0);
884}
885
886static ssize_t
887ffs_epfile_read(struct file *file, char __user *buf, size_t len, loff_t *ptr)
888{
889 ENTER();
890
891 return ffs_epfile_io(file, buf, len, 1);
892}
893
894static int
895ffs_epfile_open(struct inode *inode, struct file *file)
896{
897 struct ffs_epfile *epfile = inode->i_private;
898
899 ENTER();
900
901 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
902 return -ENODEV;
903
904 file->private_data = epfile;
905 ffs_data_opened(epfile->ffs);
906
907 return 0;
908}
909
910static int
911ffs_epfile_release(struct inode *inode, struct file *file)
912{
913 struct ffs_epfile *epfile = inode->i_private;
914
915 ENTER();
916
917 ffs_data_closed(epfile->ffs);
918
919 return 0;
920}
921
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200922static long ffs_epfile_ioctl(struct file *file, unsigned code,
923 unsigned long value)
924{
925 struct ffs_epfile *epfile = file->private_data;
926 int ret;
927
928 ENTER();
929
930 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
931 return -ENODEV;
932
933 spin_lock_irq(&epfile->ffs->eps_lock);
934 if (likely(epfile->ep)) {
935 switch (code) {
936 case FUNCTIONFS_FIFO_STATUS:
937 ret = usb_ep_fifo_status(epfile->ep->ep);
938 break;
939 case FUNCTIONFS_FIFO_FLUSH:
940 usb_ep_fifo_flush(epfile->ep->ep);
941 ret = 0;
942 break;
943 case FUNCTIONFS_CLEAR_HALT:
944 ret = usb_ep_clear_halt(epfile->ep->ep);
945 break;
946 case FUNCTIONFS_ENDPOINT_REVMAP:
947 ret = epfile->ep->num;
948 break;
949 default:
950 ret = -ENOTTY;
951 }
952 } else {
953 ret = -ENODEV;
954 }
955 spin_unlock_irq(&epfile->ffs->eps_lock);
956
957 return ret;
958}
959
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200960static const struct file_operations ffs_epfile_operations = {
961 .owner = THIS_MODULE,
962 .llseek = no_llseek,
963
964 .open = ffs_epfile_open,
965 .write = ffs_epfile_write,
966 .read = ffs_epfile_read,
967 .release = ffs_epfile_release,
968 .unlocked_ioctl = ffs_epfile_ioctl,
969};
970
971
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200972/* File system and super block operations ***********************************/
973
974/*
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100975 * Mounting the file system creates a controller file, used first for
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200976 * function configuration then later for event monitoring.
977 */
978
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200979static struct inode *__must_check
980ffs_sb_make_inode(struct super_block *sb, void *data,
981 const struct file_operations *fops,
982 const struct inode_operations *iops,
983 struct ffs_file_perms *perms)
984{
985 struct inode *inode;
986
987 ENTER();
988
989 inode = new_inode(sb);
990
991 if (likely(inode)) {
992 struct timespec current_time = CURRENT_TIME;
993
Al Viro12ba8d12010-10-27 04:19:36 +0100994 inode->i_ino = get_next_ino();
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200995 inode->i_mode = perms->mode;
996 inode->i_uid = perms->uid;
997 inode->i_gid = perms->gid;
998 inode->i_atime = current_time;
999 inode->i_mtime = current_time;
1000 inode->i_ctime = current_time;
1001 inode->i_private = data;
1002 if (fops)
1003 inode->i_fop = fops;
1004 if (iops)
1005 inode->i_op = iops;
1006 }
1007
1008 return inode;
1009}
1010
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001011/* Create "regular" file */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001012static struct inode *ffs_sb_create_file(struct super_block *sb,
1013 const char *name, void *data,
1014 const struct file_operations *fops,
1015 struct dentry **dentry_p)
1016{
1017 struct ffs_data *ffs = sb->s_fs_info;
1018 struct dentry *dentry;
1019 struct inode *inode;
1020
1021 ENTER();
1022
1023 dentry = d_alloc_name(sb->s_root, name);
1024 if (unlikely(!dentry))
1025 return NULL;
1026
1027 inode = ffs_sb_make_inode(sb, data, fops, NULL, &ffs->file_perms);
1028 if (unlikely(!inode)) {
1029 dput(dentry);
1030 return NULL;
1031 }
1032
1033 d_add(dentry, inode);
1034 if (dentry_p)
1035 *dentry_p = dentry;
1036
1037 return inode;
1038}
1039
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001040/* Super block */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001041static const struct super_operations ffs_sb_operations = {
1042 .statfs = simple_statfs,
1043 .drop_inode = generic_delete_inode,
1044};
1045
1046struct ffs_sb_fill_data {
1047 struct ffs_file_perms perms;
1048 umode_t root_mode;
1049 const char *dev_name;
1050};
1051
1052static int ffs_sb_fill(struct super_block *sb, void *_data, int silent)
1053{
1054 struct ffs_sb_fill_data *data = _data;
1055 struct inode *inode;
1056 struct dentry *d;
1057 struct ffs_data *ffs;
1058
1059 ENTER();
1060
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001061 /* Initialise data */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001062 ffs = ffs_data_new();
1063 if (unlikely(!ffs))
1064 goto enomem0;
1065
1066 ffs->sb = sb;
1067 ffs->dev_name = data->dev_name;
1068 ffs->file_perms = data->perms;
1069
1070 sb->s_fs_info = ffs;
1071 sb->s_blocksize = PAGE_CACHE_SIZE;
1072 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1073 sb->s_magic = FUNCTIONFS_MAGIC;
1074 sb->s_op = &ffs_sb_operations;
1075 sb->s_time_gran = 1;
1076
1077 /* Root inode */
1078 data->perms.mode = data->root_mode;
1079 inode = ffs_sb_make_inode(sb, NULL,
1080 &simple_dir_operations,
1081 &simple_dir_inode_operations,
1082 &data->perms);
1083 if (unlikely(!inode))
1084 goto enomem1;
1085 d = d_alloc_root(inode);
1086 if (unlikely(!d))
1087 goto enomem2;
1088 sb->s_root = d;
1089
1090 /* EP0 file */
1091 if (unlikely(!ffs_sb_create_file(sb, "ep0", ffs,
1092 &ffs_ep0_operations, NULL)))
1093 goto enomem3;
1094
1095 return 0;
1096
1097enomem3:
1098 dput(d);
1099enomem2:
1100 iput(inode);
1101enomem1:
1102 ffs_data_put(ffs);
1103enomem0:
1104 return -ENOMEM;
1105}
1106
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001107static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts)
1108{
1109 ENTER();
1110
1111 if (!opts || !*opts)
1112 return 0;
1113
1114 for (;;) {
1115 char *end, *eq, *comma;
1116 unsigned long value;
1117
1118 /* Option limit */
1119 comma = strchr(opts, ',');
1120 if (comma)
1121 *comma = 0;
1122
1123 /* Value limit */
1124 eq = strchr(opts, '=');
1125 if (unlikely(!eq)) {
1126 FERR("'=' missing in %s", opts);
1127 return -EINVAL;
1128 }
1129 *eq = 0;
1130
1131 /* Parse value */
1132 value = simple_strtoul(eq + 1, &end, 0);
1133 if (unlikely(*end != ',' && *end != 0)) {
1134 FERR("%s: invalid value: %s", opts, eq + 1);
1135 return -EINVAL;
1136 }
1137
1138 /* Interpret option */
1139 switch (eq - opts) {
1140 case 5:
1141 if (!memcmp(opts, "rmode", 5))
1142 data->root_mode = (value & 0555) | S_IFDIR;
1143 else if (!memcmp(opts, "fmode", 5))
1144 data->perms.mode = (value & 0666) | S_IFREG;
1145 else
1146 goto invalid;
1147 break;
1148
1149 case 4:
1150 if (!memcmp(opts, "mode", 4)) {
1151 data->root_mode = (value & 0555) | S_IFDIR;
1152 data->perms.mode = (value & 0666) | S_IFREG;
1153 } else {
1154 goto invalid;
1155 }
1156 break;
1157
1158 case 3:
1159 if (!memcmp(opts, "uid", 3))
1160 data->perms.uid = value;
1161 else if (!memcmp(opts, "gid", 3))
1162 data->perms.gid = value;
1163 else
1164 goto invalid;
1165 break;
1166
1167 default:
1168invalid:
1169 FERR("%s: invalid option", opts);
1170 return -EINVAL;
1171 }
1172
1173 /* Next iteration */
1174 if (!comma)
1175 break;
1176 opts = comma + 1;
1177 }
1178
1179 return 0;
1180}
1181
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001182/* "mount -t functionfs dev_name /dev/function" ends up here */
1183
Al Virofc14f2f2010-07-25 01:48:30 +04001184static struct dentry *
1185ffs_fs_mount(struct file_system_type *t, int flags,
1186 const char *dev_name, void *opts)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001187{
1188 struct ffs_sb_fill_data data = {
1189 .perms = {
1190 .mode = S_IFREG | 0600,
1191 .uid = 0,
1192 .gid = 0
1193 },
1194 .root_mode = S_IFDIR | 0500,
1195 };
1196 int ret;
1197
1198 ENTER();
1199
1200 ret = functionfs_check_dev_callback(dev_name);
1201 if (unlikely(ret < 0))
Al Virofc14f2f2010-07-25 01:48:30 +04001202 return ERR_PTR(ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001203
1204 ret = ffs_fs_parse_opts(&data, opts);
1205 if (unlikely(ret < 0))
Al Virofc14f2f2010-07-25 01:48:30 +04001206 return ERR_PTR(ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001207
1208 data.dev_name = dev_name;
Al Virofc14f2f2010-07-25 01:48:30 +04001209 return mount_single(t, flags, &data, ffs_sb_fill);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001210}
1211
1212static void
1213ffs_fs_kill_sb(struct super_block *sb)
1214{
1215 void *ptr;
1216
1217 ENTER();
1218
1219 kill_litter_super(sb);
1220 ptr = xchg(&sb->s_fs_info, NULL);
1221 if (ptr)
1222 ffs_data_put(ptr);
1223}
1224
1225static struct file_system_type ffs_fs_type = {
1226 .owner = THIS_MODULE,
1227 .name = "functionfs",
Al Virofc14f2f2010-07-25 01:48:30 +04001228 .mount = ffs_fs_mount,
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001229 .kill_sb = ffs_fs_kill_sb,
1230};
1231
1232
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001233/* Driver's main init/cleanup functions *************************************/
1234
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001235static int functionfs_init(void)
1236{
1237 int ret;
1238
1239 ENTER();
1240
1241 ret = register_filesystem(&ffs_fs_type);
1242 if (likely(!ret))
1243 FINFO("file system registered");
1244 else
1245 FERR("failed registering file system (%d)", ret);
1246
1247 return ret;
1248}
1249
1250static void functionfs_cleanup(void)
1251{
1252 ENTER();
1253
1254 FINFO("unloading");
1255 unregister_filesystem(&ffs_fs_type);
1256}
1257
1258
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001259/* ffs_data and ffs_function construction and destruction code **************/
1260
1261static void ffs_data_clear(struct ffs_data *ffs);
1262static void ffs_data_reset(struct ffs_data *ffs);
1263
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001264static void ffs_data_get(struct ffs_data *ffs)
1265{
1266 ENTER();
1267
1268 atomic_inc(&ffs->ref);
1269}
1270
1271static void ffs_data_opened(struct ffs_data *ffs)
1272{
1273 ENTER();
1274
1275 atomic_inc(&ffs->ref);
1276 atomic_inc(&ffs->opened);
1277}
1278
1279static void ffs_data_put(struct ffs_data *ffs)
1280{
1281 ENTER();
1282
1283 if (unlikely(atomic_dec_and_test(&ffs->ref))) {
1284 FINFO("%s(): freeing", __func__);
1285 ffs_data_clear(ffs);
1286 BUG_ON(mutex_is_locked(&ffs->mutex) ||
1287 spin_is_locked(&ffs->ev.waitq.lock) ||
1288 waitqueue_active(&ffs->ev.waitq) ||
1289 waitqueue_active(&ffs->ep0req_completion.wait));
1290 kfree(ffs);
1291 }
1292}
1293
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001294static void ffs_data_closed(struct ffs_data *ffs)
1295{
1296 ENTER();
1297
1298 if (atomic_dec_and_test(&ffs->opened)) {
1299 ffs->state = FFS_CLOSING;
1300 ffs_data_reset(ffs);
1301 }
1302
1303 ffs_data_put(ffs);
1304}
1305
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001306static struct ffs_data *ffs_data_new(void)
1307{
1308 struct ffs_data *ffs = kzalloc(sizeof *ffs, GFP_KERNEL);
1309 if (unlikely(!ffs))
1310 return 0;
1311
1312 ENTER();
1313
1314 atomic_set(&ffs->ref, 1);
1315 atomic_set(&ffs->opened, 0);
1316 ffs->state = FFS_READ_DESCRIPTORS;
1317 mutex_init(&ffs->mutex);
1318 spin_lock_init(&ffs->eps_lock);
1319 init_waitqueue_head(&ffs->ev.waitq);
1320 init_completion(&ffs->ep0req_completion);
1321
1322 /* XXX REVISIT need to update it in some places, or do we? */
1323 ffs->ev.can_stall = 1;
1324
1325 return ffs;
1326}
1327
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001328static void ffs_data_clear(struct ffs_data *ffs)
1329{
1330 ENTER();
1331
1332 if (test_and_clear_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags))
1333 functionfs_closed_callback(ffs);
1334
1335 BUG_ON(ffs->gadget);
1336
1337 if (ffs->epfiles)
1338 ffs_epfiles_destroy(ffs->epfiles, ffs->eps_count);
1339
1340 kfree(ffs->raw_descs);
1341 kfree(ffs->raw_strings);
1342 kfree(ffs->stringtabs);
1343}
1344
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001345static void ffs_data_reset(struct ffs_data *ffs)
1346{
1347 ENTER();
1348
1349 ffs_data_clear(ffs);
1350
1351 ffs->epfiles = NULL;
1352 ffs->raw_descs = NULL;
1353 ffs->raw_strings = NULL;
1354 ffs->stringtabs = NULL;
1355
1356 ffs->raw_descs_length = 0;
1357 ffs->raw_fs_descs_length = 0;
1358 ffs->fs_descs_count = 0;
1359 ffs->hs_descs_count = 0;
1360
1361 ffs->strings_count = 0;
1362 ffs->interfaces_count = 0;
1363 ffs->eps_count = 0;
1364
1365 ffs->ev.count = 0;
1366
1367 ffs->state = FFS_READ_DESCRIPTORS;
1368 ffs->setup_state = FFS_NO_SETUP;
1369 ffs->flags = 0;
1370}
1371
1372
1373static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev)
1374{
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001375 struct usb_gadget_strings **lang;
1376 int first_id;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001377
1378 ENTER();
1379
1380 if (WARN_ON(ffs->state != FFS_ACTIVE
1381 || test_and_set_bit(FFS_FL_BOUND, &ffs->flags)))
1382 return -EBADFD;
1383
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001384 first_id = usb_string_ids_n(cdev, ffs->strings_count);
1385 if (unlikely(first_id < 0))
1386 return first_id;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001387
1388 ffs->ep0req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
1389 if (unlikely(!ffs->ep0req))
1390 return -ENOMEM;
1391 ffs->ep0req->complete = ffs_ep0_complete;
1392 ffs->ep0req->context = ffs;
1393
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001394 lang = ffs->stringtabs;
1395 for (lang = ffs->stringtabs; *lang; ++lang) {
1396 struct usb_string *str = (*lang)->strings;
1397 int id = first_id;
1398 for (; str->s; ++id, ++str)
1399 str->id = id;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001400 }
1401
1402 ffs->gadget = cdev->gadget;
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001403 ffs_data_get(ffs);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001404 return 0;
1405}
1406
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001407static void functionfs_unbind(struct ffs_data *ffs)
1408{
1409 ENTER();
1410
1411 if (!WARN_ON(!ffs->gadget)) {
1412 usb_ep_free_request(ffs->gadget->ep0, ffs->ep0req);
1413 ffs->ep0req = NULL;
1414 ffs->gadget = NULL;
1415 ffs_data_put(ffs);
1416 }
1417}
1418
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001419static int ffs_epfiles_create(struct ffs_data *ffs)
1420{
1421 struct ffs_epfile *epfile, *epfiles;
1422 unsigned i, count;
1423
1424 ENTER();
1425
1426 count = ffs->eps_count;
1427 epfiles = kzalloc(count * sizeof *epfiles, GFP_KERNEL);
1428 if (!epfiles)
1429 return -ENOMEM;
1430
1431 epfile = epfiles;
1432 for (i = 1; i <= count; ++i, ++epfile) {
1433 epfile->ffs = ffs;
1434 mutex_init(&epfile->mutex);
1435 init_waitqueue_head(&epfile->wait);
1436 sprintf(epfiles->name, "ep%u", i);
1437 if (!unlikely(ffs_sb_create_file(ffs->sb, epfiles->name, epfile,
1438 &ffs_epfile_operations,
1439 &epfile->dentry))) {
1440 ffs_epfiles_destroy(epfiles, i - 1);
1441 return -ENOMEM;
1442 }
1443 }
1444
1445 ffs->epfiles = epfiles;
1446 return 0;
1447}
1448
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001449static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count)
1450{
1451 struct ffs_epfile *epfile = epfiles;
1452
1453 ENTER();
1454
1455 for (; count; --count, ++epfile) {
1456 BUG_ON(mutex_is_locked(&epfile->mutex) ||
1457 waitqueue_active(&epfile->wait));
1458 if (epfile->dentry) {
1459 d_delete(epfile->dentry);
1460 dput(epfile->dentry);
1461 epfile->dentry = NULL;
1462 }
1463 }
1464
1465 kfree(epfiles);
1466}
1467
Michal Nazarewicz7898aee2010-06-16 12:07:58 +02001468static int functionfs_bind_config(struct usb_composite_dev *cdev,
1469 struct usb_configuration *c,
1470 struct ffs_data *ffs)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001471{
1472 struct ffs_function *func;
1473 int ret;
1474
1475 ENTER();
1476
1477 func = kzalloc(sizeof *func, GFP_KERNEL);
1478 if (unlikely(!func))
1479 return -ENOMEM;
1480
1481 func->function.name = "Function FS Gadget";
1482 func->function.strings = ffs->stringtabs;
1483
1484 func->function.bind = ffs_func_bind;
1485 func->function.unbind = ffs_func_unbind;
1486 func->function.set_alt = ffs_func_set_alt;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001487 func->function.disable = ffs_func_disable;
1488 func->function.setup = ffs_func_setup;
1489 func->function.suspend = ffs_func_suspend;
1490 func->function.resume = ffs_func_resume;
1491
1492 func->conf = c;
1493 func->gadget = cdev->gadget;
1494 func->ffs = ffs;
1495 ffs_data_get(ffs);
1496
1497 ret = usb_add_function(c, &func->function);
1498 if (unlikely(ret))
1499 ffs_func_free(func);
1500
1501 return ret;
1502}
1503
1504static void ffs_func_free(struct ffs_function *func)
1505{
1506 ENTER();
1507
1508 ffs_data_put(func->ffs);
1509
1510 kfree(func->eps);
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001511 /*
1512 * eps and interfaces_nums are allocated in the same chunk so
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001513 * only one free is required. Descriptors are also allocated
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001514 * in the same chunk.
1515 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001516
1517 kfree(func);
1518}
1519
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001520static void ffs_func_eps_disable(struct ffs_function *func)
1521{
1522 struct ffs_ep *ep = func->eps;
1523 struct ffs_epfile *epfile = func->ffs->epfiles;
1524 unsigned count = func->ffs->eps_count;
1525 unsigned long flags;
1526
1527 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1528 do {
1529 /* pending requests get nuked */
1530 if (likely(ep->ep))
1531 usb_ep_disable(ep->ep);
1532 epfile->ep = NULL;
1533
1534 ++ep;
1535 ++epfile;
1536 } while (--count);
1537 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1538}
1539
1540static int ffs_func_eps_enable(struct ffs_function *func)
1541{
1542 struct ffs_data *ffs = func->ffs;
1543 struct ffs_ep *ep = func->eps;
1544 struct ffs_epfile *epfile = ffs->epfiles;
1545 unsigned count = ffs->eps_count;
1546 unsigned long flags;
1547 int ret = 0;
1548
1549 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1550 do {
1551 struct usb_endpoint_descriptor *ds;
1552 ds = ep->descs[ep->descs[1] ? 1 : 0];
1553
1554 ep->ep->driver_data = ep;
1555 ret = usb_ep_enable(ep->ep, ds);
1556 if (likely(!ret)) {
1557 epfile->ep = ep;
1558 epfile->in = usb_endpoint_dir_in(ds);
1559 epfile->isoc = usb_endpoint_xfer_isoc(ds);
1560 } else {
1561 break;
1562 }
1563
1564 wake_up(&epfile->wait);
1565
1566 ++ep;
1567 ++epfile;
1568 } while (--count);
1569 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1570
1571 return ret;
1572}
1573
1574
1575/* Parsing and building descriptors and strings *****************************/
1576
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001577/*
1578 * This validates if data pointed by data is a valid USB descriptor as
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001579 * well as record how many interfaces, endpoints and strings are
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001580 * required by given configuration. Returns address after the
1581 * descriptor or NULL if data is invalid.
1582 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001583
1584enum ffs_entity_type {
1585 FFS_DESCRIPTOR, FFS_INTERFACE, FFS_STRING, FFS_ENDPOINT
1586};
1587
1588typedef int (*ffs_entity_callback)(enum ffs_entity_type entity,
1589 u8 *valuep,
1590 struct usb_descriptor_header *desc,
1591 void *priv);
1592
1593static int __must_check ffs_do_desc(char *data, unsigned len,
1594 ffs_entity_callback entity, void *priv)
1595{
1596 struct usb_descriptor_header *_ds = (void *)data;
1597 u8 length;
1598 int ret;
1599
1600 ENTER();
1601
1602 /* At least two bytes are required: length and type */
1603 if (len < 2) {
1604 FVDBG("descriptor too short");
1605 return -EINVAL;
1606 }
1607
1608 /* If we have at least as many bytes as the descriptor takes? */
1609 length = _ds->bLength;
1610 if (len < length) {
1611 FVDBG("descriptor longer then available data");
1612 return -EINVAL;
1613 }
1614
1615#define __entity_check_INTERFACE(val) 1
1616#define __entity_check_STRING(val) (val)
1617#define __entity_check_ENDPOINT(val) ((val) & USB_ENDPOINT_NUMBER_MASK)
1618#define __entity(type, val) do { \
1619 FVDBG("entity " #type "(%02x)", (val)); \
1620 if (unlikely(!__entity_check_ ##type(val))) { \
1621 FVDBG("invalid entity's value"); \
1622 return -EINVAL; \
1623 } \
1624 ret = entity(FFS_ ##type, &val, _ds, priv); \
1625 if (unlikely(ret < 0)) { \
1626 FDBG("entity " #type "(%02x); ret = %d", \
1627 (val), ret); \
1628 return ret; \
1629 } \
1630 } while (0)
1631
1632 /* Parse descriptor depending on type. */
1633 switch (_ds->bDescriptorType) {
1634 case USB_DT_DEVICE:
1635 case USB_DT_CONFIG:
1636 case USB_DT_STRING:
1637 case USB_DT_DEVICE_QUALIFIER:
1638 /* function can't have any of those */
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001639 FVDBG("descriptor reserved for gadget: %d",
1640 _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001641 return -EINVAL;
1642
1643 case USB_DT_INTERFACE: {
1644 struct usb_interface_descriptor *ds = (void *)_ds;
1645 FVDBG("interface descriptor");
1646 if (length != sizeof *ds)
1647 goto inv_length;
1648
1649 __entity(INTERFACE, ds->bInterfaceNumber);
1650 if (ds->iInterface)
1651 __entity(STRING, ds->iInterface);
1652 }
1653 break;
1654
1655 case USB_DT_ENDPOINT: {
1656 struct usb_endpoint_descriptor *ds = (void *)_ds;
1657 FVDBG("endpoint descriptor");
1658 if (length != USB_DT_ENDPOINT_SIZE &&
1659 length != USB_DT_ENDPOINT_AUDIO_SIZE)
1660 goto inv_length;
1661 __entity(ENDPOINT, ds->bEndpointAddress);
1662 }
1663 break;
1664
1665 case USB_DT_OTG:
1666 if (length != sizeof(struct usb_otg_descriptor))
1667 goto inv_length;
1668 break;
1669
1670 case USB_DT_INTERFACE_ASSOCIATION: {
1671 struct usb_interface_assoc_descriptor *ds = (void *)_ds;
1672 FVDBG("interface association descriptor");
1673 if (length != sizeof *ds)
1674 goto inv_length;
1675 if (ds->iFunction)
1676 __entity(STRING, ds->iFunction);
1677 }
1678 break;
1679
1680 case USB_DT_OTHER_SPEED_CONFIG:
1681 case USB_DT_INTERFACE_POWER:
1682 case USB_DT_DEBUG:
1683 case USB_DT_SECURITY:
1684 case USB_DT_CS_RADIO_CONTROL:
1685 /* TODO */
1686 FVDBG("unimplemented descriptor: %d", _ds->bDescriptorType);
1687 return -EINVAL;
1688
1689 default:
1690 /* We should never be here */
1691 FVDBG("unknown descriptor: %d", _ds->bDescriptorType);
1692 return -EINVAL;
1693
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001694inv_length:
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001695 FVDBG("invalid length: %d (descriptor %d)",
1696 _ds->bLength, _ds->bDescriptorType);
1697 return -EINVAL;
1698 }
1699
1700#undef __entity
1701#undef __entity_check_DESCRIPTOR
1702#undef __entity_check_INTERFACE
1703#undef __entity_check_STRING
1704#undef __entity_check_ENDPOINT
1705
1706 return length;
1707}
1708
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001709static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len,
1710 ffs_entity_callback entity, void *priv)
1711{
1712 const unsigned _len = len;
1713 unsigned long num = 0;
1714
1715 ENTER();
1716
1717 for (;;) {
1718 int ret;
1719
1720 if (num == count)
1721 data = NULL;
1722
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001723 /* Record "descriptor" entity */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001724 ret = entity(FFS_DESCRIPTOR, (u8 *)num, (void *)data, priv);
1725 if (unlikely(ret < 0)) {
1726 FDBG("entity DESCRIPTOR(%02lx); ret = %d", num, ret);
1727 return ret;
1728 }
1729
1730 if (!data)
1731 return _len - len;
1732
1733 ret = ffs_do_desc(data, len, entity, priv);
1734 if (unlikely(ret < 0)) {
1735 FDBG("%s returns %d", __func__, ret);
1736 return ret;
1737 }
1738
1739 len -= ret;
1740 data += ret;
1741 ++num;
1742 }
1743}
1744
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001745static int __ffs_data_do_entity(enum ffs_entity_type type,
1746 u8 *valuep, struct usb_descriptor_header *desc,
1747 void *priv)
1748{
1749 struct ffs_data *ffs = priv;
1750
1751 ENTER();
1752
1753 switch (type) {
1754 case FFS_DESCRIPTOR:
1755 break;
1756
1757 case FFS_INTERFACE:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001758 /*
1759 * Interfaces are indexed from zero so if we
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001760 * encountered interface "n" then there are at least
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001761 * "n+1" interfaces.
1762 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001763 if (*valuep >= ffs->interfaces_count)
1764 ffs->interfaces_count = *valuep + 1;
1765 break;
1766
1767 case FFS_STRING:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001768 /*
1769 * Strings are indexed from 1 (0 is magic ;) reserved
1770 * for languages list or some such)
1771 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001772 if (*valuep > ffs->strings_count)
1773 ffs->strings_count = *valuep;
1774 break;
1775
1776 case FFS_ENDPOINT:
1777 /* Endpoints are indexed from 1 as well. */
1778 if ((*valuep & USB_ENDPOINT_NUMBER_MASK) > ffs->eps_count)
1779 ffs->eps_count = (*valuep & USB_ENDPOINT_NUMBER_MASK);
1780 break;
1781 }
1782
1783 return 0;
1784}
1785
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001786static int __ffs_data_got_descs(struct ffs_data *ffs,
1787 char *const _data, size_t len)
1788{
1789 unsigned fs_count, hs_count;
1790 int fs_len, ret = -EINVAL;
1791 char *data = _data;
1792
1793 ENTER();
1794
1795 if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_DESCRIPTORS_MAGIC ||
1796 get_unaligned_le32(data + 4) != len))
1797 goto error;
1798 fs_count = get_unaligned_le32(data + 8);
1799 hs_count = get_unaligned_le32(data + 12);
1800
1801 if (!fs_count && !hs_count)
1802 goto einval;
1803
1804 data += 16;
1805 len -= 16;
1806
1807 if (likely(fs_count)) {
1808 fs_len = ffs_do_descs(fs_count, data, len,
1809 __ffs_data_do_entity, ffs);
1810 if (unlikely(fs_len < 0)) {
1811 ret = fs_len;
1812 goto error;
1813 }
1814
1815 data += fs_len;
1816 len -= fs_len;
1817 } else {
1818 fs_len = 0;
1819 }
1820
1821 if (likely(hs_count)) {
1822 ret = ffs_do_descs(hs_count, data, len,
1823 __ffs_data_do_entity, ffs);
1824 if (unlikely(ret < 0))
1825 goto error;
1826 } else {
1827 ret = 0;
1828 }
1829
1830 if (unlikely(len != ret))
1831 goto einval;
1832
1833 ffs->raw_fs_descs_length = fs_len;
1834 ffs->raw_descs_length = fs_len + ret;
1835 ffs->raw_descs = _data;
1836 ffs->fs_descs_count = fs_count;
1837 ffs->hs_descs_count = hs_count;
1838
1839 return 0;
1840
1841einval:
1842 ret = -EINVAL;
1843error:
1844 kfree(_data);
1845 return ret;
1846}
1847
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001848static int __ffs_data_got_strings(struct ffs_data *ffs,
1849 char *const _data, size_t len)
1850{
1851 u32 str_count, needed_count, lang_count;
1852 struct usb_gadget_strings **stringtabs, *t;
1853 struct usb_string *strings, *s;
1854 const char *data = _data;
1855
1856 ENTER();
1857
1858 if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_STRINGS_MAGIC ||
1859 get_unaligned_le32(data + 4) != len))
1860 goto error;
1861 str_count = get_unaligned_le32(data + 8);
1862 lang_count = get_unaligned_le32(data + 12);
1863
1864 /* if one is zero the other must be zero */
1865 if (unlikely(!str_count != !lang_count))
1866 goto error;
1867
1868 /* Do we have at least as many strings as descriptors need? */
1869 needed_count = ffs->strings_count;
1870 if (unlikely(str_count < needed_count))
1871 goto error;
1872
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001873 /*
1874 * If we don't need any strings just return and free all
1875 * memory.
1876 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001877 if (!needed_count) {
1878 kfree(_data);
1879 return 0;
1880 }
1881
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001882 /* Allocate everything in one chunk so there's less maintenance. */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001883 {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001884 struct {
1885 struct usb_gadget_strings *stringtabs[lang_count + 1];
1886 struct usb_gadget_strings stringtab[lang_count];
1887 struct usb_string strings[lang_count*(needed_count+1)];
1888 } *d;
1889 unsigned i = 0;
1890
1891 d = kmalloc(sizeof *d, GFP_KERNEL);
1892 if (unlikely(!d)) {
1893 kfree(_data);
1894 return -ENOMEM;
1895 }
1896
1897 stringtabs = d->stringtabs;
1898 t = d->stringtab;
1899 i = lang_count;
1900 do {
1901 *stringtabs++ = t++;
1902 } while (--i);
1903 *stringtabs = NULL;
1904
1905 stringtabs = d->stringtabs;
1906 t = d->stringtab;
1907 s = d->strings;
1908 strings = s;
1909 }
1910
1911 /* For each language */
1912 data += 16;
1913 len -= 16;
1914
1915 do { /* lang_count > 0 so we can use do-while */
1916 unsigned needed = needed_count;
1917
1918 if (unlikely(len < 3))
1919 goto error_free;
1920 t->language = get_unaligned_le16(data);
1921 t->strings = s;
1922 ++t;
1923
1924 data += 2;
1925 len -= 2;
1926
1927 /* For each string */
1928 do { /* str_count > 0 so we can use do-while */
1929 size_t length = strnlen(data, len);
1930
1931 if (unlikely(length == len))
1932 goto error_free;
1933
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001934 /*
1935 * User may provide more strings then we need,
1936 * if that's the case we simply ignore the
1937 * rest
1938 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001939 if (likely(needed)) {
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001940 /*
1941 * s->id will be set while adding
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001942 * function to configuration so for
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001943 * now just leave garbage here.
1944 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001945 s->s = data;
1946 --needed;
1947 ++s;
1948 }
1949
1950 data += length + 1;
1951 len -= length + 1;
1952 } while (--str_count);
1953
1954 s->id = 0; /* terminator */
1955 s->s = NULL;
1956 ++s;
1957
1958 } while (--lang_count);
1959
1960 /* Some garbage left? */
1961 if (unlikely(len))
1962 goto error_free;
1963
1964 /* Done! */
1965 ffs->stringtabs = stringtabs;
1966 ffs->raw_strings = _data;
1967
1968 return 0;
1969
1970error_free:
1971 kfree(stringtabs);
1972error:
1973 kfree(_data);
1974 return -EINVAL;
1975}
1976
1977
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001978/* Events handling and management *******************************************/
1979
1980static void __ffs_event_add(struct ffs_data *ffs,
1981 enum usb_functionfs_event_type type)
1982{
1983 enum usb_functionfs_event_type rem_type1, rem_type2 = type;
1984 int neg = 0;
1985
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001986 /*
1987 * Abort any unhandled setup
1988 *
1989 * We do not need to worry about some cmpxchg() changing value
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001990 * of ffs->setup_state without holding the lock because when
1991 * state is FFS_SETUP_PENDING cmpxchg() in several places in
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001992 * the source does nothing.
1993 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001994 if (ffs->setup_state == FFS_SETUP_PENDING)
1995 ffs->setup_state = FFS_SETUP_CANCELED;
1996
1997 switch (type) {
1998 case FUNCTIONFS_RESUME:
1999 rem_type2 = FUNCTIONFS_SUSPEND;
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002000 /* FALL THROUGH */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002001 case FUNCTIONFS_SUSPEND:
2002 case FUNCTIONFS_SETUP:
2003 rem_type1 = type;
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002004 /* Discard all similar events */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002005 break;
2006
2007 case FUNCTIONFS_BIND:
2008 case FUNCTIONFS_UNBIND:
2009 case FUNCTIONFS_DISABLE:
2010 case FUNCTIONFS_ENABLE:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002011 /* Discard everything other then power management. */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002012 rem_type1 = FUNCTIONFS_SUSPEND;
2013 rem_type2 = FUNCTIONFS_RESUME;
2014 neg = 1;
2015 break;
2016
2017 default:
2018 BUG();
2019 }
2020
2021 {
2022 u8 *ev = ffs->ev.types, *out = ev;
2023 unsigned n = ffs->ev.count;
2024 for (; n; --n, ++ev)
2025 if ((*ev == rem_type1 || *ev == rem_type2) == neg)
2026 *out++ = *ev;
2027 else
2028 FVDBG("purging event %d", *ev);
2029 ffs->ev.count = out - ffs->ev.types;
2030 }
2031
2032 FVDBG("adding event %d", type);
2033 ffs->ev.types[ffs->ev.count++] = type;
2034 wake_up_locked(&ffs->ev.waitq);
2035}
2036
2037static void ffs_event_add(struct ffs_data *ffs,
2038 enum usb_functionfs_event_type type)
2039{
2040 unsigned long flags;
2041 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2042 __ffs_event_add(ffs, type);
2043 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2044}
2045
2046
2047/* Bind/unbind USB function hooks *******************************************/
2048
2049static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
2050 struct usb_descriptor_header *desc,
2051 void *priv)
2052{
2053 struct usb_endpoint_descriptor *ds = (void *)desc;
2054 struct ffs_function *func = priv;
2055 struct ffs_ep *ffs_ep;
2056
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002057 /*
2058 * If hs_descriptors is not NULL then we are reading hs
2059 * descriptors now
2060 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002061 const int isHS = func->function.hs_descriptors != NULL;
2062 unsigned idx;
2063
2064 if (type != FFS_DESCRIPTOR)
2065 return 0;
2066
2067 if (isHS)
2068 func->function.hs_descriptors[(long)valuep] = desc;
2069 else
2070 func->function.descriptors[(long)valuep] = desc;
2071
2072 if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT)
2073 return 0;
2074
2075 idx = (ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK) - 1;
2076 ffs_ep = func->eps + idx;
2077
2078 if (unlikely(ffs_ep->descs[isHS])) {
2079 FVDBG("two %sspeed descriptors for EP %d",
2080 isHS ? "high" : "full",
2081 ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
2082 return -EINVAL;
2083 }
2084 ffs_ep->descs[isHS] = ds;
2085
2086 ffs_dump_mem(": Original ep desc", ds, ds->bLength);
2087 if (ffs_ep->ep) {
2088 ds->bEndpointAddress = ffs_ep->descs[0]->bEndpointAddress;
2089 if (!ds->wMaxPacketSize)
2090 ds->wMaxPacketSize = ffs_ep->descs[0]->wMaxPacketSize;
2091 } else {
2092 struct usb_request *req;
2093 struct usb_ep *ep;
2094
2095 FVDBG("autoconfig");
2096 ep = usb_ep_autoconfig(func->gadget, ds);
2097 if (unlikely(!ep))
2098 return -ENOTSUPP;
Joe Perchescc7e6052010-11-14 19:04:49 -08002099 ep->driver_data = func->eps + idx;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002100
2101 req = usb_ep_alloc_request(ep, GFP_KERNEL);
2102 if (unlikely(!req))
2103 return -ENOMEM;
2104
2105 ffs_ep->ep = ep;
2106 ffs_ep->req = req;
2107 func->eps_revmap[ds->bEndpointAddress &
2108 USB_ENDPOINT_NUMBER_MASK] = idx + 1;
2109 }
2110 ffs_dump_mem(": Rewritten ep desc", ds, ds->bLength);
2111
2112 return 0;
2113}
2114
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002115static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep,
2116 struct usb_descriptor_header *desc,
2117 void *priv)
2118{
2119 struct ffs_function *func = priv;
2120 unsigned idx;
2121 u8 newValue;
2122
2123 switch (type) {
2124 default:
2125 case FFS_DESCRIPTOR:
2126 /* Handled in previous pass by __ffs_func_bind_do_descs() */
2127 return 0;
2128
2129 case FFS_INTERFACE:
2130 idx = *valuep;
2131 if (func->interfaces_nums[idx] < 0) {
2132 int id = usb_interface_id(func->conf, &func->function);
2133 if (unlikely(id < 0))
2134 return id;
2135 func->interfaces_nums[idx] = id;
2136 }
2137 newValue = func->interfaces_nums[idx];
2138 break;
2139
2140 case FFS_STRING:
2141 /* String' IDs are allocated when fsf_data is bound to cdev */
2142 newValue = func->ffs->stringtabs[0]->strings[*valuep - 1].id;
2143 break;
2144
2145 case FFS_ENDPOINT:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002146 /*
2147 * USB_DT_ENDPOINT are handled in
2148 * __ffs_func_bind_do_descs().
2149 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002150 if (desc->bDescriptorType == USB_DT_ENDPOINT)
2151 return 0;
2152
2153 idx = (*valuep & USB_ENDPOINT_NUMBER_MASK) - 1;
2154 if (unlikely(!func->eps[idx].ep))
2155 return -EINVAL;
2156
2157 {
2158 struct usb_endpoint_descriptor **descs;
2159 descs = func->eps[idx].descs;
2160 newValue = descs[descs[0] ? 0 : 1]->bEndpointAddress;
2161 }
2162 break;
2163 }
2164
2165 FVDBG("%02x -> %02x", *valuep, newValue);
2166 *valuep = newValue;
2167 return 0;
2168}
2169
2170static int ffs_func_bind(struct usb_configuration *c,
2171 struct usb_function *f)
2172{
2173 struct ffs_function *func = ffs_func_from_usb(f);
2174 struct ffs_data *ffs = func->ffs;
2175
2176 const int full = !!func->ffs->fs_descs_count;
2177 const int high = gadget_is_dualspeed(func->gadget) &&
2178 func->ffs->hs_descs_count;
2179
2180 int ret;
2181
2182 /* Make it a single chunk, less management later on */
2183 struct {
2184 struct ffs_ep eps[ffs->eps_count];
2185 struct usb_descriptor_header
2186 *fs_descs[full ? ffs->fs_descs_count + 1 : 0];
2187 struct usb_descriptor_header
2188 *hs_descs[high ? ffs->hs_descs_count + 1 : 0];
2189 short inums[ffs->interfaces_count];
2190 char raw_descs[high ? ffs->raw_descs_length
2191 : ffs->raw_fs_descs_length];
2192 } *data;
2193
2194 ENTER();
2195
2196 /* Only high speed but not supported by gadget? */
2197 if (unlikely(!(full | high)))
2198 return -ENOTSUPP;
2199
2200 /* Allocate */
2201 data = kmalloc(sizeof *data, GFP_KERNEL);
2202 if (unlikely(!data))
2203 return -ENOMEM;
2204
2205 /* Zero */
2206 memset(data->eps, 0, sizeof data->eps);
2207 memcpy(data->raw_descs, ffs->raw_descs + 16, sizeof data->raw_descs);
2208 memset(data->inums, 0xff, sizeof data->inums);
2209 for (ret = ffs->eps_count; ret; --ret)
2210 data->eps[ret].num = -1;
2211
2212 /* Save pointers */
2213 func->eps = data->eps;
2214 func->interfaces_nums = data->inums;
2215
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002216 /*
2217 * Go through all the endpoint descriptors and allocate
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002218 * endpoints first, so that later we can rewrite the endpoint
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002219 * numbers without worrying that it may be described later on.
2220 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002221 if (likely(full)) {
2222 func->function.descriptors = data->fs_descs;
2223 ret = ffs_do_descs(ffs->fs_descs_count,
2224 data->raw_descs,
2225 sizeof data->raw_descs,
2226 __ffs_func_bind_do_descs, func);
2227 if (unlikely(ret < 0))
2228 goto error;
2229 } else {
2230 ret = 0;
2231 }
2232
2233 if (likely(high)) {
2234 func->function.hs_descriptors = data->hs_descs;
2235 ret = ffs_do_descs(ffs->hs_descs_count,
2236 data->raw_descs + ret,
2237 (sizeof data->raw_descs) - ret,
2238 __ffs_func_bind_do_descs, func);
2239 }
2240
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002241 /*
2242 * Now handle interface numbers allocation and interface and
2243 * endpoint numbers rewriting. We can do that in one go
2244 * now.
2245 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002246 ret = ffs_do_descs(ffs->fs_descs_count +
2247 (high ? ffs->hs_descs_count : 0),
2248 data->raw_descs, sizeof data->raw_descs,
2249 __ffs_func_bind_do_nums, func);
2250 if (unlikely(ret < 0))
2251 goto error;
2252
2253 /* And we're done */
2254 ffs_event_add(ffs, FUNCTIONFS_BIND);
2255 return 0;
2256
2257error:
2258 /* XXX Do we need to release all claimed endpoints here? */
2259 return ret;
2260}
2261
2262
2263/* Other USB function hooks *************************************************/
2264
2265static void ffs_func_unbind(struct usb_configuration *c,
2266 struct usb_function *f)
2267{
2268 struct ffs_function *func = ffs_func_from_usb(f);
2269 struct ffs_data *ffs = func->ffs;
2270
2271 ENTER();
2272
2273 if (ffs->func == func) {
2274 ffs_func_eps_disable(func);
2275 ffs->func = NULL;
2276 }
2277
2278 ffs_event_add(ffs, FUNCTIONFS_UNBIND);
2279
2280 ffs_func_free(func);
2281}
2282
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002283static int ffs_func_set_alt(struct usb_function *f,
2284 unsigned interface, unsigned alt)
2285{
2286 struct ffs_function *func = ffs_func_from_usb(f);
2287 struct ffs_data *ffs = func->ffs;
2288 int ret = 0, intf;
2289
2290 if (alt != (unsigned)-1) {
2291 intf = ffs_func_revmap_intf(func, interface);
2292 if (unlikely(intf < 0))
2293 return intf;
2294 }
2295
2296 if (ffs->func)
2297 ffs_func_eps_disable(ffs->func);
2298
2299 if (ffs->state != FFS_ACTIVE)
2300 return -ENODEV;
2301
2302 if (alt == (unsigned)-1) {
2303 ffs->func = NULL;
2304 ffs_event_add(ffs, FUNCTIONFS_DISABLE);
2305 return 0;
2306 }
2307
2308 ffs->func = func;
2309 ret = ffs_func_eps_enable(func);
2310 if (likely(ret >= 0))
2311 ffs_event_add(ffs, FUNCTIONFS_ENABLE);
2312 return ret;
2313}
2314
2315static void ffs_func_disable(struct usb_function *f)
2316{
2317 ffs_func_set_alt(f, 0, (unsigned)-1);
2318}
2319
2320static int ffs_func_setup(struct usb_function *f,
2321 const struct usb_ctrlrequest *creq)
2322{
2323 struct ffs_function *func = ffs_func_from_usb(f);
2324 struct ffs_data *ffs = func->ffs;
2325 unsigned long flags;
2326 int ret;
2327
2328 ENTER();
2329
2330 FVDBG("creq->bRequestType = %02x", creq->bRequestType);
2331 FVDBG("creq->bRequest = %02x", creq->bRequest);
2332 FVDBG("creq->wValue = %04x", le16_to_cpu(creq->wValue));
2333 FVDBG("creq->wIndex = %04x", le16_to_cpu(creq->wIndex));
2334 FVDBG("creq->wLength = %04x", le16_to_cpu(creq->wLength));
2335
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002336 /*
2337 * Most requests directed to interface go through here
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002338 * (notable exceptions are set/get interface) so we need to
2339 * handle them. All other either handled by composite or
2340 * passed to usb_configuration->setup() (if one is set). No
2341 * matter, we will handle requests directed to endpoint here
2342 * as well (as it's straightforward) but what to do with any
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002343 * other request?
2344 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002345 if (ffs->state != FFS_ACTIVE)
2346 return -ENODEV;
2347
2348 switch (creq->bRequestType & USB_RECIP_MASK) {
2349 case USB_RECIP_INTERFACE:
2350 ret = ffs_func_revmap_intf(func, le16_to_cpu(creq->wIndex));
2351 if (unlikely(ret < 0))
2352 return ret;
2353 break;
2354
2355 case USB_RECIP_ENDPOINT:
2356 ret = ffs_func_revmap_ep(func, le16_to_cpu(creq->wIndex));
2357 if (unlikely(ret < 0))
2358 return ret;
2359 break;
2360
2361 default:
2362 return -EOPNOTSUPP;
2363 }
2364
2365 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2366 ffs->ev.setup = *creq;
2367 ffs->ev.setup.wIndex = cpu_to_le16(ret);
2368 __ffs_event_add(ffs, FUNCTIONFS_SETUP);
2369 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2370
2371 return 0;
2372}
2373
2374static void ffs_func_suspend(struct usb_function *f)
2375{
2376 ENTER();
2377 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_SUSPEND);
2378}
2379
2380static void ffs_func_resume(struct usb_function *f)
2381{
2382 ENTER();
2383 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_RESUME);
2384}
2385
2386
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002387/* Endpoint and interface numbers reverse mapping ***************************/
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002388
2389static int ffs_func_revmap_ep(struct ffs_function *func, u8 num)
2390{
2391 num = func->eps_revmap[num & USB_ENDPOINT_NUMBER_MASK];
2392 return num ? num : -EDOM;
2393}
2394
2395static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf)
2396{
2397 short *nums = func->interfaces_nums;
2398 unsigned count = func->ffs->interfaces_count;
2399
2400 for (; count; --count, ++nums) {
2401 if (*nums >= 0 && *nums == intf)
2402 return nums - func->interfaces_nums;
2403 }
2404
2405 return -EDOM;
2406}
2407
2408
2409/* Misc helper functions ****************************************************/
2410
2411static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
2412{
2413 return nonblock
2414 ? likely(mutex_trylock(mutex)) ? 0 : -EAGAIN
2415 : mutex_lock_interruptible(mutex);
2416}
2417
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002418static char *ffs_prepare_buffer(const char * __user buf, size_t len)
2419{
2420 char *data;
2421
2422 if (unlikely(!len))
2423 return NULL;
2424
2425 data = kmalloc(len, GFP_KERNEL);
2426 if (unlikely(!data))
2427 return ERR_PTR(-ENOMEM);
2428
2429 if (unlikely(__copy_from_user(data, buf, len))) {
2430 kfree(data);
2431 return ERR_PTR(-EFAULT);
2432 }
2433
2434 FVDBG("Buffer from user space:");
2435 ffs_dump_mem("", data, len);
2436
2437 return data;
2438}