led.c 10 KB

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