Skip to content

Commit d42ad9f

Browse files
committed
Split EC Communication into separate file
Signed-off-by: Daniel Schaefer <dhs@frame.work>
1 parent 98599e8 commit d42ad9f

7 files changed

Lines changed: 106 additions & 59 deletions

File tree

SensorsComboDriver/AlsClient.cpp

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// Windows User-Mode Driver Framework (WUDF)
1010

1111
#include "Clients.h"
12-
#include <handleapi.h>
12+
#include "EcCommunication.h"
1313

1414
#include "AlsClient.tmh"
1515

@@ -463,41 +463,6 @@ AlsDevice::Initialize(
463463
}
464464

465465

466-
static int CrosEcReadMemU8(HANDLE Handle, unsigned int offset, UINT8* dest)
467-
{
468-
NTSTATUS Status = STATUS_SUCCESS;
469-
DWORD retb{};
470-
CROSEC_READMEM rm{};
471-
472-
if (Handle == INVALID_HANDLE_VALUE) {
473-
Status = STATUS_INVALID_HANDLE;
474-
TraceError("COMBO %!FUNC! Invalid Handle");
475-
return 0;
476-
}
477-
478-
rm.bytes = 0x01;
479-
rm.offset = offset;
480-
Status = DeviceIoControl(Handle,
481-
(DWORD) IOCTL_CROSEC_RDMEM,
482-
&rm,
483-
sizeof(rm),
484-
&rm,
485-
sizeof(rm),
486-
&retb,
487-
nullptr);
488-
if (!NT_SUCCESS(Status)) {
489-
TraceError("COMBO %!FUNC! ConnectToEc failed %!STATUS!", Status);
490-
return 0;
491-
}
492-
493-
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);
494-
*dest = rm.buffer[0];
495-
496-
return rm.bytes;
497-
}
498-
499-
500-
501466
//------------------------------------------------------------------------------
502467
// Function: GetData
503468
//
@@ -523,10 +488,10 @@ AlsDevice::GetData(
523488
SENSOR_FunctionEnter();
524489

525490
UINT8 als[4] = {0};
526-
CrosEcReadMemU8(Handle, 0x80 + 0, &als[0]);
527-
CrosEcReadMemU8(Handle, 0x80 + 1, &als[1]);
528-
CrosEcReadMemU8(Handle, 0x80 + 2, &als[2]);
529-
CrosEcReadMemU8(Handle, 0x80 + 3, &als[3]);
491+
CrosEcReadMemU8(Handle, EC_MEMMAP_ALS + 0, &als[0]);
492+
CrosEcReadMemU8(Handle, EC_MEMMAP_ALS + 1, &als[1]);
493+
CrosEcReadMemU8(Handle, EC_MEMMAP_ALS + 2, &als[2]);
494+
CrosEcReadMemU8(Handle, EC_MEMMAP_ALS + 3, &als[3]);
530495
m_CachedData.Lux = (float) (als[0] + (als[1] << 8) + (als[2] << 16) + (als[3] << 24));
531496
TraceInformation("Read ALS value %02x %02x %02x %02x (%f)\n",
532497
als[0], als[1], als[2], als[3], m_CachedData.Lux);

SensorsComboDriver/Clients.h

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -256,19 +256,3 @@ typedef class _SimpleDeviceOrientationDevice : public _ComboDevice
256256
NTSTATUS UpdateCachedThreshold();
257257

258258
} SimpleDeviceOrientationDevice, *PSimpleDeviceOrientationDevice;
259-
260-
#define FILE_DEVICE_CROS_EMBEDDED_CONTROLLER 0x80EC
261-
262-
#define IOCTL_CROSEC_XCMD \
263-
CTL_CODE(FILE_DEVICE_CROS_EMBEDDED_CONTROLLER, 0x801, METHOD_BUFFERED, FILE_READ_DATA | FILE_WRITE_DATA)
264-
#define IOCTL_CROSEC_RDMEM CTL_CODE(FILE_DEVICE_CROS_EMBEDDED_CONTROLLER, 0x802, METHOD_BUFFERED, FILE_READ_DATA)
265-
266-
#define CROSEC_CMD_MAX_REQUEST 0x100
267-
#define CROSEC_CMD_MAX_RESPONSE 0x100
268-
#define CROSEC_MEMMAP_SIZE 0xFF
269-
270-
typedef struct _CROSEC_READMEM {
271-
ULONG offset;
272-
ULONG bytes;
273-
UCHAR buffer[CROSEC_MEMMAP_SIZE];
274-
} * PCROSEC_READMEM, CROSEC_READMEM;

SensorsComboDriver/Device.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@
1111

1212
#include "Clients.h"
1313
#include "Driver.h"
14+
#include "EcCommunication.h"
1415

1516
#include <new.h>
1617
#include <winnt.h>
1718

1819
#include "Device.tmh"
1920

