ACPI: power: don't cache power resource state
[firewire-audio.git] / drivers / acpi / bus.c
blobfdee82d37b7dd04ffdecf8ec7b0105560da37d25
1 /*
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/kernel.h>
29 #include <linux/list.h>
30 #include <linux/sched.h>
31 #include <linux/pm.h>
32 #include <linux/pm_legacy.h>
33 #include <linux/device.h>
34 #include <linux/proc_fs.h>
35 #ifdef CONFIG_X86
36 #include <asm/mpspec.h>
37 #endif
38 #include <acpi/acpi_bus.h>
39 #include <acpi/acpi_drivers.h>
41 #define _COMPONENT ACPI_BUS_COMPONENT
42 ACPI_MODULE_NAME("bus");
43 #ifdef CONFIG_X86
44 extern void __init acpi_pic_sci_set_trigger(unsigned int irq, u16 trigger);
45 #endif
47 struct acpi_device *acpi_root;
48 struct proc_dir_entry *acpi_root_dir;
49 EXPORT_SYMBOL(acpi_root_dir);
51 #define STRUCT_TO_INT(s) (*((int*)&s))
53 /* --------------------------------------------------------------------------
54 Device Management
55 -------------------------------------------------------------------------- */
57 int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
59 acpi_status status = AE_OK;
62 if (!device)
63 return -EINVAL;
65 /* TBD: Support fixed-feature devices */
67 status = acpi_get_data(handle, acpi_bus_data_handler, (void **)device);
68 if (ACPI_FAILURE(status) || !*device) {
69 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
70 handle));
71 return -ENODEV;
74 return 0;
77 EXPORT_SYMBOL(acpi_bus_get_device);
79 int acpi_bus_get_status(struct acpi_device *device)
81 acpi_status status = AE_OK;
82 unsigned long sta = 0;
85 if (!device)
86 return -EINVAL;
89 * Evaluate _STA if present.
91 if (device->flags.dynamic_status) {
92 status =
93 acpi_evaluate_integer(device->handle, "_STA", NULL, &sta);
94 if (ACPI_FAILURE(status))
95 return -ENODEV;
96 STRUCT_TO_INT(device->status) = (int)sta;
100 * Otherwise we assume the status of our parent (unless we don't
101 * have one, in which case status is implied).
103 else if (device->parent)
104 device->status = device->parent->status;
105 else
106 STRUCT_TO_INT(device->status) =
107 ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED |
108 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING;
110 if (device->status.functional && !device->status.present) {
111 printk(KERN_WARNING PREFIX "Device [%s] status [%08x]: "
112 "functional but not present; setting present\n",
113 device->pnp.bus_id, (u32) STRUCT_TO_INT(device->status));
114 device->status.present = 1;
117 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] status [%08x]\n",
118 device->pnp.bus_id,
119 (u32) STRUCT_TO_INT(device->status)));
121 return 0;
124 EXPORT_SYMBOL(acpi_bus_get_status);
126 /* --------------------------------------------------------------------------
127 Power Management
128 -------------------------------------------------------------------------- */
130 int acpi_bus_get_power(acpi_handle handle, int *state)
132 int result = 0;
133 acpi_status status = 0;
134 struct acpi_device *device = NULL;
135 unsigned long psc = 0;
138 result = acpi_bus_get_device(handle, &device);
139 if (result)
140 return result;
142 *state = ACPI_STATE_UNKNOWN;
144 if (!device->flags.power_manageable) {
145 /* TBD: Non-recursive algorithm for walking up hierarchy */
146 if (device->parent)
147 *state = device->parent->power.state;
148 else
149 *state = ACPI_STATE_D0;
150 } else {
152 * Get the device's power state either directly (via _PSC) or
153 * indirectly (via power resources).
155 if (device->power.flags.explicit_get) {
156 status = acpi_evaluate_integer(device->handle, "_PSC",
157 NULL, &psc);
158 if (ACPI_FAILURE(status))
159 return -ENODEV;
160 device->power.state = (int)psc;
161 } else if (device->power.flags.power_resources) {
162 result = acpi_power_get_inferred_state(device);
163 if (result)
164 return 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));
173 return 0;
176 EXPORT_SYMBOL(acpi_bus_get_power);
178 int acpi_bus_set_power(acpi_handle handle, int state)
180 int result = 0;
181 acpi_status status = AE_OK;
182 struct acpi_device *device = NULL;
183 char object_name[5] = { '_', 'P', 'S', '0' + state, '\0' };
186 result = acpi_bus_get_device(handle, &device);
187 if (result)
188 return result;
190 if ((state < ACPI_STATE_D0) || (state > ACPI_STATE_D3))
191 return -EINVAL;
193 /* Make sure this is a valid target state */
195 if (!device->flags.power_manageable) {
196 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device `[%s]' is not power manageable\n",
197 kobject_name(&device->dev.kobj)));
198 return -ENODEV;
201 * Get device's current power state
203 acpi_bus_get_power(device->handle, &device->power.state);
204 if ((state == device->power.state) && !device->flags.force_power_state) {
205 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device is already at D%d\n",
206 state));
207 return 0;
210 if (!device->power.states[state].flags.valid) {
211 printk(KERN_WARNING PREFIX "Device does not support D%d\n", state);
212 return -ENODEV;
214 if (device->parent && (state < device->parent->power.state)) {
215 printk(KERN_WARNING PREFIX
216 "Cannot set device to a higher-powered"
217 " state than parent\n");
218 return -ENODEV;
222 * Transition Power
223 * ----------------
224 * On transitions to a high-powered state we first apply power (via
225 * power resources) then evalute _PSx. Conversly for transitions to
226 * a lower-powered state.
228 if (state < device->power.state) {
229 if (device->power.flags.power_resources) {
230 result = acpi_power_transition(device, state);
231 if (result)
232 goto end;
234 if (device->power.states[state].flags.explicit_set) {
235 status = acpi_evaluate_object(device->handle,
236 object_name, NULL, NULL);
237 if (ACPI_FAILURE(status)) {
238 result = -ENODEV;
239 goto end;
242 } else {
243 if (device->power.states[state].flags.explicit_set) {
244 status = acpi_evaluate_object(device->handle,
245 object_name, NULL, NULL);
246 if (ACPI_FAILURE(status)) {
247 result = -ENODEV;
248 goto end;
251 if (device->power.flags.power_resources) {
252 result = acpi_power_transition(device, state);
253 if (result)
254 goto end;
258 end:
259 if (result)
260 printk(KERN_WARNING PREFIX
261 "Transitioning device [%s] to D%d\n",
262 device->pnp.bus_id, state);
263 else {
264 device->power.state = state;
265 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
266 "Device [%s] transitioned to D%d\n",
267 device->pnp.bus_id, state));
270 return result;
273 EXPORT_SYMBOL(acpi_bus_set_power);
275 /* --------------------------------------------------------------------------
276 Event Management
277 -------------------------------------------------------------------------- */
279 #ifdef CONFIG_ACPI_PROC_EVENT
280 static DEFINE_SPINLOCK(acpi_bus_event_lock);
282 LIST_HEAD(acpi_bus_event_list);
283 DECLARE_WAIT_QUEUE_HEAD(acpi_bus_event_queue);
285 extern int event_is_open;
287 int acpi_bus_generate_proc_event4(const char *device_class, const char *bus_id, u8 type, int data)
289 struct acpi_bus_event *event;
290 unsigned long flags = 0;
292 /* drop event on the floor if no one's listening */
293 if (!event_is_open)
294 return 0;
296 event = kmalloc(sizeof(struct acpi_bus_event), GFP_ATOMIC);
297 if (!event)
298 return -ENOMEM;
300 strcpy(event->device_class, device_class);
301 strcpy(event->bus_id, bus_id);
302 event->type = type;
303 event->data = data;
305 spin_lock_irqsave(&acpi_bus_event_lock, flags);
306 list_add_tail(&event->node, &acpi_bus_event_list);
307 spin_unlock_irqrestore(&acpi_bus_event_lock, flags);
309 wake_up_interruptible(&acpi_bus_event_queue);
311 return 0;
315 EXPORT_SYMBOL_GPL(acpi_bus_generate_proc_event4);
317 int acpi_bus_generate_proc_event(struct acpi_device *device, u8 type, int data)
319 if (!device)
320 return -EINVAL;
321 return acpi_bus_generate_proc_event4(device->pnp.device_class,
322 device->pnp.bus_id, type, data);
325 EXPORT_SYMBOL(acpi_bus_generate_proc_event);
327 int acpi_bus_receive_event(struct acpi_bus_event *event)
329 unsigned long flags = 0;
330 struct acpi_bus_event *entry = NULL;
332 DECLARE_WAITQUEUE(wait, current);
335 if (!event)
336 return -EINVAL;
338 if (list_empty(&acpi_bus_event_list)) {
340 set_current_state(TASK_INTERRUPTIBLE);
341 add_wait_queue(&acpi_bus_event_queue, &wait);
343 if (list_empty(&acpi_bus_event_list))
344 schedule();
346 remove_wait_queue(&acpi_bus_event_queue, &wait);
347 set_current_state(TASK_RUNNING);
349 if (signal_pending(current))
350 return -ERESTARTSYS;
353 spin_lock_irqsave(&acpi_bus_event_lock, flags);
354 entry =
355 list_entry(acpi_bus_event_list.next, struct acpi_bus_event, node);
356 if (entry)
357 list_del(&entry->node);
358 spin_unlock_irqrestore(&acpi_bus_event_lock, flags);
360 if (!entry)
361 return -ENODEV;
363 memcpy(event, entry, sizeof(struct acpi_bus_event));
365 kfree(entry);
367 return 0;
370 EXPORT_SYMBOL(acpi_bus_receive_event);
371 #endif /* CONFIG_ACPI_PROC_EVENT */
373 /* --------------------------------------------------------------------------
374 Notification Handling
375 -------------------------------------------------------------------------- */
377 static int
378 acpi_bus_check_device(struct acpi_device *device, int *status_changed)
380 acpi_status status = 0;
381 struct acpi_device_status old_status;
384 if (!device)
385 return -EINVAL;
387 if (status_changed)
388 *status_changed = 0;
390 old_status = device->status;
393 * Make sure this device's parent is present before we go about
394 * messing with the device.
396 if (device->parent && !device->parent->status.present) {
397 device->status = device->parent->status;
398 if (STRUCT_TO_INT(old_status) != STRUCT_TO_INT(device->status)) {
399 if (status_changed)
400 *status_changed = 1;
402 return 0;
405 status = acpi_bus_get_status(device);
406 if (ACPI_FAILURE(status))
407 return -ENODEV;
409 if (STRUCT_TO_INT(old_status) == STRUCT_TO_INT(device->status))
410 return 0;
412 if (status_changed)
413 *status_changed = 1;
416 * Device Insertion/Removal
418 if ((device->status.present) && !(old_status.present)) {
419 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device insertion detected\n"));
420 /* TBD: Handle device insertion */
421 } else if (!(device->status.present) && (old_status.present)) {
422 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device removal detected\n"));
423 /* TBD: Handle device removal */
426 return 0;
429 static int acpi_bus_check_scope(struct acpi_device *device)
431 int result = 0;
432 int status_changed = 0;
435 if (!device)
436 return -EINVAL;
438 /* Status Change? */
439 result = acpi_bus_check_device(device, &status_changed);
440 if (result)
441 return result;
443 if (!status_changed)
444 return 0;
447 * TBD: Enumerate child devices within this device's scope and
448 * run acpi_bus_check_device()'s on them.
451 return 0;
455 * acpi_bus_notify
456 * ---------------
457 * Callback for all 'system-level' device notifications (values 0x00-0x7F).
459 static void acpi_bus_notify(acpi_handle handle, u32 type, void *data)
461 int result = 0;
462 struct acpi_device *device = NULL;
465 if (acpi_bus_get_device(handle, &device))
466 return;
468 switch (type) {
470 case ACPI_NOTIFY_BUS_CHECK:
471 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
472 "Received BUS CHECK notification for device [%s]\n",
473 device->pnp.bus_id));
474 result = acpi_bus_check_scope(device);
476 * TBD: We'll need to outsource certain events to non-ACPI
477 * drivers via the device manager (device.c).
479 break;
481 case ACPI_NOTIFY_DEVICE_CHECK:
482 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
483 "Received DEVICE CHECK notification for device [%s]\n",
484 device->pnp.bus_id));
485 result = acpi_bus_check_device(device, NULL);
487 * TBD: We'll need to outsource certain events to non-ACPI
488 * drivers via the device manager (device.c).
490 break;
492 case ACPI_NOTIFY_DEVICE_WAKE:
493 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
494 "Received DEVICE WAKE notification for device [%s]\n",
495 device->pnp.bus_id));
496 /* TBD */
497 break;
499 case ACPI_NOTIFY_EJECT_REQUEST:
500 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
501 "Received EJECT REQUEST notification for device [%s]\n",
502 device->pnp.bus_id));
503 /* TBD */
504 break;
506 case ACPI_NOTIFY_DEVICE_CHECK_LIGHT:
507 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
508 "Received DEVICE CHECK LIGHT notification for device [%s]\n",
509 device->pnp.bus_id));
510 /* TBD: Exactly what does 'light' mean? */
511 break;
513 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
514 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
515 "Received FREQUENCY MISMATCH notification for device [%s]\n",
516 device->pnp.bus_id));
517 /* TBD */
518 break;
520 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
521 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
522 "Received BUS MODE MISMATCH notification for device [%s]\n",
523 device->pnp.bus_id));
524 /* TBD */
525 break;
527 case ACPI_NOTIFY_POWER_FAULT:
528 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
529 "Received POWER FAULT notification for device [%s]\n",
530 device->pnp.bus_id));
531 /* TBD */
532 break;
534 default:
535 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
536 "Received unknown/unsupported notification [%08x]\n",
537 type));
538 break;
541 return;
544 /* --------------------------------------------------------------------------
545 Initialization/Cleanup
546 -------------------------------------------------------------------------- */
548 static int __init acpi_bus_init_irq(void)
550 acpi_status status = AE_OK;
551 union acpi_object arg = { ACPI_TYPE_INTEGER };
552 struct acpi_object_list arg_list = { 1, &arg };
553 char *message = NULL;
557 * Let the system know what interrupt model we are using by
558 * evaluating the \_PIC object, if exists.
561 switch (acpi_irq_model) {
562 case ACPI_IRQ_MODEL_PIC:
563 message = "PIC";
564 break;
565 case ACPI_IRQ_MODEL_IOAPIC:
566 message = "IOAPIC";
567 break;
568 case ACPI_IRQ_MODEL_IOSAPIC:
569 message = "IOSAPIC";
570 break;
571 case ACPI_IRQ_MODEL_PLATFORM:
572 message = "platform specific model";
573 break;
574 default:
575 printk(KERN_WARNING PREFIX "Unknown interrupt routing model\n");
576 return -ENODEV;
579 printk(KERN_INFO PREFIX "Using %s for interrupt routing\n", message);
581 arg.integer.value = acpi_irq_model;
583 status = acpi_evaluate_object(NULL, "\\_PIC", &arg_list, NULL);
584 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
585 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PIC"));
586 return -ENODEV;
589 return 0;
592 acpi_native_uint acpi_gbl_permanent_mmap;
595 void __init acpi_early_init(void)
597 acpi_status status = AE_OK;
599 if (acpi_disabled)
600 return;
602 printk(KERN_INFO PREFIX "Core revision %08x\n", ACPI_CA_VERSION);
604 /* enable workarounds, unless strict ACPI spec. compliance */
605 if (!acpi_strict)
606 acpi_gbl_enable_interpreter_slack = TRUE;
608 acpi_gbl_permanent_mmap = 1;
610 status = acpi_reallocate_root_table();
611 if (ACPI_FAILURE(status)) {
612 printk(KERN_ERR PREFIX
613 "Unable to reallocate ACPI tables\n");
614 goto error0;
617 status = acpi_initialize_subsystem();
618 if (ACPI_FAILURE(status)) {
619 printk(KERN_ERR PREFIX
620 "Unable to initialize the ACPI Interpreter\n");
621 goto error0;
624 status = acpi_load_tables();
625 if (ACPI_FAILURE(status)) {
626 printk(KERN_ERR PREFIX
627 "Unable to load the System Description Tables\n");
628 goto error0;
631 #ifdef CONFIG_X86
632 if (!acpi_ioapic) {
633 extern u8 acpi_sci_flags;
635 /* compatible (0) means level (3) */
636 if (!(acpi_sci_flags & ACPI_MADT_TRIGGER_MASK)) {
637 acpi_sci_flags &= ~ACPI_MADT_TRIGGER_MASK;
638 acpi_sci_flags |= ACPI_MADT_TRIGGER_LEVEL;
640 /* Set PIC-mode SCI trigger type */
641 acpi_pic_sci_set_trigger(acpi_gbl_FADT.sci_interrupt,
642 (acpi_sci_flags & ACPI_MADT_TRIGGER_MASK) >> 2);
643 } else {
644 extern int acpi_sci_override_gsi;
646 * now that acpi_gbl_FADT is initialized,
647 * update it with result from INT_SRC_OVR parsing
649 acpi_gbl_FADT.sci_interrupt = acpi_sci_override_gsi;
651 #endif
653 status =
654 acpi_enable_subsystem(~
655 (ACPI_NO_HARDWARE_INIT |
656 ACPI_NO_ACPI_ENABLE));
657 if (ACPI_FAILURE(status)) {
658 printk(KERN_ERR PREFIX "Unable to enable ACPI\n");
659 goto error0;
662 return;
664 error0:
665 disable_acpi();
666 return;
669 static int __init acpi_bus_init(void)
671 int result = 0;
672 acpi_status status = AE_OK;
673 extern acpi_status acpi_os_initialize1(void);
676 status = acpi_os_initialize1();
678 status =
679 acpi_enable_subsystem(ACPI_NO_HARDWARE_INIT | ACPI_NO_ACPI_ENABLE);
680 if (ACPI_FAILURE(status)) {
681 printk(KERN_ERR PREFIX
682 "Unable to start the ACPI Interpreter\n");
683 goto error1;
686 if (ACPI_FAILURE(status)) {
687 printk(KERN_ERR PREFIX
688 "Unable to initialize ACPI OS objects\n");
689 goto error1;
691 #ifdef CONFIG_ACPI_EC
693 * ACPI 2.0 requires the EC driver to be loaded and work before
694 * the EC device is found in the namespace (i.e. before acpi_initialize_objects()
695 * is called).
697 * This is accomplished by looking for the ECDT table, and getting
698 * the EC parameters out of that.
700 status = acpi_ec_ecdt_probe();
701 /* Ignore result. Not having an ECDT is not fatal. */
702 #endif
704 status = acpi_initialize_objects(ACPI_FULL_INITIALIZATION);
705 if (ACPI_FAILURE(status)) {
706 printk(KERN_ERR PREFIX "Unable to initialize ACPI objects\n");
707 goto error1;
710 printk(KERN_INFO PREFIX "Interpreter enabled\n");
712 /* Initialize sleep structures */
713 acpi_sleep_init();
716 * Get the system interrupt model and evaluate \_PIC.
718 result = acpi_bus_init_irq();
719 if (result)
720 goto error1;
723 * Register the for all standard device notifications.
725 status =
726 acpi_install_notify_handler(ACPI_ROOT_OBJECT, ACPI_SYSTEM_NOTIFY,
727 &acpi_bus_notify, NULL);
728 if (ACPI_FAILURE(status)) {
729 printk(KERN_ERR PREFIX
730 "Unable to register for device notifications\n");
731 goto error1;
735 * Create the top ACPI proc directory
737 acpi_root_dir = proc_mkdir(ACPI_BUS_FILE_ROOT, NULL);
739 return 0;
741 /* Mimic structured exception handling */
742 error1:
743 acpi_terminate();
744 return -ENODEV;
747 decl_subsys(acpi, NULL, NULL);
749 static int __init acpi_init(void)
751 int result = 0;
754 if (acpi_disabled) {
755 printk(KERN_INFO PREFIX "Interpreter disabled.\n");
756 return -ENODEV;
759 result = firmware_register(&acpi_subsys);
760 if (result < 0)
761 printk(KERN_WARNING "%s: firmware_register error: %d\n",
762 __FUNCTION__, result);
764 result = acpi_bus_init();
766 if (!result) {
767 #ifdef CONFIG_PM_LEGACY
768 if (!PM_IS_ACTIVE())
769 pm_active = 1;
770 else {
771 printk(KERN_INFO PREFIX
772 "APM is already active, exiting\n");
773 disable_acpi();
774 result = -ENODEV;
776 #endif
777 } else
778 disable_acpi();
780 return result;
783 subsys_initcall(acpi_init);