Refactoring usb function variable in usb gadget
[platform/core/system/libdevice-node.git] / hw / usb_client_common.c
index 709d7c0..e8f8473 100644 (file)
 
 static bool legacy_is_function_supported(struct usb_function *func)
 {
-       /*
-        * TODO
-        * Instead of only checking whether we know this function
-        * we should also parse sysfs to check if it is build into
-        * slp-gadget.
-        */
-       if (find_usb_function_by_name(func->name))
-               return true;
-       else
+       char buf[PATH_MAX];
+
+       snprintf (buf, sizeof(buf), "%s/f_%s", LEGACY_ROOTPATH, func->name);
+       if (access(buf, F_OK))
                return false;
+
+       return true;
 }
 
 static bool legacy_is_gadget_supported(struct usb_gadget *gadget)
 {
        int i, j;
+       struct usb_configuration *config;
 
-       if (!gadget || !gadget->configs || !gadget->funcs)
+       if (!gadget || !gadget->configs || !gadget->configs[0] || !gadget->configs[0]->funcs[0])
                return false;
 
+       if (!gadget->attrs.idVendor || !gadget->attrs.idProduct || !gadget->attrs.bcdDevice)
+                       return false;
+
        /* only strings in US_en are allowed */
        if (gadget->strs.lang_code != DEFAULT_LANG)
                        return false;
@@ -88,7 +89,7 @@ static bool legacy_is_gadget_supported(struct usb_gadget *gadget)
                return false;
 
        for (j = 0; gadget->configs[j]; ++j) {
-               struct usb_configuration *config = gadget->configs[j];
+               config = gadget->configs[j];
 
                if (!config->funcs)
                        return false;
@@ -98,17 +99,14 @@ static bool legacy_is_gadget_supported(struct usb_gadget *gadget)
                                return false;
        }
 
-       if (j == 0 || j > 2)
-               return false;
-
        return true;
 }
 
 /* TODO. Maybe move this to sys ? */
 static int legacy_set_int_hex(char *path, int val)
 {
-       char buf[MAX_GADGET_STR_LEN];
        int r;
+       char buf[MAX_GADGET_STR_LEN];
 
        if (!path)
                return -EINVAL;
@@ -211,6 +209,7 @@ static int legacy_reconfigure_gadget(struct usb_client *usb, struct usb_gadget *
        if (!usb || !gadget)
                return -EINVAL;
 
+       /* Verify the gadget and check if function is supported */
        if (!legacy_is_gadget_supported(gadget))
                return -ENOTSUP;