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>
35 #include <acpi/acpi_bus.h>
36 #include <acpi/acpi_drivers.h>
39 #define _COMPONENT ACPI_BUS_COMPONENT
40 ACPI_MODULE_NAME ("acpi_bus")
43 extern void __init acpi_pic_sci_set_trigger(unsigned int irq, u16 trigger);
46 FADT_DESCRIPTOR acpi_fadt;
47 struct acpi_device *acpi_root;
48 struct proc_dir_entry *acpi_root_dir;
50 #define STRUCT_TO_INT(s) (*((int*)&s))
52 /* --------------------------------------------------------------------------
54 -------------------------------------------------------------------------- */
56 extern void acpi_bus_data_handler (
63 struct acpi_device **device)
65 acpi_status status = AE_OK;
67 ACPI_FUNCTION_TRACE("acpi_bus_get_device");
70 return_VALUE(-EINVAL);
72 /* TBD: Support fixed-feature devices */
74 status = acpi_get_data(handle, acpi_bus_data_handler, (void**) device);
75 if (ACPI_FAILURE(status) || !*device) {
76 ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Error getting context for object [%p]\n",
78 return_VALUE(-ENODEV);
86 struct acpi_device *device)
88 acpi_status status = AE_OK;
89 unsigned long sta = 0;
91 ACPI_FUNCTION_TRACE("acpi_bus_get_status");
94 return_VALUE(-EINVAL);
97 * Evaluate _STA if present.
99 if (device->flags.dynamic_status) {
100 status = acpi_evaluate_integer(device->handle, "_STA", NULL, &sta);
101 if (ACPI_FAILURE(status))
102 return_VALUE(-ENODEV);
103 STRUCT_TO_INT(device->status) = (int) sta;
107 * Otherwise we assume the status of our parent (unless we don't
108 * have one, in which case status is implied).
110 else if (device->parent)
111 device->status = device->parent->status;
113 STRUCT_TO_INT(device->status) = 0x0F;
115 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] status [%08x]\n",
116 device->pnp.bus_id, (u32) STRUCT_TO_INT(device->status)));
122 /* --------------------------------------------------------------------------
124 -------------------------------------------------------------------------- */
132 acpi_status status = 0;
133 struct acpi_device *device = NULL;
134 unsigned long psc = 0;
136 ACPI_FUNCTION_TRACE("acpi_bus_get_power");
138 result = acpi_bus_get_device(handle, &device);
140 return_VALUE(result);
142 *state = ACPI_STATE_UNKNOWN;
144 if (!device->flags.power_manageable) {
145 /* TBD: Non-recursive algorithm for walking up hierarchy */
147 *state = device->parent->power.state;
149 *state = ACPI_STATE_D0;
153 * Get the device's power state either directly (via _PSC) or
154 * indirectly (via power resources).
156 if (device->power.flags.explicit_get) {
157 status = acpi_evaluate_integer(device->handle, "_PSC",
159 if (ACPI_FAILURE(status))
160 return_VALUE(-ENODEV);
161 device->power.state = (int) psc;
163 else if (device->power.flags.power_resources) {
164 result = acpi_power_get_inferred_state(device);
166 return_VALUE(result);
169 *state = device->power.state;
172 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] power state is D%d\n",
173 device->pnp.bus_id, device->power.state));
185 acpi_status status = AE_OK;
186 struct acpi_device *device = NULL;
187 char object_name[5] = {'_','P','S','0'+state,'\0'};
189 ACPI_FUNCTION_TRACE("acpi_bus_set_power");
191 result = acpi_bus_get_device(handle, &device);
193 return_VALUE(result);
195 if ((state < ACPI_STATE_D0) || (state > ACPI_STATE_D3))
196 return_VALUE(-EINVAL);
198 /* Make sure this is a valid target state */
200 if (!device->flags.power_manageable) {
201 ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Device is not power manageable\n"));
202 return_VALUE(-ENODEV);
204 if (state == device->power.state) {
205 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device is already at D%d\n", state));
208 if (!device->power.states[state].flags.valid) {
209 ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Device does not support D%d\n", state));
210 return_VALUE(-ENODEV);
212 if (device->parent && (state < device->parent->power.state)) {
213 ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Cannot set device to a higher-powered state than parent\n"));
214 return_VALUE(-ENODEV);
220 * On transitions to a high-powered state we first apply power (via
221 * power resources) then evalute _PSx. Conversly for transitions to
222 * a lower-powered state.
224 if (state < device->power.state) {
225 if (device->power.flags.power_resources) {
226 result = acpi_power_transition(device, state);
230 if (device->power.states[state].flags.explicit_set) {
231 status = acpi_evaluate_object(device->handle,
232 object_name, NULL, NULL);
233 if (ACPI_FAILURE(status)) {
240 if (device->power.states[state].flags.explicit_set) {
241 status = acpi_evaluate_object(device->handle,
242 object_name, NULL, NULL);
243 if (ACPI_FAILURE(status)) {
248 if (device->power.flags.power_resources) {
249 result = acpi_power_transition(device, state);
257 ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Error transitioning device [%s] to D%d\n",
258 device->pnp.bus_id, state));
260 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] transitioned to D%d\n",
261 device->pnp.bus_id, state));
263 return_VALUE(result);
268 /* --------------------------------------------------------------------------
270 -------------------------------------------------------------------------- */
272 static spinlock_t acpi_bus_event_lock = SPIN_LOCK_UNLOCKED;
274 LIST_HEAD(acpi_bus_event_list);
275 DECLARE_WAIT_QUEUE_HEAD(acpi_bus_event_queue);
277 extern int event_is_open;
280 acpi_bus_generate_event (
281 struct acpi_device *device,
285 struct acpi_bus_event *event = NULL;
286 unsigned long flags = 0;
288 ACPI_FUNCTION_TRACE("acpi_bus_generate_event");
291 return_VALUE(-EINVAL);
293 /* drop event on the floor if no one's listening */
297 event = kmalloc(sizeof(struct acpi_bus_event), GFP_ATOMIC);
299 return_VALUE(-ENOMEM);
301 strcpy(event->device_class, device->pnp.device_class);
302 strcpy(event->bus_id, device->pnp.bus_id);
306 spin_lock_irqsave(&acpi_bus_event_lock, flags);
307 list_add_tail(&event->node, &acpi_bus_event_list);
308 spin_unlock_irqrestore(&acpi_bus_event_lock, flags);
310 wake_up_interruptible(&acpi_bus_event_queue);
316 acpi_bus_receive_event (
317 struct acpi_bus_event *event)
319 unsigned long flags = 0;
320 struct acpi_bus_event *entry = NULL;
322 DECLARE_WAITQUEUE(wait, current);
324 ACPI_FUNCTION_TRACE("acpi_bus_receive_event");
329 if (list_empty(&acpi_bus_event_list)) {
331 set_current_state(TASK_INTERRUPTIBLE);
332 add_wait_queue(&acpi_bus_event_queue, &wait);
334 if (list_empty(&acpi_bus_event_list))
337 remove_wait_queue(&acpi_bus_event_queue, &wait);
338 set_current_state(TASK_RUNNING);
340 if (signal_pending(current))
341 return_VALUE(-ERESTARTSYS);
344 spin_lock_irqsave(&acpi_bus_event_lock, flags);
345 entry = list_entry(acpi_bus_event_list.next, struct acpi_bus_event, node);
347 list_del(&entry->node);
348 spin_unlock_irqrestore(&acpi_bus_event_lock, flags);
351 return_VALUE(-ENODEV);
353 memcpy(event, entry, sizeof(struct acpi_bus_event));
361 /* --------------------------------------------------------------------------
362 Notification Handling
363 -------------------------------------------------------------------------- */
366 acpi_bus_check_device (
367 struct acpi_device *device,
370 acpi_status status = 0;
371 struct acpi_device_status old_status;
373 ACPI_FUNCTION_TRACE("acpi_bus_check_device");
376 return_VALUE(-EINVAL);
381 old_status = device->status;
384 * Make sure this device's parent is present before we go about
385 * messing with the device.
387 if (device->parent && !device->parent->status.present) {
388 device->status = device->parent->status;
389 if (STRUCT_TO_INT(old_status) != STRUCT_TO_INT(device->status)) {
396 status = acpi_bus_get_status(device);
397 if (ACPI_FAILURE(status))
398 return_VALUE(-ENODEV);
400 if (STRUCT_TO_INT(old_status) == STRUCT_TO_INT(device->status))
407 * Device Insertion/Removal
409 if ((device->status.present) && !(old_status.present)) {
410 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device insertion detected\n"));
411 /* TBD: Handle device insertion */
413 else if (!(device->status.present) && (old_status.present)) {
414 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device removal detected\n"));
415 /* TBD: Handle device removal */
423 acpi_bus_check_scope (
424 struct acpi_device *device)
427 int status_changed = 0;
429 ACPI_FUNCTION_TRACE("acpi_bus_check_scope");
432 return_VALUE(-EINVAL);
435 result = acpi_bus_check_device(device, &status_changed);
437 return_VALUE(result);
443 * TBD: Enumerate child devices within this device's scope and
444 * run acpi_bus_check_device()'s on them.
454 * Callback for all 'system-level' device notifications (values 0x00-0x7F).
463 struct acpi_device *device = NULL;
465 ACPI_FUNCTION_TRACE("acpi_bus_notify");
467 if (acpi_bus_get_device(handle, &device))
472 case ACPI_NOTIFY_BUS_CHECK:
473 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Received BUS CHECK notification for device [%s]\n",
474 device->pnp.bus_id));
475 result = acpi_bus_check_scope(device);
477 * TBD: We'll need to outsource certain events to non-ACPI
478 * drivers via the device manager (device.c).
482 case ACPI_NOTIFY_DEVICE_CHECK:
483 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "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).
492 case ACPI_NOTIFY_DEVICE_WAKE:
493 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Received DEVICE WAKE notification for device [%s]\n",
494 device->pnp.bus_id));
498 case ACPI_NOTIFY_EJECT_REQUEST:
499 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Received EJECT REQUEST notification for device [%s]\n",
500 device->pnp.bus_id));
504 case ACPI_NOTIFY_DEVICE_CHECK_LIGHT:
505 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Received DEVICE CHECK LIGHT notification for device [%s]\n",
506 device->pnp.bus_id));
507 /* TBD: Exactly what does 'light' mean? */
510 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
511 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Received FREQUENCY MISMATCH notification for device [%s]\n",
512 device->pnp.bus_id));
516 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
517 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Received BUS MODE MISMATCH notification for device [%s]\n",
518 device->pnp.bus_id));
522 case ACPI_NOTIFY_POWER_FAULT:
523 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Received POWER FAULT notification for device [%s]\n",
524 device->pnp.bus_id));
529 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Received unknown/unsupported notification [%08x]\n",
537 /* --------------------------------------------------------------------------
538 Initialization/Cleanup
539 -------------------------------------------------------------------------- */
542 acpi_bus_init_irq (void)
544 acpi_status status = AE_OK;
545 union acpi_object arg = {ACPI_TYPE_INTEGER};
546 struct acpi_object_list arg_list = {1, &arg};
547 char *message = NULL;
549 ACPI_FUNCTION_TRACE("acpi_bus_init_irq");
552 * Let the system know what interrupt model we are using by
553 * evaluating the \_PIC object, if exists.
556 switch (acpi_irq_model) {
557 case ACPI_IRQ_MODEL_PIC:
560 case ACPI_IRQ_MODEL_IOAPIC:
563 case ACPI_IRQ_MODEL_IOSAPIC:
567 printk(KERN_WARNING PREFIX "Unknown interrupt routing model\n");
568 return_VALUE(-ENODEV);
571 printk(KERN_INFO PREFIX "Using %s for interrupt routing\n", message);
573 arg.integer.value = acpi_irq_model;
575 status = acpi_evaluate_object(NULL, "\\_PIC", &arg_list, NULL);
576 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
577 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _PIC\n"));
578 return_VALUE(-ENODEV);
589 acpi_status status = AE_OK;
590 struct acpi_buffer buffer = {sizeof(acpi_fadt), &acpi_fadt};
592 ACPI_FUNCTION_TRACE("acpi_bus_init");
594 status = acpi_initialize_subsystem();
595 if (ACPI_FAILURE(status)) {
596 printk(KERN_ERR PREFIX "Unable to initialize the ACPI Interpreter\n");
600 status = acpi_load_tables();
601 if (ACPI_FAILURE(status)) {
602 printk(KERN_ERR PREFIX "Unable to load the System Description Tables\n");
607 * Get a separate copy of the FADT for use by other drivers.
609 status = acpi_get_table(ACPI_TABLE_FADT, 1, &buffer);
610 if (ACPI_FAILURE(status)) {
611 printk(KERN_ERR PREFIX "Unable to get the FADT\n");
617 extern acpi_interrupt_flags acpi_sci_flags;
619 /* compatible (0) means level (3) */
620 if (acpi_sci_flags.trigger == 0)
621 acpi_sci_flags.trigger = 3;
623 /* Set PIC-mode SCI trigger type */
624 acpi_pic_sci_set_trigger(acpi_fadt.sci_int, acpi_sci_flags.trigger);
626 extern int acpi_sci_override_gsi;
628 * now that acpi_fadt is initialized,
629 * update it with result from INT_SRC_OVR parsing
631 acpi_fadt.sci_int = acpi_sci_override_gsi;
635 status = acpi_enable_subsystem(ACPI_FULL_INITIALIZATION);
636 if (ACPI_FAILURE(status)) {
637 printk(KERN_ERR PREFIX "Unable to start the ACPI Interpreter\n");
641 #ifdef CONFIG_ACPI_EC
643 * ACPI 2.0 requires the EC driver to be loaded and work before
644 * the EC device is found in the namespace (i.e. before acpi_initialize_objects()
647 * This is accomplished by looking for the ECDT table, and getting
648 * the EC parameters out of that.
650 status = acpi_ec_ecdt_probe();
651 /* Ignore result. Not having an ECDT is not fatal. */
654 status = acpi_initialize_objects(ACPI_FULL_INITIALIZATION);
655 if (ACPI_FAILURE(status)) {
656 printk(KERN_ERR PREFIX "Unable to initialize ACPI objects\n");
660 printk(KERN_INFO PREFIX "Interpreter enabled\n");
663 * Get the system interrupt model and evaluate \_PIC.
665 result = acpi_bus_init_irq();
670 * Register the for all standard device notifications.
672 status = acpi_install_notify_handler(ACPI_ROOT_OBJECT, ACPI_SYSTEM_NOTIFY, &acpi_bus_notify, NULL);
673 if (ACPI_FAILURE(status)) {
674 printk(KERN_ERR PREFIX "Unable to register for device notifications\n");
679 * Create the top ACPI proc directory
681 acpi_root_dir = proc_mkdir(ACPI_BUS_FILE_ROOT, NULL);
685 /* Mimic structured exception handling */
689 return_VALUE(-ENODEV);
692 decl_subsys(acpi,NULL,NULL);
694 static int __init acpi_init (void)
698 ACPI_FUNCTION_TRACE("acpi_init");
700 printk(KERN_INFO PREFIX "Subsystem revision %08x\n",
703 /* Initial core debug level excludes drivers, so include them now */
704 acpi_set_debug(ACPI_DEBUG_LOW);
707 printk(KERN_INFO PREFIX "Interpreter disabled.\n");
711 firmware_register(&acpi_subsys);
713 result = acpi_bus_init();
720 printk(KERN_INFO PREFIX "APM is already active, exiting\n");
728 return_VALUE(result);
731 subsys_initcall(acpi_init);