usb: gadget: the start of the configfs interface
authorSebastian Andrzej Siewior <bigeasy@linutronix.de>
Sun, 23 Dec 2012 20:10:24 +0000 (21:10 +0100)
committerFelipe Balbi <balbi@ti.com>
Wed, 3 Apr 2013 11:43:32 +0000 (14:43 +0300)
|# modprobe dummy_hcd num=2
|# modprobe libcomposite

|# lsmod
|Module                  Size  Used by
|libcomposite           31648  0
|dummy_hcd              19871  0

|# mkdir /sys/kernel/config/usb_gadget/oha
|# cd /sys/kernel/config/usb_gadget/oha
|# mkdir configs/def.1
|# mkdir configs/def.2
|# mkdir functions/acm.ttyS1
|# mkdir strings/0x1
|mkdir: cannot create directory `strings/0x1': Invalid argument
|# mkdir strings/0x409
|# mkdir strings/1033
|mkdir: cannot create directory `strings/1033': File exists
|# mkdir strings/1032
|# mkdir configs/def.1/strings/0x409
|# mkdir configs/def.2/strings/0x409

|#find . -ls
|   975    0 drwxr-xr-x   5 root     root            0 Dec 23 17:40 .
|   978    0 drwxr-xr-x   4 root     root            0 Dec 23 17:43 ./strings
|  4100    0 drwxr-xr-x   2 root     root            0 Dec 23 17:43 ./strings/1032
|   995    0 -rw-r--r--   1 root     root         4096 Dec 23 17:43 ./strings/1032/serialnumber
|   996    0 -rw-r--r--   1 root     root         4096 Dec 23 17:43 ./strings/1032/product
|   997    0 -rw-r--r--   1 root     root         4096 Dec 23 17:43 ./strings/1032/manufacturer
|  2002    0 drwxr-xr-x   2 root     root            0 Dec 23 17:41 ./strings/0x409
|   998    0 -rw-r--r--   1 root     root         4096 Dec 23 17:43 ./strings/0x409/serialnumber
|   999    0 -rw-r--r--   1 root     root         4096 Dec 23 17:43 ./strings/0x409/product
|  1000    0 -rw-r--r--   1 root     root         4096 Dec 23 17:43 ./strings/0x409/manufacturer
|   977    0 drwxr-xr-x   4 root     root            0 Dec 23 17:41 ./configs
|  4081    0 drwxr-xr-x   3 root     root            0 Dec 23 17:41 ./configs/def.2
|  4082    0 drwxr-xr-x   3 root     root            0 Dec 23 17:42 ./configs/def.2/strings
|  2016    0 drwxr-xr-x   2 root     root            0 Dec 23 17:42 ./configs/def.2/strings/0x409
|  1001    0 -rw-r--r--   1 root     root         4096 Dec 23 17:43 ./configs/def.2/strings/0x409/configuration
|  1002    0 -rw-r--r--   1 root     root         4096 Dec 23 17:43 ./configs/def.2/bmAttributes
|  1003    0 -rw-r--r--   1 root     root         4096 Dec 23 17:43 ./configs/def.2/MaxPower
|   979    0 drwxr-xr-x   3 root     root            0 Dec 23 17:42 ./configs/def.1
|   980    0 drwxr-xr-x   3 root     root            0 Dec 23 17:42 ./configs/def.1/strings
|  5122    0 drwxr-xr-x   2 root     root            0 Dec 23 17:42 ./configs/def.1/strings/0x409
|  1004    0 -rw-r--r--   1 root     root         4096 Dec 23 17:43 ./configs/def.1/strings/0x409/configuration
|  1005    0 -rw-r--r--   1 root     root         4096 Dec 23 17:43 ./configs/def.1/bmAttributes
|  1006    0 -rw-r--r--   1 root     root         4096 Dec 23 17:43 ./configs/def.1/MaxPower
|   976    0 drwxr-xr-x   3 root     root            0 Dec 23 17:41 ./functions
|   981    0 drwxr-xr-x   2 root     root            0 Dec 23 17:41 ./functions/acm.ttyS1
|  1007    0 -r--r--r--   1 root     root         4096 Dec 23 17:43 ./functions/acm.ttyS1/port_num
|  1008    0 -rw-r--r--   1 root     root         4096 Dec 23 17:43 ./UDC
|  1009    0 -rw-r--r--   1 root     root         4096 Dec 23 17:43 ./bcdUSB
|  1010    0 -rw-r--r--   1 root     root         4096 Dec 23 17:43 ./bcdDevice
|  1011    0 -rw-r--r--   1 root     root         4096 Dec 23 17:43 ./idProduct
|  1012    0 -rw-r--r--   1 root     root         4096 Dec 23 17:43 ./idVendor
|  1013    0 -rw-r--r--   1 root     root         4096 Dec 23 17:43 ./bMaxPacketSize0
|  1014    0 -rw-r--r--   1 root     root         4096 Dec 23 17:43 ./bDeviceProtocol
|  1015    0 -rw-r--r--   1 root     root         4096 Dec 23 17:43 ./bDeviceSubClass
|  1016    0 -rw-r--r--   1 root     root         4096 Dec 23 17:43 ./bDeviceClass

|# cat functions/acm.ttyS1/port_num
|0
|# ls -lah /dev/ttyGS*
|crw-rw---T 1 root dialout 252, 0 Dec 23 17:41 /dev/ttyGS0
|
|# echo 0x1234 > idProduct
|# echo 0xabcd > idVendor
|# echo 1122 > strings/0x409/serialnumber
|# echo "The manufacturer" > strings/0x409/manufacturer
|# echo 1 > strings/1032/manufacturer
|# echo 1sa > strings/1032/product
|# echo tada > strings/1032/serialnumber
|echo "Primary configuration" > configs/def.1/strings/0x409/configuration
|# echo "Secondary configuration" > configs/def.2/strings/0x409/configuration
|# ln -s functions/acm.ttyS1 configs/def.1/
|# ln -s functions/acm.ttyS1 configs/def.2/
|find configs/def.1/ -ls
|   979    0 drwxr-xr-x   3 root     root            0 Dec 23 17:49 configs/def.1/
|  6264    0 lrwxrwxrwx   1 root     root            0 Dec 23 17:48 configs/def.1/acm.ttyS1 -> ../../../../usb_gadget/oha/functions/acm.ttyS1
|   980    0 drwxr-xr-x   3 root     root            0 Dec 23 17:42 configs/def.1/strings
|  5122    0 drwxr-xr-x   2 root     root            0 Dec 23 17:49 configs/def.1/strings/0x409
|  6284    0 -rw-r--r--   1 root     root         4096 Dec 23 17:47 configs/def.1/strings/0x409/configuration
|  6285    0 -rw-r--r--   1 root     root         4096 Dec 23 17:49 configs/def.1/bmAttributes
|  6286    0 -rw-r--r--   1 root     root         4096 Dec 23 17:49 configs/def.1/MaxPower
|
|echo 120 > configs/def.1/MaxPower
|
|# ls -lh /sys/class/udc/
|total 0
|lrwxrwxrwx 1 root root 0 Dec 23 17:50 dummy_udc.0 -> ../../devices/platform/dummy_udc.0/udc/dummy_udc.0
|lrwxrwxrwx 1 root root 0 Dec 23 17:50 dummy_udc.1 -> ../../devices/platform/dummy_udc.1/udc/dummy_udc.1
|# echo dummy_udc.0 > UDC
|# lsusb
|Bus 001 Device 002: ID abcd:1234 Unknown
|
|lsusb -d abcd:1234 -v
|Device Descriptor:

