iwlwifi: LED use correctly blink table
[pandora-kernel.git] / drivers / net / wireless / iwlwifi / iwl-led.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2003 - 2008 Intel Corporation. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17  *
18  * The full GNU General Public License is included in this distribution in the
19  * file called LICENSE.
20  *
21  * Contact Information:
22  * James P. Ketrenos <ipw2100-admin@linux.intel.com>
23  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24  *
25  *****************************************************************************/
26
27
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/version.h>
31 #include <linux/init.h>
32 #include <linux/pci.h>
33 #include <linux/dma-mapping.h>
34 #include <linux/delay.h>
35 #include <linux/skbuff.h>
36 #include <linux/netdevice.h>
37 #include <linux/wireless.h>
38 #include <net/mac80211.h>
39 #include <linux/etherdevice.h>
40 #include <asm/unaligned.h>
41
42 #include "iwl-dev.h"
43 #include "iwl-core.h"
44 #include "iwl-io.h"
45 #include "iwl-helpers.h"
46
47 #ifdef CONFIG_IWLWIFI_DEBUG
48 static const char *led_type_str[] = {
49         __stringify(IWL_LED_TRG_TX),
50         __stringify(IWL_LED_TRG_RX),
51         __stringify(IWL_LED_TRG_ASSOC),
52         __stringify(IWL_LED_TRG_RADIO),
53         NULL
54 };
55 #endif /* CONFIG_IWLWIFI_DEBUG */
56
57
58 static const struct {
59         u16 tpt;
60         u8 on_time;
61         u8 off_time;
62 } blink_tbl[] =
63 {
64         {300, 25, 25},
65         {200, 40, 40},
66         {100, 55, 55},
67         {70, 65, 65},
68         {50, 75, 75},
69         {20, 85, 85},
70         {15, 95, 95 },
71         {10, 110, 110},
72         {5, 130, 130},
73         {0, 167, 167},
74 /* SOLID_ON */
75         {-1, IWL_LED_SOLID, 0}
76 };
77
78 #define IWL_1MB_RATE (128 * 1024)
79 #define IWL_LED_THRESHOLD (16)
80 #define IWL_MAX_BLINK_TBL (ARRAY_SIZE(blink_tbl) - 1) /* exclude SOLID_ON */
81 #define IWL_SOLID_BLINK_IDX (ARRAY_SIZE(blink_tbl) - 1)
82
83 /*  [0-256] -> [0..8] FIXME: we need [0..10] */
84 static inline int iwl_brightness_to_idx(enum led_brightness brightness)
85 {
86         return fls(0x000000FF & (u32)brightness);
87 }
88
89 /* Send led command */
90 static int iwl_send_led_cmd(struct iwl_priv *priv, struct iwl_led_cmd *led_cmd)
91 {
92         struct iwl_host_cmd cmd = {
93                 .id = REPLY_LEDS_CMD,
94                 .len = sizeof(struct iwl_led_cmd),
95                 .data = led_cmd,
96                 .meta.flags = CMD_ASYNC,
97                 .meta.u.callback = NULL,
98         };
99         u32 reg;
100
101         reg = iwl_read32(priv, CSR_LED_REG);
102         if (reg != (reg & CSR_LED_BSM_CTRL_MSK))
103                 iwl_write32(priv, CSR_LED_REG, reg & CSR_LED_BSM_CTRL_MSK);
104
105         return iwl_send_cmd(priv, &cmd);
106 }
107
108 /* Set led pattern command */
109 static int iwl4965_led_pattern(struct iwl_priv *priv, int led_id, int idx)
110 {
111         struct iwl_led_cmd led_cmd = {
112                 .id = led_id,
113                 .interval = IWL_DEF_LED_INTRVL
114         };
115
116         BUG_ON(idx > IWL_MAX_BLINK_TBL);
117
118         led_cmd.on = blink_tbl[idx].on_time;
119         led_cmd.off = blink_tbl[idx].off_time;
120
121         return iwl_send_led_cmd(priv, &led_cmd);
122 }
123
124 /* Set led register off */
125 static int iwl4965_led_on_reg(struct iwl_priv *priv, int led_id)
126 {
127         IWL_DEBUG_LED("led on %d\n", led_id);
128         iwl_write32(priv, CSR_LED_REG, CSR_LED_REG_TRUN_ON);
129         return 0;
130 }
131
132 #if 0
133 /* Set led on command */
134 static int iwl4965_led_on(struct iwl_priv *priv, int led_id)
135 {
136         struct iwl_led_cmd led_cmd = {
137                 .id = led_id,
138                 .on = IWL_LED_SOLID,
139                 .off = 0,
140                 .interval = IWL_DEF_LED_INTRVL
141         };
142         return iwl_send_led_cmd(priv, &led_cmd);
143 }
144
145 /* Set led off command */
146 int iwl4965_led_off(struct iwl_priv *priv, int led_id)
147 {
148         struct iwl_led_cmd led_cmd = {
149                 .id = led_id,
150                 .on = 0,
151                 .off = 0,
152                 .interval = IWL_DEF_LED_INTRVL
153         };
154         IWL_DEBUG_LED("led off %d\n", led_id);
155         return iwl_send_led_cmd(priv, &led_cmd);
156 }
157 #endif
158
159
160 /* Set led register off */
161 static int iwl4965_led_off_reg(struct iwl_priv *priv, int led_id)
162 {
163         IWL_DEBUG_LED("radio off\n");
164         iwl_write32(priv, CSR_LED_REG, CSR_LED_REG_TRUN_OFF);
165         return 0;
166 }
167
168 /*
169  * brightness call back function for Tx/Rx LED
170  */
171 static int iwl_led_associated(struct iwl_priv *priv, int led_id)
172 {
173         if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
174             !test_bit(STATUS_READY, &priv->status))
175                 return 0;
176
177
178         /* start counting Tx/Rx bytes */
179         if (!priv->last_blink_time && priv->allow_blinking)
180                 priv->last_blink_time = jiffies;
181         return 0;
182 }
183
184 /*
185  * brightness call back for association and radio
186  */
187 static void iwl_led_brightness_set(struct led_classdev *led_cdev,
188                                        enum led_brightness brightness)
189 {
190         struct iwl_led *led = container_of(led_cdev, struct iwl_led, led_dev);
191         struct iwl_priv *priv = led->priv;
192
193         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
194                 return;
195
196
197         IWL_DEBUG_LED("Led type = %s brightness = %d\n",
198                         led_type_str[led->type], brightness);
199         switch (brightness) {
200         case LED_FULL:
201                 if (led->type == IWL_LED_TRG_ASSOC)
202                         priv->allow_blinking = 1;
203
204                 if (led->led_on)
205                         led->led_on(priv, IWL_LED_LINK);
206                 break;
207         case LED_OFF:
208                 if (led->type == IWL_LED_TRG_ASSOC)
209                         priv->allow_blinking = 0;
210
211                 if (led->led_off)
212                         led->led_off(priv, IWL_LED_LINK);
213                 break;
214         default:
215                 if (led->led_pattern) {
216                         int idx = iwl_brightness_to_idx(brightness);
217                         led->led_pattern(priv, IWL_LED_LINK, idx);
218                 }
219                 break;
220         }
221 }
222
223
224
225 /*
226  * Register led class with the system
227  */
228 static int iwl_leds_register_led(struct iwl_priv *priv, struct iwl_led *led,
229                                    enum led_type type, u8 set_led,
230                                    const char *name, char *trigger)
231 {
232         struct device *device = wiphy_dev(priv->hw->wiphy);
233         int ret;
234
235         led->led_dev.name = name;
236         led->led_dev.brightness_set = iwl_led_brightness_set;
237         led->led_dev.default_trigger = trigger;
238
239         led->priv = priv;
240         led->type = type;
241
242         ret = led_classdev_register(device, &led->led_dev);
243         if (ret) {
244                 IWL_ERROR("Error: failed to register led handler.\n");
245                 return ret;
246         }
247
248         led->registered = 1;
249
250         if (set_led && led->led_on)
251                 led->led_on(priv, IWL_LED_LINK);
252
253         return 0;
254 }
255
256
257 /*
258  * calculate blink rate according to last 2 sec Tx/Rx activities
259  */
260 static int iwl_get_blink_rate(struct iwl_priv *priv)
261 {
262         int i;
263         u64 current_tpt = priv->tx_stats[2].bytes;
264         /* FIXME: + priv->rx_stats[2].bytes; */
265         s64 tpt = current_tpt - priv->led_tpt;
266
267         if (tpt < 0) /* wrapparound */
268                 tpt = -tpt;
269
270         IWL_DEBUG_LED("tpt %lld current_tpt %lld\n", tpt, current_tpt);
271         priv->led_tpt = current_tpt;
272
273         if (!priv->allow_blinking)
274                 i = IWL_MAX_BLINK_TBL;
275         else
276                 for (i = 0; i < IWL_MAX_BLINK_TBL; i++)
277                         if (tpt  > (blink_tbl[i].tpt * IWL_1MB_RATE))
278                                 break;
279
280         IWL_DEBUG_LED("LED BLINK IDX=%d", i);
281         return i;
282 }
283
284 static inline int is_rf_kill(struct iwl_priv *priv)
285 {
286         return test_bit(STATUS_RF_KILL_HW, &priv->status) ||
287                 test_bit(STATUS_RF_KILL_SW, &priv->status);
288 }
289
290 /*
291  * this function called from handler. Since setting Led command can
292  * happen very frequent we postpone led command to be called from
293  * REPLY handler so we know ucode is up
294  */
295 void iwl_leds_background(struct iwl_priv *priv)
296 {
297         u8 blink_idx;
298
299         if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
300                 priv->last_blink_time = 0;
301                 return;
302         }
303         if (is_rf_kill(priv)) {
304                 priv->last_blink_time = 0;
305                 return;
306         }
307
308         if (!priv->allow_blinking) {
309                 priv->last_blink_time = 0;
310                 if (priv->last_blink_rate != IWL_SOLID_BLINK_IDX) {
311                         priv->last_blink_rate = IWL_SOLID_BLINK_IDX;
312                         iwl4965_led_pattern(priv, IWL_LED_LINK,
313                                             IWL_SOLID_BLINK_IDX);
314                 }
315                 return;
316         }
317         if (!priv->last_blink_time ||
318             !time_after(jiffies, priv->last_blink_time +
319                         msecs_to_jiffies(1000)))
320                 return;
321
322         blink_idx = iwl_get_blink_rate(priv);
323
324         /* call only if blink rate change */
325         if (blink_idx != priv->last_blink_rate)
326                 iwl4965_led_pattern(priv, IWL_LED_LINK, blink_idx);
327
328         priv->last_blink_time = jiffies;
329         priv->last_blink_rate = blink_idx;
330 }
331 EXPORT_SYMBOL(iwl_leds_background);
332
333 /* Register all led handler */
334 int iwl_leds_register(struct iwl_priv *priv)
335 {
336         char *trigger;
337         char name[32];
338         int ret;
339
340         priv->last_blink_rate = 0;
341         priv->led_tpt = 0;
342         priv->last_blink_time = 0;
343         priv->allow_blinking = 0;
344
345         trigger = ieee80211_get_radio_led_name(priv->hw);
346         snprintf(name, sizeof(name), "iwl-%s:radio",
347                  wiphy_name(priv->hw->wiphy));
348
349         priv->led[IWL_LED_TRG_RADIO].led_on = iwl4965_led_on_reg;
350         priv->led[IWL_LED_TRG_RADIO].led_off = iwl4965_led_off_reg;
351         priv->led[IWL_LED_TRG_RADIO].led_pattern = NULL;
352
353         ret = iwl_leds_register_led(priv, &priv->led[IWL_LED_TRG_RADIO],
354                                    IWL_LED_TRG_RADIO, 1, name, trigger);
355         if (ret)
356                 goto exit_fail;
357
358         trigger = ieee80211_get_assoc_led_name(priv->hw);
359         snprintf(name, sizeof(name), "iwl-%s:assoc",
360                  wiphy_name(priv->hw->wiphy));
361
362         ret = iwl_leds_register_led(priv, &priv->led[IWL_LED_TRG_ASSOC],
363                                    IWL_LED_TRG_ASSOC, 0, name, trigger);
364
365         /* for assoc always turn led on */
366         priv->led[IWL_LED_TRG_ASSOC].led_on = iwl4965_led_on_reg;
367         priv->led[IWL_LED_TRG_ASSOC].led_off = iwl4965_led_on_reg;
368         priv->led[IWL_LED_TRG_ASSOC].led_pattern = NULL;
369
370         if (ret)
371                 goto exit_fail;
372
373         trigger = ieee80211_get_rx_led_name(priv->hw);
374         snprintf(name, sizeof(name), "iwl-%s:RX", wiphy_name(priv->hw->wiphy));
375
376
377         ret = iwl_leds_register_led(priv, &priv->led[IWL_LED_TRG_RX],
378                                    IWL_LED_TRG_RX, 0, name, trigger);
379
380         priv->led[IWL_LED_TRG_RX].led_on = iwl_led_associated;
381         priv->led[IWL_LED_TRG_RX].led_off = iwl_led_associated;
382         priv->led[IWL_LED_TRG_RX].led_pattern = iwl4965_led_pattern;
383
384         if (ret)
385                 goto exit_fail;
386
387         trigger = ieee80211_get_tx_led_name(priv->hw);
388         snprintf(name, sizeof(name), "iwl-%s:TX", wiphy_name(priv->hw->wiphy));
389         ret = iwl_leds_register_led(priv, &priv->led[IWL_LED_TRG_TX],
390                                    IWL_LED_TRG_TX, 0, name, trigger);
391
392         priv->led[IWL_LED_TRG_TX].led_on = iwl_led_associated;
393         priv->led[IWL_LED_TRG_TX].led_off = iwl_led_associated;
394         priv->led[IWL_LED_TRG_TX].led_pattern = iwl4965_led_pattern;
395
396         if (ret)
397                 goto exit_fail;
398
399         return 0;
400
401 exit_fail:
402         iwl_leds_unregister(priv);
403         return ret;
404 }
405 EXPORT_SYMBOL(iwl_leds_register);
406
407 /* unregister led class */
408 static void iwl_leds_unregister_led(struct iwl_led *led, u8 set_led)
409 {
410         if (!led->registered)
411                 return;
412
413         led_classdev_unregister(&led->led_dev);
414
415         if (set_led)
416                 led->led_dev.brightness_set(&led->led_dev, LED_OFF);
417         led->registered = 0;
418 }
419
420 /* Unregister all led handlers */
421 void iwl_leds_unregister(struct iwl_priv *priv)
422 {
423         iwl_leds_unregister_led(&priv->led[IWL_LED_TRG_ASSOC], 0);
424         iwl_leds_unregister_led(&priv->led[IWL_LED_TRG_RX], 0);
425         iwl_leds_unregister_led(&priv->led[IWL_LED_TRG_TX], 0);
426         iwl_leds_unregister_led(&priv->led[IWL_LED_TRG_RADIO], 1);
427 }
428 EXPORT_SYMBOL(iwl_leds_unregister);
429