iwl-3945-led.c 10.0 KB

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