summaryrefslogtreecommitdiff
path: root/tests/projects/windows/driver/kmdf/serial/waitmask.c
blob: da5dfa09878392651ff6911f70607900accd51ff (plain)
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
/*++

Copyright (c) Microsoft Corporation

Module Name:

    waitmask.c

Abstract:

    This module contains the code that is very specific to get/set/wait
    on event mask operations in the serial driver

Environment:

    Kernel mode

--*/

#include "precomp.h"

#if defined(EVENT_TRACING)
#include "waitmask.tmh"
#endif

EVT_WDF_INTERRUPT_SYNCHRONIZE SerialGrabWaitFromIsr;
EVT_WDF_INTERRUPT_SYNCHRONIZE SerialGiveWaitToIsr;
EVT_WDF_INTERRUPT_SYNCHRONIZE SerialFinishOldWait;


VOID
SerialStartMask(
    IN PSERIAL_DEVICE_EXTENSION Extension
    )

/*++

Routine Description:

    This routine is used to process the set mask and wait
    mask ioctls.  Calls to this routine are serialized by
    placing irps in the list under the protection of the
    cancel spin lock.

Arguments:

    Extension - A pointer to the serial device extension.

Return Value:

    Will return pending for everything put the first
    request that we actually process.  Even in that
    case it will return pending unless it can complete
    it right away.


--*/

