iwl-3945-led.c 10 KB

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