linux-old/drivers/acpi/bus.c
<<
>>
Prefs
   1/*
   2 *  acpi_bus.c - ACPI Bus Driver ($Revision: 77 $)
   3 *
   4 *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
   5 *
   6 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   7 *
   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.
  12 *
  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.
  17 *
  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.
  21 *
  22 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  23 */
  24
  25#include <linux/kernel.h>
  26#include <linux/module.h>
  27#include <linux/init.h>
  28#include <linux/types.h>
  29#include <linux/version.h>
  30#include <linux/ioport.h>
  31#include <linux/list.h>
  32#include <linux/sched.h>
  33#include <linux/pm.h>
  34#include <linux/proc_fs.h>
  35#ifdef CONFIG_X86
  36#include <asm/mpspec.h>
  37#endif
  38#include <acpi/acpi_bus.h>
  39#include <acpi/acpi_drivers.h>
  40#include <acpi/acinterp.h>      /* for acpi_ex_eisa_id_to_string() */
  41
  42
  43#define _COMPONENT              ACPI_BUS_COMPONENT
  44ACPI_MODULE_NAME                ("acpi_bus")
  45
  46MODULE_AUTHOR("Paul Diefenbaugh");
  47MODULE_DESCRIPTION(ACPI_BUS_DRIVER_NAME);
  48MODULE_LICENSE("GPL");
  49
  50#define PREFIX                  "ACPI: "
  51
  52extern void __init acpi_pic_sci_set_trigger(unsigned int irq, u16 trigger);
  53
  54FADT_DESCRIPTOR                 acpi_fadt;
  55struct acpi_device              *acpi_root;
  56struct proc_dir_entry           *acpi_root_dir;
  57
  58#define STRUCT_TO_INT(s)        (*((int*)&s))
  59
  60
  61/* --------------------------------------------------------------------------
  62                          Linux Driver Model (LDM) Support
  63   -------------------------------------------------------------------------- */
  64
  65#ifdef CONFIG_LDM
  66
  67static int acpi_device_probe(struct device *dev);
  68static int acpi_device_remove(struct device *dev);
  69static int acpi_device_suspend(struct device *dev, u32 state, u32 stage);
  70static int acpi_device_resume(struct device *dev, u32 stage);
  71
  72static struct device_driver acpi_bus_driver = {
  73        .probe = acpi_device_probe,
  74        .remove = acpi_device_remove,   
  75        .suspend = acpi_device_suspend,
  76        .resume = acpi_device_resume,
  77};
  78
  79
  80static int
  81acpi_device_probe (
  82        struct device           *dev)
  83{
  84        ACPI_FUNCTION_TRACE("acpi_device_probe");
  85
  86        if (!dev)
  87                return_VALUE(-EINVAL);
  88
  89        /* TBD */
  90
  91        return_VALUE(0);
  92}
  93
  94
  95static int
  96acpi_device_remove (
  97        struct device           *dev)
  98{
  99        ACPI_FUNCTION_TRACE("acpi_device_remove");
 100
 101        if (!dev)
 102                return_VALUE(-EINVAL);
 103
 104        /* TBD */
 105
 106        return_VALUE(0);
 107}
 108
 109
 110static int
 111acpi_device_suspend (
 112        struct device           *dev,
 113        u32                     state,
 114        u32                     stage)
 115{
 116        ACPI_FUNCTION_TRACE("acpi_device_suspend");
 117
 118        if (!dev)
 119                return_VALUE(-EINVAL);
 120
 121        /* TBD */
 122
 123        return_VALUE(0);
 124}
 125
 126
 127static int
 128acpi_device_resume (
 129        struct device           *dev,
 130        u32                     stage)
 131{
 132        ACPI_FUNCTION_TRACE("acpi_device_resume");
 133
 134        if (!dev)
 135                return_VALUE(-EINVAL);
 136
 137        /* TBD */
 138
 139        return_VALUE(0);
 140}
 141
 142#if 0 /* not used ATM */
 143static int
 144acpi_platform_add (
 145        struct device           *dev)
 146{
 147        ACPI_FUNCTION_TRACE("acpi_platform_add");
 148
 149        if (!dev)
 150                return -EINVAL;
 151
 152        ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device %s (%s) added\n",
 153                dev->name, dev->bus_id));
 154
 155        /* TBD */
 156
 157        return_VALUE(0);
 158}
 159
 160
 161static int
 162acpi_platform_remove (
 163        struct device           *dev)
 164{
 165        ACPI_FUNCTION_TRACE("acpi_platform_add");
 166
 167        if (!dev)
 168                return -EINVAL;
 169
 170        ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device %s (%s) removed\n",
 171                dev->name, dev->bus_id));
 172
 173        /* TBD */
 174
 175        return_VALUE(0);
 176}
 177#endif /* unused */
 178
 179
 180#endif /*CONFIG_LDM*/
 181
 182
 183static int
 184acpi_device_register (
 185        struct acpi_device      *device,
 186        struct acpi_device      *parent)
 187{
 188        int                     result = 0;
 189
 190        ACPI_FUNCTION_TRACE("acpi_device_register");
 191
 192        if (!device)
 193                return_VALUE(-EINVAL);
 194
 195#ifdef CONFIG_LDM
 196        sprintf(device->dev.name, "ACPI device %s:%s", 
 197                device->pnp.hardware_id, device->pnp.unique_id);
 198        strncpy(device->dev.bus_id, device->pnp.bus_id, sizeof(acpi_bus_id));
 199        if (parent)
 200                device->dev.parent = &parent->dev;
 201        device->dev.driver = &acpi_bus_driver;
 202
 203        result = device_register(&device->dev);
 204#endif /*CONFIG_LDM*/
 205
 206        return_VALUE(result);
 207}
 208
 209
 210static int
 211acpi_device_unregister (
 212        struct acpi_device      *device)
 213{
 214        ACPI_FUNCTION_TRACE("acpi_device_unregister");
 215
 216        if (!device)
 217                return_VALUE(-EINVAL);
 218
 219#ifdef CONFIG_LDM
 220        put_device(&device->dev);
 221#endif /*CONFIG_LDM*/
 222
 223        return_VALUE(0);
 224}
 225
 226
 227/* --------------------------------------------------------------------------
 228                                Device Management
 229   -------------------------------------------------------------------------- */
 230
 231static void
 232acpi_bus_data_handler (
 233        acpi_handle             handle,
 234        u32                     function,
 235        void                    *context)
 236{
 237        ACPI_FUNCTION_TRACE("acpi_bus_data_handler");
 238
 239        /* TBD */
 240
 241        return_VOID;
 242}
 243
 244
 245int
 246acpi_bus_get_device (
 247        acpi_handle             handle,
 248        struct acpi_device      **device)
 249{
 250        acpi_status             status = AE_OK;
 251
 252        ACPI_FUNCTION_TRACE("acpi_bus_get_device");
 253
 254        if (!device)
 255                return_VALUE(-EINVAL);
 256
 257        /* TBD: Support fixed-feature devices */
 258
 259        status = acpi_get_data(handle, acpi_bus_data_handler, (void**) device);
 260        if (ACPI_FAILURE(status) || !*device) {
 261                ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Error getting context for object [%p]\n",
 262                        handle));
 263                return_VALUE(-ENODEV);
 264        }
 265
 266        return_VALUE(0);
 267}
 268
 269int
 270acpi_bus_get_status (
 271        struct acpi_device      *device)
 272{
 273        acpi_status             status = AE_OK;
 274        unsigned long           sta = 0;
 275        
 276        ACPI_FUNCTION_TRACE("acpi_bus_get_status");
 277
 278        if (!device)
 279                return_VALUE(-EINVAL);
 280
 281        /*
 282         * Evaluate _STA if present.
 283         */
 284        if (device->flags.dynamic_status) {
 285                status = acpi_evaluate_integer(device->handle, "_STA", NULL, &sta);
 286                if (ACPI_FAILURE(status))
 287                        return_VALUE(-ENODEV);
 288                STRUCT_TO_INT(device->status) = (int) sta;
 289        }
 290
 291        /*
 292         * Otherwise we assume the status of our parent (unless we don't
 293         * have one, in which case status is implied).
 294         */
 295        else if (device->parent)
 296                device->status = device->parent->status;
 297        else
 298                STRUCT_TO_INT(device->status) = 0x0F;
 299
 300        ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] status [%08x]\n", 
 301                device->pnp.bus_id, (u32) STRUCT_TO_INT(device->status)));
 302
 303        return_VALUE(0);
 304}
 305
 306
 307/*
 308static int
 309acpi_bus_create_device_fs (struct device *device)
 310{
 311        ACPI_FUNCTION_TRACE("acpi_bus_create_device_fs");
 312
 313        if (!device)
 314                return_VALUE(-EINVAL);
 315
 316        if (device->dir.entry)
 317                return_VALUE(-EEXIST);
 318
 319        if (!device->parent)
 320                device->dir.entry = proc_mkdir(device->pnp.bus_id, NULL);
 321        else
 322                device->dir.entry = proc_mkdir(device->pnp.bus_id,
 323                        device->parent->fs.entry);
 324
 325        if (!device->dir.entry) {
 326                printk(KERN_ERR PREFIX "Unable to create fs entry '%s'\n",
 327                        device->pnp.bus_id);
 328                return_VALUE(-ENODEV);
 329        }
 330
 331        return_VALUE(0);
 332}
 333
 334
 335static int
 336acpi_bus_remove_device_fs (struct device *device)
 337{
 338        ACPI_FUNCTION_TRACE("acpi_bus_create_device_fs");
 339
 340        if (!device)
 341                return_VALUE(-EINVAL);
 342
 343        if (!device->dir.entry)
 344                return_VALUE(-ENODEV);
 345
 346        if (!device->parent)
 347                remove_proc_entry(device->pnp_bus_id, NULL);
 348        else
 349                remove_proc_entry(device->pnp.bus_id, device->parent->fs.entry);
 350
 351        device->dir.entry = NULL;
 352
 353        return_VALUE(0);
 354}
 355*/
 356
 357
 358/* --------------------------------------------------------------------------
 359                                 Power Management
 360   -------------------------------------------------------------------------- */
 361
 362int
 363acpi_bus_get_power (
 364        acpi_handle             handle,
 365        int                     *state)
 366{
 367        int                     result = 0;
 368        acpi_status             status = 0;
 369        struct acpi_device      *device = NULL;
 370        unsigned long           psc = 0;
 371
 372        ACPI_FUNCTION_TRACE("acpi_bus_get_power");
 373
 374        result = acpi_bus_get_device(handle, &device);
 375        if (result)
 376                return_VALUE(result);
 377
 378        *state = ACPI_STATE_UNKNOWN;
 379
 380        if (!device->flags.power_manageable) {
 381                /* TBD: Non-recursive algorithm for walking up hierarchy */
 382                if (device->parent)
 383                        *state = device->parent->power.state;
 384                else
 385                        *state = ACPI_STATE_D0;
 386        }
 387        else {
 388                /*
 389                 * Get the device's power state either directly (via _PSC) or 
 390                 * indirectly (via power resources).
 391                 */
 392                if (device->power.flags.explicit_get) {
 393                        status = acpi_evaluate_integer(device->handle, "_PSC", 
 394                                NULL, &psc);
 395                        if (ACPI_FAILURE(status))
 396                                return_VALUE(-ENODEV);
 397                        device->power.state = (int) psc;
 398                }
 399                else if (device->power.flags.power_resources) {
 400                        result = acpi_power_get_inferred_state(device);
 401                        if (result)
 402                                return_VALUE(result);
 403                }
 404
 405                *state = device->power.state;
 406        }
 407
 408        ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] power state is D%d\n",
 409                device->pnp.bus_id, device->power.state));
 410
 411        return_VALUE(0);
 412}
 413
 414
 415int
 416acpi_bus_set_power (
 417        acpi_handle             handle,
 418        int                     state)
 419{
 420        int                     result = 0;
 421        acpi_status             status = AE_OK;
 422        struct acpi_device      *device = NULL;
 423        char                    object_name[5] = {'_','P','S','0'+state,'\0'};
 424
 425        ACPI_FUNCTION_TRACE("acpi_bus_set_power");
 426
 427        result = acpi_bus_get_device(handle, &device);
 428        if (result)
 429                return_VALUE(result);
 430
 431        if ((state < ACPI_STATE_D0) || (state > ACPI_STATE_D3))
 432                return_VALUE(-EINVAL);
 433
 434        /* Make sure this is a valid target state */
 435
 436        if (!device->flags.power_manageable) {
 437                ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Device is not power manageable\n"));
 438                return_VALUE(-ENODEV);
 439        }
 440        if (state == device->power.state) {
 441                ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device is already at D%d\n", state));
 442                return_VALUE(0);
 443        }
 444        if (!device->power.states[state].flags.valid) {
 445                ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Device does not support D%d\n", state));
 446                return_VALUE(-ENODEV);
 447        }
 448        if (device->parent && (state < device->parent->power.state)) {
 449                ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Cannot set device to a higher-powered state than parent\n"));
 450                return_VALUE(-ENODEV);
 451        }
 452
 453        /*
 454         * Transition Power
 455         * ----------------
 456         * On transitions to a high-powered state we first apply power (via
 457         * power resources) then evalute _PSx.  Conversly for transitions to
 458         * a lower-powered state.
 459         */ 
 460        if (state < device->power.state) {
 461                if (device->power.flags.power_resources) {
 462                        result = acpi_power_transition(device, state);
 463                        if (result)
 464                                goto end;
 465                }
 466                if (device->power.states[state].flags.explicit_set) {
 467                        status = acpi_evaluate_object(device->handle, 
 468                                object_name, NULL, NULL);
 469                        if (ACPI_FAILURE(status)) {
 470                                result = -ENODEV;
 471                                goto end;
 472                        }
 473                }
 474        }
 475        else {
 476                if (device->power.states[state].flags.explicit_set) {
 477                        status = acpi_evaluate_object(device->handle, 
 478                                object_name, NULL, NULL);
 479                        if (ACPI_FAILURE(status)) {
 480                                result = -ENODEV;
 481                                goto end;
 482                        }
 483                }
 484                if (device->power.flags.power_resources) {
 485                        result = acpi_power_transition(device, state);
 486                        if (result)
 487                                goto end;
 488                }
 489        }
 490
 491end:
 492        if (result)
 493                ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Error transitioning device [%s] to D%d\n",
 494                        device->pnp.bus_id, state));
 495        else
 496                ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] transitioned to D%d\n",
 497                        device->pnp.bus_id, state));
 498
 499        return_VALUE(result);
 500}
 501
 502
 503static int
 504acpi_bus_get_power_flags (
 505        struct acpi_device      *device)
 506{
 507        acpi_status             status = 0;
 508        acpi_handle             handle = 0;
 509        u32                     i = 0;
 510
 511        ACPI_FUNCTION_TRACE("acpi_bus_get_power_flags");
 512
 513        if (!device)
 514                return -ENODEV;
 515
 516        /*
 517         * Power Management Flags
 518         */
 519        status = acpi_get_handle(device->handle, "_PSC", &handle);
 520        if (ACPI_SUCCESS(status))
 521                device->power.flags.explicit_get = 1;
 522        status = acpi_get_handle(device->handle, "_IRC", &handle);
 523        if (ACPI_SUCCESS(status))
 524                device->power.flags.inrush_current = 1;
 525        status = acpi_get_handle(device->handle, "_PRW", &handle);
 526        if (ACPI_SUCCESS(status))
 527                device->power.flags.wake_capable = 1;
 528
 529        /*
 530         * Enumerate supported power management states
 531         */
 532        for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
 533                struct acpi_device_power_state *ps = &device->power.states[i];
 534                char            object_name[5] = {'_','P','R','0'+i,'\0'};
 535
 536                /* Evaluate "_PRx" to se if power resources are referenced */
 537                acpi_evaluate_reference(device->handle, object_name, NULL,
 538                        &ps->resources);
 539                if (ps->resources.count) {
 540                        device->power.flags.power_resources = 1;
 541                        ps->flags.valid = 1;
 542                }
 543
 544                /* Evaluate "_PSx" to see if we can do explicit sets */
 545                object_name[2] = 'S';
 546                status = acpi_get_handle(device->handle, object_name, &handle);
 547                if (ACPI_SUCCESS(status)) {
 548                        ps->flags.explicit_set = 1;
 549                        ps->flags.valid = 1;
 550                }
 551
 552                /* State is valid if we have some power control */
 553                if (ps->resources.count || ps->flags.explicit_set)
 554                        ps->flags.valid = 1;
 555
 556                ps->power = -1;         /* Unknown - driver assigned */
 557                ps->latency = -1;       /* Unknown - driver assigned */
 558        }
 559
 560        /* Set defaults for D0 and D3 states (always valid) */
 561        device->power.states[ACPI_STATE_D0].flags.valid = 1;
 562        device->power.states[ACPI_STATE_D0].power = 100;
 563        device->power.states[ACPI_STATE_D3].flags.valid = 1;
 564        device->power.states[ACPI_STATE_D3].power = 0;
 565
 566        /*
 567         * System Power States
 568         * -------------------
 569         */
 570        /* TBD: S1-S4 power state support and resource requirements. */
 571        /*
 572        for (i=ACPI_STATE_S1; i<ACPI_STATE_S5; i++) {
 573                char name[5] = {'_','S',('0'+i),'D','\0'};
 574                status = acpi_evaluate_integer(device->handle, name, NULL,
 575                        &state);
 576                if (ACPI_FAILURE(status))
 577                        continue;
 578        }
 579        */
 580
 581        /* TBD: System wake support and resource requirements. */
 582
 583        device->power.state = ACPI_STATE_UNKNOWN;
 584
 585        return 0;
 586}
 587
 588
 589/* --------------------------------------------------------------------------
 590                              Performance Management
 591   -------------------------------------------------------------------------- */
 592
 593static int
 594acpi_bus_get_perf_flags (
 595        struct acpi_device      *device)
 596{
 597        ACPI_FUNCTION_TRACE("acpi_bus_get_perf_flags");
 598
 599        if (!device)
 600                return -ENODEV;
 601
 602        device->performance.state = ACPI_STATE_UNKNOWN;
 603
 604        return 0;
 605}
 606
 607
 608/* --------------------------------------------------------------------------
 609                                Event Management
 610   -------------------------------------------------------------------------- */
 611
 612static spinlock_t               acpi_bus_event_lock = SPIN_LOCK_UNLOCKED;
 613
 614LIST_HEAD(acpi_bus_event_list);
 615DECLARE_WAIT_QUEUE_HEAD(acpi_bus_event_queue);
 616
 617extern int                      event_is_open;
 618
 619int
 620acpi_bus_generate_event (
 621        struct acpi_device      *device,
 622        u8                      type,
 623        int                     data)
 624{
 625        struct acpi_bus_event   *event = NULL;
 626        u32                     flags = 0;
 627
 628        ACPI_FUNCTION_TRACE("acpi_bus_generate_event");
 629
 630        if (!device)
 631                return_VALUE(-EINVAL);
 632
 633        /* drop event on the floor if no one's listening */
 634        if (!event_is_open)
 635                return_VALUE(0);
 636
 637        event = kmalloc(sizeof(struct acpi_bus_event), GFP_ATOMIC);
 638        if (!event)
 639                return_VALUE(-ENOMEM);
 640
 641        sprintf(event->device_class, "%s", device->pnp.device_class);
 642        sprintf(event->bus_id, "%s", device->pnp.bus_id);
 643        event->type = type;
 644        event->data = data;
 645
 646        spin_lock_irqsave(&acpi_bus_event_lock, flags);
 647        list_add_tail(&event->node, &acpi_bus_event_list);
 648        spin_unlock_irqrestore(&acpi_bus_event_lock, flags);
 649
 650        wake_up_interruptible(&acpi_bus_event_queue);
 651
 652        return_VALUE(0);
 653}
 654
 655int
 656acpi_bus_receive_event (
 657        struct acpi_bus_event   *event)
 658{
 659        u32                     flags = 0;
 660        struct acpi_bus_event   *entry = NULL;
 661
 662        DECLARE_WAITQUEUE(wait, current);
 663
 664        ACPI_FUNCTION_TRACE("acpi_bus_receive_event");
 665
 666        if (!event)
 667                return -EINVAL;
 668
 669        if (list_empty(&acpi_bus_event_list)) {
 670
 671                set_current_state(TASK_INTERRUPTIBLE);
 672                add_wait_queue(&acpi_bus_event_queue, &wait);
 673
 674                if (list_empty(&acpi_bus_event_list))
 675                        schedule();
 676
 677                remove_wait_queue(&acpi_bus_event_queue, &wait);
 678                set_current_state(TASK_RUNNING);
 679
 680                if (signal_pending(current))
 681                        return_VALUE(-ERESTARTSYS);
 682        }
 683
 684        spin_lock_irqsave(&acpi_bus_event_lock, flags);
 685        entry = list_entry(acpi_bus_event_list.next, struct acpi_bus_event, node);
 686        if (entry)
 687                list_del(&entry->node);
 688        spin_unlock_irqrestore(&acpi_bus_event_lock, flags);
 689
 690        if (!entry)
 691                return_VALUE(-ENODEV);
 692
 693        memcpy(event, entry, sizeof(struct acpi_bus_event));
 694
 695        kfree(entry);
 696
 697        return_VALUE(0);
 698}
 699
 700
 701/* --------------------------------------------------------------------------
 702                               Namespace Management
 703   -------------------------------------------------------------------------- */
 704
 705#define WALK_UP                 0
 706#define WALK_DOWN               1
 707
 708typedef int (*acpi_bus_walk_callback)(struct acpi_device*, int, void*);
 709
 710#define HAS_CHILDREN(d)         ((d)->children.next != &((d)->children))
 711#define HAS_SIBLINGS(d)         (((d)->parent) && ((d)->node.next != &(d)->parent->children))
 712#define NODE_TO_DEVICE(n)       (list_entry(n, struct acpi_device, node))
 713
 714
 715/**
 716 * acpi_bus_walk
 717 * -------------
 718 * Used to walk the ACPI Bus's device namespace.  Can walk down (depth-first)
 719 * or up.  Able to parse starting at any node in the namespace.  Note that a
 720 * callback return value of -ELOOP will terminate the walk.
 721 *
 722 * @start:      starting point
 723 * callback:    function to call for every device encountered while parsing
 724 * direction:   direction to parse (up or down)
 725 * @data:       context for this search operation
 726 */
 727static int
 728acpi_bus_walk (
 729        struct acpi_device      *start, 
 730        acpi_bus_walk_callback  callback, 
 731        int                     direction, 
 732        void                    *data)
 733{
 734        int                     result = 0;
 735        int                     level = 0;
 736        struct acpi_device      *device = NULL;
 737
 738        if (!start || !callback)
 739                return -EINVAL;
 740
 741        device = start;
 742
 743        /*
 744         * Parse Namespace
 745         * ---------------
 746         * Parse a given subtree (specified by start) in the given direction.
 747         * Walking 'up' simply means that we execute the callback on leaf
 748         * devices prior to their parents (useful for things like removing
 749         * or powering down a subtree).
 750         */
 751
 752        while (device) {
 753
 754                if (direction == WALK_DOWN)
 755                        if (-ELOOP == callback(device, level, data))
 756                                break;
 757
 758                /* Depth First */
 759
 760                if (HAS_CHILDREN(device)) {
 761                        device = NODE_TO_DEVICE(device->children.next);
 762                        ++level;
 763                        continue;
 764                }
 765
 766                if (direction == WALK_UP)
 767                        if (-ELOOP == callback(device, level, data))
 768                                break;
 769
 770                /* Now Breadth */
 771
 772                if (HAS_SIBLINGS(device)) {
 773                        device = NODE_TO_DEVICE(device->node.next);
 774                        continue;
 775                }
 776
 777                /* Scope Exhausted - Find Next */
 778
 779                while ((device = device->parent)) {
 780                        --level;
 781                        if (HAS_SIBLINGS(device)) {
 782                                device = NODE_TO_DEVICE(device->node.next);
 783                                break;
 784                        }
 785                }
 786        }
 787
 788        if ((direction == WALK_UP) && (result == 0))
 789                callback(start, level, data);
 790
 791        return result;
 792}
 793
 794
 795/* --------------------------------------------------------------------------
 796                             Notification Handling
 797   -------------------------------------------------------------------------- */
 798
 799static int
 800acpi_bus_check_device (
 801        struct acpi_device      *device,
 802        int                     *status_changed)
 803{
 804        acpi_status             status = 0;
 805        struct acpi_device_status old_status;
 806
 807        ACPI_FUNCTION_TRACE("acpi_bus_check_device");
 808
 809        if (!device)
 810                return_VALUE(-EINVAL);
 811
 812        if (status_changed)
 813                *status_changed = 0;
 814
 815        old_status = device->status;
 816
 817        /*
 818         * Make sure this device's parent is present before we go about
 819         * messing with the device.
 820         */
 821        if (device->parent && !device->parent->status.present) {
 822                device->status = device->parent->status;
 823                if (STRUCT_TO_INT(old_status) != STRUCT_TO_INT(device->status)) {
 824                        if (status_changed)
 825                                *status_changed = 1;
 826                }
 827                return_VALUE(0);
 828        }
 829
 830        status = acpi_bus_get_status(device);
 831        if (ACPI_FAILURE(status))
 832                return_VALUE(-ENODEV);
 833
 834        if (STRUCT_TO_INT(old_status) == STRUCT_TO_INT(device->status))
 835                return_VALUE(0);
 836
 837        if (status_changed)
 838                *status_changed = 1;
 839        
 840        /*
 841         * Device Insertion/Removal
 842         */
 843        if ((device->status.present) && !(old_status.present)) {
 844                ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device insertion detected\n"));
 845                /* TBD: Handle device insertion */
 846        }
 847        else if (!(device->status.present) && (old_status.present)) {
 848                ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device removal detected\n"));
 849                /* TBD: Handle device removal */
 850        }
 851
 852        return_VALUE(0);
 853}
 854
 855
 856static int
 857acpi_bus_check_scope (
 858        struct acpi_device      *device)
 859{
 860        int                     result = 0;
 861        int                     status_changed = 0;
 862
 863        ACPI_FUNCTION_TRACE("acpi_bus_check_scope");
 864
 865        if (!device)
 866                return_VALUE(-EINVAL);
 867
 868        /* Status Change? */
 869        result = acpi_bus_check_device(device, &status_changed);
 870        if (result)
 871                return_VALUE(result);
 872
 873        if (!status_changed)
 874                return_VALUE(0);
 875
 876        /*
 877         * TBD: Enumerate child devices within this device's scope and
 878         *       run acpi_bus_check_device()'s on them.
 879         */
 880
 881        return_VALUE(0);
 882}
 883
 884
 885/**
 886 * acpi_bus_notify
 887 * ---------------
 888 * Callback for all 'system-level' device notifications (values 0x00-0x7F).
 889 */
 890static void 
 891acpi_bus_notify (
 892        acpi_handle             handle,
 893        u32                     type,
 894        void                    *data)
 895{
 896        int                     result = 0;
 897        struct acpi_device      *device = NULL;
 898
 899        ACPI_FUNCTION_TRACE("acpi_bus_notify");
 900
 901        if (acpi_bus_get_device(handle, &device))
 902                return_VOID;
 903
 904        switch (type) {
 905
 906        case ACPI_NOTIFY_BUS_CHECK:
 907                ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Received BUS CHECK notification for device [%s]\n", 
 908                        device->pnp.bus_id));
 909                result = acpi_bus_check_scope(device);
 910                /* 
 911                 * TBD: We'll need to outsource certain events to non-ACPI
 912                 *      drivers via the device manager (device.c).
 913                 */
 914                break;
 915
 916        case ACPI_NOTIFY_DEVICE_CHECK:
 917                ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Received DEVICE CHECK notification for device [%s]\n", 
 918                        device->pnp.bus_id));
 919                result = acpi_bus_check_device(device, NULL);
 920                /* 
 921                 * TBD: We'll need to outsource certain events to non-ACPI
 922                 *      drivers via the device manager (device.c).
 923                 */
 924                break;
 925
 926        case ACPI_NOTIFY_DEVICE_WAKE:
 927                ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Received DEVICE WAKE notification for device [%s]\n", 
 928                        device->pnp.bus_id));
 929                /* TBD */
 930                break;
 931
 932        case ACPI_NOTIFY_EJECT_REQUEST:
 933                ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Received EJECT REQUEST notification for device [%s]\n", 
 934                        device->pnp.bus_id));
 935                /* TBD */
 936                break;
 937
 938        case ACPI_NOTIFY_DEVICE_CHECK_LIGHT:
 939                ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Received DEVICE CHECK LIGHT notification for device [%s]\n", 
 940                        device->pnp.bus_id));
 941                /* TBD: Exactly what does 'light' mean? */
 942                break;
 943
 944        case ACPI_NOTIFY_FREQUENCY_MISMATCH:
 945                ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Received FREQUENCY MISMATCH notification for device [%s]\n", 
 946                        device->pnp.bus_id));
 947                /* TBD */
 948                break;
 949
 950        case ACPI_NOTIFY_BUS_MODE_MISMATCH:
 951                ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Received BUS MODE MISMATCH notification for device [%s]\n", 
 952                        device->pnp.bus_id));
 953                /* TBD */
 954                break;
 955
 956        case ACPI_NOTIFY_POWER_FAULT:
 957                ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Received POWER FAULT notification for device [%s]\n", 
 958                        device->pnp.bus_id));
 959                /* TBD */
 960                break;
 961
 962        default:
 963                ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Received unknown/unsupported notification [%08x]\n", 
 964                        type));
 965                break;
 966        }
 967
 968        return_VOID;
 969}
 970
 971
 972/* --------------------------------------------------------------------------
 973                                 Driver Management
 974   -------------------------------------------------------------------------- */
 975
 976static LIST_HEAD(acpi_bus_drivers);
 977static DECLARE_MUTEX(acpi_bus_drivers_lock);
 978
 979
 980/**
 981 * acpi_bus_match 
 982 * --------------
 983 * Checks the device's hardware (_HID) or compatible (_CID) ids to see if it
 984 * matches the specified driver's criteria.
 985 */
 986static int
 987acpi_bus_match (
 988        struct acpi_device      *device,
 989        struct acpi_driver      *driver)
 990{
 991        int error = 0;
 992
 993        if (device->flags.hardware_id)
 994                if (strstr(driver->ids, device->pnp.hardware_id))
 995                        goto Done;
 996
 997        if (device->flags.compatible_ids) {
 998                struct acpi_compatible_id_list *cid_list = device->pnp.cid_list;
 999                int i;
1000
1001                /* compare multiple _CID entries against driver ids */
1002                for (i = 0; i < cid_list->count; i++)
1003                {
1004                        if (strstr(driver->ids, cid_list->id[i].value))
1005                                goto Done;
1006                }
1007        }
1008        error = -ENOENT;
1009
1010 Done:
1011        
1012        return error;
1013}
1014
1015
1016/**
1017 * acpi_bus_driver_init 
1018 * --------------------
1019 * Used to initialize a device via its device driver.  Called whenever a 
1020 * driver is bound to a device.  Invokes the driver's add() and start() ops.
1021 */
1022static int
1023acpi_bus_driver_init (
1024        struct acpi_device      *device, 
1025        struct acpi_driver      *driver)
1026{
1027        int                     result = 0;
1028
1029        ACPI_FUNCTION_TRACE("acpi_bus_driver_init");
1030
1031        if (!device || !driver)
1032                return_VALUE(-EINVAL);
1033
1034        if (!driver->ops.add)
1035                return_VALUE(-ENOSYS);
1036
1037        result = driver->ops.add(device);
1038        if (result) {
1039                device->driver = NULL;
1040                acpi_driver_data(device) = NULL;
1041                return_VALUE(result);
1042        }
1043
1044        device->driver = driver;
1045
1046        /*
1047         * TBD - Configuration Management: Assign resources to device based
1048         * upon possible configuration and currently allocated resources.
1049         */
1050
1051        if (driver->ops.start) {
1052                result = driver->ops.start(device);
1053                if (result && driver->ops.remove)
1054                        driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
1055                return_VALUE(result);
1056        }
1057
1058        ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Driver successfully bound to device\n"));
1059
1060#ifdef CONFIG_LDM
1061        /* 
1062         * Update the device information (in the global device hierarchy) now
1063         * that there's a driver bound to it.
1064         */
1065        strncpy(device->dev.name, device->pnp.device_name, 
1066                sizeof(device->dev.name));
1067#endif
1068
1069        if (driver->ops.scan) {
1070                driver->ops.scan(device);
1071        }
1072
1073        return_VALUE(0);
1074}
1075
1076
1077/**
1078 * acpi_bus_attach 
1079 * -------------
1080 * Callback for acpi_bus_walk() used to find devices that match a specific 
1081 * driver's criteria and then attach the driver.
1082 */
1083static int
1084acpi_bus_attach (
1085        struct acpi_device      *device, 
1086        int                     level, 
1087        void                    *data)
1088{
1089        int                     result = 0;
1090        struct acpi_driver      *driver = NULL;
1091
1092        ACPI_FUNCTION_TRACE("acpi_bus_attach");
1093
1094        if (!device || !data)
1095                return_VALUE(-EINVAL);
1096
1097        driver = (struct acpi_driver *) data;
1098
1099        if (device->driver)
1100                return_VALUE(-EEXIST);
1101
1102        if (!device->status.present)
1103                return_VALUE(-ENODEV);
1104
1105        result = acpi_bus_match(device, driver);
1106        if (result)
1107                return_VALUE(result);
1108
1109        ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found driver [%s] for device [%s]\n",
1110                driver->name, device->pnp.bus_id));
1111        
1112        result = acpi_bus_driver_init(device, driver);
1113        if (result)
1114                return_VALUE(result);
1115
1116        down(&acpi_bus_drivers_lock);
1117        ++driver->references;
1118        up(&acpi_bus_drivers_lock);
1119
1120        return_VALUE(0);
1121}
1122
1123
1124/**
1125 * acpi_bus_unattach 
1126 * -----------------
1127 * Callback for acpi_bus_walk() used to find devices that match a specific 
1128 * driver's criteria and unattach the driver.
1129 */
1130static int
1131acpi_bus_unattach (
1132        struct acpi_device      *device, 
1133        int                     level, 
1134        void                    *data)
1135{
1136        int                     result = 0;
1137        struct acpi_driver      *driver = (struct acpi_driver *) data;
1138
1139        ACPI_FUNCTION_TRACE("acpi_bus_unattach");
1140
1141        if (!device || !driver)
1142                return_VALUE(-EINVAL);
1143
1144        if (device->driver != driver)
1145                return_VALUE(-ENOENT);
1146
1147        if (!driver->ops.remove)
1148                return_VALUE(-ENOSYS);
1149
1150        result = driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
1151        if (result)
1152                return_VALUE(result);
1153
1154        device->driver = NULL;
1155        acpi_driver_data(device) = NULL;
1156
1157        down(&acpi_bus_drivers_lock);
1158        driver->references--;
1159        up(&acpi_bus_drivers_lock);
1160
1161        return_VALUE(0);
1162}
1163
1164
1165/**
1166 * acpi_bus_find_driver 
1167 * --------------------
1168 * Parses the list of registered drivers looking for a driver applicable for
1169 * the specified device.
1170 */
1171static int
1172acpi_bus_find_driver (
1173        struct acpi_device      *device)
1174{
1175        int                     result = -ENODEV;
1176        struct list_head        *entry = NULL;
1177        struct acpi_driver      *driver = NULL;
1178
1179        ACPI_FUNCTION_TRACE("acpi_bus_find_driver");
1180
1181        if (!device || device->driver)
1182                return_VALUE(-EINVAL);
1183
1184        down(&acpi_bus_drivers_lock);
1185
1186        list_for_each(entry, &acpi_bus_drivers) {
1187
1188                driver = list_entry(entry, struct acpi_driver, node);
1189
1190                if (acpi_bus_match(device, driver))
1191                        continue;
1192
1193                result = acpi_bus_driver_init(device, driver);
1194                if (!result)
1195                        ++driver->references;
1196
1197                break;
1198        }
1199
1200        up(&acpi_bus_drivers_lock);
1201
1202        return_VALUE(result);
1203}
1204
1205
1206/**
1207 * acpi_bus_register_driver 
1208 * ------------------------ 
1209 * Registers a driver with the ACPI bus.  Searches the namespace for all
1210 * devices that match the driver's criteria and binds.
1211 */
1212int
1213acpi_bus_register_driver (
1214        struct acpi_driver      *driver)
1215{
1216        ACPI_FUNCTION_TRACE("acpi_bus_register_driver");
1217
1218        if (!driver)
1219                return_VALUE(-EINVAL);
1220
1221        if (acpi_disabled)
1222                return_VALUE(-ENODEV);
1223
1224        down(&acpi_bus_drivers_lock);
1225        list_add_tail(&driver->node, &acpi_bus_drivers);
1226        up(&acpi_bus_drivers_lock);
1227
1228        acpi_bus_walk(acpi_root, acpi_bus_attach, 
1229                WALK_DOWN, driver);
1230
1231        return_VALUE(driver->references);
1232}
1233
1234
1235/**
1236 * acpi_bus_unregister_driver 
1237 * --------------------------
1238 * Unregisters a driver with the ACPI bus.  Searches the namespace for all
1239 * devices that match the driver's criteria and unbinds.
1240 */
1241int
1242acpi_bus_unregister_driver (
1243        struct acpi_driver      *driver)
1244{
1245        ACPI_FUNCTION_TRACE("acpi_bus_unregister_driver");
1246
1247        if (!driver)
1248                return_VALUE(-EINVAL);
1249
1250        acpi_bus_walk(acpi_root, acpi_bus_unattach, WALK_UP, driver);
1251
1252        if (driver->references)
1253                return_VALUE(driver->references);
1254
1255        down(&acpi_bus_drivers_lock);
1256        list_del(&driver->node);
1257        up(&acpi_bus_drivers_lock);
1258
1259        return_VALUE(0);
1260}
1261
1262
1263/* --------------------------------------------------------------------------
1264                                 Device Enumeration
1265   -------------------------------------------------------------------------- */
1266
1267static int 
1268acpi_bus_get_flags (
1269        struct acpi_device      *device)
1270{
1271        acpi_status             status = AE_OK;
1272        acpi_handle             temp = NULL;
1273
1274        ACPI_FUNCTION_TRACE("acpi_bus_get_flags");
1275
1276        /* Presence of _STA indicates 'dynamic_status' */
1277        status = acpi_get_handle(device->handle, "_STA", &temp);
1278        if (ACPI_SUCCESS(status))
1279                device->flags.dynamic_status = 1;
1280
1281        /* Presence of _CID indicates 'compatible_ids' */
1282        status = acpi_get_handle(device->handle, "_CID", &temp);
1283        if (ACPI_SUCCESS(status))
1284                device->flags.compatible_ids = 1;
1285
1286        /* Presence of _RMV indicates 'removable' */
1287        status = acpi_get_handle(device->handle, "_RMV", &temp);
1288        if (ACPI_SUCCESS(status))
1289                device->flags.removable = 1;
1290
1291        /* Presence of _EJD|_EJ0 indicates 'ejectable' */
1292        status = acpi_get_handle(device->handle, "_EJD", &temp);
1293        if (ACPI_SUCCESS(status))
1294                device->flags.ejectable = 1;
1295        else {
1296                status = acpi_get_handle(device->handle, "_EJ0", &temp);
1297                if (ACPI_SUCCESS(status))
1298                        device->flags.ejectable = 1;
1299        }
1300
1301        /* Presence of _LCK indicates 'lockable' */
1302        status = acpi_get_handle(device->handle, "_LCK", &temp);
1303        if (ACPI_SUCCESS(status))
1304                device->flags.lockable = 1;
1305
1306        /* Presence of _PS0|_PR0 indicates 'power manageable' */
1307        status = acpi_get_handle(device->handle, "_PS0", &temp);
1308        if (ACPI_FAILURE(status))
1309                status = acpi_get_handle(device->handle, "_PR0", &temp);
1310        if (ACPI_SUCCESS(status))
1311                device->flags.power_manageable = 1;
1312
1313        /* TBD: Peformance management */
1314
1315        return_VALUE(0);
1316}
1317
1318
1319static int 
1320acpi_bus_add (
1321        struct acpi_device      **child,
1322        struct acpi_device      *parent,
1323        acpi_handle             handle,
1324        int                     type)
1325{
1326        int                     result = 0;
1327        acpi_status             status = AE_OK;
1328        struct acpi_device      *device = NULL;
1329        char                    bus_id[5] = {'?',0};
1330        struct acpi_buffer      buffer = {0, NULL};
1331        struct acpi_device_info *info;
1332        char                    *hid = NULL;
1333        char                    *uid = NULL;
1334        struct acpi_compatible_id_list *cid_list = NULL;
1335        int                     i = 0;
1336
1337        ACPI_FUNCTION_TRACE("acpi_bus_add");
1338
1339        if (!child)
1340                return_VALUE(-EINVAL);
1341
1342        device = kmalloc(sizeof(struct acpi_device), GFP_KERNEL);
1343        if (!device) {
1344                ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Memory allocation error\n"));
1345                return_VALUE(-ENOMEM);
1346        }
1347        memset(device, 0, sizeof(struct acpi_device));
1348
1349        device->handle = handle;
1350        device->parent = parent;
1351
1352        /*
1353         * Bus ID
1354         * ------
1355         * The device's Bus ID is simply the object name.
1356         * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1357         */
1358        switch (type) {
1359        case ACPI_BUS_TYPE_SYSTEM:
1360                sprintf(device->pnp.bus_id, "%s", "ACPI");
1361                break;
1362        case ACPI_BUS_TYPE_POWER_BUTTON:
1363                sprintf(device->pnp.bus_id, "%s", "PWRF");
1364                break;
1365        case ACPI_BUS_TYPE_SLEEP_BUTTON:
1366                sprintf(device->pnp.bus_id, "%s", "SLPF");
1367                break;
1368        default:
1369                buffer.length = sizeof(bus_id);
1370                buffer.pointer = bus_id;
1371                acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
1372                /* Clean up trailing underscores (if any) */
1373                for (i = 3; i > 1; i--) {
1374                        if (bus_id[i] == '_')
1375                                bus_id[i] = '\0';
1376                        else
1377                                break;
1378                }
1379                sprintf(device->pnp.bus_id, "%s", bus_id);
1380                buffer.pointer = NULL;
1381                break;
1382        }
1383
1384        /*
1385         * Flags
1386         * -----
1387         * Get prior to calling acpi_bus_get_status() so we know whether
1388         * or not _STA is present.  Note that we only look for object
1389         * handles -- cannot evaluate objects until we know the device is
1390         * present and properly initialized.
1391         */
1392        result = acpi_bus_get_flags(device);
1393        if (result)
1394                goto end;
1395
1396        /*
1397         * Status
1398         * ------
1399         * See if the device is present.  We always assume that non-Device()
1400         * objects (e.g. thermal zones, power resources, processors, etc.) are
1401         * present, functioning, etc. (at least when parent object is present).
1402         * Note that _STA has a different meaning for some objects (e.g.
1403         * power resources) so we need to be careful how we use it.
1404         */
1405        switch (type) {
1406        case ACPI_BUS_TYPE_DEVICE:
1407                result = acpi_bus_get_status(device);
1408                if (result)
1409                        goto end;
1410                break;
1411        default:
1412                STRUCT_TO_INT(device->status) = 0x0F;
1413                break;
1414        }
1415        if (!device->status.present) {
1416                result = -ENOENT;
1417                goto end;
1418        }
1419
1420        /*
1421         * Initialize Device
1422         * -----------------
1423         * TBD: Synch with Core's enumeration/initialization process.
1424         */
1425
1426        /*
1427         * Hardware ID, Unique ID, & Bus Address
1428         * -------------------------------------
1429         */
1430        switch (type) {
1431        case ACPI_BUS_TYPE_DEVICE:
1432                buffer.length = ACPI_ALLOCATE_BUFFER;
1433                buffer.pointer = NULL;
1434                status = acpi_get_object_info(handle, &buffer);
1435                if (ACPI_FAILURE(status)) {
1436                        ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1437                                "Error reading device info\n"));
1438                        result = -ENODEV;
1439                        goto end;
1440                }
1441                info = buffer.pointer;
1442                if (info->valid & ACPI_VALID_HID)
1443                        hid = info->hardware_id.value;
1444                if (info->valid & ACPI_VALID_UID)
1445                        uid = info->unique_id.value;
1446                if (info->valid & ACPI_VALID_CID) {
1447                        cid_list = &info->compatibility_id;
1448                        device->pnp.cid_list = kmalloc(cid_list->size, GFP_KERNEL);
1449                        if (device->pnp.cid_list)
1450                                memcpy(device->pnp.cid_list, cid_list, cid_list->size);
1451                        else
1452                                ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Memory allocation error\n"));
1453                }
1454                if (info->valid & ACPI_VALID_ADR) {
1455                        device->pnp.bus_address = info->address;
1456                        device->flags.bus_address = 1;
1457                }
1458                break;
1459        case ACPI_BUS_TYPE_POWER:
1460                hid = ACPI_POWER_HID;
1461                break;
1462        case ACPI_BUS_TYPE_PROCESSOR:
1463                hid = ACPI_PROCESSOR_HID;
1464                break;
1465        case ACPI_BUS_TYPE_SYSTEM:
1466                hid = ACPI_SYSTEM_HID;
1467                break;
1468        case ACPI_BUS_TYPE_THERMAL:
1469                hid = ACPI_THERMAL_HID;
1470                break;
1471        case ACPI_BUS_TYPE_POWER_BUTTON:
1472                hid = ACPI_BUTTON_HID_POWERF;
1473                break;
1474        case ACPI_BUS_TYPE_SLEEP_BUTTON:
1475                hid = ACPI_BUTTON_HID_SLEEPF;
1476                break;
1477        }
1478
1479        /* 
1480         * \_SB
1481         * ----
1482         * Fix for the system root bus device -- the only root-level device.
1483         */
1484        if ((parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) {
1485                hid = ACPI_BUS_HID;
1486                sprintf(device->pnp.device_name, "%s", ACPI_BUS_DEVICE_NAME);
1487                sprintf(device->pnp.device_class, "%s", ACPI_BUS_CLASS);
1488        }
1489
1490        if (hid) {
1491                sprintf(device->pnp.hardware_id, "%s", hid);
1492                device->flags.hardware_id = 1;
1493        }
1494        if (uid) {
1495                sprintf(device->pnp.unique_id, "%s", uid);
1496                device->flags.unique_id = 1;
1497        }
1498
1499        /*
1500         * If we called get_object_info, we now are finished with the buffer,
1501         * so we can free it.
1502         */
1503        if (buffer.pointer)
1504                acpi_os_free(buffer.pointer);
1505
1506        /*
1507         * Power Management
1508         * ----------------
1509         */
1510        if (device->flags.power_manageable) {
1511                result = acpi_bus_get_power_flags(device);
1512                if (result)
1513                        goto end;
1514        }
1515
1516        /*
1517         * Performance Management
1518         * ----------------------
1519         */
1520        if (device->flags.performance_manageable) {
1521                result = acpi_bus_get_perf_flags(device);
1522                if (result)
1523                        goto end;
1524        }
1525
1526        /*
1527         * Context
1528         * -------
1529         * Attach this 'struct acpi_device' to the ACPI object.  This makes
1530         * resolutions from handle->device very efficient.  Note that we need
1531         * to be careful with fixed-feature devices as they all attach to the
1532         * root object.
1533         */
1534        switch (type) {
1535        case ACPI_BUS_TYPE_POWER_BUTTON:
1536        case ACPI_BUS_TYPE_SLEEP_BUTTON:
1537                break;
1538        default:
1539                status = acpi_attach_data(device->handle,
1540                        acpi_bus_data_handler, device);
1541                break;
1542        }
1543        if (ACPI_FAILURE(status)) {
1544                ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1545                        "Error attaching device data\n"));
1546                result = -ENODEV;
1547                goto end;
1548        }
1549
1550        /*
1551         * Linkage
1552         * -------
1553         * Link this device to its parent and siblings.
1554         */
1555        INIT_LIST_HEAD(&device->children);
1556        if (!device->parent)
1557                INIT_LIST_HEAD(&device->node);
1558        else
1559                list_add_tail(&device->node, &device->parent->children);
1560
1561#ifdef CONFIG_ACPI_DEBUG
1562        {
1563                char                    *type_string = NULL;
1564                char                    name[80] = {'?','\0'};
1565                struct acpi_buffer      buffer = {sizeof(name), name};
1566
1567                acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1568
1569                switch (type) {
1570                case ACPI_BUS_TYPE_DEVICE:
1571                        type_string = "Device";
1572                        break;
1573                case ACPI_BUS_TYPE_POWER:
1574                        type_string = "Power Resource";
1575                        break;
1576                case ACPI_BUS_TYPE_PROCESSOR:
1577                        type_string = "Processor";
1578                        break;
1579                case ACPI_BUS_TYPE_SYSTEM:
1580                        type_string = "System";
1581                        break;
1582                case ACPI_BUS_TYPE_THERMAL:
1583                        type_string = "Thermal Zone";
1584                        break;
1585                case ACPI_BUS_TYPE_POWER_BUTTON:
1586                        type_string = "Power Button";
1587                        sprintf(name, "PWRB");
1588                        break;
1589                case ACPI_BUS_TYPE_SLEEP_BUTTON:
1590                        type_string = "Sleep Button";
1591                        sprintf(name, "SLPB");
1592                        break;
1593                }
1594
1595                ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %s %s [%p]\n", 
1596                        type_string, name, handle));
1597        }
1598#endif /*CONFIG_ACPI_DEBUG*/
1599
1600        /*
1601         * Global Device Hierarchy:
1602         * ------------------------
1603         * Register this device with the global device hierarchy.
1604         */
1605        acpi_device_register(device, parent);
1606
1607        /*
1608         * Bind _ADR-Based Devices
1609         * -----------------------
1610         * If there's a a bus address (_ADR) then we utilize the parent's 
1611         * 'bind' function (if exists) to bind the ACPI- and natively-
1612         * enumerated device representations.
1613         */
1614        if (device->flags.bus_address) {
1615                if (device->parent && device->parent->ops.bind)
1616                        device->parent->ops.bind(device);
1617        }
1618
1619        /*
1620         * Locate & Attach Driver
1621         * ----------------------
1622         * If there's a hardware id (_HID) or compatible ids (_CID) we check
1623         * to see if there's a driver installed for this kind of device.  Note
1624         * that drivers can install before or after a device is enumerated.
1625         *
1626         * TBD: Assumes LDM provides driver hot-plug capability.
1627         */
1628        if (device->flags.hardware_id || device->flags.compatible_ids)
1629                acpi_bus_find_driver(device);
1630
1631end:
1632        if (result) {
1633                if (device->pnp.cid_list) {
1634                        kfree(device->pnp.cid_list);
1635                }
1636                kfree(device);
1637                return_VALUE(result);
1638        }
1639        *child = device;
1640
1641        return_VALUE(0);
1642}
1643
1644
1645static int
1646acpi_bus_remove (
1647        struct acpi_device      *device, 
1648        int                     type)
1649{
1650        ACPI_FUNCTION_TRACE("acpi_bus_remove");
1651
1652        if (!device)
1653                return_VALUE(-ENODEV);
1654
1655        acpi_device_unregister(device);
1656
1657#if 0
1658        if (device->pnp.cid_list)
1659                kfree(device->pnp.cid_list);
1660#endif
1661        kfree(device);
1662
1663        return_VALUE(0);
1664}
1665
1666
1667int
1668acpi_bus_scan (
1669        struct acpi_device      *start)
1670{
1671        acpi_status             status = AE_OK;
1672        struct acpi_device      *parent = NULL;
1673        struct acpi_device      *child = NULL;
1674        acpi_handle             phandle = 0;
1675        acpi_handle             chandle = 0;
1676        acpi_object_type        type = 0;
1677        u32                     level = 1;
1678
1679        ACPI_FUNCTION_TRACE("acpi_bus_scan");
1680
1681        if (!start)
1682                return_VALUE(-EINVAL);
1683
1684        parent = start;
1685        phandle = start->handle;
1686        
1687        /*
1688         * Parse through the ACPI namespace, identify all 'devices', and
1689         * create a new 'struct acpi_device' for each.
1690         */
1691        while ((level > 0) && parent) {
1692
1693                status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
1694                        chandle, &chandle);
1695
1696                /*
1697                 * If this scope is exhausted then move our way back up.
1698                 */
1699                if (ACPI_FAILURE(status)) {
1700                        level--;
1701                        chandle = phandle;
1702                        acpi_get_parent(phandle, &phandle);
1703                        if (parent->parent)
1704                                parent = parent->parent;
1705                        continue;
1706                }
1707
1708                status = acpi_get_type(chandle, &type);
1709                if (ACPI_FAILURE(status))
1710                        continue;
1711
1712                /*
1713                 * If this is a scope object then parse it (depth-first).
1714                 */
1715                if (type == ACPI_TYPE_LOCAL_SCOPE) {
1716                        level++;
1717                        phandle = chandle;
1718                        chandle = 0;
1719                        continue;
1720                }
1721
1722                /*
1723                 * We're only interested in objects that we consider 'devices'.
1724                 */
1725                switch (type) {
1726                case ACPI_TYPE_DEVICE:
1727                        type = ACPI_BUS_TYPE_DEVICE;
1728                        break;
1729                case ACPI_TYPE_PROCESSOR:
1730                        type = ACPI_BUS_TYPE_PROCESSOR;
1731                        break;
1732                case ACPI_TYPE_THERMAL:
1733                        type = ACPI_BUS_TYPE_THERMAL;
1734                        break;
1735                case ACPI_TYPE_POWER:
1736                        type = ACPI_BUS_TYPE_POWER;
1737                        break;
1738                default:
1739                        continue;
1740                }
1741
1742                status = acpi_bus_add(&child, parent, chandle, type);
1743                if (ACPI_FAILURE(status))
1744                        continue;
1745
1746                /*
1747                 * If the device is present, enabled, and functioning then
1748                 * parse its scope (depth-first).  Note that we need to
1749                 * represent absent devices to facilitate PnP notifications
1750                 * -- but only the subtree head (not all of its children,
1751                 * which will be enumerated when the parent is inserted).
1752                 *
1753                 * TBD: Need notifications and other detection mechanisms
1754                 *      in place before we can fully implement this.
1755                 */
1756                if (child->status.present) {
1757                        status = acpi_get_next_object(ACPI_TYPE_ANY, chandle,
1758                                0, NULL);
1759                        if (ACPI_SUCCESS(status)) {
1760                                level++;
1761                                phandle = chandle;
1762                                chandle = 0;
1763                                parent = child;
1764                        }
1765                }
1766        }
1767
1768        return_VALUE(0);
1769}
1770
1771
1772static int
1773acpi_bus_scan_fixed (
1774        struct acpi_device      *root)
1775{
1776        int                     result = 0;
1777        struct acpi_device      *device = NULL;
1778
1779        ACPI_FUNCTION_TRACE("acpi_bus_scan_fixed");
1780
1781        if (!root)
1782                return_VALUE(-ENODEV);
1783
1784        /*
1785         * Enumerate all fixed-feature devices.
1786         */
1787        if (acpi_fadt.pwr_button == 0)
1788                result = acpi_bus_add(&device, acpi_root, 
1789                        NULL, ACPI_BUS_TYPE_POWER_BUTTON);
1790
1791        if (acpi_fadt.sleep_button == 0)
1792                result = acpi_bus_add(&device, acpi_root, 
1793                        NULL, ACPI_BUS_TYPE_SLEEP_BUTTON);
1794
1795        return_VALUE(result);
1796}
1797
1798
1799/* --------------------------------------------------------------------------
1800                             Initialization/Cleanup
1801   -------------------------------------------------------------------------- */
1802
1803static int __init
1804acpi_bus_init_irq (void)
1805{
1806        acpi_status             status = AE_OK;
1807        union acpi_object       arg = {ACPI_TYPE_INTEGER};
1808        struct acpi_object_list arg_list = {1, &arg};
1809        char                    *message = NULL;
1810
1811        ACPI_FUNCTION_TRACE("acpi_bus_init_irq");
1812
1813        /* 
1814         * Let the system know what interrupt model we are using by
1815         * evaluating the \_PIC object, if exists.
1816         */
1817
1818        switch (acpi_irq_model) {
1819        case ACPI_IRQ_MODEL_PIC:
1820                message = "PIC";
1821                break;
1822        case ACPI_IRQ_MODEL_IOAPIC:
1823                message = "IOAPIC";
1824                break;
1825        case ACPI_IRQ_MODEL_IOSAPIC:
1826                message = "IOSAPIC";
1827                break;
1828        default:
1829                printk(KERN_WARNING PREFIX "Unknown interrupt routing model\n");
1830                return_VALUE(-ENODEV);
1831        }
1832
1833        printk(KERN_INFO PREFIX "Using %s for interrupt routing\n", message);
1834
1835        arg.integer.value = acpi_irq_model;
1836
1837        status = acpi_evaluate_object(NULL, "\\_PIC", &arg_list, NULL);
1838        if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
1839                ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _PIC\n"));
1840                return_VALUE(-ENODEV);
1841        }
1842
1843        return_VALUE(0);
1844}
1845
1846
1847void __init
1848acpi_early_init (void)
1849{
1850        acpi_status             status = AE_OK;
1851        struct acpi_buffer      buffer = {sizeof(acpi_fadt), &acpi_fadt};
1852
1853        ACPI_FUNCTION_TRACE("acpi_bus_init");
1854
1855        status = acpi_initialize_subsystem();
1856        if (ACPI_FAILURE(status)) {
1857                printk(KERN_ERR PREFIX "Unable to initialize the ACPI Interpreter\n");
1858                goto error0;
1859        }
1860
1861        status = acpi_load_tables();
1862        if (ACPI_FAILURE(status)) {
1863                printk(KERN_ERR PREFIX "Unable to load the System Description Tables\n");
1864                goto error0;
1865        }
1866
1867        /*
1868         * Get a separate copy of the FADT for use by other drivers.
1869         */
1870        status = acpi_get_table(ACPI_TABLE_FADT, 1, &buffer);
1871        if (ACPI_FAILURE(status)) {
1872                printk(KERN_ERR PREFIX "Unable to get the FADT\n");
1873                goto error0;
1874        }
1875
1876#ifdef CONFIG_X86
1877        if (!acpi_ioapic) {
1878                extern acpi_interrupt_flags acpi_sci_flags;
1879
1880                /* compatible (0) means level (3) */
1881                if (acpi_sci_flags.trigger == 0)
1882                        acpi_sci_flags.trigger = 3;
1883
1884                /* Set PIC-mode SCI trigger type */
1885                acpi_pic_sci_set_trigger(acpi_fadt.sci_int, acpi_sci_flags.trigger);
1886        } else {
1887                extern int acpi_sci_override_gsi;
1888                /*
1889                 * now that acpi_fadt is initialized,
1890                 * update it with result from INT_SRC_OVR parsing
1891                 */
1892                acpi_fadt.sci_int = acpi_sci_override_gsi;
1893        }
1894#endif
1895
1896        status = acpi_enable_subsystem(~(ACPI_NO_HARDWARE_INIT | ACPI_NO_ACPI_ENABLE));
1897        if (ACPI_FAILURE(status)) {
1898                printk(KERN_ERR PREFIX "Unable to enable ACPI\n");
1899                goto error0;
1900        }
1901
1902        return;
1903
1904error0:
1905        disable_acpi();
1906        return;
1907}
1908
1909static int __init
1910acpi_bus_init (void)
1911{
1912        int                     result = 0;
1913        acpi_status             status = AE_OK;
1914        extern acpi_status      acpi_os_initialize1(void);
1915
1916        ACPI_FUNCTION_TRACE("acpi_bus_init");
1917
1918        status = acpi_os_initialize1();
1919
1920        status = acpi_enable_subsystem(ACPI_NO_HARDWARE_INIT | ACPI_NO_ACPI_ENABLE);
1921        if (ACPI_FAILURE(status)) {
1922                printk(KERN_ERR PREFIX "Unable to start the ACPI Interpreter\n");
1923                goto error1;
1924        }
1925
1926        if (ACPI_FAILURE(status)) {
1927                printk(KERN_ERR PREFIX "Unable to initialize ACPI OS objects\n");
1928                goto error1;
1929        }
1930#ifdef CONFIG_ACPI_EC
1931        /*
1932         * ACPI 2.0 requires the EC driver to be loaded and work before
1933         * the EC device is found in the namespace. This is accomplished
1934         * by looking for the ECDT table, and getting the EC parameters out
1935         * of that.
1936         */
1937        result = acpi_ec_ecdt_probe();
1938        /* Ignore result. Not having an ECDT is not fatal. */
1939#endif
1940
1941        status = acpi_initialize_objects(ACPI_FULL_INITIALIZATION);
1942        if (ACPI_FAILURE(status)) {
1943                printk(KERN_ERR PREFIX "Unable to initialize ACPI objects\n");
1944                goto error1;
1945        }
1946
1947        printk(KERN_INFO PREFIX "Interpreter enabled\n");
1948
1949        /*
1950         * Get the system interrupt model and evaluate \_PIC.
1951         */
1952        result = acpi_bus_init_irq();
1953        if (result)
1954                goto error1;
1955
1956        /*
1957         * Register the for all standard device notifications.
1958         */
1959        status = acpi_install_notify_handler(ACPI_ROOT_OBJECT, ACPI_SYSTEM_NOTIFY, &acpi_bus_notify, NULL);
1960        if (ACPI_FAILURE(status)) {
1961                printk(KERN_ERR PREFIX "Unable to register for device notifications\n");
1962                result = -ENODEV;
1963                goto error1;
1964        }
1965
1966        /*
1967         * Create the root device in the bus's device tree
1968         */
1969        result = acpi_bus_add(&acpi_root, NULL, ACPI_ROOT_OBJECT, 
1970                ACPI_BUS_TYPE_SYSTEM);
1971        if (result)
1972                goto error2;
1973
1974        /*
1975         * Create the top ACPI proc directory
1976         */
1977        acpi_device_dir(acpi_root) = proc_mkdir(ACPI_BUS_FILE_ROOT, NULL);
1978        if (!acpi_root) {
1979                result = -ENODEV;
1980                goto error3;
1981        }
1982        acpi_root_dir = acpi_device_dir(acpi_root);
1983
1984        /*
1985         * Install drivers required for proper enumeration of the
1986         * ACPI namespace.
1987         */
1988        acpi_system_init();     /* ACPI System */
1989        acpi_power_init();      /* ACPI Bus Power Management */
1990#ifdef CONFIG_ACPI_EC
1991        acpi_ec_init();         /* ACPI Embedded Controller */
1992#endif
1993#ifdef CONFIG_ACPI_PCI
1994        if (!acpi_pci_disabled) {
1995                acpi_pci_link_init();   /* ACPI PCI Interrupt Link */
1996                acpi_pci_root_init();   /* ACPI PCI Root Bridge */
1997        }
1998#endif
1999        /*
2000         * Enumerate devices in the ACPI namespace.
2001         */
2002        result = acpi_bus_scan_fixed(acpi_root);
2003        if (result)
2004                goto error4;
2005        result = acpi_bus_scan(acpi_root);
2006        if (result)
2007                goto error4;
2008
2009        acpi_motherboard_init();
2010        return_VALUE(0);
2011
2012        /* Mimic structured exception handling */
2013error4:
2014        remove_proc_entry(ACPI_BUS_FILE_ROOT, NULL);
2015error3:
2016        acpi_bus_remove(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
2017error2:
2018        acpi_remove_notify_handler(ACPI_ROOT_OBJECT,
2019                ACPI_SYSTEM_NOTIFY, &acpi_bus_notify);
2020error1:
2021        acpi_terminate();
2022        return_VALUE(-ENODEV);
2023}
2024
2025
2026static void __exit
2027acpi_bus_exit (void)
2028{
2029        acpi_status             status = AE_OK;
2030
2031        ACPI_FUNCTION_TRACE("acpi_bus_exit");
2032
2033        status = acpi_remove_notify_handler(ACPI_ROOT_OBJECT,
2034                ACPI_SYSTEM_NOTIFY, acpi_bus_notify);
2035        if (ACPI_FAILURE(status))
2036                ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
2037                        "Error removing notify handler\n"));
2038
2039#ifdef CONFIG_ACPI_PCI
2040        acpi_pci_root_exit();
2041        acpi_pci_link_exit();
2042#endif
2043#ifdef CONFIG_ACPI_EC
2044        acpi_ec_exit();
2045#endif
2046        acpi_power_exit();
2047        acpi_system_exit();
2048
2049        acpi_bus_remove(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
2050
2051        remove_proc_entry(ACPI_BUS_FILE_ROOT, NULL);
2052
2053        status = acpi_terminate();
2054        if (ACPI_FAILURE(status))
2055                printk(KERN_ERR PREFIX "Unable to terminate the ACPI Interpreter\n");
2056        else
2057                printk(KERN_ERR PREFIX "Interpreter disabled\n");
2058
2059        return_VOID;
2060}
2061
2062
2063int __init
2064acpi_init (void)
2065{
2066        int                     result = 0;
2067
2068        ACPI_FUNCTION_TRACE("acpi_init");
2069
2070        printk(KERN_INFO PREFIX "Subsystem revision %08x\n",
2071                ACPI_CA_VERSION);
2072
2073        /* Initial core debug level excludes drivers, so include them now */
2074        acpi_set_debug(ACPI_DEBUG_LOW);
2075
2076        if (acpi_disabled) {
2077                printk(KERN_INFO PREFIX "Interpreter disabled.\n");
2078                return -ENODEV;
2079        }
2080
2081#ifdef CONFIG_PM
2082        if (PM_IS_ACTIVE()) {
2083                printk(KERN_INFO PREFIX "APM is already active, exiting\n");
2084                return -ENODEV;
2085        }
2086#endif
2087
2088        result = acpi_bus_init();
2089        if (result) {
2090                disable_acpi();
2091                return_VALUE(result);
2092        }
2093
2094#ifdef CONFIG_PM
2095        pm_active = 1;
2096#endif
2097
2098        return_VALUE(0);
2099}
2100
2101
2102void __exit
2103acpi_exit (void)
2104{
2105        ACPI_FUNCTION_TRACE("acpi_exit");
2106
2107#ifdef CONFIG_PM
2108        pm_active = 0;
2109#endif
2110
2111        acpi_bus_exit();
2112
2113        return_VOID;
2114}
2115
2116
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.