Skip to content

Commit

Permalink
Revert LKSBCallbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
hieplpvip committed Oct 2, 2020
1 parent 7eeb20f commit c15d2f2
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 61 deletions.
58 changes: 22 additions & 36 deletions AsusSMC/AsusSMC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ bool AsusSMC::init(OSDictionary *dict) {
atomic_init(&currentLux, 0);
atomic_init(&currentFanSpeed, 0);

lksbLock = IOLockAlloc();
if (!lksbLock) {
SYSLOG("lksb", "failed to allocate LKSB lock");
return false;
}

return true;
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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<lksbCallback, OSObject *>::create();
pcall->first = callback;
pcall->second = consumer;

IOLockLock(lksbLock);
if (!LKSBCallbacks.push_back(pcall)) {
SYSLOG("lksb", "failed to store lksb callback");
stored_pair<lksbCallback, OSObject *>::deleter(pcall);
}
IOLockUnlock(lksbLock);
}

void AsusSMC::letSleep() {
kev.sendMessage(kDaemonSleep, 0, 0);
}
Expand Down Expand Up @@ -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<const SMC_DATA *>(&lkb), sizeof(lkb), SmcKeyTypeLkb, new SMCKBrdBLightValue(atkDevice, &LKSBCallbacks, lksbLock),
reinterpret_cast<const SMC_DATA *>(&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(
Expand Down
7 changes: 2 additions & 5 deletions AsusSMC/AsusSMC.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include <IOKit/IOTimerEventSource.h>
#include <IOKit/IOCommandGate.h>
#include <IOKit/acpi/IOACPIPlatformDevice.h>
#include "HIDReport.hpp"
#include "HIDUsageTables.h"
#include "VirtualAppleKeyboard.hpp"
Expand Down Expand Up @@ -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];

Expand Down
8 changes: 2 additions & 6 deletions AsusSMC/KeyImplementations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 5 additions & 14 deletions AsusSMC/KeyImplementations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
#ifndef KeyImplementations_hpp
#define KeyImplementations_hpp

#include <IOKit/acpi/IOACPIPlatformDevice.h>
#include <IOKit/IOService.h>
#include <VirtualSMCSDK/kern_vsmcapi.hpp>

#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');
Expand Down Expand Up @@ -49,15 +51,6 @@ typedef struct fanTypeDescStruct {
char strFunction[DiagFunctionStrLen];
} FanTypeDescStruct;

template <typename T, typename Y=void *>
using stored_pair = ppair<T, Y>;

template <typename T, typename Y=void *>
using stored_vector = evector<stored_pair<T, Y> *, stored_pair<T, Y>::deleter>;

using lksbCallback = void (*)(const uint16_t &value, OSObject *consumer);
using lksb_vector = stored_vector<lksbCallback, OSObject *>;

class ALSForceBits : public VirtualSMCValue {
public:
enum {
Expand Down Expand Up @@ -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;
Expand All @@ -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 {
Expand Down

0 comments on commit c15d2f2

Please sign in to comment.