iwl-led.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. if (iwl_is_rfkill(priv))
  159. iwl4965_led_off_reg(priv, led_id);
  160. else
  161. iwl4965_led_on_reg(priv, led_id);
  162. return 0;
  163. }
  164. /*
  165. * brightness call back function for Tx/Rx LED
  166. */
  167. static int iwl_led_associated(struct iwl_priv *priv, int led_id)
  168. {
  169. if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
  170. !test_bit(STATUS_READY, &priv->status))
  171. return 0;
  172. /* start counting Tx/Rx bytes */
  173. if (!priv->last_blink_time && priv->allow_blinking)
  174. priv->last_blink_time = jiffies;
  175. return 0;
  176. }
  177. /*
  178. * brightness call back for association and radio
  179. */
  180. static void iwl_led_brightness_set(struct led_classdev *led_cdev,
  181. enum led_brightness brightness)
  182. {
  183. struct iwl_led *led = container_of(led_cdev, struct iwl_led, led_dev);
  184. struct iwl_priv *priv = led->priv;
  185. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  186. return;
  187. IWL_DEBUG_LED(priv, "Led type = %s brightness = %d\n",
  188. led_type_str[led->type], brightness);
  189. switch (brightness) {
  190. case LED_FULL:
  191. if (led->led_on)
  192. led->led_on(priv, IWL_LED_LINK);
  193. break;
  194. case LED_OFF:
  195. if (led->led_off)
  196. led->led_off(priv, IWL_LED_LINK);
  197. break;
  198. default:
  199. if (led->led_pattern) {
  200. int idx = iwl_brightness_to_idx(brightness);
  201. led->led_pattern(priv, IWL_LED_LINK, idx);
  202. }
  203. break;
  204. }
  205. }
  206. /*
  207. * Register led class with the system
  208. */
  209. static int iwl_leds_register_led(struct iwl_priv *priv, struct iwl_led *led,
  210. enum led_type type, u8 set_led,
  211. char *trigger)
  212. {
  213. struct device *device = wiphy_dev(priv->hw->wiphy);
  214. int ret;
  215. led->led_dev.name = led->name;
  216. led->led_dev.brightness_set = iwl_led_brightness_set;
  217. led->led_dev.default_trigger = trigger;
  218. led->priv = priv;
  219. led->type = type;
  220. ret = led_classdev_register(device, &led->led_dev);
  221. if (ret) {
  222. IWL_ERR(priv, "Error: failed to register led handler.\n");
  223. return ret;
  224. }
  225. led->registered = 1;
  226. if (set_led && led->led_on)
  227. led->led_on(priv, IWL_LED_LINK);
  228. return 0;
  229. }
  230. /*
  231. * calculate blink rate according to last 2 sec Tx/Rx activities
  232. */
  233. static int iwl_get_blink_rate(struct iwl_priv *priv)
  234. {
  235. int i;
  236. u64 current_tpt = priv->tx_stats[2].bytes;
  237. /* FIXME: + priv->rx_stats[2].bytes; */
  238. s64 tpt = current_tpt - priv->led_tpt;
  239. if (tpt < 0) /* wraparound */
  240. tpt = -tpt;
  241. IWL_DEBUG_LED(priv, "tpt %lld current_tpt %llu\n",
  242. (long long)tpt,
  243. (unsigned long long)current_tpt);
  244. priv->led_tpt = current_tpt;
  245. if (!priv->allow_blinking)
  246. i = IWL_MAX_BLINK_TBL;
  247. else
  248. for (i = 0; i < IWL_MAX_BLINK_TBL; i++)
  249. if (tpt > (blink_tbl[i].tpt * IWL_1MB_RATE))
  250. break;
  251. IWL_DEBUG_LED(priv, "LED BLINK IDX=%d\n", i);
  252. return i;
  253. }
  254. /*
  255. * this function called from handler. Since setting Led command can
  256. * happen very frequent we postpone led command to be called from
  257. * REPLY handler so we know ucode is up
  258. */
  259. void iwl_leds_background(struct iwl_priv *priv)
  260. {
  261. u8 blink_idx;
  262. if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
  263. priv->last_blink_time = 0;
  264. return;
  265. }
  266. if (iwl_is_rfkill(priv)) {
  267. priv->last_blink_time = 0;
  268. return;
  269. }
  270. if (!priv->allow_blinking) {
  271. priv->last_blink_time = 0;
  272. if (priv->last_blink_rate != IWL_SOLID_BLINK_IDX) {
  273. priv->last_blink_rate = IWL_SOLID_BLINK_IDX;
  274. iwl4965_led_pattern(priv, IWL_LED_LINK,
  275. IWL_SOLID_BLINK_IDX);
  276. }
  277. return;
  278. }
  279. if (!priv->last_blink_time ||
  280. !time_after(jiffies, priv->last_blink_time +
  281. msecs_to_jiffies(1000)))
  282. return;
  283. blink_idx = iwl_get_blink_rate(priv);
  284. /* call only if blink rate change */
  285. if (blink_idx != priv->last_blink_rate)
  286. iwl4965_led_pattern(priv, IWL_LED_LINK, blink_idx);
  287. priv->last_blink_time = jiffies;
  288. priv->last_blink_rate = blink_idx;
  289. }
  290. EXPORT_SYMBOL(iwl_leds_background);
  291. /* Register all led handler */
  292. int iwl_leds_register(struct iwl_priv *priv)
  293. {
  294. char *trigger;
  295. int ret;
  296. priv->last_blink_rate = 0;
  297. priv->led_tpt = 0;
  298. priv->last_blink_time = 0;
  299. priv->allow_blinking = 0;
  300. trigger = ieee80211_get_radio_led_name(priv->hw);
  301. snprintf(priv->led[IWL_LED_TRG_RADIO].name,
  302. sizeof(priv->led[IWL_LED_TRG_RADIO].name), "iwl-%s::radio",
  303. wiphy_name(priv->hw->wiphy));
  304. priv->led[IWL_LED_TRG_RADIO].led_on = iwl4965_led_on_reg;
  305. priv->led[IWL_LED_TRG_RADIO].led_off = iwl4965_led_off_reg;
  306. priv->led[IWL_LED_TRG_RADIO].led_pattern = NULL;
  307. ret = iwl_leds_register_led(priv, &priv->led[IWL_LED_TRG_RADIO],
  308. IWL_LED_TRG_RADIO, 1, trigger);
  309. if (ret)
  310. goto exit_fail;
  311. trigger = ieee80211_get_assoc_led_name(priv->hw);
  312. snprintf(priv->led[IWL_LED_TRG_ASSOC].name,
  313. sizeof(priv->led[IWL_LED_TRG_ASSOC].name), "iwl-%s::assoc",
  314. wiphy_name(priv->hw->wiphy));
  315. ret = iwl_leds_register_led(priv, &priv->led[IWL_LED_TRG_ASSOC],
  316. IWL_LED_TRG_ASSOC, 0, trigger);
  317. /* for assoc always turn led on */
  318. priv->led[IWL_LED_TRG_ASSOC].led_on = iwl_led_associate;
  319. priv->led[IWL_LED_TRG_ASSOC].led_off = iwl_led_disassociate;
  320. priv->led[IWL_LED_TRG_ASSOC].led_pattern = NULL;
  321. if (ret)
  322. goto exit_fail;
  323. trigger = ieee80211_get_rx_led_name(priv->hw);
  324. snprintf(priv->led[IWL_LED_TRG_RX].name,
  325. sizeof(priv->led[IWL_LED_TRG_RX].name), "iwl-%s::RX",
  326. wiphy_name(priv->hw->wiphy));
  327. ret = iwl_leds_register_led(priv, &priv->led[IWL_LED_TRG_RX],
  328. IWL_LED_TRG_RX, 0, trigger);
  329. priv->led[IWL_LED_TRG_RX].led_on = iwl_led_associated;
  330. priv->led[IWL_LED_TRG_RX].led_off = iwl_led_associated;
  331. priv->led[IWL_LED_TRG_RX].led_pattern = iwl4965_led_pattern;
  332. if (ret)
  333. goto exit_fail;
  334. trigger = ieee80211_get_tx_led_name(priv->hw);
  335. snprintf(priv->led[IWL_LED_TRG_TX].name,
  336. sizeof(priv->led[IWL_LED_TRG_TX].name), "iwl-%s::TX",
  337. wiphy_name(priv->hw->wiphy));
  338. ret = iwl_leds_register_led(priv, &priv->led[IWL_LED_TRG_TX],
  339. IWL_LED_TRG_TX, 0, trigger);
  340. priv->led[IWL_LED_TRG_TX].led_on = iwl_led_associated;
  341. priv->led[IWL_LED_TRG_TX].led_off = iwl_led_associated;
  342. priv->led[IWL_LED_TRG_TX].led_pattern = iwl4965_led_pattern;
  343. if (ret)
  344. goto exit_fail;
  345. return 0;
  346. exit_fail:
  347. iwl_leds_unregister(priv);
  348. return ret;
  349. }
  350. EXPORT_SYMBOL(iwl_leds_register);
  351. /* unregister led class */
  352. static void iwl_leds_unregister_led(struct iwl_led *led, u8 set_led)
  353. {
  354. if (!led->registered)
  355. return;
  356. led_classdev_unregister(&led->led_dev);
  357. if (set_led)
  358. led->led_dev.brightness_set(&led->led_dev, LED_OFF);
  359. led->registered = 0;
  360. }
  361. /* Unregister all led handlers */
  362. void iwl_leds_unregister(struct iwl_priv *priv)
  363. {
  364. iwl_leds_unregister_led(&priv->led[IWL_LED_TRG_ASSOC], 0);
  365. iwl_leds_unregister_led(&priv->led[IWL_LED_TRG_RX], 0);
  366. iwl_leds_unregister_led(&priv->led[IWL_LED_TRG_TX], 0);
  367. iwl_leds_unregister_led(&priv->led[IWL_LED_TRG_RADIO], 1);
  368. }
  369. EXPORT_SYMBOL(iwl_leds_unregister);