ACPI / power: Avoid maybe-uninitialized warning
[pandora-kernel.git] / drivers / acpi / ec.c
index 8176b82..30229af 100644 (file)
@@ -70,6 +70,8 @@ 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_MSI_UDELAY     550     /* Wait 550us for MSI EC */
+#define ACPI_EC_CLEAR_MAX      100     /* Maximum number of events to query
+                                        * when trying to clear the EC */
 
 enum {
        EC_FLAGS_QUERY_PENDING,         /* Query is pending */
@@ -79,6 +81,9 @@ enum {
        EC_FLAGS_BLOCKED,               /* Transactions are blocked */
 };
 
+#define ACPI_EC_COMMAND_POLL           0x01 /* Available for command byte */
+#define ACPI_EC_COMMAND_COMPLETE       0x02 /* Completed last byte */
+
 /* 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);
@@ -114,7 +119,7 @@ struct transaction {
        u8 ri;
        u8 wlen;
        u8 rlen;
-       bool done;
+       u8 flags;
 };
 
 struct acpi_ec *boot_ec, *first_ec;
@@ -123,6 +128,8 @@ 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 */
 static int EC_FLAGS_SKIP_DSDT_SCAN; /* Not all BIOS survive early DSDT scan */
+static int EC_FLAGS_CLEAR_ON_RESUME; /* Needs acpi_ec_clear() on boot/resume */
+static int EC_FLAGS_QUERY_HANDSHAKE; /* Needs QR_EC issued when SCI_EVT set */
 
 /* --------------------------------------------------------------------------
                              Transaction Management
@@ -154,62 +161,93 @@ static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
        outb(data, ec->data_addr);
 }
 
-static int ec_transaction_done(struct acpi_ec *ec)
+static int ec_transaction_completed(struct acpi_ec *ec)
 {
        unsigned long flags;
        int ret = 0;
        spin_lock_irqsave(&ec->curr_lock, flags);
-       if (!ec->curr || ec->curr->done)
+       if (ec->curr && (ec->curr->flags & ACPI_EC_COMMAND_COMPLETE))
                ret = 1;
        spin_unlock_irqrestore(&ec->curr_lock, flags);
        return ret;
 }
 
-static void start_transaction(struct acpi_ec *ec)
+static bool advance_transaction(struct acpi_ec *ec)
 {
-       ec->curr->irq_count = ec->curr->wi = ec->curr->ri = 0;
-       ec->curr->done = false;
-       acpi_ec_write_cmd(ec, ec->curr->command);
-}
-
-static void advance_transaction(struct acpi_ec *ec, u8 status)
-{
-       unsigned long flags;
-       spin_lock_irqsave(&ec->curr_lock, flags);
-       if (!ec->curr)
-               goto unlock;
-       if (ec->curr->wlen > ec->curr->wi) {
-               if ((status & ACPI_EC_FLAG_IBF) == 0)
-                       acpi_ec_write_data(ec,
-                               ec->curr->wdata[ec->curr->wi++]);
-               else
-                       goto err;
-       } else if (ec->curr->rlen > ec->curr->ri) {
-               if ((status & ACPI_EC_FLAG_OBF) == 1) {
-                       ec->curr->rdata[ec->curr->ri++] = acpi_ec_read_data(ec);
-                       if (ec->curr->rlen == ec->curr->ri)
-                               ec->curr->done = true;
+       struct transaction *t;
+       u8 status;
+       bool wakeup = false;
+
+       pr_debug(PREFIX "===== %s =====\n", in_interrupt() ? "IRQ" : "TASK");
+       status = acpi_ec_read_status(ec);
+       t = ec->curr;
+       if (!t)
+               goto err;
+       if (t->flags & ACPI_EC_COMMAND_POLL) {
+               if (t->wlen > t->wi) {
+                       if ((status & ACPI_EC_FLAG_IBF) == 0)
+                               acpi_ec_write_data(ec, t->wdata[t->wi++]);
+                       else
+                               goto err;
+               } else if (t->rlen > t->ri) {
+                       if ((status & ACPI_EC_FLAG_OBF) == 1) {
+                               t->rdata[t->ri++] = acpi_ec_read_data(ec);
+                               if (t->rlen == t->ri) {
+                                       t->flags |= ACPI_EC_COMMAND_COMPLETE;
+                                       if (t->command == ACPI_EC_COMMAND_QUERY)
+                                               pr_debug("hardware QR_EC completion\n");
+                                       wakeup = true;
+                               }
+                       } else
+                               goto err;
+               } else if (t->wlen == t->wi &&
+                          (status & ACPI_EC_FLAG_IBF) == 0) {
+                       t->flags |= ACPI_EC_COMMAND_COMPLETE;
+                       wakeup = true;
+               }
+               return wakeup;
+       } else {
+               if (EC_FLAGS_QUERY_HANDSHAKE &&
+                   !(status & ACPI_EC_FLAG_SCI) &&
+                   (t->command == ACPI_EC_COMMAND_QUERY)) {
+                       t->flags |= ACPI_EC_COMMAND_POLL;
+                       t->rdata[t->ri++] = 0x00;
+                       t->flags |= ACPI_EC_COMMAND_COMPLETE;
+                       pr_debug("software QR_EC completion\n");
+                       wakeup = true;
+               } else if ((status & ACPI_EC_FLAG_IBF) == 0) {
+                       acpi_ec_write_cmd(ec, t->command);
+                       t->flags |= ACPI_EC_COMMAND_POLL;
                } else
                        goto err;
-       } else if (ec->curr->wlen == ec->curr->wi &&
-                  (status & ACPI_EC_FLAG_IBF) == 0)
-               ec->curr->done = true;
-       goto unlock;
+               return wakeup;
+       }
 err:
-       /* false interrupt, state didn't change */
-       if (in_interrupt())
-               ++ec->curr->irq_count;
-unlock:
-       spin_unlock_irqrestore(&ec->curr_lock, flags);
+       /*
+        * If SCI bit is set, then don't think it's a false IRQ
+        * otherwise will take a not handled IRQ as a false one.
+        */
+       if (!(status & ACPI_EC_FLAG_SCI)) {
+               if (in_interrupt() && t)
+                       ++t->irq_count;
+       }
+       return wakeup;
 }
 
