Skip to content

Commit e525a53

Browse files
committed
Find lid sensor by motionsense commands
Useful in case the lid sensor is not the first one in the memmap. On Framework 12 it currently is (BIOS 3.03) but it might not be in the future. Signed-off-by: Daniel Schaefer <dhs@frame.work>
1 parent 24d9eae commit e525a53

File tree

3 files changed

+111
-6
lines changed

3 files changed

+111
-6
lines changed

FrameworkSensors/AccelerometerClient.cpp

Lines changed: 87 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,73 @@ UINT8 CrosEcGetMotionSensorCount(HANDLE Handle)
6666
return res.SensorCount;
6767
}
6868

69+
// Returns STATUS_NOT_FOUND if either base or lid accelerometer sensors are not found.
70+
NTSTATUS
71+
CrosEcGetAccelIndeces(HANDLE Handle, UINT8 *BaseSensor, UINT8 *LidSensor)
72+
{
73+
EC_REQUEST_MOTION_SENSE_INFO req{};
74+
EC_RESPONSE_MOTION_SENSE_INFO res{};
75+
BOOLEAN FoundBase = FALSE;
76+
BOOLEAN FoundLid = FALSE;
77+
UINT8 SensorCount = 0;
78+
79+
if (Handle == INVALID_HANDLE_VALUE) {
80+
TraceError("%!FUNC! Handle is invalid");
81+
return STATUS_INVALID_HANDLE;
82+
}
83+
84+
if (BaseSensor == nullptr || LidSensor == nullptr)
85+
{
86+
TraceError("%!FUNC! Invalid BaseSensor or LidSensor pointer");
87+
return STATUS_INVALID_PARAMETER;
88+
}
89+
90+
SensorCount = CrosEcGetMotionSensorCount(Handle);
91+
92+
for (UINT8 i = 0; i < SensorCount; i++)
93+
{
94+
req.Cmd = 1;
95+
req.SensorNum = i;
96+
if (0 == CrosEcSendCommand(
97+
Handle,
98+
EC_CMD_MOTION_SENSE,
99+
1,
100+
&req,
101+
sizeof(req),
102+
&res,
103+
sizeof(res)
104+
)) {
105+
TraceError("%!FUNC! EC_CMD_MOTION_SENSE_INFO failed for sensor %d", i);
106+
continue;
107+
}
108+
if (res.SensorType != MOTION_SENSE_TYPE_ACCEL) {
109+
TraceError("%!FUNC! Found sensor of type %d. Not Accelerometer - ignoring.", res.SensorType);
110+
continue;
111+
}
112+
113+
switch (res.Location) {
114+
case MOTION_SENSE_LOCATION_BASE:
115+
TraceInformation("%!FUNC! Found base accel sensor at index: %d", i);
116+
FoundBase = TRUE;
117+
*BaseSensor = i;
118+
break;
119+
case MOTION_SENSE_LOCATION_LID:
120+
TraceInformation("%!FUNC! Found lid accel sensor at index: %d", i);
121+
FoundLid = TRUE;
122+
*LidSensor = i;
123+
break;
124+
}
125+
}
126+
127+
if (!FoundBase || !FoundLid)
128+
{
129+
TraceError("%!FUNC! Base or Lid accelerometer sensor not found");
130+
return STATUS_NOT_FOUND;
131+
}
132+
133+
return STATUS_SUCCESS;
134+
}
135+
69136

