Merge branch 'ec' into release
authorLen Brown <len.brown@intel.com>
Sat, 19 Sep 2009 06:05:34 +0000 (02:05 -0400)
committerLen Brown <len.brown@intel.com>
Sat, 19 Sep 2009 06:05:34 +0000 (02:05 -0400)
Conflicts:
drivers/acpi/ec.c

Signed-off-by: Len Brown <len.brown@intel.com>
1  2 
drivers/acpi/ec.c

diff --combined drivers/acpi/ec.c
@@@ -47,6 -47,7 +47,6 @@@
  #define ACPI_EC_DEVICE_NAME           "Embedded Controller"
  #define ACPI_EC_FILE_INFO             "info"
  
 -#undef PREFIX
  #define PREFIX                                "ACPI: EC: "
  
  /* EC status register */
@@@ -67,15 -68,13 +67,13 @@@ 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
                                           per one transaction */
  
  enum {
        EC_FLAGS_QUERY_PENDING,         /* Query is pending */
-       EC_FLAGS_GPE_MODE,              /* Expect GPE to be sent
-                                        * for status change */
-       EC_FLAGS_NO_GPE,                /* Don't use GPE mode */
        EC_FLAGS_GPE_STORM,             /* GPE storm detected */
        EC_FLAGS_HANDLERS_INSTALLED     /* Handlers for GPE and
                                         * OpReg are installed */
@@@ -169,7 -168,7 +167,7 @@@ static void start_transaction(struct ac
        acpi_ec_write_cmd(ec, ec->curr->command);
  }
  
- static void gpe_transaction(struct acpi_ec *ec, u8 status)
+ static void advance_transaction(struct acpi_ec *ec, u8 status)
  {
        unsigned long flags;
        spin_lock_irqsave(&ec->curr_lock, flags);
@@@ -200,29 -199,6 +198,6 @@@ unlock
        spin_unlock_irqrestore(&ec->curr_lock, flags);
  }
  
- static int acpi_ec_wait(struct acpi_ec *ec)
- {
-       if (wait_event_timeout(ec->wait, ec_transaction_done(ec),
-                              msecs_to_jiffies(ACPI_EC_DELAY)))
-               return 0;
-       /* try restart command if we get any false interrupts */
-       if (ec->curr->irq_count &&
-           (acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF) == 0) {
-               pr_debug(PREFIX "controller reset, restart transaction\n");
-               start_transaction(ec);
-               if (wait_event_timeout(ec->wait, ec_transaction_done(ec),
-                                       msecs_to_jiffies(ACPI_EC_DELAY)))
-                       return 0;
-       }
-       /* missing GPEs, switch back to poll mode */
-       if (printk_ratelimit())
-               pr_info(PREFIX "missing confirmations, "
-                               "switch off interrupt mode.\n");
-       set_bit(EC_FLAGS_NO_GPE, &ec->flags);
-       clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
-       return 1;
- }
  static void acpi_ec_gpe_query(void *ec_cxt);
  
  static int ec_check_sci(struct acpi_ec *ec, u8 state)
        return 0;
  }
  
