0001
0002 #ifndef __GADGET_CONFIGFS__
0003 #define __GADGET_CONFIGFS__
0004
0005 #include <linux/configfs.h>
0006
0007 int check_user_usb_string(const char *name,
0008 struct usb_gadget_strings *stringtab_dev);
0009
0010 #define GS_STRINGS_W(__struct, __name) \
0011 static ssize_t __struct##_##__name##_store(struct config_item *item, \
0012 const char *page, size_t len) \
0013 { \
0014 struct __struct *gs = to_##__struct(item); \
0015 int ret; \
0016 \
0017 ret = usb_string_copy(page, &gs->__name); \
0018 if (ret) \
0019 return ret; \
0020 return len; \
0021 }
0022
0023 #define GS_STRINGS_R(__struct, __name) \
0024 static ssize_t __struct##_##__name##_show(struct config_item *item, char *page) \
0025 { \
0026 struct __struct *gs = to_##__struct(item); \
0027 return sprintf(page, "%s\n", gs->__name ?: ""); \
0028 }
0029
0030 #define GS_STRINGS_RW(struct_name, _name) \
0031 GS_STRINGS_R(struct_name, _name) \
0032 GS_STRINGS_W(struct_name, _name) \
0033 CONFIGFS_ATTR(struct_name##_, _name)
0034
0035 #define USB_CONFIG_STRING_RW_OPS(struct_in) \
0036 static struct configfs_item_operations struct_in##_langid_item_ops = { \
0037 .release = struct_in##_attr_release, \
0038 }; \
0039 \
0040 static struct config_item_type struct_in##_langid_type = { \
0041 .ct_item_ops = &struct_in##_langid_item_ops, \
0042 .ct_attrs = struct_in##_langid_attrs, \
0043 .ct_owner = THIS_MODULE, \
0044 }
0045
0046 #define USB_CONFIG_STRINGS_LANG(struct_in, struct_member) \
0047 static struct config_group *struct_in##_strings_make( \
0048 struct config_group *group, \
0049 const char *name) \
0050 { \
0051 struct struct_member *gi; \
0052 struct struct_in *gs; \
0053 struct struct_in *new; \
0054 int langs = 0; \
0055 int ret; \
0056 \
0057 new = kzalloc(sizeof(*new), GFP_KERNEL); \
0058 if (!new) \
0059 return ERR_PTR(-ENOMEM); \
0060 \
0061 ret = check_user_usb_string(name, &new->stringtab_dev); \
0062 if (ret) \
0063 goto err; \
0064 config_group_init_type_name(&new->group, name, \
0065 &struct_in##_langid_type); \
0066 \
0067 gi = container_of(group, struct struct_member, strings_group); \
0068 ret = -EEXIST; \
0069 list_for_each_entry(gs, &gi->string_list, list) { \
0070 if (gs->stringtab_dev.language == new->stringtab_dev.language) \
0071 goto err; \
0072 langs++; \
0073 } \
0074 ret = -EOVERFLOW; \
0075 if (langs >= MAX_USB_STRING_LANGS) \
0076 goto err; \
0077 \
0078 list_add_tail(&new->list, &gi->string_list); \
0079 return &new->group; \
0080 err: \
0081 kfree(new); \
0082 return ERR_PTR(ret); \
0083 } \
0084 \
0085 static void struct_in##_strings_drop( \
0086 struct config_group *group, \
0087 struct config_item *item) \
0088 { \
0089 config_item_put(item); \
0090 } \
0091 \
0092 static struct configfs_group_operations struct_in##_strings_ops = { \
0093 .make_group = &struct_in##_strings_make, \
0094 .drop_item = &struct_in##_strings_drop, \
0095 }; \
0096 \
0097 static struct config_item_type struct_in##_strings_type = { \
0098 .ct_group_ops = &struct_in##_strings_ops, \
0099 .ct_owner = THIS_MODULE, \
0100 }
0101
0102 #endif