iwl-led.c 11 KB

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