{


    WDFREQUEST NewRequest;
    PREQUEST_CONTEXT reqContext;
    WDF_REQUEST_PARAMETERS  params;

    SerialDbgPrintEx(TRACE_LEVEL_INFORMATION, DBG_IOCTLS,
                     "In SerialStartMask\n");

    ASSERT(Extension->CurrentMaskRequest);


    do {

        SerialDbgPrintEx(TRACE_LEVEL_INFORMATION, DBG_IOCTLS,
                         "STARTMASK - CurrentMaskRequest: %p\n",
                         Extension->CurrentMaskRequest);

        WDF_REQUEST_PARAMETERS_INIT(&params);

        WdfRequestGetParameters(
                 Extension->CurrentMaskRequest,
                 &params
                 );


         reqContext = SerialGetRequestContext(Extension->CurrentMaskRequest);

        ASSERT((params.Parameters.DeviceIoControl.IoControlCode ==
                IOCTL_SERIAL_WAIT_ON_MASK) ||
               (params.Parameters.DeviceIoControl.IoControlCode ==
                IOCTL_SERIAL_SET_WAIT_MASK));

        if (params.Parameters.DeviceIoControl.IoControlCode ==
                                    IOCTL_SERIAL_SET_WAIT_MASK) {

            SerialDbgPrintEx(TRACE_LEVEL_INFORMATION, DBG_IOCTLS,
                             "SERIAL - %p is a SETMASK request\n",
                             Extension->CurrentMaskRequest);

            //
            // Complete the old wait if there is one.
            //

            WdfInterruptSynchronize(
                Extension->WdfInterrupt,
                SerialFinishOldWait,
                Extension
                );

            //
            // Any current waits should be on its way to completion
            // at this point.  There certainly shouldn't be any
            // request mask location.
            //

            ASSERT(!Extension->IrpMaskLocation);

            reqContext->Status = STATUS_SUCCESS;

            //
            // The following call will also cause the current
            // call to be completed.
            //

            SerialGetNextRequest(
                &Extension->CurrentMaskRequest,
                Extension->MaskQueue,
                &NewRequest,
                TRUE,
                Extension
                );
            SerialDbgPrintEx(TRACE_LEVEL_INFORMATION, DBG_IOCTLS,
                             "Perhaps another mask request was found in "
                             "the queue\n"
                             "------- %p/%p <- values should be the same\n",
                             Extension->CurrentMaskRequest, NewRequest);


        } else {

            //
            // First make sure that we have a non-zero mask.
            // If the app queues a wait on a zero mask it can't
            // be statisfied so it makes no sense to start it.
            //

            if ((!Extension->IsrWaitMask) || (Extension->CurrentWaitRequest)) {

                SerialDbgPrintEx(TRACE_LEVEL_INFORMATION, DBG_IOCTLS,
                                 "WaitIrp is invalid\n"
                                 "------- IsrWaitMask: %x\n"
                                 "------- CurrentWaitRequest: %p\n",
                                 Extension->IsrWaitMask,
                                 Extension->CurrentWaitRequest);

                reqContext->Status = STATUS_INVALID_PARAMETER;

                SerialGetNextRequest(&Extension->CurrentMaskRequest,
                                 Extension->MaskQueue, &NewRequest, TRUE,
                                 Extension);

                SerialDbgPrintEx(TRACE_LEVEL_INFORMATION, DBG_IOCTLS,
                                 "Perhaps another mask request was found "
                                 "in the queue\n"
                                 "------- %p/%p <- values should be the same\n",
                                 Extension->CurrentMaskRequest,NewRequest);

            } else {

                //
                // Make the current mask request the current wait request and
                // get a new current mask request.  Note that when we get
                // the new current mask request we DO NOT complete the
                // old current mask request (which is now the current wait
                // request.
                //
                // Then under the protection of the cancel spin lock
                // we check to see if the current wait request needs to
                // be canceled
                //

                SERIAL_INIT_REFERENCE(reqContext);

                SerialSetCancelRoutine(Extension->CurrentMaskRequest,
                                                SerialCancelWait);

                SerialDbgPrintEx(TRACE_LEVEL_INFORMATION, DBG_IOCTLS,
                                 "%p will become the current "
                                 "wait request\n",
                                 Extension->CurrentMaskRequest);
                //
                // There should never be a mask location when
                // there isn't a current wait request.  At this point
                // there shouldn't be a current wait request also.
                //

                ASSERT(!Extension->IrpMaskLocation);
                ASSERT(!Extension->CurrentWaitRequest);

                Extension->CurrentWaitRequest = Extension->CurrentMaskRequest;

                WdfInterruptSynchronize(
                    Extension->WdfInterrupt,
                    SerialGiveWaitToIsr,
                    Extension
                    );

                //
                // Since it isn't really the mask request anymore,
                // null out that pointer.
                //
                Extension->CurrentMaskRequest = NULL;

                //
                // This will release the cancel spinlock for us
                //

                SerialGetNextRequest(&Extension->CurrentMaskRequest,
                                       Extension->MaskQueue, &NewRequest,
                                       FALSE, Extension);

                SerialDbgPrintEx(TRACE_LEVEL_INFORMATION, DBG_IOCTLS,
                                 "Perhaps another mask request was "
                                 "found in the queue\n"
                                 "------- %p/%p <- values should be the "
                                 "same\n", Extension->CurrentMaskRequest,
                                 NewRequest);
            }

        }

    } while (NewRequest);

    return;

}

BOOLEAN
SerialGrabWaitFromIsr(
    IN WDFINTERRUPT Interrupt,
    IN PVOID Context
    )

/*++

Routine Description:

    This routine will check to see if the ISR still knows about
    a wait request by checking to see if the IrpMaskLocation is non-null.
    If it is then it will zero the Irpmasklocation (which in effect
    grabs the request away from the isr).  This routine is only called
    buy the cancel code for the wait.

    NOTE: This is called by WdfInterruptSynchronize.

Arguments:

    Context - A pointer to the device extension

Return Value:

    Always FALSE.

--*/

{
    PSERIAL_DEVICE_EXTENSION Extension = Context;

    PREQUEST_CONTEXT reqContext;

    UNREFERENCED_PARAMETER(Interrupt);

    reqContext = SerialGetRequestContext(Extension->CurrentWaitRequest);

    SerialDbgPrintEx(TRACE_LEVEL_INFORMATION, DBG_IOCTLS,
                     "In SerialGrabWaitFromIsr\n");

    if (Extension->IrpMaskLocation) {

        SerialDbgPrintEx(TRACE_LEVEL_INFORMATION, DBG_IOCTLS,
                         "The isr still owns the request %p, mask "
                         "location is %p\n"
                         "------- and system buffer is %p\n",
                         Extension->CurrentWaitRequest,Extension->IrpMaskLocation,
                         reqContext->SystemBuffer);

        //
        // The isr still "owns" the request.
        //

        *Extension->IrpMaskLocation = 0;
        Extension->IrpMaskLocation = NULL;

        reqContext->Information = sizeof(ULONG);

        //
        // Since the isr no longer references the request we need to
        // decrement the reference count.
        //

        SERIAL_CLEAR_REFERENCE(
            reqContext,
            SERIAL_REF_ISR
            );

    }

    return FALSE;
}

