From 3e9bf9a43713d42f02a8f080eacdd1300ad0777b Mon Sep 17 00:00:00 2001 From: Jerry Liu Date: Mon, 22 Sep 2025 14:47:22 -0700 Subject: [PATCH 1/2] SMBusPawnIO: Extract helper method and clean up code * Extract LoadModuleAndAddSMBus helper method to reduce duplication * Simplify SMBus detection logic with consistent pattern for all types * Replace unused out parameters with discard pattern (_) * Convert nested using statements to simplified using declarations --- RAMSPDToolkit/I2CSMBus/SMBusPawnIO.cs | 87 ++++++++++++++------------- 1 file changed, 45 insertions(+), 42 deletions(-) diff --git a/RAMSPDToolkit/I2CSMBus/SMBusPawnIO.cs b/RAMSPDToolkit/I2CSMBus/SMBusPawnIO.cs index 02b9df6..08dec77 100644 --- a/RAMSPDToolkit/I2CSMBus/SMBusPawnIO.cs +++ b/RAMSPDToolkit/I2CSMBus/SMBusPawnIO.cs @@ -107,7 +107,7 @@ protected override int I2CSMBusXfer(byte addr, byte read_write, byte command, in using (var guard = new WorldMutexGuard(WorldMutexManager.WorldSMBusMutex)) { - status = PawnIO.Execute("ioctl_smbus_xfer", inBuffer, inSize, outBuffer, outSize, out var retSize); + status = PawnIO.Execute("ioctl_smbus_xfer", inBuffer, inSize, outBuffer, outSize, out var _); } if (data != null) @@ -142,48 +142,37 @@ public static bool SMBusDetect() } //Lock SMBus mutex - using (var smbm = new WorldMutexGuard(WorldMutexManager.WorldSMBusMutex)) + using var smbm = new WorldMutexGuard(WorldMutexManager.WorldSMBusMutex); + //Lock PCI mutex + using var pci = new WorldMutexGuard(WorldMutexManager.WorldPCIMutex); + + //I801 + if (LoadModuleAndAddSMBus(pawnIO, PawnIOSMBusIdentifier.I801)) + { + return true; + } + + //Piix4 + bool loadedPiix4 = false; + for (int port = 0; port < 2; port++) { - //Lock PCI mutex - using (var pci = new WorldMutexGuard(WorldMutexManager.WorldPCIMutex)) + if (!LoadModuleAndAddSMBus(pawnIO, PawnIOSMBusIdentifier.Piix4, (m) => Piix4PortSelect(m, port))) { - //I801 - var i801 = pawnIO.LoadModule(PawnIOSMBusIdentifier.I801); - if (i801 != null) - { - SMBusManager.AddSMBus(new SMBusPawnIO(i801, PawnIOSMBusIdentifier.I801)); - return true; - } + break; + } - //Piix4 - var piix4 = pawnIO.LoadModule(PawnIOSMBusIdentifier.Piix4); - if (piix4 != null) - { - if (Piix4PortSelect(piix4, 0)) - { - SMBusManager.AddSMBus(new SMBusPawnIO(piix4, PawnIOSMBusIdentifier.Piix4)); - } - - piix4 = pawnIO.LoadModule(PawnIOSMBusIdentifier.Piix4); - if (piix4 != null) - { - if (Piix4PortSelect(piix4, 1)) - { - SMBusManager.AddSMBus(new SMBusPawnIO(piix4, PawnIOSMBusIdentifier.Piix4)); - } - } - - return true; - } + loadedPiix4 = true; + } - //NCT6793 - var nct6793 = pawnIO.LoadModule(PawnIOSMBusIdentifier.NCT6793); - if (nct6793 != null) - { - SMBusManager.AddSMBus(new SMBusPawnIO(nct6793, PawnIOSMBusIdentifier.NCT6793)); - return true; - } - } + if (loadedPiix4) + { + return true; + } + + //NCT6793 + if (LoadModuleAndAddSMBus(pawnIO, PawnIOSMBusIdentifier.NCT6793)) + { + return true; } return false; @@ -193,6 +182,20 @@ public static bool SMBusDetect() #region Private + static bool LoadModuleAndAddSMBus(IPawnIODriver pawnIO, PawnIOSMBusIdentifier pawnIOSMBusIdentifier, Func addBusCondition = null) + { + var module = pawnIO.LoadModule(pawnIOSMBusIdentifier); + if (module != null) + { + if (addBusCondition == null || addBusCondition(module)) + { + SMBusManager.AddSMBus(new SMBusPawnIO(module, pawnIOSMBusIdentifier)); + } + return true; + } + return false; + } + static bool Piix4PortSelect(IPawnIOModule pawnIO, int port) { uint inSize = 1; @@ -203,7 +206,7 @@ static bool Piix4PortSelect(IPawnIOModule pawnIO, int port) inBuffer[0] = port; - return pawnIO.Execute("ioctl_piix4_port_sel", inBuffer, inSize, outBuffer, outSize, out var returnSize) == 0; + return pawnIO.Execute("ioctl_piix4_port_sel", inBuffer, inSize, outBuffer, outSize, out var _) == 0; } void GetIdentity(IPawnIOModule pawnIO) @@ -216,7 +219,7 @@ void GetIdentity(IPawnIOModule pawnIO) var inBuffer = new long[inSize]; var outBuffer = new long[outSize]; - if (pawnIO.Execute("ioctl_identity", inBuffer, inSize, outBuffer, outSize, out var returnSize) == 0) + if (pawnIO.Execute("ioctl_identity", inBuffer, inSize, outBuffer, outSize, out var _) == 0) { PortID = SMBusManager.RegisteredSMBuses.Count; // Assign next available port ID @@ -253,7 +256,7 @@ void CheckWriteProtection(IPawnIOModule pawnIO) bool writeProtectionEnabled = false; - if (pawnIO.Execute("ioctl_write_protection", inBuffer, inSize, outBuffer, outSize, out var returnSize) == 0) + if (pawnIO.Execute("ioctl_write_protection", inBuffer, inSize, outBuffer, outSize, out var _) == 0) { writeProtectionEnabled = outBuffer[0] == 1; } From 3d67910db46f65b0e6853dde4725bf128eff2d67 Mon Sep 17 00:00:00 2001 From: Jerry Liu Date: Mon, 22 Sep 2025 14:56:08 -0700 Subject: [PATCH 2/2] Update discards --- RAMSPDToolkit/I2CSMBus/SMBusPawnIO.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/RAMSPDToolkit/I2CSMBus/SMBusPawnIO.cs b/RAMSPDToolkit/I2CSMBus/SMBusPawnIO.cs index 08dec77..edf85e9 100644 --- a/RAMSPDToolkit/I2CSMBus/SMBusPawnIO.cs +++ b/RAMSPDToolkit/I2CSMBus/SMBusPawnIO.cs @@ -107,7 +107,7 @@ protected override int I2CSMBusXfer(byte addr, byte read_write, byte command, in using (var guard = new WorldMutexGuard(WorldMutexManager.WorldSMBusMutex)) { - status = PawnIO.Execute("ioctl_smbus_xfer", inBuffer, inSize, outBuffer, outSize, out var _); + status = PawnIO.Execute("ioctl_smbus_xfer", inBuffer, inSize, outBuffer, outSize, out _); } if (data != null) @@ -206,7 +206,7 @@ static bool Piix4PortSelect(IPawnIOModule pawnIO, int port) inBuffer[0] = port; - return pawnIO.Execute("ioctl_piix4_port_sel", inBuffer, inSize, outBuffer, outSize, out var _) == 0; + return pawnIO.Execute("ioctl_piix4_port_sel", inBuffer, inSize, outBuffer, outSize, out _) == 0; } void GetIdentity(IPawnIOModule pawnIO) @@ -219,7 +219,7 @@ void GetIdentity(IPawnIOModule pawnIO) var inBuffer = new long[inSize]; var outBuffer = new long[outSize]; - if (pawnIO.Execute("ioctl_identity", inBuffer, inSize, outBuffer, outSize, out var _) == 0) + if (pawnIO.Execute("ioctl_identity", inBuffer, inSize, outBuffer, outSize, out _) == 0) { PortID = SMBusManager.RegisteredSMBuses.Count; // Assign next available port ID @@ -256,7 +256,7 @@ void CheckWriteProtection(IPawnIOModule pawnIO) bool writeProtectionEnabled = false; - if (pawnIO.Execute("ioctl_write_protection", inBuffer, inSize, outBuffer, outSize, out var _) == 0) + if (pawnIO.Execute("ioctl_write_protection", inBuffer, inSize, outBuffer, outSize, out _) == 0) { writeProtectionEnabled = outBuffer[0] == 1; }