Skip to content

Commit 5801e6f

Browse files
committed
Dynamically connect to EC
If the EC driver is not installed or runinng when the sensor driver starts, need to fail gracefully instead of preventing driver start and yellow bang. So instead we keep checking and connect to the EC at the first chance. Signed-off-by: Daniel Schaefer <dhs@frame.work>
1 parent 2bf174d commit 5801e6f

File tree

4 files changed

+52
-55
lines changed

4 files changed

+52
-55
lines changed

FrameworkSensors/AccelerometerClient.cpp

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
7075
NTSTATUS
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(
663684
Exit:
664685
SENSOR_FunctionExit(Status);
665686
return Status;
666-
}
687+
}

FrameworkSensors/Clients.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
// Windows User-Mode Driver Framework (WUDF)
1111

1212
#include "Clients.h"
13+
#include "EcCommunication.h"
1314

1415
#include "Clients.tmh"
1516

@@ -47,6 +48,8 @@ OnTimerExpire(
4748

4849
// Get data and push to clx
4950
Lock(pDevice->m_Lock);
51+
// Make sure we have a handle to the EC driver
52+
ConnectToEc(&pDevice->m_CrosEcHandle);
5053
Status = pDevice->GetData(pDevice->m_CrosEcHandle);
5154
if (!NT_SUCCESS(Status) && Status != STATUS_DATA_NOT_ACCEPTED)
5255
{

FrameworkSensors/Device.cpp

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -219,43 +219,9 @@ OnPrepareHardware(
219219
)
220220
{
221221
NTSTATUS Status = STATUS_SUCCESS;
222-
ULONG i;
223-
HANDLE Handle;
224-
DWORD retb{};
225-
CROSEC_READMEM rm{};
226222

227223
SENSOR_FunctionEnter();
228224

229-
Status = ConnectToEc(&Handle);
230-
if (!NT_SUCCESS(Status)) {
231-
TraceError("COMBO %!FUNC! ConnectToEc failed %!STATUS!", Status);
232-
goto Exit;
233-
}
234-
235-
rm.bytes = 0xfe;
236-
rm.offset = 0;
237-
Status = DeviceIoControl(Handle,
238-
(DWORD) IOCTL_CROSEC_RDMEM,
239-
&rm,
240-
sizeof(rm),
241-
&rm,
242-
sizeof(rm),
243-
&retb,
244-
nullptr);
245-
if (!NT_SUCCESS(Status)) {
246-
TraceError("COMBO %!FUNC! ConnectToEc failed %!STATUS!", Status);
247-
goto Exit;
248-
}
249-
250-
UINT8 *EcMem = rm.buffer;
251-
for (i = 0; i < 0xfe-16; i+=16) {
252-
TraceInformation(
253-
"%02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X\n",
254-
EcMem[i], EcMem[i+1], EcMem[i+2], EcMem[i+3], EcMem[i+4], EcMem[i + 5], EcMem[i + 6], EcMem[i + 7],
255-
EcMem[i + 8], EcMem[i+9], EcMem[i+10], EcMem[i+11], EcMem[i+12], EcMem[i + 13], EcMem[i + 14], EcMem[i + 15]
256-
);
257-
}
258-
259225
for (ULONG Count = 0; Count < SensorInstanceCount; Count++)
260226
{
261227
PComboDevice pDevice = nullptr;
@@ -285,7 +251,7 @@ OnPrepareHardware(
285251

286252
AllocateDeviceAtIndex(Count, &pDevice);
287253

288-
pDevice->m_CrosEcHandle = Handle;
254+
pDevice->m_CrosEcHandle = INVALID_HANDLE_VALUE;
289255

290256
// Fill out properties
291257
Status = pDevice->Initialize(Device, SensorInstance);

FrameworkSensors/EcCommunication.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ NTSTATUS ConnectToEc(
2323
) {
2424
NTSTATUS Status = STATUS_SUCCESS;
2525

26+
if (*Handle != INVALID_HANDLE_VALUE) {
27+
// Already connected
28+
TraceError("%!FUNC! Already connected");
29+
return Status;
30+
}
31+
2632
*Handle = CreateFileW(
2733
LR"(\\.\GLOBALROOT\Device\CrosEC)",
2834
GENERIC_READ | GENERIC_WRITE,
@@ -37,6 +43,7 @@ NTSTATUS ConnectToEc(
3743
return STATUS_INVALID_HANDLE;
3844
}
3945

46+
TraceInformation("%!FUNC! Got Handle");
4047
return Status;
4148
}
4249

0 commit comments

Comments
 (0)