Remove unused usb_client
[platform/core/system/libdevice-node.git] / hw / usb_cfs_client_common.c
index 8eff847..fb599df 100644 (file)
  * 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 <sys/stat.h>
 #include <sys/types.h>
 #include <sys/mount.h>
-#include <usbg/usbg.h>
-#include <unistd.h>
 
+#include <usbg/usbg.h>
+#include <usbg/function/net.h>
 #include <libsyscommon/dbus-systemd.h>
 
-
-#define zalloc(amount) calloc(1, amount)
-
-#define MAX_GADGET_STR_LEN 256
-#define MAX_FUNCS 32
+#include <hw/usb_gadget.h>
+#include <hw/usb_client.h>
 
 #define CONFIGFS_PATH "/sys/kernel/config"
 
 #define NAME_INSTANCE_SEP '.'
 #define MAX_INSTANCE_LEN 512
 
-#define USB_FUNCS_PATH "/dev/usb-funcs/"
+#define USB_FUNCS_PATH "/dev/usb-funcs"
 
 #ifndef EXPORT
 #define EXPORT __attribute__ ((visibility("default")))
 #endif
 
+enum cfs_function_service_operation {
+       CFS_FUNCTION_SERVICE_START,
+       CFS_FUNCTION_SERVICE_STOP,
+       CFS_FUNCTION_SERVICE_POST_STOP,
+};
+
 struct cfs_client {
        struct usb_client client;
        usbg_state *ctx;
@@ -59,715 +58,390 @@ 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 void cfs_free_config(struct usb_configuration *config)
+static struct usb_function *cfs_find_usb_function(usbg_function *function)
 {
-       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);
+       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;
        }
 
-       /*
-        * Each function will be free later,
-        * for now we cleanup only pointers.
-        */
-       if (config->funcs)
-               free(config->funcs);
-
-       free(config);
+       return find_usb_function_by_name_instance(name, instance);
 }
 
-static void cfs_free_gadget(struct usb_gadget *gadget)
+static bool cfs_is_function_supported(struct usb_function *func)
 {
-       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]);
+       bool res;
+       int ret;
 
-               free(gadget->configs);
+       if (!func->is_functionfs) {
+               ret = usbg_lookup_function_type(func->name);
+               res = ret >= 0;
+       } else {
+               /* TODO: Check if socket is available */
+               res = true;
        }
 
-       if (gadget->funcs) {
-               for (i = 0; gadget->funcs[i]; ++i)
-                       gadget->funcs[i]->free_func(gadget->funcs[i]);
-
-               free(gadget->funcs);
-       }
+       return res;
 }
 
-static int cfs_read_gadget_attrs_strs(usbg_gadget *gadget,
-                                     struct usb_gadget *usb_gadget)
+static bool cfs_is_gadget_supported(struct usb_gadget *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;
+       int i, j;
 
-       usb_gadget->strs[0].manufacturer = strdup(strs.manufacturer);
-       usb_gadget->strs[0].product = strdup(strs.product);
-       usb_gadget->strs[0].serial = strdup(strs.serial);
+       if (!gadget || !gadget->configs || !gadget->funcs)
+               return false;
 
-       if (!usb_gadget->strs[0].manufacturer ||
-           !usb_gadget->strs[0].product ||
-           !usb_gadget->strs[0].serial) {
-               ret = -ENOMEM;
-               goto err_strs;
-       }
+       /* only strings in US_en are allowed */
+       if (gadget->strs.lang_code != DEFAULT_LANG)
+               return false;
 
-       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;
-}
+       if (!gadget->strs.manufacturer || !gadget->strs.product || !gadget->strs.serial)
+               return false;
 
-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;
+       for (j = 0; gadget->configs && gadget->configs[j]; ++j) {
+               struct usb_configuration *config = gadget->configs[j];
 
-               sep = strchr(instance, NAME_INSTANCE_SEP);
-               if (!sep || strlen(sep + 1) < 1)
+               if (!config->funcs)
                        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;
+               for (i = 0; config->funcs[i]; ++i)
+                       if (!cfs_is_function_supported(config->funcs[i]))
+                               return false;
        }
 
-       return false;
-}
-
-
-static int cfs_find_func(const char *name, const char *instance)
-{
-       int i;
-
-       for (i = 0; i < ARRAY_SIZE(_available_funcs); ++i)
-               if (cfs_match_func(_available_funcs[i], name, instance))
-                       return i;
+       if (j == 0)
+               return false;
 
-       return -ENOENT;
+       return true;
 }
 
