diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f4c605f --- /dev/null +++ b/.gitignore @@ -0,0 +1,15 @@ +.DS_Store +Distribute +# Ignore build folder +build +ModuleCache +Index +# Ignore XCode user-data +IOElectrify.xcodeproj/project.xcworkspace/xcuserdata +IOElectrify.xcodeproj/project.xcworkspace/xcshareddata +IOElectrify.xcodeproj/xcuserdata +IOElectrify/TextIndex +IOElectrify/OpenQuickly-ReferencedFrameworks* +IOElectrify/info.plist +IOElectrify/scm.plist + diff --git a/IOElectrify.xcodeproj/project.pbxproj b/IOElectrify.xcodeproj/project.pbxproj new file mode 100644 index 0000000..329a330 --- /dev/null +++ b/IOElectrify.xcodeproj/project.pbxproj @@ -0,0 +1,254 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 46; + objects = { + +/* Begin PBXBuildFile section */ + D4096F861A52FCED005C037A /* IOElectrify.h in Headers */ = {isa = PBXBuildFile; fileRef = D4096F851A52FCED005C037A /* IOElectrify.h */; }; + D4096F881A52FCED005C037A /* IOElectrify.cpp in Sources */ = {isa = PBXBuildFile; fileRef = D4096F871A52FCED005C037A /* IOElectrify.cpp */; }; + D41D13241FB57F7400412FC6 /* IOElectrifyBridge.cpp in Sources */ = {isa = PBXBuildFile; fileRef = D41D13231FB57F7400412FC6 /* IOElectrifyBridge.cpp */; }; + D49DC3741FB341EB000D0F4F /* WMI.cpp in Sources */ = {isa = PBXBuildFile; fileRef = D49DC3721FB341EB000D0F4F /* WMI.cpp */; }; + D49DC3751FB341EB000D0F4F /* WMI.h in Headers */ = {isa = PBXBuildFile; fileRef = D49DC3731FB341EB000D0F4F /* WMI.h */; }; +/* End PBXBuildFile section */ + +/* Begin PBXFileReference section */ + 84F11DE71A588B8A003DC874 /* makefile */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.make; path = makefile; sourceTree = ""; usesTabs = 1; }; + 84F11DE81A588B8A003DC874 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = ""; }; + D4096F801A52FCED005C037A /* IOElectrify.kext */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = IOElectrify.kext; sourceTree = BUILT_PRODUCTS_DIR; }; + D4096F851A52FCED005C037A /* IOElectrify.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = IOElectrify.h; sourceTree = ""; }; + D4096F871A52FCED005C037A /* IOElectrify.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = IOElectrify.cpp; sourceTree = ""; }; + D41D13201FB57F1400412FC6 /* IOElectrifyBridge.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = IOElectrifyBridge.h; sourceTree = ""; }; + D41D13231FB57F7400412FC6 /* IOElectrifyBridge.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = IOElectrifyBridge.cpp; sourceTree = ""; }; + D42AADF51FB21AE300561B9A /* Kernel.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Kernel.framework; path = System/Library/Frameworks/Kernel.framework; sourceTree = SDKROOT; }; + D45DA2621FB21CCB00A92A1E /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + D49DC3721FB341EB000D0F4F /* WMI.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = WMI.cpp; sourceTree = ""; }; + D49DC3731FB341EB000D0F4F /* WMI.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WMI.h; sourceTree = ""; }; + D49DC3761FB34719000D0F4F /* common.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = common.h; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + D4096F7C1A52FCED005C037A /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + D4096F761A52FCEC005C037A = { + isa = PBXGroup; + children = ( + 84F11DE71A588B8A003DC874 /* makefile */, + 84F11DE81A588B8A003DC874 /* README.md */, + D4096F821A52FCED005C037A /* IOElectrify */, + D4096F811A52FCED005C037A /* Products */, + D42AADF41FB21AE300561B9A /* Frameworks */, + ); + sourceTree = ""; + }; + D4096F811A52FCED005C037A /* Products */ = { + isa = PBXGroup; + children = ( + D4096F801A52FCED005C037A /* IOElectrify.kext */, + ); + name = Products; + sourceTree = ""; + }; + D4096F821A52FCED005C037A /* IOElectrify */ = { + isa = PBXGroup; + children = ( + D45DA2611FB21CAF00A92A1E /* Supporting Files */, + D4096F851A52FCED005C037A /* IOElectrify.h */, + D41D13201FB57F1400412FC6 /* IOElectrifyBridge.h */, + D4096F871A52FCED005C037A /* IOElectrify.cpp */, + D49DC3721FB341EB000D0F4F /* WMI.cpp */, + D49DC3731FB341EB000D0F4F /* WMI.h */, + D49DC3761FB34719000D0F4F /* common.h */, + D41D13231FB57F7400412FC6 /* IOElectrifyBridge.cpp */, + ); + path = IOElectrify; + sourceTree = ""; + }; + D42AADF41FB21AE300561B9A /* Frameworks */ = { + isa = PBXGroup; + children = ( + D42AADF51FB21AE300561B9A /* Kernel.framework */, + ); + name = Frameworks; + sourceTree = ""; + }; + D45DA2611FB21CAF00A92A1E /* Supporting Files */ = { + isa = PBXGroup; + children = ( + D45DA2621FB21CCB00A92A1E /* Info.plist */, + ); + path = "Supporting Files"; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXHeadersBuildPhase section */ + D4096F7D1A52FCED005C037A /* Headers */ = { + isa = PBXHeadersBuildPhase; + buildActionMask = 2147483647; + files = ( + D49DC3751FB341EB000D0F4F /* WMI.h in Headers */, + D4096F861A52FCED005C037A /* IOElectrify.h in Headers */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXHeadersBuildPhase section */ + +/* Begin PBXNativeTarget section */ + D4096F7F1A52FCED005C037A /* IOElectrify */ = { + isa = PBXNativeTarget; + buildConfigurationList = D4096F8B1A52FCED005C037A /* Build configuration list for PBXNativeTarget "IOElectrify" */; + buildPhases = ( + D4096F7B1A52FCED005C037A /* Sources */, + D4096F7C1A52FCED005C037A /* Frameworks */, + D4096F7D1A52FCED005C037A /* Headers */, + D4096F7E1A52FCED005C037A /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = IOElectrify; + productName = IOElectrify; + productReference = D4096F801A52FCED005C037A /* IOElectrify.kext */; + productType = "com.apple.product-type.kernel-extension"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + D4096F771A52FCEC005C037A /* Project object */ = { + isa = PBXProject; + attributes = { + LastUpgradeCheck = 0610; + ORGANIZATIONNAME = nobody; + TargetAttributes = { + D4096F7F1A52FCED005C037A = { + CreatedOnToolsVersion = 6.1.1; + }; + }; + }; + buildConfigurationList = D4096F7A1A52FCEC005C037A /* Build configuration list for PBXProject "IOElectrify" */; + compatibilityVersion = "Xcode 3.2"; + developmentRegion = English; + hasScannedForEncodings = 0; + knownRegions = ( + en, + ); + mainGroup = D4096F761A52FCEC005C037A; + productRefGroup = D4096F811A52FCED005C037A /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + D4096F7F1A52FCED005C037A /* IOElectrify */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXResourcesBuildPhase section */ + D4096F7E1A52FCED005C037A /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXResourcesBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + D4096F7B1A52FCED005C037A /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + D4096F881A52FCED005C037A /* IOElectrify.cpp in Sources */, + D49DC3741FB341EB000D0F4F /* WMI.cpp in Sources */, + D41D13241FB57F7400412FC6 /* IOElectrifyBridge.cpp in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + D4096F891A52FCED005C037A /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "LOGNAME=\\\"${LOGNAME}\\\"", + ); + INFOPLIST_FILE = "IOElectrify/Supporting Files/Info.plist"; + MODULE_VERSION = 1.0.0; + PRODUCT_NAME = "$(TARGET_NAME)"; + SDKROOT = macosx10.13; + WRAPPER_EXTENSION = kext; + }; + name = Debug; + }; + D4096F8A1A52FCED005C037A /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + GCC_PREPROCESSOR_DEFINITIONS = "LOGNAME=\\\"${LOGNAME}\\\""; + INFOPLIST_FILE = "IOElectrify/Supporting Files/Info.plist"; + MODULE_VERSION = 1.0.0; + PRODUCT_NAME = "$(TARGET_NAME)"; + SDKROOT = macosx10.13; + WRAPPER_EXTENSION = kext; + }; + name = Release; + }; + D4096F8C1A52FCED005C037A /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + COMBINE_HIDPI_IMAGES = YES; + INFOPLIST_FILE = "IOElectrify/Supporting Files/Info.plist"; + MODULE_NAME = org.darkvoid.driver.IOElectrify; + PRODUCT_NAME = IOElectrify; + WRAPPER_EXTENSION = kext; + }; + name = Debug; + }; + D4096F8D1A52FCED005C037A /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + COMBINE_HIDPI_IMAGES = YES; + INFOPLIST_FILE = "IOElectrify/Supporting Files/Info.plist"; + MODULE_NAME = org.darkvoid.driver.IOElectrify; + PRODUCT_NAME = IOElectrify; + WRAPPER_EXTENSION = kext; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + D4096F7A1A52FCEC005C037A /* Build configuration list for PBXProject "IOElectrify" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + D4096F891A52FCED005C037A /* Debug */, + D4096F8A1A52FCED005C037A /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + D4096F8B1A52FCED005C037A /* Build configuration list for PBXNativeTarget "IOElectrify" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + D4096F8C1A52FCED005C037A /* Debug */, + D4096F8D1A52FCED005C037A /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = D4096F771A52FCEC005C037A /* Project object */; +} diff --git a/IOElectrify.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/IOElectrify.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..9c89823 --- /dev/null +++ b/IOElectrify.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/IOElectrify.xcodeproj/xcshareddata/xcschemes/IOElectrify.xcscheme b/IOElectrify.xcodeproj/xcshareddata/xcschemes/IOElectrify.xcscheme new file mode 100644 index 0000000..904872e --- /dev/null +++ b/IOElectrify.xcodeproj/xcshareddata/xcschemes/IOElectrify.xcscheme @@ -0,0 +1,89 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/IOElectrify/IOElectrify.cpp b/IOElectrify/IOElectrify.cpp new file mode 100644 index 0000000..b7413dc --- /dev/null +++ b/IOElectrify/IOElectrify.cpp @@ -0,0 +1,187 @@ +/* + * Released under "The GNU General Public License (GPL-2.0)" + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#include +#include "IOElectrify.h" +#include "WMI.h" + +#include +extern kmod_info_t kmod_info; + +// define & enumerate power states +enum +{ + kPowerStateSleep = 0, + kPowerStateDoze = 1, + kPowerStateNormal = 2, + kPowerStateCount +}; + +// Define usable power states +static IOPMPowerState powerStateArray[ kPowerStateCount ] = +{ + { 1,0,0,0,0,0,0,0,0,0,0,0 }, + { 1,kIOPMDeviceUsable, kIOPMDoze, kIOPMDoze, 0,0,0,0,0,0,0,0 }, + { 1,kIOPMDeviceUsable, IOPMPowerOn, IOPMPowerOn, 0,0,0,0,0,0,0,0 } +}; + +OSDefineMetaClassAndStructors(IOElectrify, IOService); + +bool IOElectrify::init(OSDictionary *propTable) +{ + DebugLog("IOElectrify::init() %p\n", this); + + // announce version + IOLog("IOElectrify: Version %s starting on OS X Darwin %d.%d.\n", kmod_info.version, version_major, version_minor); + + bool ret = super::init(propTable); + if (!ret) + { + AlwaysLog("super::init returned false\n"); + return false; + } + + // place version/build info in ioreg properties DV,Build and DV,Version + char buf[128]; + snprintf(buf, sizeof(buf), "%s %s", kmod_info.name, kmod_info.version); + setProperty("DV,Version", buf); +#ifdef DEBUG + setProperty("DV,Build", "Debug-darkvoid");// LOGNAME); +#else + setProperty("DV,Build", "Release-darkvoid");// LOGNAME); +#endif + + mProvider = NULL; + + return true; +} + +bool IOElectrify::attach(IOService* provider) +{ + DebugLog("IOElectrify::attach() %s\n", provider->getName()); + + return super::attach(provider); +} + +bool IOElectrify::start(IOService *provider) +{ + DebugLog("IOElectrify::start() %s\n", provider->getName()); + + if (!super::start(provider)) + { + AlwaysLog("super::start returned false\n"); + return false; + } + + bool result = false; + + mWMI = new WMI(provider); + + if (mWMI->initialize()) { + + if (mWMI->hasMethod(INTEL_WMI_THUNDERBOLT_GUID)) { + result = true; + } + } + + if (result) { + // init power state management & set state as PowerOn + PMinit(); + registerPowerDriver(this, powerStateArray, kPowerStateCount); + provider->joinPMtree(this); + } + + return result; +} + +void IOElectrify::stop(IOService *provider) +{ + DebugLog("IOElectrify::stop() %p\n", this); + + PMstop(); + + if (mWMI != NULL) { + delete mWMI; + mWMI = NULL; + } + + super::stop(provider); +} + +void IOElectrify::free() +{ + DebugLog("IOElectrify::free() %p\n", this); + + super::free(); +} + +#ifdef DEBUG +void IOElectrify::detach(IOService *provider) +{ + DebugLog("IOElectrify::detach() %p\n", this); + + return super::detach(provider); +} +#endif + +IOReturn IOElectrify::setPowerState(unsigned long powerState, IOService *service) +{ + DebugLog("setPowerState %ld\n", powerState); + + switch (powerState) + { + case kPowerStateSleep: + DebugLog("--> sleep(%d)\n", (int)powerState); + + if (mWMI->hasMethod(INTEL_WMI_THUNDERBOLT_GUID)) { + + OSObject* params[3] = { + OSNumber::withNumber(0ULL, 32), + OSNumber::withNumber(0ULL, 32), + OSNumber::withNumber(0ULL, 32) + }; + + if (mWMI->executeMethod(INTEL_WMI_THUNDERBOLT_GUID, NULL, params, 3)) { + AlwaysLog("Thunderbolt force-power: OFF.\n"); + } + } + + break; + + case kPowerStateDoze: + case kPowerStateNormal: + DebugLog("--> awake(%d)\n", (int)powerState); + + if (mWMI->hasMethod(INTEL_WMI_THUNDERBOLT_GUID)) { + + OSObject* params[3] = { + OSNumber::withNumber(0ULL, 32), + OSNumber::withNumber(0ULL, 32), + OSNumber::withNumber(1ULL, 32) + }; + + if (mWMI->executeMethod(INTEL_WMI_THUNDERBOLT_GUID, NULL, params, 3)) { + AlwaysLog("Thunderbolt force-power: ON.\n"); + } + } + break; + } + + return IOPMAckImplied; +} diff --git a/IOElectrify/IOElectrify.h b/IOElectrify/IOElectrify.h new file mode 100644 index 0000000..8f48457 --- /dev/null +++ b/IOElectrify/IOElectrify.h @@ -0,0 +1,54 @@ +/* + * Released under "The GNU General Public License (GPL-2.0)" + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#ifndef IOElectrify_h +#define IOElectrify_h + +#include +#include + +#include "common.h" +#include "WMI.h" + +#define INTEL_WMI_THUNDERBOLT_GUID "86ccfd48-205e-4a77-9c48-2021cbede341" + +class IOElectrify : public IOService +{ + OSDeclareDefaultStructors(IOElectrify); + typedef IOService super; + +protected: + IOPCIDevice* mProvider; + WMI* mWMI; + +public: + virtual bool init(OSDictionary *propTable); + virtual bool attach(IOService *provider); + virtual bool start(IOService *provider); + virtual void stop(IOService *provider); + virtual void free(); +#ifdef DEBUG + virtual void detach(IOService *provider); +#endif + + virtual IOReturn setPowerState(unsigned long powerState, IOService *service); +}; + + +#endif diff --git a/IOElectrify/IOElectrifyBridge.cpp b/IOElectrify/IOElectrifyBridge.cpp new file mode 100644 index 0000000..75fb1b8 --- /dev/null +++ b/IOElectrify/IOElectrifyBridge.cpp @@ -0,0 +1,122 @@ +/* + * Released under "The GNU General Public License (GPL-2.0)" + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#include +#include "IOElectrifyBridge.h" + +#include +extern kmod_info_t kmod_info; + +OSDefineMetaClassAndStructors(IOElectrifyBridge, IOService); + +bool IOElectrifyBridge::init(OSDictionary *propTable) +{ + DebugLog("IOElectrifyBridge::init() %p\n", this); + + // announce version + IOLog("IOElectrifyBridge: Version %s starting on OS X Darwin %d.%d.\n", kmod_info.version, version_major, version_minor); + + bool ret = super::init(propTable); + if (!ret) + { + AlwaysLog("super::init returned false\n"); + return false; + } + + // place version/build info in ioreg properties DV,Build and DV,Version + char buf[128]; + snprintf(buf, sizeof(buf), "%s %s", kmod_info.name, kmod_info.version); + setProperty("DV,Version", buf); +#ifdef DEBUG + setProperty("DV,Build", "Debug-darkvoid");// LOGNAME); +#else + setProperty("DV,Build", "Release-darkvoid");// LOGNAME); +#endif + + mProvider = NULL; + + return true; +} + +bool IOElectrifyBridge::attach(IOService* provider) +{ + DebugLog("IOElectrifyBridge::attach() %s\n", provider->getName()); + + mProvider = OSDynamicCast(IOPCI2PCIBridge, provider); + + if (mProvider == NULL) { + return false; + } + + DebugLog("Provider -> Provider %s\n", mProvider->getProvider()->getName()); + + if (strcmp(mProvider->getProvider()->getName(), "RP01")) { + DebugLog("Mismatch %s vs RP01\n", mProvider->getProvider()->getName()); + return false; + } + + return super::attach(provider); +} + +bool IOElectrifyBridge::start(IOService *provider) +{ + DebugLog("IOElectrifyBridge::start() %s\n", provider->getName()); + + if (!super::start(provider)) + { + AlwaysLog("super::start returned false\n"); + return false; + } + + SInt32 score = 0; + + //mProvider->probe(mProvider, &score); + + //DebugLog("Probe score: %d", score); + + IOOptionBits options = 0; + + mProvider->requestProbe(options); + + return true; +} + +void IOElectrifyBridge::stop(IOService *provider) +{ + DebugLog("IOElectrifyBridge::stop() %p\n", this); + + super::stop(provider); +} + +void IOElectrifyBridge::free() +{ + DebugLog("IOElectrifyBridge::free() %p\n", this); + + super::free(); +} + +#ifdef DEBUG +void IOElectrifyBridge::detach(IOService *provider) +{ + DebugLog("IOElectrifyBridge::detach() %p\n", this); + + return super::detach(provider); +} +#endif + diff --git a/IOElectrify/IOElectrifyBridge.h b/IOElectrify/IOElectrifyBridge.h new file mode 100644 index 0000000..f540c29 --- /dev/null +++ b/IOElectrify/IOElectrifyBridge.h @@ -0,0 +1,55 @@ +/* + * Released under "The GNU General Public License (GPL-2.0)" + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#ifndef IOElectrifyBridge_h +#define IOElectrifyBridge_h + +#include +#include +#include + +#ifdef DEBUG +#define DebugLog(args...) do { IOLog("IOElectrifyBridge: " args); } while (0) +#else +#define DebugLog(args...) do { } while (0) +#endif +#define AlwaysLog(args...) do { IOLog("IOElectrifyBridge: " args); } while (0) + +class IOElectrifyBridge : public IOService +{ + OSDeclareDefaultStructors(IOElectrifyBridge); + typedef IOService super; + +protected: + IOPCI2PCIBridge* mProvider; + +public: + virtual bool init(OSDictionary *propTable); + virtual bool attach(IOService *provider); + virtual bool start(IOService *provider); + virtual void stop(IOService *provider); + virtual void free(); +#ifdef DEBUG + virtual void detach(IOService *provider); +#endif +}; + + +#endif + diff --git a/IOElectrify/Supporting Files/Info.plist b/IOElectrify/Supporting Files/Info.plist new file mode 100644 index 0000000..78a0e0f --- /dev/null +++ b/IOElectrify/Supporting Files/Info.plist @@ -0,0 +1,88 @@ + + + + + Source Code + https://github.com/the-darkvoid/OS-X-IOElectrify + CFBundleDevelopmentRegion + en + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIdentifier + org.darkvoid.driver.IOElectrify + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + IOElectrify + CFBundlePackageType + KEXT + CFBundleShortVersionString + $(MODULE_VERSION) + CFBundleSignature + ???? + CFBundleVersion + $(MODULE_VERSION) + IOKitPersonalities + + IOElectrify + + CFBundleIdentifier + org.darkvoid.driver.IOElectrify + IOClass + IOElectrify + IOProviderClass + IOACPIPlatformDevice + IOProbeScore + 10 + IONameMatch + + PNP0C14 + pnp0c14 + + IOPropertyMatch + + _UID + TBFP + + + IOElectrifyBridge + + #CFBundleIdentifier + org.darkvoid.driver.IOElectrify + #IOClass + IOElectrifyBridge + #IOProviderClass + IOPCI2PCIBridge + #IOProbeScore + 10 + #IOPCIClassMatch + 0x06040000&0xffff0000 + #IOPCIPrimaryMatch + 0x76158086 + #IOMatchCategory + IOElectrify + + + OSBundleCompatibleVersion + $(MODULE_VERSION) + OSBundleLibraries + + com.apple.iokit.IOACPIFamily + 1.0.0d1 + com.apple.iokit.IOPCIFamily + 1.0.0b1 + com.apple.kpi.bsd + 8.0.0 + com.apple.kpi.iokit + 8.0.0 + com.apple.kpi.libkern + 8.0.0 + com.apple.kpi.mach + 8.0.0 + com.apple.kpi.unsupported + 8.0.0 + + OSBundleRequired + Root + + diff --git a/IOElectrify/WMI.cpp b/IOElectrify/WMI.cpp new file mode 100644 index 0000000..6b766da --- /dev/null +++ b/IOElectrify/WMI.cpp @@ -0,0 +1,259 @@ +/* + * Released under "The GNU General Public License (GPL-2.0)" + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Based on Dolnor's IOWMIFamily (https://github.com/Dolnor/IOWMIFamily/) + */ + +#include "WMI.h" +#include + +#define kWMIMethod "_WDG" + +#define kWMIGuid "guid" +#define kWMIObjectId "object-id" +#define kWMINotifyId "notify-id" +#define kWMIInstanceCount "instance-count" +#define kWMIFlags "flags" +#define kWMIFlagsText "flags-text" + +/* + * If the GUID data block is marked as expensive, we must enable and + * explicitily disable data collection. + */ +#define ACPI_WMI_EXPENSIVE 0x1 +#define ACPI_WMI_METHOD 0x2 /* GUID is a method */ +#define ACPI_WMI_STRING 0x4 /* GUID takes & returns a string */ +#define ACPI_WMI_EVENT 0x8 /* GUID is an event */ + +struct __attribute__((packed)) WMI_DATA +{ + uuid_t guid; + union { + char object_id[2]; + struct { + unsigned char notify_id; + unsigned char reserved; + }; + }; + UInt8 instance_count; + UInt8 flags; +}; + +#define WMI_DATA_SIZE sizeof(WMI_DATA) + +// Convert UUID to little endian +void le_uuid_dec(uuid_t *in, uuid_t *out) +{ + *((uint32_t *)out + 0) = OSSwapInt32(*((uint32_t *)in + 0)); + *((uint16_t *)out + 2) = OSSwapInt16(*((uint16_t *)in + 2)); + *((uint16_t *)out + 3) = OSSwapInt16(*((uint16_t *)in + 3)); + *((uint64_t *)out + 1) = (*((uint64_t *)in + 1)); +} + +// parseWMIFlags - Parse WMI flags to a string +OSString * parseWMIFlags(UInt8 flags) +{ + char output[80]; + + memset(&output, 0, sizeof(output)); + + if (flags != 0) + { + if (flags & ACPI_WMI_EXPENSIVE) + { + strcat(output, "ACPI_WMI_EXPENSIVE "); + } + + if (flags & ACPI_WMI_METHOD) + { + strcat(output, "ACPI_WMI_METHOD "); + } + + if (flags & ACPI_WMI_STRING) + { + strcat(output, "ACPI_WMI_STRING "); + } + + if (flags & ACPI_WMI_EVENT) + { + strcat(output, "ACPI_WMI_EVENT "); + } + } + else + { + strcat(output, "NONE"); + } + + return (OSString::withCString(output)); +} + +WMI::WMI(IOService* provider) +{ + mDevice = OSDynamicCast(IOACPIPlatformDevice, provider); +} + +bool WMI::initialize() +{ + if (mDevice != NULL) { + mData = OSArray::withCapacity(0); + + if (extractData()) { + return true; + } + + AlwaysLog("WMI method %s not found on object %s\n", kWMIMethod, mDevice->getName()); + } +} + +WMI::~WMI() +{ + if (mData != NULL) { + mData->release(); + } +} + +// Parse the _WDG method output for WMI data blocks +bool WMI::extractData() +{ + OSObject *wdg; + OSData *data; + + if (mDevice->evaluateObject(kWMIMethod, &wdg) != kIOReturnSuccess) + { + AlwaysLog("ACPI object %s does not export _WDG data\n", mDevice->getName()); + return false; + } + + data = OSDynamicCast(OSData, wdg); + + if (data == NULL) { + AlwaysLog("%s:_WDG did not return a data blob\n", mDevice->getName()); + return false; + } + + int count = data->getLength() / WMI_DATA_SIZE; + + for (int i = 0; i < count; i++) { + parseWDGEntry( + (struct WMI_DATA*)data->getBytesNoCopy(i * WMI_DATA_SIZE, WMI_DATA_SIZE)); + } + + mDevice->setProperty("WDG", mData); + + data->release(); + + return true; +} + +// Parse WDG datablock into an OSArray +void WMI::parseWDGEntry(struct WMI_DATA* block) +{ + char guid_string[37]; + char object_id_string[3]; + OSDictionary *dict = OSDictionary::withCapacity(6); + + uuid_t hostUUID; + + le_uuid_dec(&block->guid, &hostUUID); + uuid_unparse_lower(hostUUID, guid_string); + + dict->setObject(kWMIGuid, OSString::withCString(guid_string)); + + if (block->flags & ACPI_WMI_EVENT) + dict->setObject(kWMINotifyId, OSNumber::withNumber(block->notify_id, 8)); + else + { + snprintf(object_id_string, 3, "%c%c", block->object_id[0], block->object_id[1]); + dict->setObject(kWMIObjectId, OSString::withCString(object_id_string)); + } + + dict->setObject(kWMIInstanceCount, OSNumber::withNumber(block->instance_count, 8)); + dict->setObject(kWMIFlags, OSNumber::withNumber(block->flags, 8)); + +#ifdef DEBUG + dict->setObject(kWMIFlagsText, parseWMIFlags(block->flags)); +#endif + + mData->setObject(dict); +} + +OSDictionary* WMI::getMethod(const char * guid) +{ + if (mData && mData->getCount() > 0) + { + if (OSCollectionIterator* iterator = OSCollectionIterator::withCollection(mData)) + { + OSDictionary* entry; + + while ((entry = OSDynamicCast(OSDictionary, iterator->getNextObject()))) + { + OSString * methodGUID = OSDynamicCast(OSString, entry->getObject(kWMIGuid)); + OSNumber * flags = OSDynamicCast(OSNumber, entry->getObject(kWMIFlags)); + + if (methodGUID && flags + && flags->unsigned32BitValue() & ACPI_WMI_METHOD + && strncmp(methodGUID->getCStringNoCopy(), guid, strlen(guid)) == 0) { + return entry; + } + } + iterator->release(); + } + } + + return NULL; +} + +bool WMI::hasMethod(const char * guid) +{ + OSDictionary* method = getMethod(guid); + + if (method != NULL) { + OSString * methodId = OSDynamicCast(OSString, method->getObject(kWMIObjectId)); + + if (methodId != NULL) { + DebugLog("found method %s with guid %s\n", methodId->getCStringNoCopy(), guid); + return true; + } + } + + return false; +} + +bool WMI::executeMethod(const char * guid, OSObject ** result, OSObject * params[], IOItemCount paramCount) +{ + char methodName[16]; + OSDictionary* method = getMethod(guid); + + if (method != NULL) + { + OSString * methodId = OSDynamicCast(OSString, method->getObject(kWMIObjectId)); + + if (methodId != NULL) { + memset(&methodName, 0, sizeof(methodName)); + + strcat(methodName, "WM"); + strcat(methodName, methodId->getCStringNoCopy()); + + DebugLog("Calling method %s\n", methodName); + mDevice->evaluateObject(methodName, result, params, paramCount); + + return true; + } + } + + return false; +} diff --git a/IOElectrify/WMI.h b/IOElectrify/WMI.h new file mode 100644 index 0000000..fe12198 --- /dev/null +++ b/IOElectrify/WMI.h @@ -0,0 +1,53 @@ +/* + * Released under "The GNU General Public License (GPL-2.0)" + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#ifndef WMI_h +#define WMI_h + +#include "common.h" + +#include +#include + +class WMI +{ + IOACPIPlatformDevice* mDevice = NULL; + OSArray* mData = NULL; + +public: + // Constructor + WMI(IOService *provider); + // Destructor + ~WMI(); + + bool initialize(); + bool hasMethod(const char * guid); + bool executeMethod(const char * guid, OSObject ** result = NULL, OSObject * params[] = NULL, IOItemCount paramCount = NULL); + + inline IOACPIPlatformDevice* getACPIDevice() { return mDevice; } + +private: + bool extractData(); + void parseWDGEntry(struct WMI_DATA * block); + + OSDictionary* getMethod(const char * guid); +}; + + +#endif /* WMI_h */ diff --git a/IOElectrify/common.h b/IOElectrify/common.h new file mode 100644 index 0000000..8031eeb --- /dev/null +++ b/IOElectrify/common.h @@ -0,0 +1,30 @@ +/* + * Released under "The GNU General Public License (GPL-2.0)" + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#ifndef common_h +#define common_h + +#ifdef DEBUG +#define DebugLog(args...) do { IOLog("IOElectrify: " args); } while (0) +#else +#define DebugLog(args...) do { } while (0) +#endif +#define AlwaysLog(args...) do { IOLog("IOElectrify: " args); } while (0) + +#endif /* common_h */ diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d6a9326 --- /dev/null +++ b/LICENSE @@ -0,0 +1,340 @@ +GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Lesser General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + {description} + Copyright (C) {year} {fullname} + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + {signature of Ty Coon}, 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. + diff --git a/makefile b/makefile new file mode 100644 index 0000000..94667e1 --- /dev/null +++ b/makefile @@ -0,0 +1,67 @@ +KEXT=IOElectrify.kext +DIST=darkvoid-IOElectrify +BUILDDIR=./Build/Products + +VERSION_ERA=$(shell ./print_version.sh) +ifeq "$(VERSION_ERA)" "10.10-" + INSTDIR=/System/Library/Extensions +else + INSTDIR=/Library/Extensions +endif + +ifeq ($(findstring 32,$(BITS)),32) +OPTIONS:=$(OPTIONS) -arch i386 +endif + +ifeq ($(findstring 64,$(BITS)),64) +OPTIONS:=$(OPTIONS) -arch x86_64 +endif + +OPTIONS:=$(OPTIONS) -scheme IOElectrify + +.PHONY: all +all: + xcodebuild build $(OPTIONS) -configuration Debug + xcodebuild build $(OPTIONS) -configuration Release + +.PHONY: clean +clean: + xcodebuild clean $(OPTIONS) -configuration Debug + xcodebuild clean $(OPTIONS) -configuration Release + +.PHONY: update_kernelcache +update_kernelcache: + sudo touch /System/Library/Extensions + sudo kextcache -update-volume / + +.PHONY: test +test: + if kextstat | grep --quiet IOElectrify; then sudo kextunload /tmp/$(KEXT); fi + sudo rm -Rf /tmp/$(KEXT) + sudo cp -R $(BUILDDIR)/Debug/$(KEXT) /tmp + if [ "`which tag`" != "" ]; then sudo tag -a Purple /tmp/$(KEXT); fi + sudo kextload /tmp/$(KEXT) + +.PHONY: install_debug +install_debug: + sudo rm -Rf $(INSTDIR)/$(KEXT) + sudo cp -R $(BUILDDIR)/Debug/$(KEXT) $(INSTDIR) + if [ "`which tag`" != "" ]; then sudo tag -a Purple $(INSTDIR)/$(KEXT); fi + make update_kernelcache + +.PHONY: install +install: + sudo rm -Rf $(INSTDIR)/$(KEXT) + sudo cp -R $(BUILDDIR)/Release/$(KEXT) $(INSTDIR) + if [ "`which tag`" != "" ]; then sudo tag -a Blue $(INSTDIR)/$(KEXT); fi + make update_kernelcache + +.PHONY: distribute +distribute: + if [ -e ./Distribute ]; then rm -r ./Distribute; fi + mkdir ./Distribute + cp -R $(BUILDDIR)/Debug ./Distribute + cp -R $(BUILDDIR)/Release ./Distribute + find ./Distribute -path *.DS_Store -delete + ditto -c -k --sequesterRsrc --zlibCompressionLevel 9 ./Distribute ./Archive.zip + mv ./Archive.zip ./Distribute/`date +$(DIST)-%Y-%m%d.zip` diff --git a/print_version.sh b/print_version.sh new file mode 100755 index 0000000..5d55635 --- /dev/null +++ b/print_version.sh @@ -0,0 +1,9 @@ +#set -x + +# extract minor version (eg. 10.9 vs. 10.10 vs. 10.11) +MINOR_VER=$([[ "$(sw_vers -productVersion)" =~ [0-9]+\.([0-9]+) ]] && echo ${BASH_REMATCH[1]}) +if [[ $MINOR_VER -ge 11 ]]; then + echo 10.11+ +else + echo 10.10- +fi diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..cfb9004 --- /dev/null +++ b/readme.md @@ -0,0 +1,32 @@ +# Introduction + +IOElectrify is a macOS kext which enables always-on power to Intel Thunderbolt hardware. + +## In Depth + +macOS expects the Intel Thunderbolt controller to be always powered on. +This opposed to the Windows implementation where Thunderbolt devices are configured through the BIOS. + +Most ACPI implementations on Windows machines expose a Thunderbolt `force-power` method. + +This method is published using the [Windows Management Instrumentation ACPI interface](https://msdn.microsoft.com/en-us/library/windows/hardware/dn614028(v=vs.85).aspx). + +macOS does not support Windows Management Instrumentation out of the box, this is where IOElectrify comes in. + +IOElectrify attaches to the `WTBT` ACPI object and determines the method with [guid](https://patchwork.kernel.org/patch/9942739/) `86ccfd48-205e-4a77-9c48-2021cbede341`. + +For both the awake and sleep events, IOElectrify will respectively activate and de-activate the Intel Thunderbolt `force-power` setting. + +## Configuration + +IOElectrify attaches to ACPI identity `PNP0C14` with an `_UID` of `TBFP` by default. +This can be modified in the `Info.plist` as required. + +## Tested + +* Dell XPS 9360 - Alpine Ridge 2C `8086:1716` + +## Credits + +* [goodwin_c](https://www.tonymacx86.com/threads/usb-c-hotplug-not-working.223534/page-5#post-1580114): Thunderbolt force-power method +* [IOWMIFamily by dolnor](https://github.com/Dolnor/IOWMIFamily): macOS WMI interface \ No newline at end of file