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/module.h>
26 #include <linux/init.h>
27 #include <linux/ioport.h>
28 #include <linux/list.h>
29 #include <linux/sched.h>
31 #include <linux/device.h>
32 #include <linux/proc_fs.h>
34 #include <asm/mpspec.h>
36 #include <acpi/acpi_bus.h>
37 #include <acpi/acpi_drivers.h>
40 #define _COMPONENT ACPI_BUS_COMPONENT
41 ACPI_MODULE_NAME ("acpi_bus")
44 extern void __init acpi_pic_sci_set_trigger(unsigned int irq, u16 trigger);
47 FADT_DESCRIPTOR acpi_fadt;
48 EXPORT_SYMBOL(acpi_fadt);
50 struct acpi_device *acpi_root;
51 struct proc_dir_entry *acpi_root_dir;
52 EXPORT_SYMBOL(acpi_root_dir);
54 #define STRUCT_TO_INT(s) (*((int*)&s))
56 /* --------------------------------------------------------------------------
58 -------------------------------------------------------------------------- */
60 extern void acpi_bus_data_handler (
67 struct acpi_device **device)
69 acpi_status status = AE_OK;
71 ACPI_FUNCTION_TRACE("acpi_bus_get_device");
74 return_VALUE(-EINVAL);
76 /* TBD: Support fixed-feature devices */
78 status = acpi_get_data(handle, acpi_bus_data_handler, (void**) device);
79 if (ACPI_FAILURE(status) || !*device) {
80 ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Error getting context for object [%p]\n",
82 return_VALUE(-ENODEV);
87 EXPORT_SYMBOL(acpi_bus_get_device);
91 struct acpi_device *device)
93 acpi_status status = AE_OK;
94 unsigned long sta = 0;
96 ACPI_FUNCTION_TRACE("acpi_bus_get_status");
99 return_VALUE(-EINVAL);
102 * Evaluate _STA if present.
104 if (device->flags.dynamic_status) {
105 status = acpi_evaluate_integer(device->handle, "_STA", NULL, &sta);
106 if (ACPI_FAILURE(status))
107 return_VALUE(-ENODEV);
108 STRUCT_TO_INT(device->status) = (int) sta;
112 * Otherwise we assume the status of our parent (unless we don't
113 * have one, in which case status is implied).
115 else if (device->parent)
116 device->status = device->parent->status;
118 STRUCT_TO_INT(device->status) = 0x0F;
120 if (device->status.functional && !device->status.present) {
121 printk(KERN_WARNING PREFIX "Device [%s] status [%08x]: "
122 "functional but not present; setting present\n",
124 (u32) STRUCT_TO_INT(device->status));
125 device->status.present = 1;
128 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] status [%08x]\n",
129 device->pnp.bus_id, (u32) STRUCT_TO_INT(device->status)));
133 EXPORT_SYMBOL(acpi_bus_get_status);
136 /* --------------------------------------------------------------------------
138 -------------------------------------------------------------------------- */
146 acpi_status status = 0;
147 struct acpi_device *device = NULL;
148 unsigned long psc = 0;
150 ACPI_FUNCTION_TRACE("acpi_bus_get_power");
152 result = acpi_bus_get_device(handle, &device);
154 return_VALUE(result);
156 *state = ACPI_STATE_UNKNOWN;
158 if (!device->flags.power_manageable) {
159 /* TBD: Non-recursive algorithm for walking up hierarchy */
161 *state = device->parent->power.state;
163 *state = ACPI_STATE_D0;
167 * Get the device's power state either directly (via _PSC) or
168 * indirectly (via power resources).
170 if (device->power.flags.explicit_get) {
171 status = acpi_evaluate_integer(device->handle, "_PSC",
173 if (ACPI_FAILURE(status))
174 return_VALUE(-ENODEV);
175 device->power.state = (int) psc;
177 else if (device->power.flags.power_resources) {
178 result = acpi_power_get_inferred_state(device);
180 return_VALUE(result);
183 *state = device->power.state;
186 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] power state is D%d\n",
187 device->pnp.bus_id, device->power.state));
191 EXPORT_SYMBOL(acpi_bus_get_power);
200 acpi_status status = AE_OK;
201 struct acpi_device *device = NULL;
202 char object_name[5] = {'_','P','S','0'+state,'\0'};
204 ACPI_FUNCTION_TRACE("acpi_bus_set_power");
206 result = acpi_bus_get_device(handle, &device);
208 return_VALUE(result);
210 if ((state < ACPI_STATE_D0) || (state > ACPI_STATE_D3))
211 return_VALUE(-EINVAL);
213 /* Make sure this is a valid target state */
215 if (!device->flags.power_manageable) {
216 ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Device is not power manageable\n"));
217 return_VALUE(-ENODEV);
219 if (state == device->power.state) {
220 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device is already at D%d\n", state));
223 if (!device->power.states[state].flags.valid) {
224 ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Device does not support D%d\n", state));
225 return_VALUE(-ENODEV);
227 if (device->parent && (state < device->parent->power.state)) {
228 ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Cannot set device to a higher-powered state than parent\n"));
229 return_VALUE(-ENODEV);
235 * On transitions to a high-powered state we first apply power (via
236 * power resources) then evalute _PSx. Conversly for transitions to
237 * a lower-powered state.
239 if (state < device->power.state) {
240 if (device->power.flags.power_resources) {
241 result = acpi_power_transition(device, state);
245 if (device->power.states[state].flags.explicit_set) {
246 status = acpi_evaluate_object(device->handle,
247 object_name, NULL, NULL);
248 if (ACPI_FAILURE(status)) {
255 if (device->power.states[state].flags.explicit_set) {
256 status = acpi_evaluate_object(device->handle,
257 object_name, NULL, NULL);
258 if (ACPI_FAILURE(status)) {
263 if (device->power.flags.power_resources) {
264 result = acpi_power_transition(device, state);
272 ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Error transitioning device [%s] to D%d\n",
273 device->pnp.bus_id, state));
275 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] transitioned to D%d\n",
276 device->pnp.bus_id, state));
278 return_VALUE(result);
280 EXPORT_SYMBOL(acpi_bus_set_power);
284 /* --------------------------------------------------------------------------
286 -------------------------------------------------------------------------- */
288 static spinlock_t acpi_bus_event_lock = SPIN_LOCK_UNLOCKED;
290 LIST_HEAD(acpi_bus_event_list);
291 DECLARE_WAIT_QUEUE_HEAD(acpi_bus_event_queue);
293 extern int event_is_open;
296 acpi_bus_generate_event (
297 struct acpi_device *device,
301 struct acpi_bus_event *event = NULL;
302 unsigned long flags = 0;
304 ACPI_FUNCTION_TRACE("acpi_bus_generate_event");
307 return_VALUE(-EINVAL);
309 /* drop event on the floor if no one's listening */
313 event = kmalloc(sizeof(struct acpi_bus_event), GFP_ATOMIC);
315 return_VALUE(-ENOMEM);
317 strcpy(event->device_class, device->pnp.device_class);
318 strcpy(event->bus_id, device->pnp.bus_id);
322 spin_lock_irqsave(&acpi_bus_event_lock, flags);
323 list_add_tail(&event->node, &acpi_bus_event_list);
324 spin_unlock_irqrestore(&acpi_bus_event_lock, flags);
326 wake_up_interruptible(&acpi_bus_event_queue);
330 EXPORT_SYMBOL(acpi_bus_generate_event);
333 acpi_bus_receive_event (
334 struct acpi_bus_event *event)
336 unsigned long flags = 0;
337 struct acpi_bus_event *entry = NULL;
339 DECLARE_WAITQUEUE(wait, current);
341 ACPI_FUNCTION_TRACE("acpi_bus_receive_event");
344 return_VALUE(-EINVAL);
346 if (list_empty(&acpi_bus_event_list)) {
348 set_current_state(TASK_INTERRUPTIBLE);
349 add_wait_queue(&acpi_bus_event_queue, &wait);
351 if (list_empty(&acpi_bus_event_list))
354 remove_wait_queue(&acpi_bus_event_queue, &wait);
355 set_current_state(TASK_RUNNING);
357 if (signal_pending(current))
358 return_VALUE(-ERESTARTSYS);
361 spin_lock_irqsave(&acpi_bus_event_lock, flags);
362 entry = list_entry(acpi_bus_event_list.next, struct acpi_bus_event, node);
364 list_del(&entry->node);
365 spin_unlock_irqrestore(&acpi_bus_event_lock, flags);
368 return_VALUE(-ENODEV);
370 memcpy(event, entry, sizeof(struct acpi_bus_event));
376 EXPORT_SYMBOL(acpi_bus_receive_event);
379 /* --------------------------------------------------------------------------
380 Notification Handling
381 -------------------------------------------------------------------------- */
384 acpi_bus_check_device (
385 struct acpi_device *device,
388 acpi_status status = 0;
389 struct acpi_device_status old_status;
391 ACPI_FUNCTION_TRACE("acpi_bus_check_device");
394 return_VALUE(-EINVAL);
399 old_status = device->status;
402 * Make sure this device's parent is present before we go about
403 * messing with the device.
405 if (device->parent && !device->parent->status.present) {
406 device->status = device->parent->status;
407 if (STRUCT_TO_INT(old_status) != STRUCT_TO_INT(device->status)) {
414 status = acpi_bus_get_status(device);
415 if (ACPI_FAILURE(status))
416 return_VALUE(-ENODEV);
418 if (STRUCT_TO_INT(old_status) == STRUCT_TO_INT(device->status))
425 * Device Insertion/Removal
427 if ((device->status.present) && !(old_status.present)) {
428 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device insertion detected\n"));
429 /* TBD: Handle device insertion */
431 else if (!(device->status.present) && (old_status.present)) {
432 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device removal detected\n"));
433 /* TBD: Handle device removal */
441 acpi_bus_check_scope (
442 struct acpi_device *device)
445 int status_changed = 0;
447 ACPI_FUNCTION_TRACE("acpi_bus_check_scope");
450 return_VALUE(-EINVAL);
453 result = acpi_bus_check_device(device, &status_changed);
455 return_VALUE(result);
461 * TBD: Enumerate child devices within this device's scope and
462 * run acpi_bus_check_device()'s on them.
472 * Callback for all 'system-level' device notifications (values 0x00-0x7F).
481 struct acpi_device *device = NULL;
483 ACPI_FUNCTION_TRACE("acpi_bus_notify");
485 if (acpi_bus_get_device(handle, &device))
490 case ACPI_NOTIFY_BUS_CHECK:
491 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Received BUS CHECK notification for device [%s]\n",
492 device->pnp.bus_id));
493 result = acpi_bus_check_scope(device);
495 * TBD: We'll need to outsource certain events to non-ACPI
496 * drivers via the device manager (device.c).
500 case ACPI_NOTIFY_DEVICE_CHECK:
501 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Received DEVICE CHECK notification for device [%s]\n",
502 device->pnp.bus_id));
503 result = acpi_bus_check_device(device, NULL);
505 * TBD: We'll need to outsource certain events to non-ACPI
506 * drivers via the device manager (device.c).
510 case ACPI_NOTIFY_DEVICE_WAKE:
511 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Received DEVICE WAKE notification for device [%s]\n",
512 device->pnp.bus_id));
516 case ACPI_NOTIFY_EJECT_REQUEST:
517 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Received EJECT REQUEST notification for device [%s]\n",
518 device->pnp.bus_id));
522 case ACPI_NOTIFY_DEVICE_CHECK_LIGHT:
523 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Received DEVICE CHECK LIGHT notification for device [%s]\n",
524 device->pnp.bus_id));
525 /* TBD: Exactly what does 'light' mean? */
528 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
529 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Received FREQUENCY MISMATCH notification for device [%s]\n",
530 device->pnp.bus_id));
534 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
535 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Received BUS MODE MISMATCH notification for device [%s]\n",
536 device->pnp.bus_id));
540 case ACPI_NOTIFY_POWER_FAULT:
541 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Received POWER FAULT notification for device [%s]\n",
542 device->pnp.bus_id));
547 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Received unknown/unsupported notification [%08x]\n",
555 /* --------------------------------------------------------------------------
556 Initialization/Cleanup
557 -------------------------------------------------------------------------- */
560 acpi_bus_init_irq (void)
562 acpi_status status = AE_OK;
563 union acpi_object arg = {ACPI_TYPE_INTEGER};
564 struct acpi_object_list arg_list = {1, &arg};
565 char *message = NULL;
567 ACPI_FUNCTION_TRACE("acpi_bus_init_irq");
570 * Let the system know what interrupt model we are using by
571 * evaluating the \_PIC object, if exists.
574 switch (acpi_irq_model) {
575 case ACPI_IRQ_MODEL_PIC:
578 case ACPI_IRQ_MODEL_IOAPIC:
581 case ACPI_IRQ_MODEL_IOSAPIC:
585 printk(KERN_WARNING PREFIX "Unknown interrupt routing model\n");
586 return_VALUE(-ENODEV);
589 printk(KERN_INFO PREFIX "Using %s for interrupt routing\n", message);
591 arg.integer.value = acpi_irq_model;
593 status = acpi_evaluate_object(NULL, "\\_PIC", &arg_list, NULL);
594 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
595 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _PIC\n"));
596 return_VALUE(-ENODEV);
604 acpi_early_init (void)
606 acpi_status status = AE_OK;
607 struct acpi_buffer buffer = {sizeof(acpi_fadt), &acpi_fadt};
609 ACPI_FUNCTION_TRACE("acpi_early_init");
614 /* enable workarounds, unless strict ACPI spec. compliance */
616 acpi_gbl_enable_interpreter_slack = TRUE;
618 status = acpi_initialize_subsystem();
619 if (ACPI_FAILURE(status)) {
620 printk(KERN_ERR PREFIX "Unable to initialize the ACPI Interpreter\n");
624 status = acpi_load_tables();
625 if (ACPI_FAILURE(status)) {
626 printk(KERN_ERR PREFIX "Unable to load the System Description Tables\n");
631 * Get a separate copy of the FADT for use by other drivers.
633 status = acpi_get_table(ACPI_TABLE_FADT, 1, &buffer);
634 if (ACPI_FAILURE(status)) {
635 printk(KERN_ERR PREFIX "Unable to get the FADT\n");
641 extern acpi_interrupt_flags acpi_sci_flags;
643 /* compatible (0) means level (3) */
644 if (acpi_sci_flags.trigger == 0)
645 acpi_sci_flags.trigger = 3;
647 /* Set PIC-mode SCI trigger type */
648 acpi_pic_sci_set_trigger(acpi_fadt.sci_int, acpi_sci_flags.trigger);
650 extern int acpi_sci_override_gsi;
652 * now that acpi_fadt is initialized,
653 * update it with result from INT_SRC_OVR parsing
655 acpi_fadt.sci_int = acpi_sci_override_gsi;
659 status = acpi_enable_subsystem(~(ACPI_NO_HARDWARE_INIT | ACPI_NO_ACPI_ENABLE));
660 if (ACPI_FAILURE(status)) {
661 printk(KERN_ERR PREFIX "Unable to enable ACPI\n");
676 acpi_status status = AE_OK;
677 extern acpi_status acpi_os_initialize1(void);
679 ACPI_FUNCTION_TRACE("acpi_bus_init");
681 status = acpi_os_initialize1();
683 status = acpi_enable_subsystem(ACPI_NO_HARDWARE_INIT | ACPI_NO_ACPI_ENABLE);
684 if (ACPI_FAILURE(status)) {
685 printk(KERN_ERR PREFIX "Unable to start the ACPI Interpreter\n");
689 if (ACPI_FAILURE(status)) {
690 printk(KERN_ERR PREFIX "Unable to initialize ACPI OS objects\n");
693 #ifdef CONFIG_ACPI_EC
695 * ACPI 2.0 requires the EC driver to be loaded and work before
696 * the EC device is found in the namespace (i.e. before acpi_initialize_objects()
699 * This is accomplished by looking for the ECDT table, and getting
700 * the EC parameters out of that.
702 status = acpi_ec_ecdt_probe();
703 /* Ignore result. Not having an ECDT is not fatal. */
706 status = acpi_initialize_objects(ACPI_FULL_INITIALIZATION);
707 if (ACPI_FAILURE(status)) {
708 printk(KERN_ERR PREFIX "Unable to initialize ACPI objects\n");
712 printk(KERN_INFO PREFIX "Interpreter enabled\n");
715 * Get the system interrupt model and evaluate \_PIC.
717 result = acpi_bus_init_irq();
722 * Register the for all standard device notifications.
724 status = acpi_install_notify_handler(ACPI_ROOT_OBJECT, ACPI_SYSTEM_NOTIFY, &acpi_bus_notify, NULL);
725 if (ACPI_FAILURE(status)) {
726 printk(KERN_ERR PREFIX "Unable to register for device notifications\n");
731 * Create the top ACPI proc directory
733 acpi_root_dir = proc_mkdir(ACPI_BUS_FILE_ROOT, NULL);
737 /* Mimic structured exception handling */
740 return_VALUE(-ENODEV);
743 decl_subsys(acpi,NULL,NULL);
745 static int __init acpi_init (void)
749 ACPI_FUNCTION_TRACE("acpi_init");
751 printk(KERN_INFO PREFIX "Subsystem revision %08x\n",
755 printk(KERN_INFO PREFIX "Interpreter disabled.\n");
756 return_VALUE(-ENODEV);
759 firmware_register(&acpi_subsys);
761 result = acpi_bus_init();
768 printk(KERN_INFO PREFIX "APM is already active, exiting\n");
776 return_VALUE(result);
779 subsys_initcall(acpi_init);