Merge with Linux 2.5.48.
[linux-2.6/linux-mips.git] / drivers / acpi / bus.c
blobfc65450f5345ee9c97fae3b9fed885eeb589192c
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/init.h>
26 #include <linux/ioport.h>
27 #include <linux/list.h>
28 #include <linux/sched.h>
29 #include <linux/pm.h>
30 #include <linux/device.h>
31 #include <linux/proc_fs.h>
32 #ifdef CONFIG_X86
33 #include <asm/mpspec.h>
34 #endif
35 #include "acpi_bus.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 /* --------------------------------------------------------------------------
51 Device Management
52 -------------------------------------------------------------------------- */
54 extern void acpi_bus_data_handler (
55 acpi_handle handle,
56 u32 function,
57 void *context);
58 int
59 acpi_bus_get_device (
60 acpi_handle handle,
61 struct acpi_device **device)
63 acpi_status status = AE_OK;
65 ACPI_FUNCTION_TRACE("acpi_bus_get_device");
67 if (!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",
75 handle));
76 return_VALUE(-ENODEV);
79 return_VALUE(0);
82 int
83 acpi_bus_get_status (
84 struct acpi_device *device)
86 acpi_status status = AE_OK;
87 unsigned long sta = 0;
89 ACPI_FUNCTION_TRACE("acpi_bus_get_status");
91 if (!device)
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;
110 else
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)));
116 return_VALUE(0);
120 /* --------------------------------------------------------------------------
121 Power Management
122 -------------------------------------------------------------------------- */
125 acpi_bus_get_power (
126 acpi_handle handle,
127 int *state)
129 int result = 0;
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);
137 if (result)
138 return_VALUE(result);
140 *state = ACPI_STATE_UNKNOWN;
142 if (!device->flags.power_manageable) {
143 /* TBD: Non-recursive algorithm for walking up hierarchy */
144 if (device->parent)
145 *state = device->parent->power.state;
146 else
147 *state = ACPI_STATE_D0;
149 else {
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",
156 NULL, &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);
163 if (result)
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));
173 return_VALUE(0);
178 acpi_bus_set_power (
179 acpi_handle handle,
180 int state)
182 int result = 0;
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);
190 if (result)
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));
204 return_VALUE(0);
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);
216 * Transition Power
217 * ----------------
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);
225 if (result)
226 goto end;
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)) {
232 result = -ENODEV;
233 goto end;
237 else {
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)) {
242 result = -ENODEV;
243 goto end;
246 if (device->power.flags.power_resources) {
247 result = acpi_power_transition(device, state);
248 if (result)
249 goto end;
253 end:
254 if (result)
255 ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Error transitioning device [%s] to D%d\n",
256 device->pnp.bus_id, state));
257 else
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 /* --------------------------------------------------------------------------
267 Event Management
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,
280 u8 type,
281 int data)
283 struct acpi_bus_event *event = NULL;
284 u32 flags = 0;
286 ACPI_FUNCTION_TRACE("acpi_bus_generate_event");
288 if (!device)
289 return_VALUE(-EINVAL);
291 /* drop event on the floor if no one's listening */
292 if (!event_is_open)
293 return_VALUE(0);
295 event = kmalloc(sizeof(struct acpi_bus_event), GFP_ATOMIC);
296 if (!event)
297 return_VALUE(-ENOMEM);
299 sprintf(event->device_class, "%s", device->pnp.device_class);
300 sprintf(event->bus_id, "%s", device->pnp.bus_id);
301 event->type = type;
302 event->data = data;
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);
310 return_VALUE(0);
314 acpi_bus_receive_event (
315 struct acpi_bus_event *event)
317 u32 flags = 0;
318 struct acpi_bus_event *entry = NULL;
320 DECLARE_WAITQUEUE(wait, current);
322 ACPI_FUNCTION_TRACE("acpi_bus_receive_event");
324 if (!event)
325 return -EINVAL;
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))
333 schedule();
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);
344 if (entry)
345 list_del(&entry->node);
346 spin_unlock_irqrestore(&acpi_bus_event_lock, flags);
348 if (!entry)
349 return_VALUE(-ENODEV);
351 memcpy(event, entry, sizeof(struct acpi_bus_event));
353 kfree(entry);
355 return_VALUE(0);
359 /* --------------------------------------------------------------------------
360 Notification Handling
361 -------------------------------------------------------------------------- */
363 static int
364 acpi_bus_check_device (
365 struct acpi_device *device,
366 int *status_changed)
368 acpi_status status = 0;
369 struct acpi_device_status old_status;
371 ACPI_FUNCTION_TRACE("acpi_bus_check_device");
373 if (!device)
374 return_VALUE(-EINVAL);
376 if (status_changed)
377 *status_changed = 0;
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)) {
388 if (status_changed)
389 *status_changed = 1;
391 return_VALUE(0);
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))
399 return_VALUE(0);
401 if (status_changed)
402 *status_changed = 1;
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 */
416 return_VALUE(0);
420 static int
421 acpi_bus_check_scope (
422 struct acpi_device *device)
424 int result = 0;
425 int status_changed = 0;
427 ACPI_FUNCTION_TRACE("acpi_bus_check_scope");
429 if (!device)
430 return_VALUE(-EINVAL);
432 /* Status Change? */
433 result = acpi_bus_check_device(device, &status_changed);
434 if (result)
435 return_VALUE(result);
437 if (!status_changed)
438 return_VALUE(0);
441 * TBD: Enumerate child devices within this device's scope and
442 * run acpi_bus_check_device()'s on them.
445 return_VALUE(0);
450 * acpi_bus_notify
451 * ---------------
452 * Callback for all 'system-level' device notifications (values 0x00-0x7F).
454 static void
455 acpi_bus_notify (
456 acpi_handle handle,
457 u32 type,
458 void *data)
460 int result = 0;
461 struct acpi_device *device = NULL;
463 ACPI_FUNCTION_TRACE("acpi_bus_notify");
465 if (acpi_bus_get_device(handle, &device))
466 return_VOID;
468 switch (type) {
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).
478 break;
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).
488 break;
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));
493 /* TBD */
494 break;
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));
499 /* TBD */
500 break;
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? */
506 break;
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));
511 /* TBD */
512 break;
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));
517 /* TBD */
518 break;
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));
523 /* TBD */
524 break;
526 default:
527 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Received unknown/unsupported notification [%08x]\n",
528 type));
529 break;
532 return_VOID;
535 /* --------------------------------------------------------------------------
536 Initialization/Cleanup
537 -------------------------------------------------------------------------- */
539 static int __init
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:
556 message = "PIC";
557 break;
558 case ACPI_IRQ_MODEL_IOAPIC:
559 message = "IOAPIC";
560 break;
561 case ACPI_IRQ_MODEL_IOSAPIC:
562 message = "IOSAPIC";
563 break;
564 default:
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);
579 return_VALUE(0);
583 static int __init
584 acpi_bus_init (void)
586 int result = 0;
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");
595 goto error0;
598 status = acpi_load_tables();
599 if (ACPI_FAILURE(status)) {
600 printk(KERN_ERR PREFIX "Unable to load the System Description Tables\n");
601 goto error0;
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");
610 goto error1;
613 #ifdef CONFIG_X86
614 /* Ensure the SCI is set to level-triggered, active-low */
615 if (acpi_ioapic)
616 mp_config_ioapic_for_sci(acpi_fadt.sci_int);
617 else
618 eisa_set_level_irq(acpi_fadt.sci_int);
619 #endif
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");
624 goto error1;
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()
631 * is called).
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))
638 goto error1;
639 #endif
641 status = acpi_initialize_objects(ACPI_FULL_INITIALIZATION);
642 if (ACPI_FAILURE(status)) {
643 printk(KERN_ERR PREFIX "Unable to initialize ACPI objects\n");
644 goto error1;
647 printk(KERN_INFO PREFIX "Interpreter enabled\n");
650 * Get the system interrupt model and evaluate \_PIC.
652 result = acpi_bus_init_irq();
653 if (result)
654 goto error1;
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");
662 goto error1;
666 * Create the top ACPI proc directory
668 acpi_root_dir = proc_mkdir(ACPI_BUS_FILE_ROOT, NULL);
670 return_VALUE(0);
672 /* Mimic structured exception handling */
673 error1:
674 acpi_terminate();
675 error0:
676 return_VALUE(-ENODEV);
679 struct subsystem acpi_subsys = {
680 .kobj = { .name = "acpi" },
683 static int __init acpi_init (void)
685 int result = 0;
687 ACPI_FUNCTION_TRACE("acpi_init");
689 printk(KERN_INFO PREFIX "Subsystem revision %08x\n",
690 ACPI_CA_VERSION);
692 /* Initial core debug level excludes drivers, so include them now */
693 acpi_set_debug(ACPI_DEBUG_LOW);
695 if (acpi_disabled) {
696 printk(KERN_INFO PREFIX "Disabled via command line (acpi=off)\n");
697 return -ENODEV;
700 firmware_register(&acpi_subsys);
702 result = acpi_bus_init();
704 if (!result) {
705 #ifdef CONFIG_PM
706 if (!PM_IS_ACTIVE())
707 pm_active = 1;
708 else {
709 printk(KERN_INFO PREFIX "APM is already active, exiting\n");
710 acpi_disabled = 1;
711 result = -ENODEV;
713 #endif
714 } else
715 acpi_disabled = 1;
717 return_VALUE(result);
721 static int __init acpi_setup(char *str)
723 while (str && *str) {
724 if (strncmp(str, "off", 3) == 0)
725 acpi_disabled = 1;
726 str = strchr(str, ',');
727 if (str)
728 str += strspn(str, ", \t");
730 return 1;
733 subsys_initcall(acpi_init);
735 __setup("acpi=", acpi_setup);