include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[safe/jmp/linux-2.6] / drivers / acpi / ac.c
index 7839b83..56205a0 100644 (file)
 
 #include <linux/kernel.h>
 #include <linux/module.h>
+#include <linux/slab.h>
 #include <linux/init.h>
 #include <linux/types.h>
+#ifdef CONFIG_ACPI_PROCFS_POWER
 #include <linux/proc_fs.h>
 #include <linux/seq_file.h>
+#endif
+#ifdef CONFIG_ACPI_SYSFS_POWER
+#include <linux/power_supply.h>
+#endif
 #include <acpi/acpi_bus.h>
 #include <acpi/acpi_drivers.h>
 
-#define ACPI_AC_COMPONENT              0x00020000
+#define PREFIX "ACPI: "
+
 #define ACPI_AC_CLASS                  "ac_adapter"
-#define ACPI_AC_HID                    "ACPI0003"
-#define ACPI_AC_DRIVER_NAME            "ACPI AC Adapter Driver"
 #define ACPI_AC_DEVICE_NAME            "AC Adapter"
 #define ACPI_AC_FILE_STATE             "state"
 #define ACPI_AC_NOTIFY_STATUS          0x80
 #define ACPI_AC_STATUS_UNKNOWN         0xFF
 
 #define _COMPONENT             ACPI_AC_COMPONENT
-ACPI_MODULE_NAME("acpi_ac")
+ACPI_MODULE_NAME("ac");
 
-    MODULE_AUTHOR("Paul Diefenbaugh");
-MODULE_DESCRIPTION(ACPI_AC_DRIVER_NAME);
+MODULE_AUTHOR("Paul Diefenbaugh");
+MODULE_DESCRIPTION("ACPI AC Adapter Driver");
 MODULE_LICENSE("GPL");
 
+#ifdef CONFIG_ACPI_PROCFS_POWER
+extern struct proc_dir_entry *acpi_lock_ac_dir(void);
+extern void *acpi_unlock_ac_dir(struct proc_dir_entry *acpi_ac_dir);
+static int acpi_ac_open_fs(struct inode *inode, struct file *file);
+#endif
+
 static int acpi_ac_add(struct acpi_device *device);
 static int acpi_ac_remove(struct acpi_device *device, int type);
-static int acpi_ac_open_fs(struct inode *inode, struct file *file);
+static int acpi_ac_resume(struct acpi_device *device);
+static void acpi_ac_notify(struct acpi_device *device, u32 event);
+
+static const struct acpi_device_id ac_device_ids[] = {
+       {"ACPI0003", 0},
+       {"", 0},
+};
+MODULE_DEVICE_TABLE(acpi, ac_device_ids);
 
 static struct acpi_driver acpi_ac_driver = {
-       .name = ACPI_AC_DRIVER_NAME,
+       .name = "ac",
        .class = ACPI_AC_CLASS,
-       .ids = ACPI_AC_HID,
+       .ids = ac_device_ids,
+       .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
        .ops = {
                .add = acpi_ac_add,
                .remove = acpi_ac_remove,
+               .resume = acpi_ac_resume,
+               .notify = acpi_ac_notify,
                },
 };
 
 struct acpi_ac {
-       acpi_handle handle;
-       unsigned long state;
+#ifdef CONFIG_ACPI_SYSFS_POWER
+       struct power_supply charger;
+#endif
+       struct acpi_device * device;
+       unsigned long long state;
 };
 
