/drivers/acpi/ac.c
C | 386 lines | 283 code | 69 blank | 34 comment | 28 complexity | 95a5771c00ec5f27b3dfbe178b21b837 MD5 | raw file
Possible License(s): LGPL-2.0, AGPL-1.0, GPL-2.0
- /*
- * acpi_ac.c - ACPI AC Adapter Driver ($Revision: 27 $)
- *
- * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
- * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
- *
- * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or (at
- * your option) any later version.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
- *
- * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- */
- #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 PREFIX "ACPI: "
- #define ACPI_AC_CLASS "ac_adapter"
- #define ACPI_AC_DEVICE_NAME "AC Adapter"
- #define ACPI_AC_FILE_STATE "state"
- #define ACPI_AC_NOTIFY_STATUS 0x80
- #define ACPI_AC_STATUS_OFFLINE 0x00
- #define ACPI_AC_STATUS_ONLINE 0x01
- #define ACPI_AC_STATUS_UNKNOWN 0xFF
- #define _COMPONENT ACPI_AC_COMPONENT
- ACPI_MODULE_NAME("ac");
- 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_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 = "ac",
- .class = ACPI_AC_CLASS,
- .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 {
- #ifdef CONFIG_ACPI_SYSFS_POWER
- struct power_supply charger;
- #endif
- struct acpi_device * device;
- unsigned long long state;
- };
- #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
- -------------------------------------------------------------------------- */
- static int acpi_ac_get_state(struct acpi_ac *ac)
- {
- acpi_status status = AE_OK;
- if (!ac)
- return -EINVAL;
- status = acpi_evaluate_integer(ac->device->handle, "_PSR", NULL, &ac->state);
- if (ACPI_FAILURE(status)) {
- ACPI_EXCEPTION((AE_INFO, status, "Error reading AC Adapter state"));
- ac->state = ACPI_AC_STATUS_UNKNOWN;
- return -ENODEV;
- }
- return 0;
- }
- #ifdef CONFIG_ACPI_PROCFS_POWER
- /* --------------------------------------------------------------------------
- FS Interface (/proc)
- -------------------------------------------------------------------------- */
- static struct proc_dir_entry *acpi_ac_dir;
- static int acpi_ac_seq_show(struct seq_file *seq, void *offset)
- {
- struct acpi_ac *ac = seq->private;
- if (!ac)
- return 0;
- if (acpi_ac_get_state(ac)) {
- seq_puts(seq, "ERROR: Unable to read AC Adapter state\n");
- return 0;
- }
- seq_puts(seq, "state: ");
- switch (ac->state) {
- case ACPI_AC_STATUS_OFFLINE:
- seq_puts(seq, "off-line\n");
- break;
- case ACPI_AC_STATUS_ONLINE:
- seq_puts(seq, "on-line\n");
- break;
- default:
- seq_puts(seq, "unknown\n");
- break;
- }
- return 0;
- }
- static int acpi_ac_open_fs(struct inode *inode, struct file *file)
- {
- return single_open(file, acpi_ac_seq_show, PDE(inode)->data);
- }
- static int acpi_ac_add_fs(struct acpi_device *device)
- {
- struct proc_dir_entry *entry = NULL;
- if (!acpi_device_dir(device)) {
- acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
- acpi_ac_dir);
- if (!acpi_device_dir(device))
- return -ENODEV;
- }
- /* 'state' [R] */
- entry = proc_create_data(ACPI_AC_FILE_STATE,
- S_IRUGO, acpi_device_dir(device),
- &acpi_ac_fops, acpi_driver_data(device));
- if (!entry)
- return -ENODEV;
- return 0;
- }
- static int acpi_ac_remove_fs(struct acpi_device *device)
- {
- if (acpi_device_dir(device)) {
- remove_proc_entry(ACPI_AC_FILE_STATE, acpi_device_dir(device));
- remove_proc_entry(acpi_device_bid(device), acpi_ac_dir);
- acpi_device_dir(device) = NULL;
- }
- return 0;
- }
- #endif
- /* --------------------------------------------------------------------------
- Driver Model
- -------------------------------------------------------------------------- */
- static void acpi_ac_notify(struct acpi_device *device, u32 event)
- {
- struct acpi_ac *ac = acpi_driver_data(device);
- if (!ac)
- return;
- switch (event) {
- default:
- ACPI_DEBUG_PRINT((ACPI_DB_INFO,
- "Unsupported event [0x%x]\n", event));
- 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,
-