iwl3945: fix scan races
[safe/jmp/linux-2.6] / drivers / net / wireless / iwlwifi / iwl-led.c
index aa6ad18..db5bfcb 100644 (file)
@@ -1,6 +1,6 @@
 /******************************************************************************
  *
- * Copyright(c) 2003 - 2008 Intel Corporation. All rights reserved.
+ * Copyright(c) 2003 - 2010 Intel Corporation. All rights reserved.
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of version 2 of the GNU General Public License as
@@ -19,7 +19,7 @@
  * file called LICENSE.
  *
  * Contact Information:
- * James P. Ketrenos <ipw2100-admin@linux.intel.com>
+ *  Intel Linux Wireless <ilw@linux.intel.com>
  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  *
  *****************************************************************************/
@@ -27,7 +27,6 @@
 
 #include <linux/kernel.h>
 #include <linux/module.h>
-#include <linux/version.h>
 #include <linux/init.h>
 #include <linux/pci.h>
 #include <linux/dma-mapping.h>
 #include "iwl-dev.h"
 #include "iwl-core.h"
 #include "iwl-io.h"
-#include "iwl-helpers.h"
 
-#define IWL_1MB_RATE (128 * 1024)
-#define IWL_LED_THRESHOLD (16)
-#define IWL_MAX_BLINK_TBL (10)
+/* default: IWL_LED_BLINK(0) using blinking index table */
+static int led_mode;
+module_param(led_mode, int, S_IRUGO);
+MODULE_PARM_DESC(led_mode, "led mode: 0=blinking, 1=On(RF On)/Off(RF Off), "
+                          "(default 0)");
+
 
 static const struct {
-       u16 tpt;
+       u16 tpt;        /* Mb/s */
        u8 on_time;
-       u8 of_time;
+       u8 off_time;
 } blink_tbl[] =
 {
        {300, 25, 25},
@@ -60,237 +61,117 @@ static const struct {
        {70, 65, 65},
        {50, 75, 75},
        {20, 85, 85},
-       {15, 95, 95 },
-       {10, 110, 110},
-       {5, 130, 130},
-       {0, 167, 167}
+       {10, 95, 95},
+       {5, 110, 110},
+       {1, 130, 130},
+       {0, 167, 167},
+       /* SOLID_ON */
+       {-1, IWL_LED_SOLID, 0}
 };
 
-static int iwl_led_cmd_callback(struct iwl_priv *priv,
-                               struct iwl_cmd *cmd, struct sk_buff *skb)
-{
-       return 1;
-}
-
-
-/* Send led command */
-static int iwl_send_led_cmd(struct iwl_priv *priv,
-                           struct iwl4965_led_cmd *led_cmd)
-{
-       struct iwl_host_cmd cmd = {
-               .id = REPLY_LEDS_CMD,
-               .len = sizeof(struct iwl4965_led_cmd),
-               .data = led_cmd,
-               .meta.flags = CMD_ASYNC,
-               .meta.u.callback = iwl_led_cmd_callback
-       };
-       u32 reg;
-
-       reg = iwl_read32(priv, CSR_LED_REG);
-       if (reg != (reg & CSR_LED_BSM_CTRL_MSK))
-               iwl_write32(priv, CSR_LED_REG, reg & CSR_LED_BSM_CTRL_MSK);
-
-       return iwl_send_cmd(priv, &cmd);
-}
-
-
-/* Set led on command */
-static int iwl4965_led_on(struct iwl_priv *priv, int led_id)
-{
-       struct iwl4965_led_cmd led_cmd = {
-               .id = led_id,
-               .on = IWL_LED_SOLID,
-               .off = 0,
-               .interval = IWL_DEF_LED_INTRVL
-       };
-       return iwl_send_led_cmd(priv, &led_cmd);
-}
+#define IWL_1MB_RATE (128 * 1024)
+#define IWL_LED_THRESHOLD (16)
+#define IWL_MAX_BLINK_TBL (ARRAY_SIZE(blink_tbl) - 1) /* exclude SOLID_ON */
+#define IWL_SOLID_BLINK_IDX (ARRAY_SIZE(blink_tbl) - 1)
 
-/* Set led on command */
-static int iwl4965_led_pattern(struct iwl_priv *priv, int led_id,
-                              enum led_brightness brightness)
+/*
+ * Adjust led blink rate to compensate on a MAC Clock difference on every HW
+ * Led blink rate analysis showed an average deviation of 0% on 3945,
+ * 5% on 4965 HW and 20% on 5000 series and up.
+ * Need to compensate on the led on/off time per HW according to the deviation
+ * to achieve the desired led frequency
+ * The calculation is: (100-averageDeviation)/100 * blinkTime
+ * For code efficiency the calculation will be:
+ *     compensation = (100 - averageDeviation) * 64 / 100
+ *     NewBlinkTime = (compensation * BlinkTime) / 64
+ */
+static inline u8 iwl_blink_compensation(struct iwl_priv *priv,
+                                   u8 time, u16 compensation)
 {
-       struct iwl4965_led_cmd led_cmd = {
-               .id = led_id,
-               .on = brightness,
-               .off = brightness,
-               .interval = IWL_DEF_LED_INTRVL
-       };
-       if (brightness == LED_FULL) {
-               led_cmd.on = IWL_LED_SOLID;
-               led_cmd.off = 0;
+       if (!compensation) {
+               IWL_ERR(priv, "undefined blink compensation: "
+                       "use pre-defined blinking time\n");
+               return time;
        }
-       return iwl_send_led_cmd(priv, &led_cmd);
-}
 
-/* Set led register off */
-static int iwl4965_led_on_reg(struct iwl_priv *priv, int led_id)
-{
-       IWL_DEBUG_LED("led on %d\n", led_id);
-       iwl_write32(priv, CSR_LED_REG, CSR_LED_REG_TRUN_ON);
-       return 0;
+       return (u8)((time * compensation) >> 6);
 }
 
-#if 0
-/* Set led off command */
-int iwl4965_led_off(struct iwl_priv *priv, int led_id)
+/* Set led pattern command */
+static int iwl_led_pattern(struct iwl_priv *priv, unsigned int idx)
 {
-       struct iwl4965_led_cmd led_cmd = {
-               .id = led_id,
-               .on = 0,
-               .off = 0,
+       struct iwl_led_cmd led_cmd = {
+               .id = IWL_LED_LINK,
                .interval = IWL_DEF_LED_INTRVL
        };
-       IWL_DEBUG_LED("led off %d\n", led_id);
-       return iwl_send_led_cmd(priv, &led_cmd);
-}
-#endif
-
 
-/* Set led register off */
-static int iwl4965_led_off_reg(struct iwl_priv *priv, int led_id)
-{
-       IWL_DEBUG_LED("radio off\n");
-       iwl_write32(priv, CSR_LED_REG, CSR_LED_REG_TRUN_OFF);
-       return 0;
-}
+       BUG_ON(idx > IWL_MAX_BLINK_TBL);
 
-/* Set led blink command */
-static int iwl4965_led_not_solid(struct iwl_priv *priv, int led_id,
-                              u8 brightness)
-{
-       struct iwl4965_led_cmd led_cmd = {
-               .id = led_id,
-               .on = brightness,
-               .off = brightness,
-               .interval = IWL_DEF_LED_INTRVL
-       };
+       IWL_DEBUG_LED(priv, "Led blink time compensation= %u\n",
+                       priv->cfg->led_compensation);
+       led_cmd.on =
+               iwl_blink_compensation(priv, blink_tbl[idx].on_time,
+                                       priv->cfg->led_compensation);
+       led_cmd.off =
+               iwl_blink_compensation(priv, blink_tbl[idx].off_time,
+                                       priv->cfg->led_compensation);
 
-       return iwl_send_led_cmd(priv, &led_cmd);
+       return priv->cfg->ops->led->cmd(priv, &led_cmd);
 }
 
-
-/*
- * brightness call back function for Tx/Rx LED
- */
-static int iwl4965_led_associated(struct iwl_priv *priv, int led_id)
+int iwl_led_start(struct iwl_priv *priv)
 {
-       if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
-           !test_bit(STATUS_READY, &priv->status))
-               return 0;
-
-
-       /* start counting Tx/Rx bytes */
-       if (!priv->last_blink_time && priv->allow_blinking)
-               priv->last_blink_time = jiffies;
-       return 0;
+       return priv->cfg->ops->led->on(priv);
 }
+EXPORT_SYMBOL(iwl_led_start);
 
-/*
- * brightness call back for association and radio
- */
-static void iwl4965_led_brightness_set(struct led_classdev *led_cdev,
-                                      enum led_brightness brightness)
+int iwl_led_associate(struct iwl_priv *priv)
 {
-       struct iwl4965_led *led = container_of(led_cdev,
-                                              struct iwl4965_led, led_dev);
-       struct iwl_priv *priv = led->priv;
-
-       if (test_bit(STATUS_EXIT_PENDING, &priv->status))
-               return;
+       IWL_DEBUG_LED(priv, "Associated\n");
+       if (led_mode == IWL_LED_BLINK)
+               priv->allow_blinking = 1;
+       priv->last_blink_time = jiffies;
 
-       switch (brightness) {
-       case LED_FULL:
-               if (led->type == IWL_LED_TRG_ASSOC)
-                       priv->allow_blinking = 1;
-
-               if (led->led_on)
-                       led->led_on(priv, IWL_LED_LINK);
-               break;
-       case LED_OFF:
-               if (led->type == IWL_LED_TRG_ASSOC)
-                       priv->allow_blinking = 0;
-
-               if (led->led_off)
-                       led->led_off(priv, IWL_LED_LINK);
-               break;
-       default:
-               if (led->led_pattern)
-                       led->led_pattern(priv, IWL_LED_LINK, brightness);
-               break;
-       }
+       return 0;
 }
 
-
-
-/*
- * Register led class with the system
- */
-static int iwl_leds_register_led(struct iwl_priv *priv,
-                                  struct iwl4965_led *led,
-                                  enum led_type type, u8 set_led,
-                                  const char *name, char *trigger)
+int iwl_led_disassociate(struct iwl_priv *priv)
 {
-       struct device *device = wiphy_dev(priv->hw->wiphy);
-       int ret;
-
-       led->led_dev.name = name;
-       led->led_dev.brightness_set = iwl4965_led_brightness_set;
-       led->led_dev.default_trigger = trigger;
-
-       led->priv = priv;
-       led->type = type;
-
-       ret = led_classdev_register(device, &led->led_dev);
-       if (ret) {
-               IWL_ERROR("Error: failed to register led handler.\n");
-               return ret;
-       }
-
-       led->registered = 1;
-
-       if (set_led && led->led_on)
-               led->led_on(priv, IWL_LED_LINK);
+       priv->allow_blinking = 0;
 
        return 0;
 }
 
-
 /*
- * calculate blink rate according to last 2 sec Tx/Rx activities
+ * calculate blink rate according to last second Tx/Rx activities
  */
-static inline u8 get_blink_rate(struct iwl_priv *priv)
+static int iwl_get_blink_rate(struct iwl_priv *priv)
 {
        int i;
-       u8 blink_rate;
-       u64 current_tpt = priv->tx_stats[2].bytes + priv->rx_stats[2].bytes;
+       /* count both tx and rx traffic to be able to
+        * handle traffic in either direction
+        */
+       u64 current_tpt = priv->tx_stats.data_bytes +
+                         priv->rx_stats.data_bytes;
        s64 tpt = current_tpt - priv->led_tpt;
 
-       if (tpt < 0) /* wrapparound */
+       if (tpt < 0) /* wraparound */
                tpt = -tpt;
 
+       IWL_DEBUG_LED(priv, "tpt %lld current_tpt %llu\n",
+               (long long)tpt,
+               (unsigned long long)current_tpt);
        priv->led_tpt = current_tpt;
 
-       if (tpt < IWL_LED_THRESHOLD) {
+       if (!priv->allow_blinking)
                i = IWL_MAX_BLINK_TBL;
-       } else {
+       else
                for (i = 0; i < IWL_MAX_BLINK_TBL; i++)
-                       if (tpt  > (blink_tbl[i].tpt * IWL_1MB_RATE))
+                       if (tpt > (blink_tbl[i].tpt * IWL_1MB_RATE))
                                break;
-       }
-       /* if 0 frame is transfered */
-       if ((i == IWL_MAX_BLINK_TBL) || !priv->allow_blinking)
-               blink_rate = IWL_LED_SOLID;
-       else
-               blink_rate = blink_tbl[i].on_time;
-
-       return blink_rate;
-}
 
-static inline int is_rf_kill(struct iwl_priv *priv)
-{
-       return test_bit(STATUS_RF_KILL_HW, &priv->status) ||
-               test_bit(STATUS_RF_KILL_SW, &priv->status);
+       IWL_DEBUG_LED(priv, "LED BLINK IDX=%d\n", i);
+       return i;
 }
 
 /*
@@ -300,22 +181,22 @@ static inline int is_rf_kill(struct iwl_priv *priv)
  */
 void iwl_leds_background(struct iwl_priv *priv)
 {
-       u8 blink_rate;
+       u8 blink_idx;
 
        if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
                priv->last_blink_time = 0;
                return;
        }
-       if (is_rf_kill(priv)) {
+       if (iwl_is_rfkill(priv)) {
                priv->last_blink_time = 0;
                return;
        }
 
        if (!priv->allow_blinking) {
                priv->last_blink_time = 0;
-               if (priv->last_blink_rate != IWL_LED_SOLID) {
-                       priv->last_blink_rate = IWL_LED_SOLID;
-                       iwl4965_led_on(priv, IWL_LED_LINK);
+               if (priv->last_blink_rate != IWL_SOLID_BLINK_IDX) {
+                       priv->last_blink_rate = IWL_SOLID_BLINK_IDX;
+                       iwl_led_pattern(priv, IWL_SOLID_BLINK_IDX);
                }
                return;
        }
@@ -324,126 +205,21 @@ void iwl_leds_background(struct iwl_priv *priv)
                        msecs_to_jiffies(1000)))
                return;
 
-       blink_rate = get_blink_rate(priv);
+       blink_idx = iwl_get_blink_rate(priv);
 
        /* call only if blink rate change */
-       if (blink_rate != priv->last_blink_rate) {
-               if (blink_rate != IWL_LED_SOLID) {
-                       priv->last_blink_time = jiffies +
-                                               msecs_to_jiffies(1000);
-                       iwl4965_led_not_solid(priv, IWL_LED_LINK, blink_rate);
-               } else {
-                       priv->last_blink_time = 0;
-                       iwl4965_led_on(priv, IWL_LED_LINK);
-               }
-       }
+       if (blink_idx != priv->last_blink_rate)
+               iwl_led_pattern(priv, blink_idx);
 
-       priv->last_blink_rate = blink_rate;
+       priv->last_blink_time = jiffies;
+       priv->last_blink_rate = blink_idx;
 }
 EXPORT_SYMBOL(iwl_leds_background);
 
-/* Register all led handler */
-int iwl_leds_register(struct iwl_priv *priv)
+void iwl_leds_init(struct iwl_priv *priv)
 {
-       char *trigger;
-       char name[32];
-       int ret;
-
        priv->last_blink_rate = 0;
-       priv->led_tpt = 0;
        priv->last_blink_time = 0;
        priv->allow_blinking = 0;
-
-       trigger = ieee80211_get_radio_led_name(priv->hw);
-       snprintf(name, sizeof(name), "iwl-%s:radio",
-                wiphy_name(priv->hw->wiphy));
-
-       priv->led[IWL_LED_TRG_RADIO].led_on = iwl4965_led_on_reg;
-       priv->led[IWL_LED_TRG_RADIO].led_off = iwl4965_led_off_reg;
-       priv->led[IWL_LED_TRG_RADIO].led_pattern = NULL;
-
-       ret = iwl_leds_register_led(priv,
-                                  &priv->led[IWL_LED_TRG_RADIO],
-                                  IWL_LED_TRG_RADIO, 1,
-                                  name, trigger);
-       if (ret)
-               goto exit_fail;
-
-       trigger = ieee80211_get_assoc_led_name(priv->hw);
-       snprintf(name, sizeof(name), "iwl-%s:assoc",
-                wiphy_name(priv->hw->wiphy));
-
-       ret = iwl_leds_register_led(priv,
-                                  &priv->led[IWL_LED_TRG_ASSOC],
-                                  IWL_LED_TRG_ASSOC, 0,
-                                  name, trigger);
-       /* for assoc always turn led on */
-       priv->led[IWL_LED_TRG_ASSOC].led_on = iwl4965_led_on_reg;
-       priv->led[IWL_LED_TRG_ASSOC].led_off = iwl4965_led_on_reg;
-       priv->led[IWL_LED_TRG_ASSOC].led_pattern = NULL;
-
-       if (ret)
-               goto exit_fail;
-
-       trigger = ieee80211_get_rx_led_name(priv->hw);
-       snprintf(name, sizeof(name), "iwl-%s:RX",
-                wiphy_name(priv->hw->wiphy));
-
-
-       ret = iwl_leds_register_led(priv,
-                                  &priv->led[IWL_LED_TRG_RX],
-                                  IWL_LED_TRG_RX, 0,
-                                  name, trigger);
-
-       priv->led[IWL_LED_TRG_RX].led_on = iwl4965_led_associated;
-       priv->led[IWL_LED_TRG_RX].led_off = iwl4965_led_associated;
-       priv->led[IWL_LED_TRG_RX].led_pattern = iwl4965_led_pattern;
-
-       if (ret)
-               goto exit_fail;
-
-       trigger = ieee80211_get_tx_led_name(priv->hw);
-       snprintf(name, sizeof(name), "iwl-%s:TX",
-                wiphy_name(priv->hw->wiphy));
-       ret = iwl_leds_register_led(priv,
-                                  &priv->led[IWL_LED_TRG_TX],
-                                  IWL_LED_TRG_TX, 0,
-                                  name, trigger);
-       priv->led[IWL_LED_TRG_TX].led_on = iwl4965_led_associated;
-       priv->led[IWL_LED_TRG_TX].led_off = iwl4965_led_associated;
-       priv->led[IWL_LED_TRG_TX].led_pattern = iwl4965_led_pattern;
-
-       if (ret)
-               goto exit_fail;
-
-       return 0;
-
-exit_fail:
-       iwl_leds_unregister(priv);
-       return ret;
-}
-EXPORT_SYMBOL(iwl_leds_register);
-
-/* unregister led class */
-static void iwl_leds_unregister_led(struct iwl4965_led *led, u8 set_led)
-{
-       if (!led->registered)
-               return;
-
-       led_classdev_unregister(&led->led_dev);
-
-       if (set_led)
-               led->led_dev.brightness_set(&led->led_dev, LED_OFF);
-       led->registered = 0;
-}
-
-/* Unregister all led handlers */
-void iwl_leds_unregister(struct iwl_priv *priv)
-{
-       iwl_leds_unregister_led(&priv->led[IWL_LED_TRG_ASSOC], 0);
-       iwl_leds_unregister_led(&priv->led[IWL_LED_TRG_RX], 0);
-       iwl_leds_unregister_led(&priv->led[IWL_LED_TRG_TX], 0);
-       iwl_leds_unregister_led(&priv->led[IWL_LED_TRG_RADIO], 1);
 }
-EXPORT_SYMBOL(iwl_leds_unregister);
-
+EXPORT_SYMBOL(iwl_leds_init);