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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
|
/*++
Copyright (C) Microsoft Corporation, All Rights Reserved
Module Name:
Device.h
Abstract:
This module contains the type definitions for the UMDF OSR Fx2 sample
driver's device callback class.
Environment:
Windows User-Mode Driver Framework (WUDF)
--*/
#pragma once
#include "internal.h"
#define ENDPOINT_TIMEOUT 10000
#define NUM_OSRUSB_ENDPOINTS 3
//
// 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
typedef struct {
UCHAR Segments;
} SEVEN_SEGMENT, *PSEVEN_SEGMENT;
//
// Context for the impersonation callback.
//
typedef struct
{
PFILE_PLAYBACK PlaybackInfo;
HANDLE FileHandle;
HRESULT Hr;
} PLAYBACK_IMPERSONATION_CONTEXT, *PPLAYBACK_IMPERSONATION_CONTEXT;
//
// Class for the driver.
//
class CMyDevice :
public CUnknown,
public IPnpCallbackHardware,
public IPnpCallback,
public IPnpCallbackSelfManagedIo,
public IUsbTargetPipeContinuousReaderCallbackReadersFailed,
public IUsbTargetPipeContinuousReaderCallbackReadComplete,
public IImpersonateCallback
{
//
// Private data members.
//
private:
//
// Weak reference to framework device
// Use IWDFDevice2 so we can set power policy settings
//
IWDFDevice2 *m_FxDevice;
//
// Weak reference to the control queue
//
PCMyReadWriteQueue m_ReadWriteQueue;
//
// Weak reference to the control queue
//
PCMyControlQueue m_ControlQueue;
//
// The bus type for this device (used here for
// illustrative purposes only)
//
GUID m_BusTypeGuid;
//
// USB Device I/O Target
//
IWDFUsbTargetDevice * m_pIUsbTargetDevice;
//
// USB Interface
//
IWDFUsbInterface * m_pIUsbInterface;
//
// USB Input pipe for Reads
//
IWDFUsbTargetPipe * m_pIUsbInputPipe;
//
// USB Output pipe for writes
//
IWDFUsbTargetPipe * m_pIUsbOutputPipe;
//
// USB interrupt pipe
//
IWDFUsbTargetPipe * m_pIUsbInterruptPipe;
//
// Use I/O target state management interfaces if you are going to
// support a continuous reader.
//
//
// USB interrupt pipe state management
//
IWDFIoTargetStateManagement * m_pIoTargetInterruptPipeStateMgmt;
//
// Device Speed (Low, Full, High)
//
UCHAR m_Speed;
//
// Current switch state
//
SWITCH_STATE m_SwitchState;
//
// Request to be used for pending reads from interrupt pipe
// (to get switch state change notifications)
//
IWDFIoRequest * m_RequestForPendingRead;
//
// If reads stopped because of a transient problem, the error status
// is stored here.
//
HRESULT m_InterruptReadProblem;
//
// Switch state buffer - this might hold the transient value
// m_SwitchState holds stable value of the switch state
//
SWITCH_STATE m_SwitchStateBuffer;
//
// A manual queue to hold requests for changes in the I/O switch state.
//
IWDFIoQueue * m_SwitchChangeQueue;
//
// Private methods.
//
private:
CMyDevice(
VOID
) :
m_FxDevice(NULL),
m_ControlQueue(NULL),
m_ReadWriteQueue(NULL),
m_SwitchChangeQueue(NULL),
m_pIUsbTargetDevice(NULL),
m_pIUsbInterface(NULL),
m_pIUsbInputPipe(NULL),
m_pIUsbOutputPipe(NULL),
m_pIUsbInterruptPipe(NULL),
m_Speed(0),
m_InterruptReadProblem(S_OK),
m_RequestForPendingRead(NULL),
m_pIoTargetInterruptPipeStateMgmt(NULL)
{
ZeroMemory(&m_BusTypeGuid, sizeof(m_BusTypeGuid));
}
~CMyDevice(
);
HRESULT
Initialize(
_In_ IWDFDriver *FxDriver,
_In_ IWDFDeviceInitialize *FxDeviceInit
);
//
// Helper methods
//
HRESULT
GetBusTypeGuid(
VOID
);
HRESULT
CreateUsbIoTargets(
VOID
);
HRESULT
ConfigureUsbPipes(
);
HRESULT
SetPowerManagement(
VOID
);
HRESULT
IndicateDeviceReady(
VOID
);
//
// Helper functions
//
HRESULT
SendControlTransferSynchronously(
_In_ PWINUSB_SETUP_PACKET SetupPacket,
_Inout_updates_(BufferLength) PBYTE Buffer,
_In_ ULONG BufferLength,
_Out_ PULONG LengthTransferred
);
static
WDF_IO_TARGET_STATE
GetTargetState(
IWDFIoTarget * pTarget
);
VOID
ServiceSwitchChangeQueue(
_In_ SWITCH_STATE NewState,
_In_ HRESULT CompletionStatus,
_In_opt_ IWDFFile *SpecificFile
);
//
// Public methods
//
public:
//
// The factory method used to create an instance of this driver.
//
static
HRESULT
CreateInstance(
_In_ IWDFDriver *FxDriver,
_In_ IWDFDeviceInitialize *FxDeviceInit,
_Out_ PCMyDevice *Device
);
IWDFDevice *
GetFxDevice(
VOID
)
{
return m_FxDevice;
}
HRESULT
Configure(
VOID
);
IPnpCallback *
QueryIPnpCallback(
VOID
)
{
AddRef();
return static_cast<IPnpCallback *>(this);
}
IPnpCallbackHardware *
QueryIPnpCallbackHardware(
VOID
)
{
AddRef();
return static_cast<IPnpCallbackHardware *>(this);
}
IPnpCallbackSelfManagedIo *
QueryIPnpCallbackSelfManagedIo(
VOID
)
{
AddRef();
return static_cast<IPnpCallbackSelfManagedIo *>(this);
}
IUsbTargetPipeContinuousReaderCallbackReadersFailed *
QueryContinousReaderFailureCompletion(
VOID
)
{
AddRef();
return static_cast<IUsbTargetPipeContinuousReaderCallbackReadersFailed *>(this);
}
IUsbTargetPipeContinuousReaderCallbackReadComplete *
QueryContinousReaderCompletion(
VOID
)
{
AddRef();
return static_cast<IUsbTargetPipeContinuousReaderCallbackReadComplete *>(this);
}
IImpersonateCallback *
QueryIImpersonateCallback(
VOID
)
{
AddRef();
return static_cast<IImpersonateCallback *>(this);
}
HRESULT
GetBarGraphDisplay(
_In_ PBAR_GRAPH_STATE BarGraphState
);
HRESULT
SetBarGraphDisplay(
_In_ PBAR_GRAPH_STATE BarGraphState
);
HRESULT
GetSevenSegmentDisplay(
_In_ PSEVEN_SEGMENT SevenSegment
);
HRESULT
SetSevenSegmentDisplay(
_In_ PSEVEN_SEGMENT SevenSegment
);
HRESULT
ReadSwitchState(
_In_ PSWITCH_STATE SwitchState
);
bool
EncodeSegmentValue(
_In_ UCHAR Character,
_Out_ SEVEN_SEGMENT *SevenSegment
);
HRESULT
PlaybackFile(
_In_ PFILE_PLAYBACK PlaybackInfo,
_In_ IWDFIoRequest *FxRequest
);
//
//returns a weak reference to the target USB device
//DO NOT release it
//
IWDFUsbTargetDevice *
GetUsbTargetDevice(
)
{
return m_pIUsbTargetDevice;
}
//
//returns a weak reference to input pipe
//DO NOT release it
//
IWDFUsbTargetPipe *
GetInputPipe(
)
{
return m_pIUsbInputPipe;
}
//
//returns a weak reference to output pipe
//DO NOT release it
//
IWDFUsbTargetPipe *
GetOutputPipe(
)
{
return m_pIUsbOutputPipe;
}
IWDFIoQueue *
GetSwitchChangeQueue(
VOID
)
{
return m_SwitchChangeQueue;
}
PSWITCH_STATE
GetCurrentSwitchState(
VOID
)
{
return &m_SwitchState;
}
HRESULT
ConfigContReaderForInterruptEndPoint(
VOID
);
//
// COM methods
//
public:
//
// IUnknown methods.
//
virtual
ULONG
STDMETHODCALLTYPE
AddRef(
VOID
)
{
return __super::AddRef();
}
_At_(this, __drv_freesMem(object))
virtual
ULONG
STDMETHODCALLTYPE
Release(
VOID
)
{
return __super::Release();
}
virtual
HRESULT
STDMETHODCALLTYPE
QueryInterface(
_In_ REFIID InterfaceId,
_Outptr_ PVOID *Object
);
//
// IPnpCallbackHardware
//
virtual
HRESULT
STDMETHODCALLTYPE
OnPrepareHardware(
_In_ IWDFDevice *FxDevice
);
virtual
HRESULT
STDMETHODCALLTYPE
OnReleaseHardware(
_In_ IWDFDevice *FxDevice
);
//
// IPnpCallback
//
virtual
HRESULT
STDMETHODCALLTYPE
OnD0Entry(
_In_ IWDFDevice* pWdfDevice,
_In_ WDF_POWER_DEVICE_STATE previousState
);
virtual
HRESULT
STDMETHODCALLTYPE
OnD0Exit(
_In_ IWDFDevice* pWdfDevice,
_In_ WDF_POWER_DEVICE_STATE previousState
);
virtual
void
STDMETHODCALLTYPE
OnSurpriseRemoval(
_In_ IWDFDevice* pWdfDevice
);
virtual
HRESULT
STDMETHODCALLTYPE
OnQueryRemove(
_In_ IWDFDevice* pWdfDevice
);
virtual
HRESULT
STDMETHODCALLTYPE
OnQueryStop(
_In_ IWDFDevice* pWdfDevice
);
//
// IPnpCallbackSelfManagedIo
//
virtual
VOID
STDMETHODCALLTYPE
OnSelfManagedIoCleanup(
_In_ IWDFDevice* pWdfDevice
);
virtual
VOID
STDMETHODCALLTYPE
OnSelfManagedIoFlush(
_In_ IWDFDevice* pWdfDevice
);
virtual
HRESULT
STDMETHODCALLTYPE
OnSelfManagedIoInit(
_In_ IWDFDevice* pWdfDevice
);
virtual
HRESULT
STDMETHODCALLTYPE
OnSelfManagedIoRestart(
_In_ IWDFDevice* pWdfDevice
);
virtual
HRESULT
STDMETHODCALLTYPE
OnSelfManagedIoStop(
_In_ IWDFDevice* pWdfDevice
);
virtual
HRESULT
STDMETHODCALLTYPE
OnSelfManagedIoSuspend(
_In_ IWDFDevice* pWdfDevice
);
//
// IUsbTargetPipeContinuousReaderCallbackReadersFailed
//
virtual
BOOL
STDMETHODCALLTYPE
OnReaderFailure(
IWDFUsbTargetPipe * pPipe,
HRESULT hrCompletion
);
//
// IUsbTargetPipeContinuousReaderCallbackReadComplete
//
virtual
VOID
STDMETHODCALLTYPE
OnReaderCompletion(
IWDFUsbTargetPipe * pPipe,
IWDFMemory * pMemory,
SIZE_T NumBytesTransferred,
PVOID Context
);
// IImpersonateCallback
virtual
VOID
STDMETHODCALLTYPE
OnImpersonate(
_In_ PVOID Context
);
};
|