- Handle ACPI_RESOURCE_TYPE_ADDRESS16, ACPI_RESOURCE_TYPE_ADDRESS32, ACPI_RESOURCE_T...
[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_ADDRESS16:
637 case ACPI_RESOURCE_TYPE_ADDRESS32:
638 case ACPI_RESOURCE_TYPE_ADDRESS64:
639 case ACPI_RESOURCE_TYPE_MEMORY24:
640 case ACPI_RESOURCE_TYPE_MEMORY32:
641 case ACPI_RESOURCE_TYPE_IO:
642 {
643 NumberOfResources++;
644 break;
645 }
646 default:
647 {
648 DPRINT1("Unknown resource type: %d\n", resource->Type);
649 break;
650 }
651 }
652 resource = ACPI_NEXT_RESOURCE(resource);
653 }
654
655 /* Allocate memory */
656 ResourceListSize = sizeof(CM_RESOURCE_LIST) + sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR) * (NumberOfResources - 1);
657 ResourceList = (PCM_RESOURCE_LIST)ExAllocatePool(PagedPool, ResourceListSize);
658
659 if (!ResourceList)
660 {
661 ExFreePool(Buffer.Pointer);
662 return STATUS_INSUFFICIENT_RESOURCES;
663 }
664 ResourceList->Count = 1;
665 ResourceList->List[0].InterfaceType = Internal; /* FIXME */
666 ResourceList->List[0].BusNumber = 0; /* We're the only ACPI bus device in the system */
667 ResourceList->List[0].PartialResourceList.Version = 1;
668 ResourceList->List[0].PartialResourceList.Revision = 1;
669 ResourceList->List[0].PartialResourceList.Count = NumberOfResources;
670 ResourceDescriptor = ResourceList->List[0].PartialResourceList.PartialDescriptors;
671
672 /* Fill resources list structure */
673 while (resource->Type != ACPI_RESOURCE_TYPE_END_TAG)
674 {
675 switch (resource->Type)
676 {
677 case ACPI_RESOURCE_TYPE_IRQ:
678 {
679 ACPI_RESOURCE_IRQ *irq_data = (ACPI_RESOURCE_IRQ*) &resource->Data;
680 for (i = 0; i < irq_data->InterruptCount; i++)
681 {
682 ResourceDescriptor->Type = CmResourceTypeInterrupt;
683
684 ResourceDescriptor->ShareDisposition =
685 (irq_data->Sharable == ACPI_SHARED ? CmResourceShareShared : CmResourceShareDeviceExclusive);
686 ResourceDescriptor->Flags =
687 (irq_data->Triggering == ACPI_LEVEL_SENSITIVE ? CM_RESOURCE_INTERRUPT_LEVEL_SENSITIVE : CM_RESOURCE_INTERRUPT_LATCHED);
688 ResourceDescriptor->u.Interrupt.Level = irq_data->Interrupts[i];
689 ResourceDescriptor->u.Interrupt.Vector = 0;
690 ResourceDescriptor->u.Interrupt.Affinity = (KAFFINITY)(-1);
691
692 ResourceDescriptor++;
693 }
694 break;
695 }
696 case ACPI_RESOURCE_TYPE_DMA:
697 {
698 ACPI_RESOURCE_DMA *dma_data = (ACPI_RESOURCE_DMA*) &resource->Data;
699 for (i = 0; i < dma_data->ChannelCount; i++)
700 {
701 ResourceDescriptor->Type = CmResourceTypeDma;
702 ResourceDescriptor->Flags = 0;
703 switch (dma_data->Type)
704 {
705 case ACPI_TYPE_A: ResourceDescriptor->Flags |= CM_RESOURCE_DMA_TYPE_A; break;
706 case ACPI_TYPE_B: ResourceDescriptor->Flags |= CM_RESOURCE_DMA_TYPE_B; break;
707 case ACPI_TYPE_F: ResourceDescriptor->Flags |= CM_RESOURCE_DMA_TYPE_F; break;
708 }
709 if (dma_data->BusMaster == ACPI_BUS_MASTER)
710 ResourceDescriptor->Flags |= CM_RESOURCE_DMA_BUS_MASTER;
711 switch (dma_data->Transfer)
712 {
713 case ACPI_TRANSFER_8: ResourceDescriptor->Flags |= CM_RESOURCE_DMA_8; break;
714 case ACPI_TRANSFER_16: ResourceDescriptor->Flags |= CM_RESOURCE_DMA_16; break;
715 case ACPI_TRANSFER_8_16: ResourceDescriptor->Flags |= CM_RESOURCE_DMA_8_AND_16; break;
716 }
717 ResourceDescriptor->u.Dma.Channel = dma_data->Channels[i];
718
719 ResourceDescriptor++;
720 }
721 break;
722 }
723 case ACPI_RESOURCE_TYPE_IO:
724 {
725 ACPI_RESOURCE_IO *io_data = (ACPI_RESOURCE_IO*) &resource->Data;
726 ResourceDescriptor->Type = CmResourceTypePort;
727 ResourceDescriptor->ShareDisposition = CmResourceShareDriverExclusive;
728 ResourceDescriptor->Flags = CM_RESOURCE_PORT_IO;
729 if (io_data->IoDecode == ACPI_DECODE_16)
730 ResourceDescriptor->Flags |= CM_RESOURCE_PORT_16_BIT_DECODE;
731 else
732 ResourceDescriptor->Flags |= CM_RESOURCE_PORT_10_BIT_DECODE;
733 ResourceDescriptor->u.Port.Start.QuadPart = io_data->Minimum;
734 ResourceDescriptor->u.Port.Length = io_data->AddressLength;
735
736 ResourceDescriptor++;
737 break;
738 }
739 case ACPI_RESOURCE_TYPE_ADDRESS16:
740 {
741 ACPI_RESOURCE_ADDRESS16 *addr16_data = (ACPI_RESOURCE_ADDRESS16*) &resource->Data;
742 if (addr16_data->ResourceType == ACPI_BUS_NUMBER_RANGE)
743 {
744 ResourceDescriptor->Type = CmResourceTypeBusNumber;
745 ResourceDescriptor->ShareDisposition = CmResourceShareShared;
746 ResourceDescriptor->Flags = 0;
747 ResourceDescriptor->u.BusNumber.Start = addr16_data->Minimum;
748 ResourceDescriptor->u.BusNumber.Length = addr16_data->AddressLength;
749 }
750 else if (addr16_data->ResourceType == ACPI_IO_RANGE)
751 {
752 ResourceDescriptor->Type = CmResourceTypePort;
753 ResourceDescriptor->ShareDisposition = CmResourceShareDeviceExclusive;
754 ResourceDescriptor->Flags = CM_RESOURCE_PORT_IO;
755 if (addr16_data->Decode == ACPI_POS_DECODE)
756 ResourceDescriptor->Flags |= CM_RESOURCE_PORT_POSITIVE_DECODE;
757 ResourceDescriptor->u.Port.Start.QuadPart = addr16_data->Minimum;
758 ResourceDescriptor->u.Port.Length = addr16_data->AddressLength;
759 }
760 else
761 {
762 ResourceDescriptor->Type = CmResourceTypeMemory;
763 ResourceDescriptor->ShareDisposition = CmResourceShareDeviceExclusive;
764 ResourceDescriptor->Flags = 0;
765 if (addr16_data->Info.Mem.WriteProtect == ACPI_READ_ONLY_MEMORY)
766 ResourceDescriptor->Flags |= CM_RESOURCE_MEMORY_READ_ONLY;
767 else
768 ResourceDescriptor->Flags |= CM_RESOURCE_MEMORY_READ_WRITE;
769 switch (addr16_data->Info.Mem.Caching)
770 {
771 case ACPI_CACHABLE_MEMORY: ResourceDescriptor->Flags |= CM_RESOURCE_MEMORY_CACHEABLE; break;
772 case ACPI_WRITE_COMBINING_MEMORY: ResourceDescriptor->Flags |= CM_RESOURCE_MEMORY_COMBINEDWRITE; break;
773 case ACPI_PREFETCHABLE_MEMORY: ResourceDescriptor->Flags |= CM_RESOURCE_MEMORY_PREFETCHABLE; break;
774 }
775 ResourceDescriptor->u.Memory.Start.QuadPart = addr16_data->Minimum;
776 ResourceDescriptor->u.Memory.Length = addr16_data->AddressLength;
777 }
778 ResourceDescriptor++;
779 break;
780 }
781 case ACPI_RESOURCE_TYPE_ADDRESS32:
782 {
783 ACPI_RESOURCE_ADDRESS32 *addr32_data = (ACPI_RESOURCE_ADDRESS32*) &resource->Data;
784 if (addr32_data->ResourceType == ACPI_BUS_NUMBER_RANGE)
785 {
786 ResourceDescriptor->Type = CmResourceTypeBusNumber;
787 ResourceDescriptor->ShareDisposition = CmResourceShareShared;
788 ResourceDescriptor->Flags = 0;
789 ResourceDescriptor->u.BusNumber.Start = addr32_data->Minimum;
790 ResourceDescriptor->u.BusNumber.Length = addr32_data->AddressLength;
791 }
792 else if (addr32_data->ResourceType == ACPI_IO_RANGE)
793 {
794 ResourceDescriptor->Type = CmResourceTypePort;
795 ResourceDescriptor->ShareDisposition = CmResourceShareDeviceExclusive;
796 ResourceDescriptor->Flags = CM_RESOURCE_PORT_IO;
797 if (addr32_data->Decode == ACPI_POS_DECODE)
798 ResourceDescriptor->Flags |= CM_RESOURCE_PORT_POSITIVE_DECODE;
799 ResourceDescriptor->u.Port.Start.QuadPart = addr32_data->Minimum;
800 ResourceDescriptor->u.Port.Length = addr32_data->AddressLength;
801 }
802 else
803 {
804 ResourceDescriptor->Type = CmResourceTypeMemory;
805 ResourceDescriptor->ShareDisposition = CmResourceShareDeviceExclusive;
806 ResourceDescriptor->Flags = 0;
807 if (addr32_data->Info.Mem.WriteProtect == ACPI_READ_ONLY_MEMORY)
808 ResourceDescriptor->Flags |= CM_RESOURCE_MEMORY_READ_ONLY;
809 else
810 ResourceDescriptor->Flags |= CM_RESOURCE_MEMORY_READ_WRITE;
811 switch (addr32_data->Info.Mem.Caching)
812 {
813 case ACPI_CACHABLE_MEMORY: ResourceDescriptor->Flags |= CM_RESOURCE_MEMORY_CACHEABLE; break;
814 case ACPI_WRITE_COMBINING_MEMORY: ResourceDescriptor->Flags |= CM_RESOURCE_MEMORY_COMBINEDWRITE; break;
815 case ACPI_PREFETCHABLE_MEMORY: ResourceDescriptor->Flags |= CM_RESOURCE_MEMORY_PREFETCHABLE; break;
816 }
817 ResourceDescriptor->u.Memory.Start.QuadPart = addr32_data->Minimum;
818 ResourceDescriptor->u.Memory.Length = addr32_data->AddressLength;
819 }
820 ResourceDescriptor++;
821 break;
822 }
823 case ACPI_RESOURCE_TYPE_ADDRESS64:
824 {
825 ACPI_RESOURCE_ADDRESS64 *addr64_data = (ACPI_RESOURCE_ADDRESS64*) &resource->Data;
826 if (addr64_data->ResourceType == ACPI_BUS_NUMBER_RANGE)
827 {
828 DPRINT1("64-bit bus address is not supported!\n");
829 ResourceDescriptor->Type = CmResourceTypeBusNumber;
830 ResourceDescriptor->ShareDisposition = CmResourceShareShared;
831 ResourceDescriptor->Flags = 0;
832 ResourceDescriptor->u.BusNumber.Start = (ULONG)addr64_data->Minimum;
833 ResourceDescriptor->u.BusNumber.Length = addr64_data->AddressLength;
834 }
835 else if (addr64_data->ResourceType == ACPI_IO_RANGE)
836 {
837 ResourceDescriptor->Type = CmResourceTypePort;
838 ResourceDescriptor->ShareDisposition = CmResourceShareDeviceExclusive;
839 ResourceDescriptor->Flags = CM_RESOURCE_PORT_IO;
840 if (addr64_data->Decode == ACPI_POS_DECODE)
841 ResourceDescriptor->Flags |= CM_RESOURCE_PORT_POSITIVE_DECODE;
842 ResourceDescriptor->u.Port.Start.QuadPart = addr64_data->Minimum;
843 ResourceDescriptor->u.Port.Length = addr64_data->AddressLength;
844 }
845 else
846 {
847 ResourceDescriptor->Type = CmResourceTypeMemory;
848 ResourceDescriptor->ShareDisposition = CmResourceShareDeviceExclusive;
849 ResourceDescriptor->Flags = 0;
850 if (addr64_data->Info.Mem.WriteProtect == ACPI_READ_ONLY_MEMORY)
851 ResourceDescriptor->Flags |= CM_RESOURCE_MEMORY_READ_ONLY;
852 else
853 ResourceDescriptor->Flags |= CM_RESOURCE_MEMORY_READ_WRITE;
854 switch (addr64_data->Info.Mem.Caching)
855 {
856 case ACPI_CACHABLE_MEMORY: ResourceDescriptor->Flags |= CM_RESOURCE_MEMORY_CACHEABLE; break;
857 case ACPI_WRITE_COMBINING_MEMORY: ResourceDescriptor->Flags |= CM_RESOURCE_MEMORY_COMBINEDWRITE; break;
858 case ACPI_PREFETCHABLE_MEMORY: ResourceDescriptor->Flags |= CM_RESOURCE_MEMORY_PREFETCHABLE; break;
859 }
860 ResourceDescriptor->u.Memory.Start.QuadPart = addr64_data->Minimum;
861 ResourceDescriptor->u.Memory.Length = addr64_data->AddressLength;
862 }
863 ResourceDescriptor++;
864 break;
865 }
866 case ACPI_RESOURCE_TYPE_MEMORY24:
867 {
868 ACPI_RESOURCE_MEMORY24 *mem24_data = (ACPI_RESOURCE_MEMORY24*) &resource->Data;
869 ResourceDescriptor->Type = CmResourceTypeMemory;
870 ResourceDescriptor->ShareDisposition = CmResourceShareDeviceExclusive;
871 ResourceDescriptor->Flags = CM_RESOURCE_MEMORY_24;
872 if (mem24_data->WriteProtect == ACPI_READ_ONLY_MEMORY)
873 ResourceDescriptor->Flags |= CM_RESOURCE_MEMORY_READ_ONLY;
874 else
875 ResourceDescriptor->Flags |= CM_RESOURCE_MEMORY_READ_WRITE;
876 ResourceDescriptor->u.Memory.Start.QuadPart = mem24_data->Minimum;
877 ResourceDescriptor->u.Memory.Length = mem24_data->AddressLength;
878
879 ResourceDescriptor++;
880 break;
881 }
882 case ACPI_RESOURCE_TYPE_MEMORY32:
883 {
884 ACPI_RESOURCE_MEMORY32 *mem32_data = (ACPI_RESOURCE_MEMORY32*) &resource->Data;
885 ResourceDescriptor->Type = CmResourceTypeMemory;
886 ResourceDescriptor->ShareDisposition = CmResourceShareDeviceExclusive;
887 ResourceDescriptor->Flags = 0;
888 if (mem32_data->WriteProtect == ACPI_READ_ONLY_MEMORY)
889 ResourceDescriptor->Flags |= CM_RESOURCE_MEMORY_READ_ONLY;
890 else
891 ResourceDescriptor->Flags |= CM_RESOURCE_MEMORY_READ_WRITE;
892 ResourceDescriptor->u.Memory.Start.QuadPart = mem32_data->Minimum;
893 ResourceDescriptor->u.Memory.Length = mem32_data->AddressLength;
894
895 ResourceDescriptor++;
896 break;
897 }
898 default:
899 {
900 break;
901 }
902 }
903 resource = ACPI_NEXT_RESOURCE(resource);
904 }
905
906 ExFreePool(Buffer.Pointer);
907 Irp->IoStatus.Information = (ULONG_PTR)ResourceList;
908 return STATUS_SUCCESS;
909 }
910
911 NTSTATUS
912 Bus_PDO_QueryResourceRequirements(
913 PPDO_DEVICE_DATA DeviceData,
914 PIRP Irp )
915 {
916 ULONG NumberOfResources = 0;
917 ACPI_STATUS AcpiStatus;
918 ACPI_BUFFER Buffer;
919 ACPI_RESOURCE* resource;
920 ULONG i, RequirementsListSize;
921 PIO_RESOURCE_REQUIREMENTS_LIST RequirementsList;
922 PIO_RESOURCE_DESCRIPTOR RequirementDescriptor;
923 BOOLEAN CurrentRes = FALSE;
924
925 PAGED_CODE ();
926
927
928 /* Get current resources */
929 while (TRUE)
930 {
931 Buffer.Length = 0;
932 if (CurrentRes)
933 AcpiStatus = AcpiGetCurrentResources(DeviceData->AcpiHandle, &Buffer);
934 else
935 AcpiStatus = AcpiGetPossibleResources(DeviceData->AcpiHandle, &Buffer);
936 if ((!ACPI_SUCCESS(AcpiStatus) && AcpiStatus != AE_BUFFER_OVERFLOW) ||
937 Buffer.Length == 0)
938 {
939 if (!CurrentRes)
940 CurrentRes = TRUE;
941 else
942 return Irp->IoStatus.Status;
943 }
944 else
945 break;
946 }
947
948 Buffer.Pointer = ExAllocatePool(PagedPool, Buffer.Length);
949 if (!Buffer.Pointer)
950 return STATUS_INSUFFICIENT_RESOURCES;
951
952 if (CurrentRes)
953 AcpiStatus = AcpiGetCurrentResources(DeviceData->AcpiHandle, &Buffer);
954 else
955 AcpiStatus = AcpiGetPossibleResources(DeviceData->AcpiHandle, &Buffer);
956 if (!ACPI_SUCCESS(AcpiStatus))
957 {
958 DPRINT1("AcpiGetCurrentResources #2 failed (0x%x)\n", AcpiStatus);
959 ASSERT(FALSE);
960 return STATUS_UNSUCCESSFUL;
961 }
962
963 resource= Buffer.Pointer;
964 /* Count number of resources */
965 while (resource->Type != ACPI_RESOURCE_TYPE_END_TAG)
966 {
967 switch (resource->Type)
968 {
969 case ACPI_RESOURCE_TYPE_IRQ:
970 {
971 ACPI_RESOURCE_IRQ *irq_data = (ACPI_RESOURCE_IRQ*) &resource->Data;
972 NumberOfResources += irq_data->InterruptCount;
973 break;
974 }
975 case ACPI_RESOURCE_TYPE_DMA:
976 {
977 ACPI_RESOURCE_DMA *dma_data = (ACPI_RESOURCE_DMA*) &resource->Data;
978 NumberOfResources += dma_data->ChannelCount;
979 break;
980 }
981 case ACPI_RESOURCE_TYPE_ADDRESS16:
982 case ACPI_RESOURCE_TYPE_ADDRESS32:
983 case ACPI_RESOURCE_TYPE_ADDRESS64:
984 case ACPI_RESOURCE_TYPE_MEMORY24:
985 case ACPI_RESOURCE_TYPE_MEMORY32:
986 case ACPI_RESOURCE_TYPE_IO:
987 {
988 NumberOfResources++;
989 break;
990 }
991 default:
992 {
993 break;
994 }
995 }
996 resource = ACPI_NEXT_RESOURCE(resource);
997 }
998
999 RequirementsListSize = sizeof(IO_RESOURCE_REQUIREMENTS_LIST) + sizeof(IO_RESOURCE_DESCRIPTOR) * (NumberOfResources - 1);
1000 RequirementsList = (PIO_RESOURCE_REQUIREMENTS_LIST)ExAllocatePool(PagedPool, RequirementsListSize);
1001
1002 if (!RequirementsList)
1003 {
1004 ExFreePool(Buffer.Pointer);
1005 return STATUS_INSUFFICIENT_RESOURCES;
1006 }
1007 RequirementsList->ListSize = RequirementsListSize;
1008 RequirementsList->InterfaceType = Internal;
1009 RequirementsList->BusNumber = 0;
1010 RequirementsList->SlotNumber = 0; /* Not used by WDM drivers */
1011 RequirementsList->AlternativeLists = 1;
1012 RequirementsList->List[0].Version = 1;
1013 RequirementsList->List[0].Revision = 1;
1014 RequirementsList->List[0].Count = NumberOfResources;
1015 RequirementDescriptor = RequirementsList->List[0].Descriptors;
1016
1017 /* Fill resources list structure */
1018 while (resource->Type != ACPI_RESOURCE_TYPE_END_TAG)
1019 {
1020 switch (resource->Type)
1021 {
1022 case ACPI_RESOURCE_TYPE_IRQ:
1023 {
1024 ACPI_RESOURCE_IRQ *irq_data = (ACPI_RESOURCE_IRQ*) &resource->Data;
1025 for (i = 0; i < irq_data->InterruptCount; i++)
1026 {
1027 RequirementDescriptor->Option = CurrentRes ? 0 : IO_RESOURCE_PREFERRED;
1028 RequirementDescriptor->Type = CmResourceTypeInterrupt;
1029 RequirementDescriptor->ShareDisposition = (irq_data->Sharable == ACPI_SHARED ? CmResourceShareShared : CmResourceShareDeviceExclusive);
1030 RequirementDescriptor->Flags =(irq_data->Triggering == ACPI_LEVEL_SENSITIVE ? CM_RESOURCE_INTERRUPT_LEVEL_SENSITIVE : CM_RESOURCE_INTERRUPT_LATCHED);
1031 RequirementDescriptor->u.Interrupt.MinimumVector =
1032 RequirementDescriptor->u.Interrupt.MaximumVector = irq_data->Interrupts[i];
1033
1034 RequirementDescriptor++;
1035 }
1036 break;
1037 }
1038 case ACPI_RESOURCE_TYPE_DMA:
1039 {
1040 ACPI_RESOURCE_DMA *dma_data = (ACPI_RESOURCE_DMA*) &resource->Data;
1041 for (i = 0; i < dma_data->ChannelCount; i++)
1042 {
1043 RequirementDescriptor->Type = CmResourceTypeDma;
1044 RequirementDescriptor->Flags = 0;
1045 switch (dma_data->Type)
1046 {
1047 case ACPI_TYPE_A: RequirementDescriptor->Flags |= CM_RESOURCE_DMA_TYPE_A; break;
1048 case ACPI_TYPE_B: RequirementDescriptor->Flags |= CM_RESOURCE_DMA_TYPE_B; break;
1049 case ACPI_TYPE_F: RequirementDescriptor->Flags |= CM_RESOURCE_DMA_TYPE_F; break;
1050 }
1051 if (dma_data->BusMaster == ACPI_BUS_MASTER)
1052 RequirementDescriptor->Flags |= CM_RESOURCE_DMA_BUS_MASTER;
1053 switch (dma_data->Transfer)
1054 {
1055 case ACPI_TRANSFER_8: RequirementDescriptor->Flags |= CM_RESOURCE_DMA_8; break;
1056 case ACPI_TRANSFER_16: RequirementDescriptor->Flags |= CM_RESOURCE_DMA_16; break;
1057 case ACPI_TRANSFER_8_16: RequirementDescriptor->Flags |= CM_RESOURCE_DMA_8_AND_16; break;
1058 }
1059
1060 RequirementDescriptor->Option = CurrentRes ? 0 : IO_RESOURCE_PREFERRED;
1061 RequirementDescriptor->ShareDisposition = CmResourceShareDriverExclusive;
1062 RequirementDescriptor->u.Dma.MinimumChannel =
1063 RequirementDescriptor->u.Dma.MaximumChannel = dma_data->Channels[i];
1064 RequirementDescriptor++;
1065 }
1066 break;
1067 }
1068 case ACPI_RESOURCE_TYPE_IO:
1069 {
1070 ACPI_RESOURCE_IO *io_data = (ACPI_RESOURCE_IO*) &resource->Data;
1071 RequirementDescriptor->Flags = CM_RESOURCE_PORT_IO;
1072 if (io_data->IoDecode == ACPI_DECODE_16)
1073 RequirementDescriptor->Flags |= CM_RESOURCE_PORT_16_BIT_DECODE;
1074 else
1075 RequirementDescriptor->Flags |= CM_RESOURCE_PORT_10_BIT_DECODE;
1076 RequirementDescriptor->u.Port.Length = io_data->AddressLength;
1077 RequirementDescriptor->Option = CurrentRes ? 0 : IO_RESOURCE_PREFERRED;
1078 RequirementDescriptor->Type = CmResourceTypePort;
1079 RequirementDescriptor->ShareDisposition = CmResourceShareDriverExclusive;
1080 RequirementDescriptor->u.Port.Alignment = io_data->Alignment;
1081 RequirementDescriptor->u.Port.MinimumAddress.QuadPart = io_data->Minimum;
1082 RequirementDescriptor->u.Port.MaximumAddress.QuadPart = io_data->Maximum;
1083
1084 RequirementDescriptor++;
1085 break;
1086 }
1087 case ACPI_RESOURCE_TYPE_ADDRESS16:
1088 {
1089 ACPI_RESOURCE_ADDRESS16 *addr16_data = (ACPI_RESOURCE_ADDRESS16*) &resource->Data;
1090 RequirementDescriptor->Option = CurrentRes ? 0 : IO_RESOURCE_PREFERRED;
1091 if (addr16_data->ResourceType == ACPI_BUS_NUMBER_RANGE)
1092 {
1093 RequirementDescriptor->Type = CmResourceTypeBusNumber;
1094 RequirementDescriptor->ShareDisposition = CmResourceShareShared;
1095 RequirementDescriptor->Flags = 0;
1096 RequirementDescriptor->u.BusNumber.MinBusNumber = addr16_data->Minimum;
1097 RequirementDescriptor->u.BusNumber.MaxBusNumber = addr16_data->Maximum;
1098 RequirementDescriptor->u.BusNumber.Length = addr16_data->AddressLength;
1099 }
1100 else if (addr16_data->ResourceType == ACPI_IO_RANGE)
1101 {
1102 RequirementDescriptor->Type = CmResourceTypePort;
1103 RequirementDescriptor->ShareDisposition = CmResourceShareDeviceExclusive;
1104 RequirementDescriptor->Flags = CM_RESOURCE_PORT_IO;
1105 if (addr16_data->Decode == ACPI_POS_DECODE)
1106 RequirementDescriptor->Flags |= CM_RESOURCE_PORT_POSITIVE_DECODE;
1107 RequirementDescriptor->u.Port.MinimumAddress.QuadPart = addr16_data->Minimum;
1108 RequirementDescriptor->u.Port.MaximumAddress.QuadPart = addr16_data->Maximum;
1109 RequirementDescriptor->u.Port.Length = addr16_data->AddressLength;
1110 }
1111 else
1112 {
1113 RequirementDescriptor->Type = CmResourceTypeMemory;
1114 RequirementDescriptor->ShareDisposition = CmResourceShareDeviceExclusive;
1115 RequirementDescriptor->Flags = 0;
1116 if (addr16_data->Info.Mem.WriteProtect == ACPI_READ_ONLY_MEMORY)
1117 RequirementDescriptor->Flags |= CM_RESOURCE_MEMORY_READ_ONLY;
1118 else
1119 RequirementDescriptor->Flags |= CM_RESOURCE_MEMORY_READ_WRITE;
1120 switch (addr16_data->Info.Mem.Caching)
1121 {
1122 case ACPI_CACHABLE_MEMORY: RequirementDescriptor->Flags |= CM_RESOURCE_MEMORY_CACHEABLE; break;
1123 case ACPI_WRITE_COMBINING_MEMORY: RequirementDescriptor->Flags |= CM_RESOURCE_MEMORY_COMBINEDWRITE; break;
1124 case ACPI_PREFETCHABLE_MEMORY: RequirementDescriptor->Flags |= CM_RESOURCE_MEMORY_PREFETCHABLE; break;
1125 }
1126 RequirementDescriptor->u.Memory.MinimumAddress.QuadPart = addr16_data->Minimum;
1127 RequirementDescriptor->u.Memory.MaximumAddress.QuadPart = addr16_data->Maximum;
1128 RequirementDescriptor->u.Memory.Length = addr16_data->AddressLength;
1129 }
1130 RequirementDescriptor++;
1131 break;
1132 }
1133 case ACPI_RESOURCE_TYPE_ADDRESS32:
1134 {
1135 ACPI_RESOURCE_ADDRESS32 *addr32_data = (ACPI_RESOURCE_ADDRESS32*) &resource->Data;
1136 RequirementDescriptor->Option = CurrentRes ? 0 : IO_RESOURCE_PREFERRED;
1137 if (addr32_data->ResourceType == ACPI_BUS_NUMBER_RANGE)
1138 {
1139 RequirementDescriptor->Type = CmResourceTypeBusNumber;
1140 RequirementDescriptor->ShareDisposition = CmResourceShareShared;
1141 RequirementDescriptor->Flags = 0;
1142 RequirementDescriptor->u.BusNumber.MinBusNumber = addr32_data->Minimum;
1143 RequirementDescriptor->u.BusNumber.MaxBusNumber = addr32_data->Maximum;
1144 RequirementDescriptor->u.BusNumber.Length = addr32_data->AddressLength;
1145 }
1146 else if (addr32_data->ResourceType == ACPI_IO_RANGE)
1147 {
1148 RequirementDescriptor->Type = CmResourceTypePort;
1149 RequirementDescriptor->ShareDisposition = CmResourceShareDeviceExclusive;
1150 RequirementDescriptor->Flags = CM_RESOURCE_PORT_IO;
1151 if (addr32_data->Decode == ACPI_POS_DECODE)
1152 RequirementDescriptor->Flags |= CM_RESOURCE_PORT_POSITIVE_DECODE;
1153 RequirementDescriptor->u.Port.MinimumAddress.QuadPart = addr32_data->Minimum;
1154 RequirementDescriptor->u.Port.MaximumAddress.QuadPart = addr32_data->Maximum;
1155 RequirementDescriptor->u.Port.Length = addr32_data->AddressLength;
1156 }
1157 else
1158 {
1159 RequirementDescriptor->Type = CmResourceTypeMemory;
1160 RequirementDescriptor->ShareDisposition = CmResourceShareDeviceExclusive;
1161 RequirementDescriptor->Flags = 0;
1162 if (addr32_data->Info.Mem.WriteProtect == ACPI_READ_ONLY_MEMORY)
1163 RequirementDescriptor->Flags |= CM_RESOURCE_MEMORY_READ_ONLY;
1164 else
1165 RequirementDescriptor->Flags |= CM_RESOURCE_MEMORY_READ_WRITE;
1166 switch (addr32_data->Info.Mem.Caching)
1167 {
1168 case ACPI_CACHABLE_MEMORY: RequirementDescriptor->Flags |= CM_RESOURCE_MEMORY_CACHEABLE; break;
1169 case ACPI_WRITE_COMBINING_MEMORY: RequirementDescriptor->Flags |= CM_RESOURCE_MEMORY_COMBINEDWRITE; break;
1170 case ACPI_PREFETCHABLE_MEMORY: RequirementDescriptor->Flags |= CM_RESOURCE_MEMORY_PREFETCHABLE; break;
1171 }
1172 RequirementDescriptor->u.Memory.MinimumAddress.QuadPart = addr32_data->Minimum;
1173 RequirementDescriptor->u.Memory.MaximumAddress.QuadPart = addr32_data->Maximum;
1174 RequirementDescriptor->u.Memory.Length = addr32_data->AddressLength;
1175 }
1176 RequirementDescriptor++;
1177 break;
1178 }
1179 case ACPI_RESOURCE_TYPE_ADDRESS64:
1180 {
1181 ACPI_RESOURCE_ADDRESS64 *addr64_data = (ACPI_RESOURCE_ADDRESS64*) &resource->Data;
1182 RequirementDescriptor->Option = CurrentRes ? 0 : IO_RESOURCE_PREFERRED;
1183 if (addr64_data->ResourceType == ACPI_BUS_NUMBER_RANGE)
1184 {
1185 DPRINT1("64-bit bus address is not supported!\n");
1186 RequirementDescriptor->Type = CmResourceTypeBusNumber;
1187 RequirementDescriptor->ShareDisposition = CmResourceShareShared;
1188 RequirementDescriptor->Flags = 0;
1189 RequirementDescriptor->u.BusNumber.MinBusNumber = (ULONG)addr64_data->Minimum;
1190 RequirementDescriptor->u.BusNumber.MaxBusNumber = (ULONG)addr64_data->Maximum;
1191 RequirementDescriptor->u.BusNumber.Length = addr64_data->AddressLength;
1192 }
1193 else if (addr64_data->ResourceType == ACPI_IO_RANGE)
1194 {
1195 RequirementDescriptor->Type = CmResourceTypePort;
1196 RequirementDescriptor->ShareDisposition = CmResourceShareDeviceExclusive;
1197 RequirementDescriptor->Flags = CM_RESOURCE_PORT_IO;
1198 if (addr64_data->Decode == ACPI_POS_DECODE)
1199 RequirementDescriptor->Flags |= CM_RESOURCE_PORT_POSITIVE_DECODE;
1200 RequirementDescriptor->u.Port.MinimumAddress.QuadPart = addr64_data->Minimum;
1201 RequirementDescriptor->u.Port.MaximumAddress.QuadPart = addr64_data->Maximum;
1202 RequirementDescriptor->u.Port.Length = addr64_data->AddressLength;
1203 }
1204 else
1205 {
1206 RequirementDescriptor->Type = CmResourceTypeMemory;
1207 RequirementDescriptor->ShareDisposition = CmResourceShareDeviceExclusive;
1208 RequirementDescriptor->Flags = 0;
1209 if (addr64_data->Info.Mem.WriteProtect == ACPI_READ_ONLY_MEMORY)
1210 RequirementDescriptor->Flags |= CM_RESOURCE_MEMORY_READ_ONLY;
1211 else
1212 RequirementDescriptor->Flags |= CM_RESOURCE_MEMORY_READ_WRITE;
1213 switch (addr64_data->Info.Mem.Caching)
1214 {
1215 case ACPI_CACHABLE_MEMORY: RequirementDescriptor->Flags |= CM_RESOURCE_MEMORY_CACHEABLE; break;
1216 case ACPI_WRITE_COMBINING_MEMORY: RequirementDescriptor->Flags |= CM_RESOURCE_MEMORY_COMBINEDWRITE; break;
1217 case ACPI_PREFETCHABLE_MEMORY: RequirementDescriptor->Flags |= CM_RESOURCE_MEMORY_PREFETCHABLE; break;
1218 }
1219 RequirementDescriptor->u.Memory.MinimumAddress.QuadPart = addr64_data->Minimum;
1220 RequirementDescriptor->u.Memory.MaximumAddress.QuadPart = addr64_data->Maximum;
1221 RequirementDescriptor->u.Memory.Length = addr64_data->AddressLength;
1222 }
1223 RequirementDescriptor++;
1224 break;
1225 }
1226 case ACPI_RESOURCE_TYPE_MEMORY24:
1227 {
1228 ACPI_RESOURCE_MEMORY24 *mem24_data = (ACPI_RESOURCE_MEMORY24*) &resource->Data;
1229 RequirementDescriptor->Option = CurrentRes ? 0 : IO_RESOURCE_PREFERRED;
1230 RequirementDescriptor->Type = CmResourceTypeMemory;
1231 RequirementDescriptor->ShareDisposition = CmResourceShareDeviceExclusive;
1232 RequirementDescriptor->Flags = CM_RESOURCE_MEMORY_24;
1233 if (mem24_data->WriteProtect == ACPI_READ_ONLY_MEMORY)
1234 RequirementDescriptor->Flags |= CM_RESOURCE_MEMORY_READ_ONLY;
1235 else
1236 RequirementDescriptor->Flags |= CM_RESOURCE_MEMORY_READ_WRITE;
1237 RequirementDescriptor->u.Memory.MinimumAddress.QuadPart = mem24_data->Minimum;
1238 RequirementDescriptor->u.Memory.MaximumAddress.QuadPart = mem24_data->Maximum;
1239 RequirementDescriptor->u.Memory.Length = mem24_data->AddressLength;
1240
1241 RequirementDescriptor++;
1242 break;
1243 }
1244 case ACPI_RESOURCE_TYPE_MEMORY32:
1245 {
1246 ACPI_RESOURCE_MEMORY32 *mem32_data = (ACPI_RESOURCE_MEMORY32*) &resource->Data;
1247 RequirementDescriptor->Option = CurrentRes ? 0 : IO_RESOURCE_PREFERRED;
1248 RequirementDescriptor->Type = CmResourceTypeMemory;
1249 RequirementDescriptor->ShareDisposition = CmResourceShareDeviceExclusive;
1250 RequirementDescriptor->Flags = 0;
1251 if (mem32_data->WriteProtect == ACPI_READ_ONLY_MEMORY)
1252 RequirementDescriptor->Flags |= CM_RESOURCE_MEMORY_READ_ONLY;
1253 else
1254 RequirementDescriptor->Flags |= CM_RESOURCE_MEMORY_READ_WRITE;
1255 RequirementDescriptor->u.Memory.MinimumAddress.QuadPart = mem32_data->Minimum;
1256 RequirementDescriptor->u.Memory.MaximumAddress.QuadPart = mem32_data->Maximum;
1257 RequirementDescriptor->u.Memory.Length = mem32_data->AddressLength;
1258
1259 RequirementDescriptor++;
1260 break;
1261 }
1262 default:
1263 {
1264 break;
1265 }
1266 }
1267 resource = ACPI_NEXT_RESOURCE(resource);
1268 }
1269 ExFreePool(Buffer.Pointer);
1270
1271 Irp->IoStatus.Information = (ULONG_PTR)RequirementsList;
1272
1273 return STATUS_SUCCESS;
1274 }
1275
1276 NTSTATUS
1277 Bus_PDO_QueryDeviceRelations(
1278 PPDO_DEVICE_DATA DeviceData,
1279 PIRP Irp )
1280 /*++
1281
1282 Routine Description:
1283
1284 The PnP Manager sends this IRP to gather information about
1285 devices with a relationship to the specified device.
1286 Bus drivers must handle this request for TargetDeviceRelation
1287 for their child devices (child PDOs).
1288
1289 If a driver returns relations in response to this IRP,
1290 it allocates a DEVICE_RELATIONS structure from paged
1291 memory containing a count and the appropriate number of
1292 device object pointers. The PnP Manager frees the structure
1293 when it is no longer needed. If a driver replaces a
1294 DEVICE_RELATIONS structure allocated by another driver,
1295 it must free the previous structure.
1296
1297 A driver must reference the PDO of any device that it
1298 reports in this IRP (ObReferenceObject). The PnP Manager
1299 removes the reference when appropriate.
1300
1301 Arguments:
1302
1303 DeviceData - Pointer to the PDO's device extension.
1304 Irp - Pointer to the irp.
1305
1306 Return Value:
1307
1308 NT STATUS
1309
1310 --*/
1311 {
1312
1313 PIO_STACK_LOCATION stack;
1314 PDEVICE_RELATIONS deviceRelations;
1315 NTSTATUS status;
1316
1317 PAGED_CODE ();
1318
1319 stack = IoGetCurrentIrpStackLocation (Irp);
1320
1321 switch (stack->Parameters.QueryDeviceRelations.Type) {
1322
1323 case TargetDeviceRelation:
1324
1325 deviceRelations = (PDEVICE_RELATIONS) Irp->IoStatus.Information;
1326 if (deviceRelations) {
1327 //
1328 // Only PDO can handle this request. Somebody above
1329 // is not playing by rule.
1330 //
1331 ASSERTMSG("Someone above is handling TargetDeviceRelation", !deviceRelations);
1332 }
1333
1334 deviceRelations = (PDEVICE_RELATIONS)
1335 ExAllocatePoolWithTag (PagedPool,
1336 sizeof(DEVICE_RELATIONS),
1337 'IPCA');
1338 if (!deviceRelations) {
1339 status = STATUS_INSUFFICIENT_RESOURCES;
1340 break;
1341 }
1342
1343 //
1344 // There is only one PDO pointer in the structure
1345 // for this relation type. The PnP Manager removes
1346 // the reference to the PDO when the driver or application
1347 // un-registers for notification on the device.
1348 //
1349
1350 deviceRelations->Count = 1;
1351 deviceRelations->Objects[0] = DeviceData->Common.Self;
1352 ObReferenceObject(DeviceData->Common.Self);
1353
1354 status = STATUS_SUCCESS;
1355 Irp->IoStatus.Information = (ULONG_PTR) deviceRelations;
1356 break;
1357
1358 case BusRelations: // Not handled by PDO
1359 case EjectionRelations: // optional for PDO
1360 case RemovalRelations: // // optional for PDO
1361 default:
1362 status = Irp->IoStatus.Status;
1363 }
1364
1365 return status;
1366 }
1367
1368 NTSTATUS
1369 Bus_PDO_QueryBusInformation(
1370 PPDO_DEVICE_DATA DeviceData,
1371 PIRP Irp )
1372 /*++
1373
1374 Routine Description:
1375
1376 The PnP Manager uses this IRP to request the type and
1377 instance number of a device's parent bus. Bus drivers
1378 should handle this request for their child devices (PDOs).
1379
1380 Arguments:
1381
1382 DeviceData - Pointer to the PDO's device extension.
1383 Irp - Pointer to the irp.
1384
1385 Return Value:
1386
1387 NT STATUS
1388
1389 --*/
1390 {
1391
1392 PPNP_BUS_INFORMATION busInfo;
1393
1394 PAGED_CODE ();
1395
1396 busInfo = ExAllocatePoolWithTag (PagedPool, sizeof(PNP_BUS_INFORMATION),
1397 'IPCA');
1398
1399 if (busInfo == NULL) {
1400 return STATUS_INSUFFICIENT_RESOURCES;
1401 }
1402
1403 busInfo->BusTypeGuid = GUID_ACPI_INTERFACE_STANDARD;
1404
1405 busInfo->LegacyBusType = InternalPowerBus;
1406
1407 busInfo->BusNumber = 0; //fixme
1408
1409 Irp->IoStatus.Information = (ULONG_PTR)busInfo;
1410
1411 return STATUS_SUCCESS;
1412 }
1413
1414
1415 NTSTATUS
1416 Bus_GetDeviceCapabilities(
1417 PDEVICE_OBJECT DeviceObject,
1418 PDEVICE_CAPABILITIES DeviceCapabilities
1419 )
1420 {
1421 IO_STATUS_BLOCK ioStatus;
1422 KEVENT pnpEvent;
1423 NTSTATUS status;
1424 PDEVICE_OBJECT targetObject;
1425 PIO_STACK_LOCATION irpStack;
1426 PIRP pnpIrp;
1427
1428 PAGED_CODE();
1429
1430 //
1431 // Initialize the capabilities that we will send down
1432 //
1433 RtlZeroMemory( DeviceCapabilities, sizeof(DEVICE_CAPABILITIES) );
1434 DeviceCapabilities->Size = sizeof(DEVICE_CAPABILITIES);
1435 DeviceCapabilities->Version = 1;
1436 DeviceCapabilities->Address = -1;
1437 DeviceCapabilities->UINumber = -1;
1438
1439 //
1440 // Initialize the event
1441 //
1442 KeInitializeEvent( &pnpEvent, NotificationEvent, FALSE );
1443
1444 targetObject = IoGetAttachedDeviceReference( DeviceObject );
1445
1446 //
1447 // Build an Irp
1448 //
1449 pnpIrp = IoBuildSynchronousFsdRequest(
1450 IRP_MJ_PNP,
1451 targetObject,
1452 NULL,
1453 0,
1454 NULL,
1455 &pnpEvent,
1456 &ioStatus
1457 );
1458 if (pnpIrp == NULL) {
1459
1460 status = STATUS_INSUFFICIENT_RESOURCES;
1461 goto GetDeviceCapabilitiesExit;
1462
1463 }
1464
1465 //
1466 // Pnp Irps all begin life as STATUS_NOT_SUPPORTED;
1467 //
1468 pnpIrp->IoStatus.Status = STATUS_NOT_SUPPORTED;
1469
1470 //
1471 // Get the top of stack
1472 //
1473 irpStack = IoGetNextIrpStackLocation( pnpIrp );
1474
1475 //
1476 // Set the top of stack
1477 //
1478 RtlZeroMemory( irpStack, sizeof(IO_STACK_LOCATION ) );
1479 irpStack->MajorFunction = IRP_MJ_PNP;
1480 irpStack->MinorFunction = IRP_MN_QUERY_CAPABILITIES;
1481 irpStack->Parameters.DeviceCapabilities.Capabilities = DeviceCapabilities;
1482
1483 //
1484 // Call the driver
1485 //
1486 status = IoCallDriver( targetObject, pnpIrp );
1487 if (status == STATUS_PENDING) {
1488
1489 //
1490 // Block until the irp comes back.
1491 // Important thing to note here is when you allocate
1492 // the memory for an event in the stack you must do a
1493 // KernelMode wait instead of UserMode to prevent
1494 // the stack from getting paged out.
1495 //
1496
1497 KeWaitForSingleObject(
1498 &pnpEvent,
1499 Executive,
1500 KernelMode,
1501 FALSE,
1502 NULL
1503 );
1504 status = ioStatus.Status;
1505
1506 }
1507
1508 GetDeviceCapabilitiesExit:
1509 //
1510 // Done with reference
1511 //
1512 ObDereferenceObject( targetObject );
1513
1514 //
1515 // Done
1516 //
1517 return status;
1518
1519 }
1520
1521