- static void ec_delay(void)
- {
-       /* EC in MSI notebooks don't tolerate delays other than 550 usec */
-       if (EC_FLAGS_MSI)
-               udelay(ACPI_EC_DELAY);
-       else
-               /* Use shortest sleep available */
-               msleep(1);
- }
  static int ec_poll(struct acpi_ec *ec)
  {
-       unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
-       udelay(ACPI_EC_CDELAY);
-       while (time_before(jiffies, delay)) {
-               gpe_transaction(ec, acpi_ec_read_status(ec));
-               ec_delay();
-               if (ec_transaction_done(ec))
-                       return 0;
+       unsigned long flags;
+       int repeat = 2; /* number of command restarts */
+       while (repeat--) {
+               unsigned long delay = jiffies +
+                       msecs_to_jiffies(ACPI_EC_DELAY);
+               do {
+                       /* don't sleep with disabled interrupts */
+                       if (EC_FLAGS_MSI || irqs_disabled()) {
+                               udelay(ACPI_EC_MSI_UDELAY);
+                               if (ec_transaction_done(ec))
+                                       return 0;
+                       } else {
+                               if (wait_event_timeout(ec->wait,
+                                               ec_transaction_done(ec),
+                                               msecs_to_jiffies(1)))
+                                       return 0;
+                       }
+                       advance_transaction(ec, acpi_ec_read_status(ec));
+               } while (time_before(jiffies, delay));
+               if (!ec->curr->irq_count ||
+                   (acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF))
+                       break;
+               /* try restart command if we get any false interrupts */
+               pr_debug(PREFIX "controller reset, restart transaction\n");
+               spin_lock_irqsave(&ec->curr_lock, flags);
+               start_transaction(ec);
+               spin_unlock_irqrestore(&ec->curr_lock, flags);
        }
        return -ETIME;
  }
  
  static int acpi_ec_transaction_unlocked(struct acpi_ec *ec,
-                                       struct transaction *t,
-                                       int force_poll)
+                                       struct transaction *t)
  {
        unsigned long tmp;
        int ret = 0;
        pr_debug(PREFIX "transaction start\n");
        /* disable GPE during transaction if storm is detected */
        if (test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
-               clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
                acpi_disable_gpe(NULL, ec->gpe);
        }
        if (EC_FLAGS_MSI)
-               udelay(ACPI_EC_DELAY);
+               udelay(ACPI_EC_MSI_UDELAY);
        /* start transaction */
        spin_lock_irqsave(&ec->curr_lock, tmp);
        /* following two actions should be kept atomic */
        if (ec->curr->command == ACPI_EC_COMMAND_QUERY)
                clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
        spin_unlock_irqrestore(&ec->curr_lock, tmp);
-       /* if we selected poll mode or failed in GPE-mode do a poll loop */
-       if (force_poll ||
-           !test_bit(EC_FLAGS_GPE_MODE, &ec->flags) ||
-           acpi_ec_wait(ec))
-               ret = ec_poll(ec);
+       ret = ec_poll(ec);
        pr_debug(PREFIX "transaction end\n");
        spin_lock_irqsave(&ec->curr_lock, tmp);
        ec->curr = NULL;
                ec_check_sci(ec, acpi_ec_read_status(ec));
                /* it is safe to enable GPE outside of transaction */
                acpi_enable_gpe(NULL, ec->gpe);
-       } else if (test_bit(EC_FLAGS_GPE_MODE, &ec->flags) &&
-                  t->irq_count > ACPI_EC_STORM_THRESHOLD) {
+       } else if (t->irq_count > ACPI_EC_STORM_THRESHOLD) {
                pr_info(PREFIX "GPE storm detected, "
                        "transactions will use polling mode\n");
                set_bit(EC_FLAGS_GPE_STORM, &ec->flags);
@@@ -313,16 -292,14 +291,14 @@@ static int ec_wait_ibf0(struct acpi_ec 
  {
        unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
        /* interrupt wait manually if GPE mode is not active */
-       unsigned long timeout = test_bit(EC_FLAGS_GPE_MODE, &ec->flags) ?
-               msecs_to_jiffies(ACPI_EC_DELAY) : msecs_to_jiffies(1);
        while (time_before(jiffies, delay))
-               if (wait_event_timeout(ec->wait, ec_check_ibf0(ec), timeout))
+               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 force_poll)
+ static int acpi_ec_transaction(struct acpi_ec *ec, struct transaction *t)
  {
        int status;
        u32 glk;
                status = -ETIME;
                goto end;
        }
-       status = acpi_ec_transaction_unlocked(ec, t, force_poll);
+       status = acpi_ec_transaction_unlocked(ec, t);
  end:
        if (ec->global_lock)
                acpi_release_global_lock(glk);
@@@ -353,10 -330,6 +329,6 @@@ unlock
        return status;
  }
  
- /*
-  * Note: samsung nv5000 doesn't work with ec burst mode.
-  * http://bugzilla.kernel.org/show_bug.cgi?id=4980
-  */
  static int acpi_ec_burst_enable(struct acpi_ec *ec)
  {
        u8 d;
                                .wdata = NULL, .rdata = &d,
                                .wlen = 0, .rlen = 1};
  
-       return acpi_ec_transaction(ec, &t, 0);
+       return acpi_ec_transaction(ec, &t);
  }
  
  static int acpi_ec_burst_disable(struct acpi_ec *ec)
                                .wlen = 0, .rlen = 0};
  
        return (acpi_ec_read_status(ec) & ACPI_EC_FLAG_BURST) ?
-                               acpi_ec_transaction(ec, &t, 0) : 0;
+                               acpi_ec_transaction(ec, &t) : 0;
  }
  
  static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
                                .wdata = &address, .rdata = &d,
                                .wlen = 1, .rlen = 1};
  
-       result = acpi_ec_transaction(ec, &t, 0);
+       result = acpi_ec_transaction(ec, &t);
        *data = d;
        return result;
  }
