Refactoring: replace ffs_service to is_functionfs
[platform/core/system/libdevice-node.git] / hw / usb_cfs_client_common.c
index e4572de..39dfca2 100644 (file)
@@ -497,7 +497,7 @@ static bool cfs_is_function_supported(struct usb_client *usb,
        bool res;
        int ret;
 
-       if (!func->ffs_service) {
+       if (!func->is_functionfs) {
                ret = usbg_lookup_function_type(func->name);
                res = ret >= 0;
        } else {
@@ -730,7 +730,7 @@ static int cfs_set_gadget_config(struct cfs_client *cfs_client,
                int type;
                usbg_function *func;
 
-               if (!usb_func->ffs_service) {
+               if (!usb_func->is_functionfs) {
                        type = usbg_lookup_function_type(usb_func->name);
                        if (strlen(usb_func->instance) >= MAX_INSTANCE_LEN)
                                return -ENAMETOOLONG;
@@ -760,11 +760,11 @@ static int cfs_set_gadget_config(struct cfs_client *cfs_client,
                        if (usbg_get_function_type(func) == USBG_F_RNDIS)
                                (void)cfs_set_rndis_mac_addr(cfs_client->gadget, func); /* A random value is used if fails */
 
-                       if (usb_func->ffs_service) {
+                       if (usb_func->is_functionfs) {
                                ret = cfs_prep_ffs_service(usb_func->name,
                                                        usb_func->instance,
                                                        instance,
-                                                       usb_func->ffs_service);
+                                                       usb_func->service);
                                if (ret)
                                        return ret;
                        }
@@ -826,22 +826,6 @@ static int cfs_reconfigure_gadget(struct usb_client *usb,
                        goto out;
        }
 
-       /* 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;
-               }
-       }
-       /* ******************************************* */
-       /* ******************************************* */
-       /* ******************************************* */
-
        ret = cfs_cleanup_left_configs(cfs_client, i);
 
        /* TODO
@@ -879,7 +863,8 @@ static int cfs_enable(struct usb_client *usb)
                if (func->handler)
                        func->handler(1);
 
-               if (func->service)
+               /* functionfs service is automatically started by socket activation */
+               if (!func->is_functionfs && func->service)
                        (void)systemd_start_unit_wait_started(func->service, ".service", -1);
        }
 
@@ -906,17 +891,31 @@ static int cfs_disable(struct usb_client *usb)
        for (i = 0; gadget->funcs[i]; ++i) {
                func = gadget->funcs[i];
 
-               if (func->service)
+               if (!func->is_functionfs && func->service)
                        (void)systemd_stop_unit_wait_stopped(func->service, ".service", -1);
 
                if (func->handler)
                        func->handler(0);
        }
 
+       cfs_client = container_of(usb, struct cfs_client, client);
+       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.
+        */
+       for (i = 0; gadget->funcs[i]; ++i) {
+               func = gadget->funcs[i];
+
+               if (func->is_functionfs && func->service)
+                       (void)systemd_stop_unit_wait_stopped(func->service, ".service", -1);
+       }
+
        cfs_free_gadget(gadget);
 
-       cfs_client = container_of(usb, struct cfs_client, client);
-       return usbg_disable_gadget(cfs_client->gadget);
+       return ret;
 }
 
 EXPORT
@@ -994,9 +993,10 @@ int hw_cfs_gadget_close(struct hw_common *common)
                        continue;
 
                usb_func = _available_funcs[ret];
-               if (usb_func->ffs_service) {
-                       systemd_stop_unit_wait_stopped(usb_func->ffs_service, ".socket", -1);
-                       systemd_stop_unit_wait_stopped(usb_func->ffs_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);
                }
        }