blob: 1b7ae7c9d6834d9857e90eda0107f726bf2e1639 [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
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020044#ifdef VERBOSE_DEBUG
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +010045# define pr_vdebug pr_debug
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020046# define ffs_dump_mem(prefix, ptr, len) \
Michal Nazarewiczaa02f172010-11-17 17:09:47 +010047 print_hex_dump_bytes(pr_fmt(prefix ": "), DUMP_PREFIX_NONE, ptr, len)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020048#else
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +010049# define pr_vdebug(...) do { } while (0)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020050# define ffs_dump_mem(prefix, ptr, len) do { } while (0)
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +010051#endif /* VERBOSE_DEBUG */
52
Michal Nazarewiczaa02f172010-11-17 17:09:47 +010053#define ENTER() pr_vdebug("%s()\n", __func__)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020054
55
56/* The data structure and setup file ****************************************/
57
58enum ffs_state {
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010059 /*
60 * Waiting for descriptors and strings.
61 *
62 * In this state no open(2), read(2) or write(2) on epfiles
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020063 * may succeed (which should not be the problem as there
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010064 * should be no such files opened in the first place).
65 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020066 FFS_READ_DESCRIPTORS,
67 FFS_READ_STRINGS,
68
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010069 /*
70 * We've got descriptors and strings. We are or have called
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020071 * functionfs_ready_callback(). functionfs_bind() may have
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010072 * been called but we don't know.
73 *
74 * This is the only state in which operations on epfiles may
75 * succeed.
76 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020077 FFS_ACTIVE,
78
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010079 /*
80 * All endpoints have been closed. This state is also set if
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020081 * we encounter an unrecoverable error. The only
82 * unrecoverable error is situation when after reading strings
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010083 * from user space we fail to initialise epfiles or
84 * functionfs_ready_callback() returns with error (<0).
85 *
86 * In this state no open(2), read(2) or write(2) (both on ep0
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020087 * as well as epfile) may succeed (at this point epfiles are
88 * unlinked and all closed so this is not a problem; ep0 is
89 * also closed but ep0 file exists and so open(2) on ep0 must
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010090 * fail).
91 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020092 FFS_CLOSING
93};
94
95
96enum ffs_setup_state {
97 /* There is no setup request pending. */
98 FFS_NO_SETUP,
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010099 /*
100 * User has read events and there was a setup request event
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200101 * there. The next read/write on ep0 will handle the
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100102 * request.
103 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200104 FFS_SETUP_PENDING,
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100105 /*
106 * There was event pending but before user space handled it
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200107 * some other event was introduced which canceled existing
108 * setup. If this state is set read/write on ep0 return
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100109 * -EIDRM. This state is only set when adding event.
110 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200111 FFS_SETUP_CANCELED
112};
113
114
115
116struct ffs_epfile;
117struct ffs_function;
118
119struct ffs_data {
120 struct usb_gadget *gadget;
121
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100122 /*
123 * Protect access read/write operations, only one read/write
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200124 * at a time. As a consequence protects ep0req and company.
125 * While setup request is being processed (queued) this is
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100126 * held.
127 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200128 struct mutex mutex;
129
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100130 /*
131 * Protect access to endpoint related structures (basically
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200132 * usb_ep_queue(), usb_ep_dequeue(), etc. calls) except for
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100133 * endpoint zero.
134 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200135 spinlock_t eps_lock;
136
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100137 /*
138 * XXX REVISIT do we need our own request? Since we are not
139 * handling setup requests immediately user space may be so
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200140 * slow that another setup will be sent to the gadget but this
141 * time not to us but another function and then there could be
Linus Torvaldsa4ce96a2010-07-21 09:25:42 -0700142 * a race. Is that the case? Or maybe we can use cdev->req
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100143 * after all, maybe we just need some spinlock for that?
144 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200145 struct usb_request *ep0req; /* P: mutex */
146 struct completion ep0req_completion; /* P: mutex */
147 int ep0req_status; /* P: mutex */
148
149 /* reference counter */
150 atomic_t ref;
151 /* how many files are opened (EP0 and others) */
152 atomic_t opened;
153
154 /* EP0 state */
155 enum ffs_state state;
156
157 /*
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100158 * Possible transitions:
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200159 * + FFS_NO_SETUP -> FFS_SETUP_PENDING -- P: ev.waitq.lock
160 * happens only in ep0 read which is P: mutex
161 * + FFS_SETUP_PENDING -> FFS_NO_SETUP -- P: ev.waitq.lock
162 * happens only in ep0 i/o which is P: mutex
163 * + FFS_SETUP_PENDING -> FFS_SETUP_CANCELED -- P: ev.waitq.lock
164 * + FFS_SETUP_CANCELED -> FFS_NO_SETUP -- cmpxchg
165 */
166 enum ffs_setup_state setup_state;
167
168#define FFS_SETUP_STATE(ffs) \
169 ((enum ffs_setup_state)cmpxchg(&(ffs)->setup_state, \
170 FFS_SETUP_CANCELED, FFS_NO_SETUP))
171
172 /* Events & such. */
173 struct {
174 u8 types[4];
175 unsigned short count;
176 /* XXX REVISIT need to update it in some places, or do we? */
177 unsigned short can_stall;
178 struct usb_ctrlrequest setup;
179
180 wait_queue_head_t waitq;
181 } ev; /* the whole structure, P: ev.waitq.lock */
182
183 /* Flags */
184 unsigned long flags;
185#define FFS_FL_CALL_CLOSED_CALLBACK 0
186#define FFS_FL_BOUND 1
187
188 /* Active function */
189 struct ffs_function *func;
190
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100191 /*
192 * Device name, write once when file system is mounted.
193 * Intended for user to read if she wants.
194 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200195 const char *dev_name;
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100196 /* Private data for our user (ie. gadget). Managed by user. */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200197 void *private_data;
198
199 /* filled by __ffs_data_got_descs() */
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100200 /*
201 * Real descriptors are 16 bytes after raw_descs (so you need
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200202 * to skip 16 bytes (ie. ffs->raw_descs + 16) to get to the
203 * first full speed descriptor). raw_descs_length and
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100204 * raw_fs_descs_length do not have those 16 bytes added.
205 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200206 const void *raw_descs;
207 unsigned raw_descs_length;
208 unsigned raw_fs_descs_length;
209 unsigned fs_descs_count;
210 unsigned hs_descs_count;
211
212 unsigned short strings_count;
213 unsigned short interfaces_count;
214 unsigned short eps_count;
215 unsigned short _pad1;
216
217 /* filled by __ffs_data_got_strings() */
218 /* ids in stringtabs are set in functionfs_bind() */
219 const void *raw_strings;
220 struct usb_gadget_strings **stringtabs;
221
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100222 /*
223 * File system's super block, write once when file system is
224 * mounted.
225 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200226 struct super_block *sb;
227
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100228 /* File permissions, written once when fs is mounted */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200229 struct ffs_file_perms {
230 umode_t mode;
231 uid_t uid;
232 gid_t gid;
233 } file_perms;
234
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100235 /*
236 * The endpoint files, filled by ffs_epfiles_create(),
237 * destroyed by ffs_epfiles_destroy().
238 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200239 struct ffs_epfile *epfiles;
240};
241
242/* Reference counter handling */
243static void ffs_data_get(struct ffs_data *ffs);
244static void ffs_data_put(struct ffs_data *ffs);
245/* Creates new ffs_data object. */
246static struct ffs_data *__must_check ffs_data_new(void) __attribute__((malloc));
247
248/* Opened counter handling. */
249static void ffs_data_opened(struct ffs_data *ffs);
250static void ffs_data_closed(struct ffs_data *ffs);
251
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100252/* Called with ffs->mutex held; take over ownership of data. */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200253static int __must_check
254__ffs_data_got_descs(struct ffs_data *ffs, char *data, size_t len);
255static int __must_check
256__ffs_data_got_strings(struct ffs_data *ffs, char *data, size_t len);
257
258
259/* The function structure ***************************************************/
260
261struct ffs_ep;
262
263struct ffs_function {
264 struct usb_configuration *conf;
265 struct usb_gadget *gadget;
266 struct ffs_data *ffs;
267
268 struct ffs_ep *eps;
269 u8 eps_revmap[16];
270 short *interfaces_nums;
271
272 struct usb_function function;
273};
274
275
276static struct ffs_function *ffs_func_from_usb(struct usb_function *f)
277{
278 return container_of(f, struct ffs_function, function);
279}
280
281static void ffs_func_free(struct ffs_function *func);
282
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200283static void ffs_func_eps_disable(struct ffs_function *func);
284static int __must_check ffs_func_eps_enable(struct ffs_function *func);
285
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200286static int ffs_func_bind(struct usb_configuration *,
287 struct usb_function *);
288static void ffs_func_unbind(struct usb_configuration *,
289 struct usb_function *);
290static int ffs_func_set_alt(struct usb_function *, unsigned, unsigned);
291static void ffs_func_disable(struct usb_function *);
292static int ffs_func_setup(struct usb_function *,
293 const struct usb_ctrlrequest *);
294static void ffs_func_suspend(struct usb_function *);
295static void ffs_func_resume(struct usb_function *);
296
297
298static int ffs_func_revmap_ep(struct ffs_function *func, u8 num);
299static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf);
300
301
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200302/* The endpoints structures *************************************************/
303
304struct ffs_ep {
305 struct usb_ep *ep; /* P: ffs->eps_lock */
306 struct usb_request *req; /* P: epfile->mutex */
307
308 /* [0]: full speed, [1]: high speed */
309 struct usb_endpoint_descriptor *descs[2];
310
311 u8 num;
312
313 int status; /* P: epfile->mutex */
314};
315
316struct ffs_epfile {
317 /* Protects ep->ep and ep->req. */
318 struct mutex mutex;
319 wait_queue_head_t wait;
320
321 struct ffs_data *ffs;
322 struct ffs_ep *ep; /* P: ffs->eps_lock */
323
324 struct dentry *dentry;
325
326 char name[5];
327
328 unsigned char in; /* P: ffs->eps_lock */
329 unsigned char isoc; /* P: ffs->eps_lock */
330
331 unsigned char _pad;
332};
333
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200334static int __must_check ffs_epfiles_create(struct ffs_data *ffs);
335static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count);
336
337static struct inode *__must_check
338ffs_sb_create_file(struct super_block *sb, const char *name, void *data,
339 const struct file_operations *fops,
340 struct dentry **dentry_p);
341
342
343/* Misc helper functions ****************************************************/
344
345static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
346 __attribute__((warn_unused_result, nonnull));
347static char *ffs_prepare_buffer(const char * __user buf, size_t len)
348 __attribute__((warn_unused_result, nonnull));
349
350
351/* Control file aka ep0 *****************************************************/
352
353static void ffs_ep0_complete(struct usb_ep *ep, struct usb_request *req)
354{
355 struct ffs_data *ffs = req->context;
356
357 complete_all(&ffs->ep0req_completion);
358}
359
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200360static int __ffs_ep0_queue_wait(struct ffs_data *ffs, char *data, size_t len)
361{
362 struct usb_request *req = ffs->ep0req;
363 int ret;
364
365 req->zero = len < le16_to_cpu(ffs->ev.setup.wLength);
366
367 spin_unlock_irq(&ffs->ev.waitq.lock);
368
369 req->buf = data;
370 req->length = len;
371
372 INIT_COMPLETION(ffs->ep0req_completion);
373
374 ret = usb_ep_queue(ffs->gadget->ep0, req, GFP_ATOMIC);
375 if (unlikely(ret < 0))
376 return ret;
377
378 ret = wait_for_completion_interruptible(&ffs->ep0req_completion);
379 if (unlikely(ret)) {
380 usb_ep_dequeue(ffs->gadget->ep0, req);
381 return -EINTR;
382 }
383
384 ffs->setup_state = FFS_NO_SETUP;
385 return ffs->ep0req_status;
386}
387
388static int __ffs_ep0_stall(struct ffs_data *ffs)
389{
390 if (ffs->ev.can_stall) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100391 pr_vdebug("ep0 stall\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200392 usb_ep_set_halt(ffs->gadget->ep0);
393 ffs->setup_state = FFS_NO_SETUP;
394 return -EL2HLT;
395 } else {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100396 pr_debug("bogus ep0 stall!\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200397 return -ESRCH;
398 }
399}
400
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200401static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
402 size_t len, loff_t *ptr)
403{
404 struct ffs_data *ffs = file->private_data;
405 ssize_t ret;
406 char *data;
407
408 ENTER();
409
410 /* Fast check if setup was canceled */
411 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED)
412 return -EIDRM;
413
414 /* Acquire mutex */
415 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
416 if (unlikely(ret < 0))
417 return ret;
418
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200419 /* Check state */
420 switch (ffs->state) {
421 case FFS_READ_DESCRIPTORS:
422 case FFS_READ_STRINGS:
423 /* Copy data */
424 if (unlikely(len < 16)) {
425 ret = -EINVAL;
426 break;
427 }
428
429 data = ffs_prepare_buffer(buf, len);
Tobias Klauser537baab2010-12-09 15:52:39 +0100430 if (IS_ERR(data)) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200431 ret = PTR_ERR(data);
432 break;
433 }
434
435 /* Handle data */
436 if (ffs->state == FFS_READ_DESCRIPTORS) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100437 pr_info("read descriptors\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200438 ret = __ffs_data_got_descs(ffs, data, len);
439 if (unlikely(ret < 0))
440 break;
441
442 ffs->state = FFS_READ_STRINGS;
443 ret = len;
444 } else {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100445 pr_info("read strings\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200446 ret = __ffs_data_got_strings(ffs, data, len);
447 if (unlikely(ret < 0))
448 break;
449
450 ret = ffs_epfiles_create(ffs);
451 if (unlikely(ret)) {
452 ffs->state = FFS_CLOSING;
453 break;
454 }
455
456 ffs->state = FFS_ACTIVE;
457 mutex_unlock(&ffs->mutex);
458
459 ret = functionfs_ready_callback(ffs);
460 if (unlikely(ret < 0)) {
461 ffs->state = FFS_CLOSING;
462 return ret;
463 }
464
465 set_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags);
466 return len;
467 }
468 break;
469
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200470 case FFS_ACTIVE:
471 data = NULL;
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100472 /*
473 * We're called from user space, we can use _irq
474 * rather then _irqsave
475 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200476 spin_lock_irq(&ffs->ev.waitq.lock);
477 switch (FFS_SETUP_STATE(ffs)) {
478 case FFS_SETUP_CANCELED:
479 ret = -EIDRM;
480 goto done_spin;
481
482 case FFS_NO_SETUP:
483 ret = -ESRCH;
484 goto done_spin;
485
486 case FFS_SETUP_PENDING:
487 break;
488 }
489
490 /* FFS_SETUP_PENDING */
491 if (!(ffs->ev.setup.bRequestType & USB_DIR_IN)) {
492 spin_unlock_irq(&ffs->ev.waitq.lock);
493 ret = __ffs_ep0_stall(ffs);
494 break;
495 }
496
497 /* FFS_SETUP_PENDING and not stall */
498 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
499
500 spin_unlock_irq(&ffs->ev.waitq.lock);
501
502 data = ffs_prepare_buffer(buf, len);
Tobias Klauser537baab2010-12-09 15:52:39 +0100503 if (IS_ERR(data)) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200504 ret = PTR_ERR(data);
505 break;
506 }
507
508 spin_lock_irq(&ffs->ev.waitq.lock);
509
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100510 /*
511 * We are guaranteed to be still in FFS_ACTIVE state
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200512 * but the state of setup could have changed from
513 * FFS_SETUP_PENDING to FFS_SETUP_CANCELED so we need
514 * to check for that. If that happened we copied data
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100515 * from user space in vain but it's unlikely.
516 *
517 * For sure we are not in FFS_NO_SETUP since this is
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200518 * the only place FFS_SETUP_PENDING -> FFS_NO_SETUP
519 * transition can be performed and it's protected by
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100520 * mutex.
521 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200522 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) {
523 ret = -EIDRM;
524done_spin:
525 spin_unlock_irq(&ffs->ev.waitq.lock);
526 } else {
527 /* unlocks spinlock */
528 ret = __ffs_ep0_queue_wait(ffs, data, len);
529 }
530 kfree(data);
531 break;
532
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200533 default:
534 ret = -EBADFD;
535 break;
536 }
537
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200538 mutex_unlock(&ffs->mutex);
539 return ret;
540}
541
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200542static ssize_t __ffs_ep0_read_events(struct ffs_data *ffs, char __user *buf,
543 size_t n)
544{
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100545 /*
546 * We are holding ffs->ev.waitq.lock and ffs->mutex and we need
547 * to release them.
548 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200549 struct usb_functionfs_event events[n];
550 unsigned i = 0;
551
552 memset(events, 0, sizeof events);
553
554 do {
555 events[i].type = ffs->ev.types[i];
556 if (events[i].type == FUNCTIONFS_SETUP) {
557 events[i].u.setup = ffs->ev.setup;
558 ffs->setup_state = FFS_SETUP_PENDING;
559 }
560 } while (++i < n);
561
562 if (n < ffs->ev.count) {
563 ffs->ev.count -= n;
564 memmove(ffs->ev.types, ffs->ev.types + n,
565 ffs->ev.count * sizeof *ffs->ev.types);
566 } else {
567 ffs->ev.count = 0;
568 }
569
570 spin_unlock_irq(&ffs->ev.waitq.lock);
571 mutex_unlock(&ffs->mutex);
572
573 return unlikely(__copy_to_user(buf, events, sizeof events))
574 ? -EFAULT : sizeof events;
575}
576
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200577static ssize_t ffs_ep0_read(struct file *file, char __user *buf,
578 size_t len, loff_t *ptr)
579{
580 struct ffs_data *ffs = file->private_data;
581 char *data = NULL;
582 size_t n;
583 int ret;
584
585 ENTER();
586
587 /* Fast check if setup was canceled */
588 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED)
589 return -EIDRM;
590
591 /* Acquire mutex */
592 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
593 if (unlikely(ret < 0))
594 return ret;
595
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200596 /* Check state */
597 if (ffs->state != FFS_ACTIVE) {
598 ret = -EBADFD;
599 goto done_mutex;
600 }
601
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100602 /*
603 * We're called from user space, we can use _irq rather then
604 * _irqsave
605 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200606 spin_lock_irq(&ffs->ev.waitq.lock);
607
608 switch (FFS_SETUP_STATE(ffs)) {
609 case FFS_SETUP_CANCELED:
610 ret = -EIDRM;
611 break;
612
613 case FFS_NO_SETUP:
614 n = len / sizeof(struct usb_functionfs_event);
615 if (unlikely(!n)) {
616 ret = -EINVAL;
617 break;
618 }
619
620 if ((file->f_flags & O_NONBLOCK) && !ffs->ev.count) {
621 ret = -EAGAIN;
622 break;
623 }
624
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100625 if (wait_event_interruptible_exclusive_locked_irq(ffs->ev.waitq,
626 ffs->ev.count)) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200627 ret = -EINTR;
628 break;
629 }
630
631 return __ffs_ep0_read_events(ffs, buf,
632 min(n, (size_t)ffs->ev.count));
633
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200634 case FFS_SETUP_PENDING:
635 if (ffs->ev.setup.bRequestType & USB_DIR_IN) {
636 spin_unlock_irq(&ffs->ev.waitq.lock);
637 ret = __ffs_ep0_stall(ffs);
638 goto done_mutex;
639 }
640
641 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
642
643 spin_unlock_irq(&ffs->ev.waitq.lock);
644
645 if (likely(len)) {
646 data = kmalloc(len, GFP_KERNEL);
647 if (unlikely(!data)) {
648 ret = -ENOMEM;
649 goto done_mutex;
650 }
651 }
652
653 spin_lock_irq(&ffs->ev.waitq.lock);
654
655 /* See ffs_ep0_write() */
656 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) {
657 ret = -EIDRM;
658 break;
659 }
660
661 /* unlocks spinlock */
662 ret = __ffs_ep0_queue_wait(ffs, data, len);
663 if (likely(ret > 0) && unlikely(__copy_to_user(buf, data, len)))
664 ret = -EFAULT;
665 goto done_mutex;
666
667 default:
668 ret = -EBADFD;
669 break;
670 }
671
672 spin_unlock_irq(&ffs->ev.waitq.lock);
673done_mutex:
674 mutex_unlock(&ffs->mutex);
675 kfree(data);
676 return ret;
677}
678
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200679static int ffs_ep0_open(struct inode *inode, struct file *file)
680{
681 struct ffs_data *ffs = inode->i_private;
682
683 ENTER();
684
685 if (unlikely(ffs->state == FFS_CLOSING))
686 return -EBUSY;
687
688 file->private_data = ffs;
689 ffs_data_opened(ffs);
690
691 return 0;
692}
693
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200694static int ffs_ep0_release(struct inode *inode, struct file *file)
695{
696 struct ffs_data *ffs = file->private_data;
697
698 ENTER();
699
700 ffs_data_closed(ffs);
701
702 return 0;
703}
704
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200705static long ffs_ep0_ioctl(struct file *file, unsigned code, unsigned long value)
706{
707 struct ffs_data *ffs = file->private_data;
708 struct usb_gadget *gadget = ffs->gadget;
709 long ret;
710
711 ENTER();
712
713 if (code == FUNCTIONFS_INTERFACE_REVMAP) {
714 struct ffs_function *func = ffs->func;
715 ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV;
716 } else if (gadget->ops->ioctl) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200717 ret = gadget->ops->ioctl(gadget, code, value);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200718 } else {
719 ret = -ENOTTY;
720 }
721
722 return ret;
723}
724
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200725static const struct file_operations ffs_ep0_operations = {
726 .owner = THIS_MODULE,
727 .llseek = no_llseek,
728
729 .open = ffs_ep0_open,
730 .write = ffs_ep0_write,
731 .read = ffs_ep0_read,
732 .release = ffs_ep0_release,
733 .unlocked_ioctl = ffs_ep0_ioctl,
734};
735
736
737/* "Normal" endpoints operations ********************************************/
738
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200739static void ffs_epfile_io_complete(struct usb_ep *_ep, struct usb_request *req)
740{
741 ENTER();
742 if (likely(req->context)) {
743 struct ffs_ep *ep = _ep->driver_data;
744 ep->status = req->status ? req->status : req->actual;
745 complete(req->context);
746 }
747}
748
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200749static ssize_t ffs_epfile_io(struct file *file,
750 char __user *buf, size_t len, int read)
751{
752 struct ffs_epfile *epfile = file->private_data;
753 struct ffs_ep *ep;
754 char *data = NULL;
755 ssize_t ret;
756 int halt;
757
758 goto first_try;
759 do {
760 spin_unlock_irq(&epfile->ffs->eps_lock);
761 mutex_unlock(&epfile->mutex);
762
763first_try:
764 /* Are we still active? */
765 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE)) {
766 ret = -ENODEV;
767 goto error;
768 }
769
770 /* Wait for endpoint to be enabled */
771 ep = epfile->ep;
772 if (!ep) {
773 if (file->f_flags & O_NONBLOCK) {
774 ret = -EAGAIN;
775 goto error;
776 }
777
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100778 if (wait_event_interruptible(epfile->wait,
779 (ep = epfile->ep))) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200780 ret = -EINTR;
781 goto error;
782 }
783 }
784
785 /* Do we halt? */
786 halt = !read == !epfile->in;
787 if (halt && epfile->isoc) {
788 ret = -EINVAL;
789 goto error;
790 }
791
792 /* Allocate & copy */
793 if (!halt && !data) {
794 data = kzalloc(len, GFP_KERNEL);
795 if (unlikely(!data))
796 return -ENOMEM;
797
798 if (!read &&
799 unlikely(__copy_from_user(data, buf, len))) {
800 ret = -EFAULT;
801 goto error;
802 }
803 }
804
805 /* We will be using request */
806 ret = ffs_mutex_lock(&epfile->mutex,
807 file->f_flags & O_NONBLOCK);
808 if (unlikely(ret))
809 goto error;
810
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100811 /*
812 * We're called from user space, we can use _irq rather then
813 * _irqsave
814 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200815 spin_lock_irq(&epfile->ffs->eps_lock);
816
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100817 /*
818 * While we were acquiring mutex endpoint got disabled
819 * or changed?
820 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200821 } while (unlikely(epfile->ep != ep));
822
823 /* Halt */
824 if (unlikely(halt)) {
825 if (likely(epfile->ep == ep) && !WARN_ON(!ep->ep))
826 usb_ep_set_halt(ep->ep);
827 spin_unlock_irq(&epfile->ffs->eps_lock);
828 ret = -EBADMSG;
829 } else {
830 /* Fire the request */
831 DECLARE_COMPLETION_ONSTACK(done);
832
833 struct usb_request *req = ep->req;
834 req->context = &done;
835 req->complete = ffs_epfile_io_complete;
836 req->buf = data;
837 req->length = len;
838
839 ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
840
841 spin_unlock_irq(&epfile->ffs->eps_lock);
842
843 if (unlikely(ret < 0)) {
844 /* nop */
845 } else if (unlikely(wait_for_completion_interruptible(&done))) {
846 ret = -EINTR;
847 usb_ep_dequeue(ep->ep, req);
848 } else {
849 ret = ep->status;
850 if (read && ret > 0 &&
851 unlikely(copy_to_user(buf, data, ret)))
852 ret = -EFAULT;
853 }
854 }
855
856 mutex_unlock(&epfile->mutex);
857error:
858 kfree(data);
859 return ret;
860}
861
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200862static ssize_t
863ffs_epfile_write(struct file *file, const char __user *buf, size_t len,
864 loff_t *ptr)
865{
866 ENTER();
867
868 return ffs_epfile_io(file, (char __user *)buf, len, 0);
869}
870
871static ssize_t
872ffs_epfile_read(struct file *file, char __user *buf, size_t len, loff_t *ptr)
873{
874 ENTER();
875
876 return ffs_epfile_io(file, buf, len, 1);
877}
878
879static int
880ffs_epfile_open(struct inode *inode, struct file *file)
881{
882 struct ffs_epfile *epfile = inode->i_private;
883
884 ENTER();
885
886 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
887 return -ENODEV;
888
889 file->private_data = epfile;
890 ffs_data_opened(epfile->ffs);
891
892 return 0;
893}
894
895static int
896ffs_epfile_release(struct inode *inode, struct file *file)
897{
898 struct ffs_epfile *epfile = inode->i_private;
899
900 ENTER();
901
902 ffs_data_closed(epfile->ffs);
903
904 return 0;
905}
906
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200907static long ffs_epfile_ioctl(struct file *file, unsigned code,
908 unsigned long value)
909{
910 struct ffs_epfile *epfile = file->private_data;
911 int ret;
912
913 ENTER();
914
915 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
916 return -ENODEV;
917
918 spin_lock_irq(&epfile->ffs->eps_lock);
919 if (likely(epfile->ep)) {
920 switch (code) {
921 case FUNCTIONFS_FIFO_STATUS:
922 ret = usb_ep_fifo_status(epfile->ep->ep);
923 break;
924 case FUNCTIONFS_FIFO_FLUSH:
925 usb_ep_fifo_flush(epfile->ep->ep);
926 ret = 0;
927 break;
928 case FUNCTIONFS_CLEAR_HALT:
929 ret = usb_ep_clear_halt(epfile->ep->ep);
930 break;
931 case FUNCTIONFS_ENDPOINT_REVMAP:
932 ret = epfile->ep->num;
933 break;
934 default:
935 ret = -ENOTTY;
936 }
937 } else {
938 ret = -ENODEV;
939 }
940 spin_unlock_irq(&epfile->ffs->eps_lock);
941
942 return ret;
943}
944
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200945static const struct file_operations ffs_epfile_operations = {
946 .owner = THIS_MODULE,
947 .llseek = no_llseek,
948
949 .open = ffs_epfile_open,
950 .write = ffs_epfile_write,
951 .read = ffs_epfile_read,
952 .release = ffs_epfile_release,
953 .unlocked_ioctl = ffs_epfile_ioctl,
954};
955
956
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200957/* File system and super block operations ***********************************/
958
959/*
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100960 * Mounting the file system creates a controller file, used first for
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200961 * function configuration then later for event monitoring.
962 */
963
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200964static struct inode *__must_check
965ffs_sb_make_inode(struct super_block *sb, void *data,
966 const struct file_operations *fops,
967 const struct inode_operations *iops,
968 struct ffs_file_perms *perms)
969{
970 struct inode *inode;
971
972 ENTER();
973
974 inode = new_inode(sb);
975
976 if (likely(inode)) {
977 struct timespec current_time = CURRENT_TIME;
978
Al Viro12ba8d12010-10-27 04:19:36 +0100979 inode->i_ino = get_next_ino();
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200980 inode->i_mode = perms->mode;
981 inode->i_uid = perms->uid;
982 inode->i_gid = perms->gid;
983 inode->i_atime = current_time;
984 inode->i_mtime = current_time;
985 inode->i_ctime = current_time;
986 inode->i_private = data;
987 if (fops)
988 inode->i_fop = fops;
989 if (iops)
990 inode->i_op = iops;
991 }
992
993 return inode;
994}
995
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200996/* Create "regular" file */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200997static struct inode *ffs_sb_create_file(struct super_block *sb,
998 const char *name, void *data,
999 const struct file_operations *fops,
1000 struct dentry **dentry_p)
1001{
1002 struct ffs_data *ffs = sb->s_fs_info;
1003 struct dentry *dentry;
1004 struct inode *inode;
1005
1006 ENTER();
1007
1008 dentry = d_alloc_name(sb->s_root, name);
1009 if (unlikely(!dentry))
1010 return NULL;
1011
1012 inode = ffs_sb_make_inode(sb, data, fops, NULL, &ffs->file_perms);
1013 if (unlikely(!inode)) {
1014 dput(dentry);
1015 return NULL;
1016 }
1017
1018 d_add(dentry, inode);
1019 if (dentry_p)
1020 *dentry_p = dentry;
1021
1022 return inode;
1023}
1024
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001025/* Super block */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001026static const struct super_operations ffs_sb_operations = {
1027 .statfs = simple_statfs,
1028 .drop_inode = generic_delete_inode,
1029};
1030
1031struct ffs_sb_fill_data {
1032 struct ffs_file_perms perms;
1033 umode_t root_mode;
1034 const char *dev_name;
1035};
1036
1037static int ffs_sb_fill(struct super_block *sb, void *_data, int silent)
1038{
1039 struct ffs_sb_fill_data *data = _data;
1040 struct inode *inode;
1041 struct dentry *d;
1042 struct ffs_data *ffs;
1043
1044 ENTER();
1045
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001046 /* Initialise data */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001047 ffs = ffs_data_new();
1048 if (unlikely(!ffs))
1049 goto enomem0;
1050
1051 ffs->sb = sb;
1052 ffs->dev_name = data->dev_name;
1053 ffs->file_perms = data->perms;
1054
1055 sb->s_fs_info = ffs;
1056 sb->s_blocksize = PAGE_CACHE_SIZE;
1057 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1058 sb->s_magic = FUNCTIONFS_MAGIC;
1059 sb->s_op = &ffs_sb_operations;
1060 sb->s_time_gran = 1;
1061
1062 /* Root inode */
1063 data->perms.mode = data->root_mode;
1064 inode = ffs_sb_make_inode(sb, NULL,
1065 &simple_dir_operations,
1066 &simple_dir_inode_operations,
1067 &data->perms);
1068 if (unlikely(!inode))
1069 goto enomem1;
1070 d = d_alloc_root(inode);
1071 if (unlikely(!d))
1072 goto enomem2;
1073 sb->s_root = d;
1074
1075 /* EP0 file */
1076 if (unlikely(!ffs_sb_create_file(sb, "ep0", ffs,
1077 &ffs_ep0_operations, NULL)))
1078 goto enomem3;
1079
1080 return 0;
1081
1082enomem3:
1083 dput(d);
1084enomem2:
1085 iput(inode);
1086enomem1:
1087 ffs_data_put(ffs);
1088enomem0:
1089 return -ENOMEM;
1090}
1091
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001092static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts)
1093{
1094 ENTER();
1095
1096 if (!opts || !*opts)
1097 return 0;
1098
1099 for (;;) {
1100 char *end, *eq, *comma;
1101 unsigned long value;
1102
1103 /* Option limit */
1104 comma = strchr(opts, ',');
1105 if (comma)
1106 *comma = 0;
1107
1108 /* Value limit */
1109 eq = strchr(opts, '=');
1110 if (unlikely(!eq)) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001111 pr_err("'=' missing in %s\n", opts);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001112 return -EINVAL;
1113 }
1114 *eq = 0;
1115
1116 /* Parse value */
1117 value = simple_strtoul(eq + 1, &end, 0);
1118 if (unlikely(*end != ',' && *end != 0)) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001119 pr_err("%s: invalid value: %s\n", opts, eq + 1);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001120 return -EINVAL;
1121 }
1122
1123 /* Interpret option */
1124 switch (eq - opts) {
1125 case 5:
1126 if (!memcmp(opts, "rmode", 5))
1127 data->root_mode = (value & 0555) | S_IFDIR;
1128 else if (!memcmp(opts, "fmode", 5))
1129 data->perms.mode = (value & 0666) | S_IFREG;
1130 else
1131 goto invalid;
1132 break;
1133
1134 case 4:
1135 if (!memcmp(opts, "mode", 4)) {
1136 data->root_mode = (value & 0555) | S_IFDIR;
1137 data->perms.mode = (value & 0666) | S_IFREG;
1138 } else {
1139 goto invalid;
1140 }
1141 break;
1142
1143 case 3:
1144 if (!memcmp(opts, "uid", 3))
1145 data->perms.uid = value;
1146 else if (!memcmp(opts, "gid", 3))
1147 data->perms.gid = value;
1148 else
1149 goto invalid;
1150 break;
1151
1152 default:
1153invalid:
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001154 pr_err("%s: invalid option\n", opts);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001155 return -EINVAL;
1156 }
1157
1158 /* Next iteration */
1159 if (!comma)
1160 break;
1161 opts = comma + 1;
1162 }
1163
1164 return 0;
1165}
1166
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001167/* "mount -t functionfs dev_name /dev/function" ends up here */
1168
Al Virofc14f2f2010-07-25 01:48:30 +04001169static struct dentry *
1170ffs_fs_mount(struct file_system_type *t, int flags,
1171 const char *dev_name, void *opts)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001172{
1173 struct ffs_sb_fill_data data = {
1174 .perms = {
1175 .mode = S_IFREG | 0600,
1176 .uid = 0,
1177 .gid = 0
1178 },
1179 .root_mode = S_IFDIR | 0500,
1180 };
1181 int ret;
1182
1183 ENTER();
1184
1185 ret = functionfs_check_dev_callback(dev_name);
1186 if (unlikely(ret < 0))
Al Virofc14f2f2010-07-25 01:48:30 +04001187 return ERR_PTR(ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001188
1189 ret = ffs_fs_parse_opts(&data, opts);
1190 if (unlikely(ret < 0))
Al Virofc14f2f2010-07-25 01:48:30 +04001191 return ERR_PTR(ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001192
1193 data.dev_name = dev_name;
Al Virofc14f2f2010-07-25 01:48:30 +04001194 return mount_single(t, flags, &data, ffs_sb_fill);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001195}
1196
1197static void
1198ffs_fs_kill_sb(struct super_block *sb)
1199{
1200 void *ptr;
1201
1202 ENTER();
1203
1204 kill_litter_super(sb);
1205 ptr = xchg(&sb->s_fs_info, NULL);
1206 if (ptr)
1207 ffs_data_put(ptr);
1208}
1209
1210static struct file_system_type ffs_fs_type = {
1211 .owner = THIS_MODULE,
1212 .name = "functionfs",
Al Virofc14f2f2010-07-25 01:48:30 +04001213 .mount = ffs_fs_mount,
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001214 .kill_sb = ffs_fs_kill_sb,
1215};
1216
1217
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001218/* Driver's main init/cleanup functions *************************************/
1219
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001220static int functionfs_init(void)
1221{
1222 int ret;
1223
1224 ENTER();
1225
1226 ret = register_filesystem(&ffs_fs_type);
1227 if (likely(!ret))
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001228 pr_info("file system registered\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001229 else
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001230 pr_err("failed registering file system (%d)\n", ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001231
1232 return ret;
1233}
1234
1235static void functionfs_cleanup(void)
1236{
1237 ENTER();
1238
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001239 pr_info("unloading\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001240 unregister_filesystem(&ffs_fs_type);
1241}
1242
1243
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001244/* ffs_data and ffs_function construction and destruction code **************/
1245
1246static void ffs_data_clear(struct ffs_data *ffs);
1247static void ffs_data_reset(struct ffs_data *ffs);
1248
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001249static void ffs_data_get(struct ffs_data *ffs)
1250{
1251 ENTER();
1252
1253 atomic_inc(&ffs->ref);
1254}
1255
1256static void ffs_data_opened(struct ffs_data *ffs)
1257{
1258 ENTER();
1259
1260 atomic_inc(&ffs->ref);
1261 atomic_inc(&ffs->opened);
1262}
1263
1264static void ffs_data_put(struct ffs_data *ffs)
1265{
1266 ENTER();
1267
1268 if (unlikely(atomic_dec_and_test(&ffs->ref))) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001269 pr_info("%s(): freeing\n", __func__);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001270 ffs_data_clear(ffs);
1271 BUG_ON(mutex_is_locked(&ffs->mutex) ||
1272 spin_is_locked(&ffs->ev.waitq.lock) ||
1273 waitqueue_active(&ffs->ev.waitq) ||
1274 waitqueue_active(&ffs->ep0req_completion.wait));
1275 kfree(ffs);
1276 }
1277}
1278
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001279static void ffs_data_closed(struct ffs_data *ffs)
1280{
1281 ENTER();
1282
1283 if (atomic_dec_and_test(&ffs->opened)) {
1284 ffs->state = FFS_CLOSING;
1285 ffs_data_reset(ffs);
1286 }
1287
1288 ffs_data_put(ffs);
1289}
1290
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001291static struct ffs_data *ffs_data_new(void)
1292{
1293 struct ffs_data *ffs = kzalloc(sizeof *ffs, GFP_KERNEL);
1294 if (unlikely(!ffs))
1295 return 0;
1296
1297 ENTER();
1298
1299 atomic_set(&ffs->ref, 1);
1300 atomic_set(&ffs->opened, 0);
1301 ffs->state = FFS_READ_DESCRIPTORS;
1302 mutex_init(&ffs->mutex);
1303 spin_lock_init(&ffs->eps_lock);
1304 init_waitqueue_head(&ffs->ev.waitq);
1305 init_completion(&ffs->ep0req_completion);
1306
1307 /* XXX REVISIT need to update it in some places, or do we? */
1308 ffs->ev.can_stall = 1;
1309
1310 return ffs;
1311}
1312
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001313static void ffs_data_clear(struct ffs_data *ffs)
1314{
1315 ENTER();
1316
1317 if (test_and_clear_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags))
1318 functionfs_closed_callback(ffs);
1319
1320 BUG_ON(ffs->gadget);
1321
1322 if (ffs->epfiles)
1323 ffs_epfiles_destroy(ffs->epfiles, ffs->eps_count);
1324
1325 kfree(ffs->raw_descs);
1326 kfree(ffs->raw_strings);
1327 kfree(ffs->stringtabs);
1328}
1329
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001330static void ffs_data_reset(struct ffs_data *ffs)
1331{
1332 ENTER();
1333
1334 ffs_data_clear(ffs);
1335
1336 ffs->epfiles = NULL;
1337 ffs->raw_descs = NULL;
1338 ffs->raw_strings = NULL;
1339 ffs->stringtabs = NULL;
1340
1341 ffs->raw_descs_length = 0;
1342 ffs->raw_fs_descs_length = 0;
1343 ffs->fs_descs_count = 0;
1344 ffs->hs_descs_count = 0;
1345
1346 ffs->strings_count = 0;
1347 ffs->interfaces_count = 0;
1348 ffs->eps_count = 0;
1349
1350 ffs->ev.count = 0;
1351
1352 ffs->state = FFS_READ_DESCRIPTORS;
1353 ffs->setup_state = FFS_NO_SETUP;
1354 ffs->flags = 0;
1355}
1356
1357
1358static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev)
1359{
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001360 struct usb_gadget_strings **lang;
1361 int first_id;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001362
1363 ENTER();
1364
1365 if (WARN_ON(ffs->state != FFS_ACTIVE
1366 || test_and_set_bit(FFS_FL_BOUND, &ffs->flags)))
1367 return -EBADFD;
1368
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001369 first_id = usb_string_ids_n(cdev, ffs->strings_count);
1370 if (unlikely(first_id < 0))
1371 return first_id;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001372
1373 ffs->ep0req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
1374 if (unlikely(!ffs->ep0req))
1375 return -ENOMEM;
1376 ffs->ep0req->complete = ffs_ep0_complete;
1377 ffs->ep0req->context = ffs;
1378
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001379 lang = ffs->stringtabs;
1380 for (lang = ffs->stringtabs; *lang; ++lang) {
1381 struct usb_string *str = (*lang)->strings;
1382 int id = first_id;
1383 for (; str->s; ++id, ++str)
1384 str->id = id;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001385 }
1386
1387 ffs->gadget = cdev->gadget;
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001388 ffs_data_get(ffs);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001389 return 0;
1390}
1391
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001392static void functionfs_unbind(struct ffs_data *ffs)
1393{
1394 ENTER();
1395
1396 if (!WARN_ON(!ffs->gadget)) {
1397 usb_ep_free_request(ffs->gadget->ep0, ffs->ep0req);
1398 ffs->ep0req = NULL;
1399 ffs->gadget = NULL;
1400 ffs_data_put(ffs);
1401 }
1402}
1403
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001404static int ffs_epfiles_create(struct ffs_data *ffs)
1405{
1406 struct ffs_epfile *epfile, *epfiles;
1407 unsigned i, count;
1408
1409 ENTER();
1410
1411 count = ffs->eps_count;
1412 epfiles = kzalloc(count * sizeof *epfiles, GFP_KERNEL);
1413 if (!epfiles)
1414 return -ENOMEM;
1415
1416 epfile = epfiles;
1417 for (i = 1; i <= count; ++i, ++epfile) {
1418 epfile->ffs = ffs;
1419 mutex_init(&epfile->mutex);
1420 init_waitqueue_head(&epfile->wait);
1421 sprintf(epfiles->name, "ep%u", i);
1422 if (!unlikely(ffs_sb_create_file(ffs->sb, epfiles->name, epfile,
1423 &ffs_epfile_operations,
1424 &epfile->dentry))) {
1425 ffs_epfiles_destroy(epfiles, i - 1);
1426 return -ENOMEM;
1427 }
1428 }
1429
1430 ffs->epfiles = epfiles;
1431 return 0;
1432}
1433
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001434static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count)
1435{
1436 struct ffs_epfile *epfile = epfiles;
1437
1438 ENTER();
1439
1440 for (; count; --count, ++epfile) {
1441 BUG_ON(mutex_is_locked(&epfile->mutex) ||
1442 waitqueue_active(&epfile->wait));
1443 if (epfile->dentry) {
1444 d_delete(epfile->dentry);
1445 dput(epfile->dentry);
1446 epfile->dentry = NULL;
1447 }
1448 }
1449
1450 kfree(epfiles);
1451}
1452
Michal Nazarewicz7898aee2010-06-16 12:07:58 +02001453static int functionfs_bind_config(struct usb_composite_dev *cdev,
1454 struct usb_configuration *c,
1455 struct ffs_data *ffs)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001456{
1457 struct ffs_function *func;
1458 int ret;
1459
1460 ENTER();
1461
1462 func = kzalloc(sizeof *func, GFP_KERNEL);
1463 if (unlikely(!func))
1464 return -ENOMEM;
1465
1466 func->function.name = "Function FS Gadget";
1467 func->function.strings = ffs->stringtabs;
1468
1469 func->function.bind = ffs_func_bind;
1470 func->function.unbind = ffs_func_unbind;
1471 func->function.set_alt = ffs_func_set_alt;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001472 func->function.disable = ffs_func_disable;
1473 func->function.setup = ffs_func_setup;
1474 func->function.suspend = ffs_func_suspend;
1475 func->function.resume = ffs_func_resume;
1476
1477 func->conf = c;
1478 func->gadget = cdev->gadget;
1479 func->ffs = ffs;
1480 ffs_data_get(ffs);
1481
1482 ret = usb_add_function(c, &func->function);
1483 if (unlikely(ret))
1484 ffs_func_free(func);
1485
1486 return ret;
1487}
1488
1489static void ffs_func_free(struct ffs_function *func)
1490{
1491 ENTER();
1492
1493 ffs_data_put(func->ffs);
1494
1495 kfree(func->eps);
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001496 /*
1497 * eps and interfaces_nums are allocated in the same chunk so
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001498 * only one free is required. Descriptors are also allocated
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001499 * in the same chunk.
1500 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001501
1502 kfree(func);
1503}
1504
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001505static void ffs_func_eps_disable(struct ffs_function *func)
1506{
1507 struct ffs_ep *ep = func->eps;
1508 struct ffs_epfile *epfile = func->ffs->epfiles;
1509 unsigned count = func->ffs->eps_count;
1510 unsigned long flags;
1511
1512 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1513 do {
1514 /* pending requests get nuked */
1515 if (likely(ep->ep))
1516 usb_ep_disable(ep->ep);
1517 epfile->ep = NULL;
1518
1519 ++ep;
1520 ++epfile;
1521 } while (--count);
1522 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1523}
1524
1525static int ffs_func_eps_enable(struct ffs_function *func)
1526{
1527 struct ffs_data *ffs = func->ffs;
1528 struct ffs_ep *ep = func->eps;
1529 struct ffs_epfile *epfile = ffs->epfiles;
1530 unsigned count = ffs->eps_count;
1531 unsigned long flags;
1532 int ret = 0;
1533
1534 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1535 do {
1536 struct usb_endpoint_descriptor *ds;
1537 ds = ep->descs[ep->descs[1] ? 1 : 0];
1538
1539 ep->ep->driver_data = ep;
1540 ret = usb_ep_enable(ep->ep, ds);
1541 if (likely(!ret)) {
1542 epfile->ep = ep;
1543 epfile->in = usb_endpoint_dir_in(ds);
1544 epfile->isoc = usb_endpoint_xfer_isoc(ds);
1545 } else {
1546 break;
1547 }
1548
1549 wake_up(&epfile->wait);
1550
1551 ++ep;
1552 ++epfile;
1553 } while (--count);
1554 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1555
1556 return ret;
1557}
1558
1559
1560/* Parsing and building descriptors and strings *****************************/
1561
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001562/*
1563 * This validates if data pointed by data is a valid USB descriptor as
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001564 * well as record how many interfaces, endpoints and strings are
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001565 * required by given configuration. Returns address after the
1566 * descriptor or NULL if data is invalid.
1567 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001568
1569enum ffs_entity_type {
1570 FFS_DESCRIPTOR, FFS_INTERFACE, FFS_STRING, FFS_ENDPOINT
1571};
1572
1573typedef int (*ffs_entity_callback)(enum ffs_entity_type entity,
1574 u8 *valuep,
1575 struct usb_descriptor_header *desc,
1576 void *priv);
1577
1578static int __must_check ffs_do_desc(char *data, unsigned len,
1579 ffs_entity_callback entity, void *priv)
1580{
1581 struct usb_descriptor_header *_ds = (void *)data;
1582 u8 length;
1583 int ret;
1584
1585 ENTER();
1586
1587 /* At least two bytes are required: length and type */
1588 if (len < 2) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001589 pr_vdebug("descriptor too short\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001590 return -EINVAL;
1591 }
1592
1593 /* If we have at least as many bytes as the descriptor takes? */
1594 length = _ds->bLength;
1595 if (len < length) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001596 pr_vdebug("descriptor longer then available data\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001597 return -EINVAL;
1598 }
1599
1600#define __entity_check_INTERFACE(val) 1
1601#define __entity_check_STRING(val) (val)
1602#define __entity_check_ENDPOINT(val) ((val) & USB_ENDPOINT_NUMBER_MASK)
1603#define __entity(type, val) do { \
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001604 pr_vdebug("entity " #type "(%02x)\n", (val)); \
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001605 if (unlikely(!__entity_check_ ##type(val))) { \
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001606 pr_vdebug("invalid entity's value\n"); \
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001607 return -EINVAL; \
1608 } \
1609 ret = entity(FFS_ ##type, &val, _ds, priv); \
1610 if (unlikely(ret < 0)) { \
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001611 pr_debug("entity " #type "(%02x); ret = %d\n", \
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +01001612 (val), ret); \
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001613 return ret; \
1614 } \
1615 } while (0)
1616
1617 /* Parse descriptor depending on type. */
1618 switch (_ds->bDescriptorType) {
1619 case USB_DT_DEVICE:
1620 case USB_DT_CONFIG:
1621 case USB_DT_STRING:
1622 case USB_DT_DEVICE_QUALIFIER:
1623 /* function can't have any of those */
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001624 pr_vdebug("descriptor reserved for gadget: %d\n",
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001625 _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001626 return -EINVAL;
1627
1628 case USB_DT_INTERFACE: {
1629 struct usb_interface_descriptor *ds = (void *)_ds;
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001630 pr_vdebug("interface descriptor\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001631 if (length != sizeof *ds)
1632 goto inv_length;
1633
1634 __entity(INTERFACE, ds->bInterfaceNumber);
1635 if (ds->iInterface)
1636 __entity(STRING, ds->iInterface);
1637 }
1638 break;
1639
1640 case USB_DT_ENDPOINT: {
1641 struct usb_endpoint_descriptor *ds = (void *)_ds;
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001642 pr_vdebug("endpoint descriptor\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001643 if (length != USB_DT_ENDPOINT_SIZE &&
1644 length != USB_DT_ENDPOINT_AUDIO_SIZE)
1645 goto inv_length;
1646 __entity(ENDPOINT, ds->bEndpointAddress);
1647 }
1648 break;
1649
1650 case USB_DT_OTG:
1651 if (length != sizeof(struct usb_otg_descriptor))
1652 goto inv_length;
1653 break;
1654
1655 case USB_DT_INTERFACE_ASSOCIATION: {
1656 struct usb_interface_assoc_descriptor *ds = (void *)_ds;
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001657 pr_vdebug("interface association descriptor\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001658 if (length != sizeof *ds)
1659 goto inv_length;
1660 if (ds->iFunction)
1661 __entity(STRING, ds->iFunction);
1662 }
1663 break;
1664
1665 case USB_DT_OTHER_SPEED_CONFIG:
1666 case USB_DT_INTERFACE_POWER:
1667 case USB_DT_DEBUG:
1668 case USB_DT_SECURITY:
1669 case USB_DT_CS_RADIO_CONTROL:
1670 /* TODO */
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001671 pr_vdebug("unimplemented descriptor: %d\n", _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001672 return -EINVAL;
1673
1674 default:
1675 /* We should never be here */
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001676 pr_vdebug("unknown descriptor: %d\n", _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001677 return -EINVAL;
1678
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001679inv_length:
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001680 pr_vdebug("invalid length: %d (descriptor %d)\n",
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +01001681 _ds->bLength, _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001682 return -EINVAL;
1683 }
1684
1685#undef __entity
1686#undef __entity_check_DESCRIPTOR
1687#undef __entity_check_INTERFACE
1688#undef __entity_check_STRING
1689#undef __entity_check_ENDPOINT
1690
1691 return length;
1692}
1693
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001694static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len,
1695 ffs_entity_callback entity, void *priv)
1696{
1697 const unsigned _len = len;
1698 unsigned long num = 0;
1699
1700 ENTER();
1701
1702 for (;;) {
1703 int ret;
1704
1705 if (num == count)
1706 data = NULL;
1707
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001708 /* Record "descriptor" entity */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001709 ret = entity(FFS_DESCRIPTOR, (u8 *)num, (void *)data, priv);
1710 if (unlikely(ret < 0)) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001711 pr_debug("entity DESCRIPTOR(%02lx); ret = %d\n",
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +01001712 num, ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001713 return ret;
1714 }
1715
1716 if (!data)
1717 return _len - len;
1718
1719 ret = ffs_do_desc(data, len, entity, priv);
1720 if (unlikely(ret < 0)) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001721 pr_debug("%s returns %d\n", __func__, ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001722 return ret;
1723 }
1724
1725 len -= ret;
1726 data += ret;
1727 ++num;
1728 }
1729}
1730
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001731static int __ffs_data_do_entity(enum ffs_entity_type type,
1732 u8 *valuep, struct usb_descriptor_header *desc,
1733 void *priv)
1734{
1735 struct ffs_data *ffs = priv;
1736
1737 ENTER();
1738
1739 switch (type) {
1740 case FFS_DESCRIPTOR:
1741 break;
1742
1743 case FFS_INTERFACE:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001744 /*
1745 * Interfaces are indexed from zero so if we
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001746 * encountered interface "n" then there are at least
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001747 * "n+1" interfaces.
1748 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001749 if (*valuep >= ffs->interfaces_count)
1750 ffs->interfaces_count = *valuep + 1;
1751 break;
1752
1753 case FFS_STRING:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001754 /*
1755 * Strings are indexed from 1 (0 is magic ;) reserved
1756 * for languages list or some such)
1757 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001758 if (*valuep > ffs->strings_count)
1759 ffs->strings_count = *valuep;
1760 break;
1761
1762 case FFS_ENDPOINT:
1763 /* Endpoints are indexed from 1 as well. */
1764 if ((*valuep & USB_ENDPOINT_NUMBER_MASK) > ffs->eps_count)
1765 ffs->eps_count = (*valuep & USB_ENDPOINT_NUMBER_MASK);
1766 break;
1767 }
1768
1769 return 0;
1770}
1771
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001772static int __ffs_data_got_descs(struct ffs_data *ffs,
1773 char *const _data, size_t len)
1774{
1775 unsigned fs_count, hs_count;
1776 int fs_len, ret = -EINVAL;
1777 char *data = _data;
1778
1779 ENTER();
1780
1781 if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_DESCRIPTORS_MAGIC ||
1782 get_unaligned_le32(data + 4) != len))
1783 goto error;
1784 fs_count = get_unaligned_le32(data + 8);
1785 hs_count = get_unaligned_le32(data + 12);
1786
1787 if (!fs_count && !hs_count)
1788 goto einval;
1789
1790 data += 16;
1791 len -= 16;
1792
1793 if (likely(fs_count)) {
1794 fs_len = ffs_do_descs(fs_count, data, len,
1795 __ffs_data_do_entity, ffs);
1796 if (unlikely(fs_len < 0)) {
1797 ret = fs_len;
1798 goto error;
1799 }
1800
1801 data += fs_len;
1802 len -= fs_len;
1803 } else {
1804 fs_len = 0;
1805 }
1806
1807 if (likely(hs_count)) {
1808 ret = ffs_do_descs(hs_count, data, len,
1809 __ffs_data_do_entity, ffs);
1810 if (unlikely(ret < 0))
1811 goto error;
1812 } else {
1813 ret = 0;
1814 }
1815
1816 if (unlikely(len != ret))
1817 goto einval;
1818
1819 ffs->raw_fs_descs_length = fs_len;
1820 ffs->raw_descs_length = fs_len + ret;
1821 ffs->raw_descs = _data;
1822 ffs->fs_descs_count = fs_count;
1823 ffs->hs_descs_count = hs_count;
1824
1825 return 0;
1826
1827einval:
1828 ret = -EINVAL;
1829error:
1830 kfree(_data);
1831 return ret;
1832}
1833
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001834static int __ffs_data_got_strings(struct ffs_data *ffs,
1835 char *const _data, size_t len)
1836{
1837 u32 str_count, needed_count, lang_count;
1838 struct usb_gadget_strings **stringtabs, *t;
1839 struct usb_string *strings, *s;
1840 const char *data = _data;
1841
1842 ENTER();
1843
1844 if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_STRINGS_MAGIC ||
1845 get_unaligned_le32(data + 4) != len))
1846 goto error;
1847 str_count = get_unaligned_le32(data + 8);
1848 lang_count = get_unaligned_le32(data + 12);
1849
1850 /* if one is zero the other must be zero */
1851 if (unlikely(!str_count != !lang_count))
1852 goto error;
1853
1854 /* Do we have at least as many strings as descriptors need? */
1855 needed_count = ffs->strings_count;
1856 if (unlikely(str_count < needed_count))
1857 goto error;
1858
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001859 /*
1860 * If we don't need any strings just return and free all
1861 * memory.
1862 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001863 if (!needed_count) {
1864 kfree(_data);
1865 return 0;
1866 }
1867
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001868 /* Allocate everything in one chunk so there's less maintenance. */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001869 {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001870 struct {
1871 struct usb_gadget_strings *stringtabs[lang_count + 1];
1872 struct usb_gadget_strings stringtab[lang_count];
1873 struct usb_string strings[lang_count*(needed_count+1)];
1874 } *d;
1875 unsigned i = 0;
1876
1877 d = kmalloc(sizeof *d, GFP_KERNEL);
1878 if (unlikely(!d)) {
1879 kfree(_data);
1880 return -ENOMEM;
1881 }
1882
1883 stringtabs = d->stringtabs;
1884 t = d->stringtab;
1885 i = lang_count;
1886 do {
1887 *stringtabs++ = t++;
1888 } while (--i);
1889 *stringtabs = NULL;
1890
1891 stringtabs = d->stringtabs;
1892 t = d->stringtab;
1893 s = d->strings;
1894 strings = s;
1895 }
1896
1897 /* For each language */
1898 data += 16;
1899 len -= 16;
1900
1901 do { /* lang_count > 0 so we can use do-while */
1902 unsigned needed = needed_count;
1903
1904 if (unlikely(len < 3))
1905 goto error_free;
1906 t->language = get_unaligned_le16(data);
1907 t->strings = s;
1908 ++t;
1909
1910 data += 2;
1911 len -= 2;
1912
1913 /* For each string */
1914 do { /* str_count > 0 so we can use do-while */
1915 size_t length = strnlen(data, len);
1916
1917 if (unlikely(length == len))
1918 goto error_free;
1919
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001920 /*
1921 * User may provide more strings then we need,
1922 * if that's the case we simply ignore the
1923 * rest
1924 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001925 if (likely(needed)) {
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001926 /*
1927 * s->id will be set while adding
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001928 * function to configuration so for
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001929 * now just leave garbage here.
1930 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001931 s->s = data;
1932 --needed;
1933 ++s;
1934 }
1935
1936 data += length + 1;
1937 len -= length + 1;
1938 } while (--str_count);
1939
1940 s->id = 0; /* terminator */
1941 s->s = NULL;
1942 ++s;
1943
1944 } while (--lang_count);
1945
1946 /* Some garbage left? */
1947 if (unlikely(len))
1948 goto error_free;
1949
1950 /* Done! */
1951 ffs->stringtabs = stringtabs;
1952 ffs->raw_strings = _data;
1953
1954 return 0;
1955
1956error_free:
1957 kfree(stringtabs);
1958error:
1959 kfree(_data);
1960 return -EINVAL;
1961}
1962
1963
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001964/* Events handling and management *******************************************/
1965
1966static void __ffs_event_add(struct ffs_data *ffs,
1967 enum usb_functionfs_event_type type)
1968{
1969 enum usb_functionfs_event_type rem_type1, rem_type2 = type;
1970 int neg = 0;
1971
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001972 /*
1973 * Abort any unhandled setup
1974 *
1975 * We do not need to worry about some cmpxchg() changing value
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001976 * of ffs->setup_state without holding the lock because when
1977 * state is FFS_SETUP_PENDING cmpxchg() in several places in
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001978 * the source does nothing.
1979 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001980 if (ffs->setup_state == FFS_SETUP_PENDING)
1981 ffs->setup_state = FFS_SETUP_CANCELED;
1982
1983 switch (type) {
1984 case FUNCTIONFS_RESUME:
1985 rem_type2 = FUNCTIONFS_SUSPEND;
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001986 /* FALL THROUGH */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001987 case FUNCTIONFS_SUSPEND:
1988 case FUNCTIONFS_SETUP:
1989 rem_type1 = type;
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001990 /* Discard all similar events */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001991 break;
1992
1993 case FUNCTIONFS_BIND:
1994 case FUNCTIONFS_UNBIND:
1995 case FUNCTIONFS_DISABLE:
1996 case FUNCTIONFS_ENABLE:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001997 /* Discard everything other then power management. */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001998 rem_type1 = FUNCTIONFS_SUSPEND;
1999 rem_type2 = FUNCTIONFS_RESUME;
2000 neg = 1;
2001 break;
2002
2003 default:
2004 BUG();
2005 }
2006
2007 {
2008 u8 *ev = ffs->ev.types, *out = ev;
2009 unsigned n = ffs->ev.count;
2010 for (; n; --n, ++ev)
2011 if ((*ev == rem_type1 || *ev == rem_type2) == neg)
2012 *out++ = *ev;
2013 else
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002014 pr_vdebug("purging event %d\n", *ev);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002015 ffs->ev.count = out - ffs->ev.types;
2016 }
2017
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002018 pr_vdebug("adding event %d\n", type);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002019 ffs->ev.types[ffs->ev.count++] = type;
2020 wake_up_locked(&ffs->ev.waitq);
2021}
2022
2023static void ffs_event_add(struct ffs_data *ffs,
2024 enum usb_functionfs_event_type type)
2025{
2026 unsigned long flags;
2027 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2028 __ffs_event_add(ffs, type);
2029 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2030}
2031
2032
2033/* Bind/unbind USB function hooks *******************************************/
2034
2035static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
2036 struct usb_descriptor_header *desc,
2037 void *priv)
2038{
2039 struct usb_endpoint_descriptor *ds = (void *)desc;
2040 struct ffs_function *func = priv;
2041 struct ffs_ep *ffs_ep;
2042
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002043 /*
2044 * If hs_descriptors is not NULL then we are reading hs
2045 * descriptors now
2046 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002047 const int isHS = func->function.hs_descriptors != NULL;
2048 unsigned idx;
2049
2050 if (type != FFS_DESCRIPTOR)
2051 return 0;
2052
2053 if (isHS)
2054 func->function.hs_descriptors[(long)valuep] = desc;
2055 else
2056 func->function.descriptors[(long)valuep] = desc;
2057
2058 if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT)
2059 return 0;
2060
2061 idx = (ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK) - 1;
2062 ffs_ep = func->eps + idx;
2063
2064 if (unlikely(ffs_ep->descs[isHS])) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002065 pr_vdebug("two %sspeed descriptors for EP %d\n",
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +01002066 isHS ? "high" : "full",
2067 ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002068 return -EINVAL;
2069 }
2070 ffs_ep->descs[isHS] = ds;
2071
2072 ffs_dump_mem(": Original ep desc", ds, ds->bLength);
2073 if (ffs_ep->ep) {
2074 ds->bEndpointAddress = ffs_ep->descs[0]->bEndpointAddress;
2075 if (!ds->wMaxPacketSize)
2076 ds->wMaxPacketSize = ffs_ep->descs[0]->wMaxPacketSize;
2077 } else {
2078 struct usb_request *req;
2079 struct usb_ep *ep;
2080
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002081 pr_vdebug("autoconfig\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002082 ep = usb_ep_autoconfig(func->gadget, ds);
2083 if (unlikely(!ep))
2084 return -ENOTSUPP;
Joe Perchescc7e6052010-11-14 19:04:49 -08002085 ep->driver_data = func->eps + idx;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002086
2087 req = usb_ep_alloc_request(ep, GFP_KERNEL);
2088 if (unlikely(!req))
2089 return -ENOMEM;
2090
2091 ffs_ep->ep = ep;
2092 ffs_ep->req = req;
2093 func->eps_revmap[ds->bEndpointAddress &
2094 USB_ENDPOINT_NUMBER_MASK] = idx + 1;
2095 }
2096 ffs_dump_mem(": Rewritten ep desc", ds, ds->bLength);
2097
2098 return 0;
2099}
2100
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002101static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep,
2102 struct usb_descriptor_header *desc,
2103 void *priv)
2104{
2105 struct ffs_function *func = priv;
2106 unsigned idx;
2107 u8 newValue;
2108
2109 switch (type) {
2110 default:
2111 case FFS_DESCRIPTOR:
2112 /* Handled in previous pass by __ffs_func_bind_do_descs() */
2113 return 0;
2114
2115 case FFS_INTERFACE:
2116 idx = *valuep;
2117 if (func->interfaces_nums[idx] < 0) {
2118 int id = usb_interface_id(func->conf, &func->function);
2119 if (unlikely(id < 0))
2120 return id;
2121 func->interfaces_nums[idx] = id;
2122 }
2123 newValue = func->interfaces_nums[idx];
2124 break;
2125
2126 case FFS_STRING:
2127 /* String' IDs are allocated when fsf_data is bound to cdev */
2128 newValue = func->ffs->stringtabs[0]->strings[*valuep - 1].id;
2129 break;
2130
2131 case FFS_ENDPOINT:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002132 /*
2133 * USB_DT_ENDPOINT are handled in
2134 * __ffs_func_bind_do_descs().
2135 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002136 if (desc->bDescriptorType == USB_DT_ENDPOINT)
2137 return 0;
2138
2139 idx = (*valuep & USB_ENDPOINT_NUMBER_MASK) - 1;
2140 if (unlikely(!func->eps[idx].ep))
2141 return -EINVAL;
2142
2143 {
2144 struct usb_endpoint_descriptor **descs;
2145 descs = func->eps[idx].descs;
2146 newValue = descs[descs[0] ? 0 : 1]->bEndpointAddress;
2147 }
2148 break;
2149 }
2150
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002151 pr_vdebug("%02x -> %02x\n", *valuep, newValue);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002152 *valuep = newValue;
2153 return 0;
2154}
2155
2156static int ffs_func_bind(struct usb_configuration *c,
2157 struct usb_function *f)
2158{
2159 struct ffs_function *func = ffs_func_from_usb(f);
2160 struct ffs_data *ffs = func->ffs;
2161
2162 const int full = !!func->ffs->fs_descs_count;
2163 const int high = gadget_is_dualspeed(func->gadget) &&
2164 func->ffs->hs_descs_count;
2165
2166 int ret;
2167
2168 /* Make it a single chunk, less management later on */
2169 struct {
2170 struct ffs_ep eps[ffs->eps_count];
2171 struct usb_descriptor_header
2172 *fs_descs[full ? ffs->fs_descs_count + 1 : 0];
2173 struct usb_descriptor_header
2174 *hs_descs[high ? ffs->hs_descs_count + 1 : 0];
2175 short inums[ffs->interfaces_count];
2176 char raw_descs[high ? ffs->raw_descs_length
2177 : ffs->raw_fs_descs_length];
2178 } *data;
2179
2180 ENTER();
2181
2182 /* Only high speed but not supported by gadget? */
2183 if (unlikely(!(full | high)))
2184 return -ENOTSUPP;
2185
2186 /* Allocate */
2187 data = kmalloc(sizeof *data, GFP_KERNEL);
2188 if (unlikely(!data))
2189 return -ENOMEM;
2190
2191 /* Zero */
2192 memset(data->eps, 0, sizeof data->eps);
2193 memcpy(data->raw_descs, ffs->raw_descs + 16, sizeof data->raw_descs);
2194 memset(data->inums, 0xff, sizeof data->inums);
2195 for (ret = ffs->eps_count; ret; --ret)
2196 data->eps[ret].num = -1;
2197
2198 /* Save pointers */
2199 func->eps = data->eps;
2200 func->interfaces_nums = data->inums;
2201
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002202 /*
2203 * Go through all the endpoint descriptors and allocate
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002204 * endpoints first, so that later we can rewrite the endpoint
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002205 * numbers without worrying that it may be described later on.
2206 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002207 if (likely(full)) {
2208 func->function.descriptors = data->fs_descs;
2209 ret = ffs_do_descs(ffs->fs_descs_count,
2210 data->raw_descs,
2211 sizeof data->raw_descs,
2212 __ffs_func_bind_do_descs, func);
2213 if (unlikely(ret < 0))
2214 goto error;
2215 } else {
2216 ret = 0;
2217 }
2218
2219 if (likely(high)) {
2220 func->function.hs_descriptors = data->hs_descs;
2221 ret = ffs_do_descs(ffs->hs_descs_count,
2222 data->raw_descs + ret,
2223 (sizeof data->raw_descs) - ret,
2224 __ffs_func_bind_do_descs, func);
2225 }
2226
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002227 /*
2228 * Now handle interface numbers allocation and interface and
2229 * endpoint numbers rewriting. We can do that in one go
2230 * now.
2231 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002232 ret = ffs_do_descs(ffs->fs_descs_count +
2233 (high ? ffs->hs_descs_count : 0),
2234 data->raw_descs, sizeof data->raw_descs,
2235 __ffs_func_bind_do_nums, func);
2236 if (unlikely(ret < 0))
2237 goto error;
2238
2239 /* And we're done */
2240 ffs_event_add(ffs, FUNCTIONFS_BIND);
2241 return 0;
2242
2243error:
2244 /* XXX Do we need to release all claimed endpoints here? */
2245 return ret;
2246}
2247
2248
2249/* Other USB function hooks *************************************************/
2250
2251static void ffs_func_unbind(struct usb_configuration *c,
2252 struct usb_function *f)
2253{
2254 struct ffs_function *func = ffs_func_from_usb(f);
2255 struct ffs_data *ffs = func->ffs;
2256
2257 ENTER();
2258
2259 if (ffs->func == func) {
2260 ffs_func_eps_disable(func);
2261 ffs->func = NULL;
2262 }
2263
2264 ffs_event_add(ffs, FUNCTIONFS_UNBIND);
2265
2266 ffs_func_free(func);
2267}
2268
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002269static int ffs_func_set_alt(struct usb_function *f,
2270 unsigned interface, unsigned alt)
2271{
2272 struct ffs_function *func = ffs_func_from_usb(f);
2273 struct ffs_data *ffs = func->ffs;
2274 int ret = 0, intf;
2275
2276 if (alt != (unsigned)-1) {
2277 intf = ffs_func_revmap_intf(func, interface);
2278 if (unlikely(intf < 0))
2279 return intf;
2280 }
2281
2282 if (ffs->func)
2283 ffs_func_eps_disable(ffs->func);
2284
2285 if (ffs->state != FFS_ACTIVE)
2286 return -ENODEV;
2287
2288 if (alt == (unsigned)-1) {
2289 ffs->func = NULL;
2290 ffs_event_add(ffs, FUNCTIONFS_DISABLE);
2291 return 0;
2292 }
2293
2294 ffs->func = func;
2295 ret = ffs_func_eps_enable(func);
2296 if (likely(ret >= 0))
2297 ffs_event_add(ffs, FUNCTIONFS_ENABLE);
2298 return ret;
2299}
2300
2301static void ffs_func_disable(struct usb_function *f)
2302{
2303 ffs_func_set_alt(f, 0, (unsigned)-1);
2304}
2305
2306static int ffs_func_setup(struct usb_function *f,
2307 const struct usb_ctrlrequest *creq)
2308{
2309 struct ffs_function *func = ffs_func_from_usb(f);
2310 struct ffs_data *ffs = func->ffs;
2311 unsigned long flags;
2312 int ret;
2313
2314 ENTER();
2315
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002316 pr_vdebug("creq->bRequestType = %02x\n", creq->bRequestType);
2317 pr_vdebug("creq->bRequest = %02x\n", creq->bRequest);
2318 pr_vdebug("creq->wValue = %04x\n", le16_to_cpu(creq->wValue));
2319 pr_vdebug("creq->wIndex = %04x\n", le16_to_cpu(creq->wIndex));
2320 pr_vdebug("creq->wLength = %04x\n", le16_to_cpu(creq->wLength));
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002321
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002322 /*
2323 * Most requests directed to interface go through here
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002324 * (notable exceptions are set/get interface) so we need to
2325 * handle them. All other either handled by composite or
2326 * passed to usb_configuration->setup() (if one is set). No
2327 * matter, we will handle requests directed to endpoint here
2328 * as well (as it's straightforward) but what to do with any
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002329 * other request?
2330 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002331 if (ffs->state != FFS_ACTIVE)
2332 return -ENODEV;
2333
2334 switch (creq->bRequestType & USB_RECIP_MASK) {
2335 case USB_RECIP_INTERFACE:
2336 ret = ffs_func_revmap_intf(func, le16_to_cpu(creq->wIndex));
2337 if (unlikely(ret < 0))
2338 return ret;
2339 break;
2340
2341 case USB_RECIP_ENDPOINT:
2342 ret = ffs_func_revmap_ep(func, le16_to_cpu(creq->wIndex));
2343 if (unlikely(ret < 0))
2344 return ret;
2345 break;
2346
2347 default:
2348 return -EOPNOTSUPP;
2349 }
2350
2351 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2352 ffs->ev.setup = *creq;
2353 ffs->ev.setup.wIndex = cpu_to_le16(ret);
2354 __ffs_event_add(ffs, FUNCTIONFS_SETUP);
2355 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2356
2357 return 0;
2358}
2359
2360static void ffs_func_suspend(struct usb_function *f)
2361{
2362 ENTER();
2363 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_SUSPEND);
2364}
2365
2366static void ffs_func_resume(struct usb_function *f)
2367{
2368 ENTER();
2369 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_RESUME);
2370}
2371
2372
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002373/* Endpoint and interface numbers reverse mapping ***************************/
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002374
2375static int ffs_func_revmap_ep(struct ffs_function *func, u8 num)
2376{
2377 num = func->eps_revmap[num & USB_ENDPOINT_NUMBER_MASK];
2378 return num ? num : -EDOM;
2379}
2380
2381static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf)
2382{
2383 short *nums = func->interfaces_nums;
2384 unsigned count = func->ffs->interfaces_count;
2385
2386 for (; count; --count, ++nums) {
2387 if (*nums >= 0 && *nums == intf)
2388 return nums - func->interfaces_nums;
2389 }
2390
2391 return -EDOM;
2392}
2393
2394
2395/* Misc helper functions ****************************************************/
2396
2397static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
2398{
2399 return nonblock
2400 ? likely(mutex_trylock(mutex)) ? 0 : -EAGAIN
2401 : mutex_lock_interruptible(mutex);
2402}
2403
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002404static char *ffs_prepare_buffer(const char * __user buf, size_t len)
2405{
2406 char *data;
2407
2408 if (unlikely(!len))
2409 return NULL;
2410
2411 data = kmalloc(len, GFP_KERNEL);
2412 if (unlikely(!data))
2413 return ERR_PTR(-ENOMEM);
2414
2415 if (unlikely(__copy_from_user(data, buf, len))) {
2416 kfree(data);
2417 return ERR_PTR(-EFAULT);
2418 }
2419
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002420 pr_vdebug("Buffer from user space:\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002421 ffs_dump_mem("", data, len);
2422
2423 return data;
2424}