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
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
|
/*++
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:
usb.c
Abstract:
Code for handling USB related requests
Author:
Environment:
kernel mode only
Revision History:
--*/
#include "hidusbfx2.h"
#if defined(EVENT_TRACING)
#include "usb.tmh"
#endif
#ifdef ALLOC_PRAGMA
#pragma alloc_text(PAGE, HidFx2EvtDevicePrepareHardware)
#pragma alloc_text(PAGE, HidFx2EvtDeviceD0Exit)
#pragma alloc_text(PAGE, HidFx2ConfigContReaderForInterruptEndPoint)
#pragma alloc_text(PAGE, HidFx2ValidateConfigurationDescriptor)
#endif
NTSTATUS
HidFx2EvtDevicePrepareHardware(
IN WDFDEVICE Device,
IN WDFCMRESLIST ResourceList,
IN WDFCMRESLIST ResourceListTranslated
)
/*++
Routine Description:
In this callback, the driver does whatever is necessary to make the
hardware ready to use. In the case of a USB device, this involves
reading and selecting descriptors.
Arguments:
Device - handle to a device
ResourceList - A handle to a framework resource-list object that
identifies the raw hardware resourcest
ResourceListTranslated - A handle to a framework resource-list object
that identifies the translated hardware resources
Return Value:
NT status value
--*/
{
NTSTATUS status = STATUS_SUCCESS;
PDEVICE_EXTENSION devContext = NULL;
WDF_USB_DEVICE_SELECT_CONFIG_PARAMS configParams;
WDF_OBJECT_ATTRIBUTES attributes;
PUSB_DEVICE_DESCRIPTOR usbDeviceDescriptor = NULL;
UNREFERENCED_PARAMETER(ResourceList);
UNREFERENCED_PARAMETER(ResourceListTranslated);
PAGED_CODE ();
TraceEvents(TRACE_LEVEL_VERBOSE, DBG_INIT,
"HidFx2EvtDevicePrepareHardware Enter\n");
devContext = GetDeviceContext(Device);
//
// Create a WDFUSBDEVICE object. WdfUsbTargetDeviceCreate obtains the
// USB device descriptor and the first USB configuration descriptor from
// the device and stores them. It also creates a framework USB interface
// object for each interface in the device's first configuration.
//
// The parent of each USB device object is the driver's framework driver
// object. The driver cannot change this parent, and the ParentObject
// member or the WDF_OBJECT_ATTRIBUTES structure must be NULL.
//
// We only create device the first time PrepareHardware is called. If
// the device is restarted by pnp manager for resource rebalance, we
// will use the same device handle but then select the interfaces again
// because the USB stack could reconfigure the device on restart.
//
if (devContext->UsbDevice == NULL) {
status = WdfUsbTargetDeviceCreate(Device,
WDF_NO_OBJECT_ATTRIBUTES,
&devContext->UsbDevice);
if (!NT_SUCCESS(status)) {
TraceEvents(TRACE_LEVEL_ERROR, DBG_PNP,
"WdfUsbTargetDeviceCreate failed 0x%x\n", status);
return status;
}
//
// TODO: If you are fetching configuration descriptor from device for
// selecting a configuration or to parse other descriptors, call
// HidFx2ValidateConfigurationDescriptor
// to do basic validation on the descriptors before you access them.
//
}
//
// Select a device configuration by using a
// WDF_USB_DEVICE_SELECT_CONFIG_PARAMS structure to specify USB
// descriptors, a URB, or handles to framework USB interface objects.
//
WDF_USB_DEVICE_SELECT_CONFIG_PARAMS_INIT_SINGLE_INTERFACE( &configParams);
status = WdfUsbTargetDeviceSelectConfig(devContext->UsbDevice,
WDF_NO_OBJECT_ATTRIBUTES,
&configParams);
if(!NT_SUCCESS(status)) {
TraceEvents(TRACE_LEVEL_ERROR, DBG_PNP,
"WdfUsbTargetDeviceSelectConfig failed %!STATUS!\n",
status);
return status;
}
devContext->UsbInterface =
configParams.Types.SingleInterface.ConfiguredUsbInterface;
//
// Get the device descriptor and store it in device context
//
WDF_OBJECT_ATTRIBUTES_INIT(&attributes);
attributes.ParentObject = Device;
status = WdfMemoryCreate(
&attributes,
NonPagedPoolNx,
0,
sizeof(USB_DEVICE_DESCRIPTOR),
&devContext->DeviceDescriptor,
&usbDeviceDescriptor
);
if(!NT_SUCCESS(status)) {
TraceEvents(TRACE_LEVEL_ERROR, DBG_PNP,
"WdfMemoryCreate for Device Descriptor failed %!STATUS!\n",
status);
return status;
}
WdfUsbTargetDeviceGetDeviceDescriptor(
devContext->UsbDevice,
usbDeviceDescriptor
);
//
// Get the Interrupt pipe. There are other endpoints but we are only
// interested in interrupt endpoint since our HID data comes from that
// endpoint. Another way to get the interrupt endpoint is by enumerating
// through all the pipes in a loop and looking for pipe of Interrupt type.
//
devContext->InterruptPipe = WdfUsbInterfaceGetConfiguredPipe(
devContext->UsbInterface,
INTERRUPT_ENDPOINT_INDEX,
NULL);// pipeInfo
if (NULL == devContext->InterruptPipe) {
TraceEvents(TRACE_LEVEL_ERROR, DBG_PNP,
"Failed to get interrupt pipe info\n");
status = STATUS_INVALID_DEVICE_STATE;
return status;
}
//
// Tell the framework that it's okay to read less than
// MaximumPacketSize
//
WdfUsbTargetPipeSetNoMaximumPacketSizeCheck(devContext->InterruptPipe);
//
//configure continuous reader
//
status = HidFx2ConfigContReaderForInterruptEndPoint(devContext);
TraceEvents(TRACE_LEVEL_VERBOSE, DBG_INIT,
"HidFx2EvtDevicePrepareHardware Exit, Status:0x%x\n", status);
return status;
}
NTSTATUS
HidFx2ConfigContReaderForInterruptEndPoint(
PDEVICE_EXTENSION DeviceContext
)
/*++
Routine Description:
This routine configures a continuous reader on the
interrupt endpoint. It's called from the PrepareHarware event.
Arguments:
DeviceContext - Pointer to device context structure
Return Value:
NT status value
--*/
{
WDF_USB_CONTINUOUS_READER_CONFIG contReaderConfig;
NTSTATUS status = STATUS_SUCCESS;
PAGED_CODE ();
TraceEvents(TRACE_LEVEL_VERBOSE, DBG_INIT,
"HidFx2ConfigContReaderForInterruptEndPoint Enter\n");
WDF_USB_CONTINUOUS_READER_CONFIG_INIT(&contReaderConfig,
HidFx2EvtUsbInterruptPipeReadComplete,
DeviceContext, // Context
sizeof(UCHAR)); // TransferLength
//
// Reader requests are not posted to the target automatically.
// Driver must explictly call WdfIoTargetStart to kick start the
// reader. In this sample, it's done in D0Entry.
// By defaut, framework queues two requests to the target
// endpoint. Driver can configure up to 10 requests with CONFIG macro.
//
status = WdfUsbTargetPipeConfigContinuousReader(DeviceContext->InterruptPipe,
&contReaderConfig);
if (!NT_SUCCESS(status)) {
TraceEvents(TRACE_LEVEL_ERROR, DBG_INIT,
"HidFx2ConfigContReaderForInterruptEndPoint failed %x\n",
status);
return status;
}
TraceEvents(TRACE_LEVEL_VERBOSE, DBG_INIT,
"HidFx2ConfigContReaderForInterruptEndPoint Exit, status:0x%x\n", status);
return status;
}
VOID
HidFx2EvtUsbInterruptPipeReadComplete(
WDFUSBPIPE Pipe,
WDFMEMORY Buffer,
size_t NumBytesTransferred,
WDFCONTEXT Context
)
/*++
Routine Description:
This the completion routine of the continuous reader. This can
called concurrently on multiprocessor system if there are
more than one readers configured. So make sure to protect
access to global resources.
Arguments:
Pipe - Handle to WDF USB pipe object
Buffer - This buffer is freed when this call returns.
If the driver wants to delay processing of the buffer, it
can take an additional referrence.
NumBytesTransferred - number of bytes of data that are in the read buffer.
Context - Provided in the WDF_USB_CONTINUOUS_READER_CONFIG_INIT macro
Return Value:
NT status value
--*/
{
PDEVICE_EXTENSION devContext = Context;
UCHAR toggledSwitch = 0;
PUCHAR switchState = NULL;
UCHAR currentSwitchState = 0;
UCHAR previousSwitchState = 0;
UNREFERENCED_PARAMETER(NumBytesTransferred);
UNREFERENCED_PARAMETER(Pipe);
TraceEvents(TRACE_LEVEL_VERBOSE, DBG_INIT,
"HidFx2EvtUsbInterruptPipeReadComplete Enter\n");
//
// Interrupt endpoints sends switch state when first started
// or when resuming from suspend. We need to ignore that data since
// user did not change the switch state.
//
if (devContext->IsPowerUpSwitchState) {
devContext->IsPowerUpSwitchState = FALSE;
TraceEvents(TRACE_LEVEL_INFORMATION, DBG_INIT,
"Dropping interrupt message since received during powerup/resume\n");
return;
}
//
// Make sure that there is data in the read packet. Depending on the device
// specification, it is possible for it to return a 0 length read in
// certain conditions.
//
if (NumBytesTransferred == 0) {
TraceEvents(TRACE_LEVEL_WARNING, DBG_INIT,
"HidFx2EvtUsbInterruptPipeReadComplete Zero length read "
"occured on the Interrupt Pipe's Continuous Reader\n"
);
return;
}
switchState = WdfMemoryGetBuffer(Buffer, NULL);
currentSwitchState = *switchState;
previousSwitchState = devContext->CurrentSwitchState;
//
// we want to know which switch got toggled from 0 to 1
// Since the device returns the state of all the swicthes and not just the
// one that got toggled, we need to store previous state and xor
// it with current state to know whcih one swicth got toggled.
// Further, the toggle is considered "on" only when it changes from 0 to 1
// (and not when it changes from 1 to 0).
//
toggledSwitch = (previousSwitchState ^ currentSwitchState) & currentSwitchState;
TraceEvents(TRACE_LEVEL_INFORMATION, DBG_INIT,
"HidFx2EvtUsbInterruptPipeReadComplete SwitchState %x, "
"prevSwitch:0x%x, x0R:0x%x\n",
currentSwitchState,
previousSwitchState,
toggledSwitch
);
//
// Store switch state in device context
//
devContext->CurrentSwitchState = *switchState;
//if (toggledSwitch != 0) {
devContext->LatestToggledSwitch = toggledSwitch;
//}
//
// Complete pending Read requests if there is at least one switch toggled
// to on position.
//
if (toggledSwitch != 0) {
BOOLEAN inTimerQueue;
//
// Debounce the switchpack. A simple logic is used for debouncing.
// A timer is started for 10 ms everytime there is a switch toggled on.
// If within 10 ms same or another switch gets toggled, the timer gets
// reset for another 10 ms. The HID read request is completed in timer
// function if there is still a switch in toggled-on state. Note that
// debouncing happens at the whole switch pack level (not individual
// switches) which means if two different switches are toggled-on within
// 10 ms only one of them (later one in this case) will get accepted and
// sent to hidclass driver
//
inTimerQueue = WdfTimerStart(
devContext->DebounceTimer,
WDF_REL_TIMEOUT_IN_MS(SWICTHPACK_DEBOUNCE_TIME_IN_MS)
);
TraceEvents(TRACE_LEVEL_INFORMATION, DBG_INIT,
"Debounce Timer started with timeout of %d ms"
" (TimerReturnValue:%d)\n",
SWICTHPACK_DEBOUNCE_TIME_IN_MS, inTimerQueue);
}
TraceEvents(TRACE_LEVEL_VERBOSE, DBG_INIT,
"HidFx2EvtUsbInterruptPipeReadComplete Exit\n");
}
VOID
HidFx2CompleteReadReport(
WDFDEVICE Device
)
/*++
Routine Description
This method handles the completion of the pended request for the
IOCTL_HID_READ_REPORT
Arguments:
Device - Handle to a framework device.
Return Value:
None.
--*/
{
NTSTATUS status = STATUS_SUCCESS;
WDFREQUEST request;
PDEVICE_EXTENSION pDevContext = NULL;
size_t bytesReturned = 0;
#ifndef USE_ALTERNATE_HID_REPORT_DESCRIPTOR
UCHAR toggledSwitch = 0;
#endif // USE_ALTERNATE_HID_REPORT_DESCRIPTOR
ULONG bytesToCopy = 0;
PHIDFX2_INPUT_REPORT inputReport = NULL;
pDevContext = GetDeviceContext(Device);
//
// Check if there are any pending requests in the Interrupt Message Queue.
// If a request is found then complete the pending request.
//
status = WdfIoQueueRetrieveNextRequest(pDevContext->InterruptMsgQueue, &request);
if (NT_SUCCESS(status)) {
//
// IOCTL_HID_READ_REPORT is METHOD_NEITHER so WdfRequestRetrieveOutputBuffer
// will correctly retrieve buffer from Irp->UserBuffer. Remember that
// HIDCLASS provides the buffer in the Irp->UserBuffer field
// irrespective of the ioctl buffer type. However, framework is very
// strict about type checking. You cannot get Irp->UserBuffer by using
// WdfRequestRetrieveOutputMemory if the ioctl is not a METHOD_NEITHER
// internal ioctl.
//
bytesToCopy = sizeof(HIDFX2_INPUT_REPORT);
status = WdfRequestRetrieveOutputBuffer(request,
bytesToCopy,
&inputReport,
&bytesReturned);// BufferLength
if (!NT_SUCCESS(status)) {
TraceEvents(TRACE_LEVEL_ERROR, DBG_IOCTL,
"WdfRequestRetrieveOutputBuffer failed with status: 0x%x\n", status);
} else {
#ifndef USE_ALTERNATE_HID_REPORT_DESCRIPTOR
//
// Map switch pack state. The lower 7 bits of switch pack
// state are mapped to usages in consumer control collection
// while the highest one bit is mapped to sleep usage in system
// control collection
//
toggledSwitch = pDevContext->LatestToggledSwitch;
if (toggledSwitch & CONSUMER_CONTROL_BUTTONS_BIT_MASK) {
//
//these are consumer control buttons
//
TraceEvents(TRACE_LEVEL_INFORMATION, DBG_IOCTL,
"Consumer control SwitchState: 0x%x\n", toggledSwitch);
inputReport->ReportId = CONSUMER_CONTROL_REPORT_ID;
inputReport->SwitchStateAsByte = toggledSwitch;
bytesReturned = bytesToCopy;
}
else if (toggledSwitch & SYSTEM_CONTROL_BUTTONS_BIT_MASK) {
//
// these are system control buttons
//
TraceEvents(TRACE_LEVEL_INFORMATION, DBG_IOCTL,
"System Control SwitchState: 0x%x\n", toggledSwitch);
inputReport->ReportId = SYSTEM_CONTROL_REPORT_ID;
inputReport->SwitchStateAsByte = toggledSwitch;
bytesReturned = bytesToCopy;
}
else {
//
// We can't be here since we already rejected the switch
// state with no swicthes turned on
//
ASSERT(FALSE);
}
#else
//
// Using vendor collection reports instead of HID collections that integrate
// into consumer and system control
//
TraceEvents(TRACE_LEVEL_INFORMATION, DBG_IOCTL,
"Vendor SwitchState: 0x%x\n", pDevContext->CurrentSwitchState);
inputReport->ReportId = DIP_SWITCHES_REPORT_ID;
inputReport->SwitchStateAsByte = pDevContext->CurrentSwitchState;
bytesReturned = bytesToCopy;
#endif // USE_ALTERNATE_HID_REPORT_DESCRIPTOR
}
WdfRequestCompleteWithInformation(request, status, bytesReturned);
} else if (status != STATUS_NO_MORE_ENTRIES) {
TraceEvents(TRACE_LEVEL_ERROR, DBG_IOCTL,
"WdfIoQueueRetrieveNextRequest status %08x\n", status);
}
return;
}
NTSTATUS
HidFx2EvtDeviceD0Entry(
IN WDFDEVICE Device,
IN WDF_POWER_DEVICE_STATE PreviousState
)
/*++
Routine Description:
EvtDeviceD0Entry event callback must perform any operations that are
necessary before the specified device is used. It will be called every
time the hardware needs to be (re-)initialized.
This function is not marked pageable because this function is in the
device power up path. When a function is marked pagable and the code
section is paged out, it will generate a page fault which could impact
the fast resume behavior because the client driver will have to wait
until the system drivers can service this page fault.
This function runs at PASSIVE_LEVEL, even though it is not paged. A
driver can optionally make this function pageable if DO_POWER_PAGABLE
is set. Even if DO_POWER_PAGABLE isn't set, this function still runs
at PASSIVE_LEVEL. In this case, though, the function absolutely must
not do anything that will cause a page fault.
Arguments:
Device - Handle to a framework device object.
PreviousState - Device power state which the device was in most recently.
If the device is being newly started, this will be
PowerDeviceUnspecified.
Return Value:
NTSTATUS
--*/
{
PDEVICE_EXTENSION devContext = NULL;
NTSTATUS status = STATUS_SUCCESS;
UCHAR switchState = 0;
devContext = GetDeviceContext(Device);
TraceEvents(TRACE_LEVEL_VERBOSE, DBG_PNP,
"HidFx2EvtDeviceD0Entry Enter - coming from %s\n",
DbgDevicePowerString(PreviousState));
//
// Retrieve the current switch state and store it in device context
//
status = HidFx2GetSwitchState(Device, &switchState);
if (!NT_SUCCESS(status)) {
TraceEvents(TRACE_LEVEL_ERROR, DBG_PNP,
"Failed to get current swicth state, status: 0x%x\n", status);
return status;
}
devContext->CurrentSwitchState = switchState;
//
// Start the target. This will start the continuous reader
//
status = WdfIoTargetStart(WdfUsbTargetPipeGetIoTarget(devContext->InterruptPipe));
if (NT_SUCCESS(status)) {
devContext->IsPowerUpSwitchState = TRUE;
}
TraceEvents(TRACE_LEVEL_ERROR, DBG_PNP,
"HidFx2EvtDeviceD0Entry Exit, status: 0x%x\n", status);
return status;
}
NTSTATUS
HidFx2EvtDeviceD0Exit(
IN WDFDEVICE Device,
IN WDF_POWER_DEVICE_STATE TargetState
)
/*++
Routine Description:
This routine undoes anything done in EvtDeviceD0Entry. It is called
whenever the device leaves the D0 state, which happens when the device is
stopped, when it is removed, and when it is powered off.
The device is still in D0 when this callback is invoked, which means that
the driver can still touch hardware in this routine.
EvtDeviceD0Exit event callback must perform any operations that are
necessary before the specified device is moved out of the D0 state. If the
driver needs to save hardware state before the device is powered down, then
that should be done here.
This function runs at PASSIVE_LEVEL, though it is generally not paged. A
driver can optionally make this function pageable if DO_POWER_PAGABLE is set.
Even if DO_POWER_PAGABLE isn't set, this function still runs at
PASSIVE_LEVEL. In this case, though, the function absolutely must not do
anything that will cause a page fault.
Arguments:
Device - Handle to a framework device object.
TargetState - Device power state which the device will be put in once this
callback is complete.
Return Value:
Success implies that the device can be used. Failure will result in the
device stack being torn down.
--*/
{
PDEVICE_EXTENSION devContext;
PAGED_CODE();
TraceEvents(TRACE_LEVEL_VERBOSE, DBG_PNP,
"HidFx2EvtDeviceD0Exit Enter- moving to %s\n",
DbgDevicePowerString(TargetState));
devContext = GetDeviceContext(Device);
WdfIoTargetStop(WdfUsbTargetPipeGetIoTarget(
devContext->InterruptPipe), WdfIoTargetCancelSentIo);
TraceEvents(TRACE_LEVEL_ERROR, DBG_PNP,
"HidFx2EvtDeviceD0Exit Exit\n");
return STATUS_SUCCESS;
}
NTSTATUS
HidFx2GetSwitchState(
IN WDFDEVICE Device,
OUT PUCHAR SwitchState
)
/*++
Routine Description:
This function gets the swicth state of teh USB device
Arguments:
Device - Handle to a framework device object.
SwitchState - Pointer to a variable that receives the switch state
Return Value:
Success implies that the device can be used. Failure will result in the
device stack being torn down.
--*/
{
PDEVICE_EXTENSION devContext = NULL;
NTSTATUS status = STATUS_SUCCESS;
WDF_MEMORY_DESCRIPTOR memDesc;
WDF_USB_CONTROL_SETUP_PACKET controlSetupPacket;
ULONG bytesTransferred = 0;
devContext = GetDeviceContext(Device);
//
// set the segment state on the USB device
//
WDF_USB_CONTROL_SETUP_PACKET_INIT_VENDOR(&controlSetupPacket,
BmRequestDeviceToHost,
BmRequestToDevice,
HIDFX2_READ_SWITCH_STATE, // Request
0, // Value
0); // Index
WDF_MEMORY_DESCRIPTOR_INIT_BUFFER(&memDesc,
SwitchState,
sizeof(UCHAR));
status = WdfUsbTargetDeviceSendControlTransferSynchronously(
devContext->UsbDevice,
NULL, // Optional WDFREQUEST
NULL, // PWDF_REQUEST_SEND_OPTIONS
&controlSetupPacket,
&memDesc,
&bytesTransferred
);
if(!NT_SUCCESS(status)) {
TraceEvents(TRACE_LEVEL_ERROR, DBG_IOCTL,
"GetSwitchState: Failed to read switch state - 0x%x \n", status);
} else {
TraceEvents(TRACE_LEVEL_VERBOSE, DBG_IOCTL,
"GetSwitchState: Switch state is 0x%x\n", *SwitchState);
}
return status;
}
VOID
HidFx2EvtTimerFunction(
IN WDFTIMER Timer
)
/*++
Routine Description:
This function gets called when the timeout period of debounce timer elapses.
Arguments:
Timer - Handle to a framework timer object.
Return Value:
none
--*/
{
#ifndef USE_ALTERNATE_HID_REPORT_DESCRIPTOR
PDEVICE_EXTENSION devContext =
GetDeviceContext(WdfTimerGetParentObject(Timer));
//
// Complete the request if there is a swicthed in toggled-on position
//
if (devContext->LatestToggledSwitch != 0) {
HidFx2CompleteReadReport(WdfTimerGetParentObject(Timer));
}
#else
//
// Always complete the read request for the vendor collection
// input report.
//
HidFx2CompleteReadReport(WdfTimerGetParentObject(Timer));
#endif // USE_ALTERNATE_HID_REPORT_DESCRIPTOR
}
USBD_STATUS
HidFx2ValidateConfigurationDescriptor(
IN PUSB_CONFIGURATION_DESCRIPTOR ConfigDesc,
IN ULONG BufferLength,
_Inout_ PUCHAR *Offset
)
/*++
Routine Description:
Validates a USB Configuration Descriptor
Parameters:
ConfigDesc: Pointer to the entire USB Configuration descriptor returned by the device
BufferLength: Known size of buffer pointed to by ConfigDesc (Not wTotalLength)
Offset: if the USBD_STATUS returned is not USBD_STATUS_SUCCESS, offet will
be set to the address within the ConfigDesc buffer where the failure occured.
Return Value:
USBD_STATUS
Success implies the configuration descriptor is valid.
--*/
{
USBD_STATUS status = USBD_STATUS_SUCCESS;
USHORT ValidationLevel = 3;
PAGED_CODE();
//
// Call USBD_ValidateConfigurationDescriptor to validate the descriptors which are present in this supplied configuration descriptor.
// USBD_ValidateConfigurationDescriptor validates that all descriptors are completely contained within the configuration descriptor buffer.
// It also checks for interface numbers, number of endpoints in an interface etc.
// Please refer to msdn documentation for this function for more information.
//
status = USBD_ValidateConfigurationDescriptor( ConfigDesc, BufferLength , ValidationLevel , Offset , POOL_TAG );
if (!(NT_SUCCESS (status)) ){
return status;
}
//
// TODO: You should validate the correctness of other descriptors which are not taken care by USBD_ValidateConfigurationDescriptor
// Check that all such descriptors have size >= sizeof(the descriptor they point to)
// Check for any association between them if required
//
return status;
}
PCHAR
DbgDevicePowerString(
IN WDF_POWER_DEVICE_STATE Type
)
/*++
Updated Routine Description:
DbgDevicePowerString does not change in this stage of the function driver.
--*/
{
switch (Type)
{
case WdfPowerDeviceInvalid:
return "WdfPowerDeviceInvalid";
case WdfPowerDeviceD0:
return "WdfPowerDeviceD0";
case WdfPowerDeviceD1:
return "WdfPowerDeviceD1";
case WdfPowerDeviceD2:
return "WdfPowerDeviceD2";
case WdfPowerDeviceD3:
return "WdfPowerDeviceD3";
case WdfPowerDeviceD3Final:
return "WdfPowerDeviceD3Final";
case WdfPowerDevicePrepareForHibernation:
return "WdfPowerDevicePrepareForHibernation";
case WdfPowerDeviceMaximum:
return "WdfPowerDeviceMaximum";
default:
return "UnKnown Device Power State";
}
}
|