-static int cfs_alloc_new_func(struct usb_gadget *gadget, const char *fname,
-                             const char *instance, struct usb_function **_func)
+static int cfs_set_gadget_attrs(struct cfs_client *cfs_client,
+                               struct usb_gadget_attrs *attrs)
 {
-       struct usb_function *func;
        int ret;
+       struct usbg_gadget_attrs gadget_attrs;
 
-       ret = cfs_find_func(fname, instance);
-       if (ret < 0)
-               return -ENOTSUP;
-
-       ret = _available_funcs[ret]->clone(_available_funcs[ret], &func);
+       ret = usbg_get_gadget_attrs(cfs_client->gadget, &gadget_attrs);
        if (ret)
                return ret;
 
-       *_func = func;
-       return 0;
-}
-
-static int cfs_read_funcs(usbg_gadget *gadget, struct usb_gadget *usb_gadget)
-{
-       usbg_function *func;
-       int i;
-       int ret;
-
-       i = 0;
-       usbg_for_each_function(func, gadget) {
-               char *func_name = (char *)usbg_get_function_type_str(
-                                           usbg_get_function_type(func));
-               char *instance = (char *)usbg_get_function_instance(func);
-
-               ret = cfs_alloc_new_func(usb_gadget, func_name, instance,
-                                        usb_gadget->funcs + i);
-               if (ret < 0)
-                       goto clean_prev;
-               ++i;
-       }
+       gadget_attrs.bDeviceClass = attrs->bDeviceClass;
+       gadget_attrs.bDeviceSubClass = attrs->bDeviceSubClass;
+       gadget_attrs.bDeviceProtocol = attrs->bDeviceProtocol;
+       gadget_attrs.idVendor = attrs->idVendor;
+       gadget_attrs.idProduct = attrs->idProduct;
+       gadget_attrs.bcdDevice = attrs->bcdDevice;
 
-       return 0;
-clean_prev:
-       while (i >= 0) {
-               usb_gadget->funcs[i]->free_func(usb_gadget->funcs[i]);
-               --i;
-       }
+       ret = usbg_set_gadget_attrs(cfs_client->gadget, &gadget_attrs);
 
        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)
+static int cfs_set_gadget_strs(struct cfs_client *cfs_client, struct usb_gadget_strings *strs)
 {
-       usbg_binding *b;
-       usbg_function *func;
-       char *name, *instance;
-       struct usbg_config_attrs c_attrs;
-       struct usbg_config_strs c_strs;
-       int i = 0;
        int ret;
 
-       usbg_for_each_binding(b, config) {
-               func = usbg_get_binding_target(b);
-
-               name = (char *)usbg_get_function_type_str(
-                       usbg_get_function_type(func));
-               instance = (char *)usbg_get_function_instance(func);
-
-               usb_config->funcs[i] = cfs_find_func_in_gadget(gadget,
-                                                              name, instance);
-               if (!usb_config->funcs[i]) {
-                       return -ENOTSUP;
-               }
-               ++i;
-       }
+       if (!strs->manufacturer || !strs->product || !strs->serial)
+               return -EINVAL;
 
-       ret = usbg_get_config_attrs(config, &c_attrs);
+       ret = usbg_set_gadget_str(cfs_client->gadget, USBG_STR_MANUFACTURER, strs->lang_code, strs->manufacturer);
        if (ret)
                return ret;
 
-       usb_config->attrs.MaxPower = c_attrs.bMaxPower*2;
-       usb_config->attrs.bmAttributs = c_attrs.bmAttributes;
+       ret = usbg_set_gadget_str(cfs_client->gadget, USBG_STR_PRODUCT, strs->lang_code, strs->product);
+       if (ret)
+               return ret;
 
-       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;
-       }
+       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_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)
+static int cfs_ensure_dir(char *path)
 {
-       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;
-       }
+       if (mkdir(path, 0770) < 0)
+               return (errno == EEXIST) ? 0 : -errno;
 
        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)
