Sync with trunk r58740.
[reactos.git] / drivers / bus / isapnp / fdo.c
1 /*
2 * PROJECT: ReactOS ISA PnP Bus driver
3 * FILE: fdo.c
4 * PURPOSE: FDO-specific code
5 * PROGRAMMERS: Cameron Gutman (cameron.gutman@reactos.org)
6 */
7 #include <isapnp.h>
8
9 #define NDEBUG
10 #include <debug.h>
11
12 NTSTATUS
13 NTAPI
14 IsaFdoStartDevice(
15 IN PISAPNP_FDO_EXTENSION FdoExt,
16 IN PIRP Irp,
17 IN PIO_STACK_LOCATION IrpSp)
18 {
19 NTSTATUS Status;
20 KIRQL OldIrql;
21
22 KeAcquireSpinLock(&FdoExt->Lock, &OldIrql);
23
24 Status = IsaHwDetectReadDataPort(FdoExt);
25 if (!NT_SUCCESS(Status))
26 {
27 KeReleaseSpinLock(&FdoExt->Lock, OldIrql);
28 return Status;
29 }
30
31 FdoExt->Common.State = dsStarted;
32
33 KeReleaseSpinLock(&FdoExt->Lock, OldIrql);
34
35 return STATUS_SUCCESS;
36 }
37
38 NTSTATUS
39 NTAPI
40 IsaFdoQueryDeviceRelations(
41 IN PISAPNP_FDO_EXTENSION FdoExt,
42 IN PIRP Irp,
43 IN PIO_STACK_LOCATION IrpSp)
44 {
45 NTSTATUS Status;
46 PLIST_ENTRY CurrentEntry;
47 PISAPNP_LOGICAL_DEVICE IsaDevice;
48 PDEVICE_RELATIONS DeviceRelations;
49 KIRQL OldIrql;
50 ULONG i = 0;
51
52 if (IrpSp->Parameters.QueryDeviceRelations.Type != BusRelations)
53 return Irp->IoStatus.Status;
54
55 KeAcquireSpinLock(&FdoExt->Lock, &OldIrql);
56
57 Status = IsaHwFillDeviceList(FdoExt);
58 if (!NT_SUCCESS(Status))
59 {
60 KeReleaseSpinLock(&FdoExt->Lock, OldIrql);
61 return Status;
62 }
63
64 DeviceRelations = ExAllocatePool(NonPagedPool,
65 sizeof(DEVICE_RELATIONS) + sizeof(DEVICE_OBJECT) * (FdoExt->DeviceCount - 1));
66 if (!DeviceRelations)
67 {
68 KeReleaseSpinLock(&FdoExt->Lock, OldIrql);
69 return STATUS_INSUFFICIENT_RESOURCES;
70 }
71
72 CurrentEntry = FdoExt->DeviceListHead.Flink;
73 while (CurrentEntry != &FdoExt->DeviceListHead)
74 {
75 IsaDevice = CONTAINING_RECORD(CurrentEntry, ISAPNP_LOGICAL_DEVICE, ListEntry);
76
77 DeviceRelations->Objects[i++] = IsaDevice->Common.Self;
78
79 ObReferenceObject(IsaDevice->Common.Self);
80
81 CurrentEntry = CurrentEntry->Flink;
82 }
83
84 DeviceRelations->Count = FdoExt->DeviceCount;
85
86 KeReleaseSpinLock(&FdoExt->Lock, OldIrql);
87
88 Irp->IoStatus.Information = (ULONG_PTR)DeviceRelations;
89
90 return STATUS_SUCCESS;
91 }
92
93 NTSTATUS
94 NTAPI
95 IsaFdoPnp(
96 IN PISAPNP_FDO_EXTENSION FdoExt,
97 IN PIRP Irp,
98 IN PIO_STACK_LOCATION IrpSp)
99 {
100 NTSTATUS Status = Irp->IoStatus.Status;
101
102 switch (IrpSp->MinorFunction)
103 {
104 case IRP_MN_START_DEVICE:
105 Status = IsaForwardIrpSynchronous(FdoExt, Irp);
106
107 if (NT_SUCCESS(Status))
108 Status = IsaFdoStartDevice(FdoExt, Irp, IrpSp);
109
110 Irp->IoStatus.Status = Status;
111
112 IoCompleteRequest(Irp, IO_NO_INCREMENT);
113 return Status;
114
115 case IRP_MN_STOP_DEVICE:
116 FdoExt->Common.State = dsStopped;
117
118 Status = STATUS_SUCCESS;
119 break;
120
121 case IRP_MN_QUERY_DEVICE_RELATIONS:
122 Status = IsaFdoQueryDeviceRelations(FdoExt, Irp, IrpSp);
123
124 Irp->IoStatus.Status = Status;
125
126 IoCompleteRequest(Irp, IO_NO_INCREMENT);
127 return Status;
128
129 case IRP_MN_FILTER_RESOURCE_REQUIREMENTS:
130 DPRINT("IRP_MN_FILTER_RESOURCE_REQUIREMENTS\n");
131 break;
132
133 default:
134 DPRINT1("Unknown PnP code: %x\n", IrpSp->MinorFunction);
135 break;
136 }
137
138 IoSkipCurrentIrpStackLocation(Irp);
139
140 return IoCallDriver(FdoExt->Ldo, Irp);
141 }