r-o-bind-mounts-elevate-write-count-for-some-ioctls-vs-forbid-user-to-change-file...
[linux-2.6/linux-trees-mm.git] / drivers / acpi / bus.c
blob36f11d0fe84877407040a65e73d838910e79a4c2
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 <linux/pci.h>
39 #include <acpi/acpi_bus.h>
40 #include <acpi/acpi_drivers.h>
42 #define _COMPONENT ACPI_BUS_COMPONENT
43 ACPI_MODULE_NAME("bus");
44 #ifdef CONFIG_X86
45 extern void __init acpi_pic_sci_set_trigger(unsigned int irq, u16 trigger);
46 #endif
48 struct acpi_device *acpi_root;
49 struct proc_dir_entry *acpi_root_dir;
50 EXPORT_SYMBOL(acpi_root_dir);
52 #define STRUCT_TO_INT(s) (*((int*)&s))
54 /* --------------------------------------------------------------------------
55 Device Management
56 -------------------------------------------------------------------------- */
58 int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
60 acpi_status status = AE_OK;
63 if (!device)
64 return -EINVAL;
66 /* TBD: Support fixed-feature devices */
68 status = acpi_get_data(handle, acpi_bus_data_handler, (void **)device);
69 if (ACPI_FAILURE(status) || !*device) {
70 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
71 handle));
72 return -ENODEV;
75 return 0;
78 EXPORT_SYMBOL(acpi_bus_get_device);
80 int acpi_bus_get_status(struct acpi_device *device)
82 acpi_status status = AE_OK;
83 unsigned long sta = 0;
86 if (!device)
87 return -EINVAL;
90 * Evaluate _STA if present.
92 if (device->flags.dynamic_status) {
93 status =
94 acpi_evaluate_integer(device->handle, "_STA", NULL, &sta);
95 if (ACPI_FAILURE(status))
96 return -ENODEV;
97 STRUCT_TO_INT(device->status) = (int)sta;
101 * Otherwise we assume the status of our parent (unless we don't
102 * have one, in which case status is implied).
104 else if (device->parent)
105 device->status = device->parent->status;
106 else
107 STRUCT_TO_INT(device->status) =
108 ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED |
109 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING;
111 if (device->status.functional && !device->status.present) {
112 printk(KERN_WARNING PREFIX "Device [%s] status [%08x]: "
113 "functional but not present; setting present\n",
114 device->pnp.bus_id, (u32) STRUCT_TO_INT(device->status));
115 device->status.present = 1;
118 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] status [%08x]\n",
119 device->pnp.bus_id,
120 (u32) STRUCT_TO_INT(device->status)));
122 return 0;
125 EXPORT_SYMBOL(acpi_bus_get_status);
127 /* --------------------------------------------------------------------------
128 Power Management
129 -------------------------------------------------------------------------- */
131 int acpi_bus_get_power(acpi_handle handle, int *state)
133 int result = 0;
134 acpi_status status = 0;
135 struct acpi_device *device = NULL;
136 unsigned long psc = 0;
139 result = acpi_bus_get_device(handle, &device);
140 if (result)
141 return result;
143 *state = ACPI_STATE_UNKNOWN;
145 if (!device->flags.power_manageable) {
146 /* TBD: Non-recursive algorithm for walking up hierarchy */
147 if (device->parent)
148 *state = device->parent->power.state;
149 else
150 *state = ACPI_STATE_D0;
151 } else {
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",
158 NULL, &psc);
159 if (ACPI_FAILURE(status))
160 return -ENODEV;
161 device->power.state = (int)psc;
162 } else if (device->power.flags.power_resources) {
163 result = acpi_power_get_inferred_state(device);
164 if (result)
165 return result;
168 *state = device->power.state;
171 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] power state is D%d\n",
172 device->pnp.bus_id, device->power.state));
174 return 0;
177 EXPORT_SYMBOL(acpi_bus_get_power);
179 int acpi_bus_set_power(acpi_handle handle, int state)
181 int result = 0;
182 acpi_status status = AE_OK;
183 struct acpi_device *device = NULL;
184 char object_name[5] = { '_', 'P', 'S', '0' + state, '\0' };
187 result = acpi_bus_get_device(handle, &device);
188 if (result)
189 return result;
191 if ((state < ACPI_STATE_D0) || (state > ACPI_STATE_D3))
192 return -EINVAL;
194 /* Make sure this is a valid target state */
196 if (!device->flags.power_manageable) {
197 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device `[%s]' is not power manageable\n",
198 kobject_name(&device->dev.kobj)));
199 return -ENODEV;
202 * Get device's current power state
204 acpi_bus_get_power(device->handle, &device->power.state);
205 if (state == device->power.state) {
206 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device is already at D%d\n",
207 state));
208 return 0;
211 if (!device->power.states[state].flags.valid) {
212 printk(KERN_WARNING PREFIX "Device does not support D%d\n", state);
213 return -ENODEV;
215 if (device->parent && (state < device->parent->power.state)) {
216 printk(KERN_WARNING PREFIX
217 "Cannot set device to a higher-powered"
218 " state than parent\n");
219 return -ENODEV;
223 * Transition Power
224 * ----------------
225 * On transitions to a high-powered state we first apply power (via
226 * power resources) then evalute _PSx. Conversly for transitions to
227 * a lower-powered state.
229 if (state < device->power.state) {
230 if (device->power.flags.power_resources) {
231 result = acpi_power_transition(device, state);
232 if (result)
233 goto end;
235 if (device->power.states[state].flags.explicit_set) {
236 status = acpi_evaluate_object(device->handle,
237 object_name, NULL, NULL);
238 if (ACPI_FAILURE(status)) {
239 result = -ENODEV;
240 goto end;
243 } else {
244 if (device->power.states[state].flags.explicit_set) {
245 status = acpi_evaluate_object(device->handle,
246 object_name, NULL, NULL);
247 if (ACPI_FAILURE(status)) {
248 result = -ENODEV;
249 goto end;
252 if (device->power.flags.power_resources) {
253 result = acpi_power_transition(device, state);
254 if (result)
255 goto end;
259 end:
260 if (result)
261 printk(KERN_WARNING PREFIX
262 "Transitioning device [%s] to D%d\n",
263 device->pnp.bus_id, state);
264 else {
265 device->power.state = state;
266 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
267 "Device [%s] transitioned to D%d\n",
268 device->pnp.bus_id, state));
271 return result;
274 EXPORT_SYMBOL(acpi_bus_set_power);
276 /* --------------------------------------------------------------------------
277 Event Management
278 -------------------------------------------------------------------------- */
280 #ifdef CONFIG_ACPI_PROC_EVENT
281 static DEFINE_SPINLOCK(acpi_bus_event_lock);
283 LIST_HEAD(acpi_bus_event_list);
284 DECLARE_WAIT_QUEUE_HEAD(acpi_bus_event_queue);
286 extern int event_is_open;
288 int acpi_bus_generate_proc_event4(const char *device_class, const char *bus_id, u8 type, int data)
290 struct acpi_bus_event *event;
291 unsigned long flags = 0;
293 /* drop event on the floor if no one's listening */
294 if (!event_is_open)
295 return 0;
297 event = kmalloc(sizeof(struct acpi_bus_event), GFP_ATOMIC);
298 if (!event)
299 return -ENOMEM;
301 strcpy(event->device_class, device_class);
302 strcpy(event->bus_id, bus_id);
303 event->type = type;
304 event->data = data;
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);
312 return 0;
316 EXPORT_SYMBOL_GPL(acpi_bus_generate_proc_event4);
318 int acpi_bus_generate_proc_event(struct acpi_device *device, u8 type, int data)
320 if (!device)
321 return -EINVAL;
322 return acpi_bus_generate_proc_event4(device->pnp.device_class,
323 device->pnp.bus_id, type, data);
326 EXPORT_SYMBOL(acpi_bus_generate_proc_event);
328 int acpi_bus_receive_event(struct acpi_bus_event *event)
330 unsigned long flags = 0;
331 struct acpi_bus_event *entry = NULL;
333 DECLARE_WAITQUEUE(wait, current);
336 if (!event)
337 return -EINVAL;
339 if (list_empty(&acpi_bus_event_list)) {
341 set_current_state(TASK_INTERRUPTIBLE);
342 add_wait_queue(&acpi_bus_event_queue, &wait);
344 if (list_empty(&acpi_bus_event_list))
345 schedule();
347 remove_wait_queue(&acpi_bus_event_queue, &wait);
348 set_current_state(TASK_RUNNING);
350 if (signal_pending(current))
351 return -ERESTARTSYS;
354 spin_lock_irqsave(&acpi_bus_event_lock, flags);
355 entry =
356 list_entry(acpi_bus_event_list.next, struct acpi_bus_event, node);
357 if (entry)
358 list_del(&entry->node);
359 spin_unlock_irqrestore(&acpi_bus_event_lock, flags);
361 if (!entry)
362 return -ENODEV;
364 memcpy(event, entry, sizeof(struct acpi_bus_event));
366 kfree(entry);
368 return 0;
371 EXPORT_SYMBOL(acpi_bus_receive_event);
372 #endif /* CONFIG_ACPI_PROC_EVENT */
374 /* --------------------------------------------------------------------------
375 Notification Handling
376 -------------------------------------------------------------------------- */
378 static int
379 acpi_bus_check_device(struct acpi_device *device, int *status_changed)
381 acpi_status status = 0;
382 struct acpi_device_status old_status;
385 if (!device)
386 return -EINVAL;
388 if (status_changed)
389 *status_changed = 0;
391 old_status = device->status;
394 * Make sure this device's parent is present before we go about
395 * messing with the device.
397 if (device->parent && !device->parent->status.present) {
398 device->status = device->parent->status;
399 if (STRUCT_TO_INT(old_status) != STRUCT_TO_INT(device->status)) {
400 if (status_changed)
401 *status_changed = 1;
403 return 0;
406 status = acpi_bus_get_status(device);
407 if (ACPI_FAILURE(status))
408 return -ENODEV;
410 if (STRUCT_TO_INT(old_status) == STRUCT_TO_INT(device->status))
411 return 0;
413 if (status_changed)
414 *status_changed = 1;
417 * Device Insertion/Removal
419 if ((device->status.present) && !(old_status.present)) {
420 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device insertion detected\n"));
421 /* TBD: Handle device insertion */
422 } else if (!(device->status.present) && (old_status.present)) {
423 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device removal detected\n"));
424 /* TBD: Handle device removal */
427 return 0;
430 static int acpi_bus_check_scope(struct acpi_device *device)
432 int result = 0;
433 int status_changed = 0;
436 if (!device)
437 return -EINVAL;
439 /* Status Change? */
440 result = acpi_bus_check_device(device, &status_changed);
441 if (result)
442 return result;
444 if (!status_changed)
445 return 0;
448 * TBD: Enumerate child devices within this device's scope and
449 * run acpi_bus_check_device()'s on them.
452 return 0;
456 * acpi_bus_notify
457 * ---------------
458 * Callback for all 'system-level' device notifications (values 0x00-0x7F).
460 static void acpi_bus_notify(acpi_handle handle, u32 type, void *data)
462 int result = 0;
463 struct acpi_device *device = NULL;
466 if (acpi_bus_get_device(handle, &device))
467 return;
469 switch (type) {
471 case ACPI_NOTIFY_BUS_CHECK:
472 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
473 "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).
480 break;
482 case ACPI_NOTIFY_DEVICE_CHECK:
483 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
484 "Received DEVICE CHECK notification for device [%s]\n",
485 device->pnp.bus_id));
486 result = acpi_bus_check_device(device, NULL);
488 * TBD: We'll need to outsource certain events to non-ACPI
489 * drivers via the device manager (device.c).
491 break;
493 case ACPI_NOTIFY_DEVICE_WAKE:
494 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
495 "Received DEVICE WAKE notification for device [%s]\n",
496 device->pnp.bus_id));
497 /* TBD */
498 break;
500 case ACPI_NOTIFY_EJECT_REQUEST:
501 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
502 "Received EJECT REQUEST notification for device [%s]\n",
503 device->pnp.bus_id));
504 /* TBD */
505 break;
507 case ACPI_NOTIFY_DEVICE_CHECK_LIGHT:
508 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
509 "Received DEVICE CHECK LIGHT notification for device [%s]\n",
510 device->pnp.bus_id));
511 /* TBD: Exactly what does 'light' mean? */
512 break;
514 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
515 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
516 "Received FREQUENCY MISMATCH notification for device [%s]\n",
517 device->pnp.bus_id));
518 /* TBD */
519 break;
521 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
522 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
523 "Received BUS MODE MISMATCH notification for device [%s]\n",
524 device->pnp.bus_id));
525 /* TBD */
526 break;
528 case ACPI_NOTIFY_POWER_FAULT:
529 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
530 "Received POWER FAULT notification for device [%s]\n",
531 device->pnp.bus_id));
532 /* TBD */
533 break;
535 default:
536 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
537 "Received unknown/unsupported notification [%08x]\n",
538 type));
539 break;
542 return;
545 /* --------------------------------------------------------------------------
546 Initialization/Cleanup
547 -------------------------------------------------------------------------- */
549 static int __init acpi_bus_init_irq(void)
551 acpi_status status = AE_OK;
552 union acpi_object arg = { ACPI_TYPE_INTEGER };
553 struct acpi_object_list arg_list = { 1, &arg };
554 char *message = NULL;
558 * Let the system know what interrupt model we are using by
559 * evaluating the \_PIC object, if exists.
562 switch (acpi_irq_model) {
563 case ACPI_IRQ_MODEL_PIC:
564 message = "PIC";
565 break;
566 case ACPI_IRQ_MODEL_IOAPIC:
567 message = "IOAPIC";
568 break;
569 case ACPI_IRQ_MODEL_IOSAPIC:
570 message = "IOSAPIC";
571 break;
572 case ACPI_IRQ_MODEL_PLATFORM:
573 message = "platform specific model";
574 break;
575 default:
576 printk(KERN_WARNING PREFIX "Unknown interrupt routing model\n");
577 return -ENODEV;
580 printk(KERN_INFO PREFIX "Using %s for interrupt routing\n", message);
582 arg.integer.value = acpi_irq_model;
584 status = acpi_evaluate_object(NULL, "\\_PIC", &arg_list, NULL);
585 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
586 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PIC"));
587 return -ENODEV;
590 return 0;
593 acpi_native_uint acpi_gbl_permanent_mmap;
596 void __init acpi_early_init(void)
598 acpi_status status = AE_OK;
600 if (acpi_disabled)
601 return;
603 printk(KERN_INFO PREFIX "Core revision %08x\n", ACPI_CA_VERSION);
605 /* enable workarounds, unless strict ACPI spec. compliance */
606 if (!acpi_strict)
607 acpi_gbl_enable_interpreter_slack = TRUE;
609 acpi_gbl_permanent_mmap = 1;
611 status = acpi_reallocate_root_table();
612 if (ACPI_FAILURE(status)) {
613 printk(KERN_ERR PREFIX
614 "Unable to reallocate ACPI tables\n");
615 goto error0;
618 status = acpi_initialize_subsystem();
619 if (ACPI_FAILURE(status)) {
620 printk(KERN_ERR PREFIX
621 "Unable to initialize the ACPI Interpreter\n");
622 goto error0;
625 status = acpi_load_tables();
626 if (ACPI_FAILURE(status)) {
627 printk(KERN_ERR PREFIX
628 "Unable to load the System Description Tables\n");
629 goto error0;
632 #ifdef CONFIG_X86
633 if (!acpi_ioapic) {
634 extern u8 acpi_sci_flags;
636 /* compatible (0) means level (3) */
637 if (!(acpi_sci_flags & ACPI_MADT_TRIGGER_MASK)) {
638 acpi_sci_flags &= ~ACPI_MADT_TRIGGER_MASK;
639 acpi_sci_flags |= ACPI_MADT_TRIGGER_LEVEL;
641 /* Set PIC-mode SCI trigger type */
642 acpi_pic_sci_set_trigger(acpi_gbl_FADT.sci_interrupt,
643 (acpi_sci_flags & ACPI_MADT_TRIGGER_MASK) >> 2);
644 } else {
645 extern int acpi_sci_override_gsi;
647 * now that acpi_gbl_FADT is initialized,
648 * update it with result from INT_SRC_OVR parsing
650 acpi_gbl_FADT.sci_interrupt = acpi_sci_override_gsi;
652 #endif
654 status =
655 acpi_enable_subsystem(~
656 (ACPI_NO_HARDWARE_INIT |
657 ACPI_NO_ACPI_ENABLE));
658 if (ACPI_FAILURE(status)) {
659 printk(KERN_ERR PREFIX "Unable to enable ACPI\n");
660 goto error0;
663 return;
665 error0:
666 disable_acpi();
667 return;
670 static int __init acpi_bus_init(void)
672 int result = 0;
673 acpi_status status = AE_OK;
674 extern acpi_status acpi_os_initialize1(void);
677 status = acpi_os_initialize1();
679 status =
680 acpi_enable_subsystem(ACPI_NO_HARDWARE_INIT | ACPI_NO_ACPI_ENABLE);
681 if (ACPI_FAILURE(status)) {
682 printk(KERN_ERR PREFIX
683 "Unable to start the ACPI Interpreter\n");
684 goto error1;
687 if (ACPI_FAILURE(status)) {
688 printk(KERN_ERR PREFIX
689 "Unable to initialize ACPI OS objects\n");
690 goto error1;
692 #ifdef CONFIG_ACPI_EC
694 * ACPI 2.0 requires the EC driver to be loaded and work before
695 * the EC device is found in the namespace (i.e. before acpi_initialize_objects()
696 * is called).
698 * This is accomplished by looking for the ECDT table, and getting
699 * the EC parameters out of that.
701 status = acpi_ec_ecdt_probe();
702 /* Ignore result. Not having an ECDT is not fatal. */
703 #endif
705 status = acpi_initialize_objects(ACPI_FULL_INITIALIZATION);
706 if (ACPI_FAILURE(status)) {
707 printk(KERN_ERR PREFIX "Unable to initialize ACPI objects\n");
708 goto error1;
711 printk(KERN_INFO PREFIX "Interpreter enabled\n");
713 /* Initialize sleep structures */
714 acpi_sleep_init();
717 * Get the system interrupt model and evaluate \_PIC.
719 result = acpi_bus_init_irq();
720 if (result)
721 goto error1;
724 * Register the for all standard device notifications.
726 status =
727 acpi_install_notify_handler(ACPI_ROOT_OBJECT, ACPI_SYSTEM_NOTIFY,
728 &acpi_bus_notify, NULL);
729 if (ACPI_FAILURE(status)) {
730 printk(KERN_ERR PREFIX
731 "Unable to register for device notifications\n");
732 goto error1;
736 * Create the top ACPI proc directory
738 acpi_root_dir = proc_mkdir(ACPI_BUS_FILE_ROOT, NULL);
740 return 0;
742 /* Mimic structured exception handling */
743 error1:
744 acpi_terminate();
745 return -ENODEV;
748 struct kset *acpi_kset;
750 static int __init acpi_init(void)
752 int result = 0;
755 if (acpi_disabled) {
756 printk(KERN_INFO PREFIX "Interpreter disabled.\n");
757 return -ENODEV;
760 acpi_kset = kset_create_and_register("acpi", NULL, firmware_kobj,
761 NULL);
762 if (!acpi_kset) {
763 printk(KERN_WARNING "%s: kset create error\n", __FUNCTION__);
764 acpi_kset = NULL;
767 result = acpi_bus_init();
769 if (!result) {
770 pci_mmcfg_late_init();
771 #ifdef CONFIG_PM_LEGACY
772 if (!PM_IS_ACTIVE())
773 pm_active = 1;
774 else {
775 printk(KERN_INFO PREFIX
776 "APM is already active, exiting\n");
777 disable_acpi();
778 result = -ENODEV;
780 #endif
781 } else
782 disable_acpi();
784 return result;
787 subsys_initcall(acpi_init);