summaryrefslogtreecommitdiff
path: root/nfc/NfcCxSample/windows-drivertemplate-nfc/Device.cpp
blob: 5429d2a80df1fa829e7a8fb21af9d4631e27ffc5 (plain)
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
#include <new>

#include "Device.h"

// [Required]
// Called when the device is provisioned during system boot or when a new device is plugged-in while the
// system is running.
//
// https://docs.microsoft.com/windows-hardware/drivers/ddi/content/wdfdriver/nc-wdfdriver-evt_wdf_driver_device_add
NTSTATUS DeviceContext::AddDevice(
    _In_ WDFDRIVER Driver,
    _Inout_ PWDFDEVICE_INIT DeviceInit)
{
    (void)Driver;

    NTSTATUS status;

    // Configure the NFC Class Extension.
    // Note: The NFC_CX_TRANSPORT_TYPE is only used for telemetry purposes. It does not affect the behavior of
    // the driver.
    NFC_CX_CLIENT_CONFIG nfcCxConfig;
    NFC_CX_CLIENT_CONFIG_INIT(&nfcCxConfig, NFC_CX_TRANSPORT_CUSTOM);

#ifdef ENABLE_IF_IMPLEMENTING_CUSTOM_IOCTLS
    nfcCxConfig.EvtNfcCxDeviceIoControl = IoControl;
#endif

    // Provide the NCI Write callback, which is called by the NFC CX when it has an NCI packet that needs to
    // be sent to the NFC Controller.
    nfcCxConfig.EvtNfcCxWriteNciPacket = WriteNciPacket;

    // Instruct the NFC CX to read the existing NCI configuration values and only update them if necessary.
    nfcCxConfig.DriverFlags = NFC_CX_DRIVER_ENABLE_EEPROM_WRITE_PROTECTION;

    status = NfcCxDeviceInitConfig(DeviceInit, &nfcCxConfig);
    if (!NT_SUCCESS(status))
    {
        return status;
    }

    // Create the PnP power callbacks configuration.
    WDF_PNPPOWER_EVENT_CALLBACKS pnpCallbacks;
    WDF_PNPPOWER_EVENT_CALLBACKS_INIT(&pnpCallbacks);
    pnpCallbacks.EvtDevicePrepareHardware = PrepareHardware;
    pnpCallbacks.EvtDeviceReleaseHardware = ReleaseHardware;
    pnpCallbacks.EvtDeviceD0Entry = D0Entry;
    pnpCallbacks.EvtDeviceD0Exit = D0Exit;

    // Set the PnP power callbacks.
    WdfDeviceInitSetPnpPowerEventCallbacks(DeviceInit, &pnpCallbacks);

    // Create WDF object attributes for the device.
    WDF_OBJECT_ATTRIBUTES deviceAttributes;
    WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&deviceAttributes, DeviceContext);
    deviceAttributes.EvtDestroyCallback = Destroy;

    // Create the device.
    WDFDEVICE device;
    status = WdfDeviceCreate(&DeviceInit, &deviceAttributes, &device);
    if (!NT_SUCCESS(status))
    {
        return status;
    }

    // Get a pointer to the raw memory that WDF allocated for the Device class.
    DeviceContext* context = DeviceGetContext(device);

    // Initialize the raw memory using C++'s placement new operator.
    // https://en.cppreference.com/w/cpp/language/new
    new (context) DeviceContext(device);

    // Let the NFC Class Extension initialize the device.
    status = NfcCxDeviceInitialize(device);
    if (!NT_SUCCESS(status))
    {
        return status;
    }

    // Create the RF config. (Enable everything.)
    NFC_CX_RF_DISCOVERY_CONFIG discoveryConfig;
    NFC_CX_RF_DISCOVERY_CONFIG_INIT(&discoveryConfig);
    discoveryConfig.PollConfig = NFC_CX_POLL_NFC_A | NFC_CX_POLL_NFC_B | NFC_CX_POLL_NFC_F_212 | NFC_CX_POLL_NFC_F_424 | NFC_CX_POLL_NFC_15693 | NFC_CX_POLL_NFC_ACTIVE | NFC_CX_POLL_NFC_A_KOVIO;
    discoveryConfig.NfcIPMode = NFC_CX_NFCIP_NFC_A | NFC_CX_NFCIP_NFC_F_212 | NFC_CX_NFCIP_NFC_F_424 | NFC_CX_NFCIP_NFC_ACTIVE | NFC_CX_NFCIP_NFC_ACTIVE_A | NFC_CX_NFCIP_NFC_ACTIVE_F_212 | NFC_CX_NFCIP_NFC_ACTIVE_F_424;
    discoveryConfig.NfcIPTgtMode = NFC_CX_NFCIP_TGT_NFC_A | NFC_CX_NFCIP_TGT_NFC_F | NFC_CX_NFCIP_TGT_NFC_ACTIVE_A | NFC_CX_NFCIP_TGT_NFC_ACTIVE_F;
    discoveryConfig.NfcCEMode = NFC_CX_CE_NFC_A | NFC_CX_CE_NFC_B | NFC_CX_CE_NFC_F;

    // Set the RF config.
    status = NfcCxSetRfDiscoveryConfig(device, &discoveryConfig);
    if (!NT_SUCCESS(status))
    {
        return status;
    }

