forked from microsoft/Windows-driver-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.cpp
More file actions
1188 lines (959 loc) · 31.4 KB
/
queue.cpp
File metadata and controls
1188 lines (959 loc) · 31.4 KB
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
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*++
Copyright (c) Microsoft Corporation, All Rights Reserved
Module Name:
queue.cpp
Abstract:
This file implements the I/O queue interface and performs
the read/write/ioctl operations.
Environment:
Windows User-Mode Driver Framework (WUDF)
--*/
#include "internal.h"
//
// IUnknown implementation
//
//
// Queue destructor.
// Free up the buffer, wait for thread to terminate and
// delete critical section.
//
CMyQueue::~CMyQueue(
VOID
)
/*++
Routine Description:
IUnknown implementation of Release
Arguments:
Return Value:
--*/
{
WUDF_TEST_DRIVER_ASSERT(m_Device);
m_Device->Release();
}
//
// Initialize
HRESULT
CMyQueue::CreateInstance(
_In_ CMyDevice *pDevice,
_In_ IWDFDevice *FxDevice,
_Out_ PCMyQueue *Queue
)
/*++
Routine Description:
CreateInstance creates an instance of the queue object.
Arguments:
ppUkwn - OUT parameter is an IUnknown interface to the queue object
Return Value:
HRESULT indicating success or failure
--*/
{
CMyQueue *pMyQueue = new CMyQueue(pDevice);
HRESULT hr;
if (pMyQueue == NULL) {
return E_OUTOFMEMORY;
}
hr = pMyQueue->Initialize(FxDevice);
if (SUCCEEDED(hr))
{
*Queue = pMyQueue;
}
else
{
pMyQueue->Release();
}
return hr;
}
HRESULT
CMyQueue::Initialize(
_In_ IWDFDevice *FxDevice
)
{
IWDFIoQueue *fxQueue;
IUnknown *unknown = NULL;
HRESULT hr;
//
// Initialize ring buffer
//
hr = m_RingBuffer.Initialize(DATA_BUFFER_SIZE);
if (FAILED(hr))
{
goto Exit;
}
unknown = QueryIUnknown();
//
// Create the default queue
//
{
hr = FxDevice->CreateIoQueue(unknown,
TRUE,
WdfIoQueueDispatchParallel,
TRUE,
FALSE,
&fxQueue);
}
if (FAILED(hr))
{
goto Exit;
}
m_FxQueue = fxQueue;
fxQueue->Release();
//
// Create a manual queue to hold pending read requests. By keeping
// them in the queue, framework takes care of cancelling them if the app
// exits
//
{
hr = FxDevice->CreateIoQueue(NULL,
FALSE,
WdfIoQueueDispatchManual,
TRUE,
FALSE,
&fxQueue);
}
if (FAILED(hr))
{
goto Exit;
}
m_FxReadQueue = fxQueue;
fxQueue->Release();
//
// Create a manual queue to hold pending ioctl wait-on-mask requests.
//
{
hr = FxDevice->CreateIoQueue(NULL,
FALSE,
WdfIoQueueDispatchManual,
TRUE,
FALSE,
&fxQueue);
}
if (FAILED(hr))
{
goto Exit;
}
m_FxWaitMaskQueue = fxQueue;
fxQueue->Release();
Exit:
SAFE_RELEASE(unknown);
return hr;
}
HRESULT
STDMETHODCALLTYPE
CMyQueue::QueryInterface(
_In_ REFIID InterfaceId,
_Out_ PVOID *Object
)
/*++
Routine Description:
Query Interface
Arguments:
Follows COM specifications
Return Value:
HRESULT indicating success or failure
--*/
{
HRESULT hr;
if (IsEqualIID(InterfaceId, __uuidof(IQueueCallbackWrite))) {
*Object = QueryIQueueCallbackWrite();
hr = S_OK;
} else if (IsEqualIID(InterfaceId, __uuidof(IQueueCallbackRead))) {
*Object = QueryIQueueCallbackRead();
hr = S_OK;
} else if (IsEqualIID(InterfaceId, __uuidof(IQueueCallbackDeviceIoControl))) {
*Object = QueryIQueueCallbackDeviceIoControl();
hr = S_OK;
} else {
hr = CUnknown::QueryInterface(InterfaceId, Object);
}
return hr;
}
VOID
STDMETHODCALLTYPE
CMyQueue::OnDeviceIoControl(
_In_ IWDFIoQueue *pWdfQueue,
_In_ IWDFIoRequest *pWdfRequest,
_In_ ULONG ControlCode,
_In_ SIZE_T InputBufferSizeInBytes,
_In_ SIZE_T OutputBufferSizeInBytes
)
/*++
Routine Description:
DeviceIoControl dispatch routine
Arguments:
pWdfQueue - Framework Queue instance
pWdfRequest - Framework Request instance
ControlCode - IO Control Code
InputBufferSizeInBytes - Length of input buffer
OutputBufferSizeInBytes - Length of output buffer
Always succeeds DeviceIoIoctl
Return Value:
VOID
--*/
{
UNREFERENCED_PARAMETER(OutputBufferSizeInBytes);
UNREFERENCED_PARAMETER(InputBufferSizeInBytes);
UNREFERENCED_PARAMETER(pWdfQueue);
HRESULT hr = S_OK;
SIZE_T reqCompletionInfo = 0;
IWDFMemory *inputMemory = NULL;
IWDFMemory *outputMemory = NULL;
UINT i;
WUDF_TEST_DRIVER_ASSERT(pWdfRequest);
WUDF_TEST_DRIVER_ASSERT(m_Device);
switch (ControlCode)
{
case IOCTL_SERIAL_SET_BAUD_RATE:
{
//
// This is a driver for a virtual serial port. Since there is no
// actual hardware, we just store the baud rate and don't do
// anything with it.
//
SERIAL_BAUD_RATE baudRateBuffer = {0};
pWdfRequest->GetInputMemory(&inputMemory);
if (NULL == inputMemory)
{
hr = HRESULT_FROM_WIN32(ERROR_INVALID_DATA);
}
if (SUCCEEDED(hr))
{
hr = inputMemory->CopyToBuffer(0,
(void*) &baudRateBuffer,
sizeof(SERIAL_BAUD_RATE));
}
if (SUCCEEDED(hr))
{
m_Device->SetBaudRate(baudRateBuffer.BaudRate);
}
break;
}
case IOCTL_SERIAL_GET_BAUD_RATE:
{
SERIAL_BAUD_RATE baudRateBuffer = {0};
baudRateBuffer.BaudRate = m_Device->GetBaudRate();
pWdfRequest->GetOutputMemory(&outputMemory);
if (NULL == outputMemory)
{
hr = HRESULT_FROM_WIN32(ERROR_INVALID_DATA);
}
if (SUCCEEDED(hr))
{
hr = outputMemory->CopyFromBuffer(0,
(void*) &baudRateBuffer,
sizeof(SERIAL_BAUD_RATE));
}
if (SUCCEEDED(hr))
{
reqCompletionInfo = sizeof(SERIAL_BAUD_RATE);
}
break;
}
case IOCTL_SERIAL_SET_MODEM_CONTROL:
{
//
// This is a driver for a virtual serial port. Since there is no
// actual hardware, we just store the modem control register
// configuration and don't do anything with it.
//
ULONG *pModemControlRegister = NULL;
pWdfRequest->GetInputMemory(&inputMemory);
if (NULL == inputMemory)
{
hr = HRESULT_FROM_WIN32(ERROR_INVALID_DATA);
}
if (SUCCEEDED(hr))
{
pModemControlRegister = m_Device->GetModemControlRegisterPtr();
WUDF_TEST_DRIVER_ASSERT(pModemControlRegister);
hr = inputMemory->CopyToBuffer(0,
(void*) pModemControlRegister,
sizeof(ULONG));
}
break;
}
case IOCTL_SERIAL_GET_MODEM_CONTROL:
{
ULONG *pModemControlRegister = NULL;
pWdfRequest->GetOutputMemory(&outputMemory);
if (NULL == outputMemory)
{
hr = HRESULT_FROM_WIN32(ERROR_INVALID_DATA);
}
if (SUCCEEDED(hr))
{
pModemControlRegister = m_Device->GetModemControlRegisterPtr();
WUDF_TEST_DRIVER_ASSERT(pModemControlRegister);
hr = outputMemory->CopyFromBuffer(0,
(void*) pModemControlRegister,
sizeof(ULONG));
}
if (SUCCEEDED(hr))
{
reqCompletionInfo = sizeof(ULONG);
}
break;
}
case IOCTL_SERIAL_SET_FIFO_CONTROL:
{
//
// This is a driver for a virtual serial port. Since there is no
// actual hardware, we just store the FIFO control register
// configuration and don't do anything with it.
//
ULONG *pFifoControlRegister = NULL;
pWdfRequest->GetInputMemory(&inputMemory);
if (NULL == inputMemory)
{
hr = HRESULT_FROM_WIN32(ERROR_INVALID_DATA);
}
if (SUCCEEDED(hr))
{
pFifoControlRegister = m_Device->GetFifoControlRegisterPtr();
hr = inputMemory->CopyToBuffer(0,
(void*) pFifoControlRegister,
sizeof(ULONG));
}
break;
}
case IOCTL_SERIAL_GET_LINE_CONTROL:
{
ULONG *pLineControlRegister = NULL;
SERIAL_LINE_CONTROL lineControl = {0};
ULONG lineControlSnapshot;
pLineControlRegister = m_Device->GetLineControlRegisterPtr();
WUDF_TEST_DRIVER_ASSERT(pLineControlRegister);
//
// Take a snapshot of the line control register variable
//
lineControlSnapshot = *pLineControlRegister;
//
// Decode the word length
//
if ((lineControlSnapshot & SERIAL_DATA_MASK) == SERIAL_5_DATA)
{
lineControl.WordLength = 5;
}
else if ((lineControlSnapshot & SERIAL_DATA_MASK) == SERIAL_6_DATA)
{
lineControl.WordLength = 6;
}
else if ((lineControlSnapshot & SERIAL_DATA_MASK) == SERIAL_7_DATA)
{
lineControl.WordLength = 7;
}
else if ((lineControlSnapshot & SERIAL_DATA_MASK) == SERIAL_8_DATA)
{
lineControl.WordLength = 8;
}
//
// Decode the parity
//
if ((lineControlSnapshot & SERIAL_PARITY_MASK) == SERIAL_NONE_PARITY)
{
lineControl.Parity = NO_PARITY;
}
else if ((lineControlSnapshot & SERIAL_PARITY_MASK) == SERIAL_ODD_PARITY)
{
lineControl.Parity = ODD_PARITY;
}
else if ((lineControlSnapshot & SERIAL_PARITY_MASK) == SERIAL_EVEN_PARITY)
{
lineControl.Parity = EVEN_PARITY;
}
else if ((lineControlSnapshot & SERIAL_PARITY_MASK) == SERIAL_MARK_PARITY)
{
lineControl.Parity = MARK_PARITY;
}
else if ((lineControlSnapshot & SERIAL_PARITY_MASK) == SERIAL_SPACE_PARITY)
{
lineControl.Parity = SPACE_PARITY;
}
//
// Decode the length of the stop bit
//
if (lineControlSnapshot & SERIAL_2_STOP)
{
if (lineControl.WordLength == 5)
{
lineControl.StopBits = STOP_BITS_1_5;
}
else
{
lineControl.StopBits = STOP_BITS_2;
}
}
else
{
lineControl.StopBits = STOP_BIT_1;
}
//
// Copy the information that was decoded to the caller's buffer
//
pWdfRequest->GetOutputMemory(&outputMemory);
if (NULL == outputMemory)
{
hr = HRESULT_FROM_WIN32(ERROR_INVALID_DATA);
}
if (SUCCEEDED(hr))
{
hr = outputMemory->CopyFromBuffer(0,
(void*) &lineControl,
sizeof(SERIAL_LINE_CONTROL));
}
if (SUCCEEDED(hr))
{
reqCompletionInfo = sizeof(SERIAL_LINE_CONTROL);
}
break;
}
case IOCTL_SERIAL_SET_LINE_CONTROL:
{
ULONG *pLineControlRegister = NULL;
SERIAL_LINE_CONTROL lineControl = {0};
UCHAR lineControlData = 0;
UCHAR lineControlStop = 0;
UCHAR lineControlParity = 0;
ULONG lineControlSnapshot;
ULONG lineControlNew;
ULONG lineControlPrevious;
pLineControlRegister = m_Device->GetLineControlRegisterPtr();
WUDF_TEST_DRIVER_ASSERT(pLineControlRegister);
//
// This is a driver for a virtual serial port. Since there is no
// actual hardware, we just store the line control register
// configuration and don't do anything with it.
//
pWdfRequest->GetInputMemory(&inputMemory);
if (NULL == inputMemory)
{
hr = HRESULT_FROM_WIN32(ERROR_INVALID_DATA);
}
if (SUCCEEDED(hr))
{
hr = inputMemory->CopyToBuffer(0,
(void*) &lineControl,
sizeof(SERIAL_LINE_CONTROL));
}
//
// Bits 0 and 1 of the line control register
//
if (SUCCEEDED(hr))
{
switch (lineControl.WordLength)
{
case 5:
lineControlData = SERIAL_5_DATA;
m_Device->SetValidDataMask(0x1f);
break;
case 6:
lineControlData = SERIAL_6_DATA;
m_Device->SetValidDataMask(0x3f);
break;
case 7:
lineControlData = SERIAL_7_DATA;
m_Device->SetValidDataMask(0x7f);
break;
case 8:
lineControlData = SERIAL_8_DATA;
m_Device->SetValidDataMask(0xff);
break;
default:
hr = E_INVALIDARG;
}
}
//
// Bit 2 of the line control register
//
if (SUCCEEDED(hr))
{
switch (lineControl.StopBits)
{
case STOP_BIT_1:
lineControlStop = SERIAL_1_STOP;
break;
case STOP_BITS_1_5:
if (lineControlData != SERIAL_5_DATA)
{
hr = E_INVALIDARG;
break;
}
lineControlStop = SERIAL_1_5_STOP;
break;
case STOP_BITS_2:
if (lineControlData == SERIAL_5_DATA)
{
hr = E_INVALIDARG;
break;
}
lineControlStop = SERIAL_2_STOP;
break;
default:
hr = E_INVALIDARG;
}
}
//
// Bits 3, 4 and 5 of the line control register
//
if (SUCCEEDED(hr))
{
switch (lineControl.Parity)
{
case NO_PARITY:
lineControlParity = SERIAL_NONE_PARITY;
break;
case EVEN_PARITY:
lineControlParity = SERIAL_EVEN_PARITY;
break;
case ODD_PARITY:
lineControlParity = SERIAL_ODD_PARITY;
break;
case SPACE_PARITY:
lineControlParity = SERIAL_SPACE_PARITY;
break;
case MARK_PARITY:
lineControlParity = SERIAL_MARK_PARITY;
break;
default:
hr = E_INVALIDARG;
}
}
//
// Update our line control register variable atomically
//
i=0;
do
{
i++;
if ((i & 0xf) == 0)
{
//
// We've been spinning in a loop for a while trying to
// update the line control register variable atomically.
// Yield the CPU for other threads for a while.
//
SwitchToThread();
}
lineControlSnapshot = *pLineControlRegister;
lineControlNew = (lineControlSnapshot & SERIAL_LCR_BREAK) |
(lineControlData |
lineControlParity |
lineControlStop);
lineControlPrevious = InterlockedCompareExchange((LONG *) pLineControlRegister,
lineControlNew,
lineControlSnapshot);
} while (lineControlPrevious != lineControlSnapshot);
break;
}
case IOCTL_SERIAL_GET_TIMEOUTS:
{
SERIAL_TIMEOUTS timeoutValues = {0};
pWdfRequest->GetOutputMemory(&outputMemory);
if (NULL == outputMemory)
{
hr = HRESULT_FROM_WIN32(ERROR_INVALID_DATA);
}
if (SUCCEEDED(hr))
{
m_Device->GetTimeouts(&timeoutValues);
hr = outputMemory->CopyFromBuffer(0,
(void*) &timeoutValues,
sizeof(timeoutValues));
}
if (SUCCEEDED(hr))
{
reqCompletionInfo = sizeof(SERIAL_TIMEOUTS);
}
break;
}
case IOCTL_SERIAL_SET_TIMEOUTS:
{
SERIAL_TIMEOUTS timeoutValues = {0};
pWdfRequest->GetInputMemory(&inputMemory);
if (NULL == inputMemory)
{
hr = HRESULT_FROM_WIN32(ERROR_INVALID_DATA);
}
if (SUCCEEDED(hr))
{
hr = inputMemory->CopyToBuffer(0,
(void*) &timeoutValues,
sizeof(timeoutValues));
}
if (SUCCEEDED(hr))
{
if ((timeoutValues.ReadIntervalTimeout == MAXULONG) &&
(timeoutValues.ReadTotalTimeoutMultiplier == MAXULONG) &&
(timeoutValues.ReadTotalTimeoutConstant == MAXULONG))
{
hr = E_INVALIDARG;
}
}
if (SUCCEEDED(hr))
{
m_Device->SetTimeouts(timeoutValues);
}
break;
}
case IOCTL_SERIAL_WAIT_ON_MASK:
{
//
// NOTE: the contract is that this ioctl should be marked pending
// and not to be completed until some wait event happens. Therefore
// it is incorrect for the driver to complete the ioctl right away,
// no matter whether success or failure is returned. In either case
// most likely the app will send down another iocl of wait-on-mask
// in a tight loop, and that gets completed again, and again.
// The end result will be high CPU utilization in task manager.
//
// The correct way would be to mark the ioctl as pending, or as in
// WDF world, keep it in a manual queue. Since this is a driver for
// a virtual serial port and there is no actual hardware, there will
// be no wait event happening. The ioctl stays in the queue until
// the calling app decides to no longer wait for it by means of
// sending down a set-wait-mask request.
//
//
// At most one pending wait-on-mask request is expected
//
IWDFIoRequest *pSavedRequest;
hr = m_FxWaitMaskQueue->RetrieveNextRequest(&pSavedRequest);
if (SUCCEEDED(hr))
{
pSavedRequest->Complete(E_FAIL);
//
// RetrieveNextRequest from a manual queue increments the reference
// counter by 1. We need to decrement it, otherwise the request will
// not be released and there will be an object leak.
//
SAFE_RELEASE(pSavedRequest);
}
//
// Keep the request in a manual queue and the framework will take
// care of cancelling them when the app exits.
//
pWdfRequest->ForwardToIoQueue(m_FxWaitMaskQueue);
//
// Instead of "break" out of the switch statement and complete the
// request at the end of this function, use "return" directly.
//
return;
}
case IOCTL_SERIAL_SET_WAIT_MASK:
{
//
// NOTE: the contract says a set-wait-mask will cause any pending
// wait-on-mask to complete with STATUS_SUCCESS and the output wait
// event mask is set to zero. This is also the way for app to break
// out of the loop of sending down wait-on-mask
//
IWDFIoRequest *pSavedRequest;
hr = m_FxWaitMaskQueue->RetrieveNextRequest(&pSavedRequest);
if (SUCCEEDED(hr))
{
pSavedRequest->GetOutputMemory(&outputMemory);
if (NULL == outputMemory)
{
hr = HRESULT_FROM_WIN32(ERROR_INVALID_DATA);
}
if (SUCCEEDED(hr))
{
ULONG eventMask = 0;
hr = outputMemory->CopyFromBuffer(0,
(void*) &eventMask,
sizeof(eventMask));
pSavedRequest->CompleteWithInformation(hr, sizeof(eventMask));
}
else
{
pSavedRequest->Complete(hr);
}
SAFE_RELEASE(pSavedRequest);
//
// outputMemory will be released at the end of the function
//
}
//
// NOTE: The application expects STATUS_SUCCESS for these IOCTLs.
//
hr = S_OK;
break;
}
case IOCTL_SERIAL_SET_QUEUE_SIZE:
case IOCTL_SERIAL_SET_DTR:
case IOCTL_SERIAL_SET_RTS:
case IOCTL_SERIAL_CLR_RTS:
case IOCTL_SERIAL_SET_XON:
case IOCTL_SERIAL_SET_XOFF:
case IOCTL_SERIAL_SET_CHARS:
case IOCTL_SERIAL_GET_CHARS:
case IOCTL_SERIAL_GET_HANDFLOW:
case IOCTL_SERIAL_SET_HANDFLOW:
case IOCTL_SERIAL_RESET_DEVICE:
{
//
// NOTE: The application expects STATUS_SUCCESS for these IOCTLs.
// so don't merge this with default.
//
break;
}
default:
{
hr = E_INVALIDARG;
}
}
//
// clean up
//
if (inputMemory)
{
inputMemory->Release();
}
if (outputMemory)
{
outputMemory->Release();
}
//
// complete the request
//
pWdfRequest->CompleteWithInformation(hr, reqCompletionInfo);
return;
}
VOID
STDMETHODCALLTYPE
CMyQueue::OnWrite(
_In_ IWDFIoQueue *pWdfQueue,
_In_ IWDFIoRequest *pWdfRequest,
_In_ SIZE_T BytesToWrite
)
/*++
Routine Description:
Write dispatch routine
IQueueCallbackWrite
Arguments:
pWdfQueue - Framework Queue instance
pWdfRequest - Framework Request instance
BytesToWrite - Length of bytes in the write buffer
Allocate and copy data to local buffer
Return Value:
VOID
--*/
{
IWDFMemory* pRequestMemory = NULL;
IWDFIoRequest* pSavedRequest = NULL;
SIZE_T availableData = 0;
SIZE_T savedRequestBufferSize = 0;
HRESULT hr = S_OK;
UNREFERENCED_PARAMETER(pWdfQueue);
//
// Get memory object
//
pWdfRequest->GetInputMemory(&pRequestMemory);
//
// Process input
//
ProcessWriteBytes((PUCHAR)pRequestMemory->GetDataBuffer(NULL), BytesToWrite);
//
// Release memory object and complete request
//
SAFE_RELEASE(pRequestMemory);
pWdfRequest->CompleteWithInformation(hr, BytesToWrite);
//
// Get the amount of data available in the ring buffer
//
m_RingBuffer.GetAvailableData(&availableData);
if (availableData > 0)
{
//
// Continue with the next request, if there is one pending
//
hr = m_FxReadQueue->RetrieveNextRequest(&pSavedRequest);
if ((pSavedRequest == NULL) || (FAILED(hr)))
{
goto Exit;
}
pSavedRequest->GetReadParameters(&savedRequestBufferSize, NULL, NULL);
OnRead(m_FxQueue,
pSavedRequest,
savedRequestBufferSize);
//
// RetrieveNextRequest from a manual queue increments the reference
// counter by 1. We need to decrement it, otherwise the request will
// not be released and there will be an object leak.
//
SAFE_RELEASE(pSavedRequest);
}
Exit:
return;
}
VOID
STDMETHODCALLTYPE
CMyQueue::OnRead(
_In_ IWDFIoQueue *pWdfQueue,
_In_ IWDFIoRequest *pWdfRequest,
_In_ SIZE_T SizeInBytes
)
/*++
Routine Description:
Read dispatch routine
IQueueCallbackRead
Arguments:
pWdfQueue - Framework Queue instance
pWdfRequest - Framework Request instance
SizeInBytes - Length of bytes in the read buffer
Copy available data into the read buffer
Return Value:
VOID
--*/
{
IWDFMemory* pRequestMemory = NULL;