@@@ -397,7 -370,7 +369,7 @@@ static int acpi_ec_write(struct acpi_e
                                .wdata = wdata, .rdata = NULL,
                                .wlen = 2, .rlen = 0};
  
-       return acpi_ec_transaction(ec, &t, 0);
+       return acpi_ec_transaction(ec, &t);
  }
  
  /*
@@@ -465,7 -438,7 +437,7 @@@ int ec_transaction(u8 command
        if (!first_ec)
                return -ENODEV;
  
-       return acpi_ec_transaction(first_ec, &t, force_poll);
+       return acpi_ec_transaction(first_ec, &t);
  }
  
  EXPORT_SYMBOL(ec_transaction);
@@@ -486,7 -459,7 +458,7 @@@ static int acpi_ec_query(struct acpi_e
         * bit to be cleared (and thus clearing the interrupt source).
         */
  
-       result = acpi_ec_transaction(ec, &t, 0);
+       result = acpi_ec_transaction(ec, &t);
        if (result)
                return result;
  
@@@ -569,28 -542,10 +541,10 @@@ static u32 acpi_ec_gpe_handler(void *da
        pr_debug(PREFIX "~~~> interrupt\n");
        status = acpi_ec_read_status(ec);
  
-       if (test_bit(EC_FLAGS_GPE_MODE, &ec->flags)) {
-               gpe_transaction(ec, status);
-               if (ec_transaction_done(ec) &&
-                   (status & ACPI_EC_FLAG_IBF) == 0)
-                       wake_up(&ec->wait);
-       }
+       advance_transaction(ec, status);
+       if (ec_transaction_done(ec) && (status & ACPI_EC_FLAG_IBF) == 0)
+               wake_up(&ec->wait);
        ec_check_sci(ec, status);
-       if (!test_bit(EC_FLAGS_GPE_MODE, &ec->flags) &&
-           !test_bit(EC_FLAGS_NO_GPE, &ec->flags)) {
-               /* this is non-query, must be confirmation */
-               if (!test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
-                       if (printk_ratelimit())
-                               pr_info(PREFIX "non-query interrupt received,"
-                                       " switching to interrupt mode\n");
-               } else {
-                       /* hush, STORM switches the mode every transaction */
-                       pr_debug(PREFIX "non-query interrupt received,"
-                               " switching to interrupt mode\n");
-               }
-               set_bit(EC_FLAGS_GPE_MODE, &ec->flags);
-       }
        return ACPI_INTERRUPT_HANDLED;
  }
  
@@@ -616,7 -571,8 +570,8 @@@ acpi_ec_space_handler(u32 function, acp
        if (bits != 8 && acpi_strict)
                return AE_BAD_PARAMETER;
  
-       acpi_ec_burst_enable(ec);
+       if (EC_FLAGS_MSI)
+               acpi_ec_burst_enable(ec);
  
        if (function == ACPI_READ) {
                result = acpi_ec_read(ec, address, &temp);
                }
        }
  
