2 * f_fs.c -- user mode filesystem api for usb composite funtcion controllers
4 * Copyright (C) 2010 Samsung Electronics
5 * Author: Michal Nazarewicz <m.nazarewicz@samsung.com>
7 * Based on inode.c (GadgetFS):
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.
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.
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
28 /* #define VERBOSE_DEBUG */
30 #include <linux/blkdev.h>
31 #include <asm/unaligned.h>
32 #include <linux/smp_lock.h>
34 #include <linux/usb/composite.h>
35 #include <linux/usb/functionfs.h>
38 #define FUNCTIONFS_MAGIC 0xa647361 /* Chosen by a honest dice roll ;) */
41 /* Debuging *****************************************************************/
43 #define ffs_printk(level, fmt, args...) printk(level "f_fs: " fmt "\n", ## args)
45 #define FERR(...) ffs_printk(KERN_ERR, __VA_ARGS__)
46 #define FINFO(...) ffs_printk(KERN_INFO, __VA_ARGS__)
49 # define FDBG(...) ffs_printk(KERN_DEBUG, __VA_ARGS__)
51 # define FDBG(...) do { } while (0)
57 # define FVDBG(...) do { } while (0)
58 #endif /* VERBOSE_DEBUG */
60 #define ENTER() FVDBG("%s()", __func__)
63 # define ffs_dump_mem(prefix, ptr, len) \
64 print_hex_dump_bytes("f_fs" prefix ": ", DUMP_PREFIX_NONE, ptr, len)
66 # define ffs_dump_mem(prefix, ptr, len) do { } while (0)
70 /* The data structure and setup file ****************************************/
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). */
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
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
101 enum ffs_setup_state {
102 /* There is no setup request pending. */
104 /* User has read events and there was a setup request event
105 * there. The next read/write on ep0 will handle the
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. */
121 struct usb_gadget *gadget;
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
129 /* Protect access to enpoint related structures (basically
130 * usb_ep_queue(), usb_ep_dequeue(), etc. calls) except for
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
138 * a race. Is taht the case? Or maybe we can use cdev->req
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 */
144 /* reference counter */
146 /* how many files are opened (EP0 and others) */
150 enum ffs_state state;
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
161 enum ffs_setup_state setup_state;
163 #define FFS_SETUP_STATE(ffs) \
164 ((enum ffs_setup_state)cmpxchg(&(ffs)->setup_state, \
165 FFS_SETUP_CANCELED, FFS_NO_SETUP))
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;
175 wait_queue_head_t waitq;
176 } ev; /* the whole structure, P: ev.waitq.lock */
180 #define FFS_FL_CALL_CLOSED_CALLBACK 0
181 #define FFS_FL_BOUND 1
183 /* Active function */
184 struct ffs_function *func;
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
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;
204 unsigned short strings_count;
205 unsigned short interfaces_count;
206 unsigned short eps_count;
207 unsigned short _pad1;
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;
214 /* File system's super block, write once when file system is mounted. */
215 struct super_block *sb;
217 /* File permissions, written once when fs is mounted*/
218 struct ffs_file_perms {
224 /* The endpoint files, filled by ffs_epfiles_create(),
225 * destroyed by ffs_epfiles_destroy(). */
226 struct ffs_epfile *epfiles;
229 /* Reference counter handling */
230 static void ffs_data_get(struct ffs_data *ffs);
231 static void ffs_data_put(struct ffs_data *ffs);
232 /* Creates new ffs_data object. */
233 static struct ffs_data *__must_check ffs_data_new(void) __attribute__((malloc));
235 /* Opened counter handling. */
236 static void ffs_data_opened(struct ffs_data *ffs);
237 static void ffs_data_closed(struct ffs_data *ffs);
239 /* Called with ffs->mutex held; take over ownerrship of data. */
240 static int __must_check
241 __ffs_data_got_descs(struct ffs_data *ffs, char *data, size_t len);
242 static int __must_check
243 __ffs_data_got_strings(struct ffs_data *ffs, char *data, size_t len);
246 /* The function structure ***************************************************/
250 struct ffs_function {
251 struct usb_configuration *conf;
252 struct usb_gadget *gadget;
253 struct ffs_data *ffs;
257 short *interfaces_nums;
259 struct usb_function function;
263 static struct ffs_function *ffs_func_from_usb(struct usb_function *f)
265 return container_of(f, struct ffs_function, function);
268 static void ffs_func_free(struct ffs_function *func);
271 static void ffs_func_eps_disable(struct ffs_function *func);
272 static int __must_check ffs_func_eps_enable(struct ffs_function *func);
275 static int ffs_func_bind(struct usb_configuration *,
276 struct usb_function *);
277 static void ffs_func_unbind(struct usb_configuration *,
278 struct usb_function *);
279 static int ffs_func_set_alt(struct usb_function *, unsigned, unsigned);
280 static void ffs_func_disable(struct usb_function *);
281 static int ffs_func_setup(struct usb_function *,
282 const struct usb_ctrlrequest *);
283 static void ffs_func_suspend(struct usb_function *);
284 static void ffs_func_resume(struct usb_function *);
287 static int ffs_func_revmap_ep(struct ffs_function *func, u8 num);
288 static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf);
292 /* The endpoints structures *************************************************/
295 struct usb_ep *ep; /* P: ffs->eps_lock */
296 struct usb_request *req; /* P: epfile->mutex */
298 /* [0]: full speed, [1]: high speed */
299 struct usb_endpoint_descriptor *descs[2];
303 int status; /* P: epfile->mutex */
307 /* Protects ep->ep and ep->req. */
309 wait_queue_head_t wait;
311 struct ffs_data *ffs;
312 struct ffs_ep *ep; /* P: ffs->eps_lock */
314 struct dentry *dentry;
318 unsigned char in; /* P: ffs->eps_lock */
319 unsigned char isoc; /* P: ffs->eps_lock */
325 static int __must_check ffs_epfiles_create(struct ffs_data *ffs);
326 static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count);
328 static struct inode *__must_check
329 ffs_sb_create_file(struct super_block *sb, const char *name, void *data,
330 const struct file_operations *fops,
331 struct dentry **dentry_p);
334 /* Misc helper functions ****************************************************/
336 static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
337 __attribute__((warn_unused_result, nonnull));
338 static char *ffs_prepare_buffer(const char * __user buf, size_t len)
339 __attribute__((warn_unused_result, nonnull));
342 /* Control file aka ep0 *****************************************************/
344 static void ffs_ep0_complete(struct usb_ep *ep, struct usb_request *req)
346 struct ffs_data *ffs = req->context;
348 complete_all(&ffs->ep0req_completion);
352 static int __ffs_ep0_queue_wait(struct ffs_data *ffs, char *data, size_t len)
354 struct usb_request *req = ffs->ep0req;
357 req->zero = len < le16_to_cpu(ffs->ev.setup.wLength);
359 spin_unlock_irq(&ffs->ev.waitq.lock);
364 INIT_COMPLETION(ffs->ep0req_completion);
366 ret = usb_ep_queue(ffs->gadget->ep0, req, GFP_ATOMIC);
367 if (unlikely(ret < 0))
370 ret = wait_for_completion_interruptible(&ffs->ep0req_completion);
372 usb_ep_dequeue(ffs->gadget->ep0, req);
376 ffs->setup_state = FFS_NO_SETUP;
377 return ffs->ep0req_status;
380 static int __ffs_ep0_stall(struct ffs_data *ffs)
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;
388 FDBG("bogus ep0 stall!\n");
394 static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
395 size_t len, loff_t *ptr)
397 struct ffs_data *ffs = file->private_data;
403 /* Fast check if setup was canceled */
404 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED)
408 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
409 if (unlikely(ret < 0))
414 switch (ffs->state) {
415 case FFS_READ_DESCRIPTORS:
416 case FFS_READ_STRINGS:
418 if (unlikely(len < 16)) {
423 data = ffs_prepare_buffer(buf, len);
424 if (unlikely(IS_ERR(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))
436 ffs->state = FFS_READ_STRINGS;
439 FINFO("read strings");
440 ret = __ffs_data_got_strings(ffs, data, len);
441 if (unlikely(ret < 0))
444 ret = ffs_epfiles_create(ffs);
446 ffs->state = FFS_CLOSING;
450 ffs->state = FFS_ACTIVE;
451 mutex_unlock(&ffs->mutex);
453 ret = functionfs_ready_callback(ffs);
454 if (unlikely(ret < 0)) {
455 ffs->state = FFS_CLOSING;
459 set_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags);
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:
479 case FFS_SETUP_PENDING:
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);
490 /* FFS_SETUP_PENDING and not stall */
491 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
493 spin_unlock_irq(&ffs->ev.waitq.lock);
495 data = ffs_prepare_buffer(buf, len);
496 if (unlikely(IS_ERR(data))) {
501 spin_lock_irq(&ffs->ev.waitq.lock);
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
513 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) {
516 spin_unlock_irq(&ffs->ev.waitq.lock);
518 /* unlocks spinlock */
519 ret = __ffs_ep0_queue_wait(ffs, data, len);
531 mutex_unlock(&ffs->mutex);
537 static ssize_t __ffs_ep0_read_events(struct ffs_data *ffs, char __user *buf,
540 /* We are holding ffs->ev.waitq.lock and ffs->mutex and we need
541 * to release them. */
543 struct usb_functionfs_event events[n];
546 memset(events, 0, sizeof events);
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;
556 if (n < ffs->ev.count) {
558 memmove(ffs->ev.types, ffs->ev.types + n,
559 ffs->ev.count * sizeof *ffs->ev.types);
564 spin_unlock_irq(&ffs->ev.waitq.lock);
565 mutex_unlock(&ffs->mutex);
567 return unlikely(__copy_to_user(buf, events, sizeof events))
568 ? -EFAULT : sizeof events;
572 static ssize_t ffs_ep0_read(struct file *file, char __user *buf,
573 size_t len, loff_t *ptr)
575 struct ffs_data *ffs = file->private_data;
582 /* Fast check if setup was canceled */
583 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED)
587 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
588 if (unlikely(ret < 0))
593 if (ffs->state != FFS_ACTIVE) {
599 /* We're called from user space, we can use _irq rather then
601 spin_lock_irq(&ffs->ev.waitq.lock);
603 switch (FFS_SETUP_STATE(ffs)) {
604 case FFS_SETUP_CANCELED:
609 n = len / sizeof(struct usb_functionfs_event);
615 if ((file->f_flags & O_NONBLOCK) && !ffs->ev.count) {
620 if (unlikely(wait_event_interruptible_exclusive_locked_irq(ffs->ev.waitq, ffs->ev.count))) {
625 return __ffs_ep0_read_events(ffs, buf,
626 min(n, (size_t)ffs->ev.count));
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);
636 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
638 spin_unlock_irq(&ffs->ev.waitq.lock);
641 data = kmalloc(len, GFP_KERNEL);
642 if (unlikely(!data)) {
648 spin_lock_irq(&ffs->ev.waitq.lock);
650 /* See ffs_ep0_write() */
651 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) {
656 /* unlocks spinlock */
657 ret = __ffs_ep0_queue_wait(ffs, data, len);
658 if (likely(ret > 0) && unlikely(__copy_to_user(buf, data, len)))
667 spin_unlock_irq(&ffs->ev.waitq.lock);
669 mutex_unlock(&ffs->mutex);
676 static int ffs_ep0_open(struct inode *inode, struct file *file)
678 struct ffs_data *ffs = inode->i_private;
682 if (unlikely(ffs->state == FFS_CLOSING))
685 file->private_data = ffs;
686 ffs_data_opened(ffs);
692 static int ffs_ep0_release(struct inode *inode, struct file *file)
694 struct ffs_data *ffs = file->private_data;
698 ffs_data_closed(ffs);
704 static long ffs_ep0_ioctl(struct file *file, unsigned code, unsigned long value)
706 struct ffs_data *ffs = file->private_data;
707 struct usb_gadget *gadget = ffs->gadget;
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) {
717 ret = gadget->ops->ioctl(gadget, code, value);
727 static const struct file_operations ffs_ep0_operations = {
728 .owner = THIS_MODULE,
731 .open = ffs_ep0_open,
732 .write = ffs_ep0_write,
733 .read = ffs_ep0_read,
734 .release = ffs_ep0_release,
735 .unlocked_ioctl = ffs_ep0_ioctl,
739 /* "Normal" endpoints operations ********************************************/
742 static void ffs_epfile_io_complete(struct usb_ep *_ep, struct usb_request *req)
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);
753 static ssize_t ffs_epfile_io(struct file *file,
754 char __user *buf, size_t len, int read)
756 struct ffs_epfile *epfile = file->private_data;
764 spin_unlock_irq(&epfile->ffs->eps_lock);
765 mutex_unlock(&epfile->mutex);
768 /* Are we still active? */
769 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE)) {
774 /* Wait for endpoint to be enabled */
777 if (file->f_flags & O_NONBLOCK) {
782 if (unlikely(wait_event_interruptible
783 (epfile->wait, (ep = epfile->ep)))) {
790 halt = !read == !epfile->in;
791 if (halt && epfile->isoc) {
796 /* Allocate & copy */
797 if (!halt && !data) {
798 data = kzalloc(len, GFP_KERNEL);
803 unlikely(__copy_from_user(data, buf, len))) {
809 /* We will be using request */
810 ret = ffs_mutex_lock(&epfile->mutex,
811 file->f_flags & O_NONBLOCK);
815 /* We're called from user space, we can use _irq rather then
817 spin_lock_irq(&epfile->ffs->eps_lock);
819 /* While we were acquiring mutex endpoint got disabled
821 } while (unlikely(epfile->ep != ep));
824 if (unlikely(halt)) {
825 if (likely(epfile->ep == ep) && !WARN_ON(!ep->ep))
826 usb_ep_set_halt(ep->ep);
827 spin_unlock_irq(&epfile->ffs->eps_lock);
830 /* Fire the request */
831 DECLARE_COMPLETION_ONSTACK(done);
833 struct usb_request *req = ep->req;
834 req->context = &done;
835 req->complete = ffs_epfile_io_complete;
839 ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
841 spin_unlock_irq(&epfile->ffs->eps_lock);
843 if (unlikely(ret < 0)) {
845 } else if (unlikely(wait_for_completion_interruptible(&done))) {
847 usb_ep_dequeue(ep->ep, req);
850 if (read && ret > 0 &&
851 unlikely(copy_to_user(buf, data, ret)))
856 mutex_unlock(&epfile->mutex);
864 ffs_epfile_write(struct file *file, const char __user *buf, size_t len,
869 return ffs_epfile_io(file, (char __user *)buf, len, 0);
873 ffs_epfile_read(struct file *file, char __user *buf, size_t len, loff_t *ptr)
877 return ffs_epfile_io(file, buf, len, 1);
881 ffs_epfile_open(struct inode *inode, struct file *file)
883 struct ffs_epfile *epfile = inode->i_private;
887 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
890 file->private_data = epfile;
891 ffs_data_opened(epfile->ffs);
897 ffs_epfile_release(struct inode *inode, struct file *file)
899 struct ffs_epfile *epfile = inode->i_private;
903 ffs_data_closed(epfile->ffs);
909 static long ffs_epfile_ioctl(struct file *file, unsigned code,
912 struct ffs_epfile *epfile = file->private_data;
917 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
920 spin_lock_irq(&epfile->ffs->eps_lock);
921 if (likely(epfile->ep)) {
923 case FUNCTIONFS_FIFO_STATUS:
924 ret = usb_ep_fifo_status(epfile->ep->ep);
926 case FUNCTIONFS_FIFO_FLUSH:
927 usb_ep_fifo_flush(epfile->ep->ep);
930 case FUNCTIONFS_CLEAR_HALT:
931 ret = usb_ep_clear_halt(epfile->ep->ep);
933 case FUNCTIONFS_ENDPOINT_REVMAP:
934 ret = epfile->ep->num;
942 spin_unlock_irq(&epfile->ffs->eps_lock);
948 static const struct file_operations ffs_epfile_operations = {
949 .owner = THIS_MODULE,
952 .open = ffs_epfile_open,
953 .write = ffs_epfile_write,
954 .read = ffs_epfile_read,
955 .release = ffs_epfile_release,
956 .unlocked_ioctl = ffs_epfile_ioctl,
961 /* File system and super block operations ***********************************/
964 * Mounting the filesystem creates a controller file, used first for
965 * function configuration then later for event monitoring.
969 static struct inode *__must_check
970 ffs_sb_make_inode(struct super_block *sb, void *data,
971 const struct file_operations *fops,
972 const struct inode_operations *iops,
973 struct ffs_file_perms *perms)
979 inode = new_inode(sb);
982 struct timespec current_time = CURRENT_TIME;
984 inode->i_mode = perms->mode;
985 inode->i_uid = perms->uid;
986 inode->i_gid = perms->gid;
987 inode->i_atime = current_time;
988 inode->i_mtime = current_time;
989 inode->i_ctime = current_time;
990 inode->i_private = data;
1001 /* Create "regular" file */
1003 static struct inode *ffs_sb_create_file(struct super_block *sb,
1004 const char *name, void *data,
1005 const struct file_operations *fops,
1006 struct dentry **dentry_p)
1008 struct ffs_data *ffs = sb->s_fs_info;
1009 struct dentry *dentry;
1010 struct inode *inode;
1014 dentry = d_alloc_name(sb->s_root, name);
1015 if (unlikely(!dentry))
1018 inode = ffs_sb_make_inode(sb, data, fops, NULL, &ffs->file_perms);
1019 if (unlikely(!inode)) {
1024 d_add(dentry, inode);
1034 static const struct super_operations ffs_sb_operations = {
1035 .statfs = simple_statfs,
1036 .drop_inode = generic_delete_inode,
1039 struct ffs_sb_fill_data {
1040 struct ffs_file_perms perms;
1042 const char *dev_name;
1045 static int ffs_sb_fill(struct super_block *sb, void *_data, int silent)
1047 struct ffs_sb_fill_data *data = _data;
1048 struct inode *inode;
1050 struct ffs_data *ffs;
1054 /* Initialize data */
1055 ffs = ffs_data_new();
1060 ffs->dev_name = data->dev_name;
1061 ffs->file_perms = data->perms;
1063 sb->s_fs_info = ffs;
1064 sb->s_blocksize = PAGE_CACHE_SIZE;
1065 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1066 sb->s_magic = FUNCTIONFS_MAGIC;
1067 sb->s_op = &ffs_sb_operations;
1068 sb->s_time_gran = 1;
1071 data->perms.mode = data->root_mode;
1072 inode = ffs_sb_make_inode(sb, NULL,
1073 &simple_dir_operations,
1074 &simple_dir_inode_operations,
1076 if (unlikely(!inode))
1078 d = d_alloc_root(inode);
1084 if (unlikely(!ffs_sb_create_file(sb, "ep0", ffs,
1085 &ffs_ep0_operations, NULL)))
1101 static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts)
1105 if (!opts || !*opts)
1109 char *end, *eq, *comma;
1110 unsigned long value;
1113 comma = strchr(opts, ',');
1118 eq = strchr(opts, '=');
1119 if (unlikely(!eq)) {
1120 FERR("'=' missing in %s", opts);
1126 value = simple_strtoul(eq + 1, &end, 0);
1127 if (unlikely(*end != ',' && *end != 0)) {
1128 FERR("%s: invalid value: %s", opts, eq + 1);
1132 /* Interpret option */
1133 switch (eq - opts) {
1135 if (!memcmp(opts, "rmode", 5))
1136 data->root_mode = (value & 0555) | S_IFDIR;
1137 else if (!memcmp(opts, "fmode", 5))
1138 data->perms.mode = (value & 0666) | S_IFREG;
1144 if (!memcmp(opts, "mode", 4)) {
1145 data->root_mode = (value & 0555) | S_IFDIR;
1146 data->perms.mode = (value & 0666) | S_IFREG;
1153 if (!memcmp(opts, "uid", 3))
1154 data->perms.uid = value;
1155 else if (!memcmp(opts, "gid", 3))
1156 data->perms.gid = value;
1163 FERR("%s: invalid option", opts);
1167 /* Next iteration */
1177 /* "mount -t functionfs dev_name /dev/function" ends up here */
1180 ffs_fs_get_sb(struct file_system_type *t, int flags,
1181 const char *dev_name, void *opts, struct vfsmount *mnt)
1183 struct ffs_sb_fill_data data = {
1185 .mode = S_IFREG | 0600,
1189 .root_mode = S_IFDIR | 0500,
1195 ret = functionfs_check_dev_callback(dev_name);
1196 if (unlikely(ret < 0))
1199 ret = ffs_fs_parse_opts(&data, opts);
1200 if (unlikely(ret < 0))
1203 data.dev_name = dev_name;
1204 return get_sb_single(t, flags, &data, ffs_sb_fill, mnt);
1208 ffs_fs_kill_sb(struct super_block *sb)
1214 kill_litter_super(sb);
1215 ptr = xchg(&sb->s_fs_info, NULL);
1220 static struct file_system_type ffs_fs_type = {
1221 .owner = THIS_MODULE,
1222 .name = "functionfs",
1223 .get_sb = ffs_fs_get_sb,
1224 .kill_sb = ffs_fs_kill_sb,
1229 /* Driver's main init/cleanup functions *************************************/
1232 static int functionfs_init(void)
1238 ret = register_filesystem(&ffs_fs_type);
1240 FINFO("file system registered");
1242 FERR("failed registering file system (%d)", ret);
1247 static void functionfs_cleanup(void)
1252 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);
1263 static void ffs_data_get(struct ffs_data *ffs)
1267 atomic_inc(&ffs->ref);
1270 static void ffs_data_opened(struct ffs_data *ffs)
1274 atomic_inc(&ffs->ref);
1275 atomic_inc(&ffs->opened);
1278 static void ffs_data_put(struct ffs_data *ffs)
1282 if (unlikely(atomic_dec_and_test(&ffs->ref))) {
1283 FINFO("%s(): freeing", __func__);
1284 ffs_data_clear(ffs);
1285 BUG_ON(mutex_is_locked(&ffs->mutex) ||
1286 spin_is_locked(&ffs->ev.waitq.lock) ||
1287 waitqueue_active(&ffs->ev.waitq) ||
1288 waitqueue_active(&ffs->ep0req_completion.wait));
1295 static void ffs_data_closed(struct ffs_data *ffs)
1299 if (atomic_dec_and_test(&ffs->opened)) {
1300 ffs->state = FFS_CLOSING;
1301 ffs_data_reset(ffs);
1308 static struct ffs_data *ffs_data_new(void)
1310 struct ffs_data *ffs = kzalloc(sizeof *ffs, GFP_KERNEL);
1316 atomic_set(&ffs->ref, 1);
1317 atomic_set(&ffs->opened, 0);
1318 ffs->state = FFS_READ_DESCRIPTORS;
1319 mutex_init(&ffs->mutex);
1320 spin_lock_init(&ffs->eps_lock);
1321 init_waitqueue_head(&ffs->ev.waitq);
1322 init_completion(&ffs->ep0req_completion);
1324 /* XXX REVISIT need to update it in some places, or do we? */
1325 ffs->ev.can_stall = 1;
1331 static void ffs_data_clear(struct ffs_data *ffs)
1335 if (test_and_clear_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags))
1336 functionfs_closed_callback(ffs);
1338 BUG_ON(ffs->gadget);
1341 ffs_epfiles_destroy(ffs->epfiles, ffs->eps_count);
1343 kfree(ffs->raw_descs);
1344 kfree(ffs->raw_strings);
1345 kfree(ffs->stringtabs);
1349 static void ffs_data_reset(struct ffs_data *ffs)
1353 ffs_data_clear(ffs);
1355 ffs->epfiles = NULL;
1356 ffs->raw_descs = NULL;
1357 ffs->raw_strings = NULL;
1358 ffs->stringtabs = NULL;
1360 ffs->raw_descs_length = 0;
1361 ffs->raw_fs_descs_length = 0;
1362 ffs->fs_descs_count = 0;
1363 ffs->hs_descs_count = 0;
1365 ffs->strings_count = 0;
1366 ffs->interfaces_count = 0;
1371 ffs->state = FFS_READ_DESCRIPTORS;
1372 ffs->setup_state = FFS_NO_SETUP;
1377 static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev)
1383 if (WARN_ON(ffs->state != FFS_ACTIVE
1384 || test_and_set_bit(FFS_FL_BOUND, &ffs->flags)))
1389 ffs->ep0req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
1390 if (unlikely(!ffs->ep0req))
1392 ffs->ep0req->complete = ffs_ep0_complete;
1393 ffs->ep0req->context = ffs;
1395 /* Get strings identifiers */
1396 for (count = ffs->strings_count, i = 0; i < count; ++i) {
1397 struct usb_gadget_strings **lang;
1399 int id = usb_string_id(cdev);
1400 if (unlikely(id < 0)) {
1401 usb_ep_free_request(cdev->gadget->ep0, ffs->ep0req);
1406 lang = ffs->stringtabs;
1408 (*lang)->strings[i].id = id;
1413 ffs->gadget = cdev->gadget;
1418 static void functionfs_unbind(struct ffs_data *ffs)
1422 if (!WARN_ON(!ffs->gadget)) {
1423 usb_ep_free_request(ffs->gadget->ep0, ffs->ep0req);
1431 static int ffs_epfiles_create(struct ffs_data *ffs)
1433 struct ffs_epfile *epfile, *epfiles;
1438 count = ffs->eps_count;
1439 epfiles = kzalloc(count * sizeof *epfiles, GFP_KERNEL);
1444 for (i = 1; i <= count; ++i, ++epfile) {
1446 mutex_init(&epfile->mutex);
1447 init_waitqueue_head(&epfile->wait);
1448 sprintf(epfiles->name, "ep%u", i);
1449 if (!unlikely(ffs_sb_create_file(ffs->sb, epfiles->name, epfile,
1450 &ffs_epfile_operations,
1451 &epfile->dentry))) {
1452 ffs_epfiles_destroy(epfiles, i - 1);
1457 ffs->epfiles = epfiles;
1462 static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count)
1464 struct ffs_epfile *epfile = epfiles;
1468 for (; count; --count, ++epfile) {
1469 BUG_ON(mutex_is_locked(&epfile->mutex) ||
1470 waitqueue_active(&epfile->wait));
1471 if (epfile->dentry) {
1472 d_delete(epfile->dentry);
1473 dput(epfile->dentry);
1474 epfile->dentry = NULL;
1482 static int functionfs_add(struct usb_composite_dev *cdev,
1483 struct usb_configuration *c,
1484 struct ffs_data *ffs)
1486 struct ffs_function *func;
1491 func = kzalloc(sizeof *func, GFP_KERNEL);
1492 if (unlikely(!func))
1495 func->function.name = "Function FS Gadget";
1496 func->function.strings = ffs->stringtabs;
1498 func->function.bind = ffs_func_bind;
1499 func->function.unbind = ffs_func_unbind;
1500 func->function.set_alt = ffs_func_set_alt;
1501 /*func->function.get_alt = ffs_func_get_alt;*/
1502 func->function.disable = ffs_func_disable;
1503 func->function.setup = ffs_func_setup;
1504 func->function.suspend = ffs_func_suspend;
1505 func->function.resume = ffs_func_resume;
1508 func->gadget = cdev->gadget;
1512 ret = usb_add_function(c, &func->function);
1514 ffs_func_free(func);
1519 static void ffs_func_free(struct ffs_function *func)
1523 ffs_data_put(func->ffs);
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. */
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);
1543 /* pending requests get nuked */
1545 usb_ep_disable(ep->ep);
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;
1563 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1565 struct usb_endpoint_descriptor *ds;
1566 ds = ep->descs[ep->descs[1] ? 1 : 0];
1568 ep->ep->driver_data = ep;
1569 ret = usb_ep_enable(ep->ep, ds);
1572 epfile->in = usb_endpoint_dir_in(ds);
1573 epfile->isoc = usb_endpoint_xfer_isoc(ds);
1578 wake_up(&epfile->wait);
1583 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1589 /* Parsing and building descriptors and strings *****************************/
1592 /* This validates if data pointed by data is a valid USB descriptor as
1593 * well as record how many interfaces, endpoints and strings are
1594 * required by given configuration. Returns address afther the
1595 * descriptor or NULL if data is invalid. */
1597 enum ffs_entity_type {
1598 FFS_DESCRIPTOR, FFS_INTERFACE, FFS_STRING, FFS_ENDPOINT
1601 typedef int (*ffs_entity_callback)(enum ffs_entity_type entity,
1603 struct usb_descriptor_header *desc,
1606 static int __must_check ffs_do_desc(char *data, unsigned len,
1607 ffs_entity_callback entity, void *priv)
1609 struct usb_descriptor_header *_ds = (void *)data;
1615 /* At least two bytes are required: length and type */
1617 FVDBG("descriptor too short");
1621 /* If we have at least as many bytes as the descriptor takes? */
1622 length = _ds->bLength;
1624 FVDBG("descriptor longer then available data");
1628 #define __entity_check_INTERFACE(val) 1
1629 #define __entity_check_STRING(val) (val)
1630 #define __entity_check_ENDPOINT(val) ((val) & USB_ENDPOINT_NUMBER_MASK)
1631 #define __entity(type, val) do { \
1632 FVDBG("entity " #type "(%02x)", (val)); \
1633 if (unlikely(!__entity_check_ ##type(val))) { \
1634 FVDBG("invalid entity's value"); \
1637 ret = entity(FFS_ ##type, &val, _ds, priv); \
1638 if (unlikely(ret < 0)) { \
1639 FDBG("entity " #type "(%02x); ret = %d", \
1645 /* Parse descriptor depending on type. */
1646 switch (_ds->bDescriptorType) {
1650 case USB_DT_DEVICE_QUALIFIER:
1651 /* function can't have any of those */
1652 FVDBG("descriptor reserved for gadget: %d", _ds->bDescriptorType);
1655 case USB_DT_INTERFACE: {
1656 struct usb_interface_descriptor *ds = (void *)_ds;
1657 FVDBG("interface descriptor");
1658 if (length != sizeof *ds)
1661 __entity(INTERFACE, ds->bInterfaceNumber);
1663 __entity(STRING, ds->iInterface);
1667 case USB_DT_ENDPOINT: {
1668 struct usb_endpoint_descriptor *ds = (void *)_ds;
1669 FVDBG("endpoint descriptor");
1670 if (length != USB_DT_ENDPOINT_SIZE &&
1671 length != USB_DT_ENDPOINT_AUDIO_SIZE)
1673 __entity(ENDPOINT, ds->bEndpointAddress);
1678 if (length != sizeof(struct usb_otg_descriptor))
1682 case USB_DT_INTERFACE_ASSOCIATION: {
1683 struct usb_interface_assoc_descriptor *ds = (void *)_ds;
1684 FVDBG("interface association descriptor");
1685 if (length != sizeof *ds)
1688 __entity(STRING, ds->iFunction);
1692 case USB_DT_OTHER_SPEED_CONFIG:
1693 case USB_DT_INTERFACE_POWER:
1695 case USB_DT_SECURITY:
1696 case USB_DT_CS_RADIO_CONTROL:
1698 FVDBG("unimplemented descriptor: %d", _ds->bDescriptorType);
1702 /* We should never be here */
1703 FVDBG("unknown descriptor: %d", _ds->bDescriptorType);
1707 FVDBG("invalid length: %d (descriptor %d)",
1708 _ds->bLength, _ds->bDescriptorType);
1713 #undef __entity_check_DESCRIPTOR
1714 #undef __entity_check_INTERFACE
1715 #undef __entity_check_STRING
1716 #undef __entity_check_ENDPOINT
1722 static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len,
1723 ffs_entity_callback entity, void *priv)
1725 const unsigned _len = len;
1726 unsigned long num = 0;
1736 /* Record "descriptor" entitny */
1737 ret = entity(FFS_DESCRIPTOR, (u8 *)num, (void *)data, priv);
1738 if (unlikely(ret < 0)) {
1739 FDBG("entity DESCRIPTOR(%02lx); ret = %d", num, ret);
1746 ret = ffs_do_desc(data, len, entity, priv);
1747 if (unlikely(ret < 0)) {
1748 FDBG("%s returns %d", __func__, ret);
1759 static int __ffs_data_do_entity(enum ffs_entity_type type,
1760 u8 *valuep, struct usb_descriptor_header *desc,
1763 struct ffs_data *ffs = priv;
1768 case FFS_DESCRIPTOR:
1772 /* Interfaces are indexed from zero so if we
1773 * encountered interface "n" then there are at least
1774 * "n+1" interfaces. */
1775 if (*valuep >= ffs->interfaces_count)
1776 ffs->interfaces_count = *valuep + 1;
1780 /* Strings are indexed from 1 (0 is magic ;) reserved
1781 * for languages list or some such) */
1782 if (*valuep > ffs->strings_count)
1783 ffs->strings_count = *valuep;
1787 /* Endpoints are indexed from 1 as well. */
1788 if ((*valuep & USB_ENDPOINT_NUMBER_MASK) > ffs->eps_count)
1789 ffs->eps_count = (*valuep & USB_ENDPOINT_NUMBER_MASK);
1797 static int __ffs_data_got_descs(struct ffs_data *ffs,
1798 char *const _data, size_t len)
1800 unsigned fs_count, hs_count;
1801 int fs_len, ret = -EINVAL;
1806 if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_DESCRIPTORS_MAGIC ||
1807 get_unaligned_le32(data + 4) != len))
1809 fs_count = get_unaligned_le32(data + 8);
1810 hs_count = get_unaligned_le32(data + 12);
1812 if (!fs_count && !hs_count)
1818 if (likely(fs_count)) {
1819 fs_len = ffs_do_descs(fs_count, data, len,
1820 __ffs_data_do_entity, ffs);
1821 if (unlikely(fs_len < 0)) {
1832 if (likely(hs_count)) {
1833 ret = ffs_do_descs(hs_count, data, len,
1834 __ffs_data_do_entity, ffs);
1835 if (unlikely(ret < 0))
1841 if (unlikely(len != ret))
1844 ffs->raw_fs_descs_length = fs_len;
1845 ffs->raw_descs_length = fs_len + ret;
1846 ffs->raw_descs = _data;
1847 ffs->fs_descs_count = fs_count;
1848 ffs->hs_descs_count = hs_count;
1861 static int __ffs_data_got_strings(struct ffs_data *ffs,
1862 char *const _data, size_t len)
1864 u32 str_count, needed_count, lang_count;
1865 struct usb_gadget_strings **stringtabs, *t;
1866 struct usb_string *strings, *s;
1867 const char *data = _data;
1871 if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_STRINGS_MAGIC ||
1872 get_unaligned_le32(data + 4) != len))
1874 str_count = get_unaligned_le32(data + 8);
1875 lang_count = get_unaligned_le32(data + 12);
1877 /* if one is zero the other must be zero */
1878 if (unlikely(!str_count != !lang_count))
1881 /* Do we have at least as many strings as descriptors need? */
1882 needed_count = ffs->strings_count;
1883 if (unlikely(str_count < needed_count))
1886 /* If we don't need any strings just return and free all
1888 if (!needed_count) {
1895 /* Allocate everything in one chunk so there's less
1898 struct usb_gadget_strings *stringtabs[lang_count + 1];
1899 struct usb_gadget_strings stringtab[lang_count];
1900 struct usb_string strings[lang_count*(needed_count+1)];
1904 d = kmalloc(sizeof *d, GFP_KERNEL);
1910 stringtabs = d->stringtabs;
1914 *stringtabs++ = t++;
1918 stringtabs = d->stringtabs;
1924 /* For each language */
1928 do { /* lang_count > 0 so we can use do-while */
1929 unsigned needed = needed_count;
1931 if (unlikely(len < 3))
1933 t->language = get_unaligned_le16(data);
1940 /* For each string */
1941 do { /* str_count > 0 so we can use do-while */
1942 size_t length = strnlen(data, len);
1944 if (unlikely(length == len))
1947 /* user may provide more strings then we need,
1948 * if that's the case we simply ingore the
1950 if (likely(needed)) {
1951 /* s->id will be set while adding
1952 * function to configuration so for
1953 * now just leave garbage here. */
1961 } while (--str_count);
1963 s->id = 0; /* terminator */
1967 } while (--lang_count);
1969 /* Some garbage left? */
1974 ffs->stringtabs = stringtabs;
1975 ffs->raw_strings = _data;
1989 /* Events handling and management *******************************************/
1991 static void __ffs_event_add(struct ffs_data *ffs,
1992 enum usb_functionfs_event_type type)
1994 enum usb_functionfs_event_type rem_type1, rem_type2 = type;
1997 /* Abort any unhandled setup */
1998 /* We do not need to worry about some cmpxchg() changing value
1999 * of ffs->setup_state without holding the lock because when
2000 * state is FFS_SETUP_PENDING cmpxchg() in several places in
2001 * the source does nothing. */
2002 if (ffs->setup_state == FFS_SETUP_PENDING)
2003 ffs->setup_state = FFS_SETUP_CANCELED;
2006 case FUNCTIONFS_RESUME:
2007 rem_type2 = FUNCTIONFS_SUSPEND;
2009 case FUNCTIONFS_SUSPEND:
2010 case FUNCTIONFS_SETUP:
2012 /* discard all similar events */
2015 case FUNCTIONFS_BIND:
2016 case FUNCTIONFS_UNBIND:
2017 case FUNCTIONFS_DISABLE:
2018 case FUNCTIONFS_ENABLE:
2019 /* discard everything other then power management. */
2020 rem_type1 = FUNCTIONFS_SUSPEND;
2021 rem_type2 = FUNCTIONFS_RESUME;
2030 u8 *ev = ffs->ev.types, *out = ev;
2031 unsigned n = ffs->ev.count;
2032 for (; n; --n, ++ev)
2033 if ((*ev == rem_type1 || *ev == rem_type2) == neg)
2036 FVDBG("purging event %d", *ev);
2037 ffs->ev.count = out - ffs->ev.types;
2040 FVDBG("adding event %d", type);
2041 ffs->ev.types[ffs->ev.count++] = type;
2042 wake_up_locked(&ffs->ev.waitq);
2045 static void ffs_event_add(struct ffs_data *ffs,
2046 enum usb_functionfs_event_type type)
2048 unsigned long flags;
2049 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2050 __ffs_event_add(ffs, type);
2051 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2055 /* Bind/unbind USB function hooks *******************************************/
2057 static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
2058 struct usb_descriptor_header *desc,
2061 struct usb_endpoint_descriptor *ds = (void *)desc;
2062 struct ffs_function *func = priv;
2063 struct ffs_ep *ffs_ep;
2065 /* If hs_descriptors is not NULL then we are reading hs
2066 * descriptors now */
2067 const int isHS = func->function.hs_descriptors != NULL;
2070 if (type != FFS_DESCRIPTOR)
2074 func->function.hs_descriptors[(long)valuep] = desc;
2076 func->function.descriptors[(long)valuep] = desc;
2078 if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT)
2081 idx = (ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK) - 1;
2082 ffs_ep = func->eps + idx;
2084 if (unlikely(ffs_ep->descs[isHS])) {
2085 FVDBG("two %sspeed descriptors for EP %d",
2086 isHS ? "high" : "full",
2087 ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
2090 ffs_ep->descs[isHS] = ds;
2092 ffs_dump_mem(": Original ep desc", ds, ds->bLength);
2094 ds->bEndpointAddress = ffs_ep->descs[0]->bEndpointAddress;
2095 if (!ds->wMaxPacketSize)
2096 ds->wMaxPacketSize = ffs_ep->descs[0]->wMaxPacketSize;
2098 struct usb_request *req;
2101 FVDBG("autoconfig");
2102 ep = usb_ep_autoconfig(func->gadget, ds);
2105 ep->driver_data = func->eps + idx;;
2107 req = usb_ep_alloc_request(ep, GFP_KERNEL);
2113 func->eps_revmap[ds->bEndpointAddress &
2114 USB_ENDPOINT_NUMBER_MASK] = idx + 1;
2116 ffs_dump_mem(": Rewritten ep desc", ds, ds->bLength);
2122 static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep,
2123 struct usb_descriptor_header *desc,
2126 struct ffs_function *func = priv;
2132 case FFS_DESCRIPTOR:
2133 /* Handled in previous pass by __ffs_func_bind_do_descs() */
2138 if (func->interfaces_nums[idx] < 0) {
2139 int id = usb_interface_id(func->conf, &func->function);
2140 if (unlikely(id < 0))
2142 func->interfaces_nums[idx] = id;
2144 newValue = func->interfaces_nums[idx];
2148 /* String' IDs are allocated when fsf_data is bound to cdev */
2149 newValue = func->ffs->stringtabs[0]->strings[*valuep - 1].id;
2153 /* USB_DT_ENDPOINT are handled in
2154 * __ffs_func_bind_do_descs(). */
2155 if (desc->bDescriptorType == USB_DT_ENDPOINT)
2158 idx = (*valuep & USB_ENDPOINT_NUMBER_MASK) - 1;
2159 if (unlikely(!func->eps[idx].ep))
2163 struct usb_endpoint_descriptor **descs;
2164 descs = func->eps[idx].descs;
2165 newValue = descs[descs[0] ? 0 : 1]->bEndpointAddress;
2170 FVDBG("%02x -> %02x", *valuep, newValue);
2175 static int ffs_func_bind(struct usb_configuration *c,
2176 struct usb_function *f)
2178 struct ffs_function *func = ffs_func_from_usb(f);
2179 struct ffs_data *ffs = func->ffs;
2181 const int full = !!func->ffs->fs_descs_count;
2182 const int high = gadget_is_dualspeed(func->gadget) &&
2183 func->ffs->hs_descs_count;
2187 /* Make it a single chunk, less management later on */
2189 struct ffs_ep eps[ffs->eps_count];
2190 struct usb_descriptor_header
2191 *fs_descs[full ? ffs->fs_descs_count + 1 : 0];
2192 struct usb_descriptor_header
2193 *hs_descs[high ? ffs->hs_descs_count + 1 : 0];
2194 short inums[ffs->interfaces_count];
2195 char raw_descs[high ? ffs->raw_descs_length
2196 : ffs->raw_fs_descs_length];
2201 /* Only high speed but not supported by gadget? */
2202 if (unlikely(!(full | high)))
2206 data = kmalloc(sizeof *data, GFP_KERNEL);
2207 if (unlikely(!data))
2211 memset(data->eps, 0, sizeof data->eps);
2212 memcpy(data->raw_descs, ffs->raw_descs + 16, sizeof data->raw_descs);
2213 memset(data->inums, 0xff, sizeof data->inums);
2214 for (ret = ffs->eps_count; ret; --ret)
2215 data->eps[ret].num = -1;
2218 func->eps = data->eps;
2219 func->interfaces_nums = data->inums;
2221 /* Go throught all the endpoint descriptors and allocate
2222 * endpoints first, so that later we can rewrite the endpoint
2223 * numbers without worying that it may be described later on. */
2225 func->function.descriptors = data->fs_descs;
2226 ret = ffs_do_descs(ffs->fs_descs_count,
2228 sizeof data->raw_descs,
2229 __ffs_func_bind_do_descs, func);
2230 if (unlikely(ret < 0))
2237 func->function.hs_descriptors = data->hs_descs;
2238 ret = ffs_do_descs(ffs->hs_descs_count,
2239 data->raw_descs + ret,
2240 (sizeof data->raw_descs) - ret,
2241 __ffs_func_bind_do_descs, func);
2244 /* Now handle interface numbers allocation and interface and
2245 * enpoint numbers rewritting. We can do that in one go
2247 ret = ffs_do_descs(ffs->fs_descs_count +
2248 (high ? ffs->hs_descs_count : 0),
2249 data->raw_descs, sizeof data->raw_descs,
2250 __ffs_func_bind_do_nums, func);
2251 if (unlikely(ret < 0))
2254 /* And we're done */
2255 ffs_event_add(ffs, FUNCTIONFS_BIND);
2259 /* XXX Do we need to release all claimed endpoints here? */
2264 /* Other USB function hooks *************************************************/
2266 static void ffs_func_unbind(struct usb_configuration *c,
2267 struct usb_function *f)
2269 struct ffs_function *func = ffs_func_from_usb(f);
2270 struct ffs_data *ffs = func->ffs;
2274 if (ffs->func == func) {
2275 ffs_func_eps_disable(func);
2279 ffs_event_add(ffs, FUNCTIONFS_UNBIND);
2281 ffs_func_free(func);
2285 static int ffs_func_set_alt(struct usb_function *f,
2286 unsigned interface, unsigned alt)
2288 struct ffs_function *func = ffs_func_from_usb(f);
2289 struct ffs_data *ffs = func->ffs;
2292 if (alt != (unsigned)-1) {
2293 intf = ffs_func_revmap_intf(func, interface);
2294 if (unlikely(intf < 0))
2299 ffs_func_eps_disable(ffs->func);
2301 if (ffs->state != FFS_ACTIVE)
2304 if (alt == (unsigned)-1) {
2306 ffs_event_add(ffs, FUNCTIONFS_DISABLE);
2311 ret = ffs_func_eps_enable(func);
2312 if (likely(ret >= 0))
2313 ffs_event_add(ffs, FUNCTIONFS_ENABLE);
2317 static void ffs_func_disable(struct usb_function *f)
2319 ffs_func_set_alt(f, 0, (unsigned)-1);
2322 static int ffs_func_setup(struct usb_function *f,
2323 const struct usb_ctrlrequest *creq)
2325 struct ffs_function *func = ffs_func_from_usb(f);
2326 struct ffs_data *ffs = func->ffs;
2327 unsigned long flags;
2332 FVDBG("creq->bRequestType = %02x", creq->bRequestType);
2333 FVDBG("creq->bRequest = %02x", creq->bRequest);
2334 FVDBG("creq->wValue = %04x", le16_to_cpu(creq->wValue));
2335 FVDBG("creq->wIndex = %04x", le16_to_cpu(creq->wIndex));
2336 FVDBG("creq->wLength = %04x", le16_to_cpu(creq->wLength));
2338 /* Most requests directed to interface go throught here
2339 * (notable exceptions are set/get interface) so we need to
2340 * handle them. All other either handled by composite or
2341 * passed to usb_configuration->setup() (if one is set). No
2342 * matter, we will handle requests directed to endpoint here
2343 * as well (as it's straightforward) but what to do with any
2346 if (ffs->state != FFS_ACTIVE)
2349 switch (creq->bRequestType & USB_RECIP_MASK) {
2350 case USB_RECIP_INTERFACE:
2351 ret = ffs_func_revmap_intf(func, le16_to_cpu(creq->wIndex));
2352 if (unlikely(ret < 0))
2356 case USB_RECIP_ENDPOINT:
2357 ret = ffs_func_revmap_ep(func, le16_to_cpu(creq->wIndex));
2358 if (unlikely(ret < 0))
2366 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2367 ffs->ev.setup = *creq;
2368 ffs->ev.setup.wIndex = cpu_to_le16(ret);
2369 __ffs_event_add(ffs, FUNCTIONFS_SETUP);
2370 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2375 static void ffs_func_suspend(struct usb_function *f)
2378 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_SUSPEND);
2381 static void ffs_func_resume(struct usb_function *f)
2384 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_RESUME);
2389 /* Enpoint and interface numbers reverse mapping ****************************/
2391 static int ffs_func_revmap_ep(struct ffs_function *func, u8 num)
2393 num = func->eps_revmap[num & USB_ENDPOINT_NUMBER_MASK];
2394 return num ? num : -EDOM;
2397 static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf)
2399 short *nums = func->interfaces_nums;
2400 unsigned count = func->ffs->interfaces_count;
2402 for (; count; --count, ++nums) {
2403 if (*nums >= 0 && *nums == intf)
2404 return nums - func->interfaces_nums;
2411 /* Misc helper functions ****************************************************/
2413 static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
2416 ? likely(mutex_trylock(mutex)) ? 0 : -EAGAIN
2417 : mutex_lock_interruptible(mutex);
2421 static char *ffs_prepare_buffer(const char * __user buf, size_t len)
2428 data = kmalloc(len, GFP_KERNEL);
2429 if (unlikely(!data))
2430 return ERR_PTR(-ENOMEM);
2432 if (unlikely(__copy_from_user(data, buf, len))) {
2434 return ERR_PTR(-EFAULT);
2437 FVDBG("Buffer from user space:");
2438 ffs_dump_mem("", data, len);