eb8f66860860ace3e3c46f41bfd3f897c95e0634
[reactos.git] / reactos / drivers / bus / acpi / buspdo.c
1 #include <ntddk.h>
2
3 #include <acpi.h>
4 #include <acpisys.h>
5 #include <wdmguid.h>
6 #include <stdio.h>
7
8 #include <acpi_bus.h>
9 #include <acpi_drivers.h>
10
11 //#define NDEBUG
12 #include <debug.h>
13
14 #ifdef ALLOC_PRAGMA
15 #pragma alloc_text (PAGE, Bus_PDO_PnP)
16 #pragma alloc_text (PAGE, Bus_PDO_QueryDeviceCaps)
17 #pragma alloc_text (PAGE, Bus_PDO_QueryDeviceId)
18 #pragma alloc_text (PAGE, Bus_PDO_QueryDeviceText)
19 #pragma alloc_text (PAGE, Bus_PDO_QueryResources)
20 #pragma alloc_text (PAGE, Bus_PDO_QueryResourceRequirements)
21 #pragma alloc_text (PAGE, Bus_PDO_QueryDeviceRelations)
22 #pragma alloc_text (PAGE, Bus_PDO_QueryBusInformation)
23 #pragma alloc_text (PAGE, Bus_GetDeviceCapabilities)
24 #endif
25
26 NTSTATUS
27 Bus_PDO_PnP (
28 PDEVICE_OBJECT DeviceObject,
29 PIRP Irp,
30 PIO_STACK_LOCATION IrpStack,
31 PPDO_DEVICE_DATA DeviceData
32 )
33 {
34 NTSTATUS status;
35 POWER_STATE state;
36
37 PAGED_CODE ();
38
39 //
40 // NB: Because we are a bus enumerator, we have no one to whom we could
41 // defer these irps. Therefore we do not pass them down but merely
42 // return them.
43 //
44
45 switch (IrpStack->MinorFunction) {
46
47 case IRP_MN_START_DEVICE:
48
49 //
50 // Here we do what ever initialization and ``turning on'' that is
51 // required to allow others to access this device.
52 // Power up the device.
53 //
54 if (DeviceData->AcpiHandle && acpi_bus_power_manageable(DeviceData->AcpiHandle) &&
55 !ACPI_SUCCESS(acpi_bus_set_power(DeviceData->AcpiHandle, ACPI_STATE_D0)))
56 {
57 DPRINT1("Device %x failed to start!\n", DeviceData->AcpiHandle);
58 status = STATUS_UNSUCCESSFUL;
59 break;
60 }
61
62 state.DeviceState = PowerDeviceD0;
63 PoSetPowerState(DeviceData->Common.Self, DevicePowerState, state);
64 DeviceData->Common.DevicePowerState = PowerDeviceD0;
65 SET_NEW_PNP_STATE(DeviceData->Common, Started);
66 status = STATUS_SUCCESS;
67 break;
68
69 case IRP_MN_STOP_DEVICE:
70
71 //
72 // Here we shut down the device and give up and unmap any resources
73 // we acquired for the device.
74 //
75 if (DeviceData->AcpiHandle && acpi_bus_power_manageable(DeviceData->AcpiHandle) &&
76 !ACPI_SUCCESS(acpi_bus_set_power(DeviceData->AcpiHandle, ACPI_STATE_D3)))
77 {
78 DPRINT1("Device %x failed to stop!\n", DeviceData->AcpiHandle);
79 status = STATUS_UNSUCCESSFUL;
80 break;
81 }
82
83 state.DeviceState = PowerDeviceD3;
84 PoSetPowerState(DeviceData->Common.Self, DevicePowerState, state);
85 DeviceData->Common.DevicePowerState = PowerDeviceD3;
86 SET_NEW_PNP_STATE(DeviceData->Common, Stopped);
87 status = STATUS_SUCCESS;
88 break;
89
90
91 case IRP_MN_QUERY_STOP_DEVICE:
92
93 //
94 // No reason here why we can't stop the device.
95 // If there were a reason we should speak now, because answering success
96 // here may result in a stop device irp.
97 //
98
99 SET_NEW_PNP_STATE(DeviceData->Common, StopPending);
100 status = STATUS_SUCCESS;
101 break;
102
103 case IRP_MN_CANCEL_STOP_DEVICE:
104
105 //
106 // The stop was canceled. Whatever state we set, or resources we put
107 // on hold in anticipation of the forthcoming STOP device IRP should be
108 // put back to normal. Someone, in the long list of concerned parties,
109 // has failed the stop device query.
110 //
111
112 //
113 // First check to see whether you have received cancel-stop
114 // without first receiving a query-stop. This could happen if someone
115 // above us fails a query-stop and passes down the subsequent
116 // cancel-stop.
117 //
118
119 if (StopPending == DeviceData->Common.DevicePnPState)
120 {
121 //
122 // We did receive a query-stop, so restore.
123 //
124 RESTORE_PREVIOUS_PNP_STATE(DeviceData->Common);
125 }
126 status = STATUS_SUCCESS;// We must not fail this IRP.
127 break;
128 case IRP_MN_QUERY_CAPABILITIES:
129
130 //
131 // Return the capabilities of a device, such as whether the device
132 // can be locked or ejected..etc
133 //
134
135 status = Bus_PDO_QueryDeviceCaps(DeviceData, Irp);
136
137 break;
138
139 case IRP_MN_QUERY_ID:
140
141 // Query the IDs of the device
142 status = Bus_PDO_QueryDeviceId(DeviceData, Irp);
143
144 break;
145
146 case IRP_MN_QUERY_DEVICE_RELATIONS:
147
148 DPRINT("\tQueryDeviceRelation Type: %s\n",DbgDeviceRelationString(\
149 IrpStack->Parameters.QueryDeviceRelations.Type));
150
151 status = Bus_PDO_QueryDeviceRelations(DeviceData, Irp);
152
153 break;
154
155 case IRP_MN_QUERY_DEVICE_TEXT:
156
157 status = Bus_PDO_QueryDeviceText(DeviceData, Irp);
158
159 break;
160
161 case IRP_MN_QUERY_RESOURCES:
162
163 status = Bus_PDO_QueryResources(DeviceData, Irp);
164
165 break;
166
167 case IRP_MN_QUERY_RESOURCE_REQUIREMENTS:
168
169 status = Bus_PDO_QueryResourceRequirements(DeviceData, Irp);
170
171 break;
172
173 case IRP_MN_QUERY_BUS_INFORMATION:
174
175 status = Bus_PDO_QueryBusInformation(DeviceData, Irp);
176
177 break;
178
179
180 case IRP_MN_FILTER_RESOURCE_REQUIREMENTS:
181
182 //
183 // OPTIONAL for bus drivers.
184 // The PnP Manager sends this IRP to a device
185 // stack so filter and function drivers can adjust the
186 // resources required by the device, if appropriate.
187 //
188
189 //break;
190
191 //case IRP_MN_QUERY_PNP_DEVICE_STATE:
192
193 //
194 // OPTIONAL for bus drivers.
195 // The PnP Manager sends this IRP after the drivers for
196 // a device return success from the IRP_MN_START_DEVICE
197 // request. The PnP Manager also sends this IRP when a
198 // driver for the device calls IoInvalidateDeviceState.
199 //
200
201 // break;
202
203 //case IRP_MN_READ_CONFIG:
204 //case IRP_MN_WRITE_CONFIG:
205
206 //
207 // Bus drivers for buses with configuration space must handle
208 // this request for their child devices. Our devices don't
209 // have a config space.
210 //
211
212 // break;
213
214 //case IRP_MN_SET_LOCK:
215
216 // break;
217
218 default:
219
220 //
221 // For PnP requests to the PDO that we do not understand we should
222 // return the IRP WITHOUT setting the status or information fields.
223 // These fields may have already been set by a filter (eg acpi).
224 status = Irp->IoStatus.Status;
225
226 break;
227 }
228
229 Irp->IoStatus.Status = status;
230 IoCompleteRequest (Irp, IO_NO_INCREMENT);
231
232 return status;
233 }
234
235 NTSTATUS
236 Bus_PDO_QueryDeviceCaps(
237 PPDO_DEVICE_DATA DeviceData,
238 PIRP Irp )
239 {
240
241 PIO_STACK_LOCATION stack;
242 PDEVICE_CAPABILITIES deviceCapabilities;
243 struct acpi_device *device = NULL;
244 ULONG i;
245
246 PAGED_CODE ();
247
248 if (DeviceData->AcpiHandle)
249 acpi_bus_get_device(DeviceData->AcpiHandle, &device);
250
251 stack = IoGetCurrentIrpStackLocation (Irp);
252
253 //
254 // Get the packet.
255 //
256 deviceCapabilities=stack->Parameters.DeviceCapabilities.Capabilities;
257
258 //
259 // Set the capabilities.
260 //
261
262 if (deviceCapabilities->Version != 1 ||
263 deviceCapabilities->Size < sizeof(DEVICE_CAPABILITIES))
264 {
265 return STATUS_UNSUCCESSFUL;
266 }
267
268 deviceCapabilities->D1Latency = 0;
269 deviceCapabilities->D2Latency = 0;
270 deviceCapabilities->D3Latency = 0;
271
272 deviceCapabilities->DeviceState[PowerSystemWorking] = PowerDeviceD0;
273 deviceCapabilities->DeviceState[PowerSystemSleeping1] = PowerDeviceD3;
274 deviceCapabilities->DeviceState[PowerSystemSleeping2] = PowerDeviceD3;
275 deviceCapabilities->DeviceState[PowerSystemSleeping3] = PowerDeviceD3;
276
277 for (i = 0; i < ACPI_D_STATE_COUNT && device; i++)
278 {
279 if (!device->power.states[i].flags.valid)
280 continue;
281
282 switch (i)
283 {
284 case ACPI_STATE_D0:
285 deviceCapabilities->DeviceState[PowerSystemWorking] = PowerDeviceD0;
286 break;
287
288 case ACPI_STATE_D1:
289 deviceCapabilities->DeviceState[PowerSystemSleeping1] = PowerDeviceD1;
290 deviceCapabilities->D1Latency = device->power.states[i].latency;
291 break;
292
293 case ACPI_STATE_D2:
294 deviceCapabilities->DeviceState[PowerSystemSleeping2] = PowerDeviceD2;
295 deviceCapabilities->D2Latency = device->power.states[i].latency;
296 break;
297
298 case ACPI_STATE_D3:
299 deviceCapabilities->DeviceState[PowerSystemSleeping3] = PowerDeviceD3;
300 deviceCapabilities->D3Latency = device->power.states[i].latency;
301 break;
302 }
303 }
304
305 // We can wake the system from D1
306 deviceCapabilities->DeviceWake = PowerDeviceD1;
307
308
309 deviceCapabilities->DeviceD1 =
310 (deviceCapabilities->DeviceState[PowerSystemSleeping1] == PowerDeviceD1) ? TRUE : FALSE;
311 deviceCapabilities->DeviceD2 =
312 (deviceCapabilities->DeviceState[PowerSystemSleeping2] == PowerDeviceD2) ? TRUE : FALSE;
313
314 deviceCapabilities->WakeFromD0 = FALSE;
315 deviceCapabilities->WakeFromD1 = TRUE; //Yes we can
316 deviceCapabilities->WakeFromD2 = FALSE;
317 deviceCapabilities->WakeFromD3 = FALSE;
318
319 if (device)
320 {
321 deviceCapabilities->EjectSupported = device->flags.ejectable;
322 deviceCapabilities->HardwareDisabled = !device->status.enabled;
323 deviceCapabilities->Removable = device->flags.removable;
324 deviceCapabilities->SurpriseRemovalOK = device->flags.suprise_removal_ok;
325 deviceCapabilities->UniqueID = device->flags.unique_id;
326 deviceCapabilities->NoDisplayInUI = !device->status.show_in_ui;
327 deviceCapabilities->Address = device->pnp.bus_address;
328 }
329 else
330 {
331 deviceCapabilities->EjectSupported = FALSE;
332 deviceCapabilities->HardwareDisabled = FALSE;
333 deviceCapabilities->Removable = FALSE;
334 deviceCapabilities->SurpriseRemovalOK = FALSE;
335 deviceCapabilities->UniqueID = FALSE;
336 deviceCapabilities->NoDisplayInUI = FALSE;
337 deviceCapabilities->Address = 0;
338 }
339
340 deviceCapabilities->SilentInstall = FALSE;
341 deviceCapabilities->UINumber = (ULONG)-1;
342
343 return STATUS_SUCCESS;
344
345 }
346
347 NTSTATUS
348 Bus_PDO_QueryDeviceId(
349 PPDO_DEVICE_DATA DeviceData,
350 PIRP Irp )
351 {
352 PIO_STACK_LOCATION stack;
353 PWCHAR buffer;
354 WCHAR temp[256];
355 ULONG length;
356 NTSTATUS status = STATUS_SUCCESS;
357 struct acpi_device *Device;
358
359 PAGED_CODE ();
360
361 stack = IoGetCurrentIrpStackLocation (Irp);
362
363 switch (stack->Parameters.QueryId.IdType) {
364
365 case BusQueryDeviceID:
366 if (DeviceData->AcpiHandle)
367 {
368 acpi_bus_get_device(DeviceData->AcpiHandle, &Device);
369
370 length = swprintf(temp,
371 L"ACPI\\%hs",
372 Device->pnp.hardware_id);
373 }
374 else
375 {
376 /* We know it's a fixed feature button because
377 * these are direct children of the ACPI root device
378 * and therefore have no handle
379 */
380 length = swprintf(temp,
381 L"ACPI\\FixedButton");
382 }
383
384 temp[++length] = UNICODE_NULL;
385
386 buffer = ExAllocatePoolWithTag (PagedPool, length * sizeof(WCHAR), 'IPCA');
387
388 if (!buffer) {
389 status = STATUS_INSUFFICIENT_RESOURCES;
390 break;
391 }
392
393 RtlCopyMemory (buffer, temp, length * sizeof(WCHAR));
394 Irp->IoStatus.Information = (ULONG_PTR) buffer;
395 DPRINT("BusQueryDeviceID: %ls\n",buffer);
396 break;
397
398 case BusQueryInstanceID:
399 /* See comment in BusQueryDeviceID case */
400 if(DeviceData->AcpiHandle)
401 {
402 acpi_bus_get_device(DeviceData->AcpiHandle, &Device);
403
404 if (Device->flags.unique_id)
405 length = swprintf(temp,
406 L"%hs",
407 Device->pnp.unique_id);
408 else
409 /* FIXME: Generate unique id! */
410 length = swprintf(temp, L"%ls", L"0000");
411 }
412 else
413 /* FIXME: Generate unique id! */
414 length = swprintf(temp, L"%ls", L"0000");
415
416 temp[++length] = UNICODE_NULL;
417
418 buffer = ExAllocatePoolWithTag (PagedPool, length * sizeof (WCHAR), 'IPCA');
419 if (!buffer) {
420 status = STATUS_INSUFFICIENT_RESOURCES;
421 break;
422 }
423
424 RtlCopyMemory (buffer, temp, length * sizeof (WCHAR));
425 DPRINT("BusQueryInstanceID: %ls\n",buffer);
426 Irp->IoStatus.Information = (ULONG_PTR) buffer;
427 break;
428
429 case BusQueryHardwareIDs:
430 length = 0;
431
432 /* See comment in BusQueryDeviceID case */
433 if (DeviceData->AcpiHandle)
434 {
435 acpi_bus_get_device(DeviceData->AcpiHandle, &Device);
436
437 length += swprintf(&temp[length],
438 L"ACPI\\%hs",
439 Device->pnp.hardware_id);
440 length++;
441
442 length += swprintf(&temp[length],
443 L"*%hs",
444 Device->pnp.hardware_id);
445 length++;
446 }
447 else
448 {
449 length += swprintf(&temp[length],
450 L"ACPI\\FixedButton");
451 length++;
452
453 length += swprintf(&temp[length],
454 L"*FixedButton");
455 length++;
456 }
457
458 temp[length] = UNICODE_NULL;
459
460 length++;
461
462 temp[length] = UNICODE_NULL;
463
464 buffer = ExAllocatePoolWithTag (PagedPool, length * sizeof(WCHAR), 'IPCA');
465
466 if (!buffer) {
467 status = STATUS_INSUFFICIENT_RESOURCES;
468 break;
469 }
470
471 RtlCopyMemory (buffer, temp, length * sizeof(WCHAR));
472 Irp->IoStatus.Information = (ULONG_PTR) buffer;
473 DPRINT("BusQueryHardwareIDs: %ls\n",buffer);
474 break;
475
476 default:
477 status = Irp->IoStatus.Status;
478 }
479 return status;
480 }
481
482 NTSTATUS
483 Bus_PDO_QueryDeviceText(
484 PPDO_DEVICE_DATA DeviceData,
485 PIRP Irp )
486 {
487 PWCHAR Buffer;
488 PIO_STACK_LOCATION stack;
489 NTSTATUS status;
490 PAGED_CODE ();
491
492 stack = IoGetCurrentIrpStackLocation (Irp);
493
494 switch (stack->Parameters.QueryDeviceText.DeviceTextType) {
495
496 case DeviceTextDescription:
497
498 if (!Irp->IoStatus.Information) {
499 if (wcsstr (DeviceData->HardwareIDs, L"PNP000") != 0)
500 Buffer = L"Programmable interrupt controller";
501 else if (wcsstr(DeviceData->HardwareIDs, L"PNP010") != 0)
502 Buffer = L"System timer";
503 else if (wcsstr(DeviceData->HardwareIDs, L"PNP020") != 0)
504 Buffer = L"DMA controller";
505 else if (wcsstr(DeviceData->HardwareIDs, L"PNP03") != 0)
506 Buffer = L"Keyboard";
507 else if (wcsstr(DeviceData->HardwareIDs, L"PNP040") != 0)
508 Buffer = L"Parallel port";
509 else if (wcsstr(DeviceData->HardwareIDs, L"PNP05") != 0)
510 Buffer = L"Serial port";
511 else if (wcsstr(DeviceData->HardwareIDs, L"PNP06") != 0)
512 Buffer = L"Disk controller";
513 else if (wcsstr(DeviceData->HardwareIDs, L"PNP07") != 0)
514 Buffer = L"Disk controller";
515 else if (wcsstr(DeviceData->HardwareIDs, L"PNP09") != 0)
516 Buffer = L"Display adapter";
517 else if (wcsstr(DeviceData->HardwareIDs, L"PNP0A0") != 0)
518 Buffer = L"Bus controller";
519 else if (wcsstr(DeviceData->HardwareIDs, L"PNP0E0") != 0)
520 Buffer = L"PCMCIA controller";
521 else if (wcsstr(DeviceData->HardwareIDs, L"PNP0F") != 0)
522 Buffer = L"Mouse device";
523 else if (wcsstr(DeviceData->HardwareIDs, L"PNP8") != 0)
524 Buffer = L"Network adapter";
525 else if (wcsstr(DeviceData->HardwareIDs, L"PNPA0") != 0)
526 Buffer = L"SCSI controller";
527 else if (wcsstr(DeviceData->HardwareIDs, L"PNPB0") != 0)
528 Buffer = L"Multimedia device";
529 else if (wcsstr(DeviceData->HardwareIDs, L"PNPC00") != 0)
530 Buffer = L"Modem";
531 else if (wcsstr(DeviceData->HardwareIDs, L"PNP0C0C") != 0)
532 Buffer = L"Power Button";
533 else if (wcsstr(DeviceData->HardwareIDs, L"PNP0C0E") != 0)
534 Buffer = L"Sleep Button";
535 else if (wcsstr(DeviceData->HardwareIDs, L"PNP0C0D") != 0)
536 Buffer = L"Lid Switch";
537 else if (wcsstr(DeviceData->HardwareIDs, L"PNP0C09") != 0)
538 Buffer = L"ACPI Embedded Controller";
539 else if (wcsstr(DeviceData->HardwareIDs, L"PNP0C0B") != 0)
540 Buffer = L"ACPI Fan";
541 else if (wcsstr(DeviceData->HardwareIDs, L"PNP0A03") != 0)
542 Buffer = L"PCI Root Bridge";
543 else if (wcsstr(DeviceData->HardwareIDs, L"PNP0C0A") != 0)
544 Buffer = L"ACPI Battery";
545 else if (wcsstr(DeviceData->HardwareIDs, L"PNP0C0F") != 0)
546 Buffer = L"PCI Interrupt Link";
547 else if (wcsstr(DeviceData->HardwareIDs, L"ACPI_PWR") != 0)
548 Buffer = L"ACPI Power Resource";
549 else if (wcsstr(DeviceData->HardwareIDs, L"Processor") != 0)
550 Buffer = L"Processor";
551 else if (wcsstr(DeviceData->HardwareIDs, L"ACPI_SYS") != 0)
552 Buffer = L"ACPI System";
553 else if (wcsstr(DeviceData->HardwareIDs, L"ThermalZone") != 0)
554 Buffer = L"ACPI Thermal Zone";
555 else if (wcsstr(DeviceData->HardwareIDs, L"ACPI0002") != 0)
556 Buffer = L"Smart Battery";
557 else if (wcsstr(DeviceData->HardwareIDs, L"ACPI0003") != 0)
558 Buffer = L"AC Adapter";
559 /* Simply checking if AcpiHandle is NULL eliminates the need to check
560 * for the 4 different names that ACPI knows the fixed feature button as internally
561 */
562 else if (!DeviceData->AcpiHandle)
563 Buffer = L"ACPI Fixed Feature Button";
564 else
565 Buffer = L"Other ACPI device";
566
567 DPRINT("\tDeviceTextDescription :%ws\n", Buffer);
568
569 Irp->IoStatus.Information = (ULONG_PTR) Buffer;
570 }
571 status = STATUS_SUCCESS;
572 break;
573
574 default:
575 status = Irp->IoStatus.Status;
576 break;
577 }
578
579 return status;
580
581 }
582
583 NTSTATUS
584 Bus_PDO_QueryResources(
585 PPDO_DEVICE_DATA DeviceData,
586 PIRP Irp )
587 {
588 ULONG NumberOfResources = 0;
589 PCM_RESOURCE_LIST ResourceList;
590 PCM_PARTIAL_RESOURCE_DESCRIPTOR ResourceDescriptor;
591 ACPI_STATUS AcpiStatus;
592 ACPI_BUFFER Buffer;
593 ACPI_RESOURCE* resource;
594 ULONG ResourceListSize;
595 ULONG i;
596
597 /* Get current resources */
598 Buffer.Length = 0;
599 AcpiStatus = AcpiGetCurrentResources(DeviceData->AcpiHandle, &Buffer);
600 if ((!ACPI_SUCCESS(AcpiStatus) && AcpiStatus != AE_BUFFER_OVERFLOW) ||
601 Buffer.Length == 0)
602 {
603 return Irp->IoStatus.Status;
604 }
605
606 Buffer.Pointer = ExAllocatePool(PagedPool, Buffer.Length);
607 if (!Buffer.Pointer)
608 return STATUS_INSUFFICIENT_RESOURCES;
609
610 AcpiStatus = AcpiGetCurrentResources(DeviceData->AcpiHandle, &Buffer);
611 if (!ACPI_SUCCESS(AcpiStatus))
612 {
613 DPRINT1("AcpiGetCurrentResources #2 failed (0x%x)\n", AcpiStatus);
614 ASSERT(FALSE);
615 return STATUS_UNSUCCESSFUL;
616 }
617
618 resource= Buffer.Pointer;
619 /* Count number of resources */
620 while (resource->Type != ACPI_RESOURCE_TYPE_END_TAG)
621 {
622 switch (resource->Type)
623 {
624 case ACPI_RESOURCE_TYPE_IRQ:
625 {
626 ACPI_RESOURCE_IRQ *irq_data = (ACPI_RESOURCE_IRQ*) &resource->Data;
627 NumberOfResources += irq_data->InterruptCount;
628 break;
629 }
630 case ACPI_RESOURCE_TYPE_DMA:
631 {
632 ACPI_RESOURCE_DMA *dma_data = (ACPI_RESOURCE_DMA*) &resource->Data;
633 NumberOfResources += dma_data->ChannelCount;
634 break;
635 }
636 case ACPI_RESOURCE_TYPE_IO:
637 {
638 NumberOfResources++;
639 break;
640 }
641 default:
642 {
643 DPRINT1("Unknown resource type: %d\n", resource->Type);
644 break;
645 }
646 }
647 resource = ACPI_NEXT_RESOURCE(resource);
648 }
649
650 /* Allocate memory */
651 ResourceListSize = sizeof(CM_RESOURCE_LIST) + sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR) * (NumberOfResources - 1);
652 ResourceList = (PCM_RESOURCE_LIST)ExAllocatePool(PagedPool, ResourceListSize);
653
654 if (!ResourceList)
655 {
656 ExFreePool(Buffer.Pointer);
657 return STATUS_INSUFFICIENT_RESOURCES;
658 }
659 ResourceList->Count = 1;
660 ResourceList->List[0].InterfaceType = Internal; /* FIXME */
661 ResourceList->List[0].BusNumber = 0; /* We're the only ACPI bus device in the system */
662 ResourceList->List[0].PartialResourceList.Version = 1;
663 ResourceList->List[0].PartialResourceList.Revision = 1;
664 ResourceList->List[0].PartialResourceList.Count = NumberOfResources;
665 ResourceDescriptor = ResourceList->List[0].PartialResourceList.PartialDescriptors;
666
667 /* Fill resources list structure */
668 while (resource->Type != ACPI_RESOURCE_TYPE_END_TAG)
669 {
670 switch (resource->Type)
671 {
672 case ACPI_RESOURCE_TYPE_IRQ:
673 {
674 ACPI_RESOURCE_IRQ *irq_data = (ACPI_RESOURCE_IRQ*) &resource->Data;
675 for (i = 0; i < irq_data->InterruptCount; i++)
676 {
677 ResourceDescriptor->Type = CmResourceTypeInterrupt;
678
679 ResourceDescriptor->ShareDisposition =
680 (irq_data->Sharable == ACPI_SHARED ? CmResourceShareShared : CmResourceShareDeviceExclusive);
681 ResourceDescriptor->Flags =
682 (irq_data->Triggering == ACPI_LEVEL_SENSITIVE ? CM_RESOURCE_INTERRUPT_LEVEL_SENSITIVE : CM_RESOURCE_INTERRUPT_LATCHED);
683 ResourceDescriptor->u.Interrupt.Level = irq_data->Interrupts[i];
684 ResourceDescriptor->u.Interrupt.Vector = 0;
685 ResourceDescriptor->u.Interrupt.Affinity = (KAFFINITY)(-1);
686
687 ResourceDescriptor++;
688 }
689 break;
690 }
691 case ACPI_RESOURCE_TYPE_DMA:
692 {
693 ACPI_RESOURCE_DMA *dma_data = (ACPI_RESOURCE_DMA*) &resource->Data;
694 for (i = 0; i < dma_data->ChannelCount; i++)
695 {
696 ResourceDescriptor->Type = CmResourceTypeDma;
697 ResourceDescriptor->Flags = 0;
698 switch (dma_data->Type)
699 {
700 case ACPI_TYPE_A: ResourceDescriptor->Flags |= CM_RESOURCE_DMA_TYPE_A; break;
701 case ACPI_TYPE_B: ResourceDescriptor->Flags |= CM_RESOURCE_DMA_TYPE_B; break;
702 case ACPI_TYPE_F: ResourceDescriptor->Flags |= CM_RESOURCE_DMA_TYPE_F; break;
703 }
704 if (dma_data->BusMaster == ACPI_BUS_MASTER)
705 ResourceDescriptor->Flags |= CM_RESOURCE_DMA_BUS_MASTER;
706 switch (dma_data->Transfer)
707 {
708 case ACPI_TRANSFER_8: ResourceDescriptor->Flags |= CM_RESOURCE_DMA_8; break;
709 case ACPI_TRANSFER_16: ResourceDescriptor->Flags |= CM_RESOURCE_DMA_16; break;
710 case ACPI_TRANSFER_8_16: ResourceDescriptor->Flags |= CM_RESOURCE_DMA_8_AND_16; break;
711 }
712 ResourceDescriptor->u.Dma.Channel = dma_data->Channels[i];
713
714 ResourceDescriptor++;
715 }
716 break;
717 }
718 case ACPI_RESOURCE_TYPE_IO:
719 {
720 ACPI_RESOURCE_IO *io_data = (ACPI_RESOURCE_IO*) &resource->Data;
721 ResourceDescriptor->Type = CmResourceTypePort;
722 ResourceDescriptor->ShareDisposition = CmResourceShareDriverExclusive;
723 ResourceDescriptor->Flags = CM_RESOURCE_PORT_IO;
724 if (io_data->IoDecode == ACPI_DECODE_16)
725 ResourceDescriptor->Flags |= CM_RESOURCE_PORT_16_BIT_DECODE;
726 else
727 ResourceDescriptor->Flags |= CM_RESOURCE_PORT_10_BIT_DECODE;
728 ResourceDescriptor->u.Port.Start.u.HighPart = 0;
729 ResourceDescriptor->u.Port.Start.u.LowPart = io_data->Minimum;
730 ResourceDescriptor->u.Port.Length = io_data->AddressLength;
731
732 ResourceDescriptor++;
733 break;
734 }
735 default:
736 {
737 break;
738 }
739 }
740 resource = ACPI_NEXT_RESOURCE(resource);
741 }
742
743 ExFreePool(Buffer.Pointer);
744 Irp->IoStatus.Information = (ULONG_PTR)ResourceList;
745 return STATUS_SUCCESS;
746 }
747
748 NTSTATUS
749 Bus_PDO_QueryResourceRequirements(
750 PPDO_DEVICE_DATA DeviceData,
751 PIRP Irp )
752 {
753 ULONG NumberOfResources = 0;
754 ACPI_STATUS AcpiStatus;
755 ACPI_BUFFER Buffer;
756 ACPI_RESOURCE* resource;
757 ULONG i, RequirementsListSize;
758 PIO_RESOURCE_REQUIREMENTS_LIST RequirementsList;
759 PIO_RESOURCE_DESCRIPTOR RequirementDescriptor;
760 BOOLEAN CurrentRes = FALSE;
761
762 PAGED_CODE ();
763
764
765 /* Get current resources */
766 while (TRUE)
767 {
768 Buffer.Length = 0;
769 if (CurrentRes)
770 AcpiStatus = AcpiGetCurrentResources(DeviceData->AcpiHandle, &Buffer);
771 else
772 AcpiStatus = AcpiGetPossibleResources(DeviceData->AcpiHandle, &Buffer);
773 if ((!ACPI_SUCCESS(AcpiStatus) && AcpiStatus != AE_BUFFER_OVERFLOW) ||
774 Buffer.Length == 0)
775 {
776 if (!CurrentRes)
777 CurrentRes = TRUE;
778 else
779 return Irp->IoStatus.Status;
780 }
781 else
782 break;
783 }
784
785 Buffer.Pointer = ExAllocatePool(PagedPool, Buffer.Length);
786 if (!Buffer.Pointer)
787 return STATUS_INSUFFICIENT_RESOURCES;
788
789 if (CurrentRes)
790 AcpiStatus = AcpiGetCurrentResources(DeviceData->AcpiHandle, &Buffer);
791 else
792 AcpiStatus = AcpiGetPossibleResources(DeviceData->AcpiHandle, &Buffer);
793 if (!ACPI_SUCCESS(AcpiStatus))
794 {
795 DPRINT1("AcpiGetCurrentResources #2 failed (0x%x)\n", AcpiStatus);
796 ASSERT(FALSE);
797 return STATUS_UNSUCCESSFUL;
798 }
799
800 resource= Buffer.Pointer;
801 /* Count number of resources */
802 while (resource->Type != ACPI_RESOURCE_TYPE_END_TAG)
803 {
804 switch (resource->Type)
805 {
806 case ACPI_RESOURCE_TYPE_IRQ:
807 {
808 ACPI_RESOURCE_IRQ *irq_data = (ACPI_RESOURCE_IRQ*) &resource->Data;
809 NumberOfResources += irq_data->InterruptCount;
810 break;
811 }
812 case ACPI_RESOURCE_TYPE_DMA:
813 {
814 ACPI_RESOURCE_DMA *dma_data = (ACPI_RESOURCE_DMA*) &resource->Data;
815 NumberOfResources += dma_data->ChannelCount;
816 break;
817 }
818 case ACPI_RESOURCE_TYPE_IO:
819 {
820 NumberOfResources++;
821 break;
822 }
823 default:
824 {
825 break;
826 }
827 }
828 resource = ACPI_NEXT_RESOURCE(resource);
829 }
830
831 RequirementsListSize = sizeof(IO_RESOURCE_REQUIREMENTS_LIST) + sizeof(IO_RESOURCE_DESCRIPTOR) * (NumberOfResources - 1);
832 RequirementsList = (PIO_RESOURCE_REQUIREMENTS_LIST)ExAllocatePool(PagedPool, RequirementsListSize);
833
834 if (!RequirementsList)
835 {
836 ExFreePool(Buffer.Pointer);
837 return STATUS_INSUFFICIENT_RESOURCES;
838 }
839 RequirementsList->ListSize = RequirementsListSize;
840 RequirementsList->InterfaceType = Internal;
841 RequirementsList->BusNumber = 0;
842 RequirementsList->SlotNumber = 0; /* Not used by WDM drivers */
843 RequirementsList->AlternativeLists = 1;
844 RequirementsList->List[0].Version = 1;
845 RequirementsList->List[0].Revision = 1;
846 RequirementsList->List[0].Count = NumberOfResources;
847 RequirementDescriptor = RequirementsList->List[0].Descriptors;
848
849 /* Fill resources list structure */
850 while (resource->Type != ACPI_RESOURCE_TYPE_END_TAG)
851 {
852 switch (resource->Type)
853 {
854 case ACPI_RESOURCE_TYPE_IRQ:
855 {
856 ACPI_RESOURCE_IRQ *irq_data = (ACPI_RESOURCE_IRQ*) &resource->Data;
857 for (i = 0; i < irq_data->InterruptCount; i++)
858 {
859 RequirementDescriptor->Option = CurrentRes ? 0 : IO_RESOURCE_PREFERRED;
860 RequirementDescriptor->Type = CmResourceTypeInterrupt;
861 RequirementDescriptor->ShareDisposition = (irq_data->Sharable == ACPI_SHARED ? CmResourceShareShared : CmResourceShareDeviceExclusive);
862 RequirementDescriptor->Flags =(irq_data->Triggering == ACPI_LEVEL_SENSITIVE ? CM_RESOURCE_INTERRUPT_LEVEL_SENSITIVE : CM_RESOURCE_INTERRUPT_LATCHED);
863 RequirementDescriptor->u.Interrupt.MinimumVector =
864 RequirementDescriptor->u.Interrupt.MaximumVector = irq_data->Interrupts[i];
865
866 RequirementDescriptor++;
867 }
868 break;
869 }
870 case ACPI_RESOURCE_TYPE_DMA:
871 {
872 ACPI_RESOURCE_DMA *dma_data = (ACPI_RESOURCE_DMA*) &resource->Data;
873 for (i = 0; i < dma_data->ChannelCount; i++)
874 {
875 RequirementDescriptor->Type = CmResourceTypeDma;
876 RequirementDescriptor->Flags = 0;
877 switch (dma_data->Type)
878 {
879 case ACPI_TYPE_A: RequirementDescriptor->Flags |= CM_RESOURCE_DMA_TYPE_A; break;
880 case ACPI_TYPE_B: RequirementDescriptor->Flags |= CM_RESOURCE_DMA_TYPE_B; break;
881 case ACPI_TYPE_F: RequirementDescriptor->Flags |= CM_RESOURCE_DMA_TYPE_F; break;
882 }
883 if (dma_data->BusMaster == ACPI_BUS_MASTER)
884 RequirementDescriptor->Flags |= CM_RESOURCE_DMA_BUS_MASTER;
885 switch (dma_data->Transfer)
886 {
887 case ACPI_TRANSFER_8: RequirementDescriptor->Flags |= CM_RESOURCE_DMA_8; break;
888 case ACPI_TRANSFER_16: RequirementDescriptor->Flags |= CM_RESOURCE_DMA_16; break;
889 case ACPI_TRANSFER_8_16: RequirementDescriptor->Flags |= CM_RESOURCE_DMA_8_AND_16; break;
890 }
891
892 RequirementDescriptor->Option = CurrentRes ? 0 : IO_RESOURCE_PREFERRED;
893 RequirementDescriptor->ShareDisposition = CmResourceShareDriverExclusive;
894 RequirementDescriptor->u.Dma.MinimumChannel =
895 RequirementDescriptor->u.Dma.MaximumChannel = dma_data->Channels[i];
896 RequirementDescriptor++;
897 }
898 break;
899 }
900 case ACPI_RESOURCE_TYPE_IO:
901 {
902 ACPI_RESOURCE_IO *io_data = (ACPI_RESOURCE_IO*) &resource->Data;
903 RequirementDescriptor->Flags = CM_RESOURCE_PORT_IO;
904 if (io_data->IoDecode == ACPI_DECODE_16)
905 RequirementDescriptor->Flags |= CM_RESOURCE_PORT_16_BIT_DECODE;
906 else
907 RequirementDescriptor->Flags |= CM_RESOURCE_PORT_10_BIT_DECODE;
908
909 RequirementDescriptor->u.Port.Length = io_data->AddressLength;
910
911 RequirementDescriptor->Option = CurrentRes ? 0 : IO_RESOURCE_PREFERRED;
912 RequirementDescriptor->Type = CmResourceTypePort;
913 RequirementDescriptor->ShareDisposition = CmResourceShareDriverExclusive;
914 RequirementDescriptor->u.Port.Alignment = io_data->Alignment;
915 RequirementDescriptor->u.Port.MinimumAddress.QuadPart = io_data->Minimum;
916 RequirementDescriptor->u.Port.MaximumAddress.QuadPart = io_data->Maximum;
917
918 RequirementDescriptor++;
919 break;
920 }
921 default:
922 {
923 break;
924 }
925 }
926 resource = ACPI_NEXT_RESOURCE(resource);
927 }
928 ExFreePool(Buffer.Pointer);
929
930 Irp->IoStatus.Information = (ULONG_PTR)RequirementsList;
931
932 return STATUS_SUCCESS;
933 }
934
935 NTSTATUS
936 Bus_PDO_QueryDeviceRelations(
937 PPDO_DEVICE_DATA DeviceData,
938 PIRP Irp )
939 /*++
940
941 Routine Description:
942
943 The PnP Manager sends this IRP to gather information about
944 devices with a relationship to the specified device.
945 Bus drivers must handle this request for TargetDeviceRelation
946 for their child devices (child PDOs).
947
948 If a driver returns relations in response to this IRP,
949 it allocates a DEVICE_RELATIONS structure from paged
950 memory containing a count and the appropriate number of
951 device object pointers. The PnP Manager frees the structure
952 when it is no longer needed. If a driver replaces a
953 DEVICE_RELATIONS structure allocated by another driver,
954 it must free the previous structure.
955
956 A driver must reference the PDO of any device that it
957 reports in this IRP (ObReferenceObject). The PnP Manager
958 removes the reference when appropriate.
959
960 Arguments:
961
962 DeviceData - Pointer to the PDO's device extension.
963 Irp - Pointer to the irp.
964
965 Return Value:
966
967 NT STATUS
968
969 --*/
970 {
971
972 PIO_STACK_LOCATION stack;
973 PDEVICE_RELATIONS deviceRelations;
974 NTSTATUS status;
975
976 PAGED_CODE ();
977
978 stack = IoGetCurrentIrpStackLocation (Irp);
979
980 switch (stack->Parameters.QueryDeviceRelations.Type) {
981
982 case TargetDeviceRelation:
983
984 deviceRelations = (PDEVICE_RELATIONS) Irp->IoStatus.Information;
985 if (deviceRelations) {
986 //
987 // Only PDO can handle this request. Somebody above
988 // is not playing by rule.
989 //
990 ASSERTMSG("Someone above is handling TargetDeviceRelation", !deviceRelations);
991 }
992
993 deviceRelations = (PDEVICE_RELATIONS)
994 ExAllocatePoolWithTag (PagedPool,
995 sizeof(DEVICE_RELATIONS),
996 'IPCA');
997 if (!deviceRelations) {
998 status = STATUS_INSUFFICIENT_RESOURCES;
999 break;
1000 }
1001
1002 //
1003 // There is only one PDO pointer in the structure
1004 // for this relation type. The PnP Manager removes
1005 // the reference to the PDO when the driver or application
1006 // un-registers for notification on the device.
1007 //
1008
1009 deviceRelations->Count = 1;
1010 deviceRelations->Objects[0] = DeviceData->Common.Self;
1011 ObReferenceObject(DeviceData->Common.Self);
1012
1013 status = STATUS_SUCCESS;
1014 Irp->IoStatus.Information = (ULONG_PTR) deviceRelations;
1015 break;
1016
1017 case BusRelations: // Not handled by PDO
1018 case EjectionRelations: // optional for PDO
1019 case RemovalRelations: // // optional for PDO
1020 default:
1021 status = Irp->IoStatus.Status;
1022 }
1023
1024 return status;
1025 }
1026
1027 NTSTATUS
1028 Bus_PDO_QueryBusInformation(
1029 PPDO_DEVICE_DATA DeviceData,
1030 PIRP Irp )
1031 /*++
1032
1033 Routine Description:
1034
1035 The PnP Manager uses this IRP to request the type and
1036 instance number of a device's parent bus. Bus drivers
1037 should handle this request for their child devices (PDOs).
1038
1039 Arguments:
1040
1041 DeviceData - Pointer to the PDO's device extension.
1042 Irp - Pointer to the irp.
1043
1044 Return Value:
1045
1046 NT STATUS
1047
1048 --*/
1049 {
1050
1051 PPNP_BUS_INFORMATION busInfo;
1052
1053 PAGED_CODE ();
1054
1055 busInfo = ExAllocatePoolWithTag (PagedPool, sizeof(PNP_BUS_INFORMATION),
1056 'IPCA');
1057
1058 if (busInfo == NULL) {
1059 return STATUS_INSUFFICIENT_RESOURCES;
1060 }
1061
1062 busInfo->BusTypeGuid = GUID_ACPI_INTERFACE_STANDARD;
1063
1064 busInfo->LegacyBusType = InternalPowerBus;
1065
1066 busInfo->BusNumber = 0; //fixme
1067
1068 Irp->IoStatus.Information = (ULONG_PTR)busInfo;
1069
1070 return STATUS_SUCCESS;
1071 }
1072
1073
1074 NTSTATUS
1075 Bus_GetDeviceCapabilities(
1076 PDEVICE_OBJECT DeviceObject,
1077 PDEVICE_CAPABILITIES DeviceCapabilities
1078 )
1079 {
1080 IO_STATUS_BLOCK ioStatus;
1081 KEVENT pnpEvent;
1082 NTSTATUS status;
1083 PDEVICE_OBJECT targetObject;
1084 PIO_STACK_LOCATION irpStack;
1085 PIRP pnpIrp;
1086
1087 PAGED_CODE();
1088
1089 //
1090 // Initialize the capabilities that we will send down
1091 //
1092 RtlZeroMemory( DeviceCapabilities, sizeof(DEVICE_CAPABILITIES) );
1093 DeviceCapabilities->Size = sizeof(DEVICE_CAPABILITIES);
1094 DeviceCapabilities->Version = 1;
1095 DeviceCapabilities->Address = -1;
1096 DeviceCapabilities->UINumber = -1;
1097
1098 //
1099 // Initialize the event
1100 //
1101 KeInitializeEvent( &pnpEvent, NotificationEvent, FALSE );
1102
1103 targetObject = IoGetAttachedDeviceReference( DeviceObject );
1104
1105 //
1106 // Build an Irp
1107 //
1108 pnpIrp = IoBuildSynchronousFsdRequest(
1109 IRP_MJ_PNP,
1110 targetObject,
1111 NULL,
1112 0,
1113 NULL,
1114 &pnpEvent,
1115 &ioStatus
1116 );
1117 if (pnpIrp == NULL) {
1118
1119 status = STATUS_INSUFFICIENT_RESOURCES;
1120 goto GetDeviceCapabilitiesExit;
1121
1122 }
1123
1124 //
1125 // Pnp Irps all begin life as STATUS_NOT_SUPPORTED;
1126 //
1127 pnpIrp->IoStatus.Status = STATUS_NOT_SUPPORTED;
1128
1129 //
1130 // Get the top of stack
1131 //
1132 irpStack = IoGetNextIrpStackLocation( pnpIrp );
1133
1134 //
1135 // Set the top of stack
1136 //
1137 RtlZeroMemory( irpStack, sizeof(IO_STACK_LOCATION ) );
1138 irpStack->MajorFunction = IRP_MJ_PNP;
1139 irpStack->MinorFunction = IRP_MN_QUERY_CAPABILITIES;
1140 irpStack->Parameters.DeviceCapabilities.Capabilities = DeviceCapabilities;
1141
1142 //
1143 // Call the driver
1144 //
1145 status = IoCallDriver( targetObject, pnpIrp );
1146 if (status == STATUS_PENDING) {
1147
1148 //
1149 // Block until the irp comes back.
1150 // Important thing to note here is when you allocate
1151 // the memory for an event in the stack you must do a
1152 // KernelMode wait instead of UserMode to prevent
1153 // the stack from getting paged out.
1154 //
1155
1156 KeWaitForSingleObject(
1157 &pnpEvent,
1158 Executive,
1159 KernelMode,
1160 FALSE,
1161 NULL
1162 );
1163 status = ioStatus.Status;
1164
1165 }
1166
1167 GetDeviceCapabilitiesExit:
1168 //
1169 // Done with reference
1170 //
1171 ObDereferenceObject( targetObject );
1172
1173 //
1174 // Done
1175 //
1176 return status;
1177
1178 }
1179
1180