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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
|
/*++
Copyright (C) Microsoft Corporation, All Rights Reserved.
Module Name:
Device.cpp
Abstract:
This module contains the implementation of the UMDF Skeleton sample driver's
device callback object.
The skeleton sample device does very little. It does not implement either
of the PNP interfaces so once the device is setup, it won't ever get any
callbacks until the device is removed.
Environment:
Windows User-Mode Driver Framework (WUDF)
--*/
#include "internal.h"
#include "initguid.h"
#include "usb_hw.h"
#include "device.tmh"
CMyDevice::~CMyDevice(
)
{
}
HRESULT
CMyDevice::CreateInstance(
_In_ IWDFDriver *FxDriver,
_In_ IWDFDeviceInitialize * FxDeviceInit,
_Out_ PCMyDevice *Device
)
/*++
Routine Description:
This method creates and initializs an instance of the skeleton driver's
device callback object.
Arguments:
FxDeviceInit - the settings for the device.
Device - a location to store the referenced pointer to the device object.
Return Value:
Status
--*/
{
PCMyDevice device;
HRESULT hr;
//
// Allocate a new instance of the device class.
//
device = new CMyDevice();
if (NULL == device)
{
return E_OUTOFMEMORY;
}
//
// Initialize the instance.
//
hr = device->Initialize(FxDriver, FxDeviceInit);
if (SUCCEEDED(hr))
{
*Device = device;
}
else
{
device->Release();
}
return hr;
}
HRESULT
CMyDevice::Initialize(
_In_ IWDFDriver * FxDriver,
_In_ IWDFDeviceInitialize * FxDeviceInit
)
/*++
Routine Description:
This method initializes the device callback object and creates the
partner device object.
The method should perform any device-specific configuration that:
* could fail (these can't be done in the constructor)
* must be done before the partner object is created -or-
* can be done after the partner object is created and which aren't
influenced by any device-level parameters the parent (the driver
in this case) might set.
Arguments:
FxDeviceInit - the settings for this device.
Return Value:
status.
--*/
{
IWDFDevice *fxDevice = NULL;
HRESULT hr = S_OK;
//
// TODO: If you're writing a filter driver then indicate that here.
//
// FxDeviceInit->SetFilter();
//
//
// Set no locking unless you need an automatic callbacks synchronization
//
FxDeviceInit->SetLockingConstraint(None);
//
// TODO: Any per-device initialization which must be done before
// creating the partner object.
//
//
// Create a new FX device object and assign the new callback object to
// handle any device level events that occur.
//
//
// QueryIUnknown references the IUnknown interface that it returns
// (which is the same as referencing the device). We pass that to
// CreateDevice, which takes its own reference if everything works.
//
if (SUCCEEDED(hr))
{
IUnknown *unknown = this->QueryIUnknown();
hr = FxDriver->CreateDevice(FxDeviceInit, unknown, &fxDevice);
unknown->Release();
}
//
// If that succeeded then set our FxDevice member variable.
//
if (SUCCEEDED(hr))
{
m_FxDevice = fxDevice;
//
// Drop the reference we got from CreateDevice. Since this object
// is partnered with the framework object they have the same
// lifespan - there is no need for an additional reference.
//
fxDevice->Release();
}
return hr;
}
HRESULT
CMyDevice::Configure(
VOID
)
/*++
Routine Description:
This method is called after the device callback object has been initialized
and returned to the driver. It would setup the device's queues and their
corresponding callback objects.
Arguments:
FxDevice - the framework device object for which we're handling events.
Return Value:
status
--*/
{
HRESULT hr = S_OK;
//
// Create the control queue and configure forwarding for IOCTL requests.
//
if (SUCCEEDED(hr))
{
hr = CMyControlQueue::CreateInstance(this, &m_ControlQueue);
if (SUCCEEDED(hr))
{
hr = m_ControlQueue->Configure();
if (SUCCEEDED(hr))
{
hr = m_FxDevice->ConfigureRequestDispatching(
m_ControlQueue->GetFxQueue(),
WdfRequestDeviceIoControl,
true
);
}
m_ControlQueue->Release();
}
}
if (SUCCEEDED(hr))
{
hr = m_FxDevice->CreateDeviceInterface(&GUID_DEVINTERFACE_OSRUSBFX2,
NULL);
}
return hr;
}
HRESULT
CMyDevice::QueryInterface(
_In_ REFIID InterfaceId,
_Outptr_ PVOID *Object
)
/*++
Routine Description:
This method is called to get a pointer to one of the object's callback
interfaces.
Since the skeleton driver doesn't support any of the device events, this
method simply calls the base class's BaseQueryInterface.
If the skeleton is extended to include device event interfaces then this
method must be changed to check the IID and return pointers to them as
appropriate.
Arguments:
InterfaceId - the interface being requested
Object - a location to store the interface pointer if successful
Return Value:
S_OK or E_NOINTERFACE
--*/
{
HRESULT hr;
if (IsEqualIID(InterfaceId, __uuidof(IPnpCallbackHardware)))
{
*Object = QueryIPnpCallbackHardware();
hr = S_OK;
}
else
{
hr = CUnknown::QueryInterface(InterfaceId, Object);
}
return hr;
}
HRESULT
CMyDevice::OnPrepareHardware(
_In_ IWDFDevice * /* FxDevice */
)
/*++
Routine Description:
This routine is invoked to ready the driver
to talk to hardware. It opens the handle to the
device and talks to it using the WINUSB interface.
It invokes WINUSB to discver the interfaces and stores
the information related to bulk endpoints.
Arguments:
FxDevice : Pointer to the WDF device interface
Return Value:
HRESULT
--*/
{
PWSTR deviceName = NULL;
DWORD deviceNameCch = 0;
HRESULT hr;
//
// Get the device name.
// Get the length to allocate first
//
hr = m_FxDevice->RetrieveDeviceName(NULL, &deviceNameCch);
if (FAILED(hr))
{
TraceEvents(TRACE_LEVEL_ERROR,
TEST_TRACE_DEVICE,
"%!FUNC! Cannot get device name %!hresult!",
hr
);
}
//
// Allocate the buffer
//
if (SUCCEEDED(hr))
{
deviceName = new WCHAR[deviceNameCch];
if (deviceName == NULL)
{
hr = E_OUTOFMEMORY;
}
}
//
// Get the actual name
//
if (SUCCEEDED(hr))
{
hr = m_FxDevice->RetrieveDeviceName(deviceName, &deviceNameCch);
if (FAILED(hr))
{
TraceEvents(TRACE_LEVEL_ERROR,
TEST_TRACE_DEVICE,
"%!FUNC! Cannot get device name %!hresult!",
hr
);
}
}
if (SUCCEEDED(hr))
{
TraceEvents(TRACE_LEVEL_INFORMATION,
TEST_TRACE_DEVICE,
"%!FUNC! Device name %S",
deviceName
);
}
//
// Create USB I/O Targets and configure them
//
if (SUCCEEDED(hr))
{
hr = CreateUsbIoTargets();
}
if (SUCCEEDED(hr))
{
ULONG length = sizeof(m_Speed);
hr = m_pIUsbTargetDevice->RetrieveDeviceInformation(DEVICE_SPEED,
&length,
&m_Speed);
if (FAILED(hr))
{
TraceEvents(TRACE_LEVEL_ERROR,
TEST_TRACE_DEVICE,
"%!FUNC! Cannot get usb device speed information %!HRESULT!",
hr
);
}
}
if (SUCCEEDED(hr))
{
TraceEvents(TRACE_LEVEL_INFORMATION,
TEST_TRACE_DEVICE,
"%!FUNC! Speed - %x\n",
m_Speed
);
}
delete[] deviceName;
return hr;
}
HRESULT
CMyDevice::OnReleaseHardware(
_In_ IWDFDevice * /* FxDevice */
)
/*++
Routine Description:
This routine is invoked when the device is being removed or stopped
It releases all resources allocated for this device.
Arguments:
FxDevice - Pointer to the Device object.
Return Value:
HRESULT - Always succeeds.
--*/
{
//
// Remove I/O target from object tree before any potential subsequent
// OnPrepareHardware creates a new one
//
if (m_pIUsbTargetDevice)
{
m_pIUsbTargetDevice->DeleteWdfObject();
}
return S_OK;
}
HRESULT
CMyDevice::CreateUsbIoTargets(
)
/*++
Routine Description:
This routine creates Usb device, interface and pipe objects
Arguments:
None
Return Value:
HRESULT
--*/
{
HRESULT hr;
IWDFUsbTargetFactory * pIUsbTargetFactory = NULL;
IWDFUsbTargetDevice * pIUsbTargetDevice = NULL;
hr = m_FxDevice->QueryInterface(IID_PPV_ARGS(&pIUsbTargetFactory));
if (FAILED(hr))
{
TraceEvents(TRACE_LEVEL_ERROR,
TEST_TRACE_DEVICE,
"%!FUNC! Cannot get usb target factory %!HRESULT!",
hr
);
}
if (SUCCEEDED(hr))
{
hr = pIUsbTargetFactory->CreateUsbTargetDevice(
&pIUsbTargetDevice);
if (FAILED(hr))
{
TraceEvents(TRACE_LEVEL_ERROR,
TEST_TRACE_DEVICE,
"%!FUNC! Unable to create USB Device I/O Target %!HRESULT!",
hr
);
}
}
if (SUCCEEDED(hr))
{
m_pIUsbTargetDevice = pIUsbTargetDevice;
//
// Release the creation reference as object tree will maintain a reference
//
pIUsbTargetDevice->Release();
}
SAFE_RELEASE(pIUsbTargetFactory);
return hr;
}
HRESULT
CMyDevice::SetBarGraphDisplay(
_In_ PBAR_GRAPH_STATE BarGraphState
)
/*++
Routine Description:
This method synchronously sets the bar graph display on the OSR USB-FX2
device using the buffers in the FxRequest as input.
Arguments:
FxRequest - the request to set the bar-graph info.
Return Value:
Status
--*/
{
WINUSB_CONTROL_SETUP_PACKET setupPacket;
ULONG bytesTransferred;
HRESULT hr = S_OK;
//
// Setup the control packet.
//
WINUSB_CONTROL_SETUP_PACKET_INIT( &setupPacket,
BmRequestHostToDevice,
BmRequestToDevice,
USBFX2LK_SET_BARGRAPH_DISPLAY,
0,
0 );
//
// Issue the request to WinUsb.
//
hr = SendControlTransferSynchronously(
&(setupPacket.WinUsb),
(PUCHAR) BarGraphState,
sizeof(BAR_GRAPH_STATE),
&bytesTransferred
);
return hr;
}
HRESULT
CMyDevice::SendControlTransferSynchronously(
_In_ PWINUSB_SETUP_PACKET SetupPacket,
_Inout_updates_(BufferLength) PBYTE Buffer,
_In_ ULONG BufferLength,
_Out_ PULONG LengthTransferred
)
{
HRESULT hr = S_OK;
HRESULT hrRequest = S_OK;
IWDFIoRequest *pWdfRequest = NULL;
IWDFDriver * FxDriver = NULL;
IWDFMemory * FxMemory = NULL;
IWDFRequestCompletionParams * FxComplParams = NULL;
IWDFUsbRequestCompletionParams * FxUsbComplParams = NULL;
*LengthTransferred = 0;
hr = m_FxDevice->CreateRequest( NULL, //pCallbackInterface
NULL, //pParentObject
&pWdfRequest);
hrRequest = hr;
if (SUCCEEDED(hr))
{
m_FxDevice->GetDriver(&FxDriver);
hr = FxDriver->CreatePreallocatedWdfMemory( Buffer,
BufferLength,
NULL, //pCallbackInterface
pWdfRequest, //pParetObject
&FxMemory );
}
if (SUCCEEDED(hr))
{
hr = m_pIUsbTargetDevice->FormatRequestForControlTransfer( pWdfRequest,
SetupPacket,
FxMemory,
NULL); //TransferOffset
}
if (SUCCEEDED(hr))
{
hr = pWdfRequest->Send( m_pIUsbTargetDevice,
WDF_REQUEST_SEND_OPTION_SYNCHRONOUS,
0); //Timeout
}
if (SUCCEEDED(hr))
{
pWdfRequest->GetCompletionParams(&FxComplParams);
hr = FxComplParams->GetCompletionStatus();
}
if (SUCCEEDED(hr))
{
HRESULT hrQI = FxComplParams->QueryInterface(IID_PPV_ARGS(&FxUsbComplParams));
WUDF_TEST_DRIVER_ASSERT(SUCCEEDED(hrQI));
WUDF_TEST_DRIVER_ASSERT( WdfUsbRequestTypeDeviceControlTransfer ==
FxUsbComplParams->GetCompletedUsbRequestType() );
FxUsbComplParams->GetDeviceControlTransferParameters( NULL,
LengthTransferred,
NULL,
NULL );
}
SAFE_RELEASE(FxUsbComplParams);
SAFE_RELEASE(FxComplParams);
SAFE_RELEASE(FxMemory);
if (SUCCEEDED(hrRequest))
{
pWdfRequest->DeleteWdfObject();
}
SAFE_RELEASE(pWdfRequest);
SAFE_RELEASE(FxDriver);
return hr;
}
|