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> 6a8bc8cc1SChristophe JAILLET #include <linux/kstrtox.h> 787213d38SAndrzej Pietrasiewicz #include <linux/nls.h> 888af8bbeSSebastian Andrzej Siewior #include <linux/usb/composite.h> 988af8bbeSSebastian Andrzej Siewior #include <linux/usb/gadget_configfs.h> 1093c47394SJó Ágila Bitsch #include <linux/usb/webusb.h> 110009e99aSRashika Kheria #include "configfs.h" 12da424314SAndrzej Pietrasiewicz #include "u_f.h" 137419485fSAndrzej Pietrasiewicz #include "u_os_desc.h" 1488af8bbeSSebastian Andrzej Siewior 1588af8bbeSSebastian Andrzej Siewior int check_user_usb_string(const char *name, 1688af8bbeSSebastian Andrzej Siewior struct usb_gadget_strings *stringtab_dev) 1788af8bbeSSebastian Andrzej Siewior { 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 2517309a6aSTao Ren if (!usb_validate_langid(num)) 2688af8bbeSSebastian Andrzej Siewior return -EINVAL; 2788af8bbeSSebastian Andrzej Siewior 2888af8bbeSSebastian Andrzej Siewior stringtab_dev->language = num; 2988af8bbeSSebastian Andrzej Siewior return 0; 3088af8bbeSSebastian Andrzej Siewior } 3188af8bbeSSebastian Andrzej Siewior 3288af8bbeSSebastian Andrzej Siewior #define MAX_NAME_LEN 40 3388af8bbeSSebastian Andrzej Siewior #define MAX_USB_STRING_LANGS 2 3488af8bbeSSebastian Andrzej Siewior 3541ce84c8SLi Jun static const struct usb_descriptor_header *otg_desc[2]; 3641ce84c8SLi Jun 3788af8bbeSSebastian Andrzej Siewior struct gadget_info { 3888af8bbeSSebastian Andrzej Siewior struct config_group group; 3988af8bbeSSebastian Andrzej Siewior struct config_group functions_group; 4088af8bbeSSebastian Andrzej Siewior struct config_group configs_group; 4188af8bbeSSebastian Andrzej Siewior struct config_group strings_group; 4287213d38SAndrzej Pietrasiewicz struct config_group os_desc_group; 4393c47394SJó Ágila Bitsch struct config_group webusb_group; 4488af8bbeSSebastian Andrzej Siewior 4588af8bbeSSebastian Andrzej Siewior struct mutex lock; 4688af8bbeSSebastian Andrzej Siewior struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1]; 4788af8bbeSSebastian Andrzej Siewior struct list_head string_list; 4888af8bbeSSebastian Andrzej Siewior struct list_head available_func; 4988af8bbeSSebastian Andrzej Siewior 5088af8bbeSSebastian Andrzej Siewior struct usb_composite_driver composite; 5188af8bbeSSebastian Andrzej Siewior struct usb_composite_dev cdev; 5287213d38SAndrzej Pietrasiewicz bool use_os_desc; 5387213d38SAndrzej Pietrasiewicz char b_vendor_code; 5487213d38SAndrzej Pietrasiewicz char qw_sign[OS_STRING_QW_SIGN_LEN]; 5593c47394SJó Ágila Bitsch bool use_webusb; 5693c47394SJó Ágila Bitsch u16 bcd_webusb_version; 5793c47394SJó Ágila Bitsch u8 b_webusb_vendor_code; 5893c47394SJó Ágila Bitsch char landing_page[WEBUSB_URL_RAW_MAX_LENGTH]; 5993c47394SJó Ágila Bitsch 601a1c851bSPeter Chen spinlock_t spinlock; 611a1c851bSPeter Chen bool unbind; 6288af8bbeSSebastian Andrzej Siewior }; 6388af8bbeSSebastian Andrzej Siewior 6445b6a73fSChristoph Hellwig static inline struct gadget_info *to_gadget_info(struct config_item *item) 6545b6a73fSChristoph Hellwig { 6645b6a73fSChristoph Hellwig return container_of(to_config_group(item), struct gadget_info, group); 6745b6a73fSChristoph Hellwig } 6845b6a73fSChristoph Hellwig 6988af8bbeSSebastian Andrzej Siewior struct config_usb_cfg { 7088af8bbeSSebastian Andrzej Siewior struct config_group group; 7188af8bbeSSebastian Andrzej Siewior struct config_group strings_group; 7288af8bbeSSebastian Andrzej Siewior struct list_head string_list; 7388af8bbeSSebastian Andrzej Siewior struct usb_configuration c; 7488af8bbeSSebastian Andrzej Siewior struct list_head func_list; 7588af8bbeSSebastian Andrzej Siewior struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1]; 7688af8bbeSSebastian Andrzej Siewior }; 7788af8bbeSSebastian Andrzej Siewior 7845b6a73fSChristoph Hellwig static inline struct config_usb_cfg *to_config_usb_cfg(struct config_item *item) 7945b6a73fSChristoph Hellwig { 8045b6a73fSChristoph Hellwig return container_of(to_config_group(item), struct config_usb_cfg, 8145b6a73fSChristoph Hellwig group); 8245b6a73fSChristoph Hellwig } 8345b6a73fSChristoph Hellwig 84260d88b7SLinyu Yuan static inline struct gadget_info *cfg_to_gadget_info(struct config_usb_cfg *cfg) 85260d88b7SLinyu Yuan { 86260d88b7SLinyu Yuan return container_of(cfg->c.cdev, struct gadget_info, cdev); 87260d88b7SLinyu Yuan } 88260d88b7SLinyu Yuan 89*6e2a512dSDaniel Scally struct gadget_language { 9088af8bbeSSebastian Andrzej Siewior struct usb_gadget_strings stringtab_dev; 9188af8bbeSSebastian Andrzej Siewior struct usb_string strings[USB_GADGET_FIRST_AVAIL_IDX]; 9288af8bbeSSebastian Andrzej Siewior char *manufacturer; 9388af8bbeSSebastian Andrzej Siewior char *product; 9488af8bbeSSebastian Andrzej Siewior char *serialnumber; 9588af8bbeSSebastian Andrzej Siewior 9688af8bbeSSebastian Andrzej Siewior struct config_group group; 9788af8bbeSSebastian Andrzej Siewior struct list_head list; 9888af8bbeSSebastian Andrzej Siewior }; 9988af8bbeSSebastian Andrzej Siewior 10088af8bbeSSebastian Andrzej Siewior struct gadget_config_name { 10188af8bbeSSebastian Andrzej Siewior struct usb_gadget_strings stringtab_dev; 10288af8bbeSSebastian Andrzej Siewior struct usb_string strings; 10388af8bbeSSebastian Andrzej Siewior char *configuration; 10488af8bbeSSebastian Andrzej Siewior 10588af8bbeSSebastian Andrzej Siewior struct config_group group; 10688af8bbeSSebastian Andrzej Siewior struct list_head list; 10788af8bbeSSebastian Andrzej Siewior }; 10888af8bbeSSebastian Andrzej Siewior 10998f153a1SJim Lin #define USB_MAX_STRING_WITH_NULL_LEN (USB_MAX_STRING_LEN+1) 11098f153a1SJim Lin 11188af8bbeSSebastian Andrzej Siewior static int usb_string_copy(const char *s, char **s_copy) 11288af8bbeSSebastian Andrzej Siewior { 11388af8bbeSSebastian Andrzej Siewior int ret; 11488af8bbeSSebastian Andrzej Siewior char *str; 11588af8bbeSSebastian Andrzej Siewior char *copy = *s_copy; 11688af8bbeSSebastian Andrzej Siewior ret = strlen(s); 11781c74628SMacpaul Lin if (ret > USB_MAX_STRING_LEN) 11888af8bbeSSebastian Andrzej Siewior return -EOVERFLOW; 11988af8bbeSSebastian Andrzej Siewior 12098f153a1SJim Lin if (copy) { 12198f153a1SJim Lin str = copy; 12298f153a1SJim Lin } else { 12398f153a1SJim Lin str = kmalloc(USB_MAX_STRING_WITH_NULL_LEN, GFP_KERNEL); 12488af8bbeSSebastian Andrzej Siewior if (!str) 12588af8bbeSSebastian Andrzej Siewior return -ENOMEM; 12698f153a1SJim Lin } 12798f153a1SJim Lin strcpy(str, s); 12888af8bbeSSebastian Andrzej Siewior if (str[ret - 1] == '\n') 12988af8bbeSSebastian Andrzej Siewior str[ret - 1] = '\0'; 13088af8bbeSSebastian Andrzej Siewior *s_copy = str; 13188af8bbeSSebastian Andrzej Siewior return 0; 13288af8bbeSSebastian Andrzej Siewior } 13388af8bbeSSebastian Andrzej Siewior 13488af8bbeSSebastian Andrzej Siewior #define GI_DEVICE_DESC_SIMPLE_R_u8(__name) \ 13545b6a73fSChristoph Hellwig static ssize_t gadget_dev_desc_##__name##_show(struct config_item *item, \ 13688af8bbeSSebastian Andrzej Siewior char *page) \ 13788af8bbeSSebastian Andrzej Siewior { \ 13845b6a73fSChristoph Hellwig return sprintf(page, "0x%02x\n", \ 13945b6a73fSChristoph Hellwig to_gadget_info(item)->cdev.desc.__name); \ 14088af8bbeSSebastian Andrzej Siewior } 14188af8bbeSSebastian Andrzej Siewior 14288af8bbeSSebastian Andrzej Siewior #define GI_DEVICE_DESC_SIMPLE_R_u16(__name) \ 14345b6a73fSChristoph Hellwig static ssize_t gadget_dev_desc_##__name##_show(struct config_item *item, \ 14488af8bbeSSebastian Andrzej Siewior char *page) \ 14588af8bbeSSebastian Andrzej Siewior { \ 14645b6a73fSChristoph Hellwig return sprintf(page, "0x%04x\n", \ 14745b6a73fSChristoph Hellwig le16_to_cpup(&to_gadget_info(item)->cdev.desc.__name)); \ 14888af8bbeSSebastian Andrzej Siewior } 14988af8bbeSSebastian Andrzej Siewior 15088af8bbeSSebastian Andrzej Siewior 15188af8bbeSSebastian Andrzej Siewior #define GI_DEVICE_DESC_SIMPLE_W_u8(_name) \ 15245b6a73fSChristoph Hellwig static ssize_t gadget_dev_desc_##_name##_store(struct config_item *item, \ 15388af8bbeSSebastian Andrzej Siewior const char *page, size_t len) \ 15488af8bbeSSebastian Andrzej Siewior { \ 15588af8bbeSSebastian Andrzej Siewior u8 val; \ 15688af8bbeSSebastian Andrzej Siewior int ret; \ 15788af8bbeSSebastian Andrzej Siewior ret = kstrtou8(page, 0, &val); \ 15888af8bbeSSebastian Andrzej Siewior if (ret) \ 15988af8bbeSSebastian Andrzej Siewior return ret; \ 16045b6a73fSChristoph Hellwig to_gadget_info(item)->cdev.desc._name = val; \ 16188af8bbeSSebastian Andrzej Siewior return len; \ 16288af8bbeSSebastian Andrzej Siewior } 16388af8bbeSSebastian Andrzej Siewior 16488af8bbeSSebastian Andrzej Siewior #define GI_DEVICE_DESC_SIMPLE_W_u16(_name) \ 16545b6a73fSChristoph Hellwig static ssize_t gadget_dev_desc_##_name##_store(struct config_item *item, \ 16688af8bbeSSebastian Andrzej Siewior const char *page, size_t len) \ 16788af8bbeSSebastian Andrzej Siewior { \ 16888af8bbeSSebastian Andrzej Siewior u16 val; \ 16988af8bbeSSebastian Andrzej Siewior int ret; \ 17088af8bbeSSebastian Andrzej Siewior ret = kstrtou16(page, 0, &val); \ 17188af8bbeSSebastian Andrzej Siewior if (ret) \ 17288af8bbeSSebastian Andrzej Siewior return ret; \ 17345b6a73fSChristoph Hellwig to_gadget_info(item)->cdev.desc._name = cpu_to_le16p(&val); \ 17488af8bbeSSebastian Andrzej Siewior return len; \ 17588af8bbeSSebastian Andrzej Siewior } 17688af8bbeSSebastian Andrzej Siewior 17788af8bbeSSebastian Andrzej Siewior #define GI_DEVICE_DESC_SIMPLE_RW(_name, _type) \ 17888af8bbeSSebastian Andrzej Siewior GI_DEVICE_DESC_SIMPLE_R_##_type(_name) \ 17988af8bbeSSebastian Andrzej Siewior GI_DEVICE_DESC_SIMPLE_W_##_type(_name) 18088af8bbeSSebastian Andrzej Siewior 18188af8bbeSSebastian Andrzej Siewior GI_DEVICE_DESC_SIMPLE_R_u16(bcdUSB); 18288af8bbeSSebastian Andrzej Siewior GI_DEVICE_DESC_SIMPLE_RW(bDeviceClass, u8); 18388af8bbeSSebastian Andrzej Siewior GI_DEVICE_DESC_SIMPLE_RW(bDeviceSubClass, u8); 18488af8bbeSSebastian Andrzej Siewior GI_DEVICE_DESC_SIMPLE_RW(bDeviceProtocol, u8); 18588af8bbeSSebastian Andrzej Siewior GI_DEVICE_DESC_SIMPLE_RW(bMaxPacketSize0, u8); 18688af8bbeSSebastian Andrzej Siewior GI_DEVICE_DESC_SIMPLE_RW(idVendor, u16); 18788af8bbeSSebastian Andrzej Siewior GI_DEVICE_DESC_SIMPLE_RW(idProduct, u16); 18888af8bbeSSebastian Andrzej Siewior GI_DEVICE_DESC_SIMPLE_R_u16(bcdDevice); 18988af8bbeSSebastian Andrzej Siewior 19088af8bbeSSebastian Andrzej Siewior static ssize_t is_valid_bcd(u16 bcd_val) 19188af8bbeSSebastian Andrzej Siewior { 19288af8bbeSSebastian Andrzej Siewior if ((bcd_val & 0xf) > 9) 19388af8bbeSSebastian Andrzej Siewior return -EINVAL; 19488af8bbeSSebastian Andrzej Siewior if (((bcd_val >> 4) & 0xf) > 9) 19588af8bbeSSebastian Andrzej Siewior return -EINVAL; 19688af8bbeSSebastian Andrzej Siewior if (((bcd_val >> 8) & 0xf) > 9) 19788af8bbeSSebastian Andrzej Siewior return -EINVAL; 19888af8bbeSSebastian Andrzej Siewior if (((bcd_val >> 12) & 0xf) > 9) 19988af8bbeSSebastian Andrzej Siewior return -EINVAL; 20088af8bbeSSebastian Andrzej Siewior return 0; 20188af8bbeSSebastian Andrzej Siewior } 20288af8bbeSSebastian Andrzej Siewior 20345b6a73fSChristoph Hellwig static ssize_t gadget_dev_desc_bcdDevice_store(struct config_item *item, 20488af8bbeSSebastian Andrzej Siewior const char *page, size_t len) 20588af8bbeSSebastian Andrzej Siewior { 20688af8bbeSSebastian Andrzej Siewior u16 bcdDevice; 20788af8bbeSSebastian Andrzej Siewior int ret; 20888af8bbeSSebastian Andrzej Siewior 20988af8bbeSSebastian Andrzej Siewior ret = kstrtou16(page, 0, &bcdDevice); 21088af8bbeSSebastian Andrzej Siewior if (ret) 21188af8bbeSSebastian Andrzej Siewior return ret; 21288af8bbeSSebastian Andrzej Siewior ret = is_valid_bcd(bcdDevice); 21388af8bbeSSebastian Andrzej Siewior if (ret) 21488af8bbeSSebastian Andrzej Siewior return ret; 21588af8bbeSSebastian Andrzej Siewior 21645b6a73fSChristoph Hellwig to_gadget_info(item)->cdev.desc.bcdDevice = cpu_to_le16(bcdDevice); 21788af8bbeSSebastian Andrzej Siewior return len; 21888af8bbeSSebastian Andrzej Siewior } 21988af8bbeSSebastian Andrzej Siewior 22045b6a73fSChristoph Hellwig static ssize_t gadget_dev_desc_bcdUSB_store(struct config_item *item, 22188af8bbeSSebastian Andrzej Siewior const char *page, size_t len) 22288af8bbeSSebastian Andrzej Siewior { 22388af8bbeSSebastian Andrzej Siewior u16 bcdUSB; 22488af8bbeSSebastian Andrzej Siewior int ret; 22588af8bbeSSebastian Andrzej Siewior 22688af8bbeSSebastian Andrzej Siewior ret = kstrtou16(page, 0, &bcdUSB); 22788af8bbeSSebastian Andrzej Siewior if (ret) 22888af8bbeSSebastian Andrzej Siewior return ret; 22988af8bbeSSebastian Andrzej Siewior ret = is_valid_bcd(bcdUSB); 23088af8bbeSSebastian Andrzej Siewior if (ret) 23188af8bbeSSebastian Andrzej Siewior return ret; 23288af8bbeSSebastian Andrzej Siewior 23345b6a73fSChristoph Hellwig to_gadget_info(item)->cdev.desc.bcdUSB = cpu_to_le16(bcdUSB); 23488af8bbeSSebastian Andrzej Siewior return len; 23588af8bbeSSebastian Andrzej Siewior } 23688af8bbeSSebastian Andrzej Siewior 23745b6a73fSChristoph Hellwig static ssize_t gadget_dev_desc_UDC_show(struct config_item *item, char *page) 23888af8bbeSSebastian Andrzej Siewior { 23964e6bbffSEddie Hung struct gadget_info *gi = to_gadget_info(item); 24064e6bbffSEddie Hung char *udc_name; 24164e6bbffSEddie Hung int ret; 242afdaadc3SRuslan Bilovol 24364e6bbffSEddie Hung mutex_lock(&gi->lock); 24464e6bbffSEddie Hung udc_name = gi->composite.gadget_driver.udc_name; 24564e6bbffSEddie Hung ret = sprintf(page, "%s\n", udc_name ?: ""); 24664e6bbffSEddie Hung mutex_unlock(&gi->lock); 24764e6bbffSEddie Hung 24864e6bbffSEddie Hung return ret; 24988af8bbeSSebastian Andrzej Siewior } 25088af8bbeSSebastian Andrzej Siewior 25188af8bbeSSebastian Andrzej Siewior static int unregister_gadget(struct gadget_info *gi) 25288af8bbeSSebastian Andrzej Siewior { 25388af8bbeSSebastian Andrzej Siewior int ret; 25488af8bbeSSebastian Andrzej Siewior 255afdaadc3SRuslan Bilovol if (!gi->composite.gadget_driver.udc_name) 25688af8bbeSSebastian Andrzej Siewior return -ENODEV; 25788af8bbeSSebastian Andrzej Siewior 25888af8bbeSSebastian Andrzej Siewior ret = usb_gadget_unregister_driver(&gi->composite.gadget_driver); 25988af8bbeSSebastian Andrzej Siewior if (ret) 26088af8bbeSSebastian Andrzej Siewior return ret; 261afdaadc3SRuslan Bilovol kfree(gi->composite.gadget_driver.udc_name); 262afdaadc3SRuslan Bilovol gi->composite.gadget_driver.udc_name = NULL; 26388af8bbeSSebastian Andrzej Siewior return 0; 26488af8bbeSSebastian Andrzej Siewior } 26588af8bbeSSebastian Andrzej Siewior 26645b6a73fSChristoph Hellwig static ssize_t gadget_dev_desc_UDC_store(struct config_item *item, 26788af8bbeSSebastian Andrzej Siewior const char *page, size_t len) 26888af8bbeSSebastian Andrzej Siewior { 26945b6a73fSChristoph Hellwig struct gadget_info *gi = to_gadget_info(item); 27088af8bbeSSebastian Andrzej Siewior char *name; 27188af8bbeSSebastian Andrzej Siewior int ret; 27288af8bbeSSebastian Andrzej Siewior 27315753588SKyungtae Kim if (strlen(page) < len) 27415753588SKyungtae Kim return -EOVERFLOW; 27515753588SKyungtae Kim 27688af8bbeSSebastian Andrzej Siewior name = kstrdup(page, GFP_KERNEL); 27788af8bbeSSebastian Andrzej Siewior if (!name) 27888af8bbeSSebastian Andrzej Siewior return -ENOMEM; 27988af8bbeSSebastian Andrzej Siewior if (name[len - 1] == '\n') 28088af8bbeSSebastian Andrzej Siewior name[len - 1] = '\0'; 28188af8bbeSSebastian Andrzej Siewior 28288af8bbeSSebastian Andrzej Siewior mutex_lock(&gi->lock); 28388af8bbeSSebastian Andrzej Siewior 28488af8bbeSSebastian Andrzej Siewior if (!strlen(name)) { 28588af8bbeSSebastian Andrzej Siewior ret = unregister_gadget(gi); 28688af8bbeSSebastian Andrzej Siewior if (ret) 28788af8bbeSSebastian Andrzej Siewior goto err; 28838355b2aSJohn Keeping kfree(name); 28988af8bbeSSebastian Andrzej Siewior } else { 290afdaadc3SRuslan Bilovol if (gi->composite.gadget_driver.udc_name) { 29188af8bbeSSebastian Andrzej Siewior ret = -EBUSY; 29288af8bbeSSebastian Andrzej Siewior goto err; 29388af8bbeSSebastian Andrzej Siewior } 294afdaadc3SRuslan Bilovol gi->composite.gadget_driver.udc_name = name; 295af1969a2SAlan Stern ret = usb_gadget_register_driver(&gi->composite.gadget_driver); 296afdaadc3SRuslan Bilovol if (ret) { 297afdaadc3SRuslan Bilovol gi->composite.gadget_driver.udc_name = NULL; 29888af8bbeSSebastian Andrzej Siewior goto err; 299afdaadc3SRuslan Bilovol } 30088af8bbeSSebastian Andrzej Siewior } 30188af8bbeSSebastian Andrzej Siewior mutex_unlock(&gi->lock); 30288af8bbeSSebastian Andrzej Siewior return len; 30388af8bbeSSebastian Andrzej Siewior err: 30488af8bbeSSebastian Andrzej Siewior kfree(name); 30588af8bbeSSebastian Andrzej Siewior mutex_unlock(&gi->lock); 30688af8bbeSSebastian Andrzej Siewior return ret; 30788af8bbeSSebastian Andrzej Siewior } 30888af8bbeSSebastian Andrzej Siewior 309a0249703SThinh Nguyen static ssize_t gadget_dev_desc_max_speed_show(struct config_item *item, 310a0249703SThinh Nguyen char *page) 311a0249703SThinh Nguyen { 312a0249703SThinh Nguyen enum usb_device_speed speed = to_gadget_info(item)->composite.max_speed; 313a0249703SThinh Nguyen 314a0249703SThinh Nguyen return sprintf(page, "%s\n", usb_speed_string(speed)); 315a0249703SThinh Nguyen } 316a0249703SThinh Nguyen 317a0249703SThinh Nguyen static ssize_t gadget_dev_desc_max_speed_store(struct config_item *item, 318a0249703SThinh Nguyen const char *page, size_t len) 319a0249703SThinh Nguyen { 320a0249703SThinh Nguyen struct gadget_info *gi = to_gadget_info(item); 321a0249703SThinh Nguyen 322a0249703SThinh Nguyen mutex_lock(&gi->lock); 323a0249703SThinh Nguyen 324a0249703SThinh Nguyen /* Prevent changing of max_speed after the driver is binded */ 325a0249703SThinh Nguyen if (gi->composite.gadget_driver.udc_name) 326a0249703SThinh Nguyen goto err; 327a0249703SThinh Nguyen 328a0249703SThinh Nguyen if (strncmp(page, "super-speed-plus", 16) == 0) 329a0249703SThinh Nguyen gi->composite.max_speed = USB_SPEED_SUPER_PLUS; 330a0249703SThinh Nguyen else if (strncmp(page, "super-speed", 11) == 0) 331a0249703SThinh Nguyen gi->composite.max_speed = USB_SPEED_SUPER; 332a0249703SThinh Nguyen else if (strncmp(page, "high-speed", 10) == 0) 333a0249703SThinh Nguyen gi->composite.max_speed = USB_SPEED_HIGH; 334a0249703SThinh Nguyen else if (strncmp(page, "full-speed", 10) == 0) 335a0249703SThinh Nguyen gi->composite.max_speed = USB_SPEED_FULL; 336a0249703SThinh Nguyen else if (strncmp(page, "low-speed", 9) == 0) 337a0249703SThinh Nguyen gi->composite.max_speed = USB_SPEED_LOW; 338a0249703SThinh Nguyen else 339a0249703SThinh Nguyen goto err; 340a0249703SThinh Nguyen 341a0249703SThinh Nguyen gi->composite.gadget_driver.max_speed = gi->composite.max_speed; 342a0249703SThinh Nguyen 343a0249703SThinh Nguyen mutex_unlock(&gi->lock); 344a0249703SThinh Nguyen return len; 345a0249703SThinh Nguyen err: 346a0249703SThinh Nguyen mutex_unlock(&gi->lock); 347a0249703SThinh Nguyen return -EINVAL; 348a0249703SThinh Nguyen } 349a0249703SThinh Nguyen 35045b6a73fSChristoph Hellwig CONFIGFS_ATTR(gadget_dev_desc_, bDeviceClass); 35145b6a73fSChristoph Hellwig CONFIGFS_ATTR(gadget_dev_desc_, bDeviceSubClass); 35245b6a73fSChristoph Hellwig CONFIGFS_ATTR(gadget_dev_desc_, bDeviceProtocol); 35345b6a73fSChristoph Hellwig CONFIGFS_ATTR(gadget_dev_desc_, bMaxPacketSize0); 35445b6a73fSChristoph Hellwig CONFIGFS_ATTR(gadget_dev_desc_, idVendor); 35545b6a73fSChristoph Hellwig CONFIGFS_ATTR(gadget_dev_desc_, idProduct); 35645b6a73fSChristoph Hellwig CONFIGFS_ATTR(gadget_dev_desc_, bcdDevice); 35745b6a73fSChristoph Hellwig CONFIGFS_ATTR(gadget_dev_desc_, bcdUSB); 35845b6a73fSChristoph Hellwig CONFIGFS_ATTR(gadget_dev_desc_, UDC); 359a0249703SThinh Nguyen CONFIGFS_ATTR(gadget_dev_desc_, max_speed); 36088af8bbeSSebastian Andrzej Siewior 36188af8bbeSSebastian Andrzej Siewior static struct configfs_attribute *gadget_root_attrs[] = { 36245b6a73fSChristoph Hellwig &gadget_dev_desc_attr_bDeviceClass, 36345b6a73fSChristoph Hellwig &gadget_dev_desc_attr_bDeviceSubClass, 36445b6a73fSChristoph Hellwig &gadget_dev_desc_attr_bDeviceProtocol, 36545b6a73fSChristoph Hellwig &gadget_dev_desc_attr_bMaxPacketSize0, 36645b6a73fSChristoph Hellwig &gadget_dev_desc_attr_idVendor, 36745b6a73fSChristoph Hellwig &gadget_dev_desc_attr_idProduct, 36845b6a73fSChristoph Hellwig &gadget_dev_desc_attr_bcdDevice, 36945b6a73fSChristoph Hellwig &gadget_dev_desc_attr_bcdUSB, 37045b6a73fSChristoph Hellwig &gadget_dev_desc_attr_UDC, 371a0249703SThinh Nguyen &gadget_dev_desc_attr_max_speed, 37288af8bbeSSebastian Andrzej Siewior NULL, 37388af8bbeSSebastian Andrzej Siewior }; 37488af8bbeSSebastian Andrzej Siewior 375*6e2a512dSDaniel Scally static inline struct gadget_language *to_gadget_language(struct config_item *item) 37688af8bbeSSebastian Andrzej Siewior { 377*6e2a512dSDaniel Scally return container_of(to_config_group(item), struct gadget_language, 37888af8bbeSSebastian Andrzej Siewior group); 37988af8bbeSSebastian Andrzej Siewior } 38088af8bbeSSebastian Andrzej Siewior 38188af8bbeSSebastian Andrzej Siewior static inline struct gadget_config_name *to_gadget_config_name( 38288af8bbeSSebastian Andrzej Siewior struct config_item *item) 38388af8bbeSSebastian Andrzej Siewior { 38488af8bbeSSebastian Andrzej Siewior return container_of(to_config_group(item), struct gadget_config_name, 38588af8bbeSSebastian Andrzej Siewior group); 38688af8bbeSSebastian Andrzej Siewior } 38788af8bbeSSebastian Andrzej Siewior 38888af8bbeSSebastian Andrzej Siewior static inline struct usb_function_instance *to_usb_function_instance( 38988af8bbeSSebastian Andrzej Siewior struct config_item *item) 39088af8bbeSSebastian Andrzej Siewior { 39188af8bbeSSebastian Andrzej Siewior return container_of(to_config_group(item), 39288af8bbeSSebastian Andrzej Siewior struct usb_function_instance, group); 39388af8bbeSSebastian Andrzej Siewior } 39488af8bbeSSebastian Andrzej Siewior 39588af8bbeSSebastian Andrzej Siewior static void gadget_info_attr_release(struct config_item *item) 39688af8bbeSSebastian Andrzej Siewior { 39788af8bbeSSebastian Andrzej Siewior struct gadget_info *gi = to_gadget_info(item); 39888af8bbeSSebastian Andrzej Siewior 39988af8bbeSSebastian Andrzej Siewior WARN_ON(!list_empty(&gi->cdev.configs)); 40088af8bbeSSebastian Andrzej Siewior WARN_ON(!list_empty(&gi->string_list)); 40188af8bbeSSebastian Andrzej Siewior WARN_ON(!list_empty(&gi->available_func)); 40288af8bbeSSebastian Andrzej Siewior kfree(gi->composite.gadget_driver.function); 4037c075538SChanh Nguyen kfree(gi->composite.gadget_driver.driver.name); 40488af8bbeSSebastian Andrzej Siewior kfree(gi); 40588af8bbeSSebastian Andrzej Siewior } 40688af8bbeSSebastian Andrzej Siewior 40788af8bbeSSebastian Andrzej Siewior static struct configfs_item_operations gadget_root_item_ops = { 40888af8bbeSSebastian Andrzej Siewior .release = gadget_info_attr_release, 40988af8bbeSSebastian Andrzej Siewior }; 41088af8bbeSSebastian Andrzej Siewior 41188af8bbeSSebastian Andrzej Siewior static void gadget_config_attr_release(struct config_item *item) 41288af8bbeSSebastian Andrzej Siewior { 41388af8bbeSSebastian Andrzej Siewior struct config_usb_cfg *cfg = to_config_usb_cfg(item); 41488af8bbeSSebastian Andrzej Siewior 41588af8bbeSSebastian Andrzej Siewior WARN_ON(!list_empty(&cfg->c.functions)); 41688af8bbeSSebastian Andrzej Siewior list_del(&cfg->c.list); 41788af8bbeSSebastian Andrzej Siewior kfree(cfg->c.label); 41888af8bbeSSebastian Andrzej Siewior kfree(cfg); 41988af8bbeSSebastian Andrzej Siewior } 42088af8bbeSSebastian Andrzej Siewior 42188af8bbeSSebastian Andrzej Siewior static int config_usb_cfg_link( 42288af8bbeSSebastian Andrzej Siewior struct config_item *usb_cfg_ci, 42388af8bbeSSebastian Andrzej Siewior struct config_item *usb_func_ci) 42488af8bbeSSebastian Andrzej Siewior { 42588af8bbeSSebastian Andrzej Siewior struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci); 426260d88b7SLinyu Yuan struct gadget_info *gi = cfg_to_gadget_info(cfg); 42788af8bbeSSebastian Andrzej Siewior 4285284acccSLinyu Yuan struct usb_function_instance *fi = 4295284acccSLinyu Yuan to_usb_function_instance(usb_func_ci); 43036f4c25cSJakob Koschel struct usb_function_instance *a_fi = NULL, *iter; 43188af8bbeSSebastian Andrzej Siewior struct usb_function *f; 43288af8bbeSSebastian Andrzej Siewior int ret; 43388af8bbeSSebastian Andrzej Siewior 43488af8bbeSSebastian Andrzej Siewior mutex_lock(&gi->lock); 43588af8bbeSSebastian Andrzej Siewior /* 43688af8bbeSSebastian Andrzej Siewior * Make sure this function is from within our _this_ gadget and not 43788af8bbeSSebastian Andrzej Siewior * from another gadget or a random directory. 43888af8bbeSSebastian Andrzej Siewior * Also a function instance can only be linked once. 43988af8bbeSSebastian Andrzej Siewior */ 44089e7252dSUdipto Goswami 44189e7252dSUdipto Goswami if (gi->composite.gadget_driver.udc_name) { 44289e7252dSUdipto Goswami ret = -EINVAL; 44389e7252dSUdipto Goswami goto out; 44489e7252dSUdipto Goswami } 44589e7252dSUdipto Goswami 44636f4c25cSJakob Koschel list_for_each_entry(iter, &gi->available_func, cfs_list) { 44736f4c25cSJakob Koschel if (iter != fi) 44836f4c25cSJakob Koschel continue; 44936f4c25cSJakob Koschel a_fi = iter; 45088af8bbeSSebastian Andrzej Siewior break; 45188af8bbeSSebastian Andrzej Siewior } 45236f4c25cSJakob Koschel if (!a_fi) { 45388af8bbeSSebastian Andrzej Siewior ret = -EINVAL; 45488af8bbeSSebastian Andrzej Siewior goto out; 45588af8bbeSSebastian Andrzej Siewior } 45688af8bbeSSebastian Andrzej Siewior 45788af8bbeSSebastian Andrzej Siewior list_for_each_entry(f, &cfg->func_list, list) { 45888af8bbeSSebastian Andrzej Siewior if (f->fi == fi) { 45988af8bbeSSebastian Andrzej Siewior ret = -EEXIST; 46088af8bbeSSebastian Andrzej Siewior goto out; 46188af8bbeSSebastian Andrzej Siewior } 46288af8bbeSSebastian Andrzej Siewior } 46388af8bbeSSebastian Andrzej Siewior 46488af8bbeSSebastian Andrzej Siewior f = usb_get_function(fi); 46588af8bbeSSebastian Andrzej Siewior if (IS_ERR(f)) { 46688af8bbeSSebastian Andrzej Siewior ret = PTR_ERR(f); 46788af8bbeSSebastian Andrzej Siewior goto out; 46888af8bbeSSebastian Andrzej Siewior } 46988af8bbeSSebastian Andrzej Siewior 47088af8bbeSSebastian Andrzej Siewior /* stash the function until we bind it to the gadget */ 47188af8bbeSSebastian Andrzej Siewior list_add_tail(&f->list, &cfg->func_list); 47288af8bbeSSebastian Andrzej Siewior ret = 0; 47388af8bbeSSebastian Andrzej Siewior out: 47488af8bbeSSebastian Andrzej Siewior mutex_unlock(&gi->lock); 47588af8bbeSSebastian Andrzej Siewior return ret; 47688af8bbeSSebastian Andrzej Siewior } 47788af8bbeSSebastian Andrzej Siewior 478e16769d4SAndrzej Pietrasiewicz static void config_usb_cfg_unlink( 47988af8bbeSSebastian Andrzej Siewior struct config_item *usb_cfg_ci, 48088af8bbeSSebastian Andrzej Siewior struct config_item *usb_func_ci) 48188af8bbeSSebastian Andrzej Siewior { 48288af8bbeSSebastian Andrzej Siewior struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci); 483260d88b7SLinyu Yuan struct gadget_info *gi = cfg_to_gadget_info(cfg); 48488af8bbeSSebastian Andrzej Siewior 4855284acccSLinyu Yuan struct usb_function_instance *fi = 4865284acccSLinyu Yuan to_usb_function_instance(usb_func_ci); 48788af8bbeSSebastian Andrzej Siewior struct usb_function *f; 48888af8bbeSSebastian Andrzej Siewior 48988af8bbeSSebastian Andrzej Siewior /* 49088af8bbeSSebastian Andrzej Siewior * ideally I would like to forbid to unlink functions while a gadget is 49188af8bbeSSebastian Andrzej Siewior * bound to an UDC. Since this isn't possible at the moment, we simply 49288af8bbeSSebastian Andrzej Siewior * force an unbind, the function is available here and then we can 49388af8bbeSSebastian Andrzej Siewior * remove the function. 49488af8bbeSSebastian Andrzej Siewior */ 49588af8bbeSSebastian Andrzej Siewior mutex_lock(&gi->lock); 496afdaadc3SRuslan Bilovol if (gi->composite.gadget_driver.udc_name) 49788af8bbeSSebastian Andrzej Siewior unregister_gadget(gi); 498afdaadc3SRuslan Bilovol WARN_ON(gi->composite.gadget_driver.udc_name); 49988af8bbeSSebastian Andrzej Siewior 50088af8bbeSSebastian Andrzej Siewior list_for_each_entry(f, &cfg->func_list, list) { 50188af8bbeSSebastian Andrzej Siewior if (f->fi == fi) { 50288af8bbeSSebastian Andrzej Siewior list_del(&f->list); 50388af8bbeSSebastian Andrzej Siewior usb_put_function(f); 50488af8bbeSSebastian Andrzej Siewior mutex_unlock(&gi->lock); 505e16769d4SAndrzej Pietrasiewicz return; 50688af8bbeSSebastian Andrzej Siewior } 50788af8bbeSSebastian Andrzej Siewior } 50888af8bbeSSebastian Andrzej Siewior mutex_unlock(&gi->lock); 50975bfe23aSDavid Rientjes WARN(1, "Unable to locate function to unbind\n"); 51088af8bbeSSebastian Andrzej Siewior } 51188af8bbeSSebastian Andrzej Siewior 51288af8bbeSSebastian Andrzej Siewior static struct configfs_item_operations gadget_config_item_ops = { 51388af8bbeSSebastian Andrzej Siewior .release = gadget_config_attr_release, 51488af8bbeSSebastian Andrzej Siewior .allow_link = config_usb_cfg_link, 51588af8bbeSSebastian Andrzej Siewior .drop_link = config_usb_cfg_unlink, 51688af8bbeSSebastian Andrzej Siewior }; 51788af8bbeSSebastian Andrzej Siewior 51888af8bbeSSebastian Andrzej Siewior 51945b6a73fSChristoph Hellwig static ssize_t gadget_config_desc_MaxPower_show(struct config_item *item, 52088af8bbeSSebastian Andrzej Siewior char *page) 52188af8bbeSSebastian Andrzej Siewior { 522c26f1c10SLinyu Yuan struct config_usb_cfg *cfg = to_config_usb_cfg(item); 523c26f1c10SLinyu Yuan 524c26f1c10SLinyu Yuan return sprintf(page, "%u\n", cfg->c.MaxPower); 52588af8bbeSSebastian Andrzej Siewior } 52688af8bbeSSebastian Andrzej Siewior 52745b6a73fSChristoph Hellwig static ssize_t gadget_config_desc_MaxPower_store(struct config_item *item, 52888af8bbeSSebastian Andrzej Siewior const char *page, size_t len) 52988af8bbeSSebastian Andrzej Siewior { 530c26f1c10SLinyu Yuan struct config_usb_cfg *cfg = to_config_usb_cfg(item); 53188af8bbeSSebastian Andrzej Siewior u16 val; 53288af8bbeSSebastian Andrzej Siewior int ret; 53388af8bbeSSebastian Andrzej Siewior ret = kstrtou16(page, 0, &val); 53488af8bbeSSebastian Andrzej Siewior if (ret) 53588af8bbeSSebastian Andrzej Siewior return ret; 53688af8bbeSSebastian Andrzej Siewior if (DIV_ROUND_UP(val, 8) > 0xff) 53788af8bbeSSebastian Andrzej Siewior return -ERANGE; 538c26f1c10SLinyu Yuan cfg->c.MaxPower = val; 53988af8bbeSSebastian Andrzej Siewior return len; 54088af8bbeSSebastian Andrzej Siewior } 54188af8bbeSSebastian Andrzej Siewior 54245b6a73fSChristoph Hellwig static ssize_t gadget_config_desc_bmAttributes_show(struct config_item *item, 54388af8bbeSSebastian Andrzej Siewior char *page) 54488af8bbeSSebastian Andrzej Siewior { 545c26f1c10SLinyu Yuan struct config_usb_cfg *cfg = to_config_usb_cfg(item); 546c26f1c10SLinyu Yuan 547c26f1c10SLinyu Yuan return sprintf(page, "0x%02x\n", cfg->c.bmAttributes); 54888af8bbeSSebastian Andrzej Siewior } 54988af8bbeSSebastian Andrzej Siewior 55045b6a73fSChristoph Hellwig static ssize_t gadget_config_desc_bmAttributes_store(struct config_item *item, 55188af8bbeSSebastian Andrzej Siewior const char *page, size_t len) 55288af8bbeSSebastian Andrzej Siewior { 553c26f1c10SLinyu Yuan struct config_usb_cfg *cfg = to_config_usb_cfg(item); 55488af8bbeSSebastian Andrzej Siewior u8 val; 55588af8bbeSSebastian Andrzej Siewior int ret; 55688af8bbeSSebastian Andrzej Siewior ret = kstrtou8(page, 0, &val); 55788af8bbeSSebastian Andrzej Siewior if (ret) 55888af8bbeSSebastian Andrzej Siewior return ret; 55988af8bbeSSebastian Andrzej Siewior if (!(val & USB_CONFIG_ATT_ONE)) 56088af8bbeSSebastian Andrzej Siewior return -EINVAL; 56188af8bbeSSebastian Andrzej Siewior if (val & ~(USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER | 56288af8bbeSSebastian Andrzej Siewior USB_CONFIG_ATT_WAKEUP)) 56388af8bbeSSebastian Andrzej Siewior return -EINVAL; 564c26f1c10SLinyu Yuan cfg->c.bmAttributes = val; 56588af8bbeSSebastian Andrzej Siewior return len; 56688af8bbeSSebastian Andrzej Siewior } 56788af8bbeSSebastian Andrzej Siewior 56845b6a73fSChristoph Hellwig CONFIGFS_ATTR(gadget_config_desc_, MaxPower); 56945b6a73fSChristoph Hellwig CONFIGFS_ATTR(gadget_config_desc_, bmAttributes); 57088af8bbeSSebastian Andrzej Siewior 57188af8bbeSSebastian Andrzej Siewior static struct configfs_attribute *gadget_config_attrs[] = { 57245b6a73fSChristoph Hellwig &gadget_config_desc_attr_MaxPower, 57345b6a73fSChristoph Hellwig &gadget_config_desc_attr_bmAttributes, 57488af8bbeSSebastian Andrzej Siewior NULL, 57588af8bbeSSebastian Andrzej Siewior }; 57688af8bbeSSebastian Andrzej Siewior 5774ad01412SBhumika Goyal static const struct config_item_type gadget_config_type = { 57888af8bbeSSebastian Andrzej Siewior .ct_item_ops = &gadget_config_item_ops, 57988af8bbeSSebastian Andrzej Siewior .ct_attrs = gadget_config_attrs, 58088af8bbeSSebastian Andrzej Siewior .ct_owner = THIS_MODULE, 58188af8bbeSSebastian Andrzej Siewior }; 58288af8bbeSSebastian Andrzej Siewior 5834ad01412SBhumika Goyal static const struct config_item_type gadget_root_type = { 58488af8bbeSSebastian Andrzej Siewior .ct_item_ops = &gadget_root_item_ops, 58588af8bbeSSebastian Andrzej Siewior .ct_attrs = gadget_root_attrs, 58688af8bbeSSebastian Andrzej Siewior .ct_owner = THIS_MODULE, 58788af8bbeSSebastian Andrzej Siewior }; 58888af8bbeSSebastian Andrzej Siewior 58988af8bbeSSebastian Andrzej Siewior static void composite_init_dev(struct usb_composite_dev *cdev) 59088af8bbeSSebastian Andrzej Siewior { 59188af8bbeSSebastian Andrzej Siewior spin_lock_init(&cdev->lock); 59288af8bbeSSebastian Andrzej Siewior INIT_LIST_HEAD(&cdev->configs); 59388af8bbeSSebastian Andrzej Siewior INIT_LIST_HEAD(&cdev->gstrings); 59488af8bbeSSebastian Andrzej Siewior } 59588af8bbeSSebastian Andrzej Siewior 59688af8bbeSSebastian Andrzej Siewior static struct config_group *function_make( 59788af8bbeSSebastian Andrzej Siewior struct config_group *group, 59888af8bbeSSebastian Andrzej Siewior const char *name) 59988af8bbeSSebastian Andrzej Siewior { 60088af8bbeSSebastian Andrzej Siewior struct gadget_info *gi; 60188af8bbeSSebastian Andrzej Siewior struct usb_function_instance *fi; 60288af8bbeSSebastian Andrzej Siewior char buf[MAX_NAME_LEN]; 60388af8bbeSSebastian Andrzej Siewior char *func_name; 60488af8bbeSSebastian Andrzej Siewior char *instance_name; 60588af8bbeSSebastian Andrzej Siewior int ret; 60688af8bbeSSebastian Andrzej Siewior 60788af8bbeSSebastian Andrzej Siewior ret = snprintf(buf, MAX_NAME_LEN, "%s", name); 60888af8bbeSSebastian Andrzej Siewior if (ret >= MAX_NAME_LEN) 60988af8bbeSSebastian Andrzej Siewior return ERR_PTR(-ENAMETOOLONG); 61088af8bbeSSebastian Andrzej Siewior 61188af8bbeSSebastian Andrzej Siewior func_name = buf; 61288af8bbeSSebastian Andrzej Siewior instance_name = strchr(func_name, '.'); 61388af8bbeSSebastian Andrzej Siewior if (!instance_name) { 61488af8bbeSSebastian Andrzej Siewior pr_err("Unable to locate . in FUNC.INSTANCE\n"); 61588af8bbeSSebastian Andrzej Siewior return ERR_PTR(-EINVAL); 61688af8bbeSSebastian Andrzej Siewior } 61788af8bbeSSebastian Andrzej Siewior *instance_name = '\0'; 61888af8bbeSSebastian Andrzej Siewior instance_name++; 61988af8bbeSSebastian Andrzej Siewior 62088af8bbeSSebastian Andrzej Siewior fi = usb_get_function_instance(func_name); 62188af8bbeSSebastian Andrzej Siewior if (IS_ERR(fi)) 622a3469411SDuan Jiong return ERR_CAST(fi); 62388af8bbeSSebastian Andrzej Siewior 6243958b792SNicolas Iooss ret = config_item_set_name(&fi->group.cg_item, "%s", name); 62588af8bbeSSebastian Andrzej Siewior if (ret) { 62688af8bbeSSebastian Andrzej Siewior usb_put_function_instance(fi); 62788af8bbeSSebastian Andrzej Siewior return ERR_PTR(ret); 62888af8bbeSSebastian Andrzej Siewior } 6291933861dSAndrzej Pietrasiewicz if (fi->set_inst_name) { 6301933861dSAndrzej Pietrasiewicz ret = fi->set_inst_name(fi, instance_name); 6311933861dSAndrzej Pietrasiewicz if (ret) { 6321933861dSAndrzej Pietrasiewicz usb_put_function_instance(fi); 6331933861dSAndrzej Pietrasiewicz return ERR_PTR(ret); 6341933861dSAndrzej Pietrasiewicz } 6351933861dSAndrzej Pietrasiewicz } 63688af8bbeSSebastian Andrzej Siewior 63788af8bbeSSebastian Andrzej Siewior gi = container_of(group, struct gadget_info, functions_group); 63888af8bbeSSebastian Andrzej Siewior 63988af8bbeSSebastian Andrzej Siewior mutex_lock(&gi->lock); 64088af8bbeSSebastian Andrzej Siewior list_add_tail(&fi->cfs_list, &gi->available_func); 64188af8bbeSSebastian Andrzej Siewior mutex_unlock(&gi->lock); 64288af8bbeSSebastian Andrzej Siewior return &fi->group; 64388af8bbeSSebastian Andrzej Siewior } 64488af8bbeSSebastian Andrzej Siewior 64588af8bbeSSebastian Andrzej Siewior static void function_drop( 64688af8bbeSSebastian Andrzej Siewior struct config_group *group, 64788af8bbeSSebastian Andrzej Siewior struct config_item *item) 64888af8bbeSSebastian Andrzej Siewior { 64988af8bbeSSebastian Andrzej Siewior struct usb_function_instance *fi = to_usb_function_instance(item); 65088af8bbeSSebastian Andrzej Siewior struct gadget_info *gi; 65188af8bbeSSebastian Andrzej Siewior 65288af8bbeSSebastian Andrzej Siewior gi = container_of(group, struct gadget_info, functions_group); 65388af8bbeSSebastian Andrzej Siewior 65488af8bbeSSebastian Andrzej Siewior mutex_lock(&gi->lock); 65588af8bbeSSebastian Andrzej Siewior list_del(&fi->cfs_list); 65688af8bbeSSebastian Andrzej Siewior mutex_unlock(&gi->lock); 65788af8bbeSSebastian Andrzej Siewior config_item_put(item); 65888af8bbeSSebastian Andrzej Siewior } 65988af8bbeSSebastian Andrzej Siewior 66088af8bbeSSebastian Andrzej Siewior static struct configfs_group_operations functions_ops = { 66188af8bbeSSebastian Andrzej Siewior .make_group = &function_make, 66288af8bbeSSebastian Andrzej Siewior .drop_item = &function_drop, 66388af8bbeSSebastian Andrzej Siewior }; 66488af8bbeSSebastian Andrzej Siewior 6654ad01412SBhumika Goyal static const struct config_item_type functions_type = { 66688af8bbeSSebastian Andrzej Siewior .ct_group_ops = &functions_ops, 66788af8bbeSSebastian Andrzej Siewior .ct_owner = THIS_MODULE, 66888af8bbeSSebastian Andrzej Siewior }; 66988af8bbeSSebastian Andrzej Siewior 67088af8bbeSSebastian Andrzej Siewior GS_STRINGS_RW(gadget_config_name, configuration); 67188af8bbeSSebastian Andrzej Siewior 67288af8bbeSSebastian Andrzej Siewior static struct configfs_attribute *gadget_config_name_langid_attrs[] = { 67345b6a73fSChristoph Hellwig &gadget_config_name_attr_configuration, 67488af8bbeSSebastian Andrzej Siewior NULL, 67588af8bbeSSebastian Andrzej Siewior }; 67688af8bbeSSebastian Andrzej Siewior 67788af8bbeSSebastian Andrzej Siewior static void gadget_config_name_attr_release(struct config_item *item) 67888af8bbeSSebastian Andrzej Siewior { 67988af8bbeSSebastian Andrzej Siewior struct gadget_config_name *cn = to_gadget_config_name(item); 68088af8bbeSSebastian Andrzej Siewior 68188af8bbeSSebastian Andrzej Siewior kfree(cn->configuration); 68288af8bbeSSebastian Andrzej Siewior 68388af8bbeSSebastian Andrzej Siewior list_del(&cn->list); 68488af8bbeSSebastian Andrzej Siewior kfree(cn); 68588af8bbeSSebastian Andrzej Siewior } 68688af8bbeSSebastian Andrzej Siewior 68788af8bbeSSebastian Andrzej Siewior USB_CONFIG_STRING_RW_OPS(gadget_config_name); 68888af8bbeSSebastian Andrzej Siewior USB_CONFIG_STRINGS_LANG(gadget_config_name, config_usb_cfg); 68988af8bbeSSebastian Andrzej Siewior 69088af8bbeSSebastian Andrzej Siewior static struct config_group *config_desc_make( 69188af8bbeSSebastian Andrzej Siewior struct config_group *group, 69288af8bbeSSebastian Andrzej Siewior const char *name) 69388af8bbeSSebastian Andrzej Siewior { 69488af8bbeSSebastian Andrzej Siewior struct gadget_info *gi; 69588af8bbeSSebastian Andrzej Siewior struct config_usb_cfg *cfg; 69688af8bbeSSebastian Andrzej Siewior char buf[MAX_NAME_LEN]; 69788af8bbeSSebastian Andrzej Siewior char *num_str; 69888af8bbeSSebastian Andrzej Siewior u8 num; 69988af8bbeSSebastian Andrzej Siewior int ret; 70088af8bbeSSebastian Andrzej Siewior 70188af8bbeSSebastian Andrzej Siewior gi = container_of(group, struct gadget_info, configs_group); 70288af8bbeSSebastian Andrzej Siewior ret = snprintf(buf, MAX_NAME_LEN, "%s", name); 70388af8bbeSSebastian Andrzej Siewior if (ret >= MAX_NAME_LEN) 70488af8bbeSSebastian Andrzej Siewior return ERR_PTR(-ENAMETOOLONG); 70588af8bbeSSebastian Andrzej Siewior 70688af8bbeSSebastian Andrzej Siewior num_str = strchr(buf, '.'); 70788af8bbeSSebastian Andrzej Siewior if (!num_str) { 70888af8bbeSSebastian Andrzej Siewior pr_err("Unable to locate . in name.bConfigurationValue\n"); 70988af8bbeSSebastian Andrzej Siewior return ERR_PTR(-EINVAL); 71088af8bbeSSebastian Andrzej Siewior } 71188af8bbeSSebastian Andrzej Siewior 71288af8bbeSSebastian Andrzej Siewior *num_str = '\0'; 71388af8bbeSSebastian Andrzej Siewior num_str++; 71488af8bbeSSebastian Andrzej Siewior 71588af8bbeSSebastian Andrzej Siewior if (!strlen(buf)) 71688af8bbeSSebastian Andrzej Siewior return ERR_PTR(-EINVAL); 71788af8bbeSSebastian Andrzej Siewior 71888af8bbeSSebastian Andrzej Siewior ret = kstrtou8(num_str, 0, &num); 71988af8bbeSSebastian Andrzej Siewior if (ret) 72088af8bbeSSebastian Andrzej Siewior return ERR_PTR(ret); 72188af8bbeSSebastian Andrzej Siewior 72288af8bbeSSebastian Andrzej Siewior cfg = kzalloc(sizeof(*cfg), GFP_KERNEL); 72388af8bbeSSebastian Andrzej Siewior if (!cfg) 72488af8bbeSSebastian Andrzej Siewior return ERR_PTR(-ENOMEM); 72588af8bbeSSebastian Andrzej Siewior cfg->c.label = kstrdup(buf, GFP_KERNEL); 72688af8bbeSSebastian Andrzej Siewior if (!cfg->c.label) { 72788af8bbeSSebastian Andrzej Siewior ret = -ENOMEM; 72888af8bbeSSebastian Andrzej Siewior goto err; 72988af8bbeSSebastian Andrzej Siewior } 73088af8bbeSSebastian Andrzej Siewior cfg->c.bConfigurationValue = num; 73188af8bbeSSebastian Andrzej Siewior cfg->c.MaxPower = CONFIG_USB_GADGET_VBUS_DRAW; 73288af8bbeSSebastian Andrzej Siewior cfg->c.bmAttributes = USB_CONFIG_ATT_ONE; 73388af8bbeSSebastian Andrzej Siewior INIT_LIST_HEAD(&cfg->string_list); 73488af8bbeSSebastian Andrzej Siewior INIT_LIST_HEAD(&cfg->func_list); 73588af8bbeSSebastian Andrzej Siewior 73688af8bbeSSebastian Andrzej Siewior config_group_init_type_name(&cfg->group, name, 73788af8bbeSSebastian Andrzej Siewior &gadget_config_type); 7381ae1602dSChristoph Hellwig 73988af8bbeSSebastian Andrzej Siewior config_group_init_type_name(&cfg->strings_group, "strings", 74088af8bbeSSebastian Andrzej Siewior &gadget_config_name_strings_type); 7411ae1602dSChristoph Hellwig configfs_add_default_group(&cfg->strings_group, &cfg->group); 74288af8bbeSSebastian Andrzej Siewior 74388af8bbeSSebastian Andrzej Siewior ret = usb_add_config_only(&gi->cdev, &cfg->c); 74488af8bbeSSebastian Andrzej Siewior if (ret) 74588af8bbeSSebastian Andrzej Siewior goto err; 74688af8bbeSSebastian Andrzej Siewior 74788af8bbeSSebastian Andrzej Siewior return &cfg->group; 74888af8bbeSSebastian Andrzej Siewior err: 74988af8bbeSSebastian Andrzej Siewior kfree(cfg->c.label); 75088af8bbeSSebastian Andrzej Siewior kfree(cfg); 75188af8bbeSSebastian Andrzej Siewior return ERR_PTR(ret); 75288af8bbeSSebastian Andrzej Siewior } 75388af8bbeSSebastian Andrzej Siewior 75488af8bbeSSebastian Andrzej Siewior static void config_desc_drop( 75588af8bbeSSebastian Andrzej Siewior struct config_group *group, 75688af8bbeSSebastian Andrzej Siewior struct config_item *item) 75788af8bbeSSebastian Andrzej Siewior { 75888af8bbeSSebastian Andrzej Siewior config_item_put(item); 75988af8bbeSSebastian Andrzej Siewior } 76088af8bbeSSebastian Andrzej Siewior 76188af8bbeSSebastian Andrzej Siewior static struct configfs_group_operations config_desc_ops = { 76288af8bbeSSebastian Andrzej Siewior .make_group = &config_desc_make, 76388af8bbeSSebastian Andrzej Siewior .drop_item = &config_desc_drop, 76488af8bbeSSebastian Andrzej Siewior }; 76588af8bbeSSebastian Andrzej Siewior 7664ad01412SBhumika Goyal static const struct config_item_type config_desc_type = { 76788af8bbeSSebastian Andrzej Siewior .ct_group_ops = &config_desc_ops, 76888af8bbeSSebastian Andrzej Siewior .ct_owner = THIS_MODULE, 76988af8bbeSSebastian Andrzej Siewior }; 77088af8bbeSSebastian Andrzej Siewior 771*6e2a512dSDaniel Scally GS_STRINGS_RW(gadget_language, manufacturer); 772*6e2a512dSDaniel Scally GS_STRINGS_RW(gadget_language, product); 773*6e2a512dSDaniel Scally GS_STRINGS_RW(gadget_language, serialnumber); 77488af8bbeSSebastian Andrzej Siewior 775*6e2a512dSDaniel Scally static struct configfs_attribute *gadget_language_langid_attrs[] = { 776*6e2a512dSDaniel Scally &gadget_language_attr_manufacturer, 777*6e2a512dSDaniel Scally &gadget_language_attr_product, 778*6e2a512dSDaniel Scally &gadget_language_attr_serialnumber, 77988af8bbeSSebastian Andrzej Siewior NULL, 78088af8bbeSSebastian Andrzej Siewior }; 78188af8bbeSSebastian Andrzej Siewior 782*6e2a512dSDaniel Scally static void gadget_language_attr_release(struct config_item *item) 78388af8bbeSSebastian Andrzej Siewior { 784*6e2a512dSDaniel Scally struct gadget_language *gs = to_gadget_language(item); 78588af8bbeSSebastian Andrzej Siewior 78688af8bbeSSebastian Andrzej Siewior kfree(gs->manufacturer); 78788af8bbeSSebastian Andrzej Siewior kfree(gs->product); 78888af8bbeSSebastian Andrzej Siewior kfree(gs->serialnumber); 78988af8bbeSSebastian Andrzej Siewior 79088af8bbeSSebastian Andrzej Siewior list_del(&gs->list); 79188af8bbeSSebastian Andrzej Siewior kfree(gs); 79288af8bbeSSebastian Andrzej Siewior } 79388af8bbeSSebastian Andrzej Siewior 794*6e2a512dSDaniel Scally USB_CONFIG_STRING_RW_OPS(gadget_language); 795*6e2a512dSDaniel Scally USB_CONFIG_STRINGS_LANG(gadget_language, gadget_info); 79688af8bbeSSebastian Andrzej Siewior 79793c47394SJó Ágila Bitsch static inline struct gadget_info *webusb_item_to_gadget_info( 79893c47394SJó Ágila Bitsch struct config_item *item) 79993c47394SJó Ágila Bitsch { 80093c47394SJó Ágila Bitsch return container_of(to_config_group(item), 80193c47394SJó Ágila Bitsch struct gadget_info, webusb_group); 80293c47394SJó Ágila Bitsch } 80393c47394SJó Ágila Bitsch 80493c47394SJó Ágila Bitsch static ssize_t webusb_use_show(struct config_item *item, char *page) 80593c47394SJó Ágila Bitsch { 80693c47394SJó Ágila Bitsch return sysfs_emit(page, "%d\n", 80793c47394SJó Ágila Bitsch webusb_item_to_gadget_info(item)->use_webusb); 80893c47394SJó Ágila Bitsch } 80993c47394SJó Ágila Bitsch 81093c47394SJó Ágila Bitsch static ssize_t webusb_use_store(struct config_item *item, const char *page, 81193c47394SJó Ágila Bitsch size_t len) 81293c47394SJó Ágila Bitsch { 81393c47394SJó Ágila Bitsch struct gadget_info *gi = webusb_item_to_gadget_info(item); 81493c47394SJó Ágila Bitsch int ret; 81593c47394SJó Ágila Bitsch bool use; 81693c47394SJó Ágila Bitsch 81793c47394SJó Ágila Bitsch ret = kstrtobool(page, &use); 8186f7fb48dSAndy Shevchenko if (ret) 8196f7fb48dSAndy Shevchenko return ret; 8206f7fb48dSAndy Shevchenko 8216f7fb48dSAndy Shevchenko mutex_lock(&gi->lock); 82293c47394SJó Ágila Bitsch gi->use_webusb = use; 82393c47394SJó Ágila Bitsch mutex_unlock(&gi->lock); 82493c47394SJó Ágila Bitsch 8256f7fb48dSAndy Shevchenko return len; 82693c47394SJó Ágila Bitsch } 82793c47394SJó Ágila Bitsch 82893c47394SJó Ágila Bitsch static ssize_t webusb_bcdVersion_show(struct config_item *item, char *page) 82993c47394SJó Ágila Bitsch { 83093c47394SJó Ágila Bitsch return sysfs_emit(page, "0x%04x\n", 83193c47394SJó Ágila Bitsch webusb_item_to_gadget_info(item)->bcd_webusb_version); 83293c47394SJó Ágila Bitsch } 83393c47394SJó Ágila Bitsch 83493c47394SJó Ágila Bitsch static ssize_t webusb_bcdVersion_store(struct config_item *item, 83593c47394SJó Ágila Bitsch const char *page, size_t len) 83693c47394SJó Ágila Bitsch { 83793c47394SJó Ágila Bitsch struct gadget_info *gi = webusb_item_to_gadget_info(item); 83893c47394SJó Ágila Bitsch u16 bcdVersion; 83993c47394SJó Ágila Bitsch int ret; 84093c47394SJó Ágila Bitsch 84193c47394SJó Ágila Bitsch ret = kstrtou16(page, 0, &bcdVersion); 84293c47394SJó Ágila Bitsch if (ret) 8436f7fb48dSAndy Shevchenko return ret; 8446f7fb48dSAndy Shevchenko 84593c47394SJó Ágila Bitsch ret = is_valid_bcd(bcdVersion); 84693c47394SJó Ágila Bitsch if (ret) 847582cef43SAndy Shevchenko return ret; 84893c47394SJó Ágila Bitsch 849582cef43SAndy Shevchenko mutex_lock(&gi->lock); 85093c47394SJó Ágila Bitsch gi->bcd_webusb_version = bcdVersion; 85193c47394SJó Ágila Bitsch mutex_unlock(&gi->lock); 85293c47394SJó Ágila Bitsch 853582cef43SAndy Shevchenko return len; 85493c47394SJó Ágila Bitsch } 85593c47394SJó Ágila Bitsch 85693c47394SJó Ágila Bitsch static ssize_t webusb_bVendorCode_show(struct config_item *item, char *page) 85793c47394SJó Ágila Bitsch { 85893c47394SJó Ágila Bitsch return sysfs_emit(page, "0x%02x\n", 85993c47394SJó Ágila Bitsch webusb_item_to_gadget_info(item)->b_webusb_vendor_code); 86093c47394SJó Ágila Bitsch } 86193c47394SJó Ágila Bitsch 86293c47394SJó Ágila Bitsch static ssize_t webusb_bVendorCode_store(struct config_item *item, 86393c47394SJó Ágila Bitsch const char *page, size_t len) 86493c47394SJó Ágila Bitsch { 86593c47394SJó Ágila Bitsch struct gadget_info *gi = webusb_item_to_gadget_info(item); 86693c47394SJó Ágila Bitsch int ret; 86793c47394SJó Ágila Bitsch u8 b_vendor_code; 86893c47394SJó Ágila Bitsch 86993c47394SJó Ágila Bitsch ret = kstrtou8(page, 0, &b_vendor_code); 8706f7fb48dSAndy Shevchenko if (ret) 8716f7fb48dSAndy Shevchenko return ret; 8726f7fb48dSAndy Shevchenko 8736f7fb48dSAndy Shevchenko mutex_lock(&gi->lock); 87493c47394SJó Ágila Bitsch gi->b_webusb_vendor_code = b_vendor_code; 87593c47394SJó Ágila Bitsch mutex_unlock(&gi->lock); 87693c47394SJó Ágila Bitsch 8776f7fb48dSAndy Shevchenko return len; 87893c47394SJó Ágila Bitsch } 87993c47394SJó Ágila Bitsch 88093c47394SJó Ágila Bitsch static ssize_t webusb_landingPage_show(struct config_item *item, char *page) 88193c47394SJó Ágila Bitsch { 88293c47394SJó Ágila Bitsch return sysfs_emit(page, "%s\n", webusb_item_to_gadget_info(item)->landing_page); 88393c47394SJó Ágila Bitsch } 88493c47394SJó Ágila Bitsch 88593c47394SJó Ágila Bitsch static ssize_t webusb_landingPage_store(struct config_item *item, const char *page, 88693c47394SJó Ágila Bitsch size_t len) 88793c47394SJó Ágila Bitsch { 88893c47394SJó Ágila Bitsch struct gadget_info *gi = webusb_item_to_gadget_info(item); 88993c47394SJó Ágila Bitsch unsigned int bytes_to_strip = 0; 89093c47394SJó Ágila Bitsch int l = len; 89193c47394SJó Ágila Bitsch 89293c47394SJó Ágila Bitsch if (page[l - 1] == '\n') { 89393c47394SJó Ágila Bitsch --l; 89493c47394SJó Ágila Bitsch ++bytes_to_strip; 89593c47394SJó Ágila Bitsch } 89693c47394SJó Ágila Bitsch 89793c47394SJó Ágila Bitsch if (l > sizeof(gi->landing_page)) { 89893c47394SJó Ágila Bitsch pr_err("webusb: landingPage URL too long\n"); 89993c47394SJó Ágila Bitsch return -EINVAL; 90093c47394SJó Ágila Bitsch } 90193c47394SJó Ágila Bitsch 90293c47394SJó Ágila Bitsch // validation 90393c47394SJó Ágila Bitsch if (strncasecmp(page, "https://", 8) == 0) 90493c47394SJó Ágila Bitsch bytes_to_strip = 8; 90593c47394SJó Ágila Bitsch else if (strncasecmp(page, "http://", 7) == 0) 90693c47394SJó Ágila Bitsch bytes_to_strip = 7; 90793c47394SJó Ágila Bitsch else 90893c47394SJó Ágila Bitsch bytes_to_strip = 0; 90993c47394SJó Ágila Bitsch 91093c47394SJó Ágila Bitsch if (l > U8_MAX - WEBUSB_URL_DESCRIPTOR_HEADER_LENGTH + bytes_to_strip) { 91193c47394SJó Ágila Bitsch pr_err("webusb: landingPage URL %d bytes too long for given URL scheme\n", 91293c47394SJó Ágila Bitsch l - U8_MAX + WEBUSB_URL_DESCRIPTOR_HEADER_LENGTH - bytes_to_strip); 91393c47394SJó Ágila Bitsch return -EINVAL; 91493c47394SJó Ágila Bitsch } 91593c47394SJó Ágila Bitsch 91693c47394SJó Ágila Bitsch mutex_lock(&gi->lock); 91793c47394SJó Ágila Bitsch // ensure 0 bytes are set, in case the new landing page is shorter then the old one. 9189c0e6fbdSAndy Shevchenko memcpy_and_pad(gi->landing_page, sizeof(gi->landing_page), page, l, 0); 91993c47394SJó Ágila Bitsch mutex_unlock(&gi->lock); 92093c47394SJó Ágila Bitsch 92193c47394SJó Ágila Bitsch return len; 92293c47394SJó Ágila Bitsch } 92393c47394SJó Ágila Bitsch 92493c47394SJó Ágila Bitsch CONFIGFS_ATTR(webusb_, use); 92593c47394SJó Ágila Bitsch CONFIGFS_ATTR(webusb_, bVendorCode); 92693c47394SJó Ágila Bitsch CONFIGFS_ATTR(webusb_, bcdVersion); 92793c47394SJó Ágila Bitsch CONFIGFS_ATTR(webusb_, landingPage); 92893c47394SJó Ágila Bitsch 92993c47394SJó Ágila Bitsch static struct configfs_attribute *webusb_attrs[] = { 93093c47394SJó Ágila Bitsch &webusb_attr_use, 93193c47394SJó Ágila Bitsch &webusb_attr_bcdVersion, 93293c47394SJó Ágila Bitsch &webusb_attr_bVendorCode, 93393c47394SJó Ágila Bitsch &webusb_attr_landingPage, 93493c47394SJó Ágila Bitsch NULL, 93593c47394SJó Ágila Bitsch }; 93693c47394SJó Ágila Bitsch 93793c47394SJó Ágila Bitsch static struct config_item_type webusb_type = { 93893c47394SJó Ágila Bitsch .ct_attrs = webusb_attrs, 93993c47394SJó Ágila Bitsch .ct_owner = THIS_MODULE, 94093c47394SJó Ágila Bitsch }; 94193c47394SJó Ágila Bitsch 94245b6a73fSChristoph Hellwig static inline struct gadget_info *os_desc_item_to_gadget_info( 94345b6a73fSChristoph Hellwig struct config_item *item) 94487213d38SAndrzej Pietrasiewicz { 945167a799cSLinyu Yuan return container_of(to_config_group(item), 946167a799cSLinyu Yuan struct gadget_info, os_desc_group); 94787213d38SAndrzej Pietrasiewicz } 94887213d38SAndrzej Pietrasiewicz 94945b6a73fSChristoph Hellwig static ssize_t os_desc_use_show(struct config_item *item, char *page) 95045b6a73fSChristoph Hellwig { 951e800e8cbSStefan Agner return sprintf(page, "%d\n", 95245b6a73fSChristoph Hellwig os_desc_item_to_gadget_info(item)->use_os_desc); 95345b6a73fSChristoph Hellwig } 95445b6a73fSChristoph Hellwig 95545b6a73fSChristoph Hellwig static ssize_t os_desc_use_store(struct config_item *item, const char *page, 95687213d38SAndrzej Pietrasiewicz size_t len) 95787213d38SAndrzej Pietrasiewicz { 95845b6a73fSChristoph Hellwig struct gadget_info *gi = os_desc_item_to_gadget_info(item); 95987213d38SAndrzej Pietrasiewicz int ret; 96087213d38SAndrzej Pietrasiewicz bool use; 96187213d38SAndrzej Pietrasiewicz 962a8bc8cc1SChristophe JAILLET ret = kstrtobool(page, &use); 9636f7fb48dSAndy Shevchenko if (ret) 9646f7fb48dSAndy Shevchenko return ret; 9656f7fb48dSAndy Shevchenko 9666f7fb48dSAndy Shevchenko mutex_lock(&gi->lock); 96787213d38SAndrzej Pietrasiewicz gi->use_os_desc = use; 96887213d38SAndrzej Pietrasiewicz mutex_unlock(&gi->lock); 96987213d38SAndrzej Pietrasiewicz 9706f7fb48dSAndy Shevchenko return len; 97187213d38SAndrzej Pietrasiewicz } 97287213d38SAndrzej Pietrasiewicz 97345b6a73fSChristoph Hellwig static ssize_t os_desc_b_vendor_code_show(struct config_item *item, char *page) 97487213d38SAndrzej Pietrasiewicz { 975e800e8cbSStefan Agner return sprintf(page, "0x%02x\n", 97645b6a73fSChristoph Hellwig os_desc_item_to_gadget_info(item)->b_vendor_code); 97787213d38SAndrzej Pietrasiewicz } 97887213d38SAndrzej Pietrasiewicz 97945b6a73fSChristoph Hellwig static ssize_t os_desc_b_vendor_code_store(struct config_item *item, 98087213d38SAndrzej Pietrasiewicz const char *page, size_t len) 98187213d38SAndrzej Pietrasiewicz { 98245b6a73fSChristoph Hellwig struct gadget_info *gi = os_desc_item_to_gadget_info(item); 98387213d38SAndrzej Pietrasiewicz int ret; 98487213d38SAndrzej Pietrasiewicz u8 b_vendor_code; 98587213d38SAndrzej Pietrasiewicz 98687213d38SAndrzej Pietrasiewicz ret = kstrtou8(page, 0, &b_vendor_code); 9876f7fb48dSAndy Shevchenko if (ret) 9886f7fb48dSAndy Shevchenko return ret; 9896f7fb48dSAndy Shevchenko 9906f7fb48dSAndy Shevchenko mutex_lock(&gi->lock); 99187213d38SAndrzej Pietrasiewicz gi->b_vendor_code = b_vendor_code; 99287213d38SAndrzej Pietrasiewicz mutex_unlock(&gi->lock); 99387213d38SAndrzej Pietrasiewicz 9946f7fb48dSAndy Shevchenko return len; 99587213d38SAndrzej Pietrasiewicz } 99687213d38SAndrzej Pietrasiewicz 99745b6a73fSChristoph Hellwig static ssize_t os_desc_qw_sign_show(struct config_item *item, char *page) 99887213d38SAndrzej Pietrasiewicz { 99945b6a73fSChristoph Hellwig struct gadget_info *gi = os_desc_item_to_gadget_info(item); 100076180d71SStefan Agner int res; 100187213d38SAndrzej Pietrasiewicz 100276180d71SStefan Agner res = utf16s_to_utf8s((wchar_t *) gi->qw_sign, OS_STRING_QW_SIGN_LEN, 100376180d71SStefan Agner UTF16_LITTLE_ENDIAN, page, PAGE_SIZE - 1); 100476180d71SStefan Agner page[res++] = '\n'; 100576180d71SStefan Agner 100676180d71SStefan Agner return res; 100787213d38SAndrzej Pietrasiewicz } 100887213d38SAndrzej Pietrasiewicz 100945b6a73fSChristoph Hellwig static ssize_t os_desc_qw_sign_store(struct config_item *item, const char *page, 101087213d38SAndrzej Pietrasiewicz size_t len) 101187213d38SAndrzej Pietrasiewicz { 101245b6a73fSChristoph Hellwig struct gadget_info *gi = os_desc_item_to_gadget_info(item); 101387213d38SAndrzej Pietrasiewicz int res, l; 101487213d38SAndrzej Pietrasiewicz 101587213d38SAndrzej Pietrasiewicz l = min((int)len, OS_STRING_QW_SIGN_LEN >> 1); 101687213d38SAndrzej Pietrasiewicz if (page[l - 1] == '\n') 101787213d38SAndrzej Pietrasiewicz --l; 101887213d38SAndrzej Pietrasiewicz 101987213d38SAndrzej Pietrasiewicz mutex_lock(&gi->lock); 102087213d38SAndrzej Pietrasiewicz res = utf8s_to_utf16s(page, l, 102187213d38SAndrzej Pietrasiewicz UTF16_LITTLE_ENDIAN, (wchar_t *) gi->qw_sign, 102287213d38SAndrzej Pietrasiewicz OS_STRING_QW_SIGN_LEN); 102387213d38SAndrzej Pietrasiewicz if (res > 0) 102487213d38SAndrzej Pietrasiewicz res = len; 102587213d38SAndrzej Pietrasiewicz mutex_unlock(&gi->lock); 102687213d38SAndrzej Pietrasiewicz 102787213d38SAndrzej Pietrasiewicz return res; 102887213d38SAndrzej Pietrasiewicz } 102987213d38SAndrzej Pietrasiewicz 103045b6a73fSChristoph Hellwig CONFIGFS_ATTR(os_desc_, use); 103145b6a73fSChristoph Hellwig CONFIGFS_ATTR(os_desc_, b_vendor_code); 103245b6a73fSChristoph Hellwig CONFIGFS_ATTR(os_desc_, qw_sign); 103387213d38SAndrzej Pietrasiewicz 103487213d38SAndrzej Pietrasiewicz static struct configfs_attribute *os_desc_attrs[] = { 103545b6a73fSChristoph Hellwig &os_desc_attr_use, 103645b6a73fSChristoph Hellwig &os_desc_attr_b_vendor_code, 103745b6a73fSChristoph Hellwig &os_desc_attr_qw_sign, 103887213d38SAndrzej Pietrasiewicz NULL, 103987213d38SAndrzej Pietrasiewicz }; 104087213d38SAndrzej Pietrasiewicz 1041da424314SAndrzej Pietrasiewicz static int os_desc_link(struct config_item *os_desc_ci, 1042da424314SAndrzej Pietrasiewicz struct config_item *usb_cfg_ci) 1043da424314SAndrzej Pietrasiewicz { 1044167a799cSLinyu Yuan struct gadget_info *gi = os_desc_item_to_gadget_info(os_desc_ci); 1045da424314SAndrzej Pietrasiewicz struct usb_composite_dev *cdev = &gi->cdev; 10465d143ec4SLinyu Yuan struct config_usb_cfg *c_target = to_config_usb_cfg(usb_cfg_ci); 104736f4c25cSJakob Koschel struct usb_configuration *c = NULL, *iter; 1048da424314SAndrzej Pietrasiewicz int ret; 1049da424314SAndrzej Pietrasiewicz 1050da424314SAndrzej Pietrasiewicz mutex_lock(&gi->lock); 105136f4c25cSJakob Koschel list_for_each_entry(iter, &cdev->configs, list) { 105236f4c25cSJakob Koschel if (iter != &c_target->c) 105336f4c25cSJakob Koschel continue; 105436f4c25cSJakob Koschel c = iter; 1055da424314SAndrzej Pietrasiewicz break; 1056da424314SAndrzej Pietrasiewicz } 105736f4c25cSJakob Koschel if (!c) { 1058da424314SAndrzej Pietrasiewicz ret = -EINVAL; 1059da424314SAndrzej Pietrasiewicz goto out; 1060da424314SAndrzej Pietrasiewicz } 1061da424314SAndrzej Pietrasiewicz 1062da424314SAndrzej Pietrasiewicz if (cdev->os_desc_config) { 1063da424314SAndrzej Pietrasiewicz ret = -EBUSY; 1064da424314SAndrzej Pietrasiewicz goto out; 1065da424314SAndrzej Pietrasiewicz } 1066da424314SAndrzej Pietrasiewicz 1067da424314SAndrzej Pietrasiewicz cdev->os_desc_config = &c_target->c; 1068da424314SAndrzej Pietrasiewicz ret = 0; 1069da424314SAndrzej Pietrasiewicz 1070da424314SAndrzej Pietrasiewicz out: 1071da424314SAndrzej Pietrasiewicz mutex_unlock(&gi->lock); 1072da424314SAndrzej Pietrasiewicz return ret; 1073da424314SAndrzej Pietrasiewicz } 1074da424314SAndrzej Pietrasiewicz 1075e16769d4SAndrzej Pietrasiewicz static void os_desc_unlink(struct config_item *os_desc_ci, 1076da424314SAndrzej Pietrasiewicz struct config_item *usb_cfg_ci) 1077da424314SAndrzej Pietrasiewicz { 1078167a799cSLinyu Yuan struct gadget_info *gi = os_desc_item_to_gadget_info(os_desc_ci); 1079da424314SAndrzej Pietrasiewicz struct usb_composite_dev *cdev = &gi->cdev; 1080da424314SAndrzej Pietrasiewicz 1081da424314SAndrzej Pietrasiewicz mutex_lock(&gi->lock); 1082afdaadc3SRuslan Bilovol if (gi->composite.gadget_driver.udc_name) 1083da424314SAndrzej Pietrasiewicz unregister_gadget(gi); 1084da424314SAndrzej Pietrasiewicz cdev->os_desc_config = NULL; 1085afdaadc3SRuslan Bilovol WARN_ON(gi->composite.gadget_driver.udc_name); 1086da424314SAndrzej Pietrasiewicz mutex_unlock(&gi->lock); 1087da424314SAndrzej Pietrasiewicz } 1088da424314SAndrzej Pietrasiewicz 108987213d38SAndrzej Pietrasiewicz static struct configfs_item_operations os_desc_ops = { 1090da424314SAndrzej Pietrasiewicz .allow_link = os_desc_link, 1091da424314SAndrzej Pietrasiewicz .drop_link = os_desc_unlink, 109287213d38SAndrzej Pietrasiewicz }; 109387213d38SAndrzej Pietrasiewicz 109487213d38SAndrzej Pietrasiewicz static struct config_item_type os_desc_type = { 109587213d38SAndrzej Pietrasiewicz .ct_item_ops = &os_desc_ops, 109687213d38SAndrzej Pietrasiewicz .ct_attrs = os_desc_attrs, 109787213d38SAndrzej Pietrasiewicz .ct_owner = THIS_MODULE, 109887213d38SAndrzej Pietrasiewicz }; 109987213d38SAndrzej Pietrasiewicz 11007419485fSAndrzej Pietrasiewicz static inline struct usb_os_desc_ext_prop 11017419485fSAndrzej Pietrasiewicz *to_usb_os_desc_ext_prop(struct config_item *item) 11027419485fSAndrzej Pietrasiewicz { 11037419485fSAndrzej Pietrasiewicz return container_of(item, struct usb_os_desc_ext_prop, item); 11047419485fSAndrzej Pietrasiewicz } 11057419485fSAndrzej Pietrasiewicz 110645b6a73fSChristoph Hellwig static ssize_t ext_prop_type_show(struct config_item *item, char *page) 11077419485fSAndrzej Pietrasiewicz { 1108e800e8cbSStefan Agner return sprintf(page, "%d\n", to_usb_os_desc_ext_prop(item)->type); 11097419485fSAndrzej Pietrasiewicz } 11107419485fSAndrzej Pietrasiewicz 111145b6a73fSChristoph Hellwig static ssize_t ext_prop_type_store(struct config_item *item, 11127419485fSAndrzej Pietrasiewicz const char *page, size_t len) 11137419485fSAndrzej Pietrasiewicz { 111445b6a73fSChristoph Hellwig struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item); 11157419485fSAndrzej Pietrasiewicz struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent); 11167419485fSAndrzej Pietrasiewicz u8 type; 11177419485fSAndrzej Pietrasiewicz int ret; 11187419485fSAndrzej Pietrasiewicz 11197419485fSAndrzej Pietrasiewicz ret = kstrtou8(page, 0, &type); 11207419485fSAndrzej Pietrasiewicz if (ret) 11216f7fb48dSAndy Shevchenko return ret; 11226f7fb48dSAndy Shevchenko 11236f7fb48dSAndy Shevchenko if (type < USB_EXT_PROP_UNICODE || type > USB_EXT_PROP_UNICODE_MULTI) 11246f7fb48dSAndy Shevchenko return -EINVAL; 11256f7fb48dSAndy Shevchenko 11266f7fb48dSAndy Shevchenko if (desc->opts_mutex) 11276f7fb48dSAndy Shevchenko mutex_lock(desc->opts_mutex); 11287419485fSAndrzej Pietrasiewicz 11297419485fSAndrzej Pietrasiewicz if ((ext_prop->type == USB_EXT_PROP_BINARY || 11307419485fSAndrzej Pietrasiewicz ext_prop->type == USB_EXT_PROP_LE32 || 11317419485fSAndrzej Pietrasiewicz ext_prop->type == USB_EXT_PROP_BE32) && 11327419485fSAndrzej Pietrasiewicz (type == USB_EXT_PROP_UNICODE || 11337419485fSAndrzej Pietrasiewicz type == USB_EXT_PROP_UNICODE_ENV || 11347419485fSAndrzej Pietrasiewicz type == USB_EXT_PROP_UNICODE_LINK)) 11357419485fSAndrzej Pietrasiewicz ext_prop->data_len <<= 1; 11367419485fSAndrzej Pietrasiewicz else if ((ext_prop->type == USB_EXT_PROP_UNICODE || 11377419485fSAndrzej Pietrasiewicz ext_prop->type == USB_EXT_PROP_UNICODE_ENV || 11387419485fSAndrzej Pietrasiewicz ext_prop->type == USB_EXT_PROP_UNICODE_LINK) && 11397419485fSAndrzej Pietrasiewicz (type == USB_EXT_PROP_BINARY || 11407419485fSAndrzej Pietrasiewicz type == USB_EXT_PROP_LE32 || 11417419485fSAndrzej Pietrasiewicz type == USB_EXT_PROP_BE32)) 11427419485fSAndrzej Pietrasiewicz ext_prop->data_len >>= 1; 11437419485fSAndrzej Pietrasiewicz ext_prop->type = type; 11447419485fSAndrzej Pietrasiewicz 11457419485fSAndrzej Pietrasiewicz if (desc->opts_mutex) 11467419485fSAndrzej Pietrasiewicz mutex_unlock(desc->opts_mutex); 11476f7fb48dSAndy Shevchenko return len; 11487419485fSAndrzej Pietrasiewicz } 11497419485fSAndrzej Pietrasiewicz 115045b6a73fSChristoph Hellwig static ssize_t ext_prop_data_show(struct config_item *item, char *page) 11517419485fSAndrzej Pietrasiewicz { 115245b6a73fSChristoph Hellwig struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item); 11537419485fSAndrzej Pietrasiewicz int len = ext_prop->data_len; 11547419485fSAndrzej Pietrasiewicz 11557419485fSAndrzej Pietrasiewicz if (ext_prop->type == USB_EXT_PROP_UNICODE || 11567419485fSAndrzej Pietrasiewicz ext_prop->type == USB_EXT_PROP_UNICODE_ENV || 11577419485fSAndrzej Pietrasiewicz ext_prop->type == USB_EXT_PROP_UNICODE_LINK) 11587419485fSAndrzej Pietrasiewicz len >>= 1; 11597419485fSAndrzej Pietrasiewicz memcpy(page, ext_prop->data, len); 11607419485fSAndrzej Pietrasiewicz 11617419485fSAndrzej Pietrasiewicz return len; 11627419485fSAndrzej Pietrasiewicz } 11637419485fSAndrzej Pietrasiewicz 116445b6a73fSChristoph Hellwig static ssize_t ext_prop_data_store(struct config_item *item, 11657419485fSAndrzej Pietrasiewicz const char *page, size_t len) 11667419485fSAndrzej Pietrasiewicz { 116745b6a73fSChristoph Hellwig struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item); 11687419485fSAndrzej Pietrasiewicz struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent); 11697419485fSAndrzej Pietrasiewicz char *new_data; 11707419485fSAndrzej Pietrasiewicz size_t ret_len = len; 11717419485fSAndrzej Pietrasiewicz 11727419485fSAndrzej Pietrasiewicz if (page[len - 1] == '\n' || page[len - 1] == '\0') 11737419485fSAndrzej Pietrasiewicz --len; 117458b949e0SBenoit Taine new_data = kmemdup(page, len, GFP_KERNEL); 11757419485fSAndrzej Pietrasiewicz if (!new_data) 11767419485fSAndrzej Pietrasiewicz return -ENOMEM; 11777419485fSAndrzej Pietrasiewicz 11787419485fSAndrzej Pietrasiewicz if (desc->opts_mutex) 11797419485fSAndrzej Pietrasiewicz mutex_lock(desc->opts_mutex); 11807419485fSAndrzej Pietrasiewicz kfree(ext_prop->data); 11817419485fSAndrzej Pietrasiewicz ext_prop->data = new_data; 11827419485fSAndrzej Pietrasiewicz desc->ext_prop_len -= ext_prop->data_len; 11837419485fSAndrzej Pietrasiewicz ext_prop->data_len = len; 11847419485fSAndrzej Pietrasiewicz desc->ext_prop_len += ext_prop->data_len; 11857419485fSAndrzej Pietrasiewicz if (ext_prop->type == USB_EXT_PROP_UNICODE || 11867419485fSAndrzej Pietrasiewicz ext_prop->type == USB_EXT_PROP_UNICODE_ENV || 11877419485fSAndrzej Pietrasiewicz ext_prop->type == USB_EXT_PROP_UNICODE_LINK) { 11887419485fSAndrzej Pietrasiewicz desc->ext_prop_len -= ext_prop->data_len; 11897419485fSAndrzej Pietrasiewicz ext_prop->data_len <<= 1; 11907419485fSAndrzej Pietrasiewicz ext_prop->data_len += 2; 11917419485fSAndrzej Pietrasiewicz desc->ext_prop_len += ext_prop->data_len; 11927419485fSAndrzej Pietrasiewicz } 11937419485fSAndrzej Pietrasiewicz if (desc->opts_mutex) 11947419485fSAndrzej Pietrasiewicz mutex_unlock(desc->opts_mutex); 11957419485fSAndrzej Pietrasiewicz return ret_len; 11967419485fSAndrzej Pietrasiewicz } 11977419485fSAndrzej Pietrasiewicz 119845b6a73fSChristoph Hellwig CONFIGFS_ATTR(ext_prop_, type); 119945b6a73fSChristoph Hellwig CONFIGFS_ATTR(ext_prop_, data); 12007419485fSAndrzej Pietrasiewicz 12017419485fSAndrzej Pietrasiewicz static struct configfs_attribute *ext_prop_attrs[] = { 120245b6a73fSChristoph Hellwig &ext_prop_attr_type, 120345b6a73fSChristoph Hellwig &ext_prop_attr_data, 12047419485fSAndrzej Pietrasiewicz NULL, 12057419485fSAndrzej Pietrasiewicz }; 12067419485fSAndrzej Pietrasiewicz 12077419485fSAndrzej Pietrasiewicz static void usb_os_desc_ext_prop_release(struct config_item *item) 12087419485fSAndrzej Pietrasiewicz { 12097419485fSAndrzej Pietrasiewicz struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item); 12107419485fSAndrzej Pietrasiewicz 12117419485fSAndrzej Pietrasiewicz kfree(ext_prop); /* frees a whole chunk */ 12127419485fSAndrzej Pietrasiewicz } 12137419485fSAndrzej Pietrasiewicz 12147419485fSAndrzej Pietrasiewicz static struct configfs_item_operations ext_prop_ops = { 12157419485fSAndrzej Pietrasiewicz .release = usb_os_desc_ext_prop_release, 12167419485fSAndrzej Pietrasiewicz }; 12177419485fSAndrzej Pietrasiewicz 12187419485fSAndrzej Pietrasiewicz static struct config_item *ext_prop_make( 12197419485fSAndrzej Pietrasiewicz struct config_group *group, 12207419485fSAndrzej Pietrasiewicz const char *name) 12217419485fSAndrzej Pietrasiewicz { 12227419485fSAndrzej Pietrasiewicz struct usb_os_desc_ext_prop *ext_prop; 12237419485fSAndrzej Pietrasiewicz struct config_item_type *ext_prop_type; 12247419485fSAndrzej Pietrasiewicz struct usb_os_desc *desc; 12257419485fSAndrzej Pietrasiewicz char *vlabuf; 12267419485fSAndrzej Pietrasiewicz 12277419485fSAndrzej Pietrasiewicz vla_group(data_chunk); 12287419485fSAndrzej Pietrasiewicz vla_item(data_chunk, struct usb_os_desc_ext_prop, ext_prop, 1); 12297419485fSAndrzej Pietrasiewicz vla_item(data_chunk, struct config_item_type, ext_prop_type, 1); 12307419485fSAndrzej Pietrasiewicz 12317419485fSAndrzej Pietrasiewicz vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL); 12327419485fSAndrzej Pietrasiewicz if (!vlabuf) 12337419485fSAndrzej Pietrasiewicz return ERR_PTR(-ENOMEM); 12347419485fSAndrzej Pietrasiewicz 12357419485fSAndrzej Pietrasiewicz ext_prop = vla_ptr(vlabuf, data_chunk, ext_prop); 12367419485fSAndrzej Pietrasiewicz ext_prop_type = vla_ptr(vlabuf, data_chunk, ext_prop_type); 12377419485fSAndrzej Pietrasiewicz 12387419485fSAndrzej Pietrasiewicz desc = container_of(group, struct usb_os_desc, group); 12397419485fSAndrzej Pietrasiewicz ext_prop_type->ct_item_ops = &ext_prop_ops; 12407419485fSAndrzej Pietrasiewicz ext_prop_type->ct_attrs = ext_prop_attrs; 12417419485fSAndrzej Pietrasiewicz ext_prop_type->ct_owner = desc->owner; 12427419485fSAndrzej Pietrasiewicz 12437419485fSAndrzej Pietrasiewicz config_item_init_type_name(&ext_prop->item, name, ext_prop_type); 12447419485fSAndrzej Pietrasiewicz 12457419485fSAndrzej Pietrasiewicz ext_prop->name = kstrdup(name, GFP_KERNEL); 12467419485fSAndrzej Pietrasiewicz if (!ext_prop->name) { 12477419485fSAndrzej Pietrasiewicz kfree(vlabuf); 12487419485fSAndrzej Pietrasiewicz return ERR_PTR(-ENOMEM); 12497419485fSAndrzej Pietrasiewicz } 12507419485fSAndrzej Pietrasiewicz desc->ext_prop_len += 14; 12517419485fSAndrzej Pietrasiewicz ext_prop->name_len = 2 * strlen(ext_prop->name) + 2; 12527419485fSAndrzej Pietrasiewicz if (desc->opts_mutex) 12537419485fSAndrzej Pietrasiewicz mutex_lock(desc->opts_mutex); 12547419485fSAndrzej Pietrasiewicz desc->ext_prop_len += ext_prop->name_len; 12557419485fSAndrzej Pietrasiewicz list_add_tail(&ext_prop->entry, &desc->ext_prop); 12567419485fSAndrzej Pietrasiewicz ++desc->ext_prop_count; 12577419485fSAndrzej Pietrasiewicz if (desc->opts_mutex) 12587419485fSAndrzej Pietrasiewicz mutex_unlock(desc->opts_mutex); 12597419485fSAndrzej Pietrasiewicz 12607419485fSAndrzej Pietrasiewicz return &ext_prop->item; 12617419485fSAndrzej Pietrasiewicz } 12627419485fSAndrzej Pietrasiewicz 12637419485fSAndrzej Pietrasiewicz static void ext_prop_drop(struct config_group *group, struct config_item *item) 12647419485fSAndrzej Pietrasiewicz { 12657419485fSAndrzej Pietrasiewicz struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item); 12667419485fSAndrzej Pietrasiewicz struct usb_os_desc *desc = to_usb_os_desc(&group->cg_item); 12677419485fSAndrzej Pietrasiewicz 12687419485fSAndrzej Pietrasiewicz if (desc->opts_mutex) 12697419485fSAndrzej Pietrasiewicz mutex_lock(desc->opts_mutex); 12707419485fSAndrzej Pietrasiewicz list_del(&ext_prop->entry); 12717419485fSAndrzej Pietrasiewicz --desc->ext_prop_count; 12727419485fSAndrzej Pietrasiewicz kfree(ext_prop->name); 12737419485fSAndrzej Pietrasiewicz desc->ext_prop_len -= (ext_prop->name_len + ext_prop->data_len + 14); 12747419485fSAndrzej Pietrasiewicz if (desc->opts_mutex) 12757419485fSAndrzej Pietrasiewicz mutex_unlock(desc->opts_mutex); 12767419485fSAndrzej Pietrasiewicz config_item_put(item); 12777419485fSAndrzej Pietrasiewicz } 12787419485fSAndrzej Pietrasiewicz 12797419485fSAndrzej Pietrasiewicz static struct configfs_group_operations interf_grp_ops = { 12807419485fSAndrzej Pietrasiewicz .make_item = &ext_prop_make, 12817419485fSAndrzej Pietrasiewicz .drop_item = &ext_prop_drop, 12827419485fSAndrzej Pietrasiewicz }; 12837419485fSAndrzej Pietrasiewicz 128445b6a73fSChristoph Hellwig static ssize_t interf_grp_compatible_id_show(struct config_item *item, 1285da424314SAndrzej Pietrasiewicz char *page) 1286da424314SAndrzej Pietrasiewicz { 128745b6a73fSChristoph Hellwig memcpy(page, to_usb_os_desc(item)->ext_compat_id, 8); 1288da424314SAndrzej Pietrasiewicz return 8; 1289da424314SAndrzej Pietrasiewicz } 1290da424314SAndrzej Pietrasiewicz 129145b6a73fSChristoph Hellwig static ssize_t interf_grp_compatible_id_store(struct config_item *item, 1292da424314SAndrzej Pietrasiewicz const char *page, size_t len) 1293da424314SAndrzej Pietrasiewicz { 129445b6a73fSChristoph Hellwig struct usb_os_desc *desc = to_usb_os_desc(item); 1295da424314SAndrzej Pietrasiewicz int l; 1296da424314SAndrzej Pietrasiewicz 1297da424314SAndrzej Pietrasiewicz l = min_t(int, 8, len); 1298da424314SAndrzej Pietrasiewicz if (page[l - 1] == '\n') 1299da424314SAndrzej Pietrasiewicz --l; 1300da424314SAndrzej Pietrasiewicz if (desc->opts_mutex) 1301da424314SAndrzej Pietrasiewicz mutex_lock(desc->opts_mutex); 1302da424314SAndrzej Pietrasiewicz memcpy(desc->ext_compat_id, page, l); 1303da424314SAndrzej Pietrasiewicz 1304da424314SAndrzej Pietrasiewicz if (desc->opts_mutex) 1305da424314SAndrzej Pietrasiewicz mutex_unlock(desc->opts_mutex); 1306da424314SAndrzej Pietrasiewicz 1307da424314SAndrzej Pietrasiewicz return len; 1308da424314SAndrzej Pietrasiewicz } 1309da424314SAndrzej Pietrasiewicz 131045b6a73fSChristoph Hellwig static ssize_t interf_grp_sub_compatible_id_show(struct config_item *item, 1311da424314SAndrzej Pietrasiewicz char *page) 1312da424314SAndrzej Pietrasiewicz { 131345b6a73fSChristoph Hellwig memcpy(page, to_usb_os_desc(item)->ext_compat_id + 8, 8); 1314da424314SAndrzej Pietrasiewicz return 8; 1315da424314SAndrzej Pietrasiewicz } 1316da424314SAndrzej Pietrasiewicz 131745b6a73fSChristoph Hellwig static ssize_t interf_grp_sub_compatible_id_store(struct config_item *item, 1318da424314SAndrzej Pietrasiewicz const char *page, size_t len) 1319da424314SAndrzej Pietrasiewicz { 132045b6a73fSChristoph Hellwig struct usb_os_desc *desc = to_usb_os_desc(item); 1321da424314SAndrzej Pietrasiewicz int l; 1322da424314SAndrzej Pietrasiewicz 1323da424314SAndrzej Pietrasiewicz l = min_t(int, 8, len); 1324da424314SAndrzej Pietrasiewicz if (page[l - 1] == '\n') 1325da424314SAndrzej Pietrasiewicz --l; 1326da424314SAndrzej Pietrasiewicz if (desc->opts_mutex) 1327da424314SAndrzej Pietrasiewicz mutex_lock(desc->opts_mutex); 1328da424314SAndrzej Pietrasiewicz memcpy(desc->ext_compat_id + 8, page, l); 1329da424314SAndrzej Pietrasiewicz 1330da424314SAndrzej Pietrasiewicz if (desc->opts_mutex) 1331da424314SAndrzej Pietrasiewicz mutex_unlock(desc->opts_mutex); 1332da424314SAndrzej Pietrasiewicz 1333da424314SAndrzej Pietrasiewicz return len; 1334da424314SAndrzej Pietrasiewicz } 1335da424314SAndrzej Pietrasiewicz 133645b6a73fSChristoph Hellwig CONFIGFS_ATTR(interf_grp_, compatible_id); 133745b6a73fSChristoph Hellwig CONFIGFS_ATTR(interf_grp_, sub_compatible_id); 1338da424314SAndrzej Pietrasiewicz 1339da424314SAndrzej Pietrasiewicz static struct configfs_attribute *interf_grp_attrs[] = { 134045b6a73fSChristoph Hellwig &interf_grp_attr_compatible_id, 134145b6a73fSChristoph Hellwig &interf_grp_attr_sub_compatible_id, 1342da424314SAndrzej Pietrasiewicz NULL 1343da424314SAndrzej Pietrasiewicz }; 1344da424314SAndrzej Pietrasiewicz 1345ff74745eSAndrew Gabbasov struct config_group *usb_os_desc_prepare_interf_dir( 1346ff74745eSAndrew Gabbasov struct config_group *parent, 1347da424314SAndrzej Pietrasiewicz int n_interf, 1348da424314SAndrzej Pietrasiewicz struct usb_os_desc **desc, 134914574b54SAndrzej Pietrasiewicz char **names, 1350da424314SAndrzej Pietrasiewicz struct module *owner) 1351da424314SAndrzej Pietrasiewicz { 13521ae1602dSChristoph Hellwig struct config_group *os_desc_group; 1353da424314SAndrzej Pietrasiewicz struct config_item_type *os_desc_type, *interface_type; 1354da424314SAndrzej Pietrasiewicz 1355da424314SAndrzej Pietrasiewicz vla_group(data_chunk); 1356da424314SAndrzej Pietrasiewicz vla_item(data_chunk, struct config_group, os_desc_group, 1); 1357da424314SAndrzej Pietrasiewicz vla_item(data_chunk, struct config_item_type, os_desc_type, 1); 1358da424314SAndrzej Pietrasiewicz vla_item(data_chunk, struct config_item_type, interface_type, 1); 1359da424314SAndrzej Pietrasiewicz 1360da424314SAndrzej Pietrasiewicz char *vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL); 1361da424314SAndrzej Pietrasiewicz if (!vlabuf) 1362ff74745eSAndrew Gabbasov return ERR_PTR(-ENOMEM); 1363da424314SAndrzej Pietrasiewicz 1364da424314SAndrzej Pietrasiewicz os_desc_group = vla_ptr(vlabuf, data_chunk, os_desc_group); 1365da424314SAndrzej Pietrasiewicz os_desc_type = vla_ptr(vlabuf, data_chunk, os_desc_type); 1366da424314SAndrzej Pietrasiewicz interface_type = vla_ptr(vlabuf, data_chunk, interface_type); 1367da424314SAndrzej Pietrasiewicz 1368da424314SAndrzej Pietrasiewicz os_desc_type->ct_owner = owner; 1369da424314SAndrzej Pietrasiewicz config_group_init_type_name(os_desc_group, "os_desc", os_desc_type); 13701ae1602dSChristoph Hellwig configfs_add_default_group(os_desc_group, parent); 1371da424314SAndrzej Pietrasiewicz 13727419485fSAndrzej Pietrasiewicz interface_type->ct_group_ops = &interf_grp_ops; 1373da424314SAndrzej Pietrasiewicz interface_type->ct_attrs = interf_grp_attrs; 1374da424314SAndrzej Pietrasiewicz interface_type->ct_owner = owner; 1375da424314SAndrzej Pietrasiewicz 1376da424314SAndrzej Pietrasiewicz while (n_interf--) { 1377da424314SAndrzej Pietrasiewicz struct usb_os_desc *d; 1378da424314SAndrzej Pietrasiewicz 1379da424314SAndrzej Pietrasiewicz d = desc[n_interf]; 13807419485fSAndrzej Pietrasiewicz d->owner = owner; 1381da424314SAndrzej Pietrasiewicz config_group_init_type_name(&d->group, "", interface_type); 138214574b54SAndrzej Pietrasiewicz config_item_set_name(&d->group.cg_item, "interface.%s", 138314574b54SAndrzej Pietrasiewicz names[n_interf]); 13841ae1602dSChristoph Hellwig configfs_add_default_group(&d->group, os_desc_group); 1385da424314SAndrzej Pietrasiewicz } 1386da424314SAndrzej Pietrasiewicz 1387ff74745eSAndrew Gabbasov return os_desc_group; 1388da424314SAndrzej Pietrasiewicz } 1389da424314SAndrzej Pietrasiewicz EXPORT_SYMBOL(usb_os_desc_prepare_interf_dir); 1390da424314SAndrzej Pietrasiewicz 139188af8bbeSSebastian Andrzej Siewior static int configfs_do_nothing(struct usb_composite_dev *cdev) 139288af8bbeSSebastian Andrzej Siewior { 139375bfe23aSDavid Rientjes WARN_ON(1); 139488af8bbeSSebastian Andrzej Siewior return -EINVAL; 139588af8bbeSSebastian Andrzej Siewior } 139688af8bbeSSebastian Andrzej Siewior 139788af8bbeSSebastian Andrzej Siewior int composite_dev_prepare(struct usb_composite_driver *composite, 139888af8bbeSSebastian Andrzej Siewior struct usb_composite_dev *dev); 139988af8bbeSSebastian Andrzej Siewior 1400da424314SAndrzej Pietrasiewicz int composite_os_desc_req_prepare(struct usb_composite_dev *cdev, 1401da424314SAndrzej Pietrasiewicz struct usb_ep *ep0); 1402da424314SAndrzej Pietrasiewicz 140388af8bbeSSebastian Andrzej Siewior static void purge_configs_funcs(struct gadget_info *gi) 140488af8bbeSSebastian Andrzej Siewior { 140588af8bbeSSebastian Andrzej Siewior struct usb_configuration *c; 140688af8bbeSSebastian Andrzej Siewior 140788af8bbeSSebastian Andrzej Siewior list_for_each_entry(c, &gi->cdev.configs, list) { 140888af8bbeSSebastian Andrzej Siewior struct usb_function *f, *tmp; 140988af8bbeSSebastian Andrzej Siewior struct config_usb_cfg *cfg; 141088af8bbeSSebastian Andrzej Siewior 141188af8bbeSSebastian Andrzej Siewior cfg = container_of(c, struct config_usb_cfg, c); 141288af8bbeSSebastian Andrzej Siewior 14136cd0fe91SChandana Kishori Chiluveru list_for_each_entry_safe_reverse(f, tmp, &c->functions, list) { 141488af8bbeSSebastian Andrzej Siewior 14156cd0fe91SChandana Kishori Chiluveru list_move(&f->list, &cfg->func_list); 141688af8bbeSSebastian Andrzej Siewior if (f->unbind) { 1417da7b895dSRomain Izard dev_dbg(&gi->cdev.gadget->dev, 1418a08f5dbfSRomain Izard "unbind function '%s'/%p\n", 1419a08f5dbfSRomain Izard f->name, f); 142088af8bbeSSebastian Andrzej Siewior f->unbind(c, f); 142188af8bbeSSebastian Andrzej Siewior } 142288af8bbeSSebastian Andrzej Siewior } 142388af8bbeSSebastian Andrzej Siewior c->next_interface_id = 0; 1424903124feSKrzysztof Opasiak memset(c->interface, 0, sizeof(c->interface)); 1425554eead5SJohn Youn c->superspeed_plus = 0; 142688af8bbeSSebastian Andrzej Siewior c->superspeed = 0; 142788af8bbeSSebastian Andrzej Siewior c->highspeed = 0; 142888af8bbeSSebastian Andrzej Siewior c->fullspeed = 0; 142988af8bbeSSebastian Andrzej Siewior } 143088af8bbeSSebastian Andrzej Siewior } 143188af8bbeSSebastian Andrzej Siewior 143288af8bbeSSebastian Andrzej Siewior static int configfs_composite_bind(struct usb_gadget *gadget, 143388af8bbeSSebastian Andrzej Siewior struct usb_gadget_driver *gdriver) 143488af8bbeSSebastian Andrzej Siewior { 143588af8bbeSSebastian Andrzej Siewior struct usb_composite_driver *composite = to_cdriver(gdriver); 143688af8bbeSSebastian Andrzej Siewior struct gadget_info *gi = container_of(composite, 143788af8bbeSSebastian Andrzej Siewior struct gadget_info, composite); 143888af8bbeSSebastian Andrzej Siewior struct usb_composite_dev *cdev = &gi->cdev; 143988af8bbeSSebastian Andrzej Siewior struct usb_configuration *c; 144088af8bbeSSebastian Andrzej Siewior struct usb_string *s; 144188af8bbeSSebastian Andrzej Siewior unsigned i; 144288af8bbeSSebastian Andrzej Siewior int ret; 144388af8bbeSSebastian Andrzej Siewior 144488af8bbeSSebastian Andrzej Siewior /* the gi->lock is hold by the caller */ 14451a1c851bSPeter Chen gi->unbind = 0; 144688af8bbeSSebastian Andrzej Siewior cdev->gadget = gadget; 144788af8bbeSSebastian Andrzej Siewior set_gadget_data(gadget, cdev); 144888af8bbeSSebastian Andrzej Siewior ret = composite_dev_prepare(composite, cdev); 144988af8bbeSSebastian Andrzej Siewior if (ret) 145088af8bbeSSebastian Andrzej Siewior return ret; 145188af8bbeSSebastian Andrzej Siewior /* and now the gadget bind */ 145288af8bbeSSebastian Andrzej Siewior ret = -EINVAL; 145388af8bbeSSebastian Andrzej Siewior 145488af8bbeSSebastian Andrzej Siewior if (list_empty(&gi->cdev.configs)) { 145588af8bbeSSebastian Andrzej Siewior pr_err("Need at least one configuration in %s.\n", 145688af8bbeSSebastian Andrzej Siewior gi->composite.name); 145788af8bbeSSebastian Andrzej Siewior goto err_comp_cleanup; 145888af8bbeSSebastian Andrzej Siewior } 145988af8bbeSSebastian Andrzej Siewior 146088af8bbeSSebastian Andrzej Siewior 146188af8bbeSSebastian Andrzej Siewior list_for_each_entry(c, &gi->cdev.configs, list) { 146288af8bbeSSebastian Andrzej Siewior struct config_usb_cfg *cfg; 146388af8bbeSSebastian Andrzej Siewior 146488af8bbeSSebastian Andrzej Siewior cfg = container_of(c, struct config_usb_cfg, c); 146588af8bbeSSebastian Andrzej Siewior if (list_empty(&cfg->func_list)) { 146688af8bbeSSebastian Andrzej Siewior pr_err("Config %s/%d of %s needs at least one function.\n", 146788af8bbeSSebastian Andrzej Siewior c->label, c->bConfigurationValue, 146888af8bbeSSebastian Andrzej Siewior gi->composite.name); 146988af8bbeSSebastian Andrzej Siewior goto err_comp_cleanup; 147088af8bbeSSebastian Andrzej Siewior } 147188af8bbeSSebastian Andrzej Siewior } 147288af8bbeSSebastian Andrzej Siewior 147388af8bbeSSebastian Andrzej Siewior /* init all strings */ 147488af8bbeSSebastian Andrzej Siewior if (!list_empty(&gi->string_list)) { 1475*6e2a512dSDaniel Scally struct gadget_language *gs; 147688af8bbeSSebastian Andrzej Siewior 147788af8bbeSSebastian Andrzej Siewior i = 0; 147888af8bbeSSebastian Andrzej Siewior list_for_each_entry(gs, &gi->string_list, list) { 147988af8bbeSSebastian Andrzej Siewior 148088af8bbeSSebastian Andrzej Siewior gi->gstrings[i] = &gs->stringtab_dev; 148188af8bbeSSebastian Andrzej Siewior gs->stringtab_dev.strings = gs->strings; 148288af8bbeSSebastian Andrzej Siewior gs->strings[USB_GADGET_MANUFACTURER_IDX].s = 148388af8bbeSSebastian Andrzej Siewior gs->manufacturer; 148488af8bbeSSebastian Andrzej Siewior gs->strings[USB_GADGET_PRODUCT_IDX].s = gs->product; 148588af8bbeSSebastian Andrzej Siewior gs->strings[USB_GADGET_SERIAL_IDX].s = gs->serialnumber; 148688af8bbeSSebastian Andrzej Siewior i++; 148788af8bbeSSebastian Andrzej Siewior } 148888af8bbeSSebastian Andrzej Siewior gi->gstrings[i] = NULL; 148988af8bbeSSebastian Andrzej Siewior s = usb_gstrings_attach(&gi->cdev, gi->gstrings, 149088af8bbeSSebastian Andrzej Siewior USB_GADGET_FIRST_AVAIL_IDX); 1491fea77077SWei Yongjun if (IS_ERR(s)) { 1492fea77077SWei Yongjun ret = PTR_ERR(s); 149388af8bbeSSebastian Andrzej Siewior goto err_comp_cleanup; 1494fea77077SWei Yongjun } 149588af8bbeSSebastian Andrzej Siewior 149688af8bbeSSebastian Andrzej Siewior gi->cdev.desc.iManufacturer = s[USB_GADGET_MANUFACTURER_IDX].id; 149788af8bbeSSebastian Andrzej Siewior gi->cdev.desc.iProduct = s[USB_GADGET_PRODUCT_IDX].id; 149888af8bbeSSebastian Andrzej Siewior gi->cdev.desc.iSerialNumber = s[USB_GADGET_SERIAL_IDX].id; 149988af8bbeSSebastian Andrzej Siewior } 150088af8bbeSSebastian Andrzej Siewior 150193c47394SJó Ágila Bitsch if (gi->use_webusb) { 150293c47394SJó Ágila Bitsch cdev->use_webusb = true; 150393c47394SJó Ágila Bitsch cdev->bcd_webusb_version = gi->bcd_webusb_version; 150493c47394SJó Ágila Bitsch cdev->b_webusb_vendor_code = gi->b_webusb_vendor_code; 150593c47394SJó Ágila Bitsch memcpy(cdev->landing_page, gi->landing_page, WEBUSB_URL_RAW_MAX_LENGTH); 150693c47394SJó Ágila Bitsch } 150793c47394SJó Ágila Bitsch 150887213d38SAndrzej Pietrasiewicz if (gi->use_os_desc) { 150987213d38SAndrzej Pietrasiewicz cdev->use_os_string = true; 151087213d38SAndrzej Pietrasiewicz cdev->b_vendor_code = gi->b_vendor_code; 151187213d38SAndrzej Pietrasiewicz memcpy(cdev->qw_sign, gi->qw_sign, OS_STRING_QW_SIGN_LEN); 151287213d38SAndrzej Pietrasiewicz } 151387213d38SAndrzej Pietrasiewicz 151441ce84c8SLi Jun if (gadget_is_otg(gadget) && !otg_desc[0]) { 151541ce84c8SLi Jun struct usb_descriptor_header *usb_desc; 151641ce84c8SLi Jun 151741ce84c8SLi Jun usb_desc = usb_otg_descriptor_alloc(gadget); 151841ce84c8SLi Jun if (!usb_desc) { 151941ce84c8SLi Jun ret = -ENOMEM; 152041ce84c8SLi Jun goto err_comp_cleanup; 152141ce84c8SLi Jun } 152241ce84c8SLi Jun usb_otg_descriptor_init(gadget, usb_desc); 152341ce84c8SLi Jun otg_desc[0] = usb_desc; 152441ce84c8SLi Jun otg_desc[1] = NULL; 152541ce84c8SLi Jun } 152641ce84c8SLi Jun 152788af8bbeSSebastian Andrzej Siewior /* Go through all configs, attach all functions */ 152888af8bbeSSebastian Andrzej Siewior list_for_each_entry(c, &gi->cdev.configs, list) { 152988af8bbeSSebastian Andrzej Siewior struct config_usb_cfg *cfg; 153088af8bbeSSebastian Andrzej Siewior struct usb_function *f; 153188af8bbeSSebastian Andrzej Siewior struct usb_function *tmp; 153288af8bbeSSebastian Andrzej Siewior struct gadget_config_name *cn; 153388af8bbeSSebastian Andrzej Siewior 153441ce84c8SLi Jun if (gadget_is_otg(gadget)) 153541ce84c8SLi Jun c->descriptors = otg_desc; 153641ce84c8SLi Jun 153788af8bbeSSebastian Andrzej Siewior cfg = container_of(c, struct config_usb_cfg, c); 153888af8bbeSSebastian Andrzej Siewior if (!list_empty(&cfg->string_list)) { 153988af8bbeSSebastian Andrzej Siewior i = 0; 154088af8bbeSSebastian Andrzej Siewior list_for_each_entry(cn, &cfg->string_list, list) { 154188af8bbeSSebastian Andrzej Siewior cfg->gstrings[i] = &cn->stringtab_dev; 154288af8bbeSSebastian Andrzej Siewior cn->stringtab_dev.strings = &cn->strings; 154388af8bbeSSebastian Andrzej Siewior cn->strings.s = cn->configuration; 154488af8bbeSSebastian Andrzej Siewior i++; 154588af8bbeSSebastian Andrzej Siewior } 154688af8bbeSSebastian Andrzej Siewior cfg->gstrings[i] = NULL; 154788af8bbeSSebastian Andrzej Siewior s = usb_gstrings_attach(&gi->cdev, cfg->gstrings, 1); 1548fea77077SWei Yongjun if (IS_ERR(s)) { 1549fea77077SWei Yongjun ret = PTR_ERR(s); 155088af8bbeSSebastian Andrzej Siewior goto err_comp_cleanup; 1551fea77077SWei Yongjun } 155288af8bbeSSebastian Andrzej Siewior c->iConfiguration = s[0].id; 155388af8bbeSSebastian Andrzej Siewior } 155488af8bbeSSebastian Andrzej Siewior 155588af8bbeSSebastian Andrzej Siewior list_for_each_entry_safe(f, tmp, &cfg->func_list, list) { 155688af8bbeSSebastian Andrzej Siewior list_del(&f->list); 155788af8bbeSSebastian Andrzej Siewior ret = usb_add_function(c, f); 15585a68e9b5SAndrzej Pietrasiewicz if (ret) { 15595a68e9b5SAndrzej Pietrasiewicz list_add(&f->list, &cfg->func_list); 156088af8bbeSSebastian Andrzej Siewior goto err_purge_funcs; 156188af8bbeSSebastian Andrzej Siewior } 15625a68e9b5SAndrzej Pietrasiewicz } 15637adf9e3aSWesley Cheng ret = usb_gadget_check_config(cdev->gadget); 15647adf9e3aSWesley Cheng if (ret) 15657adf9e3aSWesley Cheng goto err_purge_funcs; 15667adf9e3aSWesley Cheng 156788af8bbeSSebastian Andrzej Siewior usb_ep_autoconfig_reset(cdev->gadget); 156888af8bbeSSebastian Andrzej Siewior } 1569da424314SAndrzej Pietrasiewicz if (cdev->use_os_string) { 1570da424314SAndrzej Pietrasiewicz ret = composite_os_desc_req_prepare(cdev, gadget->ep0); 1571da424314SAndrzej Pietrasiewicz if (ret) 1572da424314SAndrzej Pietrasiewicz goto err_purge_funcs; 1573da424314SAndrzej Pietrasiewicz } 1574da424314SAndrzej Pietrasiewicz 157588af8bbeSSebastian Andrzej Siewior usb_ep_autoconfig_reset(cdev->gadget); 157688af8bbeSSebastian Andrzej Siewior return 0; 157788af8bbeSSebastian Andrzej Siewior 157888af8bbeSSebastian Andrzej Siewior err_purge_funcs: 157988af8bbeSSebastian Andrzej Siewior purge_configs_funcs(gi); 158088af8bbeSSebastian Andrzej Siewior err_comp_cleanup: 158188af8bbeSSebastian Andrzej Siewior composite_dev_cleanup(cdev); 158288af8bbeSSebastian Andrzej Siewior return ret; 158388af8bbeSSebastian Andrzej Siewior } 158488af8bbeSSebastian Andrzej Siewior 158588af8bbeSSebastian Andrzej Siewior static void configfs_composite_unbind(struct usb_gadget *gadget) 158688af8bbeSSebastian Andrzej Siewior { 158788af8bbeSSebastian Andrzej Siewior struct usb_composite_dev *cdev; 158888af8bbeSSebastian Andrzej Siewior struct gadget_info *gi; 15891a1c851bSPeter Chen unsigned long flags; 159088af8bbeSSebastian Andrzej Siewior 159188af8bbeSSebastian Andrzej Siewior /* the gi->lock is hold by the caller */ 159288af8bbeSSebastian Andrzej Siewior 159388af8bbeSSebastian Andrzej Siewior cdev = get_gadget_data(gadget); 159488af8bbeSSebastian Andrzej Siewior gi = container_of(cdev, struct gadget_info, cdev); 15951a1c851bSPeter Chen spin_lock_irqsave(&gi->spinlock, flags); 15961a1c851bSPeter Chen gi->unbind = 1; 15971a1c851bSPeter Chen spin_unlock_irqrestore(&gi->spinlock, flags); 159888af8bbeSSebastian Andrzej Siewior 159941ce84c8SLi Jun kfree(otg_desc[0]); 160041ce84c8SLi Jun otg_desc[0] = NULL; 160188af8bbeSSebastian Andrzej Siewior purge_configs_funcs(gi); 160288af8bbeSSebastian Andrzej Siewior composite_dev_cleanup(cdev); 160388af8bbeSSebastian Andrzej Siewior usb_ep_autoconfig_reset(cdev->gadget); 16041a1c851bSPeter Chen spin_lock_irqsave(&gi->spinlock, flags); 160588af8bbeSSebastian Andrzej Siewior cdev->gadget = NULL; 1606bf95c4d4SVijayavardhan Vennapusa cdev->deactivations = 0; 1607bf95c4d4SVijayavardhan Vennapusa gadget->deactivated = false; 160888af8bbeSSebastian Andrzej Siewior set_gadget_data(gadget, NULL); 16091a1c851bSPeter Chen spin_unlock_irqrestore(&gi->spinlock, flags); 16101a1c851bSPeter Chen } 16111a1c851bSPeter Chen 16121a1c851bSPeter Chen static int configfs_composite_setup(struct usb_gadget *gadget, 16131a1c851bSPeter Chen const struct usb_ctrlrequest *ctrl) 16141a1c851bSPeter Chen { 16151a1c851bSPeter Chen struct usb_composite_dev *cdev; 16161a1c851bSPeter Chen struct gadget_info *gi; 16171a1c851bSPeter Chen unsigned long flags; 16181a1c851bSPeter Chen int ret; 16191a1c851bSPeter Chen 16201a1c851bSPeter Chen cdev = get_gadget_data(gadget); 16211a1c851bSPeter Chen if (!cdev) 16221a1c851bSPeter Chen return 0; 16231a1c851bSPeter Chen 16241a1c851bSPeter Chen gi = container_of(cdev, struct gadget_info, cdev); 16251a1c851bSPeter Chen spin_lock_irqsave(&gi->spinlock, flags); 16261a1c851bSPeter Chen cdev = get_gadget_data(gadget); 16271a1c851bSPeter Chen if (!cdev || gi->unbind) { 16281a1c851bSPeter Chen spin_unlock_irqrestore(&gi->spinlock, flags); 16291a1c851bSPeter Chen return 0; 16301a1c851bSPeter Chen } 16311a1c851bSPeter Chen 16321a1c851bSPeter Chen ret = composite_setup(gadget, ctrl); 16331a1c851bSPeter Chen spin_unlock_irqrestore(&gi->spinlock, flags); 16341a1c851bSPeter Chen return ret; 16351a1c851bSPeter Chen } 16361a1c851bSPeter Chen 16371a1c851bSPeter Chen static void configfs_composite_disconnect(struct usb_gadget *gadget) 16381a1c851bSPeter Chen { 16391a1c851bSPeter Chen struct usb_composite_dev *cdev; 16401a1c851bSPeter Chen struct gadget_info *gi; 16411a1c851bSPeter Chen unsigned long flags; 16421a1c851bSPeter Chen 16431a1c851bSPeter Chen cdev = get_gadget_data(gadget); 16441a1c851bSPeter Chen if (!cdev) 16451a1c851bSPeter Chen return; 16461a1c851bSPeter Chen 16471a1c851bSPeter Chen gi = container_of(cdev, struct gadget_info, cdev); 16481a1c851bSPeter Chen spin_lock_irqsave(&gi->spinlock, flags); 16491a1c851bSPeter Chen cdev = get_gadget_data(gadget); 16501a1c851bSPeter Chen if (!cdev || gi->unbind) { 16511a1c851bSPeter Chen spin_unlock_irqrestore(&gi->spinlock, flags); 16521a1c851bSPeter Chen return; 16531a1c851bSPeter Chen } 16541a1c851bSPeter Chen 16551a1c851bSPeter Chen composite_disconnect(gadget); 16561a1c851bSPeter Chen spin_unlock_irqrestore(&gi->spinlock, flags); 16571a1c851bSPeter Chen } 16581a1c851bSPeter Chen 16594d7aae9fSWesley Cheng static void configfs_composite_reset(struct usb_gadget *gadget) 16604d7aae9fSWesley Cheng { 16614d7aae9fSWesley Cheng struct usb_composite_dev *cdev; 16624d7aae9fSWesley Cheng struct gadget_info *gi; 16634d7aae9fSWesley Cheng unsigned long flags; 16644d7aae9fSWesley Cheng 16654d7aae9fSWesley Cheng cdev = get_gadget_data(gadget); 16664d7aae9fSWesley Cheng if (!cdev) 16674d7aae9fSWesley Cheng return; 16684d7aae9fSWesley Cheng 16694d7aae9fSWesley Cheng gi = container_of(cdev, struct gadget_info, cdev); 16704d7aae9fSWesley Cheng spin_lock_irqsave(&gi->spinlock, flags); 16714d7aae9fSWesley Cheng cdev = get_gadget_data(gadget); 16724d7aae9fSWesley Cheng if (!cdev || gi->unbind) { 16734d7aae9fSWesley Cheng spin_unlock_irqrestore(&gi->spinlock, flags); 16744d7aae9fSWesley Cheng return; 16754d7aae9fSWesley Cheng } 16764d7aae9fSWesley Cheng 16774d7aae9fSWesley Cheng composite_reset(gadget); 16784d7aae9fSWesley Cheng spin_unlock_irqrestore(&gi->spinlock, flags); 16794d7aae9fSWesley Cheng } 16804d7aae9fSWesley Cheng 16811a1c851bSPeter Chen static void configfs_composite_suspend(struct usb_gadget *gadget) 16821a1c851bSPeter Chen { 16831a1c851bSPeter Chen struct usb_composite_dev *cdev; 16841a1c851bSPeter Chen struct gadget_info *gi; 16851a1c851bSPeter Chen unsigned long flags; 16861a1c851bSPeter Chen 16871a1c851bSPeter Chen cdev = get_gadget_data(gadget); 16881a1c851bSPeter Chen if (!cdev) 16891a1c851bSPeter Chen return; 16901a1c851bSPeter Chen 16911a1c851bSPeter Chen gi = container_of(cdev, struct gadget_info, cdev); 16921a1c851bSPeter Chen spin_lock_irqsave(&gi->spinlock, flags); 16931a1c851bSPeter Chen cdev = get_gadget_data(gadget); 16941a1c851bSPeter Chen if (!cdev || gi->unbind) { 16951a1c851bSPeter Chen spin_unlock_irqrestore(&gi->spinlock, flags); 16961a1c851bSPeter Chen return; 16971a1c851bSPeter Chen } 16981a1c851bSPeter Chen 16991a1c851bSPeter Chen composite_suspend(gadget); 17001a1c851bSPeter Chen spin_unlock_irqrestore(&gi->spinlock, flags); 17011a1c851bSPeter Chen } 17021a1c851bSPeter Chen 17031a1c851bSPeter Chen static void configfs_composite_resume(struct usb_gadget *gadget) 17041a1c851bSPeter Chen { 17051a1c851bSPeter Chen struct usb_composite_dev *cdev; 17061a1c851bSPeter Chen struct gadget_info *gi; 17071a1c851bSPeter Chen unsigned long flags; 17081a1c851bSPeter Chen 17091a1c851bSPeter Chen cdev = get_gadget_data(gadget); 17101a1c851bSPeter Chen if (!cdev) 17111a1c851bSPeter Chen return; 17121a1c851bSPeter Chen 17131a1c851bSPeter Chen gi = container_of(cdev, struct gadget_info, cdev); 17141a1c851bSPeter Chen spin_lock_irqsave(&gi->spinlock, flags); 17151a1c851bSPeter Chen cdev = get_gadget_data(gadget); 17161a1c851bSPeter Chen if (!cdev || gi->unbind) { 17171a1c851bSPeter Chen spin_unlock_irqrestore(&gi->spinlock, flags); 17181a1c851bSPeter Chen return; 17191a1c851bSPeter Chen } 17201a1c851bSPeter Chen 17211a1c851bSPeter Chen composite_resume(gadget); 17221a1c851bSPeter Chen spin_unlock_irqrestore(&gi->spinlock, flags); 172388af8bbeSSebastian Andrzej Siewior } 172488af8bbeSSebastian Andrzej Siewior 172588af8bbeSSebastian Andrzej Siewior static const struct usb_gadget_driver configfs_driver_template = { 172688af8bbeSSebastian Andrzej Siewior .bind = configfs_composite_bind, 172788af8bbeSSebastian Andrzej Siewior .unbind = configfs_composite_unbind, 172888af8bbeSSebastian Andrzej Siewior 17291a1c851bSPeter Chen .setup = configfs_composite_setup, 17304d7aae9fSWesley Cheng .reset = configfs_composite_reset, 17311a1c851bSPeter Chen .disconnect = configfs_composite_disconnect, 173288af8bbeSSebastian Andrzej Siewior 17331a1c851bSPeter Chen .suspend = configfs_composite_suspend, 17341a1c851bSPeter Chen .resume = configfs_composite_resume, 17353a571870SAndrzej Pietrasiewicz 1736e2459108Staehyun.cho .max_speed = USB_SPEED_SUPER_PLUS, 173788af8bbeSSebastian Andrzej Siewior .driver = { 173888af8bbeSSebastian Andrzej Siewior .owner = THIS_MODULE, 173988af8bbeSSebastian Andrzej Siewior }, 1740f1bddbb3SKrzysztof Opasiak .match_existing_only = 1, 174188af8bbeSSebastian Andrzej Siewior }; 174288af8bbeSSebastian Andrzej Siewior 174388af8bbeSSebastian Andrzej Siewior static struct config_group *gadgets_make( 174488af8bbeSSebastian Andrzej Siewior struct config_group *group, 174588af8bbeSSebastian Andrzej Siewior const char *name) 174688af8bbeSSebastian Andrzej Siewior { 174788af8bbeSSebastian Andrzej Siewior struct gadget_info *gi; 174888af8bbeSSebastian Andrzej Siewior 174988af8bbeSSebastian Andrzej Siewior gi = kzalloc(sizeof(*gi), GFP_KERNEL); 175088af8bbeSSebastian Andrzej Siewior if (!gi) 175188af8bbeSSebastian Andrzej Siewior return ERR_PTR(-ENOMEM); 175288af8bbeSSebastian Andrzej Siewior 17531ae1602dSChristoph Hellwig config_group_init_type_name(&gi->group, name, &gadget_root_type); 175488af8bbeSSebastian Andrzej Siewior 175588af8bbeSSebastian Andrzej Siewior config_group_init_type_name(&gi->functions_group, "functions", 175688af8bbeSSebastian Andrzej Siewior &functions_type); 17571ae1602dSChristoph Hellwig configfs_add_default_group(&gi->functions_group, &gi->group); 17581ae1602dSChristoph Hellwig 175988af8bbeSSebastian Andrzej Siewior config_group_init_type_name(&gi->configs_group, "configs", 176088af8bbeSSebastian Andrzej Siewior &config_desc_type); 17611ae1602dSChristoph Hellwig configfs_add_default_group(&gi->configs_group, &gi->group); 17621ae1602dSChristoph Hellwig 176388af8bbeSSebastian Andrzej Siewior config_group_init_type_name(&gi->strings_group, "strings", 1764*6e2a512dSDaniel Scally &gadget_language_strings_type); 17651ae1602dSChristoph Hellwig configfs_add_default_group(&gi->strings_group, &gi->group); 17661ae1602dSChristoph Hellwig 176787213d38SAndrzej Pietrasiewicz config_group_init_type_name(&gi->os_desc_group, "os_desc", 176887213d38SAndrzej Pietrasiewicz &os_desc_type); 17691ae1602dSChristoph Hellwig configfs_add_default_group(&gi->os_desc_group, &gi->group); 177088af8bbeSSebastian Andrzej Siewior 177193c47394SJó Ágila Bitsch config_group_init_type_name(&gi->webusb_group, "webusb", 177293c47394SJó Ágila Bitsch &webusb_type); 177393c47394SJó Ágila Bitsch configfs_add_default_group(&gi->webusb_group, &gi->group); 177493c47394SJó Ágila Bitsch 177588af8bbeSSebastian Andrzej Siewior gi->composite.bind = configfs_do_nothing; 177688af8bbeSSebastian Andrzej Siewior gi->composite.unbind = configfs_do_nothing; 177788af8bbeSSebastian Andrzej Siewior gi->composite.suspend = NULL; 177888af8bbeSSebastian Andrzej Siewior gi->composite.resume = NULL; 1779e2459108Staehyun.cho gi->composite.max_speed = USB_SPEED_SUPER_PLUS; 178088af8bbeSSebastian Andrzej Siewior 1781093edc2bSWei Yongjun spin_lock_init(&gi->spinlock); 178288af8bbeSSebastian Andrzej Siewior mutex_init(&gi->lock); 178388af8bbeSSebastian Andrzej Siewior INIT_LIST_HEAD(&gi->string_list); 178488af8bbeSSebastian Andrzej Siewior INIT_LIST_HEAD(&gi->available_func); 178588af8bbeSSebastian Andrzej Siewior 178688af8bbeSSebastian Andrzej Siewior composite_init_dev(&gi->cdev); 178788af8bbeSSebastian Andrzej Siewior gi->cdev.desc.bLength = USB_DT_DEVICE_SIZE; 178888af8bbeSSebastian Andrzej Siewior gi->cdev.desc.bDescriptorType = USB_DT_DEVICE; 178988af8bbeSSebastian Andrzej Siewior gi->cdev.desc.bcdDevice = cpu_to_le16(get_default_bcdDevice()); 179088af8bbeSSebastian Andrzej Siewior 179188af8bbeSSebastian Andrzej Siewior gi->composite.gadget_driver = configfs_driver_template; 179288af8bbeSSebastian Andrzej Siewior 17937c075538SChanh Nguyen gi->composite.gadget_driver.driver.name = kasprintf(GFP_KERNEL, 17947c075538SChanh Nguyen "configfs-gadget.%s", name); 17957c075538SChanh Nguyen if (!gi->composite.gadget_driver.driver.name) 17967c075538SChanh Nguyen goto err; 17977c075538SChanh Nguyen 179888af8bbeSSebastian Andrzej Siewior gi->composite.gadget_driver.function = kstrdup(name, GFP_KERNEL); 179988af8bbeSSebastian Andrzej Siewior gi->composite.name = gi->composite.gadget_driver.function; 180088af8bbeSSebastian Andrzej Siewior 180188af8bbeSSebastian Andrzej Siewior if (!gi->composite.gadget_driver.function) 18027c075538SChanh Nguyen goto out_free_driver_name; 180388af8bbeSSebastian Andrzej Siewior 180488af8bbeSSebastian Andrzej Siewior return &gi->group; 18057c075538SChanh Nguyen 18067c075538SChanh Nguyen out_free_driver_name: 18077c075538SChanh Nguyen kfree(gi->composite.gadget_driver.driver.name); 180888af8bbeSSebastian Andrzej Siewior err: 180988af8bbeSSebastian Andrzej Siewior kfree(gi); 181088af8bbeSSebastian Andrzej Siewior return ERR_PTR(-ENOMEM); 181188af8bbeSSebastian Andrzej Siewior } 181288af8bbeSSebastian Andrzej Siewior 181388af8bbeSSebastian Andrzej Siewior static void gadgets_drop(struct config_group *group, struct config_item *item) 181488af8bbeSSebastian Andrzej Siewior { 181588af8bbeSSebastian Andrzej Siewior config_item_put(item); 181688af8bbeSSebastian Andrzej Siewior } 181788af8bbeSSebastian Andrzej Siewior 181888af8bbeSSebastian Andrzej Siewior static struct configfs_group_operations gadgets_ops = { 181988af8bbeSSebastian Andrzej Siewior .make_group = &gadgets_make, 182088af8bbeSSebastian Andrzej Siewior .drop_item = &gadgets_drop, 182188af8bbeSSebastian Andrzej Siewior }; 182288af8bbeSSebastian Andrzej Siewior 18234ad01412SBhumika Goyal static const struct config_item_type gadgets_type = { 182488af8bbeSSebastian Andrzej Siewior .ct_group_ops = &gadgets_ops, 182588af8bbeSSebastian Andrzej Siewior .ct_owner = THIS_MODULE, 182688af8bbeSSebastian Andrzej Siewior }; 182788af8bbeSSebastian Andrzej Siewior 182888af8bbeSSebastian Andrzej Siewior static struct configfs_subsystem gadget_subsys = { 182988af8bbeSSebastian Andrzej Siewior .su_group = { 183088af8bbeSSebastian Andrzej Siewior .cg_item = { 183188af8bbeSSebastian Andrzej Siewior .ci_namebuf = "usb_gadget", 183288af8bbeSSebastian Andrzej Siewior .ci_type = &gadgets_type, 183388af8bbeSSebastian Andrzej Siewior }, 183488af8bbeSSebastian Andrzej Siewior }, 183588af8bbeSSebastian Andrzej Siewior .su_mutex = __MUTEX_INITIALIZER(gadget_subsys.su_mutex), 183688af8bbeSSebastian Andrzej Siewior }; 183788af8bbeSSebastian Andrzej Siewior 1838092a4bd0SAndrzej Pietrasiewicz void unregister_gadget_item(struct config_item *item) 1839092a4bd0SAndrzej Pietrasiewicz { 1840092a4bd0SAndrzej Pietrasiewicz struct gadget_info *gi = to_gadget_info(item); 1841092a4bd0SAndrzej Pietrasiewicz 1842cee51c33SWinter Wang mutex_lock(&gi->lock); 1843092a4bd0SAndrzej Pietrasiewicz unregister_gadget(gi); 1844cee51c33SWinter Wang mutex_unlock(&gi->lock); 1845092a4bd0SAndrzej Pietrasiewicz } 18460700faafSFelipe Balbi EXPORT_SYMBOL_GPL(unregister_gadget_item); 1847092a4bd0SAndrzej Pietrasiewicz 184888af8bbeSSebastian Andrzej Siewior static int __init gadget_cfs_init(void) 184988af8bbeSSebastian Andrzej Siewior { 185088af8bbeSSebastian Andrzej Siewior int ret; 185188af8bbeSSebastian Andrzej Siewior 185288af8bbeSSebastian Andrzej Siewior config_group_init(&gadget_subsys.su_group); 185388af8bbeSSebastian Andrzej Siewior 185488af8bbeSSebastian Andrzej Siewior ret = configfs_register_subsystem(&gadget_subsys); 185588af8bbeSSebastian Andrzej Siewior return ret; 185688af8bbeSSebastian Andrzej Siewior } 185788af8bbeSSebastian Andrzej Siewior module_init(gadget_cfs_init); 185888af8bbeSSebastian Andrzej Siewior 185988af8bbeSSebastian Andrzej Siewior static void __exit gadget_cfs_exit(void) 186088af8bbeSSebastian Andrzej Siewior { 186188af8bbeSSebastian Andrzej Siewior configfs_unregister_subsystem(&gadget_subsys); 186288af8bbeSSebastian Andrzej Siewior } 186388af8bbeSSebastian Andrzej Siewior module_exit(gadget_cfs_exit); 1864