Refactoring usb function variable in usb gadget
[platform/core/system/libdevice-node.git] / hw / usb_gadget.h
index a3e5d58..c0ac3dc 100644 (file)
 #ifndef __HW_USB_GADGET_H__
 #define __HW_USB_GADGET_H__
 
-#include <hw/common.h>
+#include <stdint.h>
 
-#include <stddef.h>
-#include <stdlib.h>
-#include <string.h>
-#include <errno.h>
+#include <hw/common.h>
 
-/**
- * The id of this device
- */
+/* The id of this device */
 #define USB_GADGET_DEVICE_ID   "usb_gadget"
 
-/**
- * The version of this device
- */
+/*The version of this device */
 #define USB_GADGET_DEVICE_VERSION      MAKE_VERSION(0,1)
 
+/*The default USB configuration */
+#define DEFAULT_VID 0x04e8
+#define DEFAULT_PID 0x6860
+#define DEFAULT_BCD_DEVICE 0x0100
+
+#define DEFAULT_LANG 0x409 /* US_en */
+#define DEFAULT_MANUFACTURER "Samsung"
+#define DEFAULT_PRODUCT "TIZEN"
+#define DEFAULT_SERIAL "01234TEST"
+
+#define DEFAULT_BMATTRIBUTES ((1 << 7) | (1 << 6))
+#define DEFAULT_MAX_POWER 500
+
+typedef enum {
+       USB_FUNCTION_IDX_MTP         = 0,
+       USB_FUNCTION_IDX_ACM         = 1,
+       USB_FUNCTION_IDX_SDB         = 2,
+       USB_FUNCTION_IDX_RNDIS       = 3,
+       USB_FUNCTION_IDX_DIAG        = 4,
+       USB_FUNCTION_IDX_CONN_GADGET = 5,
+       USB_FUNCTION_IDX_DM          = 6,
+       USB_FUNCTION_IDX_RMNET       = 7,
+       USB_FUNCTION_IDX_MAX         = USB_FUNCTION_IDX_RMNET + 1
+} usb_function_idx_e;
+
+typedef enum {
+       USB_FUNCTION_NONE        = 0,
+       USB_FUNCTION_MTP         = 1 << USB_FUNCTION_IDX_MTP,
+       USB_FUNCTION_ACM         = 1 << USB_FUNCTION_IDX_ACM,
+       USB_FUNCTION_SDB         = 1 << USB_FUNCTION_IDX_SDB,
+       USB_FUNCTION_RNDIS       = 1 << USB_FUNCTION_IDX_RNDIS,
+       USB_FUNCTION_DIAG        = 1 << USB_FUNCTION_IDX_DIAG,
+       USB_FUNCTION_CONN_GADGET = 1 << USB_FUNCTION_IDX_CONN_GADGET,
+       USB_FUNCTION_DM          = 1 << USB_FUNCTION_IDX_DM,
+       USB_FUNCTION_RMNET       = 1 << USB_FUNCTION_IDX_RMNET
+} usb_function_e;
+
+
 /*
- * 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 start
- * configfs enable(usb plug)    : enable gadget -> handler(1) -> service start
- * configfs disable(usb unplug) : service stop -> handler(0) -> disable gadget
- * configfs deinit              : ffs_service 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 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);
-
-       int (*clone)(struct usb_function *func, struct usb_function **_clone);
-       void (*free_func)(struct usb_function *func);
 };
 
 struct usb_configuration_attributes {
@@ -71,7 +103,7 @@ struct usb_configuration_strings {
 
 struct usb_configuration {
        struct usb_configuration_attributes attrs;
-       struct usb_configuration_strings *strs;
+       struct usb_configuration_strings strs;
        struct usb_function **funcs;
 };
 
@@ -93,122 +125,10 @@ struct usb_gadget_strings {
 
 struct usb_gadget {
        struct usb_gadget_attrs attrs;
-       struct usb_gadget_strings *strs;
-       struct usb_function **funcs;
+       struct usb_gadget_strings strs;
        struct usb_configuration **configs;
 };
 
-typedef enum {
-       USB_FUNCTION_IDX_MTP         = 0,
-       USB_FUNCTION_IDX_ACM         = 1,
-       USB_FUNCTION_IDX_SDB         = 2,
-       USB_FUNCTION_IDX_RNDIS       = 3,
-       USB_FUNCTION_IDX_DIAG        = 4,
-       USB_FUNCTION_IDX_CONN_GADGET = 5,
-       USB_FUNCTION_IDX_DM          = 6,
-       USB_FUNCTION_IDX_RMNET       = 7,
-       USB_FUNCTION_IDX_MAX         = USB_FUNCTION_IDX_RMNET + 1
-} usb_function_idx_e;
-
-typedef enum {
-       USB_FUNCTION_NONE        = 0,
-       USB_FUNCTION_MTP         = 1 << USB_FUNCTION_IDX_MTP,
-       USB_FUNCTION_ACM         = 1 << USB_FUNCTION_IDX_ACM,
-       USB_FUNCTION_SDB         = 1 << USB_FUNCTION_IDX_SDB,
-       USB_FUNCTION_RNDIS       = 1 << USB_FUNCTION_IDX_RNDIS,
-       USB_FUNCTION_DIAG        = 1 << USB_FUNCTION_IDX_DIAG,
-       USB_FUNCTION_CONN_GADGET = 1 << USB_FUNCTION_IDX_CONN_GADGET,
-       USB_FUNCTION_DM          = 1 << USB_FUNCTION_IDX_DM,
-       USB_FUNCTION_RMNET       = 1 << USB_FUNCTION_IDX_RMNET
-} usb_function_e;
-
-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);
-       }
-}
-
-static int clone_simple_func(struct usb_function *func,
-                             struct usb_function **clone)
-{
-       struct usb_function *other;
-
-       if (!func || !clone)
-               return -EINVAL;
-
-       other = (struct usb_function *)calloc(1, sizeof(struct usb_function));
-       if (!other)
-               return -ENOMEM;
-
-       *other = *func;
-
-       other->name = strdup(func->name);
-       other->instance = strdup(func->instance);
-       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)
-                       goto out_nomem;
-       }
-
-       *clone = other;
-       return 0;
-
-out_nomem:
-       free_simple_func(other);
-       return -ENOMEM;
-}
-
-void rndis_handler(int enable);
-
-#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_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);
-#undef DEFINE_USB_FUNCTION
-
-/* Caution: index order of arrary is important, because simple_id_to_gadget() uses it. */
-static struct usb_function *_available_funcs[] = {
-       [USB_FUNCTION_IDX_MTP]         = &_mtp_function,
-       [USB_FUNCTION_IDX_ACM]         = &_acm_function,
-       [USB_FUNCTION_IDX_SDB]         = &_sdb_function,
-       [USB_FUNCTION_IDX_RNDIS]       = &_rndis_function,
-       [USB_FUNCTION_IDX_DIAG]        = &_diag_function,
-       [USB_FUNCTION_IDX_CONN_GADGET] = &_conn_gadget_function,
-       [USB_FUNCTION_IDX_DM]          = &_dm_function,
-       [USB_FUNCTION_IDX_RMNET]       = &_rmnet_function,
-       [USB_FUNCTION_IDX_MAX]         = NULL /* An indicator to end the array */
-};
-
 struct usb_gadget_id {
        unsigned int function_mask;
 };
@@ -216,14 +136,15 @@ struct usb_gadget_id {
 struct usb_gadget_translator {
        struct hw_common common;
 
-       int (*id_to_gadget)(struct usb_gadget_id *gadget_id,
-                        struct usb_gadget **gadget);
-
+       int (*id_to_gadget)(struct usb_gadget_id *gadget_id,  struct usb_gadget **gadget);
        void (*cleanup_gadget)(struct usb_gadget *gadget);
 };
 
-int simple_translator_open(struct hw_info *info,
-               const char *id, struct hw_common **common);
+int simple_translator_open(struct hw_info *info, const char *id, struct hw_common **common);
 int simple_translator_close(struct hw_common *common);
 
+struct usb_function *find_usb_function_by_id(int id);
+struct usb_function *find_usb_function_by_name(const char *name);
+struct usb_function *find_usb_function_by_name_instance(const char *name, const char *instance);
+
 #endif