#ifdef ENABLE_IF_USING_SEQUENCE_HANDLER_CALLBACKS
    // Set the NFC Class Extension sequence handlers.
    for (int sequenceType = 0; sequenceType != SequenceMaximum; ++sequenceType)
    {
        status = NfcCxRegisterSequenceHandler(device, NFC_CX_SEQUENCE(sequenceType), SequenceHandler);
        if (!NT_SUCCESS(status))
        {
            return status;
        }
    }
#endif

    return STATUS_SUCCESS;
}

DeviceContext::DeviceContext(_In_ WDFDEVICE Device) :
    _Device(Device)
{
}

// [Required]
// Called when the memory for the class is about to be freed by WDF.
//
// https://docs.microsoft.com/windows-hardware/drivers/ddi/content/wdfobject/nc-wdfobject-evt_wdf_object_context_destroy
void DeviceContext::Destroy(
    _In_ WDFOBJECT Object)
{
    // Manually call the destructor for the class.
    // This mirrors the use of the placement new operator in `DeviceContext::AddDevice`.
    DeviceContext* context = DeviceGetContext(Object);
    context->~DeviceContext();
}

// [Likely required]
// Called when the device's hardware resources are ready to be initialized.
//
// https://docs.microsoft.com/windows-hardware/drivers/ddi/content/wdfdevice/nc-wdfdevice-evt_wdf_device_prepare_hardware
NTSTATUS DeviceContext::PrepareHardware(
    _In_ WDFDEVICE Device,
    _In_ WDFCMRESLIST ResourcesRaw,
    _In_ WDFCMRESLIST ResourcesTranslated)
{
    (void)Device;
    (void)ResourcesRaw;
    (void)ResourcesTranslated;

    // FIX ME:
    // Initialize the hardware so that it is ready to accept NCI packets.

    return STATUS_SUCCESS;
}

// [Likely required]
// Called when the device's hardware resources are no longer accessible.
//
// https://docs.microsoft.com/windows-hardware/drivers/ddi/content/wdfdevice/nc-wdfdevice-evt_wdf_device_release_hardware
NTSTATUS DeviceContext::ReleaseHardware(
    _In_ WDFDEVICE Device,
    _In_ WDFCMRESLIST ResourcesTranslated)
{
    (void)Device;
    (void)ResourcesTranslated;

    // FIX ME:
    // If neccessary, free any resources created by PrepareHardware.

    return STATUS_SUCCESS;
}

// [Likely required]
// Called when the NFC Controller is entering the fully powered-on state.
//
// https://docs.microsoft.com/windows-hardware/drivers/ddi/content/wdfdevice/nc-wdfdevice-evt_wdf_device_d0_entry
NTSTATUS DeviceContext::D0Entry(
    _In_ WDFDEVICE Device,
    _In_ WDF_POWER_DEVICE_STATE PreviousState)
{
    (void)PreviousState;

    NTSTATUS status;

    // Invoke the HostActionStart event, so that the NFC Class Extension initializes the NFC Controller (by
    // sending the relevant NCI packets).
    NFC_CX_HARDWARE_EVENT eventArgs = {};
    eventArgs.HostAction = HostActionStart;

    status = NfcCxHardwareEvent(Device, &eventArgs);
    if (!NT_SUCCESS(status))
    {
        return status;
    }

    return STATUS_SUCCESS;
}

// [Likely required]
// Called when the NFC Controller is entering a low power state.
//
// By default, the NFC Class Extension will prevent this callback from being invoked if there is an client
// process that may be using the NFC Controller. So it is okay for the NFC Controller to be fully
// uninitialized here.
//
// https://docs.microsoft.com/windows-hardware/drivers/ddi/content/wdfdevice/nc-wdfdevice-evt_wdf_device_d0_exit
NTSTATUS DeviceContext::D0Exit(
    _In_ WDFDEVICE Device,
    _In_ WDF_POWER_DEVICE_STATE TargetState)
{
    (void)TargetState;

    NTSTATUS status;

    // Invoke the HostActionStop event, so that the NFC Class Extension uninitializes the NFC Controller (by sending
    // the relevant NCI packets).
    NFC_CX_HARDWARE_EVENT eventArgs = {};
    eventArgs.HostAction = HostActionStop;

    status = NfcCxHardwareEvent(Device, &eventArgs);
    if (!NT_SUCCESS(status))
    {
        return status;
    }

    return STATUS_SUCCESS;
}

