Skip to content

Commit afd390e

Browse files
committed
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. Signed-off-by: Daniel Schaefer <dhs@frame.work>
1 parent 4e15675 commit afd390e

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

@@ -225,6 +203,81 @@ OnPrepareHardware(
225203

226204
SENSOR_FunctionEnter();
227205

206+
//
207+
// Dynamic sensor detection
208+
//
209+
SensorInstanceCount = 0;
210+
211+
// Detect accelerometer + hinge angle via motion sensor presence
212+
{
213+
UINT8 acc_status = 0;
214+
CrosEcReadMemU8(Handle, EC_MEMMAP_ACC_STATUS, &acc_status);
215+
if (acc_status & EC_MEMMAP_ACC_STATUS_PRESENCE_BIT)
216+
{
217+
UINT8 base = 0, lid = 0;
218+
if (NT_SUCCESS(CrosEcGetAccelIndeces(Handle, &base, &lid)))
219+
{
220+
TraceInformation("COMBO %!FUNC! Detected accelerometer (base=%d, lid=%d)", base, lid);
221+
SensorSizes[SensorInstanceCount] = sizeof(AccelerometerDevice);
222+
SensorKinds[SensorInstanceCount] = SENSOR_KIND_ACCELEROMETER;
223+
SensorInstanceCount++;
224+
}
225+
}
226+
}
227+
228+
// Detect ALS via motion sensor enumeration, then fallback to memmap
229+
{
230+
BOOLEAN alsFound = FALSE;
231+
UINT8 sensorCount = CrosEcGetMotionSensorCount(Handle);
232+
for (UINT8 idx = 0; idx < sensorCount && !alsFound; idx++)
233+
{
234+
EC_REQUEST_MOTION_SENSE_INFO infoReq{};
235+
EC_RESPONSE_MOTION_SENSE_INFO infoRes{};
236+
237+
infoReq.Cmd = 1;
238+
infoReq.SensorNum = idx;
239+
if (0 != CrosEcSendCommand(
240+
Handle,
241+
EC_CMD_MOTION_SENSE_CMD,
242+
1,
243+
&infoReq,
244+
sizeof(infoReq),
245+
&infoRes,
246+
sizeof(infoRes)))
247+
{
248+
if (infoRes.SensorType == MOTIONSENSE_TYPE_LIGHT ||
249+
infoRes.SensorType == MOTIONSENSE_TYPE_LIGHT_RGB)
250+
{
251+
TraceInformation("COMBO %!FUNC! Detected ALS via motion sensor index %d", idx);
252+
alsFound = TRUE;
253+
}
254+
}
255+
}
256+
257+
// Fallback: check EC_MEMMAP_ALS for non-zero value
258+
if (!alsFound)
259+
{
260+
UINT8 alsVal[2] = {0};
261+
CrosEcReadMemU8(Handle, EC_MEMMAP_ALS + 0, &alsVal[0]);
262+
CrosEcReadMemU8(Handle, EC_MEMMAP_ALS + 1, &alsVal[1]);
263+
UINT16 alsReading = alsVal[0] + (alsVal[1] << 8);
264+
if (alsReading != 0)
265+
{
266+
TraceInformation("COMBO %!FUNC! Detected ALS via memmap (reading=%d)", alsReading);
267+
alsFound = TRUE;
268+
}
269+
}
270+
271+
if (alsFound && SensorInstanceCount < MAX_SENSOR_COUNT)
272+
{
273+
SensorSizes[SensorInstanceCount] = sizeof(AlsDevice);
274+
SensorKinds[SensorInstanceCount] = SENSOR_KIND_ALS;
275+
SensorInstanceCount++;
276+
}
277+
}
278+
279+
TraceInformation("COMBO %!FUNC! Detected %d sensor(s) total", SensorInstanceCount);
280+
228281
for (ULONG Count = 0; Count < SensorInstanceCount; Count++)
229282
{
230283
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)