-static int acpi_ec_sync_query(struct acpi_ec *ec);
+static void start_transaction(struct acpi_ec *ec)
+{
+       ec->curr->irq_count = ec->curr->wi = ec->curr->ri = 0;
+       ec->curr->flags = 0;
+       (void)advance_transaction(ec);
+}
+
+static int acpi_ec_sync_query(struct acpi_ec *ec, u8 *data);
 
 static int ec_check_sci_sync(struct acpi_ec *ec, u8 state)
 {
        if (state & ACPI_EC_FLAG_SCI) {
                if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags))
-                       return acpi_ec_sync_query(ec);
+                       return acpi_ec_sync_query(ec, NULL);
        }
        return 0;
 }
@@ -225,15 +263,17 @@ static int ec_poll(struct acpi_ec *ec)
                        /* don't sleep with disabled interrupts */
                        if (EC_FLAGS_MSI || irqs_disabled()) {
                                udelay(ACPI_EC_MSI_UDELAY);
-                               if (ec_transaction_done(ec))
+                               if (ec_transaction_completed(ec))
                                        return 0;
                        } else {
                                if (wait_event_timeout(ec->wait,
-                                               ec_transaction_done(ec),
+                                               ec_transaction_completed(ec),
                                                msecs_to_jiffies(1)))
                                        return 0;
                        }
-                       advance_transaction(ec, acpi_ec_read_status(ec));
+                       spin_lock_irqsave(&ec->curr_lock, flags);
+                       (void)advance_transaction(ec);
+                       spin_unlock_irqrestore(&ec->curr_lock, flags);
                } while (time_before(jiffies, delay));
                pr_debug(PREFIX "controller reset, restart transaction\n");
                spin_lock_irqsave(&ec->curr_lock, flags);
@@ -265,23 +305,6 @@ static int acpi_ec_transaction_unlocked(struct acpi_ec *ec,
        return ret;
 }
 