+static int cfs_prep_ffs_service(struct usb_function *usb_func, usbg_function *function)
 {
-       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;
+       int ret;
+       const char *name;
+       const char *service;
+       const char *instance;
+       const char *dev_name;
+       char buf[MAX_INSTANCE_LEN];
 
-       if (!usb)
+       if (!usb_func || !function)
                return -EINVAL;
 
-       cfs_client = container_of(usb, struct cfs_client,
-                                 client);
-
-       usb_gadget = zalloc(sizeof(*usb_gadget));
-       if (!usb_gadget)
-               goto out;
+       if (usbg_get_function_type(function) != USBG_F_FFS)
+               return -EINVAL;
 
-       /*
-        * 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;
+       name = usb_func->name;
+       service = usb_func->service;
+       instance = usb_func->instance;
+       dev_name = usbg_get_function_instance(function);
 
-       strs[0].lang_code = LANG_US_ENG;
+       /* "/dev/usb-funcs" + "/" + "sdb" + "/" + "default"  + '0' */
+       if (strlen(USB_FUNCS_PATH) + strlen(name) + strlen(instance) + 3  > sizeof(buf))
+               return -ENAMETOOLONG;
 
-       usb_gadget->strs = strs;
+       /* mkdir /dev/usb-funcs */
+       ret = cfs_ensure_dir(USB_FUNCS_PATH);
+       if (ret < 0)
+               return ret;
 
-       ret = cfs_read_gadget_attrs_strs(cfs_client->gadget, usb_gadget);
-       if (ret)
-               goto free_strs;
+       /* mkdir /dev/usb-funcs/sdb */
+       snprintf(buf, sizeof(buf), "%s/%s", USB_FUNCS_PATH, name);
+       ret = cfs_ensure_dir(buf);
+       if (ret < 0)
+               goto out_rmdir;
 
+       /* mkdir /dev/usb-funcs/sdb/default */
+       snprintf(buf, sizeof(buf), "%s/%s/%s", USB_FUNCS_PATH, name, instance);
+       ret = cfs_ensure_dir(buf);
+       if (ret < 0)
+               goto out_rmdir;
 
-       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;
+       /* mount -t functionfs sdb.default /dev/usb-funcs/sdb/default */
+       ret = mount(dev_name, buf, "functionfs", 0, NULL);
+       if (ret < 0)
+               goto out_rmdir;
 
-       usb_gadget->funcs = usb_funcs;
+       /* start sdbd.socket */
+       ret = systemd_start_unit_wait_started(service, ".socket", -1);
+       if (ret < 0)
+               goto out_unmount;
 
-       ret = cfs_read_funcs(cfs_client->gadget, usb_gadget);
-       if (ret)
-               goto free_funcs;
+       return 0;
 
-       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;
+out_unmount:
+       umount(buf);
 
-       usb_gadget->configs = usb_configs;
+out_rmdir:
+       snprintf(buf, sizeof(buf), "%s/%s/%s", USB_FUNCS_PATH, name, instance);
+       rmdir(buf);
 
-       ret = cfs_read_configs(cfs_client->gadget, usb_gadget);
-       if (ret)
-               goto free_configs;
+       snprintf(buf, sizeof(buf), "%s/%s", USB_FUNCS_PATH, name);
+       rmdir(buf);
 
-       *_usb_gadget = usb_gadget;
-       return 0;
+       rmdir(USB_FUNCS_PATH);
 
-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)
+static int cfs_cleanup_ffs_service(usbg_function *function)
 {
-       bool res;
        int ret;
+       char buf[MAX_INSTANCE_LEN];
+       struct usb_function *usb_function;
 
-       switch (func->function_group) {
-       case USB_FUNCTION_GROUP_SIMPLE:
-       case USB_FUNCTION_GROUP_WITH_POST_SERVICE:
-               ret = usbg_lookup_function_type(func->name);
-               res = ret >= 0;
-               break;
-       case USB_FUNCTION_GROUP_WITH_SERVICE:
-               /* TODO: Check if socket is available */
-               res = true;
-               break;
-       default:
-               res = false;
-       }
+       if (!function)
+               return -EINVAL;
 
-       return res;
-}
+       usb_function = cfs_find_usb_function(function);
+       if (!usb_function)
+               return -ENOENT;
 
