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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
|
/*++
Copyright (c) Microsoft Corporation
Module Name:
write.c
Abstract:
This module contains the code that is very specific to write
operations in the serial driver
Environment:
Kernel mode
--*/
#include "precomp.h"
#if defined(EVENT_TRACING)
#include "write.tmh"
#endif
EVT_WDF_REQUEST_CANCEL SerialCancelCurrentWrite;
EVT_WDF_REQUEST_CANCEL SerialCancelCurrentXoff;
EVT_WDF_INTERRUPT_SYNCHRONIZE SerialGiveWriteToIsr;
EVT_WDF_INTERRUPT_SYNCHRONIZE SerialGiveXoffToIsr;
EVT_WDF_INTERRUPT_SYNCHRONIZE SerialGrabWriteFromIsr;
EVT_WDF_INTERRUPT_SYNCHRONIZE SerialGrabXoffFromIsr;
VOID
SerialEvtIoWrite(
IN WDFQUEUE Queue,
IN WDFREQUEST Request,
IN size_t Length
)
/*++
Routine Description:
This is the dispatch routine for write. It validates the parameters
for the write request and if all is ok then it places the request
on the work queue.
Arguments:
Queue - Handle to the framework queue object that is associated
with the I/O request.
Request - Pointer to the WDFREQUEST for the current request
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.
Return Value:
--*/
{
PSERIAL_DEVICE_EXTENSION extension;
NTSTATUS status;
WDFDEVICE hDevice;
WDF_REQUEST_PARAMETERS params;
PREQUEST_CONTEXT reqContext;
size_t bufLen;
hDevice = WdfIoQueueGetDevice(Queue);
extension = SerialGetDeviceExtension(hDevice);
SerialDbgPrintEx(TRACE_LEVEL_INFORMATION, DBG_WRITE,
">SerialEvtIoWrite(%p, 0x%I64x)\n", Request, Length);
if (SerialCompleteIfError(extension, Request) != STATUS_SUCCESS) {
SerialDbgPrintEx(TRACE_LEVEL_INFORMATION, DBG_WRITE, "<SerialEvtIoWrite (2) %d\n", STATUS_CANCELLED);
return;
}
WDF_REQUEST_PARAMETERS_INIT(¶ms);
WdfRequestGetParameters(
Request,
¶ms
);
//
// Initialize the scratch area of the request.
//
reqContext = SerialGetRequestContext(Request);
reqContext->MajorFunction = params.Type;
reqContext->Length = (ULONG) Length;
status = WdfRequestRetrieveInputBuffer (Request, Length, &reqContext->SystemBuffer, &bufLen);
if (!NT_SUCCESS (status)) {
SerialCompleteRequest(Request , status, 0);
SerialDbgPrintEx(TRACE_LEVEL_INFORMATION, DBG_WRITE, "<SerialEvtIoWrite (4) %X\n", status);
return;
}
SerialStartOrQueue(extension, Request, extension->WriteQueue,
&extension->CurrentWriteRequest,
SerialStartWrite);
SerialDbgPrintEx(TRACE_LEVEL_INFORMATION, DBG_WRITE, "<SerialEvtIoWrite (5) %X\n", status);
return ;
}
VOID
SerialStartWrite(
IN PSERIAL_DEVICE_EXTENSION Extension
)
/*++
Routine Description:
This routine is used to start off any write. It initializes
the Iostatus fields of the request. It will set up any timers
that are used to control the write.
Arguments:
Extension - Points to the serial device extension
Return Value:
--*/
{
LARGE_INTEGER TotalTime;
BOOLEAN UseATimer;
SERIAL_TIMEOUTS Timeouts;
PREQUEST_CONTEXT reqContext;
PREQUEST_CONTEXT reqContextXoff;
SerialDbgPrintEx(TRACE_LEVEL_INFORMATION, DBG_WRITE,
">SerialStartWrite(%p)\n", Extension);
TotalTime.QuadPart = 0;
do {
reqContext = SerialGetRequestContext(Extension->CurrentWriteRequest);
//
// If there is an xoff counter then complete it.
//
//
// We see if there is a actually an Xoff counter request.
//
// If there is, we put the write request back on the head
// of the write list. We then complete the xoff counter.
// The xoff counter completing code will actually make the
// xoff counter back into the current write request, and
// in the course of completing the xoff (which is now
// the current write) we will restart this request.
//
if (Extension->CurrentXoffRequest) {
reqContextXoff =
SerialGetRequestContext(Extension->CurrentXoffRequest);
if (SERIAL_REFERENCE_COUNT(reqContextXoff)) {
//
// The reference count is non-zero. This implies that
// the xoff request has not made it through the completion
// path yet. We will increment the reference count
// and attempt to complete it ourseleves.
//
SERIAL_SET_REFERENCE(
reqContextXoff,
SERIAL_REF_XOFF_REF
);
reqContextXoff->Information = 0;
//
// The following call will actually release the
// cancel spin lock.
//
SerialTryToCompleteCurrent(
Extension,
SerialGrabXoffFromIsr,
STATUS_SERIAL_MORE_WRITES,
&Extension->CurrentXoffRequest,
NULL,
NULL,
Extension->XoffCountTimer,
NULL,
NULL,
SERIAL_REF_XOFF_REF
);
} else {
//
// The request is well on its way to being finished.
// We can let the regular completion code do the
// work. Just release the spin lock.
//
}
}
UseATimer = FALSE;
//
// Calculate the timeout value needed for the
// request. Note that the values stored in the
// timeout record are in milliseconds. Note that
// if the timeout values are zero then we won't start
// the timer.
//
Timeouts = Extension->Timeouts;
if (Timeouts.WriteTotalTimeoutConstant ||
Timeouts.WriteTotalTimeoutMultiplier) {
UseATimer = TRUE;
//
// We have some timer values to calculate.
//
// Take care, we might have an xoff counter masquerading
// as a write.
//
TotalTime.QuadPart =
((LONGLONG)((UInt32x32To64(
(reqContext->MajorFunction == IRP_MJ_WRITE)?
(reqContext->Length) : (1),
Timeouts.WriteTotalTimeoutMultiplier
)
+ Timeouts.WriteTotalTimeoutConstant)))
* -10000;
}
//
// The request may be going to the isr shortly. Now
// is a good time to initialize its reference counts.
//
SERIAL_INIT_REFERENCE(reqContext);
//
// We give the request to to the isr to write out.
// We set a cancel routine that knows how to
// grab the current write away from the isr.
//
SerialSetCancelRoutine(Extension->CurrentWriteRequest,
SerialCancelCurrentWrite);
if (UseATimer) {
BOOLEAN result;
result = SerialSetTimer(
Extension->WriteRequestTotalTimer,
TotalTime
);
if(result == FALSE) {
//
// This timer now has a reference to the request.
//
SERIAL_SET_REFERENCE( reqContext, SERIAL_REF_TOTAL_TIMER );
}
}
WdfInterruptSynchronize(
Extension->WdfInterrupt,
SerialGiveWriteToIsr,
Extension
);
} WHILE (FALSE);
SerialDbgPrintEx(TRACE_LEVEL_INFORMATION, DBG_WRITE, "<SerialStartWrite \n");
return;
}
VOID
SerialGetNextWrite(
IN WDFREQUEST *CurrentOpRequest,
IN WDFQUEUE QueueToProcess,
IN WDFREQUEST *NewRequest,
IN BOOLEAN CompleteCurrent,
PSERIAL_DEVICE_EXTENSION Extension
)
/*++
Routine Description:
This routine completes the old write as well as getting
a pointer to the next write.
The reason that we have have pointers to the current write
queue as well as the current write request is so that this
routine may be used in the common completion code for
read and write.
Arguments:
CurrentOpRequest - Pointer to the pointer that points to the
current write request.
QueueToProcess - Pointer to the write queue.
NewRequest - A pointer to a pointer to the request that will be the
current request. Note that this could end up pointing
to a null pointer. This does NOT necessaryly mean
that there is no current write. What could occur
is that while the cancel lock is held the write
queue ended up being empty, but as soon as we release
the cancel spin lock a new request came in from
SerialStartWrite.
CompleteCurrent - Flag indicates whether the CurrentOpRequest should
be completed.
Return Value:
None.
--*/
{
PREQUEST_CONTEXT reqContext;
SerialDbgPrintEx(TRACE_LEVEL_INFORMATION, DBG_WRITE, ">SerialGetNextWrite\n");
do {
reqContext = SerialGetRequestContext(*CurrentOpRequest);
//
// We could be completing a flush.
//
if (reqContext->MajorFunction == IRP_MJ_WRITE) {
ASSERT(Extension->TotalCharsQueued >= reqContext->Length);
Extension->TotalCharsQueued -= reqContext->Length;
} else if (reqContext->MajorFunction == IRP_MJ_DEVICE_CONTROL) {
WDFREQUEST request = *CurrentOpRequest;
PSERIAL_XOFF_COUNTER Xc;
Xc = reqContext->SystemBuffer;
//
// We should never have a xoff counter when we
// get to this point.
//
ASSERT(!Extension->CurrentXoffRequest);
//
// This could only be a xoff counter masquerading as
// a write request.
//
Extension->TotalCharsQueued--;
//
// Check to see of the xoff request has been set with success.
// This means that the write completed normally. If that
// is the case, and it hasn't been set to cancel in the
// meanwhile, then go on and make it the CurrentXoffRequest.
//
if (reqContext->Status != STATUS_SUCCESS || reqContext->Cancelled) {
// TODO: I see Xoff request getting abandoned due to loss of
// Total timer - SERIAL_REF_TOTAL_TIMER
//
// Oh well, we can just finish it off.
//
NOTHING;
} else {
SerialSetCancelRoutine(request, SerialCancelCurrentXoff);
//
// We don't want to complete the current request now. This
// will now get completed by the Xoff counter code.
//
CompleteCurrent = FALSE;
//
// Give the counter to the isr.
//
Extension->CurrentXoffRequest = request;
WdfInterruptSynchronize(
Extension->WdfInterrupt,
SerialGiveXoffToIsr,
Extension
);
//
// Start the timer for the counter and increment
// the reference count since the timer has a
// reference to the request.
//
if (Xc->Timeout) {
LARGE_INTEGER delta;
BOOLEAN result;
delta.QuadPart = -((LONGLONG)UInt32x32To64(
1000,
Xc->Timeout
));
result = SerialSetTimer(
Extension->XoffCountTimer,
delta
);
if(result == FALSE) {
SERIAL_SET_REFERENCE(
reqContext,
SERIAL_REF_TOTAL_TIMER
);
}
}
}
}
//
// Note that the following call will (probably) also cause
// the current request to be completed.
//
SerialGetNextRequest(
CurrentOpRequest,
QueueToProcess,
NewRequest,
CompleteCurrent,
Extension
);
if (!*NewRequest) {
WdfInterruptSynchronize(
Extension->WdfInterrupt,
SerialProcessEmptyTransmit,
Extension
);
break;
} else if (SerialGetRequestContext(*NewRequest)->MajorFunction
== IRP_MJ_FLUSH_BUFFERS) {
//
// If we encounter a flush request we just want to get
// the next request and complete the flush.
//
// Note that if NewRequest is non-null then it is also
// equal to CurrentWriteRequest.
//
ASSERT((*NewRequest) == (*CurrentOpRequest));
SerialGetRequestContext(*NewRequest)->Status = STATUS_SUCCESS;
} else {
break;
}
} WHILE (TRUE);
SerialDbgPrintEx(TRACE_LEVEL_INFORMATION, DBG_WRITE, "<SerialGetNextWrite\n");
}
VOID
SerialCompleteWrite(
IN WDFDPC Dpc
)
/*++
Routine Description:
This routine is merely used to complete any write. It
assumes that the status and the information fields of
the request are already correctly filled in.
Arguments:
Dpc - Not Used.
DeferredContext - Really points to the device extension.
SystemContext1 - Not Used.
SystemContext2 - Not Used.
Return Value:
None.
--*/
{
PSERIAL_DEVICE_EXTENSION Extension = NULL;
Extension = SerialGetDeviceExtension(WdfDpcGetParentObject(Dpc));
SerialDbgPrintEx(TRACE_LEVEL_INFORMATION, DBG_WRITE, ">SerialCompleteWrite(%p)\n",
Extension);
SerialTryToCompleteCurrent(Extension, NULL, STATUS_SUCCESS,
&Extension->CurrentWriteRequest,
Extension->WriteQueue, NULL,
Extension->WriteRequestTotalTimer,
SerialStartWrite, SerialGetNextWrite,
SERIAL_REF_ISR);
SerialDbgPrintEx(TRACE_LEVEL_INFORMATION, DBG_WRITE, "<SerialCompleteWrite\n");
}
BOOLEAN
SerialProcessEmptyTransmit(
IN WDFINTERRUPT Interrupt,
IN PVOID Context
)
/*++
Routine Description:
This routine is used to determine if conditions are appropriate
to satisfy a wait for transmit empty event, and if so to complete
the request that is waiting for that event. It also call the code
that checks to see if we should lower the RTS line if we are
doing transmit toggling.
NOTE: This routine is called by WdfInterruptSynchronize.
NOTE: This routine assumes that it is called with the cancel
spinlock held.
Arguments:
Context - Really a pointer to the device extension.
Return Value:
This routine always returns FALSE.
--*/
{
PSERIAL_DEVICE_EXTENSION Extension = Context;
UNREFERENCED_PARAMETER(Interrupt);
if (Extension->IsrWaitMask && (Extension->IsrWaitMask & SERIAL_EV_TXEMPTY) &&
Extension->EmptiedTransmit && (!Extension->TransmitImmediate) &&
(!Extension->CurrentWriteRequest) && IsQueueEmpty(Extension->WriteQueue)) {
Extension->HistoryMask |= SERIAL_EV_TXEMPTY;
if (Extension->IrpMaskLocation) {
*Extension->IrpMaskLocation = Extension->HistoryMask;
Extension->IrpMaskLocation = NULL;
Extension->HistoryMask = 0;
SerialGetRequestContext(Extension->CurrentWaitRequest)->Information = sizeof(ULONG);
SerialInsertQueueDpc(
Extension->CommWaitDpc
);
}
Extension->CountOfTryingToLowerRTS++;
SerialPerhapsLowerRTS(Extension->WdfInterrupt, Extension);
}
return FALSE;
}
BOOLEAN
SerialGiveWriteToIsr(
IN WDFINTERRUPT Interrupt,
IN PVOID Context
)
/*++
Routine Description:
Try to start off the write by slipping it in behind
a transmit immediate char, or if that isn't available
and the transmit holding register is empty, "tickle"
the UART into interrupting with a transmit buffer
empty.
NOTE: This routine is called by WdfInterruptSynchronize.
NOTE: This routine assumes that it is called with the
cancel spin lock held.
Arguments:
Context - Really a pointer to the device extension.
Return Value:
This routine always returns FALSE.
--*/
{
PSERIAL_DEVICE_EXTENSION Extension = Context;
//
// The current stack location. This contains all of the
// information we need to process this particular request.
//
PREQUEST_CONTEXT reqContext;
UNREFERENCED_PARAMETER(Interrupt);
reqContext = SerialGetRequestContext(Extension->CurrentWriteRequest);
//
// We might have a xoff counter request masquerading as a
// write. The length of these requests will always be one
// and we can get a pointer to the actual character from
// the data supplied by the user.
//
if (reqContext->MajorFunction == IRP_MJ_WRITE) {
Extension->WriteLength = reqContext->Length;
Extension->WriteCurrentChar = reqContext->SystemBuffer;
} else {
Extension->WriteLength = 1;
Extension->WriteCurrentChar =
((PUCHAR)reqContext->SystemBuffer) +
FIELD_OFFSET(
SERIAL_XOFF_COUNTER,
XoffChar
);
}
//
// The isr now has a reference to the request.
//
SERIAL_SET_REFERENCE(
reqContext,
SERIAL_REF_ISR
);
//
// Check first to see if an immediate char is transmitting.
// If it is then we'll just slip in behind it when its
// done.
//
if (!Extension->TransmitImmediate) {
//
// If there is no immediate char transmitting then we
// will "re-enable" the transmit holding register empty
// interrupt. The 8250 family of devices will always
// signal a transmit holding register empty interrupt
// *ANY* time this bit is set to one. By doing things
// this way we can simply use the normal interrupt code
// to start off this write.
//
// We've been keeping track of whether the transmit holding
// register is empty so it we only need to do this
// if the register is empty.
//
if (Extension->HoldingEmpty) {
DISABLE_ALL_INTERRUPTS(Extension, Extension->Controller);
ENABLE_ALL_INTERRUPTS(Extension, Extension->Controller);
}
}
//
// The rts line may already be up from previous writes,
// however, it won't take much additional time to turn
// on the RTS line if we are doing transmit toggling.
//
if ((Extension->HandFlow.FlowReplace & SERIAL_RTS_MASK) ==
SERIAL_TRANSMIT_TOGGLE) {
SerialSetRTS(Extension->WdfInterrupt, Extension);
}
return FALSE;
}
VOID
SerialCancelCurrentWrite(
IN WDFREQUEST Request
)
/*++
Routine Description:
This routine is used to cancel the current write.
Arguments:
Device - Wdf handle for the device
Request - Pointer to the WDFREQUEST to be canceled.
Return Value:
None.
--*/
{
PSERIAL_DEVICE_EXTENSION Extension;
WDFDEVICE device = WdfIoQueueGetDevice(WdfRequestGetIoQueue(Request));
UNREFERENCED_PARAMETER(Request);
Extension = SerialGetDeviceExtension(device);
SerialTryToCompleteCurrent(
Extension,
SerialGrabWriteFromIsr,
STATUS_CANCELLED,
&Extension->CurrentWriteRequest,
Extension->WriteQueue,
NULL,
Extension->WriteRequestTotalTimer,
SerialStartWrite,
SerialGetNextWrite,
SERIAL_REF_CANCEL
);
}
VOID
SerialWriteTimeout(
IN WDFTIMER Timer
)
/*++
Routine Description:
This routine will try to timeout the current write.
Arguments:
Return Value:
None.
--*/
{
PSERIAL_DEVICE_EXTENSION Extension = NULL;
Extension = SerialGetDeviceExtension(WdfTimerGetParentObject(Timer));
SerialDbgPrintEx(TRACE_LEVEL_INFORMATION, DBG_WRITE, ">SerialWriteTimeout(%p)\n",
Extension);
SerialTryToCompleteCurrent(Extension, SerialGrabWriteFromIsr,
STATUS_TIMEOUT, &Extension->CurrentWriteRequest,
Extension->WriteQueue, NULL,
Extension->WriteRequestTotalTimer,
SerialStartWrite, SerialGetNextWrite,
SERIAL_REF_TOTAL_TIMER);
SerialDbgPrintEx(TRACE_LEVEL_INFORMATION, DBG_WRITE, "<SerialWriteTimeout\n");
}
BOOLEAN
SerialGrabWriteFromIsr(
IN WDFINTERRUPT Interrupt,
IN PVOID Context
)
/*++
Routine Description:
This routine is used to grab the current request, which could be timing
out or canceling, from the ISR
NOTE: This routine is being called from WdfInterruptSynchronize.
NOTE: This routine assumes that the cancel spin lock is held
when this routine is called.
Arguments:
Context - Really a pointer to the device extension.
Return Value:
Always false.
--*/
{
PSERIAL_DEVICE_EXTENSION Extension = Context;
PREQUEST_CONTEXT reqContext;
UNREFERENCED_PARAMETER(Interrupt);
reqContext = SerialGetRequestContext(Extension->CurrentWriteRequest);
//
// Check if the write length is non-zero. If it is non-zero
// then the ISR still owns the request. We calculate the the number
// of characters written and update the information field of the
// request with the characters written. We then clear the write length
// the isr sees.
//
if (Extension->WriteLength) {
//
// We could have an xoff counter masquerading as a
// write request. If so, don't update the write length.
//
if (reqContext->MajorFunction == IRP_MJ_WRITE) {
reqContext->Information = reqContext->Length -Extension->WriteLength;
} else {
reqContext->Information = 0;
}
//
// Since the isr no longer references this request, we can
// decrement it's reference count.
//
SERIAL_CLEAR_REFERENCE(
reqContext,
SERIAL_REF_ISR
);
Extension->WriteLength = 0;
}
return FALSE;
}
BOOLEAN
SerialGrabXoffFromIsr(
IN WDFINTERRUPT Interrupt,
IN PVOID Context
)
/*++
Routine Description:
This routine is used to grab an xoff counter request from the
isr when it is no longer masquerading as a write request. This
routine is called by the cancel and timeout code for the
xoff counter ioctl.
NOTE: This routine is being called from WdfInterruptSynchronize.
NOTE: This routine assumes that the cancel spin lock is held
when this routine is called.
Arguments:
Context - Really a pointer to the device extension.
Return Value:
Always false.
--*/
{
PSERIAL_DEVICE_EXTENSION Extension = Context;
PREQUEST_CONTEXT reqContext;
UNREFERENCED_PARAMETER(Interrupt);
reqContext = SerialGetRequestContext(Extension->CurrentXoffRequest);
if (Extension->CountSinceXoff) {
//
// This is only non-zero when there actually is a Xoff ioctl
// counting down.
//
Extension->CountSinceXoff = 0;
//
// We decrement the count since the isr no longer owns
// the request.
//
SERIAL_CLEAR_REFERENCE(
reqContext,
SERIAL_REF_ISR
);
}
return FALSE;
}
VOID
SerialCompleteXoff(
IN WDFDPC Dpc
)
/*++
Routine Description:
This routine is merely used to truely complete an xoff counter request. It
assumes that the status and the information fields of the request are
already correctly filled in.
Arguments:
Dpc - Not Used.
DeferredContext - Really points to the device extension.
SystemContext1 - Not Used.
SystemContext2 - Not Used.
Return Value:
None.
--*/
{
PSERIAL_DEVICE_EXTENSION Extension = NULL;
Extension = SerialGetDeviceExtension(WdfDpcGetParentObject(Dpc));
SerialDbgPrintEx(TRACE_LEVEL_INFORMATION, DBG_WRITE, ">SerialCompleteXoff(%p)\n",
Extension);
SerialTryToCompleteCurrent(Extension, NULL, STATUS_SUCCESS,
&Extension->CurrentXoffRequest, NULL, NULL,
Extension->XoffCountTimer, NULL, NULL,
SERIAL_REF_ISR);
SerialDbgPrintEx(TRACE_LEVEL_INFORMATION, DBG_WRITE, "<SerialCompleteXoff\n");
}
VOID
SerialTimeoutXoff(
IN WDFTIMER Timer
)
/*++
Routine Description:
This routine is merely used to truely complete an xoff counter request,
if its timer has run out.
Arguments:
Return Value:
None.
--*/
{
PSERIAL_DEVICE_EXTENSION Extension = NULL;
Extension = SerialGetDeviceExtension(WdfTimerGetParentObject(Timer));
SerialDbgPrintEx(TRACE_LEVEL_INFORMATION, DBG_WRITE, ">SerialTimeoutXoff(%p)\n", Extension);
SerialTryToCompleteCurrent(Extension, SerialGrabXoffFromIsr,
STATUS_SERIAL_COUNTER_TIMEOUT,
&Extension->CurrentXoffRequest, NULL, NULL, NULL,
NULL, NULL, SERIAL_REF_TOTAL_TIMER);
SerialDbgPrintEx(TRACE_LEVEL_INFORMATION, DBG_WRITE, "<SerialTimeoutXoff\n");
}
VOID
SerialCancelCurrentXoff(
IN WDFREQUEST Request
)
/*++
Routine Description:
This routine is used to cancel the current write.
Arguments:
Device - Wdf handle for the device
Request - Pointer to the WDFREQUEST to be canceled.
Return Value:
None.
--*/
{
PSERIAL_DEVICE_EXTENSION Extension;
WDFDEVICE device = WdfIoQueueGetDevice(WdfRequestGetIoQueue(Request));
UNREFERENCED_PARAMETER(Request);
Extension = SerialGetDeviceExtension(device);
SerialTryToCompleteCurrent(
Extension,
SerialGrabXoffFromIsr,
STATUS_CANCELLED,
&Extension->CurrentXoffRequest,
NULL,
NULL,
Extension->XoffCountTimer,
NULL,
NULL,
SERIAL_REF_CANCEL
);
}
BOOLEAN
SerialGiveXoffToIsr(
IN WDFINTERRUPT Interrupt,
IN PVOID Context
)
/*++
Routine Description:
This routine starts off the xoff counter. It merely
has to set the xoff count and increment the reference
count to denote that the isr has a reference to the request.
NOTE: This routine is called by WdfInterruptSynchronize.
NOTE: This routine assumes that it is called with the
cancel spin lock held.
Arguments:
Context - Really a pointer to the device extension.
Return Value:
This routine always returns FALSE.
--*/
{
PSERIAL_DEVICE_EXTENSION Extension = Context;
PREQUEST_CONTEXT reqContext;
PSERIAL_XOFF_COUNTER Xc = NULL;
UNREFERENCED_PARAMETER(Interrupt);
reqContext = SerialGetRequestContext(Extension->CurrentXoffRequest);
Xc = reqContext->SystemBuffer;
//
// The current stack location. This contains all of the
// information we need to process this particular request.
//
ASSERT(Extension->CurrentXoffRequest);
Extension->CountSinceXoff = Xc->Counter;
//
// The isr now has a reference to the request.
//
SERIAL_SET_REFERENCE(
reqContext,
SERIAL_REF_ISR
);
return FALSE;
}
|