ACPI: dock: cleanup the uid patch
[safe/jmp/linux-2.6] / drivers / acpi / ec.c
index 399cedf..e08cf98 100644 (file)
@@ -1,6 +1,8 @@
 /*
- *  acpi_ec.c - ACPI Embedded Controller Driver ($Revision: 38 $)
+ *  ec.c - ACPI Embedded Controller Driver (v2.0)
  *
+ *  Copyright (C) 2006, 2007 Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>
+ *  Copyright (C) 2006 Denis Sadykov <denis.m.sadykov@intel.com>
  *  Copyright (C) 2004 Luming Yu <luming.yu@intel.com>
  *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
  *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
@@ -91,9 +93,9 @@ static struct acpi_driver acpi_ec_driver = {
 };
 
 /* 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 */
 static struct acpi_ec {
        acpi_handle handle;
-       unsigned long uid;
        unsigned long gpe;
        unsigned long command_addr;
        unsigned long data_addr;
@@ -102,10 +104,7 @@ static struct acpi_ec {
        atomic_t query_pending;
        atomic_t event_count;
        wait_queue_head_t wait;
-} *ec_ecdt;
-
-/* External interfaces use first EC only, so remember */
-static struct acpi_device *first_ec;
+} *boot_ec, *first_ec;
 
 /* --------------------------------------------------------------------------
                              Transaction Management
@@ -299,38 +298,31 @@ static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
  */
 int ec_burst_enable(void)
 {
-       struct acpi_ec *ec;
        if (!first_ec)
                return -ENODEV;
-       ec = acpi_driver_data(first_ec);
-       return acpi_ec_burst_enable(ec);
+       return acpi_ec_burst_enable(first_ec);
 }
 
 EXPORT_SYMBOL(ec_burst_enable);
 
 int ec_burst_disable(void)
 {
-       struct acpi_ec *ec;
        if (!first_ec)
                return -ENODEV;
-       ec = acpi_driver_data(first_ec);
-       return acpi_ec_burst_disable(ec);
+       return acpi_ec_burst_disable(first_ec);
 }
 
 EXPORT_SYMBOL(ec_burst_disable);
 
 int ec_read(u8 addr, u8 * val)
 {
-       struct acpi_ec *ec;
        int err;
        u8 temp_data;
 
        if (!first_ec)
                return -ENODEV;
 
-       ec = acpi_driver_data(first_ec);
-
-       err = acpi_ec_read(ec, addr, &temp_data);
+       err = acpi_ec_read(first_ec, addr, &temp_data);
 
        if (!err) {
                *val = temp_data;
@@ -343,15 +335,12 @@ EXPORT_SYMBOL(ec_read);
 
 int ec_write(u8 addr, u8 val)
 {
-       struct acpi_ec *ec;
        int err;
 
        if (!first_ec)
                return -ENODEV;
 
-       ec = acpi_driver_data(first_ec);
-
-       err = acpi_ec_write(ec, addr, val);
+       err = acpi_ec_write(first_ec, addr, val);
 
        return err;
 }
@@ -362,14 +351,10 @@ int ec_transaction(u8 command,
                   const u8 * wdata, unsigned wdata_len,
                   u8 * rdata, unsigned rdata_len)
 {
-       struct acpi_ec *ec;
-
        if (!first_ec)
                return -ENODEV;
 
-       ec = acpi_driver_data(first_ec);
-
-       return acpi_ec_transaction(ec, command, wdata,
+       return acpi_ec_transaction(first_ec, command, wdata,
                                   wdata_len, rdata, rdata_len);
 }
 
@@ -541,13 +526,11 @@ static int acpi_ec_read_info(struct seq_file *seq, void *offset)
        if (!ec)
                goto end;
 
-       seq_printf(seq, "gpe:                 0x%02x\n", (u32) ec->gpe);
-       seq_printf(seq, "ports:                   0x%02x, 0x%02x\n",
-                  (u32) ec->command_addr, (u32) ec->data_addr);
-       seq_printf(seq, "use global lock:         %s\n",
+       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");
-       acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
-
       end:
        return 0;
 }
@@ -604,124 +587,122 @@ static int acpi_ec_remove_fs(struct acpi_device *device)
 /* --------------------------------------------------------------------------
                                Driver Interface
    -------------------------------------------------------------------------- */
+static acpi_status
+ec_parse_io_ports(struct acpi_resource *resource, void *context);
+
+static acpi_status
+ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval);
+
+static struct acpi_ec *make_acpi_ec(void)
+{
+       struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
+       if (!ec)
+               return NULL;
+
+       atomic_set(&ec->query_pending, 1);
+       atomic_set(&ec->event_count, 1);
+       mutex_init(&ec->lock);
+       init_waitqueue_head(&ec->wait);
+
+       return ec;
+}
 
 static int acpi_ec_add(struct acpi_device *device)
 {
-       int result = 0;
        acpi_status status = AE_OK;
        struct acpi_ec *ec = NULL;
 
        if (!device)
                return -EINVAL;
 
-       ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
-       if (!ec)
-               return -ENOMEM;
-
-       ec->handle = device->handle;
-       ec->uid = -1;
-       mutex_init(&ec->lock);
-       atomic_set(&ec->query_pending, 0);
-       atomic_set(&ec->event_count, 1);
-       if (acpi_ec_mode == EC_INTR) {
-               init_waitqueue_head(&ec->wait);
-       }
        strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
        strcpy(acpi_device_class(device), ACPI_EC_CLASS);
-       acpi_driver_data(device) = ec;
-
-       /* Use the global lock for all EC transactions? */
-       acpi_evaluate_integer(ec->handle, "_GLK", NULL, &ec->global_lock);
-
-       /* XXX we don't test uids, because on some boxes ecdt uid = 0, see:
-          http://bugzilla.kernel.org/show_bug.cgi?id=6111 */
-       if (ec_ecdt) {
-               acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
-                                                 ACPI_ADR_SPACE_EC,
-                                                 &acpi_ec_space_handler);
 
-               acpi_remove_gpe_handler(NULL, ec_ecdt->gpe,
-                                       &acpi_ec_gpe_handler);
+       ec = make_acpi_ec();
+       if (!ec)
+               return -ENOMEM;
 
-               kfree(ec_ecdt);
+       status = ec_parse_device(device->handle, 0, ec, NULL);
+       if (status != AE_CTRL_TERMINATE) {
+               kfree(ec);
+               return -EINVAL;
        }
 
-       /* Get GPE bit assignment (EC events). */
-       /* TODO: Add support for _GPE returning a package */
-       status = acpi_evaluate_integer(ec->handle, "_GPE", NULL, &ec->gpe);
-       if (ACPI_FAILURE(status)) {
-               ACPI_EXCEPTION((AE_INFO, status,
-                               "Obtaining GPE bit assignment"));
-               result = -ENODEV;
-               goto end;
-       }
+       /* Check if we found the boot EC */
+       if (boot_ec) {
+               if (boot_ec->gpe == ec->gpe) {
+                       /* We might have incorrect info for GL at boot time */
+                       mutex_lock(&boot_ec->lock);
+                       boot_ec->global_lock = ec->global_lock;
+                       mutex_unlock(&boot_ec->lock);
+                       kfree(ec);
+                       ec = boot_ec;
+               }
+       } else
+               first_ec = ec;
+       ec->handle = device->handle;
+       acpi_driver_data(device) = ec;
 
-       result = acpi_ec_add_fs(device);
-       if (result)
-               goto end;
+       acpi_ec_add_fs(device);
 
        ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s [%s] (gpe %d) interrupt mode.",
                          acpi_device_name(device), acpi_device_bid(device),
                          (u32) ec->gpe));
 
-       if (!first_ec)
-               first_ec = device;
-
-      end:
-       if (result)
-               kfree(ec);
-
-       return result;
+       return 0;
 }
 
 static int acpi_ec_remove(struct acpi_device *device, int type)
 {
-       struct acpi_ec *ec = NULL;
+       struct acpi_ec *ec;
 
        if (!device)
                return -EINVAL;
 
        ec = acpi_driver_data(device);
-
        acpi_ec_remove_fs(device);
+       acpi_driver_data(device) = NULL;
+       if (ec == first_ec)
+               first_ec = NULL;
 
-       kfree(ec);
-
+       /* Don't touch boot EC */
+       if (boot_ec != ec)
+               kfree(ec);
        return 0;
 }
 
 static acpi_status
