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
|
/*++
Copyright (c) Microsoft Corporation. All rights reserved.
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.
Module Name:
private.h
Abstract:
Contains structure definitions and function prototypes private to
the driver.
Environment:
Kernel mode
--*/
#include <initguid.h>
#include <ntddk.h>
#include "usbdi.h"
#include "usbdlib.h"
#include "public.h"
#include "driverspecs.h"
#include <wdf.h>
#include <wdfusb.h>
#define NTSTRSAFE_LIB
#include <ntstrsafe.h>
#include "trace.h"
//
// Include auto-generated ETW event functions (created by MC.EXE from
// osrusbfx2.man)
//
#include "fx2Events.h"
#ifndef _PRIVATE_H
#define _PRIVATE_H
#define POOL_TAG (ULONG) 'FRSO'
#define _DRIVER_NAME_ "OSRUSBFX2"
#define TEST_BOARD_TRANSFER_BUFFER_SIZE (64*1024)
#define DEVICE_DESC_LENGTH 256
extern const __declspec(selectany) LONGLONG DEFAULT_CONTROL_TRANSFER_TIMEOUT = 5 * -1 * WDF_TIMEOUT_TO_SEC;
//
// Define the vendor commands supported by our device
//
#define USBFX2LK_READ_7SEGMENT_DISPLAY 0xD4
#define USBFX2LK_READ_SWITCHES 0xD6
#define USBFX2LK_READ_BARGRAPH_DISPLAY 0xD7
#define USBFX2LK_SET_BARGRAPH_DISPLAY 0xD8
#define USBFX2LK_IS_HIGH_SPEED 0xD9
#define USBFX2LK_REENUMERATE 0xDA
#define USBFX2LK_SET_7SEGMENT_DISPLAY 0xDB
//
// Define the features that we can clear
// and set on our device
//
#define USBFX2LK_FEATURE_EPSTALL 0x00
#define USBFX2LK_FEATURE_WAKE 0x01
//
// Order of endpoints in the interface descriptor
//
#define INTERRUPT_IN_ENDPOINT_INDEX 0
#define BULK_OUT_ENDPOINT_INDEX 1
#define BULK_IN_ENDPOINT_INDEX 2
//
// A structure representing the instance information associated with
// this particular device.
//
typedef struct _DEVICE_CONTEXT {
WDFUSBDEVICE UsbDevice;
WDFUSBINTERFACE UsbInterface;
WDFUSBPIPE BulkReadPipe;
WDFUSBPIPE BulkWritePipe;
WDFUSBPIPE InterruptPipe;
WDFWAITLOCK ResetDeviceWaitLock;
UCHAR CurrentSwitchState;
WDFQUEUE InterruptMsgQueue;
ULONG UsbDeviceTraits;
//
// The following fields are used during event logging to
// report the events relative to this specific instance
// of the device.
//
WDFMEMORY DeviceNameMemory;
PCWSTR DeviceName;
WDFMEMORY LocationMemory;
PCWSTR Location;
} DEVICE_CONTEXT, *PDEVICE_CONTEXT;
WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(DEVICE_CONTEXT, GetDeviceContext)
extern ULONG DebugLevel;
typedef
NTSTATUS
(*PFN_IO_GET_ACTIVITY_ID_IRP) (
_In_ PIRP Irp,
_Out_ LPGUID Guid
);
typedef
NTSTATUS
(*PFN_IO_SET_DEVICE_INTERFACE_PROPERTY_DATA) (
_In_ PUNICODE_STRING SymbolicLinkName,
_In_ CONST DEVPROPKEY *PropertyKey,
_In_ LCID Lcid,
_In_ ULONG Flags,
_In_ DEVPROPTYPE Type,
_In_ ULONG Size,
_In_opt_ PVOID Data
);
//
// Global function pointer set in DriverEntry
// Check for NULL before using
//
extern PFN_IO_GET_ACTIVITY_ID_IRP g_pIoGetActivityIdIrp;
extern PFN_IO_SET_DEVICE_INTERFACE_PROPERTY_DATA g_pIoSetDeviceInterfacePropertyData;
DRIVER_INITIALIZE DriverEntry;
EVT_WDF_OBJECT_CONTEXT_CLEANUP OsrFxEvtDriverContextCleanup;
EVT_WDF_DRIVER_DEVICE_ADD OsrFxEvtDeviceAdd;
EVT_WDF_DEVICE_PREPARE_HARDWARE OsrFxEvtDevicePrepareHardware;
EVT_WDF_IO_QUEUE_IO_READ OsrFxEvtIoRead;
EVT_WDF_IO_QUEUE_IO_WRITE OsrFxEvtIoWrite;
EVT_WDF_IO_QUEUE_IO_DEVICE_CONTROL OsrFxEvtIoDeviceControl;
EVT_WDF_REQUEST_COMPLETION_ROUTINE EvtRequestReadCompletionRoutine;
EVT_WDF_REQUEST_COMPLETION_ROUTINE EvtRequestWriteCompletionRoutine;
_IRQL_requires_(PASSIVE_LEVEL)
NTSTATUS
ResetPipe(
_In_ WDFUSBPIPE Pipe
);
_IRQL_requires_(PASSIVE_LEVEL)
NTSTATUS
ResetDevice(
_In_ WDFDEVICE Device
);
_IRQL_requires_(PASSIVE_LEVEL)
NTSTATUS
SelectInterfaces(
_In_ WDFDEVICE Device
);
_IRQL_requires_(PASSIVE_LEVEL)
NTSTATUS
ReenumerateDevice(
_In_ PDEVICE_CONTEXT DevContext
);
_IRQL_requires_(PASSIVE_LEVEL)
NTSTATUS
GetBarGraphState(
_In_ PDEVICE_CONTEXT DevContext,
_Out_ PBAR_GRAPH_STATE BarGraphState
);
_IRQL_requires_(PASSIVE_LEVEL)
NTSTATUS
SetBarGraphState(
_In_ PDEVICE_CONTEXT DevContext,
_In_ PBAR_GRAPH_STATE BarGraphState
);
_IRQL_requires_(PASSIVE_LEVEL)
NTSTATUS
GetSevenSegmentState(
_In_ PDEVICE_CONTEXT DevContext,
_Out_ PUCHAR SevenSegment
);
_IRQL_requires_(PASSIVE_LEVEL)
NTSTATUS
SetSevenSegmentState(
_In_ PDEVICE_CONTEXT DevContext,
_In_ PUCHAR SevenSegment
);
_IRQL_requires_(PASSIVE_LEVEL)
NTSTATUS
GetSwitchState(
_In_ PDEVICE_CONTEXT DevContext,
_In_ PSWITCH_STATE SwitchState
);
VOID
OsrUsbIoctlGetInterruptMessage(
_In_ WDFDEVICE Device,
_In_ NTSTATUS ReaderStatus
);
_IRQL_requires_(PASSIVE_LEVEL)
NTSTATUS
OsrFxSetPowerPolicy(
_In_ WDFDEVICE Device
);
_IRQL_requires_(PASSIVE_LEVEL)
NTSTATUS
OsrFxConfigContReaderForInterruptEndPoint(
_In_ PDEVICE_CONTEXT DeviceContext
);
EVT_WDF_USB_READER_COMPLETION_ROUTINE OsrFxEvtUsbInterruptPipeReadComplete;
EVT_WDF_USB_READERS_FAILED OsrFxEvtUsbInterruptReadersFailed;
EVT_WDF_IO_QUEUE_IO_STOP OsrFxEvtIoStop;
EVT_WDF_DEVICE_D0_ENTRY OsrFxEvtDeviceD0Entry;
EVT_WDF_DEVICE_D0_EXIT OsrFxEvtDeviceD0Exit;
EVT_WDF_DEVICE_SELF_MANAGED_IO_FLUSH OsrFxEvtDeviceSelfManagedIoFlush;
_IRQL_requires_(PASSIVE_LEVEL)
BOOLEAN
OsrFxReadFdoRegistryKeyValue(
_In_ PWDFDEVICE_INIT DeviceInit,
_In_ PWCHAR Name,
_Out_ PULONG Value
);
_IRQL_requires_max_(DISPATCH_LEVEL)
VOID
OsrFxEnumerateChildren(
_In_ WDFDEVICE Device
);
_IRQL_requires_(PASSIVE_LEVEL)
VOID
GetDeviceEventLoggingNames(
_In_ WDFDEVICE Device
);
_IRQL_requires_(PASSIVE_LEVEL)
PCHAR
DbgDevicePowerString(
_In_ WDF_POWER_DEVICE_STATE Type
);
_IRQL_requires_(PASSIVE_LEVEL)
USBD_STATUS
OsrFxValidateConfigurationDescriptor(
_In_reads_bytes_(BufferLength) PUSB_CONFIGURATION_DESCRIPTOR ConfigDesc,
_In_ ULONG BufferLength,
_Inout_ PUCHAR *Offset
);
FORCEINLINE
GUID
RequestToActivityId(
_In_ WDFREQUEST Request
)
{
GUID activity = {0};
NTSTATUS status = STATUS_SUCCESS;
if (g_pIoGetActivityIdIrp != NULL) {
//
// Use activity ID generated by application (or IO manager)
//
status = g_pIoGetActivityIdIrp(WdfRequestWdmGetIrp(Request), &activity);
}
if (g_pIoGetActivityIdIrp == NULL || !NT_SUCCESS(status)) {
//
// Fall back to using the WDFREQUEST handle as the activity ID
//
RtlCopyMemory(&activity, &Request, sizeof(WDFREQUEST));
}
return activity;
}
FORCEINLINE
GUID
DeviceToActivityId(
_In_ WDFDEVICE Device
)
{
GUID activity = {0};
RtlCopyMemory(&activity, &Device, sizeof(WDFDEVICE));
return activity;
}
#endif
|