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/list.h>
 
  29 #include <linux/sched.h>
 
  31 #include <linux/pm_legacy.h>
 
  32 #include <linux/device.h>
 
  33 #include <linux/proc_fs.h>
 
  35 #include <asm/mpspec.h>
 
  37 #include <acpi/acpi_bus.h>
 
  38 #include <acpi/acpi_drivers.h>
 
  40 #define _COMPONENT              ACPI_BUS_COMPONENT
 
  41 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 EXPORT_SYMBOL(acpi_fadt);
 
  49 struct acpi_device *acpi_root;
 
  50 struct proc_dir_entry *acpi_root_dir;
 
  51 EXPORT_SYMBOL(acpi_root_dir);
 
  53 #define STRUCT_TO_INT(s)        (*((int*)&s))
 
  55 /* --------------------------------------------------------------------------
 
  57    -------------------------------------------------------------------------- */
 
  59 int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
 
  61         acpi_status status = AE_OK;
 
  63         ACPI_FUNCTION_TRACE("acpi_bus_get_device");
 
  66                 return_VALUE(-EINVAL);
 
  68         /* TBD: Support fixed-feature devices */
 
  70         status = acpi_get_data(handle, acpi_bus_data_handler, (void **)device);
 
  71         if (ACPI_FAILURE(status) || !*device) {
 
  72                 ACPI_DEBUG_PRINT((ACPI_DB_WARN, "No context for object [%p]\n",
 
  74                 return_VALUE(-ENODEV);
 
  80 EXPORT_SYMBOL(acpi_bus_get_device);
 
  82 int acpi_bus_get_status(struct acpi_device *device)
 
  84         acpi_status status = AE_OK;
 
  85         unsigned long sta = 0;
 
  87         ACPI_FUNCTION_TRACE("acpi_bus_get_status");
 
  90                 return_VALUE(-EINVAL);
 
  93          * Evaluate _STA if present.
 
  95         if (device->flags.dynamic_status) {
 
  97                     acpi_evaluate_integer(device->handle, "_STA", NULL, &sta);
 
  98                 if (ACPI_FAILURE(status))
 
  99                         return_VALUE(-ENODEV);
 
 100                 STRUCT_TO_INT(device->status) = (int)sta;
 
 104          * Otherwise we assume the status of our parent (unless we don't
 
 105          * have one, in which case status is implied).
 
 107         else if (device->parent)
 
 108                 device->status = device->parent->status;
 
 110                 STRUCT_TO_INT(device->status) = 0x0F;
 
 112         if (device->status.functional && !device->status.present) {
 
 113                 printk(KERN_WARNING PREFIX "Device [%s] status [%08x]: "
 
 114                        "functional but not present; setting present\n",
 
 115                        device->pnp.bus_id, (u32) STRUCT_TO_INT(device->status));
 
 116                 device->status.present = 1;
 
 119         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] status [%08x]\n",
 
 121                           (u32) STRUCT_TO_INT(device->status)));
 
 126 EXPORT_SYMBOL(acpi_bus_get_status);
 
 128 /* --------------------------------------------------------------------------
 
 130    -------------------------------------------------------------------------- */
 
 132 int acpi_bus_get_power(acpi_handle handle, int *state)
 
 135         acpi_status status = 0;
 
 136         struct acpi_device *device = NULL;
 
 137         unsigned long psc = 0;
 
 139         ACPI_FUNCTION_TRACE("acpi_bus_get_power");
 
 141         result = acpi_bus_get_device(handle, &device);
 
 143                 return_VALUE(result);
 
 145         *state = ACPI_STATE_UNKNOWN;
 
 147         if (!device->flags.power_manageable) {
 
 148                 /* TBD: Non-recursive algorithm for walking up hierarchy */
 
 150                         *state = device->parent->power.state;
 
 152                         *state = ACPI_STATE_D0;
 
 155                  * Get the device's power state either directly (via _PSC) or 
 
 156                  * indirectly (via power resources).
 
 158                 if (device->power.flags.explicit_get) {
 
 159                         status = acpi_evaluate_integer(device->handle, "_PSC",
 
 161                         if (ACPI_FAILURE(status))
 
 162                                 return_VALUE(-ENODEV);
 
 163                         device->power.state = (int)psc;
 
 164                 } else if (device->power.flags.power_resources) {
 
 165                         result = acpi_power_get_inferred_state(device);
 
 167                                 return_VALUE(result);
 
 170                 *state = device->power.state;
 
 173         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] power state is D%d\n",
 
 174                           device->pnp.bus_id, device->power.state));
 
 179 EXPORT_SYMBOL(acpi_bus_get_power);
 
 181 int acpi_bus_set_power(acpi_handle handle, int state)
 
 184         acpi_status status = AE_OK;
 
 185         struct acpi_device *device = NULL;
 
 186         char object_name[5] = { '_', 'P', 'S', '0' + state, '\0' };
 
 188         ACPI_FUNCTION_TRACE("acpi_bus_set_power");
 
 190         result = acpi_bus_get_device(handle, &device);
 
 192                 return_VALUE(result);
 
 194         if ((state < ACPI_STATE_D0) || (state > ACPI_STATE_D3))
 
 195                 return_VALUE(-EINVAL);
 
 197         /* Make sure this is a valid target state */
 
 199         if (!device->flags.power_manageable) {
 
 200                 ACPI_DEBUG_PRINT((ACPI_DB_WARN,
 
 201                                   "Device is not power manageable\n"));
 
 202                 return_VALUE(-ENODEV);
 
 205          * Get device's current power state if it's unknown
 
 206          * This means device power state isn't initialized or previous setting failed
 
 208         if (device->power.state == ACPI_STATE_UNKNOWN)
 
 209                 acpi_bus_get_power(device->handle, &device->power.state);
 
 210         if (state == device->power.state) {
 
 211                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device is already at D%d\n",
 
 215         if (!device->power.states[state].flags.valid) {
 
 216                 ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Device does not support D%d\n",
 
 218                 return_VALUE(-ENODEV);
 
 220         if (device->parent && (state < device->parent->power.state)) {
 
 221                 ACPI_DEBUG_PRINT((ACPI_DB_WARN,
 
 222                                   "Cannot set device to a higher-powered state than parent\n"));
 
 223                 return_VALUE(-ENODEV);
 
 229          * On transitions to a high-powered state we first apply power (via
 
 230          * power resources) then evalute _PSx.  Conversly for transitions to
 
 231          * a lower-powered state.
 
 233         if (state < device->power.state) {
 
 234                 if (device->power.flags.power_resources) {
 
 235                         result = acpi_power_transition(device, state);
 
 239                 if (device->power.states[state].flags.explicit_set) {
 
 240                         status = acpi_evaluate_object(device->handle,
 
 241                                                       object_name, NULL, NULL);
 
 242                         if (ACPI_FAILURE(status)) {
 
 248                 if (device->power.states[state].flags.explicit_set) {
 
 249                         status = acpi_evaluate_object(device->handle,
 
 250                                                       object_name, NULL, NULL);
 
 251                         if (ACPI_FAILURE(status)) {
 
 256                 if (device->power.flags.power_resources) {
 
 257                         result = acpi_power_transition(device, state);
 
 265                 ACPI_DEBUG_PRINT((ACPI_DB_WARN,
 
 266                                   "Error transitioning device [%s] to D%d\n",
 
 267                                   device->pnp.bus_id, state));
 
 269                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
 
 270                                   "Device [%s] transitioned to D%d\n",
 
 271                                   device->pnp.bus_id, state));
 
 273         return_VALUE(result);
 
 276 EXPORT_SYMBOL(acpi_bus_set_power);
 
 278 /* --------------------------------------------------------------------------
 
 280    -------------------------------------------------------------------------- */
 
 282 static DEFINE_SPINLOCK(acpi_bus_event_lock);
 
 284 LIST_HEAD(acpi_bus_event_list);
 
 285 DECLARE_WAIT_QUEUE_HEAD(acpi_bus_event_queue);
 
 287 extern int event_is_open;
 
 289 int acpi_bus_generate_event(struct acpi_device *device, u8 type, int data)
 
 291         struct acpi_bus_event *event = NULL;
 
 292         unsigned long flags = 0;
 
 294         ACPI_FUNCTION_TRACE("acpi_bus_generate_event");
 
 297                 return_VALUE(-EINVAL);
 
 299         /* drop event on the floor if no one's listening */
 
 303         event = kmalloc(sizeof(struct acpi_bus_event), GFP_ATOMIC);
 
 305                 return_VALUE(-ENOMEM);
 
 307         strcpy(event->device_class, device->pnp.device_class);
 
 308         strcpy(event->bus_id, device->pnp.bus_id);
 
 312         spin_lock_irqsave(&acpi_bus_event_lock, flags);
 
 313         list_add_tail(&event->node, &acpi_bus_event_list);
 
 314         spin_unlock_irqrestore(&acpi_bus_event_lock, flags);
 
 316         wake_up_interruptible(&acpi_bus_event_queue);
 
 321 EXPORT_SYMBOL(acpi_bus_generate_event);
 
 323 int acpi_bus_receive_event(struct acpi_bus_event *event)
 
 325         unsigned long flags = 0;
 
 326         struct acpi_bus_event *entry = NULL;
 
 328         DECLARE_WAITQUEUE(wait, current);
 
 330         ACPI_FUNCTION_TRACE("acpi_bus_receive_event");
 
 333                 return_VALUE(-EINVAL);
 
 335         if (list_empty(&acpi_bus_event_list)) {
 
 337                 set_current_state(TASK_INTERRUPTIBLE);
 
 338                 add_wait_queue(&acpi_bus_event_queue, &wait);
 
 340                 if (list_empty(&acpi_bus_event_list))
 
 343                 remove_wait_queue(&acpi_bus_event_queue, &wait);
 
 344                 set_current_state(TASK_RUNNING);
 
 346                 if (signal_pending(current))
 
 347                         return_VALUE(-ERESTARTSYS);
 
 350         spin_lock_irqsave(&acpi_bus_event_lock, flags);
 
 352             list_entry(acpi_bus_event_list.next, struct acpi_bus_event, node);
 
 354                 list_del(&entry->node);
 
 355         spin_unlock_irqrestore(&acpi_bus_event_lock, flags);
 
 358                 return_VALUE(-ENODEV);
 
 360         memcpy(event, entry, sizeof(struct acpi_bus_event));
 
 367 EXPORT_SYMBOL(acpi_bus_receive_event);
 
 369 /* --------------------------------------------------------------------------
 
 370                              Notification Handling
 
 371    -------------------------------------------------------------------------- */
 
 374 acpi_bus_check_device(struct acpi_device *device, int *status_changed)
 
 376         acpi_status status = 0;
 
 377         struct acpi_device_status old_status;
 
 379         ACPI_FUNCTION_TRACE("acpi_bus_check_device");
 
 382                 return_VALUE(-EINVAL);
 
 387         old_status = device->status;
 
 390          * Make sure this device's parent is present before we go about
 
 391          * messing with the device.
 
 393         if (device->parent && !device->parent->status.present) {
 
 394                 device->status = device->parent->status;
 
 395                 if (STRUCT_TO_INT(old_status) != STRUCT_TO_INT(device->status)) {
 
 402         status = acpi_bus_get_status(device);
 
 403         if (ACPI_FAILURE(status))
 
 404                 return_VALUE(-ENODEV);
 
 406         if (STRUCT_TO_INT(old_status) == STRUCT_TO_INT(device->status))
 
 413          * Device Insertion/Removal
 
 415         if ((device->status.present) && !(old_status.present)) {
 
 416                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device insertion detected\n"));
 
 417                 /* TBD: Handle device insertion */
 
 418         } else if (!(device->status.present) && (old_status.present)) {
 
 419                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device removal detected\n"));
 
 420                 /* TBD: Handle device removal */
 
 426 static int acpi_bus_check_scope(struct acpi_device *device)
 
 429         int status_changed = 0;
 
 431         ACPI_FUNCTION_TRACE("acpi_bus_check_scope");
 
 434                 return_VALUE(-EINVAL);
 
 437         result = acpi_bus_check_device(device, &status_changed);
 
 439                 return_VALUE(result);
 
 445          * TBD: Enumerate child devices within this device's scope and
 
 446          *       run acpi_bus_check_device()'s on them.
 
 455  * Callback for all 'system-level' device notifications (values 0x00-0x7F).
 
 457 static void acpi_bus_notify(acpi_handle handle, u32 type, void *data)
 
 460         struct acpi_device *device = NULL;
 
 462         ACPI_FUNCTION_TRACE("acpi_bus_notify");
 
 464         if (acpi_bus_get_device(handle, &device))
 
 469         case ACPI_NOTIFY_BUS_CHECK:
 
 470                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
 
 471                                   "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).
 
 480         case ACPI_NOTIFY_DEVICE_CHECK:
 
 481                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
 
 482                                   "Received DEVICE CHECK notification for device [%s]\n",
 
 483                                   device->pnp.bus_id));
 
 484                 result = acpi_bus_check_device(device, NULL);
 
 486                  * TBD: We'll need to outsource certain events to non-ACPI
 
 487                  *      drivers via the device manager (device.c).
 
 491         case ACPI_NOTIFY_DEVICE_WAKE:
 
 492                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
 
 493                                   "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,
 
 500                                   "Received EJECT REQUEST notification for device [%s]\n",
 
 501                                   device->pnp.bus_id));
 
 505         case ACPI_NOTIFY_DEVICE_CHECK_LIGHT:
 
 506                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
 
 507                                   "Received DEVICE CHECK LIGHT notification for device [%s]\n",
 
 508                                   device->pnp.bus_id));
 
 509                 /* TBD: Exactly what does 'light' mean? */
 
 512         case ACPI_NOTIFY_FREQUENCY_MISMATCH:
 
 513                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
 
 514                                   "Received FREQUENCY MISMATCH notification for device [%s]\n",
 
 515                                   device->pnp.bus_id));
 
 519         case ACPI_NOTIFY_BUS_MODE_MISMATCH:
 
 520                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
 
 521                                   "Received BUS MODE MISMATCH notification for device [%s]\n",
 
 522                                   device->pnp.bus_id));
 
 526         case ACPI_NOTIFY_POWER_FAULT:
 
 527                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
 
 528                                   "Received POWER FAULT notification for device [%s]\n",
 
 529                                   device->pnp.bus_id));
 
 534                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
 
 535                                   "Received unknown/unsupported notification [%08x]\n",
 
 543 /* --------------------------------------------------------------------------
 
 544                              Initialization/Cleanup
 
 545    -------------------------------------------------------------------------- */
 
 547 static int __init acpi_bus_init_irq(void)
 
 549         acpi_status status = AE_OK;
 
 550         union acpi_object arg = { ACPI_TYPE_INTEGER };
 
 551         struct acpi_object_list arg_list = { 1, &arg };
 
 552         char *message = NULL;
 
 554         ACPI_FUNCTION_TRACE("acpi_bus_init_irq");
 
 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:
 
 565         case ACPI_IRQ_MODEL_IOAPIC:
 
 568         case ACPI_IRQ_MODEL_IOSAPIC:
 
 572                 printk(KERN_WARNING PREFIX "Unknown interrupt routing model\n");
 
 573                 return_VALUE(-ENODEV);
 
 576         printk(KERN_INFO PREFIX "Using %s for interrupt routing\n", message);
 
 578         arg.integer.value = acpi_irq_model;
 
 580         status = acpi_evaluate_object(NULL, "\\_PIC", &arg_list, NULL);
 
 581         if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
 
 582                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _PIC\n"));
 
 583                 return_VALUE(-ENODEV);
 
 589 void __init acpi_early_init(void)
 
 591         acpi_status status = AE_OK;
 
 592         struct acpi_buffer buffer = { sizeof(acpi_fadt), &acpi_fadt };
 
 594         ACPI_FUNCTION_TRACE("acpi_early_init");
 
 599         /* enable workarounds, unless strict ACPI spec. compliance */
 
 601                 acpi_gbl_enable_interpreter_slack = TRUE;
 
 603         status = acpi_initialize_subsystem();
 
 604         if (ACPI_FAILURE(status)) {
 
 605                 printk(KERN_ERR PREFIX
 
 606                        "Unable to initialize the ACPI Interpreter\n");
 
 610         status = acpi_load_tables();
 
 611         if (ACPI_FAILURE(status)) {
 
 612                 printk(KERN_ERR PREFIX
 
 613                        "Unable to load the System Description Tables\n");
 
 618          * Get a separate copy of the FADT for use by other drivers.
 
 620         status = acpi_get_table(ACPI_TABLE_FADT, 1, &buffer);
 
 621         if (ACPI_FAILURE(status)) {
 
 622                 printk(KERN_ERR PREFIX "Unable to get the FADT\n");
 
 627                 extern acpi_interrupt_flags acpi_sci_flags;
 
 629                 /* compatible (0) means level (3) */
 
 630                 if (acpi_sci_flags.trigger == 0)
 
 631                         acpi_sci_flags.trigger = 3;
 
 633                 /* Set PIC-mode SCI trigger type */
 
 634                 acpi_pic_sci_set_trigger(acpi_fadt.sci_int,
 
 635                                          acpi_sci_flags.trigger);
 
 637                 extern int acpi_sci_override_gsi;
 
 639                  * now that acpi_fadt is initialized,
 
 640                  * update it with result from INT_SRC_OVR parsing
 
 642                 acpi_fadt.sci_int = acpi_sci_override_gsi;
 
 647             acpi_enable_subsystem(~
 
 648                                   (ACPI_NO_HARDWARE_INIT |
 
 649                                    ACPI_NO_ACPI_ENABLE));
 
 650         if (ACPI_FAILURE(status)) {
 
 651                 printk(KERN_ERR PREFIX "Unable to enable ACPI\n");
 
 662 static int __init acpi_bus_init(void)
 
 665         acpi_status status = AE_OK;
 
 666         extern acpi_status acpi_os_initialize1(void);
 
 668         ACPI_FUNCTION_TRACE("acpi_bus_init");
 
 670         status = acpi_os_initialize1();
 
 673             acpi_enable_subsystem(ACPI_NO_HARDWARE_INIT | ACPI_NO_ACPI_ENABLE);
 
 674         if (ACPI_FAILURE(status)) {
 
 675                 printk(KERN_ERR PREFIX
 
 676                        "Unable to start the ACPI Interpreter\n");
 
 680         if (ACPI_FAILURE(status)) {
 
 681                 printk(KERN_ERR PREFIX
 
 682                        "Unable to initialize ACPI OS objects\n");
 
 685 #ifdef CONFIG_ACPI_EC
 
 687          * ACPI 2.0 requires the EC driver to be loaded and work before
 
 688          * the EC device is found in the namespace (i.e. before acpi_initialize_objects()
 
 691          * This is accomplished by looking for the ECDT table, and getting 
 
 692          * the EC parameters out of that.
 
 694         status = acpi_ec_ecdt_probe();
 
 695         /* Ignore result. Not having an ECDT is not fatal. */
 
 698         status = acpi_initialize_objects(ACPI_FULL_INITIALIZATION);
 
 699         if (ACPI_FAILURE(status)) {
 
 700                 printk(KERN_ERR PREFIX "Unable to initialize ACPI objects\n");
 
 704         printk(KERN_INFO PREFIX "Interpreter enabled\n");
 
 707          * Get the system interrupt model and evaluate \_PIC.
 
 709         result = acpi_bus_init_irq();
 
 714          * Register the for all standard device notifications.
 
 717             acpi_install_notify_handler(ACPI_ROOT_OBJECT, ACPI_SYSTEM_NOTIFY,
 
 718                                         &acpi_bus_notify, NULL);
 
 719         if (ACPI_FAILURE(status)) {
 
 720                 printk(KERN_ERR PREFIX
 
 721                        "Unable to register for device notifications\n");
 
 726          * Create the top ACPI proc directory
 
 728         acpi_root_dir = proc_mkdir(ACPI_BUS_FILE_ROOT, NULL);
 
 732         /* Mimic structured exception handling */
 
 735         return_VALUE(-ENODEV);
 
 738 decl_subsys(acpi, NULL, NULL);
 
 740 static int __init acpi_init(void)
 
 744         ACPI_FUNCTION_TRACE("acpi_init");
 
 746         printk(KERN_INFO PREFIX "Subsystem revision %08x\n", ACPI_CA_VERSION);
 
 749                 printk(KERN_INFO PREFIX "Interpreter disabled.\n");
 
 750                 return_VALUE(-ENODEV);
 
 753         firmware_register(&acpi_subsys);
 
 755         result = acpi_bus_init();
 
 758 #ifdef CONFIG_PM_LEGACY
 
 762                         printk(KERN_INFO PREFIX
 
 763                                "APM is already active, exiting\n");
 
 771         return_VALUE(result);
 
 774 subsys_initcall(acpi_init);