summaryrefslogtreecommitdiffstats
path: root/private/ntos/nthals/extender/pnpisa/i386/bus.c
blob: f4c6dd4df2efb1bdabec10ddb84b4c59803b37cd (plain) (blame)
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
/*++

Copyright (c) 1995  Microsoft Corporation

Module Name:

    bus.c

Abstract:


Author:

    Shie-Lin Tzong (shielint) July-26-1995

Environment:

    Kernel mode only.

Revision History:

--*/

#include "busp.h"
#include "pnpisa.h"

PDEVICE_INFORMATION
PipFindDeviceInformation (
    IN PPI_BUS_EXTENSION BusExtension,
    IN ULONG SlotNumber
    );

PCARD_INFORMATION
PipFindCardInformation (
    IN PPI_BUS_EXTENSION BusExtension,
    IN PSERIAL_IDENTIFIER Id
    );

#ifdef ALLOC_PRAGMA
#pragma alloc_text(PAGE,PiQueryBusSlots)
#pragma alloc_text(PAGE,PiReferenceDeviceHandler)
#pragma alloc_text(PAGE,PipCheckBus)
#endif


ULONG
PiGetBusData (
    IN PBUS_HANDLER BusHandler,
    IN PBUS_HANDLER RootHandler,
    IN ULONG SlotNumber,
    IN PUCHAR Buffer,
    IN ULONG Offset,
    IN ULONG Length
    )

/*++

Routine Description:

    The function returns the Pnp Isa bus data for a device.
    This API is not supported.

Arguments:

    BusHandler - supplies a pointer to the handler of the bus.

    RootHandler - supplies a pointer to a BUS_HANDLER for the originating
        request.

    Buffer - Supplies the space to store the data.

    Offset - Supplies the offset to the device data to start reading.

    Length - Supplies a count in bytes of the maximum amount to return.

Return Value:

    Returns the amount of data stored into the buffer.

--*/

{
    return 0;
}

ULONG
PiSetBusData (
    IN PBUS_HANDLER BusHandler,
    IN PBUS_HANDLER RootHandler,
    IN ULONG SlotNumber,
    IN PUCHAR Buffer,
    IN ULONG Offset,
    IN ULONG Length
    )

/*++

Routine Description:

    The function sets the Pnp Isa bus data for a device.
    This API is not supported.

Arguments:

    BusHandler - supplies a pointer to the handler of the bus.

    RootHandler - supplies a pointer to a BUS_HANDLER for the originating
        request.

    Buffer - Supplies the space to store the data.

    Offset - Supplies the offset to the device data to be set.

    Length - Supplies a count in bytes of the maximum amount to return.

Return Value:

    Returns the amount of data stored into the buffer.

--*/

{
    return 0;
}

ULONG
PiGetDeviceData (
    IN struct _BUS_HANDLER *BusHandler,
    IN struct _BUS_HANDLER *RootHandler,
    IN PDEVICE_HANDLER_OBJECT DeviceHandler,
    IN ULONG DataType,
    IN PUCHAR Buffer,
    IN ULONG Offset,
    IN ULONG Length
    )
