summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorINSUN PYO <insun.pyo@samsung.com>2020-03-17 10:18:16 +0900
committerHyotaek Shim <hyotaek.shim@samsung.com>2020-03-17 12:16:12 +0000
commit7abdc64b7d344d1a860d3316e2e9e4580a20ef77 (patch)
tree5d1de1c3cbbb66c0c129b0414ab950a29813c781
parente735c25e91c321b6d745d2885068efa0dd6565ff (diff)
downloadlibdevice-node-7abdc64b7d344d1a860d3316e2e9e4580a20ef77.tar.gz
libdevice-node-7abdc64b7d344d1a860d3316e2e9e4580a20ef77.tar.bz2
libdevice-node-7abdc64b7d344d1a860d3316e2e9e4580a20ef77.zip
Change-Id: I0d9f5c77a729ca706b85ab988aecd76ccb4c12cc (cherry picked from commit 94e1d8be764c68c97c6f2ec15a0dc07ce6e3837f)
-rw-r--r--hw/usb_cfs_client_common.c396
-rw-r--r--hw/usb_client.h22
-rw-r--r--hw/usb_client_common.c15
-rw-r--r--hw/usb_gadget.h7
-rw-r--r--hw/usb_gadget_common.c9
-rw-r--r--unittest/device_haltests.cpp57
6 files changed, 19 insertions, 487 deletions
diff --git a/hw/usb_cfs_client_common.c b/hw/usb_cfs_client_common.c
index f9ebaf7..6a42a51 100644
--- a/hw/usb_cfs_client_common.c
+++ b/hw/usb_cfs_client_common.c
@@ -16,21 +16,16 @@
* limitations under the License.
*/
-#include <hw/usb_client.h>
-#include <hw/shared.h>
-
-#include <limits.h>
-#include <stdio.h>
-#include <string.h>
+#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/mount.h>
+
#include <usbg/usbg.h>
#include <usbg/function/net.h>
-#include <unistd.h>
-
#include <libsyscommon/dbus-systemd.h>
+#include <hw/usb_client.h>
#define zalloc(amount) calloc(1, amount)
@@ -72,107 +67,6 @@ struct usbg_gadget_strs default_g_strs = {
.serial = "01234TEST",
};
-static void cfs_free_config(struct usb_configuration *config)
-{
- int i;
-
- if (!config)
- return;
-
- if (config->strs) {
- for (i = 0; config->strs[i].lang_code; ++i)
- free(config->strs[i].config_str);
-
- free(config->strs);
- }
-
- /*
- * Each function will be free later,
- * for now we cleanup only pointers.
- */
- if (config->funcs)
- free(config->funcs);
-
- free(config);
-}
-
-static void cfs_free_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);
- }
-
- if (gadget->configs) {
- for (i = 0; gadget->configs[i]; ++i)
- cfs_free_config(gadget->configs[i]);
-
- free(gadget->configs);
- }
-
- if (gadget->funcs) {
- for (i = 0; gadget->funcs[i]; ++i)
- gadget->funcs[i]->free_func(gadget->funcs[i]);
-
- free(gadget->funcs);
- }
-
- free(gadget);
-}
-
-static int cfs_read_gadget_attrs_strs(usbg_gadget *gadget,
- struct usb_gadget *usb_gadget)
-{
- struct usbg_gadget_attrs attrs;
- struct usbg_gadget_strs strs;
- int ret;
-
- ret = usbg_get_gadget_attrs(gadget, &attrs);
- if (ret)
- goto out;
-
- usb_gadget->attrs.bDeviceClass = attrs.bDeviceClass;
- usb_gadget->attrs.bDeviceSubClass = attrs.bDeviceSubClass;
- usb_gadget->attrs.bDeviceProtocol = attrs.bDeviceProtocol;
- usb_gadget->attrs.idVendor = attrs.idVendor;
- usb_gadget->attrs.idProduct = attrs.idProduct;
- usb_gadget->attrs.bcdDevice = attrs.bcdDevice;
-
- ret = usbg_get_gadget_strs(gadget, LANG_US_ENG, &strs);
- if (ret)
- goto out;
-
- usb_gadget->strs[0].manufacturer = strdup(strs.manufacturer);
- usb_gadget->strs[0].product = strdup(strs.product);
- usb_gadget->strs[0].serial = strdup(strs.serial);
-
- usbg_free_gadget_strs(&strs);
-
- if (!usb_gadget->strs[0].manufacturer ||
- !usb_gadget->strs[0].product ||
- !usb_gadget->strs[0].serial) {
- ret = -ENOMEM;
- goto err_strs;
- }
-
- return 0;
-err_strs:
- free(usb_gadget->strs[0].manufacturer);
- free(usb_gadget->strs[0].product);
- free(usb_gadget->strs[0].serial);
-out:
- return ret;
-}
-
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))) {
@@ -213,285 +107,6 @@ static int cfs_find_func(const char *name, const char *instance)
return -ENOENT;
}
-static int cfs_alloc_new_func(struct usb_gadget *gadget, const char *fname,
- const char *instance, struct usb_function **_func)
-{
- struct usb_function *func;
- int ret;
-
- ret = cfs_find_func(fname, instance);
- if (ret < 0)
- return -ENOTSUP;
-
- ret = _available_funcs[ret]->clone(_available_funcs[ret], &func);
- if (ret)
- return ret;
-
- *_func = func;
- return 0;
-}
-
-static int cfs_read_funcs(usbg_gadget *gadget, struct usb_gadget *usb_gadget)
-{
- int i;
- int ret;
- const char *name;
- const char *instance;
- usbg_function *function;
-
- i = 0;
- usbg_for_each_function(function, gadget) {
- instance = usbg_get_function_instance(function);
- name = usbg_get_function_type_str(usbg_get_function_type(function));
-
- ret = cfs_alloc_new_func(usb_gadget, name, instance, usb_gadget->funcs + i);
- if (ret < 0)
- goto clean_prev;
- ++i;
- }
-
- return 0;
-
- clean_prev:
- while (i >= 0) {
- usb_gadget->funcs[i]->free_func(usb_gadget->funcs[i]);
- --i;
- }
-
- return ret;
-}
-
-static struct usb_function *cfs_find_func_in_gadget(
- struct usb_gadget *gadget, const char *name, const char *instance)
-{
- int i;
-
- for (i = 0; gadget->funcs[i]; ++i)
- if (cfs_match_func(gadget->funcs[i], name, instance))
- return gadget->funcs[i];
-
- return NULL;
-}
-
-static int cfs_alloc_config(int n_funcs, struct usb_configuration **_config)
-{
- struct usb_configuration *config;
-
- config = zalloc(sizeof(*config));
- if (!config)
- goto out;
-
- config->strs = calloc(2, sizeof(*config->strs));
- if (!config->strs)
- goto free_config;
-
- config->funcs = calloc(n_funcs + 1, sizeof(*config->funcs));
- if (!config->funcs)
- goto free_strs;
-
- *_config = config;
-
- return 0;
-free_strs:
- free(config->strs);
-free_config:
- free(config);
-out:
- return -ENOMEM;
-}
-
-static int cfs_read_config(usbg_config *config, struct usb_gadget *gadget,
- struct usb_configuration *usb_config)
-{
- int i;
- int ret;
- const char *name;
- const char *instance;
- usbg_binding *binding;
- usbg_function *function;
- struct usbg_config_strs c_strs;
- struct usbg_config_attrs c_attrs;
-
- i = 0;
- usbg_for_each_binding(binding, config) {
- function = usbg_get_binding_target(binding);
-
- instance = usbg_get_function_instance(function);
- name = usbg_get_function_type_str(usbg_get_function_type(function));
-
- usb_config->funcs[i] = cfs_find_func_in_gadget(gadget, name, instance);
- if (!usb_config->funcs[i]) {
- return -ENOTSUP;
- }
- ++i;
- }
-
- ret = usbg_get_config_attrs(config, &c_attrs);
- if (ret)
- return ret;
-
- usb_config->attrs.MaxPower = c_attrs.bMaxPower*2;
- usb_config->attrs.bmAttributs = c_attrs.bmAttributes;
-
- ret = usbg_get_config_strs(config, LANG_US_ENG, &c_strs);
- if (ret) {
- usb_config->strs[0].lang_code = 0;
- } else {
- usb_config->strs[0].lang_code = LANG_US_ENG;
- usb_config->strs[0].config_str = strdup(c_strs.configuration);
- if (!usb_config->strs[0].config_str)
- return -ENOMEM;
- }
-
- return 0;
-}
-
-static int cfs_count_bindings(usbg_config *config)
-{
- usbg_binding *b;
- int i = 0;
-
- usbg_for_each_binding(b, config) ++i;
-
- return i;
-}
-
-static int cfs_read_configs(usbg_gadget *gadget, struct usb_gadget *usb_gadget)
-{
- usbg_config *config;
- int i = 0;
- int n_funcs;
- int ret;
-
- usbg_for_each_config(config, gadget) {
- n_funcs = cfs_count_bindings(config);
-
- ret = cfs_alloc_config(n_funcs, usb_gadget->configs + i);
- if (ret)
- goto clean_prev;
- ret = cfs_read_config(config, usb_gadget,
- usb_gadget->configs[i]);
- if (ret)
- goto free_current;
-
- ++i;
- }
-
- return 0;
-free_current:
- free(usb_gadget->configs[i]->strs);
- free(usb_gadget->configs[i]->funcs);
- free(usb_gadget->configs[i]);
-clean_prev:
- while (i >= 0)
- cfs_free_config(usb_gadget->configs[i--]);
- return ret;
-}
-
-static int cfs_count_configs(usbg_gadget *gadget)
-{
- usbg_config *config;
- int i = 0;
-
- usbg_for_each_config(config, gadget) ++i;
-
- return i;
-}
-
-static int cfs_count_functions(usbg_gadget *gadget)
-{
- usbg_function *func;
- int i = 0;
-
- usbg_for_each_function(func, gadget) ++i;
-
- return i;
-}
-
-static int cfs_get_current_gadget(struct usb_client *usb,
- struct usb_gadget **_usb_gadget)
-{
- struct cfs_client *cfs_client;
- struct usb_gadget *usb_gadget;
- struct usb_gadget_strings *strs;
- struct usb_configuration **usb_configs;
- struct usb_function **usb_funcs;
- int n_funcs, n_configs;
- int i;
- int ret = -ENOMEM;
-
- if (!usb)
- return -EINVAL;
-
- cfs_client = container_of(usb, struct cfs_client, client);
-
- usb_gadget = zalloc(sizeof(*usb_gadget));
- if (!usb_gadget)
- goto out;
-
- /*
- * Currently there is no interface in libusbg which
- * allows to list all string languages.
- * That's why we do this only for USA english
- */
- strs = calloc(2, sizeof(*strs));
- if (!strs)
- goto free_gadget;
-
- strs[0].lang_code = LANG_US_ENG;
-
- usb_gadget->strs = strs;
-
- ret = cfs_read_gadget_attrs_strs(cfs_client->gadget, usb_gadget);
- if (ret)
- goto free_strs;
-
- n_funcs = cfs_count_functions(cfs_client->gadget);
- usb_funcs = calloc(n_funcs + 1, sizeof(*usb_funcs));
- if (!usb_funcs)
- goto free_strs_with_content;
-
- usb_gadget->funcs = usb_funcs;
-
- ret = cfs_read_funcs(cfs_client->gadget, usb_gadget);
- if (ret)
- goto free_funcs;
-
- n_configs = cfs_count_configs(cfs_client->gadget);
- usb_configs = calloc(n_configs + 1, sizeof(*usb_configs));
- if (!usb_configs)
- goto free_funcs_with_content;
-
- usb_gadget->configs = usb_configs;
-
- ret = cfs_read_configs(cfs_client->gadget, usb_gadget);
- if (ret)
- goto free_configs;
-
- *_usb_gadget = usb_gadget;
- return 0;
-
-free_configs:
- free(usb_configs);
-free_funcs_with_content:
- for (i = 0; usb_gadget->funcs[i]; ++i)
- usb_gadget->funcs[i]->free_func(usb_gadget->funcs[i]);
-free_funcs:
- free(usb_funcs);
-free_strs_with_content:
- for (i = 0; usb_gadget->strs[i].lang_code; ++i) {
- free(usb_gadget->strs[i].manufacturer);
- free(usb_gadget->strs[i].product);
- free(usb_gadget->strs[i].serial);
- }
-free_strs:
- free(usb_gadget->strs);
-free_gadget:
- free(usb_gadget);
-out:
- return ret;
-}
-
static bool cfs_is_function_supported(struct usb_client *usb,
struct usb_function *func)
{
@@ -1060,13 +675,10 @@ int hw_cfs_gadget_open(struct hw_info *info,
goto err_create_gadget;
cfs_client->client.common.info = info;
- cfs_client->client.get_current_gadget = cfs_get_current_gadget;
+
cfs_client->client.reconfigure_gadget = cfs_reconfigure_gadget;
- cfs_client->client.is_gadget_supported = cfs_is_gadget_supported;
- cfs_client->client.is_function_supported = cfs_is_function_supported;
cfs_client->client.enable = cfs_enable;
cfs_client->client.disable = cfs_disable;
- cfs_client->client.free_gadget = cfs_free_gadget;
*common = &cfs_client->client.common;
return 0;
diff --git a/hw/usb_client.h b/hw/usb_client.h
index 26a334a..9935ed1 100644
--- a/hw/usb_client.h
+++ b/hw/usb_client.h
@@ -22,8 +22,6 @@
#include <hw/common.h>
#include <hw/usb_gadget.h>
-#include <stdbool.h>
-
/**
* The id of this device
*/
@@ -38,30 +36,16 @@
struct usb_client {
struct hw_common common;
- int (*get_current_gadget)(struct usb_client *usb,
- struct usb_gadget **gadget);
-
- int (*reconfigure_gadget)(struct usb_client *usb,
- struct usb_gadget *gadget);
-
- bool (*is_gadget_supported)(struct usb_client *usb,
- struct usb_gadget *gadget);
-
- bool (*is_function_supported)(struct usb_client *usb,
- struct usb_function *func);
+ int (*reconfigure_gadget)(struct usb_client *usb, struct usb_gadget *gadget);
int (*enable)(struct usb_client *usb);
int (*disable)(struct usb_client *usb);
-
- void (*free_gadget)(struct usb_gadget *gadget);
};
-int hw_legacy_gadget_open(struct hw_info *info,
- const char *id, struct hw_common **common);
+int hw_legacy_gadget_open(struct hw_info *info, const char *id, struct hw_common **common);
int hw_legacy_gadget_close(struct hw_common *common);
-int hw_cfs_gadget_open(struct hw_info *info,
- const char *id, struct hw_common **common);
+int hw_cfs_gadget_open(struct hw_info *info, const char *id, struct hw_common **common);
int hw_cfs_gadget_close(struct hw_common *common);
#endif
diff --git a/hw/usb_client_common.c b/hw/usb_client_common.c
index 9968821..e8357e0 100644
--- a/hw/usb_client_common.c
+++ b/hw/usb_client_common.c
@@ -16,16 +16,16 @@
* limitations under the License.
*/
-#include <hw/usb_client.h>
-#include <hw/shared.h>
-#include <limits.h>
-#include <stdio.h>
+#include <errno.h>
#include <string.h>
-#include <unistd.h>
+#include <stdbool.h>
#include <libsyscommon/dbus-systemd.h>
+#include <hw/shared.h>
+#include <hw/usb_client.h>
+
#define zalloc(amount) calloc(1, amount)
#define MAX_GADGET_STR_LEN 256
@@ -671,13 +671,10 @@ int hw_legacy_gadget_open(struct hw_info *info,
return -ENOMEM;
legacy->common.info = info;
- legacy->get_current_gadget = legacy_get_current_gadget;
+
legacy->reconfigure_gadget = legacy_reconfigure_gadget;
- legacy->is_gadget_supported = legacy_is_gadget_supported;
- legacy->is_function_supported = legacy_is_function_supported;
legacy->enable = legacy_enable;
legacy->disable = legacy_disable;
- legacy->free_gadget = legacy_free_gadget;
*common = &legacy->common;
return 0;
diff --git a/hw/usb_gadget.h b/hw/usb_gadget.h
index ad53d0e..fedd8e7 100644
--- a/hw/usb_gadget.h
+++ b/hw/usb_gadget.h
@@ -19,12 +19,9 @@
#ifndef __HW_USB_GADGET_H__
#define __HW_USB_GADGET_H__
-#include <hw/common.h>
+#include <stdint.h>
-#include <stddef.h>
-#include <stdlib.h>
-#include <string.h>
-#include <errno.h>
+#include <hw/common.h>
/**
* The id of this device
diff --git a/hw/usb_gadget_common.c b/hw/usb_gadget_common.c
index c6ea9cd..08fe8a4 100644
--- a/hw/usb_gadget_common.c
+++ b/hw/usb_gadget_common.c
@@ -16,17 +16,16 @@
* limitations under the License.
*/
-#include <hw/usb_gadget.h>
-#include <hw/board.h>
-#include <stdio.h>
-#include <stdlib.h>
#include <errno.h>
#include <string.h>
-#include <unistd.h>
+#include <stdlib.h>
#include <libsyscommon/dbus-systemd.h>
+#include <hw/board.h>
+#include <hw/usb_gadget.h>
+
#define zalloc(amount) calloc(1, amount)
/* Based on slp-gadget and initial version of USB HAL by Taeyoung Kim */
diff --git a/unittest/device_haltests.cpp b/unittest/device_haltests.cpp
index 4aa0de8..3fad8b9 100644
--- a/unittest/device_haltests.cpp
+++ b/unittest/device_haltests.cpp
@@ -1138,51 +1138,6 @@ TEST_F(USBCLIENTHalTest, ReConfigureGadgetP)
EXPECT_EQ(ret, 0) << "Fail to reconfigure_gadget (" << ret << ")";
}
-TEST_F(USBCLIENTHalTest, IsGadgetSupportedP)
-{
- bool flag;
-
- if (!supported)
- return;
-
- if (!client_dev || !client_dev->is_gadget_supported) {
- cout << "There is no function for is_gadget_supported" << endl;
- return;
- }
- flag = client_dev->is_gadget_supported(client_dev, gadget_dev);
- EXPECT_EQ(flag, true) << "Fail to is_gadget_supported (" << flag << ")";
-}
-
-TEST_F(USBCLIENTHalTest, IsFunctionSupportedP)
-{
- bool flag;
-
- if (!supported)
- return;
-
- if (!client_dev || !client_dev->is_function_supported) {
- cout << "There is no function for is_function_supported" << endl;
- return;
- }
- flag = client_dev->is_function_supported(client_dev, *gadget_dev->funcs);
- EXPECT_EQ(flag, true) << "Fail to is_function_supported (" << flag << ")";
-}
-
-TEST_F(USBCLIENTHalTest, GetCurrentGadgetP)
-{
- int ret;
-
- if (!supported)
- return;
-
- if (!client_dev || !client_dev->get_current_gadget) {
- cout << "There is no function for get_current_gadget" << endl;
- return;
- }
- ret = client_dev->get_current_gadget(client_dev, &gadget_dev);
- EXPECT_EQ(ret, 0) << "Fail to get_current_gadget (" << ret << ")";
-}
-
TEST_F(USBCLIENTHalTest, DisableP)
{
int ret;
@@ -1198,18 +1153,6 @@ TEST_F(USBCLIENTHalTest, DisableP)
EXPECT_EQ(ret, 0) << "Fail to disable (" << ret << ")";
}
-TEST_F(USBCLIENTHalTest, FreeGadgetP)
-{
- if (!supported)
- return;
-
- if (!client_dev || !client_dev->free_gadget) {
- cout << "There is no function for free_gadget" << endl;
- return;
- }
- client_dev->free_gadget(gadget_dev);
-}
-
TEST_F(USBCLIENTHalTest, DeinitP)
{
int ret;