blob: 0da66bacb0d434df93ade7aa425aa448d7fdf21a [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
Andrzej Pietrasiewiczea0e6272012-06-11 11:13:15 +020037#ifndef pr_vdebug
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +010038# define pr_vdebug pr_debug
Andrzej Pietrasiewiczea0e6272012-06-11 11:13:15 +020039#endif /* pr_vdebug */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020040# define ffs_dump_mem(prefix, ptr, len) \
Michal Nazarewiczaa02f172010-11-17 17:09:47 +010041 print_hex_dump_bytes(pr_fmt(prefix ": "), DUMP_PREFIX_NONE, ptr, len)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020042#else
Andrzej Pietrasiewiczea0e6272012-06-11 11:13:15 +020043#ifndef pr_vdebug
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +010044# define pr_vdebug(...) do { } while (0)
Andrzej Pietrasiewiczea0e6272012-06-11 11:13:15 +020045#endif /* pr_vdebug */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020046# define ffs_dump_mem(prefix, ptr, len) do { } while (0)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020047#endif /* VERBOSE_DEBUG */
48
Michal Nazarewiczaa02f172010-11-17 17:09:47 +010049#define ENTER() pr_vdebug("%s()\n", __func__)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020050
51
52/* The data structure and setup file ****************************************/
53
54enum ffs_state {
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010055 /*
56 * Waiting for descriptors and strings.
57 *
58 * In this state no open(2), read(2) or write(2) on epfiles
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020059 * may succeed (which should not be the problem as there
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010060 * should be no such files opened in the first place).
61 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020062 FFS_READ_DESCRIPTORS,
63 FFS_READ_STRINGS,
64
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010065 /*
66 * We've got descriptors and strings. We are or have called
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020067 * functionfs_ready_callback(). functionfs_bind() may have
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010068 * been called but we don't know.
69 *
70 * This is the only state in which operations on epfiles may
71 * succeed.
72 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020073 FFS_ACTIVE,
74
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010075 /*
76 * All endpoints have been closed. This state is also set if
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020077 * we encounter an unrecoverable error. The only
78 * unrecoverable error is situation when after reading strings
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010079 * from user space we fail to initialise epfiles or
80 * functionfs_ready_callback() returns with error (<0).
81 *
82 * In this state no open(2), read(2) or write(2) (both on ep0
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020083 * as well as epfile) may succeed (at this point epfiles are
84 * unlinked and all closed so this is not a problem; ep0 is
85 * also closed but ep0 file exists and so open(2) on ep0 must
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010086 * fail).
87 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020088 FFS_CLOSING
89};
90
91
92enum ffs_setup_state {
93 /* There is no setup request pending. */
94 FFS_NO_SETUP,
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010095 /*
96 * User has read events and there was a setup request event
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +020097 * there. The next read/write on ep0 will handle the
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +010098 * request.
99 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200100 FFS_SETUP_PENDING,
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100101 /*
102 * There was event pending but before user space handled it
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200103 * some other event was introduced which canceled existing
104 * setup. If this state is set read/write on ep0 return
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100105 * -EIDRM. This state is only set when adding event.
106 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200107 FFS_SETUP_CANCELED
108};
109
110
111
112struct ffs_epfile;
113struct ffs_function;
114
115struct ffs_data {
116 struct usb_gadget *gadget;
117
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100118 /*
119 * Protect access read/write operations, only one read/write
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200120 * at a time. As a consequence protects ep0req and company.
121 * While setup request is being processed (queued) this is
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100122 * held.
123 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200124 struct mutex mutex;
125
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100126 /*
127 * Protect access to endpoint related structures (basically
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200128 * usb_ep_queue(), usb_ep_dequeue(), etc. calls) except for
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100129 * endpoint zero.
130 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200131 spinlock_t eps_lock;
132
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100133 /*
134 * XXX REVISIT do we need our own request? Since we are not
135 * handling setup requests immediately user space may be so
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200136 * slow that another setup will be sent to the gadget but this
137 * time not to us but another function and then there could be
Linus Torvaldsa4ce96a2010-07-21 09:25:42 -0700138 * a race. Is that the case? Or maybe we can use cdev->req
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100139 * after all, maybe we just need some spinlock for that?
140 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200141 struct usb_request *ep0req; /* P: mutex */
142 struct completion ep0req_completion; /* P: mutex */
143 int ep0req_status; /* P: mutex */
144
145 /* reference counter */
146 atomic_t ref;
147 /* how many files are opened (EP0 and others) */
148 atomic_t opened;
149
150 /* EP0 state */
151 enum ffs_state state;
152
153 /*
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100154 * Possible transitions:
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200155 * + FFS_NO_SETUP -> FFS_SETUP_PENDING -- P: ev.waitq.lock
156 * happens only in ep0 read which is P: mutex
157 * + FFS_SETUP_PENDING -> FFS_NO_SETUP -- P: ev.waitq.lock
158 * happens only in ep0 i/o which is P: mutex
159 * + FFS_SETUP_PENDING -> FFS_SETUP_CANCELED -- P: ev.waitq.lock
160 * + FFS_SETUP_CANCELED -> FFS_NO_SETUP -- cmpxchg
161 */
162 enum ffs_setup_state setup_state;
163
164#define FFS_SETUP_STATE(ffs) \
165 ((enum ffs_setup_state)cmpxchg(&(ffs)->setup_state, \
166 FFS_SETUP_CANCELED, FFS_NO_SETUP))
167
168 /* Events & such. */
169 struct {
170 u8 types[4];
171 unsigned short count;
172 /* XXX REVISIT need to update it in some places, or do we? */
173 unsigned short can_stall;
174 struct usb_ctrlrequest setup;
175
176 wait_queue_head_t waitq;
177 } ev; /* the whole structure, P: ev.waitq.lock */
178
179 /* Flags */
180 unsigned long flags;
181#define FFS_FL_CALL_CLOSED_CALLBACK 0
182#define FFS_FL_BOUND 1
183
184 /* Active function */
185 struct ffs_function *func;
186
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100187 /*
188 * Device name, write once when file system is mounted.
189 * Intended for user to read if she wants.
190 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200191 const char *dev_name;
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100192 /* Private data for our user (ie. gadget). Managed by user. */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200193 void *private_data;
194
195 /* filled by __ffs_data_got_descs() */
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100196 /*
197 * Real descriptors are 16 bytes after raw_descs (so you need
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200198 * to skip 16 bytes (ie. ffs->raw_descs + 16) to get to the
199 * first full speed descriptor). raw_descs_length and
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100200 * raw_fs_descs_length do not have those 16 bytes added.
201 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200202 const void *raw_descs;
203 unsigned raw_descs_length;
204 unsigned raw_fs_descs_length;
205 unsigned fs_descs_count;
206 unsigned hs_descs_count;
207
208 unsigned short strings_count;
209 unsigned short interfaces_count;
210 unsigned short eps_count;
211 unsigned short _pad1;
212
213 /* filled by __ffs_data_got_strings() */
214 /* ids in stringtabs are set in functionfs_bind() */
215 const void *raw_strings;
216 struct usb_gadget_strings **stringtabs;
217
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100218 /*
219 * File system's super block, write once when file system is
220 * mounted.
221 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200222 struct super_block *sb;
223
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100224 /* File permissions, written once when fs is mounted */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200225 struct ffs_file_perms {
226 umode_t mode;
Eric W. Biedermanb9b73f72012-06-14 01:19:23 -0700227 kuid_t uid;
228 kgid_t gid;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200229 } file_perms;
230
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100231 /*
232 * The endpoint files, filled by ffs_epfiles_create(),
233 * destroyed by ffs_epfiles_destroy().
234 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200235 struct ffs_epfile *epfiles;
236};
237
238/* Reference counter handling */
239static void ffs_data_get(struct ffs_data *ffs);
240static void ffs_data_put(struct ffs_data *ffs);
241/* Creates new ffs_data object. */
242static struct ffs_data *__must_check ffs_data_new(void) __attribute__((malloc));
243
244/* Opened counter handling. */
245static void ffs_data_opened(struct ffs_data *ffs);
246static void ffs_data_closed(struct ffs_data *ffs);
247
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100248/* Called with ffs->mutex held; take over ownership of data. */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200249static int __must_check
250__ffs_data_got_descs(struct ffs_data *ffs, char *data, size_t len);
251static int __must_check
252__ffs_data_got_strings(struct ffs_data *ffs, char *data, size_t len);
253
254
255/* The function structure ***************************************************/
256
257struct ffs_ep;
258
259struct ffs_function {
260 struct usb_configuration *conf;
261 struct usb_gadget *gadget;
262 struct ffs_data *ffs;
263
264 struct ffs_ep *eps;
265 u8 eps_revmap[16];
266 short *interfaces_nums;
267
268 struct usb_function function;
269};
270
271
272static struct ffs_function *ffs_func_from_usb(struct usb_function *f)
273{
274 return container_of(f, struct ffs_function, function);
275}
276
277static void ffs_func_free(struct ffs_function *func);
278
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200279static void ffs_func_eps_disable(struct ffs_function *func);
280static int __must_check ffs_func_eps_enable(struct ffs_function *func);
281
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200282static int ffs_func_bind(struct usb_configuration *,
283 struct usb_function *);
284static void ffs_func_unbind(struct usb_configuration *,
285 struct usb_function *);
286static int ffs_func_set_alt(struct usb_function *, unsigned, unsigned);
287static void ffs_func_disable(struct usb_function *);
288static int ffs_func_setup(struct usb_function *,
289 const struct usb_ctrlrequest *);
290static void ffs_func_suspend(struct usb_function *);
291static void ffs_func_resume(struct usb_function *);
292
293
294static int ffs_func_revmap_ep(struct ffs_function *func, u8 num);
295static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf);
296
297
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200298/* The endpoints structures *************************************************/
299
300struct ffs_ep {
301 struct usb_ep *ep; /* P: ffs->eps_lock */
302 struct usb_request *req; /* P: epfile->mutex */
303
304 /* [0]: full speed, [1]: high speed */
305 struct usb_endpoint_descriptor *descs[2];
306
307 u8 num;
308
309 int status; /* P: epfile->mutex */
310};
311
312struct ffs_epfile {
313 /* Protects ep->ep and ep->req. */
314 struct mutex mutex;
315 wait_queue_head_t wait;
316
317 struct ffs_data *ffs;
318 struct ffs_ep *ep; /* P: ffs->eps_lock */
319
320 struct dentry *dentry;
321
322 char name[5];
323
324 unsigned char in; /* P: ffs->eps_lock */
325 unsigned char isoc; /* P: ffs->eps_lock */
326
327 unsigned char _pad;
328};
329
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200330static int __must_check ffs_epfiles_create(struct ffs_data *ffs);
331static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count);
332
333static struct inode *__must_check
334ffs_sb_create_file(struct super_block *sb, const char *name, void *data,
335 const struct file_operations *fops,
336 struct dentry **dentry_p);
337
338
339/* Misc helper functions ****************************************************/
340
341static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
342 __attribute__((warn_unused_result, nonnull));
Al Viro260ef312012-09-26 21:43:45 -0400343static char *ffs_prepare_buffer(const char __user *buf, size_t len)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200344 __attribute__((warn_unused_result, nonnull));
345
346
347/* Control file aka ep0 *****************************************************/
348
349static void ffs_ep0_complete(struct usb_ep *ep, struct usb_request *req)
350{
351 struct ffs_data *ffs = req->context;
352
353 complete_all(&ffs->ep0req_completion);
354}
355
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200356static int __ffs_ep0_queue_wait(struct ffs_data *ffs, char *data, size_t len)
357{
358 struct usb_request *req = ffs->ep0req;
359 int ret;
360
361 req->zero = len < le16_to_cpu(ffs->ev.setup.wLength);
362
363 spin_unlock_irq(&ffs->ev.waitq.lock);
364
365 req->buf = data;
366 req->length = len;
367
Marek Szyprowskice1fd352011-01-28 13:55:36 +0100368 /*
369 * UDC layer requires to provide a buffer even for ZLP, but should
370 * not use it at all. Let's provide some poisoned pointer to catch
371 * possible bug in the driver.
372 */
373 if (req->buf == NULL)
374 req->buf = (void *)0xDEADBABE;
375
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200376 INIT_COMPLETION(ffs->ep0req_completion);
377
378 ret = usb_ep_queue(ffs->gadget->ep0, req, GFP_ATOMIC);
379 if (unlikely(ret < 0))
380 return ret;
381
382 ret = wait_for_completion_interruptible(&ffs->ep0req_completion);
383 if (unlikely(ret)) {
384 usb_ep_dequeue(ffs->gadget->ep0, req);
385 return -EINTR;
386 }
387
388 ffs->setup_state = FFS_NO_SETUP;
389 return ffs->ep0req_status;
390}
391
392static int __ffs_ep0_stall(struct ffs_data *ffs)
393{
394 if (ffs->ev.can_stall) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100395 pr_vdebug("ep0 stall\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200396 usb_ep_set_halt(ffs->gadget->ep0);
397 ffs->setup_state = FFS_NO_SETUP;
398 return -EL2HLT;
399 } else {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100400 pr_debug("bogus ep0 stall!\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200401 return -ESRCH;
402 }
403}
404
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200405static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
406 size_t len, loff_t *ptr)
407{
408 struct ffs_data *ffs = file->private_data;
409 ssize_t ret;
410 char *data;
411
412 ENTER();
413
414 /* Fast check if setup was canceled */
415 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED)
416 return -EIDRM;
417
418 /* Acquire mutex */
419 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
420 if (unlikely(ret < 0))
421 return ret;
422
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200423 /* Check state */
424 switch (ffs->state) {
425 case FFS_READ_DESCRIPTORS:
426 case FFS_READ_STRINGS:
427 /* Copy data */
428 if (unlikely(len < 16)) {
429 ret = -EINVAL;
430 break;
431 }
432
433 data = ffs_prepare_buffer(buf, len);
Tobias Klauser537baab2010-12-09 15:52:39 +0100434 if (IS_ERR(data)) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200435 ret = PTR_ERR(data);
436 break;
437 }
438
439 /* Handle data */
440 if (ffs->state == FFS_READ_DESCRIPTORS) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100441 pr_info("read descriptors\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200442 ret = __ffs_data_got_descs(ffs, data, len);
443 if (unlikely(ret < 0))
444 break;
445
446 ffs->state = FFS_READ_STRINGS;
447 ret = len;
448 } else {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +0100449 pr_info("read strings\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200450 ret = __ffs_data_got_strings(ffs, data, len);
451 if (unlikely(ret < 0))
452 break;
453
454 ret = ffs_epfiles_create(ffs);
455 if (unlikely(ret)) {
456 ffs->state = FFS_CLOSING;
457 break;
458 }
459
460 ffs->state = FFS_ACTIVE;
461 mutex_unlock(&ffs->mutex);
462
463 ret = functionfs_ready_callback(ffs);
464 if (unlikely(ret < 0)) {
465 ffs->state = FFS_CLOSING;
466 return ret;
467 }
468
469 set_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags);
470 return len;
471 }
472 break;
473
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200474 case FFS_ACTIVE:
475 data = NULL;
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100476 /*
477 * We're called from user space, we can use _irq
478 * rather then _irqsave
479 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200480 spin_lock_irq(&ffs->ev.waitq.lock);
481 switch (FFS_SETUP_STATE(ffs)) {
482 case FFS_SETUP_CANCELED:
483 ret = -EIDRM;
484 goto done_spin;
485
486 case FFS_NO_SETUP:
487 ret = -ESRCH;
488 goto done_spin;
489
490 case FFS_SETUP_PENDING:
491 break;
492 }
493
494 /* FFS_SETUP_PENDING */
495 if (!(ffs->ev.setup.bRequestType & USB_DIR_IN)) {
496 spin_unlock_irq(&ffs->ev.waitq.lock);
497 ret = __ffs_ep0_stall(ffs);
498 break;
499 }
500
501 /* FFS_SETUP_PENDING and not stall */
502 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
503
504 spin_unlock_irq(&ffs->ev.waitq.lock);
505
506 data = ffs_prepare_buffer(buf, len);
Tobias Klauser537baab2010-12-09 15:52:39 +0100507 if (IS_ERR(data)) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200508 ret = PTR_ERR(data);
509 break;
510 }
511
512 spin_lock_irq(&ffs->ev.waitq.lock);
513
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100514 /*
515 * We are guaranteed to be still in FFS_ACTIVE state
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200516 * but the state of setup could have changed from
517 * FFS_SETUP_PENDING to FFS_SETUP_CANCELED so we need
518 * to check for that. If that happened we copied data
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100519 * from user space in vain but it's unlikely.
520 *
521 * For sure we are not in FFS_NO_SETUP since this is
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200522 * the only place FFS_SETUP_PENDING -> FFS_NO_SETUP
523 * transition can be performed and it's protected by
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100524 * mutex.
525 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200526 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) {
527 ret = -EIDRM;
528done_spin:
529 spin_unlock_irq(&ffs->ev.waitq.lock);
530 } else {
531 /* unlocks spinlock */
532 ret = __ffs_ep0_queue_wait(ffs, data, len);
533 }
534 kfree(data);
535 break;
536
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200537 default:
538 ret = -EBADFD;
539 break;
540 }
541
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200542 mutex_unlock(&ffs->mutex);
543 return ret;
544}
545
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200546static ssize_t __ffs_ep0_read_events(struct ffs_data *ffs, char __user *buf,
547 size_t n)
548{
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100549 /*
550 * We are holding ffs->ev.waitq.lock and ffs->mutex and we need
551 * to release them.
552 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200553 struct usb_functionfs_event events[n];
554 unsigned i = 0;
555
556 memset(events, 0, sizeof events);
557
558 do {
559 events[i].type = ffs->ev.types[i];
560 if (events[i].type == FUNCTIONFS_SETUP) {
561 events[i].u.setup = ffs->ev.setup;
562 ffs->setup_state = FFS_SETUP_PENDING;
563 }
564 } while (++i < n);
565
566 if (n < ffs->ev.count) {
567 ffs->ev.count -= n;
568 memmove(ffs->ev.types, ffs->ev.types + n,
569 ffs->ev.count * sizeof *ffs->ev.types);
570 } else {
571 ffs->ev.count = 0;
572 }
573
574 spin_unlock_irq(&ffs->ev.waitq.lock);
575 mutex_unlock(&ffs->mutex);
576
577 return unlikely(__copy_to_user(buf, events, sizeof events))
578 ? -EFAULT : sizeof events;
579}
580
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200581static ssize_t ffs_ep0_read(struct file *file, char __user *buf,
582 size_t len, loff_t *ptr)
583{
584 struct ffs_data *ffs = file->private_data;
585 char *data = NULL;
586 size_t n;
587 int ret;
588
589 ENTER();
590
591 /* Fast check if setup was canceled */
592 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED)
593 return -EIDRM;
594
595 /* Acquire mutex */
596 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
597 if (unlikely(ret < 0))
598 return ret;
599
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200600 /* Check state */
601 if (ffs->state != FFS_ACTIVE) {
602 ret = -EBADFD;
603 goto done_mutex;
604 }
605
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100606 /*
607 * We're called from user space, we can use _irq rather then
608 * _irqsave
609 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200610 spin_lock_irq(&ffs->ev.waitq.lock);
611
612 switch (FFS_SETUP_STATE(ffs)) {
613 case FFS_SETUP_CANCELED:
614 ret = -EIDRM;
615 break;
616
617 case FFS_NO_SETUP:
618 n = len / sizeof(struct usb_functionfs_event);
619 if (unlikely(!n)) {
620 ret = -EINVAL;
621 break;
622 }
623
624 if ((file->f_flags & O_NONBLOCK) && !ffs->ev.count) {
625 ret = -EAGAIN;
626 break;
627 }
628
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100629 if (wait_event_interruptible_exclusive_locked_irq(ffs->ev.waitq,
630 ffs->ev.count)) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200631 ret = -EINTR;
632 break;
633 }
634
635 return __ffs_ep0_read_events(ffs, buf,
636 min(n, (size_t)ffs->ev.count));
637
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200638 case FFS_SETUP_PENDING:
639 if (ffs->ev.setup.bRequestType & USB_DIR_IN) {
640 spin_unlock_irq(&ffs->ev.waitq.lock);
641 ret = __ffs_ep0_stall(ffs);
642 goto done_mutex;
643 }
644
645 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
646
647 spin_unlock_irq(&ffs->ev.waitq.lock);
648
649 if (likely(len)) {
650 data = kmalloc(len, GFP_KERNEL);
651 if (unlikely(!data)) {
652 ret = -ENOMEM;
653 goto done_mutex;
654 }
655 }
656
657 spin_lock_irq(&ffs->ev.waitq.lock);
658
659 /* See ffs_ep0_write() */
660 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) {
661 ret = -EIDRM;
662 break;
663 }
664
665 /* unlocks spinlock */
666 ret = __ffs_ep0_queue_wait(ffs, data, len);
667 if (likely(ret > 0) && unlikely(__copy_to_user(buf, data, len)))
668 ret = -EFAULT;
669 goto done_mutex;
670
671 default:
672 ret = -EBADFD;
673 break;
674 }
675
676 spin_unlock_irq(&ffs->ev.waitq.lock);
677done_mutex:
678 mutex_unlock(&ffs->mutex);
679 kfree(data);
680 return ret;
681}
682
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200683static int ffs_ep0_open(struct inode *inode, struct file *file)
684{
685 struct ffs_data *ffs = inode->i_private;
686
687 ENTER();
688
689 if (unlikely(ffs->state == FFS_CLOSING))
690 return -EBUSY;
691
692 file->private_data = ffs;
693 ffs_data_opened(ffs);
694
695 return 0;
696}
697
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200698static int ffs_ep0_release(struct inode *inode, struct file *file)
699{
700 struct ffs_data *ffs = file->private_data;
701
702 ENTER();
703
704 ffs_data_closed(ffs);
705
706 return 0;
707}
708
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200709static long ffs_ep0_ioctl(struct file *file, unsigned code, unsigned long value)
710{
711 struct ffs_data *ffs = file->private_data;
712 struct usb_gadget *gadget = ffs->gadget;
713 long ret;
714
715 ENTER();
716
717 if (code == FUNCTIONFS_INTERFACE_REVMAP) {
718 struct ffs_function *func = ffs->func;
719 ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV;
Andrzej Pietrasiewicz92b0abf2012-03-28 09:30:50 +0200720 } else if (gadget && gadget->ops->ioctl) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200721 ret = gadget->ops->ioctl(gadget, code, value);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200722 } else {
723 ret = -ENOTTY;
724 }
725
726 return ret;
727}
728
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200729static const struct file_operations ffs_ep0_operations = {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200730 .llseek = no_llseek,
731
732 .open = ffs_ep0_open,
733 .write = ffs_ep0_write,
734 .read = ffs_ep0_read,
735 .release = ffs_ep0_release,
736 .unlocked_ioctl = ffs_ep0_ioctl,
737};
738
739
740/* "Normal" endpoints operations ********************************************/
741
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200742static void ffs_epfile_io_complete(struct usb_ep *_ep, struct usb_request *req)
743{
744 ENTER();
745 if (likely(req->context)) {
746 struct ffs_ep *ep = _ep->driver_data;
747 ep->status = req->status ? req->status : req->actual;
748 complete(req->context);
749 }
750}
751
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200752static ssize_t ffs_epfile_io(struct file *file,
753 char __user *buf, size_t len, int read)
754{
755 struct ffs_epfile *epfile = file->private_data;
756 struct ffs_ep *ep;
757 char *data = NULL;
758 ssize_t ret;
759 int halt;
760
761 goto first_try;
762 do {
763 spin_unlock_irq(&epfile->ffs->eps_lock);
764 mutex_unlock(&epfile->mutex);
765
766first_try:
767 /* Are we still active? */
768 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE)) {
769 ret = -ENODEV;
770 goto error;
771 }
772
773 /* Wait for endpoint to be enabled */
774 ep = epfile->ep;
775 if (!ep) {
776 if (file->f_flags & O_NONBLOCK) {
777 ret = -EAGAIN;
778 goto error;
779 }
780
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100781 if (wait_event_interruptible(epfile->wait,
782 (ep = epfile->ep))) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200783 ret = -EINTR;
784 goto error;
785 }
786 }
787
788 /* Do we halt? */
789 halt = !read == !epfile->in;
790 if (halt && epfile->isoc) {
791 ret = -EINVAL;
792 goto error;
793 }
794
795 /* Allocate & copy */
796 if (!halt && !data) {
797 data = kzalloc(len, GFP_KERNEL);
798 if (unlikely(!data))
799 return -ENOMEM;
800
801 if (!read &&
802 unlikely(__copy_from_user(data, buf, len))) {
803 ret = -EFAULT;
804 goto error;
805 }
806 }
807
808 /* We will be using request */
809 ret = ffs_mutex_lock(&epfile->mutex,
810 file->f_flags & O_NONBLOCK);
811 if (unlikely(ret))
812 goto error;
813
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100814 /*
815 * We're called from user space, we can use _irq rather then
816 * _irqsave
817 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200818 spin_lock_irq(&epfile->ffs->eps_lock);
819
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100820 /*
821 * While we were acquiring mutex endpoint got disabled
822 * or changed?
823 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200824 } while (unlikely(epfile->ep != ep));
825
826 /* Halt */
827 if (unlikely(halt)) {
828 if (likely(epfile->ep == ep) && !WARN_ON(!ep->ep))
829 usb_ep_set_halt(ep->ep);
830 spin_unlock_irq(&epfile->ffs->eps_lock);
831 ret = -EBADMSG;
832 } else {
833 /* Fire the request */
834 DECLARE_COMPLETION_ONSTACK(done);
835
836 struct usb_request *req = ep->req;
837 req->context = &done;
838 req->complete = ffs_epfile_io_complete;
839 req->buf = data;
840 req->length = len;
841
842 ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
843
844 spin_unlock_irq(&epfile->ffs->eps_lock);
845
846 if (unlikely(ret < 0)) {
847 /* nop */
848 } else if (unlikely(wait_for_completion_interruptible(&done))) {
849 ret = -EINTR;
850 usb_ep_dequeue(ep->ep, req);
851 } else {
852 ret = ep->status;
853 if (read && ret > 0 &&
854 unlikely(copy_to_user(buf, data, ret)))
855 ret = -EFAULT;
856 }
857 }
858
859 mutex_unlock(&epfile->mutex);
860error:
861 kfree(data);
862 return ret;
863}
864
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200865static ssize_t
866ffs_epfile_write(struct file *file, const char __user *buf, size_t len,
867 loff_t *ptr)
868{
869 ENTER();
870
871 return ffs_epfile_io(file, (char __user *)buf, len, 0);
872}
873
874static ssize_t
875ffs_epfile_read(struct file *file, char __user *buf, size_t len, loff_t *ptr)
876{
877 ENTER();
878
879 return ffs_epfile_io(file, buf, len, 1);
880}
881
882static int
883ffs_epfile_open(struct inode *inode, struct file *file)
884{
885 struct ffs_epfile *epfile = inode->i_private;
886
887 ENTER();
888
889 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
890 return -ENODEV;
891
892 file->private_data = epfile;
893 ffs_data_opened(epfile->ffs);
894
895 return 0;
896}
897
898static int
899ffs_epfile_release(struct inode *inode, struct file *file)
900{
901 struct ffs_epfile *epfile = inode->i_private;
902
903 ENTER();
904
905 ffs_data_closed(epfile->ffs);
906
907 return 0;
908}
909
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200910static long ffs_epfile_ioctl(struct file *file, unsigned code,
911 unsigned long value)
912{
913 struct ffs_epfile *epfile = file->private_data;
914 int ret;
915
916 ENTER();
917
918 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
919 return -ENODEV;
920
921 spin_lock_irq(&epfile->ffs->eps_lock);
922 if (likely(epfile->ep)) {
923 switch (code) {
924 case FUNCTIONFS_FIFO_STATUS:
925 ret = usb_ep_fifo_status(epfile->ep->ep);
926 break;
927 case FUNCTIONFS_FIFO_FLUSH:
928 usb_ep_fifo_flush(epfile->ep->ep);
929 ret = 0;
930 break;
931 case FUNCTIONFS_CLEAR_HALT:
932 ret = usb_ep_clear_halt(epfile->ep->ep);
933 break;
934 case FUNCTIONFS_ENDPOINT_REVMAP:
935 ret = epfile->ep->num;
936 break;
937 default:
938 ret = -ENOTTY;
939 }
940 } else {
941 ret = -ENODEV;
942 }
943 spin_unlock_irq(&epfile->ffs->eps_lock);
944
945 return ret;
946}
947
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200948static const struct file_operations ffs_epfile_operations = {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200949 .llseek = no_llseek,
950
951 .open = ffs_epfile_open,
952 .write = ffs_epfile_write,
953 .read = ffs_epfile_read,
954 .release = ffs_epfile_release,
955 .unlocked_ioctl = ffs_epfile_ioctl,
956};
957
958
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200959/* File system and super block operations ***********************************/
960
961/*
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100962 * Mounting the file system creates a controller file, used first for
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200963 * function configuration then later for event monitoring.
964 */
965
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200966static struct inode *__must_check
967ffs_sb_make_inode(struct super_block *sb, void *data,
968 const struct file_operations *fops,
969 const struct inode_operations *iops,
970 struct ffs_file_perms *perms)
971{
972 struct inode *inode;
973
974 ENTER();
975
976 inode = new_inode(sb);
977
978 if (likely(inode)) {
979 struct timespec current_time = CURRENT_TIME;
980
Al Viro12ba8d12010-10-27 04:19:36 +0100981 inode->i_ino = get_next_ino();
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200982 inode->i_mode = perms->mode;
983 inode->i_uid = perms->uid;
984 inode->i_gid = perms->gid;
985 inode->i_atime = current_time;
986 inode->i_mtime = current_time;
987 inode->i_ctime = current_time;
988 inode->i_private = data;
989 if (fops)
990 inode->i_fop = fops;
991 if (iops)
992 inode->i_op = iops;
993 }
994
995 return inode;
996}
997
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200998/* Create "regular" file */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +0200999static struct inode *ffs_sb_create_file(struct super_block *sb,
1000 const char *name, void *data,
1001 const struct file_operations *fops,
1002 struct dentry **dentry_p)
1003{
1004 struct ffs_data *ffs = sb->s_fs_info;
1005 struct dentry *dentry;
1006 struct inode *inode;
1007
1008 ENTER();
1009
1010 dentry = d_alloc_name(sb->s_root, name);
1011 if (unlikely(!dentry))
1012 return NULL;
1013
1014 inode = ffs_sb_make_inode(sb, data, fops, NULL, &ffs->file_perms);
1015 if (unlikely(!inode)) {
1016 dput(dentry);
1017 return NULL;
1018 }
1019
1020 d_add(dentry, inode);
1021 if (dentry_p)
1022 *dentry_p = dentry;
1023
1024 return inode;
1025}
1026
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001027/* Super block */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001028static const struct super_operations ffs_sb_operations = {
1029 .statfs = simple_statfs,
1030 .drop_inode = generic_delete_inode,
1031};
1032
1033struct ffs_sb_fill_data {
1034 struct ffs_file_perms perms;
1035 umode_t root_mode;
1036 const char *dev_name;
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001037 union {
1038 /* set by ffs_fs_mount(), read by ffs_sb_fill() */
1039 void *private_data;
1040 /* set by ffs_sb_fill(), read by ffs_fs_mount */
1041 struct ffs_data *ffs_data;
1042 };
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001043};
1044
1045static int ffs_sb_fill(struct super_block *sb, void *_data, int silent)
1046{
1047 struct ffs_sb_fill_data *data = _data;
1048 struct inode *inode;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001049 struct ffs_data *ffs;
1050
1051 ENTER();
1052
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001053 /* Initialise data */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001054 ffs = ffs_data_new();
1055 if (unlikely(!ffs))
Al Viro5b5f9562012-01-08 15:38:27 -05001056 goto Enomem;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001057
1058 ffs->sb = sb;
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001059 ffs->dev_name = kstrdup(data->dev_name, GFP_KERNEL);
1060 if (unlikely(!ffs->dev_name))
1061 goto Enomem;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001062 ffs->file_perms = data->perms;
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001063 ffs->private_data = data->private_data;
1064
1065 /* used by the caller of this function */
1066 data->ffs_data = ffs;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001067
1068 sb->s_fs_info = ffs;
1069 sb->s_blocksize = PAGE_CACHE_SIZE;
1070 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1071 sb->s_magic = FUNCTIONFS_MAGIC;
1072 sb->s_op = &ffs_sb_operations;
1073 sb->s_time_gran = 1;
1074
1075 /* Root inode */
1076 data->perms.mode = data->root_mode;
1077 inode = ffs_sb_make_inode(sb, NULL,
1078 &simple_dir_operations,
1079 &simple_dir_inode_operations,
1080 &data->perms);
Al Viro48fde702012-01-08 22:15:13 -05001081 sb->s_root = d_make_root(inode);
1082 if (unlikely(!sb->s_root))
Al Viro5b5f9562012-01-08 15:38:27 -05001083 goto Enomem;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001084
1085 /* EP0 file */
1086 if (unlikely(!ffs_sb_create_file(sb, "ep0", ffs,
1087 &ffs_ep0_operations, NULL)))
Al Viro5b5f9562012-01-08 15:38:27 -05001088 goto Enomem;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001089
1090 return 0;
1091
Al Viro5b5f9562012-01-08 15:38:27 -05001092Enomem:
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001093 return -ENOMEM;
1094}
1095
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001096static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts)
1097{
1098 ENTER();
1099
1100 if (!opts || !*opts)
1101 return 0;
1102
1103 for (;;) {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001104 unsigned long value;
Michal Nazarewiczafd2e182013-01-09 10:17:47 +01001105 char *eq, *comma;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001106
1107 /* Option limit */
1108 comma = strchr(opts, ',');
1109 if (comma)
1110 *comma = 0;
1111
1112 /* Value limit */
1113 eq = strchr(opts, '=');
1114 if (unlikely(!eq)) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001115 pr_err("'=' missing in %s\n", opts);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001116 return -EINVAL;
1117 }
1118 *eq = 0;
1119
1120 /* Parse value */
Michal Nazarewiczafd2e182013-01-09 10:17:47 +01001121 if (kstrtoul(eq + 1, 0, &value)) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001122 pr_err("%s: invalid value: %s\n", opts, eq + 1);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001123 return -EINVAL;
1124 }
1125
1126 /* Interpret option */
1127 switch (eq - opts) {
1128 case 5:
1129 if (!memcmp(opts, "rmode", 5))
1130 data->root_mode = (value & 0555) | S_IFDIR;
1131 else if (!memcmp(opts, "fmode", 5))
1132 data->perms.mode = (value & 0666) | S_IFREG;
1133 else
1134 goto invalid;
1135 break;
1136
1137 case 4:
1138 if (!memcmp(opts, "mode", 4)) {
1139 data->root_mode = (value & 0555) | S_IFDIR;
1140 data->perms.mode = (value & 0666) | S_IFREG;
1141 } else {
1142 goto invalid;
1143 }
1144 break;
1145
1146 case 3:
Eric W. Biedermanb9b73f72012-06-14 01:19:23 -07001147 if (!memcmp(opts, "uid", 3)) {
1148 data->perms.uid = make_kuid(current_user_ns(), value);
1149 if (!uid_valid(data->perms.uid)) {
1150 pr_err("%s: unmapped value: %lu\n", opts, value);
1151 return -EINVAL;
1152 }
Benoit Gobyb8100752013-01-08 19:57:09 -08001153 } 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 }
Benoit Gobyb8100752013-01-08 19:57:09 -08001159 } else {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001160 goto invalid;
Benoit Gobyb8100752013-01-08 19:57:09 -08001161 }
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001162 break;
1163
1164 default:
1165invalid:
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001166 pr_err("%s: invalid option\n", opts);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001167 return -EINVAL;
1168 }
1169
1170 /* Next iteration */
1171 if (!comma)
1172 break;
1173 opts = comma + 1;
1174 }
1175
1176 return 0;
1177}
1178
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001179/* "mount -t functionfs dev_name /dev/function" ends up here */
1180
Al Virofc14f2f2010-07-25 01:48:30 +04001181static struct dentry *
1182ffs_fs_mount(struct file_system_type *t, int flags,
1183 const char *dev_name, void *opts)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001184{
1185 struct ffs_sb_fill_data data = {
1186 .perms = {
1187 .mode = S_IFREG | 0600,
Eric W. Biedermanb9b73f72012-06-14 01:19:23 -07001188 .uid = GLOBAL_ROOT_UID,
1189 .gid = GLOBAL_ROOT_GID,
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001190 },
1191 .root_mode = S_IFDIR | 0500,
1192 };
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001193 struct dentry *rv;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001194 int ret;
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001195 void *ffs_dev;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001196
1197 ENTER();
1198
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001199 ret = ffs_fs_parse_opts(&data, opts);
1200 if (unlikely(ret < 0))
Al Virofc14f2f2010-07-25 01:48:30 +04001201 return ERR_PTR(ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001202
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001203 ffs_dev = functionfs_acquire_dev_callback(dev_name);
1204 if (IS_ERR(ffs_dev))
1205 return ffs_dev;
1206
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001207 data.dev_name = dev_name;
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001208 data.private_data = ffs_dev;
1209 rv = mount_nodev(t, flags, &data, ffs_sb_fill);
1210
1211 /* data.ffs_data is set by ffs_sb_fill */
1212 if (IS_ERR(rv))
1213 functionfs_release_dev_callback(data.ffs_data);
1214
1215 return rv;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001216}
1217
1218static void
1219ffs_fs_kill_sb(struct super_block *sb)
1220{
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001221 ENTER();
1222
1223 kill_litter_super(sb);
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001224 if (sb->s_fs_info) {
1225 functionfs_release_dev_callback(sb->s_fs_info);
Al Viro5b5f9562012-01-08 15:38:27 -05001226 ffs_data_put(sb->s_fs_info);
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001227 }
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001228}
1229
1230static struct file_system_type ffs_fs_type = {
1231 .owner = THIS_MODULE,
1232 .name = "functionfs",
Al Virofc14f2f2010-07-25 01:48:30 +04001233 .mount = ffs_fs_mount,
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001234 .kill_sb = ffs_fs_kill_sb,
1235};
Eric W. Biederman7f78e032013-03-02 19:39:14 -08001236MODULE_ALIAS_FS("functionfs");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001237
1238
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001239/* Driver's main init/cleanup functions *************************************/
1240
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001241static int functionfs_init(void)
1242{
1243 int ret;
1244
1245 ENTER();
1246
1247 ret = register_filesystem(&ffs_fs_type);
1248 if (likely(!ret))
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001249 pr_info("file system registered\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001250 else
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001251 pr_err("failed registering file system (%d)\n", ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001252
1253 return ret;
1254}
1255
1256static void functionfs_cleanup(void)
1257{
1258 ENTER();
1259
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001260 pr_info("unloading\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001261 unregister_filesystem(&ffs_fs_type);
1262}
1263
1264
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001265/* ffs_data and ffs_function construction and destruction code **************/
1266
1267static void ffs_data_clear(struct ffs_data *ffs);
1268static void ffs_data_reset(struct ffs_data *ffs);
1269
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001270static void ffs_data_get(struct ffs_data *ffs)
1271{
1272 ENTER();
1273
1274 atomic_inc(&ffs->ref);
1275}
1276
1277static void ffs_data_opened(struct ffs_data *ffs)
1278{
1279 ENTER();
1280
1281 atomic_inc(&ffs->ref);
1282 atomic_inc(&ffs->opened);
1283}
1284
1285static void ffs_data_put(struct ffs_data *ffs)
1286{
1287 ENTER();
1288
1289 if (unlikely(atomic_dec_and_test(&ffs->ref))) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001290 pr_info("%s(): freeing\n", __func__);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001291 ffs_data_clear(ffs);
Andi Kleen647d5582012-03-16 12:01:02 -07001292 BUG_ON(waitqueue_active(&ffs->ev.waitq) ||
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001293 waitqueue_active(&ffs->ep0req_completion.wait));
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +02001294 kfree(ffs->dev_name);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001295 kfree(ffs);
1296 }
1297}
1298
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001299static void ffs_data_closed(struct ffs_data *ffs)
1300{
1301 ENTER();
1302
1303 if (atomic_dec_and_test(&ffs->opened)) {
1304 ffs->state = FFS_CLOSING;
1305 ffs_data_reset(ffs);
1306 }
1307
1308 ffs_data_put(ffs);
1309}
1310
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001311static struct ffs_data *ffs_data_new(void)
1312{
1313 struct ffs_data *ffs = kzalloc(sizeof *ffs, GFP_KERNEL);
1314 if (unlikely(!ffs))
1315 return 0;
1316
1317 ENTER();
1318
1319 atomic_set(&ffs->ref, 1);
1320 atomic_set(&ffs->opened, 0);
1321 ffs->state = FFS_READ_DESCRIPTORS;
1322 mutex_init(&ffs->mutex);
1323 spin_lock_init(&ffs->eps_lock);
1324 init_waitqueue_head(&ffs->ev.waitq);
1325 init_completion(&ffs->ep0req_completion);
1326
1327 /* XXX REVISIT need to update it in some places, or do we? */
1328 ffs->ev.can_stall = 1;
1329
1330 return ffs;
1331}
1332
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001333static void ffs_data_clear(struct ffs_data *ffs)
1334{
1335 ENTER();
1336
1337 if (test_and_clear_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags))
1338 functionfs_closed_callback(ffs);
1339
1340 BUG_ON(ffs->gadget);
1341
1342 if (ffs->epfiles)
1343 ffs_epfiles_destroy(ffs->epfiles, ffs->eps_count);
1344
1345 kfree(ffs->raw_descs);
1346 kfree(ffs->raw_strings);
1347 kfree(ffs->stringtabs);
1348}
1349
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001350static void ffs_data_reset(struct ffs_data *ffs)
1351{
1352 ENTER();
1353
1354 ffs_data_clear(ffs);
1355
1356 ffs->epfiles = NULL;
1357 ffs->raw_descs = NULL;
1358 ffs->raw_strings = NULL;
1359 ffs->stringtabs = NULL;
1360
1361 ffs->raw_descs_length = 0;
1362 ffs->raw_fs_descs_length = 0;
1363 ffs->fs_descs_count = 0;
1364 ffs->hs_descs_count = 0;
1365
1366 ffs->strings_count = 0;
1367 ffs->interfaces_count = 0;
1368 ffs->eps_count = 0;
1369
1370 ffs->ev.count = 0;
1371
1372 ffs->state = FFS_READ_DESCRIPTORS;
1373 ffs->setup_state = FFS_NO_SETUP;
1374 ffs->flags = 0;
1375}
1376
1377
1378static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev)
1379{
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001380 struct usb_gadget_strings **lang;
1381 int first_id;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001382
1383 ENTER();
1384
1385 if (WARN_ON(ffs->state != FFS_ACTIVE
1386 || test_and_set_bit(FFS_FL_BOUND, &ffs->flags)))
1387 return -EBADFD;
1388
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001389 first_id = usb_string_ids_n(cdev, ffs->strings_count);
1390 if (unlikely(first_id < 0))
1391 return first_id;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001392
1393 ffs->ep0req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
1394 if (unlikely(!ffs->ep0req))
1395 return -ENOMEM;
1396 ffs->ep0req->complete = ffs_ep0_complete;
1397 ffs->ep0req->context = ffs;
1398
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001399 lang = ffs->stringtabs;
1400 for (lang = ffs->stringtabs; *lang; ++lang) {
1401 struct usb_string *str = (*lang)->strings;
1402 int id = first_id;
1403 for (; str->s; ++id, ++str)
1404 str->id = id;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001405 }
1406
1407 ffs->gadget = cdev->gadget;
Michal Nazarewiczfd7c9a02010-06-16 12:08:00 +02001408 ffs_data_get(ffs);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001409 return 0;
1410}
1411
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001412static void functionfs_unbind(struct ffs_data *ffs)
1413{
1414 ENTER();
1415
1416 if (!WARN_ON(!ffs->gadget)) {
1417 usb_ep_free_request(ffs->gadget->ep0, ffs->ep0req);
1418 ffs->ep0req = NULL;
1419 ffs->gadget = NULL;
Andrzej Pietrasiewicze2190a92012-03-12 12:55:41 +01001420 clear_bit(FFS_FL_BOUND, &ffs->flags);
Dan Carpenterdf498992013-08-23 11:16:15 +03001421 ffs_data_put(ffs);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001422 }
1423}
1424
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001425static int ffs_epfiles_create(struct ffs_data *ffs)
1426{
1427 struct ffs_epfile *epfile, *epfiles;
1428 unsigned i, count;
1429
1430 ENTER();
1431
1432 count = ffs->eps_count;
Thomas Meyer9823a522011-11-29 22:08:00 +01001433 epfiles = kcalloc(count, sizeof(*epfiles), GFP_KERNEL);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001434 if (!epfiles)
1435 return -ENOMEM;
1436
1437 epfile = epfiles;
1438 for (i = 1; i <= count; ++i, ++epfile) {
1439 epfile->ffs = ffs;
1440 mutex_init(&epfile->mutex);
1441 init_waitqueue_head(&epfile->wait);
1442 sprintf(epfiles->name, "ep%u", i);
1443 if (!unlikely(ffs_sb_create_file(ffs->sb, epfiles->name, epfile,
1444 &ffs_epfile_operations,
1445 &epfile->dentry))) {
1446 ffs_epfiles_destroy(epfiles, i - 1);
1447 return -ENOMEM;
1448 }
1449 }
1450
1451 ffs->epfiles = epfiles;
1452 return 0;
1453}
1454
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001455static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count)
1456{
1457 struct ffs_epfile *epfile = epfiles;
1458
1459 ENTER();
1460
1461 for (; count; --count, ++epfile) {
1462 BUG_ON(mutex_is_locked(&epfile->mutex) ||
1463 waitqueue_active(&epfile->wait));
1464 if (epfile->dentry) {
1465 d_delete(epfile->dentry);
1466 dput(epfile->dentry);
1467 epfile->dentry = NULL;
1468 }
1469 }
1470
1471 kfree(epfiles);
1472}
1473
Michal Nazarewicz7898aee2010-06-16 12:07:58 +02001474static int functionfs_bind_config(struct usb_composite_dev *cdev,
1475 struct usb_configuration *c,
1476 struct ffs_data *ffs)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001477{
1478 struct ffs_function *func;
1479 int ret;
1480
1481 ENTER();
1482
1483 func = kzalloc(sizeof *func, GFP_KERNEL);
1484 if (unlikely(!func))
1485 return -ENOMEM;
1486
1487 func->function.name = "Function FS Gadget";
1488 func->function.strings = ffs->stringtabs;
1489
1490 func->function.bind = ffs_func_bind;
1491 func->function.unbind = ffs_func_unbind;
1492 func->function.set_alt = ffs_func_set_alt;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001493 func->function.disable = ffs_func_disable;
1494 func->function.setup = ffs_func_setup;
1495 func->function.suspend = ffs_func_suspend;
1496 func->function.resume = ffs_func_resume;
1497
1498 func->conf = c;
1499 func->gadget = cdev->gadget;
1500 func->ffs = ffs;
1501 ffs_data_get(ffs);
1502
1503 ret = usb_add_function(c, &func->function);
1504 if (unlikely(ret))
1505 ffs_func_free(func);
1506
1507 return ret;
1508}
1509
1510static void ffs_func_free(struct ffs_function *func)
1511{
Peter Korsgaard4f065392012-05-03 12:58:49 +02001512 struct ffs_ep *ep = func->eps;
1513 unsigned count = func->ffs->eps_count;
1514 unsigned long flags;
1515
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001516 ENTER();
1517
Peter Korsgaard4f065392012-05-03 12:58:49 +02001518 /* cleanup after autoconfig */
1519 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1520 do {
1521 if (ep->ep && ep->req)
1522 usb_ep_free_request(ep->ep, ep->req);
1523 ep->req = NULL;
1524 ++ep;
1525 } while (--count);
1526 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1527
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001528 ffs_data_put(func->ffs);
1529
1530 kfree(func->eps);
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001531 /*
1532 * eps and interfaces_nums are allocated in the same chunk so
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001533 * only one free is required. Descriptors are also allocated
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001534 * in the same chunk.
1535 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001536
1537 kfree(func);
1538}
1539
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001540static void ffs_func_eps_disable(struct ffs_function *func)
1541{
1542 struct ffs_ep *ep = func->eps;
1543 struct ffs_epfile *epfile = func->ffs->epfiles;
1544 unsigned count = func->ffs->eps_count;
1545 unsigned long flags;
1546
1547 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1548 do {
1549 /* pending requests get nuked */
1550 if (likely(ep->ep))
1551 usb_ep_disable(ep->ep);
1552 epfile->ep = NULL;
1553
1554 ++ep;
1555 ++epfile;
1556 } while (--count);
1557 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1558}
1559
1560static int ffs_func_eps_enable(struct ffs_function *func)
1561{
1562 struct ffs_data *ffs = func->ffs;
1563 struct ffs_ep *ep = func->eps;
1564 struct ffs_epfile *epfile = ffs->epfiles;
1565 unsigned count = ffs->eps_count;
1566 unsigned long flags;
1567 int ret = 0;
1568
1569 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1570 do {
1571 struct usb_endpoint_descriptor *ds;
1572 ds = ep->descs[ep->descs[1] ? 1 : 0];
1573
1574 ep->ep->driver_data = ep;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +03001575 ep->ep->desc = ds;
1576 ret = usb_ep_enable(ep->ep);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001577 if (likely(!ret)) {
1578 epfile->ep = ep;
1579 epfile->in = usb_endpoint_dir_in(ds);
1580 epfile->isoc = usb_endpoint_xfer_isoc(ds);
1581 } else {
1582 break;
1583 }
1584
1585 wake_up(&epfile->wait);
1586
1587 ++ep;
1588 ++epfile;
1589 } while (--count);
1590 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1591
1592 return ret;
1593}
1594
1595
1596/* Parsing and building descriptors and strings *****************************/
1597
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001598/*
1599 * This validates if data pointed by data is a valid USB descriptor as
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001600 * well as record how many interfaces, endpoints and strings are
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001601 * required by given configuration. Returns address after the
1602 * descriptor or NULL if data is invalid.
1603 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001604
1605enum ffs_entity_type {
1606 FFS_DESCRIPTOR, FFS_INTERFACE, FFS_STRING, FFS_ENDPOINT
1607};
1608
1609typedef int (*ffs_entity_callback)(enum ffs_entity_type entity,
1610 u8 *valuep,
1611 struct usb_descriptor_header *desc,
1612 void *priv);
1613
1614static int __must_check ffs_do_desc(char *data, unsigned len,
1615 ffs_entity_callback entity, void *priv)
1616{
1617 struct usb_descriptor_header *_ds = (void *)data;
1618 u8 length;
1619 int ret;
1620
1621 ENTER();
1622
1623 /* At least two bytes are required: length and type */
1624 if (len < 2) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001625 pr_vdebug("descriptor too short\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001626 return -EINVAL;
1627 }
1628
1629 /* If we have at least as many bytes as the descriptor takes? */
1630 length = _ds->bLength;
1631 if (len < length) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001632 pr_vdebug("descriptor longer then available data\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001633 return -EINVAL;
1634 }
1635
1636#define __entity_check_INTERFACE(val) 1
1637#define __entity_check_STRING(val) (val)
1638#define __entity_check_ENDPOINT(val) ((val) & USB_ENDPOINT_NUMBER_MASK)
1639#define __entity(type, val) do { \
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001640 pr_vdebug("entity " #type "(%02x)\n", (val)); \
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001641 if (unlikely(!__entity_check_ ##type(val))) { \
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001642 pr_vdebug("invalid entity's value\n"); \
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001643 return -EINVAL; \
1644 } \
1645 ret = entity(FFS_ ##type, &val, _ds, priv); \
1646 if (unlikely(ret < 0)) { \
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001647 pr_debug("entity " #type "(%02x); ret = %d\n", \
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +01001648 (val), ret); \
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001649 return ret; \
1650 } \
1651 } while (0)
1652
1653 /* Parse descriptor depending on type. */
1654 switch (_ds->bDescriptorType) {
1655 case USB_DT_DEVICE:
1656 case USB_DT_CONFIG:
1657 case USB_DT_STRING:
1658 case USB_DT_DEVICE_QUALIFIER:
1659 /* function can't have any of those */
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001660 pr_vdebug("descriptor reserved for gadget: %d\n",
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001661 _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001662 return -EINVAL;
1663
1664 case USB_DT_INTERFACE: {
1665 struct usb_interface_descriptor *ds = (void *)_ds;
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001666 pr_vdebug("interface descriptor\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001667 if (length != sizeof *ds)
1668 goto inv_length;
1669
1670 __entity(INTERFACE, ds->bInterfaceNumber);
1671 if (ds->iInterface)
1672 __entity(STRING, ds->iInterface);
1673 }
1674 break;
1675
1676 case USB_DT_ENDPOINT: {
1677 struct usb_endpoint_descriptor *ds = (void *)_ds;
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001678 pr_vdebug("endpoint descriptor\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001679 if (length != USB_DT_ENDPOINT_SIZE &&
1680 length != USB_DT_ENDPOINT_AUDIO_SIZE)
1681 goto inv_length;
1682 __entity(ENDPOINT, ds->bEndpointAddress);
1683 }
1684 break;
1685
Koen Beel560f1182012-05-30 20:43:37 +02001686 case HID_DT_HID:
1687 pr_vdebug("hid descriptor\n");
1688 if (length != sizeof(struct hid_descriptor))
1689 goto inv_length;
1690 break;
1691
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001692 case USB_DT_OTG:
1693 if (length != sizeof(struct usb_otg_descriptor))
1694 goto inv_length;
1695 break;
1696
1697 case USB_DT_INTERFACE_ASSOCIATION: {
1698 struct usb_interface_assoc_descriptor *ds = (void *)_ds;
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001699 pr_vdebug("interface association descriptor\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001700 if (length != sizeof *ds)
1701 goto inv_length;
1702 if (ds->iFunction)
1703 __entity(STRING, ds->iFunction);
1704 }
1705 break;
1706
1707 case USB_DT_OTHER_SPEED_CONFIG:
1708 case USB_DT_INTERFACE_POWER:
1709 case USB_DT_DEBUG:
1710 case USB_DT_SECURITY:
1711 case USB_DT_CS_RADIO_CONTROL:
1712 /* TODO */
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001713 pr_vdebug("unimplemented descriptor: %d\n", _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001714 return -EINVAL;
1715
1716 default:
1717 /* We should never be here */
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001718 pr_vdebug("unknown descriptor: %d\n", _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001719 return -EINVAL;
1720
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001721inv_length:
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001722 pr_vdebug("invalid length: %d (descriptor %d)\n",
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +01001723 _ds->bLength, _ds->bDescriptorType);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001724 return -EINVAL;
1725 }
1726
1727#undef __entity
1728#undef __entity_check_DESCRIPTOR
1729#undef __entity_check_INTERFACE
1730#undef __entity_check_STRING
1731#undef __entity_check_ENDPOINT
1732
1733 return length;
1734}
1735
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001736static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len,
1737 ffs_entity_callback entity, void *priv)
1738{
1739 const unsigned _len = len;
1740 unsigned long num = 0;
1741
1742 ENTER();
1743
1744 for (;;) {
1745 int ret;
1746
1747 if (num == count)
1748 data = NULL;
1749
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001750 /* Record "descriptor" entity */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001751 ret = entity(FFS_DESCRIPTOR, (u8 *)num, (void *)data, priv);
1752 if (unlikely(ret < 0)) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001753 pr_debug("entity DESCRIPTOR(%02lx); ret = %d\n",
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +01001754 num, ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001755 return ret;
1756 }
1757
1758 if (!data)
1759 return _len - len;
1760
1761 ret = ffs_do_desc(data, len, entity, priv);
1762 if (unlikely(ret < 0)) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01001763 pr_debug("%s returns %d\n", __func__, ret);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001764 return ret;
1765 }
1766
1767 len -= ret;
1768 data += ret;
1769 ++num;
1770 }
1771}
1772
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001773static int __ffs_data_do_entity(enum ffs_entity_type type,
1774 u8 *valuep, struct usb_descriptor_header *desc,
1775 void *priv)
1776{
1777 struct ffs_data *ffs = priv;
1778
1779 ENTER();
1780
1781 switch (type) {
1782 case FFS_DESCRIPTOR:
1783 break;
1784
1785 case FFS_INTERFACE:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001786 /*
1787 * Interfaces are indexed from zero so if we
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001788 * encountered interface "n" then there are at least
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001789 * "n+1" interfaces.
1790 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001791 if (*valuep >= ffs->interfaces_count)
1792 ffs->interfaces_count = *valuep + 1;
1793 break;
1794
1795 case FFS_STRING:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001796 /*
1797 * Strings are indexed from 1 (0 is magic ;) reserved
1798 * for languages list or some such)
1799 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001800 if (*valuep > ffs->strings_count)
1801 ffs->strings_count = *valuep;
1802 break;
1803
1804 case FFS_ENDPOINT:
1805 /* Endpoints are indexed from 1 as well. */
1806 if ((*valuep & USB_ENDPOINT_NUMBER_MASK) > ffs->eps_count)
1807 ffs->eps_count = (*valuep & USB_ENDPOINT_NUMBER_MASK);
1808 break;
1809 }
1810
1811 return 0;
1812}
1813
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001814static int __ffs_data_got_descs(struct ffs_data *ffs,
1815 char *const _data, size_t len)
1816{
1817 unsigned fs_count, hs_count;
1818 int fs_len, ret = -EINVAL;
1819 char *data = _data;
1820
1821 ENTER();
1822
1823 if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_DESCRIPTORS_MAGIC ||
1824 get_unaligned_le32(data + 4) != len))
1825 goto error;
1826 fs_count = get_unaligned_le32(data + 8);
1827 hs_count = get_unaligned_le32(data + 12);
1828
1829 if (!fs_count && !hs_count)
1830 goto einval;
1831
1832 data += 16;
1833 len -= 16;
1834
1835 if (likely(fs_count)) {
1836 fs_len = ffs_do_descs(fs_count, data, len,
1837 __ffs_data_do_entity, ffs);
1838 if (unlikely(fs_len < 0)) {
1839 ret = fs_len;
1840 goto error;
1841 }
1842
1843 data += fs_len;
1844 len -= fs_len;
1845 } else {
1846 fs_len = 0;
1847 }
1848
1849 if (likely(hs_count)) {
1850 ret = ffs_do_descs(hs_count, data, len,
1851 __ffs_data_do_entity, ffs);
1852 if (unlikely(ret < 0))
1853 goto error;
1854 } else {
1855 ret = 0;
1856 }
1857
1858 if (unlikely(len != ret))
1859 goto einval;
1860
1861 ffs->raw_fs_descs_length = fs_len;
1862 ffs->raw_descs_length = fs_len + ret;
1863 ffs->raw_descs = _data;
1864 ffs->fs_descs_count = fs_count;
1865 ffs->hs_descs_count = hs_count;
1866
1867 return 0;
1868
1869einval:
1870 ret = -EINVAL;
1871error:
1872 kfree(_data);
1873 return ret;
1874}
1875
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001876static int __ffs_data_got_strings(struct ffs_data *ffs,
1877 char *const _data, size_t len)
1878{
1879 u32 str_count, needed_count, lang_count;
1880 struct usb_gadget_strings **stringtabs, *t;
1881 struct usb_string *strings, *s;
1882 const char *data = _data;
1883
1884 ENTER();
1885
1886 if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_STRINGS_MAGIC ||
1887 get_unaligned_le32(data + 4) != len))
1888 goto error;
1889 str_count = get_unaligned_le32(data + 8);
1890 lang_count = get_unaligned_le32(data + 12);
1891
1892 /* if one is zero the other must be zero */
1893 if (unlikely(!str_count != !lang_count))
1894 goto error;
1895
1896 /* Do we have at least as many strings as descriptors need? */
1897 needed_count = ffs->strings_count;
1898 if (unlikely(str_count < needed_count))
1899 goto error;
1900
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001901 /*
1902 * If we don't need any strings just return and free all
1903 * memory.
1904 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001905 if (!needed_count) {
1906 kfree(_data);
1907 return 0;
1908 }
1909
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001910 /* Allocate everything in one chunk so there's less maintenance. */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001911 {
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001912 struct {
1913 struct usb_gadget_strings *stringtabs[lang_count + 1];
1914 struct usb_gadget_strings stringtab[lang_count];
1915 struct usb_string strings[lang_count*(needed_count+1)];
1916 } *d;
1917 unsigned i = 0;
1918
1919 d = kmalloc(sizeof *d, GFP_KERNEL);
1920 if (unlikely(!d)) {
1921 kfree(_data);
1922 return -ENOMEM;
1923 }
1924
1925 stringtabs = d->stringtabs;
1926 t = d->stringtab;
1927 i = lang_count;
1928 do {
1929 *stringtabs++ = t++;
1930 } while (--i);
1931 *stringtabs = NULL;
1932
1933 stringtabs = d->stringtabs;
1934 t = d->stringtab;
1935 s = d->strings;
1936 strings = s;
1937 }
1938
1939 /* For each language */
1940 data += 16;
1941 len -= 16;
1942
1943 do { /* lang_count > 0 so we can use do-while */
1944 unsigned needed = needed_count;
1945
1946 if (unlikely(len < 3))
1947 goto error_free;
1948 t->language = get_unaligned_le16(data);
1949 t->strings = s;
1950 ++t;
1951
1952 data += 2;
1953 len -= 2;
1954
1955 /* For each string */
1956 do { /* str_count > 0 so we can use do-while */
1957 size_t length = strnlen(data, len);
1958
1959 if (unlikely(length == len))
1960 goto error_free;
1961
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001962 /*
1963 * User may provide more strings then we need,
1964 * if that's the case we simply ignore the
1965 * rest
1966 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001967 if (likely(needed)) {
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001968 /*
1969 * s->id will be set while adding
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001970 * function to configuration so for
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001971 * now just leave garbage here.
1972 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02001973 s->s = data;
1974 --needed;
1975 ++s;
1976 }
1977
1978 data += length + 1;
1979 len -= length + 1;
1980 } while (--str_count);
1981
1982 s->id = 0; /* terminator */
1983 s->s = NULL;
1984 ++s;
1985
1986 } while (--lang_count);
1987
1988 /* Some garbage left? */
1989 if (unlikely(len))
1990 goto error_free;
1991
1992 /* Done! */
1993 ffs->stringtabs = stringtabs;
1994 ffs->raw_strings = _data;
1995
1996 return 0;
1997
1998error_free:
1999 kfree(stringtabs);
2000error:
2001 kfree(_data);
2002 return -EINVAL;
2003}
2004
2005
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002006/* Events handling and management *******************************************/
2007
2008static void __ffs_event_add(struct ffs_data *ffs,
2009 enum usb_functionfs_event_type type)
2010{
2011 enum usb_functionfs_event_type rem_type1, rem_type2 = type;
2012 int neg = 0;
2013
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002014 /*
2015 * Abort any unhandled setup
2016 *
2017 * We do not need to worry about some cmpxchg() changing value
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002018 * of ffs->setup_state without holding the lock because when
2019 * state is FFS_SETUP_PENDING cmpxchg() in several places in
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002020 * the source does nothing.
2021 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002022 if (ffs->setup_state == FFS_SETUP_PENDING)
2023 ffs->setup_state = FFS_SETUP_CANCELED;
2024
2025 switch (type) {
2026 case FUNCTIONFS_RESUME:
2027 rem_type2 = FUNCTIONFS_SUSPEND;
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002028 /* FALL THROUGH */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002029 case FUNCTIONFS_SUSPEND:
2030 case FUNCTIONFS_SETUP:
2031 rem_type1 = type;
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002032 /* Discard all similar events */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002033 break;
2034
2035 case FUNCTIONFS_BIND:
2036 case FUNCTIONFS_UNBIND:
2037 case FUNCTIONFS_DISABLE:
2038 case FUNCTIONFS_ENABLE:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002039 /* Discard everything other then power management. */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002040 rem_type1 = FUNCTIONFS_SUSPEND;
2041 rem_type2 = FUNCTIONFS_RESUME;
2042 neg = 1;
2043 break;
2044
2045 default:
2046 BUG();
2047 }
2048
2049 {
2050 u8 *ev = ffs->ev.types, *out = ev;
2051 unsigned n = ffs->ev.count;
2052 for (; n; --n, ++ev)
2053 if ((*ev == rem_type1 || *ev == rem_type2) == neg)
2054 *out++ = *ev;
2055 else
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002056 pr_vdebug("purging event %d\n", *ev);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002057 ffs->ev.count = out - ffs->ev.types;
2058 }
2059
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002060 pr_vdebug("adding event %d\n", type);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002061 ffs->ev.types[ffs->ev.count++] = type;
2062 wake_up_locked(&ffs->ev.waitq);
2063}
2064
2065static void ffs_event_add(struct ffs_data *ffs,
2066 enum usb_functionfs_event_type type)
2067{
2068 unsigned long flags;
2069 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2070 __ffs_event_add(ffs, type);
2071 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2072}
2073
2074
2075/* Bind/unbind USB function hooks *******************************************/
2076
2077static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
2078 struct usb_descriptor_header *desc,
2079 void *priv)
2080{
2081 struct usb_endpoint_descriptor *ds = (void *)desc;
2082 struct ffs_function *func = priv;
2083 struct ffs_ep *ffs_ep;
2084
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002085 /*
2086 * If hs_descriptors is not NULL then we are reading hs
2087 * descriptors now
2088 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002089 const int isHS = func->function.hs_descriptors != NULL;
2090 unsigned idx;
2091
2092 if (type != FFS_DESCRIPTOR)
2093 return 0;
2094
2095 if (isHS)
2096 func->function.hs_descriptors[(long)valuep] = desc;
2097 else
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +02002098 func->function.fs_descriptors[(long)valuep] = desc;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002099
2100 if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT)
2101 return 0;
2102
2103 idx = (ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK) - 1;
2104 ffs_ep = func->eps + idx;
2105
2106 if (unlikely(ffs_ep->descs[isHS])) {
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002107 pr_vdebug("two %sspeed descriptors for EP %d\n",
Michal Nazarewiczd8df0b62010-11-12 14:29:29 +01002108 isHS ? "high" : "full",
2109 ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002110 return -EINVAL;
2111 }
2112 ffs_ep->descs[isHS] = ds;
2113
2114 ffs_dump_mem(": Original ep desc", ds, ds->bLength);
2115 if (ffs_ep->ep) {
2116 ds->bEndpointAddress = ffs_ep->descs[0]->bEndpointAddress;
2117 if (!ds->wMaxPacketSize)
2118 ds->wMaxPacketSize = ffs_ep->descs[0]->wMaxPacketSize;
2119 } else {
2120 struct usb_request *req;
2121 struct usb_ep *ep;
2122
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002123 pr_vdebug("autoconfig\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002124 ep = usb_ep_autoconfig(func->gadget, ds);
2125 if (unlikely(!ep))
2126 return -ENOTSUPP;
Joe Perchescc7e6052010-11-14 19:04:49 -08002127 ep->driver_data = func->eps + idx;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002128
2129 req = usb_ep_alloc_request(ep, GFP_KERNEL);
2130 if (unlikely(!req))
2131 return -ENOMEM;
2132
2133 ffs_ep->ep = ep;
2134 ffs_ep->req = req;
2135 func->eps_revmap[ds->bEndpointAddress &
2136 USB_ENDPOINT_NUMBER_MASK] = idx + 1;
2137 }
2138 ffs_dump_mem(": Rewritten ep desc", ds, ds->bLength);
2139
2140 return 0;
2141}
2142
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002143static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep,
2144 struct usb_descriptor_header *desc,
2145 void *priv)
2146{
2147 struct ffs_function *func = priv;
2148 unsigned idx;
2149 u8 newValue;
2150
2151 switch (type) {
2152 default:
2153 case FFS_DESCRIPTOR:
2154 /* Handled in previous pass by __ffs_func_bind_do_descs() */
2155 return 0;
2156
2157 case FFS_INTERFACE:
2158 idx = *valuep;
2159 if (func->interfaces_nums[idx] < 0) {
2160 int id = usb_interface_id(func->conf, &func->function);
2161 if (unlikely(id < 0))
2162 return id;
2163 func->interfaces_nums[idx] = id;
2164 }
2165 newValue = func->interfaces_nums[idx];
2166 break;
2167
2168 case FFS_STRING:
2169 /* String' IDs are allocated when fsf_data is bound to cdev */
2170 newValue = func->ffs->stringtabs[0]->strings[*valuep - 1].id;
2171 break;
2172
2173 case FFS_ENDPOINT:
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002174 /*
2175 * USB_DT_ENDPOINT are handled in
2176 * __ffs_func_bind_do_descs().
2177 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002178 if (desc->bDescriptorType == USB_DT_ENDPOINT)
2179 return 0;
2180
2181 idx = (*valuep & USB_ENDPOINT_NUMBER_MASK) - 1;
2182 if (unlikely(!func->eps[idx].ep))
2183 return -EINVAL;
2184
2185 {
2186 struct usb_endpoint_descriptor **descs;
2187 descs = func->eps[idx].descs;
2188 newValue = descs[descs[0] ? 0 : 1]->bEndpointAddress;
2189 }
2190 break;
2191 }
2192
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002193 pr_vdebug("%02x -> %02x\n", *valuep, newValue);
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002194 *valuep = newValue;
2195 return 0;
2196}
2197
2198static int ffs_func_bind(struct usb_configuration *c,
2199 struct usb_function *f)
2200{
2201 struct ffs_function *func = ffs_func_from_usb(f);
2202 struct ffs_data *ffs = func->ffs;
2203
2204 const int full = !!func->ffs->fs_descs_count;
2205 const int high = gadget_is_dualspeed(func->gadget) &&
2206 func->ffs->hs_descs_count;
2207
2208 int ret;
2209
2210 /* Make it a single chunk, less management later on */
2211 struct {
2212 struct ffs_ep eps[ffs->eps_count];
2213 struct usb_descriptor_header
2214 *fs_descs[full ? ffs->fs_descs_count + 1 : 0];
2215 struct usb_descriptor_header
2216 *hs_descs[high ? ffs->hs_descs_count + 1 : 0];
2217 short inums[ffs->interfaces_count];
2218 char raw_descs[high ? ffs->raw_descs_length
2219 : ffs->raw_fs_descs_length];
2220 } *data;
2221
2222 ENTER();
2223
2224 /* Only high speed but not supported by gadget? */
2225 if (unlikely(!(full | high)))
2226 return -ENOTSUPP;
2227
2228 /* Allocate */
2229 data = kmalloc(sizeof *data, GFP_KERNEL);
2230 if (unlikely(!data))
2231 return -ENOMEM;
2232
2233 /* Zero */
2234 memset(data->eps, 0, sizeof data->eps);
2235 memcpy(data->raw_descs, ffs->raw_descs + 16, sizeof data->raw_descs);
2236 memset(data->inums, 0xff, sizeof data->inums);
2237 for (ret = ffs->eps_count; ret; --ret)
2238 data->eps[ret].num = -1;
2239
2240 /* Save pointers */
2241 func->eps = data->eps;
2242 func->interfaces_nums = data->inums;
2243
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002244 /*
2245 * Go through all the endpoint descriptors and allocate
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002246 * endpoints first, so that later we can rewrite the endpoint
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002247 * numbers without worrying that it may be described later on.
2248 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002249 if (likely(full)) {
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +02002250 func->function.fs_descriptors = data->fs_descs;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002251 ret = ffs_do_descs(ffs->fs_descs_count,
2252 data->raw_descs,
2253 sizeof data->raw_descs,
2254 __ffs_func_bind_do_descs, func);
2255 if (unlikely(ret < 0))
2256 goto error;
2257 } else {
2258 ret = 0;
2259 }
2260
2261 if (likely(high)) {
2262 func->function.hs_descriptors = data->hs_descs;
2263 ret = ffs_do_descs(ffs->hs_descs_count,
2264 data->raw_descs + ret,
2265 (sizeof data->raw_descs) - ret,
2266 __ffs_func_bind_do_descs, func);
Robert Baldyga88548942013-09-27 12:28:54 +02002267 if (unlikely(ret < 0))
2268 goto error;
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002269 }
2270
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002271 /*
2272 * Now handle interface numbers allocation and interface and
2273 * endpoint numbers rewriting. We can do that in one go
2274 * now.
2275 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002276 ret = ffs_do_descs(ffs->fs_descs_count +
2277 (high ? ffs->hs_descs_count : 0),
2278 data->raw_descs, sizeof data->raw_descs,
2279 __ffs_func_bind_do_nums, func);
2280 if (unlikely(ret < 0))
2281 goto error;
2282
2283 /* And we're done */
2284 ffs_event_add(ffs, FUNCTIONFS_BIND);
2285 return 0;
2286
2287error:
2288 /* XXX Do we need to release all claimed endpoints here? */
2289 return ret;
2290}
2291
2292
2293/* Other USB function hooks *************************************************/
2294
2295static void ffs_func_unbind(struct usb_configuration *c,
2296 struct usb_function *f)
2297{
2298 struct ffs_function *func = ffs_func_from_usb(f);
2299 struct ffs_data *ffs = func->ffs;
2300
2301 ENTER();
2302
2303 if (ffs->func == func) {
2304 ffs_func_eps_disable(func);
2305 ffs->func = NULL;
2306 }
2307
2308 ffs_event_add(ffs, FUNCTIONFS_UNBIND);
2309
2310 ffs_func_free(func);
2311}
2312
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002313static int ffs_func_set_alt(struct usb_function *f,
2314 unsigned interface, unsigned alt)
2315{
2316 struct ffs_function *func = ffs_func_from_usb(f);
2317 struct ffs_data *ffs = func->ffs;
2318 int ret = 0, intf;
2319
2320 if (alt != (unsigned)-1) {
2321 intf = ffs_func_revmap_intf(func, interface);
2322 if (unlikely(intf < 0))
2323 return intf;
2324 }
2325
2326 if (ffs->func)
2327 ffs_func_eps_disable(ffs->func);
2328
2329 if (ffs->state != FFS_ACTIVE)
2330 return -ENODEV;
2331
2332 if (alt == (unsigned)-1) {
2333 ffs->func = NULL;
2334 ffs_event_add(ffs, FUNCTIONFS_DISABLE);
2335 return 0;
2336 }
2337
2338 ffs->func = func;
2339 ret = ffs_func_eps_enable(func);
2340 if (likely(ret >= 0))
2341 ffs_event_add(ffs, FUNCTIONFS_ENABLE);
2342 return ret;
2343}
2344
2345static void ffs_func_disable(struct usb_function *f)
2346{
2347 ffs_func_set_alt(f, 0, (unsigned)-1);
2348}
2349
2350static int ffs_func_setup(struct usb_function *f,
2351 const struct usb_ctrlrequest *creq)
2352{
2353 struct ffs_function *func = ffs_func_from_usb(f);
2354 struct ffs_data *ffs = func->ffs;
2355 unsigned long flags;
2356 int ret;
2357
2358 ENTER();
2359
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002360 pr_vdebug("creq->bRequestType = %02x\n", creq->bRequestType);
2361 pr_vdebug("creq->bRequest = %02x\n", creq->bRequest);
2362 pr_vdebug("creq->wValue = %04x\n", le16_to_cpu(creq->wValue));
2363 pr_vdebug("creq->wIndex = %04x\n", le16_to_cpu(creq->wIndex));
2364 pr_vdebug("creq->wLength = %04x\n", le16_to_cpu(creq->wLength));
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002365
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002366 /*
2367 * Most requests directed to interface go through here
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002368 * (notable exceptions are set/get interface) so we need to
2369 * handle them. All other either handled by composite or
2370 * passed to usb_configuration->setup() (if one is set). No
2371 * matter, we will handle requests directed to endpoint here
2372 * as well (as it's straightforward) but what to do with any
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002373 * other request?
2374 */
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002375 if (ffs->state != FFS_ACTIVE)
2376 return -ENODEV;
2377
2378 switch (creq->bRequestType & USB_RECIP_MASK) {
2379 case USB_RECIP_INTERFACE:
2380 ret = ffs_func_revmap_intf(func, le16_to_cpu(creq->wIndex));
2381 if (unlikely(ret < 0))
2382 return ret;
2383 break;
2384
2385 case USB_RECIP_ENDPOINT:
2386 ret = ffs_func_revmap_ep(func, le16_to_cpu(creq->wIndex));
2387 if (unlikely(ret < 0))
2388 return ret;
2389 break;
2390
2391 default:
2392 return -EOPNOTSUPP;
2393 }
2394
2395 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2396 ffs->ev.setup = *creq;
2397 ffs->ev.setup.wIndex = cpu_to_le16(ret);
2398 __ffs_event_add(ffs, FUNCTIONFS_SETUP);
2399 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2400
2401 return 0;
2402}
2403
2404static void ffs_func_suspend(struct usb_function *f)
2405{
2406 ENTER();
2407 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_SUSPEND);
2408}
2409
2410static void ffs_func_resume(struct usb_function *f)
2411{
2412 ENTER();
2413 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_RESUME);
2414}
2415
2416
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01002417/* Endpoint and interface numbers reverse mapping ***************************/
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002418
2419static int ffs_func_revmap_ep(struct ffs_function *func, u8 num)
2420{
2421 num = func->eps_revmap[num & USB_ENDPOINT_NUMBER_MASK];
2422 return num ? num : -EDOM;
2423}
2424
2425static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf)
2426{
2427 short *nums = func->interfaces_nums;
2428 unsigned count = func->ffs->interfaces_count;
2429
2430 for (; count; --count, ++nums) {
2431 if (*nums >= 0 && *nums == intf)
2432 return nums - func->interfaces_nums;
2433 }
2434
2435 return -EDOM;
2436}
2437
2438
2439/* Misc helper functions ****************************************************/
2440
2441static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
2442{
2443 return nonblock
2444 ? likely(mutex_trylock(mutex)) ? 0 : -EAGAIN
2445 : mutex_lock_interruptible(mutex);
2446}
2447
Al Viro260ef312012-09-26 21:43:45 -04002448static char *ffs_prepare_buffer(const char __user *buf, size_t len)
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002449{
2450 char *data;
2451
2452 if (unlikely(!len))
2453 return NULL;
2454
2455 data = kmalloc(len, GFP_KERNEL);
2456 if (unlikely(!data))
2457 return ERR_PTR(-ENOMEM);
2458
2459 if (unlikely(__copy_from_user(data, buf, len))) {
2460 kfree(data);
2461 return ERR_PTR(-EFAULT);
2462 }
2463
Michal Nazarewiczaa02f172010-11-17 17:09:47 +01002464 pr_vdebug("Buffer from user space:\n");
Michal Nazarewiczddf8abd2010-05-05 12:53:14 +02002465 ffs_dump_mem("", data, len);
2466
2467 return data;
2468}