Converting FunctionFS to the new function interface requires converting
the USB FunctionFS's function code and its users.
This patch converts the f_fs.c to the new function interface.
The file is now compiled into a separate usb_f_fs.ko module.
The old function interface is provided by means of preprocessor
conditional directives. After all users are converted, the old interface
can be removed.

Signed-off-by: Andrzej Pietrasiewicz <andrze...@samsung.com>
Signed-off-by: Kyungmin Park <kyungmin.p...@samsung.com>
---
 drivers/usb/gadget/Kconfig  |    3 +
 drivers/usb/gadget/Makefile |    2 +
 drivers/usb/gadget/f_fs.c   |  355 +++++++++++++++----------------------------
 drivers/usb/gadget/f_fs.h   |  246 ++++++++++++++++++++++++++++++
 drivers/usb/gadget/g_ffs.c  |   19 +++-
 5 files changed, 391 insertions(+), 234 deletions(-)
 create mode 100644 drivers/usb/gadget/f_fs.h

diff --git a/drivers/usb/gadget/Kconfig b/drivers/usb/gadget/Kconfig
index acadde2..0d3d87a 100644
--- a/drivers/usb/gadget/Kconfig
+++ b/drivers/usb/gadget/Kconfig
@@ -578,6 +578,9 @@ config USB_F_SUBSET
 config USB_F_RNDIS
        tristate
 
+config USB_F_FS
+       tristate
+
 choice
        tristate "USB Gadget Drivers"
        default USB_ETH
diff --git a/drivers/usb/gadget/Makefile b/drivers/usb/gadget/Makefile
index 6f9f4d9..65fbaf1 100644
--- a/drivers/usb/gadget/Makefile
+++ b/drivers/usb/gadget/Makefile
@@ -95,3 +95,5 @@ usb_f_ecm_subset-y            := f_subset.o
 obj-$(CONFIG_USB_F_ECM)                += usb_f_ecm_subset.o
 usb_f_rndis-y                  := f_rndis.o rndis.o
 obj-$(CONFIG_USB_F_RNDIS)      += usb_f_rndis.o
+usb_f_fs-y                     := f_fs.o
+obj-$(CONFIG_USB_F_FS)         += usb_f_fs.o
diff --git a/drivers/usb/gadget/f_fs.c b/drivers/usb/gadget/f_fs.c
index f5a66e9..38ba133 100644
--- a/drivers/usb/gadget/f_fs.c
+++ b/drivers/usb/gadget/f_fs.c
@@ -18,6 +18,7 @@
 /* #define DEBUG */
 /* #define VERBOSE_DEBUG */
 
+#include <linux/module.h>
 #include <linux/blkdev.h>
 #include <linux/pagemap.h>
 #include <linux/export.h>
@@ -27,241 +28,23 @@
 #include <linux/usb/composite.h>
 #include <uapi/linux/usb/functionfs.h>
 
+#include "f_fs.h"
 
 #define FUNCTIONFS_MAGIC       0xa647361 /* Chosen by a honest dice roll ;) */
 
-
-/* Debugging ****************************************************************/
-
-#ifdef VERBOSE_DEBUG
-#ifndef pr_vdebug
-#  define pr_vdebug pr_debug
-#endif /* pr_vdebug */
-#  define ffs_dump_mem(prefix, ptr, len) \
-       print_hex_dump_bytes(pr_fmt(prefix ": "), DUMP_PREFIX_NONE, ptr, len)
-#else
-#ifndef pr_vdebug
-#  define pr_vdebug(...)                 do { } while (0)
-#endif /* pr_vdebug */
-#  define ffs_dump_mem(prefix, ptr, len) do { } while (0)
-#endif /* VERBOSE_DEBUG */
-
-#define ENTER()    pr_vdebug("%s()\n", __func__)
-
 struct ffs_data;
 struct usb_composite_dev;
 struct usb_configuration;
 
