led.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * Copyright 2006, Johannes Berg <johannes@sipsolutions.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. /* just for IFNAMSIZ */
  9. #include <linux/if.h>
  10. #include <linux/slab.h>
  11. #include "led.h"
  12. void ieee80211_led_rx(struct ieee80211_local *local)
  13. {
  14. if (unlikely(!local->rx_led))
  15. return;
  16. if (local->rx_led_counter++ % 2 == 0)
  17. led_trigger_event(local->rx_led, LED_OFF);
  18. else
  19. led_trigger_event(local->rx_led, LED_FULL);
  20. }
  21. /* q is 1 if a packet was enqueued, 0 if it has been transmitted */
  22. void ieee80211_led_tx(struct ieee80211_local *local, int q)
  23. {
  24. if (unlikely(!local->tx_led))
  25. return;
  26. /* not sure how this is supposed to work ... */
  27. local->tx_led_counter += 2*q-1;
  28. if (local->tx_led_counter % 2 == 0)
  29. led_trigger_event(local->tx_led, LED_OFF);
  30. else
  31. led_trigger_event(local->tx_led, LED_FULL);
  32. }
  33. void ieee80211_led_assoc(struct ieee80211_local *local, bool associated)
  34. {
  35. if (unlikely(!local->assoc_led))
  36. return;
  37. if (associated)
  38. led_trigger_event(local->assoc_led, LED_FULL);
  39. else
  40. led_trigger_event(local->assoc_led, LED_OFF);
  41. }
  42. void ieee80211_led_radio(struct ieee80211_local *local, bool enabled)
  43. {
  44. if (unlikely(!local->radio_led))
  45. return;
  46. if (enabled)
  47. led_trigger_event(local->radio_led, LED_FULL);
  48. else
  49. led_trigger_event(local->radio_led, LED_OFF);
  50. }
  51. void ieee80211_led_names(struct ieee80211_local *local)
  52. {
  53. snprintf(local->rx_led_name, sizeof(local->rx_led_name),
  54. "%srx", wiphy_name(local->hw.wiphy));
  55. snprintf(local->tx_led_name, sizeof(local->tx_led_name),
  56. "%stx", wiphy_name(local->hw.wiphy));
  57. snprintf(local->assoc_led_name, sizeof(local->assoc_led_name),
  58. "%sassoc", wiphy_name(local->hw.wiphy));
  59. snprintf(local->radio_led_name, sizeof(local->radio_led_name),
  60. "%sradio", wiphy_name(local->hw.wiphy));
  61. }
  62. void ieee80211_led_init(struct ieee80211_local *local)
  63. {
  64. local->rx_led = kzalloc(sizeof(struct led_trigger), GFP_KERNEL);
  65. if (local->rx_led) {
  66. local->rx_led->name = local->rx_led_name;
  67. if (led_trigger_register(local->rx_led)) {
  68. kfree(local->rx_led);
  69. local->rx_led = NULL;
  70. }
  71. }
  72. local->tx_led = kzalloc(sizeof(struct led_trigger), GFP_KERNEL);
  73. if (local->tx_led) {
  74. local->tx_led->name = local->tx_led_name;
  75. if (led_trigger_register(local->tx_led)) {
  76. kfree(local->tx_led);
  77. local->tx_led = NULL;
  78. }
  79. }
  80. local->assoc_led = kzalloc(sizeof(struct led_trigger), GFP_KERNEL);
  81. if (local->assoc_led) {
  82. local->assoc_led->name = local->assoc_led_name;
  83. if (led_trigger_register(local->assoc_led)) {
  84. kfree(local->assoc_led);
  85. local->assoc_led = NULL;
  86. }
  87. }
  88. local->radio_led = kzalloc(sizeof(struct led_trigger), GFP_KERNEL);
  89. if (local->radio_led) {
  90. local->radio_led->name = local->radio_led_name;
  91. if (led_trigger_register(local->radio_led)) {
  92. kfree(local->radio_led);
  93. local->radio_led = NULL;
  94. }
  95. }
  96. if (local->tpt_led_trigger) {
  97. if (led_trigger_register(&local->tpt_led_trigger->trig)) {
  98. kfree(local->tpt_led_trigger);
  99. local->tpt_led_trigger = NULL;
  100. }
  101. }
  102. }
  103. void ieee80211_led_exit(struct ieee80211_local *local)
  104. {
  105. if (local->radio_led) {
  106. led_trigger_unregister(local->radio_led);
  107. kfree(local->radio_led);
  108. }
  109. if (local->assoc_led) {
  110. led_trigger_unregister(local->assoc_led);
  111. kfree(local->assoc_led);
  112. }
  113. if (local->tx_led) {
  114. led_trigger_unregister(local->tx_led);
  115. kfree(local->tx_led);
  116. }
  117. if (local->rx_led) {
  118. led_trigger_unregister(local->rx_led);
  119. kfree(local->rx_led);
  120. }
  121. if (local->tpt_led_trigger) {
  122. led_trigger_unregister(&local->tpt_led_trigger->trig);
  123. kfree(local->tpt_led_trigger);
  124. }
  125. }
  126. char *__ieee80211_get_radio_led_name(struct ieee80211_hw *hw)
  127. {
  128. struct ieee80211_local *local = hw_to_local(hw);
  129. return local->radio_led_name;
  130. }
  131. EXPORT_SYMBOL(__ieee80211_get_radio_led_name);
  132. char *__ieee80211_get_assoc_led_name(struct ieee80211_hw *hw)
  133. {
  134. struct ieee80211_local *local = hw_to_local(hw);
  135. return local->assoc_led_name;
  136. }
  137. EXPORT_SYMBOL(__ieee80211_get_assoc_led_name);
  138. char *__ieee80211_get_tx_led_name(struct ieee80211_hw *hw)
  139. {
  140. struct ieee80211_local *local = hw_to_local(hw);
  141. return local->tx_led_name;
  142. }
  143. EXPORT_SYMBOL(__ieee80211_get_tx_led_name);
  144. char *__ieee80211_get_rx_led_name(struct ieee80211_hw *hw)
  145. {
  146. struct ieee80211_local *local = hw_to_local(hw);
  147. return local->rx_led_name;
  148. }
  149. EXPORT_SYMBOL(__ieee80211_get_rx_led_name);
  150. static unsigned long tpt_trig_traffic(struct ieee80211_local *local,
  151. struct tpt_led_trigger *tpt_trig)
  152. {
  153. unsigned long traffic, delta;
  154. traffic = tpt_trig->tx_bytes + tpt_trig->rx_bytes;
  155. delta = traffic - tpt_trig->prev_traffic;
  156. tpt_trig->prev_traffic = traffic;
  157. return DIV_ROUND_UP(delta, 1024 / 8);
  158. }
  159. static void tpt_trig_timer(unsigned long data)
  160. {
  161. struct ieee80211_local *local = (void *)data;
  162. struct tpt_led_trigger *tpt_trig = local->tpt_led_trigger;
  163. struct led_classdev *led_cdev;
  164. unsigned long on, off, tpt;
  165. int i;
  166. if (!tpt_trig->running)
  167. return;
  168. mod_timer(&tpt_trig->timer, round_jiffies(jiffies + HZ));
  169. tpt = tpt_trig_traffic(local, tpt_trig);
  170. /* default to just solid on */
  171. on = 1;
  172. off = 0;
  173. for (i = tpt_trig->blink_table_len - 1; i >= 0; i--) {
  174. if (tpt_trig->blink_table[i].throughput < 0 ||
  175. tpt > tpt_trig->blink_table[i].throughput) {
  176. off = tpt_trig->blink_table[i].blink_time / 2;
  177. on = tpt_trig->blink_table[i].blink_time - off;
  178. break;
  179. }
  180. }
  181. read_lock(&tpt_trig->trig.leddev_list_lock);
  182. list_for_each_entry(led_cdev, &tpt_trig->trig.led_cdevs, trig_list)
  183. led_blink_set(led_cdev, &on, &off);
  184. read_unlock(&tpt_trig->trig.leddev_list_lock);
  185. }
  186. char *__ieee80211_create_tpt_led_trigger(struct ieee80211_hw *hw,
  187. unsigned int flags,
  188. const struct ieee80211_tpt_blink *blink_table,
  189. unsigned int blink_table_len)
  190. {
  191. struct ieee80211_local *local = hw_to_local(hw);
  192. struct tpt_led_trigger *tpt_trig;
  193. if (WARN_ON(local->tpt_led_trigger))
  194. return NULL;
  195. tpt_trig = kzalloc(sizeof(struct tpt_led_trigger), GFP_KERNEL);
  196. if (!tpt_trig)
  197. return NULL;
  198. snprintf(tpt_trig->name, sizeof(tpt_trig->name),
  199. "%stpt", wiphy_name(local->hw.wiphy));
  200. tpt_trig->trig.name = tpt_trig->name;
  201. tpt_trig->blink_table = blink_table;
  202. tpt_trig->blink_table_len = blink_table_len;
  203. tpt_trig->want = flags;
  204. setup_timer(&tpt_trig->timer, tpt_trig_timer, (unsigned long)local);
  205. local->tpt_led_trigger = tpt_trig;
  206. return tpt_trig->name;
  207. }
  208. EXPORT_SYMBOL(__ieee80211_create_tpt_led_trigger);
  209. static void ieee80211_start_tpt_led_trig(struct ieee80211_local *local)
  210. {
  211. struct tpt_led_trigger *tpt_trig = local->tpt_led_trigger;
  212. if (tpt_trig->running)
  213. return;
  214. /* reset traffic */
  215. tpt_trig_traffic(local, tpt_trig);
  216. tpt_trig->running = true;
  217. tpt_trig_timer((unsigned long)local);
  218. mod_timer(&tpt_trig->timer, round_jiffies(jiffies + HZ));
  219. }
  220. static void ieee80211_stop_tpt_led_trig(struct ieee80211_local *local)
  221. {
  222. struct tpt_led_trigger *tpt_trig = local->tpt_led_trigger;
  223. struct led_classdev *led_cdev;
  224. if (!tpt_trig->running)
  225. return;
  226. tpt_trig->running = false;
  227. del_timer_sync(&tpt_trig->timer);
  228. read_lock(&tpt_trig->trig.leddev_list_lock);
  229. list_for_each_entry(led_cdev, &tpt_trig->trig.led_cdevs, trig_list)
  230. led_brightness_set(led_cdev, LED_OFF);
  231. read_unlock(&tpt_trig->trig.leddev_list_lock);
  232. }
  233. void ieee80211_mod_tpt_led_trig(struct ieee80211_local *local,
  234. unsigned int types_on, unsigned int types_off)
  235. {
  236. struct tpt_led_trigger *tpt_trig = local->tpt_led_trigger;
  237. bool allowed;
  238. WARN_ON(types_on & types_off);
  239. if (!tpt_trig)
  240. return;
  241. tpt_trig->active &= ~types_off;
  242. tpt_trig->active |= types_on;
  243. /*
  244. * Regardless of wanted state, we shouldn't blink when
  245. * the radio is disabled -- this can happen due to some
  246. * code ordering issues with __ieee80211_recalc_idle()
  247. * being called before the radio is started.
  248. */
  249. allowed = tpt_trig->active & IEEE80211_TPT_LEDTRIG_FL_RADIO;
  250. if (!allowed || !(tpt_trig->active & tpt_trig->want))
  251. ieee80211_stop_tpt_led_trig(local);
  252. else
  253. ieee80211_start_tpt_led_trig(local);
  254. }