led.c 7.9 KB

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