Fixed to stop services of functionfs when gadget is disabled
[platform/core/system/libdevice-node.git] / hw / usb_cfs_client_common.c
index d594fd2..6d3595f 100644 (file)
@@ -865,6 +865,8 @@ static int cfs_enable(struct usb_client *usb)
 
                if (func->service)
                        (void)systemd_start_unit_wait_started(func->service, ".service", -1);
+
+               /* func->ffs_service is automatically started by socket activation */
        }
 
        cfs_free_gadget(gadget);
@@ -897,10 +899,24 @@ static int cfs_disable(struct usb_client *usb)
                        func->handler(0);
        }
 
+       cfs_client = container_of(usb, struct cfs_client, client);
+       ret = usbg_disable_gadget(cfs_client->gadget); /* ignore error checking */
+
+       /*
+        * Since ffs_service works with socket activation, you must stop it after disabling gadget.
+        * If usb data may come in after stopping ffs_service and before disabling gadget,
+        * ffs_service wakes up again by socket activation.
+        */
+       for (i = 0; gadget->funcs[i]; ++i) {
+               func = gadget->funcs[i];
+
+               if (func->ffs_service)
+                       (void)systemd_stop_unit_wait_stopped(func->ffs_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