Refactoring: replace ffs_service to is_functionfs 91/227491/2
authorINSUN PYO <insun.pyo@samsung.com>
Thu, 12 Mar 2020 07:03:07 +0000 (16:03 +0900)
committerINSUN PYO <insun.pyo@samsung.com>
Thu, 12 Mar 2020 07:26:36 +0000 (07:26 +0000)
Change-Id: Ibb74b5627ef1376be141fb23e56787e45b34c142

hw/usb_cfs_client_common.c
hw/usb_client_common.c
hw/usb_gadget.h
hw/usb_gadget_common.c

index 6d3595f..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;
                        }
@@ -863,10 +863,9 @@ 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);
-
-               /* func->ffs_service is automatically started by socket activation */
        }
 
        cfs_free_gadget(gadget);
@@ -892,7 +891,7 @@ 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)
@@ -903,15 +902,15 @@ static int cfs_disable(struct usb_client *usb)
        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.
+        * 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->ffs_service)
-                       (void)systemd_stop_unit_wait_stopped(func->ffs_service, ".service", -1);
+               if (func->is_functionfs && func->service)
+                       (void)systemd_stop_unit_wait_stopped(func->service, ".service", -1);
        }
 
        cfs_free_gadget(gadget);
@@ -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);
                }
        }
 
index d008485..9968821 100644 (file)
@@ -557,14 +557,6 @@ static int legacy_enable(struct usb_client *usb)
                if (func->handler)
                        func->handler(1);
 
-               /*
-                * Reuse configfs data structure to simplify design.
-                * Configfs has a special service for functionfs.  (E.g. sdbd and mtp-responder)
-                * The legacy usb gadget must handle both services.
-                */
-               if (func->ffs_service)
-                       (void)systemd_start_unit_wait_started(func->ffs_service, ".service", -1);
-
                if (func->service)
                        (void)systemd_start_unit_wait_started(func->service, ".service", -1);
        }
@@ -591,17 +583,9 @@ static int legacy_disable(struct usb_client *usb)
        for (i = 0; gadget->funcs[i]; ++i) {
                func = gadget->funcs[i];
 
-               /*
-                * Reuse configfs data structure to simplify design.
-                * Configfs has a special service for functionfs.  (E.g. sdbd and mtp-responder)
-                * The legacy usb gadget must handle both services.
-                */
                if (func->service)
                        (void)systemd_stop_unit_wait_stopped(func->service, ".service", -1);
 
-               if (func->ffs_service)
-                       (void)systemd_stop_unit_wait_stopped(func->ffs_service, ".service", -1);
-
                if (func->handler)
                        func->handler(0);
        }