-static bool cfs_is_gadget_supported(struct usb_client *usb,
-                                      struct usb_gadget *gadget)
-{
-       int i, j;
+       /* stop .socket first and stop .service later becuase of socket activation */
+       if (usb_function->service) {
+               (void)systemd_stop_unit_wait_stopped(usb_function->service, ".socket", -1);
+               (void)systemd_stop_unit_wait_stopped(usb_function->service, ".service", -1);
+       }
 
-       if (!gadget || !gadget->configs || !gadget->funcs)
-               return false;
+       /* umount /dev/usb-funcs/[sdb|mtp]/default and remove it's directory */
+       ret = snprintf(buf, sizeof(buf), "%s/%s/%s", USB_FUNCS_PATH, usb_function->name, usb_function->instance);
+       if (ret < 0)
+               return ret;
 
-       /*
-        * TODO
-        * Here is a good place to ensure that serial is immutable
-        */
+       ret = umount(buf);
+       if (ret < 0)
+               return ret;
 
-       /* No real restrictions for strings */
-       for (j = 0; gadget->configs && gadget->configs[j]; ++j) {
-               struct usb_configuration *config = gadget->configs[j];
+       ret = rmdir(buf);
+       if (ret < 0)
+               return ret;
 
-               if (!config->funcs)
-                       return false;
+       /* remove /dev/usb-funcs/[sdb|mtp] directory */
+       ret = snprintf(buf, sizeof(buf), "%s/%s", USB_FUNCS_PATH, usb_function->name);
+       if (ret < 0)
+               return ret;
 
-               for (i = 0; config->funcs[i]; ++i)
-                       if (!cfs_is_function_supported(usb, config->funcs[i]))
-                               return false;
-       }
+       ret = rmdir(buf);
+       if (ret < 0 && errno != ENOTEMPTY)
+               return ret;
 
-       if (j == 0)
-               return false;
+       /* remove /dev/usb-funcs/ directory */
+       ret = rmdir(USB_FUNCS_PATH);
+       if (ret < 0 && errno != ENOTEMPTY)
+               return ret;
 
-       return true;
+       return 0;
 }
 
-static int cfs_set_gadget_attrs(struct cfs_client *cfs_client,
-                               struct usb_gadget_attrs *attrs)
-{
-       int ret;
-       struct usbg_gadget_attrs gadget_attrs;
-
-       ret = usbg_get_gadget_attrs(cfs_client->gadget, &gadget_attrs);
-       if (ret)
-               return ret;
 
-       gadget_attrs.bDeviceClass = attrs->bDeviceClass;
-       gadget_attrs.bDeviceSubClass = attrs->bDeviceSubClass;
-       gadget_attrs.bDeviceProtocol = attrs->bDeviceProtocol;
-       gadget_attrs.idVendor = attrs->idVendor;
-       gadget_attrs.idProduct = attrs->idProduct;
-       gadget_attrs.bcdDevice = attrs->bcdDevice;
+static int cfs_set_rndis_mac_addr(usbg_gadget *gadget, usbg_function *func)
+{
+       int i, ret;
+       struct ether_addr ethaddr;
+       struct usbg_gadget_strs strs;
+       struct usbg_f_net *nf = usbg_to_net_function(func);
 
-       ret = usbg_set_gadget_attrs(cfs_client->gadget, &gadget_attrs);
+       if (!nf)
+               return -EINVAL;
 
-       return ret;
-}
+       ret = usbg_get_gadget_strs(gadget, LANG_US_ENG, &strs);
+       if (ret != USBG_SUCCESS)
+               return ret;
 
-static int cfs_set_gadget_strs(struct cfs_client *cfs_client,
-                                 struct usb_gadget_strings *strs)
-{
-       int ret = 0;
+       for (i = 0; i < ETHER_ADDR_LEN; i++)
+               ethaddr.ether_addr_octet[i] = 0;
 
-       /*
-        * 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;                             \
+       for (i = 0; (i < 256) && strs.serial[i]; i++) {
+               ethaddr.ether_addr_octet[i % (ETHER_ADDR_LEN - 1) + 1] ^= strs.serial[i];
        }
+       ethaddr.ether_addr_octet[0] &= 0xfe;     /* clear multicast bit */
+       ethaddr.ether_addr_octet[0] |= 0x02;     /* set local assignment bit (IEEE802) */
 
-       SET_STR(manufacturer, USBG_STR_MANUFACTURER);
-       SET_STR(product, USBG_STR_PRODUCT);
-       SET_STR(serial, USBG_STR_SERIAL_NUMBER);
-#undef SET_STR
-       return ret;
-}
-
-static int cfs_ensure_dir(char *path)
-{
-       int ret;
+       usbg_free_gadget_strs(&strs);
 
-       ret = mkdir(path, 0770);
-       if (ret < 0)
-               ret = errno == EEXIST ? 0 : errno;
+       /* host_addr changes mac address */
+       ret = usbg_f_net_set_host_addr(nf, &ethaddr);
 
        return ret;
 }
 
-static int cfs_prep_ffs_service(const char *name, const char *instance,
-                               const char *dev_name, const char *socket_name)
+static int cfs_cleanup_all_config_and_function(struct cfs_client *cfs_client)
 {
-       char buf[PATH_MAX];
-       size_t left;
-       char *pos;
        int ret;
+       usbg_config *config;
+       usbg_function *function;
 
-       /* TODO: Add some good error handling */
-
-       left = sizeof(buf);
-       pos = buf;
-       ret = snprintf(pos, left, "%s", USB_FUNCS_PATH);
-       if (ret < 0 || ret >= left) {
-               return -ENAMETOOLONG;
-       } else {
-               left -= ret;
-               pos += ret;
-       }
-       ret = cfs_ensure_dir(buf);
-       if (ret < 0)
-               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;
 
-       ret = snprintf(pos, left, "/%s", name);
-       if (ret < 0 || ret >= left) {
-               return -ENAMETOOLONG;
-       } else {
-               left -= ret;
-               pos += ret;
+               goto restart_rm_config; /* You cannot delete a config directly in an iterator. */
        }
-       ret = cfs_ensure_dir(buf);
-       if (ret < 0)
-               return ret;
 
-       ret = snprintf(pos, left, "/%s", instance);
-       if (ret < 0 || ret >= left) {
-               return -ENAMETOOLONG;
-       } else {
-               left -= ret;
-               pos += ret;
-       }
-       ret = cfs_ensure_dir(buf);
-       if (ret < 0)
-               return ret;
+       /* 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 = mount(dev_name, buf, "functionfs", 0, NULL);
-       if (ret < 0)
-               return ret;
+               ret = usbg_rm_function(function, USBG_RM_RECURSE);
+               if (ret)
+                       return ret;
 
-       ret = systemd_start_unit_wait_started(socket_name, ".socket", -1);
-       if (ret < 0)
-               goto umount_ffs;
+               goto restart_rm_function; /* You cannot delete a function directly in an iterator. */
+       }
 
        return 0;
-umount_ffs:
-       umount(buf);
-       return ret;
 }
 