-       acpi_ec_burst_disable(ec);
+       if (EC_FLAGS_MSI)
+               acpi_ec_burst_disable(ec);
  
        switch (result) {
        case -EINVAL:
@@@ -787,42 -744,6 +743,42 @@@ ec_parse_device(acpi_handle handle, u3
        return AE_CTRL_TERMINATE;
  }
  
 +static int ec_install_handlers(struct acpi_ec *ec)
 +{
 +      acpi_status status;
 +      if (test_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags))
 +              return 0;
 +      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);
 +      status = acpi_install_address_space_handler(ec->handle,
 +                                                  ACPI_ADR_SPACE_EC,
 +                                                  &acpi_ec_space_handler,
 +                                                  NULL, ec);
 +      if (ACPI_FAILURE(status)) {
 +              if (status == AE_NOT_FOUND) {
 +                      /*
 +                       * Maybe OS fails in evaluating the _REG object.
 +                       * The AE_NOT_FOUND error will be ignored and OS
 +                       * continue to initialize EC.
 +                       */
 +                      printk(KERN_ERR "Fail in evaluating the _REG object"
 +                              " of EC device. Broken bios is suspected.\n");
 +              } else {
 +                      acpi_remove_gpe_handler(NULL, ec->gpe,
 +                              &acpi_ec_gpe_handler);
 +                      return -ENODEV;
 +              }
 +      }
 +
 +      set_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
 +      return 0;
 +}
 +
  static void ec_remove_handlers(struct acpi_ec *ec)
  {
        if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
  static int acpi_ec_add(struct acpi_device *device)
  {
        struct acpi_ec *ec = NULL;
 +      int ret;
  
 -      if (!device)
 -              return -EINVAL;
        strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
        strcpy(acpi_device_class(device), ACPI_EC_CLASS);
  
        acpi_ec_add_fs(device);
        pr_info(PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
                          ec->gpe, ec->command_addr, ec->data_addr);
-       pr_info(PREFIX "driver started in %s mode\n",
-               (test_bit(EC_FLAGS_GPE_MODE, &ec->flags))?"interrupt":"poll");
 -      return 0;
 +
 +      ret = ec_install_handlers(ec);
 +
 +      /* EC is fully operational, allow queries */
 +      clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
 +      return ret;
  }
  
  static int acpi_ec_remove(struct acpi_device *device, int type)
                return -EINVAL;
  
        ec = acpi_driver_data(device);
 +      ec_remove_handlers(ec);
        mutex_lock(&ec->lock);
        list_for_each_entry_safe(handler, tmp, &ec->list, node) {
                list_del(&handler->node);
@@@ -928,6 -842,75 +882,6 @@@ ec_parse_io_ports(struct acpi_resource 
        return AE_OK;
  }
  
 -static int ec_install_handlers(struct acpi_ec *ec)
 -{
 -      acpi_status status;
 -      if (test_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags))
 -              return 0;
 -      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);
 -      status = acpi_install_address_space_handler(ec->handle,
 -                                                  ACPI_ADR_SPACE_EC,
 -                                                  &acpi_ec_space_handler,
 -                                                  NULL, ec);
 -      if (ACPI_FAILURE(status)) {
 -              if (status == AE_NOT_FOUND) {
 -                      /*
 -                       * Maybe OS fails in evaluating the _REG object.
 -                       * The AE_NOT_FOUND error will be ignored and OS
 -                       * continue to initialize EC.
 -                       */
 -                      printk(KERN_ERR "Fail in evaluating the _REG object"
 -                              " of EC device. Broken bios is suspected.\n");
 -              } else {
 -                      acpi_remove_gpe_handler(NULL, ec->gpe,
 -                              &acpi_ec_gpe_handler);
 -                      return -ENODEV;
 -              }
 -      }
 -
 -      set_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
 -      return 0;
 -}
 -
 -static int acpi_ec_start(struct acpi_device *device)
 -{
 -      struct acpi_ec *ec;
 -      int ret = 0;
 -
 -      if (!device)
 -              return -EINVAL;
 -
 -      ec = acpi_driver_data(device);
 -
 -      if (!ec)
 -              return -EINVAL;
 -
 -      ret = ec_install_handlers(ec);
 -
 -      /* EC is fully operational, allow queries */
 -      clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
 -      return ret;
 -}
 -
 -static int acpi_ec_stop(struct acpi_device *device, int type)
 -{
 -      struct acpi_ec *ec;
 -      if (!device)
 -              return -EINVAL;
 -      ec = acpi_driver_data(device);
 -      if (!ec)
 -              return -EINVAL;
 -      ec_remove_handlers(ec);
 -
 -      return 0;
 -}
 -
  int __init acpi_boot_ec_enable(void)
  {
        if (!boot_ec || test_bit(EC_FLAGS_HANDLERS_INSTALLED, &boot_ec->flags))
@@@ -1025,8 -1008,6 +979,6 @@@ static int acpi_ec_suspend(struct acpi_
  {
        struct acpi_ec *ec = acpi_driver_data(device);
        /* Stop using GPE */
-       set_bit(EC_FLAGS_NO_GPE, &ec->flags);
-       clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
        acpi_disable_gpe(NULL, ec->gpe);
        return 0;
  }
@@@ -1035,8 -1016,6 +987,6 @@@ static int acpi_ec_resume(struct acpi_d
  {
        struct acpi_ec *ec = acpi_driver_data(device);
        /* Enable use of GPE back */
-       clear_bit(EC_FLAGS_NO_GPE, &ec->flags);
-       set_bit(EC_FLAGS_GPE_MODE, &ec->flags);
        acpi_enable_gpe(NULL, ec->gpe);
        return 0;
  }
@@@ -1048,6 -1027,8 +998,6 @@@ static struct acpi_driver acpi_ec_drive
        .ops = {
                .add = acpi_ec_add,
                .remove = acpi_ec_remove,
 -              .start = acpi_ec_start,
 -              .stop = acpi_ec_stop,
                .suspend = acpi_ec_suspend,
                .resume = acpi_ec_resume,
                },