iwl-led.c 11 KB

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