|  idVendor           0xabcd Unknown
|  idProduct          0x1234
|  bcdDevice            3.06
|  iManufacturer           1 The manufacturer
|  iProduct                2
|  iSerial                 3 1122
|  bNumConfigurations      2

|echo "" > UDC

v5…v6
- wired up strings with usb_gstrings_attach()
- add UDC attribe. Write "udc-name" will bind the gadget. Write an empty
  string (it should contain \n since 0 bytes write get optimzed away)
  will unbind the UDC from the gadget. The name of available UDCs can be
  obtained from /sys/class/udc/

v4…v5
- string rework. This will add a strings folder incl. language code like
    strings/409/manufacturer
  as suggested by Alan.
- rebased ontop reworked functions.c which has usb_function_instance
  which is used prior after "mkdir acm.instance" and can be directly
  used for configuration via configfs.

v3…v4
 - moved functions from the root folde down to the gadget as suggested
   by Michał
 - configs have now their own configs folder as suggested by Michał.
   The folder is still name.bConfigurationValue where name becomes the
   sConfiguration. Is this usefull should we just stilc
   configs/bConfigurationValue/ ?
 - added configfs support to the ACM function. The port_num attribute is
   exported by f_acm. An argument has been added to the USB alloc
   function to distinguish between "old" (use facm_configure() to
   configure and configfs interface (expose a config_node).
   The port_num is currently a dumb counter. It will
   require some function re-work to make it work.

scheduled for v5:
- sym linking function into config.

v2…v3
- replaced one ifndef by ifdef as suggested by Micahał
- strstr()/strchr() function_make as suggested by Micahł
- replace [iSerialNumber|iProduct|iManufacturer] with
  [sSerialNumber|sProduct|sManufacturer] as suggested by Alan
- added creation of config descriptors

v1…v2
- moved gadgets from configfs' root directory into /udcs/ within our
  "usb_gadget" folder. Requested by Andrzej & Michał
- use a dot as a delimiter between function's name and its instance's name
  as suggested by Michał
- renamed all config_item_type, configfs_group_operations, make_group,
  drop_item as suggested by suggested by Andrzej to remain consisten
  within this file and within other configfs users
- Since configfs.c and functions.c are now part of the udc-core module,
  the module itself is now called udc. Also added a tiny ifdef around
  init code becuase udc-core is subsys init and this is too early for
  configfs in the built-in case. In the module case, we can only have
  one init function.

Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Signed-off-by: Felipe Balbi <balbi@ti.com>
drivers/usb/gadget/Kconfig
drivers/usb/gadget/Makefile
drivers/usb/gadget/composite.c
drivers/usb/gadget/configfs.c [new file with mode: 0644]
drivers/usb/gadget/f_acm.c
include/linux/usb/composite.h
include/linux/usb/gadget_configfs.h [new file with mode: 0644]

index e1d3e08..74a29de 100644 (file)
@@ -493,6 +493,7 @@ endmenu
 # composite based drivers
 config USB_LIBCOMPOSITE
        tristate
+       select CONFIGFS_FS
        depends on USB_GADGET
 
 config USB_F_ACM
index 82fb225..96e7243 100644 (file)
@@ -6,7 +6,7 @@ ccflags-$(CONFIG_USB_GADGET_DEBUG) := -DDEBUG
 obj-$(CONFIG_USB_GADGET)       += udc-core.o
 obj-$(CONFIG_USB_LIBCOMPOSITE) += libcomposite.o
 libcomposite-y                 := usbstring.o config.o epautoconf.o
-libcomposite-y                 += composite.o functions.o
+libcomposite-y                 += composite.o functions.o configfs.o
 obj-$(CONFIG_USB_DUMMY_HCD)    += dummy_hcd.o
 obj-$(CONFIG_USB_NET2272)      += net2272.o
 obj-$(CONFIG_USB_NET2280)      += net2280.o
index c0d62b2..55f4df6 100644 (file)
@@ -1637,6 +1637,7 @@ void composite_dev_cleanup(struct usb_composite_dev *cdev)
                kfree(cdev->req->buf);
                usb_ep_free_request(cdev->gadget->ep0, cdev->req);
        }
+       cdev->next_string_id = 0;
        device_remove_file(&cdev->gadget->dev, &dev_attr_suspended);
 }
 