-
-static int  functionfs_init(void) __attribute__((warn_unused_result));
-static void functionfs_cleanup(void);
-
-static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev 
*cdev)
-       __attribute__((warn_unused_result, nonnull));
-static void functionfs_unbind(struct ffs_data *ffs)
-       __attribute__((nonnull));
+#ifdef USB_FFS_INCLUDED
 
 static int functionfs_bind_config(struct usb_composite_dev *cdev,
                                  struct usb_configuration *c,
                                  struct ffs_data *ffs)
        __attribute__((warn_unused_result, nonnull));
+#endif
 
 
-static int functionfs_ready_callback(struct ffs_data *ffs)
-       __attribute__((warn_unused_result, nonnull));
-static void functionfs_closed_callback(struct ffs_data *ffs)
-       __attribute__((nonnull));
-static void *functionfs_acquire_dev_callback(const char *dev_name)
-       __attribute__((warn_unused_result, nonnull));
-static void functionfs_release_dev_callback(struct ffs_data *ffs_data)
-       __attribute__((nonnull));
-
-/* The data structure and setup file ****************************************/
-
-enum ffs_state {
-       /*
-        * Waiting for descriptors and strings.
-        *
-        * In this state no open(2), read(2) or write(2) on epfiles
-        * may succeed (which should not be the problem as there
-        * should be no such files opened in the first place).
-        */
-       FFS_READ_DESCRIPTORS,
-       FFS_READ_STRINGS,
-
-       /*
-        * We've got descriptors and strings.  We are or have called
-        * functionfs_ready_callback().  functionfs_bind() may have
-        * been called but we don't know.
-        *
-        * This is the only state in which operations on epfiles may
-        * succeed.
-        */
-       FFS_ACTIVE,
-
-       /*
-        * All endpoints have been closed.  This state is also set if
-        * we encounter an unrecoverable error.  The only
-        * unrecoverable error is situation when after reading strings
-        * from user space we fail to initialise epfiles or
-        * functionfs_ready_callback() returns with error (<0).
-        *
-        * In this state no open(2), read(2) or write(2) (both on ep0
-        * as well as epfile) may succeed (at this point epfiles are
-        * unlinked and all closed so this is not a problem; ep0 is
-        * also closed but ep0 file exists and so open(2) on ep0 must
-        * fail).
-        */
-       FFS_CLOSING
-};
-
-
-enum ffs_setup_state {
-       /* There is no setup request pending. */
-       FFS_NO_SETUP,
-       /*
-        * User has read events and there was a setup request event
-        * there.  The next read/write on ep0 will handle the
-        * request.
-        */
-       FFS_SETUP_PENDING,
-       /*
-        * There was event pending but before user space handled it
-        * some other event was introduced which canceled existing
-        * setup.  If this state is set read/write on ep0 return
-        * -EIDRM.  This state is only set when adding event.
-        */
-       FFS_SETUP_CANCELED
-};
-
-
-
-struct ffs_epfile;
-struct ffs_function;
-
-struct ffs_data {
-       struct usb_gadget               *gadget;
-
-       /*
-        * Protect access read/write operations, only one read/write
-        * at a time.  As a consequence protects ep0req and company.
-        * While setup request is being processed (queued) this is
-        * held.
-        */
-       struct mutex                    mutex;
-
-       /*
-        * Protect access to endpoint related structures (basically
-        * usb_ep_queue(), usb_ep_dequeue(), etc. calls) except for
-        * endpoint zero.
-        */
-       spinlock_t                      eps_lock;
-
-       /*
-        * XXX REVISIT do we need our own request? Since we are not
-        * handling setup requests immediately user space may be so
-        * slow that another setup will be sent to the gadget but this
-        * time not to us but another function and then there could be
-        * a race.  Is that the case? Or maybe we can use cdev->req
-        * after all, maybe we just need some spinlock for that?
-        */
-       struct usb_request              *ep0req;                /* P: mutex */
-       struct completion               ep0req_completion;      /* P: mutex */
-       int                             ep0req_status;          /* P: mutex */
-
-       /* reference counter */
-       atomic_t                        ref;
-       /* how many files are opened (EP0 and others) */
-       atomic_t                        opened;
-
-       /* EP0 state */
-       enum ffs_state                  state;
-
-       /*
-        * Possible transitions:
-        * + FFS_NO_SETUP       -> FFS_SETUP_PENDING  -- P: ev.waitq.lock
-        *               happens only in ep0 read which is P: mutex
-        * + FFS_SETUP_PENDING  -> FFS_NO_SETUP       -- P: ev.waitq.lock
-        *               happens only in ep0 i/o  which is P: mutex
-        * + FFS_SETUP_PENDING  -> FFS_SETUP_CANCELED -- P: ev.waitq.lock
-        * + FFS_SETUP_CANCELED -> FFS_NO_SETUP       -- cmpxchg
-        */
-       enum ffs_setup_state            setup_state;
-
-#define FFS_SETUP_STATE(ffs)                                   \
-       ((enum ffs_setup_state)cmpxchg(&(ffs)->setup_state,     \
-                                      FFS_SETUP_CANCELED, FFS_NO_SETUP))
-
-       /* Events & such. */
-       struct {
-               u8                              types[4];
-               unsigned short                  count;
-               /* XXX REVISIT need to update it in some places, or do we? */
-               unsigned short                  can_stall;
-               struct usb_ctrlrequest          setup;
-
-               wait_queue_head_t               waitq;
-       } ev; /* the whole structure, P: ev.waitq.lock */
-
-       /* Flags */
-       unsigned long                   flags;
-#define FFS_FL_CALL_CLOSED_CALLBACK 0
-#define FFS_FL_BOUND                1
-
-       /* Active function */
-       struct ffs_function             *func;
-
-       /*
-        * Device name, write once when file system is mounted.
-        * Intended for user to read if she wants.
-        */
-       const char                      *dev_name;
-       /* Private data for our user (ie. gadget).  Managed by user. */
-       void                            *private_data;
-
-       /* filled by __ffs_data_got_descs() */
-       /*
-        * Real descriptors are 16 bytes after raw_descs (so you need
-        * to skip 16 bytes (ie. ffs->raw_descs + 16) to get to the
-        * first full speed descriptor).  raw_descs_length and
-        * raw_fs_descs_length do not have those 16 bytes added.
-        */
-       const void                      *raw_descs;
-       unsigned                        raw_descs_length;
-       unsigned                        raw_fs_descs_length;
-       unsigned                        fs_descs_count;
-       unsigned                        hs_descs_count;
-
-       unsigned short                  strings_count;
-       unsigned short                  interfaces_count;
-       unsigned short                  eps_count;
-       unsigned short                  _pad1;
-
-       /* filled by __ffs_data_got_strings() */
-       /* ids in stringtabs are set in functionfs_bind() */
-       const void                      *raw_strings;
-       struct usb_gadget_strings       **stringtabs;
-
-       /*
-        * File system's super block, write once when file system is
-        * mounted.
-        */
-       struct super_block              *sb;
-
-       /* File permissions, written once when fs is mounted */
-       struct ffs_file_perms {
-               umode_t                         mode;
-               kuid_t                          uid;
-               kgid_t                          gid;
-       }                               file_perms;
-
-       /*
-        * The endpoint files, filled by ffs_epfiles_create(),
-        * destroyed by ffs_epfiles_destroy().
-        */
-       struct ffs_epfile               *epfiles;
-};
-
 /* Reference counter handling */
 static void ffs_data_get(struct ffs_data *ffs);
 static void ffs_data_put(struct ffs_data *ffs);
