15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 288af8bbeSSebastian Andrzej Siewior #include <linux/configfs.h> 388af8bbeSSebastian Andrzej Siewior #include <linux/module.h> 488af8bbeSSebastian Andrzej Siewior #include <linux/slab.h> 588af8bbeSSebastian Andrzej Siewior #include <linux/device.h> 687213d38SAndrzej Pietrasiewicz #include <linux/nls.h> 788af8bbeSSebastian Andrzej Siewior #include <linux/usb/composite.h> 888af8bbeSSebastian Andrzej Siewior #include <linux/usb/gadget_configfs.h> 90009e99aSRashika Kheria #include "configfs.h" 10da424314SAndrzej Pietrasiewicz #include "u_f.h" 117419485fSAndrzej Pietrasiewicz #include "u_os_desc.h" 1288af8bbeSSebastian Andrzej Siewior 1388af8bbeSSebastian Andrzej Siewior int check_user_usb_string(const char *name, 1488af8bbeSSebastian Andrzej Siewior struct usb_gadget_strings *stringtab_dev) 1588af8bbeSSebastian Andrzej Siewior { 1688af8bbeSSebastian Andrzej Siewior unsigned primary_lang; 1788af8bbeSSebastian Andrzej Siewior unsigned sub_lang; 1888af8bbeSSebastian Andrzej Siewior u16 num; 1988af8bbeSSebastian Andrzej Siewior int ret; 2088af8bbeSSebastian Andrzej Siewior 2188af8bbeSSebastian Andrzej Siewior ret = kstrtou16(name, 0, &num); 2288af8bbeSSebastian Andrzej Siewior if (ret) 2388af8bbeSSebastian Andrzej Siewior return ret; 2488af8bbeSSebastian Andrzej Siewior 2588af8bbeSSebastian Andrzej Siewior primary_lang = num & 0x3ff; 2688af8bbeSSebastian Andrzej Siewior sub_lang = num >> 10; 2788af8bbeSSebastian Andrzej Siewior 2888af8bbeSSebastian Andrzej Siewior /* simple sanity check for valid langid */ 2988af8bbeSSebastian Andrzej Siewior switch (primary_lang) { 3088af8bbeSSebastian Andrzej Siewior case 0: 3188af8bbeSSebastian Andrzej Siewior case 0x62 ... 0xfe: 3288af8bbeSSebastian Andrzej Siewior case 0x100 ... 0x3ff: 3388af8bbeSSebastian Andrzej Siewior return -EINVAL; 3488af8bbeSSebastian Andrzej Siewior } 3588af8bbeSSebastian Andrzej Siewior if (!sub_lang) 3688af8bbeSSebastian Andrzej Siewior return -EINVAL; 3788af8bbeSSebastian Andrzej Siewior 3888af8bbeSSebastian Andrzej Siewior stringtab_dev->language = num; 3988af8bbeSSebastian Andrzej Siewior return 0; 4088af8bbeSSebastian Andrzej Siewior } 4188af8bbeSSebastian Andrzej Siewior 4288af8bbeSSebastian Andrzej Siewior #define MAX_NAME_LEN 40 4388af8bbeSSebastian Andrzej Siewior #define MAX_USB_STRING_LANGS 2 4488af8bbeSSebastian Andrzej Siewior 4541ce84c8SLi Jun static const struct usb_descriptor_header *otg_desc[2]; 4641ce84c8SLi Jun 4788af8bbeSSebastian Andrzej Siewior struct gadget_info { 4888af8bbeSSebastian Andrzej Siewior struct config_group group; 4988af8bbeSSebastian Andrzej Siewior struct config_group functions_group; 5088af8bbeSSebastian Andrzej Siewior struct config_group configs_group; 5188af8bbeSSebastian Andrzej Siewior struct config_group strings_group; 5287213d38SAndrzej Pietrasiewicz struct config_group os_desc_group; 5388af8bbeSSebastian Andrzej Siewior 5488af8bbeSSebastian Andrzej Siewior struct mutex lock; 5588af8bbeSSebastian Andrzej Siewior struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1]; 5688af8bbeSSebastian Andrzej Siewior struct list_head string_list; 5788af8bbeSSebastian Andrzej Siewior struct list_head available_func; 5888af8bbeSSebastian Andrzej Siewior 5988af8bbeSSebastian Andrzej Siewior struct usb_composite_driver composite; 6088af8bbeSSebastian Andrzej Siewior struct usb_composite_dev cdev; 6187213d38SAndrzej Pietrasiewicz bool use_os_desc; 6287213d38SAndrzej Pietrasiewicz char b_vendor_code; 6387213d38SAndrzej Pietrasiewicz char qw_sign[OS_STRING_QW_SIGN_LEN]; 641a1c851bSPeter Chen spinlock_t spinlock; 651a1c851bSPeter Chen bool unbind; 6688af8bbeSSebastian Andrzej Siewior }; 6788af8bbeSSebastian Andrzej Siewior 6845b6a73fSChristoph Hellwig static inline struct gadget_info *to_gadget_info(struct config_item *item) 6945b6a73fSChristoph Hellwig { 7045b6a73fSChristoph Hellwig return container_of(to_config_group(item), struct gadget_info, group); 7145b6a73fSChristoph Hellwig } 7245b6a73fSChristoph Hellwig 7388af8bbeSSebastian Andrzej Siewior struct config_usb_cfg { 7488af8bbeSSebastian Andrzej Siewior struct config_group group; 7588af8bbeSSebastian Andrzej Siewior struct config_group strings_group; 7688af8bbeSSebastian Andrzej Siewior struct list_head string_list; 7788af8bbeSSebastian Andrzej Siewior struct usb_configuration c; 7888af8bbeSSebastian Andrzej Siewior struct list_head func_list; 7988af8bbeSSebastian Andrzej Siewior struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1]; 8088af8bbeSSebastian Andrzej Siewior }; 8188af8bbeSSebastian Andrzej Siewior 8245b6a73fSChristoph Hellwig static inline struct config_usb_cfg *to_config_usb_cfg(struct config_item *item) 8345b6a73fSChristoph Hellwig { 8445b6a73fSChristoph Hellwig return container_of(to_config_group(item), struct config_usb_cfg, 8545b6a73fSChristoph Hellwig group); 8645b6a73fSChristoph Hellwig } 8745b6a73fSChristoph Hellwig 8888af8bbeSSebastian Andrzej Siewior struct gadget_strings { 8988af8bbeSSebastian Andrzej Siewior struct usb_gadget_strings stringtab_dev; 9088af8bbeSSebastian Andrzej Siewior struct usb_string strings[USB_GADGET_FIRST_AVAIL_IDX]; 9188af8bbeSSebastian Andrzej Siewior char *manufacturer; 9288af8bbeSSebastian Andrzej Siewior char *product; 9388af8bbeSSebastian Andrzej Siewior char *serialnumber; 9488af8bbeSSebastian Andrzej Siewior 9588af8bbeSSebastian Andrzej Siewior struct config_group group; 9688af8bbeSSebastian Andrzej Siewior struct list_head list; 9788af8bbeSSebastian Andrzej Siewior }; 9888af8bbeSSebastian Andrzej Siewior 9987213d38SAndrzej Pietrasiewicz struct os_desc { 10087213d38SAndrzej Pietrasiewicz struct config_group group; 10187213d38SAndrzej Pietrasiewicz }; 10287213d38SAndrzej Pietrasiewicz 10388af8bbeSSebastian Andrzej Siewior struct gadget_config_name { 10488af8bbeSSebastian Andrzej Siewior struct usb_gadget_strings stringtab_dev; 10588af8bbeSSebastian Andrzej Siewior struct usb_string strings; 10688af8bbeSSebastian Andrzej Siewior char *configuration; 10788af8bbeSSebastian Andrzej Siewior 10888af8bbeSSebastian Andrzej Siewior struct config_group group; 10988af8bbeSSebastian Andrzej Siewior struct list_head list; 11088af8bbeSSebastian Andrzej Siewior }; 11188af8bbeSSebastian Andrzej Siewior 11288af8bbeSSebastian Andrzej Siewior static int usb_string_copy(const char *s, char **s_copy) 11388af8bbeSSebastian Andrzej Siewior { 11488af8bbeSSebastian Andrzej Siewior int ret; 11588af8bbeSSebastian Andrzej Siewior char *str; 11688af8bbeSSebastian Andrzej Siewior char *copy = *s_copy; 11788af8bbeSSebastian Andrzej Siewior ret = strlen(s); 11888af8bbeSSebastian Andrzej Siewior if (ret > 126) 11988af8bbeSSebastian Andrzej Siewior return -EOVERFLOW; 12088af8bbeSSebastian Andrzej Siewior 12188af8bbeSSebastian Andrzej Siewior str = kstrdup(s, GFP_KERNEL); 12288af8bbeSSebastian Andrzej Siewior if (!str) 12388af8bbeSSebastian Andrzej Siewior return -ENOMEM; 12488af8bbeSSebastian Andrzej Siewior if (str[ret - 1] == '\n') 12588af8bbeSSebastian Andrzej Siewior str[ret - 1] = '\0'; 12688af8bbeSSebastian Andrzej Siewior kfree(copy); 12788af8bbeSSebastian Andrzej Siewior *s_copy = str; 12888af8bbeSSebastian Andrzej Siewior return 0; 12988af8bbeSSebastian Andrzej Siewior } 13088af8bbeSSebastian Andrzej Siewior 13188af8bbeSSebastian Andrzej Siewior #define GI_DEVICE_DESC_SIMPLE_R_u8(__name) \ 13245b6a73fSChristoph Hellwig static ssize_t gadget_dev_desc_##__name##_show(struct config_item *item, \ 13388af8bbeSSebastian Andrzej Siewior char *page) \ 13488af8bbeSSebastian Andrzej Siewior { \ 13545b6a73fSChristoph Hellwig return sprintf(page, "0x%02x\n", \ 13645b6a73fSChristoph Hellwig to_gadget_info(item)->cdev.desc.__name); \ 13788af8bbeSSebastian Andrzej Siewior } 13888af8bbeSSebastian Andrzej Siewior 13988af8bbeSSebastian Andrzej Siewior #define GI_DEVICE_DESC_SIMPLE_R_u16(__name) \ 14045b6a73fSChristoph Hellwig static ssize_t gadget_dev_desc_##__name##_show(struct config_item *item, \ 14188af8bbeSSebastian Andrzej Siewior char *page) \ 14288af8bbeSSebastian Andrzej Siewior { \ 14345b6a73fSChristoph Hellwig return sprintf(page, "0x%04x\n", \ 14445b6a73fSChristoph Hellwig le16_to_cpup(&to_gadget_info(item)->cdev.desc.__name)); \ 14588af8bbeSSebastian Andrzej Siewior } 14688af8bbeSSebastian Andrzej Siewior 14788af8bbeSSebastian Andrzej Siewior 14888af8bbeSSebastian Andrzej Siewior #define GI_DEVICE_DESC_SIMPLE_W_u8(_name) \ 14945b6a73fSChristoph Hellwig static ssize_t gadget_dev_desc_##_name##_store(struct config_item *item, \ 15088af8bbeSSebastian Andrzej Siewior const char *page, size_t len) \ 15188af8bbeSSebastian Andrzej Siewior { \ 15288af8bbeSSebastian Andrzej Siewior u8 val; \ 15388af8bbeSSebastian Andrzej Siewior int ret; \ 15488af8bbeSSebastian Andrzej Siewior ret = kstrtou8(page, 0, &val); \ 15588af8bbeSSebastian Andrzej Siewior if (ret) \ 15688af8bbeSSebastian Andrzej Siewior return ret; \ 15745b6a73fSChristoph Hellwig to_gadget_info(item)->cdev.desc._name = val; \ 15888af8bbeSSebastian Andrzej Siewior return len; \ 15988af8bbeSSebastian Andrzej Siewior } 16088af8bbeSSebastian Andrzej Siewior 16188af8bbeSSebastian Andrzej Siewior #define GI_DEVICE_DESC_SIMPLE_W_u16(_name) \ 16245b6a73fSChristoph Hellwig static ssize_t gadget_dev_desc_##_name##_store(struct config_item *item, \ 16388af8bbeSSebastian Andrzej Siewior const char *page, size_t len) \ 16488af8bbeSSebastian Andrzej Siewior { \ 16588af8bbeSSebastian Andrzej Siewior u16 val; \ 16688af8bbeSSebastian Andrzej Siewior int ret; \ 16788af8bbeSSebastian Andrzej Siewior ret = kstrtou16(page, 0, &val); \ 16888af8bbeSSebastian Andrzej Siewior if (ret) \ 16988af8bbeSSebastian Andrzej Siewior return ret; \ 17045b6a73fSChristoph Hellwig to_gadget_info(item)->cdev.desc._name = cpu_to_le16p(&val); \ 17188af8bbeSSebastian Andrzej Siewior return len; \ 17288af8bbeSSebastian Andrzej Siewior } 17388af8bbeSSebastian Andrzej Siewior 17488af8bbeSSebastian Andrzej Siewior #define GI_DEVICE_DESC_SIMPLE_RW(_name, _type) \ 17588af8bbeSSebastian Andrzej Siewior GI_DEVICE_DESC_SIMPLE_R_##_type(_name) \ 17688af8bbeSSebastian Andrzej Siewior GI_DEVICE_DESC_SIMPLE_W_##_type(_name) 17788af8bbeSSebastian Andrzej Siewior 17888af8bbeSSebastian Andrzej Siewior GI_DEVICE_DESC_SIMPLE_R_u16(bcdUSB); 17988af8bbeSSebastian Andrzej Siewior GI_DEVICE_DESC_SIMPLE_RW(bDeviceClass, u8); 18088af8bbeSSebastian Andrzej Siewior GI_DEVICE_DESC_SIMPLE_RW(bDeviceSubClass, u8); 18188af8bbeSSebastian Andrzej Siewior GI_DEVICE_DESC_SIMPLE_RW(bDeviceProtocol, u8); 18288af8bbeSSebastian Andrzej Siewior GI_DEVICE_DESC_SIMPLE_RW(bMaxPacketSize0, u8); 18388af8bbeSSebastian Andrzej Siewior GI_DEVICE_DESC_SIMPLE_RW(idVendor, u16); 18488af8bbeSSebastian Andrzej Siewior GI_DEVICE_DESC_SIMPLE_RW(idProduct, u16); 18588af8bbeSSebastian Andrzej Siewior GI_DEVICE_DESC_SIMPLE_R_u16(bcdDevice); 18688af8bbeSSebastian Andrzej Siewior 18788af8bbeSSebastian Andrzej Siewior static ssize_t is_valid_bcd(u16 bcd_val) 18888af8bbeSSebastian Andrzej Siewior { 18988af8bbeSSebastian Andrzej Siewior if ((bcd_val & 0xf) > 9) 19088af8bbeSSebastian Andrzej Siewior return -EINVAL; 19188af8bbeSSebastian Andrzej Siewior if (((bcd_val >> 4) & 0xf) > 9) 19288af8bbeSSebastian Andrzej Siewior return -EINVAL; 19388af8bbeSSebastian Andrzej Siewior if (((bcd_val >> 8) & 0xf) > 9) 19488af8bbeSSebastian Andrzej Siewior return -EINVAL; 19588af8bbeSSebastian Andrzej Siewior if (((bcd_val >> 12) & 0xf) > 9) 19688af8bbeSSebastian Andrzej Siewior return -EINVAL; 19788af8bbeSSebastian Andrzej Siewior return 0; 19888af8bbeSSebastian Andrzej Siewior } 19988af8bbeSSebastian Andrzej Siewior 20045b6a73fSChristoph Hellwig static ssize_t gadget_dev_desc_bcdDevice_store(struct config_item *item, 20188af8bbeSSebastian Andrzej Siewior const char *page, size_t len) 20288af8bbeSSebastian Andrzej Siewior { 20388af8bbeSSebastian Andrzej Siewior u16 bcdDevice; 20488af8bbeSSebastian Andrzej Siewior int ret; 20588af8bbeSSebastian Andrzej Siewior 20688af8bbeSSebastian Andrzej Siewior ret = kstrtou16(page, 0, &bcdDevice); 20788af8bbeSSebastian Andrzej Siewior if (ret) 20888af8bbeSSebastian Andrzej Siewior return ret; 20988af8bbeSSebastian Andrzej Siewior ret = is_valid_bcd(bcdDevice); 21088af8bbeSSebastian Andrzej Siewior if (ret) 21188af8bbeSSebastian Andrzej Siewior return ret; 21288af8bbeSSebastian Andrzej Siewior 21345b6a73fSChristoph Hellwig to_gadget_info(item)->cdev.desc.bcdDevice = cpu_to_le16(bcdDevice); 21488af8bbeSSebastian Andrzej Siewior return len; 21588af8bbeSSebastian Andrzej Siewior } 21688af8bbeSSebastian Andrzej Siewior 21745b6a73fSChristoph Hellwig static ssize_t gadget_dev_desc_bcdUSB_store(struct config_item *item, 21888af8bbeSSebastian Andrzej Siewior const char *page, size_t len) 21988af8bbeSSebastian Andrzej Siewior { 22088af8bbeSSebastian Andrzej Siewior u16 bcdUSB; 22188af8bbeSSebastian Andrzej Siewior int ret; 22288af8bbeSSebastian Andrzej Siewior 22388af8bbeSSebastian Andrzej Siewior ret = kstrtou16(page, 0, &bcdUSB); 22488af8bbeSSebastian Andrzej Siewior if (ret) 22588af8bbeSSebastian Andrzej Siewior return ret; 22688af8bbeSSebastian Andrzej Siewior ret = is_valid_bcd(bcdUSB); 22788af8bbeSSebastian Andrzej Siewior if (ret) 22888af8bbeSSebastian Andrzej Siewior return ret; 22988af8bbeSSebastian Andrzej Siewior 23045b6a73fSChristoph Hellwig to_gadget_info(item)->cdev.desc.bcdUSB = cpu_to_le16(bcdUSB); 23188af8bbeSSebastian Andrzej Siewior return len; 23288af8bbeSSebastian Andrzej Siewior } 23388af8bbeSSebastian Andrzej Siewior 23445b6a73fSChristoph Hellwig static ssize_t gadget_dev_desc_UDC_show(struct config_item *item, char *page) 23588af8bbeSSebastian Andrzej Siewior { 236afdaadc3SRuslan Bilovol char *udc_name = to_gadget_info(item)->composite.gadget_driver.udc_name; 237afdaadc3SRuslan Bilovol 238afdaadc3SRuslan Bilovol return sprintf(page, "%s\n", udc_name ?: ""); 23988af8bbeSSebastian Andrzej Siewior } 24088af8bbeSSebastian Andrzej Siewior 24188af8bbeSSebastian Andrzej Siewior static int unregister_gadget(struct gadget_info *gi) 24288af8bbeSSebastian Andrzej Siewior { 24388af8bbeSSebastian Andrzej Siewior int ret; 24488af8bbeSSebastian Andrzej Siewior 245afdaadc3SRuslan Bilovol if (!gi->composite.gadget_driver.udc_name) 24688af8bbeSSebastian Andrzej Siewior return -ENODEV; 24788af8bbeSSebastian Andrzej Siewior 24888af8bbeSSebastian Andrzej Siewior ret = usb_gadget_unregister_driver(&gi->composite.gadget_driver); 24988af8bbeSSebastian Andrzej Siewior if (ret) 25088af8bbeSSebastian Andrzej Siewior return ret; 251afdaadc3SRuslan Bilovol kfree(gi->composite.gadget_driver.udc_name); 252afdaadc3SRuslan Bilovol gi->composite.gadget_driver.udc_name = NULL; 25388af8bbeSSebastian Andrzej Siewior return 0; 25488af8bbeSSebastian Andrzej Siewior } 25588af8bbeSSebastian Andrzej Siewior 25645b6a73fSChristoph Hellwig static ssize_t gadget_dev_desc_UDC_store(struct config_item *item, 25788af8bbeSSebastian Andrzej Siewior const char *page, size_t len) 25888af8bbeSSebastian Andrzej Siewior { 25945b6a73fSChristoph Hellwig struct gadget_info *gi = to_gadget_info(item); 26088af8bbeSSebastian Andrzej Siewior char *name; 26188af8bbeSSebastian Andrzej Siewior int ret; 26288af8bbeSSebastian Andrzej Siewior 263*15753588SKyungtae Kim if (strlen(page) < len) 264*15753588SKyungtae Kim return -EOVERFLOW; 265*15753588SKyungtae Kim 26688af8bbeSSebastian Andrzej Siewior name = kstrdup(page, GFP_KERNEL); 26788af8bbeSSebastian Andrzej Siewior if (!name) 26888af8bbeSSebastian Andrzej Siewior return -ENOMEM; 26988af8bbeSSebastian Andrzej Siewior if (name[len - 1] == '\n') 27088af8bbeSSebastian Andrzej Siewior name[len - 1] = '\0'; 27188af8bbeSSebastian Andrzej Siewior 27288af8bbeSSebastian Andrzej Siewior mutex_lock(&gi->lock); 27388af8bbeSSebastian Andrzej Siewior 27488af8bbeSSebastian Andrzej Siewior if (!strlen(name)) { 27588af8bbeSSebastian Andrzej Siewior ret = unregister_gadget(gi); 27688af8bbeSSebastian Andrzej Siewior if (ret) 27788af8bbeSSebastian Andrzej Siewior goto err; 27838355b2aSJohn Keeping kfree(name); 27988af8bbeSSebastian Andrzej Siewior } else { 280afdaadc3SRuslan Bilovol if (gi->composite.gadget_driver.udc_name) { 28188af8bbeSSebastian Andrzej Siewior ret = -EBUSY; 28288af8bbeSSebastian Andrzej Siewior goto err; 28388af8bbeSSebastian Andrzej Siewior } 284afdaadc3SRuslan Bilovol gi->composite.gadget_driver.udc_name = name; 285afdaadc3SRuslan Bilovol ret = usb_gadget_probe_driver(&gi->composite.gadget_driver); 286afdaadc3SRuslan Bilovol if (ret) { 287afdaadc3SRuslan Bilovol gi->composite.gadget_driver.udc_name = NULL; 28888af8bbeSSebastian Andrzej Siewior goto err; 289afdaadc3SRuslan Bilovol } 29088af8bbeSSebastian Andrzej Siewior } 29188af8bbeSSebastian Andrzej Siewior mutex_unlock(&gi->lock); 29288af8bbeSSebastian Andrzej Siewior return len; 29388af8bbeSSebastian Andrzej Siewior err: 29488af8bbeSSebastian Andrzej Siewior kfree(name); 29588af8bbeSSebastian Andrzej Siewior mutex_unlock(&gi->lock); 29688af8bbeSSebastian Andrzej Siewior return ret; 29788af8bbeSSebastian Andrzej Siewior } 29888af8bbeSSebastian Andrzej Siewior 299a0249703SThinh Nguyen static ssize_t gadget_dev_desc_max_speed_show(struct config_item *item, 300a0249703SThinh Nguyen char *page) 301a0249703SThinh Nguyen { 302a0249703SThinh Nguyen enum usb_device_speed speed = to_gadget_info(item)->composite.max_speed; 303a0249703SThinh Nguyen 304a0249703SThinh Nguyen return sprintf(page, "%s\n", usb_speed_string(speed)); 305a0249703SThinh Nguyen } 306a0249703SThinh Nguyen 307a0249703SThinh Nguyen static ssize_t gadget_dev_desc_max_speed_store(struct config_item *item, 308a0249703SThinh Nguyen const char *page, size_t len) 309a0249703SThinh Nguyen { 310a0249703SThinh Nguyen struct gadget_info *gi = to_gadget_info(item); 311a0249703SThinh Nguyen 312a0249703SThinh Nguyen mutex_lock(&gi->lock); 313a0249703SThinh Nguyen 314a0249703SThinh Nguyen /* Prevent changing of max_speed after the driver is binded */ 315a0249703SThinh Nguyen if (gi->composite.gadget_driver.udc_name) 316a0249703SThinh Nguyen goto err; 317a0249703SThinh Nguyen 318a0249703SThinh Nguyen if (strncmp(page, "super-speed-plus", 16) == 0) 319a0249703SThinh Nguyen gi->composite.max_speed = USB_SPEED_SUPER_PLUS; 320a0249703SThinh Nguyen else if (strncmp(page, "super-speed", 11) == 0) 321a0249703SThinh Nguyen gi->composite.max_speed = USB_SPEED_SUPER; 322a0249703SThinh Nguyen else if (strncmp(page, "high-speed", 10) == 0) 323a0249703SThinh Nguyen gi->composite.max_speed = USB_SPEED_HIGH; 324a0249703SThinh Nguyen else if (strncmp(page, "full-speed", 10) == 0) 325a0249703SThinh Nguyen gi->composite.max_speed = USB_SPEED_FULL; 326a0249703SThinh Nguyen else if (strncmp(page, "low-speed", 9) == 0) 327a0249703SThinh Nguyen gi->composite.max_speed = USB_SPEED_LOW; 328a0249703SThinh Nguyen else 329a0249703SThinh Nguyen goto err; 330a0249703SThinh Nguyen 331a0249703SThinh Nguyen gi->composite.gadget_driver.max_speed = gi->composite.max_speed; 332a0249703SThinh Nguyen 333a0249703SThinh Nguyen mutex_unlock(&gi->lock); 334a0249703SThinh Nguyen return len; 335a0249703SThinh Nguyen err: 336a0249703SThinh Nguyen mutex_unlock(&gi->lock); 337a0249703SThinh Nguyen return -EINVAL; 338a0249703SThinh Nguyen } 339a0249703SThinh Nguyen 34045b6a73fSChristoph Hellwig CONFIGFS_ATTR(gadget_dev_desc_, bDeviceClass); 34145b6a73fSChristoph Hellwig CONFIGFS_ATTR(gadget_dev_desc_, bDeviceSubClass); 34245b6a73fSChristoph Hellwig CONFIGFS_ATTR(gadget_dev_desc_, bDeviceProtocol); 34345b6a73fSChristoph Hellwig CONFIGFS_ATTR(gadget_dev_desc_, bMaxPacketSize0); 34445b6a73fSChristoph Hellwig CONFIGFS_ATTR(gadget_dev_desc_, idVendor); 34545b6a73fSChristoph Hellwig CONFIGFS_ATTR(gadget_dev_desc_, idProduct); 34645b6a73fSChristoph Hellwig CONFIGFS_ATTR(gadget_dev_desc_, bcdDevice); 34745b6a73fSChristoph Hellwig CONFIGFS_ATTR(gadget_dev_desc_, bcdUSB); 34845b6a73fSChristoph Hellwig CONFIGFS_ATTR(gadget_dev_desc_, UDC); 349a0249703SThinh Nguyen CONFIGFS_ATTR(gadget_dev_desc_, max_speed); 35088af8bbeSSebastian Andrzej Siewior 35188af8bbeSSebastian Andrzej Siewior static struct configfs_attribute *gadget_root_attrs[] = { 35245b6a73fSChristoph Hellwig &gadget_dev_desc_attr_bDeviceClass, 35345b6a73fSChristoph Hellwig &gadget_dev_desc_attr_bDeviceSubClass, 35445b6a73fSChristoph Hellwig &gadget_dev_desc_attr_bDeviceProtocol, 35545b6a73fSChristoph Hellwig &gadget_dev_desc_attr_bMaxPacketSize0, 35645b6a73fSChristoph Hellwig &gadget_dev_desc_attr_idVendor, 35745b6a73fSChristoph Hellwig &gadget_dev_desc_attr_idProduct, 35845b6a73fSChristoph Hellwig &gadget_dev_desc_attr_bcdDevice, 35945b6a73fSChristoph Hellwig &gadget_dev_desc_attr_bcdUSB, 36045b6a73fSChristoph Hellwig &gadget_dev_desc_attr_UDC, 361a0249703SThinh Nguyen &gadget_dev_desc_attr_max_speed, 36288af8bbeSSebastian Andrzej Siewior NULL, 36388af8bbeSSebastian Andrzej Siewior }; 36488af8bbeSSebastian Andrzej Siewior 36588af8bbeSSebastian Andrzej Siewior static inline struct gadget_strings *to_gadget_strings(struct config_item *item) 36688af8bbeSSebastian Andrzej Siewior { 36788af8bbeSSebastian Andrzej Siewior return container_of(to_config_group(item), struct gadget_strings, 36888af8bbeSSebastian Andrzej Siewior group); 36988af8bbeSSebastian Andrzej Siewior } 37088af8bbeSSebastian Andrzej Siewior 37188af8bbeSSebastian Andrzej Siewior static inline struct gadget_config_name *to_gadget_config_name( 37288af8bbeSSebastian Andrzej Siewior struct config_item *item) 37388af8bbeSSebastian Andrzej Siewior { 37488af8bbeSSebastian Andrzej Siewior return container_of(to_config_group(item), struct gadget_config_name, 37588af8bbeSSebastian Andrzej Siewior group); 37688af8bbeSSebastian Andrzej Siewior } 37788af8bbeSSebastian Andrzej Siewior 37888af8bbeSSebastian Andrzej Siewior static inline struct usb_function_instance *to_usb_function_instance( 37988af8bbeSSebastian Andrzej Siewior struct config_item *item) 38088af8bbeSSebastian Andrzej Siewior { 38188af8bbeSSebastian Andrzej Siewior return container_of(to_config_group(item), 38288af8bbeSSebastian Andrzej Siewior struct usb_function_instance, group); 38388af8bbeSSebastian Andrzej Siewior } 38488af8bbeSSebastian Andrzej Siewior 38588af8bbeSSebastian Andrzej Siewior static void gadget_info_attr_release(struct config_item *item) 38688af8bbeSSebastian Andrzej Siewior { 38788af8bbeSSebastian Andrzej Siewior struct gadget_info *gi = to_gadget_info(item); 38888af8bbeSSebastian Andrzej Siewior 38988af8bbeSSebastian Andrzej Siewior WARN_ON(!list_empty(&gi->cdev.configs)); 39088af8bbeSSebastian Andrzej Siewior WARN_ON(!list_empty(&gi->string_list)); 39188af8bbeSSebastian Andrzej Siewior WARN_ON(!list_empty(&gi->available_func)); 39288af8bbeSSebastian Andrzej Siewior kfree(gi->composite.gadget_driver.function); 39388af8bbeSSebastian Andrzej Siewior kfree(gi); 39488af8bbeSSebastian Andrzej Siewior } 39588af8bbeSSebastian Andrzej Siewior 39688af8bbeSSebastian Andrzej Siewior static struct configfs_item_operations gadget_root_item_ops = { 39788af8bbeSSebastian Andrzej Siewior .release = gadget_info_attr_release, 39888af8bbeSSebastian Andrzej Siewior }; 39988af8bbeSSebastian Andrzej Siewior 40088af8bbeSSebastian Andrzej Siewior static void gadget_config_attr_release(struct config_item *item) 40188af8bbeSSebastian Andrzej Siewior { 40288af8bbeSSebastian Andrzej Siewior struct config_usb_cfg *cfg = to_config_usb_cfg(item); 40388af8bbeSSebastian Andrzej Siewior 40488af8bbeSSebastian Andrzej Siewior WARN_ON(!list_empty(&cfg->c.functions)); 40588af8bbeSSebastian Andrzej Siewior list_del(&cfg->c.list); 40688af8bbeSSebastian Andrzej Siewior kfree(cfg->c.label); 40788af8bbeSSebastian Andrzej Siewior kfree(cfg); 40888af8bbeSSebastian Andrzej Siewior } 40988af8bbeSSebastian Andrzej Siewior 41088af8bbeSSebastian Andrzej Siewior static int config_usb_cfg_link( 41188af8bbeSSebastian Andrzej Siewior struct config_item *usb_cfg_ci, 41288af8bbeSSebastian Andrzej Siewior struct config_item *usb_func_ci) 41388af8bbeSSebastian Andrzej Siewior { 41488af8bbeSSebastian Andrzej Siewior struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci); 41588af8bbeSSebastian Andrzej Siewior struct usb_composite_dev *cdev = cfg->c.cdev; 41688af8bbeSSebastian Andrzej Siewior struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev); 41788af8bbeSSebastian Andrzej Siewior 41888af8bbeSSebastian Andrzej Siewior struct config_group *group = to_config_group(usb_func_ci); 41988af8bbeSSebastian Andrzej Siewior struct usb_function_instance *fi = container_of(group, 42088af8bbeSSebastian Andrzej Siewior struct usb_function_instance, group); 42188af8bbeSSebastian Andrzej Siewior struct usb_function_instance *a_fi; 42288af8bbeSSebastian Andrzej Siewior struct usb_function *f; 42388af8bbeSSebastian Andrzej Siewior int ret; 42488af8bbeSSebastian Andrzej Siewior 42588af8bbeSSebastian Andrzej Siewior mutex_lock(&gi->lock); 42688af8bbeSSebastian Andrzej Siewior /* 42788af8bbeSSebastian Andrzej Siewior * Make sure this function is from within our _this_ gadget and not 42888af8bbeSSebastian Andrzej Siewior * from another gadget or a random directory. 42988af8bbeSSebastian Andrzej Siewior * Also a function instance can only be linked once. 43088af8bbeSSebastian Andrzej Siewior */ 43188af8bbeSSebastian Andrzej Siewior list_for_each_entry(a_fi, &gi->available_func, cfs_list) { 43288af8bbeSSebastian Andrzej Siewior if (a_fi == fi) 43388af8bbeSSebastian Andrzej Siewior break; 43488af8bbeSSebastian Andrzej Siewior } 43588af8bbeSSebastian Andrzej Siewior if (a_fi != fi) { 43688af8bbeSSebastian Andrzej Siewior ret = -EINVAL; 43788af8bbeSSebastian Andrzej Siewior goto out; 43888af8bbeSSebastian Andrzej Siewior } 43988af8bbeSSebastian Andrzej Siewior 44088af8bbeSSebastian Andrzej Siewior list_for_each_entry(f, &cfg->func_list, list) { 44188af8bbeSSebastian Andrzej Siewior if (f->fi == fi) { 44288af8bbeSSebastian Andrzej Siewior ret = -EEXIST; 44388af8bbeSSebastian Andrzej Siewior goto out; 44488af8bbeSSebastian Andrzej Siewior } 44588af8bbeSSebastian Andrzej Siewior } 44688af8bbeSSebastian Andrzej Siewior 44788af8bbeSSebastian Andrzej Siewior f = usb_get_function(fi); 44888af8bbeSSebastian Andrzej Siewior if (IS_ERR(f)) { 44988af8bbeSSebastian Andrzej Siewior ret = PTR_ERR(f); 45088af8bbeSSebastian Andrzej Siewior goto out; 45188af8bbeSSebastian Andrzej Siewior } 45288af8bbeSSebastian Andrzej Siewior 45388af8bbeSSebastian Andrzej Siewior /* stash the function until we bind it to the gadget */ 45488af8bbeSSebastian Andrzej Siewior list_add_tail(&f->list, &cfg->func_list); 45588af8bbeSSebastian Andrzej Siewior ret = 0; 45688af8bbeSSebastian Andrzej Siewior out: 45788af8bbeSSebastian Andrzej Siewior mutex_unlock(&gi->lock); 45888af8bbeSSebastian Andrzej Siewior return ret; 45988af8bbeSSebastian Andrzej Siewior } 46088af8bbeSSebastian Andrzej Siewior 461e16769d4SAndrzej Pietrasiewicz static void config_usb_cfg_unlink( 46288af8bbeSSebastian Andrzej Siewior struct config_item *usb_cfg_ci, 46388af8bbeSSebastian Andrzej Siewior struct config_item *usb_func_ci) 46488af8bbeSSebastian Andrzej Siewior { 46588af8bbeSSebastian Andrzej Siewior struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci); 46688af8bbeSSebastian Andrzej Siewior struct usb_composite_dev *cdev = cfg->c.cdev; 46788af8bbeSSebastian Andrzej Siewior struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev); 46888af8bbeSSebastian Andrzej Siewior 46988af8bbeSSebastian Andrzej Siewior struct config_group *group = to_config_group(usb_func_ci); 47088af8bbeSSebastian Andrzej Siewior struct usb_function_instance *fi = container_of(group, 47188af8bbeSSebastian Andrzej Siewior struct usb_function_instance, group); 47288af8bbeSSebastian Andrzej Siewior struct usb_function *f; 47388af8bbeSSebastian Andrzej Siewior 47488af8bbeSSebastian Andrzej Siewior /* 47588af8bbeSSebastian Andrzej Siewior * ideally I would like to forbid to unlink functions while a gadget is 47688af8bbeSSebastian Andrzej Siewior * bound to an UDC. Since this isn't possible at the moment, we simply 47788af8bbeSSebastian Andrzej Siewior * force an unbind, the function is available here and then we can 47888af8bbeSSebastian Andrzej Siewior * remove the function. 47988af8bbeSSebastian Andrzej Siewior */ 48088af8bbeSSebastian Andrzej Siewior mutex_lock(&gi->lock); 481afdaadc3SRuslan Bilovol if (gi->composite.gadget_driver.udc_name) 48288af8bbeSSebastian Andrzej Siewior unregister_gadget(gi); 483afdaadc3SRuslan Bilovol WARN_ON(gi->composite.gadget_driver.udc_name); 48488af8bbeSSebastian Andrzej Siewior 48588af8bbeSSebastian Andrzej Siewior list_for_each_entry(f, &cfg->func_list, list) { 48688af8bbeSSebastian Andrzej Siewior if (f->fi == fi) { 48788af8bbeSSebastian Andrzej Siewior list_del(&f->list); 48888af8bbeSSebastian Andrzej Siewior usb_put_function(f); 48988af8bbeSSebastian Andrzej Siewior mutex_unlock(&gi->lock); 490e16769d4SAndrzej Pietrasiewicz return; 49188af8bbeSSebastian Andrzej Siewior } 49288af8bbeSSebastian Andrzej Siewior } 49388af8bbeSSebastian Andrzej Siewior mutex_unlock(&gi->lock); 49475bfe23aSDavid Rientjes WARN(1, "Unable to locate function to unbind\n"); 49588af8bbeSSebastian Andrzej Siewior } 49688af8bbeSSebastian Andrzej Siewior 49788af8bbeSSebastian Andrzej Siewior static struct configfs_item_operations gadget_config_item_ops = { 49888af8bbeSSebastian Andrzej Siewior .release = gadget_config_attr_release, 49988af8bbeSSebastian Andrzej Siewior .allow_link = config_usb_cfg_link, 50088af8bbeSSebastian Andrzej Siewior .drop_link = config_usb_cfg_unlink, 50188af8bbeSSebastian Andrzej Siewior }; 50288af8bbeSSebastian Andrzej Siewior 50388af8bbeSSebastian Andrzej Siewior 50445b6a73fSChristoph Hellwig static ssize_t gadget_config_desc_MaxPower_show(struct config_item *item, 50588af8bbeSSebastian Andrzej Siewior char *page) 50688af8bbeSSebastian Andrzej Siewior { 50745b6a73fSChristoph Hellwig return sprintf(page, "%u\n", to_config_usb_cfg(item)->c.MaxPower); 50888af8bbeSSebastian Andrzej Siewior } 50988af8bbeSSebastian Andrzej Siewior 51045b6a73fSChristoph Hellwig static ssize_t gadget_config_desc_MaxPower_store(struct config_item *item, 51188af8bbeSSebastian Andrzej Siewior const char *page, size_t len) 51288af8bbeSSebastian Andrzej Siewior { 51388af8bbeSSebastian Andrzej Siewior u16 val; 51488af8bbeSSebastian Andrzej Siewior int ret; 51588af8bbeSSebastian Andrzej Siewior ret = kstrtou16(page, 0, &val); 51688af8bbeSSebastian Andrzej Siewior if (ret) 51788af8bbeSSebastian Andrzej Siewior return ret; 51888af8bbeSSebastian Andrzej Siewior if (DIV_ROUND_UP(val, 8) > 0xff) 51988af8bbeSSebastian Andrzej Siewior return -ERANGE; 52045b6a73fSChristoph Hellwig to_config_usb_cfg(item)->c.MaxPower = val; 52188af8bbeSSebastian Andrzej Siewior return len; 52288af8bbeSSebastian Andrzej Siewior } 52388af8bbeSSebastian Andrzej Siewior 52445b6a73fSChristoph Hellwig static ssize_t gadget_config_desc_bmAttributes_show(struct config_item *item, 52588af8bbeSSebastian Andrzej Siewior char *page) 52688af8bbeSSebastian Andrzej Siewior { 52745b6a73fSChristoph Hellwig return sprintf(page, "0x%02x\n", 52845b6a73fSChristoph Hellwig to_config_usb_cfg(item)->c.bmAttributes); 52988af8bbeSSebastian Andrzej Siewior } 53088af8bbeSSebastian Andrzej Siewior 53145b6a73fSChristoph Hellwig static ssize_t gadget_config_desc_bmAttributes_store(struct config_item *item, 53288af8bbeSSebastian Andrzej Siewior const char *page, size_t len) 53388af8bbeSSebastian Andrzej Siewior { 53488af8bbeSSebastian Andrzej Siewior u8 val; 53588af8bbeSSebastian Andrzej Siewior int ret; 53688af8bbeSSebastian Andrzej Siewior ret = kstrtou8(page, 0, &val); 53788af8bbeSSebastian Andrzej Siewior if (ret) 53888af8bbeSSebastian Andrzej Siewior return ret; 53988af8bbeSSebastian Andrzej Siewior if (!(val & USB_CONFIG_ATT_ONE)) 54088af8bbeSSebastian Andrzej Siewior return -EINVAL; 54188af8bbeSSebastian Andrzej Siewior if (val & ~(USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER | 54288af8bbeSSebastian Andrzej Siewior USB_CONFIG_ATT_WAKEUP)) 54388af8bbeSSebastian Andrzej Siewior return -EINVAL; 54445b6a73fSChristoph Hellwig to_config_usb_cfg(item)->c.bmAttributes = val; 54588af8bbeSSebastian Andrzej Siewior return len; 54688af8bbeSSebastian Andrzej Siewior } 54788af8bbeSSebastian Andrzej Siewior 54845b6a73fSChristoph Hellwig CONFIGFS_ATTR(gadget_config_desc_, MaxPower); 54945b6a73fSChristoph Hellwig CONFIGFS_ATTR(gadget_config_desc_, bmAttributes); 55088af8bbeSSebastian Andrzej Siewior 55188af8bbeSSebastian Andrzej Siewior static struct configfs_attribute *gadget_config_attrs[] = { 55245b6a73fSChristoph Hellwig &gadget_config_desc_attr_MaxPower, 55345b6a73fSChristoph Hellwig &gadget_config_desc_attr_bmAttributes, 55488af8bbeSSebastian Andrzej Siewior NULL, 55588af8bbeSSebastian Andrzej Siewior }; 55688af8bbeSSebastian Andrzej Siewior 5574ad01412SBhumika Goyal static const struct config_item_type gadget_config_type = { 55888af8bbeSSebastian Andrzej Siewior .ct_item_ops = &gadget_config_item_ops, 55988af8bbeSSebastian Andrzej Siewior .ct_attrs = gadget_config_attrs, 56088af8bbeSSebastian Andrzej Siewior .ct_owner = THIS_MODULE, 56188af8bbeSSebastian Andrzej Siewior }; 56288af8bbeSSebastian Andrzej Siewior 5634ad01412SBhumika Goyal static const struct config_item_type gadget_root_type = { 56488af8bbeSSebastian Andrzej Siewior .ct_item_ops = &gadget_root_item_ops, 56588af8bbeSSebastian Andrzej Siewior .ct_attrs = gadget_root_attrs, 56688af8bbeSSebastian Andrzej Siewior .ct_owner = THIS_MODULE, 56788af8bbeSSebastian Andrzej Siewior }; 56888af8bbeSSebastian Andrzej Siewior 56988af8bbeSSebastian Andrzej Siewior static void composite_init_dev(struct usb_composite_dev *cdev) 57088af8bbeSSebastian Andrzej Siewior { 57188af8bbeSSebastian Andrzej Siewior spin_lock_init(&cdev->lock); 57288af8bbeSSebastian Andrzej Siewior INIT_LIST_HEAD(&cdev->configs); 57388af8bbeSSebastian Andrzej Siewior INIT_LIST_HEAD(&cdev->gstrings); 57488af8bbeSSebastian Andrzej Siewior } 57588af8bbeSSebastian Andrzej Siewior 57688af8bbeSSebastian Andrzej Siewior static struct config_group *function_make( 57788af8bbeSSebastian Andrzej Siewior struct config_group *group, 57888af8bbeSSebastian Andrzej Siewior const char *name) 57988af8bbeSSebastian Andrzej Siewior { 58088af8bbeSSebastian Andrzej Siewior struct gadget_info *gi; 58188af8bbeSSebastian Andrzej Siewior struct usb_function_instance *fi; 58288af8bbeSSebastian Andrzej Siewior char buf[MAX_NAME_LEN]; 58388af8bbeSSebastian Andrzej Siewior char *func_name; 58488af8bbeSSebastian Andrzej Siewior char *instance_name; 58588af8bbeSSebastian Andrzej Siewior int ret; 58688af8bbeSSebastian Andrzej Siewior 58788af8bbeSSebastian Andrzej Siewior ret = snprintf(buf, MAX_NAME_LEN, "%s", name); 58888af8bbeSSebastian Andrzej Siewior if (ret >= MAX_NAME_LEN) 58988af8bbeSSebastian Andrzej Siewior return ERR_PTR(-ENAMETOOLONG); 59088af8bbeSSebastian Andrzej Siewior 59188af8bbeSSebastian Andrzej Siewior func_name = buf; 59288af8bbeSSebastian Andrzej Siewior instance_name = strchr(func_name, '.'); 59388af8bbeSSebastian Andrzej Siewior if (!instance_name) { 59488af8bbeSSebastian Andrzej Siewior pr_err("Unable to locate . in FUNC.INSTANCE\n"); 59588af8bbeSSebastian Andrzej Siewior return ERR_PTR(-EINVAL); 59688af8bbeSSebastian Andrzej Siewior } 59788af8bbeSSebastian Andrzej Siewior *instance_name = '\0'; 59888af8bbeSSebastian Andrzej Siewior instance_name++; 59988af8bbeSSebastian Andrzej Siewior 60088af8bbeSSebastian Andrzej Siewior fi = usb_get_function_instance(func_name); 60188af8bbeSSebastian Andrzej Siewior if (IS_ERR(fi)) 602a3469411SDuan Jiong return ERR_CAST(fi); 60388af8bbeSSebastian Andrzej Siewior 6043958b792SNicolas Iooss ret = config_item_set_name(&fi->group.cg_item, "%s", name); 60588af8bbeSSebastian Andrzej Siewior if (ret) { 60688af8bbeSSebastian Andrzej Siewior usb_put_function_instance(fi); 60788af8bbeSSebastian Andrzej Siewior return ERR_PTR(ret); 60888af8bbeSSebastian Andrzej Siewior } 6091933861dSAndrzej Pietrasiewicz if (fi->set_inst_name) { 6101933861dSAndrzej Pietrasiewicz ret = fi->set_inst_name(fi, instance_name); 6111933861dSAndrzej Pietrasiewicz if (ret) { 6121933861dSAndrzej Pietrasiewicz usb_put_function_instance(fi); 6131933861dSAndrzej Pietrasiewicz return ERR_PTR(ret); 6141933861dSAndrzej Pietrasiewicz } 6151933861dSAndrzej Pietrasiewicz } 61688af8bbeSSebastian Andrzej Siewior 61788af8bbeSSebastian Andrzej Siewior gi = container_of(group, struct gadget_info, functions_group); 61888af8bbeSSebastian Andrzej Siewior 61988af8bbeSSebastian Andrzej Siewior mutex_lock(&gi->lock); 62088af8bbeSSebastian Andrzej Siewior list_add_tail(&fi->cfs_list, &gi->available_func); 62188af8bbeSSebastian Andrzej Siewior mutex_unlock(&gi->lock); 62288af8bbeSSebastian Andrzej Siewior return &fi->group; 62388af8bbeSSebastian Andrzej Siewior } 62488af8bbeSSebastian Andrzej Siewior 62588af8bbeSSebastian Andrzej Siewior static void function_drop( 62688af8bbeSSebastian Andrzej Siewior struct config_group *group, 62788af8bbeSSebastian Andrzej Siewior struct config_item *item) 62888af8bbeSSebastian Andrzej Siewior { 62988af8bbeSSebastian Andrzej Siewior struct usb_function_instance *fi = to_usb_function_instance(item); 63088af8bbeSSebastian Andrzej Siewior struct gadget_info *gi; 63188af8bbeSSebastian Andrzej Siewior 63288af8bbeSSebastian Andrzej Siewior gi = container_of(group, struct gadget_info, functions_group); 63388af8bbeSSebastian Andrzej Siewior 63488af8bbeSSebastian Andrzej Siewior mutex_lock(&gi->lock); 63588af8bbeSSebastian Andrzej Siewior list_del(&fi->cfs_list); 63688af8bbeSSebastian Andrzej Siewior mutex_unlock(&gi->lock); 63788af8bbeSSebastian Andrzej Siewior config_item_put(item); 63888af8bbeSSebastian Andrzej Siewior } 63988af8bbeSSebastian Andrzej Siewior 64088af8bbeSSebastian Andrzej Siewior static struct configfs_group_operations functions_ops = { 64188af8bbeSSebastian Andrzej Siewior .make_group = &function_make, 64288af8bbeSSebastian Andrzej Siewior .drop_item = &function_drop, 64388af8bbeSSebastian Andrzej Siewior }; 64488af8bbeSSebastian Andrzej Siewior 6454ad01412SBhumika Goyal static const struct config_item_type functions_type = { 64688af8bbeSSebastian Andrzej Siewior .ct_group_ops = &functions_ops, 64788af8bbeSSebastian Andrzej Siewior .ct_owner = THIS_MODULE, 64888af8bbeSSebastian Andrzej Siewior }; 64988af8bbeSSebastian Andrzej Siewior 65088af8bbeSSebastian Andrzej Siewior GS_STRINGS_RW(gadget_config_name, configuration); 65188af8bbeSSebastian Andrzej Siewior 65288af8bbeSSebastian Andrzej Siewior static struct configfs_attribute *gadget_config_name_langid_attrs[] = { 65345b6a73fSChristoph Hellwig &gadget_config_name_attr_configuration, 65488af8bbeSSebastian Andrzej Siewior NULL, 65588af8bbeSSebastian Andrzej Siewior }; 65688af8bbeSSebastian Andrzej Siewior 65788af8bbeSSebastian Andrzej Siewior static void gadget_config_name_attr_release(struct config_item *item) 65888af8bbeSSebastian Andrzej Siewior { 65988af8bbeSSebastian Andrzej Siewior struct gadget_config_name *cn = to_gadget_config_name(item); 66088af8bbeSSebastian Andrzej Siewior 66188af8bbeSSebastian Andrzej Siewior kfree(cn->configuration); 66288af8bbeSSebastian Andrzej Siewior 66388af8bbeSSebastian Andrzej Siewior list_del(&cn->list); 66488af8bbeSSebastian Andrzej Siewior kfree(cn); 66588af8bbeSSebastian Andrzej Siewior } 66688af8bbeSSebastian Andrzej Siewior 66788af8bbeSSebastian Andrzej Siewior USB_CONFIG_STRING_RW_OPS(gadget_config_name); 66888af8bbeSSebastian Andrzej Siewior USB_CONFIG_STRINGS_LANG(gadget_config_name, config_usb_cfg); 66988af8bbeSSebastian Andrzej Siewior 67088af8bbeSSebastian Andrzej Siewior static struct config_group *config_desc_make( 67188af8bbeSSebastian Andrzej Siewior struct config_group *group, 67288af8bbeSSebastian Andrzej Siewior const char *name) 67388af8bbeSSebastian Andrzej Siewior { 67488af8bbeSSebastian Andrzej Siewior struct gadget_info *gi; 67588af8bbeSSebastian Andrzej Siewior struct config_usb_cfg *cfg; 67688af8bbeSSebastian Andrzej Siewior char buf[MAX_NAME_LEN]; 67788af8bbeSSebastian Andrzej Siewior char *num_str; 67888af8bbeSSebastian Andrzej Siewior u8 num; 67988af8bbeSSebastian Andrzej Siewior int ret; 68088af8bbeSSebastian Andrzej Siewior 68188af8bbeSSebastian Andrzej Siewior gi = container_of(group, struct gadget_info, configs_group); 68288af8bbeSSebastian Andrzej Siewior ret = snprintf(buf, MAX_NAME_LEN, "%s", name); 68388af8bbeSSebastian Andrzej Siewior if (ret >= MAX_NAME_LEN) 68488af8bbeSSebastian Andrzej Siewior return ERR_PTR(-ENAMETOOLONG); 68588af8bbeSSebastian Andrzej Siewior 68688af8bbeSSebastian Andrzej Siewior num_str = strchr(buf, '.'); 68788af8bbeSSebastian Andrzej Siewior if (!num_str) { 68888af8bbeSSebastian Andrzej Siewior pr_err("Unable to locate . in name.bConfigurationValue\n"); 68988af8bbeSSebastian Andrzej Siewior return ERR_PTR(-EINVAL); 69088af8bbeSSebastian Andrzej Siewior } 69188af8bbeSSebastian Andrzej Siewior 69288af8bbeSSebastian Andrzej Siewior *num_str = '\0'; 69388af8bbeSSebastian Andrzej Siewior num_str++; 69488af8bbeSSebastian Andrzej Siewior 69588af8bbeSSebastian Andrzej Siewior if (!strlen(buf)) 69688af8bbeSSebastian Andrzej Siewior return ERR_PTR(-EINVAL); 69788af8bbeSSebastian Andrzej Siewior 69888af8bbeSSebastian Andrzej Siewior ret = kstrtou8(num_str, 0, &num); 69988af8bbeSSebastian Andrzej Siewior if (ret) 70088af8bbeSSebastian Andrzej Siewior return ERR_PTR(ret); 70188af8bbeSSebastian Andrzej Siewior 70288af8bbeSSebastian Andrzej Siewior cfg = kzalloc(sizeof(*cfg), GFP_KERNEL); 70388af8bbeSSebastian Andrzej Siewior if (!cfg) 70488af8bbeSSebastian Andrzej Siewior return ERR_PTR(-ENOMEM); 70588af8bbeSSebastian Andrzej Siewior cfg->c.label = kstrdup(buf, GFP_KERNEL); 70688af8bbeSSebastian Andrzej Siewior if (!cfg->c.label) { 70788af8bbeSSebastian Andrzej Siewior ret = -ENOMEM; 70888af8bbeSSebastian Andrzej Siewior goto err; 70988af8bbeSSebastian Andrzej Siewior } 71088af8bbeSSebastian Andrzej Siewior cfg->c.bConfigurationValue = num; 71188af8bbeSSebastian Andrzej Siewior cfg->c.MaxPower = CONFIG_USB_GADGET_VBUS_DRAW; 71288af8bbeSSebastian Andrzej Siewior cfg->c.bmAttributes = USB_CONFIG_ATT_ONE; 71388af8bbeSSebastian Andrzej Siewior INIT_LIST_HEAD(&cfg->string_list); 71488af8bbeSSebastian Andrzej Siewior INIT_LIST_HEAD(&cfg->func_list); 71588af8bbeSSebastian Andrzej Siewior 71688af8bbeSSebastian Andrzej Siewior config_group_init_type_name(&cfg->group, name, 71788af8bbeSSebastian Andrzej Siewior &gadget_config_type); 7181ae1602dSChristoph Hellwig 71988af8bbeSSebastian Andrzej Siewior config_group_init_type_name(&cfg->strings_group, "strings", 72088af8bbeSSebastian Andrzej Siewior &gadget_config_name_strings_type); 7211ae1602dSChristoph Hellwig configfs_add_default_group(&cfg->strings_group, &cfg->group); 72288af8bbeSSebastian Andrzej Siewior 72388af8bbeSSebastian Andrzej Siewior ret = usb_add_config_only(&gi->cdev, &cfg->c); 72488af8bbeSSebastian Andrzej Siewior if (ret) 72588af8bbeSSebastian Andrzej Siewior goto err; 72688af8bbeSSebastian Andrzej Siewior 72788af8bbeSSebastian Andrzej Siewior return &cfg->group; 72888af8bbeSSebastian Andrzej Siewior err: 72988af8bbeSSebastian Andrzej Siewior kfree(cfg->c.label); 73088af8bbeSSebastian Andrzej Siewior kfree(cfg); 73188af8bbeSSebastian Andrzej Siewior return ERR_PTR(ret); 73288af8bbeSSebastian Andrzej Siewior } 73388af8bbeSSebastian Andrzej Siewior 73488af8bbeSSebastian Andrzej Siewior static void config_desc_drop( 73588af8bbeSSebastian Andrzej Siewior struct config_group *group, 73688af8bbeSSebastian Andrzej Siewior struct config_item *item) 73788af8bbeSSebastian Andrzej Siewior { 73888af8bbeSSebastian Andrzej Siewior config_item_put(item); 73988af8bbeSSebastian Andrzej Siewior } 74088af8bbeSSebastian Andrzej Siewior 74188af8bbeSSebastian Andrzej Siewior static struct configfs_group_operations config_desc_ops = { 74288af8bbeSSebastian Andrzej Siewior .make_group = &config_desc_make, 74388af8bbeSSebastian Andrzej Siewior .drop_item = &config_desc_drop, 74488af8bbeSSebastian Andrzej Siewior }; 74588af8bbeSSebastian Andrzej Siewior 7464ad01412SBhumika Goyal static const struct config_item_type config_desc_type = { 74788af8bbeSSebastian Andrzej Siewior .ct_group_ops = &config_desc_ops, 74888af8bbeSSebastian Andrzej Siewior .ct_owner = THIS_MODULE, 74988af8bbeSSebastian Andrzej Siewior }; 75088af8bbeSSebastian Andrzej Siewior 75188af8bbeSSebastian Andrzej Siewior GS_STRINGS_RW(gadget_strings, manufacturer); 75288af8bbeSSebastian Andrzej Siewior GS_STRINGS_RW(gadget_strings, product); 75388af8bbeSSebastian Andrzej Siewior GS_STRINGS_RW(gadget_strings, serialnumber); 75488af8bbeSSebastian Andrzej Siewior 75588af8bbeSSebastian Andrzej Siewior static struct configfs_attribute *gadget_strings_langid_attrs[] = { 75645b6a73fSChristoph Hellwig &gadget_strings_attr_manufacturer, 75745b6a73fSChristoph Hellwig &gadget_strings_attr_product, 75845b6a73fSChristoph Hellwig &gadget_strings_attr_serialnumber, 75988af8bbeSSebastian Andrzej Siewior NULL, 76088af8bbeSSebastian Andrzej Siewior }; 76188af8bbeSSebastian Andrzej Siewior 76288af8bbeSSebastian Andrzej Siewior static void gadget_strings_attr_release(struct config_item *item) 76388af8bbeSSebastian Andrzej Siewior { 76488af8bbeSSebastian Andrzej Siewior struct gadget_strings *gs = to_gadget_strings(item); 76588af8bbeSSebastian Andrzej Siewior 76688af8bbeSSebastian Andrzej Siewior kfree(gs->manufacturer); 76788af8bbeSSebastian Andrzej Siewior kfree(gs->product); 76888af8bbeSSebastian Andrzej Siewior kfree(gs->serialnumber); 76988af8bbeSSebastian Andrzej Siewior 77088af8bbeSSebastian Andrzej Siewior list_del(&gs->list); 77188af8bbeSSebastian Andrzej Siewior kfree(gs); 77288af8bbeSSebastian Andrzej Siewior } 77388af8bbeSSebastian Andrzej Siewior 77488af8bbeSSebastian Andrzej Siewior USB_CONFIG_STRING_RW_OPS(gadget_strings); 77588af8bbeSSebastian Andrzej Siewior USB_CONFIG_STRINGS_LANG(gadget_strings, gadget_info); 77688af8bbeSSebastian Andrzej Siewior 77787213d38SAndrzej Pietrasiewicz static inline struct os_desc *to_os_desc(struct config_item *item) 77887213d38SAndrzej Pietrasiewicz { 77987213d38SAndrzej Pietrasiewicz return container_of(to_config_group(item), struct os_desc, group); 78087213d38SAndrzej Pietrasiewicz } 78187213d38SAndrzej Pietrasiewicz 78245b6a73fSChristoph Hellwig static inline struct gadget_info *os_desc_item_to_gadget_info( 78345b6a73fSChristoph Hellwig struct config_item *item) 78487213d38SAndrzej Pietrasiewicz { 78545b6a73fSChristoph Hellwig return to_gadget_info(to_os_desc(item)->group.cg_item.ci_parent); 78687213d38SAndrzej Pietrasiewicz } 78787213d38SAndrzej Pietrasiewicz 78845b6a73fSChristoph Hellwig static ssize_t os_desc_use_show(struct config_item *item, char *page) 78945b6a73fSChristoph Hellwig { 790e800e8cbSStefan Agner return sprintf(page, "%d\n", 79145b6a73fSChristoph Hellwig os_desc_item_to_gadget_info(item)->use_os_desc); 79245b6a73fSChristoph Hellwig } 79345b6a73fSChristoph Hellwig 79445b6a73fSChristoph Hellwig static ssize_t os_desc_use_store(struct config_item *item, const char *page, 79587213d38SAndrzej Pietrasiewicz size_t len) 79687213d38SAndrzej Pietrasiewicz { 79745b6a73fSChristoph Hellwig struct gadget_info *gi = os_desc_item_to_gadget_info(item); 79887213d38SAndrzej Pietrasiewicz int ret; 79987213d38SAndrzej Pietrasiewicz bool use; 80087213d38SAndrzej Pietrasiewicz 80187213d38SAndrzej Pietrasiewicz mutex_lock(&gi->lock); 80287213d38SAndrzej Pietrasiewicz ret = strtobool(page, &use); 80387213d38SAndrzej Pietrasiewicz if (!ret) { 80487213d38SAndrzej Pietrasiewicz gi->use_os_desc = use; 80587213d38SAndrzej Pietrasiewicz ret = len; 80687213d38SAndrzej Pietrasiewicz } 80787213d38SAndrzej Pietrasiewicz mutex_unlock(&gi->lock); 80887213d38SAndrzej Pietrasiewicz 80987213d38SAndrzej Pietrasiewicz return ret; 81087213d38SAndrzej Pietrasiewicz } 81187213d38SAndrzej Pietrasiewicz 81245b6a73fSChristoph Hellwig static ssize_t os_desc_b_vendor_code_show(struct config_item *item, char *page) 81387213d38SAndrzej Pietrasiewicz { 814e800e8cbSStefan Agner return sprintf(page, "0x%02x\n", 81545b6a73fSChristoph Hellwig os_desc_item_to_gadget_info(item)->b_vendor_code); 81687213d38SAndrzej Pietrasiewicz } 81787213d38SAndrzej Pietrasiewicz 81845b6a73fSChristoph Hellwig static ssize_t os_desc_b_vendor_code_store(struct config_item *item, 81987213d38SAndrzej Pietrasiewicz const char *page, size_t len) 82087213d38SAndrzej Pietrasiewicz { 82145b6a73fSChristoph Hellwig struct gadget_info *gi = os_desc_item_to_gadget_info(item); 82287213d38SAndrzej Pietrasiewicz int ret; 82387213d38SAndrzej Pietrasiewicz u8 b_vendor_code; 82487213d38SAndrzej Pietrasiewicz 82587213d38SAndrzej Pietrasiewicz mutex_lock(&gi->lock); 82687213d38SAndrzej Pietrasiewicz ret = kstrtou8(page, 0, &b_vendor_code); 82787213d38SAndrzej Pietrasiewicz if (!ret) { 82887213d38SAndrzej Pietrasiewicz gi->b_vendor_code = b_vendor_code; 82987213d38SAndrzej Pietrasiewicz ret = len; 83087213d38SAndrzej Pietrasiewicz } 83187213d38SAndrzej Pietrasiewicz mutex_unlock(&gi->lock); 83287213d38SAndrzej Pietrasiewicz 83387213d38SAndrzej Pietrasiewicz return ret; 83487213d38SAndrzej Pietrasiewicz } 83587213d38SAndrzej Pietrasiewicz 83645b6a73fSChristoph Hellwig static ssize_t os_desc_qw_sign_show(struct config_item *item, char *page) 83787213d38SAndrzej Pietrasiewicz { 83845b6a73fSChristoph Hellwig struct gadget_info *gi = os_desc_item_to_gadget_info(item); 83976180d71SStefan Agner int res; 84087213d38SAndrzej Pietrasiewicz 84176180d71SStefan Agner res = utf16s_to_utf8s((wchar_t *) gi->qw_sign, OS_STRING_QW_SIGN_LEN, 84276180d71SStefan Agner UTF16_LITTLE_ENDIAN, page, PAGE_SIZE - 1); 84376180d71SStefan Agner page[res++] = '\n'; 84476180d71SStefan Agner 84576180d71SStefan Agner return res; 84687213d38SAndrzej Pietrasiewicz } 84787213d38SAndrzej Pietrasiewicz 84845b6a73fSChristoph Hellwig static ssize_t os_desc_qw_sign_store(struct config_item *item, const char *page, 84987213d38SAndrzej Pietrasiewicz size_t len) 85087213d38SAndrzej Pietrasiewicz { 85145b6a73fSChristoph Hellwig struct gadget_info *gi = os_desc_item_to_gadget_info(item); 85287213d38SAndrzej Pietrasiewicz int res, l; 85387213d38SAndrzej Pietrasiewicz 85487213d38SAndrzej Pietrasiewicz l = min((int)len, OS_STRING_QW_SIGN_LEN >> 1); 85587213d38SAndrzej Pietrasiewicz if (page[l - 1] == '\n') 85687213d38SAndrzej Pietrasiewicz --l; 85787213d38SAndrzej Pietrasiewicz 85887213d38SAndrzej Pietrasiewicz mutex_lock(&gi->lock); 85987213d38SAndrzej Pietrasiewicz res = utf8s_to_utf16s(page, l, 86087213d38SAndrzej Pietrasiewicz UTF16_LITTLE_ENDIAN, (wchar_t *) gi->qw_sign, 86187213d38SAndrzej Pietrasiewicz OS_STRING_QW_SIGN_LEN); 86287213d38SAndrzej Pietrasiewicz if (res > 0) 86387213d38SAndrzej Pietrasiewicz res = len; 86487213d38SAndrzej Pietrasiewicz mutex_unlock(&gi->lock); 86587213d38SAndrzej Pietrasiewicz 86687213d38SAndrzej Pietrasiewicz return res; 86787213d38SAndrzej Pietrasiewicz } 86887213d38SAndrzej Pietrasiewicz 86945b6a73fSChristoph Hellwig CONFIGFS_ATTR(os_desc_, use); 87045b6a73fSChristoph Hellwig CONFIGFS_ATTR(os_desc_, b_vendor_code); 87145b6a73fSChristoph Hellwig CONFIGFS_ATTR(os_desc_, qw_sign); 87287213d38SAndrzej Pietrasiewicz 87387213d38SAndrzej Pietrasiewicz static struct configfs_attribute *os_desc_attrs[] = { 87445b6a73fSChristoph Hellwig &os_desc_attr_use, 87545b6a73fSChristoph Hellwig &os_desc_attr_b_vendor_code, 87645b6a73fSChristoph Hellwig &os_desc_attr_qw_sign, 87787213d38SAndrzej Pietrasiewicz NULL, 87887213d38SAndrzej Pietrasiewicz }; 87987213d38SAndrzej Pietrasiewicz 88087213d38SAndrzej Pietrasiewicz static void os_desc_attr_release(struct config_item *item) 88187213d38SAndrzej Pietrasiewicz { 88287213d38SAndrzej Pietrasiewicz struct os_desc *os_desc = to_os_desc(item); 88387213d38SAndrzej Pietrasiewicz kfree(os_desc); 88487213d38SAndrzej Pietrasiewicz } 88587213d38SAndrzej Pietrasiewicz 886da424314SAndrzej Pietrasiewicz static int os_desc_link(struct config_item *os_desc_ci, 887da424314SAndrzej Pietrasiewicz struct config_item *usb_cfg_ci) 888da424314SAndrzej Pietrasiewicz { 889da424314SAndrzej Pietrasiewicz struct gadget_info *gi = container_of(to_config_group(os_desc_ci), 890da424314SAndrzej Pietrasiewicz struct gadget_info, os_desc_group); 891da424314SAndrzej Pietrasiewicz struct usb_composite_dev *cdev = &gi->cdev; 892da424314SAndrzej Pietrasiewicz struct config_usb_cfg *c_target = 893da424314SAndrzej Pietrasiewicz container_of(to_config_group(usb_cfg_ci), 894da424314SAndrzej Pietrasiewicz struct config_usb_cfg, group); 895da424314SAndrzej Pietrasiewicz struct usb_configuration *c; 896da424314SAndrzej Pietrasiewicz int ret; 897da424314SAndrzej Pietrasiewicz 898da424314SAndrzej Pietrasiewicz mutex_lock(&gi->lock); 899da424314SAndrzej Pietrasiewicz list_for_each_entry(c, &cdev->configs, list) { 900da424314SAndrzej Pietrasiewicz if (c == &c_target->c) 901da424314SAndrzej Pietrasiewicz break; 902da424314SAndrzej Pietrasiewicz } 903da424314SAndrzej Pietrasiewicz if (c != &c_target->c) { 904da424314SAndrzej Pietrasiewicz ret = -EINVAL; 905da424314SAndrzej Pietrasiewicz goto out; 906da424314SAndrzej Pietrasiewicz } 907da424314SAndrzej Pietrasiewicz 908da424314SAndrzej Pietrasiewicz if (cdev->os_desc_config) { 909da424314SAndrzej Pietrasiewicz ret = -EBUSY; 910da424314SAndrzej Pietrasiewicz goto out; 911da424314SAndrzej Pietrasiewicz } 912da424314SAndrzej Pietrasiewicz 913da424314SAndrzej Pietrasiewicz cdev->os_desc_config = &c_target->c; 914da424314SAndrzej Pietrasiewicz ret = 0; 915da424314SAndrzej Pietrasiewicz 916da424314SAndrzej Pietrasiewicz out: 917da424314SAndrzej Pietrasiewicz mutex_unlock(&gi->lock); 918da424314SAndrzej Pietrasiewicz return ret; 919da424314SAndrzej Pietrasiewicz } 920da424314SAndrzej Pietrasiewicz 921e16769d4SAndrzej Pietrasiewicz static void os_desc_unlink(struct config_item *os_desc_ci, 922da424314SAndrzej Pietrasiewicz struct config_item *usb_cfg_ci) 923da424314SAndrzej Pietrasiewicz { 924da424314SAndrzej Pietrasiewicz struct gadget_info *gi = container_of(to_config_group(os_desc_ci), 925da424314SAndrzej Pietrasiewicz struct gadget_info, os_desc_group); 926da424314SAndrzej Pietrasiewicz struct usb_composite_dev *cdev = &gi->cdev; 927da424314SAndrzej Pietrasiewicz 928da424314SAndrzej Pietrasiewicz mutex_lock(&gi->lock); 929afdaadc3SRuslan Bilovol if (gi->composite.gadget_driver.udc_name) 930da424314SAndrzej Pietrasiewicz unregister_gadget(gi); 931da424314SAndrzej Pietrasiewicz cdev->os_desc_config = NULL; 932afdaadc3SRuslan Bilovol WARN_ON(gi->composite.gadget_driver.udc_name); 933da424314SAndrzej Pietrasiewicz mutex_unlock(&gi->lock); 934da424314SAndrzej Pietrasiewicz } 935da424314SAndrzej Pietrasiewicz 93687213d38SAndrzej Pietrasiewicz static struct configfs_item_operations os_desc_ops = { 93787213d38SAndrzej Pietrasiewicz .release = os_desc_attr_release, 938da424314SAndrzej Pietrasiewicz .allow_link = os_desc_link, 939da424314SAndrzej Pietrasiewicz .drop_link = os_desc_unlink, 94087213d38SAndrzej Pietrasiewicz }; 94187213d38SAndrzej Pietrasiewicz 94287213d38SAndrzej Pietrasiewicz static struct config_item_type os_desc_type = { 94387213d38SAndrzej Pietrasiewicz .ct_item_ops = &os_desc_ops, 94487213d38SAndrzej Pietrasiewicz .ct_attrs = os_desc_attrs, 94587213d38SAndrzej Pietrasiewicz .ct_owner = THIS_MODULE, 94687213d38SAndrzej Pietrasiewicz }; 94787213d38SAndrzej Pietrasiewicz 9487419485fSAndrzej Pietrasiewicz static inline struct usb_os_desc_ext_prop 9497419485fSAndrzej Pietrasiewicz *to_usb_os_desc_ext_prop(struct config_item *item) 9507419485fSAndrzej Pietrasiewicz { 9517419485fSAndrzej Pietrasiewicz return container_of(item, struct usb_os_desc_ext_prop, item); 9527419485fSAndrzej Pietrasiewicz } 9537419485fSAndrzej Pietrasiewicz 95445b6a73fSChristoph Hellwig static ssize_t ext_prop_type_show(struct config_item *item, char *page) 9557419485fSAndrzej Pietrasiewicz { 956e800e8cbSStefan Agner return sprintf(page, "%d\n", to_usb_os_desc_ext_prop(item)->type); 9577419485fSAndrzej Pietrasiewicz } 9587419485fSAndrzej Pietrasiewicz 95945b6a73fSChristoph Hellwig static ssize_t ext_prop_type_store(struct config_item *item, 9607419485fSAndrzej Pietrasiewicz const char *page, size_t len) 9617419485fSAndrzej Pietrasiewicz { 96245b6a73fSChristoph Hellwig struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item); 9637419485fSAndrzej Pietrasiewicz struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent); 9647419485fSAndrzej Pietrasiewicz u8 type; 9657419485fSAndrzej Pietrasiewicz int ret; 9667419485fSAndrzej Pietrasiewicz 9677419485fSAndrzej Pietrasiewicz if (desc->opts_mutex) 9687419485fSAndrzej Pietrasiewicz mutex_lock(desc->opts_mutex); 9697419485fSAndrzej Pietrasiewicz ret = kstrtou8(page, 0, &type); 9707419485fSAndrzej Pietrasiewicz if (ret) 9717419485fSAndrzej Pietrasiewicz goto end; 9727419485fSAndrzej Pietrasiewicz if (type < USB_EXT_PROP_UNICODE || type > USB_EXT_PROP_UNICODE_MULTI) { 9737419485fSAndrzej Pietrasiewicz ret = -EINVAL; 9747419485fSAndrzej Pietrasiewicz goto end; 9757419485fSAndrzej Pietrasiewicz } 9767419485fSAndrzej Pietrasiewicz 9777419485fSAndrzej Pietrasiewicz if ((ext_prop->type == USB_EXT_PROP_BINARY || 9787419485fSAndrzej Pietrasiewicz ext_prop->type == USB_EXT_PROP_LE32 || 9797419485fSAndrzej Pietrasiewicz ext_prop->type == USB_EXT_PROP_BE32) && 9807419485fSAndrzej Pietrasiewicz (type == USB_EXT_PROP_UNICODE || 9817419485fSAndrzej Pietrasiewicz type == USB_EXT_PROP_UNICODE_ENV || 9827419485fSAndrzej Pietrasiewicz type == USB_EXT_PROP_UNICODE_LINK)) 9837419485fSAndrzej Pietrasiewicz ext_prop->data_len <<= 1; 9847419485fSAndrzej Pietrasiewicz else if ((ext_prop->type == USB_EXT_PROP_UNICODE || 9857419485fSAndrzej Pietrasiewicz ext_prop->type == USB_EXT_PROP_UNICODE_ENV || 9867419485fSAndrzej Pietrasiewicz ext_prop->type == USB_EXT_PROP_UNICODE_LINK) && 9877419485fSAndrzej Pietrasiewicz (type == USB_EXT_PROP_BINARY || 9887419485fSAndrzej Pietrasiewicz type == USB_EXT_PROP_LE32 || 9897419485fSAndrzej Pietrasiewicz type == USB_EXT_PROP_BE32)) 9907419485fSAndrzej Pietrasiewicz ext_prop->data_len >>= 1; 9917419485fSAndrzej Pietrasiewicz ext_prop->type = type; 9927419485fSAndrzej Pietrasiewicz ret = len; 9937419485fSAndrzej Pietrasiewicz 9947419485fSAndrzej Pietrasiewicz end: 9957419485fSAndrzej Pietrasiewicz if (desc->opts_mutex) 9967419485fSAndrzej Pietrasiewicz mutex_unlock(desc->opts_mutex); 9977419485fSAndrzej Pietrasiewicz return ret; 9987419485fSAndrzej Pietrasiewicz } 9997419485fSAndrzej Pietrasiewicz 100045b6a73fSChristoph Hellwig static ssize_t ext_prop_data_show(struct config_item *item, char *page) 10017419485fSAndrzej Pietrasiewicz { 100245b6a73fSChristoph Hellwig struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item); 10037419485fSAndrzej Pietrasiewicz int len = ext_prop->data_len; 10047419485fSAndrzej Pietrasiewicz 10057419485fSAndrzej Pietrasiewicz if (ext_prop->type == USB_EXT_PROP_UNICODE || 10067419485fSAndrzej Pietrasiewicz ext_prop->type == USB_EXT_PROP_UNICODE_ENV || 10077419485fSAndrzej Pietrasiewicz ext_prop->type == USB_EXT_PROP_UNICODE_LINK) 10087419485fSAndrzej Pietrasiewicz len >>= 1; 10097419485fSAndrzej Pietrasiewicz memcpy(page, ext_prop->data, len); 10107419485fSAndrzej Pietrasiewicz 10117419485fSAndrzej Pietrasiewicz return len; 10127419485fSAndrzej Pietrasiewicz } 10137419485fSAndrzej Pietrasiewicz 101445b6a73fSChristoph Hellwig static ssize_t ext_prop_data_store(struct config_item *item, 10157419485fSAndrzej Pietrasiewicz const char *page, size_t len) 10167419485fSAndrzej Pietrasiewicz { 101745b6a73fSChristoph Hellwig struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item); 10187419485fSAndrzej Pietrasiewicz struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent); 10197419485fSAndrzej Pietrasiewicz char *new_data; 10207419485fSAndrzej Pietrasiewicz size_t ret_len = len; 10217419485fSAndrzej Pietrasiewicz 10227419485fSAndrzej Pietrasiewicz if (page[len - 1] == '\n' || page[len - 1] == '\0') 10237419485fSAndrzej Pietrasiewicz --len; 102458b949e0SBenoit Taine new_data = kmemdup(page, len, GFP_KERNEL); 10257419485fSAndrzej Pietrasiewicz if (!new_data) 10267419485fSAndrzej Pietrasiewicz return -ENOMEM; 10277419485fSAndrzej Pietrasiewicz 10287419485fSAndrzej Pietrasiewicz if (desc->opts_mutex) 10297419485fSAndrzej Pietrasiewicz mutex_lock(desc->opts_mutex); 10307419485fSAndrzej Pietrasiewicz kfree(ext_prop->data); 10317419485fSAndrzej Pietrasiewicz ext_prop->data = new_data; 10327419485fSAndrzej Pietrasiewicz desc->ext_prop_len -= ext_prop->data_len; 10337419485fSAndrzej Pietrasiewicz ext_prop->data_len = len; 10347419485fSAndrzej Pietrasiewicz desc->ext_prop_len += ext_prop->data_len; 10357419485fSAndrzej Pietrasiewicz if (ext_prop->type == USB_EXT_PROP_UNICODE || 10367419485fSAndrzej Pietrasiewicz ext_prop->type == USB_EXT_PROP_UNICODE_ENV || 10377419485fSAndrzej Pietrasiewicz ext_prop->type == USB_EXT_PROP_UNICODE_LINK) { 10387419485fSAndrzej Pietrasiewicz desc->ext_prop_len -= ext_prop->data_len; 10397419485fSAndrzej Pietrasiewicz ext_prop->data_len <<= 1; 10407419485fSAndrzej Pietrasiewicz ext_prop->data_len += 2; 10417419485fSAndrzej Pietrasiewicz desc->ext_prop_len += ext_prop->data_len; 10427419485fSAndrzej Pietrasiewicz } 10437419485fSAndrzej Pietrasiewicz if (desc->opts_mutex) 10447419485fSAndrzej Pietrasiewicz mutex_unlock(desc->opts_mutex); 10457419485fSAndrzej Pietrasiewicz return ret_len; 10467419485fSAndrzej Pietrasiewicz } 10477419485fSAndrzej Pietrasiewicz 104845b6a73fSChristoph Hellwig CONFIGFS_ATTR(ext_prop_, type); 104945b6a73fSChristoph Hellwig CONFIGFS_ATTR(ext_prop_, data); 10507419485fSAndrzej Pietrasiewicz 10517419485fSAndrzej Pietrasiewicz static struct configfs_attribute *ext_prop_attrs[] = { 105245b6a73fSChristoph Hellwig &ext_prop_attr_type, 105345b6a73fSChristoph Hellwig &ext_prop_attr_data, 10547419485fSAndrzej Pietrasiewicz NULL, 10557419485fSAndrzej Pietrasiewicz }; 10567419485fSAndrzej Pietrasiewicz 10577419485fSAndrzej Pietrasiewicz static void usb_os_desc_ext_prop_release(struct config_item *item) 10587419485fSAndrzej Pietrasiewicz { 10597419485fSAndrzej Pietrasiewicz struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item); 10607419485fSAndrzej Pietrasiewicz 10617419485fSAndrzej Pietrasiewicz kfree(ext_prop); /* frees a whole chunk */ 10627419485fSAndrzej Pietrasiewicz } 10637419485fSAndrzej Pietrasiewicz 10647419485fSAndrzej Pietrasiewicz static struct configfs_item_operations ext_prop_ops = { 10657419485fSAndrzej Pietrasiewicz .release = usb_os_desc_ext_prop_release, 10667419485fSAndrzej Pietrasiewicz }; 10677419485fSAndrzej Pietrasiewicz 10687419485fSAndrzej Pietrasiewicz static struct config_item *ext_prop_make( 10697419485fSAndrzej Pietrasiewicz struct config_group *group, 10707419485fSAndrzej Pietrasiewicz const char *name) 10717419485fSAndrzej Pietrasiewicz { 10727419485fSAndrzej Pietrasiewicz struct usb_os_desc_ext_prop *ext_prop; 10737419485fSAndrzej Pietrasiewicz struct config_item_type *ext_prop_type; 10747419485fSAndrzej Pietrasiewicz struct usb_os_desc *desc; 10757419485fSAndrzej Pietrasiewicz char *vlabuf; 10767419485fSAndrzej Pietrasiewicz 10777419485fSAndrzej Pietrasiewicz vla_group(data_chunk); 10787419485fSAndrzej Pietrasiewicz vla_item(data_chunk, struct usb_os_desc_ext_prop, ext_prop, 1); 10797419485fSAndrzej Pietrasiewicz vla_item(data_chunk, struct config_item_type, ext_prop_type, 1); 10807419485fSAndrzej Pietrasiewicz 10817419485fSAndrzej Pietrasiewicz vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL); 10827419485fSAndrzej Pietrasiewicz if (!vlabuf) 10837419485fSAndrzej Pietrasiewicz return ERR_PTR(-ENOMEM); 10847419485fSAndrzej Pietrasiewicz 10857419485fSAndrzej Pietrasiewicz ext_prop = vla_ptr(vlabuf, data_chunk, ext_prop); 10867419485fSAndrzej Pietrasiewicz ext_prop_type = vla_ptr(vlabuf, data_chunk, ext_prop_type); 10877419485fSAndrzej Pietrasiewicz 10887419485fSAndrzej Pietrasiewicz desc = container_of(group, struct usb_os_desc, group); 10897419485fSAndrzej Pietrasiewicz ext_prop_type->ct_item_ops = &ext_prop_ops; 10907419485fSAndrzej Pietrasiewicz ext_prop_type->ct_attrs = ext_prop_attrs; 10917419485fSAndrzej Pietrasiewicz ext_prop_type->ct_owner = desc->owner; 10927419485fSAndrzej Pietrasiewicz 10937419485fSAndrzej Pietrasiewicz config_item_init_type_name(&ext_prop->item, name, ext_prop_type); 10947419485fSAndrzej Pietrasiewicz 10957419485fSAndrzej Pietrasiewicz ext_prop->name = kstrdup(name, GFP_KERNEL); 10967419485fSAndrzej Pietrasiewicz if (!ext_prop->name) { 10977419485fSAndrzej Pietrasiewicz kfree(vlabuf); 10987419485fSAndrzej Pietrasiewicz return ERR_PTR(-ENOMEM); 10997419485fSAndrzej Pietrasiewicz } 11007419485fSAndrzej Pietrasiewicz desc->ext_prop_len += 14; 11017419485fSAndrzej Pietrasiewicz ext_prop->name_len = 2 * strlen(ext_prop->name) + 2; 11027419485fSAndrzej Pietrasiewicz if (desc->opts_mutex) 11037419485fSAndrzej Pietrasiewicz mutex_lock(desc->opts_mutex); 11047419485fSAndrzej Pietrasiewicz desc->ext_prop_len += ext_prop->name_len; 11057419485fSAndrzej Pietrasiewicz list_add_tail(&ext_prop->entry, &desc->ext_prop); 11067419485fSAndrzej Pietrasiewicz ++desc->ext_prop_count; 11077419485fSAndrzej Pietrasiewicz if (desc->opts_mutex) 11087419485fSAndrzej Pietrasiewicz mutex_unlock(desc->opts_mutex); 11097419485fSAndrzej Pietrasiewicz 11107419485fSAndrzej Pietrasiewicz return &ext_prop->item; 11117419485fSAndrzej Pietrasiewicz } 11127419485fSAndrzej Pietrasiewicz 11137419485fSAndrzej Pietrasiewicz static void ext_prop_drop(struct config_group *group, struct config_item *item) 11147419485fSAndrzej Pietrasiewicz { 11157419485fSAndrzej Pietrasiewicz struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item); 11167419485fSAndrzej Pietrasiewicz struct usb_os_desc *desc = to_usb_os_desc(&group->cg_item); 11177419485fSAndrzej Pietrasiewicz 11187419485fSAndrzej Pietrasiewicz if (desc->opts_mutex) 11197419485fSAndrzej Pietrasiewicz mutex_lock(desc->opts_mutex); 11207419485fSAndrzej Pietrasiewicz list_del(&ext_prop->entry); 11217419485fSAndrzej Pietrasiewicz --desc->ext_prop_count; 11227419485fSAndrzej Pietrasiewicz kfree(ext_prop->name); 11237419485fSAndrzej Pietrasiewicz desc->ext_prop_len -= (ext_prop->name_len + ext_prop->data_len + 14); 11247419485fSAndrzej Pietrasiewicz if (desc->opts_mutex) 11257419485fSAndrzej Pietrasiewicz mutex_unlock(desc->opts_mutex); 11267419485fSAndrzej Pietrasiewicz config_item_put(item); 11277419485fSAndrzej Pietrasiewicz } 11287419485fSAndrzej Pietrasiewicz 11297419485fSAndrzej Pietrasiewicz static struct configfs_group_operations interf_grp_ops = { 11307419485fSAndrzej Pietrasiewicz .make_item = &ext_prop_make, 11317419485fSAndrzej Pietrasiewicz .drop_item = &ext_prop_drop, 11327419485fSAndrzej Pietrasiewicz }; 11337419485fSAndrzej Pietrasiewicz 113445b6a73fSChristoph Hellwig static ssize_t interf_grp_compatible_id_show(struct config_item *item, 1135da424314SAndrzej Pietrasiewicz char *page) 1136da424314SAndrzej Pietrasiewicz { 113745b6a73fSChristoph Hellwig memcpy(page, to_usb_os_desc(item)->ext_compat_id, 8); 1138da424314SAndrzej Pietrasiewicz return 8; 1139da424314SAndrzej Pietrasiewicz } 1140da424314SAndrzej Pietrasiewicz 114145b6a73fSChristoph Hellwig static ssize_t interf_grp_compatible_id_store(struct config_item *item, 1142da424314SAndrzej Pietrasiewicz const char *page, size_t len) 1143da424314SAndrzej Pietrasiewicz { 114445b6a73fSChristoph Hellwig struct usb_os_desc *desc = to_usb_os_desc(item); 1145da424314SAndrzej Pietrasiewicz int l; 1146da424314SAndrzej Pietrasiewicz 1147da424314SAndrzej Pietrasiewicz l = min_t(int, 8, len); 1148da424314SAndrzej Pietrasiewicz if (page[l - 1] == '\n') 1149da424314SAndrzej Pietrasiewicz --l; 1150da424314SAndrzej Pietrasiewicz if (desc->opts_mutex) 1151da424314SAndrzej Pietrasiewicz mutex_lock(desc->opts_mutex); 1152da424314SAndrzej Pietrasiewicz memcpy(desc->ext_compat_id, page, l); 1153da424314SAndrzej Pietrasiewicz 1154da424314SAndrzej Pietrasiewicz if (desc->opts_mutex) 1155da424314SAndrzej Pietrasiewicz mutex_unlock(desc->opts_mutex); 1156da424314SAndrzej Pietrasiewicz 1157da424314SAndrzej Pietrasiewicz return len; 1158da424314SAndrzej Pietrasiewicz } 1159da424314SAndrzej Pietrasiewicz 116045b6a73fSChristoph Hellwig static ssize_t interf_grp_sub_compatible_id_show(struct config_item *item, 1161da424314SAndrzej Pietrasiewicz char *page) 1162da424314SAndrzej Pietrasiewicz { 116345b6a73fSChristoph Hellwig memcpy(page, to_usb_os_desc(item)->ext_compat_id + 8, 8); 1164da424314SAndrzej Pietrasiewicz return 8; 1165da424314SAndrzej Pietrasiewicz } 1166da424314SAndrzej Pietrasiewicz 116745b6a73fSChristoph Hellwig static ssize_t interf_grp_sub_compatible_id_store(struct config_item *item, 1168da424314SAndrzej Pietrasiewicz const char *page, size_t len) 1169da424314SAndrzej Pietrasiewicz { 117045b6a73fSChristoph Hellwig struct usb_os_desc *desc = to_usb_os_desc(item); 1171da424314SAndrzej Pietrasiewicz int l; 1172da424314SAndrzej Pietrasiewicz 1173da424314SAndrzej Pietrasiewicz l = min_t(int, 8, len); 1174da424314SAndrzej Pietrasiewicz if (page[l - 1] == '\n') 1175da424314SAndrzej Pietrasiewicz --l; 1176da424314SAndrzej Pietrasiewicz if (desc->opts_mutex) 1177da424314SAndrzej Pietrasiewicz mutex_lock(desc->opts_mutex); 1178da424314SAndrzej Pietrasiewicz memcpy(desc->ext_compat_id + 8, page, l); 1179da424314SAndrzej Pietrasiewicz 1180da424314SAndrzej Pietrasiewicz if (desc->opts_mutex) 1181da424314SAndrzej Pietrasiewicz mutex_unlock(desc->opts_mutex); 1182da424314SAndrzej Pietrasiewicz 1183da424314SAndrzej Pietrasiewicz return len; 1184da424314SAndrzej Pietrasiewicz } 1185da424314SAndrzej Pietrasiewicz 118645b6a73fSChristoph Hellwig CONFIGFS_ATTR(interf_grp_, compatible_id); 118745b6a73fSChristoph Hellwig CONFIGFS_ATTR(interf_grp_, sub_compatible_id); 1188da424314SAndrzej Pietrasiewicz 1189da424314SAndrzej Pietrasiewicz static struct configfs_attribute *interf_grp_attrs[] = { 119045b6a73fSChristoph Hellwig &interf_grp_attr_compatible_id, 119145b6a73fSChristoph Hellwig &interf_grp_attr_sub_compatible_id, 1192da424314SAndrzej Pietrasiewicz NULL 1193da424314SAndrzej Pietrasiewicz }; 1194da424314SAndrzej Pietrasiewicz 1195ff74745eSAndrew Gabbasov struct config_group *usb_os_desc_prepare_interf_dir( 1196ff74745eSAndrew Gabbasov struct config_group *parent, 1197da424314SAndrzej Pietrasiewicz int n_interf, 1198da424314SAndrzej Pietrasiewicz struct usb_os_desc **desc, 119914574b54SAndrzej Pietrasiewicz char **names, 1200da424314SAndrzej Pietrasiewicz struct module *owner) 1201da424314SAndrzej Pietrasiewicz { 12021ae1602dSChristoph Hellwig struct config_group *os_desc_group; 1203da424314SAndrzej Pietrasiewicz struct config_item_type *os_desc_type, *interface_type; 1204da424314SAndrzej Pietrasiewicz 1205da424314SAndrzej Pietrasiewicz vla_group(data_chunk); 1206da424314SAndrzej Pietrasiewicz vla_item(data_chunk, struct config_group, os_desc_group, 1); 1207da424314SAndrzej Pietrasiewicz vla_item(data_chunk, struct config_item_type, os_desc_type, 1); 1208da424314SAndrzej Pietrasiewicz vla_item(data_chunk, struct config_item_type, interface_type, 1); 1209da424314SAndrzej Pietrasiewicz 1210da424314SAndrzej Pietrasiewicz char *vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL); 1211da424314SAndrzej Pietrasiewicz if (!vlabuf) 1212ff74745eSAndrew Gabbasov return ERR_PTR(-ENOMEM); 1213da424314SAndrzej Pietrasiewicz 1214da424314SAndrzej Pietrasiewicz os_desc_group = vla_ptr(vlabuf, data_chunk, os_desc_group); 1215da424314SAndrzej Pietrasiewicz os_desc_type = vla_ptr(vlabuf, data_chunk, os_desc_type); 1216da424314SAndrzej Pietrasiewicz interface_type = vla_ptr(vlabuf, data_chunk, interface_type); 1217da424314SAndrzej Pietrasiewicz 1218da424314SAndrzej Pietrasiewicz os_desc_type->ct_owner = owner; 1219da424314SAndrzej Pietrasiewicz config_group_init_type_name(os_desc_group, "os_desc", os_desc_type); 12201ae1602dSChristoph Hellwig configfs_add_default_group(os_desc_group, parent); 1221da424314SAndrzej Pietrasiewicz 12227419485fSAndrzej Pietrasiewicz interface_type->ct_group_ops = &interf_grp_ops; 1223da424314SAndrzej Pietrasiewicz interface_type->ct_attrs = interf_grp_attrs; 1224da424314SAndrzej Pietrasiewicz interface_type->ct_owner = owner; 1225da424314SAndrzej Pietrasiewicz 1226da424314SAndrzej Pietrasiewicz while (n_interf--) { 1227da424314SAndrzej Pietrasiewicz struct usb_os_desc *d; 1228da424314SAndrzej Pietrasiewicz 1229da424314SAndrzej Pietrasiewicz d = desc[n_interf]; 12307419485fSAndrzej Pietrasiewicz d->owner = owner; 1231da424314SAndrzej Pietrasiewicz config_group_init_type_name(&d->group, "", interface_type); 123214574b54SAndrzej Pietrasiewicz config_item_set_name(&d->group.cg_item, "interface.%s", 123314574b54SAndrzej Pietrasiewicz names[n_interf]); 12341ae1602dSChristoph Hellwig configfs_add_default_group(&d->group, os_desc_group); 1235da424314SAndrzej Pietrasiewicz } 1236da424314SAndrzej Pietrasiewicz 1237ff74745eSAndrew Gabbasov return os_desc_group; 1238da424314SAndrzej Pietrasiewicz } 1239da424314SAndrzej Pietrasiewicz EXPORT_SYMBOL(usb_os_desc_prepare_interf_dir); 1240da424314SAndrzej Pietrasiewicz 124188af8bbeSSebastian Andrzej Siewior static int configfs_do_nothing(struct usb_composite_dev *cdev) 124288af8bbeSSebastian Andrzej Siewior { 124375bfe23aSDavid Rientjes WARN_ON(1); 124488af8bbeSSebastian Andrzej Siewior return -EINVAL; 124588af8bbeSSebastian Andrzej Siewior } 124688af8bbeSSebastian Andrzej Siewior 124788af8bbeSSebastian Andrzej Siewior int composite_dev_prepare(struct usb_composite_driver *composite, 124888af8bbeSSebastian Andrzej Siewior struct usb_composite_dev *dev); 124988af8bbeSSebastian Andrzej Siewior 1250da424314SAndrzej Pietrasiewicz int composite_os_desc_req_prepare(struct usb_composite_dev *cdev, 1251da424314SAndrzej Pietrasiewicz struct usb_ep *ep0); 1252da424314SAndrzej Pietrasiewicz 125388af8bbeSSebastian Andrzej Siewior static void purge_configs_funcs(struct gadget_info *gi) 125488af8bbeSSebastian Andrzej Siewior { 125588af8bbeSSebastian Andrzej Siewior struct usb_configuration *c; 125688af8bbeSSebastian Andrzej Siewior 125788af8bbeSSebastian Andrzej Siewior list_for_each_entry(c, &gi->cdev.configs, list) { 125888af8bbeSSebastian Andrzej Siewior struct usb_function *f, *tmp; 125988af8bbeSSebastian Andrzej Siewior struct config_usb_cfg *cfg; 126088af8bbeSSebastian Andrzej Siewior 126188af8bbeSSebastian Andrzej Siewior cfg = container_of(c, struct config_usb_cfg, c); 126288af8bbeSSebastian Andrzej Siewior 126388af8bbeSSebastian Andrzej Siewior list_for_each_entry_safe(f, tmp, &c->functions, list) { 126488af8bbeSSebastian Andrzej Siewior 126588af8bbeSSebastian Andrzej Siewior list_move_tail(&f->list, &cfg->func_list); 126688af8bbeSSebastian Andrzej Siewior if (f->unbind) { 1267da7b895dSRomain Izard dev_dbg(&gi->cdev.gadget->dev, 1268a08f5dbfSRomain Izard "unbind function '%s'/%p\n", 1269a08f5dbfSRomain Izard f->name, f); 127088af8bbeSSebastian Andrzej Siewior f->unbind(c, f); 127188af8bbeSSebastian Andrzej Siewior } 127288af8bbeSSebastian Andrzej Siewior } 127388af8bbeSSebastian Andrzej Siewior c->next_interface_id = 0; 1274903124feSKrzysztof Opasiak memset(c->interface, 0, sizeof(c->interface)); 1275554eead5SJohn Youn c->superspeed_plus = 0; 127688af8bbeSSebastian Andrzej Siewior c->superspeed = 0; 127788af8bbeSSebastian Andrzej Siewior c->highspeed = 0; 127888af8bbeSSebastian Andrzej Siewior c->fullspeed = 0; 127988af8bbeSSebastian Andrzej Siewior } 128088af8bbeSSebastian Andrzej Siewior } 128188af8bbeSSebastian Andrzej Siewior 128288af8bbeSSebastian Andrzej Siewior static int configfs_composite_bind(struct usb_gadget *gadget, 128388af8bbeSSebastian Andrzej Siewior struct usb_gadget_driver *gdriver) 128488af8bbeSSebastian Andrzej Siewior { 128588af8bbeSSebastian Andrzej Siewior struct usb_composite_driver *composite = to_cdriver(gdriver); 128688af8bbeSSebastian Andrzej Siewior struct gadget_info *gi = container_of(composite, 128788af8bbeSSebastian Andrzej Siewior struct gadget_info, composite); 128888af8bbeSSebastian Andrzej Siewior struct usb_composite_dev *cdev = &gi->cdev; 128988af8bbeSSebastian Andrzej Siewior struct usb_configuration *c; 129088af8bbeSSebastian Andrzej Siewior struct usb_string *s; 129188af8bbeSSebastian Andrzej Siewior unsigned i; 129288af8bbeSSebastian Andrzej Siewior int ret; 129388af8bbeSSebastian Andrzej Siewior 129488af8bbeSSebastian Andrzej Siewior /* the gi->lock is hold by the caller */ 12951a1c851bSPeter Chen gi->unbind = 0; 129688af8bbeSSebastian Andrzej Siewior cdev->gadget = gadget; 129788af8bbeSSebastian Andrzej Siewior set_gadget_data(gadget, cdev); 129888af8bbeSSebastian Andrzej Siewior ret = composite_dev_prepare(composite, cdev); 129988af8bbeSSebastian Andrzej Siewior if (ret) 130088af8bbeSSebastian Andrzej Siewior return ret; 130188af8bbeSSebastian Andrzej Siewior /* and now the gadget bind */ 130288af8bbeSSebastian Andrzej Siewior ret = -EINVAL; 130388af8bbeSSebastian Andrzej Siewior 130488af8bbeSSebastian Andrzej Siewior if (list_empty(&gi->cdev.configs)) { 130588af8bbeSSebastian Andrzej Siewior pr_err("Need at least one configuration in %s.\n", 130688af8bbeSSebastian Andrzej Siewior gi->composite.name); 130788af8bbeSSebastian Andrzej Siewior goto err_comp_cleanup; 130888af8bbeSSebastian Andrzej Siewior } 130988af8bbeSSebastian Andrzej Siewior 131088af8bbeSSebastian Andrzej Siewior 131188af8bbeSSebastian Andrzej Siewior list_for_each_entry(c, &gi->cdev.configs, list) { 131288af8bbeSSebastian Andrzej Siewior struct config_usb_cfg *cfg; 131388af8bbeSSebastian Andrzej Siewior 131488af8bbeSSebastian Andrzej Siewior cfg = container_of(c, struct config_usb_cfg, c); 131588af8bbeSSebastian Andrzej Siewior if (list_empty(&cfg->func_list)) { 131688af8bbeSSebastian Andrzej Siewior pr_err("Config %s/%d of %s needs at least one function.\n", 131788af8bbeSSebastian Andrzej Siewior c->label, c->bConfigurationValue, 131888af8bbeSSebastian Andrzej Siewior gi->composite.name); 131988af8bbeSSebastian Andrzej Siewior goto err_comp_cleanup; 132088af8bbeSSebastian Andrzej Siewior } 132188af8bbeSSebastian Andrzej Siewior } 132288af8bbeSSebastian Andrzej Siewior 132388af8bbeSSebastian Andrzej Siewior /* init all strings */ 132488af8bbeSSebastian Andrzej Siewior if (!list_empty(&gi->string_list)) { 132588af8bbeSSebastian Andrzej Siewior struct gadget_strings *gs; 132688af8bbeSSebastian Andrzej Siewior 132788af8bbeSSebastian Andrzej Siewior i = 0; 132888af8bbeSSebastian Andrzej Siewior list_for_each_entry(gs, &gi->string_list, list) { 132988af8bbeSSebastian Andrzej Siewior 133088af8bbeSSebastian Andrzej Siewior gi->gstrings[i] = &gs->stringtab_dev; 133188af8bbeSSebastian Andrzej Siewior gs->stringtab_dev.strings = gs->strings; 133288af8bbeSSebastian Andrzej Siewior gs->strings[USB_GADGET_MANUFACTURER_IDX].s = 133388af8bbeSSebastian Andrzej Siewior gs->manufacturer; 133488af8bbeSSebastian Andrzej Siewior gs->strings[USB_GADGET_PRODUCT_IDX].s = gs->product; 133588af8bbeSSebastian Andrzej Siewior gs->strings[USB_GADGET_SERIAL_IDX].s = gs->serialnumber; 133688af8bbeSSebastian Andrzej Siewior i++; 133788af8bbeSSebastian Andrzej Siewior } 133888af8bbeSSebastian Andrzej Siewior gi->gstrings[i] = NULL; 133988af8bbeSSebastian Andrzej Siewior s = usb_gstrings_attach(&gi->cdev, gi->gstrings, 134088af8bbeSSebastian Andrzej Siewior USB_GADGET_FIRST_AVAIL_IDX); 1341fea77077SWei Yongjun if (IS_ERR(s)) { 1342fea77077SWei Yongjun ret = PTR_ERR(s); 134388af8bbeSSebastian Andrzej Siewior goto err_comp_cleanup; 1344fea77077SWei Yongjun } 134588af8bbeSSebastian Andrzej Siewior 134688af8bbeSSebastian Andrzej Siewior gi->cdev.desc.iManufacturer = s[USB_GADGET_MANUFACTURER_IDX].id; 134788af8bbeSSebastian Andrzej Siewior gi->cdev.desc.iProduct = s[USB_GADGET_PRODUCT_IDX].id; 134888af8bbeSSebastian Andrzej Siewior gi->cdev.desc.iSerialNumber = s[USB_GADGET_SERIAL_IDX].id; 134988af8bbeSSebastian Andrzej Siewior } 135088af8bbeSSebastian Andrzej Siewior 135187213d38SAndrzej Pietrasiewicz if (gi->use_os_desc) { 135287213d38SAndrzej Pietrasiewicz cdev->use_os_string = true; 135387213d38SAndrzej Pietrasiewicz cdev->b_vendor_code = gi->b_vendor_code; 135487213d38SAndrzej Pietrasiewicz memcpy(cdev->qw_sign, gi->qw_sign, OS_STRING_QW_SIGN_LEN); 135587213d38SAndrzej Pietrasiewicz } 135687213d38SAndrzej Pietrasiewicz 135741ce84c8SLi Jun if (gadget_is_otg(gadget) && !otg_desc[0]) { 135841ce84c8SLi Jun struct usb_descriptor_header *usb_desc; 135941ce84c8SLi Jun 136041ce84c8SLi Jun usb_desc = usb_otg_descriptor_alloc(gadget); 136141ce84c8SLi Jun if (!usb_desc) { 136241ce84c8SLi Jun ret = -ENOMEM; 136341ce84c8SLi Jun goto err_comp_cleanup; 136441ce84c8SLi Jun } 136541ce84c8SLi Jun usb_otg_descriptor_init(gadget, usb_desc); 136641ce84c8SLi Jun otg_desc[0] = usb_desc; 136741ce84c8SLi Jun otg_desc[1] = NULL; 136841ce84c8SLi Jun } 136941ce84c8SLi Jun 137088af8bbeSSebastian Andrzej Siewior /* Go through all configs, attach all functions */ 137188af8bbeSSebastian Andrzej Siewior list_for_each_entry(c, &gi->cdev.configs, list) { 137288af8bbeSSebastian Andrzej Siewior struct config_usb_cfg *cfg; 137388af8bbeSSebastian Andrzej Siewior struct usb_function *f; 137488af8bbeSSebastian Andrzej Siewior struct usb_function *tmp; 137588af8bbeSSebastian Andrzej Siewior struct gadget_config_name *cn; 137688af8bbeSSebastian Andrzej Siewior 137741ce84c8SLi Jun if (gadget_is_otg(gadget)) 137841ce84c8SLi Jun c->descriptors = otg_desc; 137941ce84c8SLi Jun 138088af8bbeSSebastian Andrzej Siewior cfg = container_of(c, struct config_usb_cfg, c); 138188af8bbeSSebastian Andrzej Siewior if (!list_empty(&cfg->string_list)) { 138288af8bbeSSebastian Andrzej Siewior i = 0; 138388af8bbeSSebastian Andrzej Siewior list_for_each_entry(cn, &cfg->string_list, list) { 138488af8bbeSSebastian Andrzej Siewior cfg->gstrings[i] = &cn->stringtab_dev; 138588af8bbeSSebastian Andrzej Siewior cn->stringtab_dev.strings = &cn->strings; 138688af8bbeSSebastian Andrzej Siewior cn->strings.s = cn->configuration; 138788af8bbeSSebastian Andrzej Siewior i++; 138888af8bbeSSebastian Andrzej Siewior } 138988af8bbeSSebastian Andrzej Siewior cfg->gstrings[i] = NULL; 139088af8bbeSSebastian Andrzej Siewior s = usb_gstrings_attach(&gi->cdev, cfg->gstrings, 1); 1391fea77077SWei Yongjun if (IS_ERR(s)) { 1392fea77077SWei Yongjun ret = PTR_ERR(s); 139388af8bbeSSebastian Andrzej Siewior goto err_comp_cleanup; 1394fea77077SWei Yongjun } 139588af8bbeSSebastian Andrzej Siewior c->iConfiguration = s[0].id; 139688af8bbeSSebastian Andrzej Siewior } 139788af8bbeSSebastian Andrzej Siewior 139888af8bbeSSebastian Andrzej Siewior list_for_each_entry_safe(f, tmp, &cfg->func_list, list) { 139988af8bbeSSebastian Andrzej Siewior list_del(&f->list); 140088af8bbeSSebastian Andrzej Siewior ret = usb_add_function(c, f); 14015a68e9b5SAndrzej Pietrasiewicz if (ret) { 14025a68e9b5SAndrzej Pietrasiewicz list_add(&f->list, &cfg->func_list); 140388af8bbeSSebastian Andrzej Siewior goto err_purge_funcs; 140488af8bbeSSebastian Andrzej Siewior } 14055a68e9b5SAndrzej Pietrasiewicz } 140688af8bbeSSebastian Andrzej Siewior usb_ep_autoconfig_reset(cdev->gadget); 140788af8bbeSSebastian Andrzej Siewior } 1408da424314SAndrzej Pietrasiewicz if (cdev->use_os_string) { 1409da424314SAndrzej Pietrasiewicz ret = composite_os_desc_req_prepare(cdev, gadget->ep0); 1410da424314SAndrzej Pietrasiewicz if (ret) 1411da424314SAndrzej Pietrasiewicz goto err_purge_funcs; 1412da424314SAndrzej Pietrasiewicz } 1413da424314SAndrzej Pietrasiewicz 141488af8bbeSSebastian Andrzej Siewior usb_ep_autoconfig_reset(cdev->gadget); 141588af8bbeSSebastian Andrzej Siewior return 0; 141688af8bbeSSebastian Andrzej Siewior 141788af8bbeSSebastian Andrzej Siewior err_purge_funcs: 141888af8bbeSSebastian Andrzej Siewior purge_configs_funcs(gi); 141988af8bbeSSebastian Andrzej Siewior err_comp_cleanup: 142088af8bbeSSebastian Andrzej Siewior composite_dev_cleanup(cdev); 142188af8bbeSSebastian Andrzej Siewior return ret; 142288af8bbeSSebastian Andrzej Siewior } 142388af8bbeSSebastian Andrzej Siewior 142488af8bbeSSebastian Andrzej Siewior static void configfs_composite_unbind(struct usb_gadget *gadget) 142588af8bbeSSebastian Andrzej Siewior { 142688af8bbeSSebastian Andrzej Siewior struct usb_composite_dev *cdev; 142788af8bbeSSebastian Andrzej Siewior struct gadget_info *gi; 14281a1c851bSPeter Chen unsigned long flags; 142988af8bbeSSebastian Andrzej Siewior 143088af8bbeSSebastian Andrzej Siewior /* the gi->lock is hold by the caller */ 143188af8bbeSSebastian Andrzej Siewior 143288af8bbeSSebastian Andrzej Siewior cdev = get_gadget_data(gadget); 143388af8bbeSSebastian Andrzej Siewior gi = container_of(cdev, struct gadget_info, cdev); 14341a1c851bSPeter Chen spin_lock_irqsave(&gi->spinlock, flags); 14351a1c851bSPeter Chen gi->unbind = 1; 14361a1c851bSPeter Chen spin_unlock_irqrestore(&gi->spinlock, flags); 143788af8bbeSSebastian Andrzej Siewior 143841ce84c8SLi Jun kfree(otg_desc[0]); 143941ce84c8SLi Jun otg_desc[0] = NULL; 144088af8bbeSSebastian Andrzej Siewior purge_configs_funcs(gi); 144188af8bbeSSebastian Andrzej Siewior composite_dev_cleanup(cdev); 144288af8bbeSSebastian Andrzej Siewior usb_ep_autoconfig_reset(cdev->gadget); 14431a1c851bSPeter Chen spin_lock_irqsave(&gi->spinlock, flags); 144488af8bbeSSebastian Andrzej Siewior cdev->gadget = NULL; 144588af8bbeSSebastian Andrzej Siewior set_gadget_data(gadget, NULL); 14461a1c851bSPeter Chen spin_unlock_irqrestore(&gi->spinlock, flags); 14471a1c851bSPeter Chen } 14481a1c851bSPeter Chen 14491a1c851bSPeter Chen static int configfs_composite_setup(struct usb_gadget *gadget, 14501a1c851bSPeter Chen const struct usb_ctrlrequest *ctrl) 14511a1c851bSPeter Chen { 14521a1c851bSPeter Chen struct usb_composite_dev *cdev; 14531a1c851bSPeter Chen struct gadget_info *gi; 14541a1c851bSPeter Chen unsigned long flags; 14551a1c851bSPeter Chen int ret; 14561a1c851bSPeter Chen 14571a1c851bSPeter Chen cdev = get_gadget_data(gadget); 14581a1c851bSPeter Chen if (!cdev) 14591a1c851bSPeter Chen return 0; 14601a1c851bSPeter Chen 14611a1c851bSPeter Chen gi = container_of(cdev, struct gadget_info, cdev); 14621a1c851bSPeter Chen spin_lock_irqsave(&gi->spinlock, flags); 14631a1c851bSPeter Chen cdev = get_gadget_data(gadget); 14641a1c851bSPeter Chen if (!cdev || gi->unbind) { 14651a1c851bSPeter Chen spin_unlock_irqrestore(&gi->spinlock, flags); 14661a1c851bSPeter Chen return 0; 14671a1c851bSPeter Chen } 14681a1c851bSPeter Chen 14691a1c851bSPeter Chen ret = composite_setup(gadget, ctrl); 14701a1c851bSPeter Chen spin_unlock_irqrestore(&gi->spinlock, flags); 14711a1c851bSPeter Chen return ret; 14721a1c851bSPeter Chen } 14731a1c851bSPeter Chen 14741a1c851bSPeter Chen static void configfs_composite_disconnect(struct usb_gadget *gadget) 14751a1c851bSPeter Chen { 14761a1c851bSPeter Chen struct usb_composite_dev *cdev; 14771a1c851bSPeter Chen struct gadget_info *gi; 14781a1c851bSPeter Chen unsigned long flags; 14791a1c851bSPeter Chen 14801a1c851bSPeter Chen cdev = get_gadget_data(gadget); 14811a1c851bSPeter Chen if (!cdev) 14821a1c851bSPeter Chen return; 14831a1c851bSPeter Chen 14841a1c851bSPeter Chen gi = container_of(cdev, struct gadget_info, cdev); 14851a1c851bSPeter Chen spin_lock_irqsave(&gi->spinlock, flags); 14861a1c851bSPeter Chen cdev = get_gadget_data(gadget); 14871a1c851bSPeter Chen if (!cdev || gi->unbind) { 14881a1c851bSPeter Chen spin_unlock_irqrestore(&gi->spinlock, flags); 14891a1c851bSPeter Chen return; 14901a1c851bSPeter Chen } 14911a1c851bSPeter Chen 14921a1c851bSPeter Chen composite_disconnect(gadget); 14931a1c851bSPeter Chen spin_unlock_irqrestore(&gi->spinlock, flags); 14941a1c851bSPeter Chen } 14951a1c851bSPeter Chen 14961a1c851bSPeter Chen static void configfs_composite_suspend(struct usb_gadget *gadget) 14971a1c851bSPeter Chen { 14981a1c851bSPeter Chen struct usb_composite_dev *cdev; 14991a1c851bSPeter Chen struct gadget_info *gi; 15001a1c851bSPeter Chen unsigned long flags; 15011a1c851bSPeter Chen 15021a1c851bSPeter Chen cdev = get_gadget_data(gadget); 15031a1c851bSPeter Chen if (!cdev) 15041a1c851bSPeter Chen return; 15051a1c851bSPeter Chen 15061a1c851bSPeter Chen gi = container_of(cdev, struct gadget_info, cdev); 15071a1c851bSPeter Chen spin_lock_irqsave(&gi->spinlock, flags); 15081a1c851bSPeter Chen cdev = get_gadget_data(gadget); 15091a1c851bSPeter Chen if (!cdev || gi->unbind) { 15101a1c851bSPeter Chen spin_unlock_irqrestore(&gi->spinlock, flags); 15111a1c851bSPeter Chen return; 15121a1c851bSPeter Chen } 15131a1c851bSPeter Chen 15141a1c851bSPeter Chen composite_suspend(gadget); 15151a1c851bSPeter Chen spin_unlock_irqrestore(&gi->spinlock, flags); 15161a1c851bSPeter Chen } 15171a1c851bSPeter Chen 15181a1c851bSPeter Chen static void configfs_composite_resume(struct usb_gadget *gadget) 15191a1c851bSPeter Chen { 15201a1c851bSPeter Chen struct usb_composite_dev *cdev; 15211a1c851bSPeter Chen struct gadget_info *gi; 15221a1c851bSPeter Chen unsigned long flags; 15231a1c851bSPeter Chen 15241a1c851bSPeter Chen cdev = get_gadget_data(gadget); 15251a1c851bSPeter Chen if (!cdev) 15261a1c851bSPeter Chen return; 15271a1c851bSPeter Chen 15281a1c851bSPeter Chen gi = container_of(cdev, struct gadget_info, cdev); 15291a1c851bSPeter Chen spin_lock_irqsave(&gi->spinlock, flags); 15301a1c851bSPeter Chen cdev = get_gadget_data(gadget); 15311a1c851bSPeter Chen if (!cdev || gi->unbind) { 15321a1c851bSPeter Chen spin_unlock_irqrestore(&gi->spinlock, flags); 15331a1c851bSPeter Chen return; 15341a1c851bSPeter Chen } 15351a1c851bSPeter Chen 15361a1c851bSPeter Chen composite_resume(gadget); 15371a1c851bSPeter Chen spin_unlock_irqrestore(&gi->spinlock, flags); 153888af8bbeSSebastian Andrzej Siewior } 153988af8bbeSSebastian Andrzej Siewior 154088af8bbeSSebastian Andrzej Siewior static const struct usb_gadget_driver configfs_driver_template = { 154188af8bbeSSebastian Andrzej Siewior .bind = configfs_composite_bind, 154288af8bbeSSebastian Andrzej Siewior .unbind = configfs_composite_unbind, 154388af8bbeSSebastian Andrzej Siewior 15441a1c851bSPeter Chen .setup = configfs_composite_setup, 15451a1c851bSPeter Chen .reset = configfs_composite_disconnect, 15461a1c851bSPeter Chen .disconnect = configfs_composite_disconnect, 154788af8bbeSSebastian Andrzej Siewior 15481a1c851bSPeter Chen .suspend = configfs_composite_suspend, 15491a1c851bSPeter Chen .resume = configfs_composite_resume, 15503a571870SAndrzej Pietrasiewicz 155188af8bbeSSebastian Andrzej Siewior .max_speed = USB_SPEED_SUPER, 155288af8bbeSSebastian Andrzej Siewior .driver = { 155388af8bbeSSebastian Andrzej Siewior .owner = THIS_MODULE, 155488af8bbeSSebastian Andrzej Siewior .name = "configfs-gadget", 155588af8bbeSSebastian Andrzej Siewior }, 1556f1bddbb3SKrzysztof Opasiak .match_existing_only = 1, 155788af8bbeSSebastian Andrzej Siewior }; 155888af8bbeSSebastian Andrzej Siewior 155988af8bbeSSebastian Andrzej Siewior static struct config_group *gadgets_make( 156088af8bbeSSebastian Andrzej Siewior struct config_group *group, 156188af8bbeSSebastian Andrzej Siewior const char *name) 156288af8bbeSSebastian Andrzej Siewior { 156388af8bbeSSebastian Andrzej Siewior struct gadget_info *gi; 156488af8bbeSSebastian Andrzej Siewior 156588af8bbeSSebastian Andrzej Siewior gi = kzalloc(sizeof(*gi), GFP_KERNEL); 156688af8bbeSSebastian Andrzej Siewior if (!gi) 156788af8bbeSSebastian Andrzej Siewior return ERR_PTR(-ENOMEM); 156888af8bbeSSebastian Andrzej Siewior 15691ae1602dSChristoph Hellwig config_group_init_type_name(&gi->group, name, &gadget_root_type); 157088af8bbeSSebastian Andrzej Siewior 157188af8bbeSSebastian Andrzej Siewior config_group_init_type_name(&gi->functions_group, "functions", 157288af8bbeSSebastian Andrzej Siewior &functions_type); 15731ae1602dSChristoph Hellwig configfs_add_default_group(&gi->functions_group, &gi->group); 15741ae1602dSChristoph Hellwig 157588af8bbeSSebastian Andrzej Siewior config_group_init_type_name(&gi->configs_group, "configs", 157688af8bbeSSebastian Andrzej Siewior &config_desc_type); 15771ae1602dSChristoph Hellwig configfs_add_default_group(&gi->configs_group, &gi->group); 15781ae1602dSChristoph Hellwig 157988af8bbeSSebastian Andrzej Siewior config_group_init_type_name(&gi->strings_group, "strings", 158088af8bbeSSebastian Andrzej Siewior &gadget_strings_strings_type); 15811ae1602dSChristoph Hellwig configfs_add_default_group(&gi->strings_group, &gi->group); 15821ae1602dSChristoph Hellwig 158387213d38SAndrzej Pietrasiewicz config_group_init_type_name(&gi->os_desc_group, "os_desc", 158487213d38SAndrzej Pietrasiewicz &os_desc_type); 15851ae1602dSChristoph Hellwig configfs_add_default_group(&gi->os_desc_group, &gi->group); 158688af8bbeSSebastian Andrzej Siewior 158788af8bbeSSebastian Andrzej Siewior gi->composite.bind = configfs_do_nothing; 158888af8bbeSSebastian Andrzej Siewior gi->composite.unbind = configfs_do_nothing; 158988af8bbeSSebastian Andrzej Siewior gi->composite.suspend = NULL; 159088af8bbeSSebastian Andrzej Siewior gi->composite.resume = NULL; 159188af8bbeSSebastian Andrzej Siewior gi->composite.max_speed = USB_SPEED_SUPER; 159288af8bbeSSebastian Andrzej Siewior 1593093edc2bSWei Yongjun spin_lock_init(&gi->spinlock); 159488af8bbeSSebastian Andrzej Siewior mutex_init(&gi->lock); 159588af8bbeSSebastian Andrzej Siewior INIT_LIST_HEAD(&gi->string_list); 159688af8bbeSSebastian Andrzej Siewior INIT_LIST_HEAD(&gi->available_func); 159788af8bbeSSebastian Andrzej Siewior 159888af8bbeSSebastian Andrzej Siewior composite_init_dev(&gi->cdev); 159988af8bbeSSebastian Andrzej Siewior gi->cdev.desc.bLength = USB_DT_DEVICE_SIZE; 160088af8bbeSSebastian Andrzej Siewior gi->cdev.desc.bDescriptorType = USB_DT_DEVICE; 160188af8bbeSSebastian Andrzej Siewior gi->cdev.desc.bcdDevice = cpu_to_le16(get_default_bcdDevice()); 160288af8bbeSSebastian Andrzej Siewior 160388af8bbeSSebastian Andrzej Siewior gi->composite.gadget_driver = configfs_driver_template; 160488af8bbeSSebastian Andrzej Siewior 160588af8bbeSSebastian Andrzej Siewior gi->composite.gadget_driver.function = kstrdup(name, GFP_KERNEL); 160688af8bbeSSebastian Andrzej Siewior gi->composite.name = gi->composite.gadget_driver.function; 160788af8bbeSSebastian Andrzej Siewior 160888af8bbeSSebastian Andrzej Siewior if (!gi->composite.gadget_driver.function) 160988af8bbeSSebastian Andrzej Siewior goto err; 161088af8bbeSSebastian Andrzej Siewior 161188af8bbeSSebastian Andrzej Siewior return &gi->group; 161288af8bbeSSebastian Andrzej Siewior err: 161388af8bbeSSebastian Andrzej Siewior kfree(gi); 161488af8bbeSSebastian Andrzej Siewior return ERR_PTR(-ENOMEM); 161588af8bbeSSebastian Andrzej Siewior } 161688af8bbeSSebastian Andrzej Siewior 161788af8bbeSSebastian Andrzej Siewior static void gadgets_drop(struct config_group *group, struct config_item *item) 161888af8bbeSSebastian Andrzej Siewior { 161988af8bbeSSebastian Andrzej Siewior config_item_put(item); 162088af8bbeSSebastian Andrzej Siewior } 162188af8bbeSSebastian Andrzej Siewior 162288af8bbeSSebastian Andrzej Siewior static struct configfs_group_operations gadgets_ops = { 162388af8bbeSSebastian Andrzej Siewior .make_group = &gadgets_make, 162488af8bbeSSebastian Andrzej Siewior .drop_item = &gadgets_drop, 162588af8bbeSSebastian Andrzej Siewior }; 162688af8bbeSSebastian Andrzej Siewior 16274ad01412SBhumika Goyal static const struct config_item_type gadgets_type = { 162888af8bbeSSebastian Andrzej Siewior .ct_group_ops = &gadgets_ops, 162988af8bbeSSebastian Andrzej Siewior .ct_owner = THIS_MODULE, 163088af8bbeSSebastian Andrzej Siewior }; 163188af8bbeSSebastian Andrzej Siewior 163288af8bbeSSebastian Andrzej Siewior static struct configfs_subsystem gadget_subsys = { 163388af8bbeSSebastian Andrzej Siewior .su_group = { 163488af8bbeSSebastian Andrzej Siewior .cg_item = { 163588af8bbeSSebastian Andrzej Siewior .ci_namebuf = "usb_gadget", 163688af8bbeSSebastian Andrzej Siewior .ci_type = &gadgets_type, 163788af8bbeSSebastian Andrzej Siewior }, 163888af8bbeSSebastian Andrzej Siewior }, 163988af8bbeSSebastian Andrzej Siewior .su_mutex = __MUTEX_INITIALIZER(gadget_subsys.su_mutex), 164088af8bbeSSebastian Andrzej Siewior }; 164188af8bbeSSebastian Andrzej Siewior 1642092a4bd0SAndrzej Pietrasiewicz void unregister_gadget_item(struct config_item *item) 1643092a4bd0SAndrzej Pietrasiewicz { 1644092a4bd0SAndrzej Pietrasiewicz struct gadget_info *gi = to_gadget_info(item); 1645092a4bd0SAndrzej Pietrasiewicz 1646cee51c33SWinter Wang mutex_lock(&gi->lock); 1647092a4bd0SAndrzej Pietrasiewicz unregister_gadget(gi); 1648cee51c33SWinter Wang mutex_unlock(&gi->lock); 1649092a4bd0SAndrzej Pietrasiewicz } 16500700faafSFelipe Balbi EXPORT_SYMBOL_GPL(unregister_gadget_item); 1651092a4bd0SAndrzej Pietrasiewicz 165288af8bbeSSebastian Andrzej Siewior static int __init gadget_cfs_init(void) 165388af8bbeSSebastian Andrzej Siewior { 165488af8bbeSSebastian Andrzej Siewior int ret; 165588af8bbeSSebastian Andrzej Siewior 165688af8bbeSSebastian Andrzej Siewior config_group_init(&gadget_subsys.su_group); 165788af8bbeSSebastian Andrzej Siewior 165888af8bbeSSebastian Andrzej Siewior ret = configfs_register_subsystem(&gadget_subsys); 165988af8bbeSSebastian Andrzej Siewior return ret; 166088af8bbeSSebastian Andrzej Siewior } 166188af8bbeSSebastian Andrzej Siewior module_init(gadget_cfs_init); 166288af8bbeSSebastian Andrzej Siewior 166388af8bbeSSebastian Andrzej Siewior static void __exit gadget_cfs_exit(void) 166488af8bbeSSebastian Andrzej Siewior { 166588af8bbeSSebastian Andrzej Siewior configfs_unregister_subsystem(&gadget_subsys); 166688af8bbeSSebastian Andrzej Siewior } 166788af8bbeSSebastian Andrzej Siewior module_exit(gadget_cfs_exit); 1668