-static struct file_operations acpi_ac_fops = {
+#define to_acpi_ac(x) container_of(x, struct acpi_ac, charger);
+
+#ifdef CONFIG_ACPI_PROCFS_POWER
+static const struct file_operations acpi_ac_fops = {
+       .owner = THIS_MODULE,
        .open = acpi_ac_open_fs,
        .read = seq_read,
        .llseek = seq_lseek,
        .release = single_release,
 };
+#endif
+#ifdef CONFIG_ACPI_SYSFS_POWER
+static int get_ac_property(struct power_supply *psy,
+                          enum power_supply_property psp,
+                          union power_supply_propval *val)
+{
+       struct acpi_ac *ac = to_acpi_ac(psy);
+       switch (psp) {
+       case POWER_SUPPLY_PROP_ONLINE:
+               val->intval = ac->state;
+               break;
+       default:
+               return -EINVAL;
+       }
+       return 0;
+}
 
+static enum power_supply_property ac_props[] = {
+       POWER_SUPPLY_PROP_ONLINE,
+};
+#endif
 /* --------------------------------------------------------------------------
                                AC Adapter Management
    -------------------------------------------------------------------------- */
@@ -84,22 +132,21 @@ static int acpi_ac_get_state(struct acpi_ac *ac)
 {
        acpi_status status = AE_OK;
 
-       ACPI_FUNCTION_TRACE("acpi_ac_get_state");
 
        if (!ac)
-               return_VALUE(-EINVAL);
+               return -EINVAL;
 
-       status = acpi_evaluate_integer(ac->handle, "_PSR", NULL, &ac->state);
+       status = acpi_evaluate_integer(ac->device->handle, "_PSR", NULL, &ac->state);
        if (ACPI_FAILURE(status)) {
-               ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
-                                 "Error reading AC Adapter state\n"));
+               ACPI_EXCEPTION((AE_INFO, status, "Error reading AC Adapter state"));
                ac->state = ACPI_AC_STATUS_UNKNOWN;
-               return_VALUE(-ENODEV);
+               return -ENODEV;
        }
 
-       return_VALUE(0);
+       return 0;
 }
 
+#ifdef CONFIG_ACPI_PROCFS_POWER
 /* --------------------------------------------------------------------------
                               FS Interface (/proc)
    -------------------------------------------------------------------------- */
@@ -108,16 +155,15 @@ static struct proc_dir_entry *acpi_ac_dir;
 
 static int acpi_ac_seq_show(struct seq_file *seq, void *offset)
 {
-       struct acpi_ac *ac = (struct acpi_ac *)seq->private;
+       struct acpi_ac *ac = seq->private;
 
-       ACPI_FUNCTION_TRACE("acpi_ac_seq_show");
 
        if (!ac)
-               return_VALUE(0);
+               return 0;
 
        if (acpi_ac_get_state(ac)) {
                seq_puts(seq, "ERROR: Unable to read AC Adapter state\n");
-               return_VALUE(0);
+               return 0;
        }
 
        seq_puts(seq, "state:                   ");
@@ -133,7 +179,7 @@ static int acpi_ac_seq_show(struct seq_file *seq, void *offset)
                break;
        }
 
-       return_VALUE(0);
+       return 0;
 }
 
 static int acpi_ac_open_fs(struct inode *inode, struct file *file)
@@ -145,35 +191,25 @@ static int acpi_ac_add_fs(struct acpi_device *device)
 {
        struct proc_dir_entry *entry = NULL;
 
-       ACPI_FUNCTION_TRACE("acpi_ac_add_fs");
 
        if (!acpi_device_dir(device)) {
                acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
                                                     acpi_ac_dir);
                if (!acpi_device_dir(device))
-                       return_VALUE(-ENODEV);
-               acpi_device_dir(device)->owner = THIS_MODULE;
+                       return -ENODEV;
        }
 
        /* 'state' [R] */
-       entry = create_proc_entry(ACPI_AC_FILE_STATE,
-                                 S_IRUGO, acpi_device_dir(device));
+       entry = proc_create_data(ACPI_AC_FILE_STATE,
+                                S_IRUGO, acpi_device_dir(device),
+                                &acpi_ac_fops, acpi_driver_data(device));
        if (!entry)