diff --git a/drivers/usb/gadget/configfs.c b/drivers/usb/gadget/configfs.c
new file mode 100644 (file)
index 0000000..a34633a
--- /dev/null
@@ -0,0 +1,1003 @@
+#include <linux/configfs.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/device.h>
+#include <linux/usb/composite.h>
+#include <linux/usb/gadget_configfs.h>
+
+int check_user_usb_string(const char *name,
+               struct usb_gadget_strings *stringtab_dev)
+{
+       unsigned primary_lang;
+       unsigned sub_lang;
+       u16 num;
+       int ret;
+
+       ret = kstrtou16(name, 0, &num);
+       if (ret)
+               return ret;
+
+       primary_lang = num & 0x3ff;
+       sub_lang = num >> 10;
+
+       /* simple sanity check for valid langid */
+       switch (primary_lang) {
+       case 0:
+       case 0x62 ... 0xfe:
+       case 0x100 ... 0x3ff:
+               return -EINVAL;
+       }
+       if (!sub_lang)
+               return -EINVAL;
+
+       stringtab_dev->language = num;
+       return 0;
+}
+
+#define MAX_NAME_LEN   40
+#define MAX_USB_STRING_LANGS 2
+
+struct gadget_info {
+       struct config_group group;
+       struct config_group functions_group;
+       struct config_group configs_group;
+       struct config_group strings_group;
+       struct config_group *default_groups[4];
+
+       struct mutex lock;
+       struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1];
+       struct list_head string_list;
+       struct list_head available_func;
+
+       const char *udc_name;
+#ifdef CONFIG_USB_OTG
+       struct usb_otg_descriptor otg;
+#endif
+       struct usb_composite_driver composite;
+       struct usb_composite_dev cdev;
+};
+
+struct config_usb_cfg {
+       struct config_group group;
+       struct config_group strings_group;
+       struct config_group *default_groups[2];
+       struct list_head string_list;
+       struct usb_configuration c;
+       struct list_head func_list;
+       struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1];
+};
+
+struct gadget_strings {
+       struct usb_gadget_strings stringtab_dev;
+       struct usb_string strings[USB_GADGET_FIRST_AVAIL_IDX];
+       char *manufacturer;
+       char *product;
+       char *serialnumber;
+
+       struct config_group group;
+       struct list_head list;
+};
+
+struct gadget_config_name {
+       struct usb_gadget_strings stringtab_dev;
+       struct usb_string strings;
+       char *configuration;
+
+       struct config_group group;
+       struct list_head list;
+};
+
+static int usb_string_copy(const char *s, char **s_copy)
+{
+       int ret;
+       char *str;
+       char *copy = *s_copy;
+       ret = strlen(s);
+       if (ret > 126)
+               return -EOVERFLOW;
+
+       str = kstrdup(s, GFP_KERNEL);
+       if (!str)
+               return -ENOMEM;
+       if (str[ret - 1] == '\n')
+               str[ret - 1] = '\0';
+       kfree(copy);
+       *s_copy = str;
+       return 0;
+}
+
+CONFIGFS_ATTR_STRUCT(gadget_info);
+CONFIGFS_ATTR_STRUCT(config_usb_cfg);
+
+#define GI_DEVICE_DESC_ITEM_ATTR(name) \
+       static struct gadget_info_attribute gadget_cdev_desc_##name = \
+               __CONFIGFS_ATTR(name,  S_IRUGO | S_IWUSR,               \
+                               gadget_dev_desc_##name##_show,          \
+                               gadget_dev_desc_##name##_store)
+
+#define GI_DEVICE_DESC_SIMPLE_R_u8(__name)     \
+       static ssize_t gadget_dev_desc_##__name##_show(struct gadget_info *gi, \
+                       char *page)     \
+{      \
+       return sprintf(page, "0x%02x\n", gi->cdev.desc.__name); \
+}
+
+#define GI_DEVICE_DESC_SIMPLE_R_u16(__name)    \
+       static ssize_t gadget_dev_desc_##__name##_show(struct gadget_info *gi, \
+                       char *page)     \
+{      \
+       return sprintf(page, "0x%04x\n", le16_to_cpup(&gi->cdev.desc.__name)); \
+}
+
+
+#define GI_DEVICE_DESC_SIMPLE_W_u8(_name)              \
+       static ssize_t gadget_dev_desc_##_name##_store(struct gadget_info *gi, \
+               const char *page, size_t len)           \
+{                                                      \
+       u8 val;                                         \
+       int ret;                                        \
+       ret = kstrtou8(page, 0, &val);                  \
+       if (ret)                                        \
+               return ret;                             \
+       gi->cdev.desc._name = val;                      \
+       return len;                                     \
+}
+
+#define GI_DEVICE_DESC_SIMPLE_W_u16(_name)     \
+       static ssize_t gadget_dev_desc_##_name##_store(struct gadget_info *gi, \
+               const char *page, size_t len)           \
+{                                                      \
+       u16 val;                                        \
+       int ret;                                        \
+       ret = kstrtou16(page, 0, &val);                 \
+       if (ret)                                        \
+               return ret;                             \
+       gi->cdev.desc._name = cpu_to_le16p(&val);       \
+       return len;                                     \
+}
+
+#define GI_DEVICE_DESC_SIMPLE_RW(_name, _type) \
+       GI_DEVICE_DESC_SIMPLE_R_##_type(_name)  \
+       GI_DEVICE_DESC_SIMPLE_W_##_type(_name)
+
+GI_DEVICE_DESC_SIMPLE_R_u16(bcdUSB);
+GI_DEVICE_DESC_SIMPLE_RW(bDeviceClass, u8);
+GI_DEVICE_DESC_SIMPLE_RW(bDeviceSubClass, u8);
+GI_DEVICE_DESC_SIMPLE_RW(bDeviceProtocol, u8);
+GI_DEVICE_DESC_SIMPLE_RW(bMaxPacketSize0, u8);
+GI_DEVICE_DESC_SIMPLE_RW(idVendor, u16);
+GI_DEVICE_DESC_SIMPLE_RW(idProduct, u16);
+GI_DEVICE_DESC_SIMPLE_R_u16(bcdDevice);
+
+static ssize_t is_valid_bcd(u16 bcd_val)
+{
+       if ((bcd_val & 0xf) > 9)
+               return -EINVAL;
+       if (((bcd_val >> 4) & 0xf) > 9)
+               return -EINVAL;
+       if (((bcd_val >> 8) & 0xf) > 9)
+               return -EINVAL;
+       if (((bcd_val >> 12) & 0xf) > 9)
+               return -EINVAL;
+       return 0;
+}
+
+static ssize_t gadget_dev_desc_bcdDevice_store(struct gadget_info *gi,
+               const char *page, size_t len)
+{
+       u16 bcdDevice;
+       int ret;
+
+       ret = kstrtou16(page, 0, &bcdDevice);
+       if (ret)
+               return ret;
+       ret = is_valid_bcd(bcdDevice);
+       if (ret)
+               return ret;
+
+       gi->cdev.desc.bcdDevice = cpu_to_le16(bcdDevice);
+       return len;
+}
+
+static ssize_t gadget_dev_desc_bcdUSB_store(struct gadget_info *gi,
+               const char *page, size_t len)
+{
+       u16 bcdUSB;
+       int ret;
+
+       ret = kstrtou16(page, 0, &bcdUSB);
+       if (ret)
+               return ret;
+       ret = is_valid_bcd(bcdUSB);
+       if (ret)
+               return ret;
+
+       gi->cdev.desc.bcdUSB = cpu_to_le16(bcdUSB);
+       return len;
+}
+
+static ssize_t gadget_dev_desc_UDC_show(struct gadget_info *gi, char *page)
+{
+       return sprintf(page, "%s\n", gi->udc_name ?: "");
+}
+
+static int unregister_gadget(struct gadget_info *gi)
+{
+       int ret;
+
+       if (!gi->udc_name)
+               return -ENODEV;
+
+       ret = usb_gadget_unregister_driver(&gi->composite.gadget_driver);
+       if (ret)
+               return ret;
+       kfree(gi->udc_name);
+       gi->udc_name = NULL;
+       return 0;
+}
+
+static ssize_t gadget_dev_desc_UDC_store(struct gadget_info *gi,
+               const char *page, size_t len)
+{
+       char *name;
+       int ret;
+
+       name = kstrdup(page, GFP_KERNEL);
+       if (!name)
+               return -ENOMEM;
+       if (name[len - 1] == '\n')
+               name[len - 1] = '\0';
+
+       mutex_lock(&gi->lock);
+
+       if (!strlen(name)) {
+               ret = unregister_gadget(gi);
+               if (ret)
+                       goto err;
+       } else {
+               if (gi->udc_name) {
+                       ret = -EBUSY;
+                       goto err;
+               }
+               ret = udc_attach_driver(name, &gi->composite.gadget_driver);
+               if (ret)
+                       goto err;
+               gi->udc_name = name;
+       }
+       mutex_unlock(&gi->lock);
+       return len;
+err:
+       kfree(name);
+       mutex_unlock(&gi->lock);
+       return ret;
+}
+
+GI_DEVICE_DESC_ITEM_ATTR(bDeviceClass);
+GI_DEVICE_DESC_ITEM_ATTR(bDeviceSubClass);
+GI_DEVICE_DESC_ITEM_ATTR(bDeviceProtocol);
+GI_DEVICE_DESC_ITEM_ATTR(bMaxPacketSize0);
+GI_DEVICE_DESC_ITEM_ATTR(idVendor);
+GI_DEVICE_DESC_ITEM_ATTR(idProduct);
+GI_DEVICE_DESC_ITEM_ATTR(bcdDevice);
+GI_DEVICE_DESC_ITEM_ATTR(bcdUSB);
+GI_DEVICE_DESC_ITEM_ATTR(UDC);
+
+static struct configfs_attribute *gadget_root_attrs[] = {
+       &gadget_cdev_desc_bDeviceClass.attr,
+       &gadget_cdev_desc_bDeviceSubClass.attr,
+       &gadget_cdev_desc_bDeviceProtocol.attr,
+       &gadget_cdev_desc_bMaxPacketSize0.attr,
+       &gadget_cdev_desc_idVendor.attr,
+       &gadget_cdev_desc_idProduct.attr,
+       &gadget_cdev_desc_bcdDevice.attr,
+       &gadget_cdev_desc_bcdUSB.attr,
+       &gadget_cdev_desc_UDC.attr,
+       NULL,
+};
+
+static inline struct gadget_info *to_gadget_info(struct config_item *item)
+{
+        return container_of(to_config_group(item), struct gadget_info, group);
+}
+
+static inline struct gadget_strings *to_gadget_strings(struct config_item *item)
+{
+        return container_of(to_config_group(item), struct gadget_strings,
+                        group);
+}
+
+static inline struct gadget_config_name *to_gadget_config_name(
+               struct config_item *item)
+{
+        return container_of(to_config_group(item), struct gadget_config_name,
+                        group);
+}
+
+static inline struct config_usb_cfg *to_config_usb_cfg(struct config_item *item)
+{
+       return container_of(to_config_group(item), struct config_usb_cfg,
+                       group);
+}
+
+static inline struct usb_function_instance *to_usb_function_instance(
+               struct config_item *item)
+{
+        return container_of(to_config_group(item),
+                        struct usb_function_instance, group);
+}
+
+static void gadget_info_attr_release(struct config_item *item)
+{
+       struct gadget_info *gi = to_gadget_info(item);
+
+       WARN_ON(!list_empty(&gi->cdev.configs));
+       WARN_ON(!list_empty(&gi->string_list));
+       WARN_ON(!list_empty(&gi->available_func));
+       kfree(gi->composite.gadget_driver.function);
+       kfree(gi);
+}
+
+CONFIGFS_ATTR_OPS(gadget_info);
+
+static struct configfs_item_operations gadget_root_item_ops = {
+       .release                = gadget_info_attr_release,
+       .show_attribute         = gadget_info_attr_show,
+       .store_attribute        = gadget_info_attr_store,
+};
+
+static void gadget_config_attr_release(struct config_item *item)
+{
+       struct config_usb_cfg *cfg = to_config_usb_cfg(item);
+
+       WARN_ON(!list_empty(&cfg->c.functions));
+       list_del(&cfg->c.list);
+       kfree(cfg->c.label);
+       kfree(cfg);
+}
+
+static int config_usb_cfg_link(
+       struct config_item *usb_cfg_ci,
+       struct config_item *usb_func_ci)
+{
+       struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci);
+       struct usb_composite_dev *cdev = cfg->c.cdev;
+       struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev);
+
+       struct config_group *group = to_config_group(usb_func_ci);
+       struct usb_function_instance *fi = container_of(group,
+                       struct usb_function_instance, group);
+       struct usb_function_instance *a_fi;
+       struct usb_function *f;
+       int ret;
+
+       mutex_lock(&gi->lock);
+       /*
+        * Make sure this function is from within our _this_ gadget and not
+        * from another gadget or a random directory.
+        * Also a function instance can only be linked once.
+        */
+       list_for_each_entry(a_fi, &gi->available_func, cfs_list) {
+               if (a_fi == fi)
+                       break;
+       }
+       if (a_fi != fi) {
+               ret = -EINVAL;
+               goto out;
+       }
+
+       list_for_each_entry(f, &cfg->func_list, list) {
+               if (f->fi == fi) {
+                       ret = -EEXIST;
+                       goto out;
+               }
+       }
+
+       f = usb_get_function(fi);
+       if (IS_ERR(f)) {
+               ret = PTR_ERR(f);
+               goto out;
+       }
+
+       /* stash the function until we bind it to the gadget */
+       list_add_tail(&f->list, &cfg->func_list);
+       ret = 0;
+out:
+       mutex_unlock(&gi->lock);
+       return ret;
+}
+
+static int config_usb_cfg_unlink(
+       struct config_item *usb_cfg_ci,
+       struct config_item *usb_func_ci)
+{
+       struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci);
+       struct usb_composite_dev *cdev = cfg->c.cdev;
+       struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev);
+
+       struct config_group *group = to_config_group(usb_func_ci);
+       struct usb_function_instance *fi = container_of(group,
+                       struct usb_function_instance, group);
+       struct usb_function *f;
+
+       /*
+        * ideally I would like to forbid to unlink functions while a gadget is
+        * bound to an UDC. Since this isn't possible at the moment, we simply
+        * force an unbind, the function is available here and then we can
+        * remove the function.
+        */
+       mutex_lock(&gi->lock);
+       if (gi->udc_name)
+               unregister_gadget(gi);
+       WARN_ON(gi->udc_name);
+
+       list_for_each_entry(f, &cfg->func_list, list) {
+               if (f->fi == fi) {
+                       list_del(&f->list);
+                       usb_put_function(f);
+                       mutex_unlock(&gi->lock);
+                       return 0;
+               }
+       }
+       mutex_unlock(&gi->lock);
+       __WARN_printf("Unable to locate function to unbind\n");
+       return 0;
+}
+
+CONFIGFS_ATTR_OPS(config_usb_cfg);
+
+static struct configfs_item_operations gadget_config_item_ops = {
+       .release                = gadget_config_attr_release,
+       .show_attribute         = config_usb_cfg_attr_show,
+       .store_attribute        = config_usb_cfg_attr_store,
+       .allow_link             = config_usb_cfg_link,
+       .drop_link              = config_usb_cfg_unlink,
+};
+
+
+static ssize_t gadget_config_desc_MaxPower_show(struct config_usb_cfg *cfg,
+               char *page)
+{
+       return sprintf(page, "%u\n", cfg->c.MaxPower);
+}
+
+static ssize_t gadget_config_desc_MaxPower_store(struct config_usb_cfg *cfg,
+               const char *page, size_t len)
+{
+       u16 val;
+       int ret;
+       ret = kstrtou16(page, 0, &val);
+       if (ret)
+               return ret;
+       if (DIV_ROUND_UP(val, 8) > 0xff)
+               return -ERANGE;
+       cfg->c.MaxPower = val;
+       return len;
+}
+
+static ssize_t gadget_config_desc_bmAttributes_show(struct config_usb_cfg *cfg,
+               char *page)
+{
+       return sprintf(page, "0x%02x\n", cfg->c.bmAttributes);
+}
+
+static ssize_t gadget_config_desc_bmAttributes_store(struct config_usb_cfg *cfg,
+               const char *page, size_t len)
+{
+       u8 val;
+       int ret;
+       ret = kstrtou8(page, 0, &val);
+       if (ret)
+               return ret;
+       if (!(val & USB_CONFIG_ATT_ONE))
+               return -EINVAL;
+       if (val & ~(USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER |
+                               USB_CONFIG_ATT_WAKEUP))
+               return -EINVAL;
+       cfg->c.bmAttributes = val;
+       return len;
+}
+
+#define CFG_CONFIG_DESC_ITEM_ATTR(name)        \
+       static struct config_usb_cfg_attribute gadget_usb_cfg_##name = \
+               __CONFIGFS_ATTR(name,  S_IRUGO | S_IWUSR,               \
+                               gadget_config_desc_##name##_show,       \
+                               gadget_config_desc_##name##_store)
+
+CFG_CONFIG_DESC_ITEM_ATTR(MaxPower);
+CFG_CONFIG_DESC_ITEM_ATTR(bmAttributes);
+
+static struct configfs_attribute *gadget_config_attrs[] = {
+       &gadget_usb_cfg_MaxPower.attr,
+       &gadget_usb_cfg_bmAttributes.attr,
+       NULL,
+};
+
+static struct config_item_type gadget_config_type = {
+       .ct_item_ops    = &gadget_config_item_ops,
+       .ct_attrs       = gadget_config_attrs,
+       .ct_owner       = THIS_MODULE,
+};
+
+static struct config_item_type gadget_root_type = {
+       .ct_item_ops    = &gadget_root_item_ops,
+       .ct_attrs       = gadget_root_attrs,
+       .ct_owner       = THIS_MODULE,
+};
+
+static void composite_init_dev(struct usb_composite_dev *cdev)
+{
+       spin_lock_init(&cdev->lock);
+       INIT_LIST_HEAD(&cdev->configs);
+       INIT_LIST_HEAD(&cdev->gstrings);
+}
+
+static struct config_group *function_make(
+               struct config_group *group,
+               const char *name)
+{
+       struct gadget_info *gi;
+       struct usb_function_instance *fi;
+       char buf[MAX_NAME_LEN];
+       char *func_name;
+       char *instance_name;
+       int ret;
+
+       ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
+       if (ret >= MAX_NAME_LEN)
+               return ERR_PTR(-ENAMETOOLONG);
+
+       func_name = buf;
+       instance_name = strchr(func_name, '.');
+       if (!instance_name) {
+               pr_err("Unable to locate . in FUNC.INSTANCE\n");
+               return ERR_PTR(-EINVAL);
+       }
+       *instance_name = '\0';
+       instance_name++;
+
+       fi = usb_get_function_instance(func_name);
+       if (IS_ERR(fi))
+               return ERR_PTR(PTR_ERR(fi));
+
+       ret = config_item_set_name(&fi->group.cg_item, name);
+       if (ret) {
+               usb_put_function_instance(fi);
+               return ERR_PTR(ret);
+       }
+
+       gi = container_of(group, struct gadget_info, functions_group);
+
+       mutex_lock(&gi->lock);
+       list_add_tail(&fi->cfs_list, &gi->available_func);
+       mutex_unlock(&gi->lock);
+       return &fi->group;
+}
+
+static void function_drop(
+               struct config_group *group,
+               struct config_item *item)
+{
+       struct usb_function_instance *fi = to_usb_function_instance(item);
+       struct gadget_info *gi;
+
+       gi = container_of(group, struct gadget_info, functions_group);
+
+       mutex_lock(&gi->lock);
+       list_del(&fi->cfs_list);
+       mutex_unlock(&gi->lock);
+       config_item_put(item);
+}
+
+static struct configfs_group_operations functions_ops = {
+       .make_group     = &function_make,
+       .drop_item      = &function_drop,
+};
+
+static struct config_item_type functions_type = {
+       .ct_group_ops   = &functions_ops,
+       .ct_owner       = THIS_MODULE,
+};
+
+CONFIGFS_ATTR_STRUCT(gadget_config_name);
+GS_STRINGS_RW(gadget_config_name, configuration);
+
+static struct configfs_attribute *gadget_config_name_langid_attrs[] = {
+       &gadget_config_name_configuration.attr,
+       NULL,
+};
+
+static void gadget_config_name_attr_release(struct config_item *item)
+{
+       struct gadget_config_name *cn = to_gadget_config_name(item);
+
+       kfree(cn->configuration);
+
+       list_del(&cn->list);
+       kfree(cn);
+}
+
+USB_CONFIG_STRING_RW_OPS(gadget_config_name);
+USB_CONFIG_STRINGS_LANG(gadget_config_name, config_usb_cfg);
+
+static struct config_group *config_desc_make(
+               struct config_group *group,
+               const char *name)
+{
+       struct gadget_info *gi;
+       struct config_usb_cfg *cfg;
+       char buf[MAX_NAME_LEN];
+       char *num_str;
+       u8 num;
+       int ret;
+
+       gi = container_of(group, struct gadget_info, configs_group);
+       ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
+       if (ret >= MAX_NAME_LEN)
+               return ERR_PTR(-ENAMETOOLONG);
+
+       num_str = strchr(buf, '.');
+       if (!num_str) {
+               pr_err("Unable to locate . in name.bConfigurationValue\n");
+               return ERR_PTR(-EINVAL);
+       }
+
+       *num_str = '\0';
+       num_str++;
+
+       if (!strlen(buf))
+               return ERR_PTR(-EINVAL);
+
+       ret = kstrtou8(num_str, 0, &num);
+       if (ret)
+               return ERR_PTR(ret);
+
+       cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
+       if (!cfg)
+               return ERR_PTR(-ENOMEM);
+       cfg->c.label = kstrdup(buf, GFP_KERNEL);
+       if (!cfg->c.label) {
+               ret = -ENOMEM;
+               goto err;
+       }
+       cfg->c.bConfigurationValue = num;
+       cfg->c.MaxPower = CONFIG_USB_GADGET_VBUS_DRAW;
+       cfg->c.bmAttributes = USB_CONFIG_ATT_ONE;
+       INIT_LIST_HEAD(&cfg->string_list);
+       INIT_LIST_HEAD(&cfg->func_list);
+
+       cfg->group.default_groups = cfg->default_groups;
+       cfg->default_groups[0] = &cfg->strings_group;
+
+       config_group_init_type_name(&cfg->group, name,
+                               &gadget_config_type);
+       config_group_init_type_name(&cfg->strings_group, "strings",
+                       &gadget_config_name_strings_type);
+
+       ret = usb_add_config_only(&gi->cdev, &cfg->c);
+       if (ret)
+               goto err;
+
+       return &cfg->group;
+err:
+       kfree(cfg->c.label);
+       kfree(cfg);
+       return ERR_PTR(ret);
+}
+
+static void config_desc_drop(
+               struct config_group *group,
+               struct config_item *item)
+{
+       config_item_put(item);
+}
+
+static struct configfs_group_operations config_desc_ops = {
+       .make_group     = &config_desc_make,
+       .drop_item      = &config_desc_drop,
+};
+
+static struct config_item_type config_desc_type = {
+       .ct_group_ops   = &config_desc_ops,
+       .ct_owner       = THIS_MODULE,
+};
+
+CONFIGFS_ATTR_STRUCT(gadget_strings);
+GS_STRINGS_RW(gadget_strings, manufacturer);
+GS_STRINGS_RW(gadget_strings, product);
+GS_STRINGS_RW(gadget_strings, serialnumber);
+
+static struct configfs_attribute *gadget_strings_langid_attrs[] = {
+       &gadget_strings_manufacturer.attr,
+       &gadget_strings_product.attr,
+       &gadget_strings_serialnumber.attr,
+       NULL,
+};
+
+static void gadget_strings_attr_release(struct config_item *item)
+{
+       struct gadget_strings *gs = to_gadget_strings(item);
+
+       kfree(gs->manufacturer);
+       kfree(gs->product);
+       kfree(gs->serialnumber);
+
+       list_del(&gs->list);
+       kfree(gs);
+}
+
+USB_CONFIG_STRING_RW_OPS(gadget_strings);
+USB_CONFIG_STRINGS_LANG(gadget_strings, gadget_info);
+
+static int configfs_do_nothing(struct usb_composite_dev *cdev)
+{
+       __WARN();
+       return -EINVAL;
+}
+
+int composite_dev_prepare(struct usb_composite_driver *composite,
+               struct usb_composite_dev *dev);
+
+static void purge_configs_funcs(struct gadget_info *gi)
+{
+       struct usb_configuration        *c;
+
+       list_for_each_entry(c, &gi->cdev.configs, list) {
+               struct usb_function *f, *tmp;
+               struct config_usb_cfg *cfg;
+
+               cfg = container_of(c, struct config_usb_cfg, c);
+
+               list_for_each_entry_safe(f, tmp, &c->functions, list) {
+
+                       list_move_tail(&f->list, &cfg->func_list);
+                       if (f->unbind) {
+                               dev_err(&gi->cdev.gadget->dev, "unbind function"
+                                               " '%s'/%p\n", f->name, f);
+                               f->unbind(c, f);
+                       }
+               }
+               c->next_interface_id = 0;
+               c->superspeed = 0;
+               c->highspeed = 0;
+               c->fullspeed = 0;
+       }
+}
+
+static int configfs_composite_bind(struct usb_gadget *gadget,
+               struct usb_gadget_driver *gdriver)
+{
+       struct usb_composite_driver     *composite = to_cdriver(gdriver);
+       struct gadget_info              *gi = container_of(composite,
+                                               struct gadget_info, composite);
+       struct usb_composite_dev        *cdev = &gi->cdev;
+       struct usb_configuration        *c;
+       struct usb_string               *s;
+       unsigned                        i;
+       int                             ret;
+
+       /* the gi->lock is hold by the caller */
+       cdev->gadget = gadget;
+       set_gadget_data(gadget, cdev);
+       ret = composite_dev_prepare(composite, cdev);
+       if (ret)
+               return ret;
+       /* and now the gadget bind */
+       ret = -EINVAL;
+
+       if (list_empty(&gi->cdev.configs)) {
+               pr_err("Need atleast one configuration in %s.\n",
+                               gi->composite.name);
+               goto err_comp_cleanup;
+       }
+
+
+       list_for_each_entry(c, &gi->cdev.configs, list) {
+               struct config_usb_cfg *cfg;
+
+               cfg = container_of(c, struct config_usb_cfg, c);
+               if (list_empty(&cfg->func_list)) {
+                       pr_err("Config %s/%d of %s needs atleast one function.\n",
+                             c->label, c->bConfigurationValue,
+                             gi->composite.name);
+                       goto err_comp_cleanup;
+               }
+       }
+
+       /* init all strings */
+       if (!list_empty(&gi->string_list)) {
+               struct gadget_strings *gs;
+
+               i = 0;
+               list_for_each_entry(gs, &gi->string_list, list) {
+
+                       gi->gstrings[i] = &gs->stringtab_dev;
+                       gs->stringtab_dev.strings = gs->strings;
+                       gs->strings[USB_GADGET_MANUFACTURER_IDX].s =
+                               gs->manufacturer;
+                       gs->strings[USB_GADGET_PRODUCT_IDX].s = gs->product;
+                       gs->strings[USB_GADGET_SERIAL_IDX].s = gs->serialnumber;
+                       i++;
+               }
+               gi->gstrings[i] = NULL;
+               s = usb_gstrings_attach(&gi->cdev, gi->gstrings,
+                               USB_GADGET_FIRST_AVAIL_IDX);
+               if (IS_ERR(s))
+                       goto err_comp_cleanup;
+
+               gi->cdev.desc.iManufacturer = s[USB_GADGET_MANUFACTURER_IDX].id;
+               gi->cdev.desc.iProduct = s[USB_GADGET_PRODUCT_IDX].id;
+               gi->cdev.desc.iSerialNumber = s[USB_GADGET_SERIAL_IDX].id;
+       }
+
+       /* Go through all configs, attach all functions */
+       list_for_each_entry(c, &gi->cdev.configs, list) {
+               struct config_usb_cfg *cfg;
+               struct usb_function *f;
+               struct usb_function *tmp;
+               struct gadget_config_name *cn;
+
+               cfg = container_of(c, struct config_usb_cfg, c);
+               if (!list_empty(&cfg->string_list)) {
+                       i = 0;
+                       list_for_each_entry(cn, &cfg->string_list, list) {
+                               cfg->gstrings[i] = &cn->stringtab_dev;
+                               cn->stringtab_dev.strings = &cn->strings;
+                               cn->strings.s = cn->configuration;
+                               i++;
+                       }
+                       cfg->gstrings[i] = NULL;
+                       s = usb_gstrings_attach(&gi->cdev, cfg->gstrings, 1);
+                       if (IS_ERR(s))
+                               goto err_comp_cleanup;
+                       c->iConfiguration = s[0].id;
+               }
+
+               list_for_each_entry_safe(f, tmp, &cfg->func_list, list) {
+                       list_del(&f->list);
+                       ret = usb_add_function(c, f);
+                       if (ret)
+                               goto err_purge_funcs;
+               }
+               usb_ep_autoconfig_reset(cdev->gadget);
+       }
+       usb_ep_autoconfig_reset(cdev->gadget);
+       return 0;
+
+err_purge_funcs:
+       purge_configs_funcs(gi);
+err_comp_cleanup:
+       composite_dev_cleanup(cdev);
+       return ret;
+}
+
+static void configfs_composite_unbind(struct usb_gadget *gadget)
+{
+       struct usb_composite_dev        *cdev;
+       struct gadget_info              *gi;
+
+       /* the gi->lock is hold by the caller */
+
+       cdev = get_gadget_data(gadget);
+       gi = container_of(cdev, struct gadget_info, cdev);
+
+       purge_configs_funcs(gi);
+       composite_dev_cleanup(cdev);
+       usb_ep_autoconfig_reset(cdev->gadget);
+       cdev->gadget = NULL;
+       set_gadget_data(gadget, NULL);
+}
+
+static const struct usb_gadget_driver configfs_driver_template = {
+       .bind           = configfs_composite_bind,
+       .unbind         = configfs_composite_unbind,
+
+       .setup          = composite_setup,
+       .disconnect     = composite_disconnect,
+
+       .max_speed      = USB_SPEED_SUPER,
+       .driver = {
+               .owner          = THIS_MODULE,
+               .name           = "configfs-gadget",
+       },
+};
+
+static struct config_group *gadgets_make(
+               struct config_group *group,
+               const char *name)
+{
+       struct gadget_info *gi;
+
+       gi = kzalloc(sizeof(*gi), GFP_KERNEL);
+       if (!gi)
+               return ERR_PTR(-ENOMEM);
+
+       gi->group.default_groups = gi->default_groups;
+       gi->group.default_groups[0] = &gi->functions_group;
+       gi->group.default_groups[1] = &gi->configs_group;
+       gi->group.default_groups[2] = &gi->strings_group;
+
+       config_group_init_type_name(&gi->functions_group, "functions",
+                       &functions_type);
+       config_group_init_type_name(&gi->configs_group, "configs",
+                       &config_desc_type);
+       config_group_init_type_name(&gi->strings_group, "strings",
+                       &gadget_strings_strings_type);
+
+       gi->composite.bind = configfs_do_nothing;
+       gi->composite.unbind = configfs_do_nothing;
+       gi->composite.suspend = NULL;
+       gi->composite.resume = NULL;
+       gi->composite.max_speed = USB_SPEED_SUPER;
+
+       mutex_init(&gi->lock);
+       INIT_LIST_HEAD(&gi->string_list);
+       INIT_LIST_HEAD(&gi->available_func);
+
+       composite_init_dev(&gi->cdev);
+       gi->cdev.desc.bLength = USB_DT_DEVICE_SIZE;
+       gi->cdev.desc.bDescriptorType = USB_DT_DEVICE;
+       gi->cdev.desc.bcdDevice = cpu_to_le16(get_default_bcdDevice());
+
+       gi->composite.gadget_driver = configfs_driver_template;
+
+       gi->composite.gadget_driver.function = kstrdup(name, GFP_KERNEL);
+       gi->composite.name = gi->composite.gadget_driver.function;
+
+       if (!gi->composite.gadget_driver.function)
+               goto err;
+
+#ifdef CONFIG_USB_OTG
+       gi->otg.bLength = sizeof(struct usb_otg_descriptor);
+       gi->otg.bDescriptorType = USB_DT_OTG;
+       gi->otg.bmAttributes = USB_OTG_SRP | USB_OTG_HNP;
+#endif
+
+       config_group_init_type_name(&gi->group, name,
+                               &gadget_root_type);
+       return &gi->group;
+err:
+       kfree(gi);
+       return ERR_PTR(-ENOMEM);
+}
+
+static void gadgets_drop(struct config_group *group, struct config_item *item)
+{
+       config_item_put(item);
+}
+
+static struct configfs_group_operations gadgets_ops = {
+       .make_group     = &gadgets_make,
+       .drop_item      = &gadgets_drop,
+};
+
+static struct config_item_type gadgets_type = {
+       .ct_group_ops   = &gadgets_ops,
+       .ct_owner       = THIS_MODULE,
+};
+
+static struct configfs_subsystem gadget_subsys = {
+       .su_group = {
+               .cg_item = {
+                       .ci_namebuf = "usb_gadget",
+                       .ci_type = &gadgets_type,
+               },
+       },
+       .su_mutex = __MUTEX_INITIALIZER(gadget_subsys.su_mutex),
+};
+
+static int __init gadget_cfs_init(void)
+{
+       int ret;
+
+       config_group_init(&gadget_subsys.su_group);
+
+       ret = configfs_register_subsystem(&gadget_subsys);
+       return ret;
+}
+module_init(gadget_cfs_init);
+
+static void __exit gadget_cfs_exit(void)
+{
+       configfs_unregister_subsystem(&gadget_subsys);
+}
+module_exit(gadget_cfs_exit);
index ba7daaa..4b7e33e 100644 (file)
@@ -763,6 +763,59 @@ static struct usb_function *acm_alloc_func(struct usb_function_instance *fi)
        return &acm->port.func;
 }
 