-acpi_ec_io_ports(struct acpi_resource *resource, void *context)
+ec_parse_io_ports(struct acpi_resource *resource, void *context)
 {
        struct acpi_ec *ec = context;
 
-       if (resource->type != ACPI_RESOURCE_TYPE_IO) {
+       if (resource->type != ACPI_RESOURCE_TYPE_IO)
                return AE_OK;
-       }
 
        /*
         * The first address region returned is the data port, and
         * the second address region returned is the status/command
         * port.
         */
-       if (ec->data_addr == 0) {
+       if (ec->data_addr == 0)
                ec->data_addr = resource->data.io.minimum;
-       } else if (ec->command_addr == 0) {
+       else if (ec->command_addr == 0)
                ec->command_addr = resource->data.io.minimum;
-       } else {
+       else
                return AE_CTRL_TERMINATE;
-       }
 
        return AE_OK;
 }
 
 static int ec_install_handlers(struct acpi_ec *ec)
 {
-       acpi_status status = acpi_install_gpe_handler(NULL, ec->gpe,
-                                                     ACPI_GPE_EDGE_TRIGGERED,
-                                                     &acpi_ec_gpe_handler, ec);
+       acpi_status status;
+       status = acpi_install_gpe_handler(NULL, ec->gpe,
+                                         ACPI_GPE_EDGE_TRIGGERED,
+                                         &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, ACPI_NOT_ISR);
 
@@ -734,13 +715,15 @@ static int ec_install_handlers(struct acpi_ec *ec)
                return -ENODEV;
        }
 
