8549da43f5c0862f1d0bd035f3620cd0637acfb7
[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;
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 AcpiStatus = AcpiInitializeTables(NULL, 16, 0);
290 if (ACPI_FAILURE(AcpiStatus)){
291 DPRINT1("Unable to AcpiInitializeTables\n");
292 return STATUS_UNSUCCESSFUL;
293 }
294
295 AcpiStatus = AcpiLoadTables();
296 if(ACPI_FAILURE(AcpiStatus)){
297 DPRINT1("Unable to AcpiLoadTables\n");
298 AcpiTerminate();
299 return STATUS_UNSUCCESSFUL;
300 }
301
302 DPRINT("Acpi subsystem init\n");
303 /* Initialize ACPI bus manager */
304 AcpiStatus = acpi_init();
305 if (!ACPI_SUCCESS(AcpiStatus)) {
306 DPRINT1("acpi_init() failed with status 0x%X\n", AcpiStatus);
307 AcpiTerminate();
308 return STATUS_UNSUCCESSFUL;
309 }
310 status = ACPIEnumerateDevices(FdoData);
311
312 return status;
313 }
314
315 NTSTATUS
316 Bus_SendIrpSynchronously (
317 PDEVICE_OBJECT DeviceObject,
318 PIRP Irp
319 )
320 {
321 KEVENT event;
322 NTSTATUS status;
323
324 PAGED_CODE();
325
326 KeInitializeEvent(&event, NotificationEvent, FALSE);
327
328 IoCopyCurrentIrpStackLocationToNext(Irp);
329
330 IoSetCompletionRoutine(Irp,
331 Bus_CompletionRoutine,
332 &event,
333 TRUE,
334 TRUE,
335 TRUE
336 );
337
338 status = IoCallDriver(DeviceObject, Irp);
339
340 //
341 // Wait for lower drivers to be done with the Irp.
342 // Important thing to note here is when you allocate
343 // the memory for an event in the stack you must do a
344 // KernelMode wait instead of UserMode to prevent
345 // the stack from getting paged out.
346 //
347
348 if (status == STATUS_PENDING) {
349 KeWaitForSingleObject(&event,
350 Executive,
351 KernelMode,
352 FALSE,
353 NULL
354 );
355 status = Irp->IoStatus.Status;
356 }
357
358 return status;
359 }
360
361 NTSTATUS
362 NTAPI
363 Bus_CompletionRoutine(
364 PDEVICE_OBJECT DeviceObject,
365 PIRP Irp,
366 PVOID Context
367 )
368 {
369 UNREFERENCED_PARAMETER (DeviceObject);
370
371 //
372 // If the lower driver didn't return STATUS_PENDING, we don't need to
373 // set the event because we won't be waiting on it.
374 // This optimization avoids grabbing the dispatcher lock and improves perf.
375 //
376 if (Irp->PendingReturned == TRUE) {
377
378 KeSetEvent ((PKEVENT) Context, IO_NO_INCREMENT, FALSE);
379 }
380 return STATUS_MORE_PROCESSING_REQUIRED; // Keep this IRP
381 }
382
383 NTSTATUS
384 Bus_DestroyPdo (
385 PDEVICE_OBJECT Device,
386 PPDO_DEVICE_DATA PdoData
387 )
388 {
389 PAGED_CODE ();
390
391 //
392 // BusEnum does not queue any irps at this time so we have nothing to do.
393 //
394
395 //
396 // Free any resources.
397 //
398
399 if (PdoData->HardwareIDs) {
400 ExFreePoolWithTag(PdoData->HardwareIDs, 'DpcA');
401 PdoData->HardwareIDs = NULL;
402 }
403
404 DPRINT("\tDeleting PDO: 0x%p\n", Device);
405 IoDeleteDevice (Device);
406 return STATUS_SUCCESS;
407 }
408
409
410 VOID
411 Bus_InitializePdo (
412 PDEVICE_OBJECT Pdo,
413 PFDO_DEVICE_DATA FdoData
414 )
415 {
416 PPDO_DEVICE_DATA pdoData;
417 int acpistate;
418 DEVICE_POWER_STATE ntState;
419
420 PAGED_CODE ();
421
422 pdoData = (PPDO_DEVICE_DATA) Pdo->DeviceExtension;
423
424 DPRINT("pdo 0x%p, extension 0x%p\n", Pdo, pdoData);
425
426 if (pdoData->AcpiHandle)
427 acpi_bus_get_power(pdoData->AcpiHandle, &acpistate);
428 else
429 acpistate = ACPI_STATE_D0;
430
431 switch(acpistate)
432 {
433 case ACPI_STATE_D0:
434 ntState = PowerDeviceD0;
435 break;
436 case ACPI_STATE_D1:
437 ntState = PowerDeviceD1;
438 break;
439 case ACPI_STATE_D2:
440 ntState = PowerDeviceD2;
441 break;
442 case ACPI_STATE_D3:
443 ntState = PowerDeviceD3;
444 break;
445 default:
446 DPRINT1("Unknown power state (%d) returned by acpi\n",acpistate);
447 ntState = PowerDeviceUnspecified;
448 break;
449 }
450
451 //
452 // Initialize the rest
453 //
454 pdoData->Common.IsFDO = FALSE;
455 pdoData->Common.Self = Pdo;
456
457 pdoData->ParentFdo = FdoData->Common.Self;
458
459
460 INITIALIZE_PNP_STATE(pdoData->Common);
461
462 pdoData->Common.DevicePowerState = ntState;
463 pdoData->Common.SystemPowerState = FdoData->Common.SystemPowerState;
464
465 ExAcquireFastMutex (&FdoData->Mutex);
466 InsertTailList(&FdoData->ListOfPDOs, &pdoData->Link);
467 FdoData->NumPDOs++;
468 ExReleaseFastMutex (&FdoData->Mutex);
469
470 // This should be the last step in initialization.
471 Pdo->Flags &= ~DO_DEVICE_INITIALIZING;
472
473 }
474
475 #if DBG
476
477 PCHAR
478 PnPMinorFunctionString (
479 UCHAR MinorFunction
480 )
481 {
482 switch (MinorFunction)
483 {
484 case IRP_MN_START_DEVICE:
485 return "IRP_MN_START_DEVICE";
486 case IRP_MN_QUERY_REMOVE_DEVICE:
487 return "IRP_MN_QUERY_REMOVE_DEVICE";
488 case IRP_MN_REMOVE_DEVICE:
489 return "IRP_MN_REMOVE_DEVICE";
490 case IRP_MN_CANCEL_REMOVE_DEVICE:
491 return "IRP_MN_CANCEL_REMOVE_DEVICE";
492 case IRP_MN_STOP_DEVICE:
493 return "IRP_MN_STOP_DEVICE";
494 case IRP_MN_QUERY_STOP_DEVICE:
495 return "IRP_MN_QUERY_STOP_DEVICE";
496 case IRP_MN_CANCEL_STOP_DEVICE:
497 return "IRP_MN_CANCEL_STOP_DEVICE";
498 case IRP_MN_QUERY_DEVICE_RELATIONS:
499 return "IRP_MN_QUERY_DEVICE_RELATIONS";
500 case IRP_MN_QUERY_INTERFACE:
501 return "IRP_MN_QUERY_INTERFACE";
502 case IRP_MN_QUERY_CAPABILITIES:
503 return "IRP_MN_QUERY_CAPABILITIES";
504 case IRP_MN_QUERY_RESOURCES:
505 return "IRP_MN_QUERY_RESOURCES";
506 case IRP_MN_QUERY_RESOURCE_REQUIREMENTS:
507 return "IRP_MN_QUERY_RESOURCE_REQUIREMENTS";
508 case IRP_MN_QUERY_DEVICE_TEXT:
509 return "IRP_MN_QUERY_DEVICE_TEXT";
510 case IRP_MN_FILTER_RESOURCE_REQUIREMENTS:
511 return "IRP_MN_FILTER_RESOURCE_REQUIREMENTS";
512 case IRP_MN_READ_CONFIG:
513 return "IRP_MN_READ_CONFIG";
514 case IRP_MN_WRITE_CONFIG:
515 return "IRP_MN_WRITE_CONFIG";
516 case IRP_MN_EJECT:
517 return "IRP_MN_EJECT";
518 case IRP_MN_SET_LOCK:
519 return "IRP_MN_SET_LOCK";
520 case IRP_MN_QUERY_ID:
521 return "IRP_MN_QUERY_ID";
522 case IRP_MN_QUERY_PNP_DEVICE_STATE:
523 return "IRP_MN_QUERY_PNP_DEVICE_STATE";
524 case IRP_MN_QUERY_BUS_INFORMATION:
525 return "IRP_MN_QUERY_BUS_INFORMATION";
526 case IRP_MN_DEVICE_USAGE_NOTIFICATION:
527 return "IRP_MN_DEVICE_USAGE_NOTIFICATION";
528 case IRP_MN_SURPRISE_REMOVAL:
529 return "IRP_MN_SURPRISE_REMOVAL";
530 case IRP_MN_QUERY_LEGACY_BUS_INFORMATION:
531 return "IRP_MN_QUERY_LEGACY_BUS_INFORMATION";
532 default:
533 return "unknown_pnp_irp";
534 }
535 }
536
537 PCHAR
538 DbgDeviceRelationString(
539 DEVICE_RELATION_TYPE Type
540 )
541 {
542 switch (Type)
543 {
544 case BusRelations:
545 return "BusRelations";
546 case EjectionRelations:
547 return "EjectionRelations";
548 case RemovalRelations:
549 return "RemovalRelations";
550 case TargetDeviceRelation:
551 return "TargetDeviceRelation";
552 default:
553 return "UnKnown Relation";
554 }
555 }
556
557 PCHAR
558 DbgDeviceIDString(
559 BUS_QUERY_ID_TYPE Type
560 )
561 {
562 switch (Type)
563 {
564 case BusQueryDeviceID:
565 return "BusQueryDeviceID";
566 case BusQueryHardwareIDs:
567 return "BusQueryHardwareIDs";
568 case BusQueryCompatibleIDs:
569 return "BusQueryCompatibleIDs";
570 case BusQueryInstanceID:
571 return "BusQueryInstanceID";
572 case BusQueryDeviceSerialNumber:
573 return "BusQueryDeviceSerialNumber";
574 default:
575 return "UnKnown ID";
576 }
577 }
578
579 #endif
580
581