From 7964f1bef636055ef060b58002c4f5d23696e2be Mon Sep 17 00:00:00 2001 From: Le Bao Hiep Date: Tue, 11 Jun 2019 14:41:20 +0700 Subject: [PATCH] v1.1.1 - Fix some memory leaks - Direct ACPI messaging --- AsusSMC.xcodeproj/project.pbxproj | 12 +++--- AsusSMC/AsusSMC.cpp | 67 +++++++++++++------------------ AsusSMC/AsusSMC.hpp | 9 ++++- AsusSMC/KeyImplementations.cpp | 7 ++-- 4 files changed, 46 insertions(+), 49 deletions(-) diff --git a/AsusSMC.xcodeproj/project.pbxproj b/AsusSMC.xcodeproj/project.pbxproj index 80d4621..db09ef6 100644 --- a/AsusSMC.xcodeproj/project.pbxproj +++ b/AsusSMC.xcodeproj/project.pbxproj @@ -570,7 +570,7 @@ GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; MACOSX_DEPLOYMENT_TARGET = 10.10; - MODULE_VERSION = 1.1; + MODULE_VERSION = 1.1.1; MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; MTL_FAST_MATH = YES; ONLY_ACTIVE_ARCH = YES; @@ -622,7 +622,7 @@ GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; MACOSX_DEPLOYMENT_TARGET = 10.10; - MODULE_VERSION = 1.1; + MODULE_VERSION = 1.1.1; MTL_ENABLE_DEBUG_INFO = NO; MTL_FAST_MATH = YES; SDKROOT = macosx; @@ -633,7 +633,7 @@ isa = XCBuildConfiguration; buildSettings = { COMBINE_HIDPI_IMAGES = YES; - CURRENT_PROJECT_VERSION = 1.1; + CURRENT_PROJECT_VERSION = 1.1.1; GCC_PREPROCESSOR_DEFINITIONS = ( "MODULE_VERSION=$(MODULE_VERSION)", "PRODUCT_NAME=$(PRODUCT_NAME)", @@ -651,7 +651,7 @@ MODULE_NAME = com.hieplpvip.AsusSMC; MODULE_START = "$(PRODUCT_NAME)_kern_start"; MODULE_STOP = "$(PRODUCT_NAME)_kern_stop"; - MODULE_VERSION = 1.1; + MODULE_VERSION = 1.1.1; OTHER_CPLUSPLUSFLAGS = "-Wno-inconsistent-missing-override"; PRODUCT_BUNDLE_IDENTIFIER = com.hieplpvip.AsusSMC; PRODUCT_NAME = "$(TARGET_NAME)"; @@ -663,7 +663,7 @@ isa = XCBuildConfiguration; buildSettings = { COMBINE_HIDPI_IMAGES = YES; - CURRENT_PROJECT_VERSION = 1.1; + CURRENT_PROJECT_VERSION = 1.1.1; GCC_PREPROCESSOR_DEFINITIONS = ( "MODULE_VERSION=$(MODULE_VERSION)", "PRODUCT_NAME=$(PRODUCT_NAME)", @@ -681,7 +681,7 @@ MODULE_NAME = com.hieplpvip.AsusSMC; MODULE_START = "$(PRODUCT_NAME)_kern_start"; MODULE_STOP = "$(PRODUCT_NAME)_kern_stop"; - MODULE_VERSION = 1.1; + MODULE_VERSION = 1.1.1; OTHER_CPLUSPLUSFLAGS = "-Wno-inconsistent-missing-override"; PRODUCT_BUNDLE_IDENTIFIER = com.hieplpvip.AsusSMC; PRODUCT_NAME = "$(TARGET_NAME)"; diff --git a/AsusSMC/AsusSMC.cpp b/AsusSMC/AsusSMC.cpp index 30ea143..6b6cd26 100644 --- a/AsusSMC/AsusSMC.cpp +++ b/AsusSMC/AsusSMC.cpp @@ -210,13 +210,16 @@ bool AsusSMC::start(IOService *provider) { } atkDevice = (IOACPIPlatformDevice *) provider; - atkDevice->evaluateObject("INIT", NULL, NULL, NULL); + + OSNumber *arg = OSNumber::withNumber(1, 8); + atkDevice->evaluateObject("INIT", NULL, (OSObject**)&arg, 1); + arg->release(); SYSLOG("atk", "Found WMI Device %s", atkDevice->getName()); parse_wdg(properties); - checkKBALS(); + checkATK(); initVirtualKeyboard(); @@ -297,37 +300,20 @@ IOReturn AsusSMC::setPowerState(unsigned long powerStateOrdinal, IOService *what IOReturn AsusSMC::message(UInt32 type, IOService *provider, void *argument) { if (type == kIOACPIMessageDeviceNotification) { - OSObject *wed; - UInt32 event = *((UInt32 *) argument); - OSNumber *number = OSNumber::withNumber(event, 32); - atkDevice->evaluateObject("_WED", &wed, (OSObject**)&number, 1); - number->release(); - number = OSDynamicCast(OSNumber, wed); - if (!number) { - // try a package - OSArray *array = OSDynamicCast(OSArray, wed); - if (!array) { - // try a buffer - OSData *data = OSDynamicCast(OSData, wed); - if ((!data) || (data->getLength() == 0)) { - DBGLOG("atk", "Fail to cast _WED returned objet %s", wed->getMetaClass()->getClassName()); - return kIOReturnError; - } - const char *bytes = (const char *) data->getBytesNoCopy(); - number = OSNumber::withNumber(bytes[0],32); - } else { - number = OSDynamicCast(OSNumber, array->getObject(0)); - if (!number) { - DBGLOG("atk", "Fail to cast _WED returned 1st objet in array %s", array->getObject(0)->getMetaClass()->getClassName()); - return kIOReturnError; - } - } - } + if (directACPImessaging) { + handleMessage(*((UInt32 *) argument)); + } else { + UInt32 event = *((UInt32 *) argument); + OSNumber *arg = OSNumber::withNumber(event, sizeof(event) * 8); + UInt32 res; + atkDevice->evaluateInteger("_WED", &res, (OSObject**)&arg, 1); + arg->release(); - handleMessage(number->unsigned32BitValue()); - } - else + handleMessage(res); + } + } else { DBGLOG("atk", "Unexpected message: %u Type %x Provider %s", *((UInt32 *) argument), uint(type), provider->getName()); + } return kIOReturnSuccess; } @@ -439,7 +425,13 @@ void AsusSMC::handleMessage(int code) { DBGLOG("atk", "Received key %d(0x%x)", code, code); } -void AsusSMC::checkKBALS() { +void AsusSMC::checkATK() { + // Check direct ACPI messaging support + if (atkDevice->validateObject("DMES") == kIOReturnSuccess) { + DBGLOG("atk", "Direct ACPI message is supported"); + directACPImessaging = true; + } + // Check keyboard backlight support if (atkDevice->validateObject("SKBV") == kIOReturnSuccess) { SYSLOG("atk", "Keyboard backlight is supported"); @@ -465,15 +457,14 @@ void AsusSMC::checkKBALS() { } void AsusSMC::toggleALS(bool state) { - OSObject *params[1]; - params[0] = OSNumber::withNumber(state, 8); - UInt32 res; - if (atkDevice->evaluateInteger("ALSC", &res, params, 1) == kIOReturnSuccess) + OSNumber *arg = OSNumber::withNumber(state, sizeof(state) * 8); + if (atkDevice->evaluateInteger("ALSC", &res, (OSObject**)&arg, 1) == kIOReturnSuccess) DBGLOG("atk", "ALS has been %s (ALSC ret %d)", state ? "enabled" : "disabled", res); else DBGLOG("atk", "Failed to call ALSC"); setProperty("IsALSEnabled", state); + arg->release(); } int AsusSMC::checkBacklightEntry() { @@ -561,7 +552,7 @@ void AsusSMC::initVirtualKeyboard() { _virtualKBrd = new VirtualHIDKeyboard; if (!_virtualKBrd || !_virtualKBrd->init() || !_virtualKBrd->attach(this) || !_virtualKBrd->start(this)) { - _virtualKBrd->release(); + OSSafeReleaseNULL(_virtualKBrd); SYSLOG("virtkbrd", "Failed to init VirtualHIDKeyboard"); } else _virtualKBrd->setCountryCode(0); @@ -662,7 +653,7 @@ void AsusSMC::dispatchMessage(int message, void *data) { } #pragma mark - -#pragma mark VirtualSMC plugin +#pragma mark VirtualSMC plugin - Ported from SMCLightSensor #pragma mark - void AsusSMC::registerVSMC() { diff --git a/AsusSMC/AsusSMC.hpp b/AsusSMC/AsusSMC.hpp index 66185f6..dcf6065 100644 --- a/AsusSMC/AsusSMC.hpp +++ b/AsusSMC/AsusSMC.hpp @@ -177,6 +177,13 @@ class EXPORT AsusSMC : public IOService { */ bool hasKeybrdBLight {false}; + /** + * Direct ACPI messaging support + * Originally, receiving ACPI messages takes several unnecessary steps (thanks, ASUS!) + * By patching method IANE in DSDT, we can avoid those steps + */ + bool directACPImessaging {false}; + /** * ALS availability */ @@ -200,7 +207,7 @@ class EXPORT AsusSMC : public IOService { /** * Check ALS and keyboard backlight availability */ - void checkKBALS(); + void checkATK(); /** * Enable/Disable ALS sensor diff --git a/AsusSMC/KeyImplementations.cpp b/AsusSMC/KeyImplementations.cpp index e404687..0a42583 100644 --- a/AsusSMC/KeyImplementations.cpp +++ b/AsusSMC/KeyImplementations.cpp @@ -35,11 +35,10 @@ SMC_RESULT SMCKBrdBLightValue::update(const SMC_DATA *src) { uint16_t tval = (value->val1 << 4) | (value->val2 >> 4); DBGLOG("kbrdblight", "LKSB update %d", tval); tval = tval / 16; - OSObject *params[1]; - OSObject *ret = NULL; - params[0] = OSNumber::withNumber(tval, sizeof(tval) * 8); - atkDevice->evaluateObject("SKBV", &ret, params, 1); + OSNumber *arg = OSNumber::withNumber(tval, sizeof(tval) * 8); + atkDevice->evaluateObject("SKBV", NULL, (OSObject**)&arg, 1); + arg->release(); } delete value;