usb: support configfs usb hal 05/171905/8
authorINSUN PYO <insun.pyo@samsung.com>
Thu, 8 Mar 2018 05:51:59 +0000 (14:51 +0900)
committerINSUN PYO <insun.pyo@samsung.com>
Tue, 13 Mar 2018 08:49:51 +0000 (17:49 +0900)
Signed-off-by: INSUN PYO <insun.pyo@samsung.com>
Change-Id: Ic810941b85fa985d68117dcc979b3ce75dedc0a1

src/usb/usb.c

index 4caef6b..28c2777 100644 (file)
@@ -38,81 +38,109 @@ static struct usb_client *usb_client;
 
 struct extcon_ops extcon_usb_ops;
 
-static int usb_probe(void)
+static struct usb_gadget_translator *gadget_translator_probe(void)
 {
        struct hw_info *info;
        struct hw_common *common;
-       int ret;
+       struct usb_gadget_translator *gadget;
 
-       /* We need both HALs up and running for operating in USB client mode */
+       if (hw_get_info(USB_GADGET_DEVICE_ID, (const struct hw_info **)&info)) {
+               _E("No usb gadget translator");
+               return NULL;
+       }
 
-       if (!gadget_translator) {
-               ret = hw_get_info(USB_GADGET_DEVICE_ID,
-                                 (const struct hw_info **)&info);
-               if (ret)
-                       goto no_gadget_hal;
+       if (!info->open) {
+               _E("Failed to open usb gadget translator; open(NULL)");
+               return NULL;
+       }
 
-               if (!info->open) {
-                       _E("Failed to open gadget translator; open(NULL)");
-                       /* TODO. Shouldn't we free somehow info structure? */
-                       return -ENODEV;
-               }
+       if (info->open(info, NULL, &common) < 0) {
+               _E("Failed to open usb gadget translator");
+               return NULL;
+       }
 
-               ret = info->open(info, NULL, &common);
-               if (ret < 0) {
-                       _E("Failed to open usb gadget translator (%d)", ret);
-                       return ret;
-               }
+       gadget = container_of(common, struct usb_gadget_translator, common);
+       if (!gadget->id_to_gadget || !gadget->cleanup_gadget) {
+               _E("Invalid usb gadget translator");
 
-               gadget_translator = container_of(common,
-                                                struct usb_gadget_translator,
-                                                common);
-               if (!gadget_translator->id_to_gadget ||
-                   !gadget_translator->cleanup_gadget) {
-                       _E("Invalid gadget translator HAL");
-                       goto invalid_translator_hal;
-               }
+               if (gadget->common.info)
+                       gadget->common.info->close(&gadget->common);
+
+               return NULL;
        }
 
-       if (!usb_client) {
-               ret = hw_get_info(USB_CLIENT_HARDWARE_DEVICE_ID,
-                                 (const struct hw_info **)&info);
-               if (ret)
-                       goto no_client_hal;
-
-               if (!info->open) {
-                       _E("Failed to open usb client hw; open(NULL)");
-                       /* TODO. Shouldn't we free somehow info structure? */
-                       return -ENODEV;
-               }
+       return gadget;
+}
 
-               ret = info->open(info, NULL, &common);
-               if (ret < 0) {
-                       _E("Failed to open usb client hw (%d)", ret);
-                       return ret;
-               }
+static struct usb_client *usb_client_probe(const char *id)
+{
+       struct hw_info *info;
+       struct hw_common *common;
+       struct usb_client *client;
 
-               usb_client = container_of(common, struct usb_client, common);
+       _I("Loading usb client: %s", id);
 
-               if (!usb_client->reconfigure_gadget || !usb_client->enable ||
-                   !usb_client->disable)
-                       goto invalid_config_hal;
+       if (hw_get_info(id, (const struct hw_info **)&info)) {
+               _I("No usb_client: %s", id);
+               return NULL;
        }
 
-       return 0;
+       if (!info->open) {
+               _E("Failed to open usb client open(NULL): %s", id);
+               return NULL;
+       }
 
-invalid_config_hal:
-       if (usb_client->common.info->close)
-               usb_client->common.info->close(&usb_client->common);
+       if (info->open(info, NULL, &common) < 0) {
+               _E("Failed to open usb client: %s", id);
+               return NULL;
+       }
 
-no_client_hal:
-invalid_translator_hal:
-       if (gadget_translator->common.info)
-               gadget_translator->common.info->close(&gadget_translator->common);
-       gadget_translator = NULL;
-no_gadget_hal:
-       /* TODO. Maybe some default action here? */
-       return -ENOENT;
+       client = container_of(common, struct usb_client, common);
+       if (!client->reconfigure_gadget || !client->enable || !client->disable) {
+               _E("Invalid usb client: %s", id);
+
+               if (client->common.info->close)
+                       client->common.info->close(&client->common);
+
+               return NULL;
+       }
+
+       _I("Success lodging usb client: %s", id);
+       return client;
+}
+
+static int usb_probe(void)
+{
+       /* Both the gadget and client should be valid. */
+       if (gadget_translator || usb_client) {
+               if (gadget_translator && usb_client)
+                       return 0;
+               return -EINVAL;
+       }
+
+       /* gadget_translator */
+       gadget_translator = gadget_translator_probe();
+       if (!gadget_translator)
+               return -ENODEV;
+
+       /* usb_client */
+       usb_client = usb_client_probe(USB_CLIENT_HARDWARE_DEVICE_ID);
+
+       if (!usb_client)
+               usb_client = usb_client_probe(USB_SLP_CLIENT_HARDWARE_DEVICE_ID);
+
+       if (!usb_client)
+               usb_client = usb_client_probe(USB_CFS_CLIENT_HARDWARE_DEVICE_ID);
+
+       if (!usb_client) {
+               if (gadget_translator->common.info)
+                       gadget_translator->common.info->close(&gadget_translator->common);
+               gadget_translator = NULL;
+
+               return -ENODEV;
+       }
+
+       return 0;
 }
 
 static void usb_release()