Merge branch 'next' of git://git.kernel.org/pub/scm/linux/kernel/git/benh/powerpc
[linux-2.6.git] / drivers / acpi / ec.c
index 748ced8..b19a18d 100644 (file)
 #include <linux/init.h>
 #include <linux/types.h>
 #include <linux/delay.h>
-#include <linux/proc_fs.h>
-#include <linux/seq_file.h>
 #include <linux/interrupt.h>
 #include <linux/list.h>
 #include <linux/spinlock.h>
+#include <linux/slab.h>
 #include <asm/io.h>
 #include <acpi/acpi_bus.h>
 #include <acpi/acpi_drivers.h>
 #include <linux/dmi.h>
 
+#include "internal.h"
+
 #define ACPI_EC_CLASS                  "embedded_controller"
 #define ACPI_EC_DEVICE_NAME            "Embedded Controller"
 #define ACPI_EC_FILE_INFO              "info"
 
+#undef PREFIX
 #define PREFIX                         "ACPI: EC: "
 
 /* EC status register */
@@ -67,7 +69,6 @@ enum ec_command {
 
 #define ACPI_EC_DELAY          500     /* Wait 500ms max. during EC ops */
 #define ACPI_EC_UDELAY_GLK     1000    /* Wait 1ms max. to get global lock */
-#define ACPI_EC_CDELAY         10      /* Wait 10us before polling EC */
 #define ACPI_EC_MSI_UDELAY     550     /* Wait 550us for MSI EC */
 
 #define ACPI_EC_STORM_THRESHOLD 8      /* number of false interrupts
@@ -76,10 +77,16 @@ enum ec_command {
 enum {
        EC_FLAGS_QUERY_PENDING,         /* Query is pending */
        EC_FLAGS_GPE_STORM,             /* GPE storm detected */
-       EC_FLAGS_HANDLERS_INSTALLED     /* Handlers for GPE and
+       EC_FLAGS_HANDLERS_INSTALLED,    /* Handlers for GPE and
                                         * OpReg are installed */
+       EC_FLAGS_BLOCKED,               /* Transactions are blocked */
 };
 
+/* ec.c is compiled in acpi namespace so this shows up as acpi.ec_delay param */
+static unsigned int ec_delay __read_mostly = ACPI_EC_DELAY;
+module_param(ec_delay, uint, 0644);
+MODULE_PARM_DESC(ec_delay, "Timeout(ms) waited until an EC command completes");
+
 /* If we find an EC via the ECDT, we need to keep a ptr to its context */
 /* External interfaces use first EC only, so remember */
 typedef int (*acpi_ec_query_func) (void *data);
@@ -104,19 +111,8 @@ struct transaction {
        bool done;
 };
 
-static struct acpi_ec {
-       acpi_handle handle;
-       unsigned long gpe;
-       unsigned long command_addr;
-       unsigned long data_addr;
-       unsigned long global_lock;
-       unsigned long flags;
-       struct mutex lock;
-       wait_queue_head_t wait;
-       struct list_head list;
-       struct transaction *curr;
-       spinlock_t curr_lock;
-} *boot_ec, *first_ec;
+struct acpi_ec *boot_ec, *first_ec;
+EXPORT_SYMBOL(first_ec);
 
 static int EC_FLAGS_MSI; /* Out-of-spec MSI controller */
 static int EC_FLAGS_VALIDATE_ECDT; /* ASUStec ECDTs need to be validated */
@@ -218,7 +214,7 @@ static int ec_poll(struct acpi_ec *ec)
        int repeat = 2; /* number of command restarts */
        while (repeat--) {
                unsigned long delay = jiffies +
-                       msecs_to_jiffies(ACPI_EC_DELAY);
+                       msecs_to_jiffies(ec_delay);
                do {
                        /* don't sleep with disabled interrupts */
                        if (EC_FLAGS_MSI || irqs_disabled()) {
@@ -273,7 +269,7 @@ static int ec_check_ibf0(struct acpi_ec *ec)
 
 static int ec_wait_ibf0(struct acpi_ec *ec)
 {
-       unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
+       unsigned long delay = jiffies + msecs_to_jiffies(ec_delay);
        /* interrupt wait manually if GPE mode is not active */
        while (time_before(jiffies, delay))
                if (wait_event_timeout(ec->wait, ec_check_ibf0(ec),
@@ -291,6 +287,10 @@ static int acpi_ec_transaction(struct acpi_ec *ec, struct transaction *t)
        if (t->rdata)
                memset(t->rdata, 0, t->rlen);
        mutex_lock(&ec->lock);
+       if (test_bit(EC_FLAGS_BLOCKED, &ec->flags)) {
+               status = -EINVAL;
+               goto unlock;
+       }
        if (ec->global_lock) {
                status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
                if (ACPI_FAILURE(status)) {
@@ -307,6 +307,7 @@ static int acpi_ec_transaction(struct acpi_ec *ec, struct transaction *t)
        pr_debug(PREFIX "transaction start\n");
        /* disable GPE during transaction if storm is detected */
        if (test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
+               /* It has to be disabled, so that it doesn't trigger. */
                acpi_disable_gpe(NULL, ec->gpe);
        }
 
@@ -316,7 +317,7 @@ static int acpi_ec_transaction(struct acpi_ec *ec, struct transaction *t)
        ec_check_sci_sync(ec, acpi_ec_read_status(ec));
        if (test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
                msleep(1);
-               /* it is safe to enable GPE outside of transaction */
+               /* It is safe to enable the GPE outside of the transaction. */
                acpi_enable_gpe(NULL, ec->gpe);
        } else if (t->irq_count > ACPI_EC_STORM_THRESHOLD) {
                pr_info(PREFIX "GPE storm detected, "
@@ -431,8 +432,7 @@ EXPORT_SYMBOL(ec_write);
 
 int ec_transaction(u8 command,
                   const u8 * wdata, unsigned wdata_len,
-                  u8 * rdata, unsigned rdata_len,
-                  int force_poll)
+                  u8 * rdata, unsigned rdata_len)
 {
        struct transaction t = {.command = command,
                                .wdata = wdata, .rdata = rdata,
@@ -445,6 +445,42 @@ int ec_transaction(u8 command,
 
 EXPORT_SYMBOL(ec_transaction);
 
+void acpi_ec_block_transactions(void)
+{
+       struct acpi_ec *ec = first_ec;
+
+       if (!ec)
+               return;
+
+       mutex_lock(&ec->lock);
+       /* Prevent transactions from being carried out */
+       set_bit(EC_FLAGS_BLOCKED, &ec->flags);
+       mutex_unlock(&ec->lock);
+}
+
+void acpi_ec_unblock_transactions(void)
+{
+       struct acpi_ec *ec = first_ec;
+
+       if (!ec)
+               return;
+
+       mutex_lock(&ec->lock);
+       /* Allow transactions to be carried out again */
+       clear_bit(EC_FLAGS_BLOCKED, &ec->flags);
+       mutex_unlock(&ec->lock);
+}
+
+void acpi_ec_unblock_transactions_early(void)
+{
+       /*
+        * Allow transactions to happen again (this function is called from
+        * atomic context during wakeup, so we don't need to acquire the mutex).
+        */
+       if (first_ec)
+               clear_bit(EC_FLAGS_BLOCKED, &first_ec->flags);
+}
+
 static int acpi_ec_query_unlocked(struct acpi_ec *ec, u8 * data)
 {
        int result;
@@ -554,8 +590,6 @@ static void acpi_ec_gpe_query(void *ec_cxt)
        mutex_unlock(&ec->lock);
 }
 
-static void acpi_ec_gpe_query(void *ec_cxt);
-
 static int ec_check_sci(struct acpi_ec *ec, u8 state)
 {
        if (state & ACPI_EC_FLAG_SCI) {
@@ -568,7 +602,8 @@ static int ec_check_sci(struct acpi_ec *ec, u8 state)
        return 0;
 }
 
-static u32 acpi_ec_gpe_handler(void *data)
+static u32 acpi_ec_gpe_handler(acpi_handle gpe_device,
+       u32 gpe_number, void *data)
 {
        struct acpi_ec *ec = data;
 
@@ -580,7 +615,7 @@ static u32 acpi_ec_gpe_handler(void *data)
                wake_up(&ec->wait);
                ec_check_sci(ec, acpi_ec_read_status(ec));
        }
-       return ACPI_INTERRUPT_HANDLED;
+       return ACPI_INTERRUPT_HANDLED | ACPI_REENABLE_GPE;
 }
 
 /* --------------------------------------------------------------------------
@@ -589,12 +624,12 @@ static u32 acpi_ec_gpe_handler(void *data)
 
 static acpi_status
 acpi_ec_space_handler(u32 function, acpi_physical_address address,
-                     u32 bits, u64 *value,
+                     u32 bits, u64 *value64,
                      void *handler_context, void *region_context)
 {
        struct acpi_ec *ec = handler_context;
-       int result = 0, i;
-       u8 temp = 0;
+       int result = 0, i, bytes = bits / 8;
+       u8 *value = (u8 *)value64;
 
        if ((address > 0xFF) || !value || !handler_context)
                return AE_BAD_PARAMETER;
@@ -602,32 +637,15 @@ acpi_ec_space_handler(u32 function, acpi_physical_address address,
        if (function != ACPI_READ && function != ACPI_WRITE)
                return AE_BAD_PARAMETER;
 
-       if (bits != 8 && acpi_strict)
-               return AE_BAD_PARAMETER;
-
-       if (EC_FLAGS_MSI)
+       if (EC_FLAGS_MSI || bits > 8)
                acpi_ec_burst_enable(ec);
 
-       if (function == ACPI_READ) {
-               result = acpi_ec_read(ec, address, &temp);
-               *value = temp;
-       } else {
-               temp = 0xff & (*value);
-               result = acpi_ec_write(ec, address, temp);
-       }
+       for (i = 0; i < bytes; ++i, ++address, ++value)
+               result = (function == ACPI_READ) ?
+                       acpi_ec_read(ec, address, value) :
+                       acpi_ec_write(ec, address, *value);
 
-       for (i = 8; unlikely(bits - i > 0); i += 8) {
-               ++address;
-               if (function == ACPI_READ) {
-                       result = acpi_ec_read(ec, address, &temp);
-                       (*value) |= ((u64)temp) << i;
-               } else {
-                       temp = 0xff & ((*value) >> i);
-                       result = acpi_ec_write(ec, address, temp);
-               }
-       }
-
-       if (EC_FLAGS_MSI)
+       if (EC_FLAGS_MSI || bits > 8)
                acpi_ec_burst_disable(ec);
 
        switch (result) {
@@ -646,72 +664,6 @@ acpi_ec_space_handler(u32 function, acpi_physical_address address,
 }
 
 /* --------------------------------------------------------------------------
-                              FS Interface (/proc)
-   -------------------------------------------------------------------------- */
-
-static struct proc_dir_entry *acpi_ec_dir;
-
-static int acpi_ec_read_info(struct seq_file *seq, void *offset)
-{
-       struct acpi_ec *ec = seq->private;
-
-       if (!ec)
-               goto end;
-
-       seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
-       seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
-                  (unsigned)ec->command_addr, (unsigned)ec->data_addr);
-       seq_printf(seq, "use global lock:\t%s\n",
-                  ec->global_lock ? "yes" : "no");
-      end:
-       return 0;
-}
-
-static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
-{
-       return single_open(file, acpi_ec_read_info, PDE(inode)->data);
-}
-
-static const struct file_operations acpi_ec_info_ops = {
-       .open = acpi_ec_info_open_fs,
-       .read = seq_read,
-       .llseek = seq_lseek,
-       .release = single_release,
-       .owner = THIS_MODULE,
-};
-
-static int acpi_ec_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_ec_dir);
-               if (!acpi_device_dir(device))
-                       return -ENODEV;
-       }
-
-       entry = proc_create_data(ACPI_EC_FILE_INFO, S_IRUGO,
-                                acpi_device_dir(device),
-                                &acpi_ec_info_ops, acpi_driver_data(device));
-       if (!entry)
-               return -ENODEV;
-       return 0;
-}
-
-static int acpi_ec_remove_fs(struct acpi_device *device)
-{
-
-       if (acpi_device_dir(device)) {
-               remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
-               remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
-               acpi_device_dir(device) = NULL;
-       }
-
-       return 0;
-}
-
-/* --------------------------------------------------------------------------
                                Driver Interface
    -------------------------------------------------------------------------- */
 static acpi_status
@@ -788,7 +740,7 @@ static int ec_install_handlers(struct acpi_ec *ec)
                                  &acpi_ec_gpe_handler, ec);
        if (ACPI_FAILURE(status))
                return -ENODEV;
-       acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
+
        acpi_enable_gpe(NULL, ec->gpe);
        status = acpi_install_address_space_handler(ec->handle,
                                                    ACPI_ADR_SPACE_EC,
@@ -806,6 +758,7 @@ static int ec_install_handlers(struct acpi_ec *ec)
                } else {
                        acpi_remove_gpe_handler(NULL, ec->gpe,
                                &acpi_ec_gpe_handler);
+                       acpi_disable_gpe(NULL, ec->gpe);
                        return -ENODEV;
                }
        }
@@ -816,6 +769,7 @@ static int ec_install_handlers(struct acpi_ec *ec)
 
 static void ec_remove_handlers(struct acpi_ec *ec)
 {
+       acpi_disable_gpe(NULL, ec->gpe);
        if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
                                ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
                pr_err(PREFIX "failed to remove space handler\n");
@@ -850,8 +804,6 @@ static int acpi_ec_add(struct acpi_device *device)
                        return -EINVAL;
        }
 
-       ec->handle = device->handle;
-
        /* Find and register all query methods */
        acpi_walk_namespace(ACPI_TYPE_METHOD, ec->handle, 1,
                            acpi_ec_register_query_methods, NULL, ec, NULL);
@@ -859,7 +811,12 @@ static int acpi_ec_add(struct acpi_device *device)
        if (!first_ec)
                first_ec = ec;
        device->driver_data = ec;
-       acpi_ec_add_fs(device);
+
+       WARN(!request_region(ec->data_addr, 1, "EC data"),
+            "Could not request EC data io port 0x%lx", ec->data_addr);
+       WARN(!request_region(ec->command_addr, 1, "EC cmd"),
+            "Could not request EC cmd io port 0x%lx", ec->command_addr);
+
        pr_info(PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
                          ec->gpe, ec->command_addr, ec->data_addr);
 
@@ -886,7 +843,8 @@ static int acpi_ec_remove(struct acpi_device *device, int type)
                kfree(handler);
        }
        mutex_unlock(&ec->lock);
-       acpi_ec_remove_fs(device);
+       release_region(ec->data_addr, 1);
+       release_region(ec->command_addr, 1);
        device->driver_data = NULL;
        if (ec == first_ec)
                first_ec = NULL;
@@ -971,8 +929,22 @@ static struct dmi_system_id __initdata ec_dmi_table[] = {
        ec_flag_msi, "MSI hardware", {
        DMI_MATCH(DMI_CHASSIS_VENDOR, "MICRO-Star")}, NULL},
        {
+       ec_flag_msi, "MSI hardware", {
+       DMI_MATCH(DMI_CHASSIS_VENDOR, "MICRO-STAR")}, NULL},
+       {
+       ec_flag_msi, "Quanta hardware", {
+       DMI_MATCH(DMI_SYS_VENDOR, "Quanta"),
+       DMI_MATCH(DMI_PRODUCT_NAME, "TW8/SW8/DW8"),}, NULL},
+       {
+       ec_flag_msi, "Quanta hardware", {
+       DMI_MATCH(DMI_SYS_VENDOR, "Quanta"),
+       DMI_MATCH(DMI_PRODUCT_NAME, "TW9/SW9"),}, NULL},
+       {
        ec_validate_ecdt, "ASUS hardware", {
        DMI_MATCH(DMI_BIOS_VENDOR, "ASUS") }, NULL},
+       {
+       ec_validate_ecdt, "ASUS hardware", {
+       DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer Inc.") }, NULL},
        {},
 };
 
@@ -1002,10 +974,9 @@ int __init acpi_ec_ecdt_probe(void)
                /* Don't trust ECDT, which comes from ASUSTek */
                if (!EC_FLAGS_VALIDATE_ECDT)
                        goto install;
-               saved_ec = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
+               saved_ec = kmemdup(boot_ec, sizeof(struct acpi_ec), GFP_KERNEL);
                if (!saved_ec)
                        return -ENOMEM;
-               memcpy(saved_ec, boot_ec, sizeof(struct acpi_ec));
        /* fall through */
        }
 
@@ -1054,22 +1025,6 @@ error:
        return -ENODEV;
 }
 
-static int acpi_ec_suspend(struct acpi_device *device, pm_message_t state)
-{
-       struct acpi_ec *ec = acpi_driver_data(device);
-       /* Stop using GPE */
-       acpi_disable_gpe(NULL, ec->gpe);
-       return 0;
-}
-
-static int acpi_ec_resume(struct acpi_device *device)
-{
-       struct acpi_ec *ec = acpi_driver_data(device);
-       /* Enable use of GPE back */
-       acpi_enable_gpe(NULL, ec->gpe);
-       return 0;
-}
-
 static struct acpi_driver acpi_ec_driver = {
        .name = "ec",
        .class = ACPI_EC_CLASS,
@@ -1077,8 +1032,6 @@ static struct acpi_driver acpi_ec_driver = {
        .ops = {
                .add = acpi_ec_add,
                .remove = acpi_ec_remove,
-               .suspend = acpi_ec_suspend,
-               .resume = acpi_ec_resume,
                },
 };
 
@@ -1086,16 +1039,10 @@ int __init acpi_ec_init(void)
 {
        int result = 0;
 
-       acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
-       if (!acpi_ec_dir)
-               return -ENODEV;
-
        /* Now register the driver for the EC */
        result = acpi_bus_register_driver(&acpi_ec_driver);
-       if (result < 0) {
-               remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
+       if (result < 0)
                return -ENODEV;
-       }
 
        return result;
 }
@@ -1106,9 +1053,6 @@ static void __exit acpi_ec_exit(void)
 {
 
        acpi_bus_unregister_driver(&acpi_ec_driver);
-
-       remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
-
        return;
 }
 #endif /* 0 */