blob: d187ae73262b30486751d6fdb58bf2c236f0552f [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
Michal Nazarewicz54b83602012-01-13 15:05:16 +01005 * Author: Michal Nazarewicz <mina86@mina86.com>
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02006 *
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.
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020015 */
16
17
18/* #define DEBUG */
19/* #define VERBOSE_DEBUG */
20
21#include <linux/blkdev.h>
Randy Dunlapb0608692010-05-10 10:51:36 -070022#include <linux/pagemap.h>
Paul Gortmakerf940fcd2011-05-27 09:56:31 -040023#include <linux/export.h>
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020024#include <asm/unaligned.h>
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020025
26#include <linux/usb/composite.h>
27#include <linux/usb/functionfs.h>
28
29
30#define FUNCTIONFS_MAGIC 0xa647361 /* Chosen by a honest dice roll ;) */
31
32
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010033/* Debugging ****************************************************************/
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020034
35#ifdef VERBOSE_DEBUG
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +010036# define pr_vdebug pr_debug
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020037# define ffs_dump_mem(prefix, ptr, len) \
Michal Nazarewiczaa02f172010-11-17 17:09:47 +010038 print_hex_dump_bytes(pr_fmt(prefix ": "), DUMP_PREFIX_NONE, ptr, len)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020039#else
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +010040# define pr_vdebug(...) do { } while (0)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020041# define ffs_dump_mem(prefix, ptr, len) do { } while (0)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020042#endif /* VERBOSE_DEBUG */
43
Michal Nazarewiczaa02f172010-11-17 17:09:47 +010044#define ENTER() pr_vdebug("%s()\n", __func__)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020045
46
47/* The data structure and setup file ****************************************/
48
49enum ffs_state {
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010050 /*
51 * Waiting for descriptors and strings.
52 *
53 * In this state no open(2), read(2) or write(2) on epfiles
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020054 * may succeed (which should not be the problem as there
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010055 * should be no such files opened in the first place).
56 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020057 FFS_READ_DESCRIPTORS,
58 FFS_READ_STRINGS,
59
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010060 /*
61 * We've got descriptors and strings. We are or have called
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020062 * functionfs_ready_callback(). functionfs_bind() may have
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010063 * been called but we don't know.
64 *
65 * This is the only state in which operations on epfiles may
66 * succeed.
67 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020068 FFS_ACTIVE,
69
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010070 /*
71 * All endpoints have been closed. This state is also set if
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020072 * we encounter an unrecoverable error. The only
73 * unrecoverable error is situation when after reading strings
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010074 * from user space we fail to initialise epfiles or
75 * functionfs_ready_callback() returns with error (<0).
76 *
77 * In this state no open(2), read(2) or write(2) (both on ep0
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020078 * as well as epfile) may succeed (at this point epfiles are
79 * unlinked and all closed so this is not a problem; ep0 is
80 * also closed but ep0 file exists and so open(2) on ep0 must
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010081 * fail).
82 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020083 FFS_CLOSING
84};
85
86
87enum ffs_setup_state {
88 /* There is no setup request pending. */
89 FFS_NO_SETUP,
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010090 /*
91 * User has read events and there was a setup request event
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020092 * there. The next read/write on ep0 will handle the
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010093 * request.
94 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020095 FFS_SETUP_PENDING,
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010096 /*
97 * There was event pending but before user space handled it
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020098 * some other event was introduced which canceled existing
99 * setup. If this state is set read/write on ep0 return
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100100 * -EIDRM. This state is only set when adding event.
101 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200102 FFS_SETUP_CANCELED
103};
104
105
106
107struct ffs_epfile;
108struct ffs_function;
109
110struct ffs_data {
111 struct usb_gadget *gadget;
112
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100113 /*
114 * Protect access read/write operations, only one read/write
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200115 * at a time. As a consequence protects ep0req and company.
116 * While setup request is being processed (queued) this is
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100117 * held.
118 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200119 struct mutex mutex;
120
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100121 /*
122 * Protect access to endpoint related structures (basically
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200123 * usb_ep_queue(), usb_ep_dequeue(), etc. calls) except for
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100124 * endpoint zero.
125 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200126 spinlock_t eps_lock;
127
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100128 /*
129 * XXX REVISIT do we need our own request? Since we are not
130 * handling setup requests immediately user space may be so
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200131 * slow that another setup will be sent to the gadget but this
132 * time not to us but another function and then there could be
Linus Torvaldsa4ce96a2010-07-21 09:25:42 -0700133 * a race. Is that the case? Or maybe we can use cdev->req
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100134 * after all, maybe we just need some spinlock for that?
135 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200136 struct usb_request *ep0req; /* P: mutex */
137 struct completion ep0req_completion; /* P: mutex */
138 int ep0req_status; /* P: mutex */
139
140 /* reference counter */
141 atomic_t ref;
142 /* how many files are opened (EP0 and others) */
143 atomic_t opened;
144
145 /* EP0 state */
146 enum ffs_state state;
147
148 /*
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100149 * Possible transitions:
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200150 * + FFS_NO_SETUP -> FFS_SETUP_PENDING -- P: ev.waitq.lock
151 * happens only in ep0 read which is P: mutex
152 * + FFS_SETUP_PENDING -> FFS_NO_SETUP -- P: ev.waitq.lock
153 * happens only in ep0 i/o which is P: mutex
154 * + FFS_SETUP_PENDING -> FFS_SETUP_CANCELED -- P: ev.waitq.lock
155 * + FFS_SETUP_CANCELED -> FFS_NO_SETUP -- cmpxchg
156 */
157 enum ffs_setup_state setup_state;
158
159#define FFS_SETUP_STATE(ffs) \
160 ((enum ffs_setup_state)cmpxchg(&(ffs)->setup_state, \
161 FFS_SETUP_CANCELED, FFS_NO_SETUP))
162
163 /* Events & such. */
164 struct {
165 u8 types[4];
166 unsigned short count;
167 /* XXX REVISIT need to update it in some places, or do we? */
168 unsigned short can_stall;
169 struct usb_ctrlrequest setup;
170
171 wait_queue_head_t waitq;
172 } ev; /* the whole structure, P: ev.waitq.lock */
173
174 /* Flags */
175 unsigned long flags;
176#define FFS_FL_CALL_CLOSED_CALLBACK 0
177#define FFS_FL_BOUND 1
178
179 /* Active function */
180 struct ffs_function *func;
181
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100182 /*
183 * Device name, write once when file system is mounted.
184 * Intended for user to read if she wants.
185 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200186 const char *dev_name;
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100187 /* Private data for our user (ie. gadget). Managed by user. */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200188 void *private_data;
189
190 /* filled by __ffs_data_got_descs() */
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100191 /*
192 * Real descriptors are 16 bytes after raw_descs (so you need
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200193 * to skip 16 bytes (ie. ffs->raw_descs + 16) to get to the
194 * first full speed descriptor). raw_descs_length and
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100195 * raw_fs_descs_length do not have those 16 bytes added.
196 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200197 const void *raw_descs;
198 unsigned raw_descs_length;
199 unsigned raw_fs_descs_length;
200 unsigned fs_descs_count;
201 unsigned hs_descs_count;
202
203 unsigned short strings_count;
204 unsigned short interfaces_count;
205 unsigned short eps_count;
206 unsigned short _pad1;
207
208 /* filled by __ffs_data_got_strings() */
209 /* ids in stringtabs are set in functionfs_bind() */
210 const void *raw_strings;
211 struct usb_gadget_strings **stringtabs;
212
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100213 /*
214 * File system's super block, write once when file system is
215 * mounted.
216 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200217 struct super_block *sb;
218
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100219 /* File permissions, written once when fs is mounted */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200220 struct ffs_file_perms {
221 umode_t mode;
222 uid_t uid;
223 gid_t gid;
224 } file_perms;
225
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100226 /*
227 * The endpoint files, filled by ffs_epfiles_create(),
228 * destroyed by ffs_epfiles_destroy().
229 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200230 struct ffs_epfile *epfiles;
231};
232
233/* Reference counter handling */
234static void ffs_data_get(struct ffs_data *ffs);
235static void ffs_data_put(struct ffs_data *ffs);
236/* Creates new ffs_data object. */
237static struct ffs_data *__must_check ffs_data_new(void) __attribute__((malloc));
238
239/* Opened counter handling. */
240static void ffs_data_opened(struct ffs_data *ffs);
241static void ffs_data_closed(struct ffs_data *ffs);
242
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100243/* Called with ffs->mutex held; take over ownership of data. */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200244static int __must_check
245__ffs_data_got_descs(struct ffs_data *ffs, char *data, size_t len);
246static int __must_check
247__ffs_data_got_strings(struct ffs_data *ffs, char *data, size_t len);
248
249
250/* The function structure ***************************************************/
251
252struct ffs_ep;
253
254struct ffs_function {
255 struct usb_configuration *conf;
256 struct usb_gadget *gadget;
257 struct ffs_data *ffs;
258
259 struct ffs_ep *eps;
260 u8 eps_revmap[16];
261 short *interfaces_nums;
262
263 struct usb_function function;
264};
265
266
267static struct ffs_function *ffs_func_from_usb(struct usb_function *f)
268{
269 return container_of(f, struct ffs_function, function);
270}
271
272static void ffs_func_free(struct ffs_function *func);
273
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200274static void ffs_func_eps_disable(struct ffs_function *func);
275static int __must_check ffs_func_eps_enable(struct ffs_function *func);
276
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200277static int ffs_func_bind(struct usb_configuration *,
278 struct usb_function *);
279static void ffs_func_unbind(struct usb_configuration *,
280 struct usb_function *);
281static int ffs_func_set_alt(struct usb_function *, unsigned, unsigned);
282static void ffs_func_disable(struct usb_function *);
283static int ffs_func_setup(struct usb_function *,
284 const struct usb_ctrlrequest *);
285static void ffs_func_suspend(struct usb_function *);
286static void ffs_func_resume(struct usb_function *);
287
288
289static int ffs_func_revmap_ep(struct ffs_function *func, u8 num);
290static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf);
291
292
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200293/* The endpoints structures *************************************************/
294
295struct ffs_ep {
296 struct usb_ep *ep; /* P: ffs->eps_lock */
297 struct usb_request *req; /* P: epfile->mutex */
298
299 /* [0]: full speed, [1]: high speed */
300 struct usb_endpoint_descriptor *descs[2];
301
302 u8 num;
303
304 int status; /* P: epfile->mutex */
305};
306
307struct ffs_epfile {
308 /* Protects ep->ep and ep->req. */
309 struct mutex mutex;
310 wait_queue_head_t wait;
311
312 struct ffs_data *ffs;
313 struct ffs_ep *ep; /* P: ffs->eps_lock */
314
315 struct dentry *dentry;
316
317 char name[5];
318
319 unsigned char in; /* P: ffs->eps_lock */
320 unsigned char isoc; /* P: ffs->eps_lock */
321
322 unsigned char _pad;
323};
324
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200325static int __must_check ffs_epfiles_create(struct ffs_data *ffs);
326static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count);
327
328static struct inode *__must_check
329ffs_sb_create_file(struct super_block *sb, const char *name, void *data,
330 const struct file_operations *fops,
331 struct dentry **dentry_p);
332
333
334/* Misc helper functions ****************************************************/
335
336static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
337 __attribute__((warn_unused_result, nonnull));
338static char *ffs_prepare_buffer(const char * __user buf, size_t len)
339 __attribute__((warn_unused_result, nonnull));
340
341
342/* Control file aka ep0 *****************************************************/
343
344static void ffs_ep0_complete(struct usb_ep *ep, struct usb_request *req)
345{
346 struct ffs_data *ffs = req->context;
347
348 complete_all(&ffs->ep0req_completion);
349}
350
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200351static int __ffs_ep0_queue_wait(struct ffs_data *ffs, char *data, size_t len)
352{
353 struct usb_request *req = ffs->ep0req;
354 int ret;
355
356 req->zero = len < le16_to_cpu(ffs->ev.setup.wLength);
357
358 spin_unlock_irq(&ffs->ev.waitq.lock);
359
360 req->buf = data;
361 req->length = len;
362
Marek Szyprowskice1fd352011-01-28 13:55:36 +0100363 /*
364 * UDC layer requires to provide a buffer even for ZLP, but should
365 * not use it at all. Let's provide some poisoned pointer to catch
366 * possible bug in the driver.
367 */
368 if (req->buf == NULL)
369 req->buf = (void *)0xDEADBABE;
370
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200371 INIT_COMPLETION(ffs->ep0req_completion);
372
373 ret = usb_ep_queue(ffs->gadget->ep0, req, GFP_ATOMIC);
374 if (unlikely(ret < 0))
375 return ret;
376
377 ret = wait_for_completion_interruptible(&ffs->ep0req_completion);
378 if (unlikely(ret)) {
379 usb_ep_dequeue(ffs->gadget->ep0, req);
380 return -EINTR;
381 }
382
383 ffs->setup_state = FFS_NO_SETUP;
384 return ffs->ep0req_status;
385}
386
387static int __ffs_ep0_stall(struct ffs_data *ffs)
388{
389 if (ffs->ev.can_stall) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100390 pr_vdebug("ep0 stall\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200391 usb_ep_set_halt(ffs->gadget->ep0);
392 ffs->setup_state = FFS_NO_SETUP;
393 return -EL2HLT;
394 } else {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100395 pr_debug("bogus ep0 stall!\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200396 return -ESRCH;
397 }
398}
399
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200400static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
401 size_t len, loff_t *ptr)
402{
403 struct ffs_data *ffs = file->private_data;
404 ssize_t ret;
405 char *data;
406
407 ENTER();
408
409 /* Fast check if setup was canceled */
410 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED)
411 return -EIDRM;
412
413 /* Acquire mutex */
414 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
415 if (unlikely(ret < 0))
416 return ret;
417
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200418 /* Check state */
419 switch (ffs->state) {
420 case FFS_READ_DESCRIPTORS:
421 case FFS_READ_STRINGS:
422 /* Copy data */
423 if (unlikely(len < 16)) {
424 ret = -EINVAL;
425 break;
426 }
427
428 data = ffs_prepare_buffer(buf, len);
Tobias Klauser537baab2010-12-09 15:52:39 +0100429 if (IS_ERR(data)) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200430 ret = PTR_ERR(data);
431 break;
432 }
433
434 /* Handle data */
435 if (ffs->state == FFS_READ_DESCRIPTORS) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100436 pr_info("read descriptors\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200437 ret = __ffs_data_got_descs(ffs, data, len);
438 if (unlikely(ret < 0))
439 break;
440
441 ffs->state = FFS_READ_STRINGS;
442 ret = len;
443 } else {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100444 pr_info("read strings\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200445 ret = __ffs_data_got_strings(ffs, data, len);
446 if (unlikely(ret < 0))
447 break;
448
449 ret = ffs_epfiles_create(ffs);
450 if (unlikely(ret)) {
451 ffs->state = FFS_CLOSING;
452 break;
453 }
454
455 ffs->state = FFS_ACTIVE;
456 mutex_unlock(&ffs->mutex);
457
458 ret = functionfs_ready_callback(ffs);
459 if (unlikely(ret < 0)) {
460 ffs->state = FFS_CLOSING;
461 return ret;
462 }
463
464 set_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags);
465 return len;
466 }
467 break;
468
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200469 case FFS_ACTIVE:
470 data = NULL;
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100471 /*
472 * We're called from user space, we can use _irq
473 * rather then _irqsave
474 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200475 spin_lock_irq(&ffs->ev.waitq.lock);
476 switch (FFS_SETUP_STATE(ffs)) {
477 case FFS_SETUP_CANCELED:
478 ret = -EIDRM;
479 goto done_spin;
480
481 case FFS_NO_SETUP:
482 ret = -ESRCH;
483 goto done_spin;
484
485 case FFS_SETUP_PENDING:
486 break;
487 }
488
489 /* FFS_SETUP_PENDING */
490 if (!(ffs->ev.setup.bRequestType & USB_DIR_IN)) {
491 spin_unlock_irq(&ffs->ev.waitq.lock);
492 ret = __ffs_ep0_stall(ffs);
493 break;
494 }
495
496 /* FFS_SETUP_PENDING and not stall */
497 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
498
499 spin_unlock_irq(&ffs->ev.waitq.lock);
500
501 data = ffs_prepare_buffer(buf, len);
Tobias Klauser537baab2010-12-09 15:52:39 +0100502 if (IS_ERR(data)) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200503 ret = PTR_ERR(data);
504 break;
505 }
506
507 spin_lock_irq(&ffs->ev.waitq.lock);
508
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100509 /*
510 * We are guaranteed to be still in FFS_ACTIVE state
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200511 * but the state of setup could have changed from
512 * FFS_SETUP_PENDING to FFS_SETUP_CANCELED so we need
513 * to check for that. If that happened we copied data
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100514 * from user space in vain but it's unlikely.
515 *
516 * For sure we are not in FFS_NO_SETUP since this is
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200517 * the only place FFS_SETUP_PENDING -> FFS_NO_SETUP
518 * transition can be performed and it's protected by
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100519 * mutex.
520 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200521 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) {
522 ret = -EIDRM;
523done_spin:
524 spin_unlock_irq(&ffs->ev.waitq.lock);
525 } else {
526 /* unlocks spinlock */
527 ret = __ffs_ep0_queue_wait(ffs, data, len);
528 }
529 kfree(data);
530 break;
531
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200532 default:
533 ret = -EBADFD;
534 break;
535 }
536
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200537 mutex_unlock(&ffs->mutex);
538 return ret;
539}
540
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200541static ssize_t __ffs_ep0_read_events(struct ffs_data *ffs, char __user *buf,
542 size_t n)
543{
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100544 /*
545 * We are holding ffs->ev.waitq.lock and ffs->mutex and we need
546 * to release them.
547 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200548 struct usb_functionfs_event events[n];
549 unsigned i = 0;
550
551 memset(events, 0, sizeof events);
552
553 do {
554 events[i].type = ffs->ev.types[i];
555 if (events[i].type == FUNCTIONFS_SETUP) {
556 events[i].u.setup = ffs->ev.setup;
557 ffs->setup_state = FFS_SETUP_PENDING;
558 }
559 } while (++i < n);
560
561 if (n < ffs->ev.count) {
562 ffs->ev.count -= n;
563 memmove(ffs->ev.types, ffs->ev.types + n,
564 ffs->ev.count * sizeof *ffs->ev.types);
565 } else {
566 ffs->ev.count = 0;
567 }
568
569 spin_unlock_irq(&ffs->ev.waitq.lock);
570 mutex_unlock(&ffs->mutex);
571
572 return unlikely(__copy_to_user(buf, events, sizeof events))
573 ? -EFAULT : sizeof events;
574}
575
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200576static ssize_t ffs_ep0_read(struct file *file, char __user *buf,
577 size_t len, loff_t *ptr)
578{
579 struct ffs_data *ffs = file->private_data;
580 char *data = NULL;
581 size_t n;
582 int ret;
583
584 ENTER();
585
586 /* Fast check if setup was canceled */
587 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED)
588 return -EIDRM;
589
590 /* Acquire mutex */
591 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
592 if (unlikely(ret < 0))
593 return ret;
594
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200595 /* Check state */
596 if (ffs->state != FFS_ACTIVE) {
597 ret = -EBADFD;
598 goto done_mutex;
599 }
600
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100601 /*
602 * We're called from user space, we can use _irq rather then
603 * _irqsave
604 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200605 spin_lock_irq(&ffs->ev.waitq.lock);
606
607 switch (FFS_SETUP_STATE(ffs)) {
608 case FFS_SETUP_CANCELED:
609 ret = -EIDRM;
610 break;
611
612 case FFS_NO_SETUP:
613 n = len / sizeof(struct usb_functionfs_event);
614 if (unlikely(!n)) {
615 ret = -EINVAL;
616 break;
617 }
618
619 if ((file->f_flags & O_NONBLOCK) && !ffs->ev.count) {
620 ret = -EAGAIN;
621 break;
622 }
623
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100624 if (wait_event_interruptible_exclusive_locked_irq(ffs->ev.waitq,
625 ffs->ev.count)) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200626 ret = -EINTR;
627 break;
628 }
629
630 return __ffs_ep0_read_events(ffs, buf,
631 min(n, (size_t)ffs->ev.count));
632
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200633 case FFS_SETUP_PENDING:
634 if (ffs->ev.setup.bRequestType & USB_DIR_IN) {
635 spin_unlock_irq(&ffs->ev.waitq.lock);
636 ret = __ffs_ep0_stall(ffs);
637 goto done_mutex;
638 }
639
640 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
641
642 spin_unlock_irq(&ffs->ev.waitq.lock);
643
644 if (likely(len)) {
645 data = kmalloc(len, GFP_KERNEL);
646 if (unlikely(!data)) {
647 ret = -ENOMEM;
648 goto done_mutex;
649 }
650 }
651
652 spin_lock_irq(&ffs->ev.waitq.lock);
653
654 /* See ffs_ep0_write() */
655 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) {
656 ret = -EIDRM;
657 break;
658 }
659
660 /* unlocks spinlock */
661 ret = __ffs_ep0_queue_wait(ffs, data, len);
662 if (likely(ret > 0) && unlikely(__copy_to_user(buf, data, len)))
663 ret = -EFAULT;
664 goto done_mutex;
665
666 default:
667 ret = -EBADFD;
668 break;
669 }
670
671 spin_unlock_irq(&ffs->ev.waitq.lock);
672done_mutex:
673 mutex_unlock(&ffs->mutex);
674 kfree(data);
675 return ret;
676}
677
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200678static int ffs_ep0_open(struct inode *inode, struct file *file)
679{
680 struct ffs_data *ffs = inode->i_private;
681
682 ENTER();
683
684 if (unlikely(ffs->state == FFS_CLOSING))
685 return -EBUSY;
686
687 file->private_data = ffs;
688 ffs_data_opened(ffs);
689
690 return 0;
691}
692
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200693static int ffs_ep0_release(struct inode *inode, struct file *file)
694{
695 struct ffs_data *ffs = file->private_data;
696
697 ENTER();
698
699 ffs_data_closed(ffs);
700
701 return 0;
702}
703
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200704static long ffs_ep0_ioctl(struct file *file, unsigned code, unsigned long value)
705{
706 struct ffs_data *ffs = file->private_data;
707 struct usb_gadget *gadget = ffs->gadget;
708 long ret;
709
710 ENTER();
711
712 if (code == FUNCTIONFS_INTERFACE_REVMAP) {
713 struct ffs_function *func = ffs->func;
714 ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV;
715 } else if (gadget->ops->ioctl) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200716 ret = gadget->ops->ioctl(gadget, code, value);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200717 } else {
718 ret = -ENOTTY;
719 }
720
721 return ret;
722}
723
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200724static const struct file_operations ffs_ep0_operations = {
725 .owner = THIS_MODULE,
726 .llseek = no_llseek,
727
728 .open = ffs_ep0_open,
729 .write = ffs_ep0_write,
730 .read = ffs_ep0_read,
731 .release = ffs_ep0_release,
732 .unlocked_ioctl = ffs_ep0_ioctl,
733};
734
735
736/* "Normal" endpoints operations ********************************************/
737
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200738static void ffs_epfile_io_complete(struct usb_ep *_ep, struct usb_request *req)
739{
740 ENTER();
741 if (likely(req->context)) {
742 struct ffs_ep *ep = _ep->driver_data;
743 ep->status = req->status ? req->status : req->actual;
744 complete(req->context);
745 }
746}
747
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200748static ssize_t ffs_epfile_io(struct file *file,
749 char __user *buf, size_t len, int read)
750{
751 struct ffs_epfile *epfile = file->private_data;
752 struct ffs_ep *ep;
753 char *data = NULL;
754 ssize_t ret;
755 int halt;
756
757 goto first_try;
758 do {
759 spin_unlock_irq(&epfile->ffs->eps_lock);
760 mutex_unlock(&epfile->mutex);
761
762first_try:
763 /* Are we still active? */
764 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE)) {
765 ret = -ENODEV;
766 goto error;
767 }
768
769 /* Wait for endpoint to be enabled */
770 ep = epfile->ep;
771 if (!ep) {
772 if (file->f_flags & O_NONBLOCK) {
773 ret = -EAGAIN;
774 goto error;
775 }
776
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100777 if (wait_event_interruptible(epfile->wait,
778 (ep = epfile->ep))) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200779 ret = -EINTR;
780 goto error;
781 }
782 }
783
784 /* Do we halt? */
785 halt = !read == !epfile->in;
786 if (halt && epfile->isoc) {
787 ret = -EINVAL;
788 goto error;
789 }
790
791 /* Allocate & copy */
792 if (!halt && !data) {
793 data = kzalloc(len, GFP_KERNEL);
794 if (unlikely(!data))
795 return -ENOMEM;
796
797 if (!read &&
798 unlikely(__copy_from_user(data, buf, len))) {
799 ret = -EFAULT;
800 goto error;
801 }
802 }
803
804 /* We will be using request */
805 ret = ffs_mutex_lock(&epfile->mutex,
806 file->f_flags & O_NONBLOCK);
807 if (unlikely(ret))
808 goto error;
809
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100810 /*
811 * We're called from user space, we can use _irq rather then
812 * _irqsave
813 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200814 spin_lock_irq(&epfile->ffs->eps_lock);
815
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100816 /*
817 * While we were acquiring mutex endpoint got disabled
818 * or changed?
819 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200820 } while (unlikely(epfile->ep != ep));
821
822 /* Halt */
823 if (unlikely(halt)) {
824 if (likely(epfile->ep == ep) && !WARN_ON(!ep->ep))
825 usb_ep_set_halt(ep->ep);
826 spin_unlock_irq(&epfile->ffs->eps_lock);
827 ret = -EBADMSG;
828 } else {
829 /* Fire the request */
830 DECLARE_COMPLETION_ONSTACK(done);
831
832 struct usb_request *req = ep->req;
833 req->context = &done;
834 req->complete = ffs_epfile_io_complete;
835 req->buf = data;
836 req->length = len;
837
838 ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
839
840 spin_unlock_irq(&epfile->ffs->eps_lock);
841
842 if (unlikely(ret < 0)) {
843 /* nop */
844 } else if (unlikely(wait_for_completion_interruptible(&done))) {
845 ret = -EINTR;
846 usb_ep_dequeue(ep->ep, req);
847 } else {
848 ret = ep->status;
849 if (read && ret > 0 &&
850 unlikely(copy_to_user(buf, data, ret)))
851 ret = -EFAULT;
852 }
853 }
854
855 mutex_unlock(&epfile->mutex);
856error:
857 kfree(data);
858 return ret;
859}
860
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200861static ssize_t
862ffs_epfile_write(struct file *file, const char __user *buf, size_t len,
863 loff_t *ptr)
864{
865 ENTER();
866
867 return ffs_epfile_io(file, (char __user *)buf, len, 0);
868}
869
870static ssize_t
871ffs_epfile_read(struct file *file, char __user *buf, size_t len, loff_t *ptr)
872{
873 ENTER();
874
875 return ffs_epfile_io(file, buf, len, 1);
876}
877
878static int
879ffs_epfile_open(struct inode *inode, struct file *file)
880{
881 struct ffs_epfile *epfile = inode->i_private;
882
883 ENTER();
884
885 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
886 return -ENODEV;
887
888 file->private_data = epfile;
889 ffs_data_opened(epfile->ffs);
890
891 return 0;
892}
893
894static int
895ffs_epfile_release(struct inode *inode, struct file *file)
896{
897 struct ffs_epfile *epfile = inode->i_private;
898
899 ENTER();
900
901 ffs_data_closed(epfile->ffs);
902
903 return 0;
904}
905
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200906static long ffs_epfile_ioctl(struct file *file, unsigned code,
907 unsigned long value)
908{
909 struct ffs_epfile *epfile = file->private_data;
910 int ret;
911
912 ENTER();
913
914 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
915 return -ENODEV;
916
917 spin_lock_irq(&epfile->ffs->eps_lock);
918 if (likely(epfile->ep)) {
919 switch (code) {
920 case FUNCTIONFS_FIFO_STATUS:
921 ret = usb_ep_fifo_status(epfile->ep->ep);
922 break;
923 case FUNCTIONFS_FIFO_FLUSH:
924 usb_ep_fifo_flush(epfile->ep->ep);
925 ret = 0;
926 break;
927 case FUNCTIONFS_CLEAR_HALT:
928 ret = usb_ep_clear_halt(epfile->ep->ep);
929 break;
930 case FUNCTIONFS_ENDPOINT_REVMAP:
931 ret = epfile->ep->num;
932 break;
933 default:
934 ret = -ENOTTY;
935 }
936 } else {
937 ret = -ENODEV;
938 }
939 spin_unlock_irq(&epfile->ffs->eps_lock);
940
941 return ret;
942}
943
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200944static const struct file_operations ffs_epfile_operations = {
945 .owner = THIS_MODULE,
946 .llseek = no_llseek,
947
948 .open = ffs_epfile_open,
949 .write = ffs_epfile_write,
950 .read = ffs_epfile_read,
951 .release = ffs_epfile_release,
952 .unlocked_ioctl = ffs_epfile_ioctl,
953};
954
955
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200956/* File system and super block operations ***********************************/
957
958/*
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100959 * Mounting the file system creates a controller file, used first for
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200960 * function configuration then later for event monitoring.
961 */
962
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200963static struct inode *__must_check
964ffs_sb_make_inode(struct super_block *sb, void *data,
965 const struct file_operations *fops,
966 const struct inode_operations *iops,
967 struct ffs_file_perms *perms)
968{
969 struct inode *inode;
970
971 ENTER();
972
973 inode = new_inode(sb);
974
975 if (likely(inode)) {
976 struct timespec current_time = CURRENT_TIME;
977
Al Viro12ba8d12010-10-27 04:19:36 +0100978 inode->i_ino = get_next_ino();
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200979 inode->i_mode = perms->mode;
980 inode->i_uid = perms->uid;
981 inode->i_gid = perms->gid;
982 inode->i_atime = current_time;
983 inode->i_mtime = current_time;
984 inode->i_ctime = current_time;
985 inode->i_private = data;
986 if (fops)
987 inode->i_fop = fops;
988 if (iops)
989 inode->i_op = iops;
990 }
991
992 return inode;
993}
994
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200995/* Create "regular" file */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200996static struct inode *ffs_sb_create_file(struct super_block *sb,
997 const char *name, void *data,
998 const struct file_operations *fops,
999 struct dentry **dentry_p)
1000{
1001 struct ffs_data *ffs = sb->s_fs_info;
1002 struct dentry *dentry;
1003 struct inode *inode;
1004
1005 ENTER();
1006
1007 dentry = d_alloc_name(sb->s_root, name);
1008 if (unlikely(!dentry))
1009 return NULL;
1010
1011 inode = ffs_sb_make_inode(sb, data, fops, NULL, &ffs->file_perms);
1012 if (unlikely(!inode)) {
1013 dput(dentry);
1014 return NULL;
1015 }
1016
1017 d_add(dentry, inode);
1018 if (dentry_p)
1019 *dentry_p = dentry;
1020
1021 return inode;
1022}
1023
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001024/* Super block */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001025static const struct super_operations ffs_sb_operations = {
1026 .statfs = simple_statfs,
1027 .drop_inode = generic_delete_inode,
1028};
1029
1030struct ffs_sb_fill_data {
1031 struct ffs_file_perms perms;
1032 umode_t root_mode;
1033 const char *dev_name;
1034};
1035
1036static int ffs_sb_fill(struct super_block *sb, void *_data, int silent)
1037{
1038 struct ffs_sb_fill_data *data = _data;
1039 struct inode *inode;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001040 struct ffs_data *ffs;
1041
1042 ENTER();
1043
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001044 /* Initialise data */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001045 ffs = ffs_data_new();
1046 if (unlikely(!ffs))
Al Viro5b5f9562012-01-08 15:38:27 -05001047 goto Enomem;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001048
1049 ffs->sb = sb;
1050 ffs->dev_name = data->dev_name;
1051 ffs->file_perms = data->perms;
1052
1053 sb->s_fs_info = ffs;
1054 sb->s_blocksize = PAGE_CACHE_SIZE;
1055 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1056 sb->s_magic = FUNCTIONFS_MAGIC;
1057 sb->s_op = &ffs_sb_operations;
1058 sb->s_time_gran = 1;
1059
1060 /* Root inode */
1061 data->perms.mode = data->root_mode;
1062 inode = ffs_sb_make_inode(sb, NULL,
1063 &simple_dir_operations,
1064 &simple_dir_inode_operations,
1065 &data->perms);
Al Viro48fde702012-01-08 22:15:13 -05001066 sb->s_root = d_make_root(inode);
1067 if (unlikely(!sb->s_root))
Al Viro5b5f9562012-01-08 15:38:27 -05001068 goto Enomem;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001069
1070 /* EP0 file */
1071 if (unlikely(!ffs_sb_create_file(sb, "ep0", ffs,
1072 &ffs_ep0_operations, NULL)))
Al Viro5b5f9562012-01-08 15:38:27 -05001073 goto Enomem;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001074
1075 return 0;
1076
Al Viro5b5f9562012-01-08 15:38:27 -05001077Enomem:
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001078 return -ENOMEM;
1079}
1080
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001081static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts)
1082{
1083 ENTER();
1084
1085 if (!opts || !*opts)
1086 return 0;
1087
1088 for (;;) {
1089 char *end, *eq, *comma;
1090 unsigned long value;
1091
1092 /* Option limit */
1093 comma = strchr(opts, ',');
1094 if (comma)
1095 *comma = 0;
1096
1097 /* Value limit */
1098 eq = strchr(opts, '=');
1099 if (unlikely(!eq)) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001100 pr_err("'=' missing in %s\n", opts);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001101 return -EINVAL;
1102 }
1103 *eq = 0;
1104
1105 /* Parse value */
1106 value = simple_strtoul(eq + 1, &end, 0);
1107 if (unlikely(*end != ',' && *end != 0)) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001108 pr_err("%s: invalid value: %s\n", opts, eq + 1);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001109 return -EINVAL;
1110 }
1111
1112 /* Interpret option */
1113 switch (eq - opts) {
1114 case 5:
1115 if (!memcmp(opts, "rmode", 5))
1116 data->root_mode = (value & 0555) | S_IFDIR;
1117 else if (!memcmp(opts, "fmode", 5))
1118 data->perms.mode = (value & 0666) | S_IFREG;
1119 else
1120 goto invalid;
1121 break;
1122
1123 case 4:
1124 if (!memcmp(opts, "mode", 4)) {
1125 data->root_mode = (value & 0555) | S_IFDIR;
1126 data->perms.mode = (value & 0666) | S_IFREG;
1127 } else {
1128 goto invalid;
1129 }
1130 break;
1131
1132 case 3:
1133 if (!memcmp(opts, "uid", 3))
1134 data->perms.uid = value;
1135 else if (!memcmp(opts, "gid", 3))
1136 data->perms.gid = value;
1137 else
1138 goto invalid;
1139 break;
1140
1141 default:
1142invalid:
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001143 pr_err("%s: invalid option\n", opts);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001144 return -EINVAL;
1145 }
1146
1147 /* Next iteration */
1148 if (!comma)
1149 break;
1150 opts = comma + 1;
1151 }
1152
1153 return 0;
1154}
1155
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001156/* "mount -t functionfs dev_name /dev/function" ends up here */
1157
Al Virofc14f2f2010-07-25 01:48:30 +04001158static struct dentry *
1159ffs_fs_mount(struct file_system_type *t, int flags,
1160 const char *dev_name, void *opts)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001161{
1162 struct ffs_sb_fill_data data = {
1163 .perms = {
1164 .mode = S_IFREG | 0600,
1165 .uid = 0,
1166 .gid = 0
1167 },
1168 .root_mode = S_IFDIR | 0500,
1169 };
1170 int ret;
1171
1172 ENTER();
1173
1174 ret = functionfs_check_dev_callback(dev_name);
1175 if (unlikely(ret < 0))
Al Virofc14f2f2010-07-25 01:48:30 +04001176 return ERR_PTR(ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001177
1178 ret = ffs_fs_parse_opts(&data, opts);
1179 if (unlikely(ret < 0))
Al Virofc14f2f2010-07-25 01:48:30 +04001180 return ERR_PTR(ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001181
1182 data.dev_name = dev_name;
Al Virofc14f2f2010-07-25 01:48:30 +04001183 return mount_single(t, flags, &data, ffs_sb_fill);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001184}
1185
1186static void
1187ffs_fs_kill_sb(struct super_block *sb)
1188{
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001189 ENTER();
1190
1191 kill_litter_super(sb);
Al Viro5b5f9562012-01-08 15:38:27 -05001192 if (sb->s_fs_info)
1193 ffs_data_put(sb->s_fs_info);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001194}
1195
1196static struct file_system_type ffs_fs_type = {
1197 .owner = THIS_MODULE,
1198 .name = "functionfs",
Al Virofc14f2f2010-07-25 01:48:30 +04001199 .mount = ffs_fs_mount,
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001200 .kill_sb = ffs_fs_kill_sb,
1201};
1202
1203
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001204/* Driver's main init/cleanup functions *************************************/
1205
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001206static int functionfs_init(void)
1207{
1208 int ret;
1209
1210 ENTER();
1211
1212 ret = register_filesystem(&ffs_fs_type);
1213 if (likely(!ret))
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001214 pr_info("file system registered\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001215 else
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001216 pr_err("failed registering file system (%d)\n", ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001217
1218 return ret;
1219}
1220
1221static void functionfs_cleanup(void)
1222{
1223 ENTER();
1224
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001225 pr_info("unloading\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001226 unregister_filesystem(&ffs_fs_type);
1227}
1228
1229
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001230/* ffs_data and ffs_function construction and destruction code **************/
1231
1232static void ffs_data_clear(struct ffs_data *ffs);
1233static void ffs_data_reset(struct ffs_data *ffs);
1234
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001235static void ffs_data_get(struct ffs_data *ffs)
1236{
1237 ENTER();
1238
1239 atomic_inc(&ffs->ref);
1240}
1241
1242static void ffs_data_opened(struct ffs_data *ffs)
1243{
1244 ENTER();
1245
1246 atomic_inc(&ffs->ref);
1247 atomic_inc(&ffs->opened);
1248}
1249
1250static void ffs_data_put(struct ffs_data *ffs)
1251{
1252 ENTER();
1253
1254 if (unlikely(atomic_dec_and_test(&ffs->ref))) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001255 pr_info("%s(): freeing\n", __func__);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001256 ffs_data_clear(ffs);
Andi Kleen647d5582012-03-16 12:01:02 -07001257 BUG_ON(waitqueue_active(&ffs->ev.waitq) ||
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001258 waitqueue_active(&ffs->ep0req_completion.wait));
1259 kfree(ffs);
1260 }
1261}
1262
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001263static void ffs_data_closed(struct ffs_data *ffs)
1264{
1265 ENTER();
1266
1267 if (atomic_dec_and_test(&ffs->opened)) {
1268 ffs->state = FFS_CLOSING;
1269 ffs_data_reset(ffs);
1270 }
1271
1272 ffs_data_put(ffs);
1273}
1274
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001275static struct ffs_data *ffs_data_new(void)
1276{
1277 struct ffs_data *ffs = kzalloc(sizeof *ffs, GFP_KERNEL);
1278 if (unlikely(!ffs))
1279 return 0;
1280
1281 ENTER();
1282
1283 atomic_set(&ffs->ref, 1);
1284 atomic_set(&ffs->opened, 0);
1285 ffs->state = FFS_READ_DESCRIPTORS;
1286 mutex_init(&ffs->mutex);
1287 spin_lock_init(&ffs->eps_lock);
1288 init_waitqueue_head(&ffs->ev.waitq);
1289 init_completion(&ffs->ep0req_completion);
1290
1291 /* XXX REVISIT need to update it in some places, or do we? */
1292 ffs->ev.can_stall = 1;
1293
1294 return ffs;
1295}
1296
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001297static void ffs_data_clear(struct ffs_data *ffs)
1298{
1299 ENTER();
1300
1301 if (test_and_clear_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags))
1302 functionfs_closed_callback(ffs);
1303
1304 BUG_ON(ffs->gadget);
1305
1306 if (ffs->epfiles)
1307 ffs_epfiles_destroy(ffs->epfiles, ffs->eps_count);
1308
1309 kfree(ffs->raw_descs);
1310 kfree(ffs->raw_strings);
1311 kfree(ffs->stringtabs);
1312}
1313
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001314static void ffs_data_reset(struct ffs_data *ffs)
1315{
1316 ENTER();
1317
1318 ffs_data_clear(ffs);
1319
1320 ffs->epfiles = NULL;
1321 ffs->raw_descs = NULL;
1322 ffs->raw_strings = NULL;
1323 ffs->stringtabs = NULL;
1324
1325 ffs->raw_descs_length = 0;
1326 ffs->raw_fs_descs_length = 0;
1327 ffs->fs_descs_count = 0;
1328 ffs->hs_descs_count = 0;
1329
1330 ffs->strings_count = 0;
1331 ffs->interfaces_count = 0;
1332 ffs->eps_count = 0;
1333
1334 ffs->ev.count = 0;
1335
1336 ffs->state = FFS_READ_DESCRIPTORS;
1337 ffs->setup_state = FFS_NO_SETUP;
1338 ffs->flags = 0;
1339}
1340
1341
1342static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev)
1343{
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001344 struct usb_gadget_strings **lang;
1345 int first_id;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001346
1347 ENTER();
1348
1349 if (WARN_ON(ffs->state != FFS_ACTIVE
1350 || test_and_set_bit(FFS_FL_BOUND, &ffs->flags)))
1351 return -EBADFD;
1352
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001353 first_id = usb_string_ids_n(cdev, ffs->strings_count);
1354 if (unlikely(first_id < 0))
1355 return first_id;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001356
1357 ffs->ep0req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
1358 if (unlikely(!ffs->ep0req))
1359 return -ENOMEM;
1360 ffs->ep0req->complete = ffs_ep0_complete;
1361 ffs->ep0req->context = ffs;
1362
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001363 lang = ffs->stringtabs;
1364 for (lang = ffs->stringtabs; *lang; ++lang) {
1365 struct usb_string *str = (*lang)->strings;
1366 int id = first_id;
1367 for (; str->s; ++id, ++str)
1368 str->id = id;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001369 }
1370
1371 ffs->gadget = cdev->gadget;
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001372 ffs_data_get(ffs);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001373 return 0;
1374}
1375
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001376static void functionfs_unbind(struct ffs_data *ffs)
1377{
1378 ENTER();
1379
1380 if (!WARN_ON(!ffs->gadget)) {
1381 usb_ep_free_request(ffs->gadget->ep0, ffs->ep0req);
1382 ffs->ep0req = NULL;
1383 ffs->gadget = NULL;
1384 ffs_data_put(ffs);
Andrzej Pietrasiewicze2190a92012-03-12 12:55:41 +01001385 clear_bit(FFS_FL_BOUND, &ffs->flags);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001386 }
1387}
1388
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001389static int ffs_epfiles_create(struct ffs_data *ffs)
1390{
1391 struct ffs_epfile *epfile, *epfiles;
1392 unsigned i, count;
1393
1394 ENTER();
1395
1396 count = ffs->eps_count;
Thomas Meyer9823a522011-11-29 22:08:00 +01001397 epfiles = kcalloc(count, sizeof(*epfiles), GFP_KERNEL);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001398 if (!epfiles)
1399 return -ENOMEM;
1400
1401 epfile = epfiles;
1402 for (i = 1; i <= count; ++i, ++epfile) {
1403 epfile->ffs = ffs;
1404 mutex_init(&epfile->mutex);
1405 init_waitqueue_head(&epfile->wait);
1406 sprintf(epfiles->name, "ep%u", i);
1407 if (!unlikely(ffs_sb_create_file(ffs->sb, epfiles->name, epfile,
1408 &ffs_epfile_operations,
1409 &epfile->dentry))) {
1410 ffs_epfiles_destroy(epfiles, i - 1);
1411 return -ENOMEM;
1412 }
1413 }
1414
1415 ffs->epfiles = epfiles;
1416 return 0;
1417}
1418
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001419static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count)
1420{
1421 struct ffs_epfile *epfile = epfiles;
1422
1423 ENTER();
1424
1425 for (; count; --count, ++epfile) {
1426 BUG_ON(mutex_is_locked(&epfile->mutex) ||
1427 waitqueue_active(&epfile->wait));
1428 if (epfile->dentry) {
1429 d_delete(epfile->dentry);
1430 dput(epfile->dentry);
1431 epfile->dentry = NULL;
1432 }
1433 }
1434
1435 kfree(epfiles);
1436}
1437
Michal Nazarewicz7898aee2010-06-16 12:07:58 +02001438static int functionfs_bind_config(struct usb_composite_dev *cdev,
1439 struct usb_configuration *c,
1440 struct ffs_data *ffs)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001441{
1442 struct ffs_function *func;
1443 int ret;
1444
1445 ENTER();
1446
1447 func = kzalloc(sizeof *func, GFP_KERNEL);
1448 if (unlikely(!func))
1449 return -ENOMEM;
1450
1451 func->function.name = "Function FS Gadget";
1452 func->function.strings = ffs->stringtabs;
1453
1454 func->function.bind = ffs_func_bind;
1455 func->function.unbind = ffs_func_unbind;
1456 func->function.set_alt = ffs_func_set_alt;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001457 func->function.disable = ffs_func_disable;
1458 func->function.setup = ffs_func_setup;
1459 func->function.suspend = ffs_func_suspend;
1460 func->function.resume = ffs_func_resume;
1461
1462 func->conf = c;
1463 func->gadget = cdev->gadget;
1464 func->ffs = ffs;
1465 ffs_data_get(ffs);
1466
1467 ret = usb_add_function(c, &func->function);
1468 if (unlikely(ret))
1469 ffs_func_free(func);
1470
1471 return ret;
1472}
1473
1474static void ffs_func_free(struct ffs_function *func)
1475{
1476 ENTER();
1477
1478 ffs_data_put(func->ffs);
1479
1480 kfree(func->eps);
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001481 /*
1482 * eps and interfaces_nums are allocated in the same chunk so
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001483 * only one free is required. Descriptors are also allocated
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001484 * in the same chunk.
1485 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001486
1487 kfree(func);
1488}
1489
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001490static void ffs_func_eps_disable(struct ffs_function *func)
1491{
1492 struct ffs_ep *ep = func->eps;
1493 struct ffs_epfile *epfile = func->ffs->epfiles;
1494 unsigned count = func->ffs->eps_count;
1495 unsigned long flags;
1496
1497 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1498 do {
1499 /* pending requests get nuked */
1500 if (likely(ep->ep))
1501 usb_ep_disable(ep->ep);
1502 epfile->ep = NULL;
1503
1504 ++ep;
1505 ++epfile;
1506 } while (--count);
1507 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1508}
1509
1510static int ffs_func_eps_enable(struct ffs_function *func)
1511{
1512 struct ffs_data *ffs = func->ffs;
1513 struct ffs_ep *ep = func->eps;
1514 struct ffs_epfile *epfile = ffs->epfiles;
1515 unsigned count = ffs->eps_count;
1516 unsigned long flags;
1517 int ret = 0;
1518
1519 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1520 do {
1521 struct usb_endpoint_descriptor *ds;
1522 ds = ep->descs[ep->descs[1] ? 1 : 0];
1523
1524 ep->ep->driver_data = ep;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +03001525 ep->ep->desc = ds;
1526 ret = usb_ep_enable(ep->ep);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001527 if (likely(!ret)) {
1528 epfile->ep = ep;
1529 epfile->in = usb_endpoint_dir_in(ds);
1530 epfile->isoc = usb_endpoint_xfer_isoc(ds);
1531 } else {
1532 break;
1533 }
1534
1535 wake_up(&epfile->wait);
1536
1537 ++ep;
1538 ++epfile;
1539 } while (--count);
1540 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1541
1542 return ret;
1543}
1544
1545
1546/* Parsing and building descriptors and strings *****************************/
1547
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001548/*
1549 * This validates if data pointed by data is a valid USB descriptor as
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001550 * well as record how many interfaces, endpoints and strings are
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001551 * required by given configuration. Returns address after the
1552 * descriptor or NULL if data is invalid.
1553 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001554
1555enum ffs_entity_type {
1556 FFS_DESCRIPTOR, FFS_INTERFACE, FFS_STRING, FFS_ENDPOINT
1557};
1558
1559typedef int (*ffs_entity_callback)(enum ffs_entity_type entity,
1560 u8 *valuep,
1561 struct usb_descriptor_header *desc,
1562 void *priv);
1563
1564static int __must_check ffs_do_desc(char *data, unsigned len,
1565 ffs_entity_callback entity, void *priv)
1566{
1567 struct usb_descriptor_header *_ds = (void *)data;
1568 u8 length;
1569 int ret;
1570
1571 ENTER();
1572
1573 /* At least two bytes are required: length and type */
1574 if (len < 2) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001575 pr_vdebug("descriptor too short\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001576 return -EINVAL;
1577 }
1578
1579 /* If we have at least as many bytes as the descriptor takes? */
1580 length = _ds->bLength;
1581 if (len < length) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001582 pr_vdebug("descriptor longer then available data\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001583 return -EINVAL;
1584 }
1585
1586#define __entity_check_INTERFACE(val) 1
1587#define __entity_check_STRING(val) (val)
1588#define __entity_check_ENDPOINT(val) ((val) & USB_ENDPOINT_NUMBER_MASK)
1589#define __entity(type, val) do { \
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001590 pr_vdebug("entity " #type "(%02x)\n", (val)); \
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001591 if (unlikely(!__entity_check_ ##type(val))) { \
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001592 pr_vdebug("invalid entity's value\n"); \
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001593 return -EINVAL; \
1594 } \
1595 ret = entity(FFS_ ##type, &val, _ds, priv); \
1596 if (unlikely(ret < 0)) { \
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001597 pr_debug("entity " #type "(%02x); ret = %d\n", \
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +01001598 (val), ret); \
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001599 return ret; \
1600 } \
1601 } while (0)
1602
1603 /* Parse descriptor depending on type. */
1604 switch (_ds->bDescriptorType) {
1605 case USB_DT_DEVICE:
1606 case USB_DT_CONFIG:
1607 case USB_DT_STRING:
1608 case USB_DT_DEVICE_QUALIFIER:
1609 /* function can't have any of those */
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001610 pr_vdebug("descriptor reserved for gadget: %d\n",
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001611 _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001612 return -EINVAL;
1613
1614 case USB_DT_INTERFACE: {
1615 struct usb_interface_descriptor *ds = (void *)_ds;
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001616 pr_vdebug("interface descriptor\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001617 if (length != sizeof *ds)
1618 goto inv_length;
1619
1620 __entity(INTERFACE, ds->bInterfaceNumber);
1621 if (ds->iInterface)
1622 __entity(STRING, ds->iInterface);
1623 }
1624 break;
1625
1626 case USB_DT_ENDPOINT: {
1627 struct usb_endpoint_descriptor *ds = (void *)_ds;
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001628 pr_vdebug("endpoint descriptor\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001629 if (length != USB_DT_ENDPOINT_SIZE &&
1630 length != USB_DT_ENDPOINT_AUDIO_SIZE)
1631 goto inv_length;
1632 __entity(ENDPOINT, ds->bEndpointAddress);
1633 }
1634 break;
1635
1636 case USB_DT_OTG:
1637 if (length != sizeof(struct usb_otg_descriptor))
1638 goto inv_length;
1639 break;
1640
1641 case USB_DT_INTERFACE_ASSOCIATION: {
1642 struct usb_interface_assoc_descriptor *ds = (void *)_ds;
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001643 pr_vdebug("interface association descriptor\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001644 if (length != sizeof *ds)
1645 goto inv_length;
1646 if (ds->iFunction)
1647 __entity(STRING, ds->iFunction);
1648 }
1649 break;
1650
1651 case USB_DT_OTHER_SPEED_CONFIG:
1652 case USB_DT_INTERFACE_POWER:
1653 case USB_DT_DEBUG:
1654 case USB_DT_SECURITY:
1655 case USB_DT_CS_RADIO_CONTROL:
1656 /* TODO */
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001657 pr_vdebug("unimplemented descriptor: %d\n", _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001658 return -EINVAL;
1659
1660 default:
1661 /* We should never be here */
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001662 pr_vdebug("unknown descriptor: %d\n", _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001663 return -EINVAL;
1664
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001665inv_length:
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001666 pr_vdebug("invalid length: %d (descriptor %d)\n",
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +01001667 _ds->bLength, _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001668 return -EINVAL;
1669 }
1670
1671#undef __entity
1672#undef __entity_check_DESCRIPTOR
1673#undef __entity_check_INTERFACE
1674#undef __entity_check_STRING
1675#undef __entity_check_ENDPOINT
1676
1677 return length;
1678}
1679
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001680static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len,
1681 ffs_entity_callback entity, void *priv)
1682{
1683 const unsigned _len = len;
1684 unsigned long num = 0;
1685
1686 ENTER();
1687
1688 for (;;) {
1689 int ret;
1690
1691 if (num == count)
1692 data = NULL;
1693
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001694 /* Record "descriptor" entity */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001695 ret = entity(FFS_DESCRIPTOR, (u8 *)num, (void *)data, priv);
1696 if (unlikely(ret < 0)) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001697 pr_debug("entity DESCRIPTOR(%02lx); ret = %d\n",
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +01001698 num, ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001699 return ret;
1700 }
1701
1702 if (!data)
1703 return _len - len;
1704
1705 ret = ffs_do_desc(data, len, entity, priv);
1706 if (unlikely(ret < 0)) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001707 pr_debug("%s returns %d\n", __func__, ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001708 return ret;
1709 }
1710
1711 len -= ret;
1712 data += ret;
1713 ++num;
1714 }
1715}
1716
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001717static int __ffs_data_do_entity(enum ffs_entity_type type,
1718 u8 *valuep, struct usb_descriptor_header *desc,
1719 void *priv)
1720{
1721 struct ffs_data *ffs = priv;
1722
1723 ENTER();
1724
1725 switch (type) {
1726 case FFS_DESCRIPTOR:
1727 break;
1728
1729 case FFS_INTERFACE:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001730 /*
1731 * Interfaces are indexed from zero so if we
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001732 * encountered interface "n" then there are at least
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001733 * "n+1" interfaces.
1734 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001735 if (*valuep >= ffs->interfaces_count)
1736 ffs->interfaces_count = *valuep + 1;
1737 break;
1738
1739 case FFS_STRING:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001740 /*
1741 * Strings are indexed from 1 (0 is magic ;) reserved
1742 * for languages list or some such)
1743 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001744 if (*valuep > ffs->strings_count)
1745 ffs->strings_count = *valuep;
1746 break;
1747
1748 case FFS_ENDPOINT:
1749 /* Endpoints are indexed from 1 as well. */
1750 if ((*valuep & USB_ENDPOINT_NUMBER_MASK) > ffs->eps_count)
1751 ffs->eps_count = (*valuep & USB_ENDPOINT_NUMBER_MASK);
1752 break;
1753 }
1754
1755 return 0;
1756}
1757
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001758static int __ffs_data_got_descs(struct ffs_data *ffs,
1759 char *const _data, size_t len)
1760{
1761 unsigned fs_count, hs_count;
1762 int fs_len, ret = -EINVAL;
1763 char *data = _data;
1764
1765 ENTER();
1766
1767 if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_DESCRIPTORS_MAGIC ||
1768 get_unaligned_le32(data + 4) != len))
1769 goto error;
1770 fs_count = get_unaligned_le32(data + 8);
1771 hs_count = get_unaligned_le32(data + 12);
1772
1773 if (!fs_count && !hs_count)
1774 goto einval;
1775
1776 data += 16;
1777 len -= 16;
1778
1779 if (likely(fs_count)) {
1780 fs_len = ffs_do_descs(fs_count, data, len,
1781 __ffs_data_do_entity, ffs);
1782 if (unlikely(fs_len < 0)) {
1783 ret = fs_len;
1784 goto error;
1785 }
1786
1787 data += fs_len;
1788 len -= fs_len;
1789 } else {
1790 fs_len = 0;
1791 }
1792
1793 if (likely(hs_count)) {
1794 ret = ffs_do_descs(hs_count, data, len,
1795 __ffs_data_do_entity, ffs);
1796 if (unlikely(ret < 0))
1797 goto error;
1798 } else {
1799 ret = 0;
1800 }
1801
1802 if (unlikely(len != ret))
1803 goto einval;
1804
1805 ffs->raw_fs_descs_length = fs_len;
1806 ffs->raw_descs_length = fs_len + ret;
1807 ffs->raw_descs = _data;
1808 ffs->fs_descs_count = fs_count;
1809 ffs->hs_descs_count = hs_count;
1810
1811 return 0;
1812
1813einval:
1814 ret = -EINVAL;
1815error:
1816 kfree(_data);
1817 return ret;
1818}
1819
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001820static int __ffs_data_got_strings(struct ffs_data *ffs,
1821 char *const _data, size_t len)
1822{
1823 u32 str_count, needed_count, lang_count;
1824 struct usb_gadget_strings **stringtabs, *t;
1825 struct usb_string *strings, *s;
1826 const char *data = _data;
1827
1828 ENTER();
1829
1830 if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_STRINGS_MAGIC ||
1831 get_unaligned_le32(data + 4) != len))
1832 goto error;
1833 str_count = get_unaligned_le32(data + 8);
1834 lang_count = get_unaligned_le32(data + 12);
1835
1836 /* if one is zero the other must be zero */
1837 if (unlikely(!str_count != !lang_count))
1838 goto error;
1839
1840 /* Do we have at least as many strings as descriptors need? */
1841 needed_count = ffs->strings_count;
1842 if (unlikely(str_count < needed_count))
1843 goto error;
1844
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001845 /*
1846 * If we don't need any strings just return and free all
1847 * memory.
1848 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001849 if (!needed_count) {
1850 kfree(_data);
1851 return 0;
1852 }
1853
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001854 /* Allocate everything in one chunk so there's less maintenance. */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001855 {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001856 struct {
1857 struct usb_gadget_strings *stringtabs[lang_count + 1];
1858 struct usb_gadget_strings stringtab[lang_count];
1859 struct usb_string strings[lang_count*(needed_count+1)];
1860 } *d;
1861 unsigned i = 0;
1862
1863 d = kmalloc(sizeof *d, GFP_KERNEL);
1864 if (unlikely(!d)) {
1865 kfree(_data);
1866 return -ENOMEM;
1867 }
1868
1869 stringtabs = d->stringtabs;
1870 t = d->stringtab;
1871 i = lang_count;
1872 do {
1873 *stringtabs++ = t++;
1874 } while (--i);
1875 *stringtabs = NULL;
1876
1877 stringtabs = d->stringtabs;
1878 t = d->stringtab;
1879 s = d->strings;
1880 strings = s;
1881 }
1882
1883 /* For each language */
1884 data += 16;
1885 len -= 16;
1886
1887 do { /* lang_count > 0 so we can use do-while */
1888 unsigned needed = needed_count;
1889
1890 if (unlikely(len < 3))
1891 goto error_free;
1892 t->language = get_unaligned_le16(data);
1893 t->strings = s;
1894 ++t;
1895
1896 data += 2;
1897 len -= 2;
1898
1899 /* For each string */
1900 do { /* str_count > 0 so we can use do-while */
1901 size_t length = strnlen(data, len);
1902
1903 if (unlikely(length == len))
1904 goto error_free;
1905
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001906 /*
1907 * User may provide more strings then we need,
1908 * if that's the case we simply ignore the
1909 * rest
1910 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001911 if (likely(needed)) {
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001912 /*
1913 * s->id will be set while adding
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001914 * function to configuration so for
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001915 * now just leave garbage here.
1916 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001917 s->s = data;
1918 --needed;
1919 ++s;
1920 }
1921
1922 data += length + 1;
1923 len -= length + 1;
1924 } while (--str_count);
1925
1926 s->id = 0; /* terminator */
1927 s->s = NULL;
1928 ++s;
1929
1930 } while (--lang_count);
1931
1932 /* Some garbage left? */
1933 if (unlikely(len))
1934 goto error_free;
1935
1936 /* Done! */
1937 ffs->stringtabs = stringtabs;
1938 ffs->raw_strings = _data;
1939
1940 return 0;
1941
1942error_free:
1943 kfree(stringtabs);
1944error:
1945 kfree(_data);
1946 return -EINVAL;
1947}
1948
1949
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001950/* Events handling and management *******************************************/
1951
1952static void __ffs_event_add(struct ffs_data *ffs,
1953 enum usb_functionfs_event_type type)
1954{
1955 enum usb_functionfs_event_type rem_type1, rem_type2 = type;
1956 int neg = 0;
1957
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001958 /*
1959 * Abort any unhandled setup
1960 *
1961 * We do not need to worry about some cmpxchg() changing value
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001962 * of ffs->setup_state without holding the lock because when
1963 * state is FFS_SETUP_PENDING cmpxchg() in several places in
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001964 * the source does nothing.
1965 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001966 if (ffs->setup_state == FFS_SETUP_PENDING)
1967 ffs->setup_state = FFS_SETUP_CANCELED;
1968
1969 switch (type) {
1970 case FUNCTIONFS_RESUME:
1971 rem_type2 = FUNCTIONFS_SUSPEND;
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001972 /* FALL THROUGH */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001973 case FUNCTIONFS_SUSPEND:
1974 case FUNCTIONFS_SETUP:
1975 rem_type1 = type;
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001976 /* Discard all similar events */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001977 break;
1978
1979 case FUNCTIONFS_BIND:
1980 case FUNCTIONFS_UNBIND:
1981 case FUNCTIONFS_DISABLE:
1982 case FUNCTIONFS_ENABLE:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001983 /* Discard everything other then power management. */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001984 rem_type1 = FUNCTIONFS_SUSPEND;
1985 rem_type2 = FUNCTIONFS_RESUME;
1986 neg = 1;
1987 break;
1988
1989 default:
1990 BUG();
1991 }
1992
1993 {
1994 u8 *ev = ffs->ev.types, *out = ev;
1995 unsigned n = ffs->ev.count;
1996 for (; n; --n, ++ev)
1997 if ((*ev == rem_type1 || *ev == rem_type2) == neg)
1998 *out++ = *ev;
1999 else
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002000 pr_vdebug("purging event %d\n", *ev);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002001 ffs->ev.count = out - ffs->ev.types;
2002 }
2003
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002004 pr_vdebug("adding event %d\n", type);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002005 ffs->ev.types[ffs->ev.count++] = type;
2006 wake_up_locked(&ffs->ev.waitq);
2007}
2008
2009static void ffs_event_add(struct ffs_data *ffs,
2010 enum usb_functionfs_event_type type)
2011{
2012 unsigned long flags;
2013 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2014 __ffs_event_add(ffs, type);
2015 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2016}
2017
2018
2019/* Bind/unbind USB function hooks *******************************************/
2020
2021static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
2022 struct usb_descriptor_header *desc,
2023 void *priv)
2024{
2025 struct usb_endpoint_descriptor *ds = (void *)desc;
2026 struct ffs_function *func = priv;
2027 struct ffs_ep *ffs_ep;
2028
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002029 /*
2030 * If hs_descriptors is not NULL then we are reading hs
2031 * descriptors now
2032 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002033 const int isHS = func->function.hs_descriptors != NULL;
2034 unsigned idx;
2035
2036 if (type != FFS_DESCRIPTOR)
2037 return 0;
2038
2039 if (isHS)
2040 func->function.hs_descriptors[(long)valuep] = desc;
2041 else
2042 func->function.descriptors[(long)valuep] = desc;
2043
2044 if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT)
2045 return 0;
2046
2047 idx = (ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK) - 1;
2048 ffs_ep = func->eps + idx;
2049
2050 if (unlikely(ffs_ep->descs[isHS])) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002051 pr_vdebug("two %sspeed descriptors for EP %d\n",
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +01002052 isHS ? "high" : "full",
2053 ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002054 return -EINVAL;
2055 }
2056 ffs_ep->descs[isHS] = ds;
2057
2058 ffs_dump_mem(": Original ep desc", ds, ds->bLength);
2059 if (ffs_ep->ep) {
2060 ds->bEndpointAddress = ffs_ep->descs[0]->bEndpointAddress;
2061 if (!ds->wMaxPacketSize)
2062 ds->wMaxPacketSize = ffs_ep->descs[0]->wMaxPacketSize;
2063 } else {
2064 struct usb_request *req;
2065 struct usb_ep *ep;
2066
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002067 pr_vdebug("autoconfig\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002068 ep = usb_ep_autoconfig(func->gadget, ds);
2069 if (unlikely(!ep))
2070 return -ENOTSUPP;
Joe Perchescc7e60562010-11-14 19:04:49 -08002071 ep->driver_data = func->eps + idx;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002072
2073 req = usb_ep_alloc_request(ep, GFP_KERNEL);
2074 if (unlikely(!req))
2075 return -ENOMEM;
2076
2077 ffs_ep->ep = ep;
2078 ffs_ep->req = req;
2079 func->eps_revmap[ds->bEndpointAddress &
2080 USB_ENDPOINT_NUMBER_MASK] = idx + 1;
2081 }
2082 ffs_dump_mem(": Rewritten ep desc", ds, ds->bLength);
2083
2084 return 0;
2085}
2086
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002087static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep,
2088 struct usb_descriptor_header *desc,
2089 void *priv)
2090{
2091 struct ffs_function *func = priv;
2092 unsigned idx;
2093 u8 newValue;
2094
2095 switch (type) {
2096 default:
2097 case FFS_DESCRIPTOR:
2098 /* Handled in previous pass by __ffs_func_bind_do_descs() */
2099 return 0;
2100
2101 case FFS_INTERFACE:
2102 idx = *valuep;
2103 if (func->interfaces_nums[idx] < 0) {
2104 int id = usb_interface_id(func->conf, &func->function);
2105 if (unlikely(id < 0))
2106 return id;
2107 func->interfaces_nums[idx] = id;
2108 }
2109 newValue = func->interfaces_nums[idx];
2110 break;
2111
2112 case FFS_STRING:
2113 /* String' IDs are allocated when fsf_data is bound to cdev */
2114 newValue = func->ffs->stringtabs[0]->strings[*valuep - 1].id;
2115 break;
2116
2117 case FFS_ENDPOINT:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002118 /*
2119 * USB_DT_ENDPOINT are handled in
2120 * __ffs_func_bind_do_descs().
2121 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002122 if (desc->bDescriptorType == USB_DT_ENDPOINT)
2123 return 0;
2124
2125 idx = (*valuep & USB_ENDPOINT_NUMBER_MASK) - 1;
2126 if (unlikely(!func->eps[idx].ep))
2127 return -EINVAL;
2128
2129 {
2130 struct usb_endpoint_descriptor **descs;
2131 descs = func->eps[idx].descs;
2132 newValue = descs[descs[0] ? 0 : 1]->bEndpointAddress;
2133 }
2134 break;
2135 }
2136
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002137 pr_vdebug("%02x -> %02x\n", *valuep, newValue);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002138 *valuep = newValue;
2139 return 0;
2140}
2141
2142static int ffs_func_bind(struct usb_configuration *c,
2143 struct usb_function *f)
2144{
2145 struct ffs_function *func = ffs_func_from_usb(f);
2146 struct ffs_data *ffs = func->ffs;
2147
2148 const int full = !!func->ffs->fs_descs_count;
2149 const int high = gadget_is_dualspeed(func->gadget) &&
2150 func->ffs->hs_descs_count;
2151
2152 int ret;
2153
2154 /* Make it a single chunk, less management later on */
2155 struct {
2156 struct ffs_ep eps[ffs->eps_count];
2157 struct usb_descriptor_header
2158 *fs_descs[full ? ffs->fs_descs_count + 1 : 0];
2159 struct usb_descriptor_header
2160 *hs_descs[high ? ffs->hs_descs_count + 1 : 0];
2161 short inums[ffs->interfaces_count];
2162 char raw_descs[high ? ffs->raw_descs_length
2163 : ffs->raw_fs_descs_length];
2164 } *data;
2165
2166 ENTER();
2167
2168 /* Only high speed but not supported by gadget? */
2169 if (unlikely(!(full | high)))
2170 return -ENOTSUPP;
2171
2172 /* Allocate */
2173 data = kmalloc(sizeof *data, GFP_KERNEL);
2174 if (unlikely(!data))
2175 return -ENOMEM;
2176
2177 /* Zero */
2178 memset(data->eps, 0, sizeof data->eps);
2179 memcpy(data->raw_descs, ffs->raw_descs + 16, sizeof data->raw_descs);
2180 memset(data->inums, 0xff, sizeof data->inums);
2181 for (ret = ffs->eps_count; ret; --ret)
2182 data->eps[ret].num = -1;
2183
2184 /* Save pointers */
2185 func->eps = data->eps;
2186 func->interfaces_nums = data->inums;
2187
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002188 /*
2189 * Go through all the endpoint descriptors and allocate
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002190 * endpoints first, so that later we can rewrite the endpoint
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002191 * numbers without worrying that it may be described later on.
2192 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002193 if (likely(full)) {
2194 func->function.descriptors = data->fs_descs;
2195 ret = ffs_do_descs(ffs->fs_descs_count,
2196 data->raw_descs,
2197 sizeof data->raw_descs,
2198 __ffs_func_bind_do_descs, func);
2199 if (unlikely(ret < 0))
2200 goto error;
2201 } else {
2202 ret = 0;
2203 }
2204
2205 if (likely(high)) {
2206 func->function.hs_descriptors = data->hs_descs;
2207 ret = ffs_do_descs(ffs->hs_descs_count,
2208 data->raw_descs + ret,
2209 (sizeof data->raw_descs) - ret,
2210 __ffs_func_bind_do_descs, func);
2211 }
2212
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002213 /*
2214 * Now handle interface numbers allocation and interface and
2215 * endpoint numbers rewriting. We can do that in one go
2216 * now.
2217 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002218 ret = ffs_do_descs(ffs->fs_descs_count +
2219 (high ? ffs->hs_descs_count : 0),
2220 data->raw_descs, sizeof data->raw_descs,
2221 __ffs_func_bind_do_nums, func);
2222 if (unlikely(ret < 0))
2223 goto error;
2224
2225 /* And we're done */
2226 ffs_event_add(ffs, FUNCTIONFS_BIND);
2227 return 0;
2228
2229error:
2230 /* XXX Do we need to release all claimed endpoints here? */
2231 return ret;
2232}
2233
2234
2235/* Other USB function hooks *************************************************/
2236
2237static void ffs_func_unbind(struct usb_configuration *c,
2238 struct usb_function *f)
2239{
2240 struct ffs_function *func = ffs_func_from_usb(f);
2241 struct ffs_data *ffs = func->ffs;
2242
2243 ENTER();
2244
2245 if (ffs->func == func) {
2246 ffs_func_eps_disable(func);
2247 ffs->func = NULL;
2248 }
2249
2250 ffs_event_add(ffs, FUNCTIONFS_UNBIND);
2251
2252 ffs_func_free(func);
2253}
2254
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002255static int ffs_func_set_alt(struct usb_function *f,
2256 unsigned interface, unsigned alt)
2257{
2258 struct ffs_function *func = ffs_func_from_usb(f);
2259 struct ffs_data *ffs = func->ffs;
2260 int ret = 0, intf;
2261
2262 if (alt != (unsigned)-1) {
2263 intf = ffs_func_revmap_intf(func, interface);
2264 if (unlikely(intf < 0))
2265 return intf;
2266 }
2267
2268 if (ffs->func)
2269 ffs_func_eps_disable(ffs->func);
2270
2271 if (ffs->state != FFS_ACTIVE)
2272 return -ENODEV;
2273
2274 if (alt == (unsigned)-1) {
2275 ffs->func = NULL;
2276 ffs_event_add(ffs, FUNCTIONFS_DISABLE);
2277 return 0;
2278 }
2279
2280 ffs->func = func;
2281 ret = ffs_func_eps_enable(func);
2282 if (likely(ret >= 0))
2283 ffs_event_add(ffs, FUNCTIONFS_ENABLE);
2284 return ret;
2285}
2286
2287static void ffs_func_disable(struct usb_function *f)
2288{
2289 ffs_func_set_alt(f, 0, (unsigned)-1);
2290}
2291
2292static int ffs_func_setup(struct usb_function *f,
2293 const struct usb_ctrlrequest *creq)
2294{
2295 struct ffs_function *func = ffs_func_from_usb(f);
2296 struct ffs_data *ffs = func->ffs;
2297 unsigned long flags;
2298 int ret;
2299
2300 ENTER();
2301
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002302 pr_vdebug("creq->bRequestType = %02x\n", creq->bRequestType);
2303 pr_vdebug("creq->bRequest = %02x\n", creq->bRequest);
2304 pr_vdebug("creq->wValue = %04x\n", le16_to_cpu(creq->wValue));
2305 pr_vdebug("creq->wIndex = %04x\n", le16_to_cpu(creq->wIndex));
2306 pr_vdebug("creq->wLength = %04x\n", le16_to_cpu(creq->wLength));
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002307
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002308 /*
2309 * Most requests directed to interface go through here
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002310 * (notable exceptions are set/get interface) so we need to
2311 * handle them. All other either handled by composite or
2312 * passed to usb_configuration->setup() (if one is set). No
2313 * matter, we will handle requests directed to endpoint here
2314 * as well (as it's straightforward) but what to do with any
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002315 * other request?
2316 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002317 if (ffs->state != FFS_ACTIVE)
2318 return -ENODEV;
2319
2320 switch (creq->bRequestType & USB_RECIP_MASK) {
2321 case USB_RECIP_INTERFACE:
2322 ret = ffs_func_revmap_intf(func, le16_to_cpu(creq->wIndex));
2323 if (unlikely(ret < 0))
2324 return ret;
2325 break;
2326
2327 case USB_RECIP_ENDPOINT:
2328 ret = ffs_func_revmap_ep(func, le16_to_cpu(creq->wIndex));
2329 if (unlikely(ret < 0))
2330 return ret;
2331 break;
2332
2333 default:
2334 return -EOPNOTSUPP;
2335 }
2336
2337 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2338 ffs->ev.setup = *creq;
2339 ffs->ev.setup.wIndex = cpu_to_le16(ret);
2340 __ffs_event_add(ffs, FUNCTIONFS_SETUP);
2341 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2342
2343 return 0;
2344}
2345
2346static void ffs_func_suspend(struct usb_function *f)
2347{
2348 ENTER();
2349 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_SUSPEND);
2350}
2351
2352static void ffs_func_resume(struct usb_function *f)
2353{
2354 ENTER();
2355 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_RESUME);
2356}
2357
2358
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002359/* Endpoint and interface numbers reverse mapping ***************************/
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002360
2361static int ffs_func_revmap_ep(struct ffs_function *func, u8 num)
2362{
2363 num = func->eps_revmap[num & USB_ENDPOINT_NUMBER_MASK];
2364 return num ? num : -EDOM;
2365}
2366
2367static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf)
2368{
2369 short *nums = func->interfaces_nums;
2370 unsigned count = func->ffs->interfaces_count;
2371
2372 for (; count; --count, ++nums) {
2373 if (*nums >= 0 && *nums == intf)
2374 return nums - func->interfaces_nums;
2375 }
2376
2377 return -EDOM;
2378}
2379
2380
2381/* Misc helper functions ****************************************************/
2382
2383static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
2384{
2385 return nonblock
2386 ? likely(mutex_trylock(mutex)) ? 0 : -EAGAIN
2387 : mutex_lock_interruptible(mutex);
2388}
2389
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002390static char *ffs_prepare_buffer(const char * __user buf, size_t len)
2391{
2392 char *data;
2393
2394 if (unlikely(!len))
2395 return NULL;
2396
2397 data = kmalloc(len, GFP_KERNEL);
2398 if (unlikely(!data))
2399 return ERR_PTR(-ENOMEM);
2400
2401 if (unlikely(__copy_from_user(data, buf, len))) {
2402 kfree(data);
2403 return ERR_PTR(-EFAULT);
2404 }
2405
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002406 pr_vdebug("Buffer from user space:\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002407 ffs_dump_mem("", data, len);
2408
2409 return data;
2410}