iwl-led.c 12 KB

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