index dc4b539..ad53d0e 100644 (file)
@@ -62,23 +62,24 @@ typedef enum {
 
 
 /*
- * legacy enable(usb plug)    : enable gadget -> handler(1) -> ffs_service start -> service start
- * legacy disable(usb unplug) : service stop -> ffs_service stop -> handler(0) -> disable gadget
+ * legacy enable(usb plug)    : enable gadget -> handler(1) -> service start
+ * legacy disable(usb unplug) : service stop ->  handler(0) -> disable gadget
  *
- * configfs init(booting)       : ffs_service.socket start
- * configfs enable(usb plug)*   : enable gadget -> handler(1) -> service start
- * configfs disable(usb unplug) : service stop -> handler(0) -> disable gadget -> ffs_service.service stop
- * configfs deinit              : ffs_service.socket stop
+ * configfs init(booting)       : service.socket start
+ * configfs enable(usb plug)*   : enable gadget -> handler(1)
+ * configfs disable(usb unplug) : handler(0) -> disable gadget -> service.service stop
+ * configfs deinit              : service.socket stop
  *
- * Since ffs_service works by socket activation, it will be started automatically when data is enqueued to the usb socket.
- * So when enabling configfs gadget, it doesn't start ffs_service.
+ * Since functionfs of configfs works by socket activation,
+ * it will be started automatically when data is enqueued to the usb socket.
+ * So when enabling configfs gadget, it doesn't start the service for functionfs.
  */
 struct usb_function {
        int id;
        const char *name;
        const char *instance;
 
-       const char *ffs_service; /* only used in configfs */
+       int is_functionfs;
        const char *service;
 
        void (*handler)(int enable);
index a061084..c6ea9cd 100644 (file)
@@ -381,7 +381,6 @@ static void free_simple_func(struct usb_function *func)
        if (func) {
                free((void *)func->name);
                free((void *)func->instance);
-               free((void *)func->ffs_service);
                free((void *)func->service);
                free(func);
        }
@@ -406,12 +405,6 @@ static int clone_simple_func(struct usb_function *func,
        if (!other->name || !other->instance)
                goto out_nomem;
 
-       if (func->ffs_service) {
-               other->ffs_service = strdup(func->ffs_service);
-               if (!other->ffs_service)
-                       goto out_nomem;
-       }
-
        if (func->service) {
                other->service = strdup(func->service);
                if (!other->service)
@@ -426,26 +419,27 @@ out_nomem:
        return -ENOMEM;
 }
 
-#define DEFINE_USB_FUNCTION(_id, _name, _ffs_service, _service, _handler)      \
-       static struct usb_function _##_name##_function = {                      \
-               .id = _id,                                                      \
-               .name = #_name,                                                 \
-               .instance = "default",                                          \
-               .ffs_service = _ffs_service,                                    \
-               .service = _service,                                            \
-               .handler = _handler,                                            \
-               .clone = clone_simple_func,                                     \
-               .free_func = free_simple_func,                                  \
+#define DEFINE_USB_FUNCTION(_id, _name, _is_functionfs, _service, _handler)  \
+       static struct usb_function _##_name##_function = {                   \
+               .id = _id,                                                   \
+               .name = #_name,                                              \
+               .instance = "default",                                       \
+               .is_functionfs = _is_functionfs,                             \
+               .service = _service,                                         \
+               .handler = _handler,                                         \
+               .clone = clone_simple_func,                                  \
+               .free_func = free_simple_func,                               \
        }
 
-DEFINE_USB_FUNCTION(USB_FUNCTION_DIAG,        diag,        NULL, NULL,            NULL);
-DEFINE_USB_FUNCTION(USB_FUNCTION_RMNET,       rmnet,       NULL, NULL,            NULL);
-DEFINE_USB_FUNCTION(USB_FUNCTION_DM,          dm,          NULL, NULL,            NULL);
-DEFINE_USB_FUNCTION(USB_FUNCTION_CONN_GADGET, conn_gadget, NULL, NULL,            NULL);
-DEFINE_USB_FUNCTION(USB_FUNCTION_SDB,         sdb,         "sdbd", NULL,          NULL);
-DEFINE_USB_FUNCTION(USB_FUNCTION_MTP,         mtp,         "mtp-responder", NULL, NULL);
-DEFINE_USB_FUNCTION(USB_FUNCTION_ACM,         acm,         NULL, "data-router",   NULL);
-DEFINE_USB_FUNCTION(USB_FUNCTION_RNDIS,       rndis,       NULL, "sshd",          rndis_handler);
+DEFINE_USB_FUNCTION(USB_FUNCTION_MTP,         mtp,         1, "mtp-responder", NULL);
+DEFINE_USB_FUNCTION(USB_FUNCTION_ACM,         acm,         0, "data-router",   NULL);
+DEFINE_USB_FUNCTION(USB_FUNCTION_SDB,         sdb,         1, "sdbd",          NULL);
+DEFINE_USB_FUNCTION(USB_FUNCTION_RNDIS,       rndis,       0, "sshd",          rndis_handler);
+DEFINE_USB_FUNCTION(USB_FUNCTION_DIAG,        diag,        0, NULL,            NULL);
+DEFINE_USB_FUNCTION(USB_FUNCTION_CONN_GADGET, conn_gadget, 0, NULL,            NULL);
+DEFINE_USB_FUNCTION(USB_FUNCTION_DM,          dm,          0, NULL,            NULL);
+DEFINE_USB_FUNCTION(USB_FUNCTION_RMNET,       rmnet,       0, NULL,            NULL);
+
 #undef DEFINE_USB_FUNCTION
 
 /* Caution: index order of arrary is important, because simple_id_to_gadget() uses it. */
@@ -486,7 +480,7 @@ int simple_translator_open(struct hw_info *info,
         * So in environments that mtp doesn't support, use dummy mtp.
         */
        if (access("/usr/lib/systemd/system/mtp-responder.socket", F_OK)) {
-               _available_funcs[USB_FUNCTION_IDX_MTP]->ffs_service = "mtp-responder-dummy";
+               _available_funcs[USB_FUNCTION_IDX_MTP]->service = "mtp-responder-dummy";
        }
 
        *common = &simple_translator->common;