ACPI: drivers/acpi: elide a non-zero test on a result that is never 0
[linux/fpc-iii.git] / drivers / acpi / bus.c
blob5b6760e0f9571d20e34dcb47346c354ad6dff116
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/device.h>
33 #include <linux/proc_fs.h>
34 #include <linux/acpi.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");
44 struct acpi_device *acpi_root;
45 struct proc_dir_entry *acpi_root_dir;
46 EXPORT_SYMBOL(acpi_root_dir);
48 #define STRUCT_TO_INT(s) (*((int*)&s))
50 /* --------------------------------------------------------------------------
51 Device Management
52 -------------------------------------------------------------------------- */
54 int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
56 acpi_status status = AE_OK;
59 if (!device)
60 return -EINVAL;
62 /* TBD: Support fixed-feature devices */
64 status = acpi_get_data(handle, acpi_bus_data_handler, (void **)device);
65 if (ACPI_FAILURE(status) || !*device) {
66 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
67 handle));
68 return -ENODEV;
71 return 0;
74 EXPORT_SYMBOL(acpi_bus_get_device);
76 int acpi_bus_get_status(struct acpi_device *device)
78 acpi_status status = AE_OK;
79 unsigned long sta = 0;
82 if (!device)
83 return -EINVAL;
86 * Evaluate _STA if present.
88 if (device->flags.dynamic_status) {
89 status =
90 acpi_evaluate_integer(device->handle, "_STA", NULL, &sta);
91 if (ACPI_FAILURE(status))
92 return -ENODEV;
93 STRUCT_TO_INT(device->status) = (int)sta;
97 * Otherwise we assume the status of our parent (unless we don't
98 * have one, in which case status is implied).
100 else if (device->parent)
101 device->status = device->parent->status;
102 else
103 STRUCT_TO_INT(device->status) =
104 ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED |
105 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING;
107 if (device->status.functional && !device->status.present) {
108 printk(KERN_WARNING PREFIX "Device [%s] status [%08x]: "
109 "functional but not present; setting present\n",
110 device->pnp.bus_id, (u32) STRUCT_TO_INT(device->status));
111 device->status.present = 1;
114 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] status [%08x]\n",
115 device->pnp.bus_id,
116 (u32) STRUCT_TO_INT(device->status)));
118 return 0;
121 EXPORT_SYMBOL(acpi_bus_get_status);
123 void acpi_bus_private_data_handler(acpi_handle handle,
124 u32 function, void *context)
126 return;
128 EXPORT_SYMBOL(acpi_bus_private_data_handler);
130 int acpi_bus_get_private_data(acpi_handle handle, void **data)
132 acpi_status status = AE_OK;
134 if (!*data)
135 return -EINVAL;
137 status = acpi_get_data(handle, acpi_bus_private_data_handler, data);
138 if (ACPI_FAILURE(status) || !*data) {
139 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
140 handle));
141 return -ENODEV;
144 return 0;
146 EXPORT_SYMBOL(acpi_bus_get_private_data);
148 /* --------------------------------------------------------------------------
149 Power Management
150 -------------------------------------------------------------------------- */
152 int acpi_bus_get_power(acpi_handle handle, int *state)
154 int result = 0;
155 acpi_status status = 0;
156 struct acpi_device *device = NULL;
157 unsigned long psc = 0;
160 result = acpi_bus_get_device(handle, &device);
161 if (result)
162 return result;
164 *state = ACPI_STATE_UNKNOWN;
166 if (!device->flags.power_manageable) {
167 /* TBD: Non-recursive algorithm for walking up hierarchy */
168 if (device->parent)
169 *state = device->parent->power.state;
170 else
171 *state = ACPI_STATE_D0;
172 } else {
174 * Get the device's power state either directly (via _PSC) or
175 * indirectly (via power resources).
177 if (device->power.flags.explicit_get) {
178 status = acpi_evaluate_integer(device->handle, "_PSC",
179 NULL, &psc);
180 if (ACPI_FAILURE(status))
181 return -ENODEV;
182 device->power.state = (int)psc;
183 } else if (device->power.flags.power_resources) {
184 result = acpi_power_get_inferred_state(device);
185 if (result)
186 return result;
189 *state = device->power.state;
192 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] power state is D%d\n",
193 device->pnp.bus_id, device->power.state));
195 return 0;
198 EXPORT_SYMBOL(acpi_bus_get_power);
200 int acpi_bus_set_power(acpi_handle handle, int state)
202 int result = 0;
203 acpi_status status = AE_OK;
204 struct acpi_device *device = NULL;
205 char object_name[5] = { '_', 'P', 'S', '0' + state, '\0' };
208 result = acpi_bus_get_device(handle, &device);
209 if (result)
210 return result;
212 if ((state < ACPI_STATE_D0) || (state > ACPI_STATE_D3))
213 return -EINVAL;
215 /* Make sure this is a valid target state */
217 if (!device->flags.power_manageable) {
218 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device `[%s]' is not power manageable\n",
219 kobject_name(&device->dev.kobj)));
220 return -ENODEV;
223 * Get device's current power state
225 acpi_bus_get_power(device->handle, &device->power.state);
226 if ((state == device->power.state) && !device->flags.force_power_state) {
227 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device is already at D%d\n",
228 state));
229 return 0;
232 if (!device->power.states[state].flags.valid) {
233 printk(KERN_WARNING PREFIX "Device does not support D%d\n", state);
234 return -ENODEV;
236 if (device->parent && (state < device->parent->power.state)) {
237 printk(KERN_WARNING PREFIX
238 "Cannot set device to a higher-powered"
239 " state than parent\n");
240 return -ENODEV;
244 * Transition Power
245 * ----------------
246 * On transitions to a high-powered state we first apply power (via
247 * power resources) then evalute _PSx. Conversly for transitions to
248 * a lower-powered state.
250 if (state < device->power.state) {
251 if (device->power.flags.power_resources) {
252 result = acpi_power_transition(device, state);
253 if (result)
254 goto end;
256 if (device->power.states[state].flags.explicit_set) {
257 status = acpi_evaluate_object(device->handle,
258 object_name, NULL, NULL);
259 if (ACPI_FAILURE(status)) {
260 result = -ENODEV;
261 goto end;
264 } else {
265 if (device->power.states[state].flags.explicit_set) {
266 status = acpi_evaluate_object(device->handle,
267 object_name, NULL, NULL);
268 if (ACPI_FAILURE(status)) {
269 result = -ENODEV;
270 goto end;
273 if (device->power.flags.power_resources) {
274 result = acpi_power_transition(device, state);
275 if (result)
276 goto end;
280 end:
281 if (result)
282 printk(KERN_WARNING PREFIX
283 "Transitioning device [%s] to D%d\n",
284 device->pnp.bus_id, state);
285 else {
286 device->power.state = state;
287 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
288 "Device [%s] transitioned to D%d\n",
289 device->pnp.bus_id, state));
292 return result;
295 EXPORT_SYMBOL(acpi_bus_set_power);
297 /* --------------------------------------------------------------------------
298 Event Management
299 -------------------------------------------------------------------------- */
301 #ifdef CONFIG_ACPI_PROC_EVENT
302 static DEFINE_SPINLOCK(acpi_bus_event_lock);
304 LIST_HEAD(acpi_bus_event_list);
305 DECLARE_WAIT_QUEUE_HEAD(acpi_bus_event_queue);
307 extern int event_is_open;
309 int acpi_bus_generate_proc_event4(const char *device_class, const char *bus_id, u8 type, int data)
311 struct acpi_bus_event *event;
312 unsigned long flags = 0;
314 /* drop event on the floor if no one's listening */
315 if (!event_is_open)
316 return 0;
318 event = kmalloc(sizeof(struct acpi_bus_event), GFP_ATOMIC);
319 if (!event)
320 return -ENOMEM;
322 strcpy(event->device_class, device_class);
323 strcpy(event->bus_id, bus_id);
324 event->type = type;
325 event->data = data;
327 spin_lock_irqsave(&acpi_bus_event_lock, flags);
328 list_add_tail(&event->node, &acpi_bus_event_list);
329 spin_unlock_irqrestore(&acpi_bus_event_lock, flags);
331 wake_up_interruptible(&acpi_bus_event_queue);
333 return 0;
337 EXPORT_SYMBOL_GPL(acpi_bus_generate_proc_event4);
339 int acpi_bus_generate_proc_event(struct acpi_device *device, u8 type, int data)
341 if (!device)
342 return -EINVAL;
343 return acpi_bus_generate_proc_event4(device->pnp.device_class,
344 device->pnp.bus_id, type, data);
347 EXPORT_SYMBOL(acpi_bus_generate_proc_event);
349 int acpi_bus_receive_event(struct acpi_bus_event *event)
351 unsigned long flags = 0;
352 struct acpi_bus_event *entry = NULL;
354 DECLARE_WAITQUEUE(wait, current);
357 if (!event)
358 return -EINVAL;
360 if (list_empty(&acpi_bus_event_list)) {
362 set_current_state(TASK_INTERRUPTIBLE);
363 add_wait_queue(&acpi_bus_event_queue, &wait);
365 if (list_empty(&acpi_bus_event_list))
366 schedule();
368 remove_wait_queue(&acpi_bus_event_queue, &wait);
369 set_current_state(TASK_RUNNING);
371 if (signal_pending(current))
372 return -ERESTARTSYS;
375 spin_lock_irqsave(&acpi_bus_event_lock, flags);
376 entry =
377 list_entry(acpi_bus_event_list.next, struct acpi_bus_event, node);
378 if (entry)
379 list_del(&entry->node);
380 spin_unlock_irqrestore(&acpi_bus_event_lock, flags);
382 if (!entry)
383 return -ENODEV;
385 memcpy(event, entry, sizeof(struct acpi_bus_event));
387 kfree(entry);
389 return 0;
392 #endif /* CONFIG_ACPI_PROC_EVENT */
394 /* --------------------------------------------------------------------------
395 Notification Handling
396 -------------------------------------------------------------------------- */
398 static int
399 acpi_bus_check_device(struct acpi_device *device, int *status_changed)
401 acpi_status status = 0;
402 struct acpi_device_status old_status;
405 if (!device)
406 return -EINVAL;
408 if (status_changed)
409 *status_changed = 0;
411 old_status = device->status;
414 * Make sure this device's parent is present before we go about
415 * messing with the device.
417 if (device->parent && !device->parent->status.present) {
418 device->status = device->parent->status;
419 if (STRUCT_TO_INT(old_status) != STRUCT_TO_INT(device->status)) {
420 if (status_changed)
421 *status_changed = 1;
423 return 0;
426 status = acpi_bus_get_status(device);
427 if (ACPI_FAILURE(status))
428 return -ENODEV;
430 if (STRUCT_TO_INT(old_status) == STRUCT_TO_INT(device->status))
431 return 0;
433 if (status_changed)
434 *status_changed = 1;
437 * Device Insertion/Removal
439 if ((device->status.present) && !(old_status.present)) {
440 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device insertion detected\n"));
441 /* TBD: Handle device insertion */
442 } else if (!(device->status.present) && (old_status.present)) {
443 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device removal detected\n"));
444 /* TBD: Handle device removal */
447 return 0;
450 static int acpi_bus_check_scope(struct acpi_device *device)
452 int result = 0;
453 int status_changed = 0;
456 if (!device)
457 return -EINVAL;
459 /* Status Change? */
460 result = acpi_bus_check_device(device, &status_changed);
461 if (result)
462 return result;
464 if (!status_changed)
465 return 0;
468 * TBD: Enumerate child devices within this device's scope and
469 * run acpi_bus_check_device()'s on them.
472 return 0;
476 * acpi_bus_notify
477 * ---------------
478 * Callback for all 'system-level' device notifications (values 0x00-0x7F).
480 static void acpi_bus_notify(acpi_handle handle, u32 type, void *data)
482 int result = 0;
483 struct acpi_device *device = NULL;
486 if (acpi_bus_get_device(handle, &device))
487 return;
489 switch (type) {
491 case ACPI_NOTIFY_BUS_CHECK:
492 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
493 "Received BUS CHECK notification for device [%s]\n",
494 device->pnp.bus_id));
495 result = acpi_bus_check_scope(device);
497 * TBD: We'll need to outsource certain events to non-ACPI
498 * drivers via the device manager (device.c).
500 break;
502 case ACPI_NOTIFY_DEVICE_CHECK:
503 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
504 "Received DEVICE CHECK notification for device [%s]\n",
505 device->pnp.bus_id));
506 result = acpi_bus_check_device(device, NULL);
508 * TBD: We'll need to outsource certain events to non-ACPI
509 * drivers via the device manager (device.c).
511 break;
513 case ACPI_NOTIFY_DEVICE_WAKE:
514 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
515 "Received DEVICE WAKE notification for device [%s]\n",
516 device->pnp.bus_id));
517 /* TBD */
518 break;
520 case ACPI_NOTIFY_EJECT_REQUEST:
521 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
522 "Received EJECT REQUEST notification for device [%s]\n",
523 device->pnp.bus_id));
524 /* TBD */
525 break;
527 case ACPI_NOTIFY_DEVICE_CHECK_LIGHT:
528 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
529 "Received DEVICE CHECK LIGHT notification for device [%s]\n",
530 device->pnp.bus_id));
531 /* TBD: Exactly what does 'light' mean? */
532 break;
534 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
535 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
536 "Received FREQUENCY MISMATCH notification for device [%s]\n",
537 device->pnp.bus_id));
538 /* TBD */
539 break;
541 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
542 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
543 "Received BUS MODE MISMATCH notification for device [%s]\n",
544 device->pnp.bus_id));
545 /* TBD */
546 break;
548 case ACPI_NOTIFY_POWER_FAULT:
549 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
550 "Received POWER FAULT notification for device [%s]\n",
551 device->pnp.bus_id));
552 /* TBD */
553 break;
555 default:
556 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
557 "Received unknown/unsupported notification [%08x]\n",
558 type));
559 break;
562 return;
565 /* --------------------------------------------------------------------------
566 Initialization/Cleanup
567 -------------------------------------------------------------------------- */
569 static int __init acpi_bus_init_irq(void)
571 acpi_status status = AE_OK;
572 union acpi_object arg = { ACPI_TYPE_INTEGER };
573 struct acpi_object_list arg_list = { 1, &arg };
574 char *message = NULL;
578 * Let the system know what interrupt model we are using by
579 * evaluating the \_PIC object, if exists.
582 switch (acpi_irq_model) {
583 case ACPI_IRQ_MODEL_PIC:
584 message = "PIC";
585 break;
586 case ACPI_IRQ_MODEL_IOAPIC:
587 message = "IOAPIC";
588 break;
589 case ACPI_IRQ_MODEL_IOSAPIC:
590 message = "IOSAPIC";
591 break;
592 case ACPI_IRQ_MODEL_PLATFORM:
593 message = "platform specific model";
594 break;
595 default:
596 printk(KERN_WARNING PREFIX "Unknown interrupt routing model\n");
597 return -ENODEV;
600 printk(KERN_INFO PREFIX "Using %s for interrupt routing\n", message);
602 arg.integer.value = acpi_irq_model;
604 status = acpi_evaluate_object(NULL, "\\_PIC", &arg_list, NULL);
605 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
606 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PIC"));
607 return -ENODEV;
610 return 0;
613 acpi_native_uint acpi_gbl_permanent_mmap;
616 void __init acpi_early_init(void)
618 acpi_status status = AE_OK;
620 if (acpi_disabled)
621 return;
623 printk(KERN_INFO PREFIX "Core revision %08x\n", ACPI_CA_VERSION);
625 /* enable workarounds, unless strict ACPI spec. compliance */
626 if (!acpi_strict)
627 acpi_gbl_enable_interpreter_slack = TRUE;
629 acpi_gbl_permanent_mmap = 1;
631 status = acpi_reallocate_root_table();
632 if (ACPI_FAILURE(status)) {
633 printk(KERN_ERR PREFIX
634 "Unable to reallocate ACPI tables\n");
635 goto error0;
638 status = acpi_initialize_subsystem();
639 if (ACPI_FAILURE(status)) {
640 printk(KERN_ERR PREFIX
641 "Unable to initialize the ACPI Interpreter\n");
642 goto error0;
645 status = acpi_load_tables();
646 if (ACPI_FAILURE(status)) {
647 printk(KERN_ERR PREFIX
648 "Unable to load the System Description Tables\n");
649 goto error0;
652 #ifdef CONFIG_X86
653 if (!acpi_ioapic) {
654 /* compatible (0) means level (3) */
655 if (!(acpi_sci_flags & ACPI_MADT_TRIGGER_MASK)) {
656 acpi_sci_flags &= ~ACPI_MADT_TRIGGER_MASK;
657 acpi_sci_flags |= ACPI_MADT_TRIGGER_LEVEL;
659 /* Set PIC-mode SCI trigger type */
660 acpi_pic_sci_set_trigger(acpi_gbl_FADT.sci_interrupt,
661 (acpi_sci_flags & ACPI_MADT_TRIGGER_MASK) >> 2);
662 } else {
664 * now that acpi_gbl_FADT is initialized,
665 * update it with result from INT_SRC_OVR parsing
667 acpi_gbl_FADT.sci_interrupt = acpi_sci_override_gsi;
669 #endif
671 status =
672 acpi_enable_subsystem(~
673 (ACPI_NO_HARDWARE_INIT |
674 ACPI_NO_ACPI_ENABLE));
675 if (ACPI_FAILURE(status)) {
676 printk(KERN_ERR PREFIX "Unable to enable ACPI\n");
677 goto error0;
680 return;
682 error0:
683 disable_acpi();
684 return;
687 static int __init acpi_bus_init(void)
689 int result = 0;
690 acpi_status status = AE_OK;
691 extern acpi_status acpi_os_initialize1(void);
694 status = acpi_os_initialize1();
696 status =
697 acpi_enable_subsystem(ACPI_NO_HARDWARE_INIT | ACPI_NO_ACPI_ENABLE);
698 if (ACPI_FAILURE(status)) {
699 printk(KERN_ERR PREFIX
700 "Unable to start the ACPI Interpreter\n");
701 goto error1;
704 if (ACPI_FAILURE(status)) {
705 printk(KERN_ERR PREFIX
706 "Unable to initialize ACPI OS objects\n");
707 goto error1;
709 #ifdef CONFIG_ACPI_EC
711 * ACPI 2.0 requires the EC driver to be loaded and work before
712 * the EC device is found in the namespace (i.e. before acpi_initialize_objects()
713 * is called).
715 * This is accomplished by looking for the ECDT table, and getting
716 * the EC parameters out of that.
718 status = acpi_ec_ecdt_probe();
719 /* Ignore result. Not having an ECDT is not fatal. */
720 #endif
722 status = acpi_initialize_objects(ACPI_FULL_INITIALIZATION);
723 if (ACPI_FAILURE(status)) {
724 printk(KERN_ERR PREFIX "Unable to initialize ACPI objects\n");
725 goto error1;
728 printk(KERN_INFO PREFIX "Interpreter enabled\n");
730 /* Initialize sleep structures */
731 acpi_sleep_init();
734 * Get the system interrupt model and evaluate \_PIC.
736 result = acpi_bus_init_irq();
737 if (result)
738 goto error1;
741 * Register the for all standard device notifications.
743 status =
744 acpi_install_notify_handler(ACPI_ROOT_OBJECT, ACPI_SYSTEM_NOTIFY,
745 &acpi_bus_notify, NULL);
746 if (ACPI_FAILURE(status)) {
747 printk(KERN_ERR PREFIX
748 "Unable to register for device notifications\n");
749 goto error1;
753 * Create the top ACPI proc directory
755 acpi_root_dir = proc_mkdir(ACPI_BUS_FILE_ROOT, NULL);
757 return 0;
759 /* Mimic structured exception handling */
760 error1:
761 acpi_terminate();
762 return -ENODEV;
765 struct kobject *acpi_kobj;
767 static int __init acpi_init(void)
769 int result = 0;
772 if (acpi_disabled) {
773 printk(KERN_INFO PREFIX "Interpreter disabled.\n");
774 return -ENODEV;
777 acpi_kobj = kobject_create_and_add("acpi", firmware_kobj);
778 if (!acpi_kobj) {
779 printk(KERN_WARNING "%s: kset create error\n", __func__);
780 acpi_kobj = NULL;
783 result = acpi_bus_init();
785 if (!result) {
786 if (!(pm_flags & PM_APM))
787 pm_flags |= PM_ACPI;
788 else {
789 printk(KERN_INFO PREFIX
790 "APM is already active, exiting\n");
791 disable_acpi();
792 result = -ENODEV;
794 } else
795 disable_acpi();
797 return result;
800 subsys_initcall(acpi_init);