Skip to content

Commit c7ef185

Browse files
JohnAZoidbergclaude
andcommitted
Replace compile-time sensor enables with runtime EC detection
Query the EC at driver startup to determine which sensors are present instead of using #define ENABLE_* flags. Accelerometers are detected via EC_MEMMAP_ACC_STATUS presence bit and MOTIONSENSE_CMD_INFO. ALS is detected by enumerating motion sensors for MOTIONSENSE_TYPE_LIGHT, with a fallback to reading EC_MEMMAP_ALS. This allows a single driver binary to work across hardware configurations. Also fixes the ENABLE_ORIENTATIONACCEL_SENSOR typo in the old code. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 29f8963 commit c7ef185

2 files changed

Lines changed: 105 additions & 47 deletions

File tree

FrameworkSensors/Device.cpp

Lines changed: 100 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -21,69 +21,47 @@
2121

2222
#include "Device.tmh"
2323

24-
#define ENABLE_ALS_SENSOR 0
25-
#define ENABLE_ORIENTATION_SENSOR 0
26-
#define ENABLE_ACCEL_SENSOR 1
27-
2824
//---------------------------------------
29-
// Declare and map devices below
25+
// Dynamic sensor detection
3026
//---------------------------------------
31-
enum Device
32-
{
33-
#if ENABLE_ALS_SENSOR
34-
Device_Als,
35-
#endif
36-
#if ENABLE_ORIENTATION_SENSOR
37-
Device_SimpleDeviceOrientation,
38-
#endif
39-
#if ENABLE_ACCEL_SENSOR
40-
Device_Accelerometer,
41-
#endif
42-
// Keep this last
43-
Device_Count
44-
};
45-
46-
static const ULONG SensorInstanceCount = Device_Count;
47-
static SENSOROBJECT SensorInstancesBuffer[SensorInstanceCount]; // Global buffer to avoid allocate and free
27+
#define MAX_SENSOR_COUNT 4
28+
29+
typedef enum {
30+
SENSOR_KIND_ACCELEROMETER,
31+
SENSOR_KIND_ALS,
32+
} SensorKind;
33+
34+
static ULONG SensorInstanceCount = 0;
35+
static SENSOROBJECT SensorInstancesBuffer[MAX_SENSOR_COUNT];
36+
37+
static size_t SensorSizes[MAX_SENSOR_COUNT];
38+
static SensorKind SensorKinds[MAX_SENSOR_COUNT];
4839

4940
inline size_t GetDeviceSizeAtIndex(
5041
_In_ ULONG Index)
5142
{
52-
size_t result = 0;
53-
switch (static_cast<Device>(Index))
43+
if (Index < SensorInstanceCount)
5444
{
55-
#if ENABLE_ALS_SENSOR
56-
case Device_Als: result = sizeof(AlsDevice); break;
57-
#endif
58-
#if ENABLE_ORIENTATION_SENSOR
59-
case Device_SimpleDeviceOrientation:result = sizeof(SimpleDeviceOrientationDevice); break;
60-
#endif
61-
#if ENABLE_ACCEL_SENSOR
62-
case Device_Accelerometer: result = sizeof(AccelerometerDevice); break;
63-
#endif
64-
default: break; // invalid
45+
return SensorSizes[Index];
6546
}
66-
return result;
47+
return 0;
6748
}
6849

6950
void AllocateDeviceAtIndex(
7051
_In_ ULONG Index,
7152
_Inout_ PComboDevice* ppDevice
7253
)
7354
{
74-
switch (static_cast<Device>(Index))
55+
if (Index >= SensorInstanceCount)
56+
{
57+
return;
58+
}
59+
60+
switch (SensorKinds[Index])
7561
{
76-
#if ENABLE_ALS_SENSOR
77-
case Device_Als: *ppDevice = new(*ppDevice) AlsDevice; break;
78-
#endif
79-
#if ENABLE_ORIENTATIONACCEL_SENSOR
80-
case Device_SimpleDeviceOrientation:*ppDevice = new(*ppDevice) SimpleDeviceOrientationDevice; break;
81-
#endif
82-
#if ENABLE_ACCEL_SENSOR
83-
case Device_Accelerometer: *ppDevice = new(*ppDevice) AccelerometerDevice; break;
84-
#endif
85-
86-
default: break; // invalid (let driver fail)
62+
case SENSOR_KIND_ACCELEROMETER: *ppDevice = new(*ppDevice) AccelerometerDevice; break;
63+
case SENSOR_KIND_ALS: *ppDevice = new(*ppDevice) AlsDevice; break;
64+
default: break;
8765
}
8866
}
8967