-static int ec_check_ibf0(struct acpi_ec *ec)
-{
-       u8 status = acpi_ec_read_status(ec);
-       return (status & ACPI_EC_FLAG_IBF) == 0;
-}
-
-static int ec_wait_ibf0(struct acpi_ec *ec)
-{
-       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),
-                                       msecs_to_jiffies(1)))
-                       return 0;
-       return -ETIME;
-}
-
 static int acpi_ec_transaction(struct acpi_ec *ec, struct transaction *t)
 {
        int status;
@@ -302,13 +325,8 @@ static int acpi_ec_transaction(struct acpi_ec *ec, struct transaction *t)
                        goto unlock;
                }
        }
-       if (ec_wait_ibf0(ec)) {
-               pr_err(PREFIX "input buffer is not empty, "
-                               "aborting transaction\n");
-               status = -ETIME;
-               goto end;
-       }
-       pr_debug(PREFIX "transaction start\n");
+       pr_debug(PREFIX "transaction start (cmd=0x%02x, addr=0x%02x)\n",
+                       t->command, t->wdata ? t->wdata[0] : 0);
        /* 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. */
@@ -324,12 +342,12 @@ static int acpi_ec_transaction(struct acpi_ec *ec, struct transaction *t)
                /* It is safe to enable the GPE outside of the transaction. */
                acpi_enable_gpe(NULL, ec->gpe);
        } else if (t->irq_count > ec_storm_threshold) {
-               pr_info(PREFIX "GPE storm detected, "
-                       "transactions will use polling mode\n");
+               pr_info(PREFIX "GPE storm detected(%d GPEs), "
+                       "transactions will use polling mode\n",
+                       t->irq_count);
                set_bit(EC_FLAGS_GPE_STORM, &ec->flags);
        }
        pr_debug(PREFIX "transaction end\n");
-end:
        if (ec->global_lock)
                acpi_release_global_lock(glk);
 unlock:
@@ -401,7 +419,7 @@ int ec_burst_disable(void)
 
 EXPORT_SYMBOL(ec_burst_disable);
 
