summaryrefslogtreecommitdiff
path: root/biometrics/driver/IoQueue.cpp
blob: ce8837d92ca00e9480183635609c02804acdd02d (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
/*++

    THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
    ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
    THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
    PARTICULAR PURPOSE.

    Copyright (c) Microsoft Corporation. All rights reserved

Module Name:

    IoQueue.cpp

Abstract:

    This file implements the I/O queue interface and performs
    the ioctl operations.

Environment:

    Windows User-Mode Driver Framework (WUDF)

--*/

#include "internal.h"
#include "ioqueue.tmh"

 
HRESULT 
CBiometricIoQueue::CreateInstanceAndInitialize(
    _In_ IWDFDevice *FxDevice,
    _In_ CBiometricDevice *BiometricDevice,
    _Out_ CBiometricIoQueue** Queue
    )
/*++

Routine Description:

    CreateInstanceAndInitialize creates an instance of the queue object.

Arguments:
    

Return Value:

    HRESULT indicating success or failure

--*/
{
    //
    // Create a new instance of the device class
    //
    CComObject<CBiometricIoQueue> *pMyQueue = NULL;
    HRESULT hr = CComObject<CBiometricIoQueue>::CreateInstance( &pMyQueue );

    if (SUCCEEDED(hr)) {

        //
        // Initialize the instance.
        //

        if (NULL != pMyQueue)
        {
            hr = pMyQueue->Initialize(FxDevice, BiometricDevice);
        }

        *Queue = pMyQueue;

    }

    return hr;
}

HRESULT
CBiometricIoQueue::Initialize(
    _In_ IWDFDevice *FxDevice,
    _In_ CBiometricDevice *BiometricDevice
    )
/*++

Routine Description:

    Initialize creates a framework queue and sets up I/O for the queue object.

Arguments:

    FxDevice - Framework device associated with this queue.

    BiometricDevice - Pointer to the Biometric device class object.

Return Value:

    HRESULT indicating success or failure

--*/
{
    IWDFIoQueue *fxQueue = NULL;
    HRESULT hr = S_OK;
    IUnknown *unknown = NULL;
    
    //
    // Make sure we have valid parameters.
    //
    if (FxDevice == NULL) {
        TraceEvents(TRACE_LEVEL_ERROR, 
                   BIOMETRIC_TRACE_QUEUE, 
                   "%!FUNC!Pointer to framework device object is NULL.");
        return (E_INVALIDARG);
    }
    if (BiometricDevice == NULL) {
        TraceEvents(TRACE_LEVEL_ERROR, 
                   BIOMETRIC_TRACE_QUEUE, 
                   "%!FUNC!Pointer to Biometric device is NULL.");
        return (E_INVALIDARG);
    }

    //
    // Create the framework queue
    //

    if (SUCCEEDED(hr)) 
    {
        hr = this->QueryInterface(__uuidof(IUnknown), (void **)&unknown);

    }

    if (SUCCEEDED(hr))
    {
        hr = FxDevice->CreateIoQueue(unknown,
                                     FALSE,     // Default Queue?
                                     WdfIoQueueDispatchParallel,  // Dispatch type
                                     FALSE,     // Power managed?
                                     FALSE,     // Allow zero-length requests?
                                     &fxQueue); // I/O queue
        BiometricSafeRelease(unknown);
    }

    if (FAILED(hr))
    {
        TraceEvents(TRACE_LEVEL_ERROR, 
                   BIOMETRIC_TRACE_QUEUE, 
                   "%!FUNC!Failed to create framework queue.");
        return hr;
    }

    //
    // Configure this queue to filter all Device I/O requests.
    //
    hr = FxDevice->ConfigureRequestDispatching(fxQueue,
                                               WdfRequestDeviceIoControl,
                                               TRUE);

    if (SUCCEEDED(hr))
    {
        m_FxQueue = fxQueue;
        m_BiometricDevice= BiometricDevice;
    }

    //
    // Safe to release here.  The framework keeps a reference to the Queue
    // for the lifetime of the device.
    //
    BiometricSafeRelease(fxQueue);

    return hr;
}

VOID
STDMETHODCALLTYPE
CBiometricIoQueue::OnDeviceIoControl(
    _In_ IWDFIoQueue *FxQueue,
    _In_ IWDFIoRequest *FxRequest,
    _In_ ULONG ControlCode,
    _In_ SIZE_T InputBufferSizeInBytes,
    _In_ SIZE_T OutputBufferSizeInBytes
    )
/*++

Routine Description:


    DeviceIoControl dispatch routine

Aruments:

    FxQueue - Framework Queue instance
    FxRequest - Framework Request  instance
    ControlCode - IO Control Code
    InputBufferSizeInBytes - Lenth of input buffer
    OutputBufferSizeInBytes - Lenth of output buffer

    Always succeeds DeviceIoIoctl
Return Value:

    VOID

--*/
{
    UNREFERENCED_PARAMETER(FxQueue);
    UNREFERENCED_PARAMETER(InputBufferSizeInBytes);
    UNREFERENCED_PARAMETER(OutputBufferSizeInBytes);

    if (m_BiometricDevice == NULL) {
        // We don't have pointer to device object
        TraceEvents(TRACE_LEVEL_ERROR, 
                   BIOMETRIC_TRACE_QUEUE, 
                   "%!FUNC!NULL pointer to device object.");
        FxRequest->Complete(E_POINTER);
        return;
    }

    //
    // Process the IOCTLs
    //

    switch (ControlCode) {

        //
        // Mandatory IOCTLs
        //
        case IOCTL_BIOMETRIC_GET_ATTRIBUTES:
            m_BiometricDevice->OnGetAttributes(FxRequest);
            break;

        case IOCTL_BIOMETRIC_RESET:
            m_BiometricDevice->OnReset(FxRequest);
            break;

        case IOCTL_BIOMETRIC_CALIBRATE:
            m_BiometricDevice->OnCalibrate(FxRequest);
            break;
            
        case IOCTL_BIOMETRIC_GET_SENSOR_STATUS:
            m_BiometricDevice->OnGetSensorStatus(FxRequest);
            break;

        case IOCTL_BIOMETRIC_CAPTURE_DATA:
            m_BiometricDevice->OnCaptureData(FxRequest);
            break;

        //
        // Optional IOCTLs
        //
        case IOCTL_BIOMETRIC_UPDATE_FIRMWARE:
            m_BiometricDevice->OnUpdateFirmware(FxRequest);
            break;

        case IOCTL_BIOMETRIC_GET_SUPPORTED_ALGORITHMS:
            m_BiometricDevice->OnGetSupportedAlgorithms(FxRequest);
            break;

        case IOCTL_BIOMETRIC_GET_INDICATOR:
            m_BiometricDevice->OnGetIndicator(FxRequest);
            break;

        case IOCTL_BIOMETRIC_SET_INDICATOR:
            m_BiometricDevice->OnSetIndicator(FxRequest);
            break;

        default:

            //
            // First check to see if this is for a BIOMETRIC file.
            //
            if ((ControlCode & CTL_CODE(0xFFFFFFFF, 0, 0, 0)) == CTL_CODE(FILE_DEVICE_BIOMETRIC, 0, 0, 0)) {

                if ((ControlCode & IOCTL_BIOMETRIC_VENDOR) == IOCTL_BIOMETRIC_VENDOR) {
                    // This is a vendor IOCTL.
                    m_BiometricDevice->OnControlUnit(FxRequest);
                    break;
                }

            } else {

                // This is a legacy IOCTL - non-Windows Biometric Framework
                TraceEvents(TRACE_LEVEL_ERROR, 
                           BIOMETRIC_TRACE_QUEUE, 
                           "%!FUNC!Legacy control units not supported by the driver.");
 
            }

            //
            // Didn't match any of the above.
            //
            TraceEvents(TRACE_LEVEL_ERROR, 
                       BIOMETRIC_TRACE_QUEUE, 
                       "%!FUNC! Unsupported IOCTL - 0x%x.",
                       ControlCode);
            FxRequest->Complete(HRESULT_FROM_WIN32(ERROR_INVALID_FUNCTION));
            break;

    }

    return;

}