Refactoring usb gadget 90/228590/7
authorINSUN PYO <insun.pyo@samsung.com>
Tue, 24 Mar 2020 05:25:42 +0000 (14:25 +0900)
committerINSUN PYO <insun.pyo@samsung.com>
Thu, 26 Mar 2020 02:24:56 +0000 (02:24 +0000)
Move common USB gadget attribute value to header

Change-Id: Id06fefc9af78876d46364b39893b4f25ebd9ed82

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

index f03395c..3c4740d 100644 (file)
@@ -26,6 +26,7 @@
 #include <usbg/function/net.h>
 #include <libsyscommon/dbus-systemd.h>
 
+#include <hw/usb_gadget.h>
 #include <hw/usb_client.h>
 
 #define CONFIGFS_PATH "/sys/kernel/config"
@@ -57,16 +58,15 @@ struct cfs_client {
 
 /* Based on values in slp-gadget kernel module */
 struct usbg_gadget_attrs default_g_attrs = {
-       .bcdUSB = 0x0200,
-       .idVendor = 0x04e8,
-       .idProduct = 0x6860,
-       .bcdDevice = 0x0100,
+       .idVendor = DEFAULT_VID,
+       .idProduct = DEFAULT_PID,
+       .bcdDevice = DEFAULT_BCD_DEVICE,
 };
 
 struct usbg_gadget_strs default_g_strs = {
-       .manufacturer = "Samsung",
-       .product = "TIZEN",
-       .serial = "01234TEST",
+       .manufacturer = DEFAULT_MANUFACTURER,
+       .product = DEFAULT_PRODUCT,
+       .serial = DEFAULT_SERIAL,
 };
 
 static struct usb_function *cfs_find_usb_function(usbg_function *function)
@@ -162,12 +162,8 @@ static int cfs_set_gadget_attrs(struct cfs_client *cfs_client,
 static int cfs_set_gadget_strs(struct cfs_client *cfs_client,
                                  struct usb_gadget_strings *strs)
 {
-       int ret = 0;
+       int ret;
 
-       /*
-        * TODO
-        * Here is a good place to ensure that serial is immutable
-        */
 #define SET_STR(FIELD, STR_ID)                         \
        if (strs->FIELD) {                              \
                ret = usbg_set_gadget_str(cfs_client->gadget,   \
@@ -182,7 +178,8 @@ static int cfs_set_gadget_strs(struct cfs_client *cfs_client,
        SET_STR(product, USBG_STR_PRODUCT);
        SET_STR(serial, USBG_STR_SERIAL_NUMBER);
 #undef SET_STR
-       return ret;
+
+       return 0;
 }
 
 static int cfs_ensure_dir(char *path)
@@ -449,8 +446,7 @@ static int cfs_set_gadget_config(struct cfs_client *cfs_client, int config_id, s
        return 0;
 }
 
-static int cfs_reconfigure_gadget(struct usb_client *usb,
-                                 struct usb_gadget *gadget)
+static int cfs_reconfigure_gadget(struct usb_client *usb, struct usb_gadget *gadget)
 {
        int i;
        int ret;
index 963687a..b44ec0c 100644 (file)
@@ -24,6 +24,7 @@
 #include <libsyscommon/dbus-systemd.h>
 
 #include <hw/shared.h>
+#include <hw/usb_gadget.h>
 #include <hw/usb_client.h>
 
 #define MAX_GADGET_STR_LEN 256
@@ -81,14 +82,9 @@ static bool legacy_is_gadget_supported(struct usb_client *usb,
        if (!gadget || !gadget->configs || !gadget->funcs)
                return false;
 
-       /*
-        * TODO
-        * Here is a good place to ensure that serial is immutable
-        */
        if (gadget->strs) {
                /* only strings in US_en are allowed */
-               if (gadget->strs[0].lang_code != 0x409 ||
-                   gadget->strs[1].lang_code)
+               if (gadget->strs[0].lang_code != DEFAULT_LANG || gadget->strs[1].lang_code)
                        return false;
        }
 
@@ -162,10 +158,7 @@ static int legacy_set_gadget_strs(struct usb_gadget_strings *strs)
 {
        int ret = 0;
 
-       /*
-        * TODO
-        * Here is a good place to ensure that serial is immutable
-        */
+       /* SLP gadget's kernel driver writes serial number directly */
 
        if (strs->manufacturer) {
                ret = sys_set_str(LEGACY_IMANUFACTURER_PATH, strs->manufacturer);
index fd0e2fd..1066048 100644 (file)
 
 #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,
index 179bb4c..cfc138f 100644 (file)
 #include <hw/board.h>
 #include <hw/usb_gadget.h>
 
-#define zalloc(amount) calloc(1, amount)
-
-/* Based on slp-gadget and initial version of USB HAL by Taeyoung Kim */
-#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
-
 #ifndef EXPORT
 #define EXPORT __attribute__ ((visibility("default")))
 #endif
@@ -100,7 +85,7 @@ static int alloc_default_config(struct usb_configuration **_config)
 {
        struct usb_configuration *config;
 
-       config = zalloc(sizeof(*config));
+       config = calloc(1, sizeof(*config));
        if (!config)
                goto out;
 
@@ -144,9 +129,8 @@ static int alloc_default_gadget(struct usb_gadget **_gadget)
        struct usb_gadget *gadget;
        struct usb_gadget_strings *strs;
        struct usb_configuration **configs;
-       int ret;
 
-       gadget = zalloc(sizeof(*gadget));
+       gadget = calloc(1, sizeof(*gadget));
        if (!gadget)
                goto out;
 
@@ -158,11 +142,10 @@ static int alloc_default_gadget(struct usb_gadget **_gadget)
        if (!strs)
                goto free_gadget;
 
-       strs[0].lang_code = 0x409;
+       strs[0].lang_code = DEFAULT_LANG;
        strs[0].manufacturer = strdup(DEFAULT_MANUFACTURER);
        strs[0].product = strdup(DEFAULT_PRODUCT);
-       ret = get_device_serial(&strs[0].serial);
-       if (ret < 0)
+       if (get_device_serial(&strs[0].serial) < 0)
                strs[0].serial = strdup(DEFAULT_SERIAL);
 
        if (!strs[0].manufacturer || !strs[0].product || !strs[0].serial)
@@ -437,7 +420,7 @@ int simple_translator_open(struct hw_info *info,
        if (!info || !common)
                return -EINVAL;
 
-       simple_translator = zalloc(sizeof(*simple_translator));
+       simple_translator = calloc(1, sizeof(*simple_translator));
        if (!simple_translator)
                return -ENOMEM;