/*++

Routine Description:

    The function returns the Pnp Isa device data for a device.

Arguments:

    BusHandler - supplies a pointer to the handler of the bus.

    RootHandler - supplies a pointer to a BUS_HANDLER for the originating
        request.

    DeviceHandler - supplies a pointer to a DEVICE_HANDLER_OBJECT

    DataType - Specifies the type of device data desired.

    Buffer - Supplies the space to store the data.

    Offset - Supplies the offset to the device data to start reading.

    Length - Supplies a count in bytes of the maximum amount to return.

Return Value:

    Returns the amount of data stored into the buffer.

--*/
{
    ULONG dataLength;
    PDEVICE_INFORMATION deviceInfo;

    UNREFERENCED_PARAMETER ( DataType);

    dataLength = 0;
    deviceInfo = DeviceHandler2DeviceInfo (DeviceHandler);

    //
    // Synchronize with other pnp Isa service functions accessing device data.
    //

    ExAcquireFastMutex(&PipMutex);

    //
    // Verify caller has a valid DeviceHandler object
    //

    if (!(deviceInfo->Flags & DEVICE_FLAGS_VALID)) {

        //
        // Obsolete object, return no data
        //

        ExReleaseFastMutex(&PipMutex);
        return dataLength;
    }

    //
    // Get the device's data.
    //

    dataLength = deviceInfo->DeviceDataLength;
    if (Offset < dataLength) {
        dataLength -= Offset;
        if (dataLength > Length) {
            dataLength = Length;
        }
        RtlMoveMemory(Buffer, (PUCHAR)deviceInfo->DeviceData + Offset, dataLength);
    } else {
        dataLength = 0;
    }

    ExReleaseFastMutex(&PipMutex);
    return dataLength;
}

ULONG
PiSetDeviceData (
    IN struct _BUS_HANDLER *BusHandler,
    IN struct _BUS_HANDLER *RootHandler,
    IN PDEVICE_HANDLER_OBJECT DeviceHandler,
    IN ULONG DataType,
    IN PUCHAR Buffer,
    IN ULONG Offset,
    IN ULONG Length
    )
/*++

Routine Description:

    The function sets Pnp Isa device data for a device.

Arguments:

    BusHandler - supplies a pointer to the handler of the bus.

    RootHandler - supplies a pointer to a BUS_HANDLER for the originating
        request.

    DeviceHandler - supplies a pointer to a DEVICE_HANDLER_OBJECT

    DataType - Specifies the type of device data desired.

    Buffer - Supplies the space to store the data.

    Offset - Supplies the offset to the device data to start reading.

    Length - Supplies a count in bytes of the maximum amount to return.

Return Value:

    Returns the amount of data set.

--*/
{
    //
    // We don't let drivers change Pnp device data.
    //

    return 0;
}

NTSTATUS
PiQueryBusSlots (
    IN PBUS_HANDLER BusHandler,
    IN PBUS_HANDLER RootHandler,
    IN ULONG        BufferSize,
    OUT PULONG      SlotNumbers,
    OUT PULONG      ReturnedLength
    )

/*++

Routine Description:

    The function returns a list of currently available SlotNumber.

Arguments:

    BusHandler - supplies a pointer to the handler of the bus.

    RootHandler - supplies a pointer to a BUS_HANDLER for the originating
        request.

    Buffer - Supplies the space to store the data.

    SlotNumber - Supplies a variable to receives the number of available slots.

    Length - Supplies a count in bytes of the stored data.  If this function
        returns STATUS_BUFFER_TOO_SMALL, this variable supplies the required
        size.

Return Value:

    STATUS_BUFFER_TOO_SMALL if caller supplied buffer is not big enough.

--*/

{
    PPI_BUS_EXTENSION busExtension;
    PSINGLE_LIST_ENTRY link;
    PDEVICE_INFORMATION deviceInfo;
    ULONG count = 0;

    PAGED_CODE ();

    busExtension = (PPI_BUS_EXTENSION)BusHandler->BusData;

    //
    // Synchronize with other pnp Isa device handle assignment.
    //

    ExAcquireFastMutex(&PipMutex);

    //
    // Fill in returned buffer length, or what size buffer is needed
    //

    *ReturnedLength = busExtension->NoValidSlots  * sizeof (ULONG);
    if (BufferSize < *ReturnedLength) {

        //
        // Callers buffer is not large enough
        //

        ExReleaseFastMutex (&PipMutex);
        return STATUS_BUFFER_TOO_SMALL;
    }

    //
    // Return caller all the possible slot number
    //

    for (link = busExtension->DeviceList.Next; link; link = link->Next) {
        deviceInfo = CONTAINING_RECORD (link, DEVICE_INFORMATION, DeviceList);
        if (deviceInfo->Flags & DEVICE_FLAGS_VALID) {
            *SlotNumbers = DeviceInfoSlot(deviceInfo);
            SlotNumbers++;
            count += 1;
        }
    }

    *ReturnedLength = count * sizeof (ULONG);
    ExReleaseFastMutex (&PipMutex);
    return STATUS_SUCCESS;
}

