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 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25 #include <linux/init.h>
26 #include <linux/ioport.h>
27 #include <linux/list.h>
28 #include <linux/sched.h>
30 #include <linux/device.h>
31 #include <linux/proc_fs.h>
33 #include <asm/mpspec.h>
36 #include "acpi_drivers.h"
39 #define _COMPONENT ACPI_BUS_COMPONENT
40 ACPI_MODULE_NAME ("acpi_bus")
42 extern void eisa_set_level_irq(unsigned int irq
);
44 FADT_DESCRIPTOR acpi_fadt
;
45 struct acpi_device
*acpi_root
;
46 struct proc_dir_entry
*acpi_root_dir
;
48 #define STRUCT_TO_INT(s) (*((int*)&s))
50 /* --------------------------------------------------------------------------
52 -------------------------------------------------------------------------- */
54 extern void acpi_bus_data_handler (
61 struct acpi_device
**device
)
63 acpi_status status
= AE_OK
;
65 ACPI_FUNCTION_TRACE("acpi_bus_get_device");
68 return_VALUE(-EINVAL
);
70 /* TBD: Support fixed-feature devices */
72 status
= acpi_get_data(handle
, acpi_bus_data_handler
, (void**) device
);
73 if (ACPI_FAILURE(status
) || !*device
) {
74 ACPI_DEBUG_PRINT((ACPI_DB_WARN
, "Error getting context for object [%p]\n",
76 return_VALUE(-ENODEV
);
84 struct acpi_device
*device
)
86 acpi_status status
= AE_OK
;
87 unsigned long sta
= 0;
89 ACPI_FUNCTION_TRACE("acpi_bus_get_status");
92 return_VALUE(-EINVAL
);
95 * Evaluate _STA if present.
97 if (device
->flags
.dynamic_status
) {
98 status
= acpi_evaluate_integer(device
->handle
, "_STA", NULL
, &sta
);
99 if (ACPI_FAILURE(status
))
100 return_VALUE(-ENODEV
);
101 STRUCT_TO_INT(device
->status
) = (int) sta
;
105 * Otherwise we assume the status of our parent (unless we don't
106 * have one, in which case status is implied).
108 else if (device
->parent
)
109 device
->status
= device
->parent
->status
;
111 STRUCT_TO_INT(device
->status
) = 0x0F;
113 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Device [%s] status [%08x]\n",
114 device
->pnp
.bus_id
, (u32
) STRUCT_TO_INT(device
->status
)));
120 /* --------------------------------------------------------------------------
122 -------------------------------------------------------------------------- */
130 acpi_status status
= 0;
131 struct acpi_device
*device
= NULL
;
132 unsigned long psc
= 0;
134 ACPI_FUNCTION_TRACE("acpi_bus_get_power");
136 result
= acpi_bus_get_device(handle
, &device
);
138 return_VALUE(result
);
140 *state
= ACPI_STATE_UNKNOWN
;
142 if (!device
->flags
.power_manageable
) {
143 /* TBD: Non-recursive algorithm for walking up hierarchy */
145 *state
= device
->parent
->power
.state
;
147 *state
= ACPI_STATE_D0
;
151 * Get the device's power state either directly (via _PSC) or
152 * indirectly (via power resources).
154 if (device
->power
.flags
.explicit_get
) {
155 status
= acpi_evaluate_integer(device
->handle
, "_PSC",
157 if (ACPI_FAILURE(status
))
158 return_VALUE(-ENODEV
);
159 device
->power
.state
= (int) psc
;
161 else if (device
->power
.flags
.power_resources
) {
162 result
= acpi_power_get_inferred_state(device
);
164 return_VALUE(result
);
167 *state
= device
->power
.state
;
170 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Device [%s] power state is D%d\n",
171 device
->pnp
.bus_id
, device
->power
.state
));
183 acpi_status status
= AE_OK
;
184 struct acpi_device
*device
= NULL
;
185 char object_name
[5] = {'_','P','S','0'+state
,'\0'};
187 ACPI_FUNCTION_TRACE("acpi_bus_set_power");
189 result
= acpi_bus_get_device(handle
, &device
);
191 return_VALUE(result
);
193 if ((state
< ACPI_STATE_D0
) || (state
> ACPI_STATE_D3
))
194 return_VALUE(-EINVAL
);
196 /* Make sure this is a valid target state */
198 if (!device
->flags
.power_manageable
) {
199 ACPI_DEBUG_PRINT((ACPI_DB_WARN
, "Device is not power manageable\n"));
200 return_VALUE(-ENODEV
);
202 if (state
== device
->power
.state
) {
203 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Device is already at D%d\n", state
));
206 if (!device
->power
.states
[state
].flags
.valid
) {
207 ACPI_DEBUG_PRINT((ACPI_DB_WARN
, "Device does not support D%d\n", state
));
208 return_VALUE(-ENODEV
);
210 if (device
->parent
&& (state
< device
->parent
->power
.state
)) {
211 ACPI_DEBUG_PRINT((ACPI_DB_WARN
, "Cannot set device to a higher-powered state than parent\n"));
212 return_VALUE(-ENODEV
);
218 * On transitions to a high-powered state we first apply power (via
219 * power resources) then evalute _PSx. Conversly for transitions to
220 * a lower-powered state.
222 if (state
< device
->power
.state
) {
223 if (device
->power
.flags
.power_resources
) {
224 result
= acpi_power_transition(device
, state
);
228 if (device
->power
.states
[state
].flags
.explicit_set
) {
229 status
= acpi_evaluate_object(device
->handle
,
230 object_name
, NULL
, NULL
);
231 if (ACPI_FAILURE(status
)) {
238 if (device
->power
.states
[state
].flags
.explicit_set
) {
239 status
= acpi_evaluate_object(device
->handle
,
240 object_name
, NULL
, NULL
);
241 if (ACPI_FAILURE(status
)) {
246 if (device
->power
.flags
.power_resources
) {
247 result
= acpi_power_transition(device
, state
);
255 ACPI_DEBUG_PRINT((ACPI_DB_WARN
, "Error transitioning device [%s] to D%d\n",
256 device
->pnp
.bus_id
, state
));
258 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Device [%s] transitioned to D%d\n",
259 device
->pnp
.bus_id
, state
));
261 return_VALUE(result
);
266 /* --------------------------------------------------------------------------
268 -------------------------------------------------------------------------- */
270 static spinlock_t acpi_bus_event_lock
= SPIN_LOCK_UNLOCKED
;
272 LIST_HEAD(acpi_bus_event_list
);
273 DECLARE_WAIT_QUEUE_HEAD(acpi_bus_event_queue
);
275 extern int event_is_open
;
278 acpi_bus_generate_event (
279 struct acpi_device
*device
,
283 struct acpi_bus_event
*event
= NULL
;
286 ACPI_FUNCTION_TRACE("acpi_bus_generate_event");
289 return_VALUE(-EINVAL
);
291 /* drop event on the floor if no one's listening */
295 event
= kmalloc(sizeof(struct acpi_bus_event
), GFP_ATOMIC
);
297 return_VALUE(-ENOMEM
);
299 sprintf(event
->device_class
, "%s", device
->pnp
.device_class
);
300 sprintf(event
->bus_id
, "%s", device
->pnp
.bus_id
);
304 spin_lock_irqsave(&acpi_bus_event_lock
, flags
);
305 list_add_tail(&event
->node
, &acpi_bus_event_list
);
306 spin_unlock_irqrestore(&acpi_bus_event_lock
, flags
);
308 wake_up_interruptible(&acpi_bus_event_queue
);
314 acpi_bus_receive_event (
315 struct acpi_bus_event
*event
)
318 struct acpi_bus_event
*entry
= NULL
;
320 DECLARE_WAITQUEUE(wait
, current
);
322 ACPI_FUNCTION_TRACE("acpi_bus_receive_event");
327 if (list_empty(&acpi_bus_event_list
)) {
329 set_current_state(TASK_INTERRUPTIBLE
);
330 add_wait_queue(&acpi_bus_event_queue
, &wait
);
332 if (list_empty(&acpi_bus_event_list
))
335 remove_wait_queue(&acpi_bus_event_queue
, &wait
);
336 set_current_state(TASK_RUNNING
);
338 if (signal_pending(current
))
339 return_VALUE(-ERESTARTSYS
);
342 spin_lock_irqsave(&acpi_bus_event_lock
, flags
);
343 entry
= list_entry(acpi_bus_event_list
.next
, struct acpi_bus_event
, node
);
345 list_del(&entry
->node
);
346 spin_unlock_irqrestore(&acpi_bus_event_lock
, flags
);
349 return_VALUE(-ENODEV
);
351 memcpy(event
, entry
, sizeof(struct acpi_bus_event
));
359 /* --------------------------------------------------------------------------
360 Notification Handling
361 -------------------------------------------------------------------------- */
364 acpi_bus_check_device (
365 struct acpi_device
*device
,
368 acpi_status status
= 0;
369 struct acpi_device_status old_status
;
371 ACPI_FUNCTION_TRACE("acpi_bus_check_device");
374 return_VALUE(-EINVAL
);
379 old_status
= device
->status
;
382 * Make sure this device's parent is present before we go about
383 * messing with the device.
385 if (device
->parent
&& !device
->parent
->status
.present
) {
386 device
->status
= device
->parent
->status
;
387 if (STRUCT_TO_INT(old_status
) != STRUCT_TO_INT(device
->status
)) {
394 status
= acpi_bus_get_status(device
);
395 if (ACPI_FAILURE(status
))
396 return_VALUE(-ENODEV
);
398 if (STRUCT_TO_INT(old_status
) == STRUCT_TO_INT(device
->status
))
405 * Device Insertion/Removal
407 if ((device
->status
.present
) && !(old_status
.present
)) {
408 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Device insertion detected\n"));
409 /* TBD: Handle device insertion */
411 else if (!(device
->status
.present
) && (old_status
.present
)) {
412 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Device removal detected\n"));
413 /* TBD: Handle device removal */
421 acpi_bus_check_scope (
422 struct acpi_device
*device
)
425 int status_changed
= 0;
427 ACPI_FUNCTION_TRACE("acpi_bus_check_scope");
430 return_VALUE(-EINVAL
);
433 result
= acpi_bus_check_device(device
, &status_changed
);
435 return_VALUE(result
);
441 * TBD: Enumerate child devices within this device's scope and
442 * run acpi_bus_check_device()'s on them.
452 * Callback for all 'system-level' device notifications (values 0x00-0x7F).
461 struct acpi_device
*device
= NULL
;
463 ACPI_FUNCTION_TRACE("acpi_bus_notify");
465 if (acpi_bus_get_device(handle
, &device
))
470 case ACPI_NOTIFY_BUS_CHECK
:
471 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Received BUS CHECK notification for device [%s]\n",
472 device
->pnp
.bus_id
));
473 result
= acpi_bus_check_scope(device
);
475 * TBD: We'll need to outsource certain events to non-ACPI
476 * drivers via the device manager (device.c).
480 case ACPI_NOTIFY_DEVICE_CHECK
:
481 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Received DEVICE CHECK notification for device [%s]\n",
482 device
->pnp
.bus_id
));
483 result
= acpi_bus_check_device(device
, NULL
);
485 * TBD: We'll need to outsource certain events to non-ACPI
486 * drivers via the device manager (device.c).
490 case ACPI_NOTIFY_DEVICE_WAKE
:
491 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Received DEVICE WAKE notification for device [%s]\n",
492 device
->pnp
.bus_id
));
496 case ACPI_NOTIFY_EJECT_REQUEST
:
497 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Received EJECT REQUEST notification for device [%s]\n",
498 device
->pnp
.bus_id
));
502 case ACPI_NOTIFY_DEVICE_CHECK_LIGHT
:
503 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Received DEVICE CHECK LIGHT notification for device [%s]\n",
504 device
->pnp
.bus_id
));
505 /* TBD: Exactly what does 'light' mean? */
508 case ACPI_NOTIFY_FREQUENCY_MISMATCH
:
509 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Received FREQUENCY MISMATCH notification for device [%s]\n",
510 device
->pnp
.bus_id
));
514 case ACPI_NOTIFY_BUS_MODE_MISMATCH
:
515 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Received BUS MODE MISMATCH notification for device [%s]\n",
516 device
->pnp
.bus_id
));
520 case ACPI_NOTIFY_POWER_FAULT
:
521 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Received POWER FAULT notification for device [%s]\n",
522 device
->pnp
.bus_id
));
527 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Received unknown/unsupported notification [%08x]\n",
535 /* --------------------------------------------------------------------------
536 Initialization/Cleanup
537 -------------------------------------------------------------------------- */
540 acpi_bus_init_irq (void)
542 acpi_status status
= AE_OK
;
543 acpi_object arg
= {ACPI_TYPE_INTEGER
};
544 acpi_object_list arg_list
= {1, &arg
};
545 char *message
= NULL
;
547 ACPI_FUNCTION_TRACE("acpi_bus_init_irq");
550 * Let the system know what interrupt model we are using by
551 * evaluating the \_PIC object, if exists.
554 switch (acpi_irq_model
) {
555 case ACPI_IRQ_MODEL_PIC
:
558 case ACPI_IRQ_MODEL_IOAPIC
:
561 case ACPI_IRQ_MODEL_IOSAPIC
:
565 printk(KERN_WARNING PREFIX
"Unknown interrupt routing model\n");
566 return_VALUE(-ENODEV
);
569 printk(KERN_INFO PREFIX
"Using %s for interrupt routing\n", message
);
571 arg
.integer
.value
= acpi_irq_model
;
573 status
= acpi_evaluate_object(NULL
, "\\_PIC", &arg_list
, NULL
);
574 if (ACPI_FAILURE(status
) && (status
!= AE_NOT_FOUND
)) {
575 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
, "Error evaluating _PIC\n"));
576 return_VALUE(-ENODEV
);
587 acpi_status status
= AE_OK
;
588 acpi_buffer buffer
= {sizeof(acpi_fadt
), &acpi_fadt
};
590 ACPI_FUNCTION_TRACE("acpi_bus_init");
592 status
= acpi_initialize_subsystem();
593 if (ACPI_FAILURE(status
)) {
594 printk(KERN_ERR PREFIX
"Unable to initialize the ACPI Interpreter\n");
598 status
= acpi_load_tables();
599 if (ACPI_FAILURE(status
)) {
600 printk(KERN_ERR PREFIX
"Unable to load the System Description Tables\n");
605 * Get a separate copy of the FADT for use by other drivers.
607 status
= acpi_get_table(ACPI_TABLE_FADT
, 1, &buffer
);
608 if (ACPI_FAILURE(status
)) {
609 printk(KERN_ERR PREFIX
"Unable to get the FADT\n");
614 /* Ensure the SCI is set to level-triggered, active-low */
616 mp_config_ioapic_for_sci(acpi_fadt
.sci_int
);
618 eisa_set_level_irq(acpi_fadt
.sci_int
);
621 status
= acpi_enable_subsystem(ACPI_FULL_INITIALIZATION
);
622 if (ACPI_FAILURE(status
)) {
623 printk(KERN_ERR PREFIX
"Unable to start the ACPI Interpreter\n");
627 #ifdef CONFIG_ACPI_EC
629 * ACPI 2.0 requires the EC driver to be loaded and work before
630 * the EC device is found in the namespace (i.e. before acpi_initialize_objects()
633 * This is accomplished by looking for the ECDT table, and getting
634 * the EC parameters out of that.
636 status
= acpi_ec_ecdt_probe();
637 if (ACPI_FAILURE(status
))
641 status
= acpi_initialize_objects(ACPI_FULL_INITIALIZATION
);
642 if (ACPI_FAILURE(status
)) {
643 printk(KERN_ERR PREFIX
"Unable to initialize ACPI objects\n");
647 printk(KERN_INFO PREFIX
"Interpreter enabled\n");
650 * Get the system interrupt model and evaluate \_PIC.
652 result
= acpi_bus_init_irq();
657 * Register the for all standard device notifications.
659 status
= acpi_install_notify_handler(ACPI_ROOT_OBJECT
, ACPI_SYSTEM_NOTIFY
, &acpi_bus_notify
, NULL
);
660 if (ACPI_FAILURE(status
)) {
661 printk(KERN_ERR PREFIX
"Unable to register for device notifications\n");
666 * Create the top ACPI proc directory
668 acpi_root_dir
= proc_mkdir(ACPI_BUS_FILE_ROOT
, NULL
);
672 /* Mimic structured exception handling */
676 return_VALUE(-ENODEV
);
679 struct subsystem acpi_subsys
= {
680 .kobj
= { .name
= "acpi" },
683 static int __init
acpi_init (void)
687 ACPI_FUNCTION_TRACE("acpi_init");
689 printk(KERN_INFO PREFIX
"Subsystem revision %08x\n",
692 /* Initial core debug level excludes drivers, so include them now */
693 acpi_set_debug(ACPI_DEBUG_LOW
);
696 printk(KERN_INFO PREFIX
"Disabled via command line (acpi=off)\n");
700 firmware_register(&acpi_subsys
);
702 result
= acpi_bus_init();
709 printk(KERN_INFO PREFIX
"APM is already active, exiting\n");
717 return_VALUE(result
);
721 static int __init
acpi_setup(char *str
)
723 while (str
&& *str
) {
724 if (strncmp(str
, "off", 3) == 0)
726 str
= strchr(str
, ',');
728 str
+= strspn(str
, ", \t");
733 subsys_initcall(acpi_init
);
735 __setup("acpi=", acpi_setup
);