-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDevice.cpp
More file actions
527 lines (467 loc) · 16.9 KB
/
Device.cpp
File metadata and controls
527 lines (467 loc) · 16.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
// SPDX-License-Identifier: MS-PL
//
// Copyright (C) Microsoft Corporation, All Rights Reserved.
// Copyright (C) Framework Computer Inc, All Rights Reserved.
//
// Abstract:
//
// This module contains the implementation of WDF callback functions
// for Framework Sensors driver.
//
// Environment:
//
// Windows User-Mode Driver Framework (WUDF)
#include "Clients.h"
#include "Driver.h"
#include "EcCommunication.h"
#include <new.h>
#include <winnt.h>
#include "Device.tmh"
#define ENABLE_ALS_SENSOR 0
#define ENABLE_ORIENTATION_SENSOR 0
#define ENABLE_ACCEL_SENSOR 1
//---------------------------------------
// Declare and map devices below
//---------------------------------------
enum Device
{
#if ENABLE_ALS_SENSOR
Device_Als,
#endif
#if ENABLE_ORIENTATION_SENSOR
Device_SimpleDeviceOrientation,
#endif
#if ENABLE_ACCEL_SENSOR
Device_Accelerometer,
#endif
// Keep this last
Device_Count
};
static const ULONG SensorInstanceCount = Device_Count;
static SENSOROBJECT SensorInstancesBuffer[SensorInstanceCount]; // Global buffer to avoid allocate and free
inline size_t GetDeviceSizeAtIndex(
_In_ ULONG Index)
{
size_t result = 0;
switch (static_cast<Device>(Index))
{
#if ENABLE_ALS_SENSOR
case Device_Als: result = sizeof(AlsDevice); break;
#endif
#if ENABLE_ORIENTATION_SENSOR
case Device_SimpleDeviceOrientation:result = sizeof(SimpleDeviceOrientationDevice); break;
#endif
#if ENABLE_ACCEL_SENSOR
case Device_Accelerometer: result = sizeof(AccelerometerDevice); break;
#endif
default: break; // invalid
}
return result;
}
void AllocateDeviceAtIndex(
_In_ ULONG Index,
_Inout_ PComboDevice* ppDevice
)
{
switch (static_cast<Device>(Index))
{
#if ENABLE_ALS_SENSOR
case Device_Als: *ppDevice = new(*ppDevice) AlsDevice; break;
#endif
#if ENABLE_ORIENTATIONACCEL_SENSOR
case Device_SimpleDeviceOrientation:*ppDevice = new(*ppDevice) SimpleDeviceOrientationDevice; break;
#endif
#if ENABLE_ACCEL_SENSOR
case Device_Accelerometer: *ppDevice = new(*ppDevice) AccelerometerDevice; break;
#endif
default: break; // invalid (let driver fail)
}
}
//------------------------------------------------------------------------------
//
// Function: OnDeviceAdd
//
// This routine is the AddDevice entry point for the Framework Sensors client
// driver. This routine is called by the framework in response to AddDevice
// call from the PnP manager. It will create and initialize the device object
// to represent a new instance of the sensor client.
//
// Arguments:
// Driver: IN: Supplies a handle to the driver object created in DriverEntry
// DeviceInit: IN: Supplies a pointer to a framework-allocated WDFDEVICE_INIT structure
//
// Return Value:
// NTSTATUS code
//------------------------------------------------------------------------------
NTSTATUS
OnDeviceAdd(
_In_ WDFDRIVER /*Driver*/,
_Inout_ PWDFDEVICE_INIT pDeviceInit
)
{
WDF_PNPPOWER_EVENT_CALLBACKS Callbacks;
WDFDEVICE Device;
WDF_OBJECT_ATTRIBUTES FdoAttributes;
ULONG Flag = 0;
SENSOR_CONTROLLER_CONFIG SensorConfig;
NTSTATUS Status = STATUS_SUCCESS;
SENSOR_FunctionEnter();
WDF_OBJECT_ATTRIBUTES_INIT(&FdoAttributes);
//
// Initialize FDO attributes and set up file object with sensor extension
//
Status = SensorsCxDeviceInitConfig(pDeviceInit, &FdoAttributes, Flag);
if (!NT_SUCCESS(Status))
{
TraceError("COMBO %!FUNC! SensorsCxDeviceInitConfig failed %!STATUS!", Status);
goto Exit;
}
//
// Register the PnP callbacks with the framework.
//
WDF_PNPPOWER_EVENT_CALLBACKS_INIT(&Callbacks);
Callbacks.EvtDevicePrepareHardware = OnPrepareHardware;
Callbacks.EvtDeviceReleaseHardware = OnReleaseHardware;
Callbacks.EvtDeviceD0Entry = OnD0Entry;
Callbacks.EvtDeviceD0Exit = OnD0Exit;
WdfDeviceInitSetPnpPowerEventCallbacks(pDeviceInit, &Callbacks);
//
// Call the framework to create the device
//
Status = WdfDeviceCreate(&pDeviceInit, &FdoAttributes, &Device);
if (!NT_SUCCESS(Status))
{
TraceError("COMBO %!FUNC! WdfDeviceCreate failed %!STATUS!", Status);
goto Exit;
}
//
// Register CLX callback function pointers
//
SENSOR_CONTROLLER_CONFIG_INIT(&SensorConfig);
SensorConfig.DriverIsPowerPolicyOwner = WdfUseDefault;
SensorConfig.EvtSensorStart = OnStart;
SensorConfig.EvtSensorStop = OnStop;
SensorConfig.EvtSensorGetSupportedDataFields = OnGetSupportedDataFields;
SensorConfig.EvtSensorGetDataInterval = OnGetDataInterval;
SensorConfig.EvtSensorSetDataInterval = OnSetDataInterval;
SensorConfig.EvtSensorGetDataFieldProperties = OnGetDataFieldProperties;
SensorConfig.EvtSensorGetDataThresholds = OnGetDataThresholds;
SensorConfig.EvtSensorSetDataThresholds = OnSetDataThresholds;
SensorConfig.EvtSensorGetProperties = OnGetProperties;
SensorConfig.EvtSensorDeviceIoControl = OnIoControl;
SensorConfig.EvtSensorStartHistory = OnStartHistory;
SensorConfig.EvtSensorStopHistory = OnStopHistory;
SensorConfig.EvtSensorClearHistory = OnClearHistory;
SensorConfig.EvtSensorStartHistoryRetrieval = OnStartHistoryRetrieval;
SensorConfig.EvtSensorCancelHistoryRetrieval = OnCancelHistoryRetrieval;
SensorConfig.EvtSensorEnableWake = OnEnableWake;
SensorConfig.EvtSensorDisableWake = OnDisableWake;
//
// Set up power capabilities and IO queues
//
Status = SensorsCxDeviceInitialize(Device, &SensorConfig);
if (!NT_SUCCESS(Status))
{
TraceError("COMBO %!FUNC! SensorDeviceInitialize failed %!STATUS!", Status);
goto Exit;
}
Exit:
SENSOR_FunctionExit(Status);
return Status;
}
//------------------------------------------------------------------------------
//
// Function: OnPrepareHardware
//
// This routine is called by the framework when the PnP manager sends an
// IRP_MN_START_DEVICE request to the driver stack. This routine is
// responsible for performing operations that are necessary to make the
// driver's device operational (for e.g. mapping the hardware resources
// into memory).
//
// Argument:
// Device: IN: Supplies a handle to the framework device object
// ResourcesRaw: IN: Supplies a handle to a collection of framework resource
// objects. This collection identifies the raw (bus-relative) hardware
// resources that have been assigned to the device.
// ResourcesTranslated: IN: Supplies a handle to a collection of framework
// resource objects. This collection identifies the translated
// (system-physical) hardware resources that have been assigned to the
// device. The resources appear from the CPU's point of view.
//
// Return Value:
// NTSTATUS code
//------------------------------------------------------------------------------
NTSTATUS
OnPrepareHardware(
_In_ WDFDEVICE Device,
_In_ WDFCMRESLIST /*ResourcesRaw*/,
_In_ WDFCMRESLIST /*ResourcesTranslated*/
)
{
NTSTATUS Status = STATUS_SUCCESS;
ULONG i;
HANDLE Handle;
DWORD retb{};
CROSEC_READMEM rm{};
SENSOR_FunctionEnter();
Status = ConnectToEc(&Handle);
if (!NT_SUCCESS(Status)) {
TraceError("COMBO %!FUNC! ConnectToEc failed %!STATUS!", Status);
goto Exit;
}
rm.bytes = 0xfe;
rm.offset = 0;
Status = DeviceIoControl(Handle,
(DWORD) IOCTL_CROSEC_RDMEM,
&rm,
sizeof(rm),
&rm,
sizeof(rm),
&retb,
nullptr);
if (!NT_SUCCESS(Status)) {
TraceError("COMBO %!FUNC! ConnectToEc failed %!STATUS!", Status);
goto Exit;
}
UINT8 *EcMem = rm.buffer;
for (i = 0; i < 0xfe-16; i+=16) {
TraceInformation(
"%02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X\n",
EcMem[i], EcMem[i+1], EcMem[i+2], EcMem[i+3], EcMem[i+4], EcMem[i + 5], EcMem[i + 6], EcMem[i + 7],
EcMem[i + 8], EcMem[i+9], EcMem[i+10], EcMem[i+11], EcMem[i+12], EcMem[i + 13], EcMem[i + 14], EcMem[i + 15]
);
}
for (ULONG Count = 0; Count < SensorInstanceCount; Count++)
{
PComboDevice pDevice = nullptr;
WDF_OBJECT_ATTRIBUTES SensorAttr;
SENSOR_CONFIG SensorConfig;
SENSOROBJECT SensorInstance;
// Create WDFOBJECT for the sensor
WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&SensorAttr, ComboDevice);
SensorAttr.ContextSizeOverride = GetDeviceSizeAtIndex(Count);
// Register sensor instance with clx
Status = SensorsCxSensorCreate(Device, &SensorAttr, &SensorInstance);
if (!NT_SUCCESS(Status))
{
TraceError("COMBO %!FUNC! SensorsCxSensorCreate failed %!STATUS!", Status);
goto Exit;
}
pDevice = GetContextFromSensorInstance(SensorInstance);
if (nullptr == pDevice)
{
Status = STATUS_INSUFFICIENT_RESOURCES;
TraceError("COMBO %!FUNC! GetContextFromSensorInstance failed %!STATUS!", Status);
goto Exit;
}
AllocateDeviceAtIndex(Count, &pDevice);
pDevice->m_CrosEcHandle = Handle;
// Fill out properties
Status = pDevice->Initialize(Device, SensorInstance);
if (!NT_SUCCESS(Status))
{
TraceError("COMBO %!FUNC! Initialize device object failed %!STATUS!", Status);
goto Exit;
}
SENSOR_CONFIG_INIT(&SensorConfig);
SensorConfig.pEnumerationList = pDevice->m_pEnumerationProperties;
Status = SensorsCxSensorInitialize(SensorInstance, &SensorConfig);
if (!NT_SUCCESS(Status))
{
TraceError("COMBO %!FUNC! SensorsCxSensorInitialize failed %!STATUS!", Status);
goto Exit;
}
}
Exit:
SENSOR_FunctionExit(Status);
return Status;
}
//------------------------------------------------------------------------------
//
// Function: OnReleaseHardware
//
// This routine is called by the framework when the PnP manager is revoking
// ownership of our resources. This may be in response to either
// IRP_MN_STOP_DEVICE or IRP_MN_REMOVE_DEVICE. This routine is responsible for
// performing cleanup of resources allocated in PrepareHardware callback.
// This callback is invoked before passing the request down to the lower driver.
// This routine will also be invoked by the framework if the prepare hardware
// callback returns a failure.
//
// Argument:
// Device: IN: Supplies a handle to the framework device object
// ResourcesTranslated: IN: Supplies a handle to a collection of framework
// resource objects. This collection identifies the translated
// (system-physical) hardware resources that have been assigned to the
// device. The resources appear from the CPU's point of view.
//
// Return Value:
// NTSTATUS code
//------------------------------------------------------------------------------
NTSTATUS
OnReleaseHardware(
_In_ WDFDEVICE Device,
_In_ WDFCMRESLIST /*ResourcesTranslated*/
)
{
ULONG Count = SensorInstanceCount;
PComboDevice pDevice = nullptr;
NTSTATUS Status = STATUS_SUCCESS;
SENSOR_FunctionEnter();
Status = SensorsCxDeviceGetSensorList(Device, SensorInstancesBuffer, &Count);
if (!NT_SUCCESS(Status))
{
Status = STATUS_INVALID_PARAMETER;
TraceError("COMBO %!FUNC! SensorsCxDeviceGetSensorList failed %!STATUS!", Status);
goto Exit;
}
for (Count = 0; Count < SensorInstanceCount; Count++)
{
pDevice = GetContextFromSensorInstance(SensorInstancesBuffer[Count]);
if (nullptr == pDevice)
{
Status = STATUS_INVALID_PARAMETER;
TraceError("COMBO %!FUNC! GetContextFromSensorInstance failed %!STATUS!", Status);
goto Exit;
}
// Delete lock
if (NULL != pDevice->m_Lock)
{
WdfObjectDelete(pDevice->m_Lock);
pDevice->m_Lock = NULL;
}
// Close handle to EC driver
if (pDevice->m_CrosEcHandle && pDevice->m_CrosEcHandle != INVALID_HANDLE_VALUE) {
CloseHandle(pDevice->m_CrosEcHandle);
pDevice->m_CrosEcHandle = INVALID_HANDLE_VALUE;
}
// Delete sensor instance
if (NULL != pDevice->m_SensorInstance)
{
WdfObjectDelete(pDevice->m_SensorInstance);
// The pDevice context created using WdfMemoryCreate and parented to m_SensorInstance is automatically
// destroyed when m_SensorInstance is deleted. pDevice is therefore no longer accessible beyond the above call to WdfObjectDelete.
// We can therefore not set the m_SensorInstance member back to NULL. We instead set pDevice to nullptr.
pDevice = nullptr;
}
}
Exit:
SENSOR_FunctionExit(Status);
return Status;
}
//------------------------------------------------------------------------------
//
// Function: OnD0Entry
//
// This routine is invoked by the framework to program the device to goto
// D0, which is the working state. The framework invokes callback every
// time the hardware needs to be (re-)initialized. This includes after
// IRP_MN_START_DEVICE, IRP_MN_CANCEL_STOP_DEVICE, IRP_MN_CANCEL_REMOVE_DEVICE,
// and IRP_MN_SET_POWER-D0.
//
// Argument:
// Device: IN: Supplies a handle to the framework device object
// PreviousState: IN: WDF_POWER_DEVICE_STATE-typed enumerator that identifies
// the device power state that the device was in before this transition to D0
//
// Return Value:
// NTSTATUS code
//------------------------------------------------------------------------------
NTSTATUS
OnD0Entry(
_In_ WDFDEVICE Device,
_In_ WDF_POWER_DEVICE_STATE /*PreviousState*/
)
{
ULONG Count = SensorInstanceCount;
PComboDevice pDevice = nullptr;
NTSTATUS Status = STATUS_SUCCESS;
SENSOR_FunctionEnter();
//
// Get sensor instances
//
Status = SensorsCxDeviceGetSensorList(Device, SensorInstancesBuffer, &Count);
if (!NT_SUCCESS(Status))
{
Status = STATUS_INVALID_PARAMETER;
TraceError("COMBO %!FUNC! SensorsCxDeviceGetSensorList failed %!STATUS!", Status);
goto Exit;
}
//
// Powering on all sensor instances
//
for (Count = 0; Count < SensorInstanceCount; Count++)
{
pDevice = GetContextFromSensorInstance(SensorInstancesBuffer[Count]);
if (nullptr == pDevice)
{
Status = STATUS_INVALID_PARAMETER;
TraceError("COMBO %!FUNC! GetContextFromSensorInstance failed %!STATUS!", Status);
goto Exit;
}
pDevice->m_PoweredOn = TRUE;
InitPropVariantFromUInt32(SensorState_Idle,
&(pDevice->m_pProperties->List[SENSOR_COMMON_PROPERTY_STATE].Value));
}
Exit:
SENSOR_FunctionExit(Status);
return Status;
}
//------------------------------------------------------------------------------
//
// Function: OnD0Exit
//
// This routine is invoked by the framework to program the device to go into
// a certain Dx state. The framework invokes callback every the the device is
// leaving the D0 state, which happens when the device is stopped, when it is
// removed, and when it is powered off.
//
// Argument:
// Device: IN: Supplies a handle to the framework device object
// TargetState: IN: Supplies the device power state which the device will be put
// in once the callback is complete
//
// Return Value:
// NTSTATUS code
//------------------------------------------------------------------------------
NTSTATUS
OnD0Exit(
_In_ WDFDEVICE Device,
_In_ WDF_POWER_DEVICE_STATE /*TargetState*/
)
{
ULONG Count = SensorInstanceCount;
PComboDevice pDevice = nullptr;
NTSTATUS Status = STATUS_SUCCESS;
SENSOR_FunctionEnter();
//
// Get sensor instances
//
Status = SensorsCxDeviceGetSensorList(Device, SensorInstancesBuffer, &Count);
if (!NT_SUCCESS(Status))
{
Status = STATUS_INVALID_PARAMETER;
TraceError("COMBO %!FUNC! SensorsCxDeviceGetSensorList failed %!STATUS!", Status);
goto Exit;
}
//
// Powering off all sensor instances
//
for (Count = 0; Count < SensorInstanceCount; Count++)
{
pDevice = GetContextFromSensorInstance(SensorInstancesBuffer[Count]);
if (nullptr == pDevice)
{
Status = STATUS_INVALID_PARAMETER;
TraceError("COMBO %!FUNC! GetContextFromSensorInstance failed %!STATUS!", Status);
goto Exit;
}
pDevice->m_PoweredOn = FALSE;
InitPropVariantFromUInt32(SensorState_Idle,
&(pDevice->m_pProperties->List[SENSOR_COMMON_PROPERTY_STATE].Value));
}
Exit:
SENSOR_FunctionExit(Status);
return Status;
}