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