Skip to content

Commit

Permalink
Pass I2C HID input report and timestamp to functions
Browse files Browse the repository at this point in the history
  • Loading branch information
1Revenger1 committed Oct 18, 2024
1 parent 12384ef commit 5be336b
Show file tree
Hide file tree
Showing 22 changed files with 184 additions and 143 deletions.
9 changes: 2 additions & 7 deletions VoodooRMI/Functions/F01.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,17 +373,12 @@ int F01::rmi_f01_resume()
return error;
}

void F01::attention()
void F01::attention([[maybe_unused]] AbsoluteTime time, UInt8 *data[], size_t *size)
{
int error;
UInt8 device_status = 0;

error = readByte(getDataAddr(), &device_status);

if (error) {
IOLogError("F01: Failed to read device status: %d", error);
if (!getInputData(&device_status, sizeof(device_status), data, size))
return;
}

if (RMI_F01_STATUS_BOOTLOADER(device_status))
IOLogError("Device in bootloader mode, please update firmware");
Expand Down
2 changes: 1 addition & 1 deletion VoodooRMI/Functions/F01.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class F01 : public RMIFunction {
public:
bool attach(IOService *provider) override;
IOReturn config() override;
void attention() override;
void attention(AbsoluteTime time, UInt8 *data[], size_t *size) override;

IOReturn setPowerState(unsigned long powerStateOrdinal, IOService *whatDevice) override;

Expand Down
24 changes: 10 additions & 14 deletions VoodooRMI/Functions/F03.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ int F03::rmi_f03_pt_write(unsigned char val)
return error;
}

void F03::handlePacket(UInt8 *packet)
void F03::handlePacket(AbsoluteTime time, UInt8 *packet)
{
RMITrackpointReport report;
// Trackpoint isn't initialized!
Expand All @@ -172,6 +172,7 @@ void F03::handlePacket(UInt8 *packet)
timer->enable();
}

report.timestamp = time;
report.buttons = (packet[0] & 0x7);
report.dx = ((packet[0] & 0x10) ? 0xffffff00 : 0) | packet[1];
report.dy = -(((packet[0] & 0x20) ? 0xffffff00 : 0) | packet[2]);
Expand Down Expand Up @@ -199,26 +200,21 @@ IOReturn F03::setPowerState(unsigned long powerStateOrdinal, IOService *whatDevi
return kIOPMAckImplied;
}

void F03::attention()
void F03::attention(AbsoluteTime time, UInt8 *data[], size_t *size)
{
const UInt16 data_addr = getDataAddr() + RMI_F03_OB_OFFSET;
const UInt8 ob_len = rx_queue_length * RMI_F03_OB_SIZE;
UInt8 obs[RMI_F03_QUEUE_LENGTH * RMI_F03_OB_SIZE];
const UInt8 ob_len = RMI_F03_OB_OFFSET + (rx_queue_length * RMI_F03_OB_SIZE);
UInt8 obs[RMI_F03_OB_OFFSET + (RMI_F03_QUEUE_LENGTH * RMI_F03_OB_SIZE)];

int error = readBlock(data_addr, obs, ob_len);
if (error) {
IOLogError("F03 - Failed to read output buffers: %d", error);
if (!getInputData(obs, ob_len, data, size))
return;
}

for (int i = 0; i < ob_len; i += RMI_F03_OB_SIZE) {
for (int i = RMI_F03_OB_OFFSET; i < ob_len; i += RMI_F03_OB_SIZE) {
UInt8 ob_status = obs[i];
UInt8 ob_data = obs[i + RMI_F03_OB_DATA_OFFSET];

if (!(ob_status & RMI_F03_RX_DATA_OFB))
continue;


IOLogDebug("F03 - Recieved data over PS2: %x", ob_data);
if (ob_status & RMI_F03_OB_FLAG_TIMEOUT) {
IOLogDebug("F03 Timeout Flag");
Expand All @@ -229,7 +225,7 @@ void F03::attention()
continue;
}

handleByte(ob_data);
handleByte(time, ob_data);
}
}

Expand Down Expand Up @@ -296,7 +292,7 @@ void F03::initPS2Interrupt(OSObject *owner, IOTimerEventSource *timer)
timer->disable();
}

void F03::handleByte(UInt8 byte)
void F03::handleByte(AbsoluteTime time, UInt8 byte)
{
if (!cmdcnt && !flags) {
// Wait for start of packets
Expand All @@ -306,7 +302,7 @@ void F03::handleByte(UInt8 byte)
databuf[index++] = byte;

if (index == 3)
handlePacket(databuf);
handlePacket(time, databuf);
return;
}

Expand Down
6 changes: 3 additions & 3 deletions VoodooRMI/Functions/F03.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class F03 : public RMITrackpointFunction {
bool start(IOService *provider) override;
void stop(IOService *provider) override;
IOReturn setPowerState(unsigned long powerStateOrdinal, IOService *whatDevice) override;
void attention() override;
void attention(AbsoluteTime time, UInt8 *data[], size_t *size) override;

private:
IOWorkLoop *work_loop {nullptr};
Expand Down Expand Up @@ -53,11 +53,11 @@ class F03 : public RMITrackpointFunction {
int ps2DoSendbyteGated(UInt8 byte, uint64_t timeout);
int ps2CommandGated(UInt8 *param, unsigned int *command);
int ps2Command(UInt8 *param, unsigned int command);
void handleByte(UInt8);
void handleByte(AbsoluteTime time, UInt8);
void initPS2();
void initPS2Interrupt(OSObject *owner, IOTimerEventSource *timer);

void handlePacket(UInt8 *packet);
void handlePacket(AbsoluteTime time, UInt8 *packet);
};

#endif /* F03_hpp */
29 changes: 19 additions & 10 deletions VoodooRMI/Functions/F11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,31 @@ void F11::free()
super::free();
}

void F11::attention()
void F11::attention(AbsoluteTime time, UInt8 *data[], size_t *size)
{
int error, abs_size;
size_t fingers;
UInt8 finger_state;
AbsoluteTime timestamp;

error = readBlock(getDataAddr(), data_pkt, pkt_size);
if (error < 0) {
IOLogError("Could not read F11 attention data: %d", error);
return;
if (data) {
if (*size < attn_size) {
IOLogError("F11 attention larger than remaining data");
return;
}

memcpy(data_pkt, *data, attn_size);
(*data) += attn_size;
(*size) -= attn_size;
} else {
error = readBlock(getDataAddr(), data_pkt, pkt_size);

if (error < 0) {
IOLogError("F11 Could not read attention data: %d", error);
return;
}
}

clock_get_uptime(&timestamp);

if (shouldDiscardReport(timestamp))
if (shouldDiscardReport(time))
return;

IOLogDebug("F11 Packet");
Expand Down Expand Up @@ -94,7 +103,7 @@ void F11::attention()
}
}

report.timestamp = timestamp;
report.timestamp = time;
report.fingers = fingers;

handleReport(&report);
Expand Down
2 changes: 1 addition & 1 deletion VoodooRMI/Functions/F11.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ class F11 : public RMITrackpadFunction {

public:
bool attach(IOService *provider) override;
void attention() override;
void attention(AbsoluteTime time, UInt8 *data[], size_t *size) override;
void free() override;

IOReturn config() override;
Expand Down
44 changes: 26 additions & 18 deletions VoodooRMI/Functions/F12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,22 +267,30 @@ int F12::rmi_f12_read_sensor_tuning()
return 0;
}

void F12::attention()
void F12::attention(AbsoluteTime time, UInt8 *data[], size_t *size)
{
AbsoluteTime timestamp;

if (!data1)
return;

int retval = readBlock(getDataAddr(), data_pkt, pkt_size);

if (retval < 0) {
IOLogError("F12 - Failed to read object data. Code: %d", retval);
return;
if (data) {
if (*size < attn_size) {
IOLogError("F12 attention larger than remaining data");
return;
}

memcpy(data_pkt, *data, attn_size);
(*data) += attn_size;
(*size) -= attn_size;
} else {
IOReturn error = readBlock(getDataAddr(), data_pkt, pkt_size);

if (error < 0) {
IOLogError("F12 Could not read attention data: %d", error);
return;
}
}

clock_get_uptime(&timestamp);
if (shouldDiscardReport(timestamp))
if (shouldDiscardReport(time))
return;

IOLogDebug("F12 Packet");
Expand All @@ -293,12 +301,12 @@ void F12::attention()
#endif // debug

int fingers = min (nbr_fingers, 5);
UInt8 *data = &data_pkt[data1_offset];
UInt8 *fingerData = &data_pkt[data1_offset];

for (int i = 0; i < fingers; i++) {
rmi_2d_sensor_abs_object *obj = &report.objs[i];

switch (data[0]) {
switch (fingerData[0]) {
case RMI_F12_OBJECT_FINGER:
obj->type = RMI_2D_OBJECT_FINGER;
break;
Expand All @@ -309,16 +317,16 @@ void F12::attention()
obj->type = RMI_2D_OBJECT_NONE;
}

obj->x = (data[2] << 8) | data[1];
obj->y = (data[4] << 8) | data[3];
obj->z = data[5];
obj->wx = data[6];
obj->wy = data[7];
obj->x = (fingerData[2] << 8) | fingerData[1];
obj->y = (fingerData[4] << 8) | fingerData[3];
obj->z = fingerData[5];
obj->wx = fingerData[6];
obj->wy = fingerData[7];

data += F12_DATA1_BYTES_PER_OBJ;
}

report.timestamp = timestamp;
report.timestamp = time;
report.fingers = fingers;

handleReport(&report);
Expand Down
2 changes: 1 addition & 1 deletion VoodooRMI/Functions/F12.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class F12 : public RMITrackpadFunction {

public:
bool attach(IOService *provider) override;
void attention() override;
void attention(AbsoluteTime time, UInt8 *data[], size_t *size) override;
void free() override;

IOReturn config() override;
Expand Down
6 changes: 3 additions & 3 deletions VoodooRMI/Functions/F17.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void F17::free()
super::free();
}

void F17::attention()
void F17::attention(AbsoluteTime time, UInt8 *data[], size_t *size)
{
int retval = 0;
for (int i = 0; i < f17.query.number_of_sticks + 1 && !retval; i++)
Expand Down Expand Up @@ -244,8 +244,8 @@ int F17::rmi_f17_process_stick(struct rmi_f17_stick_data *stick) {
} else {
IOLogDebug("%s: Reporting dx: %d, dy: %d\n", __func__, stick->data.rel.x_delta, stick->data.rel.y_delta);

report.dx = (SInt32)((SInt64)stick->data.rel.x_delta * conf.trackpointMult / DEFAULT_MULT);
report.dy = -(SInt32)((SInt64)stick->data.rel.y_delta * conf.trackpointMult / DEFAULT_MULT);
report.dx = (SInt32)((SInt64)stick->data.rel.x_delta;
report.dy = -(SInt32)((SInt64)stick->data.rel.y_delta;
report.buttons = 0;

handleReport(&report);
Expand Down
2 changes: 1 addition & 1 deletion VoodooRMI/Functions/F17.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class F17 : public RMITrackpointFunction {
public:
bool attach(IOService *provider) override;
void free() override;
void attention() override;
void attention(AbsoluteTime time, UInt8 *data[], size_t *size) override;

IOReturn config() override;
private:
Expand Down
33 changes: 33 additions & 0 deletions VoodooRMI/Functions/Input/RMIFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

#include "RMIFunction.hpp"
#include "RMILogging.h"

OSDefineMetaClassAndStructors(RMIFunction, IOService)

Expand Down Expand Up @@ -39,3 +40,35 @@ bool RMIFunction::start(IOService *provider) {
registerService();
return true;
}

bool RMIFunction::getInputData(UInt8 dest[], size_t destSize, UInt8 *srcData[], size_t *srcSize) {
if (srcData) {
if (*srcSize < destSize) {
IOLogError("%s Attention size smaller than expected", getName());
return false;
}

memcpy(dest, *srcData, destSize);
(*srcData) += destSize;
(*srcSize) -= destSize;
return true;
}

if (destSize == 1) {
IOReturn error = readByte(getDataAddr(), dest);

if (error) {
IOLogError("%s Failed to read device status: %d", getName(), error);
return false;
}
} else {
IOReturn error = readBlock(getDataAddr(), dest, destSize);

if (error) {
IOLogError("%s Failed to read block data: %d", getName(), error);
return false;
}
}

return true;
}
4 changes: 3 additions & 1 deletion VoodooRMI/Functions/Input/RMIFunction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,15 @@ class RMIFunction : public IOService {
virtual IOReturn config() { return kIOReturnSuccess; };
// Attention is called whenever this function has data. Any input data
// should be read here.
virtual void attention() { };
virtual void attention(AbsoluteTime time, UInt8 *data[], size_t *size) { };
private:
RmiPdtEntry pdtEntry;
RMIBus *bus {nullptr};

protected:

bool getInputData(UInt8 dest[], size_t destSize, UInt8 *srcData[], size_t *srcSize);

// Useful functions to talk to RMI4 devicce
inline void sendVoodooInputPacket(UInt32 msg, void *packet) {
IOService *vi = bus->getVoodooInput();
Expand Down
Loading

0 comments on commit 5be336b

Please sign in to comment.