Remove unused usb_client
[platform/core/system/libdevice-node.git] / hw / usb_cfs_client_common.c
index 029988b..fb599df 100644 (file)
@@ -26,6 +26,7 @@
 #include <usbg/function/net.h>
 #include <libsyscommon/dbus-systemd.h>
 
+#include <hw/usb_gadget.h>
 #include <hw/usb_client.h>
 
 #define CONFIGFS_PATH "/sys/kernel/config"
@@ -57,16 +58,15 @@ struct cfs_client {
 
 /* Based on values in slp-gadget kernel module */
 struct usbg_gadget_attrs default_g_attrs = {
-       .bcdUSB = 0x0200,
-       .idVendor = 0x04e8,
-       .idProduct = 0x6860,
-       .bcdDevice = 0x0100,
+       .idVendor = DEFAULT_VID,
+       .idProduct = DEFAULT_PID,
+       .bcdDevice = DEFAULT_BCD_DEVICE,
 };
 
 struct usbg_gadget_strs default_g_strs = {
-       .manufacturer = "Samsung",
-       .product = "TIZEN",
-       .serial = "01234TEST",
+       .manufacturer = DEFAULT_MANUFACTURER,
+       .product = DEFAULT_PRODUCT,
+       .serial = DEFAULT_SERIAL,
 };
 
 static struct usb_function *cfs_find_usb_function(usbg_function *function)
@@ -94,8 +94,7 @@ static struct usb_function *cfs_find_usb_function(usbg_function *function)
        return find_usb_function_by_name_instance(name, instance);
 }
 
-static bool cfs_is_function_supported(struct usb_client *usb,
-                                        struct usb_function *func)
+static bool cfs_is_function_supported(struct usb_function *func)
 {
        bool res;
        int ret;
@@ -111,15 +110,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_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];
 
@@ -127,7 +131,7 @@ static bool cfs_is_gadget_supported(struct usb_client *usb,
                        return false;
 
                for (i = 0; config->funcs[i]; ++i)
-                       if (!cfs_is_function_supported(usb, config->funcs[i]))
+                       if (!cfs_is_function_supported(config->funcs[i]))
                                return false;
        }
 
@@ -159,41 +163,34 @@ 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 = 0;
+       int ret;
 
-       /*
-        * TODO
-        * Here is a good place to ensure that serial is immutable
-        */
-#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
-       return ret;
+       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;
 }
 
 static int cfs_ensure_dir(char *path)
 {
-       int ret;
+       if (mkdir(path, 0770) < 0)
+               return (errno == EEXIST) ? 0 : -errno;
 
-       ret = mkdir(path, 0770);
-       if (ret < 0)
-               ret = errno == EEXIST ? 0 : errno;
-
-       return ret;
+       return 0;
 }
 
 
@@ -402,8 +399,8 @@ static int cfs_set_gadget_config(struct cfs_client *cfs_client, int config_id, s
        if (ret)
                return ret;
 
-       for (i = 0; usb_config->strs && usb_config->strs[i].lang_code; ++i) {
-               ret = usbg_set_config_string(config, usb_config->strs[i].lang_code, usb_config->strs[i].config_str);
+       if (usb_config->strs.config_str) {
+               ret = usbg_set_config_string(config, usb_config->strs.lang_code, usb_config->strs.config_str);
                if (ret)
                        return ret;
        }
@@ -425,20 +422,23 @@ static int cfs_set_gadget_config(struct cfs_client *cfs_client, int config_id, s
                        instance[sizeof(instance) - 1] = '\0';
                }
 
-               ret = usbg_create_function(cfs_client->gadget, function_type, instance, NULL, &function);
-               if (ret)
-                       return ret;
-
-               /* Setting rndis mac address. This should be done at this point,
-                * since the node host_addr changes to read only after the function
-                * is added to config. */
-               if (usbg_get_function_type(function) == USBG_F_RNDIS)
-                       (void)cfs_set_rndis_mac_addr(cfs_client->gadget, function); /* A random value is used if fails */
-
-               if (usbg_get_function_type(function) == USBG_F_FFS) {
-                       ret = cfs_prep_ffs_service(usb_func, function);
+               function = usbg_get_function(cfs_client->gadget, function_type, instance);
+               if (!function) {
+                       ret = usbg_create_function(cfs_client->gadget, function_type, instance, NULL, &function);
                        if (ret)
                                return ret;
+
+                       /* Setting rndis mac address. This should be done at this point,
+                        * since the node host_addr changes to read only after the function
+                        * is added to config. */
+                       if (usbg_get_function_type(function) == USBG_F_RNDIS)
+                               (void)cfs_set_rndis_mac_addr(cfs_client->gadget, function); /* A random value is used if fails */
+
+                       if (usbg_get_function_type(function) == USBG_F_FFS) {
+                               ret = cfs_prep_ffs_service(usb_func, function);
+                               if (ret)
+                                       return ret;
+                       }
                }
 
                ret = usbg_add_config_function(config, NULL, function);
@@ -449,27 +449,27 @@ static int cfs_set_gadget_config(struct cfs_client *cfs_client, int config_id, s
        return 0;
 }
 
-static int cfs_reconfigure_gadget(struct usb_client *usb,
-                                 struct usb_gadget *gadget)
+static int cfs_reconfigure_gadget(struct usb_client *usb, struct usb_gadget *gadget)
 {
        int i;
        int ret;
        struct cfs_client *cfs_client;
 
-       if (!usb || !gadget || !cfs_is_gadget_supported(usb, gadget))
+       if (!usb || !gadget)
                return -EINVAL;
 
+       if (!cfs_is_gadget_supported(gadget))
+               return -ENOTSUP;
+
        cfs_client = container_of(usb, struct cfs_client, client);
 
        ret = cfs_set_gadget_attrs(cfs_client, &gadget->attrs);
        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)
@@ -591,8 +591,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;