@@ -321,6 +104,12 @@ static void ffs_func_resume(struct usb_function *);
 static int ffs_func_revmap_ep(struct ffs_function *func, u8 num);
 static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf);
 
+/* FunctionFS callbacks *****************************************************/
+
+static int (*ffs_ready_cb)(struct ffs_data *ffs);
+static void (*ffs_closed_cb)(struct ffs_data *ffs);
+static void *(*ffs_acquire_dev_cb)(const char *dev_name);
+static void (*ffs_release_dev_cb)(struct ffs_data *ffs_data);
 
 /* The endpoints structures *************************************************/
 
@@ -487,7 +276,7 @@ static ssize_t ffs_ep0_write(struct file *file, const char 
__user *buf,
                        ffs->state = FFS_ACTIVE;
                        mutex_unlock(&ffs->mutex);
 
-                       ret = functionfs_ready_callback(ffs);
+                       ret = ffs_ready_cb(ffs);
                        if (unlikely(ret < 0)) {
                                ffs->state = FFS_CLOSING;
                                return ret;
@@ -1229,7 +1018,7 @@ ffs_fs_mount(struct file_system_type *t, int flags,
        if (unlikely(ret < 0))
                return ERR_PTR(ret);
 
-       ffs_dev = functionfs_acquire_dev_callback(dev_name);
+       ffs_dev = ffs_acquire_dev_cb(dev_name);
        if (IS_ERR(ffs_dev))
                return ffs_dev;
 
@@ -1239,7 +1028,7 @@ ffs_fs_mount(struct file_system_type *t, int flags,
 
        /* data.ffs_data is set by ffs_sb_fill */
        if (IS_ERR(rv))
-               functionfs_release_dev_callback(data.ffs_data);
+               ffs_release_dev_cb(data.ffs_data);
 
        return rv;
 }
@@ -1251,7 +1040,7 @@ ffs_fs_kill_sb(struct super_block *sb)
 
        kill_litter_super(sb);
        if (sb->s_fs_info) {
-               functionfs_release_dev_callback(sb->s_fs_info);
+               ffs_release_dev_cb(sb->s_fs_info);
                ffs_data_put(sb->s_fs_info);
        }
 }
@@ -1264,15 +1053,26 @@ static struct file_system_type ffs_fs_type = {
 };
 MODULE_ALIAS_FS("functionfs");
 
-
 /* Driver's main init/cleanup functions *************************************/
 
-static int functionfs_init(void)
+int functionfs_init(struct ffs_callback_ops *cb_ops)
 {
        int ret;
 
        ENTER();
 
+       if (!cb_ops || !cb_ops->ready || !cb_ops->close ||
+           !cb_ops->acquire_dev || !cb_ops->release_dev) {
+               ret = -ENODEV;
+               pr_err("no callback operations for file system (%d)\n", ret);
+               return ret;
+       }
+
+       ffs_ready_cb = cb_ops->ready;
+       ffs_closed_cb = cb_ops->close;
+       ffs_acquire_dev_cb = cb_ops->acquire_dev;
+       ffs_release_dev_cb = cb_ops->release_dev;
+
        ret = register_filesystem(&ffs_fs_type);
        if (likely(!ret))
                pr_info("file system registered\n");
@@ -1281,15 +1081,20 @@ static int functionfs_init(void)
 
        return ret;
 }
+#ifndef USB_FFS_INCLUDED
+EXPORT_SYMBOL(functionfs_init);
+#endif
 
-static void functionfs_cleanup(void)
+void functionfs_cleanup(void)
 {
        ENTER();
 
        pr_info("unloading\n");
        unregister_filesystem(&ffs_fs_type);
 }
-
+#ifndef USB_FFS_INCLUDED
+EXPORT_SYMBOL(functionfs_cleanup);
+#endif
 
 /* ffs_data and ffs_function construction and destruction code **************/
 
@@ -1364,7 +1169,7 @@ static void ffs_data_clear(struct ffs_data *ffs)
        ENTER();
 
        if (test_and_clear_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags))
-               functionfs_closed_callback(ffs);
+               ffs_closed_cb(ffs);
 
        BUG_ON(ffs->gadget);
 
@@ -1404,7 +1209,7 @@ static void ffs_data_reset(struct ffs_data *ffs)
 }
 
 