+       /* EC is fully operational, allow queries */
+       atomic_set(&ec->query_pending, 0);
+
        return 0;
 }
 
 static int acpi_ec_start(struct acpi_device *device)
 {
-       acpi_status status = AE_OK;
-       struct acpi_ec *ec = NULL;
+       struct acpi_ec *ec;
 
        if (!device)
                return -EINVAL;
@@ -750,32 +733,31 @@ static int acpi_ec_start(struct acpi_device *device)
        if (!ec)
                return -EINVAL;
 
-       /*
-        * Get I/O port addresses. Convert to GAS format.
-        */
-       status = acpi_walk_resources(ec->handle, METHOD_NAME__CRS,
-                                    acpi_ec_io_ports, ec);
-       if (ACPI_FAILURE(status) || ec->command_addr == 0) {
-               ACPI_EXCEPTION((AE_INFO, status,
-                               "Error getting I/O port addresses"));
-               return -ENODEV;
-       }
-
        ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02lx, ports=0x%2lx,0x%2lx",
                          ec->gpe, ec->command_addr, ec->data_addr));
 
+       /* Boot EC is already working */
+       if (ec == boot_ec)
+               return 0;
+
        return ec_install_handlers(ec);
 }
 
 static int acpi_ec_stop(struct acpi_device *device, int type)
 {
-       acpi_status status = AE_OK;
-       struct acpi_ec *ec = NULL;
+       acpi_status status;
+       struct acpi_ec *ec;
 
        if (!device)
                return -EINVAL;
 
        ec = acpi_driver_data(device);
+       if (!ec)
+               return -EINVAL;
+
+       /* Don't touch boot EC */
+       if (ec == boot_ec)
+               return 0;
 
        status = acpi_remove_address_space_handler(ec->handle,
                                                   ACPI_ADR_SPACE_EC,
@@ -790,53 +772,67 @@ static int acpi_ec_stop(struct acpi_device *device, int type)
        return 0;
 }
 
-static int __init acpi_ec_get_real_ecdt(void)
+static acpi_status
+ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
 {
        acpi_status status;
-       struct acpi_table_ecdt *ecdt_ptr;
 
-       status = acpi_get_table(ACPI_SIG_ECDT, 1,
-                               (struct acpi_table_header **)&ecdt_ptr);
+       struct acpi_ec *ec = context;
+       status = acpi_walk_resources(handle, METHOD_NAME__CRS,
+                                    ec_parse_io_ports, ec);
        if (ACPI_FAILURE(status))
-               return -ENODEV;
+               return status;
 
-       ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found ECDT"));
+       /* Get GPE bit assignment (EC events). */
+       /* TODO: Add support for _GPE returning a package */
+       status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe);
+       if (ACPI_FAILURE(status))
+               return status;
 
-       /*
-        * Generate a temporary ec context to use until the namespace is scanned
-        */
-       ec_ecdt = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
-       if (!ec_ecdt)
-               return -ENOMEM;
+       /* Use the global lock for all EC transactions? */
+       acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock);
 
