Merge tag 'v3.10.105' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.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
6fa3eb70
S
200 * raw_fs_hs_descs_length do not have those 16 bytes added.
201 * ss_desc are 8 bytes (ss_magic + count) pass the hs_descs
5ab54cf7 202 */
ddf8abd2
MN
203 const void *raw_descs;
204 unsigned raw_descs_length;
6fa3eb70
S
205 unsigned raw_fs_hs_descs_length;
206 unsigned raw_ss_descs_offset;
207 unsigned raw_ss_descs_length;
ddf8abd2
MN
208 unsigned fs_descs_count;
209 unsigned hs_descs_count;
6fa3eb70 210 unsigned ss_descs_count;
ddf8abd2
MN
211
212 unsigned short strings_count;
213 unsigned short interfaces_count;
214 unsigned short eps_count;
215 unsigned short _pad1;
216
217 /* filled by __ffs_data_got_strings() */
218 /* ids in stringtabs are set in functionfs_bind() */
219 const void *raw_strings;
220 struct usb_gadget_strings **stringtabs;
221
5ab54cf7
MN
222 /*
223 * File system's super block, write once when file system is
224 * mounted.
225 */
ddf8abd2
MN
226 struct super_block *sb;
227
5ab54cf7 228 /* File permissions, written once when fs is mounted */
ddf8abd2
MN
229 struct ffs_file_perms {
230 umode_t mode;
b9b73f7c
EB
231 kuid_t uid;
232 kgid_t gid;
ddf8abd2
MN
233 } file_perms;
234
5ab54cf7
MN
235 /*
236 * The endpoint files, filled by ffs_epfiles_create(),
237 * destroyed by ffs_epfiles_destroy().
238 */
ddf8abd2
MN
239 struct ffs_epfile *epfiles;
240};
241
242/* Reference counter handling */
243static void ffs_data_get(struct ffs_data *ffs);
244static void ffs_data_put(struct ffs_data *ffs);
245/* Creates new ffs_data object. */
246static struct ffs_data *__must_check ffs_data_new(void) __attribute__((malloc));
247
248/* Opened counter handling. */
249static void ffs_data_opened(struct ffs_data *ffs);
250static void ffs_data_closed(struct ffs_data *ffs);
251
5ab54cf7 252/* Called with ffs->mutex held; take over ownership of data. */
ddf8abd2
MN
253static int __must_check
254__ffs_data_got_descs(struct ffs_data *ffs, char *data, size_t len);
255static int __must_check
256__ffs_data_got_strings(struct ffs_data *ffs, char *data, size_t len);
257
258
259/* The function structure ***************************************************/
260
261struct ffs_ep;
262
263struct ffs_function {
264 struct usb_configuration *conf;
265 struct usb_gadget *gadget;
266 struct ffs_data *ffs;
267
268 struct ffs_ep *eps;
269 u8 eps_revmap[16];
270 short *interfaces_nums;
271
272 struct usb_function function;
273};
274
275
276static struct ffs_function *ffs_func_from_usb(struct usb_function *f)
277{
278 return container_of(f, struct ffs_function, function);
279}
280
281static void ffs_func_free(struct ffs_function *func);
282
ddf8abd2
MN
283static void ffs_func_eps_disable(struct ffs_function *func);
284static int __must_check ffs_func_eps_enable(struct ffs_function *func);
285
ddf8abd2
MN
286static int ffs_func_bind(struct usb_configuration *,
287 struct usb_function *);
288static void ffs_func_unbind(struct usb_configuration *,
289 struct usb_function *);
290static int ffs_func_set_alt(struct usb_function *, unsigned, unsigned);
291static void ffs_func_disable(struct usb_function *);
292static int ffs_func_setup(struct usb_function *,
293 const struct usb_ctrlrequest *);
294static void ffs_func_suspend(struct usb_function *);
295static void ffs_func_resume(struct usb_function *);
296
297
298static int ffs_func_revmap_ep(struct ffs_function *func, u8 num);
299static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf);
300
301
ddf8abd2
MN
302/* The endpoints structures *************************************************/
303
304struct ffs_ep {
305 struct usb_ep *ep; /* P: ffs->eps_lock */
306 struct usb_request *req; /* P: epfile->mutex */
307
6fa3eb70
S
308 /* [0]: full speed, [1]: high speed, [2]: super speed */
309 struct usb_endpoint_descriptor *descs[3];
ddf8abd2
MN
310
311 u8 num;
312
313 int status; /* P: epfile->mutex */
314};
315
316struct ffs_epfile {
317 /* Protects ep->ep and ep->req. */
318 struct mutex mutex;
319 wait_queue_head_t wait;
320
321 struct ffs_data *ffs;
322 struct ffs_ep *ep; /* P: ffs->eps_lock */
323
324 struct dentry *dentry;
325
326 char name[5];
327
328 unsigned char in; /* P: ffs->eps_lock */
329 unsigned char isoc; /* P: ffs->eps_lock */
330
331 unsigned char _pad;
332};
333
ddf8abd2
MN
334static int __must_check ffs_epfiles_create(struct ffs_data *ffs);
335static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count);
336
337static struct inode *__must_check
338ffs_sb_create_file(struct super_block *sb, const char *name, void *data,
339 const struct file_operations *fops,
340 struct dentry **dentry_p);
341
342
343/* Misc helper functions ****************************************************/
344
345static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
346 __attribute__((warn_unused_result, nonnull));
260ef311 347static char *ffs_prepare_buffer(const char __user *buf, size_t len)
ddf8abd2
MN
348 __attribute__((warn_unused_result, nonnull));
349
350
351/* Control file aka ep0 *****************************************************/
352
353static void ffs_ep0_complete(struct usb_ep *ep, struct usb_request *req)
354{
355 struct ffs_data *ffs = req->context;
356
357 complete_all(&ffs->ep0req_completion);
358}
359
ddf8abd2
MN
360static int __ffs_ep0_queue_wait(struct ffs_data *ffs, char *data, size_t len)
361{
362 struct usb_request *req = ffs->ep0req;
363 int ret;
364
365 req->zero = len < le16_to_cpu(ffs->ev.setup.wLength);
366
367 spin_unlock_irq(&ffs->ev.waitq.lock);
368
369 req->buf = data;
370 req->length = len;
371
ce1fd358
MS
372 /*
373 * UDC layer requires to provide a buffer even for ZLP, but should
374 * not use it at all. Let's provide some poisoned pointer to catch
375 * possible bug in the driver.
376 */
377 if (req->buf == NULL)
378 req->buf = (void *)0xDEADBABE;
379
ddf8abd2
MN
380 INIT_COMPLETION(ffs->ep0req_completion);
381
382 ret = usb_ep_queue(ffs->gadget->ep0, req, GFP_ATOMIC);
383 if (unlikely(ret < 0))
384 return ret;
385
386 ret = wait_for_completion_interruptible(&ffs->ep0req_completion);
387 if (unlikely(ret)) {
388 usb_ep_dequeue(ffs->gadget->ep0, req);
389 return -EINTR;
390 }
391
392 ffs->setup_state = FFS_NO_SETUP;
393 return ffs->ep0req_status;
394}
395
396static int __ffs_ep0_stall(struct ffs_data *ffs)
397{
398 if (ffs->ev.can_stall) {
aa02f172 399 pr_vdebug("ep0 stall\n");
ddf8abd2
MN
400 usb_ep_set_halt(ffs->gadget->ep0);
401 ffs->setup_state = FFS_NO_SETUP;
402 return -EL2HLT;
403 } else {
aa02f172 404 pr_debug("bogus ep0 stall!\n");
ddf8abd2
MN
405 return -ESRCH;
406 }
407}
408
ddf8abd2
MN
409static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
410 size_t len, loff_t *ptr)
411{
412 struct ffs_data *ffs = file->private_data;
413 ssize_t ret;
414 char *data;
415
416 ENTER();
417
418 /* Fast check if setup was canceled */
419 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED)
420 return -EIDRM;
421
422 /* Acquire mutex */
423 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
424 if (unlikely(ret < 0))
425 return ret;
426
ddf8abd2
MN
427 /* Check state */
428 switch (ffs->state) {
429 case FFS_READ_DESCRIPTORS:
430 case FFS_READ_STRINGS:
431 /* Copy data */
432 if (unlikely(len < 16)) {
433 ret = -EINVAL;
434 break;
435 }
436
437 data = ffs_prepare_buffer(buf, len);
537baabb 438 if (IS_ERR(data)) {
ddf8abd2
MN
439 ret = PTR_ERR(data);
440 break;
441 }
442
443 /* Handle data */
444 if (ffs->state == FFS_READ_DESCRIPTORS) {
aa02f172 445 pr_info("read descriptors\n");
ddf8abd2
MN
446 ret = __ffs_data_got_descs(ffs, data, len);
447 if (unlikely(ret < 0))
448 break;
449
450 ffs->state = FFS_READ_STRINGS;
451 ret = len;
452 } else {
aa02f172 453 pr_info("read strings\n");
ddf8abd2
MN
454 ret = __ffs_data_got_strings(ffs, data, len);
455 if (unlikely(ret < 0))
456 break;
457
458 ret = ffs_epfiles_create(ffs);
459 if (unlikely(ret)) {
460 ffs->state = FFS_CLOSING;
461 break;
462 }
463
464 ffs->state = FFS_ACTIVE;
465 mutex_unlock(&ffs->mutex);
466
467 ret = functionfs_ready_callback(ffs);
468 if (unlikely(ret < 0)) {
469 ffs->state = FFS_CLOSING;
470 return ret;
471 }
472
473 set_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags);
474 return len;
475 }
476 break;
477
ddf8abd2
MN
478 case FFS_ACTIVE:
479 data = NULL;
5ab54cf7
MN
480 /*
481 * We're called from user space, we can use _irq
482 * rather then _irqsave
483 */
ddf8abd2
MN
484 spin_lock_irq(&ffs->ev.waitq.lock);
485 switch (FFS_SETUP_STATE(ffs)) {
486 case FFS_SETUP_CANCELED:
487 ret = -EIDRM;
488 goto done_spin;
489
490 case FFS_NO_SETUP:
491 ret = -ESRCH;
492 goto done_spin;
493
494 case FFS_SETUP_PENDING:
495 break;
496 }
497
498 /* FFS_SETUP_PENDING */
499 if (!(ffs->ev.setup.bRequestType & USB_DIR_IN)) {
500 spin_unlock_irq(&ffs->ev.waitq.lock);
501 ret = __ffs_ep0_stall(ffs);
502 break;
503 }
504
505 /* FFS_SETUP_PENDING and not stall */
506 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
507
508 spin_unlock_irq(&ffs->ev.waitq.lock);
509
510 data = ffs_prepare_buffer(buf, len);
537baabb 511 if (IS_ERR(data)) {
ddf8abd2
MN
512 ret = PTR_ERR(data);
513 break;
514 }
515
516 spin_lock_irq(&ffs->ev.waitq.lock);
517
5ab54cf7
MN
518 /*
519 * We are guaranteed to be still in FFS_ACTIVE state
ddf8abd2
MN
520 * but the state of setup could have changed from
521 * FFS_SETUP_PENDING to FFS_SETUP_CANCELED so we need
522 * to check for that. If that happened we copied data
5ab54cf7
MN
523 * from user space in vain but it's unlikely.
524 *
525 * For sure we are not in FFS_NO_SETUP since this is
ddf8abd2
MN
526 * the only place FFS_SETUP_PENDING -> FFS_NO_SETUP
527 * transition can be performed and it's protected by
5ab54cf7
MN
528 * mutex.
529 */
ddf8abd2
MN
530 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) {
531 ret = -EIDRM;
532done_spin:
533 spin_unlock_irq(&ffs->ev.waitq.lock);
534 } else {
535 /* unlocks spinlock */
536 ret = __ffs_ep0_queue_wait(ffs, data, len);
537 }
538 kfree(data);
539 break;
540
ddf8abd2
MN
541 default:
542 ret = -EBADFD;
543 break;
544 }
545
ddf8abd2
MN
546 mutex_unlock(&ffs->mutex);
547 return ret;
548}
549
ddf8abd2
MN
550static ssize_t __ffs_ep0_read_events(struct ffs_data *ffs, char __user *buf,
551 size_t n)
552{
5ab54cf7
MN
553 /*
554 * We are holding ffs->ev.waitq.lock and ffs->mutex and we need
555 * to release them.
556 */
ddf8abd2
MN
557 struct usb_functionfs_event events[n];
558 unsigned i = 0;
559
560 memset(events, 0, sizeof events);
561
562 do {
563 events[i].type = ffs->ev.types[i];
564 if (events[i].type == FUNCTIONFS_SETUP) {
565 events[i].u.setup = ffs->ev.setup;
566 ffs->setup_state = FFS_SETUP_PENDING;
567 }
568 } while (++i < n);
569
570 if (n < ffs->ev.count) {
571 ffs->ev.count -= n;
572 memmove(ffs->ev.types, ffs->ev.types + n,
573 ffs->ev.count * sizeof *ffs->ev.types);
574 } else {
575 ffs->ev.count = 0;
576 }
577
578 spin_unlock_irq(&ffs->ev.waitq.lock);
579 mutex_unlock(&ffs->mutex);
580
581 return unlikely(__copy_to_user(buf, events, sizeof events))
582 ? -EFAULT : sizeof events;
583}
584
ddf8abd2
MN
585static ssize_t ffs_ep0_read(struct file *file, char __user *buf,
586 size_t len, loff_t *ptr)
587{
588 struct ffs_data *ffs = file->private_data;
589 char *data = NULL;
590 size_t n;
591 int ret;
592
593 ENTER();
594
595 /* Fast check if setup was canceled */
596 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED)
597 return -EIDRM;
598
599 /* Acquire mutex */
600 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
601 if (unlikely(ret < 0))
602 return ret;
603
ddf8abd2
MN
604 /* Check state */
605 if (ffs->state != FFS_ACTIVE) {
606 ret = -EBADFD;
607 goto done_mutex;
608 }
609
5ab54cf7
MN
610 /*
611 * We're called from user space, we can use _irq rather then
612 * _irqsave
613 */
ddf8abd2
MN
614 spin_lock_irq(&ffs->ev.waitq.lock);
615
616 switch (FFS_SETUP_STATE(ffs)) {
617 case FFS_SETUP_CANCELED:
618 ret = -EIDRM;
619 break;
620
621 case FFS_NO_SETUP:
622 n = len / sizeof(struct usb_functionfs_event);
623 if (unlikely(!n)) {
624 ret = -EINVAL;
625 break;
626 }
627
628 if ((file->f_flags & O_NONBLOCK) && !ffs->ev.count) {
629 ret = -EAGAIN;
630 break;
631 }
632
5ab54cf7
MN
633 if (wait_event_interruptible_exclusive_locked_irq(ffs->ev.waitq,
634 ffs->ev.count)) {
ddf8abd2
MN
635 ret = -EINTR;
636 break;
637 }
638
639 return __ffs_ep0_read_events(ffs, buf,
640 min(n, (size_t)ffs->ev.count));
641
ddf8abd2
MN
642 case FFS_SETUP_PENDING:
643 if (ffs->ev.setup.bRequestType & USB_DIR_IN) {
644 spin_unlock_irq(&ffs->ev.waitq.lock);
645 ret = __ffs_ep0_stall(ffs);
646 goto done_mutex;
647 }
648
649 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
650
651 spin_unlock_irq(&ffs->ev.waitq.lock);
652
653 if (likely(len)) {
654 data = kmalloc(len, GFP_KERNEL);
655 if (unlikely(!data)) {
656 ret = -ENOMEM;
657 goto done_mutex;
658 }
659 }
660
661 spin_lock_irq(&ffs->ev.waitq.lock);
662
663 /* See ffs_ep0_write() */
664 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) {
665 ret = -EIDRM;
666 break;
667 }
668
669 /* unlocks spinlock */
670 ret = __ffs_ep0_queue_wait(ffs, data, len);
671 if (likely(ret > 0) && unlikely(__copy_to_user(buf, data, len)))
672 ret = -EFAULT;
673 goto done_mutex;
674
675 default:
676 ret = -EBADFD;
677 break;
678 }
679
680 spin_unlock_irq(&ffs->ev.waitq.lock);
681done_mutex:
682 mutex_unlock(&ffs->mutex);
683 kfree(data);
684 return ret;
685}
686
ddf8abd2
MN
687static int ffs_ep0_open(struct inode *inode, struct file *file)
688{
689 struct ffs_data *ffs = inode->i_private;
690
691 ENTER();
692
693 if (unlikely(ffs->state == FFS_CLOSING))
694 return -EBUSY;
695
696 file->private_data = ffs;
697 ffs_data_opened(ffs);
698
699 return 0;
700}
701
ddf8abd2
MN
702static int ffs_ep0_release(struct inode *inode, struct file *file)
703{
704 struct ffs_data *ffs = file->private_data;
705
706 ENTER();
707
708 ffs_data_closed(ffs);
709
710 return 0;
711}
712
ddf8abd2
MN
713static long ffs_ep0_ioctl(struct file *file, unsigned code, unsigned long value)
714{
715 struct ffs_data *ffs = file->private_data;
716 struct usb_gadget *gadget = ffs->gadget;
717 long ret;
718
719 ENTER();
720
721 if (code == FUNCTIONFS_INTERFACE_REVMAP) {
722 struct ffs_function *func = ffs->func;
723 ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV;
92b0abf8 724 } else if (gadget && gadget->ops->ioctl) {
ddf8abd2 725 ret = gadget->ops->ioctl(gadget, code, value);
ddf8abd2
MN
726 } else {
727 ret = -ENOTTY;
728 }
729
730 return ret;
731}
732
ddf8abd2 733static const struct file_operations ffs_ep0_operations = {
ddf8abd2
MN
734 .llseek = no_llseek,
735
736 .open = ffs_ep0_open,
737 .write = ffs_ep0_write,
738 .read = ffs_ep0_read,
739 .release = ffs_ep0_release,
740 .unlocked_ioctl = ffs_ep0_ioctl,
741};
742
743
744/* "Normal" endpoints operations ********************************************/
745
ddf8abd2
MN
746static void ffs_epfile_io_complete(struct usb_ep *_ep, struct usb_request *req)
747{
748 ENTER();
749 if (likely(req->context)) {
750 struct ffs_ep *ep = _ep->driver_data;
751 ep->status = req->status ? req->status : req->actual;
752 complete(req->context);
753 }
754}
755
ddf8abd2
MN
756static ssize_t ffs_epfile_io(struct file *file,
757 char __user *buf, size_t len, int read)
758{
759 struct ffs_epfile *epfile = file->private_data;
760 struct ffs_ep *ep;
761 char *data = NULL;
762 ssize_t ret;
763 int halt;
6fa3eb70
S
764 int buffer_len = 0;
765
766 pr_debug("%s: len %lld, read %d\n", __func__, (u64)len, read);
ddf8abd2
MN
767
768 goto first_try;
769 do {
770 spin_unlock_irq(&epfile->ffs->eps_lock);
771 mutex_unlock(&epfile->mutex);
772
773first_try:
774 /* Are we still active? */
775 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE)) {
776 ret = -ENODEV;
777 goto error;
778 }
779
780 /* Wait for endpoint to be enabled */
781 ep = epfile->ep;
782 if (!ep) {
783 if (file->f_flags & O_NONBLOCK) {
784 ret = -EAGAIN;
785 goto error;
786 }
787
5ab54cf7
MN
788 if (wait_event_interruptible(epfile->wait,
789 (ep = epfile->ep))) {
ddf8abd2
MN
790 ret = -EINTR;
791 goto error;
792 }
793 }
794
6fa3eb70
S
795 buffer_len = !read ? len : round_up(len,
796 ep->ep->desc->wMaxPacketSize);
797
ddf8abd2
MN
798 /* Do we halt? */
799 halt = !read == !epfile->in;
800 if (halt && epfile->isoc) {
801 ret = -EINVAL;
802 goto error;
803 }
804
805 /* Allocate & copy */
806 if (!halt && !data) {
6fa3eb70 807 data = kzalloc(buffer_len, GFP_KERNEL);
ddf8abd2
MN
808 if (unlikely(!data))
809 return -ENOMEM;
810
811 if (!read &&
812 unlikely(__copy_from_user(data, buf, len))) {
813 ret = -EFAULT;
814 goto error;
815 }
816 }
817
818 /* We will be using request */
819 ret = ffs_mutex_lock(&epfile->mutex,
820 file->f_flags & O_NONBLOCK);
821 if (unlikely(ret))
822 goto error;
823
5ab54cf7
MN
824 /*
825 * We're called from user space, we can use _irq rather then
826 * _irqsave
827 */
ddf8abd2
MN
828 spin_lock_irq(&epfile->ffs->eps_lock);
829
5ab54cf7
MN
830 /*
831 * While we were acquiring mutex endpoint got disabled
832 * or changed?
833 */
ddf8abd2
MN
834 } while (unlikely(epfile->ep != ep));
835
836 /* Halt */
837 if (unlikely(halt)) {
838 if (likely(epfile->ep == ep) && !WARN_ON(!ep->ep))
839 usb_ep_set_halt(ep->ep);
840 spin_unlock_irq(&epfile->ffs->eps_lock);
841 ret = -EBADMSG;
842 } else {
843 /* Fire the request */
844 DECLARE_COMPLETION_ONSTACK(done);
845
846 struct usb_request *req = ep->req;
847 req->context = &done;
848 req->complete = ffs_epfile_io_complete;
849 req->buf = data;
6fa3eb70 850 req->length = buffer_len;
ddf8abd2
MN
851
852 ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
853
854 spin_unlock_irq(&epfile->ffs->eps_lock);
855
856 if (unlikely(ret < 0)) {
857 /* nop */
858 } else if (unlikely(wait_for_completion_interruptible(&done))) {
859 ret = -EINTR;
860 usb_ep_dequeue(ep->ep, req);
861 } else {
862 ret = ep->status;
6fa3eb70
S
863 if (read && ret > 0) {
864 if (ret > len)
865 ret = -EOVERFLOW;
866 else if (unlikely(copy_to_user(buf, data, ret)))
867 ret = -EFAULT;
868 }
ddf8abd2
MN
869 }
870 }
871
872 mutex_unlock(&epfile->mutex);
873error:
874 kfree(data);
875 return ret;
876}
877
ddf8abd2
MN
878static ssize_t
879ffs_epfile_write(struct file *file, const char __user *buf, size_t len,
880 loff_t *ptr)
881{
882 ENTER();
883
884 return ffs_epfile_io(file, (char __user *)buf, len, 0);
885}
886
887static ssize_t
888ffs_epfile_read(struct file *file, char __user *buf, size_t len, loff_t *ptr)
889{
890 ENTER();
891
892 return ffs_epfile_io(file, buf, len, 1);
893}
894
895static int
896ffs_epfile_open(struct inode *inode, struct file *file)
897{
898 struct ffs_epfile *epfile = inode->i_private;
899
900 ENTER();
901
902 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
903 return -ENODEV;
904
905 file->private_data = epfile;
906 ffs_data_opened(epfile->ffs);
907
908 return 0;
909}
910
911static int
912ffs_epfile_release(struct inode *inode, struct file *file)
913{
914 struct ffs_epfile *epfile = inode->i_private;
915
916 ENTER();
917
918 ffs_data_closed(epfile->ffs);
919
920 return 0;
921}
922
ddf8abd2
MN
923static long ffs_epfile_ioctl(struct file *file, unsigned code,
924 unsigned long value)
925{
926 struct ffs_epfile *epfile = file->private_data;
927 int ret;
928
929 ENTER();
930
931 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
932 return -ENODEV;
933
934 spin_lock_irq(&epfile->ffs->eps_lock);
935 if (likely(epfile->ep)) {
936 switch (code) {
937 case FUNCTIONFS_FIFO_STATUS:
938 ret = usb_ep_fifo_status(epfile->ep->ep);
939 break;
940 case FUNCTIONFS_FIFO_FLUSH:
941 usb_ep_fifo_flush(epfile->ep->ep);
942 ret = 0;
943 break;
944 case FUNCTIONFS_CLEAR_HALT:
945 ret = usb_ep_clear_halt(epfile->ep->ep);
946 break;
947 case FUNCTIONFS_ENDPOINT_REVMAP:
948 ret = epfile->ep->num;
949 break;
950 default:
951 ret = -ENOTTY;
952 }
953 } else {
954 ret = -ENODEV;
955 }
956 spin_unlock_irq(&epfile->ffs->eps_lock);
957
958 return ret;
959}
960
ddf8abd2 961static const struct file_operations ffs_epfile_operations = {
ddf8abd2
MN
962 .llseek = no_llseek,
963
964 .open = ffs_epfile_open,
965 .write = ffs_epfile_write,
966 .read = ffs_epfile_read,
967 .release = ffs_epfile_release,
968 .unlocked_ioctl = ffs_epfile_ioctl,
969};
970
971
ddf8abd2
MN
972/* File system and super block operations ***********************************/
973
974/*
5ab54cf7 975 * Mounting the file system creates a controller file, used first for
ddf8abd2
MN
976 * function configuration then later for event monitoring.
977 */
978
ddf8abd2
MN
979static struct inode *__must_check
980ffs_sb_make_inode(struct super_block *sb, void *data,
981 const struct file_operations *fops,
982 const struct inode_operations *iops,
983 struct ffs_file_perms *perms)
984{
985 struct inode *inode;
986
987 ENTER();
988
989 inode = new_inode(sb);
990
991 if (likely(inode)) {
992 struct timespec current_time = CURRENT_TIME;
993
12ba8d1e 994 inode->i_ino = get_next_ino();
ddf8abd2
MN
995 inode->i_mode = perms->mode;
996 inode->i_uid = perms->uid;
997 inode->i_gid = perms->gid;
998 inode->i_atime = current_time;
999 inode->i_mtime = current_time;
1000 inode->i_ctime = current_time;
1001 inode->i_private = data;
1002 if (fops)
1003 inode->i_fop = fops;
1004 if (iops)
1005 inode->i_op = iops;
1006 }
1007
1008 return inode;
1009}
1010
ddf8abd2 1011/* Create "regular" file */
ddf8abd2
MN
1012static struct inode *ffs_sb_create_file(struct super_block *sb,
1013 const char *name, void *data,
1014 const struct file_operations *fops,
1015 struct dentry **dentry_p)
1016{
1017 struct ffs_data *ffs = sb->s_fs_info;
1018 struct dentry *dentry;
1019 struct inode *inode;
1020
1021 ENTER();
1022
1023 dentry = d_alloc_name(sb->s_root, name);
1024 if (unlikely(!dentry))
1025 return NULL;
1026
1027 inode = ffs_sb_make_inode(sb, data, fops, NULL, &ffs->file_perms);
1028 if (unlikely(!inode)) {
1029 dput(dentry);
1030 return NULL;
1031 }
1032
1033 d_add(dentry, inode);
1034 if (dentry_p)
1035 *dentry_p = dentry;
1036
1037 return inode;
1038}
1039
ddf8abd2 1040/* Super block */
ddf8abd2
MN
1041static const struct super_operations ffs_sb_operations = {
1042 .statfs = simple_statfs,
1043 .drop_inode = generic_delete_inode,
1044};
1045
1046struct ffs_sb_fill_data {
1047 struct ffs_file_perms perms;
1048 umode_t root_mode;
1049 const char *dev_name;
6fa3eb70
S
1050 union {
1051 /* set by ffs_fs_mount(), read by ffs_sb_fill() */
1052 void *private_data;
1053 /* set by ffs_sb_fill(), read by ffs_fs_mount */
1054 struct ffs_data *ffs_data;
1055 };
ddf8abd2
MN
1056};
1057
1058static int ffs_sb_fill(struct super_block *sb, void *_data, int silent)
1059{
1060 struct ffs_sb_fill_data *data = _data;
1061 struct inode *inode;
6fa3eb70 1062 struct ffs_data *ffs;
ddf8abd2
MN
1063
1064 ENTER();
1065
6fa3eb70
S
1066 /* Initialise data */
1067 ffs = ffs_data_new();
1068 if (unlikely(!ffs))
1069 goto Enomem;
1070
ddf8abd2 1071 ffs->sb = sb;
6fa3eb70
S
1072 ffs->dev_name = kstrdup(data->dev_name, GFP_KERNEL);
1073 if (unlikely(!ffs->dev_name))
1074 goto Enomem;
1075 ffs->file_perms = data->perms;
1076 ffs->private_data = data->private_data;
1077
1078 /* used by the caller of this function */
1079 data->ffs_data = ffs;
1080
ddf8abd2
MN
1081 sb->s_fs_info = ffs;
1082 sb->s_blocksize = PAGE_CACHE_SIZE;
1083 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1084 sb->s_magic = FUNCTIONFS_MAGIC;
1085 sb->s_op = &ffs_sb_operations;
1086 sb->s_time_gran = 1;
1087
1088 /* Root inode */
1089 data->perms.mode = data->root_mode;
1090 inode = ffs_sb_make_inode(sb, NULL,
1091 &simple_dir_operations,
1092 &simple_dir_inode_operations,
1093 &data->perms);
48fde701
AV
1094 sb->s_root = d_make_root(inode);
1095 if (unlikely(!sb->s_root))
6fa3eb70 1096 goto Enomem;
ddf8abd2
MN
1097
1098 /* EP0 file */
1099 if (unlikely(!ffs_sb_create_file(sb, "ep0", ffs,
1100 &ffs_ep0_operations, NULL)))
6fa3eb70 1101 goto Enomem;
ddf8abd2
MN
1102
1103 return 0;
6fa3eb70
S
1104
1105Enomem:
1106 return -ENOMEM;
ddf8abd2
MN
1107}
1108
ddf8abd2
MN
1109static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts)
1110{
1111 ENTER();
1112
1113 if (!opts || !*opts)
1114 return 0;
1115
1116 for (;;) {
ddf8abd2 1117 unsigned long value;
afd2e186 1118 char *eq, *comma;
ddf8abd2
MN
1119
1120 /* Option limit */
1121 comma = strchr(opts, ',');
1122 if (comma)
1123 *comma = 0;
1124
1125 /* Value limit */
1126 eq = strchr(opts, '=');
1127 if (unlikely(!eq)) {
aa02f172 1128 pr_err("'=' missing in %s\n", opts);
ddf8abd2
MN
1129 return -EINVAL;
1130 }
1131 *eq = 0;
1132
1133 /* Parse value */
afd2e186 1134 if (kstrtoul(eq + 1, 0, &value)) {
aa02f172 1135 pr_err("%s: invalid value: %s\n", opts, eq + 1);
ddf8abd2
MN
1136 return -EINVAL;
1137 }
1138
1139 /* Interpret option */
1140 switch (eq - opts) {
1141 case 5:
1142 if (!memcmp(opts, "rmode", 5))
1143 data->root_mode = (value & 0555) | S_IFDIR;
1144 else if (!memcmp(opts, "fmode", 5))
1145 data->perms.mode = (value & 0666) | S_IFREG;
1146 else
1147 goto invalid;
1148 break;
1149
1150 case 4:
1151 if (!memcmp(opts, "mode", 4)) {
1152 data->root_mode = (value & 0555) | S_IFDIR;
1153 data->perms.mode = (value & 0666) | S_IFREG;
1154 } else {
1155 goto invalid;
1156 }
1157 break;
1158
1159 case 3:
b9b73f7c
EB
1160 if (!memcmp(opts, "uid", 3)) {
1161 data->perms.uid = make_kuid(current_user_ns(), value);
1162 if (!uid_valid(data->perms.uid)) {
1163 pr_err("%s: unmapped value: %lu\n", opts, value);
1164 return -EINVAL;
1165 }
b8100750 1166 } else if (!memcmp(opts, "gid", 3)) {
b9b73f7c
EB
1167 data->perms.gid = make_kgid(current_user_ns(), value);
1168 if (!gid_valid(data->perms.gid)) {
1169 pr_err("%s: unmapped value: %lu\n", opts, value);
1170 return -EINVAL;
1171 }
b8100750 1172 } else {
ddf8abd2 1173 goto invalid;
b8100750 1174 }
ddf8abd2
MN
1175 break;
1176
1177 default:
1178invalid:
aa02f172 1179 pr_err("%s: invalid option\n", opts);
ddf8abd2
MN
1180 return -EINVAL;
1181 }
1182
1183 /* Next iteration */
1184 if (!comma)
1185 break;
1186 opts = comma + 1;
1187 }
1188
1189 return 0;
1190}
1191
ddf8abd2
MN
1192/* "mount -t functionfs dev_name /dev/function" ends up here */
1193
fc14f2fe
AV
1194static struct dentry *
1195ffs_fs_mount(struct file_system_type *t, int flags,
1196 const char *dev_name, void *opts)
ddf8abd2
MN
1197{
1198 struct ffs_sb_fill_data data = {
1199 .perms = {
1200 .mode = S_IFREG | 0600,
b9b73f7c
EB
1201 .uid = GLOBAL_ROOT_UID,
1202 .gid = GLOBAL_ROOT_GID,
ddf8abd2
MN
1203 },
1204 .root_mode = S_IFDIR | 0500,
1205 };
581791f5 1206 struct dentry *rv;
ddf8abd2 1207 int ret;
581791f5 1208 void *ffs_dev;
ddf8abd2
MN
1209
1210 ENTER();
1211
ddf8abd2
MN
1212 ret = ffs_fs_parse_opts(&data, opts);
1213 if (unlikely(ret < 0))
fc14f2fe 1214 return ERR_PTR(ret);
ddf8abd2 1215
581791f5 1216 ffs_dev = functionfs_acquire_dev_callback(dev_name);
6fa3eb70
S
1217 if (IS_ERR(ffs_dev))
1218 return ffs_dev;
581791f5 1219
6fa3eb70
S
1220 data.dev_name = dev_name;
1221 data.private_data = ffs_dev;
581791f5 1222 rv = mount_nodev(t, flags, &data, ffs_sb_fill);
6fa3eb70
S
1223
1224 /* data.ffs_data is set by ffs_sb_fill */
1225 if (IS_ERR(rv))
581791f5 1226 functionfs_release_dev_callback(data.ffs_data);
6fa3eb70 1227
581791f5 1228 return rv;
ddf8abd2
MN
1229}
1230
1231static void
1232ffs_fs_kill_sb(struct super_block *sb)
1233{
ddf8abd2
MN
1234 ENTER();
1235
1236 kill_litter_super(sb);
581791f5
AP
1237 if (sb->s_fs_info) {
1238 functionfs_release_dev_callback(sb->s_fs_info);
5b5f9560 1239 ffs_data_put(sb->s_fs_info);
581791f5 1240 }
ddf8abd2
MN
1241}
1242
1243static struct file_system_type ffs_fs_type = {
1244 .owner = THIS_MODULE,
1245 .name = "functionfs",
fc14f2fe 1246 .mount = ffs_fs_mount,
ddf8abd2
MN
1247 .kill_sb = ffs_fs_kill_sb,
1248};
7f78e035 1249MODULE_ALIAS_FS("functionfs");
ddf8abd2
MN
1250
1251
ddf8abd2
MN
1252/* Driver's main init/cleanup functions *************************************/
1253
ddf8abd2
MN
1254static int functionfs_init(void)
1255{
1256 int ret;
1257
1258 ENTER();
1259
1260 ret = register_filesystem(&ffs_fs_type);
1261 if (likely(!ret))
aa02f172 1262 pr_info("file system registered\n");
ddf8abd2 1263 else
aa02f172 1264 pr_err("failed registering file system (%d)\n", ret);
ddf8abd2
MN
1265
1266 return ret;
1267}
1268
1269static void functionfs_cleanup(void)
1270{
1271 ENTER();
1272
aa02f172 1273 pr_info("unloading\n");
ddf8abd2
MN
1274 unregister_filesystem(&ffs_fs_type);
1275}
1276
1277
ddf8abd2
MN
1278/* ffs_data and ffs_function construction and destruction code **************/
1279
1280static void ffs_data_clear(struct ffs_data *ffs);
1281static void ffs_data_reset(struct ffs_data *ffs);
1282
ddf8abd2
MN
1283static void ffs_data_get(struct ffs_data *ffs)
1284{
1285 ENTER();
1286
1287 atomic_inc(&ffs->ref);
1288}
1289
1290static void ffs_data_opened(struct ffs_data *ffs)
1291{
1292 ENTER();
1293
1294 atomic_inc(&ffs->ref);
1295 atomic_inc(&ffs->opened);
1296}
1297
1298static void ffs_data_put(struct ffs_data *ffs)
1299{
1300 ENTER();
1301
1302 if (unlikely(atomic_dec_and_test(&ffs->ref))) {
aa02f172 1303 pr_info("%s(): freeing\n", __func__);
ddf8abd2 1304 ffs_data_clear(ffs);
647d5580 1305 BUG_ON(waitqueue_active(&ffs->ev.waitq) ||
ddf8abd2 1306 waitqueue_active(&ffs->ep0req_completion.wait));
581791f5 1307 kfree(ffs->dev_name);
ddf8abd2
MN
1308 kfree(ffs);
1309 }
1310}
1311
ddf8abd2
MN
1312static void ffs_data_closed(struct ffs_data *ffs)
1313{
1314 ENTER();
1315
1316 if (atomic_dec_and_test(&ffs->opened)) {
1317 ffs->state = FFS_CLOSING;
1318 ffs_data_reset(ffs);
1319 }
1320
1321 ffs_data_put(ffs);
1322}
1323
ddf8abd2
MN
1324static struct ffs_data *ffs_data_new(void)
1325{
1326 struct ffs_data *ffs = kzalloc(sizeof *ffs, GFP_KERNEL);
1327 if (unlikely(!ffs))
1328 return 0;
1329
1330 ENTER();
1331
1332 atomic_set(&ffs->ref, 1);
1333 atomic_set(&ffs->opened, 0);
1334 ffs->state = FFS_READ_DESCRIPTORS;
1335 mutex_init(&ffs->mutex);
1336 spin_lock_init(&ffs->eps_lock);
1337 init_waitqueue_head(&ffs->ev.waitq);
1338 init_completion(&ffs->ep0req_completion);
1339
1340 /* XXX REVISIT need to update it in some places, or do we? */
1341 ffs->ev.can_stall = 1;
1342
1343 return ffs;
1344}
1345
ddf8abd2
MN
1346static void ffs_data_clear(struct ffs_data *ffs)
1347{
1348 ENTER();
1349
1350 if (test_and_clear_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags))
1351 functionfs_closed_callback(ffs);
1352
1353 BUG_ON(ffs->gadget);
1354
1355 if (ffs->epfiles)
1356 ffs_epfiles_destroy(ffs->epfiles, ffs->eps_count);
1357
1358 kfree(ffs->raw_descs);
1359 kfree(ffs->raw_strings);
1360 kfree(ffs->stringtabs);
1361}
1362
ddf8abd2
MN
1363static void ffs_data_reset(struct ffs_data *ffs)
1364{
1365 ENTER();
1366
1367 ffs_data_clear(ffs);
1368
1369 ffs->epfiles = NULL;
1370 ffs->raw_descs = NULL;
1371 ffs->raw_strings = NULL;
1372 ffs->stringtabs = NULL;
1373
1374 ffs->raw_descs_length = 0;
6fa3eb70
S
1375 ffs->raw_fs_hs_descs_length = 0;
1376 ffs->raw_ss_descs_offset = 0;
1377 ffs->raw_ss_descs_length = 0;
ddf8abd2
MN
1378 ffs->fs_descs_count = 0;
1379 ffs->hs_descs_count = 0;
6fa3eb70 1380 ffs->ss_descs_count = 0;
ddf8abd2
MN
1381
1382 ffs->strings_count = 0;
1383 ffs->interfaces_count = 0;
1384 ffs->eps_count = 0;
1385
1386 ffs->ev.count = 0;
1387
1388 ffs->state = FFS_READ_DESCRIPTORS;
1389 ffs->setup_state = FFS_NO_SETUP;
1390 ffs->flags = 0;
1391}
1392
1393
1394static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev)
1395{
fd7c9a00
MN
1396 struct usb_gadget_strings **lang;
1397 int first_id;
ddf8abd2
MN
1398
1399 ENTER();
1400
1401 if (WARN_ON(ffs->state != FFS_ACTIVE
1402 || test_and_set_bit(FFS_FL_BOUND, &ffs->flags)))
1403 return -EBADFD;
1404
fd7c9a00
MN
1405 first_id = usb_string_ids_n(cdev, ffs->strings_count);
1406 if (unlikely(first_id < 0))
1407 return first_id;
ddf8abd2
MN
1408
1409 ffs->ep0req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
1410 if (unlikely(!ffs->ep0req))
1411 return -ENOMEM;
1412 ffs->ep0req->complete = ffs_ep0_complete;
1413 ffs->ep0req->context = ffs;
1414
fd7c9a00 1415 lang = ffs->stringtabs;
6fa3eb70
S
1416 for (lang = ffs->stringtabs; *lang; ++lang) {
1417 struct usb_string *str = (*lang)->strings;
1418 int id = first_id;
1419 for (; str->s; ++id, ++str)
1420 str->id = id;
ddf8abd2
MN
1421 }
1422
1423 ffs->gadget = cdev->gadget;
fd7c9a00 1424 ffs_data_get(ffs);
ddf8abd2
MN
1425 return 0;
1426}
1427
ddf8abd2
MN
1428static void functionfs_unbind(struct ffs_data *ffs)
1429{
1430 ENTER();
1431
1432 if (!WARN_ON(!ffs->gadget)) {
1433 usb_ep_free_request(ffs->gadget->ep0, ffs->ep0req);
1434 ffs->ep0req = NULL;
1435 ffs->gadget = NULL;
1436 ffs_data_put(ffs);
e2190a97 1437 clear_bit(FFS_FL_BOUND, &ffs->flags);
ddf8abd2
MN
1438 }
1439}
1440
ddf8abd2
MN
1441static int ffs_epfiles_create(struct ffs_data *ffs)
1442{
1443 struct ffs_epfile *epfile, *epfiles;
1444 unsigned i, count;
1445
1446 ENTER();
1447
1448 count = ffs->eps_count;
9823a525 1449 epfiles = kcalloc(count, sizeof(*epfiles), GFP_KERNEL);
ddf8abd2
MN
1450 if (!epfiles)
1451 return -ENOMEM;
1452
1453 epfile = epfiles;
1454 for (i = 1; i <= count; ++i, ++epfile) {
1455 epfile->ffs = ffs;
1456 mutex_init(&epfile->mutex);
1457 init_waitqueue_head(&epfile->wait);
1458 sprintf(epfiles->name, "ep%u", i);
1459 if (!unlikely(ffs_sb_create_file(ffs->sb, epfiles->name, epfile,
1460 &ffs_epfile_operations,
1461 &epfile->dentry))) {
1462 ffs_epfiles_destroy(epfiles, i - 1);
1463 return -ENOMEM;
1464 }
1465 }
1466
1467 ffs->epfiles = epfiles;
1468 return 0;
1469}
1470
ddf8abd2
MN
1471static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count)
1472{
1473 struct ffs_epfile *epfile = epfiles;
1474
1475 ENTER();
1476
1477 for (; count; --count, ++epfile) {
1478 BUG_ON(mutex_is_locked(&epfile->mutex) ||
1479 waitqueue_active(&epfile->wait));
1480 if (epfile->dentry) {
1481 d_delete(epfile->dentry);
1482 dput(epfile->dentry);
1483 epfile->dentry = NULL;
1484 }
1485 }
1486
1487 kfree(epfiles);
1488}
1489
7898aee1
MN
1490static int functionfs_bind_config(struct usb_composite_dev *cdev,
1491 struct usb_configuration *c,
1492 struct ffs_data *ffs)
ddf8abd2
MN
1493{
1494 struct ffs_function *func;
1495 int ret;
1496
1497 ENTER();
1498
1499 func = kzalloc(sizeof *func, GFP_KERNEL);
1500 if (unlikely(!func))
1501 return -ENOMEM;
1502
1503 func->function.name = "Function FS Gadget";
1504 func->function.strings = ffs->stringtabs;
1505
1506 func->function.bind = ffs_func_bind;
1507 func->function.unbind = ffs_func_unbind;
1508 func->function.set_alt = ffs_func_set_alt;
ddf8abd2
MN
1509 func->function.disable = ffs_func_disable;
1510 func->function.setup = ffs_func_setup;
1511 func->function.suspend = ffs_func_suspend;
1512 func->function.resume = ffs_func_resume;
1513
1514 func->conf = c;
1515 func->gadget = cdev->gadget;
1516 func->ffs = ffs;
1517 ffs_data_get(ffs);
1518
1519 ret = usb_add_function(c, &func->function);
1520 if (unlikely(ret))
1521 ffs_func_free(func);
1522
1523 return ret;
1524}
1525
1526static void ffs_func_free(struct ffs_function *func)
1527{
4f06539f
PK
1528 struct ffs_ep *ep = func->eps;
1529 unsigned count = func->ffs->eps_count;
1530 unsigned long flags;
1531
ddf8abd2
MN
1532 ENTER();
1533
4f06539f
PK
1534 /* cleanup after autoconfig */
1535 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1536 do {
1537 if (ep->ep && ep->req)
1538 usb_ep_free_request(ep->ep, ep->req);
1539 ep->req = NULL;
1540 ++ep;
1541 } while (--count);
1542 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1543
ddf8abd2
MN
1544 ffs_data_put(func->ffs);
1545
1546 kfree(func->eps);
5ab54cf7
MN
1547 /*
1548 * eps and interfaces_nums are allocated in the same chunk so
ddf8abd2 1549 * only one free is required. Descriptors are also allocated
5ab54cf7
MN
1550 * in the same chunk.
1551 */
ddf8abd2
MN
1552
1553 kfree(func);
1554}
1555
ddf8abd2
MN
1556static void ffs_func_eps_disable(struct ffs_function *func)
1557{
1558 struct ffs_ep *ep = func->eps;
1559 struct ffs_epfile *epfile = func->ffs->epfiles;
1560 unsigned count = func->ffs->eps_count;
1561 unsigned long flags;
1562
1563 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1564 do {
1565 /* pending requests get nuked */
1566 if (likely(ep->ep))
1567 usb_ep_disable(ep->ep);
1568 epfile->ep = NULL;
1569
1570 ++ep;
1571 ++epfile;
1572 } while (--count);
1573 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1574}
1575
1576static int ffs_func_eps_enable(struct ffs_function *func)
1577{
1578 struct ffs_data *ffs = func->ffs;
1579 struct ffs_ep *ep = func->eps;
1580 struct ffs_epfile *epfile = ffs->epfiles;
1581 unsigned count = ffs->eps_count;
1582 unsigned long flags;
1583 int ret = 0;
1584
1585 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1586 do {
1587 struct usb_endpoint_descriptor *ds;
6fa3eb70
S
1588 int desc_idx;
1589
1590 if (ffs->gadget->speed == USB_SPEED_SUPER)
1591 desc_idx = 2;
1592 else if (ffs->gadget->speed == USB_SPEED_HIGH)
1593 desc_idx = 1;
1594 else
1595 desc_idx = 0;
1596
1597 ds = ep->descs[desc_idx];
1598 if (!ds) {
1599 ret = -EINVAL;
1600 break;
1601 }
ddf8abd2
MN
1602
1603 ep->ep->driver_data = ep;
72c973dd
TB
1604 ep->ep->desc = ds;
1605 ret = usb_ep_enable(ep->ep);
ddf8abd2
MN
1606 if (likely(!ret)) {
1607 epfile->ep = ep;
1608 epfile->in = usb_endpoint_dir_in(ds);
1609 epfile->isoc = usb_endpoint_xfer_isoc(ds);
1610 } else {
1611 break;
1612 }
1613
1614 wake_up(&epfile->wait);
1615
1616 ++ep;
1617 ++epfile;
1618 } while (--count);
1619 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1620
1621 return ret;
1622}
1623
1624
1625/* Parsing and building descriptors and strings *****************************/
1626
5ab54cf7
MN
1627/*
1628 * This validates if data pointed by data is a valid USB descriptor as
ddf8abd2 1629 * well as record how many interfaces, endpoints and strings are
5ab54cf7
MN
1630 * required by given configuration. Returns address after the
1631 * descriptor or NULL if data is invalid.
1632 */
ddf8abd2
MN
1633
1634enum ffs_entity_type {
1635 FFS_DESCRIPTOR, FFS_INTERFACE, FFS_STRING, FFS_ENDPOINT
1636};
1637
1638typedef int (*ffs_entity_callback)(enum ffs_entity_type entity,
1639 u8 *valuep,
1640 struct usb_descriptor_header *desc,
1641 void *priv);
1642
1643static int __must_check ffs_do_desc(char *data, unsigned len,
1644 ffs_entity_callback entity, void *priv)
1645{
1646 struct usb_descriptor_header *_ds = (void *)data;
1647 u8 length;
1648 int ret;
1649
1650 ENTER();
1651
1652 /* At least two bytes are required: length and type */
1653 if (len < 2) {
aa02f172 1654 pr_vdebug("descriptor too short\n");
ddf8abd2
MN
1655 return -EINVAL;
1656 }
1657
1658 /* If we have at least as many bytes as the descriptor takes? */
1659 length = _ds->bLength;
1660 if (len < length) {
aa02f172 1661 pr_vdebug("descriptor longer then available data\n");
ddf8abd2
MN
1662 return -EINVAL;
1663 }
1664
1665#define __entity_check_INTERFACE(val) 1
1666#define __entity_check_STRING(val) (val)
1667#define __entity_check_ENDPOINT(val) ((val) & USB_ENDPOINT_NUMBER_MASK)
1668#define __entity(type, val) do { \
aa02f172 1669 pr_vdebug("entity " #type "(%02x)\n", (val)); \
ddf8abd2 1670 if (unlikely(!__entity_check_ ##type(val))) { \
aa02f172 1671 pr_vdebug("invalid entity's value\n"); \
ddf8abd2
MN
1672 return -EINVAL; \
1673 } \
1674 ret = entity(FFS_ ##type, &val, _ds, priv); \
1675 if (unlikely(ret < 0)) { \
aa02f172 1676 pr_debug("entity " #type "(%02x); ret = %d\n", \
d8df0b61 1677 (val), ret); \
ddf8abd2
MN
1678 return ret; \
1679 } \
1680 } while (0)
1681
1682 /* Parse descriptor depending on type. */
1683 switch (_ds->bDescriptorType) {
1684 case USB_DT_DEVICE:
1685 case USB_DT_CONFIG:
1686 case USB_DT_STRING:
1687 case USB_DT_DEVICE_QUALIFIER:
1688 /* function can't have any of those */
aa02f172 1689 pr_vdebug("descriptor reserved for gadget: %d\n",
5ab54cf7 1690 _ds->bDescriptorType);
ddf8abd2
MN
1691 return -EINVAL;
1692
1693 case USB_DT_INTERFACE: {
1694 struct usb_interface_descriptor *ds = (void *)_ds;
aa02f172 1695 pr_vdebug("interface descriptor\n");
ddf8abd2
MN
1696 if (length != sizeof *ds)
1697 goto inv_length;
1698
1699 __entity(INTERFACE, ds->bInterfaceNumber);
1700 if (ds->iInterface)
1701 __entity(STRING, ds->iInterface);
1702 }
1703 break;
1704
1705 case USB_DT_ENDPOINT: {
1706 struct usb_endpoint_descriptor *ds = (void *)_ds;
aa02f172 1707 pr_vdebug("endpoint descriptor\n");
ddf8abd2
MN
1708 if (length != USB_DT_ENDPOINT_SIZE &&
1709 length != USB_DT_ENDPOINT_AUDIO_SIZE)
1710 goto inv_length;
1711 __entity(ENDPOINT, ds->bEndpointAddress);
1712 }
1713 break;
1714
560f1187
KB
1715 case HID_DT_HID:
1716 pr_vdebug("hid descriptor\n");
1717 if (length != sizeof(struct hid_descriptor))
1718 goto inv_length;
1719 break;
1720
ddf8abd2
MN
1721 case USB_DT_OTG:
1722 if (length != sizeof(struct usb_otg_descriptor))
1723 goto inv_length;
1724 break;
1725
1726 case USB_DT_INTERFACE_ASSOCIATION: {
1727 struct usb_interface_assoc_descriptor *ds = (void *)_ds;
aa02f172 1728 pr_vdebug("interface association descriptor\n");
ddf8abd2
MN
1729 if (length != sizeof *ds)
1730 goto inv_length;
1731 if (ds->iFunction)
1732 __entity(STRING, ds->iFunction);
1733 }
1734 break;
1735
6fa3eb70
S
1736 case USB_DT_SS_ENDPOINT_COMP:
1737 pr_vdebug("EP SS companion descriptor\n");
1738 if (length != sizeof(struct usb_ss_ep_comp_descriptor))
1739 goto inv_length;
1740 break;
1741
ddf8abd2
MN
1742 case USB_DT_OTHER_SPEED_CONFIG:
1743 case USB_DT_INTERFACE_POWER:
1744 case USB_DT_DEBUG:
1745 case USB_DT_SECURITY:
1746 case USB_DT_CS_RADIO_CONTROL:
1747 /* TODO */
aa02f172 1748 pr_vdebug("unimplemented descriptor: %d\n", _ds->bDescriptorType);
ddf8abd2
MN
1749 return -EINVAL;
1750
1751 default:
1752 /* We should never be here */
aa02f172 1753 pr_vdebug("unknown descriptor: %d\n", _ds->bDescriptorType);
ddf8abd2
MN
1754 return -EINVAL;
1755
5ab54cf7 1756inv_length:
aa02f172 1757 pr_vdebug("invalid length: %d (descriptor %d)\n",
d8df0b61 1758 _ds->bLength, _ds->bDescriptorType);
ddf8abd2
MN
1759 return -EINVAL;
1760 }
1761
1762#undef __entity
1763#undef __entity_check_DESCRIPTOR
1764#undef __entity_check_INTERFACE
1765#undef __entity_check_STRING
1766#undef __entity_check_ENDPOINT
1767
1768 return length;
1769}
1770
ddf8abd2
MN
1771static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len,
1772 ffs_entity_callback entity, void *priv)
1773{
1774 const unsigned _len = len;
1775 unsigned long num = 0;
1776
1777 ENTER();
1778
1779 for (;;) {
1780 int ret;
1781
1782 if (num == count)
1783 data = NULL;
1784
5ab54cf7 1785 /* Record "descriptor" entity */
ddf8abd2
MN
1786 ret = entity(FFS_DESCRIPTOR, (u8 *)num, (void *)data, priv);
1787 if (unlikely(ret < 0)) {
aa02f172 1788 pr_debug("entity DESCRIPTOR(%02lx); ret = %d\n",
d8df0b61 1789 num, ret);
ddf8abd2
MN
1790 return ret;
1791 }
1792
1793 if (!data)
1794 return _len - len;
1795
1796 ret = ffs_do_desc(data, len, entity, priv);
1797 if (unlikely(ret < 0)) {
aa02f172 1798 pr_debug("%s returns %d\n", __func__, ret);
ddf8abd2
MN
1799 return ret;
1800 }
1801
1802 len -= ret;
1803 data += ret;
1804 ++num;
1805 }
1806}
1807
ddf8abd2
MN
1808static int __ffs_data_do_entity(enum ffs_entity_type type,
1809 u8 *valuep, struct usb_descriptor_header *desc,
1810 void *priv)
1811{
1812 struct ffs_data *ffs = priv;
1813
1814 ENTER();
1815
1816 switch (type) {
1817 case FFS_DESCRIPTOR:
1818 break;
1819
1820 case FFS_INTERFACE:
5ab54cf7
MN
1821 /*
1822 * Interfaces are indexed from zero so if we
ddf8abd2 1823 * encountered interface "n" then there are at least
5ab54cf7
MN
1824 * "n+1" interfaces.
1825 */
ddf8abd2
MN
1826 if (*valuep >= ffs->interfaces_count)
1827 ffs->interfaces_count = *valuep + 1;
1828 break;
1829
1830 case FFS_STRING:
5ab54cf7
MN
1831 /*
1832 * Strings are indexed from 1 (0 is magic ;) reserved
1833 * for languages list or some such)
1834 */
ddf8abd2
MN
1835 if (*valuep > ffs->strings_count)
1836 ffs->strings_count = *valuep;
1837 break;
1838
1839 case FFS_ENDPOINT:
1840 /* Endpoints are indexed from 1 as well. */
1841 if ((*valuep & USB_ENDPOINT_NUMBER_MASK) > ffs->eps_count)
1842 ffs->eps_count = (*valuep & USB_ENDPOINT_NUMBER_MASK);
1843 break;
1844 }
1845
1846 return 0;
1847}
1848
ddf8abd2
MN
1849static int __ffs_data_got_descs(struct ffs_data *ffs,
1850 char *const _data, size_t len)
1851{
6fa3eb70
S
1852 unsigned fs_count, hs_count, ss_count = 0;
1853 int fs_len, hs_len, ss_len, ss_magic, ret = -EINVAL;
ddf8abd2
MN
1854 char *data = _data;
1855
1856 ENTER();
1857
1858 if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_DESCRIPTORS_MAGIC ||
1859 get_unaligned_le32(data + 4) != len))
1860 goto error;
1861 fs_count = get_unaligned_le32(data + 8);
1862 hs_count = get_unaligned_le32(data + 12);
1863
ddf8abd2
MN
1864 data += 16;
1865 len -= 16;
1866
1867 if (likely(fs_count)) {
1868 fs_len = ffs_do_descs(fs_count, data, len,
1869 __ffs_data_do_entity, ffs);
1870 if (unlikely(fs_len < 0)) {
1871 ret = fs_len;
1872 goto error;
1873 }
1874
1875 data += fs_len;
1876 len -= fs_len;
1877 } else {
1878 fs_len = 0;
1879 }
1880
1881 if (likely(hs_count)) {
6fa3eb70 1882 hs_len = ffs_do_descs(hs_count, data, len,
ddf8abd2 1883 __ffs_data_do_entity, ffs);
6fa3eb70
S
1884 if (unlikely(hs_len < 0)) {
1885 ret = hs_len;
ddf8abd2 1886 goto error;
6fa3eb70
S
1887 }
1888 } else {
1889 hs_len = 0;
1890 }
1891
1892 if ((len >= hs_len + 8)) {
1893 /* Check SS_MAGIC for presence of ss_descs and get SS_COUNT */
1894 ss_magic = get_unaligned_le32(data + hs_len);
1895 if (ss_magic != FUNCTIONFS_SS_DESC_MAGIC)
1896 goto einval;
1897
1898 ss_count = get_unaligned_le32(data + hs_len + 4);
1899 data += hs_len + 8;
1900 len -= hs_len + 8;
ddf8abd2 1901 } else {
6fa3eb70
S
1902 data += hs_len;
1903 len -= hs_len;
1904 }
1905
1906 if (!fs_count && !hs_count && !ss_count)
1907 goto einval;
1908
1909 if (ss_count) {
1910 ss_len = ffs_do_descs(ss_count, data, len,
1911 __ffs_data_do_entity, ffs);
1912 if (unlikely(ss_len < 0)) {
1913 ret = ss_len;
1914 goto error;
1915 }
1916 ret = ss_len;
1917 } else {
1918 ss_len = 0;
ddf8abd2
MN
1919 ret = 0;
1920 }
1921
1922 if (unlikely(len != ret))
1923 goto einval;
1924
6fa3eb70
S
1925 ffs->raw_fs_hs_descs_length = fs_len + hs_len;
1926 ffs->raw_ss_descs_length = ss_len;
1927 ffs->raw_descs_length = ffs->raw_fs_hs_descs_length + ss_len;
1928 ffs->raw_descs = _data;
1929 ffs->fs_descs_count = fs_count;
1930 ffs->hs_descs_count = hs_count;
1931 ffs->ss_descs_count = ss_count;
1932 if (ffs->ss_descs_count)
1933 ffs->raw_ss_descs_offset = 16 + ffs->raw_fs_hs_descs_length + 8;
ddf8abd2
MN
1934
1935 return 0;
1936
1937einval:
1938 ret = -EINVAL;
1939error:
1940 kfree(_data);
1941 return ret;
1942}
1943
ddf8abd2
MN
1944static int __ffs_data_got_strings(struct ffs_data *ffs,
1945 char *const _data, size_t len)
1946{
1947 u32 str_count, needed_count, lang_count;
1948 struct usb_gadget_strings **stringtabs, *t;
1949 struct usb_string *strings, *s;
1950 const char *data = _data;
1951
1952 ENTER();
1953
1954 if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_STRINGS_MAGIC ||
1955 get_unaligned_le32(data + 4) != len))
1956 goto error;
1957 str_count = get_unaligned_le32(data + 8);
1958 lang_count = get_unaligned_le32(data + 12);
1959
1960 /* if one is zero the other must be zero */
1961 if (unlikely(!str_count != !lang_count))
1962 goto error;
1963
1964 /* Do we have at least as many strings as descriptors need? */
1965 needed_count = ffs->strings_count;
1966 if (unlikely(str_count < needed_count))
1967 goto error;
1968
5ab54cf7
MN
1969 /*
1970 * If we don't need any strings just return and free all
1971 * memory.
1972 */
ddf8abd2
MN
1973 if (!needed_count) {
1974 kfree(_data);
1975 return 0;
1976 }
1977
5ab54cf7 1978 /* Allocate everything in one chunk so there's less maintenance. */
ddf8abd2 1979 {
ddf8abd2
MN
1980 struct {
1981 struct usb_gadget_strings *stringtabs[lang_count + 1];
1982 struct usb_gadget_strings stringtab[lang_count];
1983 struct usb_string strings[lang_count*(needed_count+1)];
1984 } *d;
1985 unsigned i = 0;
1986
1987 d = kmalloc(sizeof *d, GFP_KERNEL);
1988 if (unlikely(!d)) {
1989 kfree(_data);
1990 return -ENOMEM;
1991 }
1992
1993 stringtabs = d->stringtabs;
1994 t = d->stringtab;
1995 i = lang_count;
1996 do {
1997 *stringtabs++ = t++;
1998 } while (--i);
1999 *stringtabs = NULL;
2000
2001 stringtabs = d->stringtabs;
2002 t = d->stringtab;
2003 s = d->strings;
2004 strings = s;
2005 }
2006
2007 /* For each language */
2008 data += 16;
2009 len -= 16;
2010
2011 do { /* lang_count > 0 so we can use do-while */
2012 unsigned needed = needed_count;
2013
2014 if (unlikely(len < 3))
2015 goto error_free;
2016 t->language = get_unaligned_le16(data);
2017 t->strings = s;
2018 ++t;
2019
2020 data += 2;
2021 len -= 2;
2022
2023 /* For each string */
2024 do { /* str_count > 0 so we can use do-while */
2025 size_t length = strnlen(data, len);
2026
2027 if (unlikely(length == len))
2028 goto error_free;
2029
5ab54cf7
MN
2030 /*
2031 * User may provide more strings then we need,
2032 * if that's the case we simply ignore the
2033 * rest
2034 */
ddf8abd2 2035 if (likely(needed)) {
5ab54cf7
MN
2036 /*
2037 * s->id will be set while adding
ddf8abd2 2038 * function to configuration so for
5ab54cf7
MN
2039 * now just leave garbage here.
2040 */
ddf8abd2
MN
2041 s->s = data;
2042 --needed;
2043 ++s;
2044 }
2045
2046 data += length + 1;
2047 len -= length + 1;
2048 } while (--str_count);
2049
2050 s->id = 0; /* terminator */
2051 s->s = NULL;
2052 ++s;
2053
2054 } while (--lang_count);
2055
2056 /* Some garbage left? */
2057 if (unlikely(len))
2058 goto error_free;
2059
2060 /* Done! */
2061 ffs->stringtabs = stringtabs;
2062 ffs->raw_strings = _data;
2063
2064 return 0;
2065
2066error_free:
2067 kfree(stringtabs);
2068error:
2069 kfree(_data);
2070 return -EINVAL;
2071}
2072
2073
ddf8abd2
MN
2074/* Events handling and management *******************************************/
2075
2076static void __ffs_event_add(struct ffs_data *ffs,
2077 enum usb_functionfs_event_type type)
2078{
2079 enum usb_functionfs_event_type rem_type1, rem_type2 = type;
2080 int neg = 0;
2081
5ab54cf7
MN
2082 /*
2083 * Abort any unhandled setup
2084 *
2085 * We do not need to worry about some cmpxchg() changing value
ddf8abd2
MN
2086 * of ffs->setup_state without holding the lock because when
2087 * state is FFS_SETUP_PENDING cmpxchg() in several places in
5ab54cf7
MN
2088 * the source does nothing.
2089 */
ddf8abd2
MN
2090 if (ffs->setup_state == FFS_SETUP_PENDING)
2091 ffs->setup_state = FFS_SETUP_CANCELED;
2092
2093 switch (type) {
2094 case FUNCTIONFS_RESUME:
2095 rem_type2 = FUNCTIONFS_SUSPEND;
5ab54cf7 2096 /* FALL THROUGH */
ddf8abd2
MN
2097 case FUNCTIONFS_SUSPEND:
2098 case FUNCTIONFS_SETUP:
2099 rem_type1 = type;
5ab54cf7 2100 /* Discard all similar events */
ddf8abd2
MN
2101 break;
2102
2103 case FUNCTIONFS_BIND:
2104 case FUNCTIONFS_UNBIND:
2105 case FUNCTIONFS_DISABLE:
2106 case FUNCTIONFS_ENABLE:
5ab54cf7 2107 /* Discard everything other then power management. */
ddf8abd2
MN
2108 rem_type1 = FUNCTIONFS_SUSPEND;
2109 rem_type2 = FUNCTIONFS_RESUME;
2110 neg = 1;
2111 break;
2112
2113 default:
2114 BUG();
2115 }
2116
2117 {
2118 u8 *ev = ffs->ev.types, *out = ev;
2119 unsigned n = ffs->ev.count;
2120 for (; n; --n, ++ev)
2121 if ((*ev == rem_type1 || *ev == rem_type2) == neg)
2122 *out++ = *ev;
2123 else
aa02f172 2124 pr_vdebug("purging event %d\n", *ev);
ddf8abd2
MN
2125 ffs->ev.count = out - ffs->ev.types;
2126 }
2127
aa02f172 2128 pr_vdebug("adding event %d\n", type);
ddf8abd2
MN
2129 ffs->ev.types[ffs->ev.count++] = type;
2130 wake_up_locked(&ffs->ev.waitq);
2131}
2132
2133static void ffs_event_add(struct ffs_data *ffs,
2134 enum usb_functionfs_event_type type)
2135{
2136 unsigned long flags;
2137 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2138 __ffs_event_add(ffs, type);
2139 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2140}
2141
2142
2143/* Bind/unbind USB function hooks *******************************************/
2144
2145static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
2146 struct usb_descriptor_header *desc,
2147 void *priv)
2148{
2149 struct usb_endpoint_descriptor *ds = (void *)desc;
2150 struct ffs_function *func = priv;
2151 struct ffs_ep *ffs_ep;
2152
5ab54cf7
MN
2153 /*
2154 * If hs_descriptors is not NULL then we are reading hs
2155 * descriptors now
2156 */
6fa3eb70
S
2157 const int is_hs = func->function.hs_descriptors != NULL;
2158 const int is_ss = func->function.ss_descriptors != NULL;
2159 unsigned ep_desc_id, idx;
ddf8abd2
MN
2160
2161 if (type != FFS_DESCRIPTOR)
2162 return 0;
2163
6fa3eb70
S
2164 if (is_ss) {
2165 func->function.ss_descriptors[(long)valuep] = desc;
2166 ep_desc_id = 2;
2167 } else if (is_hs) {
ddf8abd2 2168 func->function.hs_descriptors[(long)valuep] = desc;
6fa3eb70
S
2169 ep_desc_id = 1;
2170 } else {
10287bae 2171 func->function.fs_descriptors[(long)valuep] = desc;
6fa3eb70
S
2172 ep_desc_id = 0;
2173 }
ddf8abd2
MN
2174
2175 if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT)
2176 return 0;
2177
2178 idx = (ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK) - 1;
2179 ffs_ep = func->eps + idx;
2180
6fa3eb70 2181 if (unlikely(ffs_ep->descs[ep_desc_id])) {
aa02f172 2182 pr_vdebug("two %sspeed descriptors for EP %d\n",
6fa3eb70 2183 is_ss ? "super" : "high/full",
d8df0b61 2184 ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
ddf8abd2
MN
2185 return -EINVAL;
2186 }
6fa3eb70 2187 ffs_ep->descs[ep_desc_id] = ds;
ddf8abd2
MN
2188
2189 ffs_dump_mem(": Original ep desc", ds, ds->bLength);
2190 if (ffs_ep->ep) {
2191 ds->bEndpointAddress = ffs_ep->descs[0]->bEndpointAddress;
2192 if (!ds->wMaxPacketSize)
2193 ds->wMaxPacketSize = ffs_ep->descs[0]->wMaxPacketSize;
2194 } else {
2195 struct usb_request *req;
2196 struct usb_ep *ep;
2197
aa02f172 2198 pr_vdebug("autoconfig\n");
ddf8abd2
MN
2199 ep = usb_ep_autoconfig(func->gadget, ds);
2200 if (unlikely(!ep))
2201 return -ENOTSUPP;
cc7e6056 2202 ep->driver_data = func->eps + idx;
ddf8abd2
MN
2203
2204 req = usb_ep_alloc_request(ep, GFP_KERNEL);
2205 if (unlikely(!req))
2206 return -ENOMEM;
2207
2208 ffs_ep->ep = ep;
2209 ffs_ep->req = req;
2210 func->eps_revmap[ds->bEndpointAddress &
2211 USB_ENDPOINT_NUMBER_MASK] = idx + 1;
2212 }
2213 ffs_dump_mem(": Rewritten ep desc", ds, ds->bLength);
2214
2215 return 0;
2216}
2217
ddf8abd2
MN
2218static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep,
2219 struct usb_descriptor_header *desc,
2220 void *priv)
2221{
2222 struct ffs_function *func = priv;
2223 unsigned idx;
2224 u8 newValue;
2225
2226 switch (type) {
2227 default:
2228 case FFS_DESCRIPTOR:
2229 /* Handled in previous pass by __ffs_func_bind_do_descs() */
2230 return 0;
2231
2232 case FFS_INTERFACE:
2233 idx = *valuep;
2234 if (func->interfaces_nums[idx] < 0) {
2235 int id = usb_interface_id(func->conf, &func->function);
2236 if (unlikely(id < 0))
2237 return id;
2238 func->interfaces_nums[idx] = id;
2239 }
2240 newValue = func->interfaces_nums[idx];
2241 break;
2242
2243 case FFS_STRING:
2244 /* String' IDs are allocated when fsf_data is bound to cdev */
2245 newValue = func->ffs->stringtabs[0]->strings[*valuep - 1].id;
2246 break;
2247
2248 case FFS_ENDPOINT:
5ab54cf7
MN
2249 /*
2250 * USB_DT_ENDPOINT are handled in
2251 * __ffs_func_bind_do_descs().
2252 */
ddf8abd2
MN
2253 if (desc->bDescriptorType == USB_DT_ENDPOINT)
2254 return 0;
2255
2256 idx = (*valuep & USB_ENDPOINT_NUMBER_MASK) - 1;
2257 if (unlikely(!func->eps[idx].ep))
2258 return -EINVAL;
2259
2260 {
2261 struct usb_endpoint_descriptor **descs;
2262 descs = func->eps[idx].descs;
2263 newValue = descs[descs[0] ? 0 : 1]->bEndpointAddress;
2264 }
2265 break;
2266 }
2267
aa02f172 2268 pr_vdebug("%02x -> %02x\n", *valuep, newValue);
ddf8abd2
MN
2269 *valuep = newValue;
2270 return 0;
2271}
2272
2273static int ffs_func_bind(struct usb_configuration *c,
2274 struct usb_function *f)
2275{
2276 struct ffs_function *func = ffs_func_from_usb(f);
2277 struct ffs_data *ffs = func->ffs;
2278
2279 const int full = !!func->ffs->fs_descs_count;
2280 const int high = gadget_is_dualspeed(func->gadget) &&
2281 func->ffs->hs_descs_count;
6fa3eb70
S
2282 const int super = gadget_is_superspeed(func->gadget) &&
2283 func->ffs->ss_descs_count;
ddf8abd2 2284
6fa3eb70 2285 int fs_len, hs_len, ret;
ddf8abd2
MN
2286
2287 /* Make it a single chunk, less management later on */
2288 struct {
2289 struct ffs_ep eps[ffs->eps_count];
2290 struct usb_descriptor_header
2291 *fs_descs[full ? ffs->fs_descs_count + 1 : 0];
2292 struct usb_descriptor_header
2293 *hs_descs[high ? ffs->hs_descs_count + 1 : 0];
6fa3eb70
S
2294 struct usb_descriptor_header
2295 *ss_descs[super ? ffs->ss_descs_count + 1 : 0];
ddf8abd2 2296 short inums[ffs->interfaces_count];
6fa3eb70 2297 char raw_descs[ffs->raw_descs_length];
ddf8abd2
MN
2298 } *data;
2299
2300 ENTER();
2301
6fa3eb70
S
2302 /* Only high/super speed but not supported by gadget? */
2303 if (unlikely(!(full | high | super)))
ddf8abd2
MN
2304 return -ENOTSUPP;
2305
2306 /* Allocate */
2307 data = kmalloc(sizeof *data, GFP_KERNEL);
2308 if (unlikely(!data))
2309 return -ENOMEM;
2310
2311 /* Zero */
2312 memset(data->eps, 0, sizeof data->eps);
6fa3eb70
S
2313 /* Copy only raw (hs,fs) descriptors (until ss_magic and ss_count) */
2314 memcpy(data->raw_descs, ffs->raw_descs + 16,
2315 ffs->raw_fs_hs_descs_length);
2316 /* Copy SS descriptors */
2317 if (func->ffs->ss_descs_count)
2318 memcpy(data->raw_descs + ffs->raw_fs_hs_descs_length,
2319 ffs->raw_descs + ffs->raw_ss_descs_offset,
2320 ffs->raw_ss_descs_length);
2321
ddf8abd2
MN
2322 memset(data->inums, 0xff, sizeof data->inums);
2323 for (ret = ffs->eps_count; ret; --ret)
2324 data->eps[ret].num = -1;
2325
2326 /* Save pointers */
2327 func->eps = data->eps;
2328 func->interfaces_nums = data->inums;
2329
5ab54cf7
MN
2330 /*
2331 * Go through all the endpoint descriptors and allocate
ddf8abd2 2332 * endpoints first, so that later we can rewrite the endpoint
5ab54cf7
MN
2333 * numbers without worrying that it may be described later on.
2334 */
ddf8abd2 2335 if (likely(full)) {
10287bae 2336 func->function.fs_descriptors = data->fs_descs;
6fa3eb70 2337 fs_len = ffs_do_descs(ffs->fs_descs_count,
ddf8abd2 2338 data->raw_descs,
6fa3eb70 2339 sizeof(data->raw_descs),
ddf8abd2 2340 __ffs_func_bind_do_descs, func);
6fa3eb70
S
2341 if (unlikely(fs_len < 0)) {
2342 ret = fs_len;
ddf8abd2 2343 goto error;
6fa3eb70 2344 }
ddf8abd2 2345 } else {
6fa3eb70 2346 fs_len = 0;
ddf8abd2
MN
2347 }
2348
2349 if (likely(high)) {
2350 func->function.hs_descriptors = data->hs_descs;
6fa3eb70
S
2351 hs_len = ffs_do_descs(ffs->hs_descs_count,
2352 data->raw_descs + fs_len,
2353 (sizeof(data->raw_descs)) - fs_len,
2354 __ffs_func_bind_do_descs, func);
2355 if (unlikely(hs_len < 0)) {
2356 ret = hs_len;
2357 goto error;
2358 }
2359 } else {
2360 hs_len = 0;
2361 }
2362
2363 if (likely(super)) {
2364 func->function.ss_descriptors = data->ss_descs;
2365 ret = ffs_do_descs(ffs->ss_descs_count,
2366 data->raw_descs + fs_len + hs_len,
2367 (sizeof(data->raw_descs)) - fs_len - hs_len,
ddf8abd2 2368 __ffs_func_bind_do_descs, func);
6fa3eb70
S
2369 if (unlikely(ret < 0))
2370 goto error;
ddf8abd2
MN
2371 }
2372
6fa3eb70 2373
5ab54cf7
MN
2374 /*
2375 * Now handle interface numbers allocation and interface and
2376 * endpoint numbers rewriting. We can do that in one go
2377 * now.
2378 */
ddf8abd2 2379 ret = ffs_do_descs(ffs->fs_descs_count +
6fa3eb70
S
2380 (high ? ffs->hs_descs_count : 0) +
2381 (super ? ffs->ss_descs_count : 0),
2382 data->raw_descs, sizeof(data->raw_descs),
ddf8abd2
MN
2383 __ffs_func_bind_do_nums, func);
2384 if (unlikely(ret < 0))
2385 goto error;
2386
2387 /* And we're done */
2388 ffs_event_add(ffs, FUNCTIONFS_BIND);
2389 return 0;
2390
2391error:
2392 /* XXX Do we need to release all claimed endpoints here? */
2393 return ret;
2394}
2395
2396
2397/* Other USB function hooks *************************************************/
2398
2399static void ffs_func_unbind(struct usb_configuration *c,
2400 struct usb_function *f)
2401{
2402 struct ffs_function *func = ffs_func_from_usb(f);
2403 struct ffs_data *ffs = func->ffs;
2404
2405 ENTER();
2406
2407 if (ffs->func == func) {
2408 ffs_func_eps_disable(func);
2409 ffs->func = NULL;
2410 }
2411
2412 ffs_event_add(ffs, FUNCTIONFS_UNBIND);
2413
2414 ffs_func_free(func);
2415}
2416
ddf8abd2
MN
2417static int ffs_func_set_alt(struct usb_function *f,
2418 unsigned interface, unsigned alt)
2419{
2420 struct ffs_function *func = ffs_func_from_usb(f);
2421 struct ffs_data *ffs = func->ffs;
2422 int ret = 0, intf;
2423
2424 if (alt != (unsigned)-1) {
2425 intf = ffs_func_revmap_intf(func, interface);
2426 if (unlikely(intf < 0))
2427 return intf;
2428 }
2429
6fa3eb70 2430 if (ffs->func) {
ddf8abd2 2431 ffs_func_eps_disable(ffs->func);
6fa3eb70
S
2432 ffs->func = NULL;
2433 }
ddf8abd2
MN
2434
2435 if (ffs->state != FFS_ACTIVE)
2436 return -ENODEV;
2437
2438 if (alt == (unsigned)-1) {
2439 ffs->func = NULL;
2440 ffs_event_add(ffs, FUNCTIONFS_DISABLE);
2441 return 0;
2442 }
2443
2444 ffs->func = func;
2445 ret = ffs_func_eps_enable(func);
2446 if (likely(ret >= 0))
2447 ffs_event_add(ffs, FUNCTIONFS_ENABLE);
2448 return ret;
2449}
2450
2451static void ffs_func_disable(struct usb_function *f)
2452{
2453 ffs_func_set_alt(f, 0, (unsigned)-1);
2454}
2455
2456static int ffs_func_setup(struct usb_function *f,
2457 const struct usb_ctrlrequest *creq)
2458{
2459 struct ffs_function *func = ffs_func_from_usb(f);
2460 struct ffs_data *ffs = func->ffs;
2461 unsigned long flags;
2462 int ret;
2463
2464 ENTER();
2465
aa02f172
MN
2466 pr_vdebug("creq->bRequestType = %02x\n", creq->bRequestType);
2467 pr_vdebug("creq->bRequest = %02x\n", creq->bRequest);
2468 pr_vdebug("creq->wValue = %04x\n", le16_to_cpu(creq->wValue));
2469 pr_vdebug("creq->wIndex = %04x\n", le16_to_cpu(creq->wIndex));
2470 pr_vdebug("creq->wLength = %04x\n", le16_to_cpu(creq->wLength));
ddf8abd2 2471
5ab54cf7
MN
2472 /*
2473 * Most requests directed to interface go through here
ddf8abd2
MN
2474 * (notable exceptions are set/get interface) so we need to
2475 * handle them. All other either handled by composite or
2476 * passed to usb_configuration->setup() (if one is set). No
2477 * matter, we will handle requests directed to endpoint here
2478 * as well (as it's straightforward) but what to do with any
5ab54cf7
MN
2479 * other request?
2480 */
ddf8abd2
MN
2481 if (ffs->state != FFS_ACTIVE)
2482 return -ENODEV;
2483
2484 switch (creq->bRequestType & USB_RECIP_MASK) {
2485 case USB_RECIP_INTERFACE:
2486 ret = ffs_func_revmap_intf(func, le16_to_cpu(creq->wIndex));
2487 if (unlikely(ret < 0))
2488 return ret;
2489 break;
2490
2491 case USB_RECIP_ENDPOINT:
2492 ret = ffs_func_revmap_ep(func, le16_to_cpu(creq->wIndex));
2493 if (unlikely(ret < 0))
2494 return ret;
2495 break;
2496
2497 default:
2498 return -EOPNOTSUPP;
2499 }
2500
2501 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2502 ffs->ev.setup = *creq;
2503 ffs->ev.setup.wIndex = cpu_to_le16(ret);
2504 __ffs_event_add(ffs, FUNCTIONFS_SETUP);
2505 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2506
2507 return 0;
2508}
2509
2510static void ffs_func_suspend(struct usb_function *f)
2511{
2512 ENTER();
2513 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_SUSPEND);
2514}
2515
2516static void ffs_func_resume(struct usb_function *f)
2517{
2518 ENTER();
2519 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_RESUME);
2520}
2521
2522
5ab54cf7 2523/* Endpoint and interface numbers reverse mapping ***************************/
ddf8abd2
MN
2524
2525static int ffs_func_revmap_ep(struct ffs_function *func, u8 num)
2526{
2527 num = func->eps_revmap[num & USB_ENDPOINT_NUMBER_MASK];
2528 return num ? num : -EDOM;
2529}
2530
2531static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf)
2532{
2533 short *nums = func->interfaces_nums;
2534 unsigned count = func->ffs->interfaces_count;
2535
2536 for (; count; --count, ++nums) {
2537 if (*nums >= 0 && *nums == intf)
2538 return nums - func->interfaces_nums;
2539 }
2540
2541 return -EDOM;
2542}
2543
2544
2545/* Misc helper functions ****************************************************/
2546
2547static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
2548{
2549 return nonblock
2550 ? likely(mutex_trylock(mutex)) ? 0 : -EAGAIN
2551 : mutex_lock_interruptible(mutex);
2552}
2553
260ef311 2554static char *ffs_prepare_buffer(const char __user *buf, size_t len)
ddf8abd2
MN
2555{
2556 char *data;
2557
2558 if (unlikely(!len))
2559 return NULL;
2560
2561 data = kmalloc(len, GFP_KERNEL);
2562 if (unlikely(!data))
2563 return ERR_PTR(-ENOMEM);
2564
2565 if (unlikely(__copy_from_user(data, buf, len))) {
2566 kfree(data);
2567 return ERR_PTR(-EFAULT);
2568 }
2569
aa02f172 2570 pr_vdebug("Buffer from user space:\n");
ddf8abd2
MN
2571 ffs_dump_mem("", data, len);
2572
2573 return data;
2574}