linux/drivers/acpi/scan.c
<<
>>
Prefs
   1/*
   2 * scan.c - support for transforming the ACPI namespace into individual objects
   3 */
   4
   5#include <linux/module.h>
   6#include <linux/init.h>
   7#include <linux/slab.h>
   8#include <linux/kernel.h>
   9#include <linux/acpi.h>
  10#include <linux/signal.h>
  11#include <linux/kthread.h>
  12#include <linux/dmi.h>
  13#include <linux/nls.h>
  14
  15#include <acpi/acpi_drivers.h>
  16
  17#include "internal.h"
  18
  19#define _COMPONENT              ACPI_BUS_COMPONENT
  20ACPI_MODULE_NAME("scan");
  21#define STRUCT_TO_INT(s)        (*((int*)&s))
  22extern struct acpi_device *acpi_root;
  23
  24#define ACPI_BUS_CLASS                  "system_bus"
  25#define ACPI_BUS_HID                    "LNXSYBUS"
  26#define ACPI_BUS_DEVICE_NAME            "System Bus"
  27
  28#define ACPI_IS_ROOT_DEVICE(device)    (!(device)->parent)
  29
  30static const char *dummy_hid = "device";
  31
  32static LIST_HEAD(acpi_device_list);
  33static LIST_HEAD(acpi_bus_id_list);
  34DEFINE_MUTEX(acpi_device_lock);
  35LIST_HEAD(acpi_wakeup_device_list);
  36
  37struct acpi_device_bus_id{
  38        char bus_id[15];
  39        unsigned int instance_no;
  40        struct list_head node;
  41};
  42
  43/*
  44 * Creates hid/cid(s) string needed for modalias and uevent
  45 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
  46 * char *modalias: "acpi:IBM0001:ACPI0001"
  47*/
  48static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
  49                           int size)
  50{
  51        int len;
  52        int count;
  53        struct acpi_hardware_id *id;
  54
  55        if (list_empty(&acpi_dev->pnp.ids))
  56                return 0;
  57
  58        len = snprintf(modalias, size, "acpi:");
  59        size -= len;
  60
  61        list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
  62                count = snprintf(&modalias[len], size, "%s:", id->id);
  63                if (count < 0 || count >= size)
  64                        return -EINVAL;
  65                len += count;
  66                size -= count;
  67        }
  68
  69        modalias[len] = '\0';
  70        return len;
  71}
  72
  73static ssize_t
  74acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
  75        struct acpi_device *acpi_dev = to_acpi_device(dev);
  76        int len;
  77
  78        /* Device has no HID and no CID or string is >1024 */
  79        len = create_modalias(acpi_dev, buf, 1024);
  80        if (len <= 0)
  81                return 0;
  82        buf[len++] = '\n';
  83        return len;
  84}
  85static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
  86
  87/**
  88 * acpi_bus_hot_remove_device: hot-remove a device and its children
  89 * @context: struct acpi_eject_event pointer (freed in this func)
  90 *
  91 * Hot-remove a device and its children. This function frees up the
  92 * memory space passed by arg context, so that the caller may call
  93 * this function asynchronously through acpi_os_hotplug_execute().
  94 */
  95void acpi_bus_hot_remove_device(void *context)
  96{
  97        struct acpi_eject_event *ej_event = (struct acpi_eject_event *) context;
  98        struct acpi_device *device;
  99        acpi_handle handle = ej_event->handle;
 100        struct acpi_object_list arg_list;
 101        union acpi_object arg;
 102        acpi_status status = AE_OK;
 103        u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; /* default */
 104
 105        if (acpi_bus_get_device(handle, &device))
 106                goto err_out;
 107
 108        if (!device)
 109                goto err_out;
 110
 111        ACPI_DEBUG_PRINT((ACPI_DB_INFO,
 112                "Hot-removing device %s...\n", dev_name(&device->dev)));
 113
 114        if (acpi_bus_trim(device, 1)) {
 115                printk(KERN_ERR PREFIX
 116                                "Removing device failed\n");
 117                goto err_out;
 118        }
 119
 120        /* power off device */
 121        status = acpi_evaluate_object(handle, "_PS3", NULL, NULL);
 122        if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
 123                printk(KERN_WARNING PREFIX
 124                                "Power-off device failed\n");
 125
 126        if (device->flags.lockable) {
 127                arg_list.count = 1;
 128                arg_list.pointer = &arg;
 129                arg.type = ACPI_TYPE_INTEGER;
 130                arg.integer.value = 0;
 131                acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
 132        }
 133
 134        arg_list.count = 1;
 135        arg_list.pointer = &arg;
 136        arg.type = ACPI_TYPE_INTEGER;
 137        arg.integer.value = 1;
 138
 139        /*
 140         * TBD: _EJD support.
 141         */
 142        status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
 143        if (ACPI_FAILURE(status)) {
 144                if (status != AE_NOT_FOUND)
 145                        printk(KERN_WARNING PREFIX
 146                                        "Eject device failed\n");
 147                goto err_out;
 148        }
 149
 150        kfree(context);
 151        return;
 152
 153err_out:
 154        /* Inform firmware the hot-remove operation has completed w/ error */
 155        (void) acpi_evaluate_hotplug_ost(handle,
 156                                ej_event->event, ost_code, NULL);
 157        kfree(context);
 158        return;
 159}
 160
 161static ssize_t
 162acpi_eject_store(struct device *d, struct device_attribute *attr,
 163                const char *buf, size_t count)
 164{
 165        int ret = count;
 166        acpi_status status;
 167        acpi_object_type type = 0;
 168        struct acpi_device *acpi_device = to_acpi_device(d);
 169        struct acpi_eject_event *ej_event;
 170
 171        if ((!count) || (buf[0] != '1')) {
 172                return -EINVAL;
 173        }
 174#ifndef FORCE_EJECT
 175        if (acpi_device->driver == NULL) {
 176                ret = -ENODEV;
 177                goto err;
 178        }
 179#endif
 180        status = acpi_get_type(acpi_device->handle, &type);
 181        if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
 182                ret = -ENODEV;
 183                goto err;
 184        }
 185
 186        ej_event = kmalloc(sizeof(*ej_event), GFP_KERNEL);
 187        if (!ej_event) {
 188                ret = -ENOMEM;
 189                goto err;
 190        }
 191
 192        ej_event->handle = acpi_device->handle;
 193        if (acpi_device->flags.eject_pending) {
 194                /* event originated from ACPI eject notification */
 195                ej_event->event = ACPI_NOTIFY_EJECT_REQUEST;
 196                acpi_device->flags.eject_pending = 0;
 197        } else {
 198                /* event originated from user */
 199                ej_event->event = ACPI_OST_EC_OSPM_EJECT;
 200                (void) acpi_evaluate_hotplug_ost(ej_event->handle,
 201                        ej_event->event, ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
 202        }
 203
 204        acpi_os_hotplug_execute(acpi_bus_hot_remove_device, (void *)ej_event);
 205err:
 206        return ret;
 207}
 208
 209static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
 210
 211static ssize_t
 212acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
 213        struct acpi_device *acpi_dev = to_acpi_device(dev);
 214
 215        return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
 216}
 217static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
 218
 219static ssize_t
 220acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
 221        struct acpi_device *acpi_dev = to_acpi_device(dev);
 222        struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
 223        int result;
 224
 225        result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
 226        if (result)
 227                goto end;
 228
 229        result = sprintf(buf, "%s\n", (char*)path.pointer);
 230        kfree(path.pointer);
 231end:
 232        return result;
 233}
 234static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
 235
 236/* sysfs file that shows description text from the ACPI _STR method */
 237static ssize_t description_show(struct device *dev,
 238                                struct device_attribute *attr,
 239                                char *buf) {
 240        struct acpi_device *acpi_dev = to_acpi_device(dev);
 241        int result;
 242
 243        if (acpi_dev->pnp.str_obj == NULL)
 244                return 0;
 245
 246        /*
 247         * The _STR object contains a Unicode identifier for a device.
 248         * We need to convert to utf-8 so it can be displayed.
 249         */
 250        result = utf16s_to_utf8s(
 251                (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
 252                acpi_dev->pnp.str_obj->buffer.length,
 253                UTF16_LITTLE_ENDIAN, buf,
 254                PAGE_SIZE);
 255
 256        buf[result++] = '\n';
 257
 258        return result;
 259}
 260static DEVICE_ATTR(description, 0444, description_show, NULL);
 261
 262static int acpi_device_setup_files(struct acpi_device *dev)
 263{
 264        struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
 265        acpi_status status;
 266        acpi_handle temp;
 267        int result = 0;
 268
 269        /*
 270         * Devices gotten from FADT don't have a "path" attribute
 271         */
 272        if (dev->handle) {
 273                result = device_create_file(&dev->dev, &dev_attr_path);
 274                if (result)
 275                        goto end;
 276        }
 277
 278        if (!list_empty(&dev->pnp.ids)) {
 279                result = device_create_file(&dev->dev, &dev_attr_hid);
 280                if (result)
 281                        goto end;
 282
 283                result = device_create_file(&dev->dev, &dev_attr_modalias);
 284                if (result)
 285                        goto end;
 286        }
 287
 288        /*
 289         * If device has _STR, 'description' file is created
 290         */
 291        status = acpi_get_handle(dev->handle, "_STR", &temp);
 292        if (ACPI_SUCCESS(status)) {
 293                status = acpi_evaluate_object(dev->handle, "_STR",
 294                                        NULL, &buffer);
 295                if (ACPI_FAILURE(status))
 296                        buffer.pointer = NULL;
 297                dev->pnp.str_obj = buffer.pointer;
 298                result = device_create_file(&dev->dev, &dev_attr_description);
 299                if (result)
 300                        goto end;
 301        }
 302
 303        /*
 304         * If device has _EJ0, 'eject' file is created that is used to trigger
 305         * hot-removal function from userland.
 306         */
 307        status = acpi_get_handle(dev->handle, "_EJ0", &temp);
 308        if (ACPI_SUCCESS(status))
 309                result = device_create_file(&dev->dev, &dev_attr_eject);
 310end:
 311        return result;
 312}
 313
 314static void acpi_device_remove_files(struct acpi_device *dev)
 315{
 316        acpi_status status;
 317        acpi_handle temp;
 318
 319        /*
 320         * If device has _STR, remove 'description' file
 321         */
 322        status = acpi_get_handle(dev->handle, "_STR", &temp);
 323        if (ACPI_SUCCESS(status)) {
 324                kfree(dev->pnp.str_obj);
 325                device_remove_file(&dev->dev, &dev_attr_description);
 326        }
 327        /*
 328         * If device has _EJ0, remove 'eject' file.
 329         */
 330        status = acpi_get_handle(dev->handle, "_EJ0", &temp);
 331        if (ACPI_SUCCESS(status))
 332                device_remove_file(&dev->dev, &dev_attr_eject);
 333
 334        device_remove_file(&dev->dev, &dev_attr_modalias);
 335        device_remove_file(&dev->dev, &dev_attr_hid);
 336        if (dev->handle)
 337                device_remove_file(&dev->dev, &dev_attr_path);
 338}
 339/* --------------------------------------------------------------------------
 340                        ACPI Bus operations
 341   -------------------------------------------------------------------------- */
 342
 343int acpi_match_device_ids(struct acpi_device *device,
 344                          const struct acpi_device_id *ids)
 345{
 346        const struct acpi_device_id *id;
 347        struct acpi_hardware_id *hwid;
 348
 349        /*
 350         * If the device is not present, it is unnecessary to load device
 351         * driver for it.
 352         */
 353        if (!device->status.present)
 354                return -ENODEV;
 355
 356        for (id = ids; id->id[0]; id++)
 357                list_for_each_entry(hwid, &device->pnp.ids, list)
 358                        if (!strcmp((char *) id->id, hwid->id))
 359                                return 0;
 360
 361        return -ENOENT;
 362}
 363EXPORT_SYMBOL(acpi_match_device_ids);
 364
 365static void acpi_free_ids(struct acpi_device *device)
 366{
 367        struct acpi_hardware_id *id, *tmp;
 368
 369        list_for_each_entry_safe(id, tmp, &device->pnp.ids, list) {
 370                kfree(id->id);
 371                kfree(id);
 372        }
 373}
 374
 375static void acpi_device_release(struct device *dev)
 376{
 377        struct acpi_device *acpi_dev = to_acpi_device(dev);
 378
 379        acpi_free_ids(acpi_dev);
 380        kfree(acpi_dev);
 381}
 382
 383static int acpi_bus_match(struct device *dev, struct device_driver *drv)
 384{
 385        struct acpi_device *acpi_dev = to_acpi_device(dev);
 386        struct acpi_driver *acpi_drv = to_acpi_driver(drv);
 387
 388        return !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
 389}
 390
 391static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
 392{
 393        struct acpi_device *acpi_dev = to_acpi_device(dev);
 394        int len;
 395
 396        if (list_empty(&acpi_dev->pnp.ids))
 397                return 0;
 398
 399        if (add_uevent_var(env, "MODALIAS="))
 400                return -ENOMEM;
 401        len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
 402                              sizeof(env->buf) - env->buflen);
 403        if (len >= (sizeof(env->buf) - env->buflen))
 404                return -ENOMEM;
 405        env->buflen += len;
 406        return 0;
 407}
 408
 409static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
 410{
 411        struct acpi_device *device = data;
 412
 413        device->driver->ops.notify(device, event);
 414}
 415
 416static acpi_status acpi_device_notify_fixed(void *data)
 417{
 418        struct acpi_device *device = data;
 419
 420        /* Fixed hardware devices have no handles */
 421        acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
 422        return AE_OK;
 423}
 424
 425static int acpi_device_install_notify_handler(struct acpi_device *device)
 426{
 427        acpi_status status;
 428
 429        if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
 430                status =
 431                    acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
 432                                                     acpi_device_notify_fixed,
 433                                                     device);
 434        else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
 435                status =
 436                    acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
 437                                                     acpi_device_notify_fixed,
 438                                                     device);
 439        else
 440                status = acpi_install_notify_handler(device->handle,
 441                                                     ACPI_DEVICE_NOTIFY,
 442                                                     acpi_device_notify,
 443                                                     device);
 444
 445        if (ACPI_FAILURE(status))
 446                return -EINVAL;
 447        return 0;
 448}
 449
 450static void acpi_device_remove_notify_handler(struct acpi_device *device)
 451{
 452        if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
 453                acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
 454                                                acpi_device_notify_fixed);
 455        else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
 456                acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
 457                                                acpi_device_notify_fixed);
 458        else
 459                acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
 460                                           acpi_device_notify);
 461}
 462
 463static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
 464static int acpi_start_single_object(struct acpi_device *);
 465static int acpi_device_probe(struct device * dev)
 466{
 467        struct acpi_device *acpi_dev = to_acpi_device(dev);
 468        struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
 469        int ret;
 470
 471        ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
 472        if (!ret) {
 473                if (acpi_dev->bus_ops.acpi_op_start)
 474                        acpi_start_single_object(acpi_dev);
 475
 476                if (acpi_drv->ops.notify) {
 477                        ret = acpi_device_install_notify_handler(acpi_dev);
 478                        if (ret) {
 479                                if (acpi_drv->ops.remove)
 480                                        acpi_drv->ops.remove(acpi_dev,
 481                                                     acpi_dev->removal_type);
 482                                return ret;
 483                        }
 484                }
 485
 486                ACPI_DEBUG_PRINT((ACPI_DB_INFO,
 487                        "Found driver [%s] for device [%s]\n",
 488                        acpi_drv->name, acpi_dev->pnp.bus_id));
 489                get_device(dev);
 490        }
 491        return ret;
 492}
 493
 494static int acpi_device_remove(struct device * dev)
 495{
 496        struct acpi_device *acpi_dev = to_acpi_device(dev);
 497        struct acpi_driver *acpi_drv = acpi_dev->driver;
 498
 499        if (acpi_drv) {
 500                if (acpi_drv->ops.notify)
 501                        acpi_device_remove_notify_handler(acpi_dev);
 502                if (acpi_drv->ops.remove)
 503                        acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
 504        }
 505        acpi_dev->driver = NULL;
 506        acpi_dev->driver_data = NULL;
 507
 508        put_device(dev);
 509        return 0;
 510}
 511
 512struct bus_type acpi_bus_type = {
 513        .name           = "acpi",
 514        .match          = acpi_bus_match,
 515        .probe          = acpi_device_probe,
 516        .remove         = acpi_device_remove,
 517        .uevent         = acpi_device_uevent,
 518};
 519
 520static int acpi_device_register(struct acpi_device *device)
 521{
 522        int result;
 523        struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
 524        int found = 0;
 525
 526        /*
 527         * Linkage
 528         * -------
 529         * Link this device to its parent and siblings.
 530         */
 531        INIT_LIST_HEAD(&device->children);
 532        INIT_LIST_HEAD(&device->node);
 533        INIT_LIST_HEAD(&device->wakeup_list);
 534        INIT_LIST_HEAD(&device->physical_node_list);
 535        mutex_init(&device->physical_node_lock);
 536
 537        new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
 538        if (!new_bus_id) {
 539                printk(KERN_ERR PREFIX "Memory allocation error\n");
 540                return -ENOMEM;
 541        }
 542
 543        mutex_lock(&acpi_device_lock);
 544        /*
 545         * Find suitable bus_id and instance number in acpi_bus_id_list
 546         * If failed, create one and link it into acpi_bus_id_list
 547         */
 548        list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
 549                if (!strcmp(acpi_device_bus_id->bus_id,
 550                            acpi_device_hid(device))) {
 551                        acpi_device_bus_id->instance_no++;
 552                        found = 1;
 553                        kfree(new_bus_id);
 554                        break;
 555                }
 556        }
 557        if (!found) {
 558                acpi_device_bus_id = new_bus_id;
 559                strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
 560                acpi_device_bus_id->instance_no = 0;
 561                list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
 562        }
 563        dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
 564
 565        if (device->parent)
 566                list_add_tail(&device->node, &device->parent->children);
 567
 568        if (device->wakeup.flags.valid)
 569                list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
 570        mutex_unlock(&acpi_device_lock);
 571
 572        if (device->parent)
 573                device->dev.parent = &device->parent->dev;
 574        device->dev.bus = &acpi_bus_type;
 575        device->dev.release = &acpi_device_release;
 576        result = device_register(&device->dev);
 577        if (result) {
 578                dev_err(&device->dev, "Error registering device\n");
 579                goto end;
 580        }
 581
 582        result = acpi_device_setup_files(device);
 583        if (result)
 584                printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
 585                       dev_name(&device->dev));
 586
 587        device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
 588        return 0;
 589end:
 590        mutex_lock(&acpi_device_lock);
 591        if (device->parent)
 592                list_del(&device->node);
 593        list_del(&device->wakeup_list);
 594        mutex_unlock(&acpi_device_lock);
 595        return result;
 596}
 597
 598static void acpi_device_unregister(struct acpi_device *device, int type)
 599{
 600        mutex_lock(&acpi_device_lock);
 601        if (device->parent)
 602                list_del(&device->node);
 603
 604        list_del(&device->wakeup_list);
 605        mutex_unlock(&acpi_device_lock);
 606
 607        acpi_detach_data(device->handle, acpi_bus_data_handler);
 608
 609        acpi_device_remove_files(device);
 610        device_unregister(&device->dev);
 611}
 612
 613/* --------------------------------------------------------------------------
 614                                 Driver Management
 615   -------------------------------------------------------------------------- */
 616/**
 617 * acpi_bus_driver_init - add a device to a driver
 618 * @device: the device to add and initialize
 619 * @driver: driver for the device
 620 *
 621 * Used to initialize a device via its device driver.  Called whenever a
 622 * driver is bound to a device.  Invokes the driver's add() ops.
 623 */
 624static int
 625acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
 626{
 627        int result = 0;
 628
 629        if (!device || !driver)
 630                return -EINVAL;
 631
 632        if (!driver->ops.add)
 633                return -ENOSYS;
 634
 635        result = driver->ops.add(device);
 636        if (result) {
 637                device->driver = NULL;
 638                device->driver_data = NULL;
 639                return result;
 640        }
 641
 642        device->driver = driver;
 643
 644        /*
 645         * TBD - Configuration Management: Assign resources to device based
 646         * upon possible configuration and currently allocated resources.
 647         */
 648
 649        ACPI_DEBUG_PRINT((ACPI_DB_INFO,
 650                          "Driver successfully bound to device\n"));
 651        return 0;
 652}
 653
 654static int acpi_start_single_object(struct acpi_device *device)
 655{
 656        int result = 0;
 657        struct acpi_driver *driver;
 658
 659
 660        if (!(driver = device->driver))
 661                return 0;
 662
 663        if (driver->ops.start) {
 664                result = driver->ops.start(device);
 665                if (result && driver->ops.remove)
 666                        driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
 667        }
 668
 669        return result;
 670}
 671
 672/**
 673 * acpi_bus_register_driver - register a driver with the ACPI bus
 674 * @driver: driver being registered
 675 *
 676 * Registers a driver with the ACPI bus.  Searches the namespace for all
 677 * devices that match the driver's criteria and binds.  Returns zero for
 678 * success or a negative error status for failure.
 679 */
 680int acpi_bus_register_driver(struct acpi_driver *driver)
 681{
 682        int ret;
 683
 684        if (acpi_disabled)
 685                return -ENODEV;
 686        driver->drv.name = driver->name;
 687        driver->drv.bus = &acpi_bus_type;
 688        driver->drv.owner = driver->owner;
 689
 690        ret = driver_register(&driver->drv);
 691        return ret;
 692}
 693
 694EXPORT_SYMBOL(acpi_bus_register_driver);
 695
 696/**
 697 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
 698 * @driver: driver to unregister
 699 *
 700 * Unregisters a driver with the ACPI bus.  Searches the namespace for all
 701 * devices that match the driver's criteria and unbinds.
 702 */
 703void acpi_bus_unregister_driver(struct acpi_driver *driver)
 704{
 705        driver_unregister(&driver->drv);
 706}
 707
 708EXPORT_SYMBOL(acpi_bus_unregister_driver);
 709
 710/* --------------------------------------------------------------------------
 711                                 Device Enumeration
 712   -------------------------------------------------------------------------- */
 713static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
 714{
 715        acpi_status status;
 716        int ret;
 717        struct acpi_device *device;
 718
 719        /*
 720         * Fixed hardware devices do not appear in the namespace and do not
 721         * have handles, but we fabricate acpi_devices for them, so we have
 722         * to deal with them specially.
 723         */
 724        if (handle == NULL)
 725                return acpi_root;
 726
 727        do {
 728                status = acpi_get_parent(handle, &handle);
 729                if (status == AE_NULL_ENTRY)
 730                        return NULL;
 731                if (ACPI_FAILURE(status))
 732                        return acpi_root;
 733
 734                ret = acpi_bus_get_device(handle, &device);
 735                if (ret == 0)
 736                        return device;
 737        } while (1);
 738}
 739
 740acpi_status
 741acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
 742{
 743        acpi_status status;
 744        acpi_handle tmp;
 745        struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
 746        union acpi_object *obj;
 747
 748        status = acpi_get_handle(handle, "_EJD", &tmp);
 749        if (ACPI_FAILURE(status))
 750                return status;
 751
 752        status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
 753        if (ACPI_SUCCESS(status)) {
 754                obj = buffer.pointer;
 755                status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
 756                                         ejd);
 757                kfree(buffer.pointer);
 758        }
 759        return status;
 760}
 761EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
 762
 763void acpi_bus_data_handler(acpi_handle handle, void *context)
 764{
 765
 766        /* TBD */
 767
 768        return;
 769}
 770
 771static int acpi_bus_get_perf_flags(struct acpi_device *device)
 772{
 773        device->performance.state = ACPI_STATE_UNKNOWN;
 774        return 0;
 775}
 776
 777static acpi_status
 778acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
 779                                             struct acpi_device_wakeup *wakeup)
 780{
 781        struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
 782        union acpi_object *package = NULL;
 783        union acpi_object *element = NULL;
 784        acpi_status status;
 785        int i = 0;
 786
 787        if (!wakeup)
 788                return AE_BAD_PARAMETER;
 789
 790        /* _PRW */
 791        status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
 792        if (ACPI_FAILURE(status)) {
 793                ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
 794                return status;
 795        }
 796
 797        package = (union acpi_object *)buffer.pointer;
 798
 799        if (!package || (package->package.count < 2)) {
 800                status = AE_BAD_DATA;
 801                goto out;
 802        }
 803
 804        element = &(package->package.elements[0]);
 805        if (!element) {
 806                status = AE_BAD_DATA;
 807                goto out;
 808        }
 809        if (element->type == ACPI_TYPE_PACKAGE) {
 810                if ((element->package.count < 2) ||
 811                    (element->package.elements[0].type !=
 812                     ACPI_TYPE_LOCAL_REFERENCE)
 813                    || (element->package.elements[1].type != ACPI_TYPE_INTEGER)) {
 814                        status = AE_BAD_DATA;
 815                        goto out;
 816                }
 817                wakeup->gpe_device =
 818                    element->package.elements[0].reference.handle;
 819                wakeup->gpe_number =
 820                    (u32) element->package.elements[1].integer.value;
 821        } else if (element->type == ACPI_TYPE_INTEGER) {
 822                wakeup->gpe_device = NULL;
 823                wakeup->gpe_number = element->integer.value;
 824        } else {
 825                status = AE_BAD_DATA;
 826                goto out;
 827        }
 828
 829        element = &(package->package.elements[1]);
 830        if (element->type != ACPI_TYPE_INTEGER) {
 831                status = AE_BAD_DATA;
 832                goto out;
 833        }
 834        wakeup->sleep_state = element->integer.value;
 835
 836        if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
 837                status = AE_NO_MEMORY;
 838                goto out;
 839        }
 840        wakeup->resources.count = package->package.count - 2;
 841        for (i = 0; i < wakeup->resources.count; i++) {
 842                element = &(package->package.elements[i + 2]);
 843                if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
 844                        status = AE_BAD_DATA;
 845                        goto out;
 846                }
 847
 848                wakeup->resources.handles[i] = element->reference.handle;
 849        }
 850
 851        acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
 852
 853 out:
 854        kfree(buffer.pointer);
 855
 856        return status;
 857}
 858
 859static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
 860{
 861        struct acpi_device_id button_device_ids[] = {
 862                {"PNP0C0C", 0},
 863                {"PNP0C0D", 0},
 864                {"PNP0C0E", 0},
 865                {"", 0},
 866        };
 867        acpi_status status;
 868        acpi_event_status event_status;
 869
 870        device->wakeup.flags.notifier_present = 0;
 871
 872        /* Power button, Lid switch always enable wakeup */
 873        if (!acpi_match_device_ids(device, button_device_ids)) {
 874                device->wakeup.flags.run_wake = 1;
 875                if (!acpi_match_device_ids(device, &button_device_ids[1])) {
 876                        /* Do not use Lid/sleep button for S5 wakeup */
 877                        if (device->wakeup.sleep_state == ACPI_STATE_S5)
 878                                device->wakeup.sleep_state = ACPI_STATE_S4;
 879                }
 880                device_set_wakeup_capable(&device->dev, true);
 881                return;
 882        }
 883
 884        status = acpi_get_gpe_status(device->wakeup.gpe_device,
 885                                        device->wakeup.gpe_number,
 886                                                &event_status);
 887        if (status == AE_OK)
 888                device->wakeup.flags.run_wake =
 889                                !!(event_status & ACPI_EVENT_FLAG_HANDLE);
 890}
 891
 892static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
 893{
 894        acpi_handle temp;
 895        acpi_status status = 0;
 896        int psw_error;
 897
 898        /* Presence of _PRW indicates wake capable */
 899        status = acpi_get_handle(device->handle, "_PRW", &temp);
 900        if (ACPI_FAILURE(status))
 901                return;
 902
 903        status = acpi_bus_extract_wakeup_device_power_package(device->handle,
 904                                                              &device->wakeup);
 905        if (ACPI_FAILURE(status)) {
 906                ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
 907                return;
 908        }
 909
 910        device->wakeup.flags.valid = 1;
 911        device->wakeup.prepare_count = 0;
 912        acpi_bus_set_run_wake_flags(device);
 913        /* Call _PSW/_DSW object to disable its ability to wake the sleeping
 914         * system for the ACPI device with the _PRW object.
 915         * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
 916         * So it is necessary to call _DSW object first. Only when it is not
 917         * present will the _PSW object used.
 918         */
 919        psw_error = acpi_device_sleep_wake(device, 0, 0, 0);
 920        if (psw_error)
 921                ACPI_DEBUG_PRINT((ACPI_DB_INFO,
 922                                "error in _DSW or _PSW evaluation\n"));
 923}
 924
 925static void acpi_bus_add_power_resource(acpi_handle handle);
 926
 927static int acpi_bus_get_power_flags(struct acpi_device *device)
 928{
 929        acpi_status status = 0;
 930        acpi_handle handle = NULL;
 931        u32 i = 0;
 932
 933
 934        /*
 935         * Power Management Flags
 936         */
 937        status = acpi_get_handle(device->handle, "_PSC", &handle);
 938        if (ACPI_SUCCESS(status))
 939                device->power.flags.explicit_get = 1;
 940        status = acpi_get_handle(device->handle, "_IRC", &handle);
 941        if (ACPI_SUCCESS(status))
 942                device->power.flags.inrush_current = 1;
 943
 944        /*
 945         * Enumerate supported power management states
 946         */
 947        for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
 948                struct acpi_device_power_state *ps = &device->power.states[i];
 949                char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
 950
 951                /* Evaluate "_PRx" to se if power resources are referenced */
 952                acpi_evaluate_reference(device->handle, object_name, NULL,
 953                                        &ps->resources);
 954                if (ps->resources.count) {
 955                        int j;
 956
 957                        device->power.flags.power_resources = 1;
 958                        for (j = 0; j < ps->resources.count; j++)
 959                                acpi_bus_add_power_resource(ps->resources.handles[j]);
 960                }
 961
 962                /* Evaluate "_PSx" to see if we can do explicit sets */
 963                object_name[2] = 'S';
 964                status = acpi_get_handle(device->handle, object_name, &handle);
 965                if (ACPI_SUCCESS(status))
 966                        ps->flags.explicit_set = 1;
 967
 968                /*
 969                 * State is valid if there are means to put the device into it.
 970                 * D3hot is only valid if _PR3 present.
 971                 */
 972                if (ps->resources.count ||
 973                    (ps->flags.explicit_set && i < ACPI_STATE_D3_HOT))
 974                        ps->flags.valid = 1;
 975
 976                ps->power = -1; /* Unknown - driver assigned */
 977                ps->latency = -1;       /* Unknown - driver assigned */
 978        }
 979
 980        /* Set defaults for D0 and D3 states (always valid) */
 981        device->power.states[ACPI_STATE_D0].flags.valid = 1;
 982        device->power.states[ACPI_STATE_D0].power = 100;
 983        device->power.states[ACPI_STATE_D3].flags.valid = 1;
 984        device->power.states[ACPI_STATE_D3].power = 0;
 985
 986        /* Set D3cold's explicit_set flag if _PS3 exists. */
 987        if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
 988                device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
 989
 990        acpi_bus_init_power(device);
 991
 992        return 0;
 993}
 994
 995static int acpi_bus_get_flags(struct acpi_device *device)
 996{
 997        acpi_status status = AE_OK;
 998        acpi_handle temp = NULL;
 999
1000
1001        /* Presence of _STA indicates 'dynamic_status' */
1002        status = acpi_get_handle(device->handle, "_STA", &temp);
1003        if (ACPI_SUCCESS(status))
1004                device->flags.dynamic_status = 1;
1005
1006        /* Presence of _RMV indicates 'removable' */
1007        status = acpi_get_handle(device->handle, "_RMV", &temp);
1008        if (ACPI_SUCCESS(status))
1009                device->flags.removable = 1;
1010
1011        /* Presence of _EJD|_EJ0 indicates 'ejectable' */
1012        status = acpi_get_handle(device->handle, "_EJD", &temp);
1013        if (ACPI_SUCCESS(status))
1014                device->flags.ejectable = 1;
1015        else {
1016                status = acpi_get_handle(device->handle, "_EJ0", &temp);
1017                if (ACPI_SUCCESS(status))
1018                        device->flags.ejectable = 1;
1019        }
1020
1021        /* Presence of _LCK indicates 'lockable' */
1022        status = acpi_get_handle(device->handle, "_LCK", &temp);
1023        if (ACPI_SUCCESS(status))
1024                device->flags.lockable = 1;
1025
1026        /* Power resources cannot be power manageable. */
1027        if (device->device_type == ACPI_BUS_TYPE_POWER)
1028                return 0;
1029
1030        /* Presence of _PS0|_PR0 indicates 'power manageable' */
1031        status = acpi_get_handle(device->handle, "_PS0", &temp);
1032        if (ACPI_FAILURE(status))
1033                status = acpi_get_handle(device->handle, "_PR0", &temp);
1034        if (ACPI_SUCCESS(status))
1035                device->flags.power_manageable = 1;
1036
1037        /* TBD: Performance management */
1038
1039        return 0;
1040}
1041
1042static void acpi_device_get_busid(struct acpi_device *device)
1043{
1044        char bus_id[5] = { '?', 0 };
1045        struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1046        int i = 0;
1047
1048        /*
1049         * Bus ID
1050         * ------
1051         * The device's Bus ID is simply the object name.
1052         * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1053         */
1054        if (ACPI_IS_ROOT_DEVICE(device)) {
1055                strcpy(device->pnp.bus_id, "ACPI");
1056                return;
1057        }
1058
1059        switch (device->device_type) {
1060        case ACPI_BUS_TYPE_POWER_BUTTON:
1061                strcpy(device->pnp.bus_id, "PWRF");
1062                break;
1063        case ACPI_BUS_TYPE_SLEEP_BUTTON:
1064                strcpy(device->pnp.bus_id, "SLPF");
1065                break;
1066        default:
1067                acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
1068                /* Clean up trailing underscores (if any) */
1069                for (i = 3; i > 1; i--) {
1070                        if (bus_id[i] == '_')
1071                                bus_id[i] = '\0';
1072                        else
1073                                break;
1074                }
1075                strcpy(device->pnp.bus_id, bus_id);
1076                break;
1077        }
1078}
1079
1080/*
1081 * acpi_bay_match - see if a device is an ejectable driver bay
1082 *
1083 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1084 * then we can safely call it an ejectable drive bay
1085 */
1086static int acpi_bay_match(struct acpi_device *device){
1087        acpi_status status;
1088        acpi_handle handle;
1089        acpi_handle tmp;
1090        acpi_handle phandle;
1091
1092        handle = device->handle;
1093
1094        status = acpi_get_handle(handle, "_EJ0", &tmp);
1095        if (ACPI_FAILURE(status))
1096                return -ENODEV;
1097
1098        if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
1099                (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
1100                (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
1101                (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
1102                return 0;
1103
1104        if (acpi_get_parent(handle, &phandle))
1105                return -ENODEV;
1106
1107        if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
1108                (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
1109                (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
1110                (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
1111                return 0;
1112
1113        return -ENODEV;
1114}
1115
1116/*
1117 * acpi_dock_match - see if a device has a _DCK method
1118 */
1119static int acpi_dock_match(struct acpi_device *device)
1120{
1121        acpi_handle tmp;
1122        return acpi_get_handle(device->handle, "_DCK", &tmp);
1123}
1124
1125const char *acpi_device_hid(struct acpi_device *device)
1126{
1127        struct acpi_hardware_id *hid;
1128
1129        if (list_empty(&device->pnp.ids))
1130                return dummy_hid;
1131
1132        hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1133        return hid->id;
1134}
1135EXPORT_SYMBOL(acpi_device_hid);
1136
1137static void acpi_add_id(struct acpi_device *device, const char *dev_id)
1138{
1139        struct acpi_hardware_id *id;
1140
1141        id = kmalloc(sizeof(*id), GFP_KERNEL);
1142        if (!id)
1143                return;
1144
1145        id->id = kstrdup(dev_id, GFP_KERNEL);
1146        if (!id->id) {
1147                kfree(id);
1148                return;
1149        }
1150
1151        list_add_tail(&id->list, &device->pnp.ids);
1152}
1153
1154/*
1155 * Old IBM workstations have a DSDT bug wherein the SMBus object
1156 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1157 * prefix.  Work around this.
1158 */
1159static int acpi_ibm_smbus_match(struct acpi_device *device)
1160{
1161        acpi_handle h_dummy;
1162        struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
1163        int result;
1164
1165        if (!dmi_name_in_vendors("IBM"))
1166                return -ENODEV;
1167
1168        /* Look for SMBS object */
1169        result = acpi_get_name(device->handle, ACPI_SINGLE_NAME, &path);
1170        if (result)
1171                return result;
1172
1173        if (strcmp("SMBS", path.pointer)) {
1174                result = -ENODEV;
1175                goto out;
1176        }
1177
1178        /* Does it have the necessary (but misnamed) methods? */
1179        result = -ENODEV;
1180        if (ACPI_SUCCESS(acpi_get_handle(device->handle, "SBI", &h_dummy)) &&
1181            ACPI_SUCCESS(acpi_get_handle(device->handle, "SBR", &h_dummy)) &&
1182            ACPI_SUCCESS(acpi_get_handle(device->handle, "SBW", &h_dummy)))
1183                result = 0;
1184out:
1185        kfree(path.pointer);
1186        return result;
1187}
1188
1189static void acpi_device_set_id(struct acpi_device *device)
1190{
1191        acpi_status status;
1192        struct acpi_device_info *info;
1193        struct acpica_device_id_list *cid_list;
1194        int i;
1195
1196        switch (device->device_type) {
1197        case ACPI_BUS_TYPE_DEVICE:
1198                if (ACPI_IS_ROOT_DEVICE(device)) {
1199                        acpi_add_id(device, ACPI_SYSTEM_HID);
1200                        break;
1201                }
1202
1203                status = acpi_get_object_info(device->handle, &info);
1204                if (ACPI_FAILURE(status)) {
1205                        printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
1206                        return;
1207                }
1208
1209                if (info->valid & ACPI_VALID_HID)
1210                        acpi_add_id(device, info->hardware_id.string);
1211                if (info->valid & ACPI_VALID_CID) {
1212                        cid_list = &info->compatible_id_list;
1213                        for (i = 0; i < cid_list->count; i++)
1214                                acpi_add_id(device, cid_list->ids[i].string);
1215                }
1216                if (info->valid & ACPI_VALID_ADR) {
1217                        device->pnp.bus_address = info->address;
1218                        device->flags.bus_address = 1;
1219                }
1220
1221                kfree(info);
1222
1223                /*
1224                 * Some devices don't reliably have _HIDs & _CIDs, so add
1225                 * synthetic HIDs to make sure drivers can find them.
1226                 */
1227                if (acpi_is_video_device(device))
1228                        acpi_add_id(device, ACPI_VIDEO_HID);
1229                else if (ACPI_SUCCESS(acpi_bay_match(device)))
1230                        acpi_add_id(device, ACPI_BAY_HID);
1231                else if (ACPI_SUCCESS(acpi_dock_match(device)))
1232                        acpi_add_id(device, ACPI_DOCK_HID);
1233                else if (!acpi_ibm_smbus_match(device))
1234                        acpi_add_id(device, ACPI_SMBUS_IBM_HID);
1235                else if (list_empty(&device->pnp.ids) &&
1236                         ACPI_IS_ROOT_DEVICE(device->parent)) {
1237                        acpi_add_id(device, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1238                        strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1239                        strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1240                }
1241
1242                break;
1243        case ACPI_BUS_TYPE_POWER:
1244                acpi_add_id(device, ACPI_POWER_HID);
1245                break;
1246        case ACPI_BUS_TYPE_PROCESSOR:
1247                acpi_add_id(device, ACPI_PROCESSOR_OBJECT_HID);
1248                break;
1249        case ACPI_BUS_TYPE_THERMAL:
1250                acpi_add_id(device, ACPI_THERMAL_HID);
1251                break;
1252        case ACPI_BUS_TYPE_POWER_BUTTON:
1253                acpi_add_id(device, ACPI_BUTTON_HID_POWERF);
1254                break;
1255        case ACPI_BUS_TYPE_SLEEP_BUTTON:
1256                acpi_add_id(device, ACPI_BUTTON_HID_SLEEPF);
1257                break;
1258        }
1259}
1260
1261static int acpi_device_set_context(struct acpi_device *device)
1262{
1263        acpi_status status;
1264
1265        /*
1266         * Context
1267         * -------
1268         * Attach this 'struct acpi_device' to the ACPI object.  This makes
1269         * resolutions from handle->device very efficient.  Fixed hardware
1270         * devices have no handles, so we skip them.
1271         */
1272        if (!device->handle)
1273                return 0;
1274
1275        status = acpi_attach_data(device->handle,
1276                                  acpi_bus_data_handler, device);
1277        if (ACPI_SUCCESS(status))
1278                return 0;
1279
1280        printk(KERN_ERR PREFIX "Error attaching device data\n");
1281        return -ENODEV;
1282}
1283
1284static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
1285{
1286        if (!dev)
1287                return -EINVAL;
1288
1289        dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
1290        device_release_driver(&dev->dev);
1291
1292        if (!rmdevice)
1293                return 0;
1294
1295        /*
1296         * unbind _ADR-Based Devices when hot removal
1297         */
1298        if (dev->flags.bus_address) {
1299                if ((dev->parent) && (dev->parent->ops.unbind))
1300                        dev->parent->ops.unbind(dev);
1301        }
1302        acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1303
1304        return 0;
1305}
1306
1307static int acpi_add_single_object(struct acpi_device **child,
1308                                  acpi_handle handle, int type,
1309                                  unsigned long long sta,
1310                                  struct acpi_bus_ops *ops)
1311{
1312        int result;
1313        struct acpi_device *device;
1314        struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1315
1316        device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1317        if (!device) {
1318                printk(KERN_ERR PREFIX "Memory allocation error\n");
1319                return -ENOMEM;
1320        }
1321
1322        INIT_LIST_HEAD(&device->pnp.ids);
1323        device->device_type = type;
1324        device->handle = handle;
1325        device->parent = acpi_bus_get_parent(handle);
1326        device->bus_ops = *ops; /* workround for not call .start */
1327        STRUCT_TO_INT(device->status) = sta;
1328
1329        acpi_device_get_busid(device);
1330
1331        /*
1332         * Flags
1333         * -----
1334         * Note that we only look for object handles -- cannot evaluate objects
1335         * until we know the device is present and properly initialized.
1336         */
1337        result = acpi_bus_get_flags(device);
1338        if (result)
1339                goto end;
1340
1341        /*
1342         * Initialize Device
1343         * -----------------
1344         * TBD: Synch with Core's enumeration/initialization process.
1345         */
1346        acpi_device_set_id(device);
1347
1348        /*
1349         * Power Management
1350         * ----------------
1351         */
1352        if (device->flags.power_manageable) {
1353                result = acpi_bus_get_power_flags(device);
1354                if (result)
1355                        goto end;
1356        }
1357
1358        /*
1359         * Wakeup device management
1360         *-----------------------
1361         */
1362        acpi_bus_get_wakeup_device_flags(device);
1363
1364        /*
1365         * Performance Management
1366         * ----------------------
1367         */
1368        if (device->flags.performance_manageable) {
1369                result = acpi_bus_get_perf_flags(device);
1370                if (result)
1371                        goto end;
1372        }
1373
1374        if ((result = acpi_device_set_context(device)))
1375                goto end;
1376
1377        result = acpi_device_register(device);
1378
1379        /*
1380         * Bind _ADR-Based Devices when hot add
1381         */
1382        if (device->flags.bus_address) {
1383                if (device->parent && device->parent->ops.bind)
1384                        device->parent->ops.bind(device);
1385        }
1386
1387end:
1388        if (!result) {
1389                acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1390                ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1391                        "Adding %s [%s] parent %s\n", dev_name(&device->dev),
1392                         (char *) buffer.pointer,
1393                         device->parent ? dev_name(&device->parent->dev) :
1394                                          "(null)"));
1395                kfree(buffer.pointer);
1396                *child = device;
1397        } else
1398                acpi_device_release(&device->dev);
1399
1400        return result;
1401}
1402
1403#define ACPI_STA_DEFAULT (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | \
1404                          ACPI_STA_DEVICE_UI      | ACPI_STA_DEVICE_FUNCTIONING)
1405
1406static void acpi_bus_add_power_resource(acpi_handle handle)
1407{
1408        struct acpi_bus_ops ops = {
1409                .acpi_op_add = 1,
1410                .acpi_op_start = 1,
1411        };
1412        struct acpi_device *device = NULL;
1413
1414        acpi_bus_get_device(handle, &device);
1415        if (!device)
1416                acpi_add_single_object(&device, handle, ACPI_BUS_TYPE_POWER,
1417                                        ACPI_STA_DEFAULT, &ops);
1418}
1419
1420static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1421                                    unsigned long long *sta)
1422{
1423        acpi_status status;
1424        acpi_object_type acpi_type;
1425
1426        status = acpi_get_type(handle, &acpi_type);
1427        if (ACPI_FAILURE(status))
1428                return -ENODEV;
1429
1430        switch (acpi_type) {
1431        case ACPI_TYPE_ANY:             /* for ACPI_ROOT_OBJECT */
1432        case ACPI_TYPE_DEVICE:
1433                *type = ACPI_BUS_TYPE_DEVICE;
1434                status = acpi_bus_get_status_handle(handle, sta);
1435                if (ACPI_FAILURE(status))
1436                        return -ENODEV;
1437                break;
1438        case ACPI_TYPE_PROCESSOR:
1439                *type = ACPI_BUS_TYPE_PROCESSOR;
1440                status = acpi_bus_get_status_handle(handle, sta);
1441                if (ACPI_FAILURE(status))
1442                        return -ENODEV;
1443                break;
1444        case ACPI_TYPE_THERMAL:
1445                *type = ACPI_BUS_TYPE_THERMAL;
1446                *sta = ACPI_STA_DEFAULT;
1447                break;
1448        case ACPI_TYPE_POWER:
1449                *type = ACPI_BUS_TYPE_POWER;
1450                *sta = ACPI_STA_DEFAULT;
1451                break;
1452        default:
1453                return -ENODEV;
1454        }
1455
1456        return 0;
1457}
1458
1459static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl,
1460                                      void *context, void **return_value)
1461{
1462        struct acpi_bus_ops *ops = context;
1463        int type;
1464        unsigned long long sta;
1465        struct acpi_device *device;
1466        acpi_status status;
1467        int result;
1468
1469        result = acpi_bus_type_and_status(handle, &type, &sta);
1470        if (result)
1471                return AE_OK;
1472
1473        if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
1474            !(sta & ACPI_STA_DEVICE_FUNCTIONING)) {
1475                struct acpi_device_wakeup wakeup;
1476                acpi_handle temp;
1477
1478                status = acpi_get_handle(handle, "_PRW", &temp);
1479                if (ACPI_SUCCESS(status))
1480                        acpi_bus_extract_wakeup_device_power_package(handle,
1481                                                                     &wakeup);
1482                return AE_CTRL_DEPTH;
1483        }
1484
1485        /*
1486         * We may already have an acpi_device from a previous enumeration.  If
1487         * so, we needn't add it again, but we may still have to start it.
1488         */
1489        device = NULL;
1490        acpi_bus_get_device(handle, &device);
1491        if (ops->acpi_op_add && !device)
1492                acpi_add_single_object(&device, handle, type, sta, ops);
1493
1494        if (!device)
1495                return AE_CTRL_DEPTH;
1496
1497        if (ops->acpi_op_start && !(ops->acpi_op_add)) {
1498                status = acpi_start_single_object(device);
1499                if (ACPI_FAILURE(status))
1500                        return AE_CTRL_DEPTH;
1501        }
1502
1503        if (!*return_value)
1504                *return_value = device;
1505        return AE_OK;
1506}
1507
1508static int acpi_bus_scan(acpi_handle handle, struct acpi_bus_ops *ops,
1509                         struct acpi_device **child)
1510{
1511        acpi_status status;
1512        void *device = NULL;
1513
1514        status = acpi_bus_check_add(handle, 0, ops, &device);
1515        if (ACPI_SUCCESS(status))
1516                acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
1517                                    acpi_bus_check_add, NULL, ops, &device);
1518
1519        if (child)
1520                *child = device;
1521
1522        if (device)
1523                return 0;
1524        else
1525                return -ENODEV;
1526}
1527
1528/*
1529 * acpi_bus_add and acpi_bus_start
1530 *
1531 * scan a given ACPI tree and (probably recently hot-plugged)
1532 * create and add or starts found devices.
1533 *
1534 * If no devices were found -ENODEV is returned which does not
1535 * mean that this is a real error, there just have been no suitable
1536 * ACPI objects in the table trunk from which the kernel could create
1537 * a device and add/start an appropriate driver.
1538 */
1539
1540int
1541acpi_bus_add(struct acpi_device **child,
1542             struct acpi_device *parent, acpi_handle handle, int type)
1543{
1544        struct acpi_bus_ops ops;
1545
1546        memset(&ops, 0, sizeof(ops));
1547        ops.acpi_op_add = 1;
1548
1549        return acpi_bus_scan(handle, &ops, child);
1550}
1551EXPORT_SYMBOL(acpi_bus_add);
1552
1553int acpi_bus_start(struct acpi_device *device)
1554{
1555        struct acpi_bus_ops ops;
1556        int result;
1557
1558        if (!device)
1559                return -EINVAL;
1560
1561        memset(&ops, 0, sizeof(ops));
1562        ops.acpi_op_start = 1;
1563
1564        result = acpi_bus_scan(device->handle, &ops, NULL);
1565
1566        acpi_update_all_gpes();
1567
1568        return result;
1569}
1570EXPORT_SYMBOL(acpi_bus_start);
1571
1572int acpi_bus_trim(struct acpi_device *start, int rmdevice)
1573{
1574        acpi_status status;
1575        struct acpi_device *parent, *child;
1576        acpi_handle phandle, chandle;
1577        acpi_object_type type;
1578        u32 level = 1;
1579        int err = 0;
1580
1581        parent = start;
1582        phandle = start->handle;
1583        child = chandle = NULL;
1584
1585        while ((level > 0) && parent && (!err)) {
1586                status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
1587                                              chandle, &chandle);
1588
1589                /*
1590                 * If this scope is exhausted then move our way back up.
1591                 */
1592                if (ACPI_FAILURE(status)) {
1593                        level--;
1594                        chandle = phandle;
1595                        acpi_get_parent(phandle, &phandle);
1596                        child = parent;
1597                        parent = parent->parent;
1598
1599                        if (level == 0)
1600                                err = acpi_bus_remove(child, rmdevice);
1601                        else
1602                                err = acpi_bus_remove(child, 1);
1603
1604                        continue;
1605                }
1606
1607                status = acpi_get_type(chandle, &type);
1608                if (ACPI_FAILURE(status)) {
1609                        continue;
1610                }
1611                /*
1612                 * If there is a device corresponding to chandle then
1613                 * parse it (depth-first).
1614                 */
1615                if (acpi_bus_get_device(chandle, &child) == 0) {
1616                        level++;
1617                        phandle = chandle;
1618                        chandle = NULL;
1619                        parent = child;
1620                }
1621                continue;
1622        }
1623        return err;
1624}
1625EXPORT_SYMBOL_GPL(acpi_bus_trim);
1626
1627static int acpi_bus_scan_fixed(void)
1628{
1629        int result = 0;
1630        struct acpi_device *device = NULL;
1631        struct acpi_bus_ops ops;
1632
1633        memset(&ops, 0, sizeof(ops));
1634        ops.acpi_op_add = 1;
1635        ops.acpi_op_start = 1;
1636
1637        /*
1638         * Enumerate all fixed-feature devices.
1639         */
1640        if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
1641                result = acpi_add_single_object(&device, NULL,
1642                                                ACPI_BUS_TYPE_POWER_BUTTON,
1643                                                ACPI_STA_DEFAULT,
1644                                                &ops);
1645                device_init_wakeup(&device->dev, true);
1646        }
1647
1648        if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
1649                result = acpi_add_single_object(&device, NULL,
1650                                                ACPI_BUS_TYPE_SLEEP_BUTTON,
1651                                                ACPI_STA_DEFAULT,
1652                                                &ops);
1653        }
1654
1655        return result;
1656}
1657
1658int __init acpi_scan_init(void)
1659{
1660        int result;
1661        struct acpi_bus_ops ops;
1662
1663        memset(&ops, 0, sizeof(ops));
1664        ops.acpi_op_add = 1;
1665        ops.acpi_op_start = 1;
1666
1667        result = bus_register(&acpi_bus_type);
1668        if (result) {
1669                /* We don't want to quit even if we failed to add suspend/resume */
1670                printk(KERN_ERR PREFIX "Could not register bus type\n");
1671        }
1672
1673        acpi_power_init();
1674
1675        /*
1676         * Enumerate devices in the ACPI namespace.
1677         */
1678        result = acpi_bus_scan(ACPI_ROOT_OBJECT, &ops, &acpi_root);
1679
1680        if (!result)
1681                result = acpi_bus_scan_fixed();
1682
1683        if (result)
1684                acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
1685        else
1686                acpi_update_all_gpes();
1687
1688        return result;
1689}
1690
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.