NTSTATUS
PiDeviceControl (
    IN PHAL_DEVICE_CONTROL_CONTEXT Context
    )

/*++

Routine Description:

    The function is the bus handler specific verion of HalDeviceControl.

Arguments:

    Context - The DeviceControl context.  The context has all the information
        for the HalDeviceControl operation being performed.

Return Value:

    A NTSTATUS code to indicate the result of the operation.

--*/
{
    ULONG i, junk;
    ULONG controlCode;
    PULONG bufferLength;
    NTSTATUS status = STATUS_INVALID_PARAMETER;

    for (i = 0; i < NUMBER_DEVICE_CONTROL_FUNCTIONS; i++) {

        if (PipDeviceControl[i].ControlCode == Context->DeviceControl.ControlCode) {
            if (PipDeviceControl[i].ControlHandler == NULL) {
                status = Context->DeviceControl.Status = STATUS_NOT_IMPLEMENTED;
                goto deviceControlDone;
            }

            //
            // Found DeviceControl handler and save it to HAL_DEVICE_CONTROL_CONTEXT
            // structure.
            //

            Context->ContextControlHandler = (ULONG)(PipDeviceControl + i);

            //
            // Verify callers buffer is the min required length
            //

            if (*Context->DeviceControl.BufferLength < PipDeviceControl[i].MinBuffer) {
                Context->DeviceControl.Status = STATUS_BUFFER_TOO_SMALL;
                *Context->DeviceControl.BufferLength = PipDeviceControl[i].MinBuffer;
                status = STATUS_BUFFER_TOO_SMALL;
                goto deviceControlDone;
            }

            if (KeGetCurrentIrql() < DISPATCH_LEVEL) {

                //
                // All supported slot control functions touch paged code or data.
                // If the current irql is low enough go dispatch now; otherwise,
                // queue the request to a worker thread.
                //

                PipDispatchControl (Context);

            } else {

                //
                // Enqueue to worker thread
                //

                ExInterlockedInsertTailList (
                    &PipControlWorkerList,
                    (PLIST_ENTRY) &Context->ContextWorkQueue,
                    &PipSpinlock
                    );

                //
                // Make sure worker is requested
                //

                PipStartWorker ();
            }
            return STATUS_PENDING;
        }
    }

deviceControlDone:
    HalCompleteDeviceControl (Context);
    return status;
}

PDEVICE_HANDLER_OBJECT
PiReferenceDeviceHandler (
    IN struct _BUS_HANDLER *BusHandler,
    IN struct _BUS_HANDLER *RootHandler,
    IN ULONG SlotNumber
    )
/*++

Routine Description:

    The function returns a reference to the devce handler specified by SlotNumber
    and BusHandler.

Arguments:

    BusHandler - Supplies a pointer to the bus handler structure for the bus

    RootHanler - Supplies a pointer to the root bus handler structure for the bus

    SlotNumber - Specifies the SlotNumber of the device to be referenced

Return Value:

    a reference to DEVICE_HANDLER_OBJECT.

--*/
{
    PDEVICE_INFORMATION deviceInfo;
    PDEVICE_HANDLER_OBJECT deviceHandler;
    PPI_BUS_EXTENSION busExtension;
    NTSTATUS status;

    PAGED_CODE ();

    ExAcquireFastMutex (&PipMutex);

    busExtension = (PPI_BUS_EXTENSION)BusHandler->BusData;
    deviceInfo = PipFindDeviceInformation (busExtension, SlotNumber);
    deviceHandler = NULL;
    if (deviceInfo) {
        deviceHandler = DeviceInfo2DeviceHandler (deviceInfo);
        status = ObReferenceObjectByPointer(
                    deviceHandler,
                    FILE_READ_DATA | FILE_WRITE_DATA,
                    *IoDeviceHandlerObjectType,
                    KernelMode
                    );

        if (!NT_SUCCESS(status)) {
            deviceHandler = NULL;
        }
    }

    ExReleaseFastMutex (&PipMutex);
    return deviceHandler;
}

