Linux 3.3-rc6
[linux/fpc-iii.git] / net / mac80211 / led.c
blob1bf7903496f8f4ec67ce4e861146f329bd5ad600
1 /*
2 * Copyright 2006, Johannes Berg <johannes@sipsolutions.net>
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 */
9 /* just for IFNAMSIZ */
10 #include <linux/if.h>
11 #include <linux/slab.h>
12 #include <linux/export.h>
13 #include "led.h"
15 void ieee80211_led_rx(struct ieee80211_local *local)
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);
25 /* q is 1 if a packet was enqueued, 0 if it has been transmitted */
26 void ieee80211_led_tx(struct ieee80211_local *local, int q)
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);
38 void ieee80211_led_assoc(struct ieee80211_local *local, bool associated)
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);
48 void ieee80211_led_radio(struct ieee80211_local *local, bool enabled)
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);
58 void ieee80211_led_names(struct ieee80211_local *local)
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));
70 void ieee80211_led_init(struct ieee80211_local *local)
72 local->rx_led = kzalloc(sizeof(struct led_trigger), GFP_KERNEL);
73 if (local->rx_led) {
74 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;
81 local->tx_led = kzalloc(sizeof(struct led_trigger), GFP_KERNEL);
82 if (local->tx_led) {
83 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;
90 local->assoc_led = kzalloc(sizeof(struct led_trigger), GFP_KERNEL);
91 if (local->assoc_led) {
92 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;
99 local->radio_led = kzalloc(sizeof(struct led_trigger), GFP_KERNEL);
100 if (local->radio_led) {
101 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;
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;
116 void ieee80211_led_exit(struct ieee80211_local *local)
118 if (local->radio_led) {
119 led_trigger_unregister(local->radio_led);
120 kfree(local->radio_led);
122 if (local->assoc_led) {
123 led_trigger_unregister(local->assoc_led);
124 kfree(local->assoc_led);
126 if (local->tx_led) {
127 led_trigger_unregister(local->tx_led);
128 kfree(local->tx_led);
130 if (local->rx_led) {
131 led_trigger_unregister(local->rx_led);
132 kfree(local->rx_led);
135 if (local->tpt_led_trigger) {
136 led_trigger_unregister(&local->tpt_led_trigger->trig);
137 kfree(local->tpt_led_trigger);
141 char *__ieee80211_get_radio_led_name(struct ieee80211_hw *hw)
143 struct ieee80211_local *local = hw_to_local(hw);
145 return local->radio_led_name;
147 EXPORT_SYMBOL(__ieee80211_get_radio_led_name);
149 char *__ieee80211_get_assoc_led_name(struct ieee80211_hw *hw)
151 struct ieee80211_local *local = hw_to_local(hw);
153 return local->assoc_led_name;
155 EXPORT_SYMBOL(__ieee80211_get_assoc_led_name);
157 char *__ieee80211_get_tx_led_name(struct ieee80211_hw *hw)
159 struct ieee80211_local *local = hw_to_local(hw);
161 return local->tx_led_name;
163 EXPORT_SYMBOL(__ieee80211_get_tx_led_name);
165 char *__ieee80211_get_rx_led_name(struct ieee80211_hw *hw)
167 struct ieee80211_local *local = hw_to_local(hw);
169 return local->rx_led_name;
171 EXPORT_SYMBOL(__ieee80211_get_rx_led_name);
173 static unsigned long tpt_trig_traffic(struct ieee80211_local *local,
174 struct tpt_led_trigger *tpt_trig)
176 unsigned long traffic, delta;
178 traffic = tpt_trig->tx_bytes + tpt_trig->rx_bytes;
180 delta = traffic - tpt_trig->prev_traffic;
181 tpt_trig->prev_traffic = traffic;
182 return DIV_ROUND_UP(delta, 1024 / 8);
185 static void tpt_trig_timer(unsigned long data)
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;
193 if (!tpt_trig->running)
194 return;
196 mod_timer(&tpt_trig->timer, round_jiffies(jiffies + HZ));
198 tpt = tpt_trig_traffic(local, tpt_trig);
200 /* default to just solid on */
201 on = 1;
202 off = 0;
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;
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);
219 char *__ieee80211_create_tpt_led_trigger(struct ieee80211_hw *hw,
220 unsigned int flags,
221 const struct ieee80211_tpt_blink *blink_table,
222 unsigned int blink_table_len)
224 struct ieee80211_local *local = hw_to_local(hw);
225 struct tpt_led_trigger *tpt_trig;
227 if (WARN_ON(local->tpt_led_trigger))
228 return NULL;
230 tpt_trig = kzalloc(sizeof(struct tpt_led_trigger), GFP_KERNEL);
231 if (!tpt_trig)
232 return NULL;
234 snprintf(tpt_trig->name, sizeof(tpt_trig->name),
235 "%stpt", wiphy_name(local->hw.wiphy));
237 tpt_trig->trig.name = tpt_trig->name;
239 tpt_trig->blink_table = blink_table;
240 tpt_trig->blink_table_len = blink_table_len;
241 tpt_trig->want = flags;
243 setup_timer(&tpt_trig->timer, tpt_trig_timer, (unsigned long)local);
245 local->tpt_led_trigger = tpt_trig;
247 return tpt_trig->name;
249 EXPORT_SYMBOL(__ieee80211_create_tpt_led_trigger);
251 static void ieee80211_start_tpt_led_trig(struct ieee80211_local *local)
253 struct tpt_led_trigger *tpt_trig = local->tpt_led_trigger;
255 if (tpt_trig->running)
256 return;
258 /* reset traffic */
259 tpt_trig_traffic(local, tpt_trig);
260 tpt_trig->running = true;
262 tpt_trig_timer((unsigned long)local);
263 mod_timer(&tpt_trig->timer, round_jiffies(jiffies + HZ));
266 static void ieee80211_stop_tpt_led_trig(struct ieee80211_local *local)
268 struct tpt_led_trigger *tpt_trig = local->tpt_led_trigger;
269 struct led_classdev *led_cdev;
271 if (!tpt_trig->running)
272 return;
274 tpt_trig->running = false;
275 del_timer_sync(&tpt_trig->timer);
277 read_lock(&tpt_trig->trig.leddev_list_lock);
278 list_for_each_entry(led_cdev, &tpt_trig->trig.led_cdevs, trig_list)
279 led_brightness_set(led_cdev, LED_OFF);
280 read_unlock(&tpt_trig->trig.leddev_list_lock);
283 void ieee80211_mod_tpt_led_trig(struct ieee80211_local *local,
284 unsigned int types_on, unsigned int types_off)
286 struct tpt_led_trigger *tpt_trig = local->tpt_led_trigger;
287 bool allowed;
289 WARN_ON(types_on & types_off);
291 if (!tpt_trig)
292 return;
294 tpt_trig->active &= ~types_off;
295 tpt_trig->active |= types_on;
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.
303 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);