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 -------------------------------------------------------------------------- */
460 acpi_bus_generate_event_dpc(PKDPC Dpc
,
461 PVOID DeferredContext
,
462 PVOID SystemArgument1
,
463 PVOID SystemArgument2
)
465 struct acpi_bus_event
*event
= SystemArgument1
;
468 KeAcquireSpinLock(&acpi_bus_event_lock
, &OldIrql
);
469 list_add_tail(&event
->node
, &acpi_bus_event_list
);
470 KeReleaseSpinLock(&acpi_bus_event_lock
, OldIrql
);
472 KeSetEvent(&AcpiEventQueue
, IO_NO_INCREMENT
, FALSE
);
476 acpi_bus_generate_event (
477 struct acpi_device
*device
,
481 struct acpi_bus_event
*event
= NULL
;
483 DPRINT("acpi_bus_generate_event");
486 return_VALUE(AE_BAD_PARAMETER
);
488 /* drop event on the floor if no one's listening */
492 event
= ExAllocatePool(NonPagedPool
,sizeof(struct acpi_bus_event
));
496 sprintf(event
->device_class
, "%s", device
->pnp
.device_class
);
497 sprintf(event
->bus_id
, "%s", device
->pnp
.bus_id
);
501 if (!KeInsertQueueDpc(&event_dpc
, event
, NULL
))
508 acpi_bus_receive_event (
509 struct acpi_bus_event
*event
)
511 // unsigned long flags = 0;
512 struct acpi_bus_event
*entry
= NULL
;
515 //DECLARE_WAITQUEUE(wait, current);
517 DPRINT("acpi_bus_receive_event");
520 return AE_BAD_PARAMETER
;
523 KeWaitForSingleObject(&AcpiEventQueue
,
529 KeClearEvent(&AcpiEventQueue
);
531 if (list_empty(&acpi_bus_event_list
))
532 return_VALUE(AE_NOT_FOUND
);
534 // spin_lock_irqsave(&acpi_bus_event_lock, flags);
535 KeAcquireSpinLock(&acpi_bus_event_lock
, &OldIrql
);
536 entry
= list_entry(acpi_bus_event_list
.next
, struct acpi_bus_event
, node
);
538 list_del(&entry
->node
);
539 KeReleaseSpinLock(&acpi_bus_event_lock
, OldIrql
);
540 // spin_unlock_irqrestore(&acpi_bus_event_lock, flags);
543 return_VALUE(AE_NOT_FOUND
);
545 memcpy(event
, entry
, sizeof(struct acpi_bus_event
));
552 /* --------------------------------------------------------------------------
554 -------------------------------------------------------------------------- */
560 * Used to walk the ACPI Bus's device namespace. Can walk down (depth-first)
561 * or up. Able to parse starting at any node in the namespace. Note that a
562 * callback return value of -249 will terminate the walk.
564 * @start: starting point
565 * callback: function to call for every device encountered while parsing
566 * direction: direction to parse (up or down)
567 * @data: context for this search operation
571 struct acpi_device
*start
,
572 acpi_bus_walk_callback callback
,
578 struct acpi_device
*device
= NULL
;
580 if (!start
|| !callback
)
581 return AE_BAD_PARAMETER
;
588 * Parse a given subtree (specified by start) in the given direction.
589 * Walking 'up' simply means that we execute the callback on leaf
590 * devices prior to their parents (useful for things like removing
591 * or powering down a subtree).
596 if (direction
== WALK_DOWN
)
597 if (-249 == callback(device
, level
, data
))
602 if (HAS_CHILDREN(device
)) {
603 device
= NODE_TO_DEVICE(device
->children
.next
);
608 if (direction
== WALK_UP
)
609 if (-249 == callback(device
, level
, data
))
614 if (HAS_SIBLINGS(device
)) {
615 device
= NODE_TO_DEVICE(device
->node
.next
);
619 /* Scope Exhausted - Find Next */
621 while ((device
= device
->parent
)) {
623 if (HAS_SIBLINGS(device
)) {
624 device
= NODE_TO_DEVICE(device
->node
.next
);
630 if ((direction
== WALK_UP
) && (result
== 0))
631 callback(start
, level
, data
);
637 /* --------------------------------------------------------------------------
638 Notification Handling
639 -------------------------------------------------------------------------- */
642 acpi_bus_check_device (ACPI_HANDLE handle
)
644 struct acpi_device
*device
;
645 ACPI_STATUS status
= 0;
646 struct acpi_device_status old_status
;
648 if (acpi_bus_get_device(handle
, &device
))
653 old_status
= device
->status
;
656 * Make sure this device's parent is present before we go about
657 * messing with the device.
659 if (device
->parent
&& !device
->parent
->status
.present
) {
660 device
->status
= device
->parent
->status
;
664 status
= acpi_bus_get_status(device
);
665 if (ACPI_FAILURE(status
))
668 if (STRUCT_TO_INT(old_status
) == STRUCT_TO_INT(device
->status
))
673 * Device Insertion/Removal
675 if ((device
->status
.present
) && !(old_status
.present
)) {
676 DPRINT("Device insertion detected\n");
677 /* TBD: Handle device insertion */
679 else if (!(device
->status
.present
) && (old_status
.present
)) {
680 DPRINT("Device removal detected\n");
681 /* TBD: Handle device removal */
688 acpi_bus_check_scope (ACPI_HANDLE handle
)
691 acpi_bus_check_device(handle
);
694 * TBD: Enumerate child devices within this device's scope and
695 * run acpi_bus_check_device()'s on them.
703 * Callback for all 'system-level' device notifications (values 0x00-0x7F).
711 struct acpi_device
*device
= NULL
;
712 struct acpi_driver
*driver
;
714 DPRINT1("Notification %#02x to handle %p\n", type
, handle
);
716 //blocking_notifier_call_chain(&acpi_bus_notify_list,
717 // type, (void *)handle);
721 case ACPI_NOTIFY_BUS_CHECK
:
722 DPRINT("Received BUS CHECK notification for device [%s]\n",
724 acpi_bus_check_scope(handle
);
726 * TBD: We'll need to outsource certain events to non-ACPI
727 * drivers via the device manager (device.c).
731 case ACPI_NOTIFY_DEVICE_CHECK
:
732 DPRINT("Received DEVICE CHECK notification for device [%s]\n",
734 acpi_bus_check_device(handle
);
736 * TBD: We'll need to outsource certain events to non-ACPI
737 * drivers via the device manager (device.c).
741 case ACPI_NOTIFY_DEVICE_WAKE
:
742 DPRINT("Received DEVICE WAKE notification for device [%s]\n",
744 acpi_bus_check_device(handle
);
746 * TBD: We'll need to outsource certain events to non-ACPI
747 * drivers via the device manager (device.c).
751 case ACPI_NOTIFY_EJECT_REQUEST
:
752 DPRINT1("Received EJECT REQUEST notification for device [%s]\n",
757 case ACPI_NOTIFY_DEVICE_CHECK_LIGHT
:
758 DPRINT1("Received DEVICE CHECK LIGHT notification for device [%s]\n",
760 /* TBD: Exactly what does 'light' mean? */
763 case ACPI_NOTIFY_FREQUENCY_MISMATCH
:
764 DPRINT1("Received FREQUENCY MISMATCH notification for device [%s]\n",
769 case ACPI_NOTIFY_BUS_MODE_MISMATCH
:
770 DPRINT1("Received BUS MODE MISMATCH notification for device [%s]\n",
775 case ACPI_NOTIFY_POWER_FAULT
:
776 DPRINT1("Received POWER FAULT notification for device [%s]\n",
782 DPRINT1("Received unknown/unsupported notification [%08x]\n",
787 acpi_bus_get_device(handle
, &device
);
789 driver
= device
->driver
;
790 if (driver
&& driver
->ops
.notify
&&
791 (driver
->flags
& ACPI_DRIVER_ALL_NOTIFY_EVENTS
))
792 driver
->ops
.notify(device
, type
);
797 /* --------------------------------------------------------------------------
799 -------------------------------------------------------------------------- */
802 static LIST_HEAD(acpi_bus_drivers
);
803 //static DECLARE_MUTEX(acpi_bus_drivers_lock);
804 static FAST_MUTEX acpi_bus_drivers_lock
;
810 * Checks the device's hardware (_HID) or compatible (_CID) ids to see if it
811 * matches the specified driver's criteria.
815 struct acpi_device
*device
,
816 struct acpi_driver
*driver
)
820 if (device
->flags
.hardware_id
)
821 if (strstr(driver
->ids
, device
->pnp
.hardware_id
))
824 if (device
->flags
.compatible_ids
) {
825 ACPI_DEVICE_ID_LIST
*cid_list
= device
->pnp
.cid_list
;
828 /* compare multiple _CID entries against driver ids */
829 for (i
= 0; i
< cid_list
->Count
; i
++)
831 if (strstr(driver
->ids
, cid_list
->Ids
[i
].String
))
844 * acpi_bus_driver_init
845 * --------------------
846 * Used to initialize a device via its device driver. Called whenever a
847 * driver is bound to a device. Invokes the driver's add() and start() ops.
850 acpi_bus_driver_init (
851 struct acpi_device
*device
,
852 struct acpi_driver
*driver
)
856 if (!device
|| !driver
)
857 return_VALUE(AE_BAD_PARAMETER
);
859 if (!driver
->ops
.add
)
862 result
= driver
->ops
.add(device
);
864 device
->driver
= NULL
;
865 //acpi_driver_data(device) = NULL;
866 return_VALUE(result
);
869 device
->driver
= driver
;
872 * TBD - Configuration Management: Assign resources to device based
873 * upon possible configuration and currently allocated resources.
876 if (driver
->ops
.start
) {
877 result
= driver
->ops
.start(device
);
878 if (result
&& driver
->ops
.remove
)
879 driver
->ops
.remove(device
, ACPI_BUS_REMOVAL_NORMAL
);
880 return_VALUE(result
);
883 DPRINT("Driver successfully bound to device\n");
885 if (driver
->ops
.scan
) {
886 driver
->ops
.scan(device
);
896 * Callback for acpi_bus_walk() used to find devices that match a specific
897 * driver's criteria and then attach the driver.
901 struct acpi_device
*device
,
906 struct acpi_driver
*driver
= NULL
;
908 if (!device
|| !data
)
909 return_VALUE(AE_BAD_PARAMETER
);
911 driver
= (struct acpi_driver
*) data
;
916 if (!device
->status
.present
)
917 return_VALUE(AE_NOT_FOUND
);
919 result
= acpi_bus_match(device
, driver
);
921 return_VALUE(result
);
923 DPRINT("Found driver [%s] for device [%s]\n",
924 driver
->name
, device
->pnp
.bus_id
);
926 result
= acpi_bus_driver_init(device
, driver
);
928 return_VALUE(result
);
930 down(&acpi_bus_drivers_lock
);
931 ++driver
->references
;
932 up(&acpi_bus_drivers_lock
);
941 * Callback for acpi_bus_walk() used to find devices that match a specific
942 * driver's criteria and unattach the driver.
946 struct acpi_device
*device
,
951 struct acpi_driver
*driver
= (struct acpi_driver
*) data
;
953 if (!device
|| !driver
)
954 return_VALUE(AE_BAD_PARAMETER
);
956 if (device
->driver
!= driver
)
959 if (!driver
->ops
.remove
)
962 result
= driver
->ops
.remove(device
, ACPI_BUS_REMOVAL_NORMAL
);
964 return_VALUE(result
);
966 device
->driver
= NULL
;
967 acpi_driver_data(device
) = NULL
;
969 down(&acpi_bus_drivers_lock
);
970 driver
->references
--;
971 up(&acpi_bus_drivers_lock
);
978 * acpi_bus_find_driver
979 * --------------------
980 * Parses the list of registered drivers looking for a driver applicable for
981 * the specified device.
984 acpi_bus_find_driver (
985 struct acpi_device
*device
)
987 int result
= AE_NOT_FOUND
;
988 struct list_head
*entry
= NULL
;
989 struct acpi_driver
*driver
= NULL
;
991 if (!device
|| device
->driver
)
992 return_VALUE(AE_BAD_PARAMETER
);
994 down(&acpi_bus_drivers_lock
);
996 list_for_each(entry
, &acpi_bus_drivers
) {
998 driver
= list_entry(entry
, struct acpi_driver
, node
);
1000 if (acpi_bus_match(device
, driver
))
1003 result
= acpi_bus_driver_init(device
, driver
);
1005 ++driver
->references
;
1010 up(&acpi_bus_drivers_lock
);
1012 return_VALUE(result
);
1017 * acpi_bus_register_driver
1018 * ------------------------
1019 * Registers a driver with the ACPI bus. Searches the namespace for all
1020 * devices that match the driver's criteria and binds.
1023 acpi_bus_register_driver (
1024 struct acpi_driver
*driver
)
1027 return_VALUE(AE_BAD_PARAMETER
);
1029 //if (acpi_disabled)
1030 // return_VALUE(AE_NOT_FOUND);
1032 down(&acpi_bus_drivers_lock
);
1033 list_add_tail(&driver
->node
, &acpi_bus_drivers
);
1034 up(&acpi_bus_drivers_lock
);
1036 acpi_bus_walk(acpi_root
, acpi_bus_attach
,
1039 return_VALUE(driver
->references
);
1044 * acpi_bus_unregister_driver
1045 * --------------------------
1046 * Unregisters a driver with the ACPI bus. Searches the namespace for all
1047 * devices that match the driver's criteria and unbinds.
1050 acpi_bus_unregister_driver (
1051 struct acpi_driver
*driver
)
1056 acpi_bus_walk(acpi_root
, acpi_bus_unattach
, WALK_UP
, driver
);
1058 if (driver
->references
)
1061 down(&acpi_bus_drivers_lock
);
1062 list_del(&driver
->node
);
1063 up(&acpi_bus_drivers_lock
);
1069 /* --------------------------------------------------------------------------
1071 -------------------------------------------------------------------------- */
1074 acpi_bus_get_flags (
1075 struct acpi_device
*device
)
1077 ACPI_STATUS status
= AE_OK
;
1078 ACPI_HANDLE temp
= NULL
;
1080 /* Presence of _STA indicates 'dynamic_status' */
1081 status
= AcpiGetHandle(device
->handle
, "_STA", &temp
);
1082 if (ACPI_SUCCESS(status
))
1083 device
->flags
.dynamic_status
= 1;
1085 /* Presence of _CID indicates 'compatible_ids' */
1086 status
= AcpiGetHandle(device
->handle
, "_CID", &temp
);
1087 if (ACPI_SUCCESS(status
))
1088 device
->flags
.compatible_ids
= 1;
1090 /* Presence of _RMV indicates 'removable' */
1091 status
= AcpiGetHandle(device
->handle
, "_RMV", &temp
);
1092 if (ACPI_SUCCESS(status
))
1093 device
->flags
.removable
= 1;
1095 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
1096 status
= AcpiGetHandle(device
->handle
, "_EJD", &temp
);
1097 if (ACPI_SUCCESS(status
))
1098 device
->flags
.ejectable
= 1;
1100 status
= AcpiGetHandle(device
->handle
, "_EJ0", &temp
);
1101 if (ACPI_SUCCESS(status
))
1102 device
->flags
.ejectable
= 1;
1105 /* Presence of _LCK indicates 'lockable' */
1106 status
= AcpiGetHandle(device
->handle
, "_LCK", &temp
);
1107 if (ACPI_SUCCESS(status
))
1108 device
->flags
.lockable
= 1;
1110 /* Presence of _PS0|_PR0 indicates 'power manageable' */
1111 status
= AcpiGetHandle(device
->handle
, "_PS0", &temp
);
1112 if (ACPI_FAILURE(status
))
1113 status
= AcpiGetHandle(device
->handle
, "_PR0", &temp
);
1114 if (ACPI_SUCCESS(status
))
1115 device
->flags
.power_manageable
= 1;
1117 /* TBD: Peformance management */
1125 struct acpi_device
**child
,
1126 struct acpi_device
*parent
,
1131 ACPI_STATUS status
= AE_OK
;
1132 struct acpi_device
*device
= NULL
;
1133 char bus_id
[5] = {'?',0};
1135 ACPI_DEVICE_INFO
*info
;
1138 ACPI_DEVICE_ID_LIST
*cid_list
= NULL
;
1142 return_VALUE(AE_BAD_PARAMETER
);
1144 device
= ExAllocatePool(NonPagedPool
,sizeof(struct acpi_device
));
1146 DPRINT1("Memory allocation error\n");
1149 memset(device
, 0, sizeof(struct acpi_device
));
1151 device
->handle
= handle
;
1152 device
->parent
= parent
;
1157 * The device's Bus ID is simply the object name.
1158 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1161 case ACPI_BUS_TYPE_SYSTEM
:
1162 sprintf(device
->pnp
.bus_id
, "%s", "ACPI");
1164 case ACPI_BUS_TYPE_POWER_BUTTONF
:
1165 case ACPI_BUS_TYPE_POWER_BUTTON
:
1166 sprintf(device
->pnp
.bus_id
, "%s", "PWRF");
1168 case ACPI_BUS_TYPE_SLEEP_BUTTONF
:
1169 case ACPI_BUS_TYPE_SLEEP_BUTTON
:
1170 sprintf(device
->pnp
.bus_id
, "%s", "SLPF");
1173 buffer
.Length
= sizeof(bus_id
);
1174 buffer
.Pointer
= bus_id
;
1175 AcpiGetName(handle
, ACPI_SINGLE_NAME
, &buffer
);
1178 /* Clean up trailing underscores (if any) */
1179 for (i
= 3; i
> 1; i
--) {
1180 if (bus_id
[i
] == '_')
1185 sprintf(device
->pnp
.bus_id
, "%s", bus_id
);
1186 buffer
.Pointer
= NULL
;
1193 * Get prior to calling acpi_bus_get_status() so we know whether
1194 * or not _STA is present. Note that we only look for object
1195 * handles -- cannot evaluate objects until we know the device is
1196 * present and properly initialized.
1198 result
= acpi_bus_get_flags(device
);
1205 * See if the device is present. We always assume that non-Device()
1206 * objects (e.g. thermal zones, power resources, processors, etc.) are
1207 * present, functioning, etc. (at least when parent object is present).
1208 * Note that _STA has a different meaning for some objects (e.g.
1209 * power resources) so we need to be careful how we use it.
1212 case ACPI_BUS_TYPE_DEVICE
:
1213 result
= acpi_bus_get_status(device
);
1218 STRUCT_TO_INT(device
->status
) = 0x0F;
1221 if (!device
->status
.present
) {
1229 * TBD: Synch with Core's enumeration/initialization process.
1233 * Hardware ID, Unique ID, & Bus Address
1234 * -------------------------------------
1237 case ACPI_BUS_TYPE_DEVICE
:
1238 status
= AcpiGetObjectInfo(handle
,&info
);
1239 if (ACPI_FAILURE(status
)) {
1240 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
,
1241 "Error reading device info\n"));
1242 result
= AE_NOT_FOUND
;
1245 if (info
->Valid
& ACPI_VALID_HID
)
1246 hid
= info
->HardwareId
.String
;
1247 if (info
->Valid
& ACPI_VALID_UID
)
1248 uid
= info
->UniqueId
.String
;
1249 if (info
->Valid
& ACPI_VALID_CID
) {
1250 cid_list
= &info
->CompatibleIdList
;
1251 device
->pnp
.cid_list
= ExAllocatePool(NonPagedPool
,cid_list
->ListSize
);
1252 if (device
->pnp
.cid_list
)
1253 memcpy(device
->pnp
.cid_list
, cid_list
, cid_list
->ListSize
);
1255 DPRINT("Memory allocation error\n");
1257 if (info
->Valid
& ACPI_VALID_ADR
) {
1258 device
->pnp
.bus_address
= info
->Address
;
1259 device
->flags
.bus_address
= 1;
1262 case ACPI_BUS_TYPE_POWER
:
1263 hid
= ACPI_POWER_HID
;
1265 case ACPI_BUS_TYPE_PROCESSOR
:
1266 hid
= ACPI_PROCESSOR_HID
;
1268 case ACPI_BUS_TYPE_SYSTEM
:
1269 hid
= ACPI_SYSTEM_HID
;
1271 case ACPI_BUS_TYPE_THERMAL
:
1272 hid
= ACPI_THERMAL_HID
;
1274 case ACPI_BUS_TYPE_POWER_BUTTON
:
1275 hid
= ACPI_BUTTON_HID_POWER
;
1277 case ACPI_BUS_TYPE_POWER_BUTTONF
:
1278 hid
= ACPI_BUTTON_HID_POWERF
;
1280 case ACPI_BUS_TYPE_SLEEP_BUTTON
:
1281 hid
= ACPI_BUTTON_HID_SLEEP
;
1283 case ACPI_BUS_TYPE_SLEEP_BUTTONF
:
1284 hid
= ACPI_BUTTON_HID_SLEEPF
;
1291 * Fix for the system root bus device -- the only root-level device.
1293 if ((parent
== ACPI_ROOT_OBJECT
) && (type
== ACPI_BUS_TYPE_DEVICE
)) {
1295 sprintf(device
->pnp
.device_name
, "%s", ACPI_BUS_DEVICE_NAME
);
1296 sprintf(device
->pnp
.device_class
, "%s", ACPI_BUS_CLASS
);
1300 sprintf(device
->pnp
.hardware_id
, "%s", hid
);
1301 device
->flags
.hardware_id
= 1;
1304 sprintf(device
->pnp
.unique_id
, "%s", uid
);
1305 device
->flags
.unique_id
= 1;
1309 * If we called get_object_info, we now are finished with the buffer,
1310 * so we can free it.
1312 //if (buffer.Pointer)
1313 //AcpiOsFree(buffer.Pointer);
1319 if (device
->flags
.power_manageable
) {
1320 result
= acpi_bus_get_power_flags(device
);
1326 * Performance Management
1327 * ----------------------
1329 if (device
->flags
.performance_manageable
) {
1330 result
= acpi_bus_get_perf_flags(device
);
1338 * Attach this 'struct acpi_device' to the ACPI object. This makes
1339 * resolutions from handle->device very efficient. Note that we need
1340 * to be careful with fixed-feature devices as they all attach to the
1344 case ACPI_BUS_TYPE_POWER_BUTTON
:
1345 case ACPI_BUS_TYPE_POWER_BUTTONF
:
1346 case ACPI_BUS_TYPE_SLEEP_BUTTON
:
1347 case ACPI_BUS_TYPE_SLEEP_BUTTONF
:
1350 status
= AcpiAttachData(device
->handle
,
1351 acpi_bus_data_handler
, device
);
1354 if (ACPI_FAILURE(status
)) {
1355 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
,
1356 "Error attaching device data\n"));
1357 result
= AE_NOT_FOUND
;
1364 * Link this device to its parent and siblings.
1366 INIT_LIST_HEAD(&device
->children
);
1367 if (!device
->parent
)
1368 INIT_LIST_HEAD(&device
->node
);
1370 list_add_tail(&device
->node
, &device
->parent
->children
);
1373 * Global Device Hierarchy:
1374 * ------------------------
1375 * Register this device with the global device hierarchy.
1377 acpi_device_register(device
, parent
);
1380 * Bind _ADR-Based Devices
1381 * -----------------------
1382 * If there's a a bus address (_ADR) then we utilize the parent's
1383 * 'bind' function (if exists) to bind the ACPI- and natively-
1384 * enumerated device representations.
1386 if (device
->flags
.bus_address
) {
1387 if (device
->parent
&& device
->parent
->ops
.bind
)
1388 device
->parent
->ops
.bind(device
);
1392 * Locate & Attach Driver
1393 * ----------------------
1394 * If there's a hardware id (_HID) or compatible ids (_CID) we check
1395 * to see if there's a driver installed for this kind of device. Note
1396 * that drivers can install before or after a device is enumerated.
1398 * TBD: Assumes LDM provides driver hot-plug capability.
1400 if (device
->flags
.hardware_id
|| device
->flags
.compatible_ids
)
1401 acpi_bus_find_driver(device
);
1405 if (device
->pnp
.cid_list
) {
1406 ExFreePool(device
->pnp
.cid_list
);
1409 return_VALUE(result
);
1419 struct acpi_device
*device
,
1424 return_VALUE(AE_NOT_FOUND
);
1426 acpi_device_unregister(device
);
1428 if (device
&& device
->pnp
.cid_list
)
1429 ExFreePool(device
->pnp
.cid_list
);
1440 struct acpi_device
*start
)
1442 ACPI_STATUS status
= AE_OK
;
1443 struct acpi_device
*parent
= NULL
;
1444 struct acpi_device
*child
= NULL
;
1445 ACPI_HANDLE phandle
= 0;
1446 ACPI_HANDLE chandle
= 0;
1447 ACPI_OBJECT_TYPE type
= 0;
1451 return_VALUE(AE_BAD_PARAMETER
);
1454 phandle
= start
->handle
;
1457 * Parse through the ACPI namespace, identify all 'devices', and
1458 * create a new 'struct acpi_device' for each.
1460 while ((level
> 0) && parent
) {
1462 status
= AcpiGetNextObject(ACPI_TYPE_ANY
, phandle
,
1466 * If this scope is exhausted then move our way back up.
1468 if (ACPI_FAILURE(status
)) {
1471 AcpiGetParent(phandle
, &phandle
);
1473 parent
= parent
->parent
;
1477 status
= AcpiGetType(chandle
, &type
);
1478 if (ACPI_FAILURE(status
))
1482 * If this is a scope object then parse it (depth-first).
1484 if (type
== ACPI_TYPE_LOCAL_SCOPE
) {
1492 * We're only interested in objects that we consider 'devices'.
1495 case ACPI_TYPE_DEVICE
:
1496 type
= ACPI_BUS_TYPE_DEVICE
;
1498 case ACPI_TYPE_PROCESSOR
:
1499 type
= ACPI_BUS_TYPE_PROCESSOR
;
1501 case ACPI_TYPE_THERMAL
:
1502 type
= ACPI_BUS_TYPE_THERMAL
;
1504 case ACPI_TYPE_POWER
:
1505 type
= ACPI_BUS_TYPE_POWER
;
1511 status
= acpi_bus_add(&child
, parent
, chandle
, type
);
1512 if (ACPI_FAILURE(status
))
1516 * If the device is present, enabled, and functioning then
1517 * parse its scope (depth-first). Note that we need to
1518 * represent absent devices to facilitate PnP notifications
1519 * -- but only the subtree head (not all of its children,
1520 * which will be enumerated when the parent is inserted).
1522 * TBD: Need notifications and other detection mechanisms
1523 * in place before we can fully implement this.
1525 if (child
->status
.present
) {
1526 status
= AcpiGetNextObject(ACPI_TYPE_ANY
, chandle
,
1528 if (ACPI_SUCCESS(status
)) {
1542 acpi_bus_scan_fixed (
1543 struct acpi_device
*root
)
1546 struct acpi_device
*device
= NULL
;
1549 return_VALUE(AE_NOT_FOUND
);
1551 /* If ACPI_FADT_POWER_BUTTON is set, then a control
1552 * method power button is present. Otherwise, a fixed
1553 * power button is present.
1555 if (AcpiGbl_FADT
.Flags
& ACPI_FADT_POWER_BUTTON
)
1556 result
= acpi_bus_add(&device
, acpi_root
,
1557 NULL
, ACPI_BUS_TYPE_POWER_BUTTON
);
1560 /* Enable the fixed power button so we get notified if it is pressed */
1561 AcpiWriteBitRegister(ACPI_BITREG_POWER_BUTTON_ENABLE
, 1);
1563 result
= acpi_bus_add(&device
, acpi_root
,
1564 NULL
, ACPI_BUS_TYPE_POWER_BUTTONF
);
1567 /* This one is a bit more complicated and we do it wrong
1568 * right now. If ACPI_FADT_SLEEP_BUTTON is set but no
1569 * device object is present then no sleep button is present, but
1570 * if the flags is clear and there is no device object then it is
1571 * a fixed sleep button. If the flag is set and there is a device object
1572 * the we have a control method button just like above.
1574 if (AcpiGbl_FADT
.Flags
& ACPI_FADT_SLEEP_BUTTON
)
1575 result
= acpi_bus_add(&device
, acpi_root
,
1576 NULL
, ACPI_BUS_TYPE_SLEEP_BUTTON
);
1579 /* Enable the fixed sleep button so we get notified if it is pressed */
1580 AcpiWriteBitRegister(ACPI_BITREG_SLEEP_BUTTON_ENABLE
, 1);
1582 result
= acpi_bus_add(&device
, acpi_root
,
1583 NULL
, ACPI_BUS_TYPE_SLEEP_BUTTONF
);
1586 return_VALUE(result
);
1590 /* --------------------------------------------------------------------------
1591 Initialization/Cleanup
1592 -------------------------------------------------------------------------- */
1595 acpi_bus_init (void)
1598 ACPI_STATUS status
= AE_OK
;
1600 DPRINT("acpi_bus_init");
1602 KeInitializeDpc(&event_dpc
, acpi_bus_generate_event_dpc
, NULL
);
1604 status
= AcpiEnableSubsystem(ACPI_FULL_INITIALIZATION
);
1605 if (ACPI_FAILURE(status
)) {
1606 DPRINT1("Unable to start the ACPI Interpreter\n");
1611 * ACPI 2.0 requires the EC driver to be loaded and work before
1612 * the EC device is found in the namespace. This is accomplished
1613 * by looking for the ECDT table, and getting the EC parameters out
1616 //result = acpi_ec_ecdt_probe();
1617 /* Ignore result. Not having an ECDT is not fatal. */
1619 status
= AcpiInitializeObjects(ACPI_NO_DEVICE_INIT
| ACPI_NO_OBJECT_INIT
);
1620 if (ACPI_FAILURE(status
)) {
1621 DPRINT1("Unable to initialize ACPI objects\n");
1626 * Maybe EC region is required at bus_scan/acpi_get_devices. So it
1627 * is necessary to enable it as early as possible.
1629 //acpi_boot_ec_enable();
1631 /* Initialize sleep structures */
1632 //acpi_sleep_init();
1635 * Register the for all standard device notifications.
1637 status
= AcpiInstallNotifyHandler(ACPI_ROOT_OBJECT
, ACPI_SYSTEM_NOTIFY
, &acpi_bus_notify
, NULL
);
1638 if (ACPI_FAILURE(status
)) {
1639 DPRINT1("Unable to register for device notifications\n");
1640 result
= AE_NOT_FOUND
;
1645 * Create the root device in the bus's device tree
1647 result
= acpi_bus_add(&acpi_root
, NULL
, ACPI_ROOT_OBJECT
,
1648 ACPI_BUS_TYPE_SYSTEM
);
1654 * Enumerate devices in the ACPI namespace.
1656 result
= acpi_bus_scan_fixed(acpi_root
);
1658 DPRINT1("acpi_bus_scan_fixed failed\n");
1659 result
= acpi_bus_scan(acpi_root
);
1661 DPRINT1("acpi_bus_scan failed\n");
1665 /* Mimic structured exception handling */
1667 AcpiRemoveNotifyHandler(ACPI_ROOT_OBJECT
,
1668 ACPI_SYSTEM_NOTIFY
, &acpi_bus_notify
);
1671 return_VALUE(AE_NOT_FOUND
);
1675 acpi_bus_exit (void)
1677 ACPI_STATUS status
= AE_OK
;
1679 DPRINT("acpi_bus_exit");
1681 status
= AcpiRemoveNotifyHandler(ACPI_ROOT_OBJECT
,
1682 ACPI_SYSTEM_NOTIFY
, acpi_bus_notify
);
1683 if (ACPI_FAILURE(status
))
1684 DPRINT1("Error removing notify handler\n");
1686 #ifdef CONFIG_ACPI_PCI
1687 acpi_pci_root_exit();
1688 acpi_pci_link_exit();
1690 #ifdef CONFIG_ACPI_EC
1693 //acpi_power_exit();
1696 acpi_bus_remove(acpi_root
, ACPI_BUS_REMOVAL_NORMAL
);
1698 status
= AcpiTerminate();
1699 if (ACPI_FAILURE(status
))
1700 DPRINT1("Unable to terminate the ACPI Interpreter\n");
1702 DPRINT1("Interpreter disabled\n");
1713 DPRINT("acpi_init");
1715 DPRINT("Subsystem revision %08x\n",ACPI_CA_VERSION
);
1717 KeInitializeSpinLock(&acpi_bus_event_lock
);
1718 KeInitializeEvent(&AcpiEventQueue
, NotificationEvent
, FALSE
);
1719 ExInitializeFastMutex(&acpi_bus_drivers_lock
);
1721 result
= acpi_bus_init();
1724 //pci_mmcfg_late_init();
1725 //if (!(pm_flags & PM_APM))
1726 // pm_flags |= PM_ACPI;
1728 //DPRINT1("APM is already active, exiting\n");
1736 * If the laptop falls into the DMI check table, the power state check
1737 * will be disabled in the course of device power transistion.
1739 //dmi_check_system(power_nocheck_dmi_table);
1742 * Install drivers required for proper enumeration of the
1745 acpi_system_init(); /* ACPI System */
1746 acpi_power_init(); /* ACPI Bus Power Management */
1748 //acpi_ec_init(); /* ACPI Embedded Controller */
1749 #ifdef CONFIG_ACPI_PCI
1750 if (!acpi_pci_disabled
) {
1751 acpi_pci_link_init(); /* ACPI PCI Interrupt Link */
1752 acpi_pci_root_init(); /* ACPI PCI Root Bridge */
1758 //acpi_power_init();
1759 //acpi_system_init();
1760 //acpi_debug_init();
1761 //acpi_sleep_proc_init();
1762 //acpi_wakeup_device_init();
1771 DPRINT("acpi_exit");