PDEVICE_INFORMATION
PipFindDeviceInformation (
     IN PPI_BUS_EXTENSION BusExtension,
     IN ULONG SlotNumber
    )
/*++

Routine Description:

    The function goes through device data list to find the desired SlotNumber's
    device data.  The caller must hold the PipMutex to call this routine.

Arguments:

    BusExtension - supplies a pointer to current bus'es extension.

    SlotNumber - specified the desired device data.

Return Value:

    A pointer to the DEVICE_INFORMATION if found.  Otherwise a value of NULL is returned.

--*/
{
    PDEVICE_INFORMATION deviceInfo;
    PSINGLE_LIST_ENTRY link;
    PDEVICE_HANDLER_OBJECT deviceHandler;

    //
    // Go through the slot data link list to find a match.
    //

    for (link = BusExtension->DeviceList.Next; link; link = link->Next) {
        deviceInfo = CONTAINING_RECORD (link, DEVICE_INFORMATION, DeviceList);
        if (DeviceInfoSlot(deviceInfo) == SlotNumber) {
            break;
        }
    }

    if (!link) {
        return NULL;
    } else {
        return deviceInfo;
    }
}

VOID
PipCheckBus (
    IN PBUS_HANDLER BusHandler
    )

/*++

Routine Description:

    The function enumerates the bus specified by BusHandler.
    BUGBUG, Currently the bus extender will not unload even if all the pnp isa
    cards are gone.

Arguments:

    BusHandler - supplies a pointer to the BusHandler of the bus to be enumerated.

Return Value:

    None.

--*/
{
    PPI_BUS_EXTENSION busExtension;
    NTSTATUS status;
    PDEVICE_HANDLER_OBJECT deviceHandler;
    ULONG objectSize, noDevices;
    OBJECT_ATTRIBUTES objectAttributes;
    HANDLE handle;
    PUCHAR cardData;
    ULONG dataLength;
    USHORT csn, i;
    PDEVICE_INFORMATION deviceInfo;
    PCARD_INFORMATION cardInfo;
    UCHAR tmp;
    BOOLEAN notifyBusCheck;
    PSINGLE_LIST_ENTRY link;

    PAGED_CODE();

    busExtension = (PPI_BUS_EXTENSION)BusHandler->BusData;
    notifyBusCheck = FALSE;

    //
    // We may be removing references to this bus handler, so add
    // a reference now.
    //

    HalReferenceBusHandler (BusHandler);

    //
    // Acquire fast mutex to access device data
    //

    ExAcquireFastMutex (&PipMutex);
    PipInvalidateCards(busExtension);

    //
    // Perform Pnp isolation process.  This will assign card select number for each
    // Pnp Isa card isolated by the system.  All the isolated cards will be put into
    // wait-for-key state.
    //

    ExAcquireFastMutex(&PipPortMutex);

    PipIsolateCards(&busExtension->NumberCSNs, &busExtension->ReadDataPort);
    PipLFSRInitiation ();              // send initiation key to put cards into sleep state

    //
    // For each card selected build CardInformation and DeviceInformation structures.
    //

    for (csn = 1; csn <= busExtension->NumberCSNs; csn++) {

        status = PipReadCardResourceData (
                            csn,
                            &noDevices,
                            &cardData,
                            &dataLength);
        if (!NT_SUCCESS(status)) {
            continue;
        }
        cardInfo = PipFindCardInformation(busExtension, (PSERIAL_IDENTIFIER)cardData);
        if (cardInfo) {

            //
            // Find an existing card information structure with the same serial identifier
            // Go validate the card information and its associated device information.
            //

            cardInfo->Flags |= CARD_FLAGS_VALID;
            cardInfo->CardSelectNumber = csn;
            for (link = cardInfo->LogicalDeviceList.Next; link; link = link->Next) {
                deviceInfo = CONTAINING_RECORD (link, DEVICE_INFORMATION, LogicalDeviceList);
                deviceInfo->Flags |= DEVICE_FLAGS_VALID;
            }
            ExFreePool(cardData);
        } else {

            //
            // Did not find an existing card information matched the card data.
            // Allocate and initialize new card information and its associate device
            // information structures.
            //

            cardInfo = (PCARD_INFORMATION)ExAllocatePoolWithTag(
                                                  NonPagedPool,
                                                  sizeof(CARD_INFORMATION),
                                                  'iPnP');
            if (!cardInfo) {
                ExFreePool(cardData);
#if DBG
                DebugPrint((DEBUG_MESSAGE, "PnpIsa:failed to allocate CARD_INFO structure\n"));
#endif
                continue;
            }

            //
            // Initialize card information structure
            //

            RtlZeroMemory(cardInfo, sizeof(CARD_INFORMATION));
            cardInfo->Flags = CARD_FLAGS_VALID;
            cardInfo->CardSelectNumber = csn;
            cardInfo->NumberLogicalDevices = noDevices;
            cardInfo->CardData = cardData;
            cardInfo->CardDataLength = dataLength;

            ExInterlockedPushEntryList (
                        &busExtension->CardList,
                        &cardInfo->CardList,
                        &PipSpinlock
                        );
#if DBG
            DebugPrint ((DEBUG_MESSAGE, "PnpIsa: adding one pnp card %x\n"));
#endif
            //
            // For each logical device supported by the card build its DEVICE_INFORMATION
            // structures and enable it.
            //

            cardData += sizeof(SERIAL_IDENTIFIER);
            dataLength -= sizeof(SERIAL_IDENTIFIER);
            PipFindNextLogicalDeviceTag(&cardData, &dataLength);
            for (i = 0; i < noDevices; i++) {       // logical device number starts from 0

                //
                // Select the logical device, disable its io range check and
                // enable the device.
                //

                PipWriteAddress(LOGICAL_DEVICE_PORT);
                PipWriteData(i);
                PipWriteAddress(IO_RANGE_CHECK_PORT);
                tmp = PipReadData();
                tmp &= ~2;
                PipWriteAddress(IO_RANGE_CHECK_PORT);
                PipWriteData(tmp);
//                PipWriteAddress(ACTIVATE_PORT);
//                PipWriteData(1);

                notifyBusCheck = TRUE;

                //
                // Initialize the object attributes that will be used to create the
                // Device Handler Object.
                //

                InitializeObjectAttributes(
                    &objectAttributes,
                    NULL,
                    0,
                    NULL,
                    NULL
                    );

                objectSize = PipDeviceHandlerObjectSize + sizeof (DEVICE_INFORMATION);

                //
                // Create the object
                //

                status = ObCreateObject(
                            KernelMode,
                            *IoDeviceHandlerObjectType,
                            &objectAttributes,
                            KernelMode,
                            NULL,
                            objectSize,
                            0,
                            0,
                            (PVOID *) &deviceHandler
                            );

                if (NT_SUCCESS(status)) {
                    RtlZeroMemory (deviceHandler, objectSize);

                    deviceHandler->Type = (USHORT) *IoDeviceHandlerObjectType;
                    deviceHandler->Size = (USHORT) objectSize;
                    deviceHandler->SlotNumber = busExtension->NextSlotNumber++;

                    //
                    // Get a reference to the object
                    //

                    status = ObReferenceObjectByPointer(
                                deviceHandler,
                                FILE_READ_DATA | FILE_WRITE_DATA,
                                *IoDeviceHandlerObjectType,
                                KernelMode
                                );
                }

                if (NT_SUCCESS(status)) {

                    //
                    // Insert it into the object table
                    //

                    status = ObInsertObject(
                                deviceHandler,
                                NULL,
                                FILE_READ_DATA | FILE_WRITE_DATA,
                                0,
                                NULL,
                                &handle
                            );
                }


                if (!NT_SUCCESS(status)) {

                    //
                    // BUGBUG, Deallocate, free reference of the device handler?
                    //

                    //
                    // Object not created correctly. Skip this slot.
                    //

                    continue;
                }

                ZwClose (handle);

                //
                // Intialize device tracking structure
                //

                deviceHandler->BusHandler = BusHandler;
                HalReferenceBusHandler(BusHandler);

                deviceInfo = DeviceHandler2DeviceInfo(deviceHandler);
                deviceInfo->Flags |= DEVICE_FLAGS_VALID;
                deviceInfo->CardInformation = cardInfo;
                deviceInfo->LogicalDeviceNumber = i;
                deviceInfo->DeviceData = cardData;
                deviceInfo->DeviceDataLength = PipFindNextLogicalDeviceTag(&cardData, &dataLength);

                ExInterlockedPushEntryList (
                            &cardInfo->LogicalDeviceList,
                            &deviceInfo->LogicalDeviceList,
                            &PipSpinlock
                            );
#if DBG
                DebugPrint ((DEBUG_MESSAGE, "PnpIsa: adding slot %x\n", DeviceInfoSlot(deviceInfo)));
#endif

                //
                // Add it to the list of devices for this bus
                //

                busExtension->NoValidSlots += 1;
                ExInterlockedPushEntryList (
                            &busExtension->DeviceList,
                            &deviceInfo->DeviceList,
                            &PipSpinlock
                            );

            }
        }
    }

    //
    // Finaly put all cards into wait for key state.
    //

    PipWriteAddress(CONFIG_CONTROL_PORT);
    PipWriteData(CONTROL_WAIT_FOR_KEY);

    ExReleaseFastMutex(&PipPortMutex);

    //
    // Go through the slot data link list to delete all the removed slots.
    //

    noDevices = busExtension->NoValidSlots;
    PipDeleteCards(busExtension);
    if (noDevices != busExtension->NoValidSlots) {
        notifyBusCheck = TRUE;
    }
    ExReleaseFastMutex (&PipMutex);

    //
    // Undo reference to bus handler from top of function
    //

    HalDereferenceBusHandler (BusHandler);

    //
    // Do we need to notify the system buscheck callback?
    //

    if (notifyBusCheck) {
        HAL_BUS_INFORMATION busInfo;

        busInfo.BusType = BusHandler->InterfaceType;
        busInfo.ConfigurationType = BusHandler->ConfigurationType;
        busInfo.BusNumber = BusHandler->BusNumber;
        busInfo.Reserved = 0;
        ExNotifyCallback (
            PipHalCallbacks.BusCheck,
            &busInfo,
            (PVOID)BusHandler->BusNumber
            );
    }
}