+static inline struct f_serial_opts *to_f_serial_opts(struct config_item *item)
+{
+       return container_of(to_config_group(item), struct f_serial_opts,
+                       func_inst.group);
+}
+
+CONFIGFS_ATTR_STRUCT(f_serial_opts);
+static ssize_t f_acm_attr_show(struct config_item *item,
+                                struct configfs_attribute *attr,
+                                char *page)
+{
+       struct f_serial_opts *opts = to_f_serial_opts(item);
+       struct f_serial_opts_attribute *f_serial_opts_attr =
+               container_of(attr, struct f_serial_opts_attribute, attr);
+       ssize_t ret = 0;
+
+       if (f_serial_opts_attr->show)
+               ret = f_serial_opts_attr->show(opts, page);
+       return ret;
+}
+
+static void acm_attr_release(struct config_item *item)
+{
+       struct f_serial_opts *opts = to_f_serial_opts(item);
+
+       usb_put_function_instance(&opts->func_inst);
+}
+
+static struct configfs_item_operations acm_item_ops = {
+       .release                = acm_attr_release,
+       .show_attribute         = f_acm_attr_show,
+};
+
+static ssize_t f_acm_port_num_show(struct f_serial_opts *opts, char *page)
+{
+       return sprintf(page, "%u\n", opts->port_num);
+}
+
+static struct f_serial_opts_attribute f_acm_port_num =
+       __CONFIGFS_ATTR_RO(port_num, f_acm_port_num_show);
+
+
+static struct configfs_attribute *acm_attrs[] = {
+       &f_acm_port_num.attr,
+       NULL,
+};
+
+static struct config_item_type acm_func_type = {
+       .ct_item_ops    = &acm_item_ops,
+       .ct_attrs       = acm_attrs,
+       .ct_owner       = THIS_MODULE,
+};
+
 static void acm_free_instance(struct usb_function_instance *fi)
 {
        struct f_serial_opts *opts;
@@ -786,6 +839,8 @@ static struct usb_function_instance *acm_alloc_instance(void)
                kfree(opts);
                return ERR_PTR(ret);
        }
