From b159d66fb3880855986c574a1718adb26b4267aa Mon Sep 17 00:00:00 2001 From: Xiaoguang Wu Date: Tue, 20 Nov 2018 16:19:10 +0800 Subject: [PATCH] DM USB: refine the polling thread for libusb events The thread function usb_dev_sys_thread is the source of libusb events, which is also the basis of USB/xHCI emulation. During S3 process, function libusb_handle_events_timeout may fail and return -1, which could result of exit of the thread (BTW, this failure is resulted from USB native reseting behavior during S3). Under this situation, the emulation of USB/xHCI could never continue. This patch fix this issue by continuing to call libusb_handle_events_timeout even it reture -1. The return value will be finally ok after S3 process is completed. This patch will continue poll after failure and hence fix this issue. Tracked-On: #1893 Signed-off-by: Xiaoguang Wu Reviewed-by: Liang Yang Acked-by: Yu Wang --- devicemodel/hw/platform/usb_pmapper.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/devicemodel/hw/platform/usb_pmapper.c b/devicemodel/hw/platform/usb_pmapper.c index 0a401e14f..99dff9cc3 100755 --- a/devicemodel/hw/platform/usb_pmapper.c +++ b/devicemodel/hw/platform/usb_pmapper.c @@ -10,6 +10,7 @@ #include #include #include +#include #include "usb.h" #include "usbdi.h" #include "usb_pmapper.h" @@ -1112,10 +1113,16 @@ static void * usb_dev_sys_thread(void *arg) { struct timeval t = {1, 0}; + int rc = 0; - while (g_ctx.thread_exit == 0 && - libusb_handle_events_timeout(g_ctx.libusb_ctx, &t) >= 0) - ; /* nothing */ + while (g_ctx.thread_exit == 0) { + rc = libusb_handle_events_timeout(g_ctx.libusb_ctx, &t); + if (rc < 0) + /* TODO: maybe one second as interval is too long which + * may result of slower USB enumeration process. + */ + sleep(1); + } UPRINTF(LINF, "poll thread exit\n\r"); return NULL;