VOID
PipInvalidateCards (
    IN PPI_BUS_EXTENSION BusExtension
    )

/*++

Routine Description:

    The function goes through Pnp Isa Card list and marks all the cards and
    the devices associated with the cards as invalid.
    The caller must acquire PipMutex to call this routine.

Arguments:

    BusExtension - supplies a pointer to the extension data of desired bus.

Return Value:

    None.

--*/
{
    PCARD_INFORMATION cardInfo;
    PDEVICE_INFORMATION deviceInfo;
    PSINGLE_LIST_ENTRY cardLink, deviceLink;

    //
    // Go through the card link list to mark ALL the devices as
    // in transition state.
    //

    for (cardLink = BusExtension->CardList.Next; cardLink; cardLink = cardLink->Next) {
        cardInfo = CONTAINING_RECORD (cardLink, CARD_INFORMATION, CardList);
        cardInfo->Flags &= ~CARD_FLAGS_VALID;

        //
        // For each logical device of the card mark it as invalid
        //

        for (deviceLink = cardInfo->LogicalDeviceList.Next; deviceLink; deviceLink = deviceLink->Next) {
            deviceInfo = CONTAINING_RECORD (deviceLink, DEVICE_INFORMATION, LogicalDeviceList);
            deviceInfo->Flags &= ~DEVICE_FLAGS_VALID;
        }
    }

    //
    // Reset the CSN number.
    //

    BusExtension->NumberCSNs = 0;
}

