iwl-led.c 11 KB

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