@@ -38,14 +38,18 @@ typedef enum
3838 ACCELEROMETER_DATA_COUNT
3939} ACCELEROMETER_DATA_INDEX;
4040
41- UINT8 CrosEcGetMotionSensorCount (HANDLE Handle)
41+ NTSTATUS CrosEcGetMotionSensorCount (HANDLE Handle, UINT8 *Count )
4242{
4343 EC_REQUEST_MOTION_SENSE_DUMP req{};
4444 EC_RESPONSE_MOTION_SENSE_DUMP res{};
4545
4646 if (Handle == INVALID_HANDLE_VALUE) {
4747 TraceError (" %!FUNC! Handle is invalid" );
48- return 0 ;
48+ return STATUS_INVALID_PARAMETER;
49+ }
50+ if (Count == nullptr ) {
51+ TraceError (" %!FUNC! Count==NULL" );
52+ return STATUS_INVALID_PARAMETER;
4953 }
5054
5155 req.Cmd = 0 ;
@@ -60,21 +64,21 @@ UINT8 CrosEcGetMotionSensorCount(HANDLE Handle)
6064 sizeof (res)
6165 )) {
6266 TraceError (" %!FUNC! EC_CMD_MOTION_SENSE_DUMP failed" );
63- return 0 ;
67+ return STATUS_NOT_FOUND ;
6468 }
6569
66- return res.SensorCount ;
70+ *Count = res.SensorCount ;
71+ return STATUS_SUCCESS;
6772}
6873
6974// Returns STATUS_NOT_FOUND if either base or lid accelerometer sensors are not found.
7075NTSTATUS
71- CrosEcGetAccelIndeces (HANDLE Handle, UINT8 *BaseSensor, UINT8 *LidSensor)
76+ CrosEcGetAccelIndeces (HANDLE Handle, UINT8 *BaseSensor, UINT8 *LidSensor, UINT8 SensorCount )
7277{
7378 EC_REQUEST_MOTION_SENSE_INFO req{};
7479 EC_RESPONSE_MOTION_SENSE_INFO res{};
7580 BOOLEAN FoundBase = FALSE ;
7681 BOOLEAN FoundLid = FALSE ;
77- UINT8 SensorCount = 0 ;
7882
7983 if (Handle == INVALID_HANDLE_VALUE) {
8084 TraceError (" %!FUNC! Handle is invalid" );
@@ -87,8 +91,6 @@ CrosEcGetAccelIndeces(HANDLE Handle, UINT8 *BaseSensor, UINT8 *LidSensor)
8791 return STATUS_INVALID_PARAMETER;
8892 }
8993
90- SensorCount = CrosEcGetMotionSensorCount (Handle);
91-
9294 for (UINT8 i = 0 ; i < SensorCount; i++)
9395 {
9496 req.Cmd = 1 ;
@@ -164,24 +166,36 @@ AccelerometerDevice::Initialize(
164166 m_Device = Device;
165167 m_SensorInstance = SensorInstance;
166168 m_Started = FALSE ;
169+ SensorCount = 0 ;
167170 // Sensible defaults - applies to most devices
168171 m_LidSensorIndex = 0 ;
169172 m_LidBaseSensor = 1 ;
173+ Context->m_CrosEcHandle = INVALID_HANDLE_VALUE;
170174
171- SensorCount = CrosEcGetMotionSensorCount (Context->m_CrosEcHandle );
175+ // Make sure we have a handle to the EC driver
176+ ConnectToEc (&Context->m_CrosEcHandle );
177+
178+ Status = CrosEcGetMotionSensorCount (Context->m_CrosEcHandle , &SensorCount);
172179 TraceInformation (" %!FUNC! Found %d Sensors on this device" , SensorCount);
173- if (SensorCount == 0 )
180+ // If the EC is present, we evaluate the responses,
181+ // If not just ignore it and try again later.
182+ // Want to avoid failing the driver load if the EC is not present.
183+ if (NT_SUCCESS (Status))
174184 {
175- TraceError (" %!FUNC! No Sensors available. Not initializing AccelerometerClient" );
176- Status = STATUS_NOT_FOUND;
177- goto Exit;
178- }
185+ if (SensorCount == 0 )
186+ {
187+ TraceError (" %!FUNC! No Sensors available. Not initializing AccelerometerClient" );
188+ Status = STATUS_NOT_FOUND;
189+ goto Exit;
190+ }
179191
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;
192+ Status = CrosEcGetAccelIndeces (Context->m_CrosEcHandle , &m_LidSensorIndex, &m_LidBaseSensor, SensorCount);
193+ if (!NT_SUCCESS (Status))
194+ {
195+ TraceError (" %!FUNC! Failed to get accelerometer indeces: %!STATUS!" , Status);
196+ Status = STATUS_NOT_FOUND;
197+ goto Exit;
198+ }
185199 }
186200
187201 //
@@ -515,6 +529,13 @@ AccelerometerDevice::GetData(
515529
516530 SENSOR_FunctionEnter ();
517531
532+ if (Handle == INVALID_HANDLE_VALUE) {
533+ TraceError (" %!FUNC! Handle is invalid" );
534+ return STATUS_INVALID_HANDLE;
535+ }
536+
537+ // TODO: Might want to check if sensor indeces are initialized
538+
518539 UINT8 acc_status = 0 ;
519540 CrosEcReadMemU8 (Handle, EC_MEMMAP_ACC_STATUS, &acc_status);
520541 TraceInformation (" Status: (%02x), Present: %d, Busy: %d\n " ,
@@ -663,4 +684,4 @@ AccelerometerDevice::UpdateCachedThreshold(
663684Exit:
664685 SENSOR_FunctionExit (Status);
665686 return Status;
666- }
687+ }
0 commit comments