blob: 1499f9e4afa83a70392efb794962c9577a5e5a1c [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>
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020033
34#include <linux/usb/composite.h>
35#include <linux/usb/functionfs.h>
36
37
38#define FUNCTIONFS_MAGIC 0xa647361 /* Chosen by a honest dice roll ;) */
39
40
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010041/* Debugging ****************************************************************/
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020042
43#ifdef VERBOSE_DEBUG
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +010044# define pr_vdebug pr_debug
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020045# define ffs_dump_mem(prefix, ptr, len) \
Michal Nazarewiczaa02f172010-11-17 17:09:47 +010046 print_hex_dump_bytes(pr_fmt(prefix ": "), DUMP_PREFIX_NONE, ptr, len)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020047#else
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +010048# define pr_vdebug(...) do { } while (0)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020049# define ffs_dump_mem(prefix, ptr, len) do { } while (0)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020050#endif /* VERBOSE_DEBUG */
51
Michal Nazarewiczaa02f172010-11-17 17:09:47 +010052#define ENTER() pr_vdebug("%s()\n", __func__)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020053
54
55/* The data structure and setup file ****************************************/
56
57enum ffs_state {
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010058 /*
59 * Waiting for descriptors and strings.
60 *
61 * In this state no open(2), read(2) or write(2) on epfiles
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020062 * may succeed (which should not be the problem as there
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010063 * should be no such files opened in the first place).
64 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020065 FFS_READ_DESCRIPTORS,
66 FFS_READ_STRINGS,
67
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010068 /*
69 * We've got descriptors and strings. We are or have called
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020070 * functionfs_ready_callback(). functionfs_bind() may have
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010071 * been called but we don't know.
72 *
73 * This is the only state in which operations on epfiles may
74 * succeed.
75 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020076 FFS_ACTIVE,
77
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010078 /*
79 * All endpoints have been closed. This state is also set if
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020080 * we encounter an unrecoverable error. The only
81 * unrecoverable error is situation when after reading strings
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010082 * from user space we fail to initialise epfiles or
83 * functionfs_ready_callback() returns with error (<0).
84 *
85 * In this state no open(2), read(2) or write(2) (both on ep0
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020086 * as well as epfile) may succeed (at this point epfiles are
87 * unlinked and all closed so this is not a problem; ep0 is
88 * also closed but ep0 file exists and so open(2) on ep0 must
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010089 * fail).
90 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020091 FFS_CLOSING
92};
93
94
95enum ffs_setup_state {
96 /* There is no setup request pending. */
97 FFS_NO_SETUP,
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010098 /*
99 * User has read events and there was a setup request event
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200100 * there. The next read/write on ep0 will handle the
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100101 * request.
102 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200103 FFS_SETUP_PENDING,
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100104 /*
105 * There was event pending but before user space handled it
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200106 * some other event was introduced which canceled existing
107 * setup. If this state is set read/write on ep0 return
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100108 * -EIDRM. This state is only set when adding event.
109 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200110 FFS_SETUP_CANCELED
111};
112
113
114
115struct ffs_epfile;
116struct ffs_function;
117
118struct ffs_data {
119 struct usb_gadget *gadget;
120
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100121 /*
122 * Protect access read/write operations, only one read/write
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200123 * at a time. As a consequence protects ep0req and company.
124 * While setup request is being processed (queued) this is
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100125 * held.
126 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200127 struct mutex mutex;
128
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100129 /*
130 * Protect access to endpoint related structures (basically
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200131 * usb_ep_queue(), usb_ep_dequeue(), etc. calls) except for
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100132 * endpoint zero.
133 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200134 spinlock_t eps_lock;
135
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100136 /*
137 * XXX REVISIT do we need our own request? Since we are not
138 * handling setup requests immediately user space may be so
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200139 * slow that another setup will be sent to the gadget but this
140 * time not to us but another function and then there could be
Linus Torvaldsa4ce96a2010-07-21 09:25:42 -0700141 * a race. Is that the case? Or maybe we can use cdev->req
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100142 * after all, maybe we just need some spinlock for that?
143 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200144 struct usb_request *ep0req; /* P: mutex */
145 struct completion ep0req_completion; /* P: mutex */
146 int ep0req_status; /* P: mutex */
147
148 /* reference counter */
149 atomic_t ref;
150 /* how many files are opened (EP0 and others) */
151 atomic_t opened;
152
153 /* EP0 state */
154 enum ffs_state state;
155
156 /*
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100157 * Possible transitions:
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200158 * + FFS_NO_SETUP -> FFS_SETUP_PENDING -- P: ev.waitq.lock
159 * happens only in ep0 read which is P: mutex
160 * + FFS_SETUP_PENDING -> FFS_NO_SETUP -- P: ev.waitq.lock
161 * happens only in ep0 i/o which is P: mutex
162 * + FFS_SETUP_PENDING -> FFS_SETUP_CANCELED -- P: ev.waitq.lock
163 * + FFS_SETUP_CANCELED -> FFS_NO_SETUP -- cmpxchg
164 */
165 enum ffs_setup_state setup_state;
166
167#define FFS_SETUP_STATE(ffs) \
168 ((enum ffs_setup_state)cmpxchg(&(ffs)->setup_state, \
169 FFS_SETUP_CANCELED, FFS_NO_SETUP))
170
171 /* Events & such. */
172 struct {
173 u8 types[4];
174 unsigned short count;
175 /* XXX REVISIT need to update it in some places, or do we? */
176 unsigned short can_stall;
177 struct usb_ctrlrequest setup;
178
179 wait_queue_head_t waitq;
180 } ev; /* the whole structure, P: ev.waitq.lock */
181
182 /* Flags */
183 unsigned long flags;
184#define FFS_FL_CALL_CLOSED_CALLBACK 0
185#define FFS_FL_BOUND 1
186
187 /* Active function */
188 struct ffs_function *func;
189
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100190 /*
191 * Device name, write once when file system is mounted.
192 * Intended for user to read if she wants.
193 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200194 const char *dev_name;
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100195 /* Private data for our user (ie. gadget). Managed by user. */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200196 void *private_data;
197
198 /* filled by __ffs_data_got_descs() */
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100199 /*
200 * Real descriptors are 16 bytes after raw_descs (so you need
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200201 * to skip 16 bytes (ie. ffs->raw_descs + 16) to get to the
202 * first full speed descriptor). raw_descs_length and
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100203 * raw_fs_descs_length do not have those 16 bytes added.
204 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200205 const void *raw_descs;
206 unsigned raw_descs_length;
207 unsigned raw_fs_descs_length;
208 unsigned fs_descs_count;
209 unsigned hs_descs_count;
210
211 unsigned short strings_count;
212 unsigned short interfaces_count;
213 unsigned short eps_count;
214 unsigned short _pad1;
215
216 /* filled by __ffs_data_got_strings() */
217 /* ids in stringtabs are set in functionfs_bind() */
218 const void *raw_strings;
219 struct usb_gadget_strings **stringtabs;
220
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100221 /*
222 * File system's super block, write once when file system is
223 * mounted.
224 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200225 struct super_block *sb;
226
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100227 /* File permissions, written once when fs is mounted */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200228 struct ffs_file_perms {
229 umode_t mode;
230 uid_t uid;
231 gid_t gid;
232 } file_perms;
233
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100234 /*
235 * The endpoint files, filled by ffs_epfiles_create(),
236 * destroyed by ffs_epfiles_destroy().
237 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200238 struct ffs_epfile *epfiles;
239};
240
241/* Reference counter handling */
242static void ffs_data_get(struct ffs_data *ffs);
243static void ffs_data_put(struct ffs_data *ffs);
244/* Creates new ffs_data object. */
245static struct ffs_data *__must_check ffs_data_new(void) __attribute__((malloc));
246
247/* Opened counter handling. */
248static void ffs_data_opened(struct ffs_data *ffs);
249static void ffs_data_closed(struct ffs_data *ffs);
250
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100251/* Called with ffs->mutex held; take over ownership of data. */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200252static int __must_check
253__ffs_data_got_descs(struct ffs_data *ffs, char *data, size_t len);
254static int __must_check
255__ffs_data_got_strings(struct ffs_data *ffs, char *data, size_t len);
256
257
258/* The function structure ***************************************************/
259
260struct ffs_ep;
261
262struct ffs_function {
263 struct usb_configuration *conf;
264 struct usb_gadget *gadget;
265 struct ffs_data *ffs;
266
267 struct ffs_ep *eps;
268 u8 eps_revmap[16];
269 short *interfaces_nums;
270
271 struct usb_function function;
272};
273
274
275static struct ffs_function *ffs_func_from_usb(struct usb_function *f)
276{
277 return container_of(f, struct ffs_function, function);
278}
279
280static void ffs_func_free(struct ffs_function *func);
281
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200282static void ffs_func_eps_disable(struct ffs_function *func);
283static int __must_check ffs_func_eps_enable(struct ffs_function *func);
284
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200285static int ffs_func_bind(struct usb_configuration *,
286 struct usb_function *);
287static void ffs_func_unbind(struct usb_configuration *,
288 struct usb_function *);
289static int ffs_func_set_alt(struct usb_function *, unsigned, unsigned);
290static void ffs_func_disable(struct usb_function *);
291static int ffs_func_setup(struct usb_function *,
292 const struct usb_ctrlrequest *);
293static void ffs_func_suspend(struct usb_function *);
294static void ffs_func_resume(struct usb_function *);
295
296
297static int ffs_func_revmap_ep(struct ffs_function *func, u8 num);
298static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf);
299
300
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200301/* The endpoints structures *************************************************/
302
303struct ffs_ep {
304 struct usb_ep *ep; /* P: ffs->eps_lock */
305 struct usb_request *req; /* P: epfile->mutex */
306
307 /* [0]: full speed, [1]: high speed */
308 struct usb_endpoint_descriptor *descs[2];
309
310 u8 num;
311
312 int status; /* P: epfile->mutex */
313};
314
315struct ffs_epfile {
316 /* Protects ep->ep and ep->req. */
317 struct mutex mutex;
318 wait_queue_head_t wait;
319
320 struct ffs_data *ffs;
321 struct ffs_ep *ep; /* P: ffs->eps_lock */
322
323 struct dentry *dentry;
324
325 char name[5];
326
327 unsigned char in; /* P: ffs->eps_lock */
328 unsigned char isoc; /* P: ffs->eps_lock */
329
330 unsigned char _pad;
331};
332
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200333static int __must_check ffs_epfiles_create(struct ffs_data *ffs);
334static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count);
335
336static struct inode *__must_check
337ffs_sb_create_file(struct super_block *sb, const char *name, void *data,
338 const struct file_operations *fops,
339 struct dentry **dentry_p);
340
341
342/* Misc helper functions ****************************************************/
343
344static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
345 __attribute__((warn_unused_result, nonnull));
346static char *ffs_prepare_buffer(const char * __user buf, size_t len)
347 __attribute__((warn_unused_result, nonnull));
348
349
350/* Control file aka ep0 *****************************************************/
351
352static void ffs_ep0_complete(struct usb_ep *ep, struct usb_request *req)
353{
354 struct ffs_data *ffs = req->context;
355
356 complete_all(&ffs->ep0req_completion);
357}
358
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200359static int __ffs_ep0_queue_wait(struct ffs_data *ffs, char *data, size_t len)
360{
361 struct usb_request *req = ffs->ep0req;
362 int ret;
363
364 req->zero = len < le16_to_cpu(ffs->ev.setup.wLength);
365
366 spin_unlock_irq(&ffs->ev.waitq.lock);
367
368 req->buf = data;
369 req->length = len;
370
371 INIT_COMPLETION(ffs->ep0req_completion);
372
373 ret = usb_ep_queue(ffs->gadget->ep0, req, GFP_ATOMIC);
374 if (unlikely(ret < 0))
375 return ret;
376
377 ret = wait_for_completion_interruptible(&ffs->ep0req_completion);
378 if (unlikely(ret)) {
379 usb_ep_dequeue(ffs->gadget->ep0, req);
380 return -EINTR;
381 }
382
383 ffs->setup_state = FFS_NO_SETUP;
384 return ffs->ep0req_status;
385}
386
387static int __ffs_ep0_stall(struct ffs_data *ffs)
388{
389 if (ffs->ev.can_stall) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100390 pr_vdebug("ep0 stall\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200391 usb_ep_set_halt(ffs->gadget->ep0);
392 ffs->setup_state = FFS_NO_SETUP;
393 return -EL2HLT;
394 } else {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100395 pr_debug("bogus ep0 stall!\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200396 return -ESRCH;
397 }
398}
399
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200400static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
401 size_t len, loff_t *ptr)
402{
403 struct ffs_data *ffs = file->private_data;
404 ssize_t ret;
405 char *data;
406
407 ENTER();
408
409 /* Fast check if setup was canceled */
410 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED)
411 return -EIDRM;
412
413 /* Acquire mutex */
414 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
415 if (unlikely(ret < 0))
416 return ret;
417
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200418 /* Check state */
419 switch (ffs->state) {
420 case FFS_READ_DESCRIPTORS:
421 case FFS_READ_STRINGS:
422 /* Copy data */
423 if (unlikely(len < 16)) {
424 ret = -EINVAL;
425 break;
426 }
427
428 data = ffs_prepare_buffer(buf, len);
Tobias Klauser537baab2010-12-09 15:52:39 +0100429 if (IS_ERR(data)) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200430 ret = PTR_ERR(data);
431 break;
432 }
433
434 /* Handle data */
435 if (ffs->state == FFS_READ_DESCRIPTORS) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100436 pr_info("read descriptors\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200437 ret = __ffs_data_got_descs(ffs, data, len);
438 if (unlikely(ret < 0))
439 break;
440
441 ffs->state = FFS_READ_STRINGS;
442 ret = len;
443 } else {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100444 pr_info("read strings\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200445 ret = __ffs_data_got_strings(ffs, data, len);
446 if (unlikely(ret < 0))
447 break;
448
449 ret = ffs_epfiles_create(ffs);
450 if (unlikely(ret)) {
451 ffs->state = FFS_CLOSING;
452 break;
453 }
454
455 ffs->state = FFS_ACTIVE;
456 mutex_unlock(&ffs->mutex);
457
458 ret = functionfs_ready_callback(ffs);
459 if (unlikely(ret < 0)) {
460 ffs->state = FFS_CLOSING;
461 return ret;
462 }
463
464 set_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags);
465 return len;
466 }
467 break;
468
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200469 case FFS_ACTIVE:
470 data = NULL;
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100471 /*
472 * We're called from user space, we can use _irq
473 * rather then _irqsave
474 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200475 spin_lock_irq(&ffs->ev.waitq.lock);
476 switch (FFS_SETUP_STATE(ffs)) {
477 case FFS_SETUP_CANCELED:
478 ret = -EIDRM;
479 goto done_spin;
480
481 case FFS_NO_SETUP:
482 ret = -ESRCH;
483 goto done_spin;
484
485 case FFS_SETUP_PENDING:
486 break;
487 }
488
489 /* FFS_SETUP_PENDING */
490 if (!(ffs->ev.setup.bRequestType & USB_DIR_IN)) {
491 spin_unlock_irq(&ffs->ev.waitq.lock);
492 ret = __ffs_ep0_stall(ffs);
493 break;
494 }
495
496 /* FFS_SETUP_PENDING and not stall */
497 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
498
499 spin_unlock_irq(&ffs->ev.waitq.lock);
500
501 data = ffs_prepare_buffer(buf, len);
Tobias Klauser537baab2010-12-09 15:52:39 +0100502 if (IS_ERR(data)) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200503 ret = PTR_ERR(data);
504 break;
505 }
506
507 spin_lock_irq(&ffs->ev.waitq.lock);
508
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100509 /*
510 * We are guaranteed to be still in FFS_ACTIVE state
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200511 * but the state of setup could have changed from
512 * FFS_SETUP_PENDING to FFS_SETUP_CANCELED so we need
513 * to check for that. If that happened we copied data
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100514 * from user space in vain but it's unlikely.
515 *
516 * For sure we are not in FFS_NO_SETUP since this is
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200517 * the only place FFS_SETUP_PENDING -> FFS_NO_SETUP
518 * transition can be performed and it's protected by
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100519 * mutex.
520 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200521 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) {
522 ret = -EIDRM;
523done_spin:
524 spin_unlock_irq(&ffs->ev.waitq.lock);
525 } else {
526 /* unlocks spinlock */
527 ret = __ffs_ep0_queue_wait(ffs, data, len);
528 }
529 kfree(data);
530 break;
531
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200532 default:
533 ret = -EBADFD;
534 break;
535 }
536
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200537 mutex_unlock(&ffs->mutex);
538 return ret;
539}
540
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200541static ssize_t __ffs_ep0_read_events(struct ffs_data *ffs, char __user *buf,
542 size_t n)
543{
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100544 /*
545 * We are holding ffs->ev.waitq.lock and ffs->mutex and we need
546 * to release them.
547 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200548 struct usb_functionfs_event events[n];
549 unsigned i = 0;
550
551 memset(events, 0, sizeof events);
552
553 do {
554 events[i].type = ffs->ev.types[i];
555 if (events[i].type == FUNCTIONFS_SETUP) {
556 events[i].u.setup = ffs->ev.setup;
557 ffs->setup_state = FFS_SETUP_PENDING;
558 }
559 } while (++i < n);
560
561 if (n < ffs->ev.count) {
562 ffs->ev.count -= n;
563 memmove(ffs->ev.types, ffs->ev.types + n,
564 ffs->ev.count * sizeof *ffs->ev.types);
565 } else {
566 ffs->ev.count = 0;
567 }
568
569 spin_unlock_irq(&ffs->ev.waitq.lock);
570 mutex_unlock(&ffs->mutex);
571
572 return unlikely(__copy_to_user(buf, events, sizeof events))
573 ? -EFAULT : sizeof events;
574}
575
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200576static ssize_t ffs_ep0_read(struct file *file, char __user *buf,
577 size_t len, loff_t *ptr)
578{
579 struct ffs_data *ffs = file->private_data;
580 char *data = NULL;
581 size_t n;
582 int ret;
583
584 ENTER();
585
586 /* Fast check if setup was canceled */
587 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED)
588 return -EIDRM;
589
590 /* Acquire mutex */
591 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
592 if (unlikely(ret < 0))
593 return ret;
594
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200595 /* Check state */
596 if (ffs->state != FFS_ACTIVE) {
597 ret = -EBADFD;
598 goto done_mutex;
599 }
600
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100601 /*
602 * We're called from user space, we can use _irq rather then
603 * _irqsave
604 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200605 spin_lock_irq(&ffs->ev.waitq.lock);
606
607 switch (FFS_SETUP_STATE(ffs)) {
608 case FFS_SETUP_CANCELED:
609 ret = -EIDRM;
610 break;
611
612 case FFS_NO_SETUP:
613 n = len / sizeof(struct usb_functionfs_event);
614 if (unlikely(!n)) {
615 ret = -EINVAL;
616 break;
617 }
618
619 if ((file->f_flags & O_NONBLOCK) && !ffs->ev.count) {
620 ret = -EAGAIN;
621 break;
622 }
623
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100624 if (wait_event_interruptible_exclusive_locked_irq(ffs->ev.waitq,
625 ffs->ev.count)) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200626 ret = -EINTR;
627 break;
628 }
629
630 return __ffs_ep0_read_events(ffs, buf,
631 min(n, (size_t)ffs->ev.count));
632
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200633 case FFS_SETUP_PENDING:
634 if (ffs->ev.setup.bRequestType & USB_DIR_IN) {
635 spin_unlock_irq(&ffs->ev.waitq.lock);
636 ret = __ffs_ep0_stall(ffs);
637 goto done_mutex;
638 }
639
640 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
641
642 spin_unlock_irq(&ffs->ev.waitq.lock);
643
644 if (likely(len)) {
645 data = kmalloc(len, GFP_KERNEL);
646 if (unlikely(!data)) {
647 ret = -ENOMEM;
648 goto done_mutex;
649 }
650 }
651
652 spin_lock_irq(&ffs->ev.waitq.lock);
653
654 /* See ffs_ep0_write() */
655 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) {
656 ret = -EIDRM;
657 break;
658 }
659
660 /* unlocks spinlock */
661 ret = __ffs_ep0_queue_wait(ffs, data, len);
662 if (likely(ret > 0) && unlikely(__copy_to_user(buf, data, len)))
663 ret = -EFAULT;
664 goto done_mutex;
665
666 default:
667 ret = -EBADFD;
668 break;
669 }
670
671 spin_unlock_irq(&ffs->ev.waitq.lock);
672done_mutex:
673 mutex_unlock(&ffs->mutex);
674 kfree(data);
675 return ret;
676}
677
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200678static int ffs_ep0_open(struct inode *inode, struct file *file)
679{
680 struct ffs_data *ffs = inode->i_private;
681
682 ENTER();
683
684 if (unlikely(ffs->state == FFS_CLOSING))
685 return -EBUSY;
686
687 file->private_data = ffs;
688 ffs_data_opened(ffs);
689
690 return 0;
691}
692
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200693static int ffs_ep0_release(struct inode *inode, struct file *file)
694{
695 struct ffs_data *ffs = file->private_data;
696
697 ENTER();
698
699 ffs_data_closed(ffs);
700
701 return 0;
702}
703
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200704static long ffs_ep0_ioctl(struct file *file, unsigned code, unsigned long value)
705{
706 struct ffs_data *ffs = file->private_data;
707 struct usb_gadget *gadget = ffs->gadget;
708 long ret;
709
710 ENTER();
711
712 if (code == FUNCTIONFS_INTERFACE_REVMAP) {
713 struct ffs_function *func = ffs->func;
714 ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV;
715 } else if (gadget->ops->ioctl) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200716 ret = gadget->ops->ioctl(gadget, code, value);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200717 } else {
718 ret = -ENOTTY;
719 }
720
721 return ret;
722}
723
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200724static const struct file_operations ffs_ep0_operations = {
725 .owner = THIS_MODULE,
726 .llseek = no_llseek,
727
728 .open = ffs_ep0_open,
729 .write = ffs_ep0_write,
730 .read = ffs_ep0_read,
731 .release = ffs_ep0_release,
732 .unlocked_ioctl = ffs_ep0_ioctl,
733};
734
735
736/* "Normal" endpoints operations ********************************************/
737
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200738static void ffs_epfile_io_complete(struct usb_ep *_ep, struct usb_request *req)
739{
740 ENTER();
741 if (likely(req->context)) {
742 struct ffs_ep *ep = _ep->driver_data;
743 ep->status = req->status ? req->status : req->actual;
744 complete(req->context);
745 }
746}
747
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200748static ssize_t ffs_epfile_io(struct file *file,
749 char __user *buf, size_t len, int read)
750{
751 struct ffs_epfile *epfile = file->private_data;
752 struct ffs_ep *ep;
753 char *data = NULL;
754 ssize_t ret;
755 int halt;
756
757 goto first_try;
758 do {
759 spin_unlock_irq(&epfile->ffs->eps_lock);
760 mutex_unlock(&epfile->mutex);
761
762first_try:
763 /* Are we still active? */
764 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE)) {
765 ret = -ENODEV;
766 goto error;
767 }
768
769 /* Wait for endpoint to be enabled */
770 ep = epfile->ep;
771 if (!ep) {
772 if (file->f_flags & O_NONBLOCK) {
773 ret = -EAGAIN;
774 goto error;
775 }
776
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100777 if (wait_event_interruptible(epfile->wait,
778 (ep = epfile->ep))) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200779 ret = -EINTR;
780 goto error;
781 }
782 }
783
784 /* Do we halt? */
785 halt = !read == !epfile->in;
786 if (halt && epfile->isoc) {
787 ret = -EINVAL;
788 goto error;
789 }
790
791 /* Allocate & copy */
792 if (!halt && !data) {
793 data = kzalloc(len, GFP_KERNEL);
794 if (unlikely(!data))
795 return -ENOMEM;
796
797 if (!read &&
798 unlikely(__copy_from_user(data, buf, len))) {
799 ret = -EFAULT;
800 goto error;
801 }
802 }
803
804 /* We will be using request */
805 ret = ffs_mutex_lock(&epfile->mutex,
806 file->f_flags & O_NONBLOCK);
807 if (unlikely(ret))
808 goto error;
809
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100810 /*
811 * We're called from user space, we can use _irq rather then
812 * _irqsave
813 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200814 spin_lock_irq(&epfile->ffs->eps_lock);
815
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100816 /*
817 * While we were acquiring mutex endpoint got disabled
818 * or changed?
819 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200820 } while (unlikely(epfile->ep != ep));
821
822 /* Halt */
823 if (unlikely(halt)) {
824 if (likely(epfile->ep == ep) && !WARN_ON(!ep->ep))
825 usb_ep_set_halt(ep->ep);
826 spin_unlock_irq(&epfile->ffs->eps_lock);
827 ret = -EBADMSG;
828 } else {
829 /* Fire the request */
830 DECLARE_COMPLETION_ONSTACK(done);
831
832 struct usb_request *req = ep->req;
833 req->context = &done;
834 req->complete = ffs_epfile_io_complete;
835 req->buf = data;
836 req->length = len;
837
838 ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
839
840 spin_unlock_irq(&epfile->ffs->eps_lock);
841
842 if (unlikely(ret < 0)) {
843 /* nop */
844 } else if (unlikely(wait_for_completion_interruptible(&done))) {
845 ret = -EINTR;
846 usb_ep_dequeue(ep->ep, req);
847 } else {
848 ret = ep->status;
849 if (read && ret > 0 &&
850 unlikely(copy_to_user(buf, data, ret)))
851 ret = -EFAULT;
852 }
853 }
854
855 mutex_unlock(&epfile->mutex);
856error:
857 kfree(data);
858 return ret;
859}
860
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200861static ssize_t
862ffs_epfile_write(struct file *file, const char __user *buf, size_t len,
863 loff_t *ptr)
864{
865 ENTER();
866
867 return ffs_epfile_io(file, (char __user *)buf, len, 0);
868}
869
870static ssize_t
871ffs_epfile_read(struct file *file, char __user *buf, size_t len, loff_t *ptr)
872{
873 ENTER();
874
875 return ffs_epfile_io(file, buf, len, 1);
876}
877
878static int
879ffs_epfile_open(struct inode *inode, struct file *file)
880{
881 struct ffs_epfile *epfile = inode->i_private;
882
883 ENTER();
884
885 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
886 return -ENODEV;
887
888 file->private_data = epfile;
889 ffs_data_opened(epfile->ffs);
890
891 return 0;
892}
893
894static int
895ffs_epfile_release(struct inode *inode, struct file *file)
896{
897 struct ffs_epfile *epfile = inode->i_private;
898
899 ENTER();
900
901 ffs_data_closed(epfile->ffs);
902
903 return 0;
904}
905
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200906static long ffs_epfile_ioctl(struct file *file, unsigned code,
907 unsigned long value)
908{
909 struct ffs_epfile *epfile = file->private_data;
910 int ret;
911
912 ENTER();
913
914 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
915 return -ENODEV;
916
917 spin_lock_irq(&epfile->ffs->eps_lock);
918 if (likely(epfile->ep)) {
919 switch (code) {
920 case FUNCTIONFS_FIFO_STATUS:
921 ret = usb_ep_fifo_status(epfile->ep->ep);
922 break;
923 case FUNCTIONFS_FIFO_FLUSH:
924 usb_ep_fifo_flush(epfile->ep->ep);
925 ret = 0;
926 break;
927 case FUNCTIONFS_CLEAR_HALT:
928 ret = usb_ep_clear_halt(epfile->ep->ep);
929 break;
930 case FUNCTIONFS_ENDPOINT_REVMAP:
931 ret = epfile->ep->num;
932 break;
933 default:
934 ret = -ENOTTY;
935 }
936 } else {
937 ret = -ENODEV;
938 }
939 spin_unlock_irq(&epfile->ffs->eps_lock);
940
941 return ret;
942}
943
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200944static const struct file_operations ffs_epfile_operations = {
945 .owner = THIS_MODULE,
946 .llseek = no_llseek,
947
948 .open = ffs_epfile_open,
949 .write = ffs_epfile_write,
950 .read = ffs_epfile_read,
951 .release = ffs_epfile_release,
952 .unlocked_ioctl = ffs_epfile_ioctl,
953};
954
955
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200956/* File system and super block operations ***********************************/
957
958/*
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100959 * Mounting the file system creates a controller file, used first for
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200960 * function configuration then later for event monitoring.
961 */
962
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200963static struct inode *__must_check
964ffs_sb_make_inode(struct super_block *sb, void *data,
965 const struct file_operations *fops,
966 const struct inode_operations *iops,
967 struct ffs_file_perms *perms)
968{
969 struct inode *inode;
970
971 ENTER();
972
973 inode = new_inode(sb);
974
975 if (likely(inode)) {
976 struct timespec current_time = CURRENT_TIME;
977
Al Viro12ba8d12010-10-27 04:19:36 +0100978 inode->i_ino = get_next_ino();
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200979 inode->i_mode = perms->mode;
980 inode->i_uid = perms->uid;
981 inode->i_gid = perms->gid;
982 inode->i_atime = current_time;
983 inode->i_mtime = current_time;
984 inode->i_ctime = current_time;
985 inode->i_private = data;
986 if (fops)
987 inode->i_fop = fops;
988 if (iops)
989 inode->i_op = iops;
990 }
991
992 return inode;
993}
994
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200995/* Create "regular" file */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200996static struct inode *ffs_sb_create_file(struct super_block *sb,
997 const char *name, void *data,
998 const struct file_operations *fops,
999 struct dentry **dentry_p)
1000{
1001 struct ffs_data *ffs = sb->s_fs_info;
1002 struct dentry *dentry;
1003 struct inode *inode;
1004
1005 ENTER();
1006
1007 dentry = d_alloc_name(sb->s_root, name);
1008 if (unlikely(!dentry))
1009 return NULL;
1010
1011 inode = ffs_sb_make_inode(sb, data, fops, NULL, &ffs->file_perms);
1012 if (unlikely(!inode)) {
1013 dput(dentry);
1014 return NULL;
1015 }
1016
1017 d_add(dentry, inode);
1018 if (dentry_p)
1019 *dentry_p = dentry;
1020
1021 return inode;
1022}
1023
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001024/* Super block */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001025static const struct super_operations ffs_sb_operations = {
1026 .statfs = simple_statfs,
1027 .drop_inode = generic_delete_inode,
1028};
1029
1030struct ffs_sb_fill_data {
1031 struct ffs_file_perms perms;
1032 umode_t root_mode;
1033 const char *dev_name;
1034};
1035
1036static int ffs_sb_fill(struct super_block *sb, void *_data, int silent)
1037{
1038 struct ffs_sb_fill_data *data = _data;
1039 struct inode *inode;
1040 struct dentry *d;
1041 struct ffs_data *ffs;
1042
1043 ENTER();
1044
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001045 /* Initialise data */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001046 ffs = ffs_data_new();
1047 if (unlikely(!ffs))
1048 goto enomem0;
1049
1050 ffs->sb = sb;
1051 ffs->dev_name = data->dev_name;
1052 ffs->file_perms = data->perms;
1053
1054 sb->s_fs_info = ffs;
1055 sb->s_blocksize = PAGE_CACHE_SIZE;
1056 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1057 sb->s_magic = FUNCTIONFS_MAGIC;
1058 sb->s_op = &ffs_sb_operations;
1059 sb->s_time_gran = 1;
1060
1061 /* Root inode */
1062 data->perms.mode = data->root_mode;
1063 inode = ffs_sb_make_inode(sb, NULL,
1064 &simple_dir_operations,
1065 &simple_dir_inode_operations,
1066 &data->perms);
1067 if (unlikely(!inode))
1068 goto enomem1;
1069 d = d_alloc_root(inode);
1070 if (unlikely(!d))
1071 goto enomem2;
1072 sb->s_root = d;
1073
1074 /* EP0 file */
1075 if (unlikely(!ffs_sb_create_file(sb, "ep0", ffs,
1076 &ffs_ep0_operations, NULL)))
1077 goto enomem3;
1078
1079 return 0;
1080
1081enomem3:
1082 dput(d);
1083enomem2:
1084 iput(inode);
1085enomem1:
1086 ffs_data_put(ffs);
1087enomem0:
1088 return -ENOMEM;
1089}
1090
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001091static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts)
1092{
1093 ENTER();
1094
1095 if (!opts || !*opts)
1096 return 0;
1097
1098 for (;;) {
1099 char *end, *eq, *comma;
1100 unsigned long value;
1101
1102 /* Option limit */
1103 comma = strchr(opts, ',');
1104 if (comma)
1105 *comma = 0;
1106
1107 /* Value limit */
1108 eq = strchr(opts, '=');
1109 if (unlikely(!eq)) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001110 pr_err("'=' missing in %s\n", opts);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001111 return -EINVAL;
1112 }
1113 *eq = 0;
1114
1115 /* Parse value */
1116 value = simple_strtoul(eq + 1, &end, 0);
1117 if (unlikely(*end != ',' && *end != 0)) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001118 pr_err("%s: invalid value: %s\n", opts, eq + 1);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001119 return -EINVAL;
1120 }
1121
1122 /* Interpret option */
1123 switch (eq - opts) {
1124 case 5:
1125 if (!memcmp(opts, "rmode", 5))
1126 data->root_mode = (value & 0555) | S_IFDIR;
1127 else if (!memcmp(opts, "fmode", 5))
1128 data->perms.mode = (value & 0666) | S_IFREG;
1129 else
1130 goto invalid;
1131 break;
1132
1133 case 4:
1134 if (!memcmp(opts, "mode", 4)) {
1135 data->root_mode = (value & 0555) | S_IFDIR;
1136 data->perms.mode = (value & 0666) | S_IFREG;
1137 } else {
1138 goto invalid;
1139 }
1140 break;
1141
1142 case 3:
1143 if (!memcmp(opts, "uid", 3))
1144 data->perms.uid = value;
1145 else if (!memcmp(opts, "gid", 3))
1146 data->perms.gid = value;
1147 else
1148 goto invalid;
1149 break;
1150
1151 default:
1152invalid:
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001153 pr_err("%s: invalid option\n", opts);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001154 return -EINVAL;
1155 }
1156
1157 /* Next iteration */
1158 if (!comma)
1159 break;
1160 opts = comma + 1;
1161 }
1162
1163 return 0;
1164}
1165
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001166/* "mount -t functionfs dev_name /dev/function" ends up here */
1167
Al Virofc14f2f2010-07-25 01:48:30 +04001168static struct dentry *
1169ffs_fs_mount(struct file_system_type *t, int flags,
1170 const char *dev_name, void *opts)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001171{
1172 struct ffs_sb_fill_data data = {
1173 .perms = {
1174 .mode = S_IFREG | 0600,
1175 .uid = 0,
1176 .gid = 0
1177 },
1178 .root_mode = S_IFDIR | 0500,
1179 };
1180 int ret;
1181
1182 ENTER();
1183
1184 ret = functionfs_check_dev_callback(dev_name);
1185 if (unlikely(ret < 0))
Al Virofc14f2f2010-07-25 01:48:30 +04001186 return ERR_PTR(ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001187
1188 ret = ffs_fs_parse_opts(&data, opts);
1189 if (unlikely(ret < 0))
Al Virofc14f2f2010-07-25 01:48:30 +04001190 return ERR_PTR(ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001191
1192 data.dev_name = dev_name;
Al Virofc14f2f2010-07-25 01:48:30 +04001193 return mount_single(t, flags, &data, ffs_sb_fill);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001194}
1195
1196static void
1197ffs_fs_kill_sb(struct super_block *sb)
1198{
1199 void *ptr;
1200
1201 ENTER();
1202
1203 kill_litter_super(sb);
1204 ptr = xchg(&sb->s_fs_info, NULL);
1205 if (ptr)
1206 ffs_data_put(ptr);
1207}
1208
1209static struct file_system_type ffs_fs_type = {
1210 .owner = THIS_MODULE,
1211 .name = "functionfs",
Al Virofc14f2f2010-07-25 01:48:30 +04001212 .mount = ffs_fs_mount,
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001213 .kill_sb = ffs_fs_kill_sb,
1214};
1215
1216
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001217/* Driver's main init/cleanup functions *************************************/
1218
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001219static int functionfs_init(void)
1220{
1221 int ret;
1222
1223 ENTER();
1224
1225 ret = register_filesystem(&ffs_fs_type);
1226 if (likely(!ret))
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001227 pr_info("file system registered\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001228 else
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001229 pr_err("failed registering file system (%d)\n", ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001230
1231 return ret;
1232}
1233
1234static void functionfs_cleanup(void)
1235{
1236 ENTER();
1237
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001238 pr_info("unloading\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001239 unregister_filesystem(&ffs_fs_type);
1240}
1241
1242
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001243/* ffs_data and ffs_function construction and destruction code **************/
1244
1245static void ffs_data_clear(struct ffs_data *ffs);
1246static void ffs_data_reset(struct ffs_data *ffs);
1247
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001248static void ffs_data_get(struct ffs_data *ffs)
1249{
1250 ENTER();
1251
1252 atomic_inc(&ffs->ref);
1253}
1254
1255static void ffs_data_opened(struct ffs_data *ffs)
1256{
1257 ENTER();
1258
1259 atomic_inc(&ffs->ref);
1260 atomic_inc(&ffs->opened);
1261}
1262
1263static void ffs_data_put(struct ffs_data *ffs)
1264{
1265 ENTER();
1266
1267 if (unlikely(atomic_dec_and_test(&ffs->ref))) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001268 pr_info("%s(): freeing\n", __func__);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001269 ffs_data_clear(ffs);
1270 BUG_ON(mutex_is_locked(&ffs->mutex) ||
1271 spin_is_locked(&ffs->ev.waitq.lock) ||
1272 waitqueue_active(&ffs->ev.waitq) ||
1273 waitqueue_active(&ffs->ep0req_completion.wait));
1274 kfree(ffs);
1275 }
1276}
1277
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001278static void ffs_data_closed(struct ffs_data *ffs)
1279{
1280 ENTER();
1281
1282 if (atomic_dec_and_test(&ffs->opened)) {
1283 ffs->state = FFS_CLOSING;
1284 ffs_data_reset(ffs);
1285 }
1286
1287 ffs_data_put(ffs);
1288}
1289
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001290static struct ffs_data *ffs_data_new(void)
1291{
1292 struct ffs_data *ffs = kzalloc(sizeof *ffs, GFP_KERNEL);
1293 if (unlikely(!ffs))
1294 return 0;
1295
1296 ENTER();
1297
1298 atomic_set(&ffs->ref, 1);
1299 atomic_set(&ffs->opened, 0);
1300 ffs->state = FFS_READ_DESCRIPTORS;
1301 mutex_init(&ffs->mutex);
1302 spin_lock_init(&ffs->eps_lock);
1303 init_waitqueue_head(&ffs->ev.waitq);
1304 init_completion(&ffs->ep0req_completion);
1305
1306 /* XXX REVISIT need to update it in some places, or do we? */
1307 ffs->ev.can_stall = 1;
1308
1309 return ffs;
1310}
1311
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001312static void ffs_data_clear(struct ffs_data *ffs)
1313{
1314 ENTER();
1315
1316 if (test_and_clear_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags))
1317 functionfs_closed_callback(ffs);
1318
1319 BUG_ON(ffs->gadget);
1320
1321 if (ffs->epfiles)
1322 ffs_epfiles_destroy(ffs->epfiles, ffs->eps_count);
1323
1324 kfree(ffs->raw_descs);
1325 kfree(ffs->raw_strings);
1326 kfree(ffs->stringtabs);
1327}
1328
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001329static void ffs_data_reset(struct ffs_data *ffs)
1330{
1331 ENTER();
1332
1333 ffs_data_clear(ffs);
1334
1335 ffs->epfiles = NULL;
1336 ffs->raw_descs = NULL;
1337 ffs->raw_strings = NULL;
1338 ffs->stringtabs = NULL;
1339
1340 ffs->raw_descs_length = 0;
1341 ffs->raw_fs_descs_length = 0;
1342 ffs->fs_descs_count = 0;
1343 ffs->hs_descs_count = 0;
1344
1345 ffs->strings_count = 0;
1346 ffs->interfaces_count = 0;
1347 ffs->eps_count = 0;
1348
1349 ffs->ev.count = 0;
1350
1351 ffs->state = FFS_READ_DESCRIPTORS;
1352 ffs->setup_state = FFS_NO_SETUP;
1353 ffs->flags = 0;
1354}
1355
1356
1357static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev)
1358{
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001359 struct usb_gadget_strings **lang;
1360 int first_id;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001361
1362 ENTER();
1363
1364 if (WARN_ON(ffs->state != FFS_ACTIVE
1365 || test_and_set_bit(FFS_FL_BOUND, &ffs->flags)))
1366 return -EBADFD;
1367
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001368 first_id = usb_string_ids_n(cdev, ffs->strings_count);
1369 if (unlikely(first_id < 0))
1370 return first_id;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001371
1372 ffs->ep0req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
1373 if (unlikely(!ffs->ep0req))
1374 return -ENOMEM;
1375 ffs->ep0req->complete = ffs_ep0_complete;
1376 ffs->ep0req->context = ffs;
1377
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001378 lang = ffs->stringtabs;
1379 for (lang = ffs->stringtabs; *lang; ++lang) {
1380 struct usb_string *str = (*lang)->strings;
1381 int id = first_id;
1382 for (; str->s; ++id, ++str)
1383 str->id = id;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001384 }
1385
1386 ffs->gadget = cdev->gadget;
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001387 ffs_data_get(ffs);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001388 return 0;
1389}
1390
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001391static void functionfs_unbind(struct ffs_data *ffs)
1392{
1393 ENTER();
1394
1395 if (!WARN_ON(!ffs->gadget)) {
1396 usb_ep_free_request(ffs->gadget->ep0, ffs->ep0req);
1397 ffs->ep0req = NULL;
1398 ffs->gadget = NULL;
1399 ffs_data_put(ffs);
1400 }
1401}
1402
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001403static int ffs_epfiles_create(struct ffs_data *ffs)
1404{
1405 struct ffs_epfile *epfile, *epfiles;
1406 unsigned i, count;
1407
1408 ENTER();
1409
1410 count = ffs->eps_count;
1411 epfiles = kzalloc(count * sizeof *epfiles, GFP_KERNEL);
1412 if (!epfiles)
1413 return -ENOMEM;
1414
1415 epfile = epfiles;
1416 for (i = 1; i <= count; ++i, ++epfile) {
1417 epfile->ffs = ffs;
1418 mutex_init(&epfile->mutex);
1419 init_waitqueue_head(&epfile->wait);
1420 sprintf(epfiles->name, "ep%u", i);
1421 if (!unlikely(ffs_sb_create_file(ffs->sb, epfiles->name, epfile,
1422 &ffs_epfile_operations,
1423 &epfile->dentry))) {
1424 ffs_epfiles_destroy(epfiles, i - 1);
1425 return -ENOMEM;
1426 }
1427 }
1428
1429 ffs->epfiles = epfiles;
1430 return 0;
1431}
1432
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001433static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count)
1434{
1435 struct ffs_epfile *epfile = epfiles;
1436
1437 ENTER();
1438
1439 for (; count; --count, ++epfile) {
1440 BUG_ON(mutex_is_locked(&epfile->mutex) ||
1441 waitqueue_active(&epfile->wait));
1442 if (epfile->dentry) {
1443 d_delete(epfile->dentry);
1444 dput(epfile->dentry);
1445 epfile->dentry = NULL;
1446 }
1447 }
1448
1449 kfree(epfiles);
1450}
1451
Michal Nazarewicz7898aee2010-06-16 12:07:58 +02001452static int functionfs_bind_config(struct usb_composite_dev *cdev,
1453 struct usb_configuration *c,
1454 struct ffs_data *ffs)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001455{
1456 struct ffs_function *func;
1457 int ret;
1458
1459 ENTER();
1460
1461 func = kzalloc(sizeof *func, GFP_KERNEL);
1462 if (unlikely(!func))
1463 return -ENOMEM;
1464
1465 func->function.name = "Function FS Gadget";
1466 func->function.strings = ffs->stringtabs;
1467
1468 func->function.bind = ffs_func_bind;
1469 func->function.unbind = ffs_func_unbind;
1470 func->function.set_alt = ffs_func_set_alt;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001471 func->function.disable = ffs_func_disable;
1472 func->function.setup = ffs_func_setup;
1473 func->function.suspend = ffs_func_suspend;
1474 func->function.resume = ffs_func_resume;
1475
1476 func->conf = c;
1477 func->gadget = cdev->gadget;
1478 func->ffs = ffs;
1479 ffs_data_get(ffs);
1480
1481 ret = usb_add_function(c, &func->function);
1482 if (unlikely(ret))
1483 ffs_func_free(func);
1484
1485 return ret;
1486}
1487
1488static void ffs_func_free(struct ffs_function *func)
1489{
1490 ENTER();
1491
1492 ffs_data_put(func->ffs);
1493
1494 kfree(func->eps);
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001495 /*
1496 * eps and interfaces_nums are allocated in the same chunk so
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001497 * only one free is required. Descriptors are also allocated
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001498 * in the same chunk.
1499 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001500
1501 kfree(func);
1502}
1503
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001504static void ffs_func_eps_disable(struct ffs_function *func)
1505{
1506 struct ffs_ep *ep = func->eps;
1507 struct ffs_epfile *epfile = func->ffs->epfiles;
1508 unsigned count = func->ffs->eps_count;
1509 unsigned long flags;
1510
1511 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1512 do {
1513 /* pending requests get nuked */
1514 if (likely(ep->ep))
1515 usb_ep_disable(ep->ep);
1516 epfile->ep = NULL;
1517
1518 ++ep;
1519 ++epfile;
1520 } while (--count);
1521 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1522}
1523
1524static int ffs_func_eps_enable(struct ffs_function *func)
1525{
1526 struct ffs_data *ffs = func->ffs;
1527 struct ffs_ep *ep = func->eps;
1528 struct ffs_epfile *epfile = ffs->epfiles;
1529 unsigned count = ffs->eps_count;
1530 unsigned long flags;
1531 int ret = 0;
1532
1533 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1534 do {
1535 struct usb_endpoint_descriptor *ds;
1536 ds = ep->descs[ep->descs[1] ? 1 : 0];
1537
1538 ep->ep->driver_data = ep;
1539 ret = usb_ep_enable(ep->ep, ds);
1540 if (likely(!ret)) {
1541 epfile->ep = ep;
1542 epfile->in = usb_endpoint_dir_in(ds);
1543 epfile->isoc = usb_endpoint_xfer_isoc(ds);
1544 } else {
1545 break;
1546 }
1547
1548 wake_up(&epfile->wait);
1549
1550 ++ep;
1551 ++epfile;
1552 } while (--count);
1553 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1554
1555 return ret;
1556}
1557
1558
1559/* Parsing and building descriptors and strings *****************************/
1560
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001561/*
1562 * This validates if data pointed by data is a valid USB descriptor as
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001563 * well as record how many interfaces, endpoints and strings are
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001564 * required by given configuration. Returns address after the
1565 * descriptor or NULL if data is invalid.
1566 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001567
1568enum ffs_entity_type {
1569 FFS_DESCRIPTOR, FFS_INTERFACE, FFS_STRING, FFS_ENDPOINT
1570};
1571
1572typedef int (*ffs_entity_callback)(enum ffs_entity_type entity,
1573 u8 *valuep,
1574 struct usb_descriptor_header *desc,
1575 void *priv);
1576
1577static int __must_check ffs_do_desc(char *data, unsigned len,
1578 ffs_entity_callback entity, void *priv)
1579{
1580 struct usb_descriptor_header *_ds = (void *)data;
1581 u8 length;
1582 int ret;
1583
1584 ENTER();
1585
1586 /* At least two bytes are required: length and type */
1587 if (len < 2) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001588 pr_vdebug("descriptor too short\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001589 return -EINVAL;
1590 }
1591
1592 /* If we have at least as many bytes as the descriptor takes? */
1593 length = _ds->bLength;
1594 if (len < length) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001595 pr_vdebug("descriptor longer then available data\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001596 return -EINVAL;
1597 }
1598
1599#define __entity_check_INTERFACE(val) 1
1600#define __entity_check_STRING(val) (val)
1601#define __entity_check_ENDPOINT(val) ((val) & USB_ENDPOINT_NUMBER_MASK)
1602#define __entity(type, val) do { \
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001603 pr_vdebug("entity " #type "(%02x)\n", (val)); \
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001604 if (unlikely(!__entity_check_ ##type(val))) { \
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001605 pr_vdebug("invalid entity's value\n"); \
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001606 return -EINVAL; \
1607 } \
1608 ret = entity(FFS_ ##type, &val, _ds, priv); \
1609 if (unlikely(ret < 0)) { \
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001610 pr_debug("entity " #type "(%02x); ret = %d\n", \
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +01001611 (val), ret); \
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001612 return ret; \
1613 } \
1614 } while (0)
1615
1616 /* Parse descriptor depending on type. */
1617 switch (_ds->bDescriptorType) {
1618 case USB_DT_DEVICE:
1619 case USB_DT_CONFIG:
1620 case USB_DT_STRING:
1621 case USB_DT_DEVICE_QUALIFIER:
1622 /* function can't have any of those */
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001623 pr_vdebug("descriptor reserved for gadget: %d\n",
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001624 _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001625 return -EINVAL;
1626
1627 case USB_DT_INTERFACE: {
1628 struct usb_interface_descriptor *ds = (void *)_ds;
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001629 pr_vdebug("interface descriptor\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001630 if (length != sizeof *ds)
1631 goto inv_length;
1632
1633 __entity(INTERFACE, ds->bInterfaceNumber);
1634 if (ds->iInterface)
1635 __entity(STRING, ds->iInterface);
1636 }
1637 break;
1638
1639 case USB_DT_ENDPOINT: {
1640 struct usb_endpoint_descriptor *ds = (void *)_ds;
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001641 pr_vdebug("endpoint descriptor\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001642 if (length != USB_DT_ENDPOINT_SIZE &&
1643 length != USB_DT_ENDPOINT_AUDIO_SIZE)
1644 goto inv_length;
1645 __entity(ENDPOINT, ds->bEndpointAddress);
1646 }
1647 break;
1648
1649 case USB_DT_OTG:
1650 if (length != sizeof(struct usb_otg_descriptor))
1651 goto inv_length;
1652 break;
1653
1654 case USB_DT_INTERFACE_ASSOCIATION: {
1655 struct usb_interface_assoc_descriptor *ds = (void *)_ds;
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001656 pr_vdebug("interface association descriptor\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001657 if (length != sizeof *ds)
1658 goto inv_length;
1659 if (ds->iFunction)
1660 __entity(STRING, ds->iFunction);
1661 }
1662 break;
1663
1664 case USB_DT_OTHER_SPEED_CONFIG:
1665 case USB_DT_INTERFACE_POWER:
1666 case USB_DT_DEBUG:
1667 case USB_DT_SECURITY:
1668 case USB_DT_CS_RADIO_CONTROL:
1669 /* TODO */
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001670 pr_vdebug("unimplemented descriptor: %d\n", _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001671 return -EINVAL;
1672
1673 default:
1674 /* We should never be here */
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001675 pr_vdebug("unknown descriptor: %d\n", _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001676 return -EINVAL;
1677
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001678inv_length:
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001679 pr_vdebug("invalid length: %d (descriptor %d)\n",
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +01001680 _ds->bLength, _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001681 return -EINVAL;
1682 }
1683
1684#undef __entity
1685#undef __entity_check_DESCRIPTOR
1686#undef __entity_check_INTERFACE
1687#undef __entity_check_STRING
1688#undef __entity_check_ENDPOINT
1689
1690 return length;
1691}
1692
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001693static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len,
1694 ffs_entity_callback entity, void *priv)
1695{
1696 const unsigned _len = len;
1697 unsigned long num = 0;
1698
1699 ENTER();
1700
1701 for (;;) {
1702 int ret;
1703
1704 if (num == count)
1705 data = NULL;
1706
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001707 /* Record "descriptor" entity */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001708 ret = entity(FFS_DESCRIPTOR, (u8 *)num, (void *)data, priv);
1709 if (unlikely(ret < 0)) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001710 pr_debug("entity DESCRIPTOR(%02lx); ret = %d\n",
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +01001711 num, ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001712 return ret;
1713 }
1714
1715 if (!data)
1716 return _len - len;
1717
1718 ret = ffs_do_desc(data, len, entity, priv);
1719 if (unlikely(ret < 0)) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001720 pr_debug("%s returns %d\n", __func__, ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001721 return ret;
1722 }
1723
1724 len -= ret;
1725 data += ret;
1726 ++num;
1727 }
1728}
1729
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001730static int __ffs_data_do_entity(enum ffs_entity_type type,
1731 u8 *valuep, struct usb_descriptor_header *desc,
1732 void *priv)
1733{
1734 struct ffs_data *ffs = priv;
1735
1736 ENTER();
1737
1738 switch (type) {
1739 case FFS_DESCRIPTOR:
1740 break;
1741
1742 case FFS_INTERFACE:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001743 /*
1744 * Interfaces are indexed from zero so if we
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001745 * encountered interface "n" then there are at least
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001746 * "n+1" interfaces.
1747 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001748 if (*valuep >= ffs->interfaces_count)
1749 ffs->interfaces_count = *valuep + 1;
1750 break;
1751
1752 case FFS_STRING:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001753 /*
1754 * Strings are indexed from 1 (0 is magic ;) reserved
1755 * for languages list or some such)
1756 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001757 if (*valuep > ffs->strings_count)
1758 ffs->strings_count = *valuep;
1759 break;
1760
1761 case FFS_ENDPOINT:
1762 /* Endpoints are indexed from 1 as well. */
1763 if ((*valuep & USB_ENDPOINT_NUMBER_MASK) > ffs->eps_count)
1764 ffs->eps_count = (*valuep & USB_ENDPOINT_NUMBER_MASK);
1765 break;
1766 }
1767
1768 return 0;
1769}
1770
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001771static int __ffs_data_got_descs(struct ffs_data *ffs,
1772 char *const _data, size_t len)
1773{
1774 unsigned fs_count, hs_count;
1775 int fs_len, ret = -EINVAL;
1776 char *data = _data;
1777
1778 ENTER();
1779
1780 if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_DESCRIPTORS_MAGIC ||
1781 get_unaligned_le32(data + 4) != len))
1782 goto error;
1783 fs_count = get_unaligned_le32(data + 8);
1784 hs_count = get_unaligned_le32(data + 12);
1785
1786 if (!fs_count && !hs_count)
1787 goto einval;
1788
1789 data += 16;
1790 len -= 16;
1791
1792 if (likely(fs_count)) {
1793 fs_len = ffs_do_descs(fs_count, data, len,
1794 __ffs_data_do_entity, ffs);
1795 if (unlikely(fs_len < 0)) {
1796 ret = fs_len;
1797 goto error;
1798 }
1799
1800 data += fs_len;
1801 len -= fs_len;
1802 } else {
1803 fs_len = 0;
1804 }
1805
1806 if (likely(hs_count)) {
1807 ret = ffs_do_descs(hs_count, data, len,
1808 __ffs_data_do_entity, ffs);
1809 if (unlikely(ret < 0))
1810 goto error;
1811 } else {
1812 ret = 0;
1813 }
1814
1815 if (unlikely(len != ret))
1816 goto einval;
1817
1818 ffs->raw_fs_descs_length = fs_len;
1819 ffs->raw_descs_length = fs_len + ret;
1820 ffs->raw_descs = _data;
1821 ffs->fs_descs_count = fs_count;
1822 ffs->hs_descs_count = hs_count;
1823
1824 return 0;
1825
1826einval:
1827 ret = -EINVAL;
1828error:
1829 kfree(_data);
1830 return ret;
1831}
1832
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001833static int __ffs_data_got_strings(struct ffs_data *ffs,
1834 char *const _data, size_t len)
1835{
1836 u32 str_count, needed_count, lang_count;
1837 struct usb_gadget_strings **stringtabs, *t;
1838 struct usb_string *strings, *s;
1839 const char *data = _data;
1840
1841 ENTER();
1842
1843 if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_STRINGS_MAGIC ||
1844 get_unaligned_le32(data + 4) != len))
1845 goto error;
1846 str_count = get_unaligned_le32(data + 8);
1847 lang_count = get_unaligned_le32(data + 12);
1848
1849 /* if one is zero the other must be zero */
1850 if (unlikely(!str_count != !lang_count))
1851 goto error;
1852
1853 /* Do we have at least as many strings as descriptors need? */
1854 needed_count = ffs->strings_count;
1855 if (unlikely(str_count < needed_count))
1856 goto error;
1857
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001858 /*
1859 * If we don't need any strings just return and free all
1860 * memory.
1861 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001862 if (!needed_count) {
1863 kfree(_data);
1864 return 0;
1865 }
1866
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001867 /* Allocate everything in one chunk so there's less maintenance. */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001868 {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001869 struct {
1870 struct usb_gadget_strings *stringtabs[lang_count + 1];
1871 struct usb_gadget_strings stringtab[lang_count];
1872 struct usb_string strings[lang_count*(needed_count+1)];
1873 } *d;
1874 unsigned i = 0;
1875
1876 d = kmalloc(sizeof *d, GFP_KERNEL);
1877 if (unlikely(!d)) {
1878 kfree(_data);
1879 return -ENOMEM;
1880 }
1881
1882 stringtabs = d->stringtabs;
1883 t = d->stringtab;
1884 i = lang_count;
1885 do {
1886 *stringtabs++ = t++;
1887 } while (--i);
1888 *stringtabs = NULL;
1889
1890 stringtabs = d->stringtabs;
1891 t = d->stringtab;
1892 s = d->strings;
1893 strings = s;
1894 }
1895
1896 /* For each language */
1897 data += 16;
1898 len -= 16;
1899
1900 do { /* lang_count > 0 so we can use do-while */
1901 unsigned needed = needed_count;
1902
1903 if (unlikely(len < 3))
1904 goto error_free;
1905 t->language = get_unaligned_le16(data);
1906 t->strings = s;
1907 ++t;
1908
1909 data += 2;
1910 len -= 2;
1911
1912 /* For each string */
1913 do { /* str_count > 0 so we can use do-while */
1914 size_t length = strnlen(data, len);
1915
1916 if (unlikely(length == len))
1917 goto error_free;
1918
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001919 /*
1920 * User may provide more strings then we need,
1921 * if that's the case we simply ignore the
1922 * rest
1923 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001924 if (likely(needed)) {
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001925 /*
1926 * s->id will be set while adding
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001927 * function to configuration so for
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001928 * now just leave garbage here.
1929 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001930 s->s = data;
1931 --needed;
1932 ++s;
1933 }
1934
1935 data += length + 1;
1936 len -= length + 1;
1937 } while (--str_count);
1938
1939 s->id = 0; /* terminator */
1940 s->s = NULL;
1941 ++s;
1942
1943 } while (--lang_count);
1944
1945 /* Some garbage left? */
1946 if (unlikely(len))
1947 goto error_free;
1948
1949 /* Done! */
1950 ffs->stringtabs = stringtabs;
1951 ffs->raw_strings = _data;
1952
1953 return 0;
1954
1955error_free:
1956 kfree(stringtabs);
1957error:
1958 kfree(_data);
1959 return -EINVAL;
1960}
1961
1962
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001963/* Events handling and management *******************************************/
1964
1965static void __ffs_event_add(struct ffs_data *ffs,
1966 enum usb_functionfs_event_type type)
1967{
1968 enum usb_functionfs_event_type rem_type1, rem_type2 = type;
1969 int neg = 0;
1970
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001971 /*
1972 * Abort any unhandled setup
1973 *
1974 * We do not need to worry about some cmpxchg() changing value
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001975 * of ffs->setup_state without holding the lock because when
1976 * state is FFS_SETUP_PENDING cmpxchg() in several places in
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001977 * the source does nothing.
1978 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001979 if (ffs->setup_state == FFS_SETUP_PENDING)
1980 ffs->setup_state = FFS_SETUP_CANCELED;
1981
1982 switch (type) {
1983 case FUNCTIONFS_RESUME:
1984 rem_type2 = FUNCTIONFS_SUSPEND;
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001985 /* FALL THROUGH */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001986 case FUNCTIONFS_SUSPEND:
1987 case FUNCTIONFS_SETUP:
1988 rem_type1 = type;
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001989 /* Discard all similar events */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001990 break;
1991
1992 case FUNCTIONFS_BIND:
1993 case FUNCTIONFS_UNBIND:
1994 case FUNCTIONFS_DISABLE:
1995 case FUNCTIONFS_ENABLE:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001996 /* Discard everything other then power management. */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001997 rem_type1 = FUNCTIONFS_SUSPEND;
1998 rem_type2 = FUNCTIONFS_RESUME;
1999 neg = 1;
2000 break;
2001
2002 default:
2003 BUG();
2004 }
2005
2006 {
2007 u8 *ev = ffs->ev.types, *out = ev;
2008 unsigned n = ffs->ev.count;
2009 for (; n; --n, ++ev)
2010 if ((*ev == rem_type1 || *ev == rem_type2) == neg)
2011 *out++ = *ev;
2012 else
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002013 pr_vdebug("purging event %d\n", *ev);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002014 ffs->ev.count = out - ffs->ev.types;
2015 }
2016
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002017 pr_vdebug("adding event %d\n", type);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002018 ffs->ev.types[ffs->ev.count++] = type;
2019 wake_up_locked(&ffs->ev.waitq);
2020}
2021
2022static void ffs_event_add(struct ffs_data *ffs,
2023 enum usb_functionfs_event_type type)
2024{
2025 unsigned long flags;
2026 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2027 __ffs_event_add(ffs, type);
2028 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2029}
2030
2031
2032/* Bind/unbind USB function hooks *******************************************/
2033
2034static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
2035 struct usb_descriptor_header *desc,
2036 void *priv)
2037{
2038 struct usb_endpoint_descriptor *ds = (void *)desc;
2039 struct ffs_function *func = priv;
2040 struct ffs_ep *ffs_ep;
2041
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002042 /*
2043 * If hs_descriptors is not NULL then we are reading hs
2044 * descriptors now
2045 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002046 const int isHS = func->function.hs_descriptors != NULL;
2047 unsigned idx;
2048
2049 if (type != FFS_DESCRIPTOR)
2050 return 0;
2051
2052 if (isHS)
2053 func->function.hs_descriptors[(long)valuep] = desc;
2054 else
2055 func->function.descriptors[(long)valuep] = desc;
2056
2057 if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT)
2058 return 0;
2059
2060 idx = (ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK) - 1;
2061 ffs_ep = func->eps + idx;
2062
2063 if (unlikely(ffs_ep->descs[isHS])) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002064 pr_vdebug("two %sspeed descriptors for EP %d\n",
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +01002065 isHS ? "high" : "full",
2066 ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002067 return -EINVAL;
2068 }
2069 ffs_ep->descs[isHS] = ds;
2070
2071 ffs_dump_mem(": Original ep desc", ds, ds->bLength);
2072 if (ffs_ep->ep) {
2073 ds->bEndpointAddress = ffs_ep->descs[0]->bEndpointAddress;
2074 if (!ds->wMaxPacketSize)
2075 ds->wMaxPacketSize = ffs_ep->descs[0]->wMaxPacketSize;
2076 } else {
2077 struct usb_request *req;
2078 struct usb_ep *ep;
2079
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002080 pr_vdebug("autoconfig\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002081 ep = usb_ep_autoconfig(func->gadget, ds);
2082 if (unlikely(!ep))
2083 return -ENOTSUPP;
Joe Perchescc7e6052010-11-14 19:04:49 -08002084 ep->driver_data = func->eps + idx;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002085
2086 req = usb_ep_alloc_request(ep, GFP_KERNEL);
2087 if (unlikely(!req))
2088 return -ENOMEM;
2089
2090 ffs_ep->ep = ep;
2091 ffs_ep->req = req;
2092 func->eps_revmap[ds->bEndpointAddress &
2093 USB_ENDPOINT_NUMBER_MASK] = idx + 1;
2094 }
2095 ffs_dump_mem(": Rewritten ep desc", ds, ds->bLength);
2096
2097 return 0;
2098}
2099
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002100static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep,
2101 struct usb_descriptor_header *desc,
2102 void *priv)
2103{
2104 struct ffs_function *func = priv;
2105 unsigned idx;
2106 u8 newValue;
2107
2108 switch (type) {
2109 default:
2110 case FFS_DESCRIPTOR:
2111 /* Handled in previous pass by __ffs_func_bind_do_descs() */
2112 return 0;
2113
2114 case FFS_INTERFACE:
2115 idx = *valuep;
2116 if (func->interfaces_nums[idx] < 0) {
2117 int id = usb_interface_id(func->conf, &func->function);
2118 if (unlikely(id < 0))
2119 return id;
2120 func->interfaces_nums[idx] = id;
2121 }
2122 newValue = func->interfaces_nums[idx];
2123 break;
2124
2125 case FFS_STRING:
2126 /* String' IDs are allocated when fsf_data is bound to cdev */
2127 newValue = func->ffs->stringtabs[0]->strings[*valuep - 1].id;
2128 break;
2129
2130 case FFS_ENDPOINT:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002131 /*
2132 * USB_DT_ENDPOINT are handled in
2133 * __ffs_func_bind_do_descs().
2134 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002135 if (desc->bDescriptorType == USB_DT_ENDPOINT)
2136 return 0;
2137
2138 idx = (*valuep & USB_ENDPOINT_NUMBER_MASK) - 1;
2139 if (unlikely(!func->eps[idx].ep))
2140 return -EINVAL;
2141
2142 {
2143 struct usb_endpoint_descriptor **descs;
2144 descs = func->eps[idx].descs;
2145 newValue = descs[descs[0] ? 0 : 1]->bEndpointAddress;
2146 }
2147 break;
2148 }
2149
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002150 pr_vdebug("%02x -> %02x\n", *valuep, newValue);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002151 *valuep = newValue;
2152 return 0;
2153}
2154
2155static int ffs_func_bind(struct usb_configuration *c,
2156 struct usb_function *f)
2157{
2158 struct ffs_function *func = ffs_func_from_usb(f);
2159 struct ffs_data *ffs = func->ffs;
2160
2161 const int full = !!func->ffs->fs_descs_count;
2162 const int high = gadget_is_dualspeed(func->gadget) &&
2163 func->ffs->hs_descs_count;
2164
2165 int ret;
2166
2167 /* Make it a single chunk, less management later on */
2168 struct {
2169 struct ffs_ep eps[ffs->eps_count];
2170 struct usb_descriptor_header
2171 *fs_descs[full ? ffs->fs_descs_count + 1 : 0];
2172 struct usb_descriptor_header
2173 *hs_descs[high ? ffs->hs_descs_count + 1 : 0];
2174 short inums[ffs->interfaces_count];
2175 char raw_descs[high ? ffs->raw_descs_length
2176 : ffs->raw_fs_descs_length];
2177 } *data;
2178
2179 ENTER();
2180
2181 /* Only high speed but not supported by gadget? */
2182 if (unlikely(!(full | high)))
2183 return -ENOTSUPP;
2184
2185 /* Allocate */
2186 data = kmalloc(sizeof *data, GFP_KERNEL);
2187 if (unlikely(!data))
2188 return -ENOMEM;
2189
2190 /* Zero */
2191 memset(data->eps, 0, sizeof data->eps);
2192 memcpy(data->raw_descs, ffs->raw_descs + 16, sizeof data->raw_descs);
2193 memset(data->inums, 0xff, sizeof data->inums);
2194 for (ret = ffs->eps_count; ret; --ret)
2195 data->eps[ret].num = -1;
2196
2197 /* Save pointers */
2198 func->eps = data->eps;
2199 func->interfaces_nums = data->inums;
2200
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002201 /*
2202 * Go through all the endpoint descriptors and allocate
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002203 * endpoints first, so that later we can rewrite the endpoint
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002204 * numbers without worrying that it may be described later on.
2205 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002206 if (likely(full)) {
2207 func->function.descriptors = data->fs_descs;
2208 ret = ffs_do_descs(ffs->fs_descs_count,
2209 data->raw_descs,
2210 sizeof data->raw_descs,
2211 __ffs_func_bind_do_descs, func);
2212 if (unlikely(ret < 0))
2213 goto error;
2214 } else {
2215 ret = 0;
2216 }
2217
2218 if (likely(high)) {
2219 func->function.hs_descriptors = data->hs_descs;
2220 ret = ffs_do_descs(ffs->hs_descs_count,
2221 data->raw_descs + ret,
2222 (sizeof data->raw_descs) - ret,
2223 __ffs_func_bind_do_descs, func);
2224 }
2225
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002226 /*
2227 * Now handle interface numbers allocation and interface and
2228 * endpoint numbers rewriting. We can do that in one go
2229 * now.
2230 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002231 ret = ffs_do_descs(ffs->fs_descs_count +
2232 (high ? ffs->hs_descs_count : 0),
2233 data->raw_descs, sizeof data->raw_descs,
2234 __ffs_func_bind_do_nums, func);
2235 if (unlikely(ret < 0))
2236 goto error;
2237
2238 /* And we're done */
2239 ffs_event_add(ffs, FUNCTIONFS_BIND);
2240 return 0;
2241
2242error:
2243 /* XXX Do we need to release all claimed endpoints here? */
2244 return ret;
2245}
2246
2247
2248/* Other USB function hooks *************************************************/
2249
2250static void ffs_func_unbind(struct usb_configuration *c,
2251 struct usb_function *f)
2252{
2253 struct ffs_function *func = ffs_func_from_usb(f);
2254 struct ffs_data *ffs = func->ffs;
2255
2256 ENTER();
2257
2258 if (ffs->func == func) {
2259 ffs_func_eps_disable(func);
2260 ffs->func = NULL;
2261 }
2262
2263 ffs_event_add(ffs, FUNCTIONFS_UNBIND);
2264
2265 ffs_func_free(func);
2266}
2267
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002268static int ffs_func_set_alt(struct usb_function *f,
2269 unsigned interface, unsigned alt)
2270{
2271 struct ffs_function *func = ffs_func_from_usb(f);
2272 struct ffs_data *ffs = func->ffs;
2273 int ret = 0, intf;
2274
2275 if (alt != (unsigned)-1) {
2276 intf = ffs_func_revmap_intf(func, interface);
2277 if (unlikely(intf < 0))
2278 return intf;
2279 }
2280
2281 if (ffs->func)
2282 ffs_func_eps_disable(ffs->func);
2283
2284 if (ffs->state != FFS_ACTIVE)
2285 return -ENODEV;
2286
2287 if (alt == (unsigned)-1) {
2288 ffs->func = NULL;
2289 ffs_event_add(ffs, FUNCTIONFS_DISABLE);
2290 return 0;
2291 }
2292
2293 ffs->func = func;
2294 ret = ffs_func_eps_enable(func);
2295 if (likely(ret >= 0))
2296 ffs_event_add(ffs, FUNCTIONFS_ENABLE);
2297 return ret;
2298}
2299
2300static void ffs_func_disable(struct usb_function *f)
2301{
2302 ffs_func_set_alt(f, 0, (unsigned)-1);
2303}
2304
2305static int ffs_func_setup(struct usb_function *f,
2306 const struct usb_ctrlrequest *creq)
2307{
2308 struct ffs_function *func = ffs_func_from_usb(f);
2309 struct ffs_data *ffs = func->ffs;
2310 unsigned long flags;
2311 int ret;
2312
2313 ENTER();
2314
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002315 pr_vdebug("creq->bRequestType = %02x\n", creq->bRequestType);
2316 pr_vdebug("creq->bRequest = %02x\n", creq->bRequest);
2317 pr_vdebug("creq->wValue = %04x\n", le16_to_cpu(creq->wValue));
2318 pr_vdebug("creq->wIndex = %04x\n", le16_to_cpu(creq->wIndex));
2319 pr_vdebug("creq->wLength = %04x\n", le16_to_cpu(creq->wLength));
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002320
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002321 /*
2322 * Most requests directed to interface go through here
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002323 * (notable exceptions are set/get interface) so we need to
2324 * handle them. All other either handled by composite or
2325 * passed to usb_configuration->setup() (if one is set). No
2326 * matter, we will handle requests directed to endpoint here
2327 * as well (as it's straightforward) but what to do with any
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002328 * other request?
2329 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002330 if (ffs->state != FFS_ACTIVE)
2331 return -ENODEV;
2332
2333 switch (creq->bRequestType & USB_RECIP_MASK) {
2334 case USB_RECIP_INTERFACE:
2335 ret = ffs_func_revmap_intf(func, le16_to_cpu(creq->wIndex));
2336 if (unlikely(ret < 0))
2337 return ret;
2338 break;
2339
2340 case USB_RECIP_ENDPOINT:
2341 ret = ffs_func_revmap_ep(func, le16_to_cpu(creq->wIndex));
2342 if (unlikely(ret < 0))
2343 return ret;
2344 break;
2345
2346 default:
2347 return -EOPNOTSUPP;
2348 }
2349
2350 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2351 ffs->ev.setup = *creq;
2352 ffs->ev.setup.wIndex = cpu_to_le16(ret);
2353 __ffs_event_add(ffs, FUNCTIONFS_SETUP);
2354 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2355
2356 return 0;
2357}
2358
2359static void ffs_func_suspend(struct usb_function *f)
2360{
2361 ENTER();
2362 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_SUSPEND);
2363}
2364
2365static void ffs_func_resume(struct usb_function *f)
2366{
2367 ENTER();
2368 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_RESUME);
2369}
2370
2371
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002372/* Endpoint and interface numbers reverse mapping ***************************/
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002373
2374static int ffs_func_revmap_ep(struct ffs_function *func, u8 num)
2375{
2376 num = func->eps_revmap[num & USB_ENDPOINT_NUMBER_MASK];
2377 return num ? num : -EDOM;
2378}
2379
2380static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf)
2381{
2382 short *nums = func->interfaces_nums;
2383 unsigned count = func->ffs->interfaces_count;
2384
2385 for (; count; --count, ++nums) {
2386 if (*nums >= 0 && *nums == intf)
2387 return nums - func->interfaces_nums;
2388 }
2389
2390 return -EDOM;
2391}
2392
2393
2394/* Misc helper functions ****************************************************/
2395
2396static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
2397{
2398 return nonblock
2399 ? likely(mutex_trylock(mutex)) ? 0 : -EAGAIN
2400 : mutex_lock_interruptible(mutex);
2401}
2402
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002403static char *ffs_prepare_buffer(const char * __user buf, size_t len)
2404{
2405 char *data;
2406
2407 if (unlikely(!len))
2408 return NULL;
2409
2410 data = kmalloc(len, GFP_KERNEL);
2411 if (unlikely(!data))
2412 return ERR_PTR(-ENOMEM);
2413
2414 if (unlikely(__copy_from_user(data, buf, len))) {
2415 kfree(data);
2416 return ERR_PTR(-EFAULT);
2417 }
2418
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002419 pr_vdebug("Buffer from user space:\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002420 ffs_dump_mem("", data, len);
2421
2422 return data;
2423}