-static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev 
*cdev)
+int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev)
 {
        struct usb_gadget_strings **lang;
        int first_id;
@@ -1437,8 +1242,11 @@ static int functionfs_bind(struct ffs_data *ffs, struct 
usb_composite_dev *cdev)
        ffs_data_get(ffs);
        return 0;
 }
+#ifndef USB_FFS_INCLUDED
+EXPORT_SYMBOL(functionfs_bind);
+#endif
 
-static void functionfs_unbind(struct ffs_data *ffs)
+void functionfs_unbind(struct ffs_data *ffs)
 {
        ENTER();
 
@@ -1450,6 +1258,9 @@ static void functionfs_unbind(struct ffs_data *ffs)
                clear_bit(FFS_FL_BOUND, &ffs->flags);
        }
 }
+#ifndef USB_FFS_INCLUDED
+EXPORT_SYMBOL(functionfs_unbind);
+#endif
 
 static int ffs_epfiles_create(struct ffs_data *ffs)
 {
@@ -1500,6 +1311,8 @@ static void ffs_epfiles_destroy(struct ffs_epfile 
*epfiles, unsigned count)
        kfree(epfiles);
 }
 
+#ifdef USB_FFS_INCLUDED
+
 static int functionfs_bind_config(struct usb_composite_dev *cdev,
                                  struct usb_configuration *c,
                                  struct ffs_data *ffs)
