blob: 19fffccc370d3af1e368b5ccd8a98b844c89fa1d [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
Marek Szyprowskice1fd352011-01-28 13:55:36 +0100371 /*
372 * UDC layer requires to provide a buffer even for ZLP, but should
373 * not use it at all. Let's provide some poisoned pointer to catch
374 * possible bug in the driver.
375 */
376 if (req->buf == NULL)
377 req->buf = (void *)0xDEADBABE;
378
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200379 INIT_COMPLETION(ffs->ep0req_completion);
380
381 ret = usb_ep_queue(ffs->gadget->ep0, req, GFP_ATOMIC);
382 if (unlikely(ret < 0))
383 return ret;
384
385 ret = wait_for_completion_interruptible(&ffs->ep0req_completion);
386 if (unlikely(ret)) {
387 usb_ep_dequeue(ffs->gadget->ep0, req);
388 return -EINTR;
389 }
390
391 ffs->setup_state = FFS_NO_SETUP;
392 return ffs->ep0req_status;
393}
394
395static int __ffs_ep0_stall(struct ffs_data *ffs)
396{
397 if (ffs->ev.can_stall) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100398 pr_vdebug("ep0 stall\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200399 usb_ep_set_halt(ffs->gadget->ep0);
400 ffs->setup_state = FFS_NO_SETUP;
401 return -EL2HLT;
402 } else {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100403 pr_debug("bogus ep0 stall!\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200404 return -ESRCH;
405 }
406}
407
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200408static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
409 size_t len, loff_t *ptr)
410{
411 struct ffs_data *ffs = file->private_data;
412 ssize_t ret;
413 char *data;
414
415 ENTER();
416
417 /* Fast check if setup was canceled */
418 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED)
419 return -EIDRM;
420
421 /* Acquire mutex */
422 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
423 if (unlikely(ret < 0))
424 return ret;
425
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200426 /* Check state */
427 switch (ffs->state) {
428 case FFS_READ_DESCRIPTORS:
429 case FFS_READ_STRINGS:
430 /* Copy data */
431 if (unlikely(len < 16)) {
432 ret = -EINVAL;
433 break;
434 }
435
436 data = ffs_prepare_buffer(buf, len);
Tobias Klauser537baab2010-12-09 15:52:39 +0100437 if (IS_ERR(data)) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200438 ret = PTR_ERR(data);
439 break;
440 }
441
442 /* Handle data */
443 if (ffs->state == FFS_READ_DESCRIPTORS) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100444 pr_info("read descriptors\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200445 ret = __ffs_data_got_descs(ffs, data, len);
446 if (unlikely(ret < 0))
447 break;
448
449 ffs->state = FFS_READ_STRINGS;
450 ret = len;
451 } else {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100452 pr_info("read strings\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200453 ret = __ffs_data_got_strings(ffs, data, len);
454 if (unlikely(ret < 0))
455 break;
456
457 ret = ffs_epfiles_create(ffs);
458 if (unlikely(ret)) {
459 ffs->state = FFS_CLOSING;
460 break;
461 }
462
463 ffs->state = FFS_ACTIVE;
464 mutex_unlock(&ffs->mutex);
465
466 ret = functionfs_ready_callback(ffs);
467 if (unlikely(ret < 0)) {
468 ffs->state = FFS_CLOSING;
469 return ret;
470 }
471
472 set_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags);
473 return len;
474 }
475 break;
476
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200477 case FFS_ACTIVE:
478 data = NULL;
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100479 /*
480 * We're called from user space, we can use _irq
481 * rather then _irqsave
482 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200483 spin_lock_irq(&ffs->ev.waitq.lock);
484 switch (FFS_SETUP_STATE(ffs)) {
485 case FFS_SETUP_CANCELED:
486 ret = -EIDRM;
487 goto done_spin;
488
489 case FFS_NO_SETUP:
490 ret = -ESRCH;
491 goto done_spin;
492
493 case FFS_SETUP_PENDING:
494 break;
495 }
496
497 /* FFS_SETUP_PENDING */
498 if (!(ffs->ev.setup.bRequestType & USB_DIR_IN)) {
499 spin_unlock_irq(&ffs->ev.waitq.lock);
500 ret = __ffs_ep0_stall(ffs);
501 break;
502 }
503
504 /* FFS_SETUP_PENDING and not stall */
505 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
506
507 spin_unlock_irq(&ffs->ev.waitq.lock);
508
509 data = ffs_prepare_buffer(buf, len);
Tobias Klauser537baab2010-12-09 15:52:39 +0100510 if (IS_ERR(data)) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200511 ret = PTR_ERR(data);
512 break;
513 }
514
515 spin_lock_irq(&ffs->ev.waitq.lock);
516
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100517 /*
518 * We are guaranteed to be still in FFS_ACTIVE state
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200519 * but the state of setup could have changed from
520 * FFS_SETUP_PENDING to FFS_SETUP_CANCELED so we need
521 * to check for that. If that happened we copied data
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100522 * from user space in vain but it's unlikely.
523 *
524 * For sure we are not in FFS_NO_SETUP since this is
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200525 * the only place FFS_SETUP_PENDING -> FFS_NO_SETUP
526 * transition can be performed and it's protected by
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100527 * mutex.
528 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200529 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) {
530 ret = -EIDRM;
531done_spin:
532 spin_unlock_irq(&ffs->ev.waitq.lock);
533 } else {
534 /* unlocks spinlock */
535 ret = __ffs_ep0_queue_wait(ffs, data, len);
536 }
537 kfree(data);
538 break;
539
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200540 default:
541 ret = -EBADFD;
542 break;
543 }
544
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200545 mutex_unlock(&ffs->mutex);
546 return ret;
547}
548
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200549static ssize_t __ffs_ep0_read_events(struct ffs_data *ffs, char __user *buf,
550 size_t n)
551{
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100552 /*
553 * We are holding ffs->ev.waitq.lock and ffs->mutex and we need
554 * to release them.
555 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200556 struct usb_functionfs_event events[n];
557 unsigned i = 0;
558
559 memset(events, 0, sizeof events);
560
561 do {
562 events[i].type = ffs->ev.types[i];
563 if (events[i].type == FUNCTIONFS_SETUP) {
564 events[i].u.setup = ffs->ev.setup;
565 ffs->setup_state = FFS_SETUP_PENDING;
566 }
567 } while (++i < n);
568
569 if (n < ffs->ev.count) {
570 ffs->ev.count -= n;
571 memmove(ffs->ev.types, ffs->ev.types + n,
572 ffs->ev.count * sizeof *ffs->ev.types);
573 } else {
574 ffs->ev.count = 0;
575 }
576
577 spin_unlock_irq(&ffs->ev.waitq.lock);
578 mutex_unlock(&ffs->mutex);
579
580 return unlikely(__copy_to_user(buf, events, sizeof events))
581 ? -EFAULT : sizeof events;
582}
583
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200584static ssize_t ffs_ep0_read(struct file *file, char __user *buf,
585 size_t len, loff_t *ptr)
586{
587 struct ffs_data *ffs = file->private_data;
588 char *data = NULL;
589 size_t n;
590 int ret;
591
592 ENTER();
593
594 /* Fast check if setup was canceled */
595 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED)
596 return -EIDRM;
597
598 /* Acquire mutex */
599 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
600 if (unlikely(ret < 0))
601 return ret;
602
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200603 /* Check state */
604 if (ffs->state != FFS_ACTIVE) {
605 ret = -EBADFD;
606 goto done_mutex;
607 }
608
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100609 /*
610 * We're called from user space, we can use _irq rather then
611 * _irqsave
612 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200613 spin_lock_irq(&ffs->ev.waitq.lock);
614
615 switch (FFS_SETUP_STATE(ffs)) {
616 case FFS_SETUP_CANCELED:
617 ret = -EIDRM;
618 break;
619
620 case FFS_NO_SETUP:
621 n = len / sizeof(struct usb_functionfs_event);
622 if (unlikely(!n)) {
623 ret = -EINVAL;
624 break;
625 }
626
627 if ((file->f_flags & O_NONBLOCK) && !ffs->ev.count) {
628 ret = -EAGAIN;
629 break;
630 }
631
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100632 if (wait_event_interruptible_exclusive_locked_irq(ffs->ev.waitq,
633 ffs->ev.count)) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200634 ret = -EINTR;
635 break;
636 }
637
638 return __ffs_ep0_read_events(ffs, buf,
639 min(n, (size_t)ffs->ev.count));
640
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200641 case FFS_SETUP_PENDING:
642 if (ffs->ev.setup.bRequestType & USB_DIR_IN) {
643 spin_unlock_irq(&ffs->ev.waitq.lock);
644 ret = __ffs_ep0_stall(ffs);
645 goto done_mutex;
646 }
647
648 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
649
650 spin_unlock_irq(&ffs->ev.waitq.lock);
651
652 if (likely(len)) {
653 data = kmalloc(len, GFP_KERNEL);
654 if (unlikely(!data)) {
655 ret = -ENOMEM;
656 goto done_mutex;
657 }
658 }
659
660 spin_lock_irq(&ffs->ev.waitq.lock);
661
662 /* See ffs_ep0_write() */
663 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) {
664 ret = -EIDRM;
665 break;
666 }
667
668 /* unlocks spinlock */
669 ret = __ffs_ep0_queue_wait(ffs, data, len);
670 if (likely(ret > 0) && unlikely(__copy_to_user(buf, data, len)))
671 ret = -EFAULT;
672 goto done_mutex;
673
674 default:
675 ret = -EBADFD;
676 break;
677 }
678
679 spin_unlock_irq(&ffs->ev.waitq.lock);
680done_mutex:
681 mutex_unlock(&ffs->mutex);
682 kfree(data);
683 return ret;
684}
685
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200686static int ffs_ep0_open(struct inode *inode, struct file *file)
687{
688 struct ffs_data *ffs = inode->i_private;
689
690 ENTER();
691
692 if (unlikely(ffs->state == FFS_CLOSING))
693 return -EBUSY;
694
695 file->private_data = ffs;
696 ffs_data_opened(ffs);
697
698 return 0;
699}
700
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200701static int ffs_ep0_release(struct inode *inode, struct file *file)
702{
703 struct ffs_data *ffs = file->private_data;
704
705 ENTER();
706
707 ffs_data_closed(ffs);
708
709 return 0;
710}
711
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200712static long ffs_ep0_ioctl(struct file *file, unsigned code, unsigned long value)
713{
714 struct ffs_data *ffs = file->private_data;
715 struct usb_gadget *gadget = ffs->gadget;
716 long ret;
717
718 ENTER();
719
720 if (code == FUNCTIONFS_INTERFACE_REVMAP) {
721 struct ffs_function *func = ffs->func;
722 ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV;
723 } else if (gadget->ops->ioctl) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200724 ret = gadget->ops->ioctl(gadget, code, value);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200725 } else {
726 ret = -ENOTTY;
727 }
728
729 return ret;
730}
731
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200732static const struct file_operations ffs_ep0_operations = {
733 .owner = THIS_MODULE,
734 .llseek = no_llseek,
735
736 .open = ffs_ep0_open,
737 .write = ffs_ep0_write,
738 .read = ffs_ep0_read,
739 .release = ffs_ep0_release,
740 .unlocked_ioctl = ffs_ep0_ioctl,
741};
742
743
744/* "Normal" endpoints operations ********************************************/
745
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200746static void ffs_epfile_io_complete(struct usb_ep *_ep, struct usb_request *req)
747{
748 ENTER();
749 if (likely(req->context)) {
750 struct ffs_ep *ep = _ep->driver_data;
751 ep->status = req->status ? req->status : req->actual;
752 complete(req->context);
753 }
754}
755
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200756static ssize_t ffs_epfile_io(struct file *file,
757 char __user *buf, size_t len, int read)
758{
759 struct ffs_epfile *epfile = file->private_data;
760 struct ffs_ep *ep;
761 char *data = NULL;
762 ssize_t ret;
763 int halt;
764
765 goto first_try;
766 do {
767 spin_unlock_irq(&epfile->ffs->eps_lock);
768 mutex_unlock(&epfile->mutex);
769
770first_try:
771 /* Are we still active? */
772 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE)) {
773 ret = -ENODEV;
774 goto error;
775 }
776
777 /* Wait for endpoint to be enabled */
778 ep = epfile->ep;
779 if (!ep) {
780 if (file->f_flags & O_NONBLOCK) {
781 ret = -EAGAIN;
782 goto error;
783 }
784
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100785 if (wait_event_interruptible(epfile->wait,
786 (ep = epfile->ep))) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200787 ret = -EINTR;
788 goto error;
789 }
790 }
791
792 /* Do we halt? */
793 halt = !read == !epfile->in;
794 if (halt && epfile->isoc) {
795 ret = -EINVAL;
796 goto error;
797 }
798
799 /* Allocate & copy */
800 if (!halt && !data) {
801 data = kzalloc(len, GFP_KERNEL);
802 if (unlikely(!data))
803 return -ENOMEM;
804
805 if (!read &&
806 unlikely(__copy_from_user(data, buf, len))) {
807 ret = -EFAULT;
808 goto error;
809 }
810 }
811
812 /* We will be using request */
813 ret = ffs_mutex_lock(&epfile->mutex,
814 file->f_flags & O_NONBLOCK);
815 if (unlikely(ret))
816 goto error;
817
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100818 /*
819 * We're called from user space, we can use _irq rather then
820 * _irqsave
821 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200822 spin_lock_irq(&epfile->ffs->eps_lock);
823
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100824 /*
825 * While we were acquiring mutex endpoint got disabled
826 * or changed?
827 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200828 } while (unlikely(epfile->ep != ep));
829
830 /* Halt */
831 if (unlikely(halt)) {
832 if (likely(epfile->ep == ep) && !WARN_ON(!ep->ep))
833 usb_ep_set_halt(ep->ep);
834 spin_unlock_irq(&epfile->ffs->eps_lock);
835 ret = -EBADMSG;
836 } else {
837 /* Fire the request */
838 DECLARE_COMPLETION_ONSTACK(done);
839
840 struct usb_request *req = ep->req;
841 req->context = &done;
842 req->complete = ffs_epfile_io_complete;
843 req->buf = data;
844 req->length = len;
845
846 ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
847
848 spin_unlock_irq(&epfile->ffs->eps_lock);
849
850 if (unlikely(ret < 0)) {
851 /* nop */
852 } else if (unlikely(wait_for_completion_interruptible(&done))) {
853 ret = -EINTR;
854 usb_ep_dequeue(ep->ep, req);
855 } else {
856 ret = ep->status;
857 if (read && ret > 0 &&
858 unlikely(copy_to_user(buf, data, ret)))
859 ret = -EFAULT;
860 }
861 }
862
863 mutex_unlock(&epfile->mutex);
864error:
865 kfree(data);
866 return ret;
867}
868
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200869static ssize_t
870ffs_epfile_write(struct file *file, const char __user *buf, size_t len,
871 loff_t *ptr)
872{
873 ENTER();
874
875 return ffs_epfile_io(file, (char __user *)buf, len, 0);
876}
877
878static ssize_t
879ffs_epfile_read(struct file *file, char __user *buf, size_t len, loff_t *ptr)
880{
881 ENTER();
882
883 return ffs_epfile_io(file, buf, len, 1);
884}
885
886static int
887ffs_epfile_open(struct inode *inode, struct file *file)
888{
889 struct ffs_epfile *epfile = inode->i_private;
890
891 ENTER();
892
893 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
894 return -ENODEV;
895
896 file->private_data = epfile;
897 ffs_data_opened(epfile->ffs);
898
899 return 0;
900}
901
902static int
903ffs_epfile_release(struct inode *inode, struct file *file)
904{
905 struct ffs_epfile *epfile = inode->i_private;
906
907 ENTER();
908
909 ffs_data_closed(epfile->ffs);
910
911 return 0;
912}
913
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200914static long ffs_epfile_ioctl(struct file *file, unsigned code,
915 unsigned long value)
916{
917 struct ffs_epfile *epfile = file->private_data;
918 int ret;
919
920 ENTER();
921
922 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
923 return -ENODEV;
924
925 spin_lock_irq(&epfile->ffs->eps_lock);
926 if (likely(epfile->ep)) {
927 switch (code) {
928 case FUNCTIONFS_FIFO_STATUS:
929 ret = usb_ep_fifo_status(epfile->ep->ep);
930 break;
931 case FUNCTIONFS_FIFO_FLUSH:
932 usb_ep_fifo_flush(epfile->ep->ep);
933 ret = 0;
934 break;
935 case FUNCTIONFS_CLEAR_HALT:
936 ret = usb_ep_clear_halt(epfile->ep->ep);
937 break;
938 case FUNCTIONFS_ENDPOINT_REVMAP:
939 ret = epfile->ep->num;
940 break;
941 default:
942 ret = -ENOTTY;
943 }
944 } else {
945 ret = -ENODEV;
946 }
947 spin_unlock_irq(&epfile->ffs->eps_lock);
948
949 return ret;
950}
951
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200952static const struct file_operations ffs_epfile_operations = {
953 .owner = THIS_MODULE,
954 .llseek = no_llseek,
955
956 .open = ffs_epfile_open,
957 .write = ffs_epfile_write,
958 .read = ffs_epfile_read,
959 .release = ffs_epfile_release,
960 .unlocked_ioctl = ffs_epfile_ioctl,
961};
962
963
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200964/* File system and super block operations ***********************************/
965
966/*
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100967 * Mounting the file system creates a controller file, used first for
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200968 * function configuration then later for event monitoring.
969 */
970
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200971static struct inode *__must_check
972ffs_sb_make_inode(struct super_block *sb, void *data,
973 const struct file_operations *fops,
974 const struct inode_operations *iops,
975 struct ffs_file_perms *perms)
976{
977 struct inode *inode;
978
979 ENTER();
980
981 inode = new_inode(sb);
982
983 if (likely(inode)) {
984 struct timespec current_time = CURRENT_TIME;
985
Al Viro12ba8d12010-10-27 04:19:36 +0100986 inode->i_ino = get_next_ino();
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200987 inode->i_mode = perms->mode;
988 inode->i_uid = perms->uid;
989 inode->i_gid = perms->gid;
990 inode->i_atime = current_time;
991 inode->i_mtime = current_time;
992 inode->i_ctime = current_time;
993 inode->i_private = data;
994 if (fops)
995 inode->i_fop = fops;
996 if (iops)
997 inode->i_op = iops;
998 }
999
1000 return inode;
1001}
1002
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001003/* Create "regular" file */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001004static struct inode *ffs_sb_create_file(struct super_block *sb,
1005 const char *name, void *data,
1006 const struct file_operations *fops,
1007 struct dentry **dentry_p)
1008{
1009 struct ffs_data *ffs = sb->s_fs_info;
1010 struct dentry *dentry;
1011 struct inode *inode;
1012
1013 ENTER();
1014
1015 dentry = d_alloc_name(sb->s_root, name);
1016 if (unlikely(!dentry))
1017 return NULL;
1018
1019 inode = ffs_sb_make_inode(sb, data, fops, NULL, &ffs->file_perms);
1020 if (unlikely(!inode)) {
1021 dput(dentry);
1022 return NULL;
1023 }
1024
1025 d_add(dentry, inode);
1026 if (dentry_p)
1027 *dentry_p = dentry;
1028
1029 return inode;
1030}
1031
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001032/* Super block */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001033static const struct super_operations ffs_sb_operations = {
1034 .statfs = simple_statfs,
1035 .drop_inode = generic_delete_inode,
1036};
1037
1038struct ffs_sb_fill_data {
1039 struct ffs_file_perms perms;
1040 umode_t root_mode;
1041 const char *dev_name;
1042};
1043
1044static int ffs_sb_fill(struct super_block *sb, void *_data, int silent)
1045{
1046 struct ffs_sb_fill_data *data = _data;
1047 struct inode *inode;
1048 struct dentry *d;
1049 struct ffs_data *ffs;
1050
1051 ENTER();
1052
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001053 /* Initialise data */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001054 ffs = ffs_data_new();
1055 if (unlikely(!ffs))
1056 goto enomem0;
1057
1058 ffs->sb = sb;
1059 ffs->dev_name = data->dev_name;
1060 ffs->file_perms = data->perms;
1061
1062 sb->s_fs_info = ffs;
1063 sb->s_blocksize = PAGE_CACHE_SIZE;
1064 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1065 sb->s_magic = FUNCTIONFS_MAGIC;
1066 sb->s_op = &ffs_sb_operations;
1067 sb->s_time_gran = 1;
1068
1069 /* Root inode */
1070 data->perms.mode = data->root_mode;
1071 inode = ffs_sb_make_inode(sb, NULL,
1072 &simple_dir_operations,
1073 &simple_dir_inode_operations,
1074 &data->perms);
1075 if (unlikely(!inode))
1076 goto enomem1;
1077 d = d_alloc_root(inode);
1078 if (unlikely(!d))
1079 goto enomem2;
1080 sb->s_root = d;
1081
1082 /* EP0 file */
1083 if (unlikely(!ffs_sb_create_file(sb, "ep0", ffs,
1084 &ffs_ep0_operations, NULL)))
1085 goto enomem3;
1086
1087 return 0;
1088
1089enomem3:
1090 dput(d);
1091enomem2:
1092 iput(inode);
1093enomem1:
1094 ffs_data_put(ffs);
1095enomem0:
1096 return -ENOMEM;
1097}
1098
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001099static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts)
1100{
1101 ENTER();
1102
1103 if (!opts || !*opts)
1104 return 0;
1105
1106 for (;;) {
1107 char *end, *eq, *comma;
1108 unsigned long value;
1109
1110 /* Option limit */
1111 comma = strchr(opts, ',');
1112 if (comma)
1113 *comma = 0;
1114
1115 /* Value limit */
1116 eq = strchr(opts, '=');
1117 if (unlikely(!eq)) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001118 pr_err("'=' missing in %s\n", opts);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001119 return -EINVAL;
1120 }
1121 *eq = 0;
1122
1123 /* Parse value */
1124 value = simple_strtoul(eq + 1, &end, 0);
1125 if (unlikely(*end != ',' && *end != 0)) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001126 pr_err("%s: invalid value: %s\n", opts, eq + 1);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001127 return -EINVAL;
1128 }
1129
1130 /* Interpret option */
1131 switch (eq - opts) {
1132 case 5:
1133 if (!memcmp(opts, "rmode", 5))
1134 data->root_mode = (value & 0555) | S_IFDIR;
1135 else if (!memcmp(opts, "fmode", 5))
1136 data->perms.mode = (value & 0666) | S_IFREG;
1137 else
1138 goto invalid;
1139 break;
1140
1141 case 4:
1142 if (!memcmp(opts, "mode", 4)) {
1143 data->root_mode = (value & 0555) | S_IFDIR;
1144 data->perms.mode = (value & 0666) | S_IFREG;
1145 } else {
1146 goto invalid;
1147 }
1148 break;
1149
1150 case 3:
1151 if (!memcmp(opts, "uid", 3))
1152 data->perms.uid = value;
1153 else if (!memcmp(opts, "gid", 3))
1154 data->perms.gid = value;
1155 else
1156 goto invalid;
1157 break;
1158
1159 default:
1160invalid:
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001161 pr_err("%s: invalid option\n", opts);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001162 return -EINVAL;
1163 }
1164
1165 /* Next iteration */
1166 if (!comma)
1167 break;
1168 opts = comma + 1;
1169 }
1170
1171 return 0;
1172}
1173
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001174/* "mount -t functionfs dev_name /dev/function" ends up here */
1175
Al Virofc14f2f2010-07-25 01:48:30 +04001176static struct dentry *
1177ffs_fs_mount(struct file_system_type *t, int flags,
1178 const char *dev_name, void *opts)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001179{
1180 struct ffs_sb_fill_data data = {
1181 .perms = {
1182 .mode = S_IFREG | 0600,
1183 .uid = 0,
1184 .gid = 0
1185 },
1186 .root_mode = S_IFDIR | 0500,
1187 };
1188 int ret;
1189
1190 ENTER();
1191
1192 ret = functionfs_check_dev_callback(dev_name);
1193 if (unlikely(ret < 0))
Al Virofc14f2f2010-07-25 01:48:30 +04001194 return ERR_PTR(ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001195
1196 ret = ffs_fs_parse_opts(&data, opts);
1197 if (unlikely(ret < 0))
Al Virofc14f2f2010-07-25 01:48:30 +04001198 return ERR_PTR(ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001199
1200 data.dev_name = dev_name;
Al Virofc14f2f2010-07-25 01:48:30 +04001201 return mount_single(t, flags, &data, ffs_sb_fill);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001202}
1203
1204static void
1205ffs_fs_kill_sb(struct super_block *sb)
1206{
1207 void *ptr;
1208
1209 ENTER();
1210
1211 kill_litter_super(sb);
1212 ptr = xchg(&sb->s_fs_info, NULL);
1213 if (ptr)
1214 ffs_data_put(ptr);
1215}
1216
1217static struct file_system_type ffs_fs_type = {
1218 .owner = THIS_MODULE,
1219 .name = "functionfs",
Al Virofc14f2f2010-07-25 01:48:30 +04001220 .mount = ffs_fs_mount,
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001221 .kill_sb = ffs_fs_kill_sb,
1222};
1223
1224
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001225/* Driver's main init/cleanup functions *************************************/
1226
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001227static int functionfs_init(void)
1228{
1229 int ret;
1230
1231 ENTER();
1232
1233 ret = register_filesystem(&ffs_fs_type);
1234 if (likely(!ret))
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001235 pr_info("file system registered\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001236 else
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001237 pr_err("failed registering file system (%d)\n", ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001238
1239 return ret;
1240}
1241
1242static void functionfs_cleanup(void)
1243{
1244 ENTER();
1245
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001246 pr_info("unloading\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001247 unregister_filesystem(&ffs_fs_type);
1248}
1249
1250
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001251/* ffs_data and ffs_function construction and destruction code **************/
1252
1253static void ffs_data_clear(struct ffs_data *ffs);
1254static void ffs_data_reset(struct ffs_data *ffs);
1255
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001256static void ffs_data_get(struct ffs_data *ffs)
1257{
1258 ENTER();
1259
1260 atomic_inc(&ffs->ref);
1261}
1262
1263static void ffs_data_opened(struct ffs_data *ffs)
1264{
1265 ENTER();
1266
1267 atomic_inc(&ffs->ref);
1268 atomic_inc(&ffs->opened);
1269}
1270
1271static void ffs_data_put(struct ffs_data *ffs)
1272{
1273 ENTER();
1274
1275 if (unlikely(atomic_dec_and_test(&ffs->ref))) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001276 pr_info("%s(): freeing\n", __func__);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001277 ffs_data_clear(ffs);
1278 BUG_ON(mutex_is_locked(&ffs->mutex) ||
1279 spin_is_locked(&ffs->ev.waitq.lock) ||
1280 waitqueue_active(&ffs->ev.waitq) ||
1281 waitqueue_active(&ffs->ep0req_completion.wait));
1282 kfree(ffs);
1283 }
1284}
1285
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001286static void ffs_data_closed(struct ffs_data *ffs)
1287{
1288 ENTER();
1289
1290 if (atomic_dec_and_test(&ffs->opened)) {
1291 ffs->state = FFS_CLOSING;
1292 ffs_data_reset(ffs);
1293 }
1294
1295 ffs_data_put(ffs);
1296}
1297
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001298static struct ffs_data *ffs_data_new(void)
1299{
1300 struct ffs_data *ffs = kzalloc(sizeof *ffs, GFP_KERNEL);
1301 if (unlikely(!ffs))
1302 return 0;
1303
1304 ENTER();
1305
1306 atomic_set(&ffs->ref, 1);
1307 atomic_set(&ffs->opened, 0);
1308 ffs->state = FFS_READ_DESCRIPTORS;
1309 mutex_init(&ffs->mutex);
1310 spin_lock_init(&ffs->eps_lock);
1311 init_waitqueue_head(&ffs->ev.waitq);
1312 init_completion(&ffs->ep0req_completion);
1313
1314 /* XXX REVISIT need to update it in some places, or do we? */
1315 ffs->ev.can_stall = 1;
1316
1317 return ffs;
1318}
1319
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001320static void ffs_data_clear(struct ffs_data *ffs)
1321{
1322 ENTER();
1323
1324 if (test_and_clear_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags))
1325 functionfs_closed_callback(ffs);
1326
1327 BUG_ON(ffs->gadget);
1328
1329 if (ffs->epfiles)
1330 ffs_epfiles_destroy(ffs->epfiles, ffs->eps_count);
1331
1332 kfree(ffs->raw_descs);
1333 kfree(ffs->raw_strings);
1334 kfree(ffs->stringtabs);
1335}
1336
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001337static void ffs_data_reset(struct ffs_data *ffs)
1338{
1339 ENTER();
1340
1341 ffs_data_clear(ffs);
1342
1343 ffs->epfiles = NULL;
1344 ffs->raw_descs = NULL;
1345 ffs->raw_strings = NULL;
1346 ffs->stringtabs = NULL;
1347
1348 ffs->raw_descs_length = 0;
1349 ffs->raw_fs_descs_length = 0;
1350 ffs->fs_descs_count = 0;
1351 ffs->hs_descs_count = 0;
1352
1353 ffs->strings_count = 0;
1354 ffs->interfaces_count = 0;
1355 ffs->eps_count = 0;
1356
1357 ffs->ev.count = 0;
1358
1359 ffs->state = FFS_READ_DESCRIPTORS;
1360 ffs->setup_state = FFS_NO_SETUP;
1361 ffs->flags = 0;
1362}
1363
1364
1365static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev)
1366{
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001367 struct usb_gadget_strings **lang;
1368 int first_id;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001369
1370 ENTER();
1371
1372 if (WARN_ON(ffs->state != FFS_ACTIVE
1373 || test_and_set_bit(FFS_FL_BOUND, &ffs->flags)))
1374 return -EBADFD;
1375
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001376 first_id = usb_string_ids_n(cdev, ffs->strings_count);
1377 if (unlikely(first_id < 0))
1378 return first_id;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001379
1380 ffs->ep0req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
1381 if (unlikely(!ffs->ep0req))
1382 return -ENOMEM;
1383 ffs->ep0req->complete = ffs_ep0_complete;
1384 ffs->ep0req->context = ffs;
1385
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001386 lang = ffs->stringtabs;
1387 for (lang = ffs->stringtabs; *lang; ++lang) {
1388 struct usb_string *str = (*lang)->strings;
1389 int id = first_id;
1390 for (; str->s; ++id, ++str)
1391 str->id = id;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001392 }
1393
1394 ffs->gadget = cdev->gadget;
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001395 ffs_data_get(ffs);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001396 return 0;
1397}
1398
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001399static void functionfs_unbind(struct ffs_data *ffs)
1400{
1401 ENTER();
1402
1403 if (!WARN_ON(!ffs->gadget)) {
1404 usb_ep_free_request(ffs->gadget->ep0, ffs->ep0req);
1405 ffs->ep0req = NULL;
1406 ffs->gadget = NULL;
1407 ffs_data_put(ffs);
1408 }
1409}
1410
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001411static int ffs_epfiles_create(struct ffs_data *ffs)
1412{
1413 struct ffs_epfile *epfile, *epfiles;
1414 unsigned i, count;
1415
1416 ENTER();
1417
1418 count = ffs->eps_count;
1419 epfiles = kzalloc(count * sizeof *epfiles, GFP_KERNEL);
1420 if (!epfiles)
1421 return -ENOMEM;
1422
1423 epfile = epfiles;
1424 for (i = 1; i <= count; ++i, ++epfile) {
1425 epfile->ffs = ffs;
1426 mutex_init(&epfile->mutex);
1427 init_waitqueue_head(&epfile->wait);
1428 sprintf(epfiles->name, "ep%u", i);
1429 if (!unlikely(ffs_sb_create_file(ffs->sb, epfiles->name, epfile,
1430 &ffs_epfile_operations,
1431 &epfile->dentry))) {
1432 ffs_epfiles_destroy(epfiles, i - 1);
1433 return -ENOMEM;
1434 }
1435 }
1436
1437 ffs->epfiles = epfiles;
1438 return 0;
1439}
1440
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001441static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count)
1442{
1443 struct ffs_epfile *epfile = epfiles;
1444
1445 ENTER();
1446
1447 for (; count; --count, ++epfile) {
1448 BUG_ON(mutex_is_locked(&epfile->mutex) ||
1449 waitqueue_active(&epfile->wait));
1450 if (epfile->dentry) {
1451 d_delete(epfile->dentry);
1452 dput(epfile->dentry);
1453 epfile->dentry = NULL;
1454 }
1455 }
1456
1457 kfree(epfiles);
1458}
1459
Michal Nazarewicz7898aee2010-06-16 12:07:58 +02001460static int functionfs_bind_config(struct usb_composite_dev *cdev,
1461 struct usb_configuration *c,
1462 struct ffs_data *ffs)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001463{
1464 struct ffs_function *func;
1465 int ret;
1466
1467 ENTER();
1468
1469 func = kzalloc(sizeof *func, GFP_KERNEL);
1470 if (unlikely(!func))
1471 return -ENOMEM;
1472
1473 func->function.name = "Function FS Gadget";
1474 func->function.strings = ffs->stringtabs;
1475
1476 func->function.bind = ffs_func_bind;
1477 func->function.unbind = ffs_func_unbind;
1478 func->function.set_alt = ffs_func_set_alt;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001479 func->function.disable = ffs_func_disable;
1480 func->function.setup = ffs_func_setup;
1481 func->function.suspend = ffs_func_suspend;
1482 func->function.resume = ffs_func_resume;
1483
1484 func->conf = c;
1485 func->gadget = cdev->gadget;
1486 func->ffs = ffs;
1487 ffs_data_get(ffs);
1488
1489 ret = usb_add_function(c, &func->function);
1490 if (unlikely(ret))
1491 ffs_func_free(func);
1492
1493 return ret;
1494}
1495
1496static void ffs_func_free(struct ffs_function *func)
1497{
1498 ENTER();
1499
1500 ffs_data_put(func->ffs);
1501
1502 kfree(func->eps);
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001503 /*
1504 * eps and interfaces_nums are allocated in the same chunk so
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001505 * only one free is required. Descriptors are also allocated
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001506 * in the same chunk.
1507 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001508
1509 kfree(func);
1510}
1511
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001512static void ffs_func_eps_disable(struct ffs_function *func)
1513{
1514 struct ffs_ep *ep = func->eps;
1515 struct ffs_epfile *epfile = func->ffs->epfiles;
1516 unsigned count = func->ffs->eps_count;
1517 unsigned long flags;
1518
1519 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1520 do {
1521 /* pending requests get nuked */
1522 if (likely(ep->ep))
1523 usb_ep_disable(ep->ep);
1524 epfile->ep = NULL;
1525
1526 ++ep;
1527 ++epfile;
1528 } while (--count);
1529 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1530}
1531
1532static int ffs_func_eps_enable(struct ffs_function *func)
1533{
1534 struct ffs_data *ffs = func->ffs;
1535 struct ffs_ep *ep = func->eps;
1536 struct ffs_epfile *epfile = ffs->epfiles;
1537 unsigned count = ffs->eps_count;
1538 unsigned long flags;
1539 int ret = 0;
1540
1541 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1542 do {
1543 struct usb_endpoint_descriptor *ds;
1544 ds = ep->descs[ep->descs[1] ? 1 : 0];
1545
1546 ep->ep->driver_data = ep;
1547 ret = usb_ep_enable(ep->ep, ds);
1548 if (likely(!ret)) {
1549 epfile->ep = ep;
1550 epfile->in = usb_endpoint_dir_in(ds);
1551 epfile->isoc = usb_endpoint_xfer_isoc(ds);
1552 } else {
1553 break;
1554 }
1555
1556 wake_up(&epfile->wait);
1557
1558 ++ep;
1559 ++epfile;
1560 } while (--count);
1561 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1562
1563 return ret;
1564}
1565
1566
1567/* Parsing and building descriptors and strings *****************************/
1568
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001569/*
1570 * This validates if data pointed by data is a valid USB descriptor as
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001571 * well as record how many interfaces, endpoints and strings are
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001572 * required by given configuration. Returns address after the
1573 * descriptor or NULL if data is invalid.
1574 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001575
1576enum ffs_entity_type {
1577 FFS_DESCRIPTOR, FFS_INTERFACE, FFS_STRING, FFS_ENDPOINT
1578};
1579
1580typedef int (*ffs_entity_callback)(enum ffs_entity_type entity,
1581 u8 *valuep,
1582 struct usb_descriptor_header *desc,
1583 void *priv);
1584
1585static int __must_check ffs_do_desc(char *data, unsigned len,
1586 ffs_entity_callback entity, void *priv)
1587{
1588 struct usb_descriptor_header *_ds = (void *)data;
1589 u8 length;
1590 int ret;
1591
1592 ENTER();
1593
1594 /* At least two bytes are required: length and type */
1595 if (len < 2) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001596 pr_vdebug("descriptor too short\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001597 return -EINVAL;
1598 }
1599
1600 /* If we have at least as many bytes as the descriptor takes? */
1601 length = _ds->bLength;
1602 if (len < length) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001603 pr_vdebug("descriptor longer then available data\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001604 return -EINVAL;
1605 }
1606
1607#define __entity_check_INTERFACE(val) 1
1608#define __entity_check_STRING(val) (val)
1609#define __entity_check_ENDPOINT(val) ((val) & USB_ENDPOINT_NUMBER_MASK)
1610#define __entity(type, val) do { \
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001611 pr_vdebug("entity " #type "(%02x)\n", (val)); \
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001612 if (unlikely(!__entity_check_ ##type(val))) { \
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001613 pr_vdebug("invalid entity's value\n"); \
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001614 return -EINVAL; \
1615 } \
1616 ret = entity(FFS_ ##type, &val, _ds, priv); \
1617 if (unlikely(ret < 0)) { \
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001618 pr_debug("entity " #type "(%02x); ret = %d\n", \
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +01001619 (val), ret); \
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001620 return ret; \
1621 } \
1622 } while (0)
1623
1624 /* Parse descriptor depending on type. */
1625 switch (_ds->bDescriptorType) {
1626 case USB_DT_DEVICE:
1627 case USB_DT_CONFIG:
1628 case USB_DT_STRING:
1629 case USB_DT_DEVICE_QUALIFIER:
1630 /* function can't have any of those */
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001631 pr_vdebug("descriptor reserved for gadget: %d\n",
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001632 _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001633 return -EINVAL;
1634
1635 case USB_DT_INTERFACE: {
1636 struct usb_interface_descriptor *ds = (void *)_ds;
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001637 pr_vdebug("interface descriptor\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001638 if (length != sizeof *ds)
1639 goto inv_length;
1640
1641 __entity(INTERFACE, ds->bInterfaceNumber);
1642 if (ds->iInterface)
1643 __entity(STRING, ds->iInterface);
1644 }
1645 break;
1646
1647 case USB_DT_ENDPOINT: {
1648 struct usb_endpoint_descriptor *ds = (void *)_ds;
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001649 pr_vdebug("endpoint descriptor\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001650 if (length != USB_DT_ENDPOINT_SIZE &&
1651 length != USB_DT_ENDPOINT_AUDIO_SIZE)
1652 goto inv_length;
1653 __entity(ENDPOINT, ds->bEndpointAddress);
1654 }
1655 break;
1656
1657 case USB_DT_OTG:
1658 if (length != sizeof(struct usb_otg_descriptor))
1659 goto inv_length;
1660 break;
1661
1662 case USB_DT_INTERFACE_ASSOCIATION: {
1663 struct usb_interface_assoc_descriptor *ds = (void *)_ds;
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001664 pr_vdebug("interface association descriptor\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001665 if (length != sizeof *ds)
1666 goto inv_length;
1667 if (ds->iFunction)
1668 __entity(STRING, ds->iFunction);
1669 }
1670 break;
1671
1672 case USB_DT_OTHER_SPEED_CONFIG:
1673 case USB_DT_INTERFACE_POWER:
1674 case USB_DT_DEBUG:
1675 case USB_DT_SECURITY:
1676 case USB_DT_CS_RADIO_CONTROL:
1677 /* TODO */
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001678 pr_vdebug("unimplemented descriptor: %d\n", _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001679 return -EINVAL;
1680
1681 default:
1682 /* We should never be here */
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001683 pr_vdebug("unknown descriptor: %d\n", _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001684 return -EINVAL;
1685
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001686inv_length:
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001687 pr_vdebug("invalid length: %d (descriptor %d)\n",
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +01001688 _ds->bLength, _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001689 return -EINVAL;
1690 }
1691
1692#undef __entity
1693#undef __entity_check_DESCRIPTOR
1694#undef __entity_check_INTERFACE
1695#undef __entity_check_STRING
1696#undef __entity_check_ENDPOINT
1697
1698 return length;
1699}
1700
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001701static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len,
1702 ffs_entity_callback entity, void *priv)
1703{
1704 const unsigned _len = len;
1705 unsigned long num = 0;
1706
1707 ENTER();
1708
1709 for (;;) {
1710 int ret;
1711
1712 if (num == count)
1713 data = NULL;
1714
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001715 /* Record "descriptor" entity */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001716 ret = entity(FFS_DESCRIPTOR, (u8 *)num, (void *)data, priv);
1717 if (unlikely(ret < 0)) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001718 pr_debug("entity DESCRIPTOR(%02lx); ret = %d\n",
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +01001719 num, ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001720 return ret;
1721 }
1722
1723 if (!data)
1724 return _len - len;
1725
1726 ret = ffs_do_desc(data, len, entity, priv);
1727 if (unlikely(ret < 0)) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001728 pr_debug("%s returns %d\n", __func__, ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001729 return ret;
1730 }
1731
1732 len -= ret;
1733 data += ret;
1734 ++num;
1735 }
1736}
1737
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001738static int __ffs_data_do_entity(enum ffs_entity_type type,
1739 u8 *valuep, struct usb_descriptor_header *desc,
1740 void *priv)
1741{
1742 struct ffs_data *ffs = priv;
1743
1744 ENTER();
1745
1746 switch (type) {
1747 case FFS_DESCRIPTOR:
1748 break;
1749
1750 case FFS_INTERFACE:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001751 /*
1752 * Interfaces are indexed from zero so if we
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001753 * encountered interface "n" then there are at least
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001754 * "n+1" interfaces.
1755 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001756 if (*valuep >= ffs->interfaces_count)
1757 ffs->interfaces_count = *valuep + 1;
1758 break;
1759
1760 case FFS_STRING:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001761 /*
1762 * Strings are indexed from 1 (0 is magic ;) reserved
1763 * for languages list or some such)
1764 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001765 if (*valuep > ffs->strings_count)
1766 ffs->strings_count = *valuep;
1767 break;
1768
1769 case FFS_ENDPOINT:
1770 /* Endpoints are indexed from 1 as well. */
1771 if ((*valuep & USB_ENDPOINT_NUMBER_MASK) > ffs->eps_count)
1772 ffs->eps_count = (*valuep & USB_ENDPOINT_NUMBER_MASK);
1773 break;
1774 }
1775
1776 return 0;
1777}
1778
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001779static int __ffs_data_got_descs(struct ffs_data *ffs,
1780 char *const _data, size_t len)
1781{
1782 unsigned fs_count, hs_count;
1783 int fs_len, ret = -EINVAL;
1784 char *data = _data;
1785
1786 ENTER();
1787
1788 if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_DESCRIPTORS_MAGIC ||
1789 get_unaligned_le32(data + 4) != len))
1790 goto error;
1791 fs_count = get_unaligned_le32(data + 8);
1792 hs_count = get_unaligned_le32(data + 12);
1793
1794 if (!fs_count && !hs_count)
1795 goto einval;
1796
1797 data += 16;
1798 len -= 16;
1799
1800 if (likely(fs_count)) {
1801 fs_len = ffs_do_descs(fs_count, data, len,
1802 __ffs_data_do_entity, ffs);
1803 if (unlikely(fs_len < 0)) {
1804 ret = fs_len;
1805 goto error;
1806 }
1807
1808 data += fs_len;
1809 len -= fs_len;
1810 } else {
1811 fs_len = 0;
1812 }
1813
1814 if (likely(hs_count)) {
1815 ret = ffs_do_descs(hs_count, data, len,
1816 __ffs_data_do_entity, ffs);
1817 if (unlikely(ret < 0))
1818 goto error;
1819 } else {
1820 ret = 0;
1821 }
1822
1823 if (unlikely(len != ret))
1824 goto einval;
1825
1826 ffs->raw_fs_descs_length = fs_len;
1827 ffs->raw_descs_length = fs_len + ret;
1828 ffs->raw_descs = _data;
1829 ffs->fs_descs_count = fs_count;
1830 ffs->hs_descs_count = hs_count;
1831
1832 return 0;
1833
1834einval:
1835 ret = -EINVAL;
1836error:
1837 kfree(_data);
1838 return ret;
1839}
1840
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001841static int __ffs_data_got_strings(struct ffs_data *ffs,
1842 char *const _data, size_t len)
1843{
1844 u32 str_count, needed_count, lang_count;
1845 struct usb_gadget_strings **stringtabs, *t;
1846 struct usb_string *strings, *s;
1847 const char *data = _data;
1848
1849 ENTER();
1850
1851 if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_STRINGS_MAGIC ||
1852 get_unaligned_le32(data + 4) != len))
1853 goto error;
1854 str_count = get_unaligned_le32(data + 8);
1855 lang_count = get_unaligned_le32(data + 12);
1856
1857 /* if one is zero the other must be zero */
1858 if (unlikely(!str_count != !lang_count))
1859 goto error;
1860
1861 /* Do we have at least as many strings as descriptors need? */
1862 needed_count = ffs->strings_count;
1863 if (unlikely(str_count < needed_count))
1864 goto error;
1865
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001866 /*
1867 * If we don't need any strings just return and free all
1868 * memory.
1869 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001870 if (!needed_count) {
1871 kfree(_data);
1872 return 0;
1873 }
1874
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001875 /* Allocate everything in one chunk so there's less maintenance. */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001876 {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001877 struct {
1878 struct usb_gadget_strings *stringtabs[lang_count + 1];
1879 struct usb_gadget_strings stringtab[lang_count];
1880 struct usb_string strings[lang_count*(needed_count+1)];
1881 } *d;
1882 unsigned i = 0;
1883
1884 d = kmalloc(sizeof *d, GFP_KERNEL);
1885 if (unlikely(!d)) {
1886 kfree(_data);
1887 return -ENOMEM;
1888 }
1889
1890 stringtabs = d->stringtabs;
1891 t = d->stringtab;
1892 i = lang_count;
1893 do {
1894 *stringtabs++ = t++;
1895 } while (--i);
1896 *stringtabs = NULL;
1897
1898 stringtabs = d->stringtabs;
1899 t = d->stringtab;
1900 s = d->strings;
1901 strings = s;
1902 }
1903
1904 /* For each language */
1905 data += 16;
1906 len -= 16;
1907
1908 do { /* lang_count > 0 so we can use do-while */
1909 unsigned needed = needed_count;
1910
1911 if (unlikely(len < 3))
1912 goto error_free;
1913 t->language = get_unaligned_le16(data);
1914 t->strings = s;
1915 ++t;
1916
1917 data += 2;
1918 len -= 2;
1919
1920 /* For each string */
1921 do { /* str_count > 0 so we can use do-while */
1922 size_t length = strnlen(data, len);
1923
1924 if (unlikely(length == len))
1925 goto error_free;
1926
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001927 /*
1928 * User may provide more strings then we need,
1929 * if that's the case we simply ignore the
1930 * rest
1931 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001932 if (likely(needed)) {
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001933 /*
1934 * s->id will be set while adding
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001935 * function to configuration so for
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001936 * now just leave garbage here.
1937 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001938 s->s = data;
1939 --needed;
1940 ++s;
1941 }
1942
1943 data += length + 1;
1944 len -= length + 1;
1945 } while (--str_count);
1946
1947 s->id = 0; /* terminator */
1948 s->s = NULL;
1949 ++s;
1950
1951 } while (--lang_count);
1952
1953 /* Some garbage left? */
1954 if (unlikely(len))
1955 goto error_free;
1956
1957 /* Done! */
1958 ffs->stringtabs = stringtabs;
1959 ffs->raw_strings = _data;
1960
1961 return 0;
1962
1963error_free:
1964 kfree(stringtabs);
1965error:
1966 kfree(_data);
1967 return -EINVAL;
1968}
1969
1970
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001971/* Events handling and management *******************************************/
1972
1973static void __ffs_event_add(struct ffs_data *ffs,
1974 enum usb_functionfs_event_type type)
1975{
1976 enum usb_functionfs_event_type rem_type1, rem_type2 = type;
1977 int neg = 0;
1978
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001979 /*
1980 * Abort any unhandled setup
1981 *
1982 * We do not need to worry about some cmpxchg() changing value
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001983 * of ffs->setup_state without holding the lock because when
1984 * state is FFS_SETUP_PENDING cmpxchg() in several places in
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001985 * the source does nothing.
1986 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001987 if (ffs->setup_state == FFS_SETUP_PENDING)
1988 ffs->setup_state = FFS_SETUP_CANCELED;
1989
1990 switch (type) {
1991 case FUNCTIONFS_RESUME:
1992 rem_type2 = FUNCTIONFS_SUSPEND;
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001993 /* FALL THROUGH */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001994 case FUNCTIONFS_SUSPEND:
1995 case FUNCTIONFS_SETUP:
1996 rem_type1 = type;
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001997 /* Discard all similar events */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001998 break;
1999
2000 case FUNCTIONFS_BIND:
2001 case FUNCTIONFS_UNBIND:
2002 case FUNCTIONFS_DISABLE:
2003 case FUNCTIONFS_ENABLE:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002004 /* Discard everything other then power management. */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002005 rem_type1 = FUNCTIONFS_SUSPEND;
2006 rem_type2 = FUNCTIONFS_RESUME;
2007 neg = 1;
2008 break;
2009
2010 default:
2011 BUG();
2012 }
2013
2014 {
2015 u8 *ev = ffs->ev.types, *out = ev;
2016 unsigned n = ffs->ev.count;
2017 for (; n; --n, ++ev)
2018 if ((*ev == rem_type1 || *ev == rem_type2) == neg)
2019 *out++ = *ev;
2020 else
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002021 pr_vdebug("purging event %d\n", *ev);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002022 ffs->ev.count = out - ffs->ev.types;
2023 }
2024
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002025 pr_vdebug("adding event %d\n", type);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002026 ffs->ev.types[ffs->ev.count++] = type;
2027 wake_up_locked(&ffs->ev.waitq);
2028}
2029
2030static void ffs_event_add(struct ffs_data *ffs,
2031 enum usb_functionfs_event_type type)
2032{
2033 unsigned long flags;
2034 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2035 __ffs_event_add(ffs, type);
2036 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2037}
2038
2039
2040/* Bind/unbind USB function hooks *******************************************/
2041
2042static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
2043 struct usb_descriptor_header *desc,
2044 void *priv)
2045{
2046 struct usb_endpoint_descriptor *ds = (void *)desc;
2047 struct ffs_function *func = priv;
2048 struct ffs_ep *ffs_ep;
2049
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002050 /*
2051 * If hs_descriptors is not NULL then we are reading hs
2052 * descriptors now
2053 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002054 const int isHS = func->function.hs_descriptors != NULL;
2055 unsigned idx;
2056
2057 if (type != FFS_DESCRIPTOR)
2058 return 0;
2059
2060 if (isHS)
2061 func->function.hs_descriptors[(long)valuep] = desc;
2062 else
2063 func->function.descriptors[(long)valuep] = desc;
2064
2065 if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT)
2066 return 0;
2067
2068 idx = (ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK) - 1;
2069 ffs_ep = func->eps + idx;
2070
2071 if (unlikely(ffs_ep->descs[isHS])) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002072 pr_vdebug("two %sspeed descriptors for EP %d\n",
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +01002073 isHS ? "high" : "full",
2074 ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002075 return -EINVAL;
2076 }
2077 ffs_ep->descs[isHS] = ds;
2078
2079 ffs_dump_mem(": Original ep desc", ds, ds->bLength);
2080 if (ffs_ep->ep) {
2081 ds->bEndpointAddress = ffs_ep->descs[0]->bEndpointAddress;
2082 if (!ds->wMaxPacketSize)
2083 ds->wMaxPacketSize = ffs_ep->descs[0]->wMaxPacketSize;
2084 } else {
2085 struct usb_request *req;
2086 struct usb_ep *ep;
2087
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002088 pr_vdebug("autoconfig\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002089 ep = usb_ep_autoconfig(func->gadget, ds);
2090 if (unlikely(!ep))
2091 return -ENOTSUPP;
Joe Perchescc7e6052010-11-14 19:04:49 -08002092 ep->driver_data = func->eps + idx;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002093
2094 req = usb_ep_alloc_request(ep, GFP_KERNEL);
2095 if (unlikely(!req))
2096 return -ENOMEM;
2097
2098 ffs_ep->ep = ep;
2099 ffs_ep->req = req;
2100 func->eps_revmap[ds->bEndpointAddress &
2101 USB_ENDPOINT_NUMBER_MASK] = idx + 1;
2102 }
2103 ffs_dump_mem(": Rewritten ep desc", ds, ds->bLength);
2104
2105 return 0;
2106}
2107
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002108static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep,
2109 struct usb_descriptor_header *desc,
2110 void *priv)
2111{
2112 struct ffs_function *func = priv;
2113 unsigned idx;
2114 u8 newValue;
2115
2116 switch (type) {
2117 default:
2118 case FFS_DESCRIPTOR:
2119 /* Handled in previous pass by __ffs_func_bind_do_descs() */
2120 return 0;
2121
2122 case FFS_INTERFACE:
2123 idx = *valuep;
2124 if (func->interfaces_nums[idx] < 0) {
2125 int id = usb_interface_id(func->conf, &func->function);
2126 if (unlikely(id < 0))
2127 return id;
2128 func->interfaces_nums[idx] = id;
2129 }
2130 newValue = func->interfaces_nums[idx];
2131 break;
2132
2133 case FFS_STRING:
2134 /* String' IDs are allocated when fsf_data is bound to cdev */
2135 newValue = func->ffs->stringtabs[0]->strings[*valuep - 1].id;
2136 break;
2137
2138 case FFS_ENDPOINT:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002139 /*
2140 * USB_DT_ENDPOINT are handled in
2141 * __ffs_func_bind_do_descs().
2142 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002143 if (desc->bDescriptorType == USB_DT_ENDPOINT)
2144 return 0;
2145
2146 idx = (*valuep & USB_ENDPOINT_NUMBER_MASK) - 1;
2147 if (unlikely(!func->eps[idx].ep))
2148 return -EINVAL;
2149
2150 {
2151 struct usb_endpoint_descriptor **descs;
2152 descs = func->eps[idx].descs;
2153 newValue = descs[descs[0] ? 0 : 1]->bEndpointAddress;
2154 }
2155 break;
2156 }
2157
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002158 pr_vdebug("%02x -> %02x\n", *valuep, newValue);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002159 *valuep = newValue;
2160 return 0;
2161}
2162
2163static int ffs_func_bind(struct usb_configuration *c,
2164 struct usb_function *f)
2165{
2166 struct ffs_function *func = ffs_func_from_usb(f);
2167 struct ffs_data *ffs = func->ffs;
2168
2169 const int full = !!func->ffs->fs_descs_count;
2170 const int high = gadget_is_dualspeed(func->gadget) &&
2171 func->ffs->hs_descs_count;
2172
2173 int ret;
2174
2175 /* Make it a single chunk, less management later on */
2176 struct {
2177 struct ffs_ep eps[ffs->eps_count];
2178 struct usb_descriptor_header
2179 *fs_descs[full ? ffs->fs_descs_count + 1 : 0];
2180 struct usb_descriptor_header
2181 *hs_descs[high ? ffs->hs_descs_count + 1 : 0];
2182 short inums[ffs->interfaces_count];
2183 char raw_descs[high ? ffs->raw_descs_length
2184 : ffs->raw_fs_descs_length];
2185 } *data;
2186
2187 ENTER();
2188
2189 /* Only high speed but not supported by gadget? */
2190 if (unlikely(!(full | high)))
2191 return -ENOTSUPP;
2192
2193 /* Allocate */
2194 data = kmalloc(sizeof *data, GFP_KERNEL);
2195 if (unlikely(!data))
2196 return -ENOMEM;
2197
2198 /* Zero */
2199 memset(data->eps, 0, sizeof data->eps);
2200 memcpy(data->raw_descs, ffs->raw_descs + 16, sizeof data->raw_descs);
2201 memset(data->inums, 0xff, sizeof data->inums);
2202 for (ret = ffs->eps_count; ret; --ret)
2203 data->eps[ret].num = -1;
2204
2205 /* Save pointers */
2206 func->eps = data->eps;
2207 func->interfaces_nums = data->inums;
2208
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002209 /*
2210 * Go through all the endpoint descriptors and allocate
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002211 * endpoints first, so that later we can rewrite the endpoint
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002212 * numbers without worrying that it may be described later on.
2213 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002214 if (likely(full)) {
2215 func->function.descriptors = data->fs_descs;
2216 ret = ffs_do_descs(ffs->fs_descs_count,
2217 data->raw_descs,
2218 sizeof data->raw_descs,
2219 __ffs_func_bind_do_descs, func);
2220 if (unlikely(ret < 0))
2221 goto error;
2222 } else {
2223 ret = 0;
2224 }
2225
2226 if (likely(high)) {
2227 func->function.hs_descriptors = data->hs_descs;
2228 ret = ffs_do_descs(ffs->hs_descs_count,
2229 data->raw_descs + ret,
2230 (sizeof data->raw_descs) - ret,
2231 __ffs_func_bind_do_descs, func);
2232 }
2233
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002234 /*
2235 * Now handle interface numbers allocation and interface and
2236 * endpoint numbers rewriting. We can do that in one go
2237 * now.
2238 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002239 ret = ffs_do_descs(ffs->fs_descs_count +
2240 (high ? ffs->hs_descs_count : 0),
2241 data->raw_descs, sizeof data->raw_descs,
2242 __ffs_func_bind_do_nums, func);
2243 if (unlikely(ret < 0))
2244 goto error;
2245
2246 /* And we're done */
2247 ffs_event_add(ffs, FUNCTIONFS_BIND);
2248 return 0;
2249
2250error:
2251 /* XXX Do we need to release all claimed endpoints here? */
2252 return ret;
2253}
2254
2255
2256/* Other USB function hooks *************************************************/
2257
2258static void ffs_func_unbind(struct usb_configuration *c,
2259 struct usb_function *f)
2260{
2261 struct ffs_function *func = ffs_func_from_usb(f);
2262 struct ffs_data *ffs = func->ffs;
2263
2264 ENTER();
2265
2266 if (ffs->func == func) {
2267 ffs_func_eps_disable(func);
2268 ffs->func = NULL;
2269 }
2270
2271 ffs_event_add(ffs, FUNCTIONFS_UNBIND);
2272
2273 ffs_func_free(func);
2274}
2275
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002276static int ffs_func_set_alt(struct usb_function *f,
2277 unsigned interface, unsigned alt)
2278{
2279 struct ffs_function *func = ffs_func_from_usb(f);
2280 struct ffs_data *ffs = func->ffs;
2281 int ret = 0, intf;
2282
2283 if (alt != (unsigned)-1) {
2284 intf = ffs_func_revmap_intf(func, interface);
2285 if (unlikely(intf < 0))
2286 return intf;
2287 }
2288
2289 if (ffs->func)
2290 ffs_func_eps_disable(ffs->func);
2291
2292 if (ffs->state != FFS_ACTIVE)
2293 return -ENODEV;
2294
2295 if (alt == (unsigned)-1) {
2296 ffs->func = NULL;
2297 ffs_event_add(ffs, FUNCTIONFS_DISABLE);
2298 return 0;
2299 }
2300
2301 ffs->func = func;
2302 ret = ffs_func_eps_enable(func);
2303 if (likely(ret >= 0))
2304 ffs_event_add(ffs, FUNCTIONFS_ENABLE);
2305 return ret;
2306}
2307
2308static void ffs_func_disable(struct usb_function *f)
2309{
2310 ffs_func_set_alt(f, 0, (unsigned)-1);
2311}
2312
2313static int ffs_func_setup(struct usb_function *f,
2314 const struct usb_ctrlrequest *creq)
2315{
2316 struct ffs_function *func = ffs_func_from_usb(f);
2317 struct ffs_data *ffs = func->ffs;
2318 unsigned long flags;
2319 int ret;
2320
2321 ENTER();
2322
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002323 pr_vdebug("creq->bRequestType = %02x\n", creq->bRequestType);
2324 pr_vdebug("creq->bRequest = %02x\n", creq->bRequest);
2325 pr_vdebug("creq->wValue = %04x\n", le16_to_cpu(creq->wValue));
2326 pr_vdebug("creq->wIndex = %04x\n", le16_to_cpu(creq->wIndex));
2327 pr_vdebug("creq->wLength = %04x\n", le16_to_cpu(creq->wLength));
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002328
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002329 /*
2330 * Most requests directed to interface go through here
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002331 * (notable exceptions are set/get interface) so we need to
2332 * handle them. All other either handled by composite or
2333 * passed to usb_configuration->setup() (if one is set). No
2334 * matter, we will handle requests directed to endpoint here
2335 * as well (as it's straightforward) but what to do with any
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002336 * other request?
2337 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002338 if (ffs->state != FFS_ACTIVE)
2339 return -ENODEV;
2340
2341 switch (creq->bRequestType & USB_RECIP_MASK) {
2342 case USB_RECIP_INTERFACE:
2343 ret = ffs_func_revmap_intf(func, le16_to_cpu(creq->wIndex));
2344 if (unlikely(ret < 0))
2345 return ret;
2346 break;
2347
2348 case USB_RECIP_ENDPOINT:
2349 ret = ffs_func_revmap_ep(func, le16_to_cpu(creq->wIndex));
2350 if (unlikely(ret < 0))
2351 return ret;
2352 break;
2353
2354 default:
2355 return -EOPNOTSUPP;
2356 }
2357
2358 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2359 ffs->ev.setup = *creq;
2360 ffs->ev.setup.wIndex = cpu_to_le16(ret);
2361 __ffs_event_add(ffs, FUNCTIONFS_SETUP);
2362 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2363
2364 return 0;
2365}
2366
2367static void ffs_func_suspend(struct usb_function *f)
2368{
2369 ENTER();
2370 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_SUSPEND);
2371}
2372
2373static void ffs_func_resume(struct usb_function *f)
2374{
2375 ENTER();
2376 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_RESUME);
2377}
2378
2379
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002380/* Endpoint and interface numbers reverse mapping ***************************/
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002381
2382static int ffs_func_revmap_ep(struct ffs_function *func, u8 num)
2383{
2384 num = func->eps_revmap[num & USB_ENDPOINT_NUMBER_MASK];
2385 return num ? num : -EDOM;
2386}
2387
2388static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf)
2389{
2390 short *nums = func->interfaces_nums;
2391 unsigned count = func->ffs->interfaces_count;
2392
2393 for (; count; --count, ++nums) {
2394 if (*nums >= 0 && *nums == intf)
2395 return nums - func->interfaces_nums;
2396 }
2397
2398 return -EDOM;
2399}
2400
2401
2402/* Misc helper functions ****************************************************/
2403
2404static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
2405{
2406 return nonblock
2407 ? likely(mutex_trylock(mutex)) ? 0 : -EAGAIN
2408 : mutex_lock_interruptible(mutex);
2409}
2410
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002411static char *ffs_prepare_buffer(const char * __user buf, size_t len)
2412{
2413 char *data;
2414
2415 if (unlikely(!len))
2416 return NULL;
2417
2418 data = kmalloc(len, GFP_KERNEL);
2419 if (unlikely(!data))
2420 return ERR_PTR(-ENOMEM);
2421
2422 if (unlikely(__copy_from_user(data, buf, len))) {
2423 kfree(data);
2424 return ERR_PTR(-EFAULT);
2425 }
2426
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002427 pr_vdebug("Buffer from user space:\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002428 ffs_dump_mem("", data, len);
2429
2430 return data;
2431}