iwl-led.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2008 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. * James P. Ketrenos <ipw2100-admin@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/version.h>
  29. #include <linux/init.h>
  30. #include <linux/pci.h>
  31. #include <linux/dma-mapping.h>
  32. #include <linux/delay.h>
  33. #include <linux/skbuff.h>
  34. #include <linux/netdevice.h>
  35. #include <linux/wireless.h>
  36. #include <net/mac80211.h>
  37. #include <linux/etherdevice.h>
  38. #include <asm/unaligned.h>
  39. #include "iwl-dev.h"
  40. #include "iwl-core.h"
  41. #include "iwl-io.h"
  42. #include "iwl-helpers.h"
  43. #ifdef CONFIG_IWLWIFI_DEBUG
  44. static const char *led_type_str[] = {
  45. __stringify(IWL_LED_TRG_TX),
  46. __stringify(IWL_LED_TRG_RX),
  47. __stringify(IWL_LED_TRG_ASSOC),
  48. __stringify(IWL_LED_TRG_RADIO),
  49. NULL
  50. };
  51. #endif /* CONFIG_IWLWIFI_DEBUG */
  52. static const struct {
  53. u16 tpt;
  54. u8 on_time;
  55. u8 off_time;
  56. } blink_tbl[] =
  57. {
  58. {300, 25, 25},
  59. {200, 40, 40},
  60. {100, 55, 55},
  61. {70, 65, 65},
  62. {50, 75, 75},
  63. {20, 85, 85},
  64. {15, 95, 95 },
  65. {10, 110, 110},
  66. {5, 130, 130},
  67. {0, 167, 167},
  68. /* SOLID_ON */
  69. {-1, IWL_LED_SOLID, 0}
  70. };
  71. #define IWL_1MB_RATE (128 * 1024)
  72. #define IWL_LED_THRESHOLD (16)
  73. #define IWL_MAX_BLINK_TBL (ARRAY_SIZE(blink_tbl) - 1) /* exclude SOLID_ON */
  74. #define IWL_SOLID_BLINK_IDX (ARRAY_SIZE(blink_tbl) - 1)
  75. /* [0-256] -> [0..8] FIXME: we need [0..10] */
  76. static inline int iwl_brightness_to_idx(enum led_brightness brightness)
  77. {
  78. return fls(0x000000FF & (u32)brightness);
  79. }
  80. /* Send led command */
  81. static int iwl_send_led_cmd(struct iwl_priv *priv, struct iwl_led_cmd *led_cmd)
  82. {
  83. struct iwl_host_cmd cmd = {
  84. .id = REPLY_LEDS_CMD,
  85. .len = sizeof(struct iwl_led_cmd),
  86. .data = led_cmd,
  87. .meta.flags = CMD_ASYNC,
  88. .meta.u.callback = NULL,
  89. };
  90. u32 reg;
  91. reg = iwl_read32(priv, CSR_LED_REG);
  92. if (reg != (reg & CSR_LED_BSM_CTRL_MSK))
  93. iwl_write32(priv, CSR_LED_REG, reg & CSR_LED_BSM_CTRL_MSK);
  94. return iwl_send_cmd(priv, &cmd);
  95. }
  96. /* Set led pattern command */
  97. static int iwl4965_led_pattern(struct iwl_priv *priv, int led_id, int idx)
  98. {
  99. struct iwl_led_cmd led_cmd = {
  100. .id = led_id,
  101. .interval = IWL_DEF_LED_INTRVL
  102. };
  103. BUG_ON(idx > IWL_MAX_BLINK_TBL);
  104. led_cmd.on = blink_tbl[idx].on_time;
  105. led_cmd.off = blink_tbl[idx].off_time;
  106. return iwl_send_led_cmd(priv, &led_cmd);
  107. }
  108. /* Set led register off */
  109. static int iwl4965_led_on_reg(struct iwl_priv *priv, int led_id)
  110. {
  111. IWL_DEBUG_LED("led on %d\n", led_id);
  112. iwl_write32(priv, CSR_LED_REG, CSR_LED_REG_TRUN_ON);
  113. return 0;
  114. }
  115. #if 0
  116. /* Set led on command */
  117. static int iwl4965_led_on(struct iwl_priv *priv, int led_id)
  118. {
  119. struct iwl_led_cmd led_cmd = {
  120. .id = led_id,
  121. .on = IWL_LED_SOLID,
  122. .off = 0,
  123. .interval = IWL_DEF_LED_INTRVL
  124. };
  125. return iwl_send_led_cmd(priv, &led_cmd);
  126. }
  127. /* Set led off command */
  128. int iwl4965_led_off(struct iwl_priv *priv, int led_id)
  129. {
  130. struct iwl_led_cmd led_cmd = {
  131. .id = led_id,
  132. .on = 0,
  133. .off = 0,
  134. .interval = IWL_DEF_LED_INTRVL
  135. };
  136. IWL_DEBUG_LED("led off %d\n", led_id);
  137. return iwl_send_led_cmd(priv, &led_cmd);
  138. }
  139. #endif
  140. /* Set led register off */
  141. static int iwl4965_led_off_reg(struct iwl_priv *priv, int led_id)
  142. {
  143. IWL_DEBUG_LED("radio off\n");
  144. iwl_write32(priv, CSR_LED_REG, CSR_LED_REG_TRUN_OFF);
  145. return 0;
  146. }
  147. /*
  148. * brightness call back function for Tx/Rx LED
  149. */
  150. static int iwl_led_associated(struct iwl_priv *priv, int led_id)
  151. {
  152. if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
  153. !test_bit(STATUS_READY, &priv->status))
  154. return 0;
  155. /* start counting Tx/Rx bytes */
  156. if (!priv->last_blink_time && priv->allow_blinking)
  157. priv->last_blink_time = jiffies;
  158. return 0;
  159. }
  160. /*
  161. * brightness call back for association and radio
  162. */
  163. static void iwl_led_brightness_set(struct led_classdev *led_cdev,
  164. enum led_brightness brightness)
  165. {
  166. struct iwl_led *led = container_of(led_cdev, struct iwl_led, led_dev);
  167. struct iwl_priv *priv = led->priv;
  168. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  169. return;
  170. IWL_DEBUG_LED("Led type = %s brightness = %d\n",
  171. led_type_str[led->type], brightness);
  172. switch (brightness) {
  173. case LED_FULL:
  174. if (led->type == IWL_LED_TRG_ASSOC)
  175. priv->allow_blinking = 1;
  176. if (led->led_on)
  177. led->led_on(priv, IWL_LED_LINK);
  178. break;
  179. case LED_OFF:
  180. if (led->type == IWL_LED_TRG_ASSOC)
  181. priv->allow_blinking = 0;
  182. if (led->led_off)
  183. led->led_off(priv, IWL_LED_LINK);
  184. break;
  185. default:
  186. if (led->led_pattern) {
  187. int idx = iwl_brightness_to_idx(brightness);
  188. led->led_pattern(priv, IWL_LED_LINK, idx);
  189. }
  190. break;
  191. }
  192. }
  193. /*
  194. * Register led class with the system
  195. */
  196. static int iwl_leds_register_led(struct iwl_priv *priv, struct iwl_led *led,
  197. enum led_type type, u8 set_led,
  198. const char *name, char *trigger)
  199. {
  200. struct device *device = wiphy_dev(priv->hw->wiphy);
  201. int ret;
  202. led->led_dev.name = name;
  203. led->led_dev.brightness_set = iwl_led_brightness_set;
  204. led->led_dev.default_trigger = trigger;
  205. led->priv = priv;
  206. led->type = type;
  207. ret = led_classdev_register(device, &led->led_dev);
  208. if (ret) {
  209. IWL_ERROR("Error: failed to register led handler.\n");
  210. return ret;
  211. }
  212. led->registered = 1;
  213. if (set_led && led->led_on)
  214. led->led_on(priv, IWL_LED_LINK);
  215. return 0;
  216. }
  217. /*
  218. * calculate blink rate according to last 2 sec Tx/Rx activities
  219. */
  220. static int iwl_get_blink_rate(struct iwl_priv *priv)
  221. {
  222. int i;
  223. u64 current_tpt = priv->tx_stats[2].bytes;
  224. /* FIXME: + priv->rx_stats[2].bytes; */
  225. s64 tpt = current_tpt - priv->led_tpt;
  226. if (tpt < 0) /* wrapparound */
  227. tpt = -tpt;
  228. IWL_DEBUG_LED("tpt %lld current_tpt %lld\n", tpt, current_tpt);
  229. priv->led_tpt = current_tpt;
  230. if (!priv->allow_blinking)
  231. i = IWL_MAX_BLINK_TBL;
  232. else
  233. for (i = 0; i < IWL_MAX_BLINK_TBL; i++)
  234. if (tpt > (blink_tbl[i].tpt * IWL_1MB_RATE))
  235. break;
  236. IWL_DEBUG_LED("LED BLINK IDX=%d", i);
  237. return i;
  238. }
  239. static inline int is_rf_kill(struct iwl_priv *priv)
  240. {
  241. return test_bit(STATUS_RF_KILL_HW, &priv->status) ||
  242. test_bit(STATUS_RF_KILL_SW, &priv->status);
  243. }
  244. /*
  245. * this function called from handler. Since setting Led command can
  246. * happen very frequent we postpone led command to be called from
  247. * REPLY handler so we know ucode is up
  248. */
  249. void iwl_leds_background(struct iwl_priv *priv)
  250. {
  251. u8 blink_idx;
  252. if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
  253. priv->last_blink_time = 0;
  254. return;
  255. }
  256. if (is_rf_kill(priv)) {
  257. priv->last_blink_time = 0;
  258. return;
  259. }
  260. if (!priv->allow_blinking) {
  261. priv->last_blink_time = 0;
  262. if (priv->last_blink_rate != IWL_SOLID_BLINK_IDX) {
  263. priv->last_blink_rate = IWL_SOLID_BLINK_IDX;
  264. iwl4965_led_pattern(priv, IWL_LED_LINK,
  265. IWL_SOLID_BLINK_IDX);
  266. }
  267. return;
  268. }
  269. if (!priv->last_blink_time ||
  270. !time_after(jiffies, priv->last_blink_time +
  271. msecs_to_jiffies(1000)))
  272. return;
  273. blink_idx = iwl_get_blink_rate(priv);
  274. /* call only if blink rate change */
  275. if (blink_idx != priv->last_blink_rate)
  276. iwl4965_led_pattern(priv, IWL_LED_LINK, blink_idx);
  277. priv->last_blink_time = jiffies;
  278. priv->last_blink_rate = blink_idx;
  279. }
  280. EXPORT_SYMBOL(iwl_leds_background);
  281. /* Register all led handler */
  282. int iwl_leds_register(struct iwl_priv *priv)
  283. {
  284. char *trigger;
  285. char name[32];
  286. int ret;
  287. priv->last_blink_rate = 0;
  288. priv->led_tpt = 0;
  289. priv->last_blink_time = 0;
  290. priv->allow_blinking = 0;
  291. trigger = ieee80211_get_radio_led_name(priv->hw);
  292. snprintf(name, sizeof(name), "iwl-%s:radio",
  293. wiphy_name(priv->hw->wiphy));
  294. priv->led[IWL_LED_TRG_RADIO].led_on = iwl4965_led_on_reg;
  295. priv->led[IWL_LED_TRG_RADIO].led_off = iwl4965_led_off_reg;
  296. priv->led[IWL_LED_TRG_RADIO].led_pattern = NULL;
  297. ret = iwl_leds_register_led(priv, &priv->led[IWL_LED_TRG_RADIO],
  298. IWL_LED_TRG_RADIO, 1, name, trigger);
  299. if (ret)
  300. goto exit_fail;
  301. trigger = ieee80211_get_assoc_led_name(priv->hw);
  302. snprintf(name, sizeof(name), "iwl-%s:assoc",
  303. wiphy_name(priv->hw->wiphy));
  304. ret = iwl_leds_register_led(priv, &priv->led[IWL_LED_TRG_ASSOC],
  305. IWL_LED_TRG_ASSOC, 0, name, trigger);
  306. /* for assoc always turn led on */
  307. priv->led[IWL_LED_TRG_ASSOC].led_on = iwl4965_led_on_reg;
  308. priv->led[IWL_LED_TRG_ASSOC].led_off = iwl4965_led_on_reg;
  309. priv->led[IWL_LED_TRG_ASSOC].led_pattern = NULL;
  310. if (ret)
  311. goto exit_fail;
  312. trigger = ieee80211_get_rx_led_name(priv->hw);
  313. snprintf(name, sizeof(name), "iwl-%s:RX", wiphy_name(priv->hw->wiphy));
  314. ret = iwl_leds_register_led(priv, &priv->led[IWL_LED_TRG_RX],
  315. IWL_LED_TRG_RX, 0, name, trigger);
  316. priv->led[IWL_LED_TRG_RX].led_on = iwl_led_associated;
  317. priv->led[IWL_LED_TRG_RX].led_off = iwl_led_associated;
  318. priv->led[IWL_LED_TRG_RX].led_pattern = iwl4965_led_pattern;
  319. if (ret)
  320. goto exit_fail;
  321. trigger = ieee80211_get_tx_led_name(priv->hw);
  322. snprintf(name, sizeof(name), "iwl-%s:TX", wiphy_name(priv->hw->wiphy));
  323. ret = iwl_leds_register_led(priv, &priv->led[IWL_LED_TRG_TX],
  324. IWL_LED_TRG_TX, 0, name, trigger);
  325. priv->led[IWL_LED_TRG_TX].led_on = iwl_led_associated;
  326. priv->led[IWL_LED_TRG_TX].led_off = iwl_led_associated;
  327. priv->led[IWL_LED_TRG_TX].led_pattern = iwl4965_led_pattern;
  328. if (ret)
  329. goto exit_fail;
  330. return 0;
  331. exit_fail:
  332. iwl_leds_unregister(priv);
  333. return ret;
  334. }
  335. EXPORT_SYMBOL(iwl_leds_register);
  336. /* unregister led class */
  337. static void iwl_leds_unregister_led(struct iwl_led *led, u8 set_led)
  338. {
  339. if (!led->registered)
  340. return;
  341. led_classdev_unregister(&led->led_dev);
  342. if (set_led)
  343. led->led_dev.brightness_set(&led->led_dev, LED_OFF);
  344. led->registered = 0;
  345. }
  346. /* Unregister all led handlers */
  347. void iwl_leds_unregister(struct iwl_priv *priv)
  348. {
  349. iwl_leds_unregister_led(&priv->led[IWL_LED_TRG_ASSOC], 0);
  350. iwl_leds_unregister_led(&priv->led[IWL_LED_TRG_RX], 0);
  351. iwl_leds_unregister_led(&priv->led[IWL_LED_TRG_TX], 0);
  352. iwl_leds_unregister_led(&priv->led[IWL_LED_TRG_RADIO], 1);
  353. }
  354. EXPORT_SYMBOL(iwl_leds_unregister);