Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <linux/configfs.h>
0003 #include <linux/module.h>
0004 #include <linux/slab.h>
0005 #include <linux/device.h>
0006 #include <linux/nls.h>
0007 #include <linux/usb/composite.h>
0008 #include <linux/usb/gadget_configfs.h>
0009 #include "configfs.h"
0010 #include "u_f.h"
0011 #include "u_os_desc.h"
0012 
0013 int check_user_usb_string(const char *name,
0014         struct usb_gadget_strings *stringtab_dev)
0015 {
0016     u16 num;
0017     int ret;
0018 
0019     ret = kstrtou16(name, 0, &num);
0020     if (ret)
0021         return ret;
0022 
0023     if (!usb_validate_langid(num))
0024         return -EINVAL;
0025 
0026     stringtab_dev->language = num;
0027     return 0;
0028 }
0029 
0030 #define MAX_NAME_LEN    40
0031 #define MAX_USB_STRING_LANGS 2
0032 
0033 static const struct usb_descriptor_header *otg_desc[2];
0034 
0035 struct gadget_info {
0036     struct config_group group;
0037     struct config_group functions_group;
0038     struct config_group configs_group;
0039     struct config_group strings_group;
0040     struct config_group os_desc_group;
0041 
0042     struct mutex lock;
0043     struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1];
0044     struct list_head string_list;
0045     struct list_head available_func;
0046 
0047     struct usb_composite_driver composite;
0048     struct usb_composite_dev cdev;
0049     bool use_os_desc;
0050     char b_vendor_code;
0051     char qw_sign[OS_STRING_QW_SIGN_LEN];
0052     spinlock_t spinlock;
0053     bool unbind;
0054 };
0055 
0056 static inline struct gadget_info *to_gadget_info(struct config_item *item)
0057 {
0058     return container_of(to_config_group(item), struct gadget_info, group);
0059 }
0060 
0061 struct config_usb_cfg {
0062     struct config_group group;
0063     struct config_group strings_group;
0064     struct list_head string_list;
0065     struct usb_configuration c;
0066     struct list_head func_list;
0067     struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1];
0068 };
0069 
0070 static inline struct config_usb_cfg *to_config_usb_cfg(struct config_item *item)
0071 {
0072     return container_of(to_config_group(item), struct config_usb_cfg,
0073             group);
0074 }
0075 
0076 static inline struct gadget_info *cfg_to_gadget_info(struct config_usb_cfg *cfg)
0077 {
0078     return container_of(cfg->c.cdev, struct gadget_info, cdev);
0079 }
0080 
0081 struct gadget_strings {
0082     struct usb_gadget_strings stringtab_dev;
0083     struct usb_string strings[USB_GADGET_FIRST_AVAIL_IDX];
0084     char *manufacturer;
0085     char *product;
0086     char *serialnumber;
0087 
0088     struct config_group group;
0089     struct list_head list;
0090 };
0091 
0092 struct gadget_config_name {
0093     struct usb_gadget_strings stringtab_dev;
0094     struct usb_string strings;
0095     char *configuration;
0096 
0097     struct config_group group;
0098     struct list_head list;
0099 };
0100 
0101 #define USB_MAX_STRING_WITH_NULL_LEN    (USB_MAX_STRING_LEN+1)
0102 
0103 static int usb_string_copy(const char *s, char **s_copy)
0104 {
0105     int ret;
0106     char *str;
0107     char *copy = *s_copy;
0108     ret = strlen(s);
0109     if (ret > USB_MAX_STRING_LEN)
0110         return -EOVERFLOW;
0111 
0112     if (copy) {
0113         str = copy;
0114     } else {
0115         str = kmalloc(USB_MAX_STRING_WITH_NULL_LEN, GFP_KERNEL);
0116         if (!str)
0117             return -ENOMEM;
0118     }
0119     strcpy(str, s);
0120     if (str[ret - 1] == '\n')
0121         str[ret - 1] = '\0';
0122     *s_copy = str;
0123     return 0;
0124 }
0125 
0126 #define GI_DEVICE_DESC_SIMPLE_R_u8(__name)  \
0127 static ssize_t gadget_dev_desc_##__name##_show(struct config_item *item, \
0128             char *page) \
0129 {   \
0130     return sprintf(page, "0x%02x\n", \
0131         to_gadget_info(item)->cdev.desc.__name); \
0132 }
0133 
0134 #define GI_DEVICE_DESC_SIMPLE_R_u16(__name) \
0135 static ssize_t gadget_dev_desc_##__name##_show(struct config_item *item, \
0136             char *page) \
0137 {   \
0138     return sprintf(page, "0x%04x\n", \
0139         le16_to_cpup(&to_gadget_info(item)->cdev.desc.__name)); \
0140 }
0141 
0142 
0143 #define GI_DEVICE_DESC_SIMPLE_W_u8(_name)       \
0144 static ssize_t gadget_dev_desc_##_name##_store(struct config_item *item, \
0145         const char *page, size_t len)       \
0146 {                           \
0147     u8 val;                     \
0148     int ret;                    \
0149     ret = kstrtou8(page, 0, &val);          \
0150     if (ret)                    \
0151         return ret;             \
0152     to_gadget_info(item)->cdev.desc._name = val;    \
0153     return len;                 \
0154 }
0155 
0156 #define GI_DEVICE_DESC_SIMPLE_W_u16(_name)  \
0157 static ssize_t gadget_dev_desc_##_name##_store(struct config_item *item, \
0158         const char *page, size_t len)       \
0159 {                           \
0160     u16 val;                    \
0161     int ret;                    \
0162     ret = kstrtou16(page, 0, &val);         \
0163     if (ret)                    \
0164         return ret;             \
0165     to_gadget_info(item)->cdev.desc._name = cpu_to_le16p(&val); \
0166     return len;                 \
0167 }
0168 
0169 #define GI_DEVICE_DESC_SIMPLE_RW(_name, _type)  \
0170     GI_DEVICE_DESC_SIMPLE_R_##_type(_name)  \
0171     GI_DEVICE_DESC_SIMPLE_W_##_type(_name)
0172 
0173 GI_DEVICE_DESC_SIMPLE_R_u16(bcdUSB);
0174 GI_DEVICE_DESC_SIMPLE_RW(bDeviceClass, u8);
0175 GI_DEVICE_DESC_SIMPLE_RW(bDeviceSubClass, u8);
0176 GI_DEVICE_DESC_SIMPLE_RW(bDeviceProtocol, u8);
0177 GI_DEVICE_DESC_SIMPLE_RW(bMaxPacketSize0, u8);
0178 GI_DEVICE_DESC_SIMPLE_RW(idVendor, u16);
0179 GI_DEVICE_DESC_SIMPLE_RW(idProduct, u16);
0180 GI_DEVICE_DESC_SIMPLE_R_u16(bcdDevice);
0181 
0182 static ssize_t is_valid_bcd(u16 bcd_val)
0183 {
0184     if ((bcd_val & 0xf) > 9)
0185         return -EINVAL;
0186     if (((bcd_val >> 4) & 0xf) > 9)
0187         return -EINVAL;
0188     if (((bcd_val >> 8) & 0xf) > 9)
0189         return -EINVAL;
0190     if (((bcd_val >> 12) & 0xf) > 9)
0191         return -EINVAL;
0192     return 0;
0193 }
0194 
0195 static ssize_t gadget_dev_desc_bcdDevice_store(struct config_item *item,
0196         const char *page, size_t len)
0197 {
0198     u16 bcdDevice;
0199     int ret;
0200 
0201     ret = kstrtou16(page, 0, &bcdDevice);
0202     if (ret)
0203         return ret;
0204     ret = is_valid_bcd(bcdDevice);
0205     if (ret)
0206         return ret;
0207 
0208     to_gadget_info(item)->cdev.desc.bcdDevice = cpu_to_le16(bcdDevice);
0209     return len;
0210 }
0211 
0212 static ssize_t gadget_dev_desc_bcdUSB_store(struct config_item *item,
0213         const char *page, size_t len)
0214 {
0215     u16 bcdUSB;
0216     int ret;
0217 
0218     ret = kstrtou16(page, 0, &bcdUSB);
0219     if (ret)
0220         return ret;
0221     ret = is_valid_bcd(bcdUSB);
0222     if (ret)
0223         return ret;
0224 
0225     to_gadget_info(item)->cdev.desc.bcdUSB = cpu_to_le16(bcdUSB);
0226     return len;
0227 }
0228 
0229 static ssize_t gadget_dev_desc_UDC_show(struct config_item *item, char *page)
0230 {
0231     struct gadget_info *gi = to_gadget_info(item);
0232     char *udc_name;
0233     int ret;
0234 
0235     mutex_lock(&gi->lock);
0236     udc_name = gi->composite.gadget_driver.udc_name;
0237     ret = sprintf(page, "%s\n", udc_name ?: "");
0238     mutex_unlock(&gi->lock);
0239 
0240     return ret;
0241 }
0242 
0243 static int unregister_gadget(struct gadget_info *gi)
0244 {
0245     int ret;
0246 
0247     if (!gi->composite.gadget_driver.udc_name)
0248         return -ENODEV;
0249 
0250     ret = usb_gadget_unregister_driver(&gi->composite.gadget_driver);
0251     if (ret)
0252         return ret;
0253     kfree(gi->composite.gadget_driver.udc_name);
0254     gi->composite.gadget_driver.udc_name = NULL;
0255     return 0;
0256 }
0257 
0258 static ssize_t gadget_dev_desc_UDC_store(struct config_item *item,
0259         const char *page, size_t len)
0260 {
0261     struct gadget_info *gi = to_gadget_info(item);
0262     char *name;
0263     int ret;
0264 
0265     if (strlen(page) < len)
0266         return -EOVERFLOW;
0267 
0268     name = kstrdup(page, GFP_KERNEL);
0269     if (!name)
0270         return -ENOMEM;
0271     if (name[len - 1] == '\n')
0272         name[len - 1] = '\0';
0273 
0274     mutex_lock(&gi->lock);
0275 
0276     if (!strlen(name)) {
0277         ret = unregister_gadget(gi);
0278         if (ret)
0279             goto err;
0280         kfree(name);
0281     } else {
0282         if (gi->composite.gadget_driver.udc_name) {
0283             ret = -EBUSY;
0284             goto err;
0285         }
0286         gi->composite.gadget_driver.udc_name = name;
0287         ret = usb_gadget_register_driver(&gi->composite.gadget_driver);
0288         if (ret) {
0289             gi->composite.gadget_driver.udc_name = NULL;
0290             goto err;
0291         }
0292     }
0293     mutex_unlock(&gi->lock);
0294     return len;
0295 err:
0296     kfree(name);
0297     mutex_unlock(&gi->lock);
0298     return ret;
0299 }
0300 
0301 static ssize_t gadget_dev_desc_max_speed_show(struct config_item *item,
0302                           char *page)
0303 {
0304     enum usb_device_speed speed = to_gadget_info(item)->composite.max_speed;
0305 
0306     return sprintf(page, "%s\n", usb_speed_string(speed));
0307 }
0308 
0309 static ssize_t gadget_dev_desc_max_speed_store(struct config_item *item,
0310                            const char *page, size_t len)
0311 {
0312     struct gadget_info *gi = to_gadget_info(item);
0313 
0314     mutex_lock(&gi->lock);
0315 
0316     /* Prevent changing of max_speed after the driver is binded */
0317     if (gi->composite.gadget_driver.udc_name)
0318         goto err;
0319 
0320     if (strncmp(page, "super-speed-plus", 16) == 0)
0321         gi->composite.max_speed = USB_SPEED_SUPER_PLUS;
0322     else if (strncmp(page, "super-speed", 11) == 0)
0323         gi->composite.max_speed = USB_SPEED_SUPER;
0324     else if (strncmp(page, "high-speed", 10) == 0)
0325         gi->composite.max_speed = USB_SPEED_HIGH;
0326     else if (strncmp(page, "full-speed", 10) == 0)
0327         gi->composite.max_speed = USB_SPEED_FULL;
0328     else if (strncmp(page, "low-speed", 9) == 0)
0329         gi->composite.max_speed = USB_SPEED_LOW;
0330     else
0331         goto err;
0332 
0333     gi->composite.gadget_driver.max_speed = gi->composite.max_speed;
0334 
0335     mutex_unlock(&gi->lock);
0336     return len;
0337 err:
0338     mutex_unlock(&gi->lock);
0339     return -EINVAL;
0340 }
0341 
0342 CONFIGFS_ATTR(gadget_dev_desc_, bDeviceClass);
0343 CONFIGFS_ATTR(gadget_dev_desc_, bDeviceSubClass);
0344 CONFIGFS_ATTR(gadget_dev_desc_, bDeviceProtocol);
0345 CONFIGFS_ATTR(gadget_dev_desc_, bMaxPacketSize0);
0346 CONFIGFS_ATTR(gadget_dev_desc_, idVendor);
0347 CONFIGFS_ATTR(gadget_dev_desc_, idProduct);
0348 CONFIGFS_ATTR(gadget_dev_desc_, bcdDevice);
0349 CONFIGFS_ATTR(gadget_dev_desc_, bcdUSB);
0350 CONFIGFS_ATTR(gadget_dev_desc_, UDC);
0351 CONFIGFS_ATTR(gadget_dev_desc_, max_speed);
0352 
0353 static struct configfs_attribute *gadget_root_attrs[] = {
0354     &gadget_dev_desc_attr_bDeviceClass,
0355     &gadget_dev_desc_attr_bDeviceSubClass,
0356     &gadget_dev_desc_attr_bDeviceProtocol,
0357     &gadget_dev_desc_attr_bMaxPacketSize0,
0358     &gadget_dev_desc_attr_idVendor,
0359     &gadget_dev_desc_attr_idProduct,
0360     &gadget_dev_desc_attr_bcdDevice,
0361     &gadget_dev_desc_attr_bcdUSB,
0362     &gadget_dev_desc_attr_UDC,
0363     &gadget_dev_desc_attr_max_speed,
0364     NULL,
0365 };
0366 
0367 static inline struct gadget_strings *to_gadget_strings(struct config_item *item)
0368 {
0369     return container_of(to_config_group(item), struct gadget_strings,
0370              group);
0371 }
0372 
0373 static inline struct gadget_config_name *to_gadget_config_name(
0374         struct config_item *item)
0375 {
0376     return container_of(to_config_group(item), struct gadget_config_name,
0377              group);
0378 }
0379 
0380 static inline struct usb_function_instance *to_usb_function_instance(
0381         struct config_item *item)
0382 {
0383     return container_of(to_config_group(item),
0384              struct usb_function_instance, group);
0385 }
0386 
0387 static void gadget_info_attr_release(struct config_item *item)
0388 {
0389     struct gadget_info *gi = to_gadget_info(item);
0390 
0391     WARN_ON(!list_empty(&gi->cdev.configs));
0392     WARN_ON(!list_empty(&gi->string_list));
0393     WARN_ON(!list_empty(&gi->available_func));
0394     kfree(gi->composite.gadget_driver.function);
0395     kfree(gi);
0396 }
0397 
0398 static struct configfs_item_operations gadget_root_item_ops = {
0399     .release                = gadget_info_attr_release,
0400 };
0401 
0402 static void gadget_config_attr_release(struct config_item *item)
0403 {
0404     struct config_usb_cfg *cfg = to_config_usb_cfg(item);
0405 
0406     WARN_ON(!list_empty(&cfg->c.functions));
0407     list_del(&cfg->c.list);
0408     kfree(cfg->c.label);
0409     kfree(cfg);
0410 }
0411 
0412 static int config_usb_cfg_link(
0413     struct config_item *usb_cfg_ci,
0414     struct config_item *usb_func_ci)
0415 {
0416     struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci);
0417     struct gadget_info *gi = cfg_to_gadget_info(cfg);
0418 
0419     struct usb_function_instance *fi =
0420             to_usb_function_instance(usb_func_ci);
0421     struct usb_function_instance *a_fi = NULL, *iter;
0422     struct usb_function *f;
0423     int ret;
0424 
0425     mutex_lock(&gi->lock);
0426     /*
0427      * Make sure this function is from within our _this_ gadget and not
0428      * from another gadget or a random directory.
0429      * Also a function instance can only be linked once.
0430      */
0431     list_for_each_entry(iter, &gi->available_func, cfs_list) {
0432         if (iter != fi)
0433             continue;
0434         a_fi = iter;
0435         break;
0436     }
0437     if (!a_fi) {
0438         ret = -EINVAL;
0439         goto out;
0440     }
0441 
0442     list_for_each_entry(f, &cfg->func_list, list) {
0443         if (f->fi == fi) {
0444             ret = -EEXIST;
0445             goto out;
0446         }
0447     }
0448 
0449     f = usb_get_function(fi);
0450     if (IS_ERR(f)) {
0451         ret = PTR_ERR(f);
0452         goto out;
0453     }
0454 
0455     /* stash the function until we bind it to the gadget */
0456     list_add_tail(&f->list, &cfg->func_list);
0457     ret = 0;
0458 out:
0459     mutex_unlock(&gi->lock);
0460     return ret;
0461 }
0462 
0463 static void config_usb_cfg_unlink(
0464     struct config_item *usb_cfg_ci,
0465     struct config_item *usb_func_ci)
0466 {
0467     struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci);
0468     struct gadget_info *gi = cfg_to_gadget_info(cfg);
0469 
0470     struct usb_function_instance *fi =
0471             to_usb_function_instance(usb_func_ci);
0472     struct usb_function *f;
0473 
0474     /*
0475      * ideally I would like to forbid to unlink functions while a gadget is
0476      * bound to an UDC. Since this isn't possible at the moment, we simply
0477      * force an unbind, the function is available here and then we can
0478      * remove the function.
0479      */
0480     mutex_lock(&gi->lock);
0481     if (gi->composite.gadget_driver.udc_name)
0482         unregister_gadget(gi);
0483     WARN_ON(gi->composite.gadget_driver.udc_name);
0484 
0485     list_for_each_entry(f, &cfg->func_list, list) {
0486         if (f->fi == fi) {
0487             list_del(&f->list);
0488             usb_put_function(f);
0489             mutex_unlock(&gi->lock);
0490             return;
0491         }
0492     }
0493     mutex_unlock(&gi->lock);
0494     WARN(1, "Unable to locate function to unbind\n");
0495 }
0496 
0497 static struct configfs_item_operations gadget_config_item_ops = {
0498     .release                = gadget_config_attr_release,
0499     .allow_link             = config_usb_cfg_link,
0500     .drop_link              = config_usb_cfg_unlink,
0501 };
0502 
0503 
0504 static ssize_t gadget_config_desc_MaxPower_show(struct config_item *item,
0505         char *page)
0506 {
0507     struct config_usb_cfg *cfg = to_config_usb_cfg(item);
0508 
0509     return sprintf(page, "%u\n", cfg->c.MaxPower);
0510 }
0511 
0512 static ssize_t gadget_config_desc_MaxPower_store(struct config_item *item,
0513         const char *page, size_t len)
0514 {
0515     struct config_usb_cfg *cfg = to_config_usb_cfg(item);
0516     u16 val;
0517     int ret;
0518     ret = kstrtou16(page, 0, &val);
0519     if (ret)
0520         return ret;
0521     if (DIV_ROUND_UP(val, 8) > 0xff)
0522         return -ERANGE;
0523     cfg->c.MaxPower = val;
0524     return len;
0525 }
0526 
0527 static ssize_t gadget_config_desc_bmAttributes_show(struct config_item *item,
0528         char *page)
0529 {
0530     struct config_usb_cfg *cfg = to_config_usb_cfg(item);
0531 
0532     return sprintf(page, "0x%02x\n", cfg->c.bmAttributes);
0533 }
0534 
0535 static ssize_t gadget_config_desc_bmAttributes_store(struct config_item *item,
0536         const char *page, size_t len)
0537 {
0538     struct config_usb_cfg *cfg = to_config_usb_cfg(item);
0539     u8 val;
0540     int ret;
0541     ret = kstrtou8(page, 0, &val);
0542     if (ret)
0543         return ret;
0544     if (!(val & USB_CONFIG_ATT_ONE))
0545         return -EINVAL;
0546     if (val & ~(USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER |
0547                 USB_CONFIG_ATT_WAKEUP))
0548         return -EINVAL;
0549     cfg->c.bmAttributes = val;
0550     return len;
0551 }
0552 
0553 CONFIGFS_ATTR(gadget_config_desc_, MaxPower);
0554 CONFIGFS_ATTR(gadget_config_desc_, bmAttributes);
0555 
0556 static struct configfs_attribute *gadget_config_attrs[] = {
0557     &gadget_config_desc_attr_MaxPower,
0558     &gadget_config_desc_attr_bmAttributes,
0559     NULL,
0560 };
0561 
0562 static const struct config_item_type gadget_config_type = {
0563     .ct_item_ops    = &gadget_config_item_ops,
0564     .ct_attrs   = gadget_config_attrs,
0565     .ct_owner   = THIS_MODULE,
0566 };
0567 
0568 static const struct config_item_type gadget_root_type = {
0569     .ct_item_ops    = &gadget_root_item_ops,
0570     .ct_attrs   = gadget_root_attrs,
0571     .ct_owner   = THIS_MODULE,
0572 };
0573 
0574 static void composite_init_dev(struct usb_composite_dev *cdev)
0575 {
0576     spin_lock_init(&cdev->lock);
0577     INIT_LIST_HEAD(&cdev->configs);
0578     INIT_LIST_HEAD(&cdev->gstrings);
0579 }
0580 
0581 static struct config_group *function_make(
0582         struct config_group *group,
0583         const char *name)
0584 {
0585     struct gadget_info *gi;
0586     struct usb_function_instance *fi;
0587     char buf[MAX_NAME_LEN];
0588     char *func_name;
0589     char *instance_name;
0590     int ret;
0591 
0592     ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
0593     if (ret >= MAX_NAME_LEN)
0594         return ERR_PTR(-ENAMETOOLONG);
0595 
0596     func_name = buf;
0597     instance_name = strchr(func_name, '.');
0598     if (!instance_name) {
0599         pr_err("Unable to locate . in FUNC.INSTANCE\n");
0600         return ERR_PTR(-EINVAL);
0601     }
0602     *instance_name = '\0';
0603     instance_name++;
0604 
0605     fi = usb_get_function_instance(func_name);
0606     if (IS_ERR(fi))
0607         return ERR_CAST(fi);
0608 
0609     ret = config_item_set_name(&fi->group.cg_item, "%s", name);
0610     if (ret) {
0611         usb_put_function_instance(fi);
0612         return ERR_PTR(ret);
0613     }
0614     if (fi->set_inst_name) {
0615         ret = fi->set_inst_name(fi, instance_name);
0616         if (ret) {
0617             usb_put_function_instance(fi);
0618             return ERR_PTR(ret);
0619         }
0620     }
0621 
0622     gi = container_of(group, struct gadget_info, functions_group);
0623 
0624     mutex_lock(&gi->lock);
0625     list_add_tail(&fi->cfs_list, &gi->available_func);
0626     mutex_unlock(&gi->lock);
0627     return &fi->group;
0628 }
0629 
0630 static void function_drop(
0631         struct config_group *group,
0632         struct config_item *item)
0633 {
0634     struct usb_function_instance *fi = to_usb_function_instance(item);
0635     struct gadget_info *gi;
0636 
0637     gi = container_of(group, struct gadget_info, functions_group);
0638 
0639     mutex_lock(&gi->lock);
0640     list_del(&fi->cfs_list);
0641     mutex_unlock(&gi->lock);
0642     config_item_put(item);
0643 }
0644 
0645 static struct configfs_group_operations functions_ops = {
0646     .make_group     = &function_make,
0647     .drop_item      = &function_drop,
0648 };
0649 
0650 static const struct config_item_type functions_type = {
0651     .ct_group_ops   = &functions_ops,
0652     .ct_owner       = THIS_MODULE,
0653 };
0654 
0655 GS_STRINGS_RW(gadget_config_name, configuration);
0656 
0657 static struct configfs_attribute *gadget_config_name_langid_attrs[] = {
0658     &gadget_config_name_attr_configuration,
0659     NULL,
0660 };
0661 
0662 static void gadget_config_name_attr_release(struct config_item *item)
0663 {
0664     struct gadget_config_name *cn = to_gadget_config_name(item);
0665 
0666     kfree(cn->configuration);
0667 
0668     list_del(&cn->list);
0669     kfree(cn);
0670 }
0671 
0672 USB_CONFIG_STRING_RW_OPS(gadget_config_name);
0673 USB_CONFIG_STRINGS_LANG(gadget_config_name, config_usb_cfg);
0674 
0675 static struct config_group *config_desc_make(
0676         struct config_group *group,
0677         const char *name)
0678 {
0679     struct gadget_info *gi;
0680     struct config_usb_cfg *cfg;
0681     char buf[MAX_NAME_LEN];
0682     char *num_str;
0683     u8 num;
0684     int ret;
0685 
0686     gi = container_of(group, struct gadget_info, configs_group);
0687     ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
0688     if (ret >= MAX_NAME_LEN)
0689         return ERR_PTR(-ENAMETOOLONG);
0690 
0691     num_str = strchr(buf, '.');
0692     if (!num_str) {
0693         pr_err("Unable to locate . in name.bConfigurationValue\n");
0694         return ERR_PTR(-EINVAL);
0695     }
0696 
0697     *num_str = '\0';
0698     num_str++;
0699 
0700     if (!strlen(buf))
0701         return ERR_PTR(-EINVAL);
0702 
0703     ret = kstrtou8(num_str, 0, &num);
0704     if (ret)
0705         return ERR_PTR(ret);
0706 
0707     cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
0708     if (!cfg)
0709         return ERR_PTR(-ENOMEM);
0710     cfg->c.label = kstrdup(buf, GFP_KERNEL);
0711     if (!cfg->c.label) {
0712         ret = -ENOMEM;
0713         goto err;
0714     }
0715     cfg->c.bConfigurationValue = num;
0716     cfg->c.MaxPower = CONFIG_USB_GADGET_VBUS_DRAW;
0717     cfg->c.bmAttributes = USB_CONFIG_ATT_ONE;
0718     INIT_LIST_HEAD(&cfg->string_list);
0719     INIT_LIST_HEAD(&cfg->func_list);
0720 
0721     config_group_init_type_name(&cfg->group, name,
0722                 &gadget_config_type);
0723 
0724     config_group_init_type_name(&cfg->strings_group, "strings",
0725             &gadget_config_name_strings_type);
0726     configfs_add_default_group(&cfg->strings_group, &cfg->group);
0727 
0728     ret = usb_add_config_only(&gi->cdev, &cfg->c);
0729     if (ret)
0730         goto err;
0731 
0732     return &cfg->group;
0733 err:
0734     kfree(cfg->c.label);
0735     kfree(cfg);
0736     return ERR_PTR(ret);
0737 }
0738 
0739 static void config_desc_drop(
0740         struct config_group *group,
0741         struct config_item *item)
0742 {
0743     config_item_put(item);
0744 }
0745 
0746 static struct configfs_group_operations config_desc_ops = {
0747     .make_group     = &config_desc_make,
0748     .drop_item      = &config_desc_drop,
0749 };
0750 
0751 static const struct config_item_type config_desc_type = {
0752     .ct_group_ops   = &config_desc_ops,
0753     .ct_owner       = THIS_MODULE,
0754 };
0755 
0756 GS_STRINGS_RW(gadget_strings, manufacturer);
0757 GS_STRINGS_RW(gadget_strings, product);
0758 GS_STRINGS_RW(gadget_strings, serialnumber);
0759 
0760 static struct configfs_attribute *gadget_strings_langid_attrs[] = {
0761     &gadget_strings_attr_manufacturer,
0762     &gadget_strings_attr_product,
0763     &gadget_strings_attr_serialnumber,
0764     NULL,
0765 };
0766 
0767 static void gadget_strings_attr_release(struct config_item *item)
0768 {
0769     struct gadget_strings *gs = to_gadget_strings(item);
0770 
0771     kfree(gs->manufacturer);
0772     kfree(gs->product);
0773     kfree(gs->serialnumber);
0774 
0775     list_del(&gs->list);
0776     kfree(gs);
0777 }
0778 
0779 USB_CONFIG_STRING_RW_OPS(gadget_strings);
0780 USB_CONFIG_STRINGS_LANG(gadget_strings, gadget_info);
0781 
0782 static inline struct gadget_info *os_desc_item_to_gadget_info(
0783         struct config_item *item)
0784 {
0785     return container_of(to_config_group(item),
0786             struct gadget_info, os_desc_group);
0787 }
0788 
0789 static ssize_t os_desc_use_show(struct config_item *item, char *page)
0790 {
0791     return sprintf(page, "%d\n",
0792             os_desc_item_to_gadget_info(item)->use_os_desc);
0793 }
0794 
0795 static ssize_t os_desc_use_store(struct config_item *item, const char *page,
0796                  size_t len)
0797 {
0798     struct gadget_info *gi = os_desc_item_to_gadget_info(item);
0799     int ret;
0800     bool use;
0801 
0802     mutex_lock(&gi->lock);
0803     ret = strtobool(page, &use);
0804     if (!ret) {
0805         gi->use_os_desc = use;
0806         ret = len;
0807     }
0808     mutex_unlock(&gi->lock);
0809 
0810     return ret;
0811 }
0812 
0813 static ssize_t os_desc_b_vendor_code_show(struct config_item *item, char *page)
0814 {
0815     return sprintf(page, "0x%02x\n",
0816             os_desc_item_to_gadget_info(item)->b_vendor_code);
0817 }
0818 
0819 static ssize_t os_desc_b_vendor_code_store(struct config_item *item,
0820                        const char *page, size_t len)
0821 {
0822     struct gadget_info *gi = os_desc_item_to_gadget_info(item);
0823     int ret;
0824     u8 b_vendor_code;
0825 
0826     mutex_lock(&gi->lock);
0827     ret = kstrtou8(page, 0, &b_vendor_code);
0828     if (!ret) {
0829         gi->b_vendor_code = b_vendor_code;
0830         ret = len;
0831     }
0832     mutex_unlock(&gi->lock);
0833 
0834     return ret;
0835 }
0836 
0837 static ssize_t os_desc_qw_sign_show(struct config_item *item, char *page)
0838 {
0839     struct gadget_info *gi = os_desc_item_to_gadget_info(item);
0840     int res;
0841 
0842     res = utf16s_to_utf8s((wchar_t *) gi->qw_sign, OS_STRING_QW_SIGN_LEN,
0843                   UTF16_LITTLE_ENDIAN, page, PAGE_SIZE - 1);
0844     page[res++] = '\n';
0845 
0846     return res;
0847 }
0848 
0849 static ssize_t os_desc_qw_sign_store(struct config_item *item, const char *page,
0850                      size_t len)
0851 {
0852     struct gadget_info *gi = os_desc_item_to_gadget_info(item);
0853     int res, l;
0854 
0855     l = min((int)len, OS_STRING_QW_SIGN_LEN >> 1);
0856     if (page[l - 1] == '\n')
0857         --l;
0858 
0859     mutex_lock(&gi->lock);
0860     res = utf8s_to_utf16s(page, l,
0861                   UTF16_LITTLE_ENDIAN, (wchar_t *) gi->qw_sign,
0862                   OS_STRING_QW_SIGN_LEN);
0863     if (res > 0)
0864         res = len;
0865     mutex_unlock(&gi->lock);
0866 
0867     return res;
0868 }
0869 
0870 CONFIGFS_ATTR(os_desc_, use);
0871 CONFIGFS_ATTR(os_desc_, b_vendor_code);
0872 CONFIGFS_ATTR(os_desc_, qw_sign);
0873 
0874 static struct configfs_attribute *os_desc_attrs[] = {
0875     &os_desc_attr_use,
0876     &os_desc_attr_b_vendor_code,
0877     &os_desc_attr_qw_sign,
0878     NULL,
0879 };
0880 
0881 static int os_desc_link(struct config_item *os_desc_ci,
0882             struct config_item *usb_cfg_ci)
0883 {
0884     struct gadget_info *gi = os_desc_item_to_gadget_info(os_desc_ci);
0885     struct usb_composite_dev *cdev = &gi->cdev;
0886     struct config_usb_cfg *c_target = to_config_usb_cfg(usb_cfg_ci);
0887     struct usb_configuration *c = NULL, *iter;
0888     int ret;
0889 
0890     mutex_lock(&gi->lock);
0891     list_for_each_entry(iter, &cdev->configs, list) {
0892         if (iter != &c_target->c)
0893             continue;
0894         c = iter;
0895         break;
0896     }
0897     if (!c) {
0898         ret = -EINVAL;
0899         goto out;
0900     }
0901 
0902     if (cdev->os_desc_config) {
0903         ret = -EBUSY;
0904         goto out;
0905     }
0906 
0907     cdev->os_desc_config = &c_target->c;
0908     ret = 0;
0909 
0910 out:
0911     mutex_unlock(&gi->lock);
0912     return ret;
0913 }
0914 
0915 static void os_desc_unlink(struct config_item *os_desc_ci,
0916               struct config_item *usb_cfg_ci)
0917 {
0918     struct gadget_info *gi = os_desc_item_to_gadget_info(os_desc_ci);
0919     struct usb_composite_dev *cdev = &gi->cdev;
0920 
0921     mutex_lock(&gi->lock);
0922     if (gi->composite.gadget_driver.udc_name)
0923         unregister_gadget(gi);
0924     cdev->os_desc_config = NULL;
0925     WARN_ON(gi->composite.gadget_driver.udc_name);
0926     mutex_unlock(&gi->lock);
0927 }
0928 
0929 static struct configfs_item_operations os_desc_ops = {
0930     .allow_link     = os_desc_link,
0931     .drop_link      = os_desc_unlink,
0932 };
0933 
0934 static struct config_item_type os_desc_type = {
0935     .ct_item_ops    = &os_desc_ops,
0936     .ct_attrs   = os_desc_attrs,
0937     .ct_owner   = THIS_MODULE,
0938 };
0939 
0940 static inline struct usb_os_desc_ext_prop
0941 *to_usb_os_desc_ext_prop(struct config_item *item)
0942 {
0943     return container_of(item, struct usb_os_desc_ext_prop, item);
0944 }
0945 
0946 static ssize_t ext_prop_type_show(struct config_item *item, char *page)
0947 {
0948     return sprintf(page, "%d\n", to_usb_os_desc_ext_prop(item)->type);
0949 }
0950 
0951 static ssize_t ext_prop_type_store(struct config_item *item,
0952                    const char *page, size_t len)
0953 {
0954     struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
0955     struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent);
0956     u8 type;
0957     int ret;
0958 
0959     if (desc->opts_mutex)
0960         mutex_lock(desc->opts_mutex);
0961     ret = kstrtou8(page, 0, &type);
0962     if (ret)
0963         goto end;
0964     if (type < USB_EXT_PROP_UNICODE || type > USB_EXT_PROP_UNICODE_MULTI) {
0965         ret = -EINVAL;
0966         goto end;
0967     }
0968 
0969     if ((ext_prop->type == USB_EXT_PROP_BINARY ||
0970         ext_prop->type == USB_EXT_PROP_LE32 ||
0971         ext_prop->type == USB_EXT_PROP_BE32) &&
0972         (type == USB_EXT_PROP_UNICODE ||
0973         type == USB_EXT_PROP_UNICODE_ENV ||
0974         type == USB_EXT_PROP_UNICODE_LINK))
0975         ext_prop->data_len <<= 1;
0976     else if ((ext_prop->type == USB_EXT_PROP_UNICODE ||
0977            ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
0978            ext_prop->type == USB_EXT_PROP_UNICODE_LINK) &&
0979            (type == USB_EXT_PROP_BINARY ||
0980            type == USB_EXT_PROP_LE32 ||
0981            type == USB_EXT_PROP_BE32))
0982         ext_prop->data_len >>= 1;
0983     ext_prop->type = type;
0984     ret = len;
0985 
0986 end:
0987     if (desc->opts_mutex)
0988         mutex_unlock(desc->opts_mutex);
0989     return ret;
0990 }
0991 
0992 static ssize_t ext_prop_data_show(struct config_item *item, char *page)
0993 {
0994     struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
0995     int len = ext_prop->data_len;
0996 
0997     if (ext_prop->type == USB_EXT_PROP_UNICODE ||
0998         ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
0999         ext_prop->type == USB_EXT_PROP_UNICODE_LINK)
1000         len >>= 1;
1001     memcpy(page, ext_prop->data, len);
1002 
1003     return len;
1004 }
1005 
1006 static ssize_t ext_prop_data_store(struct config_item *item,
1007                    const char *page, size_t len)
1008 {
1009     struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
1010     struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent);
1011     char *new_data;
1012     size_t ret_len = len;
1013 
1014     if (page[len - 1] == '\n' || page[len - 1] == '\0')
1015         --len;
1016     new_data = kmemdup(page, len, GFP_KERNEL);
1017     if (!new_data)
1018         return -ENOMEM;
1019 
1020     if (desc->opts_mutex)
1021         mutex_lock(desc->opts_mutex);
1022     kfree(ext_prop->data);
1023     ext_prop->data = new_data;
1024     desc->ext_prop_len -= ext_prop->data_len;
1025     ext_prop->data_len = len;
1026     desc->ext_prop_len += ext_prop->data_len;
1027     if (ext_prop->type == USB_EXT_PROP_UNICODE ||
1028         ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
1029         ext_prop->type == USB_EXT_PROP_UNICODE_LINK) {
1030         desc->ext_prop_len -= ext_prop->data_len;
1031         ext_prop->data_len <<= 1;
1032         ext_prop->data_len += 2;
1033         desc->ext_prop_len += ext_prop->data_len;
1034     }
1035     if (desc->opts_mutex)
1036         mutex_unlock(desc->opts_mutex);
1037     return ret_len;
1038 }
1039 
1040 CONFIGFS_ATTR(ext_prop_, type);
1041 CONFIGFS_ATTR(ext_prop_, data);
1042 
1043 static struct configfs_attribute *ext_prop_attrs[] = {
1044     &ext_prop_attr_type,
1045     &ext_prop_attr_data,
1046     NULL,
1047 };
1048 
1049 static void usb_os_desc_ext_prop_release(struct config_item *item)
1050 {
1051     struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
1052 
1053     kfree(ext_prop); /* frees a whole chunk */
1054 }
1055 
1056 static struct configfs_item_operations ext_prop_ops = {
1057     .release        = usb_os_desc_ext_prop_release,
1058 };
1059 
1060 static struct config_item *ext_prop_make(
1061         struct config_group *group,
1062         const char *name)
1063 {
1064     struct usb_os_desc_ext_prop *ext_prop;
1065     struct config_item_type *ext_prop_type;
1066     struct usb_os_desc *desc;
1067     char *vlabuf;
1068 
1069     vla_group(data_chunk);
1070     vla_item(data_chunk, struct usb_os_desc_ext_prop, ext_prop, 1);
1071     vla_item(data_chunk, struct config_item_type, ext_prop_type, 1);
1072 
1073     vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
1074     if (!vlabuf)
1075         return ERR_PTR(-ENOMEM);
1076 
1077     ext_prop = vla_ptr(vlabuf, data_chunk, ext_prop);
1078     ext_prop_type = vla_ptr(vlabuf, data_chunk, ext_prop_type);
1079 
1080     desc = container_of(group, struct usb_os_desc, group);
1081     ext_prop_type->ct_item_ops = &ext_prop_ops;
1082     ext_prop_type->ct_attrs = ext_prop_attrs;
1083     ext_prop_type->ct_owner = desc->owner;
1084 
1085     config_item_init_type_name(&ext_prop->item, name, ext_prop_type);
1086 
1087     ext_prop->name = kstrdup(name, GFP_KERNEL);
1088     if (!ext_prop->name) {
1089         kfree(vlabuf);
1090         return ERR_PTR(-ENOMEM);
1091     }
1092     desc->ext_prop_len += 14;
1093     ext_prop->name_len = 2 * strlen(ext_prop->name) + 2;
1094     if (desc->opts_mutex)
1095         mutex_lock(desc->opts_mutex);
1096     desc->ext_prop_len += ext_prop->name_len;
1097     list_add_tail(&ext_prop->entry, &desc->ext_prop);
1098     ++desc->ext_prop_count;
1099     if (desc->opts_mutex)
1100         mutex_unlock(desc->opts_mutex);
1101 
1102     return &ext_prop->item;
1103 }
1104 
1105 static void ext_prop_drop(struct config_group *group, struct config_item *item)
1106 {
1107     struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
1108     struct usb_os_desc *desc = to_usb_os_desc(&group->cg_item);
1109 
1110     if (desc->opts_mutex)
1111         mutex_lock(desc->opts_mutex);
1112     list_del(&ext_prop->entry);
1113     --desc->ext_prop_count;
1114     kfree(ext_prop->name);
1115     desc->ext_prop_len -= (ext_prop->name_len + ext_prop->data_len + 14);
1116     if (desc->opts_mutex)
1117         mutex_unlock(desc->opts_mutex);
1118     config_item_put(item);
1119 }
1120 
1121 static struct configfs_group_operations interf_grp_ops = {
1122     .make_item  = &ext_prop_make,
1123     .drop_item  = &ext_prop_drop,
1124 };
1125 
1126 static ssize_t interf_grp_compatible_id_show(struct config_item *item,
1127                          char *page)
1128 {
1129     memcpy(page, to_usb_os_desc(item)->ext_compat_id, 8);
1130     return 8;
1131 }
1132 
1133 static ssize_t interf_grp_compatible_id_store(struct config_item *item,
1134                           const char *page, size_t len)
1135 {
1136     struct usb_os_desc *desc = to_usb_os_desc(item);
1137     int l;
1138 
1139     l = min_t(int, 8, len);
1140     if (page[l - 1] == '\n')
1141         --l;
1142     if (desc->opts_mutex)
1143         mutex_lock(desc->opts_mutex);
1144     memcpy(desc->ext_compat_id, page, l);
1145 
1146     if (desc->opts_mutex)
1147         mutex_unlock(desc->opts_mutex);
1148 
1149     return len;
1150 }
1151 
1152 static ssize_t interf_grp_sub_compatible_id_show(struct config_item *item,
1153                          char *page)
1154 {
1155     memcpy(page, to_usb_os_desc(item)->ext_compat_id + 8, 8);
1156     return 8;
1157 }
1158 
1159 static ssize_t interf_grp_sub_compatible_id_store(struct config_item *item,
1160                           const char *page, size_t len)
1161 {
1162     struct usb_os_desc *desc = to_usb_os_desc(item);
1163     int l;
1164 
1165     l = min_t(int, 8, len);
1166     if (page[l - 1] == '\n')
1167         --l;
1168     if (desc->opts_mutex)
1169         mutex_lock(desc->opts_mutex);
1170     memcpy(desc->ext_compat_id + 8, page, l);
1171 
1172     if (desc->opts_mutex)
1173         mutex_unlock(desc->opts_mutex);
1174 
1175     return len;
1176 }
1177 
1178 CONFIGFS_ATTR(interf_grp_, compatible_id);
1179 CONFIGFS_ATTR(interf_grp_, sub_compatible_id);
1180 
1181 static struct configfs_attribute *interf_grp_attrs[] = {
1182     &interf_grp_attr_compatible_id,
1183     &interf_grp_attr_sub_compatible_id,
1184     NULL
1185 };
1186 
1187 struct config_group *usb_os_desc_prepare_interf_dir(
1188         struct config_group *parent,
1189         int n_interf,
1190         struct usb_os_desc **desc,
1191         char **names,
1192         struct module *owner)
1193 {
1194     struct config_group *os_desc_group;
1195     struct config_item_type *os_desc_type, *interface_type;
1196 
1197     vla_group(data_chunk);
1198     vla_item(data_chunk, struct config_group, os_desc_group, 1);
1199     vla_item(data_chunk, struct config_item_type, os_desc_type, 1);
1200     vla_item(data_chunk, struct config_item_type, interface_type, 1);
1201 
1202     char *vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
1203     if (!vlabuf)
1204         return ERR_PTR(-ENOMEM);
1205 
1206     os_desc_group = vla_ptr(vlabuf, data_chunk, os_desc_group);
1207     os_desc_type = vla_ptr(vlabuf, data_chunk, os_desc_type);
1208     interface_type = vla_ptr(vlabuf, data_chunk, interface_type);
1209 
1210     os_desc_type->ct_owner = owner;
1211     config_group_init_type_name(os_desc_group, "os_desc", os_desc_type);
1212     configfs_add_default_group(os_desc_group, parent);
1213 
1214     interface_type->ct_group_ops = &interf_grp_ops;
1215     interface_type->ct_attrs = interf_grp_attrs;
1216     interface_type->ct_owner = owner;
1217 
1218     while (n_interf--) {
1219         struct usb_os_desc *d;
1220 
1221         d = desc[n_interf];
1222         d->owner = owner;
1223         config_group_init_type_name(&d->group, "", interface_type);
1224         config_item_set_name(&d->group.cg_item, "interface.%s",
1225                      names[n_interf]);
1226         configfs_add_default_group(&d->group, os_desc_group);
1227     }
1228 
1229     return os_desc_group;
1230 }
1231 EXPORT_SYMBOL(usb_os_desc_prepare_interf_dir);
1232 
1233 static int configfs_do_nothing(struct usb_composite_dev *cdev)
1234 {
1235     WARN_ON(1);
1236     return -EINVAL;
1237 }
1238 
1239 int composite_dev_prepare(struct usb_composite_driver *composite,
1240         struct usb_composite_dev *dev);
1241 
1242 int composite_os_desc_req_prepare(struct usb_composite_dev *cdev,
1243                   struct usb_ep *ep0);
1244 
1245 static void purge_configs_funcs(struct gadget_info *gi)
1246 {
1247     struct usb_configuration    *c;
1248 
1249     list_for_each_entry(c, &gi->cdev.configs, list) {
1250         struct usb_function *f, *tmp;
1251         struct config_usb_cfg *cfg;
1252 
1253         cfg = container_of(c, struct config_usb_cfg, c);
1254 
1255         list_for_each_entry_safe_reverse(f, tmp, &c->functions, list) {
1256 
1257             list_move(&f->list, &cfg->func_list);
1258             if (f->unbind) {
1259                 dev_dbg(&gi->cdev.gadget->dev,
1260                     "unbind function '%s'/%p\n",
1261                     f->name, f);
1262                 f->unbind(c, f);
1263             }
1264         }
1265         c->next_interface_id = 0;
1266         memset(c->interface, 0, sizeof(c->interface));
1267         c->superspeed_plus = 0;
1268         c->superspeed = 0;
1269         c->highspeed = 0;
1270         c->fullspeed = 0;
1271     }
1272 }
1273 
1274 static int configfs_composite_bind(struct usb_gadget *gadget,
1275         struct usb_gadget_driver *gdriver)
1276 {
1277     struct usb_composite_driver     *composite = to_cdriver(gdriver);
1278     struct gadget_info      *gi = container_of(composite,
1279                         struct gadget_info, composite);
1280     struct usb_composite_dev    *cdev = &gi->cdev;
1281     struct usb_configuration    *c;
1282     struct usb_string       *s;
1283     unsigned            i;
1284     int             ret;
1285 
1286     /* the gi->lock is hold by the caller */
1287     gi->unbind = 0;
1288     cdev->gadget = gadget;
1289     set_gadget_data(gadget, cdev);
1290     ret = composite_dev_prepare(composite, cdev);
1291     if (ret)
1292         return ret;
1293     /* and now the gadget bind */
1294     ret = -EINVAL;
1295 
1296     if (list_empty(&gi->cdev.configs)) {
1297         pr_err("Need at least one configuration in %s.\n",
1298                 gi->composite.name);
1299         goto err_comp_cleanup;
1300     }
1301 
1302 
1303     list_for_each_entry(c, &gi->cdev.configs, list) {
1304         struct config_usb_cfg *cfg;
1305 
1306         cfg = container_of(c, struct config_usb_cfg, c);
1307         if (list_empty(&cfg->func_list)) {
1308             pr_err("Config %s/%d of %s needs at least one function.\n",
1309                   c->label, c->bConfigurationValue,
1310                   gi->composite.name);
1311             goto err_comp_cleanup;
1312         }
1313     }
1314 
1315     /* init all strings */
1316     if (!list_empty(&gi->string_list)) {
1317         struct gadget_strings *gs;
1318 
1319         i = 0;
1320         list_for_each_entry(gs, &gi->string_list, list) {
1321 
1322             gi->gstrings[i] = &gs->stringtab_dev;
1323             gs->stringtab_dev.strings = gs->strings;
1324             gs->strings[USB_GADGET_MANUFACTURER_IDX].s =
1325                 gs->manufacturer;
1326             gs->strings[USB_GADGET_PRODUCT_IDX].s = gs->product;
1327             gs->strings[USB_GADGET_SERIAL_IDX].s = gs->serialnumber;
1328             i++;
1329         }
1330         gi->gstrings[i] = NULL;
1331         s = usb_gstrings_attach(&gi->cdev, gi->gstrings,
1332                 USB_GADGET_FIRST_AVAIL_IDX);
1333         if (IS_ERR(s)) {
1334             ret = PTR_ERR(s);
1335             goto err_comp_cleanup;
1336         }
1337 
1338         gi->cdev.desc.iManufacturer = s[USB_GADGET_MANUFACTURER_IDX].id;
1339         gi->cdev.desc.iProduct = s[USB_GADGET_PRODUCT_IDX].id;
1340         gi->cdev.desc.iSerialNumber = s[USB_GADGET_SERIAL_IDX].id;
1341     }
1342 
1343     if (gi->use_os_desc) {
1344         cdev->use_os_string = true;
1345         cdev->b_vendor_code = gi->b_vendor_code;
1346         memcpy(cdev->qw_sign, gi->qw_sign, OS_STRING_QW_SIGN_LEN);
1347     }
1348 
1349     if (gadget_is_otg(gadget) && !otg_desc[0]) {
1350         struct usb_descriptor_header *usb_desc;
1351 
1352         usb_desc = usb_otg_descriptor_alloc(gadget);
1353         if (!usb_desc) {
1354             ret = -ENOMEM;
1355             goto err_comp_cleanup;
1356         }
1357         usb_otg_descriptor_init(gadget, usb_desc);
1358         otg_desc[0] = usb_desc;
1359         otg_desc[1] = NULL;
1360     }
1361 
1362     /* Go through all configs, attach all functions */
1363     list_for_each_entry(c, &gi->cdev.configs, list) {
1364         struct config_usb_cfg *cfg;
1365         struct usb_function *f;
1366         struct usb_function *tmp;
1367         struct gadget_config_name *cn;
1368 
1369         if (gadget_is_otg(gadget))
1370             c->descriptors = otg_desc;
1371 
1372         cfg = container_of(c, struct config_usb_cfg, c);
1373         if (!list_empty(&cfg->string_list)) {
1374             i = 0;
1375             list_for_each_entry(cn, &cfg->string_list, list) {
1376                 cfg->gstrings[i] = &cn->stringtab_dev;
1377                 cn->stringtab_dev.strings = &cn->strings;
1378                 cn->strings.s = cn->configuration;
1379                 i++;
1380             }
1381             cfg->gstrings[i] = NULL;
1382             s = usb_gstrings_attach(&gi->cdev, cfg->gstrings, 1);
1383             if (IS_ERR(s)) {
1384                 ret = PTR_ERR(s);
1385                 goto err_comp_cleanup;
1386             }
1387             c->iConfiguration = s[0].id;
1388         }
1389 
1390         list_for_each_entry_safe(f, tmp, &cfg->func_list, list) {
1391             list_del(&f->list);
1392             ret = usb_add_function(c, f);
1393             if (ret) {
1394                 list_add(&f->list, &cfg->func_list);
1395                 goto err_purge_funcs;
1396             }
1397         }
1398         ret = usb_gadget_check_config(cdev->gadget);
1399         if (ret)
1400             goto err_purge_funcs;
1401 
1402         usb_ep_autoconfig_reset(cdev->gadget);
1403     }
1404     if (cdev->use_os_string) {
1405         ret = composite_os_desc_req_prepare(cdev, gadget->ep0);
1406         if (ret)
1407             goto err_purge_funcs;
1408     }
1409 
1410     usb_ep_autoconfig_reset(cdev->gadget);
1411     return 0;
1412 
1413 err_purge_funcs:
1414     purge_configs_funcs(gi);
1415 err_comp_cleanup:
1416     composite_dev_cleanup(cdev);
1417     return ret;
1418 }
1419 
1420 static void configfs_composite_unbind(struct usb_gadget *gadget)
1421 {
1422     struct usb_composite_dev    *cdev;
1423     struct gadget_info      *gi;
1424     unsigned long flags;
1425 
1426     /* the gi->lock is hold by the caller */
1427 
1428     cdev = get_gadget_data(gadget);
1429     gi = container_of(cdev, struct gadget_info, cdev);
1430     spin_lock_irqsave(&gi->spinlock, flags);
1431     gi->unbind = 1;
1432     spin_unlock_irqrestore(&gi->spinlock, flags);
1433 
1434     kfree(otg_desc[0]);
1435     otg_desc[0] = NULL;
1436     purge_configs_funcs(gi);
1437     composite_dev_cleanup(cdev);
1438     usb_ep_autoconfig_reset(cdev->gadget);
1439     spin_lock_irqsave(&gi->spinlock, flags);
1440     cdev->gadget = NULL;
1441     cdev->deactivations = 0;
1442     gadget->deactivated = false;
1443     set_gadget_data(gadget, NULL);
1444     spin_unlock_irqrestore(&gi->spinlock, flags);
1445 }
1446 
1447 static int configfs_composite_setup(struct usb_gadget *gadget,
1448         const struct usb_ctrlrequest *ctrl)
1449 {
1450     struct usb_composite_dev *cdev;
1451     struct gadget_info *gi;
1452     unsigned long flags;
1453     int ret;
1454 
1455     cdev = get_gadget_data(gadget);
1456     if (!cdev)
1457         return 0;
1458 
1459     gi = container_of(cdev, struct gadget_info, cdev);
1460     spin_lock_irqsave(&gi->spinlock, flags);
1461     cdev = get_gadget_data(gadget);
1462     if (!cdev || gi->unbind) {
1463         spin_unlock_irqrestore(&gi->spinlock, flags);
1464         return 0;
1465     }
1466 
1467     ret = composite_setup(gadget, ctrl);
1468     spin_unlock_irqrestore(&gi->spinlock, flags);
1469     return ret;
1470 }
1471 
1472 static void configfs_composite_disconnect(struct usb_gadget *gadget)
1473 {
1474     struct usb_composite_dev *cdev;
1475     struct gadget_info *gi;
1476     unsigned long flags;
1477 
1478     cdev = get_gadget_data(gadget);
1479     if (!cdev)
1480         return;
1481 
1482     gi = container_of(cdev, struct gadget_info, cdev);
1483     spin_lock_irqsave(&gi->spinlock, flags);
1484     cdev = get_gadget_data(gadget);
1485     if (!cdev || gi->unbind) {
1486         spin_unlock_irqrestore(&gi->spinlock, flags);
1487         return;
1488     }
1489 
1490     composite_disconnect(gadget);
1491     spin_unlock_irqrestore(&gi->spinlock, flags);
1492 }
1493 
1494 static void configfs_composite_reset(struct usb_gadget *gadget)
1495 {
1496     struct usb_composite_dev *cdev;
1497     struct gadget_info *gi;
1498     unsigned long flags;
1499 
1500     cdev = get_gadget_data(gadget);
1501     if (!cdev)
1502         return;
1503 
1504     gi = container_of(cdev, struct gadget_info, cdev);
1505     spin_lock_irqsave(&gi->spinlock, flags);
1506     cdev = get_gadget_data(gadget);
1507     if (!cdev || gi->unbind) {
1508         spin_unlock_irqrestore(&gi->spinlock, flags);
1509         return;
1510     }
1511 
1512     composite_reset(gadget);
1513     spin_unlock_irqrestore(&gi->spinlock, flags);
1514 }
1515 
1516 static void configfs_composite_suspend(struct usb_gadget *gadget)
1517 {
1518     struct usb_composite_dev *cdev;
1519     struct gadget_info *gi;
1520     unsigned long flags;
1521 
1522     cdev = get_gadget_data(gadget);
1523     if (!cdev)
1524         return;
1525 
1526     gi = container_of(cdev, struct gadget_info, cdev);
1527     spin_lock_irqsave(&gi->spinlock, flags);
1528     cdev = get_gadget_data(gadget);
1529     if (!cdev || gi->unbind) {
1530         spin_unlock_irqrestore(&gi->spinlock, flags);
1531         return;
1532     }
1533 
1534     composite_suspend(gadget);
1535     spin_unlock_irqrestore(&gi->spinlock, flags);
1536 }
1537 
1538 static void configfs_composite_resume(struct usb_gadget *gadget)
1539 {
1540     struct usb_composite_dev *cdev;
1541     struct gadget_info *gi;
1542     unsigned long flags;
1543 
1544     cdev = get_gadget_data(gadget);
1545     if (!cdev)
1546         return;
1547 
1548     gi = container_of(cdev, struct gadget_info, cdev);
1549     spin_lock_irqsave(&gi->spinlock, flags);
1550     cdev = get_gadget_data(gadget);
1551     if (!cdev || gi->unbind) {
1552         spin_unlock_irqrestore(&gi->spinlock, flags);
1553         return;
1554     }
1555 
1556     composite_resume(gadget);
1557     spin_unlock_irqrestore(&gi->spinlock, flags);
1558 }
1559 
1560 static const struct usb_gadget_driver configfs_driver_template = {
1561     .bind           = configfs_composite_bind,
1562     .unbind         = configfs_composite_unbind,
1563 
1564     .setup          = configfs_composite_setup,
1565     .reset          = configfs_composite_reset,
1566     .disconnect     = configfs_composite_disconnect,
1567 
1568     .suspend    = configfs_composite_suspend,
1569     .resume     = configfs_composite_resume,
1570 
1571     .max_speed  = USB_SPEED_SUPER_PLUS,
1572     .driver = {
1573         .owner          = THIS_MODULE,
1574         .name       = "configfs-gadget",
1575     },
1576     .match_existing_only = 1,
1577 };
1578 
1579 static struct config_group *gadgets_make(
1580         struct config_group *group,
1581         const char *name)
1582 {
1583     struct gadget_info *gi;
1584 
1585     gi = kzalloc(sizeof(*gi), GFP_KERNEL);
1586     if (!gi)
1587         return ERR_PTR(-ENOMEM);
1588 
1589     config_group_init_type_name(&gi->group, name, &gadget_root_type);
1590 
1591     config_group_init_type_name(&gi->functions_group, "functions",
1592             &functions_type);
1593     configfs_add_default_group(&gi->functions_group, &gi->group);
1594 
1595     config_group_init_type_name(&gi->configs_group, "configs",
1596             &config_desc_type);
1597     configfs_add_default_group(&gi->configs_group, &gi->group);
1598 
1599     config_group_init_type_name(&gi->strings_group, "strings",
1600             &gadget_strings_strings_type);
1601     configfs_add_default_group(&gi->strings_group, &gi->group);
1602 
1603     config_group_init_type_name(&gi->os_desc_group, "os_desc",
1604             &os_desc_type);
1605     configfs_add_default_group(&gi->os_desc_group, &gi->group);
1606 
1607     gi->composite.bind = configfs_do_nothing;
1608     gi->composite.unbind = configfs_do_nothing;
1609     gi->composite.suspend = NULL;
1610     gi->composite.resume = NULL;
1611     gi->composite.max_speed = USB_SPEED_SUPER_PLUS;
1612 
1613     spin_lock_init(&gi->spinlock);
1614     mutex_init(&gi->lock);
1615     INIT_LIST_HEAD(&gi->string_list);
1616     INIT_LIST_HEAD(&gi->available_func);
1617 
1618     composite_init_dev(&gi->cdev);
1619     gi->cdev.desc.bLength = USB_DT_DEVICE_SIZE;
1620     gi->cdev.desc.bDescriptorType = USB_DT_DEVICE;
1621     gi->cdev.desc.bcdDevice = cpu_to_le16(get_default_bcdDevice());
1622 
1623     gi->composite.gadget_driver = configfs_driver_template;
1624 
1625     gi->composite.gadget_driver.function = kstrdup(name, GFP_KERNEL);
1626     gi->composite.name = gi->composite.gadget_driver.function;
1627 
1628     if (!gi->composite.gadget_driver.function)
1629         goto err;
1630 
1631     return &gi->group;
1632 err:
1633     kfree(gi);
1634     return ERR_PTR(-ENOMEM);
1635 }
1636 
1637 static void gadgets_drop(struct config_group *group, struct config_item *item)
1638 {
1639     config_item_put(item);
1640 }
1641 
1642 static struct configfs_group_operations gadgets_ops = {
1643     .make_group     = &gadgets_make,
1644     .drop_item      = &gadgets_drop,
1645 };
1646 
1647 static const struct config_item_type gadgets_type = {
1648     .ct_group_ops   = &gadgets_ops,
1649     .ct_owner       = THIS_MODULE,
1650 };
1651 
1652 static struct configfs_subsystem gadget_subsys = {
1653     .su_group = {
1654         .cg_item = {
1655             .ci_namebuf = "usb_gadget",
1656             .ci_type = &gadgets_type,
1657         },
1658     },
1659     .su_mutex = __MUTEX_INITIALIZER(gadget_subsys.su_mutex),
1660 };
1661 
1662 void unregister_gadget_item(struct config_item *item)
1663 {
1664     struct gadget_info *gi = to_gadget_info(item);
1665 
1666     mutex_lock(&gi->lock);
1667     unregister_gadget(gi);
1668     mutex_unlock(&gi->lock);
1669 }
1670 EXPORT_SYMBOL_GPL(unregister_gadget_item);
1671 
1672 static int __init gadget_cfs_init(void)
1673 {
1674     int ret;
1675 
1676     config_group_init(&gadget_subsys.su_group);
1677 
1678     ret = configfs_register_subsystem(&gadget_subsys);
1679     return ret;
1680 }
1681 module_init(gadget_cfs_init);
1682 
1683 static void __exit gadget_cfs_exit(void)
1684 {
1685     configfs_unregister_subsystem(&gadget_subsys);
1686 }
1687 module_exit(gadget_cfs_exit);