Potentially connect udc device incorrectly
The fs::directory_iterator doesn't specify the order of enumeration of
folders. The if-condition here doesn't prevent the folder other than
gadget port, e.g. 1e6a0000.usb-vhub:pX, be taken into account, e.g.
power folder. In this case, an exception will be generate internally
in connect(). This patch add another check to make sure the folder is
a gadget port.
Without this patch, occasionally obmc-ikvm shows below message and
failed to enumerate USB-HID gadget devices (mouse and keyboard).
```
[ 101.840770] Error: Driver 'configfs-gadget' is already registered,
aborting...
[ 101.848547] UDC core: obmc_hid: driver registration failed: -16
```
Tested with evb-ast2500 to connect VM, then launch KVM and disconnect
for several times.
Fixes: c11257d864ff ("Connect HID gadget device dynamically")
Signed-off-by: Troy Lee <troy_lee@aspeedtech.com>
Change-Id: I0cad041d69d580a544e60c79e1c991482ca79819
diff --git a/ikvm_input.cpp b/ikvm_input.cpp
index 107756b..0d35408 100644
--- a/ikvm_input.cpp
+++ b/ikvm_input.cpp
@@ -55,13 +55,33 @@
{
if (udcName.empty())
{
+ bool found = false;
for (const auto& port : fs::directory_iterator(usbVirtualHubPath))
{
- if (fs::is_directory(port) && !fs::is_symlink(port) &&
- !fs::exists(port.path() / "gadget/suspended"))
+ // /sys/bus/platform/devices/1e6a0000.usb-vhub/1e6a0000.usb-vhub:pX
+ if (fs::is_directory(port) && !fs::is_symlink(port))
{
- const std::string portId = port.path().filename();
- hidUdcStream << portId << std::endl;
+ for (const auto& gadget :
+ fs::directory_iterator(port.path()))
+ {
+ // Kernel 6.0:
+ // /sys/.../1e6a0000.usb-vhub:pX/gadget.Y/suspended
+ // Kernel 5.15:
+ // /sys/.../1e6a0000.usb-vhub:pX/gadget/suspended
+ if (fs::is_directory(gadget) &&
+ gadget.path().string().find("gadget") !=
+ std::string::npos &&
+ !fs::exists(gadget.path() / "suspended"))
+ {
+ const std::string portId = gadget.path().filename();
+ hidUdcStream << portId << std::endl;
+ found = true;
+ break;
+ }
+ }
+ }
+ if (found)
+ {
break;
}
}