2 * acpi_bus.c - ACPI Bus Driver ($Revision: 80 $)
4 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or (at
11 * your option) any later version.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26 * Modified for ReactOS and latest ACPICA
27 * Copyright (C)2009 Samuel Serapion
34 #include <acpi_drivers.h>
40 #define _COMPONENT ACPI_BUS_COMPONENT
41 ACPI_MODULE_NAME ("acpi_bus")
46 #define STRUCT_TO_INT(s) (*((int*)&s))
47 #define HAS_CHILDREN(d) ((d)->children.next != &((d)->children))
48 #define HAS_SIBLINGS(d) (((d)->parent) && ((d)->node.next != &(d)->parent->children))
49 #define NODE_TO_DEVICE(n) (list_entry(n, struct acpi_device, node))
52 extern void acpi_pic_sci_set_trigger(unsigned int irq
, UINT16 trigger
);
54 typedef int (*acpi_bus_walk_callback
)(struct acpi_device
*, int, void*);
56 struct acpi_device
*acpi_root
;
57 KSPIN_LOCK acpi_bus_event_lock
;
58 LIST_HEAD(acpi_bus_event_list
);
59 //DECLARE_WAIT_QUEUE_HEAD(acpi_bus_event_queue);
60 KEVENT AcpiEventQueue
;
65 acpi_device_register (
66 struct acpi_device
*device
,
67 struct acpi_device
*parent
)
72 return_VALUE(AE_BAD_PARAMETER
);
79 acpi_device_unregister (
80 struct acpi_device
*device
)
83 return_VALUE(AE_BAD_PARAMETER
);
86 put_device(&device
->dev
);
93 /* --------------------------------------------------------------------------
95 -------------------------------------------------------------------------- */
98 acpi_bus_data_handler (
102 DPRINT1("acpi_bus_data_handler not implemented");
111 acpi_bus_get_device (
113 struct acpi_device
**device
)
115 ACPI_STATUS status
= AE_OK
;
118 return_VALUE(AE_BAD_PARAMETER
);
120 /* TBD: Support fixed-feature devices */
122 status
= AcpiGetData(handle
, acpi_bus_data_handler
, (void**)device
);
123 if (ACPI_FAILURE(status
) || !*device
) {
124 DPRINT( "Error getting context for object [%p]\n",
126 return_VALUE(AE_NOT_FOUND
);
132 ACPI_STATUS
acpi_bus_get_status_handle(ACPI_HANDLE handle
,
133 unsigned long long *sta
)
137 status
= acpi_evaluate_integer(handle
, "_STA", NULL
, sta
);
138 if (ACPI_SUCCESS(status
))
141 if (status
== AE_NOT_FOUND
) {
142 *sta
= ACPI_STA_DEVICE_PRESENT
| ACPI_STA_DEVICE_ENABLED
|
143 ACPI_STA_DEVICE_UI
| ACPI_STA_DEVICE_FUNCTIONING
;
150 acpi_bus_get_status (
151 struct acpi_device
*device
)
154 unsigned long long sta
;
156 status
= acpi_bus_get_status_handle(device
->handle
, &sta
);
157 if (ACPI_FAILURE(status
))
160 STRUCT_TO_INT(device
->status
) = (int) sta
;
162 if (device
->status
.functional
&& !device
->status
.present
) {
163 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Device [%s] status [%08x]: "
164 "functional but not present;\n",
166 (UINT32
) STRUCT_TO_INT(device
->status
)));
169 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Device [%s] status [%08x]\n",
171 (UINT32
) STRUCT_TO_INT(device
->status
)));
175 void acpi_bus_private_data_handler(ACPI_HANDLE handle
,
181 int acpi_bus_get_private_data(ACPI_HANDLE handle
, void **data
)
183 ACPI_STATUS status
= AE_OK
;
188 status
= AcpiGetData(handle
, acpi_bus_private_data_handler
, data
);
189 if (ACPI_FAILURE(status
) || !*data
) {
190 DPRINT("No context for object [%p]\n", handle
);
196 /* --------------------------------------------------------------------------
198 -------------------------------------------------------------------------- */
206 ACPI_STATUS status
= 0;
207 struct acpi_device
*device
= NULL
;
208 unsigned long long psc
= 0;
210 result
= acpi_bus_get_device(handle
, &device
);
212 return_VALUE(result
);
214 *state
= ACPI_STATE_UNKNOWN
;
216 if (!device
->flags
.power_manageable
) {
217 /* TBD: Non-recursive algorithm for walking up hierarchy */
219 *state
= device
->parent
->power
.state
;
221 *state
= ACPI_STATE_D0
;
225 * Get the device's power state either directly (via _PSC) or
226 * indirectly (via power resources).
228 if (device
->power
.flags
.explicit_get
) {
229 status
= acpi_evaluate_integer(device
->handle
, "_PSC",
231 if (ACPI_FAILURE(status
))
232 return_VALUE(AE_NOT_FOUND
);
233 device
->power
.state
= (int) psc
;
235 else if (device
->power
.flags
.power_resources
) {
236 result
= acpi_power_get_inferred_state(device
);
238 return_VALUE(result
);
241 *state
= device
->power
.state
;
244 DPRINT("Device [%s] power state is D%d\n",
245 device
->pnp
.bus_id
, device
->power
.state
);
257 ACPI_STATUS status
= AE_OK
;
258 struct acpi_device
*device
= NULL
;
259 char object_name
[5] = {'_','P','S','0'+state
,'\0'};
262 result
= acpi_bus_get_device(handle
, &device
);
264 return_VALUE(result
);
266 if ((state
< ACPI_STATE_D0
) || (state
> ACPI_STATE_D3
))
267 return_VALUE(AE_BAD_PARAMETER
);
269 /* Make sure this is a valid target state */
271 if (!device
->flags
.power_manageable
) {
272 DPRINT1( "Device is not power manageable\n");
273 return_VALUE(AE_NOT_FOUND
);
276 * Get device's current power state
278 //if (!acpi_power_nocheck) {
280 * Maybe the incorrect power state is returned on the bogus
281 * bios, which is different with the real power state.
282 * For example: the bios returns D0 state and the real power
283 * state is D3. OS expects to set the device to D0 state. In
284 * such case if OS uses the power state returned by the BIOS,
285 * the device can't be transisted to the correct power state.
286 * So if the acpi_power_nocheck is set, it is unnecessary to
287 * get the power state by calling acpi_bus_get_power.
289 acpi_bus_get_power(device
->handle
, &device
->power
.state
);
292 if ((state
== device
->power
.state
) && !device
->flags
.force_power_state
) {
293 DPRINT1("Device is already at D%d\n", state
);
296 if (!device
->power
.states
[state
].flags
.valid
) {
297 DPRINT1( "Device does not support D%d\n", state
);
300 if (device
->parent
&& (state
< device
->parent
->power
.state
)) {
301 DPRINT1( "Cannot set device to a higher-powered state than parent\n");
308 * On transitions to a high-powered state we first apply power (via
309 * power resources) then evalute _PSx. Conversly for transitions to
310 * a lower-powered state.
312 if (state
< device
->power
.state
) {
313 if (device
->power
.flags
.power_resources
) {
314 result
= acpi_power_transition(device
, state
);
318 if (device
->power
.states
[state
].flags
.explicit_set
) {
319 status
= AcpiEvaluateObject(device
->handle
,
320 object_name
, NULL
, NULL
);
321 if (ACPI_FAILURE(status
)) {
322 result
= AE_NOT_FOUND
;
328 if (device
->power
.states
[state
].flags
.explicit_set
) {
329 status
= AcpiEvaluateObject(device
->handle
,
330 object_name
, NULL
, NULL
);
331 if (ACPI_FAILURE(status
)) {
332 result
= AE_NOT_FOUND
;
336 if (device
->power
.flags
.power_resources
) {
337 result
= acpi_power_transition(device
, state
);
345 DPRINT( "Error transitioning device [%s] to D%d\n",
346 device
->pnp
.bus_id
, state
);
348 DPRINT("Device [%s] transitioned to D%d\n",
349 device
->pnp
.bus_id
, state
);
354 BOOLEAN
acpi_bus_power_manageable(ACPI_HANDLE handle
)
356 struct acpi_device
*device
;
359 result
= acpi_bus_get_device(handle
, &device
);
360 return result
? 0 : device
->flags
.power_manageable
;
363 BOOLEAN
acpi_bus_can_wakeup(ACPI_HANDLE handle
)
365 struct acpi_device
*device
;
368 result
= acpi_bus_get_device(handle
, &device
);
369 return result
? 0 : device
->wakeup
.flags
.valid
;
373 acpi_bus_get_power_flags (
374 struct acpi_device
*device
)
376 ACPI_STATUS status
= 0;
377 ACPI_HANDLE handle
= 0;
384 * Power Management Flags
386 status
= AcpiGetHandle(device
->handle
, "_PSC", &handle
);
387 if (ACPI_SUCCESS(status
))
388 device
->power
.flags
.explicit_get
= 1;
389 status
= AcpiGetHandle(device
->handle
, "_IRC", &handle
);
390 if (ACPI_SUCCESS(status
))
391 device
->power
.flags
.inrush_current
= 1;
392 status
= AcpiGetHandle(device
->handle
, "_PRW", &handle
);
393 if (ACPI_SUCCESS(status
))
394 device
->flags
.wake_capable
= 1;
397 * Enumerate supported power management states
399 for (i
= ACPI_STATE_D0
; i
<= ACPI_STATE_D3
; i
++) {
400 struct acpi_device_power_state
*ps
= &device
->power
.states
[i
];
401 char object_name
[5] = {'_','P','R','0'+i
,'\0'};
403 /* Evaluate "_PRx" to se if power resources are referenced */
404 acpi_evaluate_reference(device
->handle
, object_name
, NULL
,
406 if (ps
->resources
.count
) {
407 device
->power
.flags
.power_resources
= 1;
411 /* Evaluate "_PSx" to see if we can do explicit sets */
412 object_name
[2] = 'S';
413 status
= AcpiGetHandle(device
->handle
, object_name
, &handle
);
414 if (ACPI_SUCCESS(status
)) {
415 ps
->flags
.explicit_set
= 1;
419 /* State is valid if we have some power control */
420 if (ps
->resources
.count
|| ps
->flags
.explicit_set
)
423 ps
->power
= -1; /* Unknown - driver assigned */
424 ps
->latency
= -1; /* Unknown - driver assigned */
427 /* Set defaults for D0 and D3 states (always valid) */
428 device
->power
.states
[ACPI_STATE_D0
].flags
.valid
= 1;
429 device
->power
.states
[ACPI_STATE_D0
].power
= 100;
430 device
->power
.states
[ACPI_STATE_D3
].flags
.valid
= 1;
431 device
->power
.states
[ACPI_STATE_D3
].power
= 0;
433 device
->power
.state
= ACPI_STATE_UNKNOWN
;
438 /* --------------------------------------------------------------------------
439 Performance Management
440 -------------------------------------------------------------------------- */
443 acpi_bus_get_perf_flags (
444 struct acpi_device
*device
)
449 device
->performance
.state
= ACPI_STATE_UNKNOWN
;
455 /* --------------------------------------------------------------------------
457 -------------------------------------------------------------------------- */
461 acpi_bus_generate_event_dpc(PKDPC Dpc
,
462 PVOID DeferredContext
,
463 PVOID SystemArgument1
,
464 PVOID SystemArgument2
)
466 struct acpi_bus_event
*event
;
467 struct acpi_device
*device
= SystemArgument1
;
468 ULONG_PTR TypeData
= (ULONG_PTR
)SystemArgument2
;
471 event
= ExAllocatePool(NonPagedPool
,sizeof(struct acpi_bus_event
));
475 sprintf(event
->device_class
, "%s", device
->pnp
.device_class
);
476 sprintf(event
->bus_id
, "%s", device
->pnp
.bus_id
);
477 event
->type
= (TypeData
& 0xFF000000) >> 24;
478 event
->data
= (TypeData
& 0x00FFFFFF);
480 KeAcquireSpinLock(&acpi_bus_event_lock
, &OldIrql
);
481 list_add_tail(&event
->node
, &acpi_bus_event_list
);
482 KeReleaseSpinLock(&acpi_bus_event_lock
, OldIrql
);
484 KeSetEvent(&AcpiEventQueue
, IO_NO_INCREMENT
, FALSE
);
488 acpi_bus_generate_event (
489 struct acpi_device
*device
,
493 ULONG_PTR TypeData
= 0;
495 DPRINT("acpi_bus_generate_event");
498 return_VALUE(AE_BAD_PARAMETER
);
500 /* drop event on the floor if no one's listening */
504 /* Data shouldn't even get near 24 bits */
505 ASSERT(!(data
& 0xFF000000));
508 TypeData
|= type
<< 24;
510 KeInsertQueueDpc(&event_dpc
, device
, (PVOID
)TypeData
);
516 acpi_bus_receive_event (
517 struct acpi_bus_event
*event
)
519 // unsigned long flags = 0;
520 struct acpi_bus_event
*entry
= NULL
;
523 //DECLARE_WAITQUEUE(wait, current);
525 DPRINT("acpi_bus_receive_event");
528 return AE_BAD_PARAMETER
;
531 KeWaitForSingleObject(&AcpiEventQueue
,
537 KeClearEvent(&AcpiEventQueue
);
539 if (list_empty(&acpi_bus_event_list
))
540 return_VALUE(AE_NOT_FOUND
);
542 // spin_lock_irqsave(&acpi_bus_event_lock, flags);
543 KeAcquireSpinLock(&acpi_bus_event_lock
, &OldIrql
);
544 entry
= list_entry(acpi_bus_event_list
.next
, struct acpi_bus_event
, node
);
546 list_del(&entry
->node
);
547 KeReleaseSpinLock(&acpi_bus_event_lock
, OldIrql
);
548 // spin_unlock_irqrestore(&acpi_bus_event_lock, flags);
551 return_VALUE(AE_NOT_FOUND
);
553 memcpy(event
, entry
, sizeof(struct acpi_bus_event
));
560 /* --------------------------------------------------------------------------
562 -------------------------------------------------------------------------- */
568 * Used to walk the ACPI Bus's device namespace. Can walk down (depth-first)
569 * or up. Able to parse starting at any node in the namespace. Note that a
570 * callback return value of -249 will terminate the walk.
572 * @start: starting point
573 * callback: function to call for every device encountered while parsing
574 * direction: direction to parse (up or down)
575 * @data: context for this search operation
579 struct acpi_device
*start
,
580 acpi_bus_walk_callback callback
,
586 struct acpi_device
*device
= NULL
;
588 if (!start
|| !callback
)
589 return AE_BAD_PARAMETER
;
596 * Parse a given subtree (specified by start) in the given direction.
597 * Walking 'up' simply means that we execute the callback on leaf
598 * devices prior to their parents (useful for things like removing
599 * or powering down a subtree).
604 if (direction
== WALK_DOWN
)
605 if (-249 == callback(device
, level
, data
))
610 if (HAS_CHILDREN(device
)) {
611 device
= NODE_TO_DEVICE(device
->children
.next
);
616 if (direction
== WALK_UP
)
617 if (-249 == callback(device
, level
, data
))
622 if (HAS_SIBLINGS(device
)) {
623 device
= NODE_TO_DEVICE(device
->node
.next
);
627 /* Scope Exhausted - Find Next */
629 while ((device
= device
->parent
)) {
631 if (HAS_SIBLINGS(device
)) {
632 device
= NODE_TO_DEVICE(device
->node
.next
);
638 if ((direction
== WALK_UP
) && (result
== 0))
639 callback(start
, level
, data
);
645 /* --------------------------------------------------------------------------
646 Notification Handling
647 -------------------------------------------------------------------------- */
650 acpi_bus_check_device (ACPI_HANDLE handle
)
652 struct acpi_device
*device
;
653 ACPI_STATUS status
= 0;
654 struct acpi_device_status old_status
;
656 if (acpi_bus_get_device(handle
, &device
))
661 old_status
= device
->status
;
664 * Make sure this device's parent is present before we go about
665 * messing with the device.
667 if (device
->parent
&& !device
->parent
->status
.present
) {
668 device
->status
= device
->parent
->status
;
672 status
= acpi_bus_get_status(device
);
673 if (ACPI_FAILURE(status
))
676 if (STRUCT_TO_INT(old_status
) == STRUCT_TO_INT(device
->status
))
681 * Device Insertion/Removal
683 if ((device
->status
.present
) && !(old_status
.present
)) {
684 DPRINT("Device insertion detected\n");
685 /* TBD: Handle device insertion */
687 else if (!(device
->status
.present
) && (old_status
.present
)) {
688 DPRINT("Device removal detected\n");
689 /* TBD: Handle device removal */
696 acpi_bus_check_scope (ACPI_HANDLE handle
)
699 acpi_bus_check_device(handle
);
702 * TBD: Enumerate child devices within this device's scope and
703 * run acpi_bus_check_device()'s on them.
711 * Callback for all 'system-level' device notifications (values 0x00-0x7F).
719 struct acpi_device
*device
= NULL
;
720 struct acpi_driver
*driver
;
722 DPRINT1("Notification %#02x to handle %p\n", type
, handle
);
724 //blocking_notifier_call_chain(&acpi_bus_notify_list,
725 // type, (void *)handle);
729 case ACPI_NOTIFY_BUS_CHECK
:
730 DPRINT("Received BUS CHECK notification for device [%s]\n",
732 acpi_bus_check_scope(handle
);
734 * TBD: We'll need to outsource certain events to non-ACPI
735 * drivers via the device manager (device.c).
739 case ACPI_NOTIFY_DEVICE_CHECK
:
740 DPRINT("Received DEVICE CHECK notification for device [%s]\n",
742 acpi_bus_check_device(handle
);
744 * TBD: We'll need to outsource certain events to non-ACPI
745 * drivers via the device manager (device.c).
749 case ACPI_NOTIFY_DEVICE_WAKE
:
750 DPRINT("Received DEVICE WAKE notification for device [%s]\n",
752 acpi_bus_check_device(handle
);
754 * TBD: We'll need to outsource certain events to non-ACPI
755 * drivers via the device manager (device.c).
759 case ACPI_NOTIFY_EJECT_REQUEST
:
760 DPRINT1("Received EJECT REQUEST notification for device [%s]\n",
765 case ACPI_NOTIFY_DEVICE_CHECK_LIGHT
:
766 DPRINT1("Received DEVICE CHECK LIGHT notification for device [%s]\n",
768 /* TBD: Exactly what does 'light' mean? */
771 case ACPI_NOTIFY_FREQUENCY_MISMATCH
:
772 DPRINT1("Received FREQUENCY MISMATCH notification for device [%s]\n",
777 case ACPI_NOTIFY_BUS_MODE_MISMATCH
:
778 DPRINT1("Received BUS MODE MISMATCH notification for device [%s]\n",
783 case ACPI_NOTIFY_POWER_FAULT
:
784 DPRINT1("Received POWER FAULT notification for device [%s]\n",
790 DPRINT1("Received unknown/unsupported notification [%08x]\n",
795 acpi_bus_get_device(handle
, &device
);
797 driver
= device
->driver
;
798 if (driver
&& driver
->ops
.notify
&&
799 (driver
->flags
& ACPI_DRIVER_ALL_NOTIFY_EVENTS
))
800 driver
->ops
.notify(device
, type
);
805 /* --------------------------------------------------------------------------
807 -------------------------------------------------------------------------- */
810 static LIST_HEAD(acpi_bus_drivers
);
811 //static DECLARE_MUTEX(acpi_bus_drivers_lock);
812 static FAST_MUTEX acpi_bus_drivers_lock
;
818 * Checks the device's hardware (_HID) or compatible (_CID) ids to see if it
819 * matches the specified driver's criteria.
823 struct acpi_device
*device
,
824 struct acpi_driver
*driver
)
828 if (device
->flags
.hardware_id
)
829 if (strstr(driver
->ids
, device
->pnp
.hardware_id
))
832 if (device
->flags
.compatible_ids
) {
833 ACPI_DEVICE_ID_LIST
*cid_list
= device
->pnp
.cid_list
;
836 /* compare multiple _CID entries against driver ids */
837 for (i
= 0; i
< cid_list
->Count
; i
++)
839 if (strstr(driver
->ids
, cid_list
->Ids
[i
].String
))
852 * acpi_bus_driver_init
853 * --------------------
854 * Used to initialize a device via its device driver. Called whenever a
855 * driver is bound to a device. Invokes the driver's add() and start() ops.
858 acpi_bus_driver_init (
859 struct acpi_device
*device
,
860 struct acpi_driver
*driver
)
864 if (!device
|| !driver
)
865 return_VALUE(AE_BAD_PARAMETER
);
867 if (!driver
->ops
.add
)
870 result
= driver
->ops
.add(device
);
872 device
->driver
= NULL
;
873 //acpi_driver_data(device) = NULL;
874 return_VALUE(result
);
877 device
->driver
= driver
;
880 * TBD - Configuration Management: Assign resources to device based
881 * upon possible configuration and currently allocated resources.
884 if (driver
->ops
.start
) {
885 result
= driver
->ops
.start(device
);
886 if (result
&& driver
->ops
.remove
)
887 driver
->ops
.remove(device
, ACPI_BUS_REMOVAL_NORMAL
);
888 return_VALUE(result
);
891 DPRINT("Driver successfully bound to device\n");
893 if (driver
->ops
.scan
) {
894 driver
->ops
.scan(device
);
904 * Callback for acpi_bus_walk() used to find devices that match a specific
905 * driver's criteria and then attach the driver.
909 struct acpi_device
*device
,
914 struct acpi_driver
*driver
= NULL
;
916 if (!device
|| !data
)
917 return_VALUE(AE_BAD_PARAMETER
);
919 driver
= (struct acpi_driver
*) data
;
924 if (!device
->status
.present
)
925 return_VALUE(AE_NOT_FOUND
);
927 result
= acpi_bus_match(device
, driver
);
929 return_VALUE(result
);
931 DPRINT("Found driver [%s] for device [%s]\n",
932 driver
->name
, device
->pnp
.bus_id
);
934 result
= acpi_bus_driver_init(device
, driver
);
936 return_VALUE(result
);
938 down(&acpi_bus_drivers_lock
);
939 ++driver
->references
;
940 up(&acpi_bus_drivers_lock
);
949 * Callback for acpi_bus_walk() used to find devices that match a specific
950 * driver's criteria and unattach the driver.
954 struct acpi_device
*device
,
959 struct acpi_driver
*driver
= (struct acpi_driver
*) data
;
961 if (!device
|| !driver
)
962 return_VALUE(AE_BAD_PARAMETER
);
964 if (device
->driver
!= driver
)
967 if (!driver
->ops
.remove
)
970 result
= driver
->ops
.remove(device
, ACPI_BUS_REMOVAL_NORMAL
);
972 return_VALUE(result
);
974 device
->driver
= NULL
;
975 acpi_driver_data(device
) = NULL
;
977 down(&acpi_bus_drivers_lock
);
978 driver
->references
--;
979 up(&acpi_bus_drivers_lock
);
986 * acpi_bus_find_driver
987 * --------------------
988 * Parses the list of registered drivers looking for a driver applicable for
989 * the specified device.
992 acpi_bus_find_driver (
993 struct acpi_device
*device
)
995 int result
= AE_NOT_FOUND
;
996 struct list_head
*entry
= NULL
;
997 struct acpi_driver
*driver
= NULL
;
999 if (!device
|| device
->driver
)
1000 return_VALUE(AE_BAD_PARAMETER
);
1002 down(&acpi_bus_drivers_lock
);
1004 list_for_each(entry
, &acpi_bus_drivers
) {
1006 driver
= list_entry(entry
, struct acpi_driver
, node
);
1008 if (acpi_bus_match(device
, driver
))
1011 result
= acpi_bus_driver_init(device
, driver
);
1013 ++driver
->references
;
1018 up(&acpi_bus_drivers_lock
);
1020 return_VALUE(result
);
1025 * acpi_bus_register_driver
1026 * ------------------------
1027 * Registers a driver with the ACPI bus. Searches the namespace for all
1028 * devices that match the driver's criteria and binds.
1031 acpi_bus_register_driver (
1032 struct acpi_driver
*driver
)
1035 return_VALUE(AE_BAD_PARAMETER
);
1037 //if (acpi_disabled)
1038 // return_VALUE(AE_NOT_FOUND);
1040 down(&acpi_bus_drivers_lock
);
1041 list_add_tail(&driver
->node
, &acpi_bus_drivers
);
1042 up(&acpi_bus_drivers_lock
);
1044 acpi_bus_walk(acpi_root
, acpi_bus_attach
,
1047 return_VALUE(driver
->references
);
1052 * acpi_bus_unregister_driver
1053 * --------------------------
1054 * Unregisters a driver with the ACPI bus. Searches the namespace for all
1055 * devices that match the driver's criteria and unbinds.
1058 acpi_bus_unregister_driver (
1059 struct acpi_driver
*driver
)
1064 acpi_bus_walk(acpi_root
, acpi_bus_unattach
, WALK_UP
, driver
);
1066 if (driver
->references
)
1069 down(&acpi_bus_drivers_lock
);
1070 list_del(&driver
->node
);
1071 up(&acpi_bus_drivers_lock
);
1077 /* --------------------------------------------------------------------------
1079 -------------------------------------------------------------------------- */
1082 acpi_bus_get_flags (
1083 struct acpi_device
*device
)
1085 ACPI_STATUS status
= AE_OK
;
1086 ACPI_HANDLE temp
= NULL
;
1088 /* Presence of _STA indicates 'dynamic_status' */
1089 status
= AcpiGetHandle(device
->handle
, "_STA", &temp
);
1090 if (ACPI_SUCCESS(status
))
1091 device
->flags
.dynamic_status
= 1;
1093 /* Presence of _CID indicates 'compatible_ids' */
1094 status
= AcpiGetHandle(device
->handle
, "_CID", &temp
);
1095 if (ACPI_SUCCESS(status
))
1096 device
->flags
.compatible_ids
= 1;
1098 /* Presence of _RMV indicates 'removable' */
1099 status
= AcpiGetHandle(device
->handle
, "_RMV", &temp
);
1100 if (ACPI_SUCCESS(status
))
1101 device
->flags
.removable
= 1;
1103 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
1104 status
= AcpiGetHandle(device
->handle
, "_EJD", &temp
);
1105 if (ACPI_SUCCESS(status
))
1106 device
->flags
.ejectable
= 1;
1108 status
= AcpiGetHandle(device
->handle
, "_EJ0", &temp
);
1109 if (ACPI_SUCCESS(status
))
1110 device
->flags
.ejectable
= 1;
1113 /* Presence of _LCK indicates 'lockable' */
1114 status
= AcpiGetHandle(device
->handle
, "_LCK", &temp
);
1115 if (ACPI_SUCCESS(status
))
1116 device
->flags
.lockable
= 1;
1118 /* Presence of _PS0|_PR0 indicates 'power manageable' */
1119 status
= AcpiGetHandle(device
->handle
, "_PS0", &temp
);
1120 if (ACPI_FAILURE(status
))
1121 status
= AcpiGetHandle(device
->handle
, "_PR0", &temp
);
1122 if (ACPI_SUCCESS(status
))
1123 device
->flags
.power_manageable
= 1;
1125 /* TBD: Peformance management */
1133 struct acpi_device
**child
,
1134 struct acpi_device
*parent
,
1139 ACPI_STATUS status
= AE_OK
;
1140 struct acpi_device
*device
= NULL
;
1141 char bus_id
[5] = {'?',0};
1143 ACPI_DEVICE_INFO
*info
;
1146 ACPI_DEVICE_ID_LIST
*cid_list
= NULL
;
1150 return_VALUE(AE_BAD_PARAMETER
);
1152 device
= ExAllocatePool(NonPagedPool
,sizeof(struct acpi_device
));
1154 DPRINT1("Memory allocation error\n");
1157 memset(device
, 0, sizeof(struct acpi_device
));
1159 device
->handle
= handle
;
1160 device
->parent
= parent
;
1165 * The device's Bus ID is simply the object name.
1166 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1169 case ACPI_BUS_TYPE_SYSTEM
:
1170 sprintf(device
->pnp
.bus_id
, "%s", "ACPI");
1172 case ACPI_BUS_TYPE_POWER_BUTTONF
:
1173 case ACPI_BUS_TYPE_POWER_BUTTON
:
1174 sprintf(device
->pnp
.bus_id
, "%s", "PWRF");
1176 case ACPI_BUS_TYPE_SLEEP_BUTTONF
:
1177 case ACPI_BUS_TYPE_SLEEP_BUTTON
:
1178 sprintf(device
->pnp
.bus_id
, "%s", "SLPF");
1181 buffer
.Length
= sizeof(bus_id
);
1182 buffer
.Pointer
= bus_id
;
1183 AcpiGetName(handle
, ACPI_SINGLE_NAME
, &buffer
);
1186 /* Clean up trailing underscores (if any) */
1187 for (i
= 3; i
> 1; i
--) {
1188 if (bus_id
[i
] == '_')
1193 sprintf(device
->pnp
.bus_id
, "%s", bus_id
);
1194 buffer
.Pointer
= NULL
;
1201 * Get prior to calling acpi_bus_get_status() so we know whether
1202 * or not _STA is present. Note that we only look for object
1203 * handles -- cannot evaluate objects until we know the device is
1204 * present and properly initialized.
1206 result
= acpi_bus_get_flags(device
);
1213 * See if the device is present. We always assume that non-Device()
1214 * objects (e.g. thermal zones, power resources, processors, etc.) are
1215 * present, functioning, etc. (at least when parent object is present).
1216 * Note that _STA has a different meaning for some objects (e.g.
1217 * power resources) so we need to be careful how we use it.
1220 case ACPI_BUS_TYPE_DEVICE
:
1221 result
= acpi_bus_get_status(device
);
1226 STRUCT_TO_INT(device
->status
) = 0x0F;
1229 if (!device
->status
.present
) {
1237 * TBD: Synch with Core's enumeration/initialization process.
1241 * Hardware ID, Unique ID, & Bus Address
1242 * -------------------------------------
1245 case ACPI_BUS_TYPE_DEVICE
:
1246 status
= AcpiGetObjectInfo(handle
,&info
);
1247 if (ACPI_FAILURE(status
)) {
1248 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
,
1249 "Error reading device info\n"));
1250 result
= AE_NOT_FOUND
;
1253 if (info
->Valid
& ACPI_VALID_HID
)
1254 hid
= info
->HardwareId
.String
;
1255 if (info
->Valid
& ACPI_VALID_UID
)
1256 uid
= info
->UniqueId
.String
;
1257 if (info
->Valid
& ACPI_VALID_CID
) {
1258 cid_list
= &info
->CompatibleIdList
;
1259 device
->pnp
.cid_list
= ExAllocatePool(NonPagedPool
,cid_list
->ListSize
);
1260 if (device
->pnp
.cid_list
)
1261 memcpy(device
->pnp
.cid_list
, cid_list
, cid_list
->ListSize
);
1263 DPRINT("Memory allocation error\n");
1265 if (info
->Valid
& ACPI_VALID_ADR
) {
1266 device
->pnp
.bus_address
= info
->Address
;
1267 device
->flags
.bus_address
= 1;
1270 case ACPI_BUS_TYPE_POWER
:
1271 hid
= ACPI_POWER_HID
;
1273 case ACPI_BUS_TYPE_PROCESSOR
:
1274 hid
= ACPI_PROCESSOR_HID
;
1276 case ACPI_BUS_TYPE_SYSTEM
:
1277 hid
= ACPI_SYSTEM_HID
;
1279 case ACPI_BUS_TYPE_THERMAL
:
1280 hid
= ACPI_THERMAL_HID
;
1282 case ACPI_BUS_TYPE_POWER_BUTTON
:
1283 hid
= ACPI_BUTTON_HID_POWER
;
1285 case ACPI_BUS_TYPE_POWER_BUTTONF
:
1286 hid
= ACPI_BUTTON_HID_POWERF
;
1288 case ACPI_BUS_TYPE_SLEEP_BUTTON
:
1289 hid
= ACPI_BUTTON_HID_SLEEP
;
1291 case ACPI_BUS_TYPE_SLEEP_BUTTONF
:
1292 hid
= ACPI_BUTTON_HID_SLEEPF
;
1299 * Fix for the system root bus device -- the only root-level device.
1301 if ((parent
== ACPI_ROOT_OBJECT
) && (type
== ACPI_BUS_TYPE_DEVICE
)) {
1303 sprintf(device
->pnp
.device_name
, "%s", ACPI_BUS_DEVICE_NAME
);
1304 sprintf(device
->pnp
.device_class
, "%s", ACPI_BUS_CLASS
);
1308 sprintf(device
->pnp
.hardware_id
, "%s", hid
);
1309 device
->flags
.hardware_id
= 1;
1312 sprintf(device
->pnp
.unique_id
, "%s", uid
);
1313 device
->flags
.unique_id
= 1;
1317 * If we called get_object_info, we now are finished with the buffer,
1318 * so we can free it.
1320 //if (buffer.Pointer)
1321 //AcpiOsFree(buffer.Pointer);
1327 if (device
->flags
.power_manageable
) {
1328 result
= acpi_bus_get_power_flags(device
);
1334 * Performance Management
1335 * ----------------------
1337 if (device
->flags
.performance_manageable
) {
1338 result
= acpi_bus_get_perf_flags(device
);
1346 * Attach this 'struct acpi_device' to the ACPI object. This makes
1347 * resolutions from handle->device very efficient. Note that we need
1348 * to be careful with fixed-feature devices as they all attach to the
1352 case ACPI_BUS_TYPE_POWER_BUTTON
:
1353 case ACPI_BUS_TYPE_POWER_BUTTONF
:
1354 case ACPI_BUS_TYPE_SLEEP_BUTTON
:
1355 case ACPI_BUS_TYPE_SLEEP_BUTTONF
:
1358 status
= AcpiAttachData(device
->handle
,
1359 acpi_bus_data_handler
, device
);
1362 if (ACPI_FAILURE(status
)) {
1363 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
,
1364 "Error attaching device data\n"));
1365 result
= AE_NOT_FOUND
;
1372 * Link this device to its parent and siblings.
1374 INIT_LIST_HEAD(&device
->children
);
1375 if (!device
->parent
)
1376 INIT_LIST_HEAD(&device
->node
);
1378 list_add_tail(&device
->node
, &device
->parent
->children
);
1381 * Global Device Hierarchy:
1382 * ------------------------
1383 * Register this device with the global device hierarchy.
1385 acpi_device_register(device
, parent
);
1388 * Bind _ADR-Based Devices
1389 * -----------------------
1390 * If there's a a bus address (_ADR) then we utilize the parent's
1391 * 'bind' function (if exists) to bind the ACPI- and natively-
1392 * enumerated device representations.
1394 if (device
->flags
.bus_address
) {
1395 if (device
->parent
&& device
->parent
->ops
.bind
)
1396 device
->parent
->ops
.bind(device
);
1400 * Locate & Attach Driver
1401 * ----------------------
1402 * If there's a hardware id (_HID) or compatible ids (_CID) we check
1403 * to see if there's a driver installed for this kind of device. Note
1404 * that drivers can install before or after a device is enumerated.
1406 * TBD: Assumes LDM provides driver hot-plug capability.
1408 if (device
->flags
.hardware_id
|| device
->flags
.compatible_ids
)
1409 acpi_bus_find_driver(device
);
1413 if (device
->pnp
.cid_list
) {
1414 ExFreePool(device
->pnp
.cid_list
);
1417 return_VALUE(result
);
1427 struct acpi_device
*device
,
1432 return_VALUE(AE_NOT_FOUND
);
1434 acpi_device_unregister(device
);
1436 if (device
&& device
->pnp
.cid_list
)
1437 ExFreePool(device
->pnp
.cid_list
);
1448 struct acpi_device
*start
)
1450 ACPI_STATUS status
= AE_OK
;
1451 struct acpi_device
*parent
= NULL
;
1452 struct acpi_device
*child
= NULL
;
1453 ACPI_HANDLE phandle
= 0;
1454 ACPI_HANDLE chandle
= 0;
1455 ACPI_OBJECT_TYPE type
= 0;
1459 return_VALUE(AE_BAD_PARAMETER
);
1462 phandle
= start
->handle
;
1465 * Parse through the ACPI namespace, identify all 'devices', and
1466 * create a new 'struct acpi_device' for each.
1468 while ((level
> 0) && parent
) {
1470 status
= AcpiGetNextObject(ACPI_TYPE_ANY
, phandle
,
1474 * If this scope is exhausted then move our way back up.
1476 if (ACPI_FAILURE(status
)) {
1479 AcpiGetParent(phandle
, &phandle
);
1481 parent
= parent
->parent
;
1485 status
= AcpiGetType(chandle
, &type
);
1486 if (ACPI_FAILURE(status
))
1490 * If this is a scope object then parse it (depth-first).
1492 if (type
== ACPI_TYPE_LOCAL_SCOPE
) {
1500 * We're only interested in objects that we consider 'devices'.
1503 case ACPI_TYPE_DEVICE
:
1504 type
= ACPI_BUS_TYPE_DEVICE
;
1506 case ACPI_TYPE_PROCESSOR
:
1507 type
= ACPI_BUS_TYPE_PROCESSOR
;
1509 case ACPI_TYPE_THERMAL
:
1510 type
= ACPI_BUS_TYPE_THERMAL
;
1512 case ACPI_TYPE_POWER
:
1513 type
= ACPI_BUS_TYPE_POWER
;
1519 status
= acpi_bus_add(&child
, parent
, chandle
, type
);
1520 if (ACPI_FAILURE(status
))
1524 * If the device is present, enabled, and functioning then
1525 * parse its scope (depth-first). Note that we need to
1526 * represent absent devices to facilitate PnP notifications
1527 * -- but only the subtree head (not all of its children,
1528 * which will be enumerated when the parent is inserted).
1530 * TBD: Need notifications and other detection mechanisms
1531 * in place before we can fully implement this.
1533 if (child
->status
.present
) {
1534 status
= AcpiGetNextObject(ACPI_TYPE_ANY
, chandle
,
1536 if (ACPI_SUCCESS(status
)) {
1550 acpi_bus_scan_fixed (
1551 struct acpi_device
*root
)
1554 struct acpi_device
*device
= NULL
;
1557 return_VALUE(AE_NOT_FOUND
);
1559 /* If ACPI_FADT_POWER_BUTTON is set, then a control
1560 * method power button is present. Otherwise, a fixed
1561 * power button is present.
1563 if (AcpiGbl_FADT
.Flags
& ACPI_FADT_POWER_BUTTON
)
1564 result
= acpi_bus_add(&device
, acpi_root
,
1565 NULL
, ACPI_BUS_TYPE_POWER_BUTTON
);
1568 /* Enable the fixed power button so we get notified if it is pressed */
1569 AcpiWriteBitRegister(ACPI_BITREG_POWER_BUTTON_ENABLE
, 1);
1571 result
= acpi_bus_add(&device
, acpi_root
,
1572 NULL
, ACPI_BUS_TYPE_POWER_BUTTONF
);
1575 /* This one is a bit more complicated and we do it wrong
1576 * right now. If ACPI_FADT_SLEEP_BUTTON is set but no
1577 * device object is present then no sleep button is present, but
1578 * if the flags is clear and there is no device object then it is
1579 * a fixed sleep button. If the flag is set and there is a device object
1580 * the we have a control method button just like above.
1582 if (AcpiGbl_FADT
.Flags
& ACPI_FADT_SLEEP_BUTTON
)
1583 result
= acpi_bus_add(&device
, acpi_root
,
1584 NULL
, ACPI_BUS_TYPE_SLEEP_BUTTON
);
1587 /* Enable the fixed sleep button so we get notified if it is pressed */
1588 AcpiWriteBitRegister(ACPI_BITREG_SLEEP_BUTTON_ENABLE
, 1);
1590 result
= acpi_bus_add(&device
, acpi_root
,
1591 NULL
, ACPI_BUS_TYPE_SLEEP_BUTTONF
);
1594 return_VALUE(result
);
1598 /* --------------------------------------------------------------------------
1599 Initialization/Cleanup
1600 -------------------------------------------------------------------------- */
1603 acpi_bus_init (void)
1606 ACPI_STATUS status
= AE_OK
;
1608 DPRINT("acpi_bus_init");
1610 KeInitializeDpc(&event_dpc
, acpi_bus_generate_event_dpc
, NULL
);
1612 status
= AcpiEnableSubsystem(ACPI_FULL_INITIALIZATION
);
1613 if (ACPI_FAILURE(status
)) {
1614 DPRINT1("Unable to start the ACPI Interpreter\n");
1619 * ACPI 2.0 requires the EC driver to be loaded and work before
1620 * the EC device is found in the namespace. This is accomplished
1621 * by looking for the ECDT table, and getting the EC parameters out
1624 //result = acpi_ec_ecdt_probe();
1625 /* Ignore result. Not having an ECDT is not fatal. */
1627 status
= AcpiInitializeObjects(ACPI_NO_DEVICE_INIT
| ACPI_NO_OBJECT_INIT
);
1628 if (ACPI_FAILURE(status
)) {
1629 DPRINT1("Unable to initialize ACPI objects\n");
1634 * Maybe EC region is required at bus_scan/acpi_get_devices. So it
1635 * is necessary to enable it as early as possible.
1637 //acpi_boot_ec_enable();
1639 /* Initialize sleep structures */
1640 //acpi_sleep_init();
1643 * Register the for all standard device notifications.
1645 status
= AcpiInstallNotifyHandler(ACPI_ROOT_OBJECT
, ACPI_SYSTEM_NOTIFY
, &acpi_bus_notify
, NULL
);
1646 if (ACPI_FAILURE(status
)) {
1647 DPRINT1("Unable to register for device notifications\n");
1648 result
= AE_NOT_FOUND
;
1653 * Create the root device in the bus's device tree
1655 result
= acpi_bus_add(&acpi_root
, NULL
, ACPI_ROOT_OBJECT
,
1656 ACPI_BUS_TYPE_SYSTEM
);
1662 * Enumerate devices in the ACPI namespace.
1664 result
= acpi_bus_scan_fixed(acpi_root
);
1666 DPRINT1("acpi_bus_scan_fixed failed\n");
1667 result
= acpi_bus_scan(acpi_root
);
1669 DPRINT1("acpi_bus_scan failed\n");
1673 /* Mimic structured exception handling */
1675 AcpiRemoveNotifyHandler(ACPI_ROOT_OBJECT
,
1676 ACPI_SYSTEM_NOTIFY
, &acpi_bus_notify
);
1679 return_VALUE(AE_NOT_FOUND
);
1683 acpi_bus_exit (void)
1685 ACPI_STATUS status
= AE_OK
;
1687 DPRINT("acpi_bus_exit");
1689 status
= AcpiRemoveNotifyHandler(ACPI_ROOT_OBJECT
,
1690 ACPI_SYSTEM_NOTIFY
, acpi_bus_notify
);
1691 if (ACPI_FAILURE(status
))
1692 DPRINT1("Error removing notify handler\n");
1694 #ifdef CONFIG_ACPI_PCI
1695 acpi_pci_root_exit();
1696 acpi_pci_link_exit();
1698 #ifdef CONFIG_ACPI_EC
1701 //acpi_power_exit();
1704 acpi_bus_remove(acpi_root
, ACPI_BUS_REMOVAL_NORMAL
);
1706 status
= AcpiTerminate();
1707 if (ACPI_FAILURE(status
))
1708 DPRINT1("Unable to terminate the ACPI Interpreter\n");
1710 DPRINT1("Interpreter disabled\n");
1721 DPRINT("acpi_init");
1723 DPRINT("Subsystem revision %08x\n",ACPI_CA_VERSION
);
1725 KeInitializeSpinLock(&acpi_bus_event_lock
);
1726 KeInitializeEvent(&AcpiEventQueue
, NotificationEvent
, FALSE
);
1727 ExInitializeFastMutex(&acpi_bus_drivers_lock
);
1729 result
= acpi_bus_init();
1732 //pci_mmcfg_late_init();
1733 //if (!(pm_flags & PM_APM))
1734 // pm_flags |= PM_ACPI;
1736 //DPRINT1("APM is already active, exiting\n");
1744 * If the laptop falls into the DMI check table, the power state check
1745 * will be disabled in the course of device power transistion.
1747 //dmi_check_system(power_nocheck_dmi_table);
1750 * Install drivers required for proper enumeration of the
1753 acpi_system_init(); /* ACPI System */
1754 acpi_power_init(); /* ACPI Bus Power Management */
1756 //acpi_ec_init(); /* ACPI Embedded Controller */
1757 #ifdef CONFIG_ACPI_PCI
1758 if (!acpi_pci_disabled
) {
1759 acpi_pci_link_init(); /* ACPI PCI Interrupt Link */
1760 acpi_pci_root_init(); /* ACPI PCI Root Bridge */
1766 //acpi_power_init();
1767 //acpi_system_init();
1768 //acpi_debug_init();
1769 //acpi_sleep_proc_init();
1770 //acpi_wakeup_device_init();
1779 DPRINT("acpi_exit");