blob: f9ee4e08a4a8c4a03562b94ccf694256a3e0e800 [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>
Koen Beel560f1182012-05-30 20:43:37 +020024#include <linux/hid.h>
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020025#include <asm/unaligned.h>
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020026
27#include <linux/usb/composite.h>
28#include <linux/usb/functionfs.h>
29
30
31#define FUNCTIONFS_MAGIC 0xa647361 /* Chosen by a honest dice roll ;) */
32
33
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010034/* Debugging ****************************************************************/
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020035
36#ifdef VERBOSE_DEBUG
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +010037# define pr_vdebug pr_debug
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020038# define ffs_dump_mem(prefix, ptr, len) \
Michal Nazarewiczaa02f172010-11-17 17:09:47 +010039 print_hex_dump_bytes(pr_fmt(prefix ": "), DUMP_PREFIX_NONE, ptr, len)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020040#else
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +010041# define pr_vdebug(...) do { } while (0)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020042# define ffs_dump_mem(prefix, ptr, len) do { } while (0)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020043#endif /* VERBOSE_DEBUG */
44
Michal Nazarewiczaa02f172010-11-17 17:09:47 +010045#define ENTER() pr_vdebug("%s()\n", __func__)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020046
47
48/* The data structure and setup file ****************************************/
49
50enum ffs_state {
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010051 /*
52 * Waiting for descriptors and strings.
53 *
54 * In this state no open(2), read(2) or write(2) on epfiles
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020055 * may succeed (which should not be the problem as there
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010056 * should be no such files opened in the first place).
57 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020058 FFS_READ_DESCRIPTORS,
59 FFS_READ_STRINGS,
60
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010061 /*
62 * We've got descriptors and strings. We are or have called
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020063 * functionfs_ready_callback(). functionfs_bind() may have
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010064 * been called but we don't know.
65 *
66 * This is the only state in which operations on epfiles may
67 * succeed.
68 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020069 FFS_ACTIVE,
70
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010071 /*
72 * All endpoints have been closed. This state is also set if
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020073 * we encounter an unrecoverable error. The only
74 * unrecoverable error is situation when after reading strings
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010075 * from user space we fail to initialise epfiles or
76 * functionfs_ready_callback() returns with error (<0).
77 *
78 * In this state no open(2), read(2) or write(2) (both on ep0
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020079 * as well as epfile) may succeed (at this point epfiles are
80 * unlinked and all closed so this is not a problem; ep0 is
81 * also closed but ep0 file exists and so open(2) on ep0 must
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010082 * fail).
83 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020084 FFS_CLOSING
85};
86
87
88enum ffs_setup_state {
89 /* There is no setup request pending. */
90 FFS_NO_SETUP,
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010091 /*
92 * User has read events and there was a setup request event
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020093 * there. The next read/write on ep0 will handle the
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010094 * request.
95 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020096 FFS_SETUP_PENDING,
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010097 /*
98 * There was event pending but before user space handled it
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020099 * some other event was introduced which canceled existing
100 * setup. If this state is set read/write on ep0 return
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100101 * -EIDRM. This state is only set when adding event.
102 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200103 FFS_SETUP_CANCELED
104};
105
106
107
108struct ffs_epfile;
109struct ffs_function;
110
111struct ffs_data {
112 struct usb_gadget *gadget;
113
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100114 /*
115 * Protect access read/write operations, only one read/write
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200116 * at a time. As a consequence protects ep0req and company.
117 * While setup request is being processed (queued) this is
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100118 * held.
119 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200120 struct mutex mutex;
121
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100122 /*
123 * Protect access to endpoint related structures (basically
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200124 * usb_ep_queue(), usb_ep_dequeue(), etc. calls) except for
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100125 * endpoint zero.
126 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200127 spinlock_t eps_lock;
128
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100129 /*
130 * XXX REVISIT do we need our own request? Since we are not
131 * handling setup requests immediately user space may be so
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200132 * slow that another setup will be sent to the gadget but this
133 * time not to us but another function and then there could be
Linus Torvaldsa4ce96a2010-07-21 09:25:42 -0700134 * a race. Is that the case? Or maybe we can use cdev->req
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100135 * after all, maybe we just need some spinlock for that?
136 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200137 struct usb_request *ep0req; /* P: mutex */
138 struct completion ep0req_completion; /* P: mutex */
139 int ep0req_status; /* P: mutex */
140
141 /* reference counter */
142 atomic_t ref;
143 /* how many files are opened (EP0 and others) */
144 atomic_t opened;
145
146 /* EP0 state */
147 enum ffs_state state;
148
149 /*
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100150 * Possible transitions:
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200151 * + FFS_NO_SETUP -> FFS_SETUP_PENDING -- P: ev.waitq.lock
152 * happens only in ep0 read which is P: mutex
153 * + FFS_SETUP_PENDING -> FFS_NO_SETUP -- P: ev.waitq.lock
154 * happens only in ep0 i/o which is P: mutex
155 * + FFS_SETUP_PENDING -> FFS_SETUP_CANCELED -- P: ev.waitq.lock
156 * + FFS_SETUP_CANCELED -> FFS_NO_SETUP -- cmpxchg
157 */
158 enum ffs_setup_state setup_state;
159
160#define FFS_SETUP_STATE(ffs) \
161 ((enum ffs_setup_state)cmpxchg(&(ffs)->setup_state, \
162 FFS_SETUP_CANCELED, FFS_NO_SETUP))
163
164 /* Events & such. */
165 struct {
166 u8 types[4];
167 unsigned short count;
168 /* XXX REVISIT need to update it in some places, or do we? */
169 unsigned short can_stall;
170 struct usb_ctrlrequest setup;
171
172 wait_queue_head_t waitq;
173 } ev; /* the whole structure, P: ev.waitq.lock */
174
175 /* Flags */
176 unsigned long flags;
177#define FFS_FL_CALL_CLOSED_CALLBACK 0
178#define FFS_FL_BOUND 1
179
180 /* Active function */
181 struct ffs_function *func;
182
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100183 /*
184 * Device name, write once when file system is mounted.
185 * Intended for user to read if she wants.
186 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200187 const char *dev_name;
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100188 /* Private data for our user (ie. gadget). Managed by user. */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200189 void *private_data;
190
191 /* filled by __ffs_data_got_descs() */
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100192 /*
193 * Real descriptors are 16 bytes after raw_descs (so you need
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200194 * to skip 16 bytes (ie. ffs->raw_descs + 16) to get to the
195 * first full speed descriptor). raw_descs_length and
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100196 * raw_fs_descs_length do not have those 16 bytes added.
197 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200198 const void *raw_descs;
199 unsigned raw_descs_length;
200 unsigned raw_fs_descs_length;
201 unsigned fs_descs_count;
202 unsigned hs_descs_count;
203
204 unsigned short strings_count;
205 unsigned short interfaces_count;
206 unsigned short eps_count;
207 unsigned short _pad1;
208
209 /* filled by __ffs_data_got_strings() */
210 /* ids in stringtabs are set in functionfs_bind() */
211 const void *raw_strings;
212 struct usb_gadget_strings **stringtabs;
213
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100214 /*
215 * File system's super block, write once when file system is
216 * mounted.
217 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200218 struct super_block *sb;
219
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100220 /* File permissions, written once when fs is mounted */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200221 struct ffs_file_perms {
222 umode_t mode;
Eric W. Biedermanb9b73f72012-06-14 01:19:23 -0700223 kuid_t uid;
224 kgid_t gid;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200225 } file_perms;
226
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100227 /*
228 * The endpoint files, filled by ffs_epfiles_create(),
229 * destroyed by ffs_epfiles_destroy().
230 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200231 struct ffs_epfile *epfiles;
232};
233
234/* Reference counter handling */
235static void ffs_data_get(struct ffs_data *ffs);
236static void ffs_data_put(struct ffs_data *ffs);
237/* Creates new ffs_data object. */
238static struct ffs_data *__must_check ffs_data_new(void) __attribute__((malloc));
239
240/* Opened counter handling. */
241static void ffs_data_opened(struct ffs_data *ffs);
242static void ffs_data_closed(struct ffs_data *ffs);
243
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100244/* Called with ffs->mutex held; take over ownership of data. */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200245static int __must_check
246__ffs_data_got_descs(struct ffs_data *ffs, char *data, size_t len);
247static int __must_check
248__ffs_data_got_strings(struct ffs_data *ffs, char *data, size_t len);
249
250
251/* The function structure ***************************************************/
252
253struct ffs_ep;
254
255struct ffs_function {
256 struct usb_configuration *conf;
257 struct usb_gadget *gadget;
258 struct ffs_data *ffs;
259
260 struct ffs_ep *eps;
261 u8 eps_revmap[16];
262 short *interfaces_nums;
263
264 struct usb_function function;
265};
266
267
268static struct ffs_function *ffs_func_from_usb(struct usb_function *f)
269{
270 return container_of(f, struct ffs_function, function);
271}
272
273static void ffs_func_free(struct ffs_function *func);
274
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200275static void ffs_func_eps_disable(struct ffs_function *func);
276static int __must_check ffs_func_eps_enable(struct ffs_function *func);
277
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200278static int ffs_func_bind(struct usb_configuration *,
279 struct usb_function *);
280static void ffs_func_unbind(struct usb_configuration *,
281 struct usb_function *);
282static int ffs_func_set_alt(struct usb_function *, unsigned, unsigned);
283static void ffs_func_disable(struct usb_function *);
284static int ffs_func_setup(struct usb_function *,
285 const struct usb_ctrlrequest *);
286static void ffs_func_suspend(struct usb_function *);
287static void ffs_func_resume(struct usb_function *);
288
289
290static int ffs_func_revmap_ep(struct ffs_function *func, u8 num);
291static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf);
292
293
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200294/* The endpoints structures *************************************************/
295
296struct ffs_ep {
297 struct usb_ep *ep; /* P: ffs->eps_lock */
298 struct usb_request *req; /* P: epfile->mutex */
299
300 /* [0]: full speed, [1]: high speed */
301 struct usb_endpoint_descriptor *descs[2];
302
303 u8 num;
304
305 int status; /* P: epfile->mutex */
306};
307
308struct ffs_epfile {
309 /* Protects ep->ep and ep->req. */
310 struct mutex mutex;
311 wait_queue_head_t wait;
312
313 struct ffs_data *ffs;
314 struct ffs_ep *ep; /* P: ffs->eps_lock */
315
316 struct dentry *dentry;
317
318 char name[5];
319
320 unsigned char in; /* P: ffs->eps_lock */
321 unsigned char isoc; /* P: ffs->eps_lock */
322
323 unsigned char _pad;
324};
325
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200326static int __must_check ffs_epfiles_create(struct ffs_data *ffs);
327static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count);
328
329static struct inode *__must_check
330ffs_sb_create_file(struct super_block *sb, const char *name, void *data,
331 const struct file_operations *fops,
332 struct dentry **dentry_p);
333
334
335/* Misc helper functions ****************************************************/
336
337static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
338 __attribute__((warn_unused_result, nonnull));
339static char *ffs_prepare_buffer(const char * __user buf, size_t len)
340 __attribute__((warn_unused_result, nonnull));
341
342
343/* Control file aka ep0 *****************************************************/
344
345static void ffs_ep0_complete(struct usb_ep *ep, struct usb_request *req)
346{
347 struct ffs_data *ffs = req->context;
348
349 complete_all(&ffs->ep0req_completion);
350}
351
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200352static int __ffs_ep0_queue_wait(struct ffs_data *ffs, char *data, size_t len)
353{
354 struct usb_request *req = ffs->ep0req;
355 int ret;
356
357 req->zero = len < le16_to_cpu(ffs->ev.setup.wLength);
358
359 spin_unlock_irq(&ffs->ev.waitq.lock);
360
361 req->buf = data;
362 req->length = len;
363
Marek Szyprowskice1fd352011-01-28 13:55:36 +0100364 /*
365 * UDC layer requires to provide a buffer even for ZLP, but should
366 * not use it at all. Let's provide some poisoned pointer to catch
367 * possible bug in the driver.
368 */
369 if (req->buf == NULL)
370 req->buf = (void *)0xDEADBABE;
371
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200372 INIT_COMPLETION(ffs->ep0req_completion);
373
374 ret = usb_ep_queue(ffs->gadget->ep0, req, GFP_ATOMIC);
375 if (unlikely(ret < 0))
376 return ret;
377
378 ret = wait_for_completion_interruptible(&ffs->ep0req_completion);
379 if (unlikely(ret)) {
380 usb_ep_dequeue(ffs->gadget->ep0, req);
381 return -EINTR;
382 }
383
384 ffs->setup_state = FFS_NO_SETUP;
385 return ffs->ep0req_status;
386}
387
388static int __ffs_ep0_stall(struct ffs_data *ffs)
389{
390 if (ffs->ev.can_stall) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100391 pr_vdebug("ep0 stall\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200392 usb_ep_set_halt(ffs->gadget->ep0);
393 ffs->setup_state = FFS_NO_SETUP;
394 return -EL2HLT;
395 } else {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100396 pr_debug("bogus ep0 stall!\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200397 return -ESRCH;
398 }
399}
400
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200401static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
402 size_t len, loff_t *ptr)
403{
404 struct ffs_data *ffs = file->private_data;
405 ssize_t ret;
406 char *data;
407
408 ENTER();
409
410 /* Fast check if setup was canceled */
411 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED)
412 return -EIDRM;
413
414 /* Acquire mutex */
415 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
416 if (unlikely(ret < 0))
417 return ret;
418
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200419 /* Check state */
420 switch (ffs->state) {
421 case FFS_READ_DESCRIPTORS:
422 case FFS_READ_STRINGS:
423 /* Copy data */
424 if (unlikely(len < 16)) {
425 ret = -EINVAL;
426 break;
427 }
428
429 data = ffs_prepare_buffer(buf, len);
Tobias Klauser537baab2010-12-09 15:52:39 +0100430 if (IS_ERR(data)) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200431 ret = PTR_ERR(data);
432 break;
433 }
434
435 /* Handle data */
436 if (ffs->state == FFS_READ_DESCRIPTORS) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100437 pr_info("read descriptors\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200438 ret = __ffs_data_got_descs(ffs, data, len);
439 if (unlikely(ret < 0))
440 break;
441
442 ffs->state = FFS_READ_STRINGS;
443 ret = len;
444 } else {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100445 pr_info("read strings\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200446 ret = __ffs_data_got_strings(ffs, data, len);
447 if (unlikely(ret < 0))
448 break;
449
450 ret = ffs_epfiles_create(ffs);
451 if (unlikely(ret)) {
452 ffs->state = FFS_CLOSING;
453 break;
454 }
455
456 ffs->state = FFS_ACTIVE;
457 mutex_unlock(&ffs->mutex);
458
459 ret = functionfs_ready_callback(ffs);
460 if (unlikely(ret < 0)) {
461 ffs->state = FFS_CLOSING;
462 return ret;
463 }
464
465 set_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags);
466 return len;
467 }
468 break;
469
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200470 case FFS_ACTIVE:
471 data = NULL;
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100472 /*
473 * We're called from user space, we can use _irq
474 * rather then _irqsave
475 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200476 spin_lock_irq(&ffs->ev.waitq.lock);
477 switch (FFS_SETUP_STATE(ffs)) {
478 case FFS_SETUP_CANCELED:
479 ret = -EIDRM;
480 goto done_spin;
481
482 case FFS_NO_SETUP:
483 ret = -ESRCH;
484 goto done_spin;
485
486 case FFS_SETUP_PENDING:
487 break;
488 }
489
490 /* FFS_SETUP_PENDING */
491 if (!(ffs->ev.setup.bRequestType & USB_DIR_IN)) {
492 spin_unlock_irq(&ffs->ev.waitq.lock);
493 ret = __ffs_ep0_stall(ffs);
494 break;
495 }
496
497 /* FFS_SETUP_PENDING and not stall */
498 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
499
500 spin_unlock_irq(&ffs->ev.waitq.lock);
501
502 data = ffs_prepare_buffer(buf, len);
Tobias Klauser537baab2010-12-09 15:52:39 +0100503 if (IS_ERR(data)) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200504 ret = PTR_ERR(data);
505 break;
506 }
507
508 spin_lock_irq(&ffs->ev.waitq.lock);
509
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100510 /*
511 * We are guaranteed to be still in FFS_ACTIVE state
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200512 * but the state of setup could have changed from
513 * FFS_SETUP_PENDING to FFS_SETUP_CANCELED so we need
514 * to check for that. If that happened we copied data
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100515 * from user space in vain but it's unlikely.
516 *
517 * For sure we are not in FFS_NO_SETUP since this is
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200518 * the only place FFS_SETUP_PENDING -> FFS_NO_SETUP
519 * transition can be performed and it's protected by
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100520 * mutex.
521 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200522 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) {
523 ret = -EIDRM;
524done_spin:
525 spin_unlock_irq(&ffs->ev.waitq.lock);
526 } else {
527 /* unlocks spinlock */
528 ret = __ffs_ep0_queue_wait(ffs, data, len);
529 }
530 kfree(data);
531 break;
532
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200533 default:
534 ret = -EBADFD;
535 break;
536 }
537
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200538 mutex_unlock(&ffs->mutex);
539 return ret;
540}
541
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200542static ssize_t __ffs_ep0_read_events(struct ffs_data *ffs, char __user *buf,
543 size_t n)
544{
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100545 /*
546 * We are holding ffs->ev.waitq.lock and ffs->mutex and we need
547 * to release them.
548 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200549 struct usb_functionfs_event events[n];
550 unsigned i = 0;
551
552 memset(events, 0, sizeof events);
553
554 do {
555 events[i].type = ffs->ev.types[i];
556 if (events[i].type == FUNCTIONFS_SETUP) {
557 events[i].u.setup = ffs->ev.setup;
558 ffs->setup_state = FFS_SETUP_PENDING;
559 }
560 } while (++i < n);
561
562 if (n < ffs->ev.count) {
563 ffs->ev.count -= n;
564 memmove(ffs->ev.types, ffs->ev.types + n,
565 ffs->ev.count * sizeof *ffs->ev.types);
566 } else {
567 ffs->ev.count = 0;
568 }
569
570 spin_unlock_irq(&ffs->ev.waitq.lock);
571 mutex_unlock(&ffs->mutex);
572
573 return unlikely(__copy_to_user(buf, events, sizeof events))
574 ? -EFAULT : sizeof events;
575}
576
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200577static ssize_t ffs_ep0_read(struct file *file, char __user *buf,
578 size_t len, loff_t *ptr)
579{
580 struct ffs_data *ffs = file->private_data;
581 char *data = NULL;
582 size_t n;
583 int ret;
584
585 ENTER();
586
587 /* Fast check if setup was canceled */
588 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED)
589 return -EIDRM;
590
591 /* Acquire mutex */
592 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
593 if (unlikely(ret < 0))
594 return ret;
595
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200596 /* Check state */
597 if (ffs->state != FFS_ACTIVE) {
598 ret = -EBADFD;
599 goto done_mutex;
600 }
601
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100602 /*
603 * We're called from user space, we can use _irq rather then
604 * _irqsave
605 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200606 spin_lock_irq(&ffs->ev.waitq.lock);
607
608 switch (FFS_SETUP_STATE(ffs)) {
609 case FFS_SETUP_CANCELED:
610 ret = -EIDRM;
611 break;
612
613 case FFS_NO_SETUP:
614 n = len / sizeof(struct usb_functionfs_event);
615 if (unlikely(!n)) {
616 ret = -EINVAL;
617 break;
618 }
619
620 if ((file->f_flags & O_NONBLOCK) && !ffs->ev.count) {
621 ret = -EAGAIN;
622 break;
623 }
624
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100625 if (wait_event_interruptible_exclusive_locked_irq(ffs->ev.waitq,
626 ffs->ev.count)) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200627 ret = -EINTR;
628 break;
629 }
630
631 return __ffs_ep0_read_events(ffs, buf,
632 min(n, (size_t)ffs->ev.count));
633
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200634 case FFS_SETUP_PENDING:
635 if (ffs->ev.setup.bRequestType & USB_DIR_IN) {
636 spin_unlock_irq(&ffs->ev.waitq.lock);
637 ret = __ffs_ep0_stall(ffs);
638 goto done_mutex;
639 }
640
641 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
642
643 spin_unlock_irq(&ffs->ev.waitq.lock);
644
645 if (likely(len)) {
646 data = kmalloc(len, GFP_KERNEL);
647 if (unlikely(!data)) {
648 ret = -ENOMEM;
649 goto done_mutex;
650 }
651 }
652
653 spin_lock_irq(&ffs->ev.waitq.lock);
654
655 /* See ffs_ep0_write() */
656 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) {
657 ret = -EIDRM;
658 break;
659 }
660
661 /* unlocks spinlock */
662 ret = __ffs_ep0_queue_wait(ffs, data, len);
663 if (likely(ret > 0) && unlikely(__copy_to_user(buf, data, len)))
664 ret = -EFAULT;
665 goto done_mutex;
666
667 default:
668 ret = -EBADFD;
669 break;
670 }
671
672 spin_unlock_irq(&ffs->ev.waitq.lock);
673done_mutex:
674 mutex_unlock(&ffs->mutex);
675 kfree(data);
676 return ret;
677}
678
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200679static int ffs_ep0_open(struct inode *inode, struct file *file)
680{
681 struct ffs_data *ffs = inode->i_private;
682
683 ENTER();
684
685 if (unlikely(ffs->state == FFS_CLOSING))
686 return -EBUSY;
687
688 file->private_data = ffs;
689 ffs_data_opened(ffs);
690
691 return 0;
692}
693
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200694static int ffs_ep0_release(struct inode *inode, struct file *file)
695{
696 struct ffs_data *ffs = file->private_data;
697
698 ENTER();
699
700 ffs_data_closed(ffs);
701
702 return 0;
703}
704
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200705static long ffs_ep0_ioctl(struct file *file, unsigned code, unsigned long value)
706{
707 struct ffs_data *ffs = file->private_data;
708 struct usb_gadget *gadget = ffs->gadget;
709 long ret;
710
711 ENTER();
712
713 if (code == FUNCTIONFS_INTERFACE_REVMAP) {
714 struct ffs_function *func = ffs->func;
715 ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV;
Andrzej Pietrasiewicz92b0abf2012-03-28 09:30:50 +0200716 } else if (gadget && gadget->ops->ioctl) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200717 ret = gadget->ops->ioctl(gadget, code, value);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200718 } else {
719 ret = -ENOTTY;
720 }
721
722 return ret;
723}
724
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200725static const struct file_operations ffs_ep0_operations = {
726 .owner = THIS_MODULE,
727 .llseek = no_llseek,
728
729 .open = ffs_ep0_open,
730 .write = ffs_ep0_write,
731 .read = ffs_ep0_read,
732 .release = ffs_ep0_release,
733 .unlocked_ioctl = ffs_ep0_ioctl,
734};
735
736
737/* "Normal" endpoints operations ********************************************/
738
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200739static void ffs_epfile_io_complete(struct usb_ep *_ep, struct usb_request *req)
740{
741 ENTER();
742 if (likely(req->context)) {
743 struct ffs_ep *ep = _ep->driver_data;
744 ep->status = req->status ? req->status : req->actual;
745 complete(req->context);
746 }
747}
748
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200749static ssize_t ffs_epfile_io(struct file *file,
750 char __user *buf, size_t len, int read)
751{
752 struct ffs_epfile *epfile = file->private_data;
753 struct ffs_ep *ep;
754 char *data = NULL;
755 ssize_t ret;
756 int halt;
757
758 goto first_try;
759 do {
760 spin_unlock_irq(&epfile->ffs->eps_lock);
761 mutex_unlock(&epfile->mutex);
762
763first_try:
764 /* Are we still active? */
765 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE)) {
766 ret = -ENODEV;
767 goto error;
768 }
769
770 /* Wait for endpoint to be enabled */
771 ep = epfile->ep;
772 if (!ep) {
773 if (file->f_flags & O_NONBLOCK) {
774 ret = -EAGAIN;
775 goto error;
776 }
777
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100778 if (wait_event_interruptible(epfile->wait,
779 (ep = epfile->ep))) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200780 ret = -EINTR;
781 goto error;
782 }
783 }
784
785 /* Do we halt? */
786 halt = !read == !epfile->in;
787 if (halt && epfile->isoc) {
788 ret = -EINVAL;
789 goto error;
790 }
791
792 /* Allocate & copy */
793 if (!halt && !data) {
794 data = kzalloc(len, GFP_KERNEL);
795 if (unlikely(!data))
796 return -ENOMEM;
797
798 if (!read &&
799 unlikely(__copy_from_user(data, buf, len))) {
800 ret = -EFAULT;
801 goto error;
802 }
803 }
804
805 /* We will be using request */
806 ret = ffs_mutex_lock(&epfile->mutex,
807 file->f_flags & O_NONBLOCK);
808 if (unlikely(ret))
809 goto error;
810
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100811 /*
812 * We're called from user space, we can use _irq rather then
813 * _irqsave
814 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200815 spin_lock_irq(&epfile->ffs->eps_lock);
816
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100817 /*
818 * While we were acquiring mutex endpoint got disabled
819 * or changed?
820 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200821 } while (unlikely(epfile->ep != ep));
822
823 /* Halt */
824 if (unlikely(halt)) {
825 if (likely(epfile->ep == ep) && !WARN_ON(!ep->ep))
826 usb_ep_set_halt(ep->ep);
827 spin_unlock_irq(&epfile->ffs->eps_lock);
828 ret = -EBADMSG;
829 } else {
830 /* Fire the request */
831 DECLARE_COMPLETION_ONSTACK(done);
832
833 struct usb_request *req = ep->req;
834 req->context = &done;
835 req->complete = ffs_epfile_io_complete;
836 req->buf = data;
837 req->length = len;
838
839 ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
840
841 spin_unlock_irq(&epfile->ffs->eps_lock);
842
843 if (unlikely(ret < 0)) {
844 /* nop */
845 } else if (unlikely(wait_for_completion_interruptible(&done))) {
846 ret = -EINTR;
847 usb_ep_dequeue(ep->ep, req);
848 } else {
849 ret = ep->status;
850 if (read && ret > 0 &&
851 unlikely(copy_to_user(buf, data, ret)))
852 ret = -EFAULT;
853 }
854 }
855
856 mutex_unlock(&epfile->mutex);
857error:
858 kfree(data);
859 return ret;
860}
861
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200862static ssize_t
863ffs_epfile_write(struct file *file, const char __user *buf, size_t len,
864 loff_t *ptr)
865{
866 ENTER();
867
868 return ffs_epfile_io(file, (char __user *)buf, len, 0);
869}
870
871static ssize_t
872ffs_epfile_read(struct file *file, char __user *buf, size_t len, loff_t *ptr)
873{
874 ENTER();
875
876 return ffs_epfile_io(file, buf, len, 1);
877}
878
879static int
880ffs_epfile_open(struct inode *inode, struct file *file)
881{
882 struct ffs_epfile *epfile = inode->i_private;
883
884 ENTER();
885
886 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
887 return -ENODEV;
888
889 file->private_data = epfile;
890 ffs_data_opened(epfile->ffs);
891
892 return 0;
893}
894
895static int
896ffs_epfile_release(struct inode *inode, struct file *file)
897{
898 struct ffs_epfile *epfile = inode->i_private;
899
900 ENTER();
901
902 ffs_data_closed(epfile->ffs);
903
904 return 0;
905}
906
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200907static long ffs_epfile_ioctl(struct file *file, unsigned code,
908 unsigned long value)
909{
910 struct ffs_epfile *epfile = file->private_data;
911 int ret;
912
913 ENTER();
914
915 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
916 return -ENODEV;
917
918 spin_lock_irq(&epfile->ffs->eps_lock);
919 if (likely(epfile->ep)) {
920 switch (code) {
921 case FUNCTIONFS_FIFO_STATUS:
922 ret = usb_ep_fifo_status(epfile->ep->ep);
923 break;
924 case FUNCTIONFS_FIFO_FLUSH:
925 usb_ep_fifo_flush(epfile->ep->ep);
926 ret = 0;
927 break;
928 case FUNCTIONFS_CLEAR_HALT:
929 ret = usb_ep_clear_halt(epfile->ep->ep);
930 break;
931 case FUNCTIONFS_ENDPOINT_REVMAP:
932 ret = epfile->ep->num;
933 break;
934 default:
935 ret = -ENOTTY;
936 }
937 } else {
938 ret = -ENODEV;
939 }
940 spin_unlock_irq(&epfile->ffs->eps_lock);
941
942 return ret;
943}
944
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200945static const struct file_operations ffs_epfile_operations = {
946 .owner = THIS_MODULE,
947 .llseek = no_llseek,
948
949 .open = ffs_epfile_open,
950 .write = ffs_epfile_write,
951 .read = ffs_epfile_read,
952 .release = ffs_epfile_release,
953 .unlocked_ioctl = ffs_epfile_ioctl,
954};
955
956
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200957/* File system and super block operations ***********************************/
958
959/*
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100960 * Mounting the file system creates a controller file, used first for
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200961 * function configuration then later for event monitoring.
962 */
963
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200964static struct inode *__must_check
965ffs_sb_make_inode(struct super_block *sb, void *data,
966 const struct file_operations *fops,
967 const struct inode_operations *iops,
968 struct ffs_file_perms *perms)
969{
970 struct inode *inode;
971
972 ENTER();
973
974 inode = new_inode(sb);
975
976 if (likely(inode)) {
977 struct timespec current_time = CURRENT_TIME;
978
Al Viro12ba8d12010-10-27 04:19:36 +0100979 inode->i_ino = get_next_ino();
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200980 inode->i_mode = perms->mode;
981 inode->i_uid = perms->uid;
982 inode->i_gid = perms->gid;
983 inode->i_atime = current_time;
984 inode->i_mtime = current_time;
985 inode->i_ctime = current_time;
986 inode->i_private = data;
987 if (fops)
988 inode->i_fop = fops;
989 if (iops)
990 inode->i_op = iops;
991 }
992
993 return inode;
994}
995
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200996/* Create "regular" file */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200997static struct inode *ffs_sb_create_file(struct super_block *sb,
998 const char *name, void *data,
999 const struct file_operations *fops,
1000 struct dentry **dentry_p)
1001{
1002 struct ffs_data *ffs = sb->s_fs_info;
1003 struct dentry *dentry;
1004 struct inode *inode;
1005
1006 ENTER();
1007
1008 dentry = d_alloc_name(sb->s_root, name);
1009 if (unlikely(!dentry))
1010 return NULL;
1011
1012 inode = ffs_sb_make_inode(sb, data, fops, NULL, &ffs->file_perms);
1013 if (unlikely(!inode)) {
1014 dput(dentry);
1015 return NULL;
1016 }
1017
1018 d_add(dentry, inode);
1019 if (dentry_p)
1020 *dentry_p = dentry;
1021
1022 return inode;
1023}
1024
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001025/* Super block */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001026static const struct super_operations ffs_sb_operations = {
1027 .statfs = simple_statfs,
1028 .drop_inode = generic_delete_inode,
1029};
1030
1031struct ffs_sb_fill_data {
1032 struct ffs_file_perms perms;
1033 umode_t root_mode;
1034 const char *dev_name;
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001035 union {
1036 /* set by ffs_fs_mount(), read by ffs_sb_fill() */
1037 void *private_data;
1038 /* set by ffs_sb_fill(), read by ffs_fs_mount */
1039 struct ffs_data *ffs_data;
1040 };
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001041};
1042
1043static int ffs_sb_fill(struct super_block *sb, void *_data, int silent)
1044{
1045 struct ffs_sb_fill_data *data = _data;
1046 struct inode *inode;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001047 struct ffs_data *ffs;
1048
1049 ENTER();
1050
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001051 /* Initialise data */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001052 ffs = ffs_data_new();
1053 if (unlikely(!ffs))
Al Viro5b5f9562012-01-08 15:38:27 -05001054 goto Enomem;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001055
1056 ffs->sb = sb;
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001057 ffs->dev_name = kstrdup(data->dev_name, GFP_KERNEL);
1058 if (unlikely(!ffs->dev_name))
1059 goto Enomem;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001060 ffs->file_perms = data->perms;
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001061 ffs->private_data = data->private_data;
1062
1063 /* used by the caller of this function */
1064 data->ffs_data = ffs;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001065
1066 sb->s_fs_info = ffs;
1067 sb->s_blocksize = PAGE_CACHE_SIZE;
1068 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1069 sb->s_magic = FUNCTIONFS_MAGIC;
1070 sb->s_op = &ffs_sb_operations;
1071 sb->s_time_gran = 1;
1072
1073 /* Root inode */
1074 data->perms.mode = data->root_mode;
1075 inode = ffs_sb_make_inode(sb, NULL,
1076 &simple_dir_operations,
1077 &simple_dir_inode_operations,
1078 &data->perms);
Al Viro48fde702012-01-08 22:15:13 -05001079 sb->s_root = d_make_root(inode);
1080 if (unlikely(!sb->s_root))
Al Viro5b5f9562012-01-08 15:38:27 -05001081 goto Enomem;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001082
1083 /* EP0 file */
1084 if (unlikely(!ffs_sb_create_file(sb, "ep0", ffs,
1085 &ffs_ep0_operations, NULL)))
Al Viro5b5f9562012-01-08 15:38:27 -05001086 goto Enomem;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001087
1088 return 0;
1089
Al Viro5b5f9562012-01-08 15:38:27 -05001090Enomem:
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001091 return -ENOMEM;
1092}
1093
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001094static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts)
1095{
1096 ENTER();
1097
1098 if (!opts || !*opts)
1099 return 0;
1100
1101 for (;;) {
1102 char *end, *eq, *comma;
1103 unsigned long value;
1104
1105 /* Option limit */
1106 comma = strchr(opts, ',');
1107 if (comma)
1108 *comma = 0;
1109
1110 /* Value limit */
1111 eq = strchr(opts, '=');
1112 if (unlikely(!eq)) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001113 pr_err("'=' missing in %s\n", opts);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001114 return -EINVAL;
1115 }
1116 *eq = 0;
1117
1118 /* Parse value */
1119 value = simple_strtoul(eq + 1, &end, 0);
1120 if (unlikely(*end != ',' && *end != 0)) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001121 pr_err("%s: invalid value: %s\n", opts, eq + 1);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001122 return -EINVAL;
1123 }
1124
1125 /* Interpret option */
1126 switch (eq - opts) {
1127 case 5:
1128 if (!memcmp(opts, "rmode", 5))
1129 data->root_mode = (value & 0555) | S_IFDIR;
1130 else if (!memcmp(opts, "fmode", 5))
1131 data->perms.mode = (value & 0666) | S_IFREG;
1132 else
1133 goto invalid;
1134 break;
1135
1136 case 4:
1137 if (!memcmp(opts, "mode", 4)) {
1138 data->root_mode = (value & 0555) | S_IFDIR;
1139 data->perms.mode = (value & 0666) | S_IFREG;
1140 } else {
1141 goto invalid;
1142 }
1143 break;
1144
1145 case 3:
Eric W. Biedermanb9b73f72012-06-14 01:19:23 -07001146 if (!memcmp(opts, "uid", 3)) {
1147 data->perms.uid = make_kuid(current_user_ns(), value);
1148 if (!uid_valid(data->perms.uid)) {
1149 pr_err("%s: unmapped value: %lu\n", opts, value);
1150 return -EINVAL;
1151 }
1152 }
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001153 else if (!memcmp(opts, "gid", 3))
Eric W. Biedermanb9b73f72012-06-14 01:19:23 -07001154 data->perms.gid = make_kgid(current_user_ns(), value);
1155 if (!gid_valid(data->perms.gid)) {
1156 pr_err("%s: unmapped value: %lu\n", opts, value);
1157 return -EINVAL;
1158 }
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001159 else
1160 goto invalid;
1161 break;
1162
1163 default:
1164invalid:
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001165 pr_err("%s: invalid option\n", opts);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001166 return -EINVAL;
1167 }
1168
1169 /* Next iteration */
1170 if (!comma)
1171 break;
1172 opts = comma + 1;
1173 }
1174
1175 return 0;
1176}
1177
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001178/* "mount -t functionfs dev_name /dev/function" ends up here */
1179
Al Virofc14f2f2010-07-25 01:48:30 +04001180static struct dentry *
1181ffs_fs_mount(struct file_system_type *t, int flags,
1182 const char *dev_name, void *opts)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001183{
1184 struct ffs_sb_fill_data data = {
1185 .perms = {
1186 .mode = S_IFREG | 0600,
Eric W. Biedermanb9b73f72012-06-14 01:19:23 -07001187 .uid = GLOBAL_ROOT_UID,
1188 .gid = GLOBAL_ROOT_GID,
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001189 },
1190 .root_mode = S_IFDIR | 0500,
1191 };
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001192 struct dentry *rv;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001193 int ret;
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001194 void *ffs_dev;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001195
1196 ENTER();
1197
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001198 ret = ffs_fs_parse_opts(&data, opts);
1199 if (unlikely(ret < 0))
Al Virofc14f2f2010-07-25 01:48:30 +04001200 return ERR_PTR(ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001201
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001202 ffs_dev = functionfs_acquire_dev_callback(dev_name);
1203 if (IS_ERR(ffs_dev))
1204 return ffs_dev;
1205
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001206 data.dev_name = dev_name;
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001207 data.private_data = ffs_dev;
1208 rv = mount_nodev(t, flags, &data, ffs_sb_fill);
1209
1210 /* data.ffs_data is set by ffs_sb_fill */
1211 if (IS_ERR(rv))
1212 functionfs_release_dev_callback(data.ffs_data);
1213
1214 return rv;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001215}
1216
1217static void
1218ffs_fs_kill_sb(struct super_block *sb)
1219{
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001220 ENTER();
1221
1222 kill_litter_super(sb);
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001223 if (sb->s_fs_info) {
1224 functionfs_release_dev_callback(sb->s_fs_info);
Al Viro5b5f9562012-01-08 15:38:27 -05001225 ffs_data_put(sb->s_fs_info);
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001226 }
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001227}
1228
1229static struct file_system_type ffs_fs_type = {
1230 .owner = THIS_MODULE,
1231 .name = "functionfs",
Al Virofc14f2f2010-07-25 01:48:30 +04001232 .mount = ffs_fs_mount,
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001233 .kill_sb = ffs_fs_kill_sb,
1234};
1235
1236
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001237/* Driver's main init/cleanup functions *************************************/
1238
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001239static int functionfs_init(void)
1240{
1241 int ret;
1242
1243 ENTER();
1244
1245 ret = register_filesystem(&ffs_fs_type);
1246 if (likely(!ret))
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001247 pr_info("file system registered\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001248 else
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001249 pr_err("failed registering file system (%d)\n", ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001250
1251 return ret;
1252}
1253
1254static void functionfs_cleanup(void)
1255{
1256 ENTER();
1257
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001258 pr_info("unloading\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001259 unregister_filesystem(&ffs_fs_type);
1260}
1261
1262
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001263/* ffs_data and ffs_function construction and destruction code **************/
1264
1265static void ffs_data_clear(struct ffs_data *ffs);
1266static void ffs_data_reset(struct ffs_data *ffs);
1267
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001268static void ffs_data_get(struct ffs_data *ffs)
1269{
1270 ENTER();
1271
1272 atomic_inc(&ffs->ref);
1273}
1274
1275static void ffs_data_opened(struct ffs_data *ffs)
1276{
1277 ENTER();
1278
1279 atomic_inc(&ffs->ref);
1280 atomic_inc(&ffs->opened);
1281}
1282
1283static void ffs_data_put(struct ffs_data *ffs)
1284{
1285 ENTER();
1286
1287 if (unlikely(atomic_dec_and_test(&ffs->ref))) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001288 pr_info("%s(): freeing\n", __func__);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001289 ffs_data_clear(ffs);
Andi Kleen647d5582012-03-16 12:01:02 -07001290 BUG_ON(waitqueue_active(&ffs->ev.waitq) ||
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001291 waitqueue_active(&ffs->ep0req_completion.wait));
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001292 kfree(ffs->dev_name);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001293 kfree(ffs);
1294 }
1295}
1296
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001297static void ffs_data_closed(struct ffs_data *ffs)
1298{
1299 ENTER();
1300
1301 if (atomic_dec_and_test(&ffs->opened)) {
1302 ffs->state = FFS_CLOSING;
1303 ffs_data_reset(ffs);
1304 }
1305
1306 ffs_data_put(ffs);
1307}
1308
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001309static struct ffs_data *ffs_data_new(void)
1310{
1311 struct ffs_data *ffs = kzalloc(sizeof *ffs, GFP_KERNEL);
1312 if (unlikely(!ffs))
1313 return 0;
1314
1315 ENTER();
1316
1317 atomic_set(&ffs->ref, 1);
1318 atomic_set(&ffs->opened, 0);
1319 ffs->state = FFS_READ_DESCRIPTORS;
1320 mutex_init(&ffs->mutex);
1321 spin_lock_init(&ffs->eps_lock);
1322 init_waitqueue_head(&ffs->ev.waitq);
1323 init_completion(&ffs->ep0req_completion);
1324
1325 /* XXX REVISIT need to update it in some places, or do we? */
1326 ffs->ev.can_stall = 1;
1327
1328 return ffs;
1329}
1330
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001331static void ffs_data_clear(struct ffs_data *ffs)
1332{
1333 ENTER();
1334
1335 if (test_and_clear_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags))
1336 functionfs_closed_callback(ffs);
1337
1338 BUG_ON(ffs->gadget);
1339
1340 if (ffs->epfiles)
1341 ffs_epfiles_destroy(ffs->epfiles, ffs->eps_count);
1342
1343 kfree(ffs->raw_descs);
1344 kfree(ffs->raw_strings);
1345 kfree(ffs->stringtabs);
1346}
1347
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001348static void ffs_data_reset(struct ffs_data *ffs)
1349{
1350 ENTER();
1351
1352 ffs_data_clear(ffs);
1353
1354 ffs->epfiles = NULL;
1355 ffs->raw_descs = NULL;
1356 ffs->raw_strings = NULL;
1357 ffs->stringtabs = NULL;
1358
1359 ffs->raw_descs_length = 0;
1360 ffs->raw_fs_descs_length = 0;
1361 ffs->fs_descs_count = 0;
1362 ffs->hs_descs_count = 0;
1363
1364 ffs->strings_count = 0;
1365 ffs->interfaces_count = 0;
1366 ffs->eps_count = 0;
1367
1368 ffs->ev.count = 0;
1369
1370 ffs->state = FFS_READ_DESCRIPTORS;
1371 ffs->setup_state = FFS_NO_SETUP;
1372 ffs->flags = 0;
1373}
1374
1375
1376static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev)
1377{
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001378 struct usb_gadget_strings **lang;
1379 int first_id;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001380
1381 ENTER();
1382
1383 if (WARN_ON(ffs->state != FFS_ACTIVE
1384 || test_and_set_bit(FFS_FL_BOUND, &ffs->flags)))
1385 return -EBADFD;
1386
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001387 first_id = usb_string_ids_n(cdev, ffs->strings_count);
1388 if (unlikely(first_id < 0))
1389 return first_id;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001390
1391 ffs->ep0req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
1392 if (unlikely(!ffs->ep0req))
1393 return -ENOMEM;
1394 ffs->ep0req->complete = ffs_ep0_complete;
1395 ffs->ep0req->context = ffs;
1396
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001397 lang = ffs->stringtabs;
1398 for (lang = ffs->stringtabs; *lang; ++lang) {
1399 struct usb_string *str = (*lang)->strings;
1400 int id = first_id;
1401 for (; str->s; ++id, ++str)
1402 str->id = id;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001403 }
1404
1405 ffs->gadget = cdev->gadget;
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001406 ffs_data_get(ffs);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001407 return 0;
1408}
1409
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001410static void functionfs_unbind(struct ffs_data *ffs)
1411{
1412 ENTER();
1413
1414 if (!WARN_ON(!ffs->gadget)) {
1415 usb_ep_free_request(ffs->gadget->ep0, ffs->ep0req);
1416 ffs->ep0req = NULL;
1417 ffs->gadget = NULL;
1418 ffs_data_put(ffs);
Andrzej Pietrasiewicze2190a92012-03-12 12:55:41 +01001419 clear_bit(FFS_FL_BOUND, &ffs->flags);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001420 }
1421}
1422
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001423static int ffs_epfiles_create(struct ffs_data *ffs)
1424{
1425 struct ffs_epfile *epfile, *epfiles;
1426 unsigned i, count;
1427
1428 ENTER();
1429
1430 count = ffs->eps_count;
Thomas Meyer9823a522011-11-29 22:08:00 +01001431 epfiles = kcalloc(count, sizeof(*epfiles), GFP_KERNEL);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001432 if (!epfiles)
1433 return -ENOMEM;
1434
1435 epfile = epfiles;
1436 for (i = 1; i <= count; ++i, ++epfile) {
1437 epfile->ffs = ffs;
1438 mutex_init(&epfile->mutex);
1439 init_waitqueue_head(&epfile->wait);
1440 sprintf(epfiles->name, "ep%u", i);
1441 if (!unlikely(ffs_sb_create_file(ffs->sb, epfiles->name, epfile,
1442 &ffs_epfile_operations,
1443 &epfile->dentry))) {
1444 ffs_epfiles_destroy(epfiles, i - 1);
1445 return -ENOMEM;
1446 }
1447 }
1448
1449 ffs->epfiles = epfiles;
1450 return 0;
1451}
1452
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001453static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count)
1454{
1455 struct ffs_epfile *epfile = epfiles;
1456
1457 ENTER();
1458
1459 for (; count; --count, ++epfile) {
1460 BUG_ON(mutex_is_locked(&epfile->mutex) ||
1461 waitqueue_active(&epfile->wait));
1462 if (epfile->dentry) {
1463 d_delete(epfile->dentry);
1464 dput(epfile->dentry);
1465 epfile->dentry = NULL;
1466 }
1467 }
1468
1469 kfree(epfiles);
1470}
1471
Michal Nazarewicz7898aee2010-06-16 12:07:58 +02001472static int functionfs_bind_config(struct usb_composite_dev *cdev,
1473 struct usb_configuration *c,
1474 struct ffs_data *ffs)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001475{
1476 struct ffs_function *func;
1477 int ret;
1478
1479 ENTER();
1480
1481 func = kzalloc(sizeof *func, GFP_KERNEL);
1482 if (unlikely(!func))
1483 return -ENOMEM;
1484
1485 func->function.name = "Function FS Gadget";
1486 func->function.strings = ffs->stringtabs;
1487
1488 func->function.bind = ffs_func_bind;
1489 func->function.unbind = ffs_func_unbind;
1490 func->function.set_alt = ffs_func_set_alt;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001491 func->function.disable = ffs_func_disable;
1492 func->function.setup = ffs_func_setup;
1493 func->function.suspend = ffs_func_suspend;
1494 func->function.resume = ffs_func_resume;
1495
1496 func->conf = c;
1497 func->gadget = cdev->gadget;
1498 func->ffs = ffs;
1499 ffs_data_get(ffs);
1500
1501 ret = usb_add_function(c, &func->function);
1502 if (unlikely(ret))
1503 ffs_func_free(func);
1504
1505 return ret;
1506}
1507
1508static void ffs_func_free(struct ffs_function *func)
1509{
Peter Korsgaard4f065392012-05-03 12:58:49 +02001510 struct ffs_ep *ep = func->eps;
1511 unsigned count = func->ffs->eps_count;
1512 unsigned long flags;
1513
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001514 ENTER();
1515
Peter Korsgaard4f065392012-05-03 12:58:49 +02001516 /* cleanup after autoconfig */
1517 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1518 do {
1519 if (ep->ep && ep->req)
1520 usb_ep_free_request(ep->ep, ep->req);
1521 ep->req = NULL;
1522 ++ep;
1523 } while (--count);
1524 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1525
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001526 ffs_data_put(func->ffs);
1527
1528 kfree(func->eps);
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001529 /*
1530 * eps and interfaces_nums are allocated in the same chunk so
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001531 * only one free is required. Descriptors are also allocated
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001532 * in the same chunk.
1533 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001534
1535 kfree(func);
1536}
1537
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001538static void ffs_func_eps_disable(struct ffs_function *func)
1539{
1540 struct ffs_ep *ep = func->eps;
1541 struct ffs_epfile *epfile = func->ffs->epfiles;
1542 unsigned count = func->ffs->eps_count;
1543 unsigned long flags;
1544
1545 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1546 do {
1547 /* pending requests get nuked */
1548 if (likely(ep->ep))
1549 usb_ep_disable(ep->ep);
1550 epfile->ep = NULL;
1551
1552 ++ep;
1553 ++epfile;
1554 } while (--count);
1555 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1556}
1557
1558static int ffs_func_eps_enable(struct ffs_function *func)
1559{
1560 struct ffs_data *ffs = func->ffs;
1561 struct ffs_ep *ep = func->eps;
1562 struct ffs_epfile *epfile = ffs->epfiles;
1563 unsigned count = ffs->eps_count;
1564 unsigned long flags;
1565 int ret = 0;
1566
1567 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1568 do {
1569 struct usb_endpoint_descriptor *ds;
1570 ds = ep->descs[ep->descs[1] ? 1 : 0];
1571
1572 ep->ep->driver_data = ep;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +03001573 ep->ep->desc = ds;
1574 ret = usb_ep_enable(ep->ep);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001575 if (likely(!ret)) {
1576 epfile->ep = ep;
1577 epfile->in = usb_endpoint_dir_in(ds);
1578 epfile->isoc = usb_endpoint_xfer_isoc(ds);
1579 } else {
1580 break;
1581 }
1582
1583 wake_up(&epfile->wait);
1584
1585 ++ep;
1586 ++epfile;
1587 } while (--count);
1588 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1589
1590 return ret;
1591}
1592
1593
1594/* Parsing and building descriptors and strings *****************************/
1595
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001596/*
1597 * This validates if data pointed by data is a valid USB descriptor as
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001598 * well as record how many interfaces, endpoints and strings are
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001599 * required by given configuration. Returns address after the
1600 * descriptor or NULL if data is invalid.
1601 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001602
1603enum ffs_entity_type {
1604 FFS_DESCRIPTOR, FFS_INTERFACE, FFS_STRING, FFS_ENDPOINT
1605};
1606
1607typedef int (*ffs_entity_callback)(enum ffs_entity_type entity,
1608 u8 *valuep,
1609 struct usb_descriptor_header *desc,
1610 void *priv);
1611
1612static int __must_check ffs_do_desc(char *data, unsigned len,
1613 ffs_entity_callback entity, void *priv)
1614{
1615 struct usb_descriptor_header *_ds = (void *)data;
1616 u8 length;
1617 int ret;
1618
1619 ENTER();
1620
1621 /* At least two bytes are required: length and type */
1622 if (len < 2) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001623 pr_vdebug("descriptor too short\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001624 return -EINVAL;
1625 }
1626
1627 /* If we have at least as many bytes as the descriptor takes? */
1628 length = _ds->bLength;
1629 if (len < length) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001630 pr_vdebug("descriptor longer then available data\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001631 return -EINVAL;
1632 }
1633
1634#define __entity_check_INTERFACE(val) 1
1635#define __entity_check_STRING(val) (val)
1636#define __entity_check_ENDPOINT(val) ((val) & USB_ENDPOINT_NUMBER_MASK)
1637#define __entity(type, val) do { \
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001638 pr_vdebug("entity " #type "(%02x)\n", (val)); \
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001639 if (unlikely(!__entity_check_ ##type(val))) { \
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001640 pr_vdebug("invalid entity's value\n"); \
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001641 return -EINVAL; \
1642 } \
1643 ret = entity(FFS_ ##type, &val, _ds, priv); \
1644 if (unlikely(ret < 0)) { \
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001645 pr_debug("entity " #type "(%02x); ret = %d\n", \
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +01001646 (val), ret); \
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001647 return ret; \
1648 } \
1649 } while (0)
1650
1651 /* Parse descriptor depending on type. */
1652 switch (_ds->bDescriptorType) {
1653 case USB_DT_DEVICE:
1654 case USB_DT_CONFIG:
1655 case USB_DT_STRING:
1656 case USB_DT_DEVICE_QUALIFIER:
1657 /* function can't have any of those */
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001658 pr_vdebug("descriptor reserved for gadget: %d\n",
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001659 _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001660 return -EINVAL;
1661
1662 case USB_DT_INTERFACE: {
1663 struct usb_interface_descriptor *ds = (void *)_ds;
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001664 pr_vdebug("interface descriptor\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001665 if (length != sizeof *ds)
1666 goto inv_length;
1667
1668 __entity(INTERFACE, ds->bInterfaceNumber);
1669 if (ds->iInterface)
1670 __entity(STRING, ds->iInterface);
1671 }
1672 break;
1673
1674 case USB_DT_ENDPOINT: {
1675 struct usb_endpoint_descriptor *ds = (void *)_ds;
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001676 pr_vdebug("endpoint descriptor\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001677 if (length != USB_DT_ENDPOINT_SIZE &&
1678 length != USB_DT_ENDPOINT_AUDIO_SIZE)
1679 goto inv_length;
1680 __entity(ENDPOINT, ds->bEndpointAddress);
1681 }
1682 break;
1683
Koen Beel560f1182012-05-30 20:43:37 +02001684 case HID_DT_HID:
1685 pr_vdebug("hid descriptor\n");
1686 if (length != sizeof(struct hid_descriptor))
1687 goto inv_length;
1688 break;
1689
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001690 case USB_DT_OTG:
1691 if (length != sizeof(struct usb_otg_descriptor))
1692 goto inv_length;
1693 break;
1694
1695 case USB_DT_INTERFACE_ASSOCIATION: {
1696 struct usb_interface_assoc_descriptor *ds = (void *)_ds;
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001697 pr_vdebug("interface association descriptor\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001698 if (length != sizeof *ds)
1699 goto inv_length;
1700 if (ds->iFunction)
1701 __entity(STRING, ds->iFunction);
1702 }
1703 break;
1704
1705 case USB_DT_OTHER_SPEED_CONFIG:
1706 case USB_DT_INTERFACE_POWER:
1707 case USB_DT_DEBUG:
1708 case USB_DT_SECURITY:
1709 case USB_DT_CS_RADIO_CONTROL:
1710 /* TODO */
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001711 pr_vdebug("unimplemented descriptor: %d\n", _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001712 return -EINVAL;
1713
1714 default:
1715 /* We should never be here */
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001716 pr_vdebug("unknown descriptor: %d\n", _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001717 return -EINVAL;
1718
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001719inv_length:
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001720 pr_vdebug("invalid length: %d (descriptor %d)\n",
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +01001721 _ds->bLength, _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001722 return -EINVAL;
1723 }
1724
1725#undef __entity
1726#undef __entity_check_DESCRIPTOR
1727#undef __entity_check_INTERFACE
1728#undef __entity_check_STRING
1729#undef __entity_check_ENDPOINT
1730
1731 return length;
1732}
1733
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001734static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len,
1735 ffs_entity_callback entity, void *priv)
1736{
1737 const unsigned _len = len;
1738 unsigned long num = 0;
1739
1740 ENTER();
1741
1742 for (;;) {
1743 int ret;
1744
1745 if (num == count)
1746 data = NULL;
1747
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001748 /* Record "descriptor" entity */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001749 ret = entity(FFS_DESCRIPTOR, (u8 *)num, (void *)data, priv);
1750 if (unlikely(ret < 0)) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001751 pr_debug("entity DESCRIPTOR(%02lx); ret = %d\n",
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +01001752 num, ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001753 return ret;
1754 }
1755
1756 if (!data)
1757 return _len - len;
1758
1759 ret = ffs_do_desc(data, len, entity, priv);
1760 if (unlikely(ret < 0)) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001761 pr_debug("%s returns %d\n", __func__, ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001762 return ret;
1763 }
1764
1765 len -= ret;
1766 data += ret;
1767 ++num;
1768 }
1769}
1770
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001771static int __ffs_data_do_entity(enum ffs_entity_type type,
1772 u8 *valuep, struct usb_descriptor_header *desc,
1773 void *priv)
1774{
1775 struct ffs_data *ffs = priv;
1776
1777 ENTER();
1778
1779 switch (type) {
1780 case FFS_DESCRIPTOR:
1781 break;
1782
1783 case FFS_INTERFACE:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001784 /*
1785 * Interfaces are indexed from zero so if we
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001786 * encountered interface "n" then there are at least
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001787 * "n+1" interfaces.
1788 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001789 if (*valuep >= ffs->interfaces_count)
1790 ffs->interfaces_count = *valuep + 1;
1791 break;
1792
1793 case FFS_STRING:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001794 /*
1795 * Strings are indexed from 1 (0 is magic ;) reserved
1796 * for languages list or some such)
1797 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001798 if (*valuep > ffs->strings_count)
1799 ffs->strings_count = *valuep;
1800 break;
1801
1802 case FFS_ENDPOINT:
1803 /* Endpoints are indexed from 1 as well. */
1804 if ((*valuep & USB_ENDPOINT_NUMBER_MASK) > ffs->eps_count)
1805 ffs->eps_count = (*valuep & USB_ENDPOINT_NUMBER_MASK);
1806 break;
1807 }
1808
1809 return 0;
1810}
1811
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001812static int __ffs_data_got_descs(struct ffs_data *ffs,
1813 char *const _data, size_t len)
1814{
1815 unsigned fs_count, hs_count;
1816 int fs_len, ret = -EINVAL;
1817 char *data = _data;
1818
1819 ENTER();
1820
1821 if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_DESCRIPTORS_MAGIC ||
1822 get_unaligned_le32(data + 4) != len))
1823 goto error;
1824 fs_count = get_unaligned_le32(data + 8);
1825 hs_count = get_unaligned_le32(data + 12);
1826
1827 if (!fs_count && !hs_count)
1828 goto einval;
1829
1830 data += 16;
1831 len -= 16;
1832
1833 if (likely(fs_count)) {
1834 fs_len = ffs_do_descs(fs_count, data, len,
1835 __ffs_data_do_entity, ffs);
1836 if (unlikely(fs_len < 0)) {
1837 ret = fs_len;
1838 goto error;
1839 }
1840
1841 data += fs_len;
1842 len -= fs_len;
1843 } else {
1844 fs_len = 0;
1845 }
1846
1847 if (likely(hs_count)) {
1848 ret = ffs_do_descs(hs_count, data, len,
1849 __ffs_data_do_entity, ffs);
1850 if (unlikely(ret < 0))
1851 goto error;
1852 } else {
1853 ret = 0;
1854 }
1855
1856 if (unlikely(len != ret))
1857 goto einval;
1858
1859 ffs->raw_fs_descs_length = fs_len;
1860 ffs->raw_descs_length = fs_len + ret;
1861 ffs->raw_descs = _data;
1862 ffs->fs_descs_count = fs_count;
1863 ffs->hs_descs_count = hs_count;
1864
1865 return 0;
1866
1867einval:
1868 ret = -EINVAL;
1869error:
1870 kfree(_data);
1871 return ret;
1872}
1873
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001874static int __ffs_data_got_strings(struct ffs_data *ffs,
1875 char *const _data, size_t len)
1876{
1877 u32 str_count, needed_count, lang_count;
1878 struct usb_gadget_strings **stringtabs, *t;
1879 struct usb_string *strings, *s;
1880 const char *data = _data;
1881
1882 ENTER();
1883
1884 if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_STRINGS_MAGIC ||
1885 get_unaligned_le32(data + 4) != len))
1886 goto error;
1887 str_count = get_unaligned_le32(data + 8);
1888 lang_count = get_unaligned_le32(data + 12);
1889
1890 /* if one is zero the other must be zero */
1891 if (unlikely(!str_count != !lang_count))
1892 goto error;
1893
1894 /* Do we have at least as many strings as descriptors need? */
1895 needed_count = ffs->strings_count;
1896 if (unlikely(str_count < needed_count))
1897 goto error;
1898
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001899 /*
1900 * If we don't need any strings just return and free all
1901 * memory.
1902 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001903 if (!needed_count) {
1904 kfree(_data);
1905 return 0;
1906 }
1907
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001908 /* Allocate everything in one chunk so there's less maintenance. */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001909 {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001910 struct {
1911 struct usb_gadget_strings *stringtabs[lang_count + 1];
1912 struct usb_gadget_strings stringtab[lang_count];
1913 struct usb_string strings[lang_count*(needed_count+1)];
1914 } *d;
1915 unsigned i = 0;
1916
1917 d = kmalloc(sizeof *d, GFP_KERNEL);
1918 if (unlikely(!d)) {
1919 kfree(_data);
1920 return -ENOMEM;
1921 }
1922
1923 stringtabs = d->stringtabs;
1924 t = d->stringtab;
1925 i = lang_count;
1926 do {
1927 *stringtabs++ = t++;
1928 } while (--i);
1929 *stringtabs = NULL;
1930
1931 stringtabs = d->stringtabs;
1932 t = d->stringtab;
1933 s = d->strings;
1934 strings = s;
1935 }
1936
1937 /* For each language */
1938 data += 16;
1939 len -= 16;
1940
1941 do { /* lang_count > 0 so we can use do-while */
1942 unsigned needed = needed_count;
1943
1944 if (unlikely(len < 3))
1945 goto error_free;
1946 t->language = get_unaligned_le16(data);
1947 t->strings = s;
1948 ++t;
1949
1950 data += 2;
1951 len -= 2;
1952
1953 /* For each string */
1954 do { /* str_count > 0 so we can use do-while */
1955 size_t length = strnlen(data, len);
1956
1957 if (unlikely(length == len))
1958 goto error_free;
1959
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001960 /*
1961 * User may provide more strings then we need,
1962 * if that's the case we simply ignore the
1963 * rest
1964 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001965 if (likely(needed)) {
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001966 /*
1967 * s->id will be set while adding
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001968 * function to configuration so for
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001969 * now just leave garbage here.
1970 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001971 s->s = data;
1972 --needed;
1973 ++s;
1974 }
1975
1976 data += length + 1;
1977 len -= length + 1;
1978 } while (--str_count);
1979
1980 s->id = 0; /* terminator */
1981 s->s = NULL;
1982 ++s;
1983
1984 } while (--lang_count);
1985
1986 /* Some garbage left? */
1987 if (unlikely(len))
1988 goto error_free;
1989
1990 /* Done! */
1991 ffs->stringtabs = stringtabs;
1992 ffs->raw_strings = _data;
1993
1994 return 0;
1995
1996error_free:
1997 kfree(stringtabs);
1998error:
1999 kfree(_data);
2000 return -EINVAL;
2001}
2002
2003
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002004/* Events handling and management *******************************************/
2005
2006static void __ffs_event_add(struct ffs_data *ffs,
2007 enum usb_functionfs_event_type type)
2008{
2009 enum usb_functionfs_event_type rem_type1, rem_type2 = type;
2010 int neg = 0;
2011
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002012 /*
2013 * Abort any unhandled setup
2014 *
2015 * We do not need to worry about some cmpxchg() changing value
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002016 * of ffs->setup_state without holding the lock because when
2017 * state is FFS_SETUP_PENDING cmpxchg() in several places in
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002018 * the source does nothing.
2019 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002020 if (ffs->setup_state == FFS_SETUP_PENDING)
2021 ffs->setup_state = FFS_SETUP_CANCELED;
2022
2023 switch (type) {
2024 case FUNCTIONFS_RESUME:
2025 rem_type2 = FUNCTIONFS_SUSPEND;
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002026 /* FALL THROUGH */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002027 case FUNCTIONFS_SUSPEND:
2028 case FUNCTIONFS_SETUP:
2029 rem_type1 = type;
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002030 /* Discard all similar events */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002031 break;
2032
2033 case FUNCTIONFS_BIND:
2034 case FUNCTIONFS_UNBIND:
2035 case FUNCTIONFS_DISABLE:
2036 case FUNCTIONFS_ENABLE:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002037 /* Discard everything other then power management. */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002038 rem_type1 = FUNCTIONFS_SUSPEND;
2039 rem_type2 = FUNCTIONFS_RESUME;
2040 neg = 1;
2041 break;
2042
2043 default:
2044 BUG();
2045 }
2046
2047 {
2048 u8 *ev = ffs->ev.types, *out = ev;
2049 unsigned n = ffs->ev.count;
2050 for (; n; --n, ++ev)
2051 if ((*ev == rem_type1 || *ev == rem_type2) == neg)
2052 *out++ = *ev;
2053 else
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002054 pr_vdebug("purging event %d\n", *ev);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002055 ffs->ev.count = out - ffs->ev.types;
2056 }
2057
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002058 pr_vdebug("adding event %d\n", type);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002059 ffs->ev.types[ffs->ev.count++] = type;
2060 wake_up_locked(&ffs->ev.waitq);
2061}
2062
2063static void ffs_event_add(struct ffs_data *ffs,
2064 enum usb_functionfs_event_type type)
2065{
2066 unsigned long flags;
2067 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2068 __ffs_event_add(ffs, type);
2069 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2070}
2071
2072
2073/* Bind/unbind USB function hooks *******************************************/
2074
2075static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
2076 struct usb_descriptor_header *desc,
2077 void *priv)
2078{
2079 struct usb_endpoint_descriptor *ds = (void *)desc;
2080 struct ffs_function *func = priv;
2081 struct ffs_ep *ffs_ep;
2082
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002083 /*
2084 * If hs_descriptors is not NULL then we are reading hs
2085 * descriptors now
2086 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002087 const int isHS = func->function.hs_descriptors != NULL;
2088 unsigned idx;
2089
2090 if (type != FFS_DESCRIPTOR)
2091 return 0;
2092
2093 if (isHS)
2094 func->function.hs_descriptors[(long)valuep] = desc;
2095 else
2096 func->function.descriptors[(long)valuep] = desc;
2097
2098 if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT)
2099 return 0;
2100
2101 idx = (ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK) - 1;
2102 ffs_ep = func->eps + idx;
2103
2104 if (unlikely(ffs_ep->descs[isHS])) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002105 pr_vdebug("two %sspeed descriptors for EP %d\n",
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +01002106 isHS ? "high" : "full",
2107 ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002108 return -EINVAL;
2109 }
2110 ffs_ep->descs[isHS] = ds;
2111
2112 ffs_dump_mem(": Original ep desc", ds, ds->bLength);
2113 if (ffs_ep->ep) {
2114 ds->bEndpointAddress = ffs_ep->descs[0]->bEndpointAddress;
2115 if (!ds->wMaxPacketSize)
2116 ds->wMaxPacketSize = ffs_ep->descs[0]->wMaxPacketSize;
2117 } else {
2118 struct usb_request *req;
2119 struct usb_ep *ep;
2120
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002121 pr_vdebug("autoconfig\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002122 ep = usb_ep_autoconfig(func->gadget, ds);
2123 if (unlikely(!ep))
2124 return -ENOTSUPP;
Joe Perchescc7e6052010-11-14 19:04:49 -08002125 ep->driver_data = func->eps + idx;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002126
2127 req = usb_ep_alloc_request(ep, GFP_KERNEL);
2128 if (unlikely(!req))
2129 return -ENOMEM;
2130
2131 ffs_ep->ep = ep;
2132 ffs_ep->req = req;
2133 func->eps_revmap[ds->bEndpointAddress &
2134 USB_ENDPOINT_NUMBER_MASK] = idx + 1;
2135 }
2136 ffs_dump_mem(": Rewritten ep desc", ds, ds->bLength);
2137
2138 return 0;
2139}
2140
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002141static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep,
2142 struct usb_descriptor_header *desc,
2143 void *priv)
2144{
2145 struct ffs_function *func = priv;
2146 unsigned idx;
2147 u8 newValue;
2148
2149 switch (type) {
2150 default:
2151 case FFS_DESCRIPTOR:
2152 /* Handled in previous pass by __ffs_func_bind_do_descs() */
2153 return 0;
2154
2155 case FFS_INTERFACE:
2156 idx = *valuep;
2157 if (func->interfaces_nums[idx] < 0) {
2158 int id = usb_interface_id(func->conf, &func->function);
2159 if (unlikely(id < 0))
2160 return id;
2161 func->interfaces_nums[idx] = id;
2162 }
2163 newValue = func->interfaces_nums[idx];
2164 break;
2165
2166 case FFS_STRING:
2167 /* String' IDs are allocated when fsf_data is bound to cdev */
2168 newValue = func->ffs->stringtabs[0]->strings[*valuep - 1].id;
2169 break;
2170
2171 case FFS_ENDPOINT:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002172 /*
2173 * USB_DT_ENDPOINT are handled in
2174 * __ffs_func_bind_do_descs().
2175 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002176 if (desc->bDescriptorType == USB_DT_ENDPOINT)
2177 return 0;
2178
2179 idx = (*valuep & USB_ENDPOINT_NUMBER_MASK) - 1;
2180 if (unlikely(!func->eps[idx].ep))
2181 return -EINVAL;
2182
2183 {
2184 struct usb_endpoint_descriptor **descs;
2185 descs = func->eps[idx].descs;
2186 newValue = descs[descs[0] ? 0 : 1]->bEndpointAddress;
2187 }
2188 break;
2189 }
2190
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002191 pr_vdebug("%02x -> %02x\n", *valuep, newValue);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002192 *valuep = newValue;
2193 return 0;
2194}
2195
2196static int ffs_func_bind(struct usb_configuration *c,
2197 struct usb_function *f)
2198{
2199 struct ffs_function *func = ffs_func_from_usb(f);
2200 struct ffs_data *ffs = func->ffs;
2201
2202 const int full = !!func->ffs->fs_descs_count;
2203 const int high = gadget_is_dualspeed(func->gadget) &&
2204 func->ffs->hs_descs_count;
2205
2206 int ret;
2207
2208 /* Make it a single chunk, less management later on */
2209 struct {
2210 struct ffs_ep eps[ffs->eps_count];
2211 struct usb_descriptor_header
2212 *fs_descs[full ? ffs->fs_descs_count + 1 : 0];
2213 struct usb_descriptor_header
2214 *hs_descs[high ? ffs->hs_descs_count + 1 : 0];
2215 short inums[ffs->interfaces_count];
2216 char raw_descs[high ? ffs->raw_descs_length
2217 : ffs->raw_fs_descs_length];
2218 } *data;
2219
2220 ENTER();
2221
2222 /* Only high speed but not supported by gadget? */
2223 if (unlikely(!(full | high)))
2224 return -ENOTSUPP;
2225
2226 /* Allocate */
2227 data = kmalloc(sizeof *data, GFP_KERNEL);
2228 if (unlikely(!data))
2229 return -ENOMEM;
2230
2231 /* Zero */
2232 memset(data->eps, 0, sizeof data->eps);
2233 memcpy(data->raw_descs, ffs->raw_descs + 16, sizeof data->raw_descs);
2234 memset(data->inums, 0xff, sizeof data->inums);
2235 for (ret = ffs->eps_count; ret; --ret)
2236 data->eps[ret].num = -1;
2237
2238 /* Save pointers */
2239 func->eps = data->eps;
2240 func->interfaces_nums = data->inums;
2241
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002242 /*
2243 * Go through all the endpoint descriptors and allocate
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002244 * endpoints first, so that later we can rewrite the endpoint
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002245 * numbers without worrying that it may be described later on.
2246 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002247 if (likely(full)) {
2248 func->function.descriptors = data->fs_descs;
2249 ret = ffs_do_descs(ffs->fs_descs_count,
2250 data->raw_descs,
2251 sizeof data->raw_descs,
2252 __ffs_func_bind_do_descs, func);
2253 if (unlikely(ret < 0))
2254 goto error;
2255 } else {
2256 ret = 0;
2257 }
2258
2259 if (likely(high)) {
2260 func->function.hs_descriptors = data->hs_descs;
2261 ret = ffs_do_descs(ffs->hs_descs_count,
2262 data->raw_descs + ret,
2263 (sizeof data->raw_descs) - ret,
2264 __ffs_func_bind_do_descs, func);
2265 }
2266
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002267 /*
2268 * Now handle interface numbers allocation and interface and
2269 * endpoint numbers rewriting. We can do that in one go
2270 * now.
2271 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002272 ret = ffs_do_descs(ffs->fs_descs_count +
2273 (high ? ffs->hs_descs_count : 0),
2274 data->raw_descs, sizeof data->raw_descs,
2275 __ffs_func_bind_do_nums, func);
2276 if (unlikely(ret < 0))
2277 goto error;
2278
2279 /* And we're done */
2280 ffs_event_add(ffs, FUNCTIONFS_BIND);
2281 return 0;
2282
2283error:
2284 /* XXX Do we need to release all claimed endpoints here? */
2285 return ret;
2286}
2287
2288
2289/* Other USB function hooks *************************************************/
2290
2291static void ffs_func_unbind(struct usb_configuration *c,
2292 struct usb_function *f)
2293{
2294 struct ffs_function *func = ffs_func_from_usb(f);
2295 struct ffs_data *ffs = func->ffs;
2296
2297 ENTER();
2298
2299 if (ffs->func == func) {
2300 ffs_func_eps_disable(func);
2301 ffs->func = NULL;
2302 }
2303
2304 ffs_event_add(ffs, FUNCTIONFS_UNBIND);
2305
2306 ffs_func_free(func);
2307}
2308
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002309static int ffs_func_set_alt(struct usb_function *f,
2310 unsigned interface, unsigned alt)
2311{
2312 struct ffs_function *func = ffs_func_from_usb(f);
2313 struct ffs_data *ffs = func->ffs;
2314 int ret = 0, intf;
2315
2316 if (alt != (unsigned)-1) {
2317 intf = ffs_func_revmap_intf(func, interface);
2318 if (unlikely(intf < 0))
2319 return intf;
2320 }
2321
2322 if (ffs->func)
2323 ffs_func_eps_disable(ffs->func);
2324
2325 if (ffs->state != FFS_ACTIVE)
2326 return -ENODEV;
2327
2328 if (alt == (unsigned)-1) {
2329 ffs->func = NULL;
2330 ffs_event_add(ffs, FUNCTIONFS_DISABLE);
2331 return 0;
2332 }
2333
2334 ffs->func = func;
2335 ret = ffs_func_eps_enable(func);
2336 if (likely(ret >= 0))
2337 ffs_event_add(ffs, FUNCTIONFS_ENABLE);
2338 return ret;
2339}
2340
2341static void ffs_func_disable(struct usb_function *f)
2342{
2343 ffs_func_set_alt(f, 0, (unsigned)-1);
2344}
2345
2346static int ffs_func_setup(struct usb_function *f,
2347 const struct usb_ctrlrequest *creq)
2348{
2349 struct ffs_function *func = ffs_func_from_usb(f);
2350 struct ffs_data *ffs = func->ffs;
2351 unsigned long flags;
2352 int ret;
2353
2354 ENTER();
2355
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002356 pr_vdebug("creq->bRequestType = %02x\n", creq->bRequestType);
2357 pr_vdebug("creq->bRequest = %02x\n", creq->bRequest);
2358 pr_vdebug("creq->wValue = %04x\n", le16_to_cpu(creq->wValue));
2359 pr_vdebug("creq->wIndex = %04x\n", le16_to_cpu(creq->wIndex));
2360 pr_vdebug("creq->wLength = %04x\n", le16_to_cpu(creq->wLength));
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002361
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002362 /*
2363 * Most requests directed to interface go through here
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002364 * (notable exceptions are set/get interface) so we need to
2365 * handle them. All other either handled by composite or
2366 * passed to usb_configuration->setup() (if one is set). No
2367 * matter, we will handle requests directed to endpoint here
2368 * as well (as it's straightforward) but what to do with any
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002369 * other request?
2370 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002371 if (ffs->state != FFS_ACTIVE)
2372 return -ENODEV;
2373
2374 switch (creq->bRequestType & USB_RECIP_MASK) {
2375 case USB_RECIP_INTERFACE:
2376 ret = ffs_func_revmap_intf(func, le16_to_cpu(creq->wIndex));
2377 if (unlikely(ret < 0))
2378 return ret;
2379 break;
2380
2381 case USB_RECIP_ENDPOINT:
2382 ret = ffs_func_revmap_ep(func, le16_to_cpu(creq->wIndex));
2383 if (unlikely(ret < 0))
2384 return ret;
2385 break;
2386
2387 default:
2388 return -EOPNOTSUPP;
2389 }
2390
2391 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2392 ffs->ev.setup = *creq;
2393 ffs->ev.setup.wIndex = cpu_to_le16(ret);
2394 __ffs_event_add(ffs, FUNCTIONFS_SETUP);
2395 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2396
2397 return 0;
2398}
2399
2400static void ffs_func_suspend(struct usb_function *f)
2401{
2402 ENTER();
2403 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_SUSPEND);
2404}
2405
2406static void ffs_func_resume(struct usb_function *f)
2407{
2408 ENTER();
2409 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_RESUME);
2410}
2411
2412
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002413/* Endpoint and interface numbers reverse mapping ***************************/
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002414
2415static int ffs_func_revmap_ep(struct ffs_function *func, u8 num)
2416{
2417 num = func->eps_revmap[num & USB_ENDPOINT_NUMBER_MASK];
2418 return num ? num : -EDOM;
2419}
2420
2421static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf)
2422{
2423 short *nums = func->interfaces_nums;
2424 unsigned count = func->ffs->interfaces_count;
2425
2426 for (; count; --count, ++nums) {
2427 if (*nums >= 0 && *nums == intf)
2428 return nums - func->interfaces_nums;
2429 }
2430
2431 return -EDOM;
2432}
2433
2434
2435/* Misc helper functions ****************************************************/
2436
2437static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
2438{
2439 return nonblock
2440 ? likely(mutex_trylock(mutex)) ? 0 : -EAGAIN
2441 : mutex_lock_interruptible(mutex);
2442}
2443
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002444static char *ffs_prepare_buffer(const char * __user buf, size_t len)
2445{
2446 char *data;
2447
2448 if (unlikely(!len))
2449 return NULL;
2450
2451 data = kmalloc(len, GFP_KERNEL);
2452 if (unlikely(!data))
2453 return ERR_PTR(-ENOMEM);
2454
2455 if (unlikely(__copy_from_user(data, buf, len))) {
2456 kfree(data);
2457 return ERR_PTR(-EFAULT);
2458 }
2459
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002460 pr_vdebug("Buffer from user space:\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002461 ffs_dump_mem("", data, len);
2462
2463 return data;
2464}