blob: 3e13eb86f85d0fcc2f75d5fc956b1464967c0cc3 [file] [log] [blame]
Jiri Bencf0706e82007-05-05 11:45:53 -07001/*
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
9/* just for IFNAMSIZ */
10#include <linux/if.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040012#include <linux/export.h>
Johannes Berg2c8dccc2008-04-08 15:14:40 -040013#include "led.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070014
Fabio Baltierie47f2502013-07-25 12:00:26 +020015#define MAC80211_BLINK_DELAY 50 /* ms */
16
Jiri Bencf0706e82007-05-05 11:45:53 -070017void ieee80211_led_rx(struct ieee80211_local *local)
18{
Fabio Baltierie47f2502013-07-25 12:00:26 +020019 unsigned long led_delay = MAC80211_BLINK_DELAY;
Jiri Bencf0706e82007-05-05 11:45:53 -070020 if (unlikely(!local->rx_led))
21 return;
Fabio Baltierie47f2502013-07-25 12:00:26 +020022 led_trigger_blink_oneshot(local->rx_led, &led_delay, &led_delay, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -070023}
24
Fabio Baltierie47f2502013-07-25 12:00:26 +020025void ieee80211_led_tx(struct ieee80211_local *local)
Jiri Bencf0706e82007-05-05 11:45:53 -070026{
Fabio Baltierie47f2502013-07-25 12:00:26 +020027 unsigned long led_delay = MAC80211_BLINK_DELAY;
Jiri Bencf0706e82007-05-05 11:45:53 -070028 if (unlikely(!local->tx_led))
29 return;
Fabio Baltierie47f2502013-07-25 12:00:26 +020030 led_trigger_blink_oneshot(local->tx_led, &led_delay, &led_delay, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -070031}
32
Michael Buesch47f0c502007-09-27 15:10:44 +020033void 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
Ivo van Doorncdcb0062008-01-07 19:45:24 +010043void 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
Johannes Bergfe67c912010-11-27 20:02:59 +010053void ieee80211_led_names(struct ieee80211_local *local)
54{
55 snprintf(local->rx_led_name, sizeof(local->rx_led_name),
56 "%srx", wiphy_name(local->hw.wiphy));
57 snprintf(local->tx_led_name, sizeof(local->tx_led_name),
58 "%stx", wiphy_name(local->hw.wiphy));
59 snprintf(local->assoc_led_name, sizeof(local->assoc_led_name),
60 "%sassoc", wiphy_name(local->hw.wiphy));
61 snprintf(local->radio_led_name, sizeof(local->radio_led_name),
62 "%sradio", wiphy_name(local->hw.wiphy));
63}
64
Jiri Bencf0706e82007-05-05 11:45:53 -070065void ieee80211_led_init(struct ieee80211_local *local)
66{
67 local->rx_led = kzalloc(sizeof(struct led_trigger), GFP_KERNEL);
Michael Buesch47f0c502007-09-27 15:10:44 +020068 if (local->rx_led) {
Michael Buesch47f0c502007-09-27 15:10:44 +020069 local->rx_led->name = local->rx_led_name;
70 if (led_trigger_register(local->rx_led)) {
71 kfree(local->rx_led);
72 local->rx_led = NULL;
73 }
Jiri Bencf0706e82007-05-05 11:45:53 -070074 }
75
76 local->tx_led = kzalloc(sizeof(struct led_trigger), GFP_KERNEL);
Michael Buesch47f0c502007-09-27 15:10:44 +020077 if (local->tx_led) {
Michael Buesch47f0c502007-09-27 15:10:44 +020078 local->tx_led->name = local->tx_led_name;
79 if (led_trigger_register(local->tx_led)) {
80 kfree(local->tx_led);
81 local->tx_led = NULL;
82 }
83 }
84
85 local->assoc_led = kzalloc(sizeof(struct led_trigger), GFP_KERNEL);
86 if (local->assoc_led) {
Michael Buesch47f0c502007-09-27 15:10:44 +020087 local->assoc_led->name = local->assoc_led_name;
88 if (led_trigger_register(local->assoc_led)) {
89 kfree(local->assoc_led);
90 local->assoc_led = NULL;
91 }
Jiri Bencf0706e82007-05-05 11:45:53 -070092 }
Ivo van Doorncdcb0062008-01-07 19:45:24 +010093
94 local->radio_led = kzalloc(sizeof(struct led_trigger), GFP_KERNEL);
95 if (local->radio_led) {
Ivo van Doorncdcb0062008-01-07 19:45:24 +010096 local->radio_led->name = local->radio_led_name;
97 if (led_trigger_register(local->radio_led)) {
98 kfree(local->radio_led);
99 local->radio_led = NULL;
100 }
101 }
Johannes Berge1e54062010-11-30 08:58:45 +0100102
103 if (local->tpt_led_trigger) {
104 if (led_trigger_register(&local->tpt_led_trigger->trig)) {
105 kfree(local->tpt_led_trigger);
106 local->tpt_led_trigger = NULL;
107 }
108 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700109}
110
111void ieee80211_led_exit(struct ieee80211_local *local)
112{
Ivo van Doorncdcb0062008-01-07 19:45:24 +0100113 if (local->radio_led) {
114 led_trigger_unregister(local->radio_led);
115 kfree(local->radio_led);
116 }
Michael Buesch47f0c502007-09-27 15:10:44 +0200117 if (local->assoc_led) {
118 led_trigger_unregister(local->assoc_led);
119 kfree(local->assoc_led);
120 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700121 if (local->tx_led) {
122 led_trigger_unregister(local->tx_led);
123 kfree(local->tx_led);
124 }
125 if (local->rx_led) {
126 led_trigger_unregister(local->rx_led);
127 kfree(local->rx_led);
128 }
Johannes Berge1e54062010-11-30 08:58:45 +0100129
130 if (local->tpt_led_trigger) {
131 led_trigger_unregister(&local->tpt_led_trigger->trig);
132 kfree(local->tpt_led_trigger);
133 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700134}
135
Johannes Bergf5c4ae02015-04-23 12:09:01 +0200136const char *__ieee80211_get_radio_led_name(struct ieee80211_hw *hw)
Ivo van Doorncdcb0062008-01-07 19:45:24 +0100137{
138 struct ieee80211_local *local = hw_to_local(hw);
139
Johannes Bergfe67c912010-11-27 20:02:59 +0100140 return local->radio_led_name;
Ivo van Doorncdcb0062008-01-07 19:45:24 +0100141}
142EXPORT_SYMBOL(__ieee80211_get_radio_led_name);
143
Johannes Bergf5c4ae02015-04-23 12:09:01 +0200144const char *__ieee80211_get_assoc_led_name(struct ieee80211_hw *hw)
Michael Buesch47f0c502007-09-27 15:10:44 +0200145{
146 struct ieee80211_local *local = hw_to_local(hw);
147
Johannes Bergfe67c912010-11-27 20:02:59 +0100148 return local->assoc_led_name;
Michael Buesch47f0c502007-09-27 15:10:44 +0200149}
150EXPORT_SYMBOL(__ieee80211_get_assoc_led_name);
151
Johannes Bergf5c4ae02015-04-23 12:09:01 +0200152const char *__ieee80211_get_tx_led_name(struct ieee80211_hw *hw)
Jiri Bencf0706e82007-05-05 11:45:53 -0700153{
154 struct ieee80211_local *local = hw_to_local(hw);
155
Johannes Bergfe67c912010-11-27 20:02:59 +0100156 return local->tx_led_name;
Jiri Bencf0706e82007-05-05 11:45:53 -0700157}
158EXPORT_SYMBOL(__ieee80211_get_tx_led_name);
159
Johannes Bergf5c4ae02015-04-23 12:09:01 +0200160const char *__ieee80211_get_rx_led_name(struct ieee80211_hw *hw)
Jiri Bencf0706e82007-05-05 11:45:53 -0700161{
162 struct ieee80211_local *local = hw_to_local(hw);
163
Johannes Bergfe67c912010-11-27 20:02:59 +0100164 return local->rx_led_name;
Jiri Bencf0706e82007-05-05 11:45:53 -0700165}
166EXPORT_SYMBOL(__ieee80211_get_rx_led_name);
Johannes Berge1e54062010-11-30 08:58:45 +0100167
168static unsigned long tpt_trig_traffic(struct ieee80211_local *local,
169 struct tpt_led_trigger *tpt_trig)
170{
171 unsigned long traffic, delta;
172
173 traffic = tpt_trig->tx_bytes + tpt_trig->rx_bytes;
174
175 delta = traffic - tpt_trig->prev_traffic;
176 tpt_trig->prev_traffic = traffic;
177 return DIV_ROUND_UP(delta, 1024 / 8);
178}
179
180static void tpt_trig_timer(unsigned long data)
181{
182 struct ieee80211_local *local = (void *)data;
183 struct tpt_led_trigger *tpt_trig = local->tpt_led_trigger;
184 struct led_classdev *led_cdev;
185 unsigned long on, off, tpt;
186 int i;
187
188 if (!tpt_trig->running)
189 return;
190
191 mod_timer(&tpt_trig->timer, round_jiffies(jiffies + HZ));
192
193 tpt = tpt_trig_traffic(local, tpt_trig);
194
195 /* default to just solid on */
196 on = 1;
197 off = 0;
198
199 for (i = tpt_trig->blink_table_len - 1; i >= 0; i--) {
200 if (tpt_trig->blink_table[i].throughput < 0 ||
201 tpt > tpt_trig->blink_table[i].throughput) {
202 off = tpt_trig->blink_table[i].blink_time / 2;
203 on = tpt_trig->blink_table[i].blink_time - off;
204 break;
205 }
206 }
207
208 read_lock(&tpt_trig->trig.leddev_list_lock);
209 list_for_each_entry(led_cdev, &tpt_trig->trig.led_cdevs, trig_list)
210 led_blink_set(led_cdev, &on, &off);
211 read_unlock(&tpt_trig->trig.leddev_list_lock);
212}
213
Johannes Bergf5c4ae02015-04-23 12:09:01 +0200214const char *
215__ieee80211_create_tpt_led_trigger(struct ieee80211_hw *hw,
216 unsigned int flags,
217 const struct ieee80211_tpt_blink *blink_table,
218 unsigned int blink_table_len)
Johannes Berge1e54062010-11-30 08:58:45 +0100219{
220 struct ieee80211_local *local = hw_to_local(hw);
221 struct tpt_led_trigger *tpt_trig;
222
223 if (WARN_ON(local->tpt_led_trigger))
224 return NULL;
225
226 tpt_trig = kzalloc(sizeof(struct tpt_led_trigger), GFP_KERNEL);
227 if (!tpt_trig)
228 return NULL;
229
230 snprintf(tpt_trig->name, sizeof(tpt_trig->name),
231 "%stpt", wiphy_name(local->hw.wiphy));
232
233 tpt_trig->trig.name = tpt_trig->name;
234
235 tpt_trig->blink_table = blink_table;
236 tpt_trig->blink_table_len = blink_table_len;
Johannes Berg67408c82010-11-30 08:59:23 +0100237 tpt_trig->want = flags;
Johannes Berge1e54062010-11-30 08:58:45 +0100238
239 setup_timer(&tpt_trig->timer, tpt_trig_timer, (unsigned long)local);
240
241 local->tpt_led_trigger = tpt_trig;
242
243 return tpt_trig->name;
244}
245EXPORT_SYMBOL(__ieee80211_create_tpt_led_trigger);
246
Johannes Berg67408c82010-11-30 08:59:23 +0100247static void ieee80211_start_tpt_led_trig(struct ieee80211_local *local)
Johannes Berge1e54062010-11-30 08:58:45 +0100248{
249 struct tpt_led_trigger *tpt_trig = local->tpt_led_trigger;
250
Johannes Berg67408c82010-11-30 08:59:23 +0100251 if (tpt_trig->running)
Johannes Berge1e54062010-11-30 08:58:45 +0100252 return;
253
254 /* reset traffic */
255 tpt_trig_traffic(local, tpt_trig);
256 tpt_trig->running = true;
257
258 tpt_trig_timer((unsigned long)local);
259 mod_timer(&tpt_trig->timer, round_jiffies(jiffies + HZ));
260}
261
Johannes Berg67408c82010-11-30 08:59:23 +0100262static void ieee80211_stop_tpt_led_trig(struct ieee80211_local *local)
Johannes Berge1e54062010-11-30 08:58:45 +0100263{
264 struct tpt_led_trigger *tpt_trig = local->tpt_led_trigger;
265 struct led_classdev *led_cdev;
266
Johannes Berg67408c82010-11-30 08:59:23 +0100267 if (!tpt_trig->running)
Johannes Berge1e54062010-11-30 08:58:45 +0100268 return;
269
270 tpt_trig->running = false;
271 del_timer_sync(&tpt_trig->timer);
272
273 read_lock(&tpt_trig->trig.leddev_list_lock);
274 list_for_each_entry(led_cdev, &tpt_trig->trig.led_cdevs, trig_list)
Shuah Khan19cd67e2012-06-14 04:34:30 +0800275 led_set_brightness(led_cdev, LED_OFF);
Johannes Berge1e54062010-11-30 08:58:45 +0100276 read_unlock(&tpt_trig->trig.leddev_list_lock);
277}
Johannes Berg67408c82010-11-30 08:59:23 +0100278
279void ieee80211_mod_tpt_led_trig(struct ieee80211_local *local,
280 unsigned int types_on, unsigned int types_off)
281{
282 struct tpt_led_trigger *tpt_trig = local->tpt_led_trigger;
283 bool allowed;
284
285 WARN_ON(types_on & types_off);
286
287 if (!tpt_trig)
288 return;
289
290 tpt_trig->active &= ~types_off;
291 tpt_trig->active |= types_on;
292
293 /*
294 * Regardless of wanted state, we shouldn't blink when
295 * the radio is disabled -- this can happen due to some
296 * code ordering issues with __ieee80211_recalc_idle()
297 * being called before the radio is started.
298 */
299 allowed = tpt_trig->active & IEEE80211_TPT_LEDTRIG_FL_RADIO;
300
301 if (!allowed || !(tpt_trig->active & tpt_trig->want))
302 ieee80211_stop_tpt_led_trig(local);
303 else
304 ieee80211_start_tpt_led_trig(local);
305}