-               ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
-                                 "Unable to create '%s' fs entry\n",
-                                 ACPI_AC_FILE_STATE));
-       else {
-               entry->proc_fops = &acpi_ac_fops;
-               entry->data = acpi_driver_data(device);
-               entry->owner = THIS_MODULE;
-       }
-
-       return_VALUE(0);
+               return -ENODEV;
+       return 0;
 }
 
 static int acpi_ac_remove_fs(struct acpi_device *device)
 {
-       ACPI_FUNCTION_TRACE("acpi_ac_remove_fs");
 
        if (acpi_device_dir(device)) {
                remove_proc_entry(ACPI_AC_FILE_STATE, acpi_device_dir(device));
@@ -182,78 +218,78 @@ static int acpi_ac_remove_fs(struct acpi_device *device)
                acpi_device_dir(device) = NULL;
        }
 
-       return_VALUE(0);
+       return 0;
 }
+#endif
 
 /* --------------------------------------------------------------------------
                                    Driver Model
    -------------------------------------------------------------------------- */
 
-static void acpi_ac_notify(acpi_handle handle, u32 event, void *data)
+static void acpi_ac_notify(struct acpi_device *device, u32 event)
 {
-       struct acpi_ac *ac = (struct acpi_ac *)data;
-       struct acpi_device *device = NULL;
+       struct acpi_ac *ac = acpi_driver_data(device);
 
-       ACPI_FUNCTION_TRACE("acpi_ac_notify");
 
        if (!ac)
-               return_VOID;
-
-       if (acpi_bus_get_device(ac->handle, &device))
-               return_VOID;
+               return;
 
        switch (event) {
-       case ACPI_AC_NOTIFY_STATUS:
-               acpi_ac_get_state(ac);
-               acpi_bus_generate_event(device, event, (u32) ac->state);
-               break;
        default:
                ACPI_DEBUG_PRINT((ACPI_DB_INFO,
                                  "Unsupported event [0x%x]\n", event));
-               break;
+       case ACPI_AC_NOTIFY_STATUS:
+       case ACPI_NOTIFY_BUS_CHECK:
+       case ACPI_NOTIFY_DEVICE_CHECK:
+               acpi_ac_get_state(ac);
+               acpi_bus_generate_proc_event(device, event, (u32) ac->state);
+               acpi_bus_generate_netlink_event(device->pnp.device_class,
+                                                 dev_name(&device->dev), event,
+                                                 (u32) ac->state);
+               acpi_notifier_call_chain(device, event, (u32) ac->state);
+#ifdef CONFIG_ACPI_SYSFS_POWER
+               kobject_uevent(&ac->charger.dev->kobj, KOBJ_CHANGE);
+#endif
        }
 
-       return_VOID;
+       return;
 }
 
 static int acpi_ac_add(struct acpi_device *device)
 {
        int result = 0;
-       acpi_status status = AE_OK;
        struct acpi_ac *ac = NULL;
 
-       ACPI_FUNCTION_TRACE("acpi_ac_add");
 
        if (!device)
-               return_VALUE(-EINVAL);
+               return -EINVAL;
 
-       ac = kmalloc(sizeof(struct acpi_ac), GFP_KERNEL);
+       ac = kzalloc(sizeof(struct acpi_ac), GFP_KERNEL);
        if (!ac)
-               return_VALUE(-ENOMEM);
-       memset(ac, 0, sizeof(struct acpi_ac));
+               return -ENOMEM;
 
-       ac->handle = device->handle;
+       ac->device = device;
        strcpy(acpi_device_name(device), ACPI_AC_DEVICE_NAME);
        strcpy(acpi_device_class(device), ACPI_AC_CLASS);
-       acpi_driver_data(device) = ac;
+       device->driver_data = ac;
 
        result = acpi_ac_get_state(ac);
        if (result)
                goto end;
 
+#ifdef CONFIG_ACPI_PROCFS_POWER
        result = acpi_ac_add_fs(device);
+#endif
        if (result)
                goto end;
-
-       status = acpi_install_notify_handler(ac->handle,
-                                            ACPI_DEVICE_NOTIFY, acpi_ac_notify,
-                                            ac);
-       if (ACPI_FAILURE(status)) {
-               ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
-                                 "Error installing notify handler\n"));
-               result = -ENODEV;
-               goto end;
-       }
+#ifdef CONFIG_ACPI_SYSFS_POWER
+       ac->charger.name = acpi_device_bid(device);
+       ac->charger.type = POWER_SUPPLY_TYPE_MAINS;
+       ac->charger.properties = ac_props;
+       ac->charger.num_properties = ARRAY_SIZE(ac_props);
+       ac->charger.get_property = get_ac_property;
+       power_supply_register(&ac->device->dev, &ac->charger);
+#endif
 
        printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
               acpi_device_name(device), acpi_device_bid(device),
