iwl-led.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2010 Intel Corporation. All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of version 2 of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  17. *
  18. * The full GNU General Public License is included in this distribution in the
  19. * file called LICENSE.
  20. *
  21. * Contact Information:
  22. * Intel Linux Wireless <ilw@linux.intel.com>
  23. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  24. *
  25. *****************************************************************************/
  26. #include <linux/kernel.h>
  27. #include <linux/module.h>
  28. #include <linux/init.h>
  29. #include <linux/pci.h>
  30. #include <linux/dma-mapping.h>
  31. #include <linux/delay.h>
  32. #include <linux/skbuff.h>
  33. #include <linux/netdevice.h>
  34. #include <linux/wireless.h>
  35. #include <net/mac80211.h>
  36. #include <linux/etherdevice.h>
  37. #include <asm/unaligned.h>
  38. #include "iwl-dev.h"
  39. #include "iwl-core.h"
  40. #include "iwl-io.h"
  41. /* default: IWL_LED_BLINK(0) using blinking index table */
  42. static int led_mode;
  43. module_param(led_mode, int, S_IRUGO);
  44. MODULE_PARM_DESC(led_mode, "led mode: 0=blinking, 1=On(RF On)/Off(RF Off), "
  45. "(default 0)");
  46. static const struct {
  47. u16 tpt; /* Mb/s */
  48. u8 on_time;
  49. u8 off_time;
  50. } blink_tbl[] =
  51. {
  52. {300, 25, 25},
  53. {200, 40, 40},
  54. {100, 55, 55},
  55. {70, 65, 65},
  56. {50, 75, 75},
  57. {20, 85, 85},
  58. {10, 95, 95},
  59. {5, 110, 110},
  60. {1, 130, 130},
  61. {0, 167, 167},
  62. /* SOLID_ON */
  63. {-1, IWL_LED_SOLID, 0}
  64. };
  65. #define IWL_1MB_RATE (128 * 1024)
  66. #define IWL_LED_THRESHOLD (16)
  67. #define IWL_MAX_BLINK_TBL (ARRAY_SIZE(blink_tbl) - 1) /* exclude SOLID_ON */
  68. #define IWL_SOLID_BLINK_IDX (ARRAY_SIZE(blink_tbl) - 1)
  69. /*
  70. * Adjust led blink rate to compensate on a MAC Clock difference on every HW
  71. * Led blink rate analysis showed an average deviation of 0% on 3945,
  72. * 5% on 4965 HW and 20% on 5000 series and up.
  73. * Need to compensate on the led on/off time per HW according to the deviation
  74. * to achieve the desired led frequency
  75. * The calculation is: (100-averageDeviation)/100 * blinkTime
  76. * For code efficiency the calculation will be:
  77. * compensation = (100 - averageDeviation) * 64 / 100
  78. * NewBlinkTime = (compensation * BlinkTime) / 64
  79. */
  80. static inline u8 iwl_blink_compensation(struct iwl_priv *priv,
  81. u8 time, u16 compensation)
  82. {
  83. if (!compensation) {
  84. IWL_ERR(priv, "undefined blink compensation: "
  85. "use pre-defined blinking time\n");
  86. return time;
  87. }
  88. return (u8)((time * compensation) >> 6);
  89. }
  90. /* Set led pattern command */
  91. static int iwl_led_pattern(struct iwl_priv *priv, unsigned int idx)
  92. {
  93. struct iwl_led_cmd led_cmd = {
  94. .id = IWL_LED_LINK,
  95. .interval = IWL_DEF_LED_INTRVL
  96. };
  97. BUG_ON(idx > IWL_MAX_BLINK_TBL);
  98. IWL_DEBUG_LED(priv, "Led blink time compensation= %u\n",
  99. priv->cfg->base_params->led_compensation);
  100. led_cmd.on =
  101. iwl_blink_compensation(priv, blink_tbl[idx].on_time,
  102. priv->cfg->base_params->led_compensation);
  103. led_cmd.off =
  104. iwl_blink_compensation(priv, blink_tbl[idx].off_time,
  105. priv->cfg->base_params->led_compensation);
  106. return priv->cfg->ops->led->cmd(priv, &led_cmd);
  107. }
  108. int iwl_led_start(struct iwl_priv *priv)
  109. {
  110. return priv->cfg->ops->led->on(priv);
  111. }
  112. EXPORT_SYMBOL(iwl_led_start);
  113. int iwl_led_associate(struct iwl_priv *priv)
  114. {
  115. IWL_DEBUG_LED(priv, "Associated\n");
  116. if (led_mode == IWL_LED_BLINK)
  117. priv->allow_blinking = 1;
  118. priv->last_blink_time = jiffies;
  119. return 0;
  120. }
  121. int iwl_led_disassociate(struct iwl_priv *priv)
  122. {
  123. priv->allow_blinking = 0;
  124. return 0;
  125. }
  126. /*
  127. * calculate blink rate according to last second Tx/Rx activities
  128. */
  129. static int iwl_get_blink_rate(struct iwl_priv *priv)
  130. {
  131. int i;
  132. /* count both tx and rx traffic to be able to
  133. * handle traffic in either direction
  134. */
  135. u64 current_tpt = priv->tx_stats.data_bytes +
  136. priv->rx_stats.data_bytes;
  137. s64 tpt = current_tpt - priv->led_tpt;
  138. if (tpt < 0) /* wraparound */
  139. tpt = -tpt;
  140. IWL_DEBUG_LED(priv, "tpt %lld current_tpt %llu\n",
  141. (long long)tpt,
  142. (unsigned long long)current_tpt);
  143. priv->led_tpt = current_tpt;
  144. if (!priv->allow_blinking)
  145. i = IWL_MAX_BLINK_TBL;
  146. else
  147. for (i = 0; i < IWL_MAX_BLINK_TBL; i++)
  148. if (tpt > (blink_tbl[i].tpt * IWL_1MB_RATE))
  149. break;
  150. IWL_DEBUG_LED(priv, "LED BLINK IDX=%d\n", i);
  151. return i;
  152. }
  153. /*
  154. * this function called from handler. Since setting Led command can
  155. * happen very frequent we postpone led command to be called from
  156. * REPLY handler so we know ucode is up
  157. */
  158. void iwl_leds_background(struct iwl_priv *priv)
  159. {
  160. u8 blink_idx;
  161. if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
  162. priv->last_blink_time = 0;
  163. return;
  164. }
  165. if (iwl_is_rfkill(priv)) {
  166. priv->last_blink_time = 0;
  167. return;
  168. }
  169. if (!priv->allow_blinking) {
  170. priv->last_blink_time = 0;
  171. if (priv->last_blink_rate != IWL_SOLID_BLINK_IDX) {
  172. priv->last_blink_rate = IWL_SOLID_BLINK_IDX;
  173. iwl_led_pattern(priv, IWL_SOLID_BLINK_IDX);
  174. }
  175. return;
  176. }
  177. if (!priv->last_blink_time ||
  178. !time_after(jiffies, priv->last_blink_time +
  179. msecs_to_jiffies(1000)))
  180. return;
  181. blink_idx = iwl_get_blink_rate(priv);
  182. /* call only if blink rate change */
  183. if (blink_idx != priv->last_blink_rate)
  184. iwl_led_pattern(priv, blink_idx);
  185. priv->last_blink_time = jiffies;
  186. priv->last_blink_rate = blink_idx;
  187. }
  188. EXPORT_SYMBOL(iwl_leds_background);
  189. void iwl_leds_init(struct iwl_priv *priv)
  190. {
  191. priv->last_blink_rate = 0;
  192. priv->last_blink_time = 0;
  193. priv->allow_blinking = 0;
  194. }
  195. EXPORT_SYMBOL(iwl_leds_init);