Rearrange display lock and unlock in the usb module 09/226609/4
authorINSUN PYO <insun.pyo@samsung.com>
Wed, 4 Mar 2020 06:17:33 +0000 (15:17 +0900)
committerINSUN PYO <insun.pyo@samsung.com>
Wed, 4 Mar 2020 06:51:13 +0000 (15:51 +0900)
If an error occurs during usb enable, do not acquire display lock.
Target may enter sleep mode with the USB cable is connected

To prevent this situation, acquire the lock when usb cable is connected,
and release the lock when usb cable is disconnected.

Change-Id: I9850e593957e182a415298eeb63361d09c21dc3a

src/usb/usb.c

index b926a74..581c0ae 100644 (file)
@@ -228,6 +228,7 @@ no_hal:
        return -ENODEV;
 }
 
+/* Precondition: USB_FUNCTION_NONE */
 static int usb_change_gadget(unsigned mode)
 {
        int ret;
@@ -261,7 +262,7 @@ static int usb_change_gadget(unsigned mode)
        return ret;
 
 out:
-       /* TODO. Maybe some default action here? */
+       /* Although this function has a USB_FUNCTION_NONE precondition, it is to protect against coding mistakes. */
        (void)usb_state_set_current_mode(USB_FUNCTION_NONE); /* because usb does not work properly */
        return ret;
 }
@@ -281,12 +282,10 @@ static int usb_enable(unsigned int mode)
        (void)usb_state_set_current_mode(mode);
        change_usb_state_notification_handler(mode);
 
-       if (disp_plgn.pm_lock_internal)
-               disp_plgn.pm_lock_internal(INTERNAL_LOCK_USB, LCD_OFF, STAY_CUR_STATE, 0);
-
        return 0;
 
 out:
+       /* Although this function has a USB_FUNCTION_NONE precondition, it is to protect against coding mistakes. */
        (void)usb_state_set_current_mode(USB_FUNCTION_NONE); /* because usb does not work properly */
        return ret;
 }
@@ -301,19 +300,16 @@ static int usb_disable(void)
        else
                ; /* Do nothing: A send_usb_state_changed_event(VCONFKEY_SYSMAN_USB_DISCONNECTED) has already called by caller */
 
-       (void)usb_state_set_current_mode(USB_FUNCTION_NONE); /* run unconditionally */
+       (void)usb_state_set_current_mode(USB_FUNCTION_NONE);
        change_usb_state_notification_handler(USB_FUNCTION_NONE);
 
        ret = usb_config_disable();
-       if (ret != 0) {
+       if (ret < 0) {
                _E("Failed to disable usb config: %d", ret);
-               /* Important: You have to keep going to unlock disp_plgn.pm_unlock_internal */
+               return ret;
        }
 
-       if (disp_plgn.pm_unlock_internal)
-               disp_plgn.pm_unlock_internal(INTERNAL_LOCK_USB, LCD_OFF, STAY_CUR_STATE);
-
-       return ret;
+       return 0;
 }
 
 /* Precondition: USB_DISCONNECTED (Implicitly contains USB_FUNCTION_NONE) */
@@ -322,12 +318,15 @@ static int usb_connected(void)
        int ret;
        unsigned int mode = usb_state_get_selected_mode();
 
+       if (disp_plgn.pm_lock_internal)
+               disp_plgn.pm_lock_internal(INTERNAL_LOCK_USB, LCD_OFF, STAY_CUR_STATE, 0);
+
        usb_state_set_connection(USB_CONNECTED);
        send_usb_state_changed_event(VCONFKEY_SYSMAN_USB_CONNECTED);
 
        ret = usb_enable(mode);
        if (ret < 0) {
-               _E("Failed to enable usb gadget(%d): %d", mode, ret);
+               _E("Failed to enable usb gadget(0x%x): %d", mode, ret);
                return ret;
        }
 
@@ -337,10 +336,21 @@ static int usb_connected(void)
 /* Precondition: USB_CONNECTED */
 static int usb_disconnected(void)
 {
+       int ret;
+
        usb_state_set_connection(USB_DISCONNECTED);
        send_usb_state_changed_event(VCONFKEY_SYSMAN_USB_DISCONNECTED);
 
-       return usb_disable();
+       ret = usb_disable();
+       if(ret < 0) {
+               _E("Failed to disable usb gadget: %d", ret);
+               /* Important: You have to keep going to unlock disp_plgn.pm_unlock_internal */
+       }
+
+       if (disp_plgn.pm_unlock_internal)
+               disp_plgn.pm_unlock_internal(INTERNAL_LOCK_USB, LCD_OFF, STAY_CUR_STATE);
+
+       return ret;
 }
 
 /* Called by dbus signal and vconf change(tethering, debug mode) */
@@ -366,8 +376,8 @@ int usb_change_mode(unsigned int new_mode)
 
        /*
         * You should always change the gadget once when the usb mode is changed.
-        * In this state, usb_enable() is called when the usb cable is connected.
-        * A usb_enable() is called when the usb cable is disconnected.
+        * In this state, usb_enable() is called when the usb cable is connected
+        * and usb_disable() is called when the usb cable is disconnected.
         */
        ret = usb_change_gadget(new_mode);
        if (ret < 0) {