VOID
PipDeleteCards (
     IN PPI_BUS_EXTENSION BusExtension
    )
/*++

Routine Description:

    The function goes through card list and deletes all the invalid
    cards and their associated logical devices.
    Note the PipMutex must be acquired before calling this routine.

Arguments:

    BusExtension - supplies a pointer to the extension data of desired bus.

Return Value:

    None.

--*/
{
    PDEVICE_INFORMATION deviceInfo;
    PCARD_INFORMATION cardInfo;
    PDEVICE_HANDLER_OBJECT deviceHandler;
    PSINGLE_LIST_ENTRY *cardLink, *deviceLink ;

    //
    // Go through the card link list to free all the devices
    // marked as invalid.
    //

    cardLink = &BusExtension->CardList.Next;
    while (*cardLink) {
        cardInfo = CONTAINING_RECORD (*cardLink, CARD_INFORMATION, CardList);
        if (!(cardInfo->Flags & CARD_FLAGS_VALID)) {

            //
            // For each logical device of the card mark it as invalid
            //

            deviceLink = &cardInfo->LogicalDeviceList.Next;
            while (*deviceLink) {
                deviceInfo = CONTAINING_RECORD (*deviceLink, DEVICE_INFORMATION, LogicalDeviceList);
#if DBG
                DebugPrint((DEBUG_MESSAGE, "Remove slot %x on PnpIsa bus\n", DeviceInfoSlot(deviceInfo)));
#endif
                deviceHandler = DeviceInfo2DeviceHandler(deviceInfo);
                ObDereferenceObject (deviceHandler);
                HalDereferenceBusHandler (BusExtension->BusHandler);
                BusExtension->NoValidSlots--;
                *deviceLink = (*deviceLink)->Next;
            }

            //
            // In theory I can't deallocate the card data and card info structures. Because device
            // info structure may still exist.  The deallocation should be done after device info
            // structure ref count is decremented to zero and deallocated.  Here I am safe to do
            // so because DispatchControl routine checks DeviceInfo flag to make sure it is valid
            // before reference card data and card info.
            //

            if (cardInfo->CardData) {
                ExFreePool(cardInfo->CardData);
            }
            *cardLink = (*cardLink)->Next;
            ExFreePool(cardInfo);
        } else {
            cardLink = &((*cardLink)->Next);
        }
    }
}

PCARD_INFORMATION
PipFindCardInformation (
    PPI_BUS_EXTENSION BusExtension,
    PSERIAL_IDENTIFIER Id
    )

/*++

Routine Description:

    The function goes through card list to find the card information structure
    which matches the caller supplied Id.  Note, this routine ignore the card
    structure flags.  So, it may return the card information whcih is marked
    as invalid.
    Note the PipMutex must be acquired before calling this routine.

Arguments:

    Id - supplies a pointer to the serial identifier of the card.

Return Value:

    If succeed, the pointer to the card information structure is return.
    Otherwise a NULL pointer is returned.

--*/
{
    PCARD_INFORMATION cardInfo;
    PSINGLE_LIST_ENTRY cardLink;

    //
    // Go through the card link list to match the id.
    //

    cardLink = BusExtension->CardList.Next;
    while (cardLink) {
        cardInfo = CONTAINING_RECORD (cardLink, CARD_INFORMATION, CardList);
        ASSERT (cardInfo->CardData);
        if (RtlEqualMemory(cardInfo->CardData, Id, sizeof(SERIAL_IDENTIFIER))) {
            return cardInfo;
        }
        cardLink = cardLink->Next;
    }
    return NULL;
}