From 998f44215d67cdf7aa33d63f012369c3a413bfaa Mon Sep 17 00:00:00 2001 From: INSUN PYO Date: Wed, 25 Mar 2020 19:49:21 +0900 Subject: Refactoring gadget's string The usb gadget uses only one string information. So instead of dynamically creating string information, only use one fixed. Change-Id: I01adf3d0c7ce498e516b08b5a284c63d2940bcb6 --- hw/usb_cfs_client_common.c | 47 ++++++++++++++++++++++--------------------- hw/usb_client_common.c | 46 +++++++++++++++++++++--------------------- hw/usb_gadget.h | 2 +- hw/usb_gadget_common.c | 50 +++++++++++++++++----------------------------- 4 files changed, 66 insertions(+), 79 deletions(-) diff --git a/hw/usb_cfs_client_common.c b/hw/usb_cfs_client_common.c index 3c4740d..db6b781 100644 --- a/hw/usb_cfs_client_common.c +++ b/hw/usb_cfs_client_common.c @@ -111,15 +111,20 @@ static bool cfs_is_function_supported(struct usb_client *usb, 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]; @@ -159,25 +164,24 @@ static int cfs_set_gadget_attrs(struct cfs_client *cfs_client, 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; } @@ -461,11 +465,9 @@ static int cfs_reconfigure_gadget(struct usb_client *usb, struct usb_gadget *gad 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) @@ -587,8 +589,7 @@ int hw_cfs_gadget_open(struct hw_info *info, } 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; diff --git a/hw/usb_client_common.c b/hw/usb_client_common.c index b44ec0c..f0e4e1f 100644 --- a/hw/usb_client_common.c +++ b/hw/usb_client_common.c @@ -82,11 +82,12 @@ static bool legacy_is_gadget_supported(struct usb_client *usb, 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]; @@ -150,29 +151,30 @@ static int legacy_set_gadget_attrs(struct usb_gadget_attrs *attrs) 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, @@ -219,11 +221,9 @@ static int legacy_reconfigure_gadget(struct usb_client *usb, 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) diff --git a/hw/usb_gadget.h b/hw/usb_gadget.h index 1066048..95ad859 100644 --- a/hw/usb_gadget.h +++ b/hw/usb_gadget.h @@ -125,7 +125,7 @@ struct usb_gadget_strings { 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; }; diff --git a/hw/usb_gadget_common.c b/hw/usb_gadget_common.c index cfc138f..17393ec 100644 --- a/hw/usb_gadget_common.c +++ b/hw/usb_gadget_common.c @@ -52,21 +52,16 @@ static void simple_cleanup_config(struct usb_configuration *config) 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) @@ -127,7 +122,6 @@ static int get_device_serial(char **out) 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)); @@ -138,21 +132,15 @@ static int alloc_default_gadget(struct usb_gadget **_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) @@ -164,11 +152,9 @@ static int alloc_default_gadget(struct usb_gadget **_gadget) 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; @@ -184,7 +170,7 @@ static inline struct usb_function *find_func(struct usb_gadget *gadget, 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; @@ -335,7 +321,7 @@ static int simple_id_to_gadget(struct usb_gadget_id *gadget_id, free_configs: free_gadget: - simple_cleanup_gadget(gadget); + cleanup_gadget(gadget); out: return ret; } @@ -369,7 +355,7 @@ DEFINE_USB_FUNCTION(USB_FUNCTION_RMNET, rmnet, 0, NULL, N #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, @@ -425,8 +411,8 @@ int simple_translator_open(struct hw_info *info, 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. * -- cgit v1.2.3