summaryrefslogtreecommitdiff
path: root/meta-openbmc-mods/meta-common/recipes-graphics/obmc-ikvm/obmc-ikvm/0005-Refine-HID-report-writing-logic.patch
blob: 5293f3f27f152bdc2ef57290d5d87c6720eacc29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
From 68885eb4d056b8343c567c48ece7e875feb28fc0 Mon Sep 17 00:00:00 2001
From: Jae Hyun Yoo <jae.hyun.yoo@intel.com>
Date: Thu, 30 Jul 2020 00:29:19 -0700
Subject: [PATCH] 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@intel.com>
---
 ikvm_input.cpp  | 106 ++++++++++++++++++++++++++++++++----------------
 ikvm_input.hpp  |  13 +++---
 ikvm_server.cpp |   2 -
 3 files changed, 79 insertions(+), 42 deletions(-)

diff --git a/ikvm_input.cpp b/ikvm_input.cpp
index c4cce5088461..480db3c094bc 100644
--- a/ikvm_input.cpp
+++ b/ikvm_input.cpp
@@ -25,9 +25,8 @@ using namespace phosphor::logging;
 using namespace sdbusplus::xyz::openbmc_project::Common::File::Error;
 
 Input::Input(const std::string& kbdPath, const std::string& ptrPath) :
-    sendKeyboard(false), sendPointer(false), keyboardFd(-1), pointerFd(-1),
-    keyboardReport{0}, pointerReport{0}, keyboardPath(kbdPath),
-    pointerPath(ptrPath)
+    keyboardFd(-1), pointerFd(-1), keyboardReport{0}, pointerReport{0},
+    keyboardPath(kbdPath), pointerPath(ptrPath)
 {
     hidUdcStream.exceptions(std::ofstream::failbit | std::ofstream::badbit);
     hidUdcStream.open(hidUdcPath, std::ios::out | std::ios::app);
@@ -79,7 +78,8 @@ void Input::connect()
 
     if (!keyboardPath.empty())
     {
-        keyboardFd = open(keyboardPath.c_str(), O_RDWR | O_CLOEXEC);
+        keyboardFd = open(keyboardPath.c_str(),
+                          O_RDWR | O_CLOEXEC | O_NONBLOCK);
         if (keyboardFd < 0)
         {
             log<level::ERR>("Failed to open input device",
@@ -135,6 +135,12 @@ void Input::keyEvent(rfbBool down, rfbKeySym key, rfbClientPtr cl)
 {
     Server::ClientData* cd = (Server::ClientData*)cl->clientData;
     Input* input = cd->input;
+    bool sendKeyboard = false;
+
+    if (input->keyboardFd < 0)
+    {
+        return;
+    }
 
     if (down)
     {
@@ -150,7 +156,7 @@ void Input::keyEvent(rfbBool down, rfbKeySym key, rfbClientPtr cl)
                     {
                         input->keyboardReport[i] = sc;
                         input->keysDown.insert(std::make_pair(key, i));
-                        input->sendKeyboard = true;
+                        sendKeyboard = true;
                         break;
                     }
                 }
@@ -163,7 +169,7 @@ void Input::keyEvent(rfbBool down, rfbKeySym key, rfbClientPtr cl)
             if (mod)
             {
                 input->keyboardReport[0] |= mod;
-                input->sendKeyboard = true;
+                sendKeyboard = true;
             }
         }
     }
@@ -175,7 +181,7 @@ void Input::keyEvent(rfbBool down, rfbKeySym key, rfbClientPtr cl)
         {
             input->keyboardReport[it->second] = 0;
             input->keysDown.erase(it);
-            input->sendKeyboard = true;
+            sendKeyboard = true;
         }
         else
         {
@@ -184,10 +190,15 @@ void Input::keyEvent(rfbBool down, rfbKeySym key, rfbClientPtr cl)
             if (mod)
             {
                 input->keyboardReport[0] &= ~mod;
-                input->sendKeyboard = true;
+                sendKeyboard = true;
             }
         }
     }
+
+    if (sendKeyboard)
+    {
+        input->writeKeyboard(input->keyboardReport);
+    }
 }
 
 void Input::pointerEvent(int buttonMask, int x, int y, rfbClientPtr cl)
@@ -197,6 +208,11 @@ void Input::pointerEvent(int buttonMask, int x, int y, rfbClientPtr cl)
     Server* server = (Server*)cl->screen->screenData;
     const Video& video = server->getVideo();
 
+    if (input->pointerFd < 0)
+    {
+        return;
+    }
+
     input->pointerReport[0] = ((buttonMask & 0x4) >> 1) |
                               ((buttonMask & 0x2) << 1) | (buttonMask & 0x1);
 
@@ -214,8 +230,8 @@ void Input::pointerEvent(int buttonMask, int x, int y, rfbClientPtr cl)
         memcpy(&input->pointerReport[3], &yy, 2);
     }
 
