diff options
author | INSUN PYO <insun.pyo@samsung.com> | 2020-03-20 09:03:51 +0900 |
---|---|---|
committer | Hyotaek Shim <hyotaek.shim@samsung.com> | 2020-03-20 01:50:08 +0000 |
commit | 8a8e28cfb44dadc830b8e0931c96ec1d9818b53c (patch) | |
tree | 183673720012a7f6533fcca4c5ecda2f2145e2d6 /hw/usb_cfs_client_common.c | |
parent | 08008d6dfb95c58192b0ba6021762dbe51f9bc36 (diff) | |
download | libdevice-node-8a8e28cfb44dadc830b8e0931c96ec1d9818b53c.tar.gz libdevice-node-8a8e28cfb44dadc830b8e0931c96ec1d9818b53c.tar.bz2 libdevice-node-8a8e28cfb44dadc830b8e0931c96ec1d9818b53c.zip |
Rewrite usb_function to hide global variable _available_funcs
To prevent the global variable _available_funcs from being exposed.
Change-Id: I26b09d3fa8fb5c8117c0029eb94dd3b7efe9a2c7
(cherry picked from commit 3d1a1c4e666cd31b88cd678968658368e988cbaa)
Diffstat (limited to 'hw/usb_cfs_client_common.c')
-rw-r--r-- | hw/usb_cfs_client_common.c | 174 |
1 files changed, 71 insertions, 103 deletions
diff --git a/hw/usb_cfs_client_common.c b/hw/usb_cfs_client_common.c index 577e508..029988b 100644 --- a/hw/usb_cfs_client_common.c +++ b/hw/usb_cfs_client_common.c @@ -69,44 +69,29 @@ struct usbg_gadget_strs default_g_strs = { .serial = "01234TEST", }; -static bool cfs_match_func(struct usb_function *f, - const char *name, const char *instance) { - if (strcmp(name, usbg_get_function_type_str(USBG_F_FFS))) { - /* Standard functions */ - if (!strcmp(name, f->name) && !strcmp(instance, f->instance)) - return true; - } else { - /* Function with service */ - const char *sep, *fname, *finst; - int len; - - sep = strchr(instance, NAME_INSTANCE_SEP); - if (!sep || strlen(sep + 1) < 1) - return false; - - fname = instance; - len = sep - instance; - finst = sep + 1; - - if (strlen(f->name) == len - && !strncmp(f->name, fname, len) - && !strcmp(f->instance, finst)) - return true; - } - - return false; -} - - -static int cfs_find_func(const char *name, const char *instance) +static struct usb_function *cfs_find_usb_function(usbg_function *function) { - int i; - - for (i = 0; _available_funcs[i]; ++i) - if (cfs_match_func(_available_funcs[i], name, instance)) - return i; + char *sep; + char buf[MAX_INSTANCE_LEN]; + const char *instance = usbg_get_function_instance(function); + const char *name = usbg_get_function_type_str(usbg_get_function_type(function)); + + /* Ex. name:"ffs", instance: "sdb.default" */ + if (strcmp(name, usbg_get_function_type_str(USBG_F_FFS)) == 0) { + strncpy(buf, instance, sizeof(buf) - 1); + buf[sizeof(buf) - 1] = '\0'; + + /* Ex. "sdb.default" ==> "sdb" + "default" */ + sep = strchr(buf, NAME_INSTANCE_SEP); + if (!sep || !sep[1]) + return NULL; + *sep = '\0'; + + name = buf; + instance = sep + 1; + } - return -ENOENT; + return find_usb_function_by_name_instance(name, instance); } static bool cfs_is_function_supported(struct usb_client *usb, @@ -134,11 +119,6 @@ static bool cfs_is_gadget_supported(struct usb_client *usb, if (!gadget || !gadget->configs || !gadget->funcs) return false; - /* - * TODO - * Here is a good place to ensure that serial is immutable - */ - /* No real restrictions for strings */ for (j = 0; gadget->configs && gadget->configs[j]; ++j) { struct usb_configuration *config = gadget->configs[j]; @@ -288,23 +268,15 @@ out_rmdir: static int cfs_cleanup_ffs_service(usbg_function *function) { int ret; - int index; - const char *name; - const char *instance; char buf[MAX_INSTANCE_LEN]; struct usb_function *usb_function; if (!function) return -EINVAL; - instance = usbg_get_function_instance(function); /* Ex: sdb.default */ - name = usbg_get_function_type_str(usbg_get_function_type(function)); /* Fixed: ffs */ - - index = cfs_find_func(name, instance); - if (index < 0) - return index; - - usb_function = _available_funcs[index]; + usb_function = cfs_find_usb_function(function); + if (!usb_function) + return -ENOENT; /* stop .socket first and stop .service later becuase of socket activation */ if (usb_function->service) { @@ -374,6 +346,41 @@ static int cfs_set_rndis_mac_addr(usbg_gadget *gadget, usbg_function *func) return ret; } +static int cfs_cleanup_all_config_and_function(struct cfs_client *cfs_client) +{ + int ret; + usbg_config *config; + usbg_function *function; + + /* delete all configs */ +restart_rm_config: + usbg_for_each_config(config, cfs_client->gadget) { + ret = usbg_rm_config(config, USBG_RM_RECURSE); + if (ret) + return ret; + + goto restart_rm_config; /* You cannot delete a config directly in an iterator. */ + } + + /* delete all functions */ +restart_rm_function: + usbg_for_each_function(function, cfs_client->gadget) { + if (usbg_get_function_type(function) == USBG_F_FFS) { + ret = cfs_cleanup_ffs_service(function); + if (ret) + return ret; + } + + ret = usbg_rm_function(function, USBG_RM_RECURSE); + if (ret) + return ret; + + goto restart_rm_function; /* You cannot delete a function directly in an iterator. */ + } + + return 0; +} + static int cfs_set_gadget_config(struct cfs_client *cfs_client, int config_id, struct usb_configuration *usb_config) { int i; @@ -411,10 +418,11 @@ static int cfs_set_gadget_config(struct cfs_client *cfs_client, int config_id, s /* In functionfs, the instance is used in the format "[sdb|mtp].default" instead of "default" */ if (usb_func->is_functionfs) { function_type = USBG_F_FFS; - snprintf(instance, sizeof(instance) - 1, "%s%c%s", usb_func->name, NAME_INSTANCE_SEP, usb_func->instance); + snprintf(instance, sizeof(instance), "%s%c%s", usb_func->name, NAME_INSTANCE_SEP, usb_func->instance); } else { function_type = usbg_lookup_function_type(usb_func->name); - strncpy(instance, usb_func->instance, MAX_INSTANCE_LEN - 1); + strncpy(instance, usb_func->instance, sizeof(instance) - 1); + instance[sizeof(instance) - 1] = '\0'; } ret = usbg_create_function(cfs_client->gadget, function_type, instance, NULL, &function); @@ -446,8 +454,6 @@ static int cfs_reconfigure_gadget(struct usb_client *usb, { int i; int ret; - usbg_config *config; - usbg_function *function; struct cfs_client *cfs_client; if (!usb || !gadget || !cfs_is_gadget_supported(usb, gadget)) @@ -465,31 +471,9 @@ static int cfs_reconfigure_gadget(struct usb_client *usb, return ret; } - /* delete all configs */ -restart_rm_config: - usbg_for_each_config(config, cfs_client->gadget) { - ret = usbg_rm_config(config, USBG_RM_RECURSE); - if (ret) - return ret; - - goto restart_rm_config; /* You cannot delete a config directly in an iterator. */ - } - - /* delete all functions */ -restart_rm_function: - usbg_for_each_function(function, cfs_client->gadget) { - if (usbg_get_function_type(function) == USBG_F_FFS) { - ret = cfs_cleanup_ffs_service(function); - if (ret) - return ret; - } - - ret = usbg_rm_function(function, USBG_RM_RECURSE); - if (ret) - return ret; - - goto restart_rm_function; /* You cannot delete a function directly in an iterator. */ - } + ret = cfs_cleanup_all_config_and_function(cfs_client); + if (ret) + return ret; for (i = 0; gadget->configs && gadget->configs[i]; ++i) { ret = cfs_set_gadget_config(cfs_client, i + 1, gadget->configs[i]); @@ -502,22 +486,14 @@ restart_rm_function: static void cfs_start_stop_service_and_handler(usbg_gadget *gadget, enum cfs_function_service_operation operation) { - int index; - const char *name; - const char *instance; usbg_function *function; struct usb_function *usb_function; usbg_for_each_function(function, gadget) { - instance = usbg_get_function_instance(function); - name = usbg_get_function_type_str(usbg_get_function_type(function)); - - index = cfs_find_func(name, instance); - if (index < 0) + usb_function = cfs_find_usb_function(function); + if (!usb_function) continue; - usb_function = _available_funcs[index]; - switch(operation) { case CFS_FUNCTION_SERVICE_START: if (usb_function->handler) @@ -627,6 +603,7 @@ int hw_cfs_gadget_open(struct hw_info *info, cfs_client->client.disable = cfs_disable; *common = &cfs_client->client.common; + return 0; err_create_gadget: @@ -641,9 +618,6 @@ err_usbg_init: EXPORT int hw_cfs_gadget_close(struct hw_common *common) { - int ret; - const char *name; - const char *instance; usbg_function *function; struct cfs_client *cfs_client; struct usb_function *usb_func; @@ -651,19 +625,13 @@ int hw_cfs_gadget_close(struct hw_common *common) if (!common) return -EINVAL; - cfs_client = container_of(common, struct cfs_client, - client.common); + cfs_client = container_of(common, struct cfs_client, client.common); usbg_for_each_function(function, cfs_client->gadget) { - instance = usbg_get_function_instance(function); - name = usbg_get_function_type_str(usbg_get_function_type(function)); - - ret = cfs_find_func(name, instance); - if (ret < 0) + usb_func = cfs_find_usb_function(function); + if (!usb_func) continue; - usb_func = _available_funcs[ret]; - if (usb_func->is_functionfs && usb_func->service) { (void)systemd_stop_unit_wait_stopped(usb_func->service, ".socket", -1); (void)systemd_stop_unit_wait_stopped(usb_func->service, ".service", -1); |