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