The usb gadget uses only one string information.
So instead of dynamically creating string information, only use one fixed.
Change-Id: I01adf3d0c7ce498e516b08b5a284c63d2940bcb6
return res;
}
-static bool cfs_is_gadget_supported(struct usb_client *usb,
- struct usb_gadget *gadget)
+static bool cfs_is_gadget_supported(struct usb_client *usb, struct usb_gadget *gadget)
{
int i, j;
if (!gadget || !gadget->configs || !gadget->funcs)
return false;
- /* No real restrictions for strings */
+ /* only strings in US_en are allowed */
+ if (gadget->strs.lang_code != DEFAULT_LANG)
+ return false;
+
+ if (!gadget->strs.manufacturer || !gadget->strs.product || !gadget->strs.serial)
+ return false;
+
for (j = 0; gadget->configs && gadget->configs[j]; ++j) {
struct usb_configuration *config = gadget->configs[j];
return ret;
}
-static int cfs_set_gadget_strs(struct cfs_client *cfs_client,
- struct usb_gadget_strings *strs)
+static int cfs_set_gadget_strs(struct cfs_client *cfs_client, struct usb_gadget_strings *strs)
{
int ret;
-#define SET_STR(FIELD, STR_ID) \
- if (strs->FIELD) { \
- ret = usbg_set_gadget_str(cfs_client->gadget, \
- STR_ID, \
- strs->lang_code, \
- strs->FIELD); \
- if (ret) \
- return ret; \
- }
+ if (!strs->manufacturer || !strs->product || !strs->serial)
+ return -EINVAL;
- SET_STR(manufacturer, USBG_STR_MANUFACTURER);
- SET_STR(product, USBG_STR_PRODUCT);
- SET_STR(serial, USBG_STR_SERIAL_NUMBER);
-#undef SET_STR
+ ret = usbg_set_gadget_str(cfs_client->gadget, USBG_STR_MANUFACTURER, strs->lang_code, strs->manufacturer);
+ if (ret)
+ return ret;
+
+ ret = usbg_set_gadget_str(cfs_client->gadget, USBG_STR_PRODUCT, strs->lang_code, strs->product);
+ if (ret)
+ return ret;
+
+ ret = usbg_set_gadget_str(cfs_client->gadget, USBG_STR_SERIAL_NUMBER, strs->lang_code, strs->serial);
+ if (ret)
+ return ret;
return 0;
}
if (ret)
return ret;
- for (i = 0; gadget->strs && gadget->strs[i].lang_code > 0; ++i) {
- ret = cfs_set_gadget_strs(cfs_client, gadget->strs + i);
+ ret = cfs_set_gadget_strs(cfs_client, &gadget->strs);
if (ret)
return ret;
- }
ret = cfs_cleanup_all_config_and_function(cfs_client);
if (ret)
}
ret = usbg_create_gadget(cfs_client->ctx, CONFIGFS_GADGET_NAME,
- &default_g_attrs, &default_g_strs,
- &cfs_client->gadget);
+ &default_g_attrs, &default_g_strs, &cfs_client->gadget);
if (ret)
goto err_create_gadget;
if (!gadget || !gadget->configs || !gadget->funcs)
return false;
- if (gadget->strs) {
- /* only strings in US_en are allowed */
- if (gadget->strs[0].lang_code != DEFAULT_LANG || gadget->strs[1].lang_code)
+ /* only strings in US_en are allowed */
+ if (gadget->strs.lang_code != DEFAULT_LANG)
return false;
- }
+
+ if (!gadget->strs.manufacturer || !gadget->strs.product)
+ return false;
for (j = 0; gadget->configs[j]; ++j) {
struct usb_configuration *config = gadget->configs[j];
return ret;
ret = legacy_set_int_hex(LEGACY_BCD_DEVICE_PATH, attrs->bcdDevice);
+ if (ret)
+ return ret;
- return ret;
+ return 0;
}
static int legacy_set_gadget_strs(struct usb_gadget_strings *strs)
{
- int ret = 0;
+ int ret;
- /* SLP gadget's kernel driver writes serial number directly */
+ if (!strs->manufacturer || !strs->product)
+ return -EINVAL;
- if (strs->manufacturer) {
- ret = sys_set_str(LEGACY_IMANUFACTURER_PATH, strs->manufacturer);
- if (ret)
- return ret;
- }
+ ret = sys_set_str(LEGACY_IMANUFACTURER_PATH, strs->manufacturer);
+ if (ret)
+ return ret;
- if (strs->product) {
- ret = sys_set_str(LEGACY_IPRODUCT_PATH, strs->product);
- if (ret)
- return ret;
- }
+ ret = sys_set_str(LEGACY_IPRODUCT_PATH, strs->product);
+ if (ret)
+ return ret;
- return ret;
+ /* The serial is written by the slp gadget kernel driver */
+
+ return 0;
}
static int legacy_set_gadget_config(char *cpath,
if (ret)
return ret;
- if (gadget->strs) {
- ret = legacy_set_gadget_strs(gadget->strs + 0);
- if (ret)
- return ret;
- }
+ ret = legacy_set_gadget_strs(&gadget->strs);
+ if (ret)
+ return ret;
ret = legacy_set_gadget_config(LEGACY_CONFIG_1_PATH, gadget->configs[0]);
if (ret)
struct usb_gadget {
struct usb_gadget_attrs attrs;
- struct usb_gadget_strings *strs;
+ struct usb_gadget_strings strs;
struct usb_function **funcs;
struct usb_configuration **configs;
};
free(config);
}
-static void simple_cleanup_gadget(struct usb_gadget *gadget)
+static void cleanup_gadget(struct usb_gadget *gadget)
{
int i;
if (!gadget)
return;
- if (gadget->strs) {
- for (i = 0; gadget->strs[i].lang_code; ++i) {
- free(gadget->strs[i].manufacturer);
- free(gadget->strs[i].product);
- free(gadget->strs[i].serial);
- }
- free(gadget->strs);
- }
+ free(gadget->strs.manufacturer);
+ free(gadget->strs.product);
+ free(gadget->strs.serial);
if (gadget->configs) {
for (i = 0; gadget->configs[i]; ++i)
static int alloc_default_gadget(struct usb_gadget **_gadget)
{
struct usb_gadget *gadget;
- struct usb_gadget_strings *strs;
struct usb_configuration **configs;
gadget = calloc(1, sizeof(*gadget));
gadget->attrs.idProduct = DEFAULT_PID;
gadget->attrs.bcdDevice = DEFAULT_BCD_DEVICE;
- strs = calloc(2, sizeof(*strs));
- if (!strs)
- goto free_gadget;
-
- strs[0].lang_code = DEFAULT_LANG;
- strs[0].manufacturer = strdup(DEFAULT_MANUFACTURER);
- strs[0].product = strdup(DEFAULT_PRODUCT);
- if (get_device_serial(&strs[0].serial) < 0)
- strs[0].serial = strdup(DEFAULT_SERIAL);
+ gadget->strs.lang_code = DEFAULT_LANG;
+ gadget->strs.manufacturer = strdup(DEFAULT_MANUFACTURER);
+ gadget->strs.product = strdup(DEFAULT_PRODUCT);
+ if (get_device_serial(&gadget->strs.serial) < 0)
+ gadget->strs.serial = strdup(DEFAULT_SERIAL);
- if (!strs[0].manufacturer || !strs[0].product || !strs[0].serial)
+ if (!gadget->strs.manufacturer || !gadget->strs.product || !gadget->strs.serial)
goto free_strs;
- gadget->strs = strs;
-
/* slp-gadget use max 2 confiuration and NULL termination */
configs = calloc(3, sizeof(*configs));
if (!configs)
return 0;
free_strs:
- free(strs[0].manufacturer);
- free(strs[0].product);
- free(strs[0].serial);
- free(strs);
-free_gadget:
+ free(gadget->strs.manufacturer);
+ free(gadget->strs.product);
+ free(gadget->strs.serial);
free(gadget);
out:
return -ENOMEM;
return gadget->funcs[i];
}
-static int simple_id_to_gadget(struct usb_gadget_id *gadget_id,
+static int id_to_gadget(struct usb_gadget_id *gadget_id,
struct usb_gadget **_gadget)
{
int ret;
free_configs:
free_gadget:
- simple_cleanup_gadget(gadget);
+ cleanup_gadget(gadget);
out:
return ret;
}
#undef DEFINE_USB_FUNCTION
-/* Caution: index order of arrary is important, because simple_id_to_gadget() uses it. */
+/* Caution: index order of arrary is important, because id_to_gadget() uses it. */
static struct usb_function *_available_funcs[] = {
[USB_FUNCTION_IDX_MTP] = &_mtp_function,
[USB_FUNCTION_IDX_ACM] = &_acm_function,
return -ENOMEM;
simple_translator->common.info = info;
- simple_translator->id_to_gadget = simple_id_to_gadget;
- simple_translator->cleanup_gadget = simple_cleanup_gadget;
+ simple_translator->id_to_gadget = id_to_gadget;
+ simple_translator->cleanup_gadget = cleanup_gadget;
/* Use mtp-responder-dummy.socket when there is no mtp-responser.socket.
*