usb-gadget: Handle more than one extcon reporting USB cable 65/287065/3
authorMarek Szyprowski <m.szyprowski@samsung.com>
Thu, 19 Jan 2023 07:30:10 +0000 (08:30 +0100)
committerMichal Bloch <m.bloch@partner.samsung.com>
Thu, 2 Feb 2023 17:06:07 +0000 (17:06 +0000)
Signed-off-by: Marek Szyprowski <m.szyprowski@samsung.com>
Change-Id: I0c26ac4ecb13f1cd6d0c464a105e1e333b411806
(cherry picked from commit 80bf51f40a15940a966acc363a7e48480b9f11fc)

src/usb-gadget/usb-gadget.c

index 1a0769c..bc551ea 100644 (file)
@@ -205,15 +205,26 @@ static int usb_state_changed(const char *index, int new_status)
 {
        int ret = -1;
        static int old_status = -1; /* -1: Uninitialized, 0: disconnected, 1: connected */
+       static char old_index[NAME_MAX];
 
        /* For debugging. Do not move the location. */
-       _I("USB state is changed by extcon from (%d) to (%d).", old_status, new_status);
+       _I("USB state is changed by extcon \"%s\" from (%d) to (%d).", index ? index : "(NULL)", old_status, new_status);
+
+       if (old_index[0] != '\0' && index && strncmp(old_index, index, sizeof(old_index)) != 0) {
+               _I("USB state change ignored on extcon \"%s\"\n", index);
+               return 0;
+       }
 
        if (old_status == new_status)
                return 0;
 
        switch (new_status) {
        case USB_CONNECTED:
+               old_index[0] = '\0';
+               if (index) {
+                       strncpy(old_index, index, sizeof(old_index) - 1);
+                       old_index[sizeof(old_index) - 1] = '\0';
+               }
                ret = usb_connected();
                break;
 
@@ -221,6 +232,7 @@ static int usb_state_changed(const char *index, int new_status)
                if (old_status == -1) {
                        /* only initialize the internal data state and skip usb hal operation. */
                        _I("USB is initialized without USB connection");
+                       old_index[0] = '\0';
 
                        /* From usb_disconnected() */
                        usb_state_set_connection(USB_DISCONNECTED);
@@ -231,8 +243,10 @@ static int usb_state_changed(const char *index, int new_status)
                        change_usb_state_notification_handler(USB_GADGET_FUNC_NONE);
 
                        ret = 0;
-               } else
+               } else {
+                       old_index[0] = '\0';
                        ret = usb_disconnected();
+               }
                break;
 
        default: