iwl-3945-led.c 9.9 KB

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