[ACPI]
[reactos.git] / reactos / drivers / bus / acpi / pnp.c
1 #include "precomp.h"
2
3 #define NDEBUG
4 #include <debug.h>
5
6 NTSTATUS
7 Bus_PlugInDevice (
8 struct acpi_device *Device,
9 PFDO_DEVICE_DATA FdoData
10 );
11
12 #ifdef ALLOC_PRAGMA
13 #pragma alloc_text (PAGE, Bus_PnP)
14 #pragma alloc_text (PAGE, Bus_PlugInDevice)
15 #pragma alloc_text (PAGE, Bus_InitializePdo)
16 #pragma alloc_text (PAGE, Bus_DestroyPdo)
17 #pragma alloc_text (PAGE, Bus_FDO_PnP)
18 #pragma alloc_text (PAGE, Bus_StartFdo)
19 #pragma alloc_text (PAGE, Bus_SendIrpSynchronously)
20 #endif
21
22
23 NTSTATUS
24 NTAPI
25 Bus_PnP (
26 PDEVICE_OBJECT DeviceObject,
27 PIRP Irp
28 )
29 {
30 PIO_STACK_LOCATION irpStack;
31 NTSTATUS status;
32 PCOMMON_DEVICE_DATA commonData;
33
34 PAGED_CODE ();
35
36 irpStack = IoGetCurrentIrpStackLocation (Irp);
37 ASSERT (IRP_MJ_PNP == irpStack->MajorFunction);
38
39 commonData = (PCOMMON_DEVICE_DATA) DeviceObject->DeviceExtension;
40
41
42 if (commonData->IsFDO) {
43 DPRINT("FDO %s IRP:0x%p\n",
44 PnPMinorFunctionString(irpStack->MinorFunction),
45 Irp);
46 //
47 // Request is for the bus FDO
48 //
49 status = Bus_FDO_PnP (
50 DeviceObject,
51 Irp,
52 irpStack,
53 (PFDO_DEVICE_DATA) commonData);
54 } else {
55 DPRINT("PDO %s IRP: 0x%p\n",
56 PnPMinorFunctionString(irpStack->MinorFunction),
57 Irp);
58 //
59 // Request is for the child PDO.
60 //
61 status = Bus_PDO_PnP (
62 DeviceObject,
63 Irp,
64 irpStack,
65 (PPDO_DEVICE_DATA) commonData);
66 }
67
68 return status;
69 }
70
71 NTSTATUS
72 Bus_FDO_PnP (
73 PDEVICE_OBJECT DeviceObject,
74 PIRP Irp,
75 PIO_STACK_LOCATION IrpStack,
76 PFDO_DEVICE_DATA DeviceData
77 )
78 {
79 NTSTATUS status;
80 ULONG length, prevcount, numPdosPresent;
81 PLIST_ENTRY entry;
82 PPDO_DEVICE_DATA pdoData;
83 PDEVICE_RELATIONS relations, oldRelations;
84
85 PAGED_CODE ();
86
87 switch (IrpStack->MinorFunction) {
88
89 case IRP_MN_START_DEVICE:
90
91 status = Bus_StartFdo (DeviceData, Irp);
92
93
94 //
95 // We must now complete the IRP, since we stopped it in the
96 // completion routine with MORE_PROCESSING_REQUIRED.
97 //
98
99 Irp->IoStatus.Status = status;
100 IoCompleteRequest (Irp, IO_NO_INCREMENT);
101
102 return status;
103
104 case IRP_MN_QUERY_STOP_DEVICE:
105
106 //
107 // The PnP manager is trying to stop the device
108 // for resource rebalancing.
109 //
110 SET_NEW_PNP_STATE(DeviceData->Common, StopPending);
111 Irp->IoStatus.Status = STATUS_SUCCESS;
112 break;
113
114 case IRP_MN_CANCEL_STOP_DEVICE:
115
116 //
117 // The PnP Manager sends this IRP, at some point after an
118 // IRP_MN_QUERY_STOP_DEVICE, to inform the drivers for a
119 // device that the device will not be stopped for
120 // resource reconfiguration.
121 //
122 //
123 // First check to see whether you have received cancel-stop
124 // without first receiving a query-stop. This could happen if
125 // someone above us fails a query-stop and passes down the subsequent
126 // cancel-stop.
127 //
128
129 if (StopPending == DeviceData->Common.DevicePnPState)
130 {
131 //
132 // We did receive a query-stop, so restore.
133 //
134 RESTORE_PREVIOUS_PNP_STATE(DeviceData->Common);
135 ASSERT(DeviceData->Common.DevicePnPState == Started);
136 }
137 Irp->IoStatus.Status = STATUS_SUCCESS; // We must not fail the IRP.
138 break;
139
140 case IRP_MN_QUERY_DEVICE_RELATIONS:
141
142 DPRINT("\tQueryDeviceRelation Type: %s\n",
143 DbgDeviceRelationString(\
144 IrpStack->Parameters.QueryDeviceRelations.Type));
145
146 if (BusRelations != IrpStack->Parameters.QueryDeviceRelations.Type) {
147 //
148 // We don't support any other Device Relations
149 //
150 break;
151 }
152
153
154 ExAcquireFastMutex (&DeviceData->Mutex);
155
156 oldRelations = (PDEVICE_RELATIONS) Irp->IoStatus.Information;
157 if (oldRelations) {
158 prevcount = oldRelations->Count;
159 if (!DeviceData->NumPDOs) {
160 //
161 // There is a device relations struct already present and we have
162 // nothing to add to it, so just call IoSkip and IoCall
163 //
164 ExReleaseFastMutex (&DeviceData->Mutex);
165 break;
166 }
167 }
168 else {
169 prevcount = 0;
170 }
171
172 //
173 // Calculate the number of PDOs actually present on the bus
174 //
175 numPdosPresent = 0;
176 for (entry = DeviceData->ListOfPDOs.Flink;
177 entry != &DeviceData->ListOfPDOs;
178 entry = entry->Flink) {
179 pdoData = CONTAINING_RECORD (entry, PDO_DEVICE_DATA, Link);
180 numPdosPresent++;
181 }
182
183 //
184 // Need to allocate a new relations structure and add our
185 // PDOs to it.
186 //
187
188 length = sizeof(DEVICE_RELATIONS) +
189 (((numPdosPresent + prevcount) - 1) * sizeof (PDEVICE_OBJECT));
190
191 relations = ExAllocatePoolWithTag(PagedPool, length, 'IpcA');
192
193 if (NULL == relations) {
194 //
195 // Fail the IRP
196 //
197 ExReleaseFastMutex (&DeviceData->Mutex);
198 Irp->IoStatus.Status = status = STATUS_INSUFFICIENT_RESOURCES;
199 IoCompleteRequest (Irp, IO_NO_INCREMENT);
200 return status;
201
202 }
203
204 //
205 // Copy in the device objects so far
206 //
207 if (prevcount) {
208 RtlCopyMemory (relations->Objects, oldRelations->Objects,
209 prevcount * sizeof (PDEVICE_OBJECT));
210 }
211
212 relations->Count = prevcount + numPdosPresent;
213
214 //
215 // For each PDO present on this bus add a pointer to the device relations
216 // buffer, being sure to take out a reference to that object.
217 // The Plug & Play system will dereference the object when it is done
218 // with it and free the device relations buffer.
219 //
220
221 for (entry = DeviceData->ListOfPDOs.Flink;
222 entry != &DeviceData->ListOfPDOs;
223 entry = entry->Flink) {
224
225 pdoData = CONTAINING_RECORD (entry, PDO_DEVICE_DATA, Link);
226 relations->Objects[prevcount] = pdoData->Common.Self;
227 ObReferenceObject (pdoData->Common.Self);
228 prevcount++;
229 }
230
231 DPRINT("\t#PDOs present = %d\n\t#PDOs reported = %d\n",
232 DeviceData->NumPDOs, relations->Count);
233
234 //
235 // Replace the relations structure in the IRP with the new
236 // one.
237 //
238 if (oldRelations) {
239 ExFreePoolWithTag(oldRelations, 0);
240 }
241 Irp->IoStatus.Information = (ULONG_PTR) relations;
242
243 ExReleaseFastMutex (&DeviceData->Mutex);
244
245 //
246 // Set up and pass the IRP further down the stack
247 //
248 Irp->IoStatus.Status = STATUS_SUCCESS;
249 break;
250
251 default:
252
253 //
254 // In the default case we merely call the next driver.
255 // We must not modify Irp->IoStatus.Status or complete the IRP.
256 //
257
258 break;
259 }
260
261 IoSkipCurrentIrpStackLocation (Irp);
262 status = IoCallDriver (DeviceData->NextLowerDriver, Irp);
263 return STATUS_SUCCESS;
264 }
265
266 NTSTATUS
267 Bus_StartFdo (
268 PFDO_DEVICE_DATA FdoData,
269 PIRP Irp )
270 {
271 NTSTATUS status = STATUS_SUCCESS;
272 POWER_STATE powerState;
273 ACPI_STATUS AcpiStatus;
274
275 PAGED_CODE ();
276
277 FdoData->Common.DevicePowerState = PowerDeviceD0;
278 powerState.DeviceState = PowerDeviceD0;
279 PoSetPowerState ( FdoData->Common.Self, DevicePowerState, powerState );
280
281 SET_NEW_PNP_STATE(FdoData->Common, Started);
282
283 AcpiStatus = AcpiInitializeSubsystem();
284 if(ACPI_FAILURE(AcpiStatus)){
285 DPRINT1("Unable to AcpiInitializeSubsystem\n");
286 return STATUS_UNSUCCESSFUL;
287 }
288
289
290 AcpiStatus = AcpiInitializeTables(NULL, 16, 0);
291 if (ACPI_FAILURE(status)){
292 DPRINT1("Unable to AcpiInitializeSubsystem\n");
293 return STATUS_UNSUCCESSFUL;
294 }
295
296 AcpiStatus = AcpiLoadTables();
297 if(ACPI_FAILURE(AcpiStatus)){
298 DPRINT1("Unable to AcpiLoadTables\n");
299 AcpiTerminate();
300 return STATUS_UNSUCCESSFUL;
301 }
302
303 DPRINT("Acpi subsystem init\n");
304 /* Initialize ACPI bus manager */
305 AcpiStatus = acpi_init();
306 if (!ACPI_SUCCESS(AcpiStatus)) {
307 DPRINT1("acpi_init() failed with status 0x%X\n", AcpiStatus);
308 AcpiTerminate();
309 return STATUS_UNSUCCESSFUL;
310 }
311 status = ACPIEnumerateDevices(FdoData);
312
313 return status;
314 }
315
316 NTSTATUS
317 Bus_SendIrpSynchronously (
318 PDEVICE_OBJECT DeviceObject,
319 PIRP Irp
320 )
321 {
322 KEVENT event;
323 NTSTATUS status;
324
325 PAGED_CODE();
326
327 KeInitializeEvent(&event, NotificationEvent, FALSE);
328
329 IoCopyCurrentIrpStackLocationToNext(Irp);
330
331 IoSetCompletionRoutine(Irp,
332 Bus_CompletionRoutine,
333 &event,
334 TRUE,
335 TRUE,
336 TRUE
337 );
338
339 status = IoCallDriver(DeviceObject, Irp);
340
341 //
342 // Wait for lower drivers to be done with the Irp.
343 // Important thing to note here is when you allocate
344 // the memory for an event in the stack you must do a
345 // KernelMode wait instead of UserMode to prevent
346 // the stack from getting paged out.
347 //
348
349 if (status == STATUS_PENDING) {
350 KeWaitForSingleObject(&event,
351 Executive,
352 KernelMode,
353 FALSE,
354 NULL
355 );
356 status = Irp->IoStatus.Status;
357 }
358
359 return status;
360 }
361
362 NTSTATUS
363 NTAPI
364 Bus_CompletionRoutine(
365 PDEVICE_OBJECT DeviceObject,
366 PIRP Irp,
367 PVOID Context
368 )
369 {
370 UNREFERENCED_PARAMETER (DeviceObject);
371
372 //
373 // If the lower driver didn't return STATUS_PENDING, we don't need to
374 // set the event because we won't be waiting on it.
375 // This optimization avoids grabbing the dispatcher lock and improves perf.
376 //
377 if (Irp->PendingReturned == TRUE) {
378
379 KeSetEvent ((PKEVENT) Context, IO_NO_INCREMENT, FALSE);
380 }
381 return STATUS_MORE_PROCESSING_REQUIRED; // Keep this IRP
382 }
383
384 NTSTATUS
385 Bus_DestroyPdo (
386 PDEVICE_OBJECT Device,
387 PPDO_DEVICE_DATA PdoData
388 )
389 {
390 PAGED_CODE ();
391
392 //
393 // BusEnum does not queue any irps at this time so we have nothing to do.
394 //
395
396 //
397 // Free any resources.
398 //
399
400 if (PdoData->HardwareIDs) {
401 ExFreePoolWithTag(PdoData->HardwareIDs, 'DpcA');
402 PdoData->HardwareIDs = NULL;
403 }
404
405 DPRINT("\tDeleting PDO: 0x%p\n", Device);
406 IoDeleteDevice (Device);
407 return STATUS_SUCCESS;
408 }
409
410
411 VOID
412 Bus_InitializePdo (
413 PDEVICE_OBJECT Pdo,
414 PFDO_DEVICE_DATA FdoData
415 )
416 {
417 PPDO_DEVICE_DATA pdoData;
418 int acpistate;
419 DEVICE_POWER_STATE ntState;
420
421 PAGED_CODE ();
422
423 pdoData = (PPDO_DEVICE_DATA) Pdo->DeviceExtension;
424
425 DPRINT("pdo 0x%p, extension 0x%p\n", Pdo, pdoData);
426
427 if (pdoData->AcpiHandle)
428 acpi_bus_get_power(pdoData->AcpiHandle, &acpistate);
429 else
430 acpistate = ACPI_STATE_D0;
431
432 switch(acpistate)
433 {
434 case ACPI_STATE_D0:
435 ntState = PowerDeviceD0;
436 break;
437 case ACPI_STATE_D1:
438 ntState = PowerDeviceD1;
439 break;
440 case ACPI_STATE_D2:
441 ntState = PowerDeviceD2;
442 break;
443 case ACPI_STATE_D3:
444 ntState = PowerDeviceD3;
445 break;
446 default:
447 DPRINT1("Unknown power state (%d) returned by acpi\n",acpistate);
448 ntState = PowerDeviceUnspecified;
449 break;
450 }
451
452 //
453 // Initialize the rest
454 //
455 pdoData->Common.IsFDO = FALSE;
456 pdoData->Common.Self = Pdo;
457
458 pdoData->ParentFdo = FdoData->Common.Self;
459
460
461 INITIALIZE_PNP_STATE(pdoData->Common);
462
463 pdoData->Common.DevicePowerState = ntState;
464 pdoData->Common.SystemPowerState = FdoData->Common.SystemPowerState;
465
466 Pdo->Flags |= DO_POWER_PAGABLE;
467
468 ExAcquireFastMutex (&FdoData->Mutex);
469 InsertTailList(&FdoData->ListOfPDOs, &pdoData->Link);
470 FdoData->NumPDOs++;
471 ExReleaseFastMutex (&FdoData->Mutex);
472
473 // This should be the last step in initialization.
474 Pdo->Flags &= ~DO_DEVICE_INITIALIZING;
475
476 }
477
478 #if DBG
479
480 PCHAR
481 PnPMinorFunctionString (
482 UCHAR MinorFunction
483 )
484 {
485 switch (MinorFunction)
486 {
487 case IRP_MN_START_DEVICE:
488 return "IRP_MN_START_DEVICE";
489 case IRP_MN_QUERY_REMOVE_DEVICE:
490 return "IRP_MN_QUERY_REMOVE_DEVICE";
491 case IRP_MN_REMOVE_DEVICE:
492 return "IRP_MN_REMOVE_DEVICE";
493 case IRP_MN_CANCEL_REMOVE_DEVICE:
494 return "IRP_MN_CANCEL_REMOVE_DEVICE";
495 case IRP_MN_STOP_DEVICE:
496 return "IRP_MN_STOP_DEVICE";
497 case IRP_MN_QUERY_STOP_DEVICE:
498 return "IRP_MN_QUERY_STOP_DEVICE";
499 case IRP_MN_CANCEL_STOP_DEVICE:
500 return "IRP_MN_CANCEL_STOP_DEVICE";
501 case IRP_MN_QUERY_DEVICE_RELATIONS:
502 return "IRP_MN_QUERY_DEVICE_RELATIONS";
503 case IRP_MN_QUERY_INTERFACE:
504 return "IRP_MN_QUERY_INTERFACE";
505 case IRP_MN_QUERY_CAPABILITIES:
506 return "IRP_MN_QUERY_CAPABILITIES";
507 case IRP_MN_QUERY_RESOURCES:
508 return "IRP_MN_QUERY_RESOURCES";
509 case IRP_MN_QUERY_RESOURCE_REQUIREMENTS:
510 return "IRP_MN_QUERY_RESOURCE_REQUIREMENTS";
511 case IRP_MN_QUERY_DEVICE_TEXT:
512 return "IRP_MN_QUERY_DEVICE_TEXT";
513 case IRP_MN_FILTER_RESOURCE_REQUIREMENTS:
514 return "IRP_MN_FILTER_RESOURCE_REQUIREMENTS";
515 case IRP_MN_READ_CONFIG:
516 return "IRP_MN_READ_CONFIG";
517 case IRP_MN_WRITE_CONFIG:
518 return "IRP_MN_WRITE_CONFIG";
519 case IRP_MN_EJECT:
520 return "IRP_MN_EJECT";
521 case IRP_MN_SET_LOCK:
522 return "IRP_MN_SET_LOCK";
523 case IRP_MN_QUERY_ID:
524 return "IRP_MN_QUERY_ID";
525 case IRP_MN_QUERY_PNP_DEVICE_STATE:
526 return "IRP_MN_QUERY_PNP_DEVICE_STATE";
527 case IRP_MN_QUERY_BUS_INFORMATION:
528 return "IRP_MN_QUERY_BUS_INFORMATION";
529 case IRP_MN_DEVICE_USAGE_NOTIFICATION:
530 return "IRP_MN_DEVICE_USAGE_NOTIFICATION";
531 case IRP_MN_SURPRISE_REMOVAL:
532 return "IRP_MN_SURPRISE_REMOVAL";
533 case IRP_MN_QUERY_LEGACY_BUS_INFORMATION:
534 return "IRP_MN_QUERY_LEGACY_BUS_INFORMATION";
535 default:
536 return "unknown_pnp_irp";
537 }
538 }
539
540 PCHAR
541 DbgDeviceRelationString(
542 DEVICE_RELATION_TYPE Type
543 )
544 {
545 switch (Type)
546 {
547 case BusRelations:
548 return "BusRelations";
549 case EjectionRelations:
550 return "EjectionRelations";
551 case RemovalRelations:
552 return "RemovalRelations";
553 case TargetDeviceRelation:
554 return "TargetDeviceRelation";
555 default:
556 return "UnKnown Relation";
557 }
558 }
559
560 PCHAR
561 DbgDeviceIDString(
562 BUS_QUERY_ID_TYPE Type
563 )
564 {
565 switch (Type)
566 {
567 case BusQueryDeviceID:
568 return "BusQueryDeviceID";
569 case BusQueryHardwareIDs:
570 return "BusQueryHardwareIDs";
571 case BusQueryCompatibleIDs:
572 return "BusQueryCompatibleIDs";
573 case BusQueryInstanceID:
574 return "BusQueryInstanceID";
575 case BusQueryDeviceSerialNumber:
576 return "BusQueryDeviceSerialNumber";
577 default:
578 return "UnKnown ID";
579 }
580 }
581
582 #endif
583
584