-int ec_read(u8 addr, u8 * val)
+int ec_read(u8 addr, u8 *val)
 {
        int err;
        u8 temp_data;
@@ -449,6 +467,27 @@ int ec_transaction(u8 command,
 
 EXPORT_SYMBOL(ec_transaction);
 
+/*
+ * Process _Q events that might have accumulated in the EC.
+ * Run with locked ec mutex.
+ */
+static void acpi_ec_clear(struct acpi_ec *ec)
+{
+       int i, status;
+       u8 value = 0;
+
+       for (i = 0; i < ACPI_EC_CLEAR_MAX; i++) {
+               status = acpi_ec_sync_query(ec, &value);
+               if (status || !value)
+                       break;
+       }
+
+       if (unlikely(i == ACPI_EC_CLEAR_MAX))
+               pr_warn("Warning: Maximum of %d stale EC events cleared\n", i);
+       else
+               pr_info("%d stale EC events cleared\n", i);
+}
+
 void acpi_ec_block_transactions(void)
 {
        struct acpi_ec *ec = first_ec;
@@ -472,6 +511,10 @@ void acpi_ec_unblock_transactions(void)
        mutex_lock(&ec->lock);
        /* Allow transactions to be carried out again */
        clear_bit(EC_FLAGS_BLOCKED, &ec->flags);
+
+       if (EC_FLAGS_CLEAR_ON_RESUME)
+               acpi_ec_clear(ec);
+
        mutex_unlock(&ec->lock);
 }
 
@@ -561,13 +604,18 @@ static void acpi_ec_run(void *cxt)
        kfree(handler);
 }
 
-static int acpi_ec_sync_query(struct acpi_ec *ec)
+static int acpi_ec_sync_query(struct acpi_ec *ec, u8 *data)
 {
        u8 value = 0;
        int status;
        struct acpi_ec_query_handler *handler, *copy;
-       if ((status = acpi_ec_query_unlocked(ec, &value)))
+
+       status = acpi_ec_query_unlocked(ec, &value);
+       if (data)
+               *data = value;
+       if (status)
                return status;
+
        list_for_each_entry(handler, &ec->list, node) {
                if (value == handler->query_bit) {
                        /* have custom handler for this bit */
@@ -590,7 +638,7 @@ static void acpi_ec_gpe_query(void *ec_cxt)
        if (!ec)
                return;
        mutex_lock(&ec->lock);
-       acpi_ec_sync_query(ec);
+       acpi_ec_sync_query(ec, NULL);
        mutex_unlock(&ec->lock);
 }
 
@@ -609,16 +657,14 @@ static int ec_check_sci(struct acpi_ec *ec, u8 state)
 static u32 acpi_ec_gpe_handler(acpi_handle gpe_device,
        u32 gpe_number, void *data)
 {
+       unsigned long flags;
        struct acpi_ec *ec = data;
 
-       pr_debug(PREFIX "~~~> interrupt\n");
-
-       advance_transaction(ec, acpi_ec_read_status(ec));
-       if (ec_transaction_done(ec) &&
-           (acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF) == 0) {
+       spin_lock_irqsave(&ec->curr_lock, flags);
+       if (advance_transaction(ec))
                wake_up(&ec->wait);
-               ec_check_sci(ec, acpi_ec_read_status(ec));
-       }
+       spin_unlock_irqrestore(&ec->curr_lock, flags);
+       ec_check_sci(ec, acpi_ec_read_status(ec));
        return ACPI_INTERRUPT_HANDLED | ACPI_REENABLE_GPE;
 }
 
@@ -828,6 +874,13 @@ static int acpi_ec_add(struct acpi_device *device)
 
        /* EC is fully operational, allow queries */
        clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
+
+       /* Clear stale _Q events if hardware might require that */
+       if (EC_FLAGS_CLEAR_ON_RESUME) {
+               mutex_lock(&ec->lock);
+               acpi_ec_clear(ec);
+               mutex_unlock(&ec->lock);
+       }
        return ret;
 }
 
@@ -929,6 +982,42 @@ static int ec_enlarge_storm_threshold(const struct dmi_system_id *id)
        return 0;
 }
 
+/*
+ * Acer EC firmware refuses to respond QR_EC when SCI_EVT is not set, for
+ * which case, we complete the QR_EC without issuing it to the firmware.
+ * https://bugzilla.kernel.org/show_bug.cgi?id=86211
+ */
+static int ec_flag_query_handshake(const struct dmi_system_id *id)
+{
+       pr_debug("Detected the EC firmware requiring QR_EC issued when SCI_EVT set\n");
+       EC_FLAGS_QUERY_HANDSHAKE = 1;
+       return 0;
+}
+
+/*
+ * On some hardware it is necessary to clear events accumulated by the EC during
+ * sleep. These ECs stop reporting GPEs until they are manually polled, if too
+ * many events are accumulated. (e.g. Samsung Series 5/9 notebooks)
+ *
+ * https://bugzilla.kernel.org/show_bug.cgi?id=44161
+ *
+ * Ideally, the EC should also be instructed NOT to accumulate events during
+ * sleep (which Windows seems to do somehow), but the interface to control this
+ * behaviour is not known at this time.
+ *
+ * Models known to be affected are Samsung 530Uxx/535Uxx/540Uxx/550Pxx/900Xxx,
+ * however it is very likely that other Samsung models are affected.
+ *
+ * On systems which don't accumulate _Q events during sleep, this extra check
+ * should be harmless.
+ */
+static int ec_clear_on_resume(const struct dmi_system_id *id)
+{
+       pr_debug("Detected system needing EC poll on resume.\n");
+       EC_FLAGS_CLEAR_ON_RESUME = 1;
+       return 0;
+}
+
 static struct dmi_system_id __initdata ec_dmi_table[] = {
        {
        ec_skip_dsdt_scan, "Compal JFL92", {
@@ -968,6 +1057,12 @@ static struct dmi_system_id __initdata ec_dmi_table[] = {
        ec_validate_ecdt, "ASUS hardware", {
        DMI_MATCH(DMI_SYS_VENDOR, "ASUSTek Computer Inc."),
        DMI_MATCH(DMI_PRODUCT_NAME, "L4R"),}, NULL},
+       {
+       ec_clear_on_resume, "Samsung hardware", {
+       DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD.")}, NULL},
+       {
+       ec_flag_query_handshake, "Acer hardware", {
+       DMI_MATCH(DMI_SYS_VENDOR, "Acer"), }, NULL},
        {},
 };