+       config_group_init_type_name(&opts->func_inst.group, "",
+                       &acm_func_type);
        return &opts->func_inst;
 }
 DECLARE_USB_FUNCTION_INIT(acm, acm_alloc_instance, acm_alloc_func);
index 8860594..5e61589 100644 (file)
@@ -39,6 +39,7 @@
 #include <linux/usb/ch9.h>
 #include <linux/usb/gadget.h>
 #include <linux/log2.h>
+#include <linux/configfs.h>
 
 /*
  * USB function drivers should return USB_GADGET_DELAYED_STATUS if they
@@ -464,6 +465,8 @@ struct usb_function_driver {
 };
 
 struct usb_function_instance {
+       struct config_group group;
+       struct list_head cfs_list;
        struct usb_function_driver *fd;
        void (*free_func_inst)(struct usb_function_instance *inst);
 };
diff --git a/include/linux/usb/gadget_configfs.h b/include/linux/usb/gadget_configfs.h
new file mode 100644 (file)
index 0000000..d74c0ae
--- /dev/null
@@ -0,0 +1,110 @@
+#ifndef __GADGET_CONFIGFS__
+#define __GADGET_CONFIGFS__
+
+#include <linux/configfs.h>
+
+int check_user_usb_string(const char *name,
+               struct usb_gadget_strings *stringtab_dev);
+
+#define GS_STRINGS_W(__struct, __name) \
+       static ssize_t __struct##_##__name##_store(struct __struct *gs, \
+               const char *page, size_t len)           \
+{                                                      \
+       int ret;                                        \
+                                                       \
+       ret = usb_string_copy(page, &gs->__name);       \
+       if (ret)                                        \
+               return ret;                             \
+       return len;                                     \
+}
+
+#define GS_STRINGS_R(__struct, __name) \
+       static ssize_t __struct##_##__name##_show(struct __struct *gs, \
+                       char *page)     \
+{      \
+       return sprintf(page, "%s\n", gs->__name ?: ""); \
+}
+
+#define GS_STRING_ITEM_ATTR(struct_name, name) \
+       static struct struct_name##_attribute struct_name##_##name = \
+               __CONFIGFS_ATTR(name,  S_IRUGO | S_IWUSR,               \
+                               struct_name##_##name##_show,            \
+                               struct_name##_##name##_store)
+
+#define GS_STRINGS_RW(struct_name, _name)      \
+       GS_STRINGS_R(struct_name, _name)        \
+       GS_STRINGS_W(struct_name, _name)        \
+       GS_STRING_ITEM_ATTR(struct_name, _name)
+
+#define USB_CONFIG_STRING_RW_OPS(struct_in)                            \
+       CONFIGFS_ATTR_OPS(struct_in);                                   \
+                                                                       \
+static struct configfs_item_operations struct_in##_langid_item_ops = { \
+       .release                = struct_in##_attr_release,             \
+       .show_attribute         = struct_in##_attr_show,                \
+       .store_attribute        = struct_in##_attr_store,               \
+};                                                                     \
+                                                                       \
+static struct config_item_type struct_in##_langid_type = {             \
+       .ct_item_ops    = &struct_in##_langid_item_ops,                 \
+       .ct_attrs       = struct_in##_langid_attrs,                     \
+       .ct_owner       = THIS_MODULE,                                  \
+}
+
+#define USB_CONFIG_STRINGS_LANG(struct_in, struct_member)      \
+       static struct config_group *struct_in##_strings_make(           \
+                       struct config_group *group,                     \
+                       const char *name)                               \
+       {                                                               \
+       struct struct_member *gi;                                       \
+       struct struct_in *gs;                                           \
+       struct struct_in *new;                                          \
+       int langs = 0;                                                  \
+       int ret;                                                        \
+                                                                       \
+       new = kzalloc(sizeof(*new), GFP_KERNEL);                        \
+       if (!new)                                                       \
+               return ERR_PTR(-ENOMEM);                                \
+                                                                       \
+       ret = check_user_usb_string(name, &new->stringtab_dev);         \
+       if (ret)                                                        \
+               goto err;                                               \
+       config_group_init_type_name(&new->group, name,                  \
+                       &struct_in##_langid_type);                      \
+                                                                       \
+       gi = container_of(group, struct struct_member, strings_group);  \
+       ret = -EEXIST;                                                  \
+       list_for_each_entry(gs, &gi->string_list, list) {               \
+               if (gs->stringtab_dev.language == new->stringtab_dev.language) \
+                       goto err;                                       \
+               langs++;                                                \
+       }                                                               \
+       ret = -EOVERFLOW;                                               \
+       if (langs >= MAX_USB_STRING_LANGS)                              \
+               goto err;                                               \
+                                                                       \
+       list_add_tail(&new->list, &gi->string_list);                    \
+       return &new->group;                                             \
+err:                                                                   \
+       kfree(new);                                                     \
+       return ERR_PTR(ret);                                            \
+}                                                                      \
+                                                                       \
+static void struct_in##_strings_drop(                                  \
+               struct config_group *group,                             \
+               struct config_item *item)                               \
+{                                                                      \
+       config_item_put(item);                                          \
+}                                                                      \
+                                                                       \
+static struct configfs_group_operations struct_in##_strings_ops = {    \
+       .make_group     = &struct_in##_strings_make,                    \
+       .drop_item      = &struct_in##_strings_drop,                    \
+};                                                                     \
+                                                                       \
+static struct config_item_type struct_in##_strings_type = {            \
+       .ct_group_ops   = &struct_in##_strings_ops,                     \
+       .ct_owner       = THIS_MODULE,                                  \
+}
+
+#endif