@@ -1536,6 +1349,74 @@ static int functionfs_bind_config(struct 
usb_composite_dev *cdev,
        return ret;
 }
 
+#else
+
+static void functionfs_free_inst(struct usb_function_instance *f)
+{
+       struct ffs_opts *opts;
+
+       opts = container_of(f, struct ffs_opts, func_inst);
+       kfree(opts);
+}
+
+static struct usb_function_instance *functionfs_alloc_inst(void)
+{
+       struct ffs_opts *opts;
+
+       opts = kzalloc(sizeof(*opts), GFP_KERNEL);
+       if (!opts)
+               return ERR_PTR(-ENOMEM);
+
+       opts->func_inst.free_func_inst = functionfs_free_inst;
+
+       return &opts->func_inst;
+}
+
+static void functionfs_free(struct usb_function *f)
+{
+       struct ffs_function *ffs;
+
+       ffs = ffs_func_from_usb(f);
+       kfree(ffs);
+}
+
+static struct usb_function *functionfs_alloc(struct usb_function_instance *fi)
+{
+       struct ffs_function *func;
+       struct ffs_opts *opts;
+
+       ENTER();
+
+       func = kzalloc(sizeof(*func), GFP_KERNEL);
+       if (unlikely(!func))
+               return ERR_PTR(-ENOMEM);
+
+       opts = container_of(fi, struct ffs_opts, func_inst);
+
+       func->function.name    = "Function FS Gadget";
+       func->function.strings = opts->ffs->stringtabs;
+
+       func->function.bind    = ffs_func_bind;
+       func->function.unbind  = ffs_func_unbind;
+       func->function.set_alt = ffs_func_set_alt;
+       func->function.disable = ffs_func_disable;
+       func->function.setup   = ffs_func_setup;
+       func->function.suspend = ffs_func_suspend;
+       func->function.resume  = ffs_func_resume;
+       func->function.free_func  = functionfs_free;
+
+       func->conf   = opts->conf;
+       func->gadget = opts->conf->cdev->gadget;
+       func->ffs = opts->ffs;
+       ffs_data_get(func->ffs);
+
+       return &func->function;
+}
+
+DECLARE_USB_FUNCTION_INIT(functionfs, functionfs_alloc_inst, functionfs_alloc);
+
+#endif
+
 static void ffs_func_free(struct ffs_function *func)
 {
        struct ffs_ep *ep         = func->eps;
@@ -1553,10 +1434,12 @@ static void ffs_func_free(struct ffs_function *func)
                ++ep;
        } while (--count);
        spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
