#include <linux/configfs.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/device.h>
#include <linux/usb/composite.h>
#include <linux/usb/gadget_configfs.h>
int check_user_usb_string(const char *name,
struct usb_gadget_strings *stringtab_dev)
{
unsigned primary_lang;
unsigned sub_lang;
u16 num;
int ret;
ret = kstrtou16(name, 0, &num);
if (ret)
return ret;
primary_lang = num & 0x3ff;
sub_lang = num >> 10;
/* simple sanity check for valid langid */
switch (primary_lang) {
case 0:
case 0x62 ... 0xfe:
case 0x100 ... 0x3ff:
return -EINVAL;
}
if (!sub_lang)
return -EINVAL;
stringtab_dev->language = num;
return 0;
}
#define MAX_NAME_LEN 40
#define MAX_USB_STRING_LANGS 2
struct gadget_info {
struct config_group group;
struct config_group functions_group;
struct config_group configs_group;
struct config_group strings_group;
struct config_group *default_groups[4];
struct mutex lock;
struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1];
struct list_head string_list;
struct list_head available_func;
const char *udc_name;
#ifdef CONFIG_USB_OTG
struct usb_otg_descriptor otg;
#endif
struct usb_composite_driver composite;
struct usb_composite_dev cdev;
};
struct config_usb_cfg {
struct config_group group;
struct config_group strings_group;
struct config_group *default_groups[2];
struct list_head string_list;
struct usb_configuration c;
struct list_head func_list;
struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1];
};
struct gadget_strings {
struct usb_gadget_strings stringtab_dev;
struct usb_string strings[USB_GADGET_FIRST_AVAIL_IDX];
char *manufacturer;
char *product;
char *serialnumber;
struct config_group group;
struct list_head list;
};
struct gadget_config_name {
struct usb_gadget_strings stringtab_dev;
struct usb_string strings;
char *configuration;
struct config_group group;
struct list_head list;
};
static int usb_string_copy(const char *s, char **s_copy)
{
int ret;
char *str;
char *copy = *s_copy;
ret = strlen(s);
if (ret > 126)
return -EOVERFLOW;
str = kstrdup(s, GFP_KERNEL);
if (!str)
return -ENOMEM;
if (str[ret - 1] == '\n')
str[ret - 1] = '\0';
kfree(copy);
*s_copy = str;
return 0;
}
CONFIGFS_ATTR_STRUCT(gadget_info);
CONFIGFS_ATTR_STRUCT(config_usb_cfg);
#define GI_DEVICE_DESC_ITEM_ATTR(name) \
static struct gadget_info_attribute gadget_cdev_desc_##name = \
__CONFIGFS_ATTR(name, S_IRUGO | S_IWUSR, \
gadget_dev_desc_##name##_show, \
gadget_dev_desc_##name##_store)
#define GI_DEVICE_DESC_SIMPLE_R_u8(__name) \
static ssize_t gadget_dev_desc_##__name##_show(struct gadget_info *gi, \
char *page) \
{ \
return sprintf(page, "0x%02x\n", gi->cdev.desc.__name); \
}
#define GI_DEVICE_DESC_SIMPLE_R_u16(__name) \
static ssize_t gadget_dev_desc_##__name##_show(struct gadget_info *gi, \
char *page) \
{ \
return sprintf(page, "0x%04x\n", le16_to_cpup(&gi->cdev.desc.__name)); \
}
#define GI_DEVICE_DESC_SIMPLE_W_u8(_name) \
static ssize_t gadget_dev_desc_##_name##_store(struct gadget_info *gi, \
const char *page, size_t len) \
{ \
u8 val; \
int ret; \
ret = kstrtou8(page, 0, &val); \
if (ret) \
return ret; \
gi->cdev.desc._name = val; \
return len; \
}
#define GI_DEVICE_DESC_SIMPLE_W_u16(_name) \
static ssize_t gadget_dev_desc_##_name##_store(struct gadget_info *gi, \
const char *page, size_t len) \
{ \
u16 val; \
int ret; \
ret = kstrtou16(page, 0, &val); \
if (ret) \
return ret; \
gi->cdev.desc._name = cpu_to_le16p(&val); \
return len; \
}
#define GI_DEVICE_DESC_SIMPLE_RW(_name, _type) \
GI_DEVICE_DESC_SIMPLE_R_##_type(_name) \
GI_DEVICE_DESC_SIMPLE_W_##_type(_name)
GI_DEVICE_DESC_SIMPLE_R_u16(bcdUSB);
GI_DEVICE_DESC_SIMPLE_RW(bDeviceClass, u8);
GI_DEVICE_DESC_SIMPLE_RW(bDeviceSubClass, u8);
GI_DEVICE_DESC_SIMPLE_RW(bDeviceProtocol, u8);
GI_DEVICE_DESC_SIMPLE_RW(bMaxPacketSize0, u8);
GI_DEVICE_DESC_SIMPLE_RW(idVendor, u16);
GI_DEVICE_DESC_SIMPLE_RW(idProduct, u16);
GI_DEVICE_DESC_SIMPLE_R_u16(bcdDevice);
static ssize_t is_valid_bcd(u16 bcd_val)
{
if ((bcd_val & 0xf) > 9)
return -EINVAL;
if (((bcd_val >> 4) & 0xf) > 9)
return -EINVAL;
if (((bcd_val >> 8) & 0xf) > 9)
return -EINVAL;
if (((bcd_val >> 12) & 0xf