blob: bcffa69031298f97ee99f50ade02a6e862c5a8a8 [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
15void ieee80211_led_rx(struct ieee80211_local *local)
16{
17 if (unlikely(!local->rx_led))
18 return;
19 if (local->rx_led_counter++ % 2 == 0)
20 led_trigger_event(local->rx_led, LED_OFF);
21 else
22 led_trigger_event(local->rx_led, LED_FULL);
23}
24
25/* q is 1 if a packet was enqueued, 0 if it has been transmitted */
26void ieee80211_led_tx(struct ieee80211_local *local, int q)
27{
28 if (unlikely(!local->tx_led))
29 return;
30 /* not sure how this is supposed to work ... */
31 local->tx_led_counter += 2*q-1;
32 if (local->tx_led_counter % 2 == 0)
33 led_trigger_event(local->tx_led, LED_OFF);
34 else
35 led_trigger_event(local->tx_led, LED_FULL);
36}
37
Michael Buesch47f0c502007-09-27 15:10:44 +020038void ieee80211_led_assoc(struct ieee80211_local *local, bool associated)
39{
40 if (unlikely(!local->assoc_led))
41 return;
42 if (associated)
43 led_trigger_event(local->assoc_led, LED_FULL);
44 else
45 led_trigger_event(local->assoc_led, LED_OFF);
46}
47
Ivo van Doorncdcb0062008-01-07 19:45:24 +010048void ieee80211_led_radio(struct ieee80211_local *local, bool enabled)
49{
50 if (unlikely(!local->radio_led))
51 return;
52 if (enabled)
53 led_trigger_event(local->radio_led, LED_FULL);
54 else
55 led_trigger_event(local->radio_led, LED_OFF);
56}
57
Johannes Bergfe67c912010-11-27 20:02:59 +010058void ieee80211_led_names(struct ieee80211_local *local)
59{
60 snprintf(local->rx_led_name, sizeof(local->rx_led_name),
61 "%srx", wiphy_name(local->hw.wiphy));
62 snprintf(local->tx_led_name, sizeof(local->tx_led_name),
63 "%stx", wiphy_name(local->hw.wiphy));
64 snprintf(local->assoc_led_name, sizeof(local->assoc_led_name),
65 "%sassoc", wiphy_name(local->hw.wiphy));
66 snprintf(local->radio_led_name, sizeof(local->radio_led_name),
67 "%sradio", wiphy_name(local->hw.wiphy));
68}
69
Jiri Bencf0706e82007-05-05 11:45:53 -070070void ieee80211_led_init(struct ieee80211_local *local)
71{
72 local->rx_led = kzalloc(sizeof(struct led_trigger), GFP_KERNEL);
Michael Buesch47f0c502007-09-27 15:10:44 +020073 if (local->rx_led) {
Michael Buesch47f0c502007-09-27 15:10:44 +020074 local->rx_led->name = local->rx_led_name;
75 if (led_trigger_register(local->rx_led)) {
76 kfree(local->rx_led);
77 local->rx_led = NULL;
78 }
Jiri Bencf0706e82007-05-05 11:45:53 -070079 }
80
81 local->tx_led = kzalloc(sizeof(struct led_trigger), GFP_KERNEL);
Michael Buesch47f0c502007-09-27 15:10:44 +020082 if (local->tx_led) {
Michael Buesch47f0c502007-09-27 15:10:44 +020083 local->tx_led->name = local->tx_led_name;
84 if (led_trigger_register(local->tx_led)) {
85 kfree(local->tx_led);
86 local->tx_led = NULL;
87 }
88 }
89
90 local->assoc_led = kzalloc(sizeof(struct led_trigger), GFP_KERNEL);
91 if (local->assoc_led) {
Michael Buesch47f0c502007-09-27 15:10:44 +020092 local->assoc_led->name = local->assoc_led_name;
93 if (led_trigger_register(local->assoc_led)) {
94 kfree(local->assoc_led);
95 local->assoc_led = NULL;
96 }
Jiri Bencf0706e82007-05-05 11:45:53 -070097 }
Ivo van Doorncdcb0062008-01-07 19:45:24 +010098
99 local->radio_led = kzalloc(sizeof(struct led_trigger), GFP_KERNEL);
100 if (local->radio_led) {
Ivo van Doorncdcb0062008-01-07 19:45:24 +0100101 local->radio_led->name = local->radio_led_name;
102 if (led_trigger_register(local->radio_led)) {
103 kfree(local->radio_led);
104 local->radio_led = NULL;
105 }
106 }
Johannes Berge1e54062010-11-30 08:58:45 +0100107
108 if (local->tpt_led_trigger) {
109 if (led_trigger_register(&local->tpt_led_trigger->trig)) {
110 kfree(local->tpt_led_trigger);
111 local->tpt_led_trigger = NULL;
112 }
113 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700114}
115
116void ieee80211_led_exit(struct ieee80211_local *local)
117{
Ivo van Doorncdcb0062008-01-07 19:45:24 +0100118 if (local->radio_led) {
119 led_trigger_unregister(local->radio_led);
120 kfree(local->radio_led);
121 }
Michael Buesch47f0c502007-09-27 15:10:44 +0200122 if (local->assoc_led) {
123 led_trigger_unregister(local->assoc_led);
124 kfree(local->assoc_led);
125 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700126 if (local->tx_led) {
127 led_trigger_unregister(local->tx_led);
128 kfree(local->tx_led);
129 }
130 if (local->rx_led) {
131 led_trigger_unregister(local->rx_led);
132 kfree(local->rx_led);
133 }
Johannes Berge1e54062010-11-30 08:58:45 +0100134
135 if (local->tpt_led_trigger) {
136 led_trigger_unregister(&local->tpt_led_trigger->trig);
137 kfree(local->tpt_led_trigger);
138 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700139}
140
Ivo van Doorncdcb0062008-01-07 19:45:24 +0100141char *__ieee80211_get_radio_led_name(struct ieee80211_hw *hw)
142{
143 struct ieee80211_local *local = hw_to_local(hw);
144
Johannes Bergfe67c912010-11-27 20:02:59 +0100145 return local->radio_led_name;
Ivo van Doorncdcb0062008-01-07 19:45:24 +0100146}
147EXPORT_SYMBOL(__ieee80211_get_radio_led_name);
148
Michael Buesch47f0c502007-09-27 15:10:44 +0200149char *__ieee80211_get_assoc_led_name(struct ieee80211_hw *hw)
150{
151 struct ieee80211_local *local = hw_to_local(hw);
152
Johannes Bergfe67c912010-11-27 20:02:59 +0100153 return local->assoc_led_name;
Michael Buesch47f0c502007-09-27 15:10:44 +0200154}
155EXPORT_SYMBOL(__ieee80211_get_assoc_led_name);
156
Jiri Bencf0706e82007-05-05 11:45:53 -0700157char *__ieee80211_get_tx_led_name(struct ieee80211_hw *hw)
158{
159 struct ieee80211_local *local = hw_to_local(hw);
160
Johannes Bergfe67c912010-11-27 20:02:59 +0100161 return local->tx_led_name;
Jiri Bencf0706e82007-05-05 11:45:53 -0700162}
163EXPORT_SYMBOL(__ieee80211_get_tx_led_name);
164
165char *__ieee80211_get_rx_led_name(struct ieee80211_hw *hw)
166{
167 struct ieee80211_local *local = hw_to_local(hw);
168
Johannes Bergfe67c912010-11-27 20:02:59 +0100169 return local->rx_led_name;
Jiri Bencf0706e82007-05-05 11:45:53 -0700170}
171EXPORT_SYMBOL(__ieee80211_get_rx_led_name);
Johannes Berge1e54062010-11-30 08:58:45 +0100172
173static unsigned long tpt_trig_traffic(struct ieee80211_local *local,
174 struct tpt_led_trigger *tpt_trig)
175{
176 unsigned long traffic, delta;
177
178 traffic = tpt_trig->tx_bytes + tpt_trig->rx_bytes;
179
180 delta = traffic - tpt_trig->prev_traffic;
181 tpt_trig->prev_traffic = traffic;
182 return DIV_ROUND_UP(delta, 1024 / 8);
183}
184
185static void tpt_trig_timer(unsigned long data)
186{
187 struct ieee80211_local *local = (void *)data;
188 struct tpt_led_trigger *tpt_trig = local->tpt_led_trigger;
189 struct led_classdev *led_cdev;
190 unsigned long on, off, tpt;
191 int i;
192
193 if (!tpt_trig->running)
194 return;
195
196 mod_timer(&tpt_trig->timer, round_jiffies(jiffies + HZ));
197
198 tpt = tpt_trig_traffic(local, tpt_trig);
199
200 /* default to just solid on */
201 on = 1;
202 off = 0;
203
204 for (i = tpt_trig->blink_table_len - 1; i >= 0; i--) {
205 if (tpt_trig->blink_table[i].throughput < 0 ||
206 tpt > tpt_trig->blink_table[i].throughput) {
207 off = tpt_trig->blink_table[i].blink_time / 2;
208 on = tpt_trig->blink_table[i].blink_time - off;
209 break;
210 }
211 }
212
213 read_lock(&tpt_trig->trig.leddev_list_lock);
214 list_for_each_entry(led_cdev, &tpt_trig->trig.led_cdevs, trig_list)
215 led_blink_set(led_cdev, &on, &off);
216 read_unlock(&tpt_trig->trig.leddev_list_lock);
217}
218
Johannes Berg06778b12010-12-22 10:15:52 +0100219char *__ieee80211_create_tpt_led_trigger(struct ieee80211_hw *hw,
220 unsigned int flags,
Johannes Berge1e54062010-11-30 08:58:45 +0100221 const struct ieee80211_tpt_blink *blink_table,
222 unsigned int blink_table_len)
223{
224 struct ieee80211_local *local = hw_to_local(hw);
225 struct tpt_led_trigger *tpt_trig;
226
227 if (WARN_ON(local->tpt_led_trigger))
228 return NULL;
229
230 tpt_trig = kzalloc(sizeof(struct tpt_led_trigger), GFP_KERNEL);
231 if (!tpt_trig)
232 return NULL;
233
234 snprintf(tpt_trig->name, sizeof(tpt_trig->name),
235 "%stpt", wiphy_name(local->hw.wiphy));
236
237 tpt_trig->trig.name = tpt_trig->name;
238
239 tpt_trig->blink_table = blink_table;
240 tpt_trig->blink_table_len = blink_table_len;
Johannes Berg67408c82010-11-30 08:59:23 +0100241 tpt_trig->want = flags;
Johannes Berge1e54062010-11-30 08:58:45 +0100242
243 setup_timer(&tpt_trig->timer, tpt_trig_timer, (unsigned long)local);
244
245 local->tpt_led_trigger = tpt_trig;
246
247 return tpt_trig->name;
248}
249EXPORT_SYMBOL(__ieee80211_create_tpt_led_trigger);
250
Johannes Berg67408c82010-11-30 08:59:23 +0100251static void ieee80211_start_tpt_led_trig(struct ieee80211_local *local)
Johannes Berge1e54062010-11-30 08:58:45 +0100252{
253 struct tpt_led_trigger *tpt_trig = local->tpt_led_trigger;
254
Johannes Berg67408c82010-11-30 08:59:23 +0100255 if (tpt_trig->running)
Johannes Berge1e54062010-11-30 08:58:45 +0100256 return;
257
258 /* reset traffic */
259 tpt_trig_traffic(local, tpt_trig);
260 tpt_trig->running = true;
261
262 tpt_trig_timer((unsigned long)local);
263 mod_timer(&tpt_trig->timer, round_jiffies(jiffies + HZ));
264}
265
Johannes Berg67408c82010-11-30 08:59:23 +0100266static void ieee80211_stop_tpt_led_trig(struct ieee80211_local *local)
Johannes Berge1e54062010-11-30 08:58:45 +0100267{
268 struct tpt_led_trigger *tpt_trig = local->tpt_led_trigger;
269 struct led_classdev *led_cdev;
270
Johannes Berg67408c82010-11-30 08:59:23 +0100271 if (!tpt_trig->running)
Johannes Berge1e54062010-11-30 08:58:45 +0100272 return;
273
274 tpt_trig->running = false;
275 del_timer_sync(&tpt_trig->timer);
276
277 read_lock(&tpt_trig->trig.leddev_list_lock);
278 list_for_each_entry(led_cdev, &tpt_trig->trig.led_cdevs, trig_list)
Shuah Khan19cd67e2012-06-14 04:34:30 +0800279 led_set_brightness(led_cdev, LED_OFF);
Johannes Berge1e54062010-11-30 08:58:45 +0100280 read_unlock(&tpt_trig->trig.leddev_list_lock);
281}
Johannes Berg67408c82010-11-30 08:59:23 +0100282
283void ieee80211_mod_tpt_led_trig(struct ieee80211_local *local,
284 unsigned int types_on, unsigned int types_off)
285{
286 struct tpt_led_trigger *tpt_trig = local->tpt_led_trigger;
287 bool allowed;
288
289 WARN_ON(types_on & types_off);
290
291 if (!tpt_trig)
292 return;
293
294 tpt_trig->active &= ~types_off;
295 tpt_trig->active |= types_on;
296
297 /*
298 * Regardless of wanted state, we shouldn't blink when
299 * the radio is disabled -- this can happen due to some
300 * code ordering issues with __ieee80211_recalc_idle()
301 * being called before the radio is started.
302 */
303 allowed = tpt_trig->active & IEEE80211_TPT_LEDTRIG_FL_RADIO;
304
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}