-
        ffs_data_put(func->ffs);
 
        kfree(func->eps);
+       func->eps = NULL;
+
+#ifdef USB_FFS_INCLUDED
        /*
         * eps and interfaces_nums are allocated in the same chunk so
         * only one free is required.  Descriptors are also allocated
@@ -1564,6 +1447,7 @@ static void ffs_func_free(struct ffs_function *func)
         */
 
        kfree(func);
+#endif
 }
 
 static void ffs_func_eps_disable(struct ffs_function *func)
@@ -2493,3 +2377,8 @@ static char *ffs_prepare_buffer(const char __user *buf, 
size_t len)
 
        return data;
 }
+
+#ifndef USB_FFS_INCLUDED
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Michal Nazarewicz");
+#endif
diff --git a/drivers/usb/gadget/f_fs.h b/drivers/usb/gadget/f_fs.h
new file mode 100644
index 0000000..e453b5f
--- /dev/null
+++ b/drivers/usb/gadget/f_fs.h
@@ -0,0 +1,246 @@
+/*
+ * u_fs.h
+ *
+ * Utility definitions for the Function FS function
+ *
+ * Copyright (c) 2013 Samsung Electronics Co., Ltd.
+ *             http://www.samsung.com
+ *
+ * Author: Michal Nazarewicz <min...@mina86.com>
+ * Author: Andrzej Pietrasiewicz <andrze...@samsung.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#ifndef U_F_FS_H
+#define U_F_FS_H
+
+#include <linux/usb/composite.h>
+
+/* Debugging ****************************************************************/
+
+#ifdef VERBOSE_DEBUG
+#ifndef pr_vdebug
+#  define pr_vdebug pr_debug
+#endif /* pr_vdebug */
+#  define ffs_dump_mem(prefix, ptr, len) \
+       print_hex_dump_bytes(pr_fmt(prefix ": "), DUMP_PREFIX_NONE, ptr, len)
+#else
+#ifndef pr_vdebug
+#  define pr_vdebug(...)                 do { } while (0)
+#endif /* pr_vdebug */
+#  define ffs_dump_mem(prefix, ptr, len) do { } while (0)
+#endif /* VERBOSE_DEBUG */
+
+#define ENTER()    pr_vdebug("%s()\n", __func__)
+
+/* The data structure and setup file ****************************************/
+
+enum ffs_state {
+       /*
+        * Waiting for descriptors and strings.
+        *
+        * In this state no open(2), read(2) or write(2) on epfiles
+        * may succeed (which should not be the problem as there
+        * should be no such files opened in the first place).
+        */
+       FFS_READ_DESCRIPTORS,
+       FFS_READ_STRINGS,
+
+       /*
+        * We've got descriptors and strings.  We are or have called
+        * functionfs_ready_callback().  functionfs_bind() may have
+        * been called but we don't know.
+        *
+        * This is the only state in which operations on epfiles may
+        * succeed.
+        */
+       FFS_ACTIVE,
+
+       /*
+        * All endpoints have been closed.  This state is also set if
+        * we encounter an unrecoverable error.  The only
+        * unrecoverable error is situation when after reading strings
+        * from user space we fail to initialise epfiles or
+        * functionfs_ready_callback() returns with error (<0).
+        *
+        * In this state no open(2), read(2) or write(2) (both on ep0
+        * as well as epfile) may succeed (at this point epfiles are
+        * unlinked and all closed so this is not a problem; ep0 is
+        * also closed but ep0 file exists and so open(2) on ep0 must
+        * fail).
+        */
+       FFS_CLOSING
+};
+
+
+enum ffs_setup_state {
+       /* There is no setup request pending. */
+       FFS_NO_SETUP,
+       /*
+        * User has read events and there was a setup request event
+        * there.  The next read/write on ep0 will handle the
+        * request.
+        */
+       FFS_SETUP_PENDING,
+       /*
+        * There was event pending but before user space handled it
+        * some other event was introduced which canceled existing
+        * setup.  If this state is set read/write on ep0 return
+        * -EIDRM.  This state is only set when adding event.
+        */
+       FFS_SETUP_CANCELED
+};
+
+
+
+struct ffs_epfile;
+struct ffs_function;
+
+struct ffs_data {
+       struct usb_gadget               *gadget;
+
+       /*
+        * Protect access read/write operations, only one read/write
+        * at a time.  As a consequence protects ep0req and company.
+        * While setup request is being processed (queued) this is
+        * held.
+        */
+       struct mutex                    mutex;
+
+       /*
+        * Protect access to endpoint related structures (basically
+        * usb_ep_queue(), usb_ep_dequeue(), etc. calls) except for
+        * endpoint zero.
+        */
+       spinlock_t                      eps_lock;
+
+       /*
+        * XXX REVISIT do we need our own request? Since we are not
+        * handling setup requests immediately user space may be so
+        * slow that another setup will be sent to the gadget but this
+        * time not to us but another function and then there could be
+        * a race.  Is that the case? Or maybe we can use cdev->req
+        * after all, maybe we just need some spinlock for that?
+        */
+       struct usb_request              *ep0req;                /* P: mutex */
+       struct completion               ep0req_completion;      /* P: mutex */
+       int                             ep0req_status;          /* P: mutex */
+
+       /* reference counter */
+       atomic_t                        ref;
+       /* how many files are opened (EP0 and others) */
+       atomic_t                        opened;
+
+       /* EP0 state */
+       enum ffs_state                  state;
+
+       /*
+        * Possible transitions:
+        * + FFS_NO_SETUP       -> FFS_SETUP_PENDING  -- P: ev.waitq.lock
+        *               happens only in ep0 read which is P: mutex
+        * + FFS_SETUP_PENDING  -> FFS_NO_SETUP       -- P: ev.waitq.lock
+        *               happens only in ep0 i/o  which is P: mutex
+        * + FFS_SETUP_PENDING  -> FFS_SETUP_CANCELED -- P: ev.waitq.lock
+        * + FFS_SETUP_CANCELED -> FFS_NO_SETUP       -- cmpxchg
+        */
+       enum ffs_setup_state            setup_state;
+
+#define FFS_SETUP_STATE(ffs)                                   \
+       ((enum ffs_setup_state)cmpxchg(&(ffs)->setup_state,     \
+                                      FFS_SETUP_CANCELED, FFS_NO_SETUP))
+
+       /* Events & such. */
+       struct {
+               u8                              types[4];
+               unsigned short                  count;
+               /* XXX REVISIT need to update it in some places, or do we? */
+               unsigned short                  can_stall;
+               struct usb_ctrlrequest          setup;
+
+               wait_queue_head_t               waitq;
+       } ev; /* the whole structure, P: ev.waitq.lock */
+
+       /* Flags */
+       unsigned long                   flags;
+#define FFS_FL_CALL_CLOSED_CALLBACK 0
+#define FFS_FL_BOUND                1
+
+       /* Active function */
+       struct ffs_function             *func;
+
+       /*
+        * Device name, write once when file system is mounted.
+        * Intended for user to read if she wants.
+        */
+       const char                      *dev_name;
+       /* Private data for our user (ie. gadget).  Managed by user. */
+       void                            *private_data;
+
+       /* filled by __ffs_data_got_descs() */
+       /*
+        * Real descriptors are 16 bytes after raw_descs (so you need
+        * to skip 16 bytes (ie. ffs->raw_descs + 16) to get to the
+        * first full speed descriptor).  raw_descs_length and
+        * raw_fs_descs_length do not have those 16 bytes added.
+        */
+       const void                      *raw_descs;
+       unsigned                        raw_descs_length;
+       unsigned                        raw_fs_descs_length;
+       unsigned                        fs_descs_count;
+       unsigned                        hs_descs_count;
+
+       unsigned short                  strings_count;
+       unsigned short                  interfaces_count;
+       unsigned short                  eps_count;
+       unsigned short                  _pad1;
+
+       /* filled by __ffs_data_got_strings() */
+       /* ids in stringtabs are set in functionfs_bind() */
+       const void                      *raw_strings;
+       struct usb_gadget_strings       **stringtabs;
+
+       /*
+        * File system's super block, write once when file system is
+        * mounted.
+        */
+       struct super_block              *sb;
+
+       /* File permissions, written once when fs is mounted */
+       struct ffs_file_perms {
+               umode_t                         mode;
+               kuid_t                          uid;
+               kgid_t                          gid;
+       }                               file_perms;
+
+       /*
+        * The endpoint files, filled by ffs_epfiles_create(),
+        * destroyed by ffs_epfiles_destroy().
+        */
+       struct ffs_epfile               *epfiles;
+};
+
+struct ffs_callback_ops {
+       int (*ready)(struct ffs_data *ffs);
+       void (*close)(struct ffs_data *ffs);
+       void *(*acquire_dev)(const char *dev_name);
+       void (*release_dev)(struct ffs_data *ffs_data);
+};
+
+struct ffs_opts {
+       struct usb_function_instance    func_inst;
+       struct usb_configuration        *conf;
+       struct ffs_data                 *ffs;
+};
+
+int functionfs_init(struct ffs_callback_ops *cb_ops)
+       __attribute__((warn_unused_result));
+void functionfs_cleanup(void);
+int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev)
+       __attribute__((warn_unused_result, nonnull));
+void functionfs_unbind(struct ffs_data *ffs)
+       __attribute__((nonnull));
+
+#endif /* U_F_FS_H */
diff --git a/drivers/usb/gadget/g_ffs.c b/drivers/usb/gadget/g_ffs.c
index 71164fb..1914c3d 100644
--- a/drivers/usb/gadget/g_ffs.c
+++ b/drivers/usb/gadget/g_ffs.c
@@ -54,6 +54,7 @@ static int rndis_bind_config(struct usb_configuration *c,
 struct eth_dev;
 #endif
 
+#define USB_FFS_INCLUDED
 #include "f_fs.c"
 
 #define DRIVER_NAME    "g_ffs"
@@ -187,6 +188,22 @@ static bool gfs_registered;
 static bool gfs_single_func;
 static struct gfs_ffs_obj *ffs_tab;
 
+static int functionfs_ready_callback(struct ffs_data *ffs)
+       __attribute__((warn_unused_result, nonnull));
+static void functionfs_closed_callback(struct ffs_data *ffs)
+       __attribute__((nonnull));
+static void *functionfs_acquire_dev_callback(const char *dev_name)
+       __attribute__((warn_unused_result, nonnull));
+static void functionfs_release_dev_callback(struct ffs_data *ffs_data)
+       __attribute__((nonnull));
+
+static struct ffs_callback_ops ffs_cb_ops = {
+       .ready = functionfs_ready_callback,
+       .close = functionfs_closed_callback,
+       .acquire_dev = functionfs_acquire_dev_callback,
+       .release_dev = functionfs_release_dev_callback,
+};
+
 static inline
 struct gfs_configuration *to_gfs_configuration(struct usb_configuration *c)
 {
@@ -214,7 +231,7 @@ static int __init gfs_init(void)
 
        missing_funcs = func_num;
 
-       return functionfs_init();
+       return functionfs_init(&ffs_cb_ops);
 }
 module_init(gfs_init);
 
-- 
1.7.0.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to