Refactoring usb gadget for functionfs 01/223101/5 submit/tizen/20200128.022129
authorINSUN PYO <insun.pyo@samsung.com>
Thu, 23 Jan 2020 01:26:06 +0000 (10:26 +0900)
committerINSUN PYO <insun.pyo@samsung.com>
Tue, 28 Jan 2020 01:42:32 +0000 (10:42 +0900)
Change-Id: Ieb87a66543095a44773bdfba105c37b19b3b2cfa

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

index 8f8c69f..03a61d1 100644 (file)
@@ -496,18 +496,12 @@ static bool cfs_is_function_supported(struct usb_client *usb,
        bool res;
        int ret;
 
-       switch (func->function_group) {
-       case USB_FUNCTION_GROUP_SIMPLE:
-       case USB_FUNCTION_GROUP_WITH_POST_SERVICE:
+       if (!func->ffs_service) {
                ret = usbg_lookup_function_type(func->name);
                res = ret >= 0;
-               break;
-       case USB_FUNCTION_GROUP_WITH_SERVICE:
+       } else {
                /* TODO: Check if socket is available */
                res = true;
-               break;
-       default:
-               res = false;
        }
 
        return res;
@@ -704,28 +698,21 @@ static int cfs_set_gadget_config(struct cfs_client *cfs_client,
                int type;
                usbg_function *func;
 
-               switch (usb_func->function_group) {
-               case USB_FUNCTION_GROUP_SIMPLE:
-               case USB_FUNCTION_GROUP_WITH_POST_SERVICE:
+               if (!usb_func->ffs_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:
+               } else {
                        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;
                }
 
-
                func = usbg_get_function(cfs_client->gadget, type, instance);
                if (!func) {
                        ret = usbg_create_function(cfs_client->gadget,
@@ -735,18 +722,14 @@ static int cfs_set_gadget_config(struct cfs_client *cfs_client,
                        if (ret)
                                return ret;
 
-                       if (usb_func->function_group ==
-                           USB_FUNCTION_GROUP_WITH_SERVICE) {
-                               if (usb_func->service) {
-                                       ret = cfs_prep_ffs_service(usb_func->name,
-                                                               usb_func->instance,
-                                                               instance,
-                                                               usb_func->service);
-                                       if (ret)
-                                               return ret;
-                               }
+                       if (usb_func->ffs_service) {
+                               ret = cfs_prep_ffs_service(usb_func->name,
+                                                       usb_func->instance,
+                                                       instance,
+                                                       usb_func->ffs_service);
+                               if (ret)
+                                       return ret;
                        }
-
                }
 
                ret = usbg_add_config_function(config, NULL, func);
@@ -835,6 +818,7 @@ static int cfs_enable(struct usb_client *usb)
        int i;
        int ret;
        struct usb_gadget *gadget;
+       struct usb_function *func;
        struct cfs_client *cfs_client;
 
        if (!usb)
@@ -852,11 +836,10 @@ static int cfs_enable(struct usb_client *usb)
        }
 
        for (i = 0; gadget->funcs[i]; ++i) {
-               if (gadget->funcs[i]->function_group != USB_FUNCTION_GROUP_WITH_POST_SERVICE)
-                       continue;
+               func = gadget->funcs[i];
 
-               if (gadget->funcs[i]->service)
-                       (void)systemd_start_unit_wait_started(gadget->funcs[i]->service, ".service", -1);
+               if (func->service)
+                       (void)systemd_start_unit_wait_started(func->service, ".service", -1);
        }
 
        cfs_free_gadget(gadget);
@@ -869,6 +852,7 @@ static int cfs_disable(struct usb_client *usb)
        int i;
        int ret;
        struct usb_gadget *gadget;
+       struct usb_function *func;
        struct cfs_client *cfs_client;
 
        if (!usb)
@@ -879,11 +863,10 @@ static int cfs_disable(struct usb_client *usb)
                return ret;
 
        for (i = 0; gadget->funcs[i]; ++i) {
-               if (gadget->funcs[i]->function_group != USB_FUNCTION_GROUP_WITH_POST_SERVICE)
-                       continue;
+               func = gadget->funcs[i];
 
-               if (gadget->funcs[i]->service)
-                       (void)systemd_stop_unit_wait_stopped(gadget->funcs[i]->service, ".service", -1);
+               if (func->service)
+                       (void)systemd_stop_unit_wait_stopped(func->service, ".service", -1);
        }
 
        cfs_free_gadget(gadget);
@@ -967,11 +950,9 @@ int hw_cfs_gadget_close(struct hw_common *common)
                        continue;
 
                usb_func = _available_funcs[ret];
-               if (usb_func->function_group == USB_FUNCTION_GROUP_WITH_SERVICE) {
-                       if (usb_func->service) {
-                               systemd_stop_unit_wait_stopped(usb_func->service, ".socket", -1);
-                               systemd_stop_unit_wait_stopped(usb_func->service, ".service", -1);
-                       }
+               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);
                }
        }
 
index 5bd2651..2726049 100644 (file)
@@ -543,6 +543,7 @@ static int legacy_enable(struct usb_client *usb)
        int ret;
        int i;
        struct usb_gadget *gadget;
+       struct usb_function *func;
 
        ret = sys_set_str(LEGACY_ENABLE_PATH,
                           LEGACY_ENABLE);
@@ -554,8 +555,18 @@ static int legacy_enable(struct usb_client *usb)
                goto disable_gadget;
 
        for (i = 0; gadget->funcs[i]; ++i) {
-               if (gadget->funcs[i]->service)
-                       (void)systemd_start_unit_wait_started(gadget->funcs[i]->service, ".service", -1);
+               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->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);
        }
 
        legacy_free_gadget(gadget);
@@ -571,14 +582,25 @@ static int legacy_disable(struct usb_client *usb)
        int ret;
        int i;
        struct usb_gadget *gadget;
+       struct usb_function *func;
 
        ret = legacy_get_current_gadget(usb, &gadget);
        if (ret < 0)
                return ret;
 
        for (i = 0; gadget->funcs[i]; ++i) {
-               if (gadget->funcs[i]->service)
-                       (void)systemd_stop_unit_wait_stopped(gadget->funcs[i]->service, ".service", -1);
+               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);
        }
 
        ret = sys_set_str(LEGACY_ENABLE_PATH, LEGACY_DISABLE);
index 44ff468..629504a 100644 (file)
 /* Function IDX in array is number of trailing zeros */
 #define FUNC_IDX_FROM_MASK(mask) _HELPER_CTZ(mask)
 
-/**
- * USB_FUNCTION_GROUP_WITH_POST_SERVICE is the same as USB_FUNCTION_GROUP_SIMPLE
- * except running the service as a post.
- */
-typedef enum {
-       USB_FUNCTION_GROUP_SIMPLE,
-       USB_FUNCTION_GROUP_WITH_SERVICE,
-       USB_FUNCTION_GROUP_WITH_POST_SERVICE,
-} usb_function_group_e;
-
 struct usb_function {
-       int function_group;
        int id;
        const char *name;
        const char *instance;
 
+       const char *ffs_service; /* only used in configfs */
        const char *service;
 
        int (*clone)(struct usb_function *func, struct usb_function **_clone);
@@ -166,39 +156,28 @@ out_nomem:
        return -ENOMEM;
 }
 
-#define DEFINE_USB_FUNCTION(_group, _id, _name, _service)   \
-       static struct usb_function _##_name##_function = {  \
-               .function_group = _group,                   \
-               .id = _id,                                  \
-               .name = #_name,                             \
-               .instance = "default",                      \
-               .service = _service,                        \
-               .clone = clone_simple_func,                 \
-               .free_func = free_simple_func,              \
+#define DEFINE_USB_FUNCTION(_id, _name, _ffs_service, _service)  \
+       static struct usb_function _##_name##_function = {       \
+               .id = _id,                                       \
+               .name = #_name,                                  \
+               .instance = "default",                           \
+               .ffs_service = _ffs_service,                     \
+               .service = _service,                             \
+               .clone = clone_simple_func,                      \
+               .free_func = free_simple_func,                   \
        }
 
-#define DEFINE_USB_FUNCTION_NO_SERVICE(_id, _name)                                      \
-       DEFINE_USB_FUNCTION(USB_FUNCTION_GROUP_SIMPLE, _id, _name, NULL)
-
-#define DEFINE_USB_FUNCTION_WITH_SERVICE(_id, _name, _service)                          \
-       DEFINE_USB_FUNCTION(USB_FUNCTION_GROUP_WITH_SERVICE, _id, _name, _service)
-
-#define DEFINE_USB_FUNCTION_WITH_POST_SERVICE(_id, _name, _service)                     \
-       DEFINE_USB_FUNCTION(USB_FUNCTION_GROUP_WITH_POST_SERVICE, _id, _name, _service)
-
-DEFINE_USB_FUNCTION_NO_SERVICE(USB_FUNCTION_DIAG, diag);
-DEFINE_USB_FUNCTION_NO_SERVICE(USB_FUNCTION_RMNET, rmnet);
-DEFINE_USB_FUNCTION_NO_SERVICE(USB_FUNCTION_DM, dm);
-DEFINE_USB_FUNCTION_NO_SERVICE(USB_FUNCTION_CONN_GADGET, conn_gadget);
-
-DEFINE_USB_FUNCTION_WITH_SERVICE(USB_FUNCTION_SDB, sdb, "sdbd");
-DEFINE_USB_FUNCTION_WITH_SERVICE(USB_FUNCTION_MTP, mtp, "mtp-responder");
-
-DEFINE_USB_FUNCTION_WITH_POST_SERVICE(USB_FUNCTION_ACM, acm, "data-router");
-DEFINE_USB_FUNCTION_WITH_POST_SERVICE(USB_FUNCTION_RNDIS, rndis, "rndis");
-
+DEFINE_USB_FUNCTION(USB_FUNCTION_DIAG,        diag,        NULL, NULL);
+DEFINE_USB_FUNCTION(USB_FUNCTION_RMNET,       rmnet,       NULL, NULL);
+DEFINE_USB_FUNCTION(USB_FUNCTION_DM,          dm,          NULL, NULL);
+DEFINE_USB_FUNCTION(USB_FUNCTION_CONN_GADGET, conn_gadget, NULL, NULL);
+DEFINE_USB_FUNCTION(USB_FUNCTION_SDB,         sdb,         "sdbd", NULL);
+DEFINE_USB_FUNCTION(USB_FUNCTION_MTP,         mtp,         "mtp-responder", NULL);
+DEFINE_USB_FUNCTION(USB_FUNCTION_ACM,         acm,         NULL, "data-router");
+DEFINE_USB_FUNCTION(USB_FUNCTION_RNDIS,       rndis,       NULL, "rndis");
+#undef DEFINE_USB_FUNCTION
 
-#define MAKE_FUNC_AVAILABLE(_name, _vname)                             \
+#define MAKE_FUNC_AVAILABLE(_name, _vname)  \
        [FUNC_IDX_FROM_MASK(USB_FUNCTION_##_name)] = &_##_vname##_function
 
 static struct usb_function *_available_funcs[] = {
@@ -211,11 +190,6 @@ static struct usb_function *_available_funcs[] = {
        MAKE_FUNC_AVAILABLE(DM, dm),
        MAKE_FUNC_AVAILABLE(RMNET, rmnet),
 };
-
-#undef DEFINE_USB_FUNCTION
-#undef DEFINE_USB_FUNCTION_NO_SERVICE
-#undef DEFINE_USB_FUNCTION_WITH_SERVICE
-#undef DEFIND_USB_FUNCTION_WITH_POST_SERVICE
 #undef MAKE_FUNC_AVAILABLE
 
 struct usb_gadget_id {