^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <linux/configfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/nls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/usb/composite.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/usb/gadget_configfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include "configfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "u_f.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include "u_os_desc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #ifdef CONFIG_USB_CONFIGFS_UEVENT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/kdev_t.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/usb/ch9.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #ifdef CONFIG_USB_CONFIGFS_F_ACC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) extern int acc_ctrlrequest(struct usb_composite_dev *cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) const struct usb_ctrlrequest *ctrl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) void acc_disconnect(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) static struct class *android_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) static struct device *android_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) static int index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static int gadget_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct device *create_function_device(char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) if (android_device && !IS_ERR(android_device))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) return device_create(android_class, android_device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) MKDEV(0, index++), NULL, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) EXPORT_SYMBOL_GPL(create_function_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) int check_user_usb_string(const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct usb_gadget_strings *stringtab_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) u16 num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) ret = kstrtou16(name, 0, &num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) if (!usb_validate_langid(num))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) stringtab_dev->language = num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #define MAX_NAME_LEN 40
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) #define MAX_USB_STRING_LANGS 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) static const struct usb_descriptor_header *otg_desc[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct gadget_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) struct config_group group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) struct config_group functions_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct config_group configs_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) struct config_group strings_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) struct config_group os_desc_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) struct mutex lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) struct list_head string_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) struct list_head available_func;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) struct usb_composite_driver composite;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) struct usb_composite_dev cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) bool use_os_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) char b_vendor_code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) char qw_sign[OS_STRING_QW_SIGN_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) spinlock_t spinlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) bool unbind;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) #ifdef CONFIG_USB_CONFIGFS_UEVENT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) bool connected;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) bool sw_connected;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) struct work_struct work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) static inline struct gadget_info *to_gadget_info(struct config_item *item)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) return container_of(to_config_group(item), struct gadget_info, group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) struct config_usb_cfg {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) struct config_group group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) struct config_group strings_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) struct list_head string_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) struct usb_configuration c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) struct list_head func_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) static inline struct config_usb_cfg *to_config_usb_cfg(struct config_item *item)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) return container_of(to_config_group(item), struct config_usb_cfg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) struct gadget_strings {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) struct usb_gadget_strings stringtab_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) struct usb_string strings[USB_GADGET_FIRST_AVAIL_IDX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) char *manufacturer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) char *product;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) char *serialnumber;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) struct config_group group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) struct os_desc {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) struct config_group group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) struct gadget_config_name {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) struct usb_gadget_strings stringtab_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) struct usb_string strings;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) char *configuration;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) struct config_group group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) #define USB_MAX_STRING_WITH_NULL_LEN (USB_MAX_STRING_LEN+1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) static int usb_string_copy(const char *s, char **s_copy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) char *str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) char *copy = *s_copy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) ret = strlen(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) if (ret > USB_MAX_STRING_LEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) return -EOVERFLOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (copy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) str = copy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) str = kmalloc(USB_MAX_STRING_WITH_NULL_LEN, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) if (!str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) strcpy(str, s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) if (str[ret - 1] == '\n')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) str[ret - 1] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) *s_copy = str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) #define GI_DEVICE_DESC_SIMPLE_R_u8(__name) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) static ssize_t gadget_dev_desc_##__name##_show(struct config_item *item, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) char *page) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) return sprintf(page, "0x%02x\n", \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) to_gadget_info(item)->cdev.desc.__name); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) #define GI_DEVICE_DESC_SIMPLE_R_u16(__name) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static ssize_t gadget_dev_desc_##__name##_show(struct config_item *item, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) char *page) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) return sprintf(page, "0x%04x\n", \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) le16_to_cpup(&to_gadget_info(item)->cdev.desc.__name)); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) #define GI_DEVICE_DESC_SIMPLE_W_u8(_name) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) static ssize_t gadget_dev_desc_##_name##_store(struct config_item *item, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) const char *page, size_t len) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) u8 val; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) int ret; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) ret = kstrtou8(page, 0, &val); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) if (ret) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) return ret; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) to_gadget_info(item)->cdev.desc._name = val; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) return len; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) #define GI_DEVICE_DESC_SIMPLE_W_u16(_name) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) static ssize_t gadget_dev_desc_##_name##_store(struct config_item *item, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) const char *page, size_t len) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) u16 val; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) int ret; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) ret = kstrtou16(page, 0, &val); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) if (ret) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) return ret; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) to_gadget_info(item)->cdev.desc._name = cpu_to_le16p(&val); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) return len; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) #define GI_DEVICE_DESC_SIMPLE_RW(_name, _type) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) GI_DEVICE_DESC_SIMPLE_R_##_type(_name) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) GI_DEVICE_DESC_SIMPLE_W_##_type(_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) GI_DEVICE_DESC_SIMPLE_R_u16(bcdUSB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) GI_DEVICE_DESC_SIMPLE_RW(bDeviceClass, u8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) GI_DEVICE_DESC_SIMPLE_RW(bDeviceSubClass, u8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) GI_DEVICE_DESC_SIMPLE_RW(bDeviceProtocol, u8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) GI_DEVICE_DESC_SIMPLE_RW(bMaxPacketSize0, u8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) GI_DEVICE_DESC_SIMPLE_RW(idVendor, u16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) GI_DEVICE_DESC_SIMPLE_RW(idProduct, u16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) GI_DEVICE_DESC_SIMPLE_R_u16(bcdDevice);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) static ssize_t is_valid_bcd(u16 bcd_val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) if ((bcd_val & 0xf) > 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) if (((bcd_val >> 4) & 0xf) > 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) if (((bcd_val >> 8) & 0xf) > 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) if (((bcd_val >> 12) & 0xf) > 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) static ssize_t gadget_dev_desc_bcdDevice_store(struct config_item *item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) const char *page, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) u16 bcdDevice;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) ret = kstrtou16(page, 0, &bcdDevice);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) ret = is_valid_bcd(bcdDevice);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) to_gadget_info(item)->cdev.desc.bcdDevice = cpu_to_le16(bcdDevice);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) static ssize_t gadget_dev_desc_bcdUSB_store(struct config_item *item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) const char *page, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) u16 bcdUSB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) ret = kstrtou16(page, 0, &bcdUSB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) ret = is_valid_bcd(bcdUSB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) to_gadget_info(item)->cdev.desc.bcdUSB = cpu_to_le16(bcdUSB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) static ssize_t gadget_dev_desc_UDC_show(struct config_item *item, char *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) struct gadget_info *gi = to_gadget_info(item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) char *udc_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) mutex_lock(&gi->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) udc_name = gi->composite.gadget_driver.udc_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) ret = sprintf(page, "%s\n", udc_name ?: "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) mutex_unlock(&gi->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) static int unregister_gadget(struct gadget_info *gi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) if (!gi->composite.gadget_driver.udc_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) ret = usb_gadget_unregister_driver(&gi->composite.gadget_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) kfree(gi->composite.gadget_driver.udc_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) gi->composite.gadget_driver.udc_name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) static ssize_t gadget_dev_desc_UDC_store(struct config_item *item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) const char *page, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) struct gadget_info *gi = to_gadget_info(item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) if (strlen(page) < len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) return -EOVERFLOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) name = kstrdup(page, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) if (!name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) if (name[len - 1] == '\n')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) name[len - 1] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) mutex_lock(&gi->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) if (!strlen(name) || strcmp(name, "none") == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) ret = unregister_gadget(gi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) kfree(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) if (gi->composite.gadget_driver.udc_name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) ret = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) gi->composite.gadget_driver.udc_name = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) ret = usb_gadget_probe_driver(&gi->composite.gadget_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) gi->composite.gadget_driver.udc_name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) mutex_unlock(&gi->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) kfree(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) mutex_unlock(&gi->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) static ssize_t gadget_dev_desc_max_speed_show(struct config_item *item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) char *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) enum usb_device_speed speed = to_gadget_info(item)->composite.max_speed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) return sprintf(page, "%s\n", usb_speed_string(speed));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) static ssize_t gadget_dev_desc_max_speed_store(struct config_item *item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) const char *page, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) struct gadget_info *gi = to_gadget_info(item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) mutex_lock(&gi->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) /* Prevent changing of max_speed after the driver is binded */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) if (gi->composite.gadget_driver.udc_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) if (strncmp(page, "super-speed-plus", 16) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) gi->composite.max_speed = USB_SPEED_SUPER_PLUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) else if (strncmp(page, "super-speed", 11) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) gi->composite.max_speed = USB_SPEED_SUPER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) else if (strncmp(page, "high-speed", 10) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) gi->composite.max_speed = USB_SPEED_HIGH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) else if (strncmp(page, "full-speed", 10) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) gi->composite.max_speed = USB_SPEED_FULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) else if (strncmp(page, "low-speed", 9) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) gi->composite.max_speed = USB_SPEED_LOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) gi->composite.gadget_driver.max_speed = gi->composite.max_speed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) mutex_unlock(&gi->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) mutex_unlock(&gi->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) CONFIGFS_ATTR(gadget_dev_desc_, bDeviceClass);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) CONFIGFS_ATTR(gadget_dev_desc_, bDeviceSubClass);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) CONFIGFS_ATTR(gadget_dev_desc_, bDeviceProtocol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) CONFIGFS_ATTR(gadget_dev_desc_, bMaxPacketSize0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) CONFIGFS_ATTR(gadget_dev_desc_, idVendor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) CONFIGFS_ATTR(gadget_dev_desc_, idProduct);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) CONFIGFS_ATTR(gadget_dev_desc_, bcdDevice);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) CONFIGFS_ATTR(gadget_dev_desc_, bcdUSB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) CONFIGFS_ATTR(gadget_dev_desc_, UDC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) CONFIGFS_ATTR(gadget_dev_desc_, max_speed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) static struct configfs_attribute *gadget_root_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) &gadget_dev_desc_attr_bDeviceClass,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) &gadget_dev_desc_attr_bDeviceSubClass,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) &gadget_dev_desc_attr_bDeviceProtocol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) &gadget_dev_desc_attr_bMaxPacketSize0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) &gadget_dev_desc_attr_idVendor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) &gadget_dev_desc_attr_idProduct,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) &gadget_dev_desc_attr_bcdDevice,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) &gadget_dev_desc_attr_bcdUSB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) &gadget_dev_desc_attr_UDC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) &gadget_dev_desc_attr_max_speed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) static inline struct gadget_strings *to_gadget_strings(struct config_item *item)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) return container_of(to_config_group(item), struct gadget_strings,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) static inline struct gadget_config_name *to_gadget_config_name(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) struct config_item *item)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) return container_of(to_config_group(item), struct gadget_config_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) static inline struct usb_function_instance *to_usb_function_instance(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) struct config_item *item)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) return container_of(to_config_group(item),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) struct usb_function_instance, group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) static void gadget_info_attr_release(struct config_item *item)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) struct gadget_info *gi = to_gadget_info(item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) WARN_ON(!list_empty(&gi->cdev.configs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) WARN_ON(!list_empty(&gi->string_list));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) WARN_ON(!list_empty(&gi->available_func));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) kfree(gi->composite.gadget_driver.function);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) kfree(gi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) static struct configfs_item_operations gadget_root_item_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) .release = gadget_info_attr_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) static void gadget_config_attr_release(struct config_item *item)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) struct config_usb_cfg *cfg = to_config_usb_cfg(item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) WARN_ON(!list_empty(&cfg->c.functions));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) list_del(&cfg->c.list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) kfree(cfg->c.label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) kfree(cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) static int config_usb_cfg_link(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) struct config_item *usb_cfg_ci,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) struct config_item *usb_func_ci)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) struct usb_composite_dev *cdev = cfg->c.cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) struct config_group *group = to_config_group(usb_func_ci);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) struct usb_function_instance *fi = container_of(group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) struct usb_function_instance, group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) struct usb_function_instance *a_fi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) struct usb_function *f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) mutex_lock(&gi->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) * Make sure this function is from within our _this_ gadget and not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) * from another gadget or a random directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) * Also a function instance can only be linked once.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) list_for_each_entry(a_fi, &gi->available_func, cfs_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) if (a_fi == fi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) if (a_fi != fi) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) list_for_each_entry(f, &cfg->func_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) if (f->fi == fi) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) ret = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) f = usb_get_function(fi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) if (IS_ERR(f)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) ret = PTR_ERR(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) /* stash the function until we bind it to the gadget */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) list_add_tail(&f->list, &cfg->func_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) mutex_unlock(&gi->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) static void config_usb_cfg_unlink(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) struct config_item *usb_cfg_ci,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) struct config_item *usb_func_ci)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) struct usb_composite_dev *cdev = cfg->c.cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) struct config_group *group = to_config_group(usb_func_ci);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) struct usb_function_instance *fi = container_of(group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) struct usb_function_instance, group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) struct usb_function *f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) * ideally I would like to forbid to unlink functions while a gadget is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) * bound to an UDC. Since this isn't possible at the moment, we simply
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) * force an unbind, the function is available here and then we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) * remove the function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) mutex_lock(&gi->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) if (gi->composite.gadget_driver.udc_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) unregister_gadget(gi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) WARN_ON(gi->composite.gadget_driver.udc_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) list_for_each_entry(f, &cfg->func_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) if (f->fi == fi) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) list_del(&f->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) usb_put_function(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) mutex_unlock(&gi->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) mutex_unlock(&gi->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) WARN(1, "Unable to locate function to unbind\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) static struct configfs_item_operations gadget_config_item_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) .release = gadget_config_attr_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) .allow_link = config_usb_cfg_link,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) .drop_link = config_usb_cfg_unlink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) static ssize_t gadget_config_desc_MaxPower_show(struct config_item *item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) char *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) return sprintf(page, "%u\n", to_config_usb_cfg(item)->c.MaxPower);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) static ssize_t gadget_config_desc_MaxPower_store(struct config_item *item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) const char *page, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) u16 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) ret = kstrtou16(page, 0, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) if (DIV_ROUND_UP(val, 8) > 0xff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) to_config_usb_cfg(item)->c.MaxPower = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) static ssize_t gadget_config_desc_bmAttributes_show(struct config_item *item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) char *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) return sprintf(page, "0x%02x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) to_config_usb_cfg(item)->c.bmAttributes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) static ssize_t gadget_config_desc_bmAttributes_store(struct config_item *item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) const char *page, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) u8 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) ret = kstrtou8(page, 0, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) if (!(val & USB_CONFIG_ATT_ONE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) if (val & ~(USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) USB_CONFIG_ATT_WAKEUP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) to_config_usb_cfg(item)->c.bmAttributes = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) CONFIGFS_ATTR(gadget_config_desc_, MaxPower);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) CONFIGFS_ATTR(gadget_config_desc_, bmAttributes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) static struct configfs_attribute *gadget_config_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) &gadget_config_desc_attr_MaxPower,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) &gadget_config_desc_attr_bmAttributes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) static const struct config_item_type gadget_config_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) .ct_item_ops = &gadget_config_item_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) .ct_attrs = gadget_config_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) .ct_owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) static const struct config_item_type gadget_root_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) .ct_item_ops = &gadget_root_item_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) .ct_attrs = gadget_root_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) .ct_owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) static void composite_init_dev(struct usb_composite_dev *cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) spin_lock_init(&cdev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) INIT_LIST_HEAD(&cdev->configs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) INIT_LIST_HEAD(&cdev->gstrings);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) static struct config_group *function_make(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) struct config_group *group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) struct gadget_info *gi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) struct usb_function_instance *fi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) char buf[MAX_NAME_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) char *func_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) char *instance_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) if (ret >= MAX_NAME_LEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) return ERR_PTR(-ENAMETOOLONG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) func_name = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) instance_name = strchr(func_name, '.');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) if (!instance_name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) pr_err("Unable to locate . in FUNC.INSTANCE\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) *instance_name = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) instance_name++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) fi = usb_get_function_instance(func_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) if (IS_ERR(fi))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) return ERR_CAST(fi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) ret = config_item_set_name(&fi->group.cg_item, "%s", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) usb_put_function_instance(fi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) if (fi->set_inst_name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) ret = fi->set_inst_name(fi, instance_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) usb_put_function_instance(fi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) gi = container_of(group, struct gadget_info, functions_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) mutex_lock(&gi->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) list_add_tail(&fi->cfs_list, &gi->available_func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) mutex_unlock(&gi->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) return &fi->group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) static void function_drop(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) struct config_group *group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) struct config_item *item)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) struct usb_function_instance *fi = to_usb_function_instance(item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) struct gadget_info *gi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) gi = container_of(group, struct gadget_info, functions_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) mutex_lock(&gi->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) list_del(&fi->cfs_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) mutex_unlock(&gi->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) config_item_put(item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) static struct configfs_group_operations functions_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) .make_group = &function_make,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) .drop_item = &function_drop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) static const struct config_item_type functions_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) .ct_group_ops = &functions_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) .ct_owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) GS_STRINGS_RW(gadget_config_name, configuration);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) static struct configfs_attribute *gadget_config_name_langid_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) &gadget_config_name_attr_configuration,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) static void gadget_config_name_attr_release(struct config_item *item)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) struct gadget_config_name *cn = to_gadget_config_name(item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) kfree(cn->configuration);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) list_del(&cn->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) kfree(cn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) USB_CONFIG_STRING_RW_OPS(gadget_config_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) USB_CONFIG_STRINGS_LANG(gadget_config_name, config_usb_cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) static struct config_group *config_desc_make(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) struct config_group *group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) struct gadget_info *gi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) struct config_usb_cfg *cfg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) char buf[MAX_NAME_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) char *num_str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) u8 num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) gi = container_of(group, struct gadget_info, configs_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) if (ret >= MAX_NAME_LEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) return ERR_PTR(-ENAMETOOLONG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) num_str = strchr(buf, '.');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) if (!num_str) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) pr_err("Unable to locate . in name.bConfigurationValue\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) *num_str = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) num_str++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) if (!strlen(buf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) ret = kstrtou8(num_str, 0, &num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) if (!cfg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) cfg->c.label = kstrdup(buf, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) if (!cfg->c.label) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) cfg->c.bConfigurationValue = num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) cfg->c.MaxPower = CONFIG_USB_GADGET_VBUS_DRAW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) cfg->c.bmAttributes = USB_CONFIG_ATT_ONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) INIT_LIST_HEAD(&cfg->string_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) INIT_LIST_HEAD(&cfg->func_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) config_group_init_type_name(&cfg->group, name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) &gadget_config_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) config_group_init_type_name(&cfg->strings_group, "strings",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) &gadget_config_name_strings_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) configfs_add_default_group(&cfg->strings_group, &cfg->group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) ret = usb_add_config_only(&gi->cdev, &cfg->c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) return &cfg->group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) kfree(cfg->c.label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) kfree(cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) static void config_desc_drop(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) struct config_group *group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) struct config_item *item)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) config_item_put(item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) static struct configfs_group_operations config_desc_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) .make_group = &config_desc_make,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) .drop_item = &config_desc_drop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) static const struct config_item_type config_desc_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) .ct_group_ops = &config_desc_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) .ct_owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) GS_STRINGS_RW(gadget_strings, manufacturer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) GS_STRINGS_RW(gadget_strings, product);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) GS_STRINGS_RW(gadget_strings, serialnumber);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) static struct configfs_attribute *gadget_strings_langid_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) &gadget_strings_attr_manufacturer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) &gadget_strings_attr_product,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) &gadget_strings_attr_serialnumber,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) static void gadget_strings_attr_release(struct config_item *item)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) struct gadget_strings *gs = to_gadget_strings(item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) kfree(gs->manufacturer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) kfree(gs->product);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) kfree(gs->serialnumber);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) list_del(&gs->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) kfree(gs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) USB_CONFIG_STRING_RW_OPS(gadget_strings);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) USB_CONFIG_STRINGS_LANG(gadget_strings, gadget_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) static inline struct os_desc *to_os_desc(struct config_item *item)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) return container_of(to_config_group(item), struct os_desc, group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) static inline struct gadget_info *os_desc_item_to_gadget_info(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) struct config_item *item)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) return to_gadget_info(to_os_desc(item)->group.cg_item.ci_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) static ssize_t os_desc_use_show(struct config_item *item, char *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) return sprintf(page, "%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) os_desc_item_to_gadget_info(item)->use_os_desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) static ssize_t os_desc_use_store(struct config_item *item, const char *page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) struct gadget_info *gi = os_desc_item_to_gadget_info(item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) bool use;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) mutex_lock(&gi->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) ret = strtobool(page, &use);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) gi->use_os_desc = use;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) ret = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) mutex_unlock(&gi->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) static ssize_t os_desc_b_vendor_code_show(struct config_item *item, char *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) return sprintf(page, "0x%02x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) os_desc_item_to_gadget_info(item)->b_vendor_code);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) static ssize_t os_desc_b_vendor_code_store(struct config_item *item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) const char *page, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) struct gadget_info *gi = os_desc_item_to_gadget_info(item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) u8 b_vendor_code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) mutex_lock(&gi->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) ret = kstrtou8(page, 0, &b_vendor_code);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) gi->b_vendor_code = b_vendor_code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) ret = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) mutex_unlock(&gi->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) static ssize_t os_desc_qw_sign_show(struct config_item *item, char *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) struct gadget_info *gi = os_desc_item_to_gadget_info(item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) res = utf16s_to_utf8s((wchar_t *) gi->qw_sign, OS_STRING_QW_SIGN_LEN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) UTF16_LITTLE_ENDIAN, page, PAGE_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) page[res++] = '\n';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) static ssize_t os_desc_qw_sign_store(struct config_item *item, const char *page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) struct gadget_info *gi = os_desc_item_to_gadget_info(item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) int res, l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) l = min((int)len, OS_STRING_QW_SIGN_LEN >> 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) if (page[l - 1] == '\n')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) --l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) mutex_lock(&gi->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) res = utf8s_to_utf16s(page, l,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) UTF16_LITTLE_ENDIAN, (wchar_t *) gi->qw_sign,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) OS_STRING_QW_SIGN_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) if (res > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) res = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) mutex_unlock(&gi->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) CONFIGFS_ATTR(os_desc_, use);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) CONFIGFS_ATTR(os_desc_, b_vendor_code);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) CONFIGFS_ATTR(os_desc_, qw_sign);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) static struct configfs_attribute *os_desc_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) &os_desc_attr_use,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) &os_desc_attr_b_vendor_code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) &os_desc_attr_qw_sign,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) static void os_desc_attr_release(struct config_item *item)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) struct os_desc *os_desc = to_os_desc(item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) kfree(os_desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) static int os_desc_link(struct config_item *os_desc_ci,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) struct config_item *usb_cfg_ci)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) struct gadget_info *gi = container_of(to_config_group(os_desc_ci),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) struct gadget_info, os_desc_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) struct usb_composite_dev *cdev = &gi->cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) struct config_usb_cfg *c_target =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) container_of(to_config_group(usb_cfg_ci),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) struct config_usb_cfg, group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) struct usb_configuration *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) mutex_lock(&gi->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) list_for_each_entry(c, &cdev->configs, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) if (c == &c_target->c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) if (c != &c_target->c) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) if (cdev->os_desc_config) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) ret = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) cdev->os_desc_config = &c_target->c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) mutex_unlock(&gi->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) static void os_desc_unlink(struct config_item *os_desc_ci,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) struct config_item *usb_cfg_ci)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) struct gadget_info *gi = container_of(to_config_group(os_desc_ci),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) struct gadget_info, os_desc_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) struct usb_composite_dev *cdev = &gi->cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) mutex_lock(&gi->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) if (gi->composite.gadget_driver.udc_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) unregister_gadget(gi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) cdev->os_desc_config = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) WARN_ON(gi->composite.gadget_driver.udc_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) mutex_unlock(&gi->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) static struct configfs_item_operations os_desc_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) .release = os_desc_attr_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) .allow_link = os_desc_link,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) .drop_link = os_desc_unlink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) static struct config_item_type os_desc_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) .ct_item_ops = &os_desc_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) .ct_attrs = os_desc_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) .ct_owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) static inline struct usb_os_desc_ext_prop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) *to_usb_os_desc_ext_prop(struct config_item *item)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) return container_of(item, struct usb_os_desc_ext_prop, item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) static ssize_t ext_prop_type_show(struct config_item *item, char *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) return sprintf(page, "%d\n", to_usb_os_desc_ext_prop(item)->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) static ssize_t ext_prop_type_store(struct config_item *item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) const char *page, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) u8 type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) if (desc->opts_mutex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) mutex_lock(desc->opts_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) ret = kstrtou8(page, 0, &type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) if (type < USB_EXT_PROP_UNICODE || type > USB_EXT_PROP_UNICODE_MULTI) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) if ((ext_prop->type == USB_EXT_PROP_BINARY ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) ext_prop->type == USB_EXT_PROP_LE32 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) ext_prop->type == USB_EXT_PROP_BE32) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) (type == USB_EXT_PROP_UNICODE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) type == USB_EXT_PROP_UNICODE_ENV ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) type == USB_EXT_PROP_UNICODE_LINK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) ext_prop->data_len <<= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) else if ((ext_prop->type == USB_EXT_PROP_UNICODE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) ext_prop->type == USB_EXT_PROP_UNICODE_LINK) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) (type == USB_EXT_PROP_BINARY ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) type == USB_EXT_PROP_LE32 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) type == USB_EXT_PROP_BE32))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) ext_prop->data_len >>= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) ext_prop->type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) ret = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) end:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) if (desc->opts_mutex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) mutex_unlock(desc->opts_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) static ssize_t ext_prop_data_show(struct config_item *item, char *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) int len = ext_prop->data_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) if (ext_prop->type == USB_EXT_PROP_UNICODE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) ext_prop->type == USB_EXT_PROP_UNICODE_LINK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) len >>= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) memcpy(page, ext_prop->data, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) static ssize_t ext_prop_data_store(struct config_item *item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) const char *page, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) char *new_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) size_t ret_len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) if (page[len - 1] == '\n' || page[len - 1] == '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) --len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) new_data = kmemdup(page, len, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) if (!new_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) if (desc->opts_mutex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) mutex_lock(desc->opts_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) kfree(ext_prop->data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) ext_prop->data = new_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) desc->ext_prop_len -= ext_prop->data_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) ext_prop->data_len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) desc->ext_prop_len += ext_prop->data_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) if (ext_prop->type == USB_EXT_PROP_UNICODE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) ext_prop->type == USB_EXT_PROP_UNICODE_LINK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) desc->ext_prop_len -= ext_prop->data_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) ext_prop->data_len <<= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) ext_prop->data_len += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) desc->ext_prop_len += ext_prop->data_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) if (desc->opts_mutex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) mutex_unlock(desc->opts_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) return ret_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) CONFIGFS_ATTR(ext_prop_, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) CONFIGFS_ATTR(ext_prop_, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) static struct configfs_attribute *ext_prop_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) &ext_prop_attr_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) &ext_prop_attr_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) static void usb_os_desc_ext_prop_release(struct config_item *item)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) kfree(ext_prop); /* frees a whole chunk */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) static struct configfs_item_operations ext_prop_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) .release = usb_os_desc_ext_prop_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) static struct config_item *ext_prop_make(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) struct config_group *group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) struct usb_os_desc_ext_prop *ext_prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) struct config_item_type *ext_prop_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) struct usb_os_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) char *vlabuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) vla_group(data_chunk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) vla_item(data_chunk, struct usb_os_desc_ext_prop, ext_prop, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) vla_item(data_chunk, struct config_item_type, ext_prop_type, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) if (!vlabuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) ext_prop = vla_ptr(vlabuf, data_chunk, ext_prop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) ext_prop_type = vla_ptr(vlabuf, data_chunk, ext_prop_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) desc = container_of(group, struct usb_os_desc, group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) ext_prop_type->ct_item_ops = &ext_prop_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) ext_prop_type->ct_attrs = ext_prop_attrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) ext_prop_type->ct_owner = desc->owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) config_item_init_type_name(&ext_prop->item, name, ext_prop_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) ext_prop->name = kstrdup(name, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) if (!ext_prop->name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) kfree(vlabuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) desc->ext_prop_len += 14;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) ext_prop->name_len = 2 * strlen(ext_prop->name) + 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) if (desc->opts_mutex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) mutex_lock(desc->opts_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) desc->ext_prop_len += ext_prop->name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) list_add_tail(&ext_prop->entry, &desc->ext_prop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) ++desc->ext_prop_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) if (desc->opts_mutex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) mutex_unlock(desc->opts_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) return &ext_prop->item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) static void ext_prop_drop(struct config_group *group, struct config_item *item)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) struct usb_os_desc *desc = to_usb_os_desc(&group->cg_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) if (desc->opts_mutex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) mutex_lock(desc->opts_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) list_del(&ext_prop->entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) --desc->ext_prop_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) kfree(ext_prop->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) desc->ext_prop_len -= (ext_prop->name_len + ext_prop->data_len + 14);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) if (desc->opts_mutex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) mutex_unlock(desc->opts_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) config_item_put(item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) static struct configfs_group_operations interf_grp_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) .make_item = &ext_prop_make,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) .drop_item = &ext_prop_drop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) static ssize_t interf_grp_compatible_id_show(struct config_item *item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) char *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) memcpy(page, to_usb_os_desc(item)->ext_compat_id, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) return 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) static ssize_t interf_grp_compatible_id_store(struct config_item *item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) const char *page, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) struct usb_os_desc *desc = to_usb_os_desc(item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) int l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) l = min_t(int, 8, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) if (page[l - 1] == '\n')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) --l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) if (desc->opts_mutex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) mutex_lock(desc->opts_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) memcpy(desc->ext_compat_id, page, l);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) if (desc->opts_mutex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) mutex_unlock(desc->opts_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) static ssize_t interf_grp_sub_compatible_id_show(struct config_item *item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) char *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) memcpy(page, to_usb_os_desc(item)->ext_compat_id + 8, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) return 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) static ssize_t interf_grp_sub_compatible_id_store(struct config_item *item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) const char *page, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) struct usb_os_desc *desc = to_usb_os_desc(item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) int l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) l = min_t(int, 8, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) if (page[l - 1] == '\n')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) --l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) if (desc->opts_mutex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) mutex_lock(desc->opts_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) memcpy(desc->ext_compat_id + 8, page, l);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) if (desc->opts_mutex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) mutex_unlock(desc->opts_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) CONFIGFS_ATTR(interf_grp_, compatible_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) CONFIGFS_ATTR(interf_grp_, sub_compatible_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) static struct configfs_attribute *interf_grp_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) &interf_grp_attr_compatible_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) &interf_grp_attr_sub_compatible_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) struct config_group *usb_os_desc_prepare_interf_dir(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) struct config_group *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) int n_interf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) struct usb_os_desc **desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) char **names,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) struct module *owner)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) struct config_group *os_desc_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) struct config_item_type *os_desc_type, *interface_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) vla_group(data_chunk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) vla_item(data_chunk, struct config_group, os_desc_group, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) vla_item(data_chunk, struct config_item_type, os_desc_type, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) vla_item(data_chunk, struct config_item_type, interface_type, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) char *vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) if (!vlabuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) os_desc_group = vla_ptr(vlabuf, data_chunk, os_desc_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) os_desc_type = vla_ptr(vlabuf, data_chunk, os_desc_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) interface_type = vla_ptr(vlabuf, data_chunk, interface_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) os_desc_type->ct_owner = owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) config_group_init_type_name(os_desc_group, "os_desc", os_desc_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) configfs_add_default_group(os_desc_group, parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) interface_type->ct_group_ops = &interf_grp_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) interface_type->ct_attrs = interf_grp_attrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) interface_type->ct_owner = owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) while (n_interf--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) struct usb_os_desc *d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) d = desc[n_interf];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) d->owner = owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) config_group_init_type_name(&d->group, "", interface_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) config_item_set_name(&d->group.cg_item, "interface.%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) names[n_interf]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) configfs_add_default_group(&d->group, os_desc_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) return os_desc_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) EXPORT_SYMBOL(usb_os_desc_prepare_interf_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) static int configfs_do_nothing(struct usb_composite_dev *cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) int composite_dev_prepare(struct usb_composite_driver *composite,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) struct usb_composite_dev *dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) int composite_os_desc_req_prepare(struct usb_composite_dev *cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) struct usb_ep *ep0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) static void purge_configs_funcs(struct gadget_info *gi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) struct usb_configuration *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) list_for_each_entry(c, &gi->cdev.configs, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) struct usb_function *f, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) struct config_usb_cfg *cfg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) cfg = container_of(c, struct config_usb_cfg, c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) list_for_each_entry_safe_reverse(f, tmp, &c->functions, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) list_move(&f->list, &cfg->func_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) if (f->unbind) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) dev_dbg(&gi->cdev.gadget->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) "unbind function '%s'/%p\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) f->name, f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) f->unbind(c, f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) if (f->bind_deactivated)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) usb_function_activate(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) c->next_interface_id = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) memset(c->interface, 0, sizeof(c->interface));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) c->superspeed_plus = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) c->superspeed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) c->highspeed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) c->fullspeed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) static int configfs_composite_bind(struct usb_gadget *gadget,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) struct usb_gadget_driver *gdriver)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) struct usb_composite_driver *composite = to_cdriver(gdriver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) struct gadget_info *gi = container_of(composite,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) struct gadget_info, composite);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) struct usb_composite_dev *cdev = &gi->cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) struct usb_configuration *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) struct usb_string *s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) unsigned i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) /* the gi->lock is hold by the caller */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) gi->unbind = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) cdev->gadget = gadget;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) set_gadget_data(gadget, cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) ret = composite_dev_prepare(composite, cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) /* and now the gadget bind */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) if (list_empty(&gi->cdev.configs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) pr_err("Need at least one configuration in %s.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) gi->composite.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) goto err_comp_cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) list_for_each_entry(c, &gi->cdev.configs, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) struct config_usb_cfg *cfg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) cfg = container_of(c, struct config_usb_cfg, c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) if (list_empty(&cfg->func_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) pr_err("Config %s/%d of %s needs at least one function.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) c->label, c->bConfigurationValue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) gi->composite.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) goto err_comp_cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) /* init all strings */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) if (!list_empty(&gi->string_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) struct gadget_strings *gs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) list_for_each_entry(gs, &gi->string_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) gi->gstrings[i] = &gs->stringtab_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) gs->stringtab_dev.strings = gs->strings;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) gs->strings[USB_GADGET_MANUFACTURER_IDX].s =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) gs->manufacturer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) gs->strings[USB_GADGET_PRODUCT_IDX].s = gs->product;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) gs->strings[USB_GADGET_SERIAL_IDX].s = gs->serialnumber;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) gi->gstrings[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) s = usb_gstrings_attach(&gi->cdev, gi->gstrings,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) USB_GADGET_FIRST_AVAIL_IDX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) if (IS_ERR(s)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) ret = PTR_ERR(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) goto err_comp_cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) gi->cdev.desc.iManufacturer = s[USB_GADGET_MANUFACTURER_IDX].id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) gi->cdev.desc.iProduct = s[USB_GADGET_PRODUCT_IDX].id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) gi->cdev.desc.iSerialNumber = s[USB_GADGET_SERIAL_IDX].id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) if (gi->use_os_desc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) cdev->use_os_string = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) cdev->b_vendor_code = gi->b_vendor_code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) memcpy(cdev->qw_sign, gi->qw_sign, OS_STRING_QW_SIGN_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) if (gadget_is_otg(gadget) && !otg_desc[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) struct usb_descriptor_header *usb_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) usb_desc = usb_otg_descriptor_alloc(gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) if (!usb_desc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) goto err_comp_cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) usb_otg_descriptor_init(gadget, usb_desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) otg_desc[0] = usb_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) otg_desc[1] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) /* Go through all configs, attach all functions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) list_for_each_entry(c, &gi->cdev.configs, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) struct config_usb_cfg *cfg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) struct usb_function *f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) struct usb_function *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) struct gadget_config_name *cn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) if (gadget_is_otg(gadget))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) c->descriptors = otg_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) cfg = container_of(c, struct config_usb_cfg, c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) if (!list_empty(&cfg->string_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) list_for_each_entry(cn, &cfg->string_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) cfg->gstrings[i] = &cn->stringtab_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) cn->stringtab_dev.strings = &cn->strings;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) cn->strings.s = cn->configuration;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) cfg->gstrings[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) s = usb_gstrings_attach(&gi->cdev, cfg->gstrings, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) if (IS_ERR(s)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) ret = PTR_ERR(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) goto err_comp_cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) c->iConfiguration = s[0].id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) list_for_each_entry_safe(f, tmp, &cfg->func_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) list_del(&f->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) ret = usb_add_function(c, f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) list_add(&f->list, &cfg->func_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) goto err_purge_funcs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) ret = usb_gadget_check_config(cdev->gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) goto err_purge_funcs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) usb_ep_autoconfig_reset(cdev->gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) if (cdev->use_os_string) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) ret = composite_os_desc_req_prepare(cdev, gadget->ep0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) goto err_purge_funcs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) usb_ep_autoconfig_reset(cdev->gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) err_purge_funcs:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) purge_configs_funcs(gi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) err_comp_cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) composite_dev_cleanup(cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) #ifdef CONFIG_USB_CONFIGFS_UEVENT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) static void android_work(struct work_struct *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) struct gadget_info *gi = container_of(data, struct gadget_info, work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) struct usb_composite_dev *cdev = &gi->cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) char *disconnected[2] = { "USB_STATE=DISCONNECTED", NULL };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) char *connected[2] = { "USB_STATE=CONNECTED", NULL };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) char *configured[2] = { "USB_STATE=CONFIGURED", NULL };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) /* 0-connected 1-configured 2-disconnected*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) bool status[3] = { false, false, false };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) bool uevent_sent = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) spin_lock_irqsave(&cdev->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) if (cdev->config)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) status[1] = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) if (gi->connected != gi->sw_connected) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) if (gi->connected)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) status[0] = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) status[2] = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) gi->sw_connected = gi->connected;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) spin_unlock_irqrestore(&cdev->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) if (status[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) kobject_uevent_env(&gi->dev->kobj, KOBJ_CHANGE, connected);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) pr_info("%s: sent uevent %s\n", __func__, connected[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) uevent_sent = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) if (status[1]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) kobject_uevent_env(&gi->dev->kobj, KOBJ_CHANGE, configured);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) pr_info("%s: sent uevent %s\n", __func__, configured[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) uevent_sent = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) if (status[2]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) kobject_uevent_env(&gi->dev->kobj, KOBJ_CHANGE, disconnected);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) pr_info("%s: sent uevent %s\n", __func__, disconnected[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) uevent_sent = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) if (!uevent_sent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) pr_info("%s: did not send uevent (%d %d %p)\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) gi->connected, gi->sw_connected, cdev->config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) static void configfs_composite_unbind(struct usb_gadget *gadget)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) struct usb_composite_dev *cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) struct gadget_info *gi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) /* the gi->lock is hold by the caller */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) cdev = get_gadget_data(gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) gi = container_of(cdev, struct gadget_info, cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) spin_lock_irqsave(&gi->spinlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) gi->unbind = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) spin_unlock_irqrestore(&gi->spinlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) kfree(otg_desc[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) otg_desc[0] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) purge_configs_funcs(gi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) composite_dev_cleanup(cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) usb_ep_autoconfig_reset(cdev->gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) spin_lock_irqsave(&gi->spinlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) cdev->gadget = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) cdev->deactivations = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) gadget->deactivated = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) set_gadget_data(gadget, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) spin_unlock_irqrestore(&gi->spinlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) #ifdef CONFIG_USB_CONFIGFS_UEVENT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) static int android_setup(struct usb_gadget *gadget,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) const struct usb_ctrlrequest *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) struct usb_composite_dev *cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) struct gadget_info *gi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) int value = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) struct usb_function_instance *fi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) if (!android_device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) gi = dev_get_drvdata(android_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) spin_lock_irqsave(&gi->spinlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) cdev = get_gadget_data(gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) if (!cdev || gi->unbind) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) spin_unlock_irqrestore(&gi->spinlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) if (c->bRequest == USB_REQ_GET_DESCRIPTOR &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) (c->wValue >> 8) == USB_DT_CONFIG && !gi->connected) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) gi->connected = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) schedule_work(&gi->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) list_for_each_entry(fi, &gi->available_func, cfs_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) if (fi != NULL && fi->f != NULL && fi->f->setup != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) value = fi->f->setup(fi->f, c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) if (value >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) #ifdef CONFIG_USB_CONFIGFS_F_ACC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) if (value < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) value = acc_ctrlrequest(cdev, c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) if (value < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) value = composite_setup(gadget, c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) if (c->bRequest == USB_REQ_SET_CONFIGURATION &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) cdev->config) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) schedule_work(&gi->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) spin_unlock_irqrestore(&gi->spinlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) return value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) #else // CONFIG_USB_CONFIGFS_UEVENT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) static int configfs_composite_setup(struct usb_gadget *gadget,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) const struct usb_ctrlrequest *ctrl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) struct usb_composite_dev *cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) struct gadget_info *gi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) cdev = get_gadget_data(gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) if (!cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) gi = container_of(cdev, struct gadget_info, cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) spin_lock_irqsave(&gi->spinlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) cdev = get_gadget_data(gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) if (!cdev || gi->unbind) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) spin_unlock_irqrestore(&gi->spinlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) ret = composite_setup(gadget, ctrl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) spin_unlock_irqrestore(&gi->spinlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) #endif // CONFIG_USB_CONFIGFS_UEVENT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) static void configfs_composite_disconnect(struct usb_gadget *gadget)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) struct usb_composite_dev *cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) struct gadget_info *gi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) cdev = get_gadget_data(gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) if (!cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) #ifdef CONFIG_USB_CONFIGFS_F_ACC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) * accessory HID support can be active while the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) * accessory function is not actually enabled,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) * so we need to inform it when we are disconnected.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) acc_disconnect();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) gi = container_of(cdev, struct gadget_info, cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) spin_lock_irqsave(&gi->spinlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) cdev = get_gadget_data(gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) if (!cdev || gi->unbind) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) spin_unlock_irqrestore(&gi->spinlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) #ifdef CONFIG_USB_CONFIGFS_UEVENT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) gi->connected = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) schedule_work(&gi->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) composite_disconnect(gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) spin_unlock_irqrestore(&gi->spinlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) static void configfs_composite_reset(struct usb_gadget *gadget)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) struct usb_composite_dev *cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) struct gadget_info *gi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) cdev = get_gadget_data(gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) if (!cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) gi = container_of(cdev, struct gadget_info, cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) spin_lock_irqsave(&gi->spinlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) cdev = get_gadget_data(gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) if (!cdev || gi->unbind) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) spin_unlock_irqrestore(&gi->spinlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) composite_reset(gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) spin_unlock_irqrestore(&gi->spinlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) static void configfs_composite_suspend(struct usb_gadget *gadget)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) struct usb_composite_dev *cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) struct gadget_info *gi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) cdev = get_gadget_data(gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) if (!cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) gi = container_of(cdev, struct gadget_info, cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) spin_lock_irqsave(&gi->spinlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) cdev = get_gadget_data(gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) if (!cdev || gi->unbind) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) spin_unlock_irqrestore(&gi->spinlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) composite_suspend(gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) spin_unlock_irqrestore(&gi->spinlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) static void configfs_composite_resume(struct usb_gadget *gadget)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) struct usb_composite_dev *cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) struct gadget_info *gi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) cdev = get_gadget_data(gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) if (!cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) gi = container_of(cdev, struct gadget_info, cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) spin_lock_irqsave(&gi->spinlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) cdev = get_gadget_data(gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) if (!cdev || gi->unbind) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) spin_unlock_irqrestore(&gi->spinlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) composite_resume(gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) spin_unlock_irqrestore(&gi->spinlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) static const struct usb_gadget_driver configfs_driver_template = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) .bind = configfs_composite_bind,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) .unbind = configfs_composite_unbind,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) #ifdef CONFIG_USB_CONFIGFS_UEVENT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) .setup = android_setup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) .setup = configfs_composite_setup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) .reset = configfs_composite_reset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) .disconnect = configfs_composite_disconnect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) .suspend = configfs_composite_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) .resume = configfs_composite_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) .max_speed = USB_SPEED_SUPER_PLUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) .name = "configfs-gadget",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) .match_existing_only = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) #ifdef CONFIG_USB_CONFIGFS_UEVENT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) static ssize_t state_show(struct device *pdev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) struct gadget_info *dev = dev_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) struct usb_composite_dev *cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) char *state = "DISCONNECTED";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) cdev = &dev->cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) if (!cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) spin_lock_irqsave(&cdev->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) if (cdev->config)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) state = "CONFIGURED";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) else if (dev->connected)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) state = "CONNECTED";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) spin_unlock_irqrestore(&cdev->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) return sprintf(buf, "%s\n", state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) static DEVICE_ATTR(state, S_IRUGO, state_show, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) static struct device_attribute *android_usb_attributes[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) &dev_attr_state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) static int android_device_create(struct gadget_info *gi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) struct device_attribute **attrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) struct device_attribute *attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) INIT_WORK(&gi->work, android_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) gi->dev = device_create(android_class, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) MKDEV(0, 0), NULL, "android%d", gadget_index++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) if (IS_ERR(gi->dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) return PTR_ERR(gi->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) dev_set_drvdata(gi->dev, gi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) if (!android_device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) android_device = gi->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) attrs = android_usb_attributes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) while ((attr = *attrs++)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) err = device_create_file(gi->dev, attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) device_destroy(gi->dev->class,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) gi->dev->devt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) static void android_device_destroy(struct gadget_info *gi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) struct device_attribute **attrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) struct device_attribute *attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) attrs = android_usb_attributes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) while ((attr = *attrs++))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) device_remove_file(gi->dev, attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) device_destroy(gi->dev->class, gi->dev->devt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) static inline int android_device_create(struct gadget_info *gi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) static inline void android_device_destroy(struct gadget_info *gi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) static struct config_group *gadgets_make(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) struct config_group *group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) struct gadget_info *gi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) gi = kzalloc(sizeof(*gi), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) if (!gi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) config_group_init_type_name(&gi->group, name, &gadget_root_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) config_group_init_type_name(&gi->functions_group, "functions",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) &functions_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) configfs_add_default_group(&gi->functions_group, &gi->group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) config_group_init_type_name(&gi->configs_group, "configs",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) &config_desc_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) configfs_add_default_group(&gi->configs_group, &gi->group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) config_group_init_type_name(&gi->strings_group, "strings",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) &gadget_strings_strings_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) configfs_add_default_group(&gi->strings_group, &gi->group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) config_group_init_type_name(&gi->os_desc_group, "os_desc",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) &os_desc_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) configfs_add_default_group(&gi->os_desc_group, &gi->group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) gi->composite.bind = configfs_do_nothing;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) gi->composite.unbind = configfs_do_nothing;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) gi->composite.suspend = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) gi->composite.resume = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) gi->composite.max_speed = USB_SPEED_SUPER_PLUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) spin_lock_init(&gi->spinlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) mutex_init(&gi->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) INIT_LIST_HEAD(&gi->string_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) INIT_LIST_HEAD(&gi->available_func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) composite_init_dev(&gi->cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) gi->cdev.desc.bLength = USB_DT_DEVICE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) gi->cdev.desc.bDescriptorType = USB_DT_DEVICE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) gi->cdev.desc.bcdDevice = cpu_to_le16(get_default_bcdDevice());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) gi->composite.gadget_driver = configfs_driver_template;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) gi->composite.gadget_driver.function = kstrdup(name, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) gi->composite.name = gi->composite.gadget_driver.function;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) if (!gi->composite.gadget_driver.function)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) if (android_device_create(gi) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) return &gi->group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) kfree(gi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) static void gadgets_drop(struct config_group *group, struct config_item *item)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) struct gadget_info *gi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) gi = container_of(to_config_group(item), struct gadget_info, group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) config_item_put(item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) android_device_destroy(gi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) static struct configfs_group_operations gadgets_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) .make_group = &gadgets_make,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) .drop_item = &gadgets_drop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) static const struct config_item_type gadgets_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) .ct_group_ops = &gadgets_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) .ct_owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) static struct configfs_subsystem gadget_subsys = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) .su_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) .cg_item = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) .ci_namebuf = "usb_gadget",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) .ci_type = &gadgets_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) .su_mutex = __MUTEX_INITIALIZER(gadget_subsys.su_mutex),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) void unregister_gadget_item(struct config_item *item)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) struct gadget_info *gi = to_gadget_info(item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) mutex_lock(&gi->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) unregister_gadget(gi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) mutex_unlock(&gi->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) EXPORT_SYMBOL_GPL(unregister_gadget_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) static int __init gadget_cfs_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) config_group_init(&gadget_subsys.su_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) ret = configfs_register_subsystem(&gadget_subsys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) #ifdef CONFIG_USB_CONFIGFS_UEVENT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) android_class = class_create(THIS_MODULE, "android_usb");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) if (IS_ERR(android_class))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) return PTR_ERR(android_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) module_init(gadget_cfs_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) static void __exit gadget_cfs_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) configfs_unregister_subsystem(&gadget_subsys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) #ifdef CONFIG_USB_CONFIGFS_UEVENT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) if (!IS_ERR(android_class))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) class_destroy(android_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) module_exit(gadget_cfs_exit);