BOOLEAN
SerialGiveWaitToIsr(
    IN WDFINTERRUPT Interrupt,
    IN PVOID Context
    )

/*++

Routine Description:

    This routine simply sets a variable in the device extension
    so that the isr knows that we have a wait request.

    NOTE: This is called by WdfInterruptSynchronize.

    NOTE: This routine assumes that it is called with the
          cancel spinlock held.

Arguments:

    Context - Simply a pointer to the device extension.

Return Value:

    Always FALSE.

--*/

{

    PSERIAL_DEVICE_EXTENSION Extension = Context;

    PREQUEST_CONTEXT reqContext;

    UNREFERENCED_PARAMETER(Interrupt);

    reqContext = SerialGetRequestContext(Extension->CurrentWaitRequest);

    SerialDbgPrintEx(TRACE_LEVEL_INFORMATION, DBG_IOCTLS,
                     "In SerialGiveWaitToIsr\n");
    //
    // There certainly shouldn't be a current mask location at
    // this point since we have a new current wait request.
    //

    ASSERT(!Extension->IrpMaskLocation);

    //
    // The isr may or may not actually reference this request.  It
    // won't if the wait can be satisfied immediately.  However,
    // since it will then go through the normal completion sequence,
    // we need to have an incremented reference count anyway.
    //

    SERIAL_SET_REFERENCE(
        reqContext,
        SERIAL_REF_ISR
        );

    if (!Extension->HistoryMask) {

        SerialDbgPrintEx(TRACE_LEVEL_INFORMATION, DBG_IOCTLS,
                         "No events occured prior to the wait call"
                         "\n");

        //
        // Although this wait might not be for empty transmit
        // queue, it doesn't hurt anything to set it to false.
        //

        Extension->EmptiedTransmit = FALSE;

        //
        // Record where the "completion mask" should be set.
        //

        Extension->IrpMaskLocation = reqContext->SystemBuffer;
        SerialDbgPrintEx( TRACE_LEVEL_INFORMATION, DBG_IOCTLS,
                          "The isr owns the request %p, mask location is "
                          "%p\n"
                          "------- and system buffer is %p\n",
                          Extension->CurrentWaitRequest,Extension->IrpMaskLocation,
                          reqContext->SystemBuffer);

    } else {

        SerialDbgPrintEx(TRACE_LEVEL_INFORMATION, DBG_IOCTLS,
                         "%x occurred prior to the wait - starting "
                         "the\n"
                         "------- completion code for %p\n",
                         Extension->HistoryMask,Extension->CurrentWaitRequest);

        *((ULONG *)reqContext->SystemBuffer) =
            Extension->HistoryMask;
        Extension->HistoryMask = 0;
        reqContext->Information = sizeof(ULONG);
        reqContext->Status = STATUS_SUCCESS;

        SerialInsertQueueDpc(Extension->CommWaitDpc);

    }

    return FALSE;
}

BOOLEAN
SerialFinishOldWait(
    IN WDFINTERRUPT  Interrupt,
    IN PVOID Context
    )

/*++

Routine Description:

    This routine will check to see if the ISR still knows about
    a wait request by checking to see if the Irpmasklocation is non-null.
    If it is then it will zero the Irpmasklocation (which in effect
    grabs the request away from the isr).  This routine is only called
    buy the cancel code for the wait.

    NOTE: This is called by WdfInterruptSynchronize.

Arguments:

    Context - A pointer to the device extension

Return Value:

    Always FALSE.

--*/

