summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorINSUN PYO <insun.pyo@samsung.com>2020-03-30 05:05:19 (GMT)
committerHyotaek Shim <hyotaek.shim@samsung.com>2020-04-07 01:27:47 (GMT)
commit4d8f43f7cf575e93aa0318c5b1a9571b077284dc (patch)
treee9672b952da62d68439b2dc96e9086fae60ce4a4
parent65079e9b1722bbd03370e53f3e86469bd3eaf9dc (diff)
downloadlibdevice-node-4d8f43f7cf575e93aa0318c5b1a9571b077284dc.zip
libdevice-node-4d8f43f7cf575e93aa0318c5b1a9571b077284dc.tar.gz
libdevice-node-4d8f43f7cf575e93aa0318c5b1a9571b077284dc.tar.bz2
Refactoring usb function variable in usb gadgetrefs/changes/61/229961/1
Change-Id: I494c5dbb285870f226fe64392b6b880797309761 (cherry picked from commit 8e03ec0d2c4b7beb09d8a49fa626fb807b991789)
-rw-r--r--hw/usb_cfs_client_common.c2
-rw-r--r--hw/usb_client_common.c2
-rw-r--r--hw/usb_gadget.h2
-rw-r--r--hw/usb_gadget_common.c69
4 files changed, 26 insertions, 49 deletions
diff --git a/hw/usb_cfs_client_common.c b/hw/usb_cfs_client_common.c
index 1e30ab5..f0cbd7d 100644
--- a/hw/usb_cfs_client_common.c
+++ b/hw/usb_cfs_client_common.c
@@ -119,7 +119,7 @@ static bool cfs_is_gadget_supported(struct usb_gadget *gadget)
int i, j;
struct usb_configuration *config;
- if (!gadget || !gadget->configs || !gadget->configs[0] || !gadget->funcs)
+ if (!gadget || !gadget->configs || !gadget->configs[0] || !gadget->configs[0]->funcs[0])
return false;
if (!gadget->attrs.idVendor || !gadget->attrs.idProduct || !gadget->attrs.bcdDevice)
diff --git a/hw/usb_client_common.c b/hw/usb_client_common.c
index 87a418d..e8f8473 100644
--- a/hw/usb_client_common.c
+++ b/hw/usb_client_common.c
@@ -75,7 +75,7 @@ static bool legacy_is_gadget_supported(struct usb_gadget *gadget)
int i, j;
struct usb_configuration *config;
- if (!gadget || !gadget->configs || !gadget->configs[0] || !gadget->funcs)
+ if (!gadget || !gadget->configs || !gadget->configs[0] || !gadget->configs[0]->funcs[0])
return false;
if (!gadget->attrs.idVendor || !gadget->attrs.idProduct || !gadget->attrs.bcdDevice)
diff --git a/hw/usb_gadget.h b/hw/usb_gadget.h
index 4dd3be9..c0ac3dc 100644
--- a/hw/usb_gadget.h
+++ b/hw/usb_gadget.h
@@ -126,7 +126,6 @@ struct usb_gadget_strings {
struct usb_gadget {
struct usb_gadget_attrs attrs;
struct usb_gadget_strings strs;
- struct usb_function **funcs;
struct usb_configuration **configs;
};
@@ -144,6 +143,7 @@ struct usb_gadget_translator {
int simple_translator_open(struct hw_info *info, const char *id, struct hw_common **common);
int simple_translator_close(struct hw_common *common);
+struct usb_function *find_usb_function_by_id(int id);
struct usb_function *find_usb_function_by_name(const char *name);
struct usb_function *find_usb_function_by_name_instance(const char *name, const char *instance);
diff --git a/hw/usb_gadget_common.c b/hw/usb_gadget_common.c
index 270d72b..d0de9fd 100644
--- a/hw/usb_gadget_common.c
+++ b/hw/usb_gadget_common.c
@@ -63,9 +63,6 @@ static void cleanup_gadget(struct usb_gadget *gadget)
free(gadget->configs);
}
- if (gadget->funcs)
- free(gadget->funcs);
-
free(gadget);
}
@@ -146,25 +143,12 @@ out:
return -ENOMEM;
}
-static inline struct usb_function *find_func(struct usb_gadget *gadget,
- int func_id)
-{
- int i;
-
- for (i = 0; gadget->funcs[i] && gadget->funcs[i]->id != func_id; ++i);
-
- return gadget->funcs[i];
-}
-
-static int id_to_gadget(struct usb_gadget_id *gadget_id,
- struct usb_gadget **_gadget)
+static int id_to_gadget(struct usb_gadget_id *gadget_id, struct usb_gadget **_gadget)
{
int ret;
- int idx, i, j;
- int n_functions;
- int n_configs = 0;
+ int i, j;
+ int n_configs;
struct usb_gadget *gadget;
- struct usb_function **funcs;
int functions[2][sizeof(gadget_id->function_mask)*8]; /* zero terminates */
if (!gadget_id || !_gadget)
@@ -260,30 +244,9 @@ static int id_to_gadget(struct usb_gadget_id *gadget_id,
goto free_gadget;
};
- n_functions = __builtin_popcount(gadget_id->function_mask);
-
- funcs = calloc(n_functions + 1, sizeof(*funcs));
- if (!funcs) {
- ret = -ENOMEM;
- goto free_gadget;
- }
-
- gadget->funcs = funcs;
-
- idx = 0;
- for (i = 0; _available_funcs[i]; ++i) {
- int func_id = 1 << i;
-
- if (!(gadget_id->function_mask & func_id))
- continue;
-
- gadget->funcs[idx] = _available_funcs[i];
- ++idx;
- }
-
for (j = 0; j < n_configs; ++j) {
- struct usb_configuration *config;
int n_funcs_in_config;
+ struct usb_configuration *config;
for (i = 0; functions[j][i]; ++i);
n_funcs_in_config = i;
@@ -293,16 +256,19 @@ static int id_to_gadget(struct usb_gadget_id *gadget_id,
goto free_configs;
gadget->configs[j] = config;
- config->funcs = calloc(n_funcs_in_config + 1,
- sizeof(void *));
+ config->funcs = calloc(n_funcs_in_config + 1, sizeof(void *));
if (!config->funcs)
goto free_configs;
- for (i = 0; functions[j][i]; ++i)
- config->funcs[i] = find_func(gadget, functions[j][i]);
+ for (i = 0; functions[j][i]; ++i) {
+ config->funcs[i] = find_usb_function_by_id(functions[j][i]);
+ if (!config->funcs[i])
+ goto free_configs;
+ }
}
*_gadget = gadget;
+
return 0;
free_configs:
@@ -341,7 +307,7 @@ DEFINE_USB_FUNCTION(USB_FUNCTION_RMNET, rmnet, 0, NULL, N
#undef DEFINE_USB_FUNCTION
-/* Caution: index order of arrary is important, because id_to_gadget() uses it. */
+/* Caution: index order of arrary is important, because simple_translator_open() uses it. */
static struct usb_function *_available_funcs[] = {
[USB_FUNCTION_IDX_MTP] = &_mtp_function,
[USB_FUNCTION_IDX_ACM] = &_acm_function,
@@ -354,6 +320,17 @@ static struct usb_function *_available_funcs[] = {
[USB_FUNCTION_IDX_MAX] = NULL /* An indicator to end the array */
};
+struct usb_function *find_usb_function_by_id(int id)
+{
+ int i;
+
+ for (i = 0; _available_funcs[i]; i++)
+ if (_available_funcs[i]->id == id)
+ return _available_funcs[i];
+
+ return NULL;
+}
+
struct usb_function *find_usb_function_by_name(const char *name)
{
int i;