-static int cfs_set_gadget_config(struct cfs_client *cfs_client,
-                                   int config_id,
-                                   struct usb_configuration *usb_config)
+static int cfs_set_gadget_config(struct cfs_client *cfs_client, int config_id, struct usb_configuration *usb_config)
 {
+       int i;
+       int ret;
+       int function_type;
+       usbg_config *config;
+       usbg_function *function;
+       struct usb_function *usb_func;
+       char instance[MAX_INSTANCE_LEN];
        struct usbg_config_attrs cattrs = {
                .bmAttributes = usb_config->attrs.bmAttributs,
                .bMaxPower = usb_config->attrs.MaxPower/2,
        };
-       usbg_config *config;
-       int i;
-       int ret;
 
        if (!usb_config->funcs || !usb_config->funcs[0])
                return -EINVAL;
 
-       config = usbg_get_config(cfs_client->gadget, config_id, NULL);
-       if (config) {
-               ret = usbg_rm_config(config, USBG_RM_RECURSE);
-               if (ret)
-                       return ret;
-       }
-
-       ret = usbg_create_config(cfs_client->gadget, config_id,
-                                CONFIGFS_CONFIG_LABEL, &cattrs, NULL, &config);
+       ret = usbg_create_config(cfs_client->gadget, config_id, CONFIGFS_CONFIG_LABEL, &cattrs, NULL, &config);
        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;
        }
 
