From c9dee14badcb31d4a238798d48a17e9b260e6a3c Mon Sep 17 00:00:00 2001 From: Marek Szyprowski Date: Thu, 19 Jan 2023 08:30:10 +0100 Subject: [PATCH] usb-gadget: Handle more than one extcon reporting USB cable Signed-off-by: Marek Szyprowski Change-Id: I0c26ac4ecb13f1cd6d0c464a105e1e333b411806 --- src/usb-gadget/usb-gadget.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/usb-gadget/usb-gadget.c b/src/usb-gadget/usb-gadget.c index 1a0769c..bc551ea 100644 --- a/src/usb-gadget/usb-gadget.c +++ b/src/usb-gadget/usb-gadget.c @@ -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: -- 2.7.4