@@ -259,6 +237,81 @@ OnPrepareHardware(
259237
);
260238
}
261239

240+
//
241+
// Dynamic sensor detection
242+
//
243+
SensorInstanceCount = 0;
244+
245+
// Detect accelerometer + hinge angle via motion sensor presence
246+
{
247+
UINT8 acc_status = 0;
248+
CrosEcReadMemU8(Handle, EC_MEMMAP_ACC_STATUS, &acc_status);
249+
if (acc_status & EC_MEMMAP_ACC_STATUS_PRESENCE_BIT)
250+
{
251+
UINT8 base = 0, lid = 0;
252+
if (NT_SUCCESS(CrosEcGetAccelIndeces(Handle, &base, &lid)))
253+
{
254+
TraceInformation("COMBO %!FUNC! Detected accelerometer (base=%d, lid=%d)", base, lid);
255+
SensorSizes[SensorInstanceCount] = sizeof(AccelerometerDevice);
256+
SensorKinds[SensorInstanceCount] = SENSOR_KIND_ACCELEROMETER;
257+
SensorInstanceCount++;
258+
}
259+
}
260+
}
261+
262+
// Detect ALS via motion sensor enumeration, then fallback to memmap
263+
{
264+
BOOLEAN alsFound = FALSE;
265+
UINT8 sensorCount = CrosEcGetMotionSensorCount(Handle);
266+
for (UINT8 idx = 0; idx < sensorCount && !alsFound; idx++)
267+
{
268+
EC_REQUEST_MOTION_SENSE_INFO infoReq{};
269+
EC_RESPONSE_MOTION_SENSE_INFO infoRes{};
270+
271+
infoReq.Cmd = 1;
272+
infoReq.SensorNum = idx;
273+
if (0 != CrosEcSendCommand(
274+
Handle,
275+
EC_CMD_MOTION_SENSE_CMD,
276+
1,
277+
&infoReq,
278+
sizeof(infoReq),
279+
&infoRes,
280+
sizeof(infoRes)))
281+
{
282+
if (infoRes.SensorType == MOTIONSENSE_TYPE_LIGHT ||
283+
infoRes.SensorType == MOTIONSENSE_TYPE_LIGHT_RGB)
284+
{
285+
TraceInformation("COMBO %!FUNC! Detected ALS via motion sensor index %d", idx);
286+
alsFound = TRUE;
287+
}
288+
}
289+
}
290+
291+
// Fallback: check EC_MEMMAP_ALS for non-zero value
292+
if (!alsFound)
293+
{
294+
UINT8 alsVal[2] = {0};
295+
CrosEcReadMemU8(Handle, EC_MEMMAP_ALS + 0, &alsVal[0]);
296+
CrosEcReadMemU8(Handle, EC_MEMMAP_ALS + 1, &alsVal[1]);
297+
UINT16 alsReading = alsVal[0] + (alsVal[1] << 8);
298+
if (alsReading != 0)
299+
{
300+
TraceInformation("COMBO %!FUNC! Detected ALS via memmap (reading=%d)", alsReading);
301+
alsFound = TRUE;
302+
}
303+
}
304+
305+
if (alsFound && SensorInstanceCount < MAX_SENSOR_COUNT)
306+
{
307+
SensorSizes[SensorInstanceCount] = sizeof(AlsDevice);
308+
SensorKinds[SensorInstanceCount] = SENSOR_KIND_ALS;
309+
SensorInstanceCount++;
310+
}
311+
}
312+
313+
TraceInformation("COMBO %!FUNC! Detected %d sensor(s) total", SensorInstanceCount);
314+
262315
for (ULONG Count = 0; Count < SensorInstanceCount; Count++)
263316
{
264317
PComboDevice pDevice = nullptr;

FrameworkSensors/EcCommunication.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,9 @@ int CrosEcReadMemU8(HANDLE Handle, unsigned int offset, UINT8* dest);
120120

121121
#ifdef __cplusplus
122122
}
123+
124+
// Motion sensor helpers (defined in AccelerometerClient.cpp)
125+
UINT8 CrosEcGetMotionSensorCount(HANDLE Handle);
126+
NTSTATUS CrosEcGetAccelIndeces(HANDLE Handle, UINT8 *BaseSensor, UINT8 *LidSensor);
127+
123128
#endif

0 commit comments

Comments
 (0)