@@ -261,67 +297,89 @@ static int acpi_ac_add(struct acpi_device *device)
 
       end:
        if (result) {
+#ifdef CONFIG_ACPI_PROCFS_POWER
                acpi_ac_remove_fs(device);
+#endif
                kfree(ac);
        }
 
-       return_VALUE(result);
+       return result;
+}
+
+static int acpi_ac_resume(struct acpi_device *device)
+{
+       struct acpi_ac *ac;
+       unsigned old_state;
+       if (!device || !acpi_driver_data(device))
+               return -EINVAL;
+       ac = acpi_driver_data(device);
+       old_state = ac->state;
+       if (acpi_ac_get_state(ac))
+               return 0;
+#ifdef CONFIG_ACPI_SYSFS_POWER
+       if (old_state != ac->state)
+               kobject_uevent(&ac->charger.dev->kobj, KOBJ_CHANGE);
+#endif
+       return 0;
 }
 
 static int acpi_ac_remove(struct acpi_device *device, int type)
 {
-       acpi_status status = AE_OK;
        struct acpi_ac *ac = NULL;
 
-       ACPI_FUNCTION_TRACE("acpi_ac_remove");
 
        if (!device || !acpi_driver_data(device))
-               return_VALUE(-EINVAL);
+               return -EINVAL;
 
-       ac = (struct acpi_ac *)acpi_driver_data(device);
-
-       status = acpi_remove_notify_handler(ac->handle,
-                                           ACPI_DEVICE_NOTIFY, acpi_ac_notify);
-       if (ACPI_FAILURE(status))
-               ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
-                                 "Error removing notify handler\n"));
+       ac = acpi_driver_data(device);
 
+#ifdef CONFIG_ACPI_SYSFS_POWER
+       if (ac->charger.dev)
+               power_supply_unregister(&ac->charger);
+#endif
+#ifdef CONFIG_ACPI_PROCFS_POWER
        acpi_ac_remove_fs(device);
+#endif
 
        kfree(ac);
 
-       return_VALUE(0);
+       return 0;
 }
 
 static int __init acpi_ac_init(void)
 {
-       int result = 0;
+       int result;
 
-       ACPI_FUNCTION_TRACE("acpi_ac_init");
+       if (acpi_disabled)
+               return -ENODEV;
 
-       acpi_ac_dir = proc_mkdir(ACPI_AC_CLASS, acpi_root_dir);
+#ifdef CONFIG_ACPI_PROCFS_POWER
+       acpi_ac_dir = acpi_lock_ac_dir();
        if (!acpi_ac_dir)
-               return_VALUE(-ENODEV);
-       acpi_ac_dir->owner = THIS_MODULE;
+               return -ENODEV;
+#endif
 
        result = acpi_bus_register_driver(&acpi_ac_driver);
        if (result < 0) {
-               remove_proc_entry(ACPI_AC_CLASS, acpi_root_dir);
-               return_VALUE(-ENODEV);
+#ifdef CONFIG_ACPI_PROCFS_POWER
+               acpi_unlock_ac_dir(acpi_ac_dir);
+#endif
+               return -ENODEV;
        }
 
-       return_VALUE(0);
+       return 0;
 }
 
 static void __exit acpi_ac_exit(void)
 {
-       ACPI_FUNCTION_TRACE("acpi_ac_exit");
 
        acpi_bus_unregister_driver(&acpi_ac_driver);
 
-       remove_proc_entry(ACPI_AC_CLASS, acpi_root_dir);
+#ifdef CONFIG_ACPI_PROCFS_POWER
+       acpi_unlock_ac_dir(acpi_ac_dir);
+#endif
 
-       return_VOID;
+       return;
 }
 
 module_init(acpi_ac_init);