- Define NDEBUG and demote several non-critical debug prints to DPRINT
[reactos.git] / reactos / drivers / bus / acpi / main.c
1 #include <ntddk.h>
2
3 #include <acpi.h>
4 #include <acpisys.h>
5
6 #include <acpi_bus.h>
7 #include <acpi_drivers.h>
8
9 #define NDEBUG
10 #include <debug.h>
11
12 #ifdef ALLOC_PRAGMA
13 #pragma alloc_text (INIT, DriverEntry)
14 #pragma alloc_text (PAGE, Bus_AddDevice)
15
16 #endif
17
18
19
20 NTSTATUS
21 NTAPI
22 Bus_AddDevice(
23 PDRIVER_OBJECT DriverObject,
24 PDEVICE_OBJECT PhysicalDeviceObject
25 )
26
27 {
28 NTSTATUS status;
29 PDEVICE_OBJECT deviceObject = NULL;
30 PFDO_DEVICE_DATA deviceData = NULL;
31 PWCHAR deviceName = NULL;
32 ULONG nameLength;
33
34 PAGED_CODE ();
35
36 DPRINT("Add Device: 0x%p\n", PhysicalDeviceObject);
37
38 DPRINT("#################### Bus_CreateClose Creating FDO Device ####################\n");
39 status = IoCreateDevice(DriverObject,
40 sizeof(FDO_DEVICE_DATA),
41 NULL,
42 FILE_DEVICE_ACPI,
43 FILE_DEVICE_SECURE_OPEN,
44 TRUE,
45 &deviceObject);
46 if (!NT_SUCCESS(status))
47 {
48 DPRINT1("IoCreateDevice() failed with status 0x%X\n", status);
49 goto End;
50 }
51
52 deviceData = (PFDO_DEVICE_DATA) deviceObject->DeviceExtension;
53 RtlZeroMemory (deviceData, sizeof (FDO_DEVICE_DATA));
54
55 //
56 // Set the initial state of the FDO
57 //
58
59 INITIALIZE_PNP_STATE(deviceData->Common);
60
61 deviceData->Common.IsFDO = TRUE;
62
63 deviceData->Common.Self = deviceObject;
64
65 ExInitializeFastMutex (&deviceData->Mutex);
66
67 InitializeListHead (&deviceData->ListOfPDOs);
68
69 // Set the PDO for use with PlugPlay functions
70
71 deviceData->UnderlyingPDO = PhysicalDeviceObject;
72
73 //
74 // Set the initial powerstate of the FDO
75 //
76
77 deviceData->Common.DevicePowerState = PowerDeviceUnspecified;
78 deviceData->Common.SystemPowerState = PowerSystemWorking;
79
80 deviceObject->Flags |= DO_POWER_PAGABLE;
81
82 //
83 // Attach our FDO to the device stack.
84 // The return value of IoAttachDeviceToDeviceStack is the top of the
85 // attachment chain. This is where all the IRPs should be routed.
86 //
87
88 deviceData->NextLowerDriver = IoAttachDeviceToDeviceStack (
89 deviceObject,
90 PhysicalDeviceObject);
91
92 if (NULL == deviceData->NextLowerDriver) {
93
94 status = STATUS_NO_SUCH_DEVICE;
95 goto End;
96 }
97
98
99 #ifndef NDEBUG
100 //
101 // We will demonstrate here the step to retrieve the name of the PDO
102 //
103
104 status = IoGetDeviceProperty (PhysicalDeviceObject,
105 DevicePropertyPhysicalDeviceObjectName,
106 0,
107 NULL,
108 &nameLength);
109
110 if (status != STATUS_BUFFER_TOO_SMALL)
111 {
112 DPRINT1("AddDevice:IoGDP failed (0x%x)\n", status);
113 goto End;
114 }
115
116 deviceName = ExAllocatePoolWithTag (NonPagedPool,
117 nameLength, 'IPCA');
118
119 if (NULL == deviceName) {
120 DPRINT1("AddDevice: no memory to alloc for deviceName(0x%x)\n", nameLength);
121 status = STATUS_INSUFFICIENT_RESOURCES;
122 goto End;
123 }
124
125 status = IoGetDeviceProperty (PhysicalDeviceObject,
126 DevicePropertyPhysicalDeviceObjectName,
127 nameLength,
128 deviceName,
129 &nameLength);
130
131 if (!NT_SUCCESS (status)) {
132
133 DPRINT1("AddDevice:IoGDP(2) failed (0x%x)", status);
134 goto End;
135 }
136
137 DPRINT("AddDevice: %p to %p->%p (%ws) \n",
138 deviceObject,
139 deviceData->NextLowerDriver,
140 PhysicalDeviceObject,
141 deviceName);
142
143 #endif
144
145 //
146 // We are done with initializing, so let's indicate that and return.
147 // This should be the final step in the AddDevice process.
148 //
149 deviceObject->Flags &= ~DO_DEVICE_INITIALIZING;
150
151 End:
152 if (deviceName){
153 ExFreePool(deviceName);
154 }
155 if (!NT_SUCCESS(status) && deviceObject){
156 if (deviceData && deviceData->NextLowerDriver){
157 IoDetachDevice (deviceData->NextLowerDriver);
158 }
159 IoDeleteDevice (deviceObject);
160 }
161 return status;
162
163 }
164
165 NTSTATUS
166 NTAPI
167 ACPIDispatchDeviceControl(
168 IN PDEVICE_OBJECT DeviceObject,
169 IN PIRP Irp)
170 {
171 PIO_STACK_LOCATION IrpSp;
172 NTSTATUS Status;
173
174 DPRINT("Called. IRP is at (0x%X)\n", Irp);
175
176 Irp->IoStatus.Information = 0;
177
178 IrpSp = IoGetCurrentIrpStackLocation(Irp);
179 switch (IrpSp->Parameters.DeviceIoControl.IoControlCode) {
180 default:
181 DPRINT("Unknown IOCTL 0x%X\n", IrpSp->Parameters.DeviceIoControl.IoControlCode);
182 Status = STATUS_NOT_IMPLEMENTED;
183 break;
184 }
185
186 if (Status != STATUS_PENDING) {
187 Irp->IoStatus.Status = Status;
188
189 DPRINT("Completing IRP at 0x%X\n", Irp);
190
191 IoCompleteRequest(Irp, IO_NO_INCREMENT);
192 }
193
194 DPRINT("Leaving. Status 0x%X\n", Status);
195
196 return Status;
197 }
198
199 NTSTATUS
200 NTAPI
201 DriverEntry (
202 PDRIVER_OBJECT DriverObject,
203 PUNICODE_STRING RegistryPath
204 )
205 {
206 DPRINT("Driver Entry \n");
207
208 //
209 // Set entry points into the driver
210 //
211 DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = ACPIDispatchDeviceControl;
212 DriverObject->MajorFunction [IRP_MJ_PNP] = Bus_PnP;
213 DriverObject->MajorFunction [IRP_MJ_POWER] = Bus_Power;
214
215 DriverObject->DriverExtension->AddDevice = Bus_AddDevice;
216
217 return STATUS_SUCCESS;
218 }