From c15d2f297c86b65a3cb866cc2bcece595c136417 Mon Sep 17 00:00:00 2001 From: Le Bao Hiep Date: Fri, 2 Oct 2020 17:06:12 +0700 Subject: [PATCH] Revert LKSBCallbacks --- AsusSMC/AsusSMC.cpp | 58 +++++++++++++--------------------- AsusSMC/AsusSMC.hpp | 7 ++-- AsusSMC/KeyImplementations.cpp | 8 ++--- AsusSMC/KeyImplementations.hpp | 19 +++-------- 4 files changed, 31 insertions(+), 61 deletions(-) diff --git a/AsusSMC/AsusSMC.cpp b/AsusSMC/AsusSMC.cpp index 799ac48..c3c3fc5 100755 --- a/AsusSMC/AsusSMC.cpp +++ b/AsusSMC/AsusSMC.cpp @@ -26,12 +26,6 @@ bool AsusSMC::init(OSDictionary *dict) { atomic_init(¤tLux, 0); atomic_init(¤tFanSpeed, 0); - lksbLock = IOLockAlloc(); - if (!lksbLock) { - SYSLOG("lksb", "failed to allocate LKSB lock"); - return false; - } - return true; } @@ -140,16 +134,16 @@ void AsusSMC::stop(IOService *provider) { OSSafeReleaseNULL(kbdDevice); - IOLockFree(lksbLock); - LKSBCallbacks.deinit(); - super::stop(provider); return; } IOReturn AsusSMC::message(uint32_t type, IOService *provider, void *argument) { + DBGLOG("atk", "Received message: %u Type %x Provider %s", *((uint32_t *)argument), type, provider ? provider->getName() : "unknown"); + switch (type) { case kIOACPIMessageDeviceNotification: + { if (directACPImessaging) { handleMessage(*((uint32_t *)argument)); } else { @@ -161,9 +155,20 @@ IOReturn AsusSMC::message(uint32_t type, IOService *provider, void *argument) { handleMessage(res); } break; - default: - DBGLOG("atk", "Unexpected message: %u Type %x Provider %s", *((uint32_t *)argument), type, provider->getName()); + } + + case kSetKeyboardBacklightMessage: + { + if (hasKeyboardBacklight) { + OSNumber *arg = OSNumber::withNumber(*((uint16_t *)argument) / 16, 16); + atkDevice->evaluateObject("SKBV", NULL, (OSObject **)&arg, 1); + arg->release(); + } break; + } + + default: + return kIOReturnInvalid; } return kIOReturnSuccess; } @@ -404,18 +409,12 @@ void AsusSMC::startATKDevice() { // Check keyboard backlight support if (atkDevice->validateObject("SKBV") == kIOReturnSuccess) { SYSLOG("atk", "Keyboard backlight is supported"); - hasKeybrdBLight = true; - addLKSBConsumer([](const uint16_t &value, OSObject *consumer) { - auto atk = OSDynamicCast(IOACPIPlatformDevice, consumer); - OSNumber *arg = OSNumber::withNumber(value / 16, 16); - atk->evaluateObject("SKBV", NULL, (OSObject **)&arg, 1); - arg->release(); - }, atkDevice); + hasKeyboardBacklight = true; } else { - hasKeybrdBLight = false; + hasKeyboardBacklight = false; DBGLOG("atk", "Keyboard backlight is not supported"); } - setProperty("IsKeyboardBacklightSupported", hasKeybrdBLight); + setProperty("IsKeyboardBacklightSupported", hasKeyboardBacklight); // Turn on ALS sensor toggleALS(true); @@ -550,13 +549,13 @@ void AsusSMC::handleMessage(int code) { break; case 0xC5: // Keyboard Backlight Down - if (hasKeybrdBLight) { + if (hasKeyboardBacklight) { dispatchTCReport(kHIDUsage_AV_TopCase_IlluminationDown); } break; case 0xC4: // Keyboard Backlight Up - if (hasKeybrdBLight) { + if (hasKeyboardBacklight) { dispatchTCReport(kHIDUsage_AV_TopCase_IlluminationUp); } break; @@ -572,19 +571,6 @@ void AsusSMC::handleMessage(int code) { DBGLOG("atk", "Received key %d(0x%x)", code, code); } -void AsusSMC::addLKSBConsumer(lksbCallback callback, OSObject *consumer) { - auto *pcall = stored_pair::create(); - pcall->first = callback; - pcall->second = consumer; - - IOLockLock(lksbLock); - if (!LKSBCallbacks.push_back(pcall)) { - SYSLOG("lksb", "failed to store lksb callback"); - stored_pair::deleter(pcall); - } - IOLockUnlock(lksbLock); -} - void AsusSMC::letSleep() { kev.sendMessage(kDaemonSleep, 0, 0); } @@ -856,7 +842,7 @@ void AsusSMC::registerVSMC() { SMC_KEY_ATTRIBUTE_READ | SMC_KEY_ATTRIBUTE_WRITE | SMC_KEY_ATTRIBUTE_FUNCTION)); VirtualSMCAPI::addKey(KeyLKSB, vsmcPlugin.data, VirtualSMCAPI::valueWithData( - reinterpret_cast(&lkb), sizeof(lkb), SmcKeyTypeLkb, new SMCKBrdBLightValue(atkDevice, &LKSBCallbacks, lksbLock), + reinterpret_cast(&lkb), sizeof(lkb), SmcKeyTypeLkb, new SMCKBrdBLightValue(this), SMC_KEY_ATTRIBUTE_READ | SMC_KEY_ATTRIBUTE_WRITE | SMC_KEY_ATTRIBUTE_FUNCTION)); VirtualSMCAPI::addKey(KeyLKSS, vsmcPlugin.data, VirtualSMCAPI::valueWithData( diff --git a/AsusSMC/AsusSMC.hpp b/AsusSMC/AsusSMC.hpp index 31d93d9..0c69ddb 100644 --- a/AsusSMC/AsusSMC.hpp +++ b/AsusSMC/AsusSMC.hpp @@ -10,6 +10,7 @@ #include #include +#include #include "HIDReport.hpp" #include "HIDUsageTables.h" #include "VirtualAppleKeyboard.hpp" @@ -132,18 +133,14 @@ class AsusSMC : public IOService { consumer_input csmrreport; apple_vendor_top_case_input tcreport; - bool hasKeybrdBLight {false}; bool directACPImessaging {false}; + bool hasKeyboardBacklight {false}; bool isALSEnabled {true}; bool isTouchpadEnabled {true}; bool isPanelBackLightOn {true}; bool isTACHAvailable {false}; bool isBatteryRSOCAvailable {false}; - lksb_vector LKSBCallbacks; - IOLock *lksbLock {nullptr}; - void addLKSBConsumer(lksbCallback callback, OSObject *consumer); - uint32_t panelBrightnessLevel {16}; char backlightEntry[1000]; diff --git a/AsusSMC/KeyImplementations.cpp b/AsusSMC/KeyImplementations.cpp index 3124b68..e1db666 100644 --- a/AsusSMC/KeyImplementations.cpp +++ b/AsusSMC/KeyImplementations.cpp @@ -38,13 +38,9 @@ SMC_RESULT SMCKBrdBLightValue::update(const SMC_DATA *src) { delete value; - IOLockLock(lksbLock); - for (size_t i = 0; i < callbacks->size(); i++) { - auto p = (*callbacks)[i]; - p->first(tval, p->second); - DBGLOG("lksb", "callback %u consumer %p", i, p->second); + if (asusSMCInstance) { + asusSMCInstance->message(kSetKeyboardBacklightMessage, nullptr, &tval); } - IOLockUnlock(lksbLock); lilu_os_memcpy(data, src, size); return SmcSuccess; diff --git a/AsusSMC/KeyImplementations.hpp b/AsusSMC/KeyImplementations.hpp index 950315f..4509f7d 100644 --- a/AsusSMC/KeyImplementations.hpp +++ b/AsusSMC/KeyImplementations.hpp @@ -9,9 +9,11 @@ #ifndef KeyImplementations_hpp #define KeyImplementations_hpp -#include +#include #include +#define kSetKeyboardBacklightMessage 812002 + static constexpr SMC_KEY KeyAL = SMC_MAKE_IDENTIFIER('A','L','!',' '); static constexpr SMC_KEY KeyALI0 = SMC_MAKE_IDENTIFIER('A','L','I','0'); static constexpr SMC_KEY KeyALI1 = SMC_MAKE_IDENTIFIER('A','L','I','1'); @@ -49,15 +51,6 @@ typedef struct fanTypeDescStruct { char strFunction[DiagFunctionStrLen]; } FanTypeDescStruct; -template -using stored_pair = ppair; - -template -using stored_vector = evector *, stored_pair::deleter>; - -using lksbCallback = void (*)(const uint16_t &value, OSObject *consumer); -using lksb_vector = stored_vector; - class ALSForceBits : public VirtualSMCValue { public: enum { @@ -110,9 +103,7 @@ class SMCALSValue : public VirtualSMCValue { }; class SMCKBrdBLightValue : public VirtualSMCValue { - IOACPIPlatformDevice *atkDevice {nullptr}; - lksb_vector *callbacks {nullptr}; - IOLock *lksbLock {nullptr}; + IOService *asusSMCInstance {nullptr}; protected: SMC_RESULT update(const SMC_DATA *src) override; @@ -127,7 +118,7 @@ class SMCKBrdBLightValue : public VirtualSMCValue { uint8_t val2 {1}; }; - SMCKBrdBLightValue(IOACPIPlatformDevice *atkDevice, lksb_vector *callbacks, IOLock *lksbLock): atkDevice(atkDevice), callbacks(callbacks), lksbLock(lksbLock) {} + SMCKBrdBLightValue(IOService *asusSMCInstance): asusSMCInstance(asusSMCInstance) {} }; class F0Ac : public VirtualSMCValue {