mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / drivers / usb / gadget / f_fs.c
blobc3067f4f213a8cb5e4a0c78b0bee4dcaedbf3bd8
1 /*
2 * f_fs.c -- user mode file system API for USB composite function controllers
4 * Copyright (C) 2010 Samsung Electronics
5 * Author: Michal Nazarewicz <mina86@mina86.com>
7 * Based on inode.c (GadgetFS) which was:
8 * Copyright (C) 2003-2004 David Brownell
9 * Copyright (C) 2003 Agilent Technologies
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.
18 /* #define DEBUG */
19 /* #define VERBOSE_DEBUG */
21 #include <linux/blkdev.h>
22 #include <linux/pagemap.h>
23 #include <linux/export.h>
24 #include <linux/hid.h>
25 #include <asm/unaligned.h>
27 #include <linux/usb/composite.h>
28 #include <linux/usb/functionfs.h>
31 #define FUNCTIONFS_MAGIC 0xa647361 /* Chosen by a honest dice roll ;) */
34 /* Debugging ****************************************************************/
36 #ifdef VERBOSE_DEBUG
37 #ifndef pr_vdebug
38 # define pr_vdebug pr_debug
39 #endif /* pr_vdebug */
40 # define ffs_dump_mem(prefix, ptr, len) \
41 print_hex_dump_bytes(pr_fmt(prefix ": "), DUMP_PREFIX_NONE, ptr, len)
42 #else
43 #ifndef pr_vdebug
44 # define pr_vdebug(...) do { } while (0)
45 #endif /* pr_vdebug */
46 # define ffs_dump_mem(prefix, ptr, len) do { } while (0)
47 #endif /* VERBOSE_DEBUG */
49 #define ENTER() pr_vdebug("%s()\n", __func__)
52 /* The data structure and setup file ****************************************/
54 enum ffs_state {
56 * Waiting for descriptors and strings.
58 * In this state no open(2), read(2) or write(2) on epfiles
59 * may succeed (which should not be the problem as there
60 * should be no such files opened in the first place).
62 FFS_READ_DESCRIPTORS,
63 FFS_READ_STRINGS,
66 * We've got descriptors and strings. We are or have called
67 * functionfs_ready_callback(). functionfs_bind() may have
68 * been called but we don't know.
70 * This is the only state in which operations on epfiles may
71 * succeed.
73 FFS_ACTIVE,
76 * All endpoints have been closed. This state is also set if
77 * we encounter an unrecoverable error. The only
78 * unrecoverable error is situation when after reading strings
79 * from user space we fail to initialise epfiles or
80 * functionfs_ready_callback() returns with error (<0).
82 * In this state no open(2), read(2) or write(2) (both on ep0
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
86 * fail).
88 FFS_CLOSING
92 enum ffs_setup_state {
93 /* There is no setup request pending. */
94 FFS_NO_SETUP,
96 * User has read events and there was a setup request event
97 * there. The next read/write on ep0 will handle the
98 * request.
100 FFS_SETUP_PENDING,
102 * There was event pending but before user space handled it
103 * some other event was introduced which canceled existing
104 * setup. If this state is set read/write on ep0 return
105 * -EIDRM. This state is only set when adding event.
107 FFS_SETUP_CANCELED
112 struct ffs_epfile;
113 struct ffs_function;
115 struct ffs_data {
116 struct usb_gadget *gadget;
119 * Protect access read/write operations, only one read/write
120 * at a time. As a consequence protects ep0req and company.
121 * While setup request is being processed (queued) this is
122 * held.
124 struct mutex mutex;
127 * Protect access to endpoint related structures (basically
128 * usb_ep_queue(), usb_ep_dequeue(), etc. calls) except for
129 * endpoint zero.
131 spinlock_t eps_lock;
134 * XXX REVISIT do we need our own request? Since we are not
135 * handling setup requests immediately 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
138 * a race. Is that the case? Or maybe we can use cdev->req
139 * after all, maybe we just need some spinlock for that?
141 struct usb_request *ep0req; /* P: mutex */
142 struct completion ep0req_completion; /* P: mutex */
143 int ep0req_status; /* P: mutex */
145 /* reference counter */
146 atomic_t ref;
147 /* how many files are opened (EP0 and others) */
148 atomic_t opened;
150 /* EP0 state */
151 enum ffs_state state;
154 * Possible transitions:
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
162 enum ffs_setup_state setup_state;
164 #define FFS_SETUP_STATE(ffs) \
165 ((enum ffs_setup_state)cmpxchg(&(ffs)->setup_state, \
166 FFS_SETUP_CANCELED, FFS_NO_SETUP))
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;
176 wait_queue_head_t waitq;
177 } ev; /* the whole structure, P: ev.waitq.lock */
179 /* Flags */
180 unsigned long flags;
181 #define FFS_FL_CALL_CLOSED_CALLBACK 0
182 #define FFS_FL_BOUND 1
184 /* Active function */
185 struct ffs_function *func;
188 * Device name, write once when file system is mounted.
189 * Intended for user to read if she wants.
191 const char *dev_name;
192 /* Private data for our user (ie. gadget). Managed by user. */
193 void *private_data;
195 /* filled by __ffs_data_got_descs() */
197 * Real descriptors are 16 bytes after raw_descs (so you need
198 * to skip 16 bytes (ie. ffs->raw_descs + 16) to get to the
199 * first full speed descriptor). raw_descs_length and
200 * raw_fs_descs_length do not have those 16 bytes added.
202 const void *raw_descs;
203 unsigned raw_descs_length;
204 unsigned raw_fs_descs_length;
205 unsigned fs_descs_count;
206 unsigned hs_descs_count;
208 unsigned short strings_count;
209 unsigned short interfaces_count;
210 unsigned short eps_count;
211 unsigned short _pad1;
213 /* filled by __ffs_data_got_strings() */
214 /* ids in stringtabs are set in functionfs_bind() */
215 const void *raw_strings;
216 struct usb_gadget_strings **stringtabs;
219 * File system's super block, write once when file system is
220 * mounted.
222 struct super_block *sb;
224 /* File permissions, written once when fs is mounted */
225 struct ffs_file_perms {
226 umode_t mode;
227 kuid_t uid;
228 kgid_t gid;
229 } file_perms;
232 * The endpoint files, filled by ffs_epfiles_create(),
233 * destroyed by ffs_epfiles_destroy().
235 struct ffs_epfile *epfiles;
238 /* Reference counter handling */
239 static void ffs_data_get(struct ffs_data *ffs);
240 static void ffs_data_put(struct ffs_data *ffs);
241 /* Creates new ffs_data object. */
242 static struct ffs_data *__must_check ffs_data_new(void) __attribute__((malloc));
244 /* Opened counter handling. */
245 static void ffs_data_opened(struct ffs_data *ffs);
246 static void ffs_data_closed(struct ffs_data *ffs);
248 /* Called with ffs->mutex held; take over ownership of data. */
249 static int __must_check
250 __ffs_data_got_descs(struct ffs_data *ffs, char *data, size_t len);
251 static int __must_check
252 __ffs_data_got_strings(struct ffs_data *ffs, char *data, size_t len);
255 /* The function structure ***************************************************/
257 struct ffs_ep;
259 struct ffs_function {
260 struct usb_configuration *conf;
261 struct usb_gadget *gadget;
262 struct ffs_data *ffs;
264 struct ffs_ep *eps;
265 u8 eps_revmap[16];
266 short *interfaces_nums;
268 struct usb_function function;
272 static struct ffs_function *ffs_func_from_usb(struct usb_function *f)
274 return container_of(f, struct ffs_function, function);
277 static void ffs_func_free(struct ffs_function *func);
279 static void ffs_func_eps_disable(struct ffs_function *func);
280 static int __must_check ffs_func_eps_enable(struct ffs_function *func);
282 static int ffs_func_bind(struct usb_configuration *,
283 struct usb_function *);
284 static void ffs_func_unbind(struct usb_configuration *,
285 struct usb_function *);
286 static int ffs_func_set_alt(struct usb_function *, unsigned, unsigned);
287 static void ffs_func_disable(struct usb_function *);
288 static int ffs_func_setup(struct usb_function *,
289 const struct usb_ctrlrequest *);
290 static void ffs_func_suspend(struct usb_function *);
291 static void ffs_func_resume(struct usb_function *);
294 static int ffs_func_revmap_ep(struct ffs_function *func, u8 num);
295 static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf);
298 /* The endpoints structures *************************************************/
300 struct ffs_ep {
301 struct usb_ep *ep; /* P: ffs->eps_lock */
302 struct usb_request *req; /* P: epfile->mutex */
304 /* [0]: full speed, [1]: high speed */
305 struct usb_endpoint_descriptor *descs[2];
307 u8 num;
309 int status; /* P: epfile->mutex */
312 struct ffs_epfile {
313 /* Protects ep->ep and ep->req. */
314 struct mutex mutex;
315 wait_queue_head_t wait;
317 struct ffs_data *ffs;
318 struct ffs_ep *ep; /* P: ffs->eps_lock */
320 struct dentry *dentry;
322 char name[5];
324 unsigned char in; /* P: ffs->eps_lock */
325 unsigned char isoc; /* P: ffs->eps_lock */
327 unsigned char _pad;
330 static int __must_check ffs_epfiles_create(struct ffs_data *ffs);
331 static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count);
333 static struct inode *__must_check
334 ffs_sb_create_file(struct super_block *sb, const char *name, void *data,
335 const struct file_operations *fops,
336 struct dentry **dentry_p);
339 /* Misc helper functions ****************************************************/
341 static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
342 __attribute__((warn_unused_result, nonnull));
343 static char *ffs_prepare_buffer(const char __user *buf, size_t len)
344 __attribute__((warn_unused_result, nonnull));
347 /* Control file aka ep0 *****************************************************/
349 static void ffs_ep0_complete(struct usb_ep *ep, struct usb_request *req)
351 struct ffs_data *ffs = req->context;
353 complete_all(&ffs->ep0req_completion);
356 static int __ffs_ep0_queue_wait(struct ffs_data *ffs, char *data, size_t len)
358 struct usb_request *req = ffs->ep0req;
359 int ret;
361 req->zero = len < le16_to_cpu(ffs->ev.setup.wLength);
363 spin_unlock_irq(&ffs->ev.waitq.lock);
365 req->buf = data;
366 req->length = len;
369 * UDC layer requires to provide a buffer even for ZLP, but should
370 * not use it at all. Let's provide some poisoned pointer to catch
371 * possible bug in the driver.
373 if (req->buf == NULL)
374 req->buf = (void *)0xDEADBABE;
376 INIT_COMPLETION(ffs->ep0req_completion);
378 ret = usb_ep_queue(ffs->gadget->ep0, req, GFP_ATOMIC);
379 if (unlikely(ret < 0))
380 return ret;
382 ret = wait_for_completion_interruptible(&ffs->ep0req_completion);
383 if (unlikely(ret)) {
384 usb_ep_dequeue(ffs->gadget->ep0, req);
385 return -EINTR;
388 ffs->setup_state = FFS_NO_SETUP;
389 return ffs->ep0req_status;
392 static int __ffs_ep0_stall(struct ffs_data *ffs)
394 if (ffs->ev.can_stall) {
395 pr_vdebug("ep0 stall\n");
396 usb_ep_set_halt(ffs->gadget->ep0);
397 ffs->setup_state = FFS_NO_SETUP;
398 return -EL2HLT;
399 } else {
400 pr_debug("bogus ep0 stall!\n");
401 return -ESRCH;
405 static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
406 size_t len, loff_t *ptr)
408 struct ffs_data *ffs = file->private_data;
409 ssize_t ret;
410 char *data;
412 ENTER();
414 /* Fast check if setup was canceled */
415 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED)
416 return -EIDRM;
418 /* Acquire mutex */
419 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
420 if (unlikely(ret < 0))
421 return ret;
423 /* Check state */
424 switch (ffs->state) {
425 case FFS_READ_DESCRIPTORS:
426 case FFS_READ_STRINGS:
427 /* Copy data */
428 if (unlikely(len < 16)) {
429 ret = -EINVAL;
430 break;
433 data = ffs_prepare_buffer(buf, len);
434 if (IS_ERR(data)) {
435 ret = PTR_ERR(data);
436 break;
439 /* Handle data */
440 if (ffs->state == FFS_READ_DESCRIPTORS) {
441 pr_info("read descriptors\n");
442 ret = __ffs_data_got_descs(ffs, data, len);
443 if (unlikely(ret < 0))
444 break;
446 ffs->state = FFS_READ_STRINGS;
447 ret = len;
448 } else {
449 pr_info("read strings\n");
450 ret = __ffs_data_got_strings(ffs, data, len);
451 if (unlikely(ret < 0))
452 break;
454 ret = ffs_epfiles_create(ffs);
455 if (unlikely(ret)) {
456 ffs->state = FFS_CLOSING;
457 break;
460 ffs->state = FFS_ACTIVE;
461 mutex_unlock(&ffs->mutex);
463 ret = functionfs_ready_callback(ffs);
464 if (unlikely(ret < 0)) {
465 ffs->state = FFS_CLOSING;
466 return ret;
469 set_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags);
470 return len;
472 break;
474 case FFS_ACTIVE:
475 data = NULL;
477 * We're called from user space, we can use _irq
478 * rather then _irqsave
480 spin_lock_irq(&ffs->ev.waitq.lock);
481 switch (FFS_SETUP_STATE(ffs)) {
482 case FFS_SETUP_CANCELED:
483 ret = -EIDRM;
484 goto done_spin;
486 case FFS_NO_SETUP:
487 ret = -ESRCH;
488 goto done_spin;
490 case FFS_SETUP_PENDING:
491 break;
494 /* FFS_SETUP_PENDING */
495 if (!(ffs->ev.setup.bRequestType & USB_DIR_IN)) {
496 spin_unlock_irq(&ffs->ev.waitq.lock);
497 ret = __ffs_ep0_stall(ffs);
498 break;
501 /* FFS_SETUP_PENDING and not stall */
502 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
504 spin_unlock_irq(&ffs->ev.waitq.lock);
506 data = ffs_prepare_buffer(buf, len);
507 if (IS_ERR(data)) {
508 ret = PTR_ERR(data);
509 break;
512 spin_lock_irq(&ffs->ev.waitq.lock);
515 * We are guaranteed to be still in FFS_ACTIVE state
516 * but the state of setup could have changed from
517 * FFS_SETUP_PENDING to FFS_SETUP_CANCELED so we need
518 * to check for that. If that happened we copied data
519 * from user space in vain but it's unlikely.
521 * For sure we are not in FFS_NO_SETUP since this is
522 * the only place FFS_SETUP_PENDING -> FFS_NO_SETUP
523 * transition can be performed and it's protected by
524 * mutex.
526 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) {
527 ret = -EIDRM;
528 done_spin:
529 spin_unlock_irq(&ffs->ev.waitq.lock);
530 } else {
531 /* unlocks spinlock */
532 ret = __ffs_ep0_queue_wait(ffs, data, len);
534 kfree(data);
535 break;
537 default:
538 ret = -EBADFD;
539 break;
542 mutex_unlock(&ffs->mutex);
543 return ret;
546 static ssize_t __ffs_ep0_read_events(struct ffs_data *ffs, char __user *buf,
547 size_t n)
550 * We are holding ffs->ev.waitq.lock and ffs->mutex and we need
551 * to release them.
553 struct usb_functionfs_event events[n];
554 unsigned i = 0;
556 memset(events, 0, sizeof events);
558 do {
559 events[i].type = ffs->ev.types[i];
560 if (events[i].type == FUNCTIONFS_SETUP) {
561 events[i].u.setup = ffs->ev.setup;
562 ffs->setup_state = FFS_SETUP_PENDING;
564 } while (++i < n);
566 if (n < ffs->ev.count) {
567 ffs->ev.count -= n;
568 memmove(ffs->ev.types, ffs->ev.types + n,
569 ffs->ev.count * sizeof *ffs->ev.types);
570 } else {
571 ffs->ev.count = 0;
574 spin_unlock_irq(&ffs->ev.waitq.lock);
575 mutex_unlock(&ffs->mutex);
577 return unlikely(__copy_to_user(buf, events, sizeof events))
578 ? -EFAULT : sizeof events;
581 static ssize_t ffs_ep0_read(struct file *file, char __user *buf,
582 size_t len, loff_t *ptr)
584 struct ffs_data *ffs = file->private_data;
585 char *data = NULL;
586 size_t n;
587 int ret;
589 ENTER();
591 /* Fast check if setup was canceled */
592 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED)
593 return -EIDRM;
595 /* Acquire mutex */
596 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
597 if (unlikely(ret < 0))
598 return ret;
600 /* Check state */
601 if (ffs->state != FFS_ACTIVE) {
602 ret = -EBADFD;
603 goto done_mutex;
607 * We're called from user space, we can use _irq rather then
608 * _irqsave
610 spin_lock_irq(&ffs->ev.waitq.lock);
612 switch (FFS_SETUP_STATE(ffs)) {
613 case FFS_SETUP_CANCELED:
614 ret = -EIDRM;
615 break;
617 case FFS_NO_SETUP:
618 n = len / sizeof(struct usb_functionfs_event);
619 if (unlikely(!n)) {
620 ret = -EINVAL;
621 break;
624 if ((file->f_flags & O_NONBLOCK) && !ffs->ev.count) {
625 ret = -EAGAIN;
626 break;
629 if (wait_event_interruptible_exclusive_locked_irq(ffs->ev.waitq,
630 ffs->ev.count)) {
631 ret = -EINTR;
632 break;
635 return __ffs_ep0_read_events(ffs, buf,
636 min(n, (size_t)ffs->ev.count));
638 case FFS_SETUP_PENDING:
639 if (ffs->ev.setup.bRequestType & USB_DIR_IN) {
640 spin_unlock_irq(&ffs->ev.waitq.lock);
641 ret = __ffs_ep0_stall(ffs);
642 goto done_mutex;
645 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
647 spin_unlock_irq(&ffs->ev.waitq.lock);
649 if (likely(len)) {
650 data = kmalloc(len, GFP_KERNEL);
651 if (unlikely(!data)) {
652 ret = -ENOMEM;
653 goto done_mutex;
657 spin_lock_irq(&ffs->ev.waitq.lock);
659 /* See ffs_ep0_write() */
660 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) {
661 ret = -EIDRM;
662 break;
665 /* unlocks spinlock */
666 ret = __ffs_ep0_queue_wait(ffs, data, len);
667 if (likely(ret > 0) && unlikely(__copy_to_user(buf, data, len)))
668 ret = -EFAULT;
669 goto done_mutex;
671 default:
672 ret = -EBADFD;
673 break;
676 spin_unlock_irq(&ffs->ev.waitq.lock);
677 done_mutex:
678 mutex_unlock(&ffs->mutex);
679 kfree(data);
680 return ret;
683 static int ffs_ep0_open(struct inode *inode, struct file *file)
685 struct ffs_data *ffs = inode->i_private;
687 ENTER();
689 if (unlikely(ffs->state == FFS_CLOSING))
690 return -EBUSY;
692 file->private_data = ffs;
693 ffs_data_opened(ffs);
695 return 0;
698 static int ffs_ep0_release(struct inode *inode, struct file *file)
700 struct ffs_data *ffs = file->private_data;
702 ENTER();
704 ffs_data_closed(ffs);
706 return 0;
709 static long ffs_ep0_ioctl(struct file *file, unsigned code, unsigned long value)
711 struct ffs_data *ffs = file->private_data;
712 struct usb_gadget *gadget = ffs->gadget;
713 long ret;
715 ENTER();
717 if (code == FUNCTIONFS_INTERFACE_REVMAP) {
718 struct ffs_function *func = ffs->func;
719 ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV;
720 } else if (gadget && gadget->ops->ioctl) {
721 ret = gadget->ops->ioctl(gadget, code, value);
722 } else {
723 ret = -ENOTTY;
726 return ret;
729 static const struct file_operations ffs_ep0_operations = {
730 .llseek = no_llseek,
732 .open = ffs_ep0_open,
733 .write = ffs_ep0_write,
734 .read = ffs_ep0_read,
735 .release = ffs_ep0_release,
736 .unlocked_ioctl = ffs_ep0_ioctl,
740 /* "Normal" endpoints operations ********************************************/
742 static void ffs_epfile_io_complete(struct usb_ep *_ep, struct usb_request *req)
744 ENTER();
745 if (likely(req->context)) {
746 struct ffs_ep *ep = _ep->driver_data;
747 ep->status = req->status ? req->status : req->actual;
748 complete(req->context);
752 static ssize_t ffs_epfile_io(struct file *file,
753 char __user *buf, size_t len, int read)
755 struct ffs_epfile *epfile = file->private_data;
756 struct ffs_ep *ep;
757 char *data = NULL;
758 ssize_t ret;
759 int halt;
761 goto first_try;
762 do {
763 spin_unlock_irq(&epfile->ffs->eps_lock);
764 mutex_unlock(&epfile->mutex);
766 first_try:
767 /* Are we still active? */
768 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE)) {
769 ret = -ENODEV;
770 goto error;
773 /* Wait for endpoint to be enabled */
774 ep = epfile->ep;
775 if (!ep) {
776 if (file->f_flags & O_NONBLOCK) {
777 ret = -EAGAIN;
778 goto error;
781 if (wait_event_interruptible(epfile->wait,
782 (ep = epfile->ep))) {
783 ret = -EINTR;
784 goto error;
788 /* Do we halt? */
789 halt = !read == !epfile->in;
790 if (halt && epfile->isoc) {
791 ret = -EINVAL;
792 goto error;
795 /* Allocate & copy */
796 if (!halt && !data) {
797 data = kzalloc(len, GFP_KERNEL);
798 if (unlikely(!data))
799 return -ENOMEM;
801 if (!read &&
802 unlikely(__copy_from_user(data, buf, len))) {
803 ret = -EFAULT;
804 goto error;
808 /* We will be using request */
809 ret = ffs_mutex_lock(&epfile->mutex,
810 file->f_flags & O_NONBLOCK);
811 if (unlikely(ret))
812 goto error;
815 * We're called from user space, we can use _irq rather then
816 * _irqsave
818 spin_lock_irq(&epfile->ffs->eps_lock);
821 * While we were acquiring mutex endpoint got disabled
822 * or changed?
824 } while (unlikely(epfile->ep != ep));
826 /* Halt */
827 if (unlikely(halt)) {
828 if (likely(epfile->ep == ep) && !WARN_ON(!ep->ep))
829 usb_ep_set_halt(ep->ep);
830 spin_unlock_irq(&epfile->ffs->eps_lock);
831 ret = -EBADMSG;
832 } else {
833 /* Fire the request */
834 DECLARE_COMPLETION_ONSTACK(done);
836 struct usb_request *req = ep->req;
837 req->context = &done;
838 req->complete = ffs_epfile_io_complete;
839 req->buf = data;
840 req->length = len;
842 ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
844 spin_unlock_irq(&epfile->ffs->eps_lock);
846 if (unlikely(ret < 0)) {
847 /* nop */
848 } else if (unlikely(wait_for_completion_interruptible(&done))) {
849 ret = -EINTR;
850 usb_ep_dequeue(ep->ep, req);
851 } else {
852 ret = ep->status;
853 if (read && ret > 0 &&
854 unlikely(copy_to_user(buf, data, ret)))
855 ret = -EFAULT;
859 mutex_unlock(&epfile->mutex);
860 error:
861 kfree(data);
862 return ret;
865 static ssize_t
866 ffs_epfile_write(struct file *file, const char __user *buf, size_t len,
867 loff_t *ptr)
869 ENTER();
871 return ffs_epfile_io(file, (char __user *)buf, len, 0);
874 static ssize_t
875 ffs_epfile_read(struct file *file, char __user *buf, size_t len, loff_t *ptr)
877 ENTER();
879 return ffs_epfile_io(file, buf, len, 1);
882 static int
883 ffs_epfile_open(struct inode *inode, struct file *file)
885 struct ffs_epfile *epfile = inode->i_private;
887 ENTER();
889 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
890 return -ENODEV;
892 file->private_data = epfile;
893 ffs_data_opened(epfile->ffs);
895 return 0;
898 static int
899 ffs_epfile_release(struct inode *inode, struct file *file)
901 struct ffs_epfile *epfile = inode->i_private;
903 ENTER();
905 ffs_data_closed(epfile->ffs);
907 return 0;
910 static long ffs_epfile_ioctl(struct file *file, unsigned code,
911 unsigned long value)
913 struct ffs_epfile *epfile = file->private_data;
914 int ret;
916 ENTER();
918 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
919 return -ENODEV;
921 spin_lock_irq(&epfile->ffs->eps_lock);
922 if (likely(epfile->ep)) {
923 switch (code) {
924 case FUNCTIONFS_FIFO_STATUS:
925 ret = usb_ep_fifo_status(epfile->ep->ep);
926 break;
927 case FUNCTIONFS_FIFO_FLUSH:
928 usb_ep_fifo_flush(epfile->ep->ep);
929 ret = 0;
930 break;
931 case FUNCTIONFS_CLEAR_HALT:
932 ret = usb_ep_clear_halt(epfile->ep->ep);
933 break;
934 case FUNCTIONFS_ENDPOINT_REVMAP:
935 ret = epfile->ep->num;
936 break;
937 default:
938 ret = -ENOTTY;
940 } else {
941 ret = -ENODEV;
943 spin_unlock_irq(&epfile->ffs->eps_lock);
945 return ret;
948 static const struct file_operations ffs_epfile_operations = {
949 .llseek = no_llseek,
951 .open = ffs_epfile_open,
952 .write = ffs_epfile_write,
953 .read = ffs_epfile_read,
954 .release = ffs_epfile_release,
955 .unlocked_ioctl = ffs_epfile_ioctl,
959 /* File system and super block operations ***********************************/
962 * Mounting the file system creates a controller file, used first for
963 * function configuration then later for event monitoring.
966 static struct inode *__must_check
967 ffs_sb_make_inode(struct super_block *sb, void *data,
968 const struct file_operations *fops,
969 const struct inode_operations *iops,
970 struct ffs_file_perms *perms)
972 struct inode *inode;
974 ENTER();
976 inode = new_inode(sb);
978 if (likely(inode)) {
979 struct timespec current_time = CURRENT_TIME;
981 inode->i_ino = get_next_ino();
982 inode->i_mode = perms->mode;
983 inode->i_uid = perms->uid;
984 inode->i_gid = perms->gid;
985 inode->i_atime = current_time;
986 inode->i_mtime = current_time;
987 inode->i_ctime = current_time;
988 inode->i_private = data;
989 if (fops)
990 inode->i_fop = fops;
991 if (iops)
992 inode->i_op = iops;
995 return inode;
998 /* Create "regular" file */
999 static struct inode *ffs_sb_create_file(struct super_block *sb,
1000 const char *name, void *data,
1001 const struct file_operations *fops,
1002 struct dentry **dentry_p)
1004 struct ffs_data *ffs = sb->s_fs_info;
1005 struct dentry *dentry;
1006 struct inode *inode;
1008 ENTER();
1010 dentry = d_alloc_name(sb->s_root, name);
1011 if (unlikely(!dentry))
1012 return NULL;
1014 inode = ffs_sb_make_inode(sb, data, fops, NULL, &ffs->file_perms);
1015 if (unlikely(!inode)) {
1016 dput(dentry);
1017 return NULL;
1020 d_add(dentry, inode);
1021 if (dentry_p)
1022 *dentry_p = dentry;
1024 return inode;
1027 /* Super block */
1028 static const struct super_operations ffs_sb_operations = {
1029 .statfs = simple_statfs,
1030 .drop_inode = generic_delete_inode,
1033 struct ffs_sb_fill_data {
1034 struct ffs_file_perms perms;
1035 umode_t root_mode;
1036 const char *dev_name;
1037 struct ffs_data *ffs_data;
1040 static int ffs_sb_fill(struct super_block *sb, void *_data, int silent)
1042 struct ffs_sb_fill_data *data = _data;
1043 struct inode *inode;
1044 struct ffs_data *ffs = data->ffs_data;
1046 ENTER();
1048 ffs->sb = sb;
1049 data->ffs_data = NULL;
1050 sb->s_fs_info = ffs;
1051 sb->s_blocksize = PAGE_CACHE_SIZE;
1052 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1053 sb->s_magic = FUNCTIONFS_MAGIC;
1054 sb->s_op = &ffs_sb_operations;
1055 sb->s_time_gran = 1;
1057 /* Root inode */
1058 data->perms.mode = data->root_mode;
1059 inode = ffs_sb_make_inode(sb, NULL,
1060 &simple_dir_operations,
1061 &simple_dir_inode_operations,
1062 &data->perms);
1063 sb->s_root = d_make_root(inode);
1064 if (unlikely(!sb->s_root))
1065 return -ENOMEM;
1067 /* EP0 file */
1068 if (unlikely(!ffs_sb_create_file(sb, "ep0", ffs,
1069 &ffs_ep0_operations, NULL)))
1070 return -ENOMEM;
1072 return 0;
1075 static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts)
1077 ENTER();
1079 if (!opts || !*opts)
1080 return 0;
1082 for (;;) {
1083 unsigned long value;
1084 char *eq, *comma;
1086 /* Option limit */
1087 comma = strchr(opts, ',');
1088 if (comma)
1089 *comma = 0;
1091 /* Value limit */
1092 eq = strchr(opts, '=');
1093 if (unlikely(!eq)) {
1094 pr_err("'=' missing in %s\n", opts);
1095 return -EINVAL;
1097 *eq = 0;
1099 /* Parse value */
1100 if (kstrtoul(eq + 1, 0, &value)) {
1101 pr_err("%s: invalid value: %s\n", opts, eq + 1);
1102 return -EINVAL;
1105 /* Interpret option */
1106 switch (eq - opts) {
1107 case 5:
1108 if (!memcmp(opts, "rmode", 5))
1109 data->root_mode = (value & 0555) | S_IFDIR;
1110 else if (!memcmp(opts, "fmode", 5))
1111 data->perms.mode = (value & 0666) | S_IFREG;
1112 else
1113 goto invalid;
1114 break;
1116 case 4:
1117 if (!memcmp(opts, "mode", 4)) {
1118 data->root_mode = (value & 0555) | S_IFDIR;
1119 data->perms.mode = (value & 0666) | S_IFREG;
1120 } else {
1121 goto invalid;
1123 break;
1125 case 3:
1126 if (!memcmp(opts, "uid", 3)) {
1127 data->perms.uid = make_kuid(current_user_ns(), value);
1128 if (!uid_valid(data->perms.uid)) {
1129 pr_err("%s: unmapped value: %lu\n", opts, value);
1130 return -EINVAL;
1132 } else if (!memcmp(opts, "gid", 3)) {
1133 data->perms.gid = make_kgid(current_user_ns(), value);
1134 if (!gid_valid(data->perms.gid)) {
1135 pr_err("%s: unmapped value: %lu\n", opts, value);
1136 return -EINVAL;
1138 } else {
1139 goto invalid;
1141 break;
1143 default:
1144 invalid:
1145 pr_err("%s: invalid option\n", opts);
1146 return -EINVAL;
1149 /* Next iteration */
1150 if (!comma)
1151 break;
1152 opts = comma + 1;
1155 return 0;
1158 /* "mount -t functionfs dev_name /dev/function" ends up here */
1160 static struct dentry *
1161 ffs_fs_mount(struct file_system_type *t, int flags,
1162 const char *dev_name, void *opts)
1164 struct ffs_sb_fill_data data = {
1165 .perms = {
1166 .mode = S_IFREG | 0600,
1167 .uid = GLOBAL_ROOT_UID,
1168 .gid = GLOBAL_ROOT_GID,
1170 .root_mode = S_IFDIR | 0500,
1172 struct dentry *rv;
1173 int ret;
1174 void *ffs_dev;
1175 struct ffs_data *ffs;
1177 ENTER();
1179 ret = ffs_fs_parse_opts(&data, opts);
1180 if (unlikely(ret < 0))
1181 return ERR_PTR(ret);
1183 ffs = ffs_data_new();
1184 if (unlikely(!ffs))
1185 return ERR_PTR(-ENOMEM);
1186 ffs->file_perms = data.perms;
1188 ffs->dev_name = kstrdup(dev_name, GFP_KERNEL);
1189 if (unlikely(!ffs->dev_name)) {
1190 ffs_data_put(ffs);
1191 return ERR_PTR(-ENOMEM);
1194 ffs_dev = functionfs_acquire_dev_callback(dev_name);
1195 if (IS_ERR(ffs_dev)) {
1196 ffs_data_put(ffs);
1197 return ERR_CAST(ffs_dev);
1199 ffs->private_data = ffs_dev;
1200 data.ffs_data = ffs;
1202 rv = mount_nodev(t, flags, &data, ffs_sb_fill);
1203 if (IS_ERR(rv) && data.ffs_data) {
1204 functionfs_release_dev_callback(data.ffs_data);
1205 ffs_data_put(data.ffs_data);
1207 return rv;
1210 static void
1211 ffs_fs_kill_sb(struct super_block *sb)
1213 ENTER();
1215 kill_litter_super(sb);
1216 if (sb->s_fs_info) {
1217 functionfs_release_dev_callback(sb->s_fs_info);
1218 ffs_data_put(sb->s_fs_info);
1222 static struct file_system_type ffs_fs_type = {
1223 .owner = THIS_MODULE,
1224 .name = "functionfs",
1225 .mount = ffs_fs_mount,
1226 .kill_sb = ffs_fs_kill_sb,
1228 MODULE_ALIAS_FS("functionfs");
1231 /* Driver's main init/cleanup functions *************************************/
1233 static int functionfs_init(void)
1235 int ret;
1237 ENTER();
1239 ret = register_filesystem(&ffs_fs_type);
1240 if (likely(!ret))
1241 pr_info("file system registered\n");
1242 else
1243 pr_err("failed registering file system (%d)\n", ret);
1245 return ret;
1248 static void functionfs_cleanup(void)
1250 ENTER();
1252 pr_info("unloading\n");
1253 unregister_filesystem(&ffs_fs_type);
1257 /* ffs_data and ffs_function construction and destruction code **************/
1259 static void ffs_data_clear(struct ffs_data *ffs);
1260 static void ffs_data_reset(struct ffs_data *ffs);
1262 static void ffs_data_get(struct ffs_data *ffs)
1264 ENTER();
1266 atomic_inc(&ffs->ref);
1269 static void ffs_data_opened(struct ffs_data *ffs)
1271 ENTER();
1273 atomic_inc(&ffs->ref);
1274 atomic_inc(&ffs->opened);
1277 static void ffs_data_put(struct ffs_data *ffs)
1279 ENTER();
1281 if (unlikely(atomic_dec_and_test(&ffs->ref))) {
1282 pr_info("%s(): freeing\n", __func__);
1283 ffs_data_clear(ffs);
1284 BUG_ON(waitqueue_active(&ffs->ev.waitq) ||
1285 waitqueue_active(&ffs->ep0req_completion.wait));
1286 kfree(ffs->dev_name);
1287 kfree(ffs);
1291 static void ffs_data_closed(struct ffs_data *ffs)
1293 ENTER();
1295 if (atomic_dec_and_test(&ffs->opened)) {
1296 ffs->state = FFS_CLOSING;
1297 ffs_data_reset(ffs);
1300 ffs_data_put(ffs);
1303 static struct ffs_data *ffs_data_new(void)
1305 struct ffs_data *ffs = kzalloc(sizeof *ffs, GFP_KERNEL);
1306 if (unlikely(!ffs))
1307 return 0;
1309 ENTER();
1311 atomic_set(&ffs->ref, 1);
1312 atomic_set(&ffs->opened, 0);
1313 ffs->state = FFS_READ_DESCRIPTORS;
1314 mutex_init(&ffs->mutex);
1315 spin_lock_init(&ffs->eps_lock);
1316 init_waitqueue_head(&ffs->ev.waitq);
1317 init_completion(&ffs->ep0req_completion);
1319 /* XXX REVISIT need to update it in some places, or do we? */
1320 ffs->ev.can_stall = 1;
1322 return ffs;
1325 static void ffs_data_clear(struct ffs_data *ffs)
1327 ENTER();
1329 if (test_and_clear_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags))
1330 functionfs_closed_callback(ffs);
1332 BUG_ON(ffs->gadget);
1334 if (ffs->epfiles)
1335 ffs_epfiles_destroy(ffs->epfiles, ffs->eps_count);
1337 kfree(ffs->raw_descs);
1338 kfree(ffs->raw_strings);
1339 kfree(ffs->stringtabs);
1342 static void ffs_data_reset(struct ffs_data *ffs)
1344 ENTER();
1346 ffs_data_clear(ffs);
1348 ffs->epfiles = NULL;
1349 ffs->raw_descs = NULL;
1350 ffs->raw_strings = NULL;
1351 ffs->stringtabs = NULL;
1353 ffs->raw_descs_length = 0;
1354 ffs->raw_fs_descs_length = 0;
1355 ffs->fs_descs_count = 0;
1356 ffs->hs_descs_count = 0;
1358 ffs->strings_count = 0;
1359 ffs->interfaces_count = 0;
1360 ffs->eps_count = 0;
1362 ffs->ev.count = 0;
1364 ffs->state = FFS_READ_DESCRIPTORS;
1365 ffs->setup_state = FFS_NO_SETUP;
1366 ffs->flags = 0;
1370 static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev)
1372 struct usb_gadget_strings **lang;
1373 int first_id;
1375 ENTER();
1377 if (WARN_ON(ffs->state != FFS_ACTIVE
1378 || test_and_set_bit(FFS_FL_BOUND, &ffs->flags)))
1379 return -EBADFD;
1381 first_id = usb_string_ids_n(cdev, ffs->strings_count);
1382 if (unlikely(first_id < 0))
1383 return first_id;
1385 ffs->ep0req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
1386 if (unlikely(!ffs->ep0req))
1387 return -ENOMEM;
1388 ffs->ep0req->complete = ffs_ep0_complete;
1389 ffs->ep0req->context = ffs;
1391 lang = ffs->stringtabs;
1392 if (lang) {
1393 for (; *lang; ++lang) {
1394 struct usb_string *str = (*lang)->strings;
1395 int id = first_id;
1396 for (; str->s; ++id, ++str)
1397 str->id = id;
1401 ffs->gadget = cdev->gadget;
1402 ffs_data_get(ffs);
1403 return 0;
1406 static void functionfs_unbind(struct ffs_data *ffs)
1408 ENTER();
1410 if (!WARN_ON(!ffs->gadget)) {
1411 usb_ep_free_request(ffs->gadget->ep0, ffs->ep0req);
1412 ffs->ep0req = NULL;
1413 ffs->gadget = NULL;
1414 clear_bit(FFS_FL_BOUND, &ffs->flags);
1415 ffs_data_put(ffs);
1419 static int ffs_epfiles_create(struct ffs_data *ffs)
1421 struct ffs_epfile *epfile, *epfiles;
1422 unsigned i, count;
1424 ENTER();
1426 count = ffs->eps_count;
1427 epfiles = kcalloc(count, sizeof(*epfiles), GFP_KERNEL);
1428 if (!epfiles)
1429 return -ENOMEM;
1431 epfile = epfiles;
1432 for (i = 1; i <= count; ++i, ++epfile) {
1433 epfile->ffs = ffs;
1434 mutex_init(&epfile->mutex);
1435 init_waitqueue_head(&epfile->wait);
1436 sprintf(epfiles->name, "ep%u", i);
1437 if (!unlikely(ffs_sb_create_file(ffs->sb, epfiles->name, epfile,
1438 &ffs_epfile_operations,
1439 &epfile->dentry))) {
1440 ffs_epfiles_destroy(epfiles, i - 1);
1441 return -ENOMEM;
1445 ffs->epfiles = epfiles;
1446 return 0;
1449 static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count)
1451 struct ffs_epfile *epfile = epfiles;
1453 ENTER();
1455 for (; count; --count, ++epfile) {
1456 BUG_ON(mutex_is_locked(&epfile->mutex) ||
1457 waitqueue_active(&epfile->wait));
1458 if (epfile->dentry) {
1459 d_delete(epfile->dentry);
1460 dput(epfile->dentry);
1461 epfile->dentry = NULL;
1465 kfree(epfiles);
1468 static int functionfs_bind_config(struct usb_composite_dev *cdev,
1469 struct usb_configuration *c,
1470 struct ffs_data *ffs)
1472 struct ffs_function *func;
1473 int ret;
1475 ENTER();
1477 func = kzalloc(sizeof *func, GFP_KERNEL);
1478 if (unlikely(!func))
1479 return -ENOMEM;
1481 func->function.name = "Function FS Gadget";
1482 func->function.strings = ffs->stringtabs;
1484 func->function.bind = ffs_func_bind;
1485 func->function.unbind = ffs_func_unbind;
1486 func->function.set_alt = ffs_func_set_alt;
1487 func->function.disable = ffs_func_disable;
1488 func->function.setup = ffs_func_setup;
1489 func->function.suspend = ffs_func_suspend;
1490 func->function.resume = ffs_func_resume;
1492 func->conf = c;
1493 func->gadget = cdev->gadget;
1494 func->ffs = ffs;
1495 ffs_data_get(ffs);
1497 ret = usb_add_function(c, &func->function);
1498 if (unlikely(ret))
1499 ffs_func_free(func);
1501 return ret;
1504 static void ffs_func_free(struct ffs_function *func)
1506 struct ffs_ep *ep = func->eps;
1507 unsigned count = func->ffs->eps_count;
1508 unsigned long flags;
1510 ENTER();
1512 /* cleanup after autoconfig */
1513 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1514 do {
1515 if (ep->ep && ep->req)
1516 usb_ep_free_request(ep->ep, ep->req);
1517 ep->req = NULL;
1518 ++ep;
1519 } while (--count);
1520 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1522 ffs_data_put(func->ffs);
1524 kfree(func->eps);
1526 * eps and interfaces_nums are allocated in the same chunk so
1527 * only one free is required. Descriptors are also allocated
1528 * in the same chunk.
1531 kfree(func);
1534 static void ffs_func_eps_disable(struct ffs_function *func)
1536 struct ffs_ep *ep = func->eps;
1537 struct ffs_epfile *epfile = func->ffs->epfiles;
1538 unsigned count = func->ffs->eps_count;
1539 unsigned long flags;
1541 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1542 do {
1543 /* pending requests get nuked */
1544 if (likely(ep->ep))
1545 usb_ep_disable(ep->ep);
1546 epfile->ep = NULL;
1548 ++ep;
1549 ++epfile;
1550 } while (--count);
1551 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1554 static int ffs_func_eps_enable(struct ffs_function *func)
1556 struct ffs_data *ffs = func->ffs;
1557 struct ffs_ep *ep = func->eps;
1558 struct ffs_epfile *epfile = ffs->epfiles;
1559 unsigned count = ffs->eps_count;
1560 unsigned long flags;
1561 int ret = 0;
1563 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1564 do {
1565 struct usb_endpoint_descriptor *ds;
1566 ds = ep->descs[ep->descs[1] ? 1 : 0];
1568 ep->ep->driver_data = ep;
1569 ep->ep->desc = ds;
1570 ret = usb_ep_enable(ep->ep);
1571 if (likely(!ret)) {
1572 epfile->ep = ep;
1573 epfile->in = usb_endpoint_dir_in(ds);
1574 epfile->isoc = usb_endpoint_xfer_isoc(ds);
1575 } else {
1576 break;
1579 wake_up(&epfile->wait);
1581 ++ep;
1582 ++epfile;
1583 } while (--count);
1584 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1586 return ret;
1590 /* Parsing and building descriptors and strings *****************************/
1593 * This validates if data pointed by data is a valid USB descriptor as
1594 * well as record how many interfaces, endpoints and strings are
1595 * required by given configuration. Returns address after the
1596 * descriptor or NULL if data is invalid.
1599 enum ffs_entity_type {
1600 FFS_DESCRIPTOR, FFS_INTERFACE, FFS_STRING, FFS_ENDPOINT
1603 typedef int (*ffs_entity_callback)(enum ffs_entity_type entity,
1604 u8 *valuep,
1605 struct usb_descriptor_header *desc,
1606 void *priv);
1608 static int __must_check ffs_do_desc(char *data, unsigned len,
1609 ffs_entity_callback entity, void *priv)
1611 struct usb_descriptor_header *_ds = (void *)data;
1612 u8 length;
1613 int ret;
1615 ENTER();
1617 /* At least two bytes are required: length and type */
1618 if (len < 2) {
1619 pr_vdebug("descriptor too short\n");
1620 return -EINVAL;
1623 /* If we have at least as many bytes as the descriptor takes? */
1624 length = _ds->bLength;
1625 if (len < length) {
1626 pr_vdebug("descriptor longer then available data\n");
1627 return -EINVAL;
1630 #define __entity_check_INTERFACE(val) 1
1631 #define __entity_check_STRING(val) (val)
1632 #define __entity_check_ENDPOINT(val) ((val) & USB_ENDPOINT_NUMBER_MASK)
1633 #define __entity(type, val) do { \
1634 pr_vdebug("entity " #type "(%02x)\n", (val)); \
1635 if (unlikely(!__entity_check_ ##type(val))) { \
1636 pr_vdebug("invalid entity's value\n"); \
1637 return -EINVAL; \
1639 ret = entity(FFS_ ##type, &val, _ds, priv); \
1640 if (unlikely(ret < 0)) { \
1641 pr_debug("entity " #type "(%02x); ret = %d\n", \
1642 (val), ret); \
1643 return ret; \
1645 } while (0)
1647 /* Parse descriptor depending on type. */
1648 switch (_ds->bDescriptorType) {
1649 case USB_DT_DEVICE:
1650 case USB_DT_CONFIG:
1651 case USB_DT_STRING:
1652 case USB_DT_DEVICE_QUALIFIER:
1653 /* function can't have any of those */
1654 pr_vdebug("descriptor reserved for gadget: %d\n",
1655 _ds->bDescriptorType);
1656 return -EINVAL;
1658 case USB_DT_INTERFACE: {
1659 struct usb_interface_descriptor *ds = (void *)_ds;
1660 pr_vdebug("interface descriptor\n");
1661 if (length != sizeof *ds)
1662 goto inv_length;
1664 __entity(INTERFACE, ds->bInterfaceNumber);
1665 if (ds->iInterface)
1666 __entity(STRING, ds->iInterface);
1668 break;
1670 case USB_DT_ENDPOINT: {
1671 struct usb_endpoint_descriptor *ds = (void *)_ds;
1672 pr_vdebug("endpoint descriptor\n");
1673 if (length != USB_DT_ENDPOINT_SIZE &&
1674 length != USB_DT_ENDPOINT_AUDIO_SIZE)
1675 goto inv_length;
1676 __entity(ENDPOINT, ds->bEndpointAddress);
1678 break;
1680 case HID_DT_HID:
1681 pr_vdebug("hid descriptor\n");
1682 if (length != sizeof(struct hid_descriptor))
1683 goto inv_length;
1684 break;
1686 case USB_DT_OTG:
1687 if (length != sizeof(struct usb_otg_descriptor))
1688 goto inv_length;
1689 break;
1691 case USB_DT_INTERFACE_ASSOCIATION: {
1692 struct usb_interface_assoc_descriptor *ds = (void *)_ds;
1693 pr_vdebug("interface association descriptor\n");
1694 if (length != sizeof *ds)
1695 goto inv_length;
1696 if (ds->iFunction)
1697 __entity(STRING, ds->iFunction);
1699 break;
1701 case USB_DT_OTHER_SPEED_CONFIG:
1702 case USB_DT_INTERFACE_POWER:
1703 case USB_DT_DEBUG:
1704 case USB_DT_SECURITY:
1705 case USB_DT_CS_RADIO_CONTROL:
1706 /* TODO */
1707 pr_vdebug("unimplemented descriptor: %d\n", _ds->bDescriptorType);
1708 return -EINVAL;
1710 default:
1711 /* We should never be here */
1712 pr_vdebug("unknown descriptor: %d\n", _ds->bDescriptorType);
1713 return -EINVAL;
1715 inv_length:
1716 pr_vdebug("invalid length: %d (descriptor %d)\n",
1717 _ds->bLength, _ds->bDescriptorType);
1718 return -EINVAL;
1721 #undef __entity
1722 #undef __entity_check_DESCRIPTOR
1723 #undef __entity_check_INTERFACE
1724 #undef __entity_check_STRING
1725 #undef __entity_check_ENDPOINT
1727 return length;
1730 static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len,
1731 ffs_entity_callback entity, void *priv)
1733 const unsigned _len = len;
1734 unsigned long num = 0;
1736 ENTER();
1738 for (;;) {
1739 int ret;
1741 if (num == count)
1742 data = NULL;
1744 /* Record "descriptor" entity */
1745 ret = entity(FFS_DESCRIPTOR, (u8 *)num, (void *)data, priv);
1746 if (unlikely(ret < 0)) {
1747 pr_debug("entity DESCRIPTOR(%02lx); ret = %d\n",
1748 num, ret);
1749 return ret;
1752 if (!data)
1753 return _len - len;
1755 ret = ffs_do_desc(data, len, entity, priv);
1756 if (unlikely(ret < 0)) {
1757 pr_debug("%s returns %d\n", __func__, ret);
1758 return ret;
1761 len -= ret;
1762 data += ret;
1763 ++num;
1767 static int __ffs_data_do_entity(enum ffs_entity_type type,
1768 u8 *valuep, struct usb_descriptor_header *desc,
1769 void *priv)
1771 struct ffs_data *ffs = priv;
1773 ENTER();
1775 switch (type) {
1776 case FFS_DESCRIPTOR:
1777 break;
1779 case FFS_INTERFACE:
1781 * Interfaces are indexed from zero so if we
1782 * encountered interface "n" then there are at least
1783 * "n+1" interfaces.
1785 if (*valuep >= ffs->interfaces_count)
1786 ffs->interfaces_count = *valuep + 1;
1787 break;
1789 case FFS_STRING:
1791 * Strings are indexed from 1 (0 is magic ;) reserved
1792 * for languages list or some such)
1794 if (*valuep > ffs->strings_count)
1795 ffs->strings_count = *valuep;
1796 break;
1798 case FFS_ENDPOINT:
1799 /* Endpoints are indexed from 1 as well. */
1800 if ((*valuep & USB_ENDPOINT_NUMBER_MASK) > ffs->eps_count)
1801 ffs->eps_count = (*valuep & USB_ENDPOINT_NUMBER_MASK);
1802 break;
1805 return 0;
1808 static int __ffs_data_got_descs(struct ffs_data *ffs,
1809 char *const _data, size_t len)
1811 unsigned fs_count, hs_count;
1812 int fs_len, ret = -EINVAL;
1813 char *data = _data;
1815 ENTER();
1817 if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_DESCRIPTORS_MAGIC ||
1818 get_unaligned_le32(data + 4) != len))
1819 goto error;
1820 fs_count = get_unaligned_le32(data + 8);
1821 hs_count = get_unaligned_le32(data + 12);
1823 if (!fs_count && !hs_count)
1824 goto einval;
1826 data += 16;
1827 len -= 16;
1829 if (likely(fs_count)) {
1830 fs_len = ffs_do_descs(fs_count, data, len,
1831 __ffs_data_do_entity, ffs);
1832 if (unlikely(fs_len < 0)) {
1833 ret = fs_len;
1834 goto error;
1837 data += fs_len;
1838 len -= fs_len;
1839 } else {
1840 fs_len = 0;
1843 if (likely(hs_count)) {
1844 ret = ffs_do_descs(hs_count, data, len,
1845 __ffs_data_do_entity, ffs);
1846 if (unlikely(ret < 0))
1847 goto error;
1848 } else {
1849 ret = 0;
1852 if (unlikely(len != ret))
1853 goto einval;
1855 ffs->raw_fs_descs_length = fs_len;
1856 ffs->raw_descs_length = fs_len + ret;
1857 ffs->raw_descs = _data;
1858 ffs->fs_descs_count = fs_count;
1859 ffs->hs_descs_count = hs_count;
1861 return 0;
1863 einval:
1864 ret = -EINVAL;
1865 error:
1866 kfree(_data);
1867 return ret;
1870 static int __ffs_data_got_strings(struct ffs_data *ffs,
1871 char *const _data, size_t len)
1873 u32 str_count, needed_count, lang_count;
1874 struct usb_gadget_strings **stringtabs, *t;
1875 struct usb_string *strings, *s;
1876 const char *data = _data;
1878 ENTER();
1880 if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_STRINGS_MAGIC ||
1881 get_unaligned_le32(data + 4) != len))
1882 goto error;
1883 str_count = get_unaligned_le32(data + 8);
1884 lang_count = get_unaligned_le32(data + 12);
1886 /* if one is zero the other must be zero */
1887 if (unlikely(!str_count != !lang_count))
1888 goto error;
1890 /* Do we have at least as many strings as descriptors need? */
1891 needed_count = ffs->strings_count;
1892 if (unlikely(str_count < needed_count))
1893 goto error;
1896 * If we don't need any strings just return and free all
1897 * memory.
1899 if (!needed_count) {
1900 kfree(_data);
1901 return 0;
1904 /* Allocate everything in one chunk so there's less maintenance. */
1906 struct {
1907 struct usb_gadget_strings *stringtabs[lang_count + 1];
1908 struct usb_gadget_strings stringtab[lang_count];
1909 struct usb_string strings[lang_count*(needed_count+1)];
1910 } *d;
1911 unsigned i = 0;
1913 d = kmalloc(sizeof *d, GFP_KERNEL);
1914 if (unlikely(!d)) {
1915 kfree(_data);
1916 return -ENOMEM;
1919 stringtabs = d->stringtabs;
1920 t = d->stringtab;
1921 i = lang_count;
1922 do {
1923 *stringtabs++ = t++;
1924 } while (--i);
1925 *stringtabs = NULL;
1927 stringtabs = d->stringtabs;
1928 t = d->stringtab;
1929 s = d->strings;
1930 strings = s;
1933 /* For each language */
1934 data += 16;
1935 len -= 16;
1937 do { /* lang_count > 0 so we can use do-while */
1938 unsigned needed = needed_count;
1940 if (unlikely(len < 3))
1941 goto error_free;
1942 t->language = get_unaligned_le16(data);
1943 t->strings = s;
1944 ++t;
1946 data += 2;
1947 len -= 2;
1949 /* For each string */
1950 do { /* str_count > 0 so we can use do-while */
1951 size_t length = strnlen(data, len);
1953 if (unlikely(length == len))
1954 goto error_free;
1957 * User may provide more strings then we need,
1958 * if that's the case we simply ignore the
1959 * rest
1961 if (likely(needed)) {
1963 * s->id will be set while adding
1964 * function to configuration so for
1965 * now just leave garbage here.
1967 s->s = data;
1968 --needed;
1969 ++s;
1972 data += length + 1;
1973 len -= length + 1;
1974 } while (--str_count);
1976 s->id = 0; /* terminator */
1977 s->s = NULL;
1978 ++s;
1980 } while (--lang_count);
1982 /* Some garbage left? */
1983 if (unlikely(len))
1984 goto error_free;
1986 /* Done! */
1987 ffs->stringtabs = stringtabs;
1988 ffs->raw_strings = _data;
1990 return 0;
1992 error_free:
1993 kfree(stringtabs);
1994 error:
1995 kfree(_data);
1996 return -EINVAL;
2000 /* Events handling and management *******************************************/
2002 static void __ffs_event_add(struct ffs_data *ffs,
2003 enum usb_functionfs_event_type type)
2005 enum usb_functionfs_event_type rem_type1, rem_type2 = type;
2006 int neg = 0;
2009 * Abort any unhandled setup
2011 * We do not need to worry about some cmpxchg() changing value
2012 * of ffs->setup_state without holding the lock because when
2013 * state is FFS_SETUP_PENDING cmpxchg() in several places in
2014 * the source does nothing.
2016 if (ffs->setup_state == FFS_SETUP_PENDING)
2017 ffs->setup_state = FFS_SETUP_CANCELED;
2019 switch (type) {
2020 case FUNCTIONFS_RESUME:
2021 rem_type2 = FUNCTIONFS_SUSPEND;
2022 /* FALL THROUGH */
2023 case FUNCTIONFS_SUSPEND:
2024 case FUNCTIONFS_SETUP:
2025 rem_type1 = type;
2026 /* Discard all similar events */
2027 break;
2029 case FUNCTIONFS_BIND:
2030 case FUNCTIONFS_UNBIND:
2031 case FUNCTIONFS_DISABLE:
2032 case FUNCTIONFS_ENABLE:
2033 /* Discard everything other then power management. */
2034 rem_type1 = FUNCTIONFS_SUSPEND;
2035 rem_type2 = FUNCTIONFS_RESUME;
2036 neg = 1;
2037 break;
2039 default:
2040 BUG();
2044 u8 *ev = ffs->ev.types, *out = ev;
2045 unsigned n = ffs->ev.count;
2046 for (; n; --n, ++ev)
2047 if ((*ev == rem_type1 || *ev == rem_type2) == neg)
2048 *out++ = *ev;
2049 else
2050 pr_vdebug("purging event %d\n", *ev);
2051 ffs->ev.count = out - ffs->ev.types;
2054 pr_vdebug("adding event %d\n", type);
2055 ffs->ev.types[ffs->ev.count++] = type;
2056 wake_up_locked(&ffs->ev.waitq);
2059 static void ffs_event_add(struct ffs_data *ffs,
2060 enum usb_functionfs_event_type type)
2062 unsigned long flags;
2063 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2064 __ffs_event_add(ffs, type);
2065 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2069 /* Bind/unbind USB function hooks *******************************************/
2071 static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
2072 struct usb_descriptor_header *desc,
2073 void *priv)
2075 struct usb_endpoint_descriptor *ds = (void *)desc;
2076 struct ffs_function *func = priv;
2077 struct ffs_ep *ffs_ep;
2080 * If hs_descriptors is not NULL then we are reading hs
2081 * descriptors now
2083 const int isHS = func->function.hs_descriptors != NULL;
2084 unsigned idx;
2086 if (type != FFS_DESCRIPTOR)
2087 return 0;
2089 if (isHS)
2090 func->function.hs_descriptors[(long)valuep] = desc;
2091 else
2092 func->function.fs_descriptors[(long)valuep] = desc;
2094 if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT)
2095 return 0;
2097 idx = (ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK) - 1;
2098 ffs_ep = func->eps + idx;
2100 if (unlikely(ffs_ep->descs[isHS])) {
2101 pr_vdebug("two %sspeed descriptors for EP %d\n",
2102 isHS ? "high" : "full",
2103 ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
2104 return -EINVAL;
2106 ffs_ep->descs[isHS] = ds;
2108 ffs_dump_mem(": Original ep desc", ds, ds->bLength);
2109 if (ffs_ep->ep) {
2110 ds->bEndpointAddress = ffs_ep->descs[0]->bEndpointAddress;
2111 if (!ds->wMaxPacketSize)
2112 ds->wMaxPacketSize = ffs_ep->descs[0]->wMaxPacketSize;
2113 } else {
2114 struct usb_request *req;
2115 struct usb_ep *ep;
2117 pr_vdebug("autoconfig\n");
2118 ep = usb_ep_autoconfig(func->gadget, ds);
2119 if (unlikely(!ep))
2120 return -ENOTSUPP;
2121 ep->driver_data = func->eps + idx;
2123 req = usb_ep_alloc_request(ep, GFP_KERNEL);
2124 if (unlikely(!req))
2125 return -ENOMEM;
2127 ffs_ep->ep = ep;
2128 ffs_ep->req = req;
2129 func->eps_revmap[ds->bEndpointAddress &
2130 USB_ENDPOINT_NUMBER_MASK] = idx + 1;
2132 ffs_dump_mem(": Rewritten ep desc", ds, ds->bLength);
2134 return 0;
2137 static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep,
2138 struct usb_descriptor_header *desc,
2139 void *priv)
2141 struct ffs_function *func = priv;
2142 unsigned idx;
2143 u8 newValue;
2145 switch (type) {
2146 default:
2147 case FFS_DESCRIPTOR:
2148 /* Handled in previous pass by __ffs_func_bind_do_descs() */
2149 return 0;
2151 case FFS_INTERFACE:
2152 idx = *valuep;
2153 if (func->interfaces_nums[idx] < 0) {
2154 int id = usb_interface_id(func->conf, &func->function);
2155 if (unlikely(id < 0))
2156 return id;
2157 func->interfaces_nums[idx] = id;
2159 newValue = func->interfaces_nums[idx];
2160 break;
2162 case FFS_STRING:
2163 /* String' IDs are allocated when fsf_data is bound to cdev */
2164 newValue = func->ffs->stringtabs[0]->strings[*valuep - 1].id;
2165 break;
2167 case FFS_ENDPOINT:
2169 * USB_DT_ENDPOINT are handled in
2170 * __ffs_func_bind_do_descs().
2172 if (desc->bDescriptorType == USB_DT_ENDPOINT)
2173 return 0;
2175 idx = (*valuep & USB_ENDPOINT_NUMBER_MASK) - 1;
2176 if (unlikely(!func->eps[idx].ep))
2177 return -EINVAL;
2180 struct usb_endpoint_descriptor **descs;
2181 descs = func->eps[idx].descs;
2182 newValue = descs[descs[0] ? 0 : 1]->bEndpointAddress;
2184 break;
2187 pr_vdebug("%02x -> %02x\n", *valuep, newValue);
2188 *valuep = newValue;
2189 return 0;
2192 static int ffs_func_bind(struct usb_configuration *c,
2193 struct usb_function *f)
2195 struct ffs_function *func = ffs_func_from_usb(f);
2196 struct ffs_data *ffs = func->ffs;
2198 const int full = !!func->ffs->fs_descs_count;
2199 const int high = gadget_is_dualspeed(func->gadget) &&
2200 func->ffs->hs_descs_count;
2202 int ret;
2204 /* Make it a single chunk, less management later on */
2205 struct {
2206 struct ffs_ep eps[ffs->eps_count];
2207 struct usb_descriptor_header
2208 *fs_descs[full ? ffs->fs_descs_count + 1 : 0];
2209 struct usb_descriptor_header
2210 *hs_descs[high ? ffs->hs_descs_count + 1 : 0];
2211 short inums[ffs->interfaces_count];
2212 char raw_descs[high ? ffs->raw_descs_length
2213 : ffs->raw_fs_descs_length];
2214 } *data;
2216 ENTER();
2218 /* Only high speed but not supported by gadget? */
2219 if (unlikely(!(full | high)))
2220 return -ENOTSUPP;
2222 /* Allocate */
2223 data = kmalloc(sizeof *data, GFP_KERNEL);
2224 if (unlikely(!data))
2225 return -ENOMEM;
2227 /* Zero */
2228 memset(data->eps, 0, sizeof data->eps);
2229 memcpy(data->raw_descs, ffs->raw_descs + 16, sizeof data->raw_descs);
2230 memset(data->inums, 0xff, sizeof data->inums);
2231 for (ret = ffs->eps_count; ret; --ret)
2232 data->eps[ret].num = -1;
2234 /* Save pointers */
2235 func->eps = data->eps;
2236 func->interfaces_nums = data->inums;
2239 * Go through all the endpoint descriptors and allocate
2240 * endpoints first, so that later we can rewrite the endpoint
2241 * numbers without worrying that it may be described later on.
2243 if (likely(full)) {
2244 func->function.fs_descriptors = data->fs_descs;
2245 ret = ffs_do_descs(ffs->fs_descs_count,
2246 data->raw_descs,
2247 sizeof data->raw_descs,
2248 __ffs_func_bind_do_descs, func);
2249 if (unlikely(ret < 0))
2250 goto error;
2251 } else {
2252 ret = 0;
2255 if (likely(high)) {
2256 func->function.hs_descriptors = data->hs_descs;
2257 ret = ffs_do_descs(ffs->hs_descs_count,
2258 data->raw_descs + ret,
2259 (sizeof data->raw_descs) - ret,
2260 __ffs_func_bind_do_descs, func);
2261 if (unlikely(ret < 0))
2262 goto error;
2266 * Now handle interface numbers allocation and interface and
2267 * endpoint numbers rewriting. We can do that in one go
2268 * now.
2270 ret = ffs_do_descs(ffs->fs_descs_count +
2271 (high ? ffs->hs_descs_count : 0),
2272 data->raw_descs, sizeof data->raw_descs,
2273 __ffs_func_bind_do_nums, func);
2274 if (unlikely(ret < 0))
2275 goto error;
2277 /* And we're done */
2278 ffs_event_add(ffs, FUNCTIONFS_BIND);
2279 return 0;
2281 error:
2282 /* XXX Do we need to release all claimed endpoints here? */
2283 return ret;
2287 /* Other USB function hooks *************************************************/
2289 static void ffs_func_unbind(struct usb_configuration *c,
2290 struct usb_function *f)
2292 struct ffs_function *func = ffs_func_from_usb(f);
2293 struct ffs_data *ffs = func->ffs;
2295 ENTER();
2297 if (ffs->func == func) {
2298 ffs_func_eps_disable(func);
2299 ffs->func = NULL;
2302 ffs_event_add(ffs, FUNCTIONFS_UNBIND);
2304 ffs_func_free(func);
2307 static int ffs_func_set_alt(struct usb_function *f,
2308 unsigned interface, unsigned alt)
2310 struct ffs_function *func = ffs_func_from_usb(f);
2311 struct ffs_data *ffs = func->ffs;
2312 int ret = 0, intf;
2314 if (alt != (unsigned)-1) {
2315 intf = ffs_func_revmap_intf(func, interface);
2316 if (unlikely(intf < 0))
2317 return intf;
2320 if (ffs->func)
2321 ffs_func_eps_disable(ffs->func);
2323 if (ffs->state != FFS_ACTIVE)
2324 return -ENODEV;
2326 if (alt == (unsigned)-1) {
2327 ffs->func = NULL;
2328 ffs_event_add(ffs, FUNCTIONFS_DISABLE);
2329 return 0;
2332 ffs->func = func;
2333 ret = ffs_func_eps_enable(func);
2334 if (likely(ret >= 0))
2335 ffs_event_add(ffs, FUNCTIONFS_ENABLE);
2336 return ret;
2339 static void ffs_func_disable(struct usb_function *f)
2341 ffs_func_set_alt(f, 0, (unsigned)-1);
2344 static int ffs_func_setup(struct usb_function *f,
2345 const struct usb_ctrlrequest *creq)
2347 struct ffs_function *func = ffs_func_from_usb(f);
2348 struct ffs_data *ffs = func->ffs;
2349 unsigned long flags;
2350 int ret;
2352 ENTER();
2354 pr_vdebug("creq->bRequestType = %02x\n", creq->bRequestType);
2355 pr_vdebug("creq->bRequest = %02x\n", creq->bRequest);
2356 pr_vdebug("creq->wValue = %04x\n", le16_to_cpu(creq->wValue));
2357 pr_vdebug("creq->wIndex = %04x\n", le16_to_cpu(creq->wIndex));
2358 pr_vdebug("creq->wLength = %04x\n", le16_to_cpu(creq->wLength));
2361 * Most requests directed to interface go through here
2362 * (notable exceptions are set/get interface) so we need to
2363 * handle them. All other either handled by composite or
2364 * passed to usb_configuration->setup() (if one is set). No
2365 * matter, we will handle requests directed to endpoint here
2366 * as well (as it's straightforward) but what to do with any
2367 * other request?
2369 if (ffs->state != FFS_ACTIVE)
2370 return -ENODEV;
2372 switch (creq->bRequestType & USB_RECIP_MASK) {
2373 case USB_RECIP_INTERFACE:
2374 ret = ffs_func_revmap_intf(func, le16_to_cpu(creq->wIndex));
2375 if (unlikely(ret < 0))
2376 return ret;
2377 break;
2379 case USB_RECIP_ENDPOINT:
2380 ret = ffs_func_revmap_ep(func, le16_to_cpu(creq->wIndex));
2381 if (unlikely(ret < 0))
2382 return ret;
2383 break;
2385 default:
2386 return -EOPNOTSUPP;
2389 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2390 ffs->ev.setup = *creq;
2391 ffs->ev.setup.wIndex = cpu_to_le16(ret);
2392 __ffs_event_add(ffs, FUNCTIONFS_SETUP);
2393 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2395 return 0;
2398 static void ffs_func_suspend(struct usb_function *f)
2400 ENTER();
2401 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_SUSPEND);
2404 static void ffs_func_resume(struct usb_function *f)
2406 ENTER();
2407 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_RESUME);
2411 /* Endpoint and interface numbers reverse mapping ***************************/
2413 static int ffs_func_revmap_ep(struct ffs_function *func, u8 num)
2415 num = func->eps_revmap[num & USB_ENDPOINT_NUMBER_MASK];
2416 return num ? num : -EDOM;
2419 static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf)
2421 short *nums = func->interfaces_nums;
2422 unsigned count = func->ffs->interfaces_count;
2424 for (; count; --count, ++nums) {
2425 if (*nums >= 0 && *nums == intf)
2426 return nums - func->interfaces_nums;
2429 return -EDOM;
2433 /* Misc helper functions ****************************************************/
2435 static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
2437 return nonblock
2438 ? likely(mutex_trylock(mutex)) ? 0 : -EAGAIN
2439 : mutex_lock_interruptible(mutex);
2442 static char *ffs_prepare_buffer(const char __user *buf, size_t len)
2444 char *data;
2446 if (unlikely(!len))
2447 return NULL;
2449 data = kmalloc(len, GFP_KERNEL);
2450 if (unlikely(!data))
2451 return ERR_PTR(-ENOMEM);
2453 if (unlikely(__copy_from_user(data, buf, len))) {
2454 kfree(data);
2455 return ERR_PTR(-EFAULT);
2458 pr_vdebug("Buffer from user space:\n");
2459 ffs_dump_mem("", data, len);
2461 return data;