usb: gadget: rndis: merge u_rndis.ko with usb_f_rndis.ko
[deliverable/linux.git] / drivers / usb / gadget / f_fs.c
CommitLineData
ddf8abd2 1/*
5ab54cf7 2 * f_fs.c -- user mode file system API for USB composite function controllers
ddf8abd2
MN
3 *
4 * Copyright (C) 2010 Samsung Electronics
54b8360f 5 * Author: Michal Nazarewicz <mina86@mina86.com>
ddf8abd2 6 *
5ab54cf7 7 * Based on inode.c (GadgetFS) which was:
ddf8abd2
MN
8 * 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.
ddf8abd2
MN
15 */
16
17
18/* #define DEBUG */
19/* #define VERBOSE_DEBUG */
20
21#include <linux/blkdev.h>
b0608690 22#include <linux/pagemap.h>
f940fcd8 23#include <linux/export.h>
560f1187 24#include <linux/hid.h>
ddf8abd2 25#include <asm/unaligned.h>
ddf8abd2
MN
26
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
5ab54cf7 34/* Debugging ****************************************************************/
ddf8abd2
MN
35
36#ifdef VERBOSE_DEBUG
ea0e6276 37#ifndef pr_vdebug
d8df0b61 38# define pr_vdebug pr_debug
ea0e6276 39#endif /* pr_vdebug */
ddf8abd2 40# define ffs_dump_mem(prefix, ptr, len) \
aa02f172 41 print_hex_dump_bytes(pr_fmt(prefix ": "), DUMP_PREFIX_NONE, ptr, len)
ddf8abd2 42#else
ea0e6276 43#ifndef pr_vdebug
d8df0b61 44# define pr_vdebug(...) do { } while (0)
ea0e6276 45#endif /* pr_vdebug */
ddf8abd2 46# define ffs_dump_mem(prefix, ptr, len) do { } while (0)
d8df0b61
MN
47#endif /* VERBOSE_DEBUG */
48
aa02f172 49#define ENTER() pr_vdebug("%s()\n", __func__)
ddf8abd2
MN
50
51
52/* The data structure and setup file ****************************************/
53
54enum ffs_state {
5ab54cf7
MN
55 /*
56 * Waiting for descriptors and strings.
57 *
58 * In this state no open(2), read(2) or write(2) on epfiles
ddf8abd2 59 * may succeed (which should not be the problem as there
5ab54cf7
MN
60 * should be no such files opened in the first place).
61 */
ddf8abd2
MN
62 FFS_READ_DESCRIPTORS,
63 FFS_READ_STRINGS,
64
5ab54cf7
MN
65 /*
66 * We've got descriptors and strings. We are or have called
ddf8abd2 67 * functionfs_ready_callback(). functionfs_bind() may have
5ab54cf7
MN
68 * been called but we don't know.
69 *
70 * This is the only state in which operations on epfiles may
71 * succeed.
72 */
ddf8abd2
MN
73 FFS_ACTIVE,
74
5ab54cf7
MN
75 /*
76 * All endpoints have been closed. This state is also set if
ddf8abd2
MN
77 * we encounter an unrecoverable error. The only
78 * unrecoverable error is situation when after reading strings
5ab54cf7
MN
79 * 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
ddf8abd2
MN
83 * 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
5ab54cf7
MN
86 * fail).
87 */
ddf8abd2
MN
88 FFS_CLOSING
89};
90
91
92enum ffs_setup_state {
93 /* There is no setup request pending. */
94 FFS_NO_SETUP,
5ab54cf7
MN
95 /*
96 * User has read events and there was a setup request event
ddf8abd2 97 * there. The next read/write on ep0 will handle the
5ab54cf7
MN
98 * request.
99 */
ddf8abd2 100 FFS_SETUP_PENDING,
5ab54cf7
MN
101 /*
102 * There was event pending but before user space handled it
ddf8abd2
MN
103 * some other event was introduced which canceled existing
104 * setup. If this state is set read/write on ep0 return
5ab54cf7
MN
105 * -EIDRM. This state is only set when adding event.
106 */
ddf8abd2
MN
107 FFS_SETUP_CANCELED
108};
109
110
111
112struct ffs_epfile;
113struct ffs_function;
114
115struct ffs_data {
116 struct usb_gadget *gadget;
117
5ab54cf7
MN
118 /*
119 * Protect access read/write operations, only one read/write
ddf8abd2
MN
120 * at a time. As a consequence protects ep0req and company.
121 * While setup request is being processed (queued) this is
5ab54cf7
MN
122 * held.
123 */
ddf8abd2
MN
124 struct mutex mutex;
125
5ab54cf7
MN
126 /*
127 * Protect access to endpoint related structures (basically
ddf8abd2 128 * usb_ep_queue(), usb_ep_dequeue(), etc. calls) except for
5ab54cf7
MN
129 * endpoint zero.
130 */
ddf8abd2
MN
131 spinlock_t eps_lock;
132
5ab54cf7
MN
133 /*
134 * XXX REVISIT do we need our own request? Since we are not
135 * handling setup requests immediately user space may be so
ddf8abd2
MN
136 * 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
a4ce96ac 138 * a race. Is that the case? Or maybe we can use cdev->req
5ab54cf7
MN
139 * after all, maybe we just need some spinlock for that?
140 */
ddf8abd2
MN
141 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 /*
5ab54cf7 154 * Possible transitions:
ddf8abd2
MN
155 * + 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
5ab54cf7
MN
187 /*
188 * Device name, write once when file system is mounted.
189 * Intended for user to read if she wants.
190 */
ddf8abd2 191 const char *dev_name;
5ab54cf7 192 /* Private data for our user (ie. gadget). Managed by user. */
ddf8abd2
MN
193 void *private_data;
194
195 /* filled by __ffs_data_got_descs() */
5ab54cf7
MN
196 /*
197 * Real descriptors are 16 bytes after raw_descs (so you need
ddf8abd2
MN
198 * to skip 16 bytes (ie. ffs->raw_descs + 16) to get to the
199 * first full speed descriptor). raw_descs_length and
5ab54cf7
MN
200 * raw_fs_descs_length do not have those 16 bytes added.
201 */
ddf8abd2
MN
202 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
5ab54cf7
MN
218 /*
219 * File system's super block, write once when file system is
220 * mounted.
221 */
ddf8abd2
MN
222 struct super_block *sb;
223
5ab54cf7 224 /* File permissions, written once when fs is mounted */
ddf8abd2
MN
225 struct ffs_file_perms {
226 umode_t mode;
b9b73f7c
EB
227 kuid_t uid;
228 kgid_t gid;
ddf8abd2
MN
229 } file_perms;
230
5ab54cf7
MN
231 /*
232 * The endpoint files, filled by ffs_epfiles_create(),
233 * destroyed by ffs_epfiles_destroy().
234 */
ddf8abd2
MN
235 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
5ab54cf7 248/* Called with ffs->mutex held; take over ownership of data. */
ddf8abd2
MN
249static 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
ddf8abd2
MN
279static void ffs_func_eps_disable(struct ffs_function *func);
280static int __must_check ffs_func_eps_enable(struct ffs_function *func);
281
ddf8abd2
MN
282static 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
ddf8abd2
MN
298/* 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
ddf8abd2
MN
330static 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));
260ef311 343static char *ffs_prepare_buffer(const char __user *buf, size_t len)
ddf8abd2
MN
344 __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
ddf8abd2
MN
356static 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
ce1fd358
MS
368 /*
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
16735d02 376 reinit_completion(&ffs->ep0req_completion);
ddf8abd2
MN
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) {
aa02f172 395 pr_vdebug("ep0 stall\n");
ddf8abd2
MN
396 usb_ep_set_halt(ffs->gadget->ep0);
397 ffs->setup_state = FFS_NO_SETUP;
398 return -EL2HLT;
399 } else {
aa02f172 400 pr_debug("bogus ep0 stall!\n");
ddf8abd2
MN
401 return -ESRCH;
402 }
403}
404
ddf8abd2
MN
405static 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
ddf8abd2
MN
423 /* 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);
537baabb 434 if (IS_ERR(data)) {
ddf8abd2
MN
435 ret = PTR_ERR(data);
436 break;
437 }
438
439 /* Handle data */
440 if (ffs->state == FFS_READ_DESCRIPTORS) {
aa02f172 441 pr_info("read descriptors\n");
ddf8abd2
MN
442 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 {
aa02f172 449 pr_info("read strings\n");
ddf8abd2
MN
450 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
ddf8abd2
MN
474 case FFS_ACTIVE:
475 data = NULL;
5ab54cf7
MN
476 /*
477 * We're called from user space, we can use _irq
478 * rather then _irqsave
479 */
ddf8abd2
MN
480 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);
537baabb 507 if (IS_ERR(data)) {
ddf8abd2
MN
508 ret = PTR_ERR(data);
509 break;
510 }
511
512 spin_lock_irq(&ffs->ev.waitq.lock);
513
5ab54cf7
MN
514 /*
515 * We are guaranteed to be still in FFS_ACTIVE state
ddf8abd2
MN
516 * 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
5ab54cf7
MN
519 * from user space in vain but it's unlikely.
520 *
521 * For sure we are not in FFS_NO_SETUP since this is
ddf8abd2
MN
522 * the only place FFS_SETUP_PENDING -> FFS_NO_SETUP
523 * transition can be performed and it's protected by
5ab54cf7
MN
524 * mutex.
525 */
ddf8abd2
MN
526 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
ddf8abd2
MN
537 default:
538 ret = -EBADFD;
539 break;
540 }
541
ddf8abd2
MN
542 mutex_unlock(&ffs->mutex);
543 return ret;
544}
545
ddf8abd2
MN
546static ssize_t __ffs_ep0_read_events(struct ffs_data *ffs, char __user *buf,
547 size_t n)
548{
5ab54cf7
MN
549 /*
550 * We are holding ffs->ev.waitq.lock and ffs->mutex and we need
551 * to release them.
552 */
ddf8abd2
MN
553 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
ddf8abd2
MN
581static 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
ddf8abd2
MN
600 /* Check state */
601 if (ffs->state != FFS_ACTIVE) {
602 ret = -EBADFD;
603 goto done_mutex;
604 }
605
5ab54cf7
MN
606 /*
607 * We're called from user space, we can use _irq rather then
608 * _irqsave
609 */
ddf8abd2
MN
610 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
5ab54cf7
MN
629 if (wait_event_interruptible_exclusive_locked_irq(ffs->ev.waitq,
630 ffs->ev.count)) {
ddf8abd2
MN
631 ret = -EINTR;
632 break;
633 }
634
635 return __ffs_ep0_read_events(ffs, buf,
636 min(n, (size_t)ffs->ev.count));
637
ddf8abd2
MN
638 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
ddf8abd2
MN
683static 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
ddf8abd2
MN
698static 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
ddf8abd2
MN
709static 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;
92b0abf8 720 } else if (gadget && gadget->ops->ioctl) {
ddf8abd2 721 ret = gadget->ops->ioctl(gadget, code, value);
ddf8abd2
MN
722 } else {
723 ret = -ENOTTY;
724 }
725
726 return ret;
727}
728
ddf8abd2 729static const struct file_operations ffs_ep0_operations = {
ddf8abd2
MN
730 .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
ddf8abd2
MN
742static 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
ddf8abd2
MN
752static 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;
219580e6 756 struct usb_gadget *gadget = epfile->ffs->gadget;
ddf8abd2
MN
757 struct ffs_ep *ep;
758 char *data = NULL;
219580e6 759 ssize_t ret, data_len;
ddf8abd2
MN
760 int halt;
761
7fa68034
MN
762 /* Are we still active? */
763 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE)) {
764 ret = -ENODEV;
765 goto error;
766 }
ddf8abd2 767
7fa68034
MN
768 /* Wait for endpoint to be enabled */
769 ep = epfile->ep;
770 if (!ep) {
771 if (file->f_flags & O_NONBLOCK) {
772 ret = -EAGAIN;
ddf8abd2
MN
773 goto error;
774 }
775
7fa68034
MN
776 ret = wait_event_interruptible(epfile->wait, (ep = epfile->ep));
777 if (ret) {
778 ret = -EINTR;
ddf8abd2
MN
779 goto error;
780 }
7fa68034 781 }
ddf8abd2 782
7fa68034
MN
783 /* Do we halt? */
784 halt = !read == !epfile->in;
785 if (halt && epfile->isoc) {
786 ret = -EINVAL;
787 goto error;
788 }
ddf8abd2 789
7fa68034
MN
790 /* Allocate & copy */
791 if (!halt) {
219580e6
MN
792 /*
793 * Controller may require buffer size to be aligned to
794 * maxpacketsize of an out endpoint.
795 */
796 data_len = read ? usb_ep_align_maybe(gadget, ep->ep, len) : len;
797
798 data = kmalloc(data_len, GFP_KERNEL);
7fa68034
MN
799 if (unlikely(!data))
800 return -ENOMEM;
ddf8abd2 801
7fa68034
MN
802 if (!read && unlikely(copy_from_user(data, buf, len))) {
803 ret = -EFAULT;
ddf8abd2 804 goto error;
7fa68034
MN
805 }
806 }
ddf8abd2 807
7fa68034
MN
808 /* We will be using request */
809 ret = ffs_mutex_lock(&epfile->mutex, file->f_flags & O_NONBLOCK);
810 if (unlikely(ret))
811 goto error;
ddf8abd2 812
7fa68034 813 spin_lock_irq(&epfile->ffs->eps_lock);
ddf8abd2 814
7fa68034
MN
815 if (epfile->ep != ep) {
816 /* In the meantime, endpoint got disabled or changed. */
817 ret = -ESHUTDOWN;
818 spin_unlock_irq(&epfile->ffs->eps_lock);
819 } else if (halt) {
820 /* Halt */
ddf8abd2
MN
821 if (likely(epfile->ep == ep) && !WARN_ON(!ep->ep))
822 usb_ep_set_halt(ep->ep);
823 spin_unlock_irq(&epfile->ffs->eps_lock);
824 ret = -EBADMSG;
825 } else {
826 /* Fire the request */
827 DECLARE_COMPLETION_ONSTACK(done);
828
829 struct usb_request *req = ep->req;
830 req->context = &done;
831 req->complete = ffs_epfile_io_complete;
832 req->buf = data;
219580e6 833 req->length = data_len;
ddf8abd2
MN
834
835 ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
836
837 spin_unlock_irq(&epfile->ffs->eps_lock);
838
839 if (unlikely(ret < 0)) {
840 /* nop */
841 } else if (unlikely(wait_for_completion_interruptible(&done))) {
842 ret = -EINTR;
843 usb_ep_dequeue(ep->ep, req);
844 } else {
219580e6
MN
845 /*
846 * XXX We may end up silently droping data here.
847 * Since data_len (i.e. req->length) may be bigger
848 * than len (after being rounded up to maxpacketsize),
849 * we may end up with more data then user space has
850 * space for.
851 */
ddf8abd2
MN
852 ret = ep->status;
853 if (read && ret > 0 &&
219580e6
MN
854 unlikely(copy_to_user(buf, data,
855 min_t(size_t, ret, len))))
ddf8abd2
MN
856 ret = -EFAULT;
857 }
858 }
859
860 mutex_unlock(&epfile->mutex);
861error:
862 kfree(data);
863 return ret;
864}
865
ddf8abd2
MN
866static ssize_t
867ffs_epfile_write(struct file *file, const char __user *buf, size_t len,
868 loff_t *ptr)
869{
870 ENTER();
871
872 return ffs_epfile_io(file, (char __user *)buf, len, 0);
873}
874
875static ssize_t
876ffs_epfile_read(struct file *file, char __user *buf, size_t len, loff_t *ptr)
877{
878 ENTER();
879
880 return ffs_epfile_io(file, buf, len, 1);
881}
882
883static int
884ffs_epfile_open(struct inode *inode, struct file *file)
885{
886 struct ffs_epfile *epfile = inode->i_private;
887
888 ENTER();
889
890 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
891 return -ENODEV;
892
893 file->private_data = epfile;
894 ffs_data_opened(epfile->ffs);
895
896 return 0;
897}
898
899static int
900ffs_epfile_release(struct inode *inode, struct file *file)
901{
902 struct ffs_epfile *epfile = inode->i_private;
903
904 ENTER();
905
906 ffs_data_closed(epfile->ffs);
907
908 return 0;
909}
910
ddf8abd2
MN
911static long ffs_epfile_ioctl(struct file *file, unsigned code,
912 unsigned long value)
913{
914 struct ffs_epfile *epfile = file->private_data;
915 int ret;
916
917 ENTER();
918
919 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
920 return -ENODEV;
921
922 spin_lock_irq(&epfile->ffs->eps_lock);
923 if (likely(epfile->ep)) {
924 switch (code) {
925 case FUNCTIONFS_FIFO_STATUS:
926 ret = usb_ep_fifo_status(epfile->ep->ep);
927 break;
928 case FUNCTIONFS_FIFO_FLUSH:
929 usb_ep_fifo_flush(epfile->ep->ep);
930 ret = 0;
931 break;
932 case FUNCTIONFS_CLEAR_HALT:
933 ret = usb_ep_clear_halt(epfile->ep->ep);
934 break;
935 case FUNCTIONFS_ENDPOINT_REVMAP:
936 ret = epfile->ep->num;
937 break;
938 default:
939 ret = -ENOTTY;
940 }
941 } else {
942 ret = -ENODEV;
943 }
944 spin_unlock_irq(&epfile->ffs->eps_lock);
945
946 return ret;
947}
948
ddf8abd2 949static const struct file_operations ffs_epfile_operations = {
ddf8abd2
MN
950 .llseek = no_llseek,
951
952 .open = ffs_epfile_open,
953 .write = ffs_epfile_write,
954 .read = ffs_epfile_read,
955 .release = ffs_epfile_release,
956 .unlocked_ioctl = ffs_epfile_ioctl,
957};
958
959
ddf8abd2
MN
960/* File system and super block operations ***********************************/
961
962/*
5ab54cf7 963 * Mounting the file system creates a controller file, used first for
ddf8abd2
MN
964 * function configuration then later for event monitoring.
965 */
966
ddf8abd2
MN
967static struct inode *__must_check
968ffs_sb_make_inode(struct super_block *sb, void *data,
969 const struct file_operations *fops,
970 const struct inode_operations *iops,
971 struct ffs_file_perms *perms)
972{
973 struct inode *inode;
974
975 ENTER();
976
977 inode = new_inode(sb);
978
979 if (likely(inode)) {
980 struct timespec current_time = CURRENT_TIME;
981
12ba8d1e 982 inode->i_ino = get_next_ino();
ddf8abd2
MN
983 inode->i_mode = perms->mode;
984 inode->i_uid = perms->uid;
985 inode->i_gid = perms->gid;
986 inode->i_atime = current_time;
987 inode->i_mtime = current_time;
988 inode->i_ctime = current_time;
989 inode->i_private = data;
990 if (fops)
991 inode->i_fop = fops;
992 if (iops)
993 inode->i_op = iops;
994 }
995
996 return inode;
997}
998
ddf8abd2 999/* Create "regular" file */
ddf8abd2
MN
1000static struct inode *ffs_sb_create_file(struct super_block *sb,
1001 const char *name, void *data,
1002 const struct file_operations *fops,
1003 struct dentry **dentry_p)
1004{
1005 struct ffs_data *ffs = sb->s_fs_info;
1006 struct dentry *dentry;
1007 struct inode *inode;
1008
1009 ENTER();
1010
1011 dentry = d_alloc_name(sb->s_root, name);
1012 if (unlikely(!dentry))
1013 return NULL;
1014
1015 inode = ffs_sb_make_inode(sb, data, fops, NULL, &ffs->file_perms);
1016 if (unlikely(!inode)) {
1017 dput(dentry);
1018 return NULL;
1019 }
1020
1021 d_add(dentry, inode);
1022 if (dentry_p)
1023 *dentry_p = dentry;
1024
1025 return inode;
1026}
1027
ddf8abd2 1028/* Super block */
ddf8abd2
MN
1029static const struct super_operations ffs_sb_operations = {
1030 .statfs = simple_statfs,
1031 .drop_inode = generic_delete_inode,
1032};
1033
1034struct ffs_sb_fill_data {
1035 struct ffs_file_perms perms;
1036 umode_t root_mode;
1037 const char *dev_name;
2606b28a 1038 struct ffs_data *ffs_data;
ddf8abd2
MN
1039};
1040
1041static int ffs_sb_fill(struct super_block *sb, void *_data, int silent)
1042{
1043 struct ffs_sb_fill_data *data = _data;
1044 struct inode *inode;
2606b28a 1045 struct ffs_data *ffs = data->ffs_data;
ddf8abd2
MN
1046
1047 ENTER();
1048
ddf8abd2 1049 ffs->sb = sb;
2606b28a 1050 data->ffs_data = NULL;
ddf8abd2
MN
1051 sb->s_fs_info = ffs;
1052 sb->s_blocksize = PAGE_CACHE_SIZE;
1053 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1054 sb->s_magic = FUNCTIONFS_MAGIC;
1055 sb->s_op = &ffs_sb_operations;
1056 sb->s_time_gran = 1;
1057
1058 /* Root inode */
1059 data->perms.mode = data->root_mode;
1060 inode = ffs_sb_make_inode(sb, NULL,
1061 &simple_dir_operations,
1062 &simple_dir_inode_operations,
1063 &data->perms);
48fde701
AV
1064 sb->s_root = d_make_root(inode);
1065 if (unlikely(!sb->s_root))
2606b28a 1066 return -ENOMEM;
ddf8abd2
MN
1067
1068 /* EP0 file */
1069 if (unlikely(!ffs_sb_create_file(sb, "ep0", ffs,
1070 &ffs_ep0_operations, NULL)))
2606b28a 1071 return -ENOMEM;
ddf8abd2
MN
1072
1073 return 0;
ddf8abd2
MN
1074}
1075
ddf8abd2
MN
1076static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts)
1077{
1078 ENTER();
1079
1080 if (!opts || !*opts)
1081 return 0;
1082
1083 for (;;) {
ddf8abd2 1084 unsigned long value;
afd2e186 1085 char *eq, *comma;
ddf8abd2
MN
1086
1087 /* Option limit */
1088 comma = strchr(opts, ',');
1089 if (comma)
1090 *comma = 0;
1091
1092 /* Value limit */
1093 eq = strchr(opts, '=');
1094 if (unlikely(!eq)) {
aa02f172 1095 pr_err("'=' missing in %s\n", opts);
ddf8abd2
MN
1096 return -EINVAL;
1097 }
1098 *eq = 0;
1099
1100 /* Parse value */
afd2e186 1101 if (kstrtoul(eq + 1, 0, &value)) {
aa02f172 1102 pr_err("%s: invalid value: %s\n", opts, eq + 1);
ddf8abd2
MN
1103 return -EINVAL;
1104 }
1105
1106 /* Interpret option */
1107 switch (eq - opts) {
1108 case 5:
1109 if (!memcmp(opts, "rmode", 5))
1110 data->root_mode = (value & 0555) | S_IFDIR;
1111 else if (!memcmp(opts, "fmode", 5))
1112 data->perms.mode = (value & 0666) | S_IFREG;
1113 else
1114 goto invalid;
1115 break;
1116
1117 case 4:
1118 if (!memcmp(opts, "mode", 4)) {
1119 data->root_mode = (value & 0555) | S_IFDIR;
1120 data->perms.mode = (value & 0666) | S_IFREG;
1121 } else {
1122 goto invalid;
1123 }
1124 break;
1125
1126 case 3:
b9b73f7c
EB
1127 if (!memcmp(opts, "uid", 3)) {
1128 data->perms.uid = make_kuid(current_user_ns(), value);
1129 if (!uid_valid(data->perms.uid)) {
1130 pr_err("%s: unmapped value: %lu\n", opts, value);
1131 return -EINVAL;
1132 }
b8100750 1133 } else if (!memcmp(opts, "gid", 3)) {
b9b73f7c
EB
1134 data->perms.gid = make_kgid(current_user_ns(), value);
1135 if (!gid_valid(data->perms.gid)) {
1136 pr_err("%s: unmapped value: %lu\n", opts, value);
1137 return -EINVAL;
1138 }
b8100750 1139 } else {
ddf8abd2 1140 goto invalid;
b8100750 1141 }
ddf8abd2
MN
1142 break;
1143
1144 default:
1145invalid:
aa02f172 1146 pr_err("%s: invalid option\n", opts);
ddf8abd2
MN
1147 return -EINVAL;
1148 }
1149
1150 /* Next iteration */
1151 if (!comma)
1152 break;
1153 opts = comma + 1;
1154 }
1155
1156 return 0;
1157}
1158
ddf8abd2
MN
1159/* "mount -t functionfs dev_name /dev/function" ends up here */
1160
fc14f2fe
AV
1161static struct dentry *
1162ffs_fs_mount(struct file_system_type *t, int flags,
1163 const char *dev_name, void *opts)
ddf8abd2
MN
1164{
1165 struct ffs_sb_fill_data data = {
1166 .perms = {
1167 .mode = S_IFREG | 0600,
b9b73f7c
EB
1168 .uid = GLOBAL_ROOT_UID,
1169 .gid = GLOBAL_ROOT_GID,
ddf8abd2
MN
1170 },
1171 .root_mode = S_IFDIR | 0500,
1172 };
581791f5 1173 struct dentry *rv;
ddf8abd2 1174 int ret;
581791f5 1175 void *ffs_dev;
2606b28a 1176 struct ffs_data *ffs;
ddf8abd2
MN
1177
1178 ENTER();
1179
ddf8abd2
MN
1180 ret = ffs_fs_parse_opts(&data, opts);
1181 if (unlikely(ret < 0))
fc14f2fe 1182 return ERR_PTR(ret);
ddf8abd2 1183
2606b28a
AV
1184 ffs = ffs_data_new();
1185 if (unlikely(!ffs))
1186 return ERR_PTR(-ENOMEM);
1187 ffs->file_perms = data.perms;
1188
1189 ffs->dev_name = kstrdup(dev_name, GFP_KERNEL);
1190 if (unlikely(!ffs->dev_name)) {
1191 ffs_data_put(ffs);
1192 return ERR_PTR(-ENOMEM);
1193 }
1194
581791f5 1195 ffs_dev = functionfs_acquire_dev_callback(dev_name);
2606b28a
AV
1196 if (IS_ERR(ffs_dev)) {
1197 ffs_data_put(ffs);
1198 return ERR_CAST(ffs_dev);
1199 }
1200 ffs->private_data = ffs_dev;
1201 data.ffs_data = ffs;
581791f5 1202
581791f5 1203 rv = mount_nodev(t, flags, &data, ffs_sb_fill);
2606b28a 1204 if (IS_ERR(rv) && data.ffs_data) {
581791f5 1205 functionfs_release_dev_callback(data.ffs_data);
2606b28a
AV
1206 ffs_data_put(data.ffs_data);
1207 }
581791f5 1208 return rv;
ddf8abd2
MN
1209}
1210
1211static void
1212ffs_fs_kill_sb(struct super_block *sb)
1213{
ddf8abd2
MN
1214 ENTER();
1215
1216 kill_litter_super(sb);
581791f5
AP
1217 if (sb->s_fs_info) {
1218 functionfs_release_dev_callback(sb->s_fs_info);
5b5f9560 1219 ffs_data_put(sb->s_fs_info);
581791f5 1220 }
ddf8abd2
MN
1221}
1222
1223static struct file_system_type ffs_fs_type = {
1224 .owner = THIS_MODULE,
1225 .name = "functionfs",
fc14f2fe 1226 .mount = ffs_fs_mount,
ddf8abd2
MN
1227 .kill_sb = ffs_fs_kill_sb,
1228};
7f78e035 1229MODULE_ALIAS_FS("functionfs");
ddf8abd2
MN
1230
1231
ddf8abd2
MN
1232/* Driver's main init/cleanup functions *************************************/
1233
ddf8abd2
MN
1234static int functionfs_init(void)
1235{
1236 int ret;
1237
1238 ENTER();
1239
1240 ret = register_filesystem(&ffs_fs_type);
1241 if (likely(!ret))
aa02f172 1242 pr_info("file system registered\n");
ddf8abd2 1243 else
aa02f172 1244 pr_err("failed registering file system (%d)\n", ret);
ddf8abd2
MN
1245
1246 return ret;
1247}
1248
1249static void functionfs_cleanup(void)
1250{
1251 ENTER();
1252
aa02f172 1253 pr_info("unloading\n");
ddf8abd2
MN
1254 unregister_filesystem(&ffs_fs_type);
1255}
1256
1257
ddf8abd2
MN
1258/* ffs_data and ffs_function construction and destruction code **************/
1259
1260static void ffs_data_clear(struct ffs_data *ffs);
1261static void ffs_data_reset(struct ffs_data *ffs);
1262
ddf8abd2
MN
1263static void ffs_data_get(struct ffs_data *ffs)
1264{
1265 ENTER();
1266
1267 atomic_inc(&ffs->ref);
1268}
1269
1270static void ffs_data_opened(struct ffs_data *ffs)
1271{
1272 ENTER();
1273
1274 atomic_inc(&ffs->ref);
1275 atomic_inc(&ffs->opened);
1276}
1277
1278static void ffs_data_put(struct ffs_data *ffs)
1279{
1280 ENTER();
1281
1282 if (unlikely(atomic_dec_and_test(&ffs->ref))) {
aa02f172 1283 pr_info("%s(): freeing\n", __func__);
ddf8abd2 1284 ffs_data_clear(ffs);
647d5580 1285 BUG_ON(waitqueue_active(&ffs->ev.waitq) ||
ddf8abd2 1286 waitqueue_active(&ffs->ep0req_completion.wait));
581791f5 1287 kfree(ffs->dev_name);
ddf8abd2
MN
1288 kfree(ffs);
1289 }
1290}
1291
ddf8abd2
MN
1292static void ffs_data_closed(struct ffs_data *ffs)
1293{
1294 ENTER();
1295
1296 if (atomic_dec_and_test(&ffs->opened)) {
1297 ffs->state = FFS_CLOSING;
1298 ffs_data_reset(ffs);
1299 }
1300
1301 ffs_data_put(ffs);
1302}
1303
ddf8abd2
MN
1304static struct ffs_data *ffs_data_new(void)
1305{
1306 struct ffs_data *ffs = kzalloc(sizeof *ffs, GFP_KERNEL);
1307 if (unlikely(!ffs))
1308 return 0;
1309
1310 ENTER();
1311
1312 atomic_set(&ffs->ref, 1);
1313 atomic_set(&ffs->opened, 0);
1314 ffs->state = FFS_READ_DESCRIPTORS;
1315 mutex_init(&ffs->mutex);
1316 spin_lock_init(&ffs->eps_lock);
1317 init_waitqueue_head(&ffs->ev.waitq);
1318 init_completion(&ffs->ep0req_completion);
1319
1320 /* XXX REVISIT need to update it in some places, or do we? */
1321 ffs->ev.can_stall = 1;
1322
1323 return ffs;
1324}
1325
ddf8abd2
MN
1326static void ffs_data_clear(struct ffs_data *ffs)
1327{
1328 ENTER();
1329
1330 if (test_and_clear_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags))
1331 functionfs_closed_callback(ffs);
1332
1333 BUG_ON(ffs->gadget);
1334
1335 if (ffs->epfiles)
1336 ffs_epfiles_destroy(ffs->epfiles, ffs->eps_count);
1337
1338 kfree(ffs->raw_descs);
1339 kfree(ffs->raw_strings);
1340 kfree(ffs->stringtabs);
1341}
1342
ddf8abd2
MN
1343static void ffs_data_reset(struct ffs_data *ffs)
1344{
1345 ENTER();
1346
1347 ffs_data_clear(ffs);
1348
1349 ffs->epfiles = NULL;
1350 ffs->raw_descs = NULL;
1351 ffs->raw_strings = NULL;
1352 ffs->stringtabs = NULL;
1353
1354 ffs->raw_descs_length = 0;
1355 ffs->raw_fs_descs_length = 0;
1356 ffs->fs_descs_count = 0;
1357 ffs->hs_descs_count = 0;
1358
1359 ffs->strings_count = 0;
1360 ffs->interfaces_count = 0;
1361 ffs->eps_count = 0;
1362
1363 ffs->ev.count = 0;
1364
1365 ffs->state = FFS_READ_DESCRIPTORS;
1366 ffs->setup_state = FFS_NO_SETUP;
1367 ffs->flags = 0;
1368}
1369
1370
1371static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev)
1372{
fd7c9a00
MN
1373 struct usb_gadget_strings **lang;
1374 int first_id;
ddf8abd2
MN
1375
1376 ENTER();
1377
1378 if (WARN_ON(ffs->state != FFS_ACTIVE
1379 || test_and_set_bit(FFS_FL_BOUND, &ffs->flags)))
1380 return -EBADFD;
1381
fd7c9a00
MN
1382 first_id = usb_string_ids_n(cdev, ffs->strings_count);
1383 if (unlikely(first_id < 0))
1384 return first_id;
ddf8abd2
MN
1385
1386 ffs->ep0req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
1387 if (unlikely(!ffs->ep0req))
1388 return -ENOMEM;
1389 ffs->ep0req->complete = ffs_ep0_complete;
1390 ffs->ep0req->context = ffs;
1391
fd7c9a00
MN
1392 lang = ffs->stringtabs;
1393 for (lang = ffs->stringtabs; *lang; ++lang) {
1394 struct usb_string *str = (*lang)->strings;
1395 int id = first_id;
1396 for (; str->s; ++id, ++str)
1397 str->id = id;
ddf8abd2
MN
1398 }
1399
1400 ffs->gadget = cdev->gadget;
fd7c9a00 1401 ffs_data_get(ffs);
ddf8abd2
MN
1402 return 0;
1403}
1404
ddf8abd2
MN
1405static void functionfs_unbind(struct ffs_data *ffs)
1406{
1407 ENTER();
1408
1409 if (!WARN_ON(!ffs->gadget)) {
1410 usb_ep_free_request(ffs->gadget->ep0, ffs->ep0req);
1411 ffs->ep0req = NULL;
1412 ffs->gadget = NULL;
e2190a97 1413 clear_bit(FFS_FL_BOUND, &ffs->flags);
df498995 1414 ffs_data_put(ffs);
ddf8abd2
MN
1415 }
1416}
1417
ddf8abd2
MN
1418static int ffs_epfiles_create(struct ffs_data *ffs)
1419{
1420 struct ffs_epfile *epfile, *epfiles;
1421 unsigned i, count;
1422
1423 ENTER();
1424
1425 count = ffs->eps_count;
9823a525 1426 epfiles = kcalloc(count, sizeof(*epfiles), GFP_KERNEL);
ddf8abd2
MN
1427 if (!epfiles)
1428 return -ENOMEM;
1429
1430 epfile = epfiles;
1431 for (i = 1; i <= count; ++i, ++epfile) {
1432 epfile->ffs = ffs;
1433 mutex_init(&epfile->mutex);
1434 init_waitqueue_head(&epfile->wait);
1435 sprintf(epfiles->name, "ep%u", i);
1436 if (!unlikely(ffs_sb_create_file(ffs->sb, epfiles->name, epfile,
1437 &ffs_epfile_operations,
1438 &epfile->dentry))) {
1439 ffs_epfiles_destroy(epfiles, i - 1);
1440 return -ENOMEM;
1441 }
1442 }
1443
1444 ffs->epfiles = epfiles;
1445 return 0;
1446}
1447
ddf8abd2
MN
1448static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count)
1449{
1450 struct ffs_epfile *epfile = epfiles;
1451
1452 ENTER();
1453
1454 for (; count; --count, ++epfile) {
1455 BUG_ON(mutex_is_locked(&epfile->mutex) ||
1456 waitqueue_active(&epfile->wait));
1457 if (epfile->dentry) {
1458 d_delete(epfile->dentry);
1459 dput(epfile->dentry);
1460 epfile->dentry = NULL;
1461 }
1462 }
1463
1464 kfree(epfiles);
1465}
1466
7898aee1
MN
1467static int functionfs_bind_config(struct usb_composite_dev *cdev,
1468 struct usb_configuration *c,
1469 struct ffs_data *ffs)
ddf8abd2
MN
1470{
1471 struct ffs_function *func;
1472 int ret;
1473
1474 ENTER();
1475
1476 func = kzalloc(sizeof *func, GFP_KERNEL);
1477 if (unlikely(!func))
1478 return -ENOMEM;
1479
1480 func->function.name = "Function FS Gadget";
1481 func->function.strings = ffs->stringtabs;
1482
1483 func->function.bind = ffs_func_bind;
1484 func->function.unbind = ffs_func_unbind;
1485 func->function.set_alt = ffs_func_set_alt;
ddf8abd2
MN
1486 func->function.disable = ffs_func_disable;
1487 func->function.setup = ffs_func_setup;
1488 func->function.suspend = ffs_func_suspend;
1489 func->function.resume = ffs_func_resume;
1490
1491 func->conf = c;
1492 func->gadget = cdev->gadget;
1493 func->ffs = ffs;
1494 ffs_data_get(ffs);
1495
1496 ret = usb_add_function(c, &func->function);
1497 if (unlikely(ret))
1498 ffs_func_free(func);
1499
1500 return ret;
1501}
1502
1503static void ffs_func_free(struct ffs_function *func)
1504{
4f06539f
PK
1505 struct ffs_ep *ep = func->eps;
1506 unsigned count = func->ffs->eps_count;
1507 unsigned long flags;
1508
ddf8abd2
MN
1509 ENTER();
1510
4f06539f
PK
1511 /* cleanup after autoconfig */
1512 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1513 do {
1514 if (ep->ep && ep->req)
1515 usb_ep_free_request(ep->ep, ep->req);
1516 ep->req = NULL;
1517 ++ep;
1518 } while (--count);
1519 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1520
ddf8abd2
MN
1521 ffs_data_put(func->ffs);
1522
1523 kfree(func->eps);
5ab54cf7
MN
1524 /*
1525 * eps and interfaces_nums are allocated in the same chunk so
ddf8abd2 1526 * only one free is required. Descriptors are also allocated
5ab54cf7
MN
1527 * in the same chunk.
1528 */
ddf8abd2
MN
1529
1530 kfree(func);
1531}
1532
ddf8abd2
MN
1533static void ffs_func_eps_disable(struct ffs_function *func)
1534{
1535 struct ffs_ep *ep = func->eps;
1536 struct ffs_epfile *epfile = func->ffs->epfiles;
1537 unsigned count = func->ffs->eps_count;
1538 unsigned long flags;
1539
1540 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1541 do {
1542 /* pending requests get nuked */
1543 if (likely(ep->ep))
1544 usb_ep_disable(ep->ep);
1545 epfile->ep = NULL;
1546
1547 ++ep;
1548 ++epfile;
1549 } while (--count);
1550 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1551}
1552
1553static int ffs_func_eps_enable(struct ffs_function *func)
1554{
1555 struct ffs_data *ffs = func->ffs;
1556 struct ffs_ep *ep = func->eps;
1557 struct ffs_epfile *epfile = ffs->epfiles;
1558 unsigned count = ffs->eps_count;
1559 unsigned long flags;
1560 int ret = 0;
1561
1562 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1563 do {
1564 struct usb_endpoint_descriptor *ds;
1565 ds = ep->descs[ep->descs[1] ? 1 : 0];
1566
1567 ep->ep->driver_data = ep;
72c973dd
TB
1568 ep->ep->desc = ds;
1569 ret = usb_ep_enable(ep->ep);
ddf8abd2
MN
1570 if (likely(!ret)) {
1571 epfile->ep = ep;
1572 epfile->in = usb_endpoint_dir_in(ds);
1573 epfile->isoc = usb_endpoint_xfer_isoc(ds);
1574 } else {
1575 break;
1576 }
1577
1578 wake_up(&epfile->wait);
1579
1580 ++ep;
1581 ++epfile;
1582 } while (--count);
1583 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1584
1585 return ret;
1586}
1587
1588
1589/* Parsing and building descriptors and strings *****************************/
1590
5ab54cf7
MN
1591/*
1592 * This validates if data pointed by data is a valid USB descriptor as
ddf8abd2 1593 * well as record how many interfaces, endpoints and strings are
5ab54cf7
MN
1594 * required by given configuration. Returns address after the
1595 * descriptor or NULL if data is invalid.
1596 */
ddf8abd2
MN
1597
1598enum ffs_entity_type {
1599 FFS_DESCRIPTOR, FFS_INTERFACE, FFS_STRING, FFS_ENDPOINT
1600};
1601
1602typedef int (*ffs_entity_callback)(enum ffs_entity_type entity,
1603 u8 *valuep,
1604 struct usb_descriptor_header *desc,
1605 void *priv);
1606
1607static int __must_check ffs_do_desc(char *data, unsigned len,
1608 ffs_entity_callback entity, void *priv)
1609{
1610 struct usb_descriptor_header *_ds = (void *)data;
1611 u8 length;
1612 int ret;
1613
1614 ENTER();
1615
1616 /* At least two bytes are required: length and type */
1617 if (len < 2) {
aa02f172 1618 pr_vdebug("descriptor too short\n");
ddf8abd2
MN
1619 return -EINVAL;
1620 }
1621
1622 /* If we have at least as many bytes as the descriptor takes? */
1623 length = _ds->bLength;
1624 if (len < length) {
aa02f172 1625 pr_vdebug("descriptor longer then available data\n");
ddf8abd2
MN
1626 return -EINVAL;
1627 }
1628
1629#define __entity_check_INTERFACE(val) 1
1630#define __entity_check_STRING(val) (val)
1631#define __entity_check_ENDPOINT(val) ((val) & USB_ENDPOINT_NUMBER_MASK)
1632#define __entity(type, val) do { \
aa02f172 1633 pr_vdebug("entity " #type "(%02x)\n", (val)); \
ddf8abd2 1634 if (unlikely(!__entity_check_ ##type(val))) { \
aa02f172 1635 pr_vdebug("invalid entity's value\n"); \
ddf8abd2
MN
1636 return -EINVAL; \
1637 } \
1638 ret = entity(FFS_ ##type, &val, _ds, priv); \
1639 if (unlikely(ret < 0)) { \
aa02f172 1640 pr_debug("entity " #type "(%02x); ret = %d\n", \
d8df0b61 1641 (val), ret); \
ddf8abd2
MN
1642 return ret; \
1643 } \
1644 } while (0)
1645
1646 /* Parse descriptor depending on type. */
1647 switch (_ds->bDescriptorType) {
1648 case USB_DT_DEVICE:
1649 case USB_DT_CONFIG:
1650 case USB_DT_STRING:
1651 case USB_DT_DEVICE_QUALIFIER:
1652 /* function can't have any of those */
aa02f172 1653 pr_vdebug("descriptor reserved for gadget: %d\n",
5ab54cf7 1654 _ds->bDescriptorType);
ddf8abd2
MN
1655 return -EINVAL;
1656
1657 case USB_DT_INTERFACE: {
1658 struct usb_interface_descriptor *ds = (void *)_ds;
aa02f172 1659 pr_vdebug("interface descriptor\n");
ddf8abd2
MN
1660 if (length != sizeof *ds)
1661 goto inv_length;
1662
1663 __entity(INTERFACE, ds->bInterfaceNumber);
1664 if (ds->iInterface)
1665 __entity(STRING, ds->iInterface);
1666 }
1667 break;
1668
1669 case USB_DT_ENDPOINT: {
1670 struct usb_endpoint_descriptor *ds = (void *)_ds;
aa02f172 1671 pr_vdebug("endpoint descriptor\n");
ddf8abd2
MN
1672 if (length != USB_DT_ENDPOINT_SIZE &&
1673 length != USB_DT_ENDPOINT_AUDIO_SIZE)
1674 goto inv_length;
1675 __entity(ENDPOINT, ds->bEndpointAddress);
1676 }
1677 break;
1678
560f1187
KB
1679 case HID_DT_HID:
1680 pr_vdebug("hid descriptor\n");
1681 if (length != sizeof(struct hid_descriptor))
1682 goto inv_length;
1683 break;
1684
ddf8abd2
MN
1685 case USB_DT_OTG:
1686 if (length != sizeof(struct usb_otg_descriptor))
1687 goto inv_length;
1688 break;
1689
1690 case USB_DT_INTERFACE_ASSOCIATION: {
1691 struct usb_interface_assoc_descriptor *ds = (void *)_ds;
aa02f172 1692 pr_vdebug("interface association descriptor\n");
ddf8abd2
MN
1693 if (length != sizeof *ds)
1694 goto inv_length;
1695 if (ds->iFunction)
1696 __entity(STRING, ds->iFunction);
1697 }
1698 break;
1699
1700 case USB_DT_OTHER_SPEED_CONFIG:
1701 case USB_DT_INTERFACE_POWER:
1702 case USB_DT_DEBUG:
1703 case USB_DT_SECURITY:
1704 case USB_DT_CS_RADIO_CONTROL:
1705 /* TODO */
aa02f172 1706 pr_vdebug("unimplemented descriptor: %d\n", _ds->bDescriptorType);
ddf8abd2
MN
1707 return -EINVAL;
1708
1709 default:
1710 /* We should never be here */
aa02f172 1711 pr_vdebug("unknown descriptor: %d\n", _ds->bDescriptorType);
ddf8abd2
MN
1712 return -EINVAL;
1713
5ab54cf7 1714inv_length:
aa02f172 1715 pr_vdebug("invalid length: %d (descriptor %d)\n",
d8df0b61 1716 _ds->bLength, _ds->bDescriptorType);
ddf8abd2
MN
1717 return -EINVAL;
1718 }
1719
1720#undef __entity
1721#undef __entity_check_DESCRIPTOR
1722#undef __entity_check_INTERFACE
1723#undef __entity_check_STRING
1724#undef __entity_check_ENDPOINT
1725
1726 return length;
1727}
1728
ddf8abd2
MN
1729static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len,
1730 ffs_entity_callback entity, void *priv)
1731{
1732 const unsigned _len = len;
1733 unsigned long num = 0;
1734
1735 ENTER();
1736
1737 for (;;) {
1738 int ret;
1739
1740 if (num == count)
1741 data = NULL;
1742
5ab54cf7 1743 /* Record "descriptor" entity */
ddf8abd2
MN
1744 ret = entity(FFS_DESCRIPTOR, (u8 *)num, (void *)data, priv);
1745 if (unlikely(ret < 0)) {
aa02f172 1746 pr_debug("entity DESCRIPTOR(%02lx); ret = %d\n",
d8df0b61 1747 num, ret);
ddf8abd2
MN
1748 return ret;
1749 }
1750
1751 if (!data)
1752 return _len - len;
1753
1754 ret = ffs_do_desc(data, len, entity, priv);
1755 if (unlikely(ret < 0)) {
aa02f172 1756 pr_debug("%s returns %d\n", __func__, ret);
ddf8abd2
MN
1757 return ret;
1758 }
1759
1760 len -= ret;
1761 data += ret;
1762 ++num;
1763 }
1764}
1765
ddf8abd2
MN
1766static int __ffs_data_do_entity(enum ffs_entity_type type,
1767 u8 *valuep, struct usb_descriptor_header *desc,
1768 void *priv)
1769{
1770 struct ffs_data *ffs = priv;
1771
1772 ENTER();
1773
1774 switch (type) {
1775 case FFS_DESCRIPTOR:
1776 break;
1777
1778 case FFS_INTERFACE:
5ab54cf7
MN
1779 /*
1780 * Interfaces are indexed from zero so if we
ddf8abd2 1781 * encountered interface "n" then there are at least
5ab54cf7
MN
1782 * "n+1" interfaces.
1783 */
ddf8abd2
MN
1784 if (*valuep >= ffs->interfaces_count)
1785 ffs->interfaces_count = *valuep + 1;
1786 break;
1787
1788 case FFS_STRING:
5ab54cf7
MN
1789 /*
1790 * Strings are indexed from 1 (0 is magic ;) reserved
1791 * for languages list or some such)
1792 */
ddf8abd2
MN
1793 if (*valuep > ffs->strings_count)
1794 ffs->strings_count = *valuep;
1795 break;
1796
1797 case FFS_ENDPOINT:
1798 /* Endpoints are indexed from 1 as well. */
1799 if ((*valuep & USB_ENDPOINT_NUMBER_MASK) > ffs->eps_count)
1800 ffs->eps_count = (*valuep & USB_ENDPOINT_NUMBER_MASK);
1801 break;
1802 }
1803
1804 return 0;
1805}
1806
ddf8abd2
MN
1807static int __ffs_data_got_descs(struct ffs_data *ffs,
1808 char *const _data, size_t len)
1809{
1810 unsigned fs_count, hs_count;
1811 int fs_len, ret = -EINVAL;
1812 char *data = _data;
1813
1814 ENTER();
1815
1816 if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_DESCRIPTORS_MAGIC ||
1817 get_unaligned_le32(data + 4) != len))
1818 goto error;
1819 fs_count = get_unaligned_le32(data + 8);
1820 hs_count = get_unaligned_le32(data + 12);
1821
1822 if (!fs_count && !hs_count)
1823 goto einval;
1824
1825 data += 16;
1826 len -= 16;
1827
1828 if (likely(fs_count)) {
1829 fs_len = ffs_do_descs(fs_count, data, len,
1830 __ffs_data_do_entity, ffs);
1831 if (unlikely(fs_len < 0)) {
1832 ret = fs_len;
1833 goto error;
1834 }
1835
1836 data += fs_len;
1837 len -= fs_len;
1838 } else {
1839 fs_len = 0;
1840 }
1841
1842 if (likely(hs_count)) {
1843 ret = ffs_do_descs(hs_count, data, len,
1844 __ffs_data_do_entity, ffs);
1845 if (unlikely(ret < 0))
1846 goto error;
1847 } else {
1848 ret = 0;
1849 }
1850
1851 if (unlikely(len != ret))
1852 goto einval;
1853
1854 ffs->raw_fs_descs_length = fs_len;
1855 ffs->raw_descs_length = fs_len + ret;
1856 ffs->raw_descs = _data;
1857 ffs->fs_descs_count = fs_count;
1858 ffs->hs_descs_count = hs_count;
1859
1860 return 0;
1861
1862einval:
1863 ret = -EINVAL;
1864error:
1865 kfree(_data);
1866 return ret;
1867}
1868
ddf8abd2
MN
1869static int __ffs_data_got_strings(struct ffs_data *ffs,
1870 char *const _data, size_t len)
1871{
1872 u32 str_count, needed_count, lang_count;
1873 struct usb_gadget_strings **stringtabs, *t;
1874 struct usb_string *strings, *s;
1875 const char *data = _data;
1876
1877 ENTER();
1878
1879 if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_STRINGS_MAGIC ||
1880 get_unaligned_le32(data + 4) != len))
1881 goto error;
1882 str_count = get_unaligned_le32(data + 8);
1883 lang_count = get_unaligned_le32(data + 12);
1884
1885 /* if one is zero the other must be zero */
1886 if (unlikely(!str_count != !lang_count))
1887 goto error;
1888
1889 /* Do we have at least as many strings as descriptors need? */
1890 needed_count = ffs->strings_count;
1891 if (unlikely(str_count < needed_count))
1892 goto error;
1893
5ab54cf7
MN
1894 /*
1895 * If we don't need any strings just return and free all
1896 * memory.
1897 */
ddf8abd2
MN
1898 if (!needed_count) {
1899 kfree(_data);
1900 return 0;
1901 }
1902
5ab54cf7 1903 /* Allocate everything in one chunk so there's less maintenance. */
ddf8abd2 1904 {
ddf8abd2
MN
1905 struct {
1906 struct usb_gadget_strings *stringtabs[lang_count + 1];
1907 struct usb_gadget_strings stringtab[lang_count];
1908 struct usb_string strings[lang_count*(needed_count+1)];
1909 } *d;
1910 unsigned i = 0;
1911
1912 d = kmalloc(sizeof *d, GFP_KERNEL);
1913 if (unlikely(!d)) {
1914 kfree(_data);
1915 return -ENOMEM;
1916 }
1917
1918 stringtabs = d->stringtabs;
1919 t = d->stringtab;
1920 i = lang_count;
1921 do {
1922 *stringtabs++ = t++;
1923 } while (--i);
1924 *stringtabs = NULL;
1925
1926 stringtabs = d->stringtabs;
1927 t = d->stringtab;
1928 s = d->strings;
1929 strings = s;
1930 }
1931
1932 /* For each language */
1933 data += 16;
1934 len -= 16;
1935
1936 do { /* lang_count > 0 so we can use do-while */
1937 unsigned needed = needed_count;
1938
1939 if (unlikely(len < 3))
1940 goto error_free;
1941 t->language = get_unaligned_le16(data);
1942 t->strings = s;
1943 ++t;
1944
1945 data += 2;
1946 len -= 2;
1947
1948 /* For each string */
1949 do { /* str_count > 0 so we can use do-while */
1950 size_t length = strnlen(data, len);
1951
1952 if (unlikely(length == len))
1953 goto error_free;
1954
5ab54cf7
MN
1955 /*
1956 * User may provide more strings then we need,
1957 * if that's the case we simply ignore the
1958 * rest
1959 */
ddf8abd2 1960 if (likely(needed)) {
5ab54cf7
MN
1961 /*
1962 * s->id will be set while adding
ddf8abd2 1963 * function to configuration so for
5ab54cf7
MN
1964 * now just leave garbage here.
1965 */
ddf8abd2
MN
1966 s->s = data;
1967 --needed;
1968 ++s;
1969 }
1970
1971 data += length + 1;
1972 len -= length + 1;
1973 } while (--str_count);
1974
1975 s->id = 0; /* terminator */
1976 s->s = NULL;
1977 ++s;
1978
1979 } while (--lang_count);
1980
1981 /* Some garbage left? */
1982 if (unlikely(len))
1983 goto error_free;
1984
1985 /* Done! */
1986 ffs->stringtabs = stringtabs;
1987 ffs->raw_strings = _data;
1988
1989 return 0;
1990
1991error_free:
1992 kfree(stringtabs);
1993error:
1994 kfree(_data);
1995 return -EINVAL;
1996}
1997
1998
ddf8abd2
MN
1999/* Events handling and management *******************************************/
2000
2001static void __ffs_event_add(struct ffs_data *ffs,
2002 enum usb_functionfs_event_type type)
2003{
2004 enum usb_functionfs_event_type rem_type1, rem_type2 = type;
2005 int neg = 0;
2006
5ab54cf7
MN
2007 /*
2008 * Abort any unhandled setup
2009 *
2010 * We do not need to worry about some cmpxchg() changing value
ddf8abd2
MN
2011 * of ffs->setup_state without holding the lock because when
2012 * state is FFS_SETUP_PENDING cmpxchg() in several places in
5ab54cf7
MN
2013 * the source does nothing.
2014 */
ddf8abd2
MN
2015 if (ffs->setup_state == FFS_SETUP_PENDING)
2016 ffs->setup_state = FFS_SETUP_CANCELED;
2017
2018 switch (type) {
2019 case FUNCTIONFS_RESUME:
2020 rem_type2 = FUNCTIONFS_SUSPEND;
5ab54cf7 2021 /* FALL THROUGH */
ddf8abd2
MN
2022 case FUNCTIONFS_SUSPEND:
2023 case FUNCTIONFS_SETUP:
2024 rem_type1 = type;
5ab54cf7 2025 /* Discard all similar events */
ddf8abd2
MN
2026 break;
2027
2028 case FUNCTIONFS_BIND:
2029 case FUNCTIONFS_UNBIND:
2030 case FUNCTIONFS_DISABLE:
2031 case FUNCTIONFS_ENABLE:
5ab54cf7 2032 /* Discard everything other then power management. */
ddf8abd2
MN
2033 rem_type1 = FUNCTIONFS_SUSPEND;
2034 rem_type2 = FUNCTIONFS_RESUME;
2035 neg = 1;
2036 break;
2037
2038 default:
2039 BUG();
2040 }
2041
2042 {
2043 u8 *ev = ffs->ev.types, *out = ev;
2044 unsigned n = ffs->ev.count;
2045 for (; n; --n, ++ev)
2046 if ((*ev == rem_type1 || *ev == rem_type2) == neg)
2047 *out++ = *ev;
2048 else
aa02f172 2049 pr_vdebug("purging event %d\n", *ev);
ddf8abd2
MN
2050 ffs->ev.count = out - ffs->ev.types;
2051 }
2052
aa02f172 2053 pr_vdebug("adding event %d\n", type);
ddf8abd2
MN
2054 ffs->ev.types[ffs->ev.count++] = type;
2055 wake_up_locked(&ffs->ev.waitq);
2056}
2057
2058static void ffs_event_add(struct ffs_data *ffs,
2059 enum usb_functionfs_event_type type)
2060{
2061 unsigned long flags;
2062 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2063 __ffs_event_add(ffs, type);
2064 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2065}
2066
2067
2068/* Bind/unbind USB function hooks *******************************************/
2069
2070static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
2071 struct usb_descriptor_header *desc,
2072 void *priv)
2073{
2074 struct usb_endpoint_descriptor *ds = (void *)desc;
2075 struct ffs_function *func = priv;
2076 struct ffs_ep *ffs_ep;
2077
5ab54cf7
MN
2078 /*
2079 * If hs_descriptors is not NULL then we are reading hs
2080 * descriptors now
2081 */
ddf8abd2
MN
2082 const int isHS = func->function.hs_descriptors != NULL;
2083 unsigned idx;
2084
2085 if (type != FFS_DESCRIPTOR)
2086 return 0;
2087
2088 if (isHS)
2089 func->function.hs_descriptors[(long)valuep] = desc;
2090 else
10287bae 2091 func->function.fs_descriptors[(long)valuep] = desc;
ddf8abd2
MN
2092
2093 if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT)
2094 return 0;
2095
2096 idx = (ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK) - 1;
2097 ffs_ep = func->eps + idx;
2098
2099 if (unlikely(ffs_ep->descs[isHS])) {
aa02f172 2100 pr_vdebug("two %sspeed descriptors for EP %d\n",
d8df0b61
MN
2101 isHS ? "high" : "full",
2102 ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
ddf8abd2
MN
2103 return -EINVAL;
2104 }
2105 ffs_ep->descs[isHS] = ds;
2106
2107 ffs_dump_mem(": Original ep desc", ds, ds->bLength);
2108 if (ffs_ep->ep) {
2109 ds->bEndpointAddress = ffs_ep->descs[0]->bEndpointAddress;
2110 if (!ds->wMaxPacketSize)
2111 ds->wMaxPacketSize = ffs_ep->descs[0]->wMaxPacketSize;
2112 } else {
2113 struct usb_request *req;
2114 struct usb_ep *ep;
2115
aa02f172 2116 pr_vdebug("autoconfig\n");
ddf8abd2
MN
2117 ep = usb_ep_autoconfig(func->gadget, ds);
2118 if (unlikely(!ep))
2119 return -ENOTSUPP;
cc7e6056 2120 ep->driver_data = func->eps + idx;
ddf8abd2
MN
2121
2122 req = usb_ep_alloc_request(ep, GFP_KERNEL);
2123 if (unlikely(!req))
2124 return -ENOMEM;
2125
2126 ffs_ep->ep = ep;
2127 ffs_ep->req = req;
2128 func->eps_revmap[ds->bEndpointAddress &
2129 USB_ENDPOINT_NUMBER_MASK] = idx + 1;
2130 }
2131 ffs_dump_mem(": Rewritten ep desc", ds, ds->bLength);
2132
2133 return 0;
2134}
2135
ddf8abd2
MN
2136static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep,
2137 struct usb_descriptor_header *desc,
2138 void *priv)
2139{
2140 struct ffs_function *func = priv;
2141 unsigned idx;
2142 u8 newValue;
2143
2144 switch (type) {
2145 default:
2146 case FFS_DESCRIPTOR:
2147 /* Handled in previous pass by __ffs_func_bind_do_descs() */
2148 return 0;
2149
2150 case FFS_INTERFACE:
2151 idx = *valuep;
2152 if (func->interfaces_nums[idx] < 0) {
2153 int id = usb_interface_id(func->conf, &func->function);
2154 if (unlikely(id < 0))
2155 return id;
2156 func->interfaces_nums[idx] = id;
2157 }
2158 newValue = func->interfaces_nums[idx];
2159 break;
2160
2161 case FFS_STRING:
2162 /* String' IDs are allocated when fsf_data is bound to cdev */
2163 newValue = func->ffs->stringtabs[0]->strings[*valuep - 1].id;
2164 break;
2165
2166 case FFS_ENDPOINT:
5ab54cf7
MN
2167 /*
2168 * USB_DT_ENDPOINT are handled in
2169 * __ffs_func_bind_do_descs().
2170 */
ddf8abd2
MN
2171 if (desc->bDescriptorType == USB_DT_ENDPOINT)
2172 return 0;
2173
2174 idx = (*valuep & USB_ENDPOINT_NUMBER_MASK) - 1;
2175 if (unlikely(!func->eps[idx].ep))
2176 return -EINVAL;
2177
2178 {
2179 struct usb_endpoint_descriptor **descs;
2180 descs = func->eps[idx].descs;
2181 newValue = descs[descs[0] ? 0 : 1]->bEndpointAddress;
2182 }
2183 break;
2184 }
2185
aa02f172 2186 pr_vdebug("%02x -> %02x\n", *valuep, newValue);
ddf8abd2
MN
2187 *valuep = newValue;
2188 return 0;
2189}
2190
2191static int ffs_func_bind(struct usb_configuration *c,
2192 struct usb_function *f)
2193{
2194 struct ffs_function *func = ffs_func_from_usb(f);
2195 struct ffs_data *ffs = func->ffs;
2196
2197 const int full = !!func->ffs->fs_descs_count;
2198 const int high = gadget_is_dualspeed(func->gadget) &&
2199 func->ffs->hs_descs_count;
2200
2201 int ret;
2202
2203 /* Make it a single chunk, less management later on */
2204 struct {
2205 struct ffs_ep eps[ffs->eps_count];
2206 struct usb_descriptor_header
2207 *fs_descs[full ? ffs->fs_descs_count + 1 : 0];
2208 struct usb_descriptor_header
2209 *hs_descs[high ? ffs->hs_descs_count + 1 : 0];
2210 short inums[ffs->interfaces_count];
2211 char raw_descs[high ? ffs->raw_descs_length
2212 : ffs->raw_fs_descs_length];
2213 } *data;
2214
2215 ENTER();
2216
2217 /* Only high speed but not supported by gadget? */
2218 if (unlikely(!(full | high)))
2219 return -ENOTSUPP;
2220
2221 /* Allocate */
2222 data = kmalloc(sizeof *data, GFP_KERNEL);
2223 if (unlikely(!data))
2224 return -ENOMEM;
2225
2226 /* Zero */
2227 memset(data->eps, 0, sizeof data->eps);
2228 memcpy(data->raw_descs, ffs->raw_descs + 16, sizeof data->raw_descs);
2229 memset(data->inums, 0xff, sizeof data->inums);
2230 for (ret = ffs->eps_count; ret; --ret)
2231 data->eps[ret].num = -1;
2232
2233 /* Save pointers */
2234 func->eps = data->eps;
2235 func->interfaces_nums = data->inums;
2236
5ab54cf7
MN
2237 /*
2238 * Go through all the endpoint descriptors and allocate
ddf8abd2 2239 * endpoints first, so that later we can rewrite the endpoint
5ab54cf7
MN
2240 * numbers without worrying that it may be described later on.
2241 */
ddf8abd2 2242 if (likely(full)) {
10287bae 2243 func->function.fs_descriptors = data->fs_descs;
ddf8abd2
MN
2244 ret = ffs_do_descs(ffs->fs_descs_count,
2245 data->raw_descs,
2246 sizeof data->raw_descs,
2247 __ffs_func_bind_do_descs, func);
2248 if (unlikely(ret < 0))
2249 goto error;
2250 } else {
2251 ret = 0;
2252 }
2253
2254 if (likely(high)) {
2255 func->function.hs_descriptors = data->hs_descs;
2256 ret = ffs_do_descs(ffs->hs_descs_count,
2257 data->raw_descs + ret,
2258 (sizeof data->raw_descs) - ret,
2259 __ffs_func_bind_do_descs, func);
8854894c
RB
2260 if (unlikely(ret < 0))
2261 goto error;
ddf8abd2
MN
2262 }
2263
5ab54cf7
MN
2264 /*
2265 * Now handle interface numbers allocation and interface and
2266 * endpoint numbers rewriting. We can do that in one go
2267 * now.
2268 */
ddf8abd2
MN
2269 ret = ffs_do_descs(ffs->fs_descs_count +
2270 (high ? ffs->hs_descs_count : 0),
2271 data->raw_descs, sizeof data->raw_descs,
2272 __ffs_func_bind_do_nums, func);
2273 if (unlikely(ret < 0))
2274 goto error;
2275
2276 /* And we're done */
2277 ffs_event_add(ffs, FUNCTIONFS_BIND);
2278 return 0;
2279
2280error:
2281 /* XXX Do we need to release all claimed endpoints here? */
2282 return ret;
2283}
2284
2285
2286/* Other USB function hooks *************************************************/
2287
2288static void ffs_func_unbind(struct usb_configuration *c,
2289 struct usb_function *f)
2290{
2291 struct ffs_function *func = ffs_func_from_usb(f);
2292 struct ffs_data *ffs = func->ffs;
2293
2294 ENTER();
2295
2296 if (ffs->func == func) {
2297 ffs_func_eps_disable(func);
2298 ffs->func = NULL;
2299 }
2300
2301 ffs_event_add(ffs, FUNCTIONFS_UNBIND);
2302
2303 ffs_func_free(func);
2304}
2305
ddf8abd2
MN
2306static int ffs_func_set_alt(struct usb_function *f,
2307 unsigned interface, unsigned alt)
2308{
2309 struct ffs_function *func = ffs_func_from_usb(f);
2310 struct ffs_data *ffs = func->ffs;
2311 int ret = 0, intf;
2312
2313 if (alt != (unsigned)-1) {
2314 intf = ffs_func_revmap_intf(func, interface);
2315 if (unlikely(intf < 0))
2316 return intf;
2317 }
2318
2319 if (ffs->func)
2320 ffs_func_eps_disable(ffs->func);
2321
2322 if (ffs->state != FFS_ACTIVE)
2323 return -ENODEV;
2324
2325 if (alt == (unsigned)-1) {
2326 ffs->func = NULL;
2327 ffs_event_add(ffs, FUNCTIONFS_DISABLE);
2328 return 0;
2329 }
2330
2331 ffs->func = func;
2332 ret = ffs_func_eps_enable(func);
2333 if (likely(ret >= 0))
2334 ffs_event_add(ffs, FUNCTIONFS_ENABLE);
2335 return ret;
2336}
2337
2338static void ffs_func_disable(struct usb_function *f)
2339{
2340 ffs_func_set_alt(f, 0, (unsigned)-1);
2341}
2342
2343static int ffs_func_setup(struct usb_function *f,
2344 const struct usb_ctrlrequest *creq)
2345{
2346 struct ffs_function *func = ffs_func_from_usb(f);
2347 struct ffs_data *ffs = func->ffs;
2348 unsigned long flags;
2349 int ret;
2350
2351 ENTER();
2352
aa02f172
MN
2353 pr_vdebug("creq->bRequestType = %02x\n", creq->bRequestType);
2354 pr_vdebug("creq->bRequest = %02x\n", creq->bRequest);
2355 pr_vdebug("creq->wValue = %04x\n", le16_to_cpu(creq->wValue));
2356 pr_vdebug("creq->wIndex = %04x\n", le16_to_cpu(creq->wIndex));
2357 pr_vdebug("creq->wLength = %04x\n", le16_to_cpu(creq->wLength));
ddf8abd2 2358
5ab54cf7
MN
2359 /*
2360 * Most requests directed to interface go through here
ddf8abd2
MN
2361 * (notable exceptions are set/get interface) so we need to
2362 * handle them. All other either handled by composite or
2363 * passed to usb_configuration->setup() (if one is set). No
2364 * matter, we will handle requests directed to endpoint here
2365 * as well (as it's straightforward) but what to do with any
5ab54cf7
MN
2366 * other request?
2367 */
ddf8abd2
MN
2368 if (ffs->state != FFS_ACTIVE)
2369 return -ENODEV;
2370
2371 switch (creq->bRequestType & USB_RECIP_MASK) {
2372 case USB_RECIP_INTERFACE:
2373 ret = ffs_func_revmap_intf(func, le16_to_cpu(creq->wIndex));
2374 if (unlikely(ret < 0))
2375 return ret;
2376 break;
2377
2378 case USB_RECIP_ENDPOINT:
2379 ret = ffs_func_revmap_ep(func, le16_to_cpu(creq->wIndex));
2380 if (unlikely(ret < 0))
2381 return ret;
2382 break;
2383
2384 default:
2385 return -EOPNOTSUPP;
2386 }
2387
2388 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2389 ffs->ev.setup = *creq;
2390 ffs->ev.setup.wIndex = cpu_to_le16(ret);
2391 __ffs_event_add(ffs, FUNCTIONFS_SETUP);
2392 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2393
2394 return 0;
2395}
2396
2397static void ffs_func_suspend(struct usb_function *f)
2398{
2399 ENTER();
2400 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_SUSPEND);
2401}
2402
2403static void ffs_func_resume(struct usb_function *f)
2404{
2405 ENTER();
2406 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_RESUME);
2407}
2408
2409
5ab54cf7 2410/* Endpoint and interface numbers reverse mapping ***************************/
ddf8abd2
MN
2411
2412static int ffs_func_revmap_ep(struct ffs_function *func, u8 num)
2413{
2414 num = func->eps_revmap[num & USB_ENDPOINT_NUMBER_MASK];
2415 return num ? num : -EDOM;
2416}
2417
2418static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf)
2419{
2420 short *nums = func->interfaces_nums;
2421 unsigned count = func->ffs->interfaces_count;
2422
2423 for (; count; --count, ++nums) {
2424 if (*nums >= 0 && *nums == intf)
2425 return nums - func->interfaces_nums;
2426 }
2427
2428 return -EDOM;
2429}
2430
2431
2432/* Misc helper functions ****************************************************/
2433
2434static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
2435{
2436 return nonblock
2437 ? likely(mutex_trylock(mutex)) ? 0 : -EAGAIN
2438 : mutex_lock_interruptible(mutex);
2439}
2440
260ef311 2441static char *ffs_prepare_buffer(const char __user *buf, size_t len)
ddf8abd2
MN
2442{
2443 char *data;
2444
2445 if (unlikely(!len))
2446 return NULL;
2447
2448 data = kmalloc(len, GFP_KERNEL);
2449 if (unlikely(!data))
2450 return ERR_PTR(-ENOMEM);
2451
2452 if (unlikely(__copy_from_user(data, buf, len))) {
2453 kfree(data);
2454 return ERR_PTR(-EFAULT);
2455 }
2456
aa02f172 2457 pr_vdebug("Buffer from user space:\n");
ddf8abd2
MN
2458 ffs_dump_mem("", data, len);
2459
2460 return data;
2461}
This page took 0.469802 seconds and 5 git commands to generate.