Skip to content

Commit

Permalink
Retrieve Configuration for I2C devices from ACPI (#156)
Browse files Browse the repository at this point in the history
Co-authored-by: zhen-zen <66577170+zhen-zen@users.noreply.github.com>
  • Loading branch information
1Revenger1 and zhen-zen authored Jan 24, 2023
1 parent 9f8f359 commit 372267e
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 5 deletions.
7 changes: 7 additions & 0 deletions VoodooRMI/RMIBus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ RMIBus * RMIBus::probe(IOService *provider, SInt32 *score) {

bool RMIBus::start(IOService *provider) {
int retval;
OSDictionary *config;

if (!super::start(provider))
return false;
Expand All @@ -94,6 +95,12 @@ bool RMIBus::start(IOService *provider) {
if (!transport->open(this))
return false;

config = transport->createConfig();
if (config != nullptr) {
updateConfiguration(config);
OSSafeReleaseNULL(config);
}

registerService();
return true;
err:
Expand Down
24 changes: 23 additions & 1 deletion VoodooRMI/Transports/I2C/RMII2C.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/

#include "RMII2C.hpp"
#include "Configuration.hpp"

OSDefineMetaClassAndStructors(RMII2C, RMITransport)

Expand Down Expand Up @@ -152,7 +153,6 @@ void RMII2C::releaseResources() {
OSSafeReleaseNULL(interrupt_source);
OSSafeReleaseNULL(interrupt_simulator);
OSSafeReleaseNULL(work_loop);
OSSafeReleaseNULL(acpi_device);

IOLockFree(page_mutex);
}
Expand Down Expand Up @@ -461,3 +461,25 @@ void RMII2C::stopInterrupt() {
interrupt_source->disable();
}
}

OSDictionary *RMII2C::createConfig() {
OSDictionary *config = nullptr;
OSArray *acpiArray = nullptr;
OSObject *acpiReturn = nullptr;

IOACPIPlatformDevice *acpi_device = OSDynamicCast(IOACPIPlatformDevice, device_nub->getProperty("acpi-device"));
if (!acpi_device) {
IOLogError("%s::%s Could not retrieve acpi device", getName(), name);
return nullptr;
};

if (acpi_device->evaluateObject("RCFG", &acpiReturn) != kIOReturnSuccess) {
return nullptr;
}

acpiArray = OSDynamicCast(OSArray, acpiReturn);
config = Configuration::mapArrayToDict(acpiArray);
OSSafeReleaseNULL(acpiReturn);

return config;
}
2 changes: 1 addition & 1 deletion VoodooRMI/Transports/I2C/RMII2C.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ class RMII2C : public RMITransport {
int reset() APPLE_KEXT_OVERRIDE;
int readBlock(u16 rmiaddr, u8 *databuff, size_t len) APPLE_KEXT_OVERRIDE;
int blockWrite(u16 rmiaddr, u8 *buf, size_t len) APPLE_KEXT_OVERRIDE;
OSDictionary *createConfig() APPLE_KEXT_OVERRIDE;

private:
bool ready {false};
unsigned long currentPowerState {1};
int page {0};
int reportMode {RMI_MODE_NO_PACKED_ATTN_REPORTS};

IOACPIPlatformDevice *acpi_device {nullptr};
VoodooI2CDeviceNub *device_nub {nullptr};

IOLock *page_mutex {nullptr};
Expand Down
8 changes: 5 additions & 3 deletions VoodooRMI/Transports/RMITransport.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ class RMITransport : public IOService {

public:
// rmi_read_block
virtual int readBlock(u16 rmiaddr, u8 *databuff, size_t len) {return -1;};
virtual int readBlock(u16 rmiaddr, u8 *databuff, size_t len) { return -1; };
// rmi_block_write
virtual int blockWrite(u16 rmiaddr, u8 *buf, size_t len) {return -1;};
virtual int blockWrite(u16 rmiaddr, u8 *buf, size_t len) { return -1; };

virtual int reset() {return 0;};
virtual int reset() { return 0; };

virtual OSDictionary *createConfig() { return nullptr; };

/*
* IMPORTANT: These handleClose/handleOpen must be called. These can be overriden,
* but said implementation must call the ones below.
Expand Down
18 changes: 18 additions & 0 deletions VoodooRMI/Utility/Configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,21 @@ bool Configuration::loadUInt8Configuration(OSDictionary *dict, const char* confi
*defaultValue = value->unsigned8BitValue();
return true;
}

OSDictionary *Configuration::mapArrayToDict(OSArray *arr) {
if (!arr)
return nullptr;

int pairs = arr->getCount() / 2;
OSDictionary *dict = OSDictionary::withCapacity(pairs);

for (int index = 0; index < pairs; index ++) {
if (OSString *key = OSDynamicCast(OSString, arr->getObject(index * 2))) {
dict->setObject(key, arr->getObject(index * 2 + 1));
} else {
break;
}
}

return dict;
}
1 change: 1 addition & 0 deletions VoodooRMI/Utility/Configuration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class Configuration {
static bool loadUInt8Configuration(OSDictionary *dict, const char* configurationKey, UInt8 *defaultValue);
static bool loadUInt32Configuration(OSDictionary *dict, const char *configurationKey, UInt32 *defaultValue);
static bool loadUInt64Configuration(OSDictionary *dict, const char* configurationKey, UInt64 *defaultValue);
static OSDictionary *mapArrayToDict(OSArray *arr);

private:
Configuration() {}
Expand Down

0 comments on commit 372267e

Please sign in to comment.