iwl-3945-led.c 10 KB

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