Skip to content

Commit 09cfd8d

Browse files
committed
EcCommunication: Add support for host commands
Copy from FrameworkArgb 3a8d21ebae7ee6c0adc579c8e86f3e9f0bb7dfa Signed-off-by: Daniel Schaefer <dhs@frame.work>
1 parent 2449342 commit 09cfd8d

3 files changed

Lines changed: 153 additions & 37 deletions

File tree

FrameworkSensors/Device.cpp

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -84,38 +84,6 @@ void AllocateDeviceAtIndex(
8484
}
8585
}
8686

87-
NTSTATUS ConnectToEc(
88-
_In_ WDFDEVICE FxDevice,
89-
_Inout_ HANDLE *Handle
90-
) {
91-
SENSOR_FunctionEnter();
92-
NTSTATUS Status = STATUS_SUCCESS;
93-
94-
UNREFERENCED_PARAMETER(FxDevice);
95-
96-
*Handle = CreateFileW(
97-
LR"(\\.\GLOBALROOT\Device\CrosEC)",
98-
GENERIC_READ | GENERIC_WRITE,
99-
FILE_SHARE_READ | FILE_SHARE_WRITE,
100-
NULL,
101-
OPEN_EXISTING,
102-
FILE_FLAG_OVERLAPPED,
103-
NULL);
104-
105-
if (*Handle == INVALID_HANDLE_VALUE) {
106-
Status = STATUS_INVALID_HANDLE;
107-
TraceError("COMBO %!FUNC! CreateFileW failed %!STATUS!", Status);
108-
goto Exit;
109-
}
110-
111-
Exit:
112-
SENSOR_FunctionExit(Status);
113-
114-
return Status;
115-
}
116-
117-
118-
11987
//------------------------------------------------------------------------------
12088
//
12189
// Function: OnDeviceAdd
@@ -258,7 +226,7 @@ OnPrepareHardware(
258226

259227
SENSOR_FunctionEnter();
260228

261-
Status = ConnectToEc(Device, &Handle);
229+
Status = ConnectToEc(&Handle);
262230
if (!NT_SUCCESS(Status)) {
263231
TraceError("COMBO %!FUNC! ConnectToEc failed %!STATUS!", Status);
264232
goto Exit;

FrameworkSensors/EcCommunication.cpp

Lines changed: 96 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,105 @@
1212
//
1313
// Windows User-Mode Driver Framework (UMDF)
1414

15-
#include "Clients.h"
1615
#include "EcCommunication.h"
1716
#include <windows.h>
1817
#include <wdf.h>
1918

2019
#include "EcCommunication.tmh"
2120

21+
NTSTATUS ConnectToEc(
22+
_Inout_ HANDLE* Handle
23+
) {
24+
NTSTATUS Status = STATUS_SUCCESS;
25+
26+
*Handle = CreateFileW(
27+
LR"(\\.\GLOBALROOT\Device\CrosEC)",
28+
GENERIC_READ | GENERIC_WRITE,
29+
FILE_SHARE_READ | FILE_SHARE_WRITE,
30+
NULL,
31+
OPEN_EXISTING,
32+
FILE_FLAG_OVERLAPPED,
33+
NULL);
34+
35+
if (*Handle == INVALID_HANDLE_VALUE) {
36+
TraceError("%!FUNC! CreateFileW failed %!STATUS!", Status);
37+
return STATUS_INVALID_HANDLE;
38+
}
39+
40+
return Status;
41+
}
42+
43+
int CrosEcSendCommand(
44+
HANDLE Handle,
45+
UINT16 command,
46+
UINT8 version,
47+
LPVOID outdata,
48+
unsigned int outlen,
49+
LPVOID indata,
50+
unsigned int inlen
51+
)
52+
{
53+
NTSTATUS Status = STATUS_SUCCESS;
54+
DWORD retb{};
55+
CROSEC_COMMAND cmd{};
56+
57+
if (Handle == INVALID_HANDLE_VALUE) {
58+
Status = STATUS_INVALID_HANDLE;
59+
TraceError("%!FUNC! Invalid Handle");
60+
return 0;
61+
}
62+
63+
if (outlen > CROS_EC_CMD_MAX_REQUEST || inlen > CROS_EC_CMD_MAX_REQUEST) {
64+
TraceError("%!FUNC! outlen %d or inlen %d too large", outlen, inlen);
65+
return 0;
66+
}
67+
if (outlen == 0) {
68+
TraceError("%!FUNC! outlen is 0");
69+
return 0;
70+
}
71+
if (outdata == nullptr) {
72+
TraceError("%!FUNC! Invalid outdata - NULL");
73+
return 0;
74+
}
75+
76+
cmd.command = command;
77+
cmd.version = version;
78+
cmd.result = 0xFF;
79+
cmd.outlen = outlen;
80+
cmd.inlen = CROS_EC_CMD_MAX_REQUEST - 8; // 8 is the header length
81+
82+
RtlCopyMemory(cmd.data, outdata, outlen);
83+
84+
Status = DeviceIoControl(Handle,
85+
(DWORD) IOCTL_CROSEC_XCMD,
86+
&cmd,
87+
sizeof(cmd),
88+
&cmd,
89+
sizeof(cmd),
90+
&retb,
91+
nullptr);
92+
if (!NT_SUCCESS(Status)) {
93+
TraceError("%!FUNC! ConnectToEc failed %!STATUS!", Status);
94+
return 0;
95+
}
96+
97+
if (cmd.result != EC_RES_SUCCESS) {
98+
TraceError("%!FUNC! Host command failed - EC result %d", cmd.result);
99+
return 0;
100+
}
101+
102+
if (inlen > 0) {
103+
if (indata == nullptr) {
104+
TraceError("%!FUNC! inlen is %d. But indata is NULL", inlen);
105+
return 0;
106+
}
107+
RtlCopyMemory(indata, cmd.data, inlen);
108+
}
109+
110+
return cmd.inlen;
111+
}
112+
113+
22114
int CrosEcReadMemU8(HANDLE Handle, unsigned int offset, UINT8* dest)
23115
{
24116
NTSTATUS Status = STATUS_SUCCESS;
@@ -27,7 +119,7 @@ int CrosEcReadMemU8(HANDLE Handle, unsigned int offset, UINT8* dest)
27119

28120
if (Handle == INVALID_HANDLE_VALUE) {
29121
Status = STATUS_INVALID_HANDLE;
30-
TraceError("COMBO %!FUNC! Invalid Handle");
122+
TraceError("%!FUNC! Invalid Handle");
31123
return 0;
32124
}
33125

@@ -42,11 +134,11 @@ int CrosEcReadMemU8(HANDLE Handle, unsigned int offset, UINT8* dest)
42134
&retb,
43135
nullptr);
44136
if (!NT_SUCCESS(Status)) {
45-
TraceError("COMBO %!FUNC! ConnectToEc failed %!STATUS!", Status);
137+
TraceError("%!FUNC! ConnectToEc failed %!STATUS!", Status);
46138
return 0;
47139
}
48140

49-
TraceInformation("COMBO %!FUNC! Successfully read %d bytes from EC memory at %02x. First one %02x. retb=%d", rm.bytes, rm.offset, rm.buffer[0], retb);
141+
TraceInformation("%!FUNC! Successfully read %d bytes from EC memory at %02x. First one %02x. retb=%d", rm.bytes, rm.offset, rm.buffer[0], retb);
50142
*dest = rm.buffer[0];
51143

52144
return rm.bytes;

FrameworkSensors/EcCommunication.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ extern "C" {
1212
#endif
1313

1414
#include <handleapi.h>
15+
#include <windows.h>
16+
#include <wdf.h>
17+
#include "SensorsTrace.h"
1518

1619
/* Command version mask */
1720
#define EC_VER_MASK(version) (1UL << (version))
@@ -42,12 +45,65 @@ extern "C" {
4245
#define CROSEC_CMD_MAX_RESPONSE 0x100
4346
#define CROSEC_MEMMAP_SIZE 0xFF
4447

48+
NTSTATUS ConnectToEc(
49+
_Inout_ HANDLE* Handle
50+
);
51+
52+
#define EC_CMD_RGBKBD_SET_COLOR 0x013A
53+
#define EC_CMD_RGBKBD 0x013B
54+
55+
#define EC_RES_SUCCESS 0
56+
#define EC_INVALID_COMMAND 1
57+
#define EC_ERROR 2
58+
#define EC_INVALID_PARAMETER 3
59+
#define EC_ACCESS_DENIED 4
60+
#define EC_INVALID_RESPONSE 5
61+
#define EC_INVALID_VERSION 6
62+
#define EC_INVALID_CHECKSUM 7
63+
64+
#define CROS_EC_CMD_MAX_REQUEST (0x100-8)
65+
66+
typedef struct _CROSEC_COMMAND {
67+
UINT32 version;
68+
UINT32 command;
69+
UINT32 outlen;
70+
UINT32 inlen;
71+
UINT32 result;
72+
UINT8 data[CROS_EC_CMD_MAX_REQUEST];
73+
} * PCROSEC_COMMAND, CROSEC_COMMAND;
74+
4575
typedef struct _CROSEC_READMEM {
4676
ULONG offset;
4777
ULONG bytes;
4878
UCHAR buffer[CROSEC_MEMMAP_SIZE];
4979
} * PCROSEC_READMEM, CROSEC_READMEM;
5080

81+
82+
#include <pshpack1.h>
83+
#define CROS_EC_CMD_MAX_KEY_COUNT 64
84+
typedef struct {
85+
UINT8 r;
86+
UINT8 g;
87+
UINT8 b;
88+
} Rgb;
89+
90+
typedef struct {
91+
UINT8 StartKey;
92+
UINT8 Length;
93+
Rgb Colors[CROS_EC_CMD_MAX_KEY_COUNT];
94+
} EC_REQUEST_RGB_KBD_SET_COLOR;
95+
96+
#include <poppack.h>
97+
98+
int CrosEcSendCommand(
99+
HANDLE Handle,
100+
UINT16 command,
101+
UINT8 version,
102+
LPVOID outdata,
103+
unsigned int outlen,
104+
LPVOID indata,
105+
unsigned int inlen
106+
);
51107
int CrosEcReadMemU8(HANDLE Handle, unsigned int offset, UINT8* dest);
52108

53109
#ifdef __cplusplus

0 commit comments

Comments
 (0)