Skip to content

Commit c2a8569

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 7b54e8c commit c2a8569

File tree

3 files changed

+98
-6
lines changed

3 files changed

+98
-6
lines changed

FrameworkSensors/AccelerometerClient.cpp

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,63 @@ UINT8 CrosEcGetMotionSensorCount(HANDLE Handle)
6161
return res.SensorCount;
6262
}
6363

64+
// Returns STATUS_NOT_FOUND if either base or lid accelerometer sensors are not found.
65+
NTSTATUS
66+
CrosEcGetAccelIndeces(HANDLE Handle, UINT8 *BaseSensor, UINT8 *LidSensor)
67+
{
68+
EC_REQUEST_MOTION_SENSE_INFO req{};
69+
EC_RESPONSE_MOTION_SENSE_INFO res{};
70+
UINT8 SensorCount = CrosEcGetMotionSensorCount(Handle);
71+
BOOLEAN FoundBase = FALSE;
72+
BOOLEAN FoundLid = FALSE;
73+
74+
if (BaseSensor == nullptr || LidSensor == nullptr)
75+
{
76+
TraceError("%!FUNC! Invalid BaseSensor or LidSensor pointer");
77+
return STATUS_INVALID_PARAMETER;
78+
}
79+
80+
for (UINT8 i = 0; i < SensorCount; i++)
81+
{
82+
req.Cmd = 1;
83+
req.SensorNum = i;
84+
if (0 == CrosEcSendCommand(
85+
Handle,
86+
EC_CMD_MOTION_SENSE,
87+
0,
88+
&req,
89+
sizeof(req),
90+
&res,
91+
sizeof(res)
92+
)) {
93+
TraceError("%!FUNC! EC_CMD_MOTION_SENSE_INFO failed for sensor %d", i);
94+
continue;
95+
}
96+
if (res.SensorType != MOTION_SENSE_TYPE_ACCEL) {
97+
continue;
98+
}
99+
100+
switch (res.Location) {
101+
case MOTION_SENSE_LOCATION_BASE:
102+
FoundBase = TRUE;
103+
*BaseSensor = i;
104+
break;
105+
case MOTION_SENSE_LOCATION_LID:
106+
FoundLid = TRUE;
107+
*LidSensor = i;
108+
break;
109+
}
110+
}
111+
112+
if (!FoundBase || !FoundLid)
113+
{
114+
TraceError("%!FUNC! Base or Lid accelerometer sensor not found");
115+
return STATUS_NOT_FOUND;
116+
}
117+
118+
return STATUS_SUCCESS;
119+
}
120+
64121

65122
//------------------------------------------------------------------------------
66123
// Function: Initialize
@@ -102,6 +159,13 @@ AccelerometerDevice::Initialize(
102159
goto Exit;
103160
}
104161

162+
Status = CrosEcGetAccelIndeces(Context->m_CrosEcHandle, &m_LidSensorIndex, &m_LidBaseSensor);
163+
if (!NT_SUCCESS(Status))
164+
{
165+
TraceError("%!FUNC! Failed to get accelerometer indeces: %!STATUS!", Status);
166+
goto Exit;
167+
}
168+
105169
//
106170
// Create Lock
107171
//
@@ -446,13 +510,17 @@ AccelerometerDevice::GetData(
446510
UINT16 lid_angle = lid_angle_bytes[0] + (lid_angle_bytes[1] << 8);
447511
TraceInformation("Lid Angle Status: %dDeg%s", lid_angle, lid_angle == 500 ? "(Unreliable)" : "");
448512

513+
// Lid accelerometer is relevant for screen rotation
514+
// Base accelerometer is not used in this driver
515+
// It's only used for lid angle in the EC firmware
516+
UINT SensorOffset = 6 * m_LidSensorIndex + EC_MEMMAP_ACC_DATA + 2;
449517
UINT16 Sensor1[6] = {0};
450-
CrosEcReadMemU8(Handle, EC_MEMMAP_ACC_DATA + 2, (UINT8*)&Sensor1[0]);
451-
CrosEcReadMemU8(Handle, EC_MEMMAP_ACC_DATA + 3, (UINT8*)&Sensor1[1]);
452-
CrosEcReadMemU8(Handle, EC_MEMMAP_ACC_DATA + 4, (UINT8*)&Sensor1[2]);
453-
CrosEcReadMemU8(Handle, EC_MEMMAP_ACC_DATA + 5, (UINT8*)&Sensor1[3]);
454-
CrosEcReadMemU8(Handle, EC_MEMMAP_ACC_DATA + 6, (UINT8*)&Sensor1[4]);
455-
CrosEcReadMemU8(Handle, EC_MEMMAP_ACC_DATA + 7, (UINT8*)&Sensor1[5]);
518+
CrosEcReadMemU8(Handle, SensorOffset, (UINT8*)&Sensor1[0]);
519+
CrosEcReadMemU8(Handle, SensorOffset + 1, (UINT8*)&Sensor1[1]);
520+
CrosEcReadMemU8(Handle, SensorOffset + 2, (UINT8*)&Sensor1[2]);
521+
CrosEcReadMemU8(Handle, SensorOffset + 3, (UINT8*)&Sensor1[3]);
522+
CrosEcReadMemU8(Handle, SensorOffset + 4, (UINT8*)&Sensor1[4]);
523+
CrosEcReadMemU8(Handle, SensorOffset + 5, (UINT8*)&Sensor1[5]);
456524
m_CachedData.Axis.X = (float) (Sensor1[0] + (Sensor1[1] << 8));
457525
m_CachedData.Axis.Y = (float) (Sensor1[2] + (Sensor1[3] << 8));
458526
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)