led.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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_assoc(struct ieee80211_local *local, bool associated)
  14. {
  15. if (!atomic_read(&local->assoc_led_active))
  16. return;
  17. if (associated)
  18. led_trigger_event(&local->assoc_led, LED_FULL);
  19. else
  20. led_trigger_event(&local->assoc_led, LED_OFF);
  21. }
  22. void ieee80211_led_radio(struct ieee80211_local *local, bool enabled)
  23. {
  24. if (!atomic_read(&local->radio_led_active))
  25. return;
  26. if (enabled)
  27. led_trigger_event(&local->radio_led, LED_FULL);
  28. else
  29. led_trigger_event(&local->radio_led, LED_OFF);
  30. }
  31. void ieee80211_alloc_led_names(struct ieee80211_local *local)
  32. {
  33. local->rx_led.name = kasprintf(GFP_KERNEL, "%srx",
  34. wiphy_name(local->hw.wiphy));
  35. local->tx_led.name = kasprintf(GFP_KERNEL, "%stx",
  36. wiphy_name(local->hw.wiphy));
  37. local->assoc_led.name = kasprintf(GFP_KERNEL, "%sassoc",
  38. wiphy_name(local->hw.wiphy));
  39. local->radio_led.name = kasprintf(GFP_KERNEL, "%sradio",
  40. wiphy_name(local->hw.wiphy));
  41. }
  42. void ieee80211_free_led_names(struct ieee80211_local *local)
  43. {
  44. kfree(local->rx_led.name);
  45. kfree(local->tx_led.name);
  46. kfree(local->assoc_led.name);
  47. kfree(local->radio_led.name);
  48. }
  49. static void ieee80211_tx_led_activate(struct led_classdev *led_cdev)
  50. {
  51. struct ieee80211_local *local = container_of(led_cdev->trigger,
  52. struct ieee80211_local,
  53. tx_led);
  54. atomic_inc(&local->tx_led_active);
  55. }
  56. static void ieee80211_tx_led_deactivate(struct led_classdev *led_cdev)
  57. {
  58. struct ieee80211_local *local = container_of(led_cdev->trigger,
  59. struct ieee80211_local,
  60. tx_led);
  61. atomic_dec(&local->tx_led_active);
  62. }
  63. static void ieee80211_rx_led_activate(struct led_classdev *led_cdev)
  64. {
  65. struct ieee80211_local *local = container_of(led_cdev->trigger,
  66. struct ieee80211_local,
  67. rx_led);
  68. atomic_inc(&local->rx_led_active);
  69. }
  70. static void ieee80211_rx_led_deactivate(struct led_classdev *led_cdev)
  71. {
  72. struct ieee80211_local *local = container_of(led_cdev->trigger,
  73. struct ieee80211_local,
  74. rx_led);
  75. atomic_dec(&local->rx_led_active);
  76. }
  77. static void ieee80211_assoc_led_activate(struct led_classdev *led_cdev)
  78. {
  79. struct ieee80211_local *local = container_of(led_cdev->trigger,
  80. struct ieee80211_local,
  81. assoc_led);
  82. atomic_inc(&local->assoc_led_active);
  83. }
  84. static void ieee80211_assoc_led_deactivate(struct led_classdev *led_cdev)
  85. {
  86. struct ieee80211_local *local = container_of(led_cdev->trigger,
  87. struct ieee80211_local,
  88. assoc_led);
  89. atomic_dec(&local->assoc_led_active);
  90. }
  91. static void ieee80211_radio_led_activate(struct led_classdev *led_cdev)
  92. {
  93. struct ieee80211_local *local = container_of(led_cdev->trigger,
  94. struct ieee80211_local,
  95. radio_led);
  96. atomic_inc(&local->radio_led_active);
  97. }
  98. static void ieee80211_radio_led_deactivate(struct led_classdev *led_cdev)
  99. {
  100. struct ieee80211_local *local = container_of(led_cdev->trigger,
  101. struct ieee80211_local,
  102. radio_led);
  103. atomic_dec(&local->radio_led_active);
  104. }
  105. static void ieee80211_tpt_led_activate(struct led_classdev *led_cdev)
  106. {
  107. struct ieee80211_local *local = container_of(led_cdev->trigger,
  108. struct ieee80211_local,
  109. tpt_led);
  110. atomic_inc(&local->tpt_led_active);
  111. }
  112. static void ieee80211_tpt_led_deactivate(struct led_classdev *led_cdev)
  113. {
  114. struct ieee80211_local *local = container_of(led_cdev->trigger,
  115. struct ieee80211_local,
  116. tpt_led);
  117. atomic_dec(&local->tpt_led_active);
  118. }
  119. void ieee80211_led_init(struct ieee80211_local *local)
  120. {
  121. atomic_set(&local->rx_led_active, 0);
  122. local->rx_led.activate = ieee80211_rx_led_activate;
  123. local->rx_led.deactivate = ieee80211_rx_led_deactivate;
  124. if (local->rx_led.name && led_trigger_register(&local->rx_led)) {
  125. kfree(local->rx_led.name);
  126. local->rx_led.name = NULL;
  127. }
  128. atomic_set(&local->tx_led_active, 0);
  129. local->tx_led.activate = ieee80211_tx_led_activate;
  130. local->tx_led.deactivate = ieee80211_tx_led_deactivate;
  131. if (local->tx_led.name && led_trigger_register(&local->tx_led)) {
  132. kfree(local->tx_led.name);
  133. local->tx_led.name = NULL;
  134. }
  135. atomic_set(&local->assoc_led_active, 0);
  136. local->assoc_led.activate = ieee80211_assoc_led_activate;
  137. local->assoc_led.deactivate = ieee80211_assoc_led_deactivate;
  138. if (local->assoc_led.name && led_trigger_register(&local->assoc_led)) {
  139. kfree(local->assoc_led.name);
  140. local->assoc_led.name = NULL;
  141. }
  142. atomic_set(&local->radio_led_active, 0);
  143. local->radio_led.activate = ieee80211_radio_led_activate;
  144. local->radio_led.deactivate = ieee80211_radio_led_deactivate;
  145. if (local->radio_led.name && led_trigger_register(&local->radio_led)) {
  146. kfree(local->radio_led.name);
  147. local->radio_led.name = NULL;
  148. }
  149. atomic_set(&local->tpt_led_active, 0);
  150. if (local->tpt_led_trigger) {
  151. local->tpt_led.activate = ieee80211_tpt_led_activate;
  152. local->tpt_led.deactivate = ieee80211_tpt_led_deactivate;
  153. if (led_trigger_register(&local->tpt_led)) {
  154. kfree(local->tpt_led_trigger);
  155. local->tpt_led_trigger = NULL;
  156. }
  157. }
  158. }
  159. void ieee80211_led_exit(struct ieee80211_local *local)
  160. {
  161. if (local->radio_led.name)
  162. led_trigger_unregister(&local->radio_led);
  163. if (local->assoc_led.name)
  164. led_trigger_unregister(&local->assoc_led);
  165. if (local->tx_led.name)
  166. led_trigger_unregister(&local->tx_led);
  167. if (local->rx_led.name)
  168. led_trigger_unregister(&local->rx_led);
  169. if (local->tpt_led_trigger) {
  170. led_trigger_unregister(&local->tpt_led);
  171. kfree(local->tpt_led_trigger);
  172. }
  173. }
  174. const char *__ieee80211_get_radio_led_name(struct ieee80211_hw *hw)
  175. {
  176. struct ieee80211_local *local = hw_to_local(hw);
  177. return local->radio_led.name;
  178. }
  179. EXPORT_SYMBOL(__ieee80211_get_radio_led_name);
  180. const char *__ieee80211_get_assoc_led_name(struct ieee80211_hw *hw)
  181. {
  182. struct ieee80211_local *local = hw_to_local(hw);
  183. return local->assoc_led.name;
  184. }
  185. EXPORT_SYMBOL(__ieee80211_get_assoc_led_name);
  186. const char *__ieee80211_get_tx_led_name(struct ieee80211_hw *hw)
  187. {
  188. struct ieee80211_local *local = hw_to_local(hw);
  189. return local->tx_led.name;
  190. }
  191. EXPORT_SYMBOL(__ieee80211_get_tx_led_name);
  192. const char *__ieee80211_get_rx_led_name(struct ieee80211_hw *hw)
  193. {
  194. struct ieee80211_local *local = hw_to_local(hw);
  195. return local->rx_led.name;
  196. }
  197. EXPORT_SYMBOL(__ieee80211_get_rx_led_name);
  198. static unsigned long tpt_trig_traffic(struct ieee80211_local *local,
  199. struct tpt_led_trigger *tpt_trig)
  200. {
  201. unsigned long traffic, delta;
  202. traffic = tpt_trig->tx_bytes + tpt_trig->rx_bytes;
  203. delta = traffic - tpt_trig->prev_traffic;
  204. tpt_trig->prev_traffic = traffic;
  205. return DIV_ROUND_UP(delta, 1024 / 8);
  206. }
  207. static void tpt_trig_timer(unsigned long data)
  208. {
  209. struct ieee80211_local *local = (void *)data;
  210. struct tpt_led_trigger *tpt_trig = local->tpt_led_trigger;
  211. struct led_classdev *led_cdev;
  212. unsigned long on, off, tpt;
  213. int i;
  214. if (!tpt_trig->running)
  215. return;
  216. mod_timer(&tpt_trig->timer, round_jiffies(jiffies + HZ));
  217. tpt = tpt_trig_traffic(local, tpt_trig);
  218. /* default to just solid on */
  219. on = 1;
  220. off = 0;
  221. for (i = tpt_trig->blink_table_len - 1; i >= 0; i--) {
  222. if (tpt_trig->blink_table[i].throughput < 0 ||
  223. tpt > tpt_trig->blink_table[i].throughput) {
  224. off = tpt_trig->blink_table[i].blink_time / 2;
  225. on = tpt_trig->blink_table[i].blink_time - off;
  226. break;
  227. }
  228. }
  229. read_lock(&local->tpt_led.leddev_list_lock);
  230. list_for_each_entry(led_cdev, &local->tpt_led.led_cdevs, trig_list)
  231. led_blink_set(led_cdev, &on, &off);
  232. read_unlock(&local->tpt_led.leddev_list_lock);
  233. }
  234. const char *
  235. __ieee80211_create_tpt_led_trigger(struct ieee80211_hw *hw,
  236. unsigned int flags,
  237. const struct ieee80211_tpt_blink *blink_table,
  238. unsigned int blink_table_len)
  239. {
  240. struct ieee80211_local *local = hw_to_local(hw);
  241. struct tpt_led_trigger *tpt_trig;
  242. if (WARN_ON(local->tpt_led_trigger))
  243. return NULL;
  244. tpt_trig = kzalloc(sizeof(struct tpt_led_trigger), GFP_KERNEL);
  245. if (!tpt_trig)
  246. return NULL;
  247. snprintf(tpt_trig->name, sizeof(tpt_trig->name),
  248. "%stpt", wiphy_name(local->hw.wiphy));
  249. local->tpt_led.name = tpt_trig->name;
  250. tpt_trig->blink_table = blink_table;
  251. tpt_trig->blink_table_len = blink_table_len;
  252. tpt_trig->want = flags;
  253. setup_timer(&tpt_trig->timer, tpt_trig_timer, (unsigned long)local);
  254. local->tpt_led_trigger = tpt_trig;
  255. return tpt_trig->name;
  256. }
  257. EXPORT_SYMBOL(__ieee80211_create_tpt_led_trigger);
  258. static void ieee80211_start_tpt_led_trig(struct ieee80211_local *local)
  259. {
  260. struct tpt_led_trigger *tpt_trig = local->tpt_led_trigger;
  261. if (tpt_trig->running)
  262. return;
  263. /* reset traffic */
  264. tpt_trig_traffic(local, tpt_trig);
  265. tpt_trig->running = true;
  266. tpt_trig_timer((unsigned long)local);
  267. mod_timer(&tpt_trig->timer, round_jiffies(jiffies + HZ));
  268. }
  269. static void ieee80211_stop_tpt_led_trig(struct ieee80211_local *local)
  270. {
  271. struct tpt_led_trigger *tpt_trig = local->tpt_led_trigger;
  272. struct led_classdev *led_cdev;
  273. if (!tpt_trig->running)
  274. return;
  275. tpt_trig->running = false;
  276. del_timer_sync(&tpt_trig->timer);
  277. read_lock(&local->tpt_led.leddev_list_lock);
  278. list_for_each_entry(led_cdev, &local->tpt_led.led_cdevs, trig_list)
  279. led_set_brightness(led_cdev, LED_OFF);
  280. read_unlock(&local->tpt_led.leddev_list_lock);
  281. }
  282. void ieee80211_mod_tpt_led_trig(struct ieee80211_local *local,
  283. unsigned int types_on, unsigned int types_off)
  284. {
  285. struct tpt_led_trigger *tpt_trig = local->tpt_led_trigger;
  286. bool allowed;
  287. WARN_ON(types_on & types_off);
  288. if (!tpt_trig)
  289. return;
  290. tpt_trig->active &= ~types_off;
  291. tpt_trig->active |= types_on;
  292. /*
  293. * Regardless of wanted state, we shouldn't blink when
  294. * the radio is disabled -- this can happen due to some
  295. * code ordering issues with __ieee80211_recalc_idle()
  296. * being called before the radio is started.
  297. */
  298. allowed = tpt_trig->active & IEEE80211_TPT_LEDTRIG_FL_RADIO;
  299. if (!allowed || !(tpt_trig->active & tpt_trig->want))
  300. ieee80211_stop_tpt_led_trig(local);
  301. else
  302. ieee80211_start_tpt_led_trig(local);
  303. }