@@ -38,6 +38,102 @@ typedef enum
3838 ACCELEROMETER_DATA_COUNT
3939} ACCELEROMETER_DATA_INDEX;
4040
41+ UINT8 CrosEcGetMotionSensorCount (HANDLE Handle)
42+ {
43+ EC_REQUEST_MOTION_SENSE_DUMP req{};
44+ EC_RESPONSE_MOTION_SENSE_DUMP res{};
45+
46+ if (Handle == INVALID_HANDLE_VALUE) {
47+ TraceError (" %!FUNC! Handle is invalid" );
48+ return 0 ;
49+ }
50+
51+ req.Cmd = 0 ;
52+ req.MaxSensorCount = 0 ;
53+ if (0 == CrosEcSendCommand (
54+ Handle,
55+ EC_CMD_MOTION_SENSE,
56+ 1 ,
57+ &req,
58+ sizeof (req),
59+ &res,
60+ sizeof (res)
61+ )) {
62+ TraceError (" %!FUNC! EC_CMD_MOTION_SENSE_DUMP failed" );
63+ return 0 ;
64+ }
65+
66+ return res.SensorCount ;
67+ }
68+
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+
136+
41137// ------------------------------------------------------------------------------
42138// Function: Initialize
43139//
@@ -57,6 +153,8 @@ AccelerometerDevice::Initialize(
57153 )
58154{
59155 NTSTATUS Status = STATUS_SUCCESS;
156+ UINT8 SensorCount = 0 ;
157+ PComboDevice Context = GetContextFromSensorInstance (SensorInstance);
60158
61159 SENSOR_FunctionEnter ();
62160
@@ -66,6 +164,25 @@ AccelerometerDevice::Initialize(
66164 m_Device = Device;
67165 m_SensorInstance = SensorInstance;
68166 m_Started = FALSE ;
167+ // Sensible defaults - applies to most devices
168+ m_LidSensorIndex = 0 ;
169+ m_LidBaseSensor = 1 ;
170+
171+ SensorCount = CrosEcGetMotionSensorCount (Context->m_CrosEcHandle );
172+ TraceInformation (" %!FUNC! Found %d Sensors on this device" , SensorCount);
173+ if (SensorCount == 0 )
174+ {
175+ TraceError (" %!FUNC! No Sensors available. Not initializing AccelerometerClient" );
176+ Status = STATUS_NOT_FOUND;
177+ goto Exit;
178+ }
179+
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+ }
69186
70187 //
71188 // Create Lock
@@ -411,13 +528,17 @@ AccelerometerDevice::GetData(
411528 UINT16 lid_angle = lid_angle_bytes[0 ] + (lid_angle_bytes[1 ] << 8 );
412529 TraceInformation (" Lid Angle Status: %dDeg%s" , lid_angle, lid_angle == 500 ? " (Unreliable)" : " " );
413530
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 ;
414535 UINT16 Sensor1[6 ] = {0 };
415- CrosEcReadMemU8 (Handle, EC_MEMMAP_ACC_DATA + 2 , (UINT8*)&Sensor1[0 ]);
416- CrosEcReadMemU8 (Handle, EC_MEMMAP_ACC_DATA + 3 , (UINT8*)&Sensor1[1 ]);
417- CrosEcReadMemU8 (Handle, EC_MEMMAP_ACC_DATA + 4 , (UINT8*)&Sensor1[2 ]);
418- CrosEcReadMemU8 (Handle, EC_MEMMAP_ACC_DATA + 5 , (UINT8*)&Sensor1[3 ]);
419- CrosEcReadMemU8 (Handle, EC_MEMMAP_ACC_DATA + 6 , (UINT8*)&Sensor1[4 ]);
420- 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 ]);
421542 m_CachedData.Axis .X = (float ) (Sensor1[0 ] + (Sensor1[1 ] << 8 ));
422543 m_CachedData.Axis .Y = (float ) (Sensor1[2 ] + (Sensor1[3 ] << 8 ));
423544 m_CachedData.Axis .Z = (float ) (Sensor1[4 ] + (Sensor1[5 ] << 8 ));
0 commit comments