-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
queue.c
963 lines (802 loc) · 26.5 KB
/
queue.c
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
/*++
Copyright (c) Microsoft Corporation, All Rights Reserved
Module Name:
Queue.c
Abstract:
This file implements the I/O queue interface and performs
the read/write/ioctl operations.
Environment:
Windows Driver Framework
--*/
#include "internal.h"
NTSTATUS
QueueCreate(
_In_ PDEVICE_CONTEXT DeviceContext
)
{
NTSTATUS status;
WDFDEVICE device = DeviceContext->Device;
WDF_IO_QUEUE_CONFIG queueConfig;
WDF_OBJECT_ATTRIBUTES queueAttributes;
WDFQUEUE queue;
PQUEUE_CONTEXT queueContext;
//
// Create the default queue
//
WDF_IO_QUEUE_CONFIG_INIT_DEFAULT_QUEUE(
&queueConfig,
WdfIoQueueDispatchParallel);
queueConfig.EvtIoRead = EvtIoRead;
queueConfig.EvtIoWrite = EvtIoWrite;
queueConfig.EvtIoDeviceControl = EvtIoDeviceControl;
WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(
&queueAttributes,
QUEUE_CONTEXT);
status = WdfIoQueueCreate(
device,
&queueConfig,
&queueAttributes,
&queue);
if( !NT_SUCCESS(status) ) {
Trace(TRACE_LEVEL_ERROR,
"Error: WdfIoQueueCreate failed 0x%x", status);
return status;
}
queueContext = GetQueueContext(queue);
queueContext->Queue = queue;
queueContext->DeviceContext = DeviceContext;
//
// 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
//
WDF_IO_QUEUE_CONFIG_INIT(
&queueConfig,
WdfIoQueueDispatchManual);
status = WdfIoQueueCreate(
device,
&queueConfig,
WDF_NO_OBJECT_ATTRIBUTES,
&queue);
if( !NT_SUCCESS(status) ) {
Trace(TRACE_LEVEL_ERROR,
"Error: WdfIoQueueCreate manual queue failed 0x%x", status);
return status;
}
queueContext->ReadQueue = queue;
//
// Create another manual queue to hold pending IOCTL_SERIAL_WAIT_ON_MASK
//
WDF_IO_QUEUE_CONFIG_INIT(
&queueConfig,
WdfIoQueueDispatchManual);
status = WdfIoQueueCreate(
device,
&queueConfig,
WDF_NO_OBJECT_ATTRIBUTES,
&queue);
if( !NT_SUCCESS(status) ) {
Trace(TRACE_LEVEL_ERROR,
"Error: WdfIoQueueCreate manual queue failed 0x%x", status);
return status;
}
queueContext->WaitMaskQueue = queue;
RingBufferInitialize(&queueContext->RingBuffer,
queueContext->Buffer,
sizeof(queueContext->Buffer));
return status;
}
NTSTATUS
RequestCopyFromBuffer(
_In_ WDFREQUEST Request,
_In_ PVOID SourceBuffer,
_In_ size_t NumBytesToCopyFrom
)
{
NTSTATUS status;
WDFMEMORY memory;
status = WdfRequestRetrieveOutputMemory(Request, &memory);
if( !NT_SUCCESS(status) ) {
Trace(TRACE_LEVEL_ERROR,
"Error: WdfRequestRetrieveOutputMemory failed 0x%x", status);
return status;
}
status = WdfMemoryCopyFromBuffer(memory, 0,
SourceBuffer, NumBytesToCopyFrom);
if( !NT_SUCCESS(status) ) {
Trace(TRACE_LEVEL_ERROR,
"Error: WdfMemoryCopyFromBuffer failed 0x%x", status);
return status;
}
WdfRequestSetInformation(Request, NumBytesToCopyFrom);
return status;
}
NTSTATUS
RequestCopyToBuffer(
_In_ WDFREQUEST Request,
_In_ PVOID DestinationBuffer,
_In_ size_t NumBytesToCopyTo
)
{
NTSTATUS status;
WDFMEMORY memory;
status = WdfRequestRetrieveInputMemory(Request, &memory);
if( !NT_SUCCESS(status) ) {
Trace(TRACE_LEVEL_ERROR,
"Error: WdfRequestRetrieveInputMemory failed 0x%x", status);
return status;
}
status = WdfMemoryCopyToBuffer(memory, 0,
DestinationBuffer, NumBytesToCopyTo);
if( !NT_SUCCESS(status) ) {
Trace(TRACE_LEVEL_ERROR,
"Error: WdfMemoryCopyToBuffer failed 0x%x", status);
return status;
}
WdfRequestSetInformation(Request, NumBytesToCopyTo);
return status;
}
VOID
EvtIoDeviceControl(
_In_ WDFQUEUE Queue,
_In_ WDFREQUEST Request,
_In_ size_t OutputBufferLength,
_In_ size_t InputBufferLength,
_In_ ULONG IoControlCode
)
{
NTSTATUS status;
PQUEUE_CONTEXT queueContext = GetQueueContext(Queue);
PDEVICE_CONTEXT deviceContext = queueContext->DeviceContext;
UNREFERENCED_PARAMETER (OutputBufferLength);
UNREFERENCED_PARAMETER (InputBufferLength);
Trace(TRACE_LEVEL_INFO,
"EvtIoDeviceControl 0x%x", IoControlCode);
switch (IoControlCode)
{
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};
status = RequestCopyToBuffer(Request,
&baudRateBuffer,
sizeof(baudRateBuffer));
if( NT_SUCCESS(status) ) {
SetBaudRate(deviceContext, baudRateBuffer.BaudRate);
};
break;
}
case IOCTL_SERIAL_GET_BAUD_RATE:
{
SERIAL_BAUD_RATE baudRateBuffer = {0};
baudRateBuffer.BaudRate = GetBaudRate(deviceContext);
status = RequestCopyFromBuffer(Request,
&baudRateBuffer,
sizeof(baudRateBuffer));
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 *modemControlRegister = GetModemControlRegisterPtr(deviceContext);
ASSERT(modemControlRegister);
status = RequestCopyToBuffer(Request,
modemControlRegister,
sizeof(ULONG));
break;
}
case IOCTL_SERIAL_GET_MODEM_CONTROL:
{
ULONG *modemControlRegister = GetModemControlRegisterPtr(deviceContext);
ASSERT(modemControlRegister);
status = RequestCopyFromBuffer(Request,
modemControlRegister,
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 *fifoControlRegister = GetFifoControlRegisterPtr(deviceContext);
ASSERT(fifoControlRegister);
status = RequestCopyToBuffer(Request,
fifoControlRegister,
sizeof(ULONG));
break;
}
case IOCTL_SERIAL_GET_LINE_CONTROL:
{
status = QueueProcessGetLineControl(
queueContext,
Request);
break;
}
case IOCTL_SERIAL_SET_LINE_CONTROL:
{
status = QueueProcessSetLineControl(
queueContext,
Request);
break;
}
case IOCTL_SERIAL_GET_TIMEOUTS:
{
SERIAL_TIMEOUTS timeoutValues = {0};
status = RequestCopyFromBuffer(Request,
(void*) &timeoutValues,
sizeof(timeoutValues));
break;
}
case IOCTL_SERIAL_SET_TIMEOUTS:
{
SERIAL_TIMEOUTS timeoutValues = {0};
status = RequestCopyToBuffer(Request,
(void*) &timeoutValues,
sizeof(timeoutValues));
if( NT_SUCCESS(status) )
{
if ((timeoutValues.ReadIntervalTimeout == MAXULONG) &&
(timeoutValues.ReadTotalTimeoutMultiplier == MAXULONG) &&
(timeoutValues.ReadTotalTimeoutConstant == MAXULONG))
{
status = STATUS_INVALID_PARAMETER;
}
}
if( NT_SUCCESS(status) ) {
SetTimeouts(deviceContext, timeoutValues);
}
break;
}
case IOCTL_SERIAL_WAIT_ON_MASK:
{
//
// NOTE: A wait-on-mask request should not be completed until either:
// 1) A wait event occurs; or
// 2) A set-wait-mask request is received
//
// This is a driver for a virtual serial port. Since there is no
// actual hardware, we complete the request with some failure code.
//
WDFREQUEST savedRequest;
status = WdfIoQueueRetrieveNextRequest(
queueContext->WaitMaskQueue,
&savedRequest);
if (NT_SUCCESS(status)) {
WdfRequestComplete(savedRequest,
STATUS_UNSUCCESSFUL);
}
//
// Keep the request in a manual queue and the framework will take
// care of cancelling them when the app exits
//
status = WdfRequestForwardToIoQueue(
Request,
queueContext->WaitMaskQueue);
if( !NT_SUCCESS(status) ) {
Trace(TRACE_LEVEL_ERROR,
"Error: WdfRequestForwardToIoQueue failed 0x%x", status);
WdfRequestComplete(Request, status);
}
//
// Instead of "break", use "return" to prevent the current request
// from being completed.
//
return;
}
case IOCTL_SERIAL_SET_WAIT_MASK:
{
//
// NOTE: If a wait-on-mask request is already pending when set-wait-mask
// request is processed, the pending wait-on-event request is completed
// with STATUS_SUCCESS and the output wait event mask is set to zero.
//
WDFREQUEST savedRequest;
status = WdfIoQueueRetrieveNextRequest(
queueContext->WaitMaskQueue,
&savedRequest);
if (NT_SUCCESS(status)) {
ULONG eventMask = 0;
status = RequestCopyFromBuffer(
savedRequest,
&eventMask,
sizeof(eventMask));
WdfRequestComplete(savedRequest, status);
}
//
// NOTE: The application expects STATUS_SUCCESS for these IOCTLs.
//
status = STATUS_SUCCESS;
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.
//
status = STATUS_SUCCESS;
break;
default:
status = STATUS_INVALID_PARAMETER;
break;
}
//
// complete the request
//
WdfRequestComplete(Request, status);
}
VOID
EvtIoWrite(
_In_ WDFQUEUE Queue,
_In_ WDFREQUEST Request,
_In_ size_t Length
)
{
NTSTATUS status;
PQUEUE_CONTEXT queueContext = GetQueueContext(Queue);
WDFMEMORY memory;
WDFREQUEST savedRequest;
size_t availableData = 0;
Trace(TRACE_LEVEL_INFO,
"EvtIoWrite 0x%p", Request);
status = WdfRequestRetrieveInputMemory(Request, &memory);
if( !NT_SUCCESS(status) ) {
Trace(TRACE_LEVEL_ERROR,
"Error: WdfRequestRetrieveInputMemory failed 0x%x", status);
return;
}
//
// Process input
//
status = QueueProcessWriteBytes(
queueContext,
(PUCHAR)WdfMemoryGetBuffer(memory, NULL),
Length);
if( !NT_SUCCESS(status) ) {
return;
}
WdfRequestCompleteWithInformation(Request, status, Length);
//
// Get the amount of data available in the ring buffer
//
RingBufferGetAvailableData(
&queueContext->RingBuffer,
&availableData);
if (availableData == 0) {
return;
}
//
// Continue with the next request, if there is one pending
//
for ( ; ; ) {
status = WdfIoQueueRetrieveNextRequest(
queueContext->ReadQueue,
&savedRequest);
if (!NT_SUCCESS(status)) {
break;
}
status = WdfRequestForwardToIoQueue(
savedRequest,
Queue);
if( !NT_SUCCESS(status) ) {
Trace(TRACE_LEVEL_ERROR,
"Error: WdfRequestForwardToIoQueue failed 0x%x", status);
WdfRequestComplete(savedRequest, status);
}
}
}
VOID
EvtIoRead(
_In_ WDFQUEUE Queue,
_In_ WDFREQUEST Request,
_In_ size_t Length
)
{
NTSTATUS status;
PQUEUE_CONTEXT queueContext = GetQueueContext(Queue);
WDFMEMORY memory;
size_t bytesCopied = 0;
Trace(TRACE_LEVEL_INFO,
"EvtIoRead 0x%p", Request);
status = WdfRequestRetrieveOutputMemory(Request, &memory);
if( !NT_SUCCESS(status) ) {
Trace(TRACE_LEVEL_ERROR,
"Error: WdfRequestRetrieveOutputMemory failed 0x%x", status);
WdfRequestComplete(Request, status);
return;
}
status = RingBufferRead(&queueContext->RingBuffer,
(BYTE*)WdfMemoryGetBuffer(memory, NULL),
Length,
&bytesCopied);
if( !NT_SUCCESS(status) ) {
WdfRequestComplete(Request, status);
return;
}
if (bytesCopied > 0) {
//
// Data was read from buffer succesfully
//
WdfRequestCompleteWithInformation(Request, status, bytesCopied);
return;
}
else {
//
// No data to read. Queue the request for later processing.
//
status = WdfRequestForwardToIoQueue(Request,
queueContext->ReadQueue);
if( !NT_SUCCESS(status) ) {
Trace(TRACE_LEVEL_ERROR,
"Error: WdfRequestForwardToIoQueue failed 0x%x", status);
WdfRequestComplete(Request, status);
}
}
}
NTSTATUS
QueueProcessWriteBytes(
_In_ PQUEUE_CONTEXT QueueContext,
_In_reads_bytes_(Length)
PUCHAR Characters,
_In_ size_t Length
)
/*++
Routine Description:
This function is called when the framework receives IRP_MJ_WRITE
requests from the system. The write event handler(FmEvtIoWrite) calls ProcessWriteBytes.
It parses the Characters passed in and looks for the for sequences "AT" -ok ,
"ATA" --CONNECT, ATD<number> -- CONNECT and sets the state of the device appropriately.
These bytes are placed in the read Buffer to be processed later since this device
works in a loopback fashion.
Arguments:
Characters - Pointer to the write IRP's system buffer.
Length - Length of the IO operation
The default property of the queue is to not dispatch
zero lenght read & write requests to the driver and
complete is with status success. So we will never get
a zero length request.
--*/
{
NTSTATUS status = STATUS_SUCCESS;
UCHAR currentCharacter;
UCHAR connectString[] = "\r\nCONNECT\r\n";
UCHAR connectStringCch = ARRAY_SIZE(connectString) - 1;
UCHAR okString[] = "\r\nOK\r\n";
UCHAR okStringCch = ARRAY_SIZE(okString) - 1;
while (Length != 0) {
currentCharacter = *(Characters++);
Length--;
if(currentCharacter == '\0') {
continue;
}
status = RingBufferWrite(&QueueContext->RingBuffer,
¤tCharacter,
sizeof(currentCharacter));
if( !NT_SUCCESS(status) ) {
return status;
}
switch (QueueContext->CommandMatchState) {
case COMMAND_MATCH_STATE_IDLE:
if ((currentCharacter == 'a') || (currentCharacter == 'A')) {
//
// got an A
//
QueueContext->CommandMatchState = COMMAND_MATCH_STATE_GOT_A;
QueueContext->ConnectCommand = FALSE;
QueueContext->IgnoreNextChar = FALSE;
}
break;
case COMMAND_MATCH_STATE_GOT_A:
if ((currentCharacter == 't') || (currentCharacter == 'T')) {
//
// got a T
//
QueueContext->CommandMatchState = COMMAND_MATCH_STATE_GOT_T;
}
else {
QueueContext->CommandMatchState = COMMAND_MATCH_STATE_IDLE;
}
break;
case COMMAND_MATCH_STATE_GOT_T:
if (! QueueContext->IgnoreNextChar) {
//
// the last char was not a special char
// check for CONNECT command
//
if ((currentCharacter == 'A') || (currentCharacter == 'a')) {
QueueContext->ConnectCommand = TRUE;
}
if ((currentCharacter == 'D') || (currentCharacter == 'd')) {
QueueContext->ConnectCommand = TRUE;
}
}
QueueContext->IgnoreNextChar = TRUE;
if (currentCharacter == '\r') {
//
// got a CR, send a response to the command
//
QueueContext->CommandMatchState = COMMAND_MATCH_STATE_IDLE;
if (QueueContext->ConnectCommand) {
//
// place <cr><lf>CONNECT<cr><lf> in the buffer
//
status = RingBufferWrite(&QueueContext->RingBuffer,
connectString,
connectStringCch);
if( !NT_SUCCESS(status) ) {
return status;
}
//
// connected now raise CD
//
QueueContext->CurrentlyConnected = TRUE;
QueueContext->ConnectionStateChanged = TRUE;
}
else {
//
// place <cr><lf>OK<cr><lf> in the buffer
//
status = RingBufferWrite(&QueueContext->RingBuffer,
okString,
okStringCch);
if( !NT_SUCCESS(status) ) {
return status;
}
}
}
break;
default:
break;
}
}
return status;
}
NTSTATUS
QueueProcessGetLineControl(
_In_ PQUEUE_CONTEXT QueueContext,
_In_ WDFREQUEST Request
)
{
NTSTATUS status;
PDEVICE_CONTEXT deviceContext;
SERIAL_LINE_CONTROL lineControl = {0};
ULONG lineControlSnapshot;
ULONG *lineControlRegister;
deviceContext = QueueContext->DeviceContext;
lineControlRegister = GetLineControlRegisterPtr(deviceContext);
ASSERT(lineControlRegister);
//
// Take a snapshot of the line control register variable
//
lineControlSnapshot = *lineControlRegister;
//
// 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
//
status = RequestCopyFromBuffer(Request,
(void*) &lineControl,
sizeof(lineControl));
return status;
}
NTSTATUS
QueueProcessSetLineControl(
_In_ PQUEUE_CONTEXT QueueContext,
_In_ WDFREQUEST Request
)
{
NTSTATUS status;
PDEVICE_CONTEXT deviceContext;
SERIAL_LINE_CONTROL lineControl = {0};
ULONG *lineControlRegister;
UCHAR lineControlData = 0;
UCHAR lineControlStop = 0;
UCHAR lineControlParity = 0;
ULONG lineControlSnapshot;
ULONG lineControlNew;
ULONG lineControlPrevious;
ULONG i;
deviceContext = QueueContext->DeviceContext;
lineControlRegister = GetLineControlRegisterPtr(deviceContext);
ASSERT(lineControlRegister);
//
// 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.
//
status = RequestCopyToBuffer(Request,
(void*) &lineControl,
sizeof(lineControl));
//
// Bits 0 and 1 of the line control register
//
if( NT_SUCCESS(status) )
{
switch (lineControl.WordLength)
{
case 5:
lineControlData = SERIAL_5_DATA;
SetValidDataMask(deviceContext, 0x1f);
break;
case 6:
lineControlData = SERIAL_6_DATA;
SetValidDataMask(deviceContext, 0x3f);
break;
case 7:
lineControlData = SERIAL_7_DATA;
SetValidDataMask(deviceContext, 0x7f);
break;
case 8:
lineControlData = SERIAL_8_DATA;
SetValidDataMask(deviceContext, 0xff);
break;
default:
status = STATUS_INVALID_PARAMETER;
break;
}
}
//
// Bit 2 of the line control register
//
if( NT_SUCCESS(status) )
{
switch (lineControl.StopBits)
{
case STOP_BIT_1:
lineControlStop = SERIAL_1_STOP;
break;
case STOP_BITS_1_5:
if (lineControlData != SERIAL_5_DATA)
{
status = STATUS_INVALID_PARAMETER;
break;
}
lineControlStop = SERIAL_1_5_STOP;
break;
case STOP_BITS_2:
if (lineControlData == SERIAL_5_DATA)
{
status = STATUS_INVALID_PARAMETER;
break;
}
lineControlStop = SERIAL_2_STOP;
break;
default:
status = STATUS_INVALID_PARAMETER;
break;
}
}
//
// Bits 3, 4 and 5 of the line control register
//
if( NT_SUCCESS(status) )
{
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:
status = STATUS_INVALID_PARAMETER;
break;
}
}
//
// 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.
//
#ifdef _KERNEL_MODE
LARGE_INTEGER interval;
interval.QuadPart = 0;
KeDelayExecutionThread(UserMode, FALSE, &interval);
#else
SwitchToThread();
#endif
}
lineControlSnapshot = *lineControlRegister;
lineControlNew = (lineControlSnapshot & SERIAL_LCR_BREAK) |
(lineControlData | lineControlParity | lineControlStop);
lineControlPrevious = InterlockedCompareExchange(
(LONG *) lineControlRegister,
lineControlNew,
lineControlSnapshot);
} while (lineControlPrevious != lineControlSnapshot);
return status;
}