iwl-led.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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; /* Mb/s */
  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. .flags = CMD_ASYNC,
  86. .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 iwl_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 iwl_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 iwl_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 iwl_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 iwl_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 iwl_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 second Tx/Rx activities
  228. */
  229. static int iwl_get_blink_rate(struct iwl_priv *priv)
  230. {
  231. int i;
  232. /* count both tx and rx traffic to be able to
  233. * handle traffic in either direction
  234. */
  235. u64 current_tpt = priv->tx_stats.data_bytes +
  236. priv->rx_stats.data_bytes;
  237. s64 tpt = current_tpt - priv->led_tpt;
  238. if (tpt < 0) /* wraparound */
  239. tpt = -tpt;
  240. IWL_DEBUG_LED(priv, "tpt %lld current_tpt %llu\n",
  241. (long long)tpt,
  242. (unsigned long long)current_tpt);
  243. priv->led_tpt = current_tpt;
  244. if (!priv->allow_blinking)
  245. i = IWL_MAX_BLINK_TBL;
  246. else
  247. for (i = 0; i < IWL_MAX_BLINK_TBL; i++)
  248. if (tpt > (blink_tbl[i].tpt * IWL_1MB_RATE))
  249. break;
  250. IWL_DEBUG_LED(priv, "LED BLINK IDX=%d\n", i);
  251. return i;
  252. }
  253. /*
  254. * this function called from handler. Since setting Led command can
  255. * happen very frequent we postpone led command to be called from
  256. * REPLY handler so we know ucode is up
  257. */
  258. void iwl_leds_background(struct iwl_priv *priv)
  259. {
  260. u8 blink_idx;
  261. if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
  262. priv->last_blink_time = 0;
  263. return;
  264. }
  265. if (iwl_is_rfkill(priv)) {
  266. priv->last_blink_time = 0;
  267. return;
  268. }
  269. if (!priv->allow_blinking) {
  270. priv->last_blink_time = 0;
  271. if (priv->last_blink_rate != IWL_SOLID_BLINK_IDX) {
  272. priv->last_blink_rate = IWL_SOLID_BLINK_IDX;
  273. iwl_led_pattern(priv, IWL_LED_LINK,
  274. IWL_SOLID_BLINK_IDX);
  275. }
  276. return;
  277. }
  278. if (!priv->last_blink_time ||
  279. !time_after(jiffies, priv->last_blink_time +
  280. msecs_to_jiffies(1000)))
  281. return;
  282. blink_idx = iwl_get_blink_rate(priv);
  283. /* call only if blink rate change */
  284. if (blink_idx != priv->last_blink_rate)
  285. iwl_led_pattern(priv, IWL_LED_LINK, blink_idx);
  286. priv->last_blink_time = jiffies;
  287. priv->last_blink_rate = blink_idx;
  288. }
  289. /* Register all led handler */
  290. int iwl_leds_register(struct iwl_priv *priv)
  291. {
  292. char *trigger;
  293. int ret;
  294. priv->last_blink_rate = 0;
  295. priv->led_tpt = 0;
  296. priv->last_blink_time = 0;
  297. priv->allow_blinking = 0;
  298. trigger = ieee80211_get_radio_led_name(priv->hw);
  299. snprintf(priv->led[IWL_LED_TRG_RADIO].name,
  300. sizeof(priv->led[IWL_LED_TRG_RADIO].name), "iwl-%s::radio",
  301. wiphy_name(priv->hw->wiphy));
  302. priv->led[IWL_LED_TRG_RADIO].led_on = iwl_led_on_reg;
  303. priv->led[IWL_LED_TRG_RADIO].led_off = iwl_led_off_reg;
  304. priv->led[IWL_LED_TRG_RADIO].led_pattern = NULL;
  305. ret = iwl_leds_register_led(priv, &priv->led[IWL_LED_TRG_RADIO],
  306. IWL_LED_TRG_RADIO, 1, trigger);
  307. if (ret)
  308. goto exit_fail;
  309. trigger = ieee80211_get_assoc_led_name(priv->hw);
  310. snprintf(priv->led[IWL_LED_TRG_ASSOC].name,
  311. sizeof(priv->led[IWL_LED_TRG_ASSOC].name), "iwl-%s::assoc",
  312. wiphy_name(priv->hw->wiphy));
  313. ret = iwl_leds_register_led(priv, &priv->led[IWL_LED_TRG_ASSOC],
  314. IWL_LED_TRG_ASSOC, 0, trigger);
  315. /* for assoc always turn led on */
  316. priv->led[IWL_LED_TRG_ASSOC].led_on = iwl_led_associate;
  317. priv->led[IWL_LED_TRG_ASSOC].led_off = iwl_led_disassociate;
  318. priv->led[IWL_LED_TRG_ASSOC].led_pattern = NULL;
  319. if (ret)
  320. goto exit_fail;
  321. trigger = ieee80211_get_rx_led_name(priv->hw);
  322. snprintf(priv->led[IWL_LED_TRG_RX].name,
  323. sizeof(priv->led[IWL_LED_TRG_RX].name), "iwl-%s::RX",
  324. wiphy_name(priv->hw->wiphy));
  325. ret = iwl_leds_register_led(priv, &priv->led[IWL_LED_TRG_RX],
  326. IWL_LED_TRG_RX, 0, trigger);
  327. priv->led[IWL_LED_TRG_RX].led_on = iwl_led_associated;
  328. priv->led[IWL_LED_TRG_RX].led_off = iwl_led_associated;
  329. priv->led[IWL_LED_TRG_RX].led_pattern = iwl_led_pattern;
  330. if (ret)
  331. goto exit_fail;
  332. trigger = ieee80211_get_tx_led_name(priv->hw);
  333. snprintf(priv->led[IWL_LED_TRG_TX].name,
  334. sizeof(priv->led[IWL_LED_TRG_TX].name), "iwl-%s::TX",
  335. wiphy_name(priv->hw->wiphy));
  336. ret = iwl_leds_register_led(priv, &priv->led[IWL_LED_TRG_TX],
  337. IWL_LED_TRG_TX, 0, trigger);
  338. priv->led[IWL_LED_TRG_TX].led_on = iwl_led_associated;
  339. priv->led[IWL_LED_TRG_TX].led_off = iwl_led_associated;
  340. priv->led[IWL_LED_TRG_TX].led_pattern = iwl_led_pattern;
  341. if (ret)
  342. goto exit_fail;
  343. return 0;
  344. exit_fail:
  345. iwl_leds_unregister(priv);
  346. return ret;
  347. }
  348. EXPORT_SYMBOL(iwl_leds_register);
  349. /* unregister led class */
  350. static void iwl_leds_unregister_led(struct iwl_led *led, u8 set_led)
  351. {
  352. if (!led->registered)
  353. return;
  354. led_classdev_unregister(&led->led_dev);
  355. if (set_led)
  356. led->led_dev.brightness_set(&led->led_dev, LED_OFF);
  357. led->registered = 0;
  358. }
  359. /* Unregister all led handlers */
  360. void iwl_leds_unregister(struct iwl_priv *priv)
  361. {
  362. iwl_leds_unregister_led(&priv->led[IWL_LED_TRG_ASSOC], 0);
  363. iwl_leds_unregister_led(&priv->led[IWL_LED_TRG_RX], 0);
  364. iwl_leds_unregister_led(&priv->led[IWL_LED_TRG_TX], 0);
  365. iwl_leds_unregister_led(&priv->led[IWL_LED_TRG_RADIO], 1);
  366. }
  367. EXPORT_SYMBOL(iwl_leds_unregister);