20-
#define EC_LPC_ADDR_MEMMAP 0xE00
21-
#define EC_MEMMAP_SIZE 255 /* ACPI IO buffer max is 255 bytes */
2221
#define ENABLE_ALS_SENSOR 1
2322
#define ENABLE_ORIENTATION_SENSOR 0
2423
#define ENABLE_ACCEL_SENSOR 0
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
// Only need EC commands to use EC_CMD_MOTION_SENSE_CMD to determine which accel sensors there are and which position they are
3+
// ALl the rest can be done using memory map reads
4+
5+
#include "Clients.h"
6+
#include "EcCommunication.h"
7+
#include <windows.h>
8+
#include <wdf.h>
9+
10+
#include "EcCommunication.tmh"
11+
12+
int CrosEcReadMemU8(HANDLE Handle, unsigned int offset, UINT8* dest)
13+
{
14+
NTSTATUS Status = STATUS_SUCCESS;
15+
DWORD retb{};
16+
CROSEC_READMEM rm{};
17+
18+
if (Handle == INVALID_HANDLE_VALUE) {
19+
Status = STATUS_INVALID_HANDLE;
20+
TraceError("COMBO %!FUNC! Invalid Handle");
21+
return 0;
22+
}
23+
24+
rm.bytes = 0x01;
25+
rm.offset = offset;
26+
Status = DeviceIoControl(Handle,
27+
(DWORD) IOCTL_CROSEC_RDMEM,
28+
&rm,
29+
sizeof(rm),
30+
&rm,
31+
sizeof(rm),
32+
&retb,
33+
nullptr);
34+
if (!NT_SUCCESS(Status)) {
35+
TraceError("COMBO %!FUNC! ConnectToEc failed %!STATUS!", Status);
36+
return 0;
37+
}
38+
39+
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);
40+
*dest = rm.buffer[0];
41+
42+
return rm.bytes;
43+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//Copyright (C) Framework Computer Inc
2+
//Copyright (C) 2014 The ChromiumOS Authors
3+
//
4+
//Abstract:
5+
//
6+
// Definitions for accessing EC
7+
8+
#pragma once
9+
10+
#ifdef __cplusplus
11+
extern "C" {
12+
#endif
13+
14+
#include <handleapi.h>
15+
16+
/* Command version mask */
17+
#define EC_VER_MASK(version) (1UL << (version))
18+
19+
#define EC_MEMMAP_ALS 0x80 /* ALS readings in lux (2 X 16 bits) */
20+
/* Unused 0x84 - 0x8f */
21+
#define EC_MEMMAP_ACC_STATUS 0x90 /* Accelerometer status (8 bits )*/
22+
/* Unused 0x91 */
23+
#define EC_MEMMAP_ACC_DATA 0x92 /* Accelerometers data 0x92 - 0x9f */
24+
/* 0x92: Lid Angle if available, LID_ANGLE_UNRELIABLE otherwise */
25+
/* 0x94 - 0x99: 1st Accelerometer */
26+
/* 0x9a - 0x9f: 2nd Accelerometer */
27+
28+
/* Define the format of the accelerometer mapped memory status byte. */
29+
#define EC_MEMMAP_ACC_STATUS_SAMPLE_ID_MASK 0x0f
30+
#define EC_MEMMAP_ACC_STATUS_BUSY_BIT BIT(4)
31+
#define EC_MEMMAP_ACC_STATUS_PRESENCE_BIT BIT(7)
32+
33+
#define FILE_DEVICE_CROS_EMBEDDED_CONTROLLER 0x80EC
34+
35+
#define IOCTL_CROSEC_XCMD \
36+
CTL_CODE(FILE_DEVICE_CROS_EMBEDDED_CONTROLLER, 0x801, METHOD_BUFFERED, FILE_READ_DATA | FILE_WRITE_DATA)
37+
#define IOCTL_CROSEC_RDMEM CTL_CODE(FILE_DEVICE_CROS_EMBEDDED_CONTROLLER, 0x802, METHOD_BUFFERED, FILE_READ_DATA)
38+
39+
#define CROSEC_CMD_MAX_REQUEST 0x100
40+
#define CROSEC_CMD_MAX_RESPONSE 0x100
41+
#define CROSEC_MEMMAP_SIZE 0xFF
42+
43+
typedef struct _CROSEC_READMEM {
44+
ULONG offset;
45+
ULONG bytes;
46+
UCHAR buffer[CROSEC_MEMMAP_SIZE];
47+
} * PCROSEC_READMEM, CROSEC_READMEM;
48+
49+
int CrosEcReadMemU8(HANDLE Handle, unsigned int offset, UINT8* dest);
50+
51+
#ifdef __cplusplus
52+
}
53+
#endif

SensorsComboDriver/SensorsComboDriver.vcxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" />
8181
</ImportGroup>
8282
<ItemGroup Label="WrappedTaskItems">
83-
<ClCompile Include="AlsClient.cpp; Clients.cpp; device.cpp; driver.cpp; SimpleDeviceOrientationClient.cpp; linearaccelerometerclient.cpp; ">
83+
<ClCompile Include="AlsClient.cpp; Clients.cpp; device.cpp; driver.cpp; EcCommunication.cpp; SimpleDeviceOrientationClient.cpp; linearaccelerometerclient.cpp; ">
8484
<WppEnabled>true</WppEnabled>
8585
<WppDllMacro>true</WppDllMacro>
8686
<WppModuleName>SensorsComboDriver</WppModuleName>

SensorsComboDriver/SensorsComboDriver.vcxproj.Filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
<ClCompile Include="driver.cpp">
3232
<Filter>Source Files</Filter>
3333
</ClCompile>
34+
<ClCompile Include="EcCommunication.cpp">
35+
<Filter>Source Files</Filter>
36+
</ClCompile>
3437
<ClCompile Include="SimpleDeviceOrientationClient.cpp">
3538
<Filter>Source Files</Filter>
3639
</ClCompile>

0 commit comments

Comments
 (0)