70137
//------------------------------------------------------------------------------
71138
// Function: Initialize
@@ -97,6 +164,9 @@ AccelerometerDevice::Initialize(
97164
m_Device = Device;
98165
m_SensorInstance = SensorInstance;
99166
m_Started = FALSE;
167+
// Sensible defaults - applies to most devices
168+
m_LidSensorIndex = 0;
169+
m_LidBaseSensor = 1;
100170

101171
SensorCount = CrosEcGetMotionSensorCount(Context->m_CrosEcHandle);
102172
TraceInformation("%!FUNC! Found %d Sensors on this device", SensorCount);
@@ -107,6 +177,13 @@ AccelerometerDevice::Initialize(
107177
goto Exit;
108178
}
109179

180+
Status = CrosEcGetAccelIndeces(Context->m_CrosEcHandle, &m_LidSensorIndex, &m_LidBaseSensor);
181+
if (!NT_SUCCESS(Status))
182+
{
183+
TraceError("%!FUNC! Failed to get accelerometer indeces: %!STATUS!", Status);
184+
goto Exit;
185+
}
186+
110187
//
111188
// Create Lock
112189
//
@@ -451,13 +528,17 @@ AccelerometerDevice::GetData(
451528
UINT16 lid_angle = lid_angle_bytes[0] + (lid_angle_bytes[1] << 8);
452529
TraceInformation("Lid Angle Status: %dDeg%s", lid_angle, lid_angle == 500 ? "(Unreliable)" : "");
453530

531+
// Lid accelerometer is relevant for screen rotation
532+
// Base accelerometer is not used in this driver
533+
// It's only used for lid angle in the EC firmware
534+
UINT SensorOffset = 6 * m_LidSensorIndex + EC_MEMMAP_ACC_DATA + 2;
454535
UINT16 Sensor1[6] = {0};
455-
CrosEcReadMemU8(Handle, EC_MEMMAP_ACC_DATA + 2, (UINT8*)&Sensor1[0]);
456-
CrosEcReadMemU8(Handle, EC_MEMMAP_ACC_DATA + 3, (UINT8*)&Sensor1[1]);
457-
CrosEcReadMemU8(Handle, EC_MEMMAP_ACC_DATA + 4, (UINT8*)&Sensor1[2]);
458-
CrosEcReadMemU8(Handle, EC_MEMMAP_ACC_DATA + 5, (UINT8*)&Sensor1[3]);
459-
CrosEcReadMemU8(Handle, EC_MEMMAP_ACC_DATA + 6, (UINT8*)&Sensor1[4]);
460-
CrosEcReadMemU8(Handle, EC_MEMMAP_ACC_DATA + 7, (UINT8*)&Sensor1[5]);
536+
CrosEcReadMemU8(Handle, SensorOffset, (UINT8*)&Sensor1[0]);
537+
CrosEcReadMemU8(Handle, SensorOffset + 1, (UINT8*)&Sensor1[1]);
538+
CrosEcReadMemU8(Handle, SensorOffset + 2, (UINT8*)&Sensor1[2]);
539+
CrosEcReadMemU8(Handle, SensorOffset + 3, (UINT8*)&Sensor1[3]);
540+
CrosEcReadMemU8(Handle, SensorOffset + 4, (UINT8*)&Sensor1[4]);
541+
CrosEcReadMemU8(Handle, SensorOffset + 5, (UINT8*)&Sensor1[5]);
461542
m_CachedData.Axis.X = (float) (Sensor1[0] + (Sensor1[1] << 8));
462543
m_CachedData.Axis.Y = (float) (Sensor1[2] + (Sensor1[3] << 8));
463544
m_CachedData.Axis.Z = (float) (Sensor1[4] + (Sensor1[5] << 8));

FrameworkSensors/Clients.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,8 @@ typedef class _AccelerometerDevice : public _ComboDevice
221221
AccelerometerSample m_CachedThresholds;
222222
AccelerometerSample m_CachedData;
223223
AccelerometerSample m_LastSample;
224+
UINT8 m_LidSensorIndex;
225+
UINT8 m_LidBaseSensor;
224226

225227
public:
226228

FrameworkSensors/EcCommunication.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,28 @@ typedef struct {
109109
// UINT8 Sensors[];
110110
} EC_RESPONSE_MOTION_SENSE_DUMP;
111111

112+
typedef struct {
113+
// Info = 1
114+
UINT8 Cmd;
115+
UINT8 SensorNum;
116+
} EC_REQUEST_MOTION_SENSE_INFO;
117+
118+
#define MOTION_SENSE_TYPE_ACCEL 0x00
119+
#define MOTION_SENSE_TYPE_GYRO 0x01
120+
#define MOTION_SENSE_TYPE_MAG 0x02
121+
#define MOTION_SENSE_TYPE_PROX 0x03
122+
#define MOTION_SENSE_TYPE_LIGHT 0x04
123+
124+
#define MOTION_SENSE_LOCATION_BASE 0x00
125+
#define MOTION_SENSE_LOCATION_LID 0x01
126+
#define MOTION_SENSE_LOCATION_CAMERA 0x02
127+
128+
typedef struct {
129+
UINT8 SensorType;
130+
UINT8 Location;
131+
UINT8 Chip;
132+
} EC_RESPONSE_MOTION_SENSE_INFO;
133+
112134
#include <poppack.h>
113135

114136
int CrosEcSendCommand(

0 commit comments

Comments
 (0)