{
    PSERIAL_DEVICE_EXTENSION Extension = Context;

    PREQUEST_CONTEXT reqContext = NULL;
    PREQUEST_CONTEXT reqContextMask;

    UNREFERENCED_PARAMETER(Interrupt);

    reqContextMask = SerialGetRequestContext(Extension->CurrentMaskRequest);

    SerialDbgPrintEx(TRACE_LEVEL_INFORMATION, DBG_IOCTLS,
                     "In SerialFinishOldWait\n");

    if (Extension->IrpMaskLocation) {

        reqContext = SerialGetRequestContext(Extension->CurrentWaitRequest);

        SerialDbgPrintEx(TRACE_LEVEL_INFORMATION, DBG_IOCTLS,
                         "The isr still owns the request %p, mask "
                         "location is %p\n"
                         "------- and system buffer is %p\n",
                         Extension->CurrentWaitRequest,Extension->IrpMaskLocation,
                         reqContext->SystemBuffer);
        //
        // The isr still "owns" the request.
        //

        *Extension->IrpMaskLocation = 0;
        Extension->IrpMaskLocation = NULL;

        reqContext->Information = sizeof(ULONG);

        //
        // We don't decrement the reference since the completion routine
        // will do that.
        //

        SerialInsertQueueDpc(Extension->CommWaitDpc);

    }

    //
    // Don't wipe out any historical data we are still interested in.
    //

    Extension->HistoryMask &= *((ULONG *)reqContextMask->SystemBuffer);

    Extension->IsrWaitMask = *((ULONG *)reqContextMask->SystemBuffer);
    SerialDbgPrintEx( TRACE_LEVEL_INFORMATION, DBG_IOCTLS,
                      "Set mask location of %p, in request %p, with "
                      "system buffer of %p\n",
                      Extension->IrpMaskLocation, Extension->CurrentMaskRequest,
                      reqContextMask->SystemBuffer);
    return FALSE;
}

VOID
SerialCancelWait(
     IN WDFREQUEST Request
    )

/*++

Routine Description:

    This routine is used to cancel a request that is waiting on
    a comm event.

Arguments:

    Device - Wdf handle for the device

    Request - Pointer to the WDFREQUEST for the current request

Return Value:

    None.

--*/

{

    PSERIAL_DEVICE_EXTENSION Extension;
    WDFDEVICE  device = WdfIoQueueGetDevice(WdfRequestGetIoQueue(Request));

    UNREFERENCED_PARAMETER(Request);

    Extension = SerialGetDeviceExtension(device);

    SerialDbgPrintEx(TRACE_LEVEL_INFORMATION, DBG_IOCTLS,
                     "Canceling wait for request %p\n",
                     Extension->CurrentWaitRequest);

    SerialTryToCompleteCurrent(Extension,
                               SerialGrabWaitFromIsr,
                               STATUS_CANCELLED,
                               &Extension->CurrentWaitRequest,
                               NULL, NULL, NULL,
                               NULL, NULL, SERIAL_REF_CANCEL);

}


VOID
SerialCompleteWait(
    IN WDFDPC Dpc
    )

{

    PSERIAL_DEVICE_EXTENSION Extension = NULL;

    Extension = SerialGetDeviceExtension(WdfDpcGetParentObject(Dpc));

    SerialDbgPrintEx(TRACE_LEVEL_INFORMATION, DBG_IOCTLS,
                     ">SerialCompleteWait(%p)\n",
                     Extension);

    SerialDbgPrintEx(TRACE_LEVEL_INFORMATION, DBG_IOCTLS,
                     "Completing wait for request %p\n",
                     Extension->CurrentWaitRequest);

    SerialTryToCompleteCurrent(Extension, NULL, STATUS_SUCCESS,
                               &Extension->CurrentWaitRequest, NULL, NULL, NULL,
                               NULL, NULL, SERIAL_REF_ISR);

    SerialDbgPrintEx(TRACE_LEVEL_INFORMATION, DBG_IOCTLS,
                     "<SerialCompleteWait\n");
}