Refine HID report writing logic
Blocking write on the keyboard HID device causes screen freezing
during turning off the host power. To fix this issue, this commit
refines the logic using non-blocking write. As a side effect,
non-blocking write introduces event dropping when kernel HID driver
returns -EAGAIN when the driver is in busy state so this commit also
adds retry logic to cover the case.
Tested: Didn't see the screen freezing issue.
Change-Id: Ibd95f567c49f448cd053948c14c006de17c52420
Signed-off-by: Jae Hyun Yoo <jae.hyun.yoo@linux.intel.com>
diff --git a/ikvm_input.hpp b/ikvm_input.hpp
index aae7cef..558251d 100644
--- a/ikvm_input.hpp
+++ b/ikvm_input.hpp
@@ -5,6 +5,7 @@
#include <filesystem>
#include <fstream>
#include <map>
+#include <mutex>
#include <string>
namespace ikvm
@@ -56,8 +57,6 @@
/* @brief Sends a wakeup data packet to the USB input device */
void sendWakeupPacket();
- /* @brief Sends an HID report to the USB input device */
- void sendReport();
private:
static constexpr int NUM_MODIFIER_BITS = 4;
@@ -84,6 +83,8 @@
/* @brief Path to the USB virtual hub */
static constexpr const char* usbVirtualHubPath =
"/sys/bus/platform/devices/1e6a0000.usb-vhub";
+ /* @brief Retry limit for writing an HID report */
+ static constexpr int HID_REPORT_RETRY_MAX = 5;
/*
* @brief Translates a RFB-specific key code to HID modifier bit
*
@@ -100,10 +101,6 @@
bool writeKeyboard(const uint8_t *report);
void writePointer(const uint8_t *report);
- /* @brief Indicates whether or not to send a keyboard report */
- bool sendKeyboard;
- /* @brief Indicates whether or not to send a pointer report */
- bool sendPointer;
/* @brief File descriptor for the USB keyboard device */
int keyboardFd;
/* @brief File descriptor for the USB mouse device */
@@ -123,6 +120,10 @@
std::map<int, int> keysDown;
/* @brief Handle of the HID gadget UDC */
std::ofstream hidUdcStream;
+ /* @brief Mutex for sending keyboard reports */
+ std::mutex keyMutex;
+ /* @brief Mutex for sending pointer reports */
+ std::mutex ptrMutex;
};
} // namespace ikvm