-    input->sendPointer = true;
     rfbDefaultPtrAddEvent(buttonMask, x, y, cl);
+    input->writePointer(input->pointerReport);
 }
 
 void Input::sendWakeupPacket()
@@ -249,23 +265,6 @@ void Input::sendWakeupPacket()
     }
 }
 
-void Input::sendReport()
-{
-    if (sendKeyboard && keyboardFd >= 0)
-    {
-        writeKeyboard(keyboardReport);
-
-        sendKeyboard = false;
-    }
-
-    if (sendPointer && pointerFd >= 0)
-    {
-        writePointer(pointerReport);
-
-        sendPointer = false;
-    }
-}
-
 uint8_t Input::keyToMod(rfbKeySym key)
 {
     uint8_t mod = 0;
@@ -489,14 +488,35 @@ uint8_t Input::keyToScancode(rfbKeySym key)
 
 bool Input::writeKeyboard(const uint8_t *report)
 {
-    if (write(keyboardFd, report, KEY_REPORT_LENGTH) != KEY_REPORT_LENGTH)
+    std::unique_lock<std::mutex> lk(keyMutex);
+    uint retryCount = HID_REPORT_RETRY_MAX;
+
+    while (retryCount > 0)
     {
-        if (errno != ESHUTDOWN && errno != EAGAIN)
+        if (write(keyboardFd, report, KEY_REPORT_LENGTH) == KEY_REPORT_LENGTH)
         {
-            log<level::ERR>("Failed to write keyboard report",
-                            entry("ERROR=%s", strerror(errno)));
+            break;
+        }
+
+        if (errno != EAGAIN)
+        {
+            if (errno != ESHUTDOWN)
+            {
+                log<level::ERR>("Failed to write keyboard report",
+                                entry("ERROR=%s", strerror(errno)));
+            }
+
+            break;
         }
 
+        lk.unlock();
+        std::this_thread::sleep_for(std::chrono::milliseconds(10));
+        lk.lock();
+        retryCount--;
+    }
+
+    if (!retryCount || errno)
+    {
         return false;
     }
 
@@ -505,13 +525,31 @@ bool Input::writeKeyboard(const uint8_t *report)
 
 void Input::writePointer(const uint8_t *report)
 {
-    if (write(pointerFd, report, PTR_REPORT_LENGTH) != PTR_REPORT_LENGTH)
+    std::unique_lock<std::mutex> lk(ptrMutex);
+    uint retryCount = HID_REPORT_RETRY_MAX;
+
+    while (retryCount > 0)
     {
-        if (errno != ESHUTDOWN && errno != EAGAIN)
+        if (write(pointerFd, report, PTR_REPORT_LENGTH) == PTR_REPORT_LENGTH)
         {
-            log<level::ERR>("Failed to write pointer report",
-                            entry("ERROR=%s", strerror(errno)));
+            break;
+        }
+
+        if (errno != EAGAIN)
+        {
+            if (errno != ESHUTDOWN)
+            {
+                log<level::ERR>("Failed to write pointer report",
+                                entry("ERROR=%s", strerror(errno)));
+            }
+
+            break;
         }
+
+        lk.unlock();
+        std::this_thread::sleep_for(std::chrono::milliseconds(10));
+        lk.lock();
+        retryCount--;
     }
 }
 
diff --git a/ikvm_input.hpp b/ikvm_input.hpp
index aae7cefbef6e..558251d673cc 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 @@ class Input
 
     /* @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 @@ class Input
     /* @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 @@ class Input
     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 @@ class Input
     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
diff --git a/ikvm_server.cpp b/ikvm_server.cpp
index 0736d1f55f73..7be99e4379d1 100644
--- a/ikvm_server.cpp
+++ b/ikvm_server.cpp
@@ -79,8 +79,6 @@ void Server::run()
 
     if (server->clientHead)
     {
-        input.sendReport();
-
         frameCounter++;
         if (pendingResize && frameCounter > video.getFrameRate())
         {
-- 
2.17.1