1 Subject: mac80211: add throughput based LED blink trigger
2 From: Johannes Berg <johannes.berg@intel.com>
4 iwlwifi and other drivers like to blink their LED
5 based on throughput. Implement this generically in
6 mac80211, based on a throughput table the driver
7 specifies. That way, drivers can set the blink
8 frequencies depending on their desired behaviour
11 All the drivers need to do is provide an LED class
12 device, best with blink hardware offload.
14 Signed-off-by: Johannes Berg <johannes.berg@intel.com>
16 v2: turn off LED when turning off radio
18 include/net/mac80211.h | 42 +++++++++++++++
19 net/mac80211/ieee80211_i.h | 14 +++++
20 net/mac80211/iface.c | 1
21 net/mac80211/led.c | 120 +++++++++++++++++++++++++++++++++++++++++++++
22 net/mac80211/led.h | 44 +++++++++++++---
25 net/mac80211/util.c | 2
26 8 files changed, 216 insertions(+), 9 deletions(-)
28 --- a/include/net/mac80211.h
29 +++ b/include/net/mac80211.h
30 @@ -1849,11 +1849,27 @@ struct ieee80211_hw *ieee80211_alloc_hw(
32 int ieee80211_register_hw(struct ieee80211_hw *hw);
35 + * struct ieee80211_tpt_blink - throughput blink description
36 + * @throughput: throughput in Kbit/sec
37 + * @blink_time: blink time in milliseconds
38 + * (full cycle, ie. one off + one on period)
40 +struct ieee80211_tpt_blink {
45 #ifdef CONFIG_MAC80211_LEDS
46 extern char *__ieee80211_get_tx_led_name(struct ieee80211_hw *hw);
47 extern char *__ieee80211_get_rx_led_name(struct ieee80211_hw *hw);
48 extern char *__ieee80211_get_assoc_led_name(struct ieee80211_hw *hw);
49 extern char *__ieee80211_get_radio_led_name(struct ieee80211_hw *hw);
50 +extern char *__ieee80211_create_tpt_led_trigger(
51 + struct ieee80211_hw *hw,
52 + unsigned long update_timeout,
53 + const struct ieee80211_tpt_blink *blink_table,
54 + unsigned int blink_table_len);
57 * ieee80211_get_tx_led_name - get name of TX LED
58 @@ -1932,6 +1948,32 @@ static inline char *ieee80211_get_radio_
62 + * ieee80211_create_tpt_led_trigger - create throughput LED trigger
63 + * @hw: the hardware to create the trigger for
64 + * @update_timeout: the update timeout (in jiffies)
65 + * @blink_table: the blink table -- needs to be ordered by throughput
66 + * @blink_table_len: size of the blink table
68 + * This function returns %NULL (in case of error, or if no LED
69 + * triggers are configured) or the name of the new trigger.
70 + * This function must be called before ieee80211_register_hw().
73 +ieee80211_create_tpt_led_trigger(struct ieee80211_hw *hw,
74 + unsigned long update_timeout,
75 + const struct ieee80211_tpt_blink *blink_table,
76 + unsigned int blink_table_len)
78 +#ifdef CONFIG_MAC80211_LEDS
79 + return __ieee80211_create_tpt_led_trigger(hw, update_timeout,
88 * ieee80211_unregister_hw - Unregister a hardware device
90 * This function instructs mac80211 to free allocated resources
91 --- a/net/mac80211/ieee80211_i.h
92 +++ b/net/mac80211/ieee80211_i.h
94 #include <linux/types.h>
95 #include <linux/spinlock.h>
96 #include <linux/etherdevice.h>
97 +#include <linux/leds.h>
98 #include <net/ieee80211_radiotap.h>
99 #include <net/cfg80211.h>
100 #include <net/mac80211.h>
101 @@ -636,6 +637,18 @@ enum queue_stop_reason {
102 IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
105 +struct tpt_led_trigger {
106 + struct led_trigger trig;
108 + const struct ieee80211_tpt_blink *blink_table;
109 + unsigned int blink_table_len;
110 + unsigned long update_timeout;
111 + struct timer_list timer;
113 + unsigned long prev_traffic;
114 + unsigned long tx_bytes, rx_bytes;
118 * mac80211 scan flags - currently active scan mode
120 @@ -849,6 +862,7 @@ struct ieee80211_local {
121 #ifdef CONFIG_MAC80211_LEDS
122 int tx_led_counter, rx_led_counter;
123 struct led_trigger *tx_led, *rx_led, *assoc_led, *radio_led;
124 + struct tpt_led_trigger *tpt_led_trigger;
125 char tx_led_name[32], rx_led_name[32],
126 assoc_led_name[32], radio_led_name[32];
128 --- a/net/mac80211/led.c
129 +++ b/net/mac80211/led.c
130 @@ -103,6 +103,13 @@ void ieee80211_led_init(struct ieee80211
131 local->radio_led = NULL;
135 + if (local->tpt_led_trigger) {
136 + if (led_trigger_register(&local->tpt_led_trigger->trig)) {
137 + kfree(local->tpt_led_trigger);
138 + local->tpt_led_trigger = NULL;
143 void ieee80211_led_exit(struct ieee80211_local *local)
144 @@ -123,6 +130,11 @@ void ieee80211_led_exit(struct ieee80211
145 led_trigger_unregister(local->rx_led);
146 kfree(local->rx_led);
149 + if (local->tpt_led_trigger) {
150 + led_trigger_unregister(&local->tpt_led_trigger->trig);
151 + kfree(local->tpt_led_trigger);
155 char *__ieee80211_get_radio_led_name(struct ieee80211_hw *hw)
156 @@ -156,3 +168,111 @@ char *__ieee80211_get_rx_led_name(struct
157 return local->rx_led_name;
159 EXPORT_SYMBOL(__ieee80211_get_rx_led_name);
161 +static unsigned long tpt_trig_traffic(struct ieee80211_local *local,
162 + struct tpt_led_trigger *tpt_trig)
164 + unsigned long traffic, delta;
166 + traffic = tpt_trig->tx_bytes + tpt_trig->rx_bytes;
168 + delta = traffic - tpt_trig->prev_traffic;
169 + tpt_trig->prev_traffic = traffic;
170 + return delta / (1024 / 8);
173 +static void tpt_trig_timer(unsigned long data)
175 + struct ieee80211_local *local = (void *)data;
176 + struct tpt_led_trigger *tpt_trig = local->tpt_led_trigger;
177 + struct led_classdev *led_cdev;
178 + unsigned long on, off, tpt;
181 + if (!tpt_trig->running)
184 + mod_timer(&tpt_trig->timer, jiffies + tpt_trig->update_timeout);
186 + tpt = tpt_trig_traffic(local, tpt_trig);
188 + /* default to just solid on */
192 + for (i = tpt_trig->blink_table_len - 1; i >= 0; i--) {
193 + if (tpt >= tpt_trig->blink_table[i].throughput) {
194 + off = tpt_trig->blink_table[i].blink_time / 2;
195 + on = tpt_trig->blink_table[i].blink_time - off;
200 + read_lock(&tpt_trig->trig.leddev_list_lock);
201 + list_for_each_entry(led_cdev, &tpt_trig->trig.led_cdevs, trig_list)
202 + led_blink_set(led_cdev, &on, &off);
203 + read_unlock(&tpt_trig->trig.leddev_list_lock);
206 +extern char *__ieee80211_create_tpt_led_trigger(
207 + struct ieee80211_hw *hw,
208 + unsigned long update_timeout,
209 + const struct ieee80211_tpt_blink *blink_table,
210 + unsigned int blink_table_len)
212 + struct ieee80211_local *local = hw_to_local(hw);
213 + struct tpt_led_trigger *tpt_trig;
215 + if (WARN_ON(local->tpt_led_trigger))
218 + tpt_trig = kzalloc(sizeof(struct tpt_led_trigger), GFP_KERNEL);
222 + snprintf(tpt_trig->name, sizeof(tpt_trig->name),
223 + "%stpt", wiphy_name(local->hw.wiphy));
225 + tpt_trig->trig.name = tpt_trig->name;
227 + tpt_trig->update_timeout = update_timeout;
228 + tpt_trig->blink_table = blink_table;
229 + tpt_trig->blink_table_len = blink_table_len;
231 + setup_timer(&tpt_trig->timer, tpt_trig_timer, (unsigned long)local);
233 + local->tpt_led_trigger = tpt_trig;
235 + return tpt_trig->name;
237 +EXPORT_SYMBOL(__ieee80211_create_tpt_led_trigger);
239 +void ieee80211_start_tpt_led_trig(struct ieee80211_local *local)
241 + struct tpt_led_trigger *tpt_trig = local->tpt_led_trigger;
246 + /* reset traffic */
247 + tpt_trig_traffic(local, tpt_trig);
248 + tpt_trig->running = true;
249 + mod_timer(&tpt_trig->timer, jiffies + tpt_trig->update_timeout);
252 +void ieee80211_stop_tpt_led_trig(struct ieee80211_local *local)
254 + struct tpt_led_trigger *tpt_trig = local->tpt_led_trigger;
255 + struct led_classdev *led_cdev;
260 + tpt_trig->running = false;
261 + del_timer_sync(&tpt_trig->timer);
263 + read_lock(&tpt_trig->trig.leddev_list_lock);
264 + list_for_each_entry(led_cdev, &tpt_trig->trig.led_cdevs, trig_list)
265 + led_brightness_set(led_cdev, LED_OFF);
266 + read_unlock(&tpt_trig->trig.leddev_list_lock);
268 --- a/net/mac80211/led.h
269 +++ b/net/mac80211/led.h
271 #include "ieee80211_i.h"
273 #ifdef CONFIG_MAC80211_LEDS
274 -extern void ieee80211_led_rx(struct ieee80211_local *local);
275 -extern void ieee80211_led_tx(struct ieee80211_local *local, int q);
276 -extern void ieee80211_led_assoc(struct ieee80211_local *local,
278 -extern void ieee80211_led_radio(struct ieee80211_local *local,
280 -extern void ieee80211_led_names(struct ieee80211_local *local);
281 -extern void ieee80211_led_init(struct ieee80211_local *local);
282 -extern void ieee80211_led_exit(struct ieee80211_local *local);
283 +void ieee80211_led_rx(struct ieee80211_local *local);
284 +void ieee80211_led_tx(struct ieee80211_local *local, int q);
285 +void ieee80211_led_assoc(struct ieee80211_local *local,
287 +void ieee80211_led_radio(struct ieee80211_local *local,
289 +void ieee80211_led_names(struct ieee80211_local *local);
290 +void ieee80211_led_init(struct ieee80211_local *local);
291 +void ieee80211_led_exit(struct ieee80211_local *local);
292 +void ieee80211_start_tpt_led_trig(struct ieee80211_local *local);
293 +void ieee80211_stop_tpt_led_trig(struct ieee80211_local *local);
295 static inline void ieee80211_led_rx(struct ieee80211_local *local)
297 @@ -45,4 +47,28 @@ static inline void ieee80211_led_init(st
298 static inline void ieee80211_led_exit(struct ieee80211_local *local)
301 +static inline void ieee80211_start_tpt_led_trig(struct ieee80211_local *local)
304 +static inline void ieee80211_stop_tpt_led_trig(struct ieee80211_local *local)
310 +ieee80211_tpt_led_trig_tx(struct ieee80211_local *local, int bytes)
312 +#ifdef CONFIG_MAC80211_LEDS
313 + if (local->tpt_led_trigger)
314 + local->tpt_led_trigger->tx_bytes += bytes;
319 +ieee80211_tpt_led_trig_rx(struct ieee80211_local *local, int bytes)
321 +#ifdef CONFIG_MAC80211_LEDS
322 + if (local->tpt_led_trigger)
323 + local->tpt_led_trigger->rx_bytes += bytes;
326 --- a/net/mac80211/iface.c
327 +++ b/net/mac80211/iface.c
328 @@ -225,6 +225,7 @@ static int ieee80211_do_open(struct net_
329 /* we're brought up, everything changes */
330 hw_reconf_flags = ~0;
331 ieee80211_led_radio(local, true);
332 + ieee80211_start_tpt_led_trig(local);
336 --- a/net/mac80211/util.c
337 +++ b/net/mac80211/util.c
338 @@ -1141,6 +1141,7 @@ u32 ieee80211_sta_get_rates(struct ieee8
339 void ieee80211_stop_device(struct ieee80211_local *local)
341 ieee80211_led_radio(local, false);
342 + ieee80211_stop_tpt_led_trig(local);
344 cancel_work_sync(&local->reconfig_filter);
346 @@ -1175,6 +1176,7 @@ int ieee80211_reconfig(struct ieee80211_
349 ieee80211_led_radio(local, true);
350 + ieee80211_start_tpt_led_trig(local);
354 --- a/net/mac80211/rx.c
355 +++ b/net/mac80211/rx.c
356 @@ -2873,6 +2873,7 @@ void ieee80211_rx(struct ieee80211_hw *h
360 + ieee80211_tpt_led_trig_rx(local, skb->len);
361 __ieee80211_rx_handle_packet(hw, skb);
364 --- a/net/mac80211/tx.c
365 +++ b/net/mac80211/tx.c
366 @@ -1344,6 +1344,7 @@ static int __ieee80211_tx(struct ieee802
367 return IEEE80211_TX_AGAIN;
370 + ieee80211_tpt_led_trig_tx(local, len);
372 ieee80211_led_tx(local, 1);