mac80211: add pending patches for throughput based led blinking and replace the broke...
[openwrt.git] / package / mac80211 / patches / 562-throughput_trigger.patch
1 Subject: mac80211: add throughput based LED blink trigger
2 From: Johannes Berg <johannes.berg@intel.com>
3
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
9 and max throughput.
10
11 All the drivers need to do is provide an LED class
12 device, best with blink hardware offload.
13
14 Signed-off-by: Johannes Berg <johannes.berg@intel.com>
15 ---
16 v2: turn off LED when turning off radio
17
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 +++++++++++++---
23  net/mac80211/rx.c          |    1 
24  net/mac80211/tx.c          |    1 
25  net/mac80211/util.c        |    2 
26  8 files changed, 216 insertions(+), 9 deletions(-)
27
28 --- a/include/net/mac80211.h
29 +++ b/include/net/mac80211.h
30 @@ -1849,11 +1849,27 @@ struct ieee80211_hw *ieee80211_alloc_hw(
31   */
32  int ieee80211_register_hw(struct ieee80211_hw *hw);
33  
34 +/**
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)
39 + */
40 +struct ieee80211_tpt_blink {
41 +       int throughput;
42 +       int blink_time;
43 +};
44 +
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);
55  #endif
56  /**
57   * ieee80211_get_tx_led_name - get name of TX LED
58 @@ -1932,6 +1948,32 @@ static inline char *ieee80211_get_radio_
59  }
60  
61  /**
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
67 + *
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().
71 + */
72 +static inline char *
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)
77 +{
78 +#ifdef CONFIG_MAC80211_LEDS
79 +       return __ieee80211_create_tpt_led_trigger(hw, update_timeout,
80 +                                                 blink_table,
81 +                                                 blink_table_len);
82 +#else
83 +       return NULL;
84 +#endif
85 +}
86 +
87 +/**
88   * ieee80211_unregister_hw - Unregister a hardware device
89   *
90   * This function instructs mac80211 to free allocated resources
91 --- a/net/mac80211/ieee80211_i.h
92 +++ b/net/mac80211/ieee80211_i.h
93 @@ -23,6 +23,7 @@
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,
103  };
104  
105 +struct tpt_led_trigger {
106 +       struct led_trigger trig;
107 +       char name[32];
108 +       const struct ieee80211_tpt_blink *blink_table;
109 +       unsigned int blink_table_len;
110 +       unsigned long update_timeout;
111 +       struct timer_list timer;
112 +       bool running;
113 +       unsigned long prev_traffic;
114 +       unsigned long tx_bytes, rx_bytes;
115 +};
116 +
117  /**
118   * mac80211 scan flags - currently active scan mode
119   *
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];
127  #endif
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;
132                 }
133         }
134 +
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;
139 +               }
140 +       }
141  }
142  
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);
147         }
148 +
149 +       if (local->tpt_led_trigger) {
150 +               led_trigger_unregister(&local->tpt_led_trigger->trig);
151 +               kfree(local->tpt_led_trigger);
152 +       }
153  }
154  
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;
158  }
159  EXPORT_SYMBOL(__ieee80211_get_rx_led_name);
160 +
161 +static unsigned long tpt_trig_traffic(struct ieee80211_local *local,
162 +                                     struct tpt_led_trigger *tpt_trig)
163 +{
164 +       unsigned long traffic, delta;
165 +
166 +       traffic = tpt_trig->tx_bytes + tpt_trig->rx_bytes;
167 +
168 +       delta = traffic - tpt_trig->prev_traffic;
169 +       tpt_trig->prev_traffic = traffic;
170 +       return delta / (1024 / 8);
171 +}
172 +
173 +static void tpt_trig_timer(unsigned long data)
174 +{
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;
179 +       int i;
180 +
181 +       if (!tpt_trig->running)
182 +               return;
183 +
184 +       mod_timer(&tpt_trig->timer, jiffies + tpt_trig->update_timeout);
185 +
186 +       tpt = tpt_trig_traffic(local, tpt_trig);
187 +
188 +       /* default to just solid on */
189 +       on = 1;
190 +       off = 0;
191 +
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;
196 +                       break;
197 +               }
198 +       }
199 +
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);
204 +}
205 +
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)
211 +{
212 +       struct ieee80211_local *local = hw_to_local(hw);
213 +       struct tpt_led_trigger *tpt_trig;
214 +
215 +       if (WARN_ON(local->tpt_led_trigger))
216 +               return NULL;
217 +
218 +       tpt_trig = kzalloc(sizeof(struct tpt_led_trigger), GFP_KERNEL);
219 +       if (!tpt_trig)
220 +               return NULL;
221 +
222 +       snprintf(tpt_trig->name, sizeof(tpt_trig->name),
223 +                "%stpt", wiphy_name(local->hw.wiphy));
224 +
225 +       tpt_trig->trig.name = tpt_trig->name;
226 +
227 +       tpt_trig->update_timeout = update_timeout;
228 +       tpt_trig->blink_table = blink_table;
229 +       tpt_trig->blink_table_len = blink_table_len;
230 +
231 +       setup_timer(&tpt_trig->timer, tpt_trig_timer, (unsigned long)local);
232 +
233 +       local->tpt_led_trigger = tpt_trig;
234 +
235 +       return tpt_trig->name;
236 +}
237 +EXPORT_SYMBOL(__ieee80211_create_tpt_led_trigger);
238 +
239 +void ieee80211_start_tpt_led_trig(struct ieee80211_local *local)
240 +{
241 +       struct tpt_led_trigger *tpt_trig = local->tpt_led_trigger;
242 +
243 +       if (!tpt_trig)
244 +               return;
245 +
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);
250 +}
251 +
252 +void ieee80211_stop_tpt_led_trig(struct ieee80211_local *local)
253 +{
254 +       struct tpt_led_trigger *tpt_trig = local->tpt_led_trigger;
255 +       struct led_classdev *led_cdev;
256 +
257 +       if (!tpt_trig)
258 +               return;
259 +
260 +       tpt_trig->running = false;
261 +       del_timer_sync(&tpt_trig->timer);
262 +
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);
267 +}
268 --- a/net/mac80211/led.h
269 +++ b/net/mac80211/led.h
270 @@ -12,15 +12,17 @@
271  #include "ieee80211_i.h"
272  
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,
277 -                               bool associated);
278 -extern void ieee80211_led_radio(struct ieee80211_local *local,
279 -                               bool enabled);
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,
286 +                        bool associated);
287 +void ieee80211_led_radio(struct ieee80211_local *local,
288 +                        bool enabled);
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);
294  #else
295  static inline void ieee80211_led_rx(struct ieee80211_local *local)
296  {
297 @@ -45,4 +47,28 @@ static inline void ieee80211_led_init(st
298  static inline void ieee80211_led_exit(struct ieee80211_local *local)
299  {
300  }
301 +static inline void ieee80211_start_tpt_led_trig(struct ieee80211_local *local)
302 +{
303 +}
304 +static inline void ieee80211_stop_tpt_led_trig(struct ieee80211_local *local)
305 +{
306 +}
307 +#endif
308 +
309 +static inline void
310 +ieee80211_tpt_led_trig_tx(struct ieee80211_local *local, int bytes)
311 +{
312 +#ifdef CONFIG_MAC80211_LEDS
313 +       if (local->tpt_led_trigger)
314 +               local->tpt_led_trigger->tx_bytes += bytes;
315  #endif
316 +}
317 +
318 +static inline void
319 +ieee80211_tpt_led_trig_rx(struct ieee80211_local *local, int bytes)
320 +{
321 +#ifdef CONFIG_MAC80211_LEDS
322 +       if (local->tpt_led_trigger)
323 +               local->tpt_led_trigger->rx_bytes += bytes;
324 +#endif
325 +}
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);
333         }
334  
335         /*
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)
340  {
341         ieee80211_led_radio(local, false);
342 +       ieee80211_stop_tpt_led_trig(local);
343  
344         cancel_work_sync(&local->reconfig_filter);
345  
346 @@ -1175,6 +1176,7 @@ int ieee80211_reconfig(struct ieee80211_
347                 }
348  
349                 ieee80211_led_radio(local, true);
350 +               ieee80211_start_tpt_led_trig(local);
351         }
352  
353         /* add interfaces */
354 --- a/net/mac80211/rx.c
355 +++ b/net/mac80211/rx.c
356 @@ -2873,6 +2873,7 @@ void ieee80211_rx(struct ieee80211_hw *h
357                 return;
358         }
359  
360 +       ieee80211_tpt_led_trig_rx(local, skb->len);
361         __ieee80211_rx_handle_packet(hw, skb);
362  
363         rcu_read_unlock();
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;
368                 }
369  
370 +               ieee80211_tpt_led_trig_tx(local, len);
371                 *skbp = skb = next;
372                 ieee80211_led_tx(local, 1);
373                 fragm = true;