-       mutex_init(&ec_ecdt->lock);
-       atomic_set(&ec_ecdt->event_count, 1);
-       if (acpi_ec_mode == EC_INTR) {
-               init_waitqueue_head(&ec_ecdt->wait);
-       }
-       ec_ecdt->command_addr = ecdt_ptr->control.address;
-       ec_ecdt->data_addr = ecdt_ptr->data.address;
-       ec_ecdt->gpe = ecdt_ptr->gpe;
-       ec_ecdt->uid = ecdt_ptr->uid;
+       ec->handle = handle;
 
-       ec_ecdt->handle = ACPI_ROOT_OBJECT;
-       return 0;
+       ACPI_DEBUG_PRINT((ACPI_DB_INFO, "GPE=0x%02lx, ports=0x%2lx, 0x%2lx",
+                         ec->gpe, ec->command_addr, ec->data_addr));
+
+       return AE_CTRL_TERMINATE;
 }
 
 int __init acpi_ec_ecdt_probe(void)
 {
        int ret;
+       acpi_status status;
+       struct acpi_table_ecdt *ecdt_ptr;
 
-       ret = acpi_ec_get_real_ecdt();
-       if (ret)
-               return 0;
+       boot_ec = make_acpi_ec();
+       if (!boot_ec)
+               return -ENOMEM;
+       /*
+        * Generate a boot ec context
+        */
 
-       ret = ec_install_handlers(ec_ecdt);
-       if (!ret)
-               return 0;
+       status = acpi_get_table(ACPI_SIG_ECDT, 1,
+                               (struct acpi_table_header **)&ecdt_ptr);
+       if (ACPI_FAILURE(status))
+               goto error;
 
-       kfree(ec_ecdt);
-       ec_ecdt = NULL;
+       ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found ECDT"));
+
+       boot_ec->command_addr = ecdt_ptr->control.address;
+       boot_ec->data_addr = ecdt_ptr->data.address;
+       boot_ec->gpe = ecdt_ptr->gpe;
+       boot_ec->handle = ACPI_ROOT_OBJECT;
+
+       ret = ec_install_handlers(boot_ec);
+       if (!ret) {
+               first_ec = boot_ec;
+               return 0;
+       }
+      error:
+       kfree(boot_ec);
+       boot_ec = NULL;
 
        return -ENODEV;
 }
@@ -884,12 +880,8 @@ static int __init acpi_ec_set_intr_mode(char *str)
        if (!get_option(&str, &intr))
                return 0;
 
-       if (intr) {
-               acpi_ec_mode = EC_INTR;
-       } else {
-               acpi_ec_mode = EC_POLL;
-       }
-       acpi_ec_driver.ops.add = acpi_ec_add;
+       acpi_ec_mode = (intr) ? EC_INTR : EC_POLL;
+
        printk(KERN_NOTICE PREFIX "%s mode.\n", intr ? "interrupt" : "polling");
 
        return 1;