// [Optional]
// Called when certain state transitions occur within the NFC Class Extension state machine. This can be used to send
// custom commands to the NFC Controller if neccessary.
//
// https://docs.microsoft.com/windows-hardware/drivers/nfc/sequence-handling
void DeviceContext::SequenceHandler(
    _In_ WDFDEVICE Device,
    _In_ NFC_CX_SEQUENCE Sequence,
    _In_ PFN_NFC_CX_SEQUENCE_COMPLETION_ROUTINE CompletionRoutine,
    _In_opt_ WDFCONTEXT CompletionContext)
{
    (void)Sequence;

    // Nothing to do. So complete the sequence handler immediately.
    // Note: CompletionRoutine may be called asynchronously.
    CompletionRoutine(Device, STATUS_SUCCESS, 0, CompletionContext);
}

// [Optional]
// Can be used to implement custom IOCTLs.
//
// The NFC Class Extension registers for the default I/O queue and so it gets the first crack at handling all I/O requests.
// If this callback is enabled, any IOCTL the NFC Class Extension doesn't recognize will be forwarded here.
//
// https://docs.microsoft.com/windows-hardware/drivers/ddi/content/nfccx/nc-nfccx-evt_nfc_cx_device_io_control
void DeviceContext::IoControl(
    _In_ WDFDEVICE Device,
    _In_ WDFREQUEST Request,
    _In_ size_t OutputBufferLength,
    _In_ size_t InputBufferLength,
    _In_ ULONG  IoControlCode)
{
    (void)Device;
    (void)OutputBufferLength;
    (void)InputBufferLength;
    (void)IoControlCode;

    // No custom IOCTLs are currently supported. So complete all I/O requests with a standard error.
    WdfRequestComplete(Request, STATUS_INVALID_DEVICE_STATE);
}

// [Required]
// Called by the NFC Class Extension when an NCI packet must be sent to the NFC Controller.
//
// https://docs.microsoft.com/windows-hardware/drivers/ddi/content/nfccx/nc-nfccx-evt_nfc_cx_write_nci_packet
void DeviceContext::WriteNciPacket(
    _In_ WDFDEVICE Device,
    _In_ WDFREQUEST Request)
{
    (void)Device;

    NTSTATUS status;

    // Get the NCI packet.
    void* nciPacket;
    size_t nciPacketLength;
    status = WdfRequestRetrieveInputBuffer(Request, 0, &nciPacket, &nciPacketLength);
    if (!NT_SUCCESS(status))
    {
        WdfRequestComplete(Request, status);
        return;
    }

    // FIX ME:
    // Send NCI packet to NFC Controller hardware using the relevant bus API.
    // For example,
    //   - I2C or SPI: https://docs.microsoft.com/windows-hardware/drivers/spb/spb-peripheral-device-drivers
    //   - USB: https://docs.microsoft.com/windows-hardware/drivers/usbcon/usb-driver-development-guide

    // FIX ME:
    // Complete I/O request with STATUS_SUCCESS if NCI packet is succesfully sent to the NFC Controller.
    WdfRequestComplete(Request, STATUS_NOT_IMPLEMENTED);
}

// FIX ME:
// Ensure NfcCxNciReadNotification is called when the NFC Controller needs to send an NCI packet to the driver.
// This is usually done in response to a hardware notification. For example,
//   - GPIO interrupt (I2C or SPI): https://docs.microsoft.com/windows-hardware/drivers/gpio/gpio-interrupts
//   - USB continuous reader: https://docs.microsoft.com/windows-hardware/drivers/usbcon/how-to-use-the-continous-reader-for-getting-data-from-a-usb-endpoint--umdf-/
//
// https://docs.microsoft.com/windows-hardware/drivers/ddi/content/nfccx/nf-nfccx-nfccxncireadnotification

// A helper function that forwards NCI packets from the NFC Controller to the NFC Class Extension driver.
//
// NOTE: If the NCI packet is already packaged within an existing WDFMEMORY, then the NfcCxNciReadNotification
// function can be called directly.
NTSTATUS DeviceContext::ReadNciPacket(
    _In_reads_bytes_(nciPacketLength) void* nciPacket,
    _In_ size_t nciPacketLength)
{
    NTSTATUS status;

    if (!_NciReadMemory)
    {
        // Create the WDFMEMORY object that will be used for all NCI reads.
        WDF_OBJECT_ATTRIBUTES memoryAttributes;
        WDF_OBJECT_ATTRIBUTES_INIT(&memoryAttributes);
        memoryAttributes.ParentObject = _Device;

        status = WdfMemoryCreatePreallocated(&memoryAttributes, nciPacket, nciPacketLength, &_NciReadMemory);
        if (!NT_SUCCESS(status))
        {
            return status;
        }
    }
    else
    {
        // Re-assign the WDFMEMORY to point to the new NCI packet.
        status = WdfMemoryAssignBuffer(_NciReadMemory, nciPacket, nciPacketLength);
        if (!NT_SUCCESS(status))
        {
            return status;
        }
    }

    // Note: NfcCxNciReadNotification does not store a reference to the WDFMEMORY object passed to it. So it
    // is safe to free the NCI packet's memory after the call has completed.
    status = NfcCxNciReadNotification(_Device, _NciReadMemory);
    if (!NT_SUCCESS(status))
    {
        return status;
    }

    return STATUS_SUCCESS;
}