- Let the ACPI driver handle fixed feature buttons
[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 deviceCapabilities->RawDeviceOK = FALSE;
329 }
330 else
331 {
332 deviceCapabilities->EjectSupported = FALSE;
333 deviceCapabilities->HardwareDisabled = FALSE;
334 deviceCapabilities->Removable = FALSE;
335 deviceCapabilities->SurpriseRemovalOK = FALSE;
336 deviceCapabilities->UniqueID = FALSE;
337 deviceCapabilities->NoDisplayInUI = FALSE;
338 deviceCapabilities->Address = 0;
339
340 /* The ACPI driver will run fixed buttons */
341 deviceCapabilities->RawDeviceOK = TRUE;
342 }
343
344 deviceCapabilities->SilentInstall = FALSE;
345 deviceCapabilities->UINumber = (ULONG)-1;
346
347 return STATUS_SUCCESS;
348
349 }
350
351 NTSTATUS
352 Bus_PDO_QueryDeviceId(
353 PPDO_DEVICE_DATA DeviceData,
354 PIRP Irp )
355 {
356 PIO_STACK_LOCATION stack;
357 PWCHAR buffer;
358 WCHAR temp[256];
359 ULONG length;
360 NTSTATUS status = STATUS_SUCCESS;
361 struct acpi_device *Device;
362
363 PAGED_CODE ();
364
365 stack = IoGetCurrentIrpStackLocation (Irp);
366
367 switch (stack->Parameters.QueryId.IdType) {
368
369 case BusQueryDeviceID:
370 if (DeviceData->AcpiHandle)
371 {
372 acpi_bus_get_device(DeviceData->AcpiHandle, &Device);
373
374 length = swprintf(temp,
375 L"ACPI\\%hs",
376 Device->pnp.hardware_id);
377 }
378 else
379 {
380 /* We know it's a fixed feature button because
381 * these are direct children of the ACPI root device
382 * and therefore have no handle
383 */
384 length = swprintf(temp,
385 L"ACPI\\FixedButton");
386 }
387
388 temp[++length] = UNICODE_NULL;
389
390 buffer = ExAllocatePoolWithTag (PagedPool, length * sizeof(WCHAR), 'IPCA');
391
392 if (!buffer) {
393 status = STATUS_INSUFFICIENT_RESOURCES;
394 break;
395 }
396
397 RtlCopyMemory (buffer, temp, length * sizeof(WCHAR));
398 Irp->IoStatus.Information = (ULONG_PTR) buffer;
399 DPRINT("BusQueryDeviceID: %ls\n",buffer);
400 break;
401
402 case BusQueryInstanceID:
403 /* See comment in BusQueryDeviceID case */
404 if(DeviceData->AcpiHandle)
405 {
406 acpi_bus_get_device(DeviceData->AcpiHandle, &Device);
407
408 if (Device->flags.unique_id)
409 length = swprintf(temp,
410 L"%hs",
411 Device->pnp.unique_id);
412 else
413 /* FIXME: Generate unique id! */
414 length = swprintf(temp, L"%ls", L"0000");
415 }
416 else
417 /* FIXME: Generate unique id! */
418 length = swprintf(temp, L"%ls", L"0000");
419
420 temp[++length] = UNICODE_NULL;
421
422 buffer = ExAllocatePoolWithTag (PagedPool, length * sizeof (WCHAR), 'IPCA');
423 if (!buffer) {
424 status = STATUS_INSUFFICIENT_RESOURCES;
425 break;
426 }
427
428 RtlCopyMemory (buffer, temp, length * sizeof (WCHAR));
429 DPRINT("BusQueryInstanceID: %ls\n",buffer);
430 Irp->IoStatus.Information = (ULONG_PTR) buffer;
431 break;
432
433 case BusQueryHardwareIDs:
434 length = 0;
435
436 /* See comment in BusQueryDeviceID case */
437 if (DeviceData->AcpiHandle)
438 {
439 acpi_bus_get_device(DeviceData->AcpiHandle, &Device);
440
441 length += swprintf(&temp[length],
442 L"ACPI\\%hs",
443 Device->pnp.hardware_id);
444 length++;
445
446 length += swprintf(&temp[length],
447 L"*%hs",
448 Device->pnp.hardware_id);
449 length++;
450 }
451 else
452 {
453 length += swprintf(&temp[length],
454 L"ACPI\\FixedButton");
455 length++;
456
457 length += swprintf(&temp[length],
458 L"*FixedButton");
459 length++;
460 }
461
462 temp[length] = UNICODE_NULL;
463
464 length++;
465
466 temp[length] = UNICODE_NULL;
467
468 buffer = ExAllocatePoolWithTag (PagedPool, length * sizeof(WCHAR), 'IPCA');
469
470 if (!buffer) {
471 status = STATUS_INSUFFICIENT_RESOURCES;
472 break;
473 }
474
475 RtlCopyMemory (buffer, temp, length * sizeof(WCHAR));
476 Irp->IoStatus.Information = (ULONG_PTR) buffer;
477 DPRINT("BusQueryHardwareIDs: %ls\n",buffer);
478 break;
479
480 default:
481 status = Irp->IoStatus.Status;
482 }
483 return status;
484 }
485
486 NTSTATUS
487 Bus_PDO_QueryDeviceText(
488 PPDO_DEVICE_DATA DeviceData,
489 PIRP Irp )
490 {
491 PWCHAR Buffer;
492 PIO_STACK_LOCATION stack;
493 NTSTATUS status;
494 PAGED_CODE ();
495
496 stack = IoGetCurrentIrpStackLocation (Irp);
497
498 switch (stack->Parameters.QueryDeviceText.DeviceTextType) {
499
500 case DeviceTextDescription:
501
502 if (!Irp->IoStatus.Information) {
503 if (wcsstr (DeviceData->HardwareIDs, L"PNP000") != 0)
504 Buffer = L"Programmable interrupt controller";
505 else if (wcsstr(DeviceData->HardwareIDs, L"PNP010") != 0)
506 Buffer = L"System timer";
507 else if (wcsstr(DeviceData->HardwareIDs, L"PNP020") != 0)
508 Buffer = L"DMA controller";
509 else if (wcsstr(DeviceData->HardwareIDs, L"PNP03") != 0)
510 Buffer = L"Keyboard";
511 else if (wcsstr(DeviceData->HardwareIDs, L"PNP040") != 0)
512 Buffer = L"Parallel port";
513 else if (wcsstr(DeviceData->HardwareIDs, L"PNP05") != 0)
514 Buffer = L"Serial port";
515 else if (wcsstr(DeviceData->HardwareIDs, L"PNP06") != 0)
516 Buffer = L"Disk controller";
517 else if (wcsstr(DeviceData->HardwareIDs, L"PNP07") != 0)
518 Buffer = L"Disk controller";
519 else if (wcsstr(DeviceData->HardwareIDs, L"PNP09") != 0)
520 Buffer = L"Display adapter";
521 else if (wcsstr(DeviceData->HardwareIDs, L"PNP0A0") != 0)
522 Buffer = L"Bus controller";
523 else if (wcsstr(DeviceData->HardwareIDs, L"PNP0E0") != 0)
524 Buffer = L"PCMCIA controller";
525 else if (wcsstr(DeviceData->HardwareIDs, L"PNP0F") != 0)
526 Buffer = L"Mouse device";
527 else if (wcsstr(DeviceData->HardwareIDs, L"PNP8") != 0)
528 Buffer = L"Network adapter";
529 else if (wcsstr(DeviceData->HardwareIDs, L"PNPA0") != 0)
530 Buffer = L"SCSI controller";
531 else if (wcsstr(DeviceData->HardwareIDs, L"PNPB0") != 0)
532 Buffer = L"Multimedia device";
533 else if (wcsstr(DeviceData->HardwareIDs, L"PNPC00") != 0)
534 Buffer = L"Modem";
535 else if (wcsstr(DeviceData->HardwareIDs, L"PNP0C0C") != 0)
536 Buffer = L"Power Button";
537 else if (wcsstr(DeviceData->HardwareIDs, L"PNP0C0E") != 0)
538 Buffer = L"Sleep Button";
539 else if (wcsstr(DeviceData->HardwareIDs, L"PNP0C0D") != 0)
540 Buffer = L"Lid Switch";
541 else if (wcsstr(DeviceData->HardwareIDs, L"PNP0C09") != 0)
542 Buffer = L"ACPI Embedded Controller";
543 else if (wcsstr(DeviceData->HardwareIDs, L"PNP0C0B") != 0)
544 Buffer = L"ACPI Fan";
545 else if (wcsstr(DeviceData->HardwareIDs, L"PNP0A03") != 0)
546 Buffer = L"PCI Root Bridge";
547 else if (wcsstr(DeviceData->HardwareIDs, L"PNP0C0A") != 0)
548 Buffer = L"ACPI Battery";
549 else if (wcsstr(DeviceData->HardwareIDs, L"PNP0C0F") != 0)
550 Buffer = L"PCI Interrupt Link";
551 else if (wcsstr(DeviceData->HardwareIDs, L"ACPI_PWR") != 0)
552 Buffer = L"ACPI Power Resource";
553 else if (wcsstr(DeviceData->HardwareIDs, L"Processor") != 0)
554 Buffer = L"Processor";
555 else if (wcsstr(DeviceData->HardwareIDs, L"ThermalZone") != 0)
556 Buffer = L"ACPI Thermal Zone";
557 else if (wcsstr(DeviceData->HardwareIDs, L"ACPI0002") != 0)
558 Buffer = L"Smart Battery";
559 else if (wcsstr(DeviceData->HardwareIDs, L"ACPI0003") != 0)
560 Buffer = L"AC Adapter";
561 /* Simply checking if AcpiHandle is NULL eliminates the need to check
562 * for the 4 different names that ACPI knows the fixed feature button as internally
563 */
564 else if (!DeviceData->AcpiHandle)
565 Buffer = L"ACPI Fixed Feature Button";
566 else
567 Buffer = L"Other ACPI device";
568
569 DPRINT("\tDeviceTextDescription :%ws\n", Buffer);
570
571 Irp->IoStatus.Information = (ULONG_PTR) Buffer;
572 }
573 status = STATUS_SUCCESS;
574 break;
575
576 default:
577 status = Irp->IoStatus.Status;
578 break;
579 }
580
581 return status;
582
583 }
584
585 NTSTATUS
586 Bus_PDO_QueryResources(
587 PPDO_DEVICE_DATA DeviceData,
588 PIRP Irp )
589 {
590 ULONG NumberOfResources = 0;
591 PCM_RESOURCE_LIST ResourceList;
592 PCM_PARTIAL_RESOURCE_DESCRIPTOR ResourceDescriptor;
593 ACPI_STATUS AcpiStatus;
594 ACPI_BUFFER Buffer;
595 ACPI_RESOURCE* resource;
596 ULONG ResourceListSize;
597 ULONG i;
598
599 /* Get current resources */
600 Buffer.Length = 0;
601 AcpiStatus = AcpiGetCurrentResources(DeviceData->AcpiHandle, &Buffer);
602 if ((!ACPI_SUCCESS(AcpiStatus) && AcpiStatus != AE_BUFFER_OVERFLOW) ||
603 Buffer.Length == 0)
604 {
605 return Irp->IoStatus.Status;
606 }
607
608 Buffer.Pointer = ExAllocatePool(PagedPool, Buffer.Length);
609 if (!Buffer.Pointer)
610 return STATUS_INSUFFICIENT_RESOURCES;
611
612 AcpiStatus = AcpiGetCurrentResources(DeviceData->AcpiHandle, &Buffer);
613 if (!ACPI_SUCCESS(AcpiStatus))
614 {
615 DPRINT1("AcpiGetCurrentResources #2 failed (0x%x)\n", AcpiStatus);
616 ASSERT(FALSE);
617 return STATUS_UNSUCCESSFUL;
618 }
619
620 resource= Buffer.Pointer;
621 /* Count number of resources */
622 while (resource->Type != ACPI_RESOURCE_TYPE_END_TAG)
623 {
624 switch (resource->Type)
625 {
626 case ACPI_RESOURCE_TYPE_IRQ:
627 {
628 ACPI_RESOURCE_IRQ *irq_data = (ACPI_RESOURCE_IRQ*) &resource->Data;
629 NumberOfResources += irq_data->InterruptCount;
630 break;
631 }
632 case ACPI_RESOURCE_TYPE_DMA:
633 {
634 ACPI_RESOURCE_DMA *dma_data = (ACPI_RESOURCE_DMA*) &resource->Data;
635 NumberOfResources += dma_data->ChannelCount;
636 break;
637 }
638 case ACPI_RESOURCE_TYPE_ADDRESS16:
639 case ACPI_RESOURCE_TYPE_ADDRESS32:
640 case ACPI_RESOURCE_TYPE_ADDRESS64:
641 case ACPI_RESOURCE_TYPE_MEMORY24:
642 case ACPI_RESOURCE_TYPE_MEMORY32:
643 case ACPI_RESOURCE_TYPE_IO:
644 {
645 NumberOfResources++;
646 break;
647 }
648 default:
649 {
650 DPRINT1("Unknown resource type: %d\n", resource->Type);
651 break;
652 }
653 }
654 resource = ACPI_NEXT_RESOURCE(resource);
655 }
656
657 /* Allocate memory */
658 ResourceListSize = sizeof(CM_RESOURCE_LIST) + sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR) * (NumberOfResources - 1);
659 ResourceList = (PCM_RESOURCE_LIST)ExAllocatePool(PagedPool, ResourceListSize);
660
661 if (!ResourceList)
662 {
663 ExFreePool(Buffer.Pointer);
664 return STATUS_INSUFFICIENT_RESOURCES;
665 }
666 ResourceList->Count = 1;
667 ResourceList->List[0].InterfaceType = Internal; /* FIXME */
668 ResourceList->List[0].BusNumber = 0; /* We're the only ACPI bus device in the system */
669 ResourceList->List[0].PartialResourceList.Version = 1;
670 ResourceList->List[0].PartialResourceList.Revision = 1;
671 ResourceList->List[0].PartialResourceList.Count = NumberOfResources;
672 ResourceDescriptor = ResourceList->List[0].PartialResourceList.PartialDescriptors;
673
674 /* Fill resources list structure */
675 while (resource->Type != ACPI_RESOURCE_TYPE_END_TAG)
676 {
677 switch (resource->Type)
678 {
679 case ACPI_RESOURCE_TYPE_IRQ:
680 {
681 ACPI_RESOURCE_IRQ *irq_data = (ACPI_RESOURCE_IRQ*) &resource->Data;
682 for (i = 0; i < irq_data->InterruptCount; i++)
683 {
684 ResourceDescriptor->Type = CmResourceTypeInterrupt;
685
686 ResourceDescriptor->ShareDisposition =
687 (irq_data->Sharable == ACPI_SHARED ? CmResourceShareShared : CmResourceShareDeviceExclusive);
688 ResourceDescriptor->Flags =
689 (irq_data->Triggering == ACPI_LEVEL_SENSITIVE ? CM_RESOURCE_INTERRUPT_LEVEL_SENSITIVE : CM_RESOURCE_INTERRUPT_LATCHED);
690 ResourceDescriptor->u.Interrupt.Level = irq_data->Interrupts[i];
691 ResourceDescriptor->u.Interrupt.Vector = 0;
692 ResourceDescriptor->u.Interrupt.Affinity = (KAFFINITY)(-1);
693
694 ResourceDescriptor++;
695 }
696 break;
697 }
698 case ACPI_RESOURCE_TYPE_DMA:
699 {
700 ACPI_RESOURCE_DMA *dma_data = (ACPI_RESOURCE_DMA*) &resource->Data;
701 for (i = 0; i < dma_data->ChannelCount; i++)
702 {
703 ResourceDescriptor->Type = CmResourceTypeDma;
704 ResourceDescriptor->Flags = 0;
705 switch (dma_data->Type)
706 {
707 case ACPI_TYPE_A: ResourceDescriptor->Flags |= CM_RESOURCE_DMA_TYPE_A; break;
708 case ACPI_TYPE_B: ResourceDescriptor->Flags |= CM_RESOURCE_DMA_TYPE_B; break;
709 case ACPI_TYPE_F: ResourceDescriptor->Flags |= CM_RESOURCE_DMA_TYPE_F; break;
710 }
711 if (dma_data->BusMaster == ACPI_BUS_MASTER)
712 ResourceDescriptor->Flags |= CM_RESOURCE_DMA_BUS_MASTER;
713 switch (dma_data->Transfer)
714 {
715 case ACPI_TRANSFER_8: ResourceDescriptor->Flags |= CM_RESOURCE_DMA_8; break;
716 case ACPI_TRANSFER_16: ResourceDescriptor->Flags |= CM_RESOURCE_DMA_16; break;
717 case ACPI_TRANSFER_8_16: ResourceDescriptor->Flags |= CM_RESOURCE_DMA_8_AND_16; break;
718 }
719 ResourceDescriptor->u.Dma.Channel = dma_data->Channels[i];
720
721 ResourceDescriptor++;
722 }
723 break;
724 }
725 case ACPI_RESOURCE_TYPE_IO:
726 {
727 ACPI_RESOURCE_IO *io_data = (ACPI_RESOURCE_IO*) &resource->Data;
728 ResourceDescriptor->Type = CmResourceTypePort;
729 ResourceDescriptor->ShareDisposition = CmResourceShareDriverExclusive;
730 ResourceDescriptor->Flags = CM_RESOURCE_PORT_IO;
731 if (io_data->IoDecode == ACPI_DECODE_16)
732 ResourceDescriptor->Flags |= CM_RESOURCE_PORT_16_BIT_DECODE;
733 else
734 ResourceDescriptor->Flags |= CM_RESOURCE_PORT_10_BIT_DECODE;
735 ResourceDescriptor->u.Port.Start.QuadPart = io_data->Minimum;
736 ResourceDescriptor->u.Port.Length = io_data->AddressLength;
737
738 ResourceDescriptor++;
739 break;
740 }
741 case ACPI_RESOURCE_TYPE_ADDRESS16:
742 {
743 ACPI_RESOURCE_ADDRESS16 *addr16_data = (ACPI_RESOURCE_ADDRESS16*) &resource->Data;
744 if (addr16_data->ResourceType == ACPI_BUS_NUMBER_RANGE)
745 {
746 ResourceDescriptor->Type = CmResourceTypeBusNumber;
747 ResourceDescriptor->ShareDisposition = CmResourceShareShared;
748 ResourceDescriptor->Flags = 0;
749 ResourceDescriptor->u.BusNumber.Start = addr16_data->Minimum;
750 ResourceDescriptor->u.BusNumber.Length = addr16_data->AddressLength;
751 }
752 else if (addr16_data->ResourceType == ACPI_IO_RANGE)
753 {
754 ResourceDescriptor->Type = CmResourceTypePort;
755 ResourceDescriptor->ShareDisposition = CmResourceShareDeviceExclusive;
756 ResourceDescriptor->Flags = CM_RESOURCE_PORT_IO;
757 if (addr16_data->Decode == ACPI_POS_DECODE)
758 ResourceDescriptor->Flags |= CM_RESOURCE_PORT_POSITIVE_DECODE;
759 ResourceDescriptor->u.Port.Start.QuadPart = addr16_data->Minimum;
760 ResourceDescriptor->u.Port.Length = addr16_data->AddressLength;
761 }
762 else
763 {
764 ResourceDescriptor->Type = CmResourceTypeMemory;
765 ResourceDescriptor->ShareDisposition = CmResourceShareDeviceExclusive;
766 ResourceDescriptor->Flags = 0;
767 if (addr16_data->Info.Mem.WriteProtect == ACPI_READ_ONLY_MEMORY)
768 ResourceDescriptor->Flags |= CM_RESOURCE_MEMORY_READ_ONLY;
769 else
770 ResourceDescriptor->Flags |= CM_RESOURCE_MEMORY_READ_WRITE;
771 switch (addr16_data->Info.Mem.Caching)
772 {
773 case ACPI_CACHABLE_MEMORY: ResourceDescriptor->Flags |= CM_RESOURCE_MEMORY_CACHEABLE; break;
774 case ACPI_WRITE_COMBINING_MEMORY: ResourceDescriptor->Flags |= CM_RESOURCE_MEMORY_COMBINEDWRITE; break;
775 case ACPI_PREFETCHABLE_MEMORY: ResourceDescriptor->Flags |= CM_RESOURCE_MEMORY_PREFETCHABLE; break;
776 }
777 ResourceDescriptor->u.Memory.Start.QuadPart = addr16_data->Minimum;
778 ResourceDescriptor->u.Memory.Length = addr16_data->AddressLength;
779 }
780 ResourceDescriptor++;
781 break;
782 }
783 case ACPI_RESOURCE_TYPE_ADDRESS32:
784 {
785 ACPI_RESOURCE_ADDRESS32 *addr32_data = (ACPI_RESOURCE_ADDRESS32*) &resource->Data;
786 if (addr32_data->ResourceType == ACPI_BUS_NUMBER_RANGE)
787 {
788 ResourceDescriptor->Type = CmResourceTypeBusNumber;
789 ResourceDescriptor->ShareDisposition = CmResourceShareShared;
790 ResourceDescriptor->Flags = 0;
791 ResourceDescriptor->u.BusNumber.Start = addr32_data->Minimum;
792 ResourceDescriptor->u.BusNumber.Length = addr32_data->AddressLength;
793 }
794 else if (addr32_data->ResourceType == ACPI_IO_RANGE)
795 {
796 ResourceDescriptor->Type = CmResourceTypePort;
797 ResourceDescriptor->ShareDisposition = CmResourceShareDeviceExclusive;
798 ResourceDescriptor->Flags = CM_RESOURCE_PORT_IO;
799 if (addr32_data->Decode == ACPI_POS_DECODE)
800 ResourceDescriptor->Flags |= CM_RESOURCE_PORT_POSITIVE_DECODE;
801 ResourceDescriptor->u.Port.Start.QuadPart = addr32_data->Minimum;
802 ResourceDescriptor->u.Port.Length = addr32_data->AddressLength;
803 }
804 else
805 {
806 ResourceDescriptor->Type = CmResourceTypeMemory;
807 ResourceDescriptor->ShareDisposition = CmResourceShareDeviceExclusive;
808 ResourceDescriptor->Flags = 0;
809 if (addr32_data->Info.Mem.WriteProtect == ACPI_READ_ONLY_MEMORY)
810 ResourceDescriptor->Flags |= CM_RESOURCE_MEMORY_READ_ONLY;
811 else
812 ResourceDescriptor->Flags |= CM_RESOURCE_MEMORY_READ_WRITE;
813 switch (addr32_data->Info.Mem.Caching)
814 {
815 case ACPI_CACHABLE_MEMORY: ResourceDescriptor->Flags |= CM_RESOURCE_MEMORY_CACHEABLE; break;
816 case ACPI_WRITE_COMBINING_MEMORY: ResourceDescriptor->Flags |= CM_RESOURCE_MEMORY_COMBINEDWRITE; break;
817 case ACPI_PREFETCHABLE_MEMORY: ResourceDescriptor->Flags |= CM_RESOURCE_MEMORY_PREFETCHABLE; break;
818 }
819 ResourceDescriptor->u.Memory.Start.QuadPart = addr32_data->Minimum;
820 ResourceDescriptor->u.Memory.Length = addr32_data->AddressLength;
821 }
822 ResourceDescriptor++;
823 break;
824 }
825 case ACPI_RESOURCE_TYPE_ADDRESS64:
826 {
827 ACPI_RESOURCE_ADDRESS64 *addr64_data = (ACPI_RESOURCE_ADDRESS64*) &resource->Data;
828 if (addr64_data->ResourceType == ACPI_BUS_NUMBER_RANGE)
829 {
830 DPRINT1("64-bit bus address is not supported!\n");
831 ResourceDescriptor->Type = CmResourceTypeBusNumber;
832 ResourceDescriptor->ShareDisposition = CmResourceShareShared;
833 ResourceDescriptor->Flags = 0;
834 ResourceDescriptor->u.BusNumber.Start = (ULONG)addr64_data->Minimum;
835 ResourceDescriptor->u.BusNumber.Length = addr64_data->AddressLength;
836 }
837 else if (addr64_data->ResourceType == ACPI_IO_RANGE)
838 {
839 ResourceDescriptor->Type = CmResourceTypePort;
840 ResourceDescriptor->ShareDisposition = CmResourceShareDeviceExclusive;
841 ResourceDescriptor->Flags = CM_RESOURCE_PORT_IO;
842 if (addr64_data->Decode == ACPI_POS_DECODE)
843 ResourceDescriptor->Flags |= CM_RESOURCE_PORT_POSITIVE_DECODE;
844 ResourceDescriptor->u.Port.Start.QuadPart = addr64_data->Minimum;
845 ResourceDescriptor->u.Port.Length = addr64_data->AddressLength;
846 }
847 else
848 {
849 ResourceDescriptor->Type = CmResourceTypeMemory;
850 ResourceDescriptor->ShareDisposition = CmResourceShareDeviceExclusive;
851 ResourceDescriptor->Flags = 0;
852 if (addr64_data->Info.Mem.WriteProtect == ACPI_READ_ONLY_MEMORY)
853 ResourceDescriptor->Flags |= CM_RESOURCE_MEMORY_READ_ONLY;
854 else
855 ResourceDescriptor->Flags |= CM_RESOURCE_MEMORY_READ_WRITE;
856 switch (addr64_data->Info.Mem.Caching)
857 {
858 case ACPI_CACHABLE_MEMORY: ResourceDescriptor->Flags |= CM_RESOURCE_MEMORY_CACHEABLE; break;
859 case ACPI_WRITE_COMBINING_MEMORY: ResourceDescriptor->Flags |= CM_RESOURCE_MEMORY_COMBINEDWRITE; break;
860 case ACPI_PREFETCHABLE_MEMORY: ResourceDescriptor->Flags |= CM_RESOURCE_MEMORY_PREFETCHABLE; break;
861 }
862 ResourceDescriptor->u.Memory.Start.QuadPart = addr64_data->Minimum;
863 ResourceDescriptor->u.Memory.Length = addr64_data->AddressLength;
864 }
865 ResourceDescriptor++;
866 break;
867 }
868 case ACPI_RESOURCE_TYPE_MEMORY24:
869 {
870 ACPI_RESOURCE_MEMORY24 *mem24_data = (ACPI_RESOURCE_MEMORY24*) &resource->Data;
871 ResourceDescriptor->Type = CmResourceTypeMemory;
872 ResourceDescriptor->ShareDisposition = CmResourceShareDeviceExclusive;
873 ResourceDescriptor->Flags = CM_RESOURCE_MEMORY_24;
874 if (mem24_data->WriteProtect == ACPI_READ_ONLY_MEMORY)
875 ResourceDescriptor->Flags |= CM_RESOURCE_MEMORY_READ_ONLY;
876 else
877 ResourceDescriptor->Flags |= CM_RESOURCE_MEMORY_READ_WRITE;
878 ResourceDescriptor->u.Memory.Start.QuadPart = mem24_data->Minimum;
879 ResourceDescriptor->u.Memory.Length = mem24_data->AddressLength;
880
881 ResourceDescriptor++;
882 break;
883 }
884 case ACPI_RESOURCE_TYPE_MEMORY32:
885 {
886 ACPI_RESOURCE_MEMORY32 *mem32_data = (ACPI_RESOURCE_MEMORY32*) &resource->Data;
887 ResourceDescriptor->Type = CmResourceTypeMemory;
888 ResourceDescriptor->ShareDisposition = CmResourceShareDeviceExclusive;
889 ResourceDescriptor->Flags = 0;
890 if (mem32_data->WriteProtect == ACPI_READ_ONLY_MEMORY)
891 ResourceDescriptor->Flags |= CM_RESOURCE_MEMORY_READ_ONLY;
892 else
893 ResourceDescriptor->Flags |= CM_RESOURCE_MEMORY_READ_WRITE;
894 ResourceDescriptor->u.Memory.Start.QuadPart = mem32_data->Minimum;
895 ResourceDescriptor->u.Memory.Length = mem32_data->AddressLength;
896
897 ResourceDescriptor++;
898 break;
899 }
900 default:
901 {
902 break;
903 }
904 }
905 resource = ACPI_NEXT_RESOURCE(resource);
906 }
907
908 ExFreePool(Buffer.Pointer);
909 Irp->IoStatus.Information = (ULONG_PTR)ResourceList;
910 return STATUS_SUCCESS;
911 }
912
913 NTSTATUS
914 Bus_PDO_QueryResourceRequirements(
915 PPDO_DEVICE_DATA DeviceData,
916 PIRP Irp )
917 {
918 ULONG NumberOfResources = 0;
919 ACPI_STATUS AcpiStatus;
920 ACPI_BUFFER Buffer;
921 ACPI_RESOURCE* resource;
922 ULONG i, RequirementsListSize;
923 PIO_RESOURCE_REQUIREMENTS_LIST RequirementsList;
924 PIO_RESOURCE_DESCRIPTOR RequirementDescriptor;
925 BOOLEAN CurrentRes = FALSE;
926
927 PAGED_CODE ();
928
929
930 /* Get current resources */
931 while (TRUE)
932 {
933 Buffer.Length = 0;
934 if (CurrentRes)
935 AcpiStatus = AcpiGetCurrentResources(DeviceData->AcpiHandle, &Buffer);
936 else
937 AcpiStatus = AcpiGetPossibleResources(DeviceData->AcpiHandle, &Buffer);
938 if ((!ACPI_SUCCESS(AcpiStatus) && AcpiStatus != AE_BUFFER_OVERFLOW) ||
939 Buffer.Length == 0)
940 {
941 if (!CurrentRes)
942 CurrentRes = TRUE;
943 else
944 return Irp->IoStatus.Status;
945 }
946 else
947 break;
948 }
949
950 Buffer.Pointer = ExAllocatePool(PagedPool, Buffer.Length);
951 if (!Buffer.Pointer)
952 return STATUS_INSUFFICIENT_RESOURCES;
953
954 if (CurrentRes)
955 AcpiStatus = AcpiGetCurrentResources(DeviceData->AcpiHandle, &Buffer);
956 else
957 AcpiStatus = AcpiGetPossibleResources(DeviceData->AcpiHandle, &Buffer);
958 if (!ACPI_SUCCESS(AcpiStatus))
959 {
960 DPRINT1("AcpiGetCurrentResources #2 failed (0x%x)\n", AcpiStatus);
961 ASSERT(FALSE);
962 return STATUS_UNSUCCESSFUL;
963 }
964
965 resource= Buffer.Pointer;
966 /* Count number of resources */
967 while (resource->Type != ACPI_RESOURCE_TYPE_END_TAG)
968 {
969 switch (resource->Type)
970 {
971 case ACPI_RESOURCE_TYPE_IRQ:
972 {
973 ACPI_RESOURCE_IRQ *irq_data = (ACPI_RESOURCE_IRQ*) &resource->Data;
974 NumberOfResources += irq_data->InterruptCount;
975 break;
976 }
977 case ACPI_RESOURCE_TYPE_DMA:
978 {
979 ACPI_RESOURCE_DMA *dma_data = (ACPI_RESOURCE_DMA*) &resource->Data;
980 NumberOfResources += dma_data->ChannelCount;
981 break;
982 }
983 case ACPI_RESOURCE_TYPE_ADDRESS16:
984 case ACPI_RESOURCE_TYPE_ADDRESS32:
985 case ACPI_RESOURCE_TYPE_ADDRESS64:
986 case ACPI_RESOURCE_TYPE_MEMORY24:
987 case ACPI_RESOURCE_TYPE_MEMORY32:
988 case ACPI_RESOURCE_TYPE_IO:
989 {
990 NumberOfResources++;
991 break;
992 }
993 default:
994 {
995 break;
996 }
997 }
998 resource = ACPI_NEXT_RESOURCE(resource);
999 }
1000
1001 RequirementsListSize = sizeof(IO_RESOURCE_REQUIREMENTS_LIST) + sizeof(IO_RESOURCE_DESCRIPTOR) * (NumberOfResources - 1);
1002 RequirementsList = (PIO_RESOURCE_REQUIREMENTS_LIST)ExAllocatePool(PagedPool, RequirementsListSize);
1003
1004 if (!RequirementsList)
1005 {
1006 ExFreePool(Buffer.Pointer);
1007 return STATUS_INSUFFICIENT_RESOURCES;
1008 }
1009 RequirementsList->ListSize = RequirementsListSize;
1010 RequirementsList->InterfaceType = Internal;
1011 RequirementsList->BusNumber = 0;
1012 RequirementsList->SlotNumber = 0; /* Not used by WDM drivers */
1013 RequirementsList->AlternativeLists = 1;
1014 RequirementsList->List[0].Version = 1;
1015 RequirementsList->List[0].Revision = 1;
1016 RequirementsList->List[0].Count = NumberOfResources;
1017 RequirementDescriptor = RequirementsList->List[0].Descriptors;
1018
1019 /* Fill resources list structure */
1020 while (resource->Type != ACPI_RESOURCE_TYPE_END_TAG)
1021 {
1022 switch (resource->Type)
1023 {
1024 case ACPI_RESOURCE_TYPE_IRQ:
1025 {
1026 ACPI_RESOURCE_IRQ *irq_data = (ACPI_RESOURCE_IRQ*) &resource->Data;
1027 for (i = 0; i < irq_data->InterruptCount; i++)
1028 {
1029 RequirementDescriptor->Option = CurrentRes ? 0 : IO_RESOURCE_PREFERRED;
1030 RequirementDescriptor->Type = CmResourceTypeInterrupt;
1031 RequirementDescriptor->ShareDisposition = (irq_data->Sharable == ACPI_SHARED ? CmResourceShareShared : CmResourceShareDeviceExclusive);
1032 RequirementDescriptor->Flags =(irq_data->Triggering == ACPI_LEVEL_SENSITIVE ? CM_RESOURCE_INTERRUPT_LEVEL_SENSITIVE : CM_RESOURCE_INTERRUPT_LATCHED);
1033 RequirementDescriptor->u.Interrupt.MinimumVector =
1034 RequirementDescriptor->u.Interrupt.MaximumVector = irq_data->Interrupts[i];
1035
1036 RequirementDescriptor++;
1037 }
1038 break;
1039 }
1040 case ACPI_RESOURCE_TYPE_DMA:
1041 {
1042 ACPI_RESOURCE_DMA *dma_data = (ACPI_RESOURCE_DMA*) &resource->Data;
1043 for (i = 0; i < dma_data->ChannelCount; i++)
1044 {
1045 RequirementDescriptor->Type = CmResourceTypeDma;
1046 RequirementDescriptor->Flags = 0;
1047 switch (dma_data->Type)
1048 {
1049 case ACPI_TYPE_A: RequirementDescriptor->Flags |= CM_RESOURCE_DMA_TYPE_A; break;
1050 case ACPI_TYPE_B: RequirementDescriptor->Flags |= CM_RESOURCE_DMA_TYPE_B; break;
1051 case ACPI_TYPE_F: RequirementDescriptor->Flags |= CM_RESOURCE_DMA_TYPE_F; break;
1052 }
1053 if (dma_data->BusMaster == ACPI_BUS_MASTER)
1054 RequirementDescriptor->Flags |= CM_RESOURCE_DMA_BUS_MASTER;
1055 switch (dma_data->Transfer)
1056 {
1057 case ACPI_TRANSFER_8: RequirementDescriptor->Flags |= CM_RESOURCE_DMA_8; break;
1058 case ACPI_TRANSFER_16: RequirementDescriptor->Flags |= CM_RESOURCE_DMA_16; break;
1059 case ACPI_TRANSFER_8_16: RequirementDescriptor->Flags |= CM_RESOURCE_DMA_8_AND_16; break;
1060 }
1061
1062 RequirementDescriptor->Option = CurrentRes ? 0 : IO_RESOURCE_PREFERRED;
1063 RequirementDescriptor->ShareDisposition = CmResourceShareDriverExclusive;
1064 RequirementDescriptor->u.Dma.MinimumChannel =
1065 RequirementDescriptor->u.Dma.MaximumChannel = dma_data->Channels[i];
1066 RequirementDescriptor++;
1067 }
1068 break;
1069 }
1070 case ACPI_RESOURCE_TYPE_IO:
1071 {
1072 ACPI_RESOURCE_IO *io_data = (ACPI_RESOURCE_IO*) &resource->Data;
1073 RequirementDescriptor->Flags = CM_RESOURCE_PORT_IO;
1074 if (io_data->IoDecode == ACPI_DECODE_16)
1075 RequirementDescriptor->Flags |= CM_RESOURCE_PORT_16_BIT_DECODE;
1076 else
1077 RequirementDescriptor->Flags |= CM_RESOURCE_PORT_10_BIT_DECODE;
1078 RequirementDescriptor->u.Port.Length = io_data->AddressLength;
1079 RequirementDescriptor->Option = CurrentRes ? 0 : IO_RESOURCE_PREFERRED;
1080 RequirementDescriptor->Type = CmResourceTypePort;
1081 RequirementDescriptor->ShareDisposition = CmResourceShareDriverExclusive;
1082 RequirementDescriptor->u.Port.Alignment = io_data->Alignment;
1083 RequirementDescriptor->u.Port.MinimumAddress.QuadPart = io_data->Minimum;
1084 RequirementDescriptor->u.Port.MaximumAddress.QuadPart = io_data->Maximum;
1085
1086 RequirementDescriptor++;
1087 break;
1088 }
1089 case ACPI_RESOURCE_TYPE_ADDRESS16:
1090 {
1091 ACPI_RESOURCE_ADDRESS16 *addr16_data = (ACPI_RESOURCE_ADDRESS16*) &resource->Data;
1092 RequirementDescriptor->Option = CurrentRes ? 0 : IO_RESOURCE_PREFERRED;
1093 if (addr16_data->ResourceType == ACPI_BUS_NUMBER_RANGE)
1094 {
1095 RequirementDescriptor->Type = CmResourceTypeBusNumber;
1096 RequirementDescriptor->ShareDisposition = CmResourceShareShared;
1097 RequirementDescriptor->Flags = 0;
1098 RequirementDescriptor->u.BusNumber.MinBusNumber = addr16_data->Minimum;
1099 RequirementDescriptor->u.BusNumber.MaxBusNumber = addr16_data->Maximum;
1100 RequirementDescriptor->u.BusNumber.Length = addr16_data->AddressLength;
1101 }
1102 else if (addr16_data->ResourceType == ACPI_IO_RANGE)
1103 {
1104 RequirementDescriptor->Type = CmResourceTypePort;
1105 RequirementDescriptor->ShareDisposition = CmResourceShareDeviceExclusive;
1106 RequirementDescriptor->Flags = CM_RESOURCE_PORT_IO;
1107 if (addr16_data->Decode == ACPI_POS_DECODE)
1108 RequirementDescriptor->Flags |= CM_RESOURCE_PORT_POSITIVE_DECODE;
1109 RequirementDescriptor->u.Port.MinimumAddress.QuadPart = addr16_data->Minimum;
1110 RequirementDescriptor->u.Port.MaximumAddress.QuadPart = addr16_data->Maximum;
1111 RequirementDescriptor->u.Port.Length = addr16_data->AddressLength;
1112 }
1113 else
1114 {
1115 RequirementDescriptor->Type = CmResourceTypeMemory;
1116 RequirementDescriptor->ShareDisposition = CmResourceShareDeviceExclusive;
1117 RequirementDescriptor->Flags = 0;
1118 if (addr16_data->Info.Mem.WriteProtect == ACPI_READ_ONLY_MEMORY)
1119 RequirementDescriptor->Flags |= CM_RESOURCE_MEMORY_READ_ONLY;
1120 else
1121 RequirementDescriptor->Flags |= CM_RESOURCE_MEMORY_READ_WRITE;
1122 switch (addr16_data->Info.Mem.Caching)
1123 {
1124 case ACPI_CACHABLE_MEMORY: RequirementDescriptor->Flags |= CM_RESOURCE_MEMORY_CACHEABLE; break;
1125 case ACPI_WRITE_COMBINING_MEMORY: RequirementDescriptor->Flags |= CM_RESOURCE_MEMORY_COMBINEDWRITE; break;
1126 case ACPI_PREFETCHABLE_MEMORY: RequirementDescriptor->Flags |= CM_RESOURCE_MEMORY_PREFETCHABLE; break;
1127 }
1128 RequirementDescriptor->u.Memory.MinimumAddress.QuadPart = addr16_data->Minimum;
1129 RequirementDescriptor->u.Memory.MaximumAddress.QuadPart = addr16_data->Maximum;
1130 RequirementDescriptor->u.Memory.Length = addr16_data->AddressLength;
1131 }
1132 RequirementDescriptor++;
1133 break;
1134 }
1135 case ACPI_RESOURCE_TYPE_ADDRESS32:
1136 {
1137 ACPI_RESOURCE_ADDRESS32 *addr32_data = (ACPI_RESOURCE_ADDRESS32*) &resource->Data;
1138 RequirementDescriptor->Option = CurrentRes ? 0 : IO_RESOURCE_PREFERRED;
1139 if (addr32_data->ResourceType == ACPI_BUS_NUMBER_RANGE)
1140 {
1141 RequirementDescriptor->Type = CmResourceTypeBusNumber;
1142 RequirementDescriptor->ShareDisposition = CmResourceShareShared;
1143 RequirementDescriptor->Flags = 0;
1144 RequirementDescriptor->u.BusNumber.MinBusNumber = addr32_data->Minimum;
1145 RequirementDescriptor->u.BusNumber.MaxBusNumber = addr32_data->Maximum;
1146 RequirementDescriptor->u.BusNumber.Length = addr32_data->AddressLength;
1147 }
1148 else if (addr32_data->ResourceType == ACPI_IO_RANGE)
1149 {
1150 RequirementDescriptor->Type = CmResourceTypePort;
1151 RequirementDescriptor->ShareDisposition = CmResourceShareDeviceExclusive;
1152 RequirementDescriptor->Flags = CM_RESOURCE_PORT_IO;
1153 if (addr32_data->Decode == ACPI_POS_DECODE)
1154 RequirementDescriptor->Flags |= CM_RESOURCE_PORT_POSITIVE_DECODE;
1155 RequirementDescriptor->u.Port.MinimumAddress.QuadPart = addr32_data->Minimum;
1156 RequirementDescriptor->u.Port.MaximumAddress.QuadPart = addr32_data->Maximum;
1157 RequirementDescriptor->u.Port.Length = addr32_data->AddressLength;
1158 }
1159 else
1160 {
1161 RequirementDescriptor->Type = CmResourceTypeMemory;
1162 RequirementDescriptor->ShareDisposition = CmResourceShareDeviceExclusive;
1163 RequirementDescriptor->Flags = 0;
1164 if (addr32_data->Info.Mem.WriteProtect == ACPI_READ_ONLY_MEMORY)
1165 RequirementDescriptor->Flags |= CM_RESOURCE_MEMORY_READ_ONLY;
1166 else
1167 RequirementDescriptor->Flags |= CM_RESOURCE_MEMORY_READ_WRITE;
1168 switch (addr32_data->Info.Mem.Caching)
1169 {
1170 case ACPI_CACHABLE_MEMORY: RequirementDescriptor->Flags |= CM_RESOURCE_MEMORY_CACHEABLE; break;
1171 case ACPI_WRITE_COMBINING_MEMORY: RequirementDescriptor->Flags |= CM_RESOURCE_MEMORY_COMBINEDWRITE; break;
1172 case ACPI_PREFETCHABLE_MEMORY: RequirementDescriptor->Flags |= CM_RESOURCE_MEMORY_PREFETCHABLE; break;
1173 }
1174 RequirementDescriptor->u.Memory.MinimumAddress.QuadPart = addr32_data->Minimum;
1175 RequirementDescriptor->u.Memory.MaximumAddress.QuadPart = addr32_data->Maximum;
1176 RequirementDescriptor->u.Memory.Length = addr32_data->AddressLength;
1177 }
1178 RequirementDescriptor++;
1179 break;
1180 }
1181 case ACPI_RESOURCE_TYPE_ADDRESS64:
1182 {
1183 ACPI_RESOURCE_ADDRESS64 *addr64_data = (ACPI_RESOURCE_ADDRESS64*) &resource->Data;
1184 RequirementDescriptor->Option = CurrentRes ? 0 : IO_RESOURCE_PREFERRED;
1185 if (addr64_data->ResourceType == ACPI_BUS_NUMBER_RANGE)
1186 {
1187 DPRINT1("64-bit bus address is not supported!\n");
1188 RequirementDescriptor->Type = CmResourceTypeBusNumber;
1189 RequirementDescriptor->ShareDisposition = CmResourceShareShared;
1190 RequirementDescriptor->Flags = 0;
1191 RequirementDescriptor->u.BusNumber.MinBusNumber = (ULONG)addr64_data->Minimum;
1192 RequirementDescriptor->u.BusNumber.MaxBusNumber = (ULONG)addr64_data->Maximum;
1193 RequirementDescriptor->u.BusNumber.Length = addr64_data->AddressLength;
1194 }
1195 else if (addr64_data->ResourceType == ACPI_IO_RANGE)
1196 {
1197 RequirementDescriptor->Type = CmResourceTypePort;
1198 RequirementDescriptor->ShareDisposition = CmResourceShareDeviceExclusive;
1199 RequirementDescriptor->Flags = CM_RESOURCE_PORT_IO;
1200 if (addr64_data->Decode == ACPI_POS_DECODE)
1201 RequirementDescriptor->Flags |= CM_RESOURCE_PORT_POSITIVE_DECODE;
1202 RequirementDescriptor->u.Port.MinimumAddress.QuadPart = addr64_data->Minimum;
1203 RequirementDescriptor->u.Port.MaximumAddress.QuadPart = addr64_data->Maximum;
1204 RequirementDescriptor->u.Port.Length = addr64_data->AddressLength;
1205 }
1206 else
1207 {
1208 RequirementDescriptor->Type = CmResourceTypeMemory;
1209 RequirementDescriptor->ShareDisposition = CmResourceShareDeviceExclusive;
1210 RequirementDescriptor->Flags = 0;
1211 if (addr64_data->Info.Mem.WriteProtect == ACPI_READ_ONLY_MEMORY)
1212 RequirementDescriptor->Flags |= CM_RESOURCE_MEMORY_READ_ONLY;
1213 else
1214 RequirementDescriptor->Flags |= CM_RESOURCE_MEMORY_READ_WRITE;
1215 switch (addr64_data->Info.Mem.Caching)
1216 {
1217 case ACPI_CACHABLE_MEMORY: RequirementDescriptor->Flags |= CM_RESOURCE_MEMORY_CACHEABLE; break;
1218 case ACPI_WRITE_COMBINING_MEMORY: RequirementDescriptor->Flags |= CM_RESOURCE_MEMORY_COMBINEDWRITE; break;
1219 case ACPI_PREFETCHABLE_MEMORY: RequirementDescriptor->Flags |= CM_RESOURCE_MEMORY_PREFETCHABLE; break;
1220 }
1221 RequirementDescriptor->u.Memory.MinimumAddress.QuadPart = addr64_data->Minimum;
1222 RequirementDescriptor->u.Memory.MaximumAddress.QuadPart = addr64_data->Maximum;
1223 RequirementDescriptor->u.Memory.Length = addr64_data->AddressLength;
1224 }
1225 RequirementDescriptor++;
1226 break;
1227 }
1228 case ACPI_RESOURCE_TYPE_MEMORY24:
1229 {
1230 ACPI_RESOURCE_MEMORY24 *mem24_data = (ACPI_RESOURCE_MEMORY24*) &resource->Data;
1231 RequirementDescriptor->Option = CurrentRes ? 0 : IO_RESOURCE_PREFERRED;
1232 RequirementDescriptor->Type = CmResourceTypeMemory;
1233 RequirementDescriptor->ShareDisposition = CmResourceShareDeviceExclusive;
1234 RequirementDescriptor->Flags = CM_RESOURCE_MEMORY_24;
1235 if (mem24_data->WriteProtect == ACPI_READ_ONLY_MEMORY)
1236 RequirementDescriptor->Flags |= CM_RESOURCE_MEMORY_READ_ONLY;
1237 else
1238 RequirementDescriptor->Flags |= CM_RESOURCE_MEMORY_READ_WRITE;
1239 RequirementDescriptor->u.Memory.MinimumAddress.QuadPart = mem24_data->Minimum;
1240 RequirementDescriptor->u.Memory.MaximumAddress.QuadPart = mem24_data->Maximum;
1241 RequirementDescriptor->u.Memory.Length = mem24_data->AddressLength;
1242
1243 RequirementDescriptor++;
1244 break;
1245 }
1246 case ACPI_RESOURCE_TYPE_MEMORY32:
1247 {
1248 ACPI_RESOURCE_MEMORY32 *mem32_data = (ACPI_RESOURCE_MEMORY32*) &resource->Data;
1249 RequirementDescriptor->Option = CurrentRes ? 0 : IO_RESOURCE_PREFERRED;
1250 RequirementDescriptor->Type = CmResourceTypeMemory;
1251 RequirementDescriptor->ShareDisposition = CmResourceShareDeviceExclusive;
1252 RequirementDescriptor->Flags = 0;
1253 if (mem32_data->WriteProtect == ACPI_READ_ONLY_MEMORY)
1254 RequirementDescriptor->Flags |= CM_RESOURCE_MEMORY_READ_ONLY;
1255 else
1256 RequirementDescriptor->Flags |= CM_RESOURCE_MEMORY_READ_WRITE;
1257 RequirementDescriptor->u.Memory.MinimumAddress.QuadPart = mem32_data->Minimum;
1258 RequirementDescriptor->u.Memory.MaximumAddress.QuadPart = mem32_data->Maximum;
1259 RequirementDescriptor->u.Memory.Length = mem32_data->AddressLength;
1260
1261 RequirementDescriptor++;
1262 break;
1263 }
1264 default:
1265 {
1266 break;
1267 }
1268 }
1269 resource = ACPI_NEXT_RESOURCE(resource);
1270 }
1271 ExFreePool(Buffer.Pointer);
1272
1273 Irp->IoStatus.Information = (ULONG_PTR)RequirementsList;
1274
1275 return STATUS_SUCCESS;
1276 }
1277
1278 NTSTATUS
1279 Bus_PDO_QueryDeviceRelations(
1280 PPDO_DEVICE_DATA DeviceData,
1281 PIRP Irp )
1282 /*++
1283
1284 Routine Description:
1285
1286 The PnP Manager sends this IRP to gather information about
1287 devices with a relationship to the specified device.
1288 Bus drivers must handle this request for TargetDeviceRelation
1289 for their child devices (child PDOs).
1290
1291 If a driver returns relations in response to this IRP,
1292 it allocates a DEVICE_RELATIONS structure from paged
1293 memory containing a count and the appropriate number of
1294 device object pointers. The PnP Manager frees the structure
1295 when it is no longer needed. If a driver replaces a
1296 DEVICE_RELATIONS structure allocated by another driver,
1297 it must free the previous structure.
1298
1299 A driver must reference the PDO of any device that it
1300 reports in this IRP (ObReferenceObject). The PnP Manager
1301 removes the reference when appropriate.
1302
1303 Arguments:
1304
1305 DeviceData - Pointer to the PDO's device extension.
1306 Irp - Pointer to the irp.
1307
1308 Return Value:
1309
1310 NT STATUS
1311
1312 --*/
1313 {
1314
1315 PIO_STACK_LOCATION stack;
1316 PDEVICE_RELATIONS deviceRelations;
1317 NTSTATUS status;
1318
1319 PAGED_CODE ();
1320
1321 stack = IoGetCurrentIrpStackLocation (Irp);
1322
1323 switch (stack->Parameters.QueryDeviceRelations.Type) {
1324
1325 case TargetDeviceRelation:
1326
1327 deviceRelations = (PDEVICE_RELATIONS) Irp->IoStatus.Information;
1328 if (deviceRelations) {
1329 //
1330 // Only PDO can handle this request. Somebody above
1331 // is not playing by rule.
1332 //
1333 ASSERTMSG("Someone above is handling TargetDeviceRelation", !deviceRelations);
1334 }
1335
1336 deviceRelations = (PDEVICE_RELATIONS)
1337 ExAllocatePoolWithTag (PagedPool,
1338 sizeof(DEVICE_RELATIONS),
1339 'IPCA');
1340 if (!deviceRelations) {
1341 status = STATUS_INSUFFICIENT_RESOURCES;
1342 break;
1343 }
1344
1345 //
1346 // There is only one PDO pointer in the structure
1347 // for this relation type. The PnP Manager removes
1348 // the reference to the PDO when the driver or application
1349 // un-registers for notification on the device.
1350 //
1351
1352 deviceRelations->Count = 1;
1353 deviceRelations->Objects[0] = DeviceData->Common.Self;
1354 ObReferenceObject(DeviceData->Common.Self);
1355
1356 status = STATUS_SUCCESS;
1357 Irp->IoStatus.Information = (ULONG_PTR) deviceRelations;
1358 break;
1359
1360 case BusRelations: // Not handled by PDO
1361 case EjectionRelations: // optional for PDO
1362 case RemovalRelations: // // optional for PDO
1363 default:
1364 status = Irp->IoStatus.Status;
1365 }
1366
1367 return status;
1368 }
1369
1370 NTSTATUS
1371 Bus_PDO_QueryBusInformation(
1372 PPDO_DEVICE_DATA DeviceData,
1373 PIRP Irp )
1374 /*++
1375
1376 Routine Description:
1377
1378 The PnP Manager uses this IRP to request the type and
1379 instance number of a device's parent bus. Bus drivers
1380 should handle this request for their child devices (PDOs).
1381
1382 Arguments:
1383
1384 DeviceData - Pointer to the PDO's device extension.
1385 Irp - Pointer to the irp.
1386
1387 Return Value:
1388
1389 NT STATUS
1390
1391 --*/
1392 {
1393
1394 PPNP_BUS_INFORMATION busInfo;
1395
1396 PAGED_CODE ();
1397
1398 busInfo = ExAllocatePoolWithTag (PagedPool, sizeof(PNP_BUS_INFORMATION),
1399 'IPCA');
1400
1401 if (busInfo == NULL) {
1402 return STATUS_INSUFFICIENT_RESOURCES;
1403 }
1404
1405 busInfo->BusTypeGuid = GUID_ACPI_INTERFACE_STANDARD;
1406
1407 busInfo->LegacyBusType = InternalPowerBus;
1408
1409 busInfo->BusNumber = 0; //fixme
1410
1411 Irp->IoStatus.Information = (ULONG_PTR)busInfo;
1412
1413 return STATUS_SUCCESS;
1414 }
1415
1416
1417 NTSTATUS
1418 Bus_GetDeviceCapabilities(
1419 PDEVICE_OBJECT DeviceObject,
1420 PDEVICE_CAPABILITIES DeviceCapabilities
1421 )
1422 {
1423 IO_STATUS_BLOCK ioStatus;
1424 KEVENT pnpEvent;
1425 NTSTATUS status;
1426 PDEVICE_OBJECT targetObject;
1427 PIO_STACK_LOCATION irpStack;
1428 PIRP pnpIrp;
1429
1430 PAGED_CODE();
1431
1432 //
1433 // Initialize the capabilities that we will send down
1434 //
1435 RtlZeroMemory( DeviceCapabilities, sizeof(DEVICE_CAPABILITIES) );
1436 DeviceCapabilities->Size = sizeof(DEVICE_CAPABILITIES);
1437 DeviceCapabilities->Version = 1;
1438 DeviceCapabilities->Address = -1;
1439 DeviceCapabilities->UINumber = -1;
1440
1441 //
1442 // Initialize the event
1443 //
1444 KeInitializeEvent( &pnpEvent, NotificationEvent, FALSE );
1445
1446 targetObject = IoGetAttachedDeviceReference( DeviceObject );
1447
1448 //
1449 // Build an Irp
1450 //
1451 pnpIrp = IoBuildSynchronousFsdRequest(
1452 IRP_MJ_PNP,
1453 targetObject,
1454 NULL,
1455 0,
1456 NULL,
1457 &pnpEvent,
1458 &ioStatus
1459 );
1460 if (pnpIrp == NULL) {
1461
1462 status = STATUS_INSUFFICIENT_RESOURCES;
1463 goto GetDeviceCapabilitiesExit;
1464
1465 }
1466
1467 //
1468 // Pnp Irps all begin life as STATUS_NOT_SUPPORTED;
1469 //
1470 pnpIrp->IoStatus.Status = STATUS_NOT_SUPPORTED;
1471
1472 //
1473 // Get the top of stack
1474 //
1475 irpStack = IoGetNextIrpStackLocation( pnpIrp );
1476
1477 //
1478 // Set the top of stack
1479 //
1480 RtlZeroMemory( irpStack, sizeof(IO_STACK_LOCATION ) );
1481 irpStack->MajorFunction = IRP_MJ_PNP;
1482 irpStack->MinorFunction = IRP_MN_QUERY_CAPABILITIES;
1483 irpStack->Parameters.DeviceCapabilities.Capabilities = DeviceCapabilities;
1484
1485 //
1486 // Call the driver
1487 //
1488 status = IoCallDriver( targetObject, pnpIrp );
1489 if (status == STATUS_PENDING) {
1490
1491 //
1492 // Block until the irp comes back.
1493 // Important thing to note here is when you allocate
1494 // the memory for an event in the stack you must do a
1495 // KernelMode wait instead of UserMode to prevent
1496 // the stack from getting paged out.
1497 //
1498
1499 KeWaitForSingleObject(
1500 &pnpEvent,
1501 Executive,
1502 KernelMode,
1503 FALSE,
1504 NULL
1505 );
1506 status = ioStatus.Status;
1507
1508 }
1509
1510 GetDeviceCapabilitiesExit:
1511 //
1512 // Done with reference
1513 //
1514 ObDereferenceObject( targetObject );
1515
1516 //
1517 // Done
1518 //
1519 return status;
1520
1521 }
1522
1523