-       for (i = 0; usb_config->funcs && usb_config->funcs[i]; ++i) {
-               struct usb_function *usb_func = usb_config->funcs[i];
-               char instance[MAX_INSTANCE_LEN];
-               int type;
-               usbg_function *func;
-
-               switch (usb_func->function_group) {
-               case USB_FUNCTION_GROUP_SIMPLE:
-               case USB_FUNCTION_GROUP_WITH_POST_SERVICE:
-                       type = usbg_lookup_function_type(usb_func->name);
-                       if (strlen(usb_func->instance) >= MAX_INSTANCE_LEN)
-                               return -ENAMETOOLONG;
-                       strncpy(instance, usb_func->instance, MAX_INSTANCE_LEN);
-                       instance[MAX_INSTANCE_LEN - 1] = '\0';
-                       break;
-               case USB_FUNCTION_GROUP_WITH_SERVICE:
-                       type = USBG_F_FFS;
-                       ret = snprintf(instance, sizeof(instance), "%s%c%s",
-                                      usb_func->name, NAME_INSTANCE_SEP,
-                                      usb_func->instance);
-                       if (ret < 0 || ret >= sizeof(instance))
-                               return -ENAMETOOLONG;
-                       break;
-               default:
-                       return -EINVAL;
+       for (i = 0; usb_config->funcs[i]; ++i) {
+               usb_func = usb_config->funcs[i];
+
+               /* name("sdb") + NAME_INSTANCE_SEP(".") + instance("default") + '\0' */
+               if (strlen(usb_func->name) + strlen(usb_func->instance) + 2 > sizeof(instance))
+                       return -ENAMETOOLONG;
+
+               /* 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), "%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, sizeof(instance) - 1);
+                       instance[sizeof(instance) - 1] = '\0';
                }
 
-
-               func = usbg_get_function(cfs_client->gadget, type, instance);
-               if (!func) {
-                       ret = usbg_create_function(cfs_client->gadget,
-                                                  type,
-                                                  instance,
-                                                  NULL, &func);
+               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;
 
-                       if (usb_func->function_group ==
-                           USB_FUNCTION_GROUP_WITH_SERVICE) {
-                               struct usb_function_with_service *fws;
-
-                               fws = container_of(usb_func,
-                                                  struct usb_function_with_service,
-                                                  func);
-                               ret = cfs_prep_ffs_service(usb_func->name,
-                                                          usb_func->instance,
-                                                          instance,
-                                                          fws->service);
+                       /* 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, func);
-               if (ret)
-                       return ret;
-       }
-
-       return ret;
-}
-
-static int cfs_cleanup_left_configs(struct cfs_client *cfs_client,
-                                   int last_config)
-{
-       usbg_config *lconfig, *config;
-       int ret;
-
-       lconfig = usbg_get_config(cfs_client->gadget, last_config, NULL);
-       for (config = usbg_get_next_config(lconfig);
-            config;
-            config = usbg_get_next_config(lconfig)) {
-               ret = usbg_rm_config(config, USBG_RM_RECURSE);
+               ret = usbg_add_config_function(config, NULL, function);
                if (ret)
                        return ret;
        }
@@ -775,85 +449,121 @@ static int cfs_cleanup_left_configs(struct cfs_client *cfs_client,
        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)
 {
-       struct cfs_client *cfs_client;
        int i;
        int ret;
+       struct cfs_client *cfs_client;
 
-       if (!usb || !gadget || !cfs_is_gadget_supported(usb, gadget))
+       if (!usb || !gadget)
                return -EINVAL;
 
-       cfs_client = container_of(usb, struct cfs_client,
-                                 client);
+       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)
-               goto out;
+               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)
-                       goto out;
-       }
+                       return ret;
+
+       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]);
+               ret = cfs_set_gadget_config(cfs_client, i + 1, gadget->configs[i]);
                if (ret)
-                       goto out;
+                       return ret;
        }
 
-       /* Workaround for enabling extcon notification */
-       /* ******************************************* */
-       /* ******************************************* */
-       {
-               const char *ARTIK_UDC_NAME = "c0040000.dwc2otg";
-               const char *udc_name = usbg_get_udc_name(cfs_client->udc);
-               if (udc_name && !strncmp(udc_name, ARTIK_UDC_NAME, strlen(ARTIK_UDC_NAME))) {
-                       ret = usbg_enable_gadget(cfs_client->gadget, cfs_client->udc);
-                       if (ret)
-                               goto out;
-               }
-       }
-       /* ******************************************* */
-       /* ******************************************* */
-       /* ******************************************* */
+       return 0;
+}
 
-       ret = cfs_cleanup_left_configs(cfs_client, i);
+static void cfs_start_stop_service_and_handler(usbg_gadget *gadget, enum cfs_function_service_operation operation)
+{
+       usbg_function *function;
+       struct usb_function *usb_function;
 
-       /* TODO
-        * Cleanup things which are left after previous gadget
-        */
-out:
-       return ret;
+       usbg_for_each_function(function, gadget) {
+               usb_function = cfs_find_usb_function(function);
+               if (!usb_function)
+                       continue;
+
+               switch(operation) {
+               case CFS_FUNCTION_SERVICE_START:
+                       if (usb_function->handler)
+                               usb_function->handler(1);
+
+                       /* functionfs service is automatically started by socket activation */
+                       if (!usb_function->is_functionfs && usb_function->service)
+                               (void)systemd_start_unit_wait_started(usb_function->service, ".service", -1);
+                       break;
+
+               case CFS_FUNCTION_SERVICE_STOP:
+                       if (!usb_function->is_functionfs && usb_function->service)
+                               (void)systemd_stop_unit_wait_stopped(usb_function->service, ".service", -1);
+
+                       if (usb_function->handler)
+                               usb_function->handler(0);
+                       break;
+
+               case CFS_FUNCTION_SERVICE_POST_STOP:
+                       if (usb_function->is_functionfs && usb_function->service)
+                               (void)systemd_stop_unit_wait_stopped(usb_function->service, ".service", -1);
+                       break;
+
+               default:
+                       break;
+               }
+       }
 }
 
 static int cfs_enable(struct usb_client *usb)
 {
+       int ret;
        struct cfs_client *cfs_client;
 
        if (!usb)
                return -EINVAL;
 
-       cfs_client = container_of(usb, struct cfs_client,
-                                 client);
+       cfs_client = container_of(usb, struct cfs_client, client);
 
-       return usbg_enable_gadget(cfs_client->gadget, cfs_client->udc);
+       ret = usbg_enable_gadget(cfs_client->gadget, cfs_client->udc);
+       if (ret)
+               return ret;
+
+       cfs_start_stop_service_and_handler(cfs_client->gadget, CFS_FUNCTION_SERVICE_START);
+
+       return 0;
 }
 
 static int cfs_disable(struct usb_client *usb)
 {
+       int ret;
        struct cfs_client *cfs_client;
 
        if (!usb)
                return -EINVAL;
 
-       cfs_client = container_of(usb, struct cfs_client,
-                                 client);
+       cfs_client = container_of(usb, struct cfs_client, client);
+
+       cfs_start_stop_service_and_handler(cfs_client->gadget, CFS_FUNCTION_SERVICE_STOP);
 
-       return usbg_disable_gadget(cfs_client->gadget);
+       ret = usbg_disable_gadget(cfs_client->gadget); /* ignore error checking */
+
+       /*
+        * Since functionfs service works with socket activation, you must stop it after disabling gadget.
+        * If usb data may come in after stopping functionfs service and before disabling gadget,
+        * functionfs service wakes up again by socket activation.
+        */
+       cfs_start_stop_service_and_handler(cfs_client->gadget, CFS_FUNCTION_SERVICE_POST_STOP);
+
+       return ret;
 }
 
 EXPORT
@@ -866,7 +576,7 @@ int hw_cfs_gadget_open(struct hw_info *info,
        if (!info || !common)
                return -EINVAL;
 
-       cfs_client = zalloc(sizeof(*cfs_client));
+       cfs_client = calloc(1, sizeof(*cfs_client));
        if (!cfs_client)
                return -ENOMEM;
 
@@ -881,21 +591,18 @@ 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;
 
        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;
 
 err_create_gadget:
@@ -910,36 +617,23 @@ err_usbg_init:
 EXPORT
 int hw_cfs_gadget_close(struct hw_common *common)
 {
+       usbg_function *function;
        struct cfs_client *cfs_client;
-       usbg_function *func;
-       int ret;
+       struct usb_function *usb_func;
 
        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(func, cfs_client->gadget) {
-               char *name = (char *)usbg_get_function_type_str(
-                                           usbg_get_function_type(func));
-               char *instance = (char *)usbg_get_function_instance(func);
-               struct usb_function *usb_func;
-
-               ret = cfs_find_func(name, instance);
-               if (ret < 0)
+       usbg_for_each_function(function, cfs_client->gadget) {
+               usb_func = cfs_find_usb_function(function);
+               if (!usb_func)
                        continue;
 
-               usb_func = _available_funcs[ret];
-               if (usb_func->function_group ==
-                   USB_FUNCTION_GROUP_WITH_SERVICE) {
-                       struct usb_function_with_service *fws;
-
-                       fws = container_of(usb_func,
-                                          struct usb_function_with_service,
-                                          func);
-                       systemd_stop_unit_wait_stopped(fws->service, ".socket", -1);
-                       systemd_stop_unit_wait_stopped(fws->service, ".service", -1);
+               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);
                }
        }