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
|
/*++
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
--*/
#pragma warning(disable:4200) //
#pragma warning(disable:4201) // nameless struct/union
#pragma warning(disable:4214) // bit field types other than int
#include <initguid.h>
#include <ntddk.h>
#include <ntintsafe.h>
#include "usbdi.h"
#include "usbdlib.h"
#pragma warning(default:4200)
#pragma warning(default:4201)
#pragma warning(default:4214)
#include <wdf.h>
#include <wdfusb.h>
#include "public.h"
#ifndef _H
#define _H
#define POOL_TAG (ULONG) 'SBSU'
#define MAX_SUPPORTED_PACKETS_FOR_SUPER_SPEED 1024
#define MAX_SUPPORTED_PACKETS_FOR_HIGH_SPEED 1024
#define MAX_SUPPORTED_PACKETS_FOR_FULL_SPEED 255
#define DISPATCH_LATENCY_IN_MS 10
#if DBG
#define UsbSamp_DbgPrint(level, _x_) \
if((level) <= DebugLevel) { \
DbgPrint("UsbSamp: "); DbgPrint _x_; \
}
#else
#define UsbSamp_DbgPrint(level, _x_)
#endif
#define MAX_FULL_SPEED_PACKET_SIZE 64
#define MAX_HIGH_SPEED_PACKET_SIZE 512
#define MAX_SUPER_SPEED_PACKET_SIZE 1024
#define MAX_STREAM_VALID_PACKET_SIZE 1024
#define REMOTE_WAKEUP_MASK 0x20
#define DEFAULT_REGISTRY_TRANSFER_SIZE 65536
#define IDLE_CAPS_TYPE IdleUsbSelectiveSuspend
//
// A structure representing the instance information associated with
// this particular device.
//
typedef struct _DEVICE_CONTEXT {
USB_DEVICE_DESCRIPTOR UsbDeviceDescriptor;
PUSB_CONFIGURATION_DESCRIPTOR UsbConfigurationDescriptor;
WDFUSBDEVICE WdfUsbTargetDevice;
ULONG WaitWakeEnable;
BOOLEAN IsDeviceHighSpeed;
BOOLEAN IsDeviceSuperSpeed;
WDFUSBINTERFACE UsbInterface;
UCHAR SelectedAlternateSetting;
UCHAR NumberConfiguredPipes;
ULONG MaximumTransferSize;
WDFQUEUE IsochReadQueue;
WDFQUEUE IsochWriteQueue;
BOOLEAN IsStaticStreamsSupported;
USHORT NumberOfStreamsSupportedByController;
USBD_HANDLE UsbdHandle;
} DEVICE_CONTEXT, *PDEVICE_CONTEXT;
WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(DEVICE_CONTEXT, GetDeviceContext)
//
// This context is associated with every open handle.
//
typedef struct _FILE_CONTEXT {
WDFUSBPIPE Pipe;
} FILE_CONTEXT, *PFILE_CONTEXT;
WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(FILE_CONTEXT, GetFileContext)
#if (NTDDI_VERSION >= NTDDI_WIN8)
typedef struct _USBSAMP_STREAM_INFO {
// Number of enabled streams on this pipe
ULONG NumberOfStreams;
// Array of stream information structures representing streams on this pipe
PUSBD_STREAM_INFORMATION StreamList;
} USBSAMP_STREAM_INFO, *PUSBSAMP_STREAM_INFO;
#endif
//
// This context is associated with every pipe handle. In this sample,
// it used for isoch transfers.
//
typedef struct _PIPE_CONTEXT {
ULONG NextFrameNumber;
ULONG TransferSizePerMicroframe;
ULONG TransferSizePerFrame;
BOOLEAN StreamConfigured;
#if (NTDDI_VERSION >= NTDDI_WIN8)
USBSAMP_STREAM_INFO StreamInfo;
#endif
} PIPE_CONTEXT, *PPIPE_CONTEXT;
WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(PIPE_CONTEXT, GetPipeContext)
//
// This context is associated with every request received by the driver
// from the app.
//
typedef struct _REQUEST_CONTEXT {
WDFMEMORY UrbMemory;
PMDL Mdl;
ULONG Length; // remaining to xfer
ULONG Numxfer;
ULONG_PTR VirtualAddress; // va for next segment of xfer.
BOOLEAN Read; // TRUE if Read
} REQUEST_CONTEXT, * PREQUEST_CONTEXT;
WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(REQUEST_CONTEXT, GetRequestContext)
typedef struct _WORKITEM_CONTEXT {
WDFDEVICE Device;
WDFUSBPIPE Pipe;
} WORKITEM_CONTEXT, *PWORKITEM_CONTEXT;
WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(WORKITEM_CONTEXT, GetWorkItemContext)
extern ULONG DebugLevel;
DRIVER_INITIALIZE DriverEntry;
EVT_WDF_DRIVER_DEVICE_ADD UsbSamp_EvtDeviceAdd;
EVT_WDF_DEVICE_PREPARE_HARDWARE UsbSamp_EvtDevicePrepareHardware;
EVT_WDF_OBJECT_CONTEXT_CLEANUP UsbSamp_EvtDeviceContextCleanup;
#if (NTDDI_VERSION >= NTDDI_WIN8)
EVT_WDF_OBJECT_CONTEXT_CLEANUP UsbSamp_EvtPipeContextCleanup;
#endif
EVT_WDF_DEVICE_FILE_CREATE UsbSamp_EvtDeviceFileCreate;
EVT_WDF_IO_QUEUE_IO_READ UsbSamp_EvtIoRead;
EVT_WDF_IO_QUEUE_IO_WRITE UsbSamp_EvtIoWrite;
EVT_WDF_IO_QUEUE_IO_DEVICE_CONTROL UsbSamp_EvtIoDeviceControl;
EVT_WDF_REQUEST_COMPLETION_ROUTINE UsbSamp_EvtReadWriteCompletion;
EVT_WDF_REQUEST_COMPLETION_ROUTINE UsbSamp_EvtIsoRequestCompletionRoutine;
EVT_WDF_IO_QUEUE_IO_STOP UsbSamp_EvtIoStop;
EVT_WDF_IO_QUEUE_STATE UsbSamp_EvtIoQueueReadyNotification;
EVT_WDF_WORKITEM UsbSamp_EvtReadWriteWorkItem;
WDFUSBPIPE
GetPipeFromName(
_In_ PDEVICE_CONTEXT DeviceContext,
_In_ PUNICODE_STRING FileName
);
VOID
ReadWriteIsochEndPoints(
_In_ WDFQUEUE Queue,
_In_ WDFREQUEST Request,
_In_ ULONG Length,
_In_ WDF_REQUEST_TYPE RequestType
);
VOID
ReadWriteBulkEndPoints(
_In_ WDFQUEUE Queue,
_In_ WDFREQUEST Request,
_In_ ULONG Length,
_In_ WDF_REQUEST_TYPE RequestType
);
NTSTATUS
ResetPipe(
_In_ WDFUSBPIPE Pipe
);
NTSTATUS
ResetDevice(
_In_ WDFDEVICE Device
);
NTSTATUS
ReadAndSelectDescriptors(
_In_ WDFDEVICE Device
);
NTSTATUS
ConfigureDevice(
_In_ WDFDEVICE Device
);
NTSTATUS
SelectInterfaces(
_In_ WDFDEVICE Device
);
NTSTATUS
SetPowerPolicy(
_In_ WDFDEVICE Device
);
NTSTATUS
AbortPipes(
_In_ WDFDEVICE Device
);
NTSTATUS
QueuePassiveLevelCallback(
_In_ WDFDEVICE Device,
_In_ WDFUSBPIPE Pipe
);
VOID
PerformIsochTransfer(
_In_ PDEVICE_CONTEXT DeviceContext,
_In_ WDFREQUEST Request,
_In_ ULONG TotalLength
);
VOID
DbgPrintRWContext(
PREQUEST_CONTEXT rwContext
);
NTSTATUS
ReadFdoRegistryKeyValue(
_In_ WDFDRIVER Driver,
_In_ LPWSTR Name,
_Out_ PULONG Value
);
NTSTATUS
InitializePipeContextForHighSpeedDevice(
_In_ WDFUSBPIPE Pipe
);
NTSTATUS
InitializePipeContextForFullSpeedDevice(
_In_ WDFUSBPIPE Pipe
);
NTSTATUS
RetrieveDeviceInformation(
_In_ WDFDEVICE Device
);
USBD_STATUS
UsbSamp_ValidateConfigurationDescriptor(
_In_reads_bytes_(BufferLength) PUSB_CONFIGURATION_DESCRIPTOR ConfigDesc,
_In_ ULONG BufferLength,
_Inout_ PUCHAR *Offset
);
NTSTATUS
GetStackCapability(
_In_ WDFDEVICE Device,
_In_ const GUID* CapabilityType,
_In_ ULONG OutputBufferLength,
_When_(OutputBufferLength == 0, _Pre_null_)
_When_(OutputBufferLength != 0, _Out_writes_bytes_(OutputBufferLength))
PUCHAR OutputBuffer
);
#if (NTDDI_VERSION >= NTDDI_WIN8)
NTSTATUS
InitializePipeContextForSuperSpeedDevice(
_In_ PDEVICE_CONTEXT DeviceContext,
_In_ WDFUSBPIPE Pipe
);
NTSTATUS
InitializePipeContextForSuperSpeedIsochPipe(
_In_ PDEVICE_CONTEXT DeviceContext,
_In_ UCHAR InterfaceNumber,
_In_ WDFUSBPIPE Pipe
);
PUSB_ENDPOINT_DESCRIPTOR
GetEndpointDescriptorForEndpointAddress(
_In_ PDEVICE_CONTEXT DeviceContext,
_In_ UCHAR InterfaceNumber,
_In_ UCHAR EndpointAddress,
_Out_ PUSB_SUPERSPEED_ENDPOINT_COMPANION_DESCRIPTOR *ppEndpointCompanionDescriptor
);
NTSTATUS
InitializePipeContextForSuperSpeedBulkPipe(
_In_ PDEVICE_CONTEXT DeviceContext,
_In_ UCHAR InterfaceNumber,
_In_ WDFUSBPIPE Pipe
);
USBD_PIPE_HANDLE
GetStreamPipeHandleFromBulkPipe(
_In_ WDFUSBPIPE Pipe
);
VOID
ConfigureStreamPipeHandleForRequest(
_In_ WDFREQUEST Request,
_In_ WDFUSBPIPE Pipe
);
#endif
ULONG
GetMaxPacketSize(
_In_ WDFUSBPIPE Pipe,
_In_ PDEVICE_CONTEXT DeviceContext
);
#endif
|