685ba9d6f08255eb7e1747f97f0ce0155bbcb13a
[linux-2.6.git] / drivers / net / wireless / iwlwifi / iwl-led.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2003 - 2009 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  *  Intel Linux Wireless <ilw@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/init.h>
31 #include <linux/pci.h>
32 #include <linux/dma-mapping.h>
33 #include <linux/delay.h>
34 #include <linux/skbuff.h>
35 #include <linux/netdevice.h>
36 #include <linux/wireless.h>
37 #include <net/mac80211.h>
38 #include <linux/etherdevice.h>
39 #include <asm/unaligned.h>
40
41 #include "iwl-dev.h"
42 #include "iwl-core.h"
43 #include "iwl-io.h"
44
45 /* default: IWL_LED_BLINK(0) using blinking index table */
46 static int led_mode;
47 module_param(led_mode, int, S_IRUGO);
48 MODULE_PARM_DESC(led_mode, "led mode: 0=blinking, 1=On(RF On)/Off(RF Off), "
49                            "(default 0)\n");
50
51 #ifdef CONFIG_IWLWIFI_DEBUG
52 static const char *led_type_str[] = {
53         __stringify(IWL_LED_TRG_TX),
54         __stringify(IWL_LED_TRG_RX),
55         __stringify(IWL_LED_TRG_ASSOC),
56         __stringify(IWL_LED_TRG_RADIO),
57         NULL
58 };
59 #endif /* CONFIG_IWLWIFI_DEBUG */
60
61
62 static const struct {
63         u16 tpt;        /* Mb/s */
64         u8 on_time;
65         u8 off_time;
66 } blink_tbl[] =
67 {
68         {300, 25, 25},
69         {200, 40, 40},
70         {100, 55, 55},
71         {70, 65, 65},
72         {50, 75, 75},
73         {20, 85, 85},
74         {10, 95, 95},
75         {5, 110, 110},
76         {1, 130, 130},
77         {0, 167, 167},
78 /* SOLID_ON */
79         {-1, IWL_LED_SOLID, 0}
80 };
81
82 #define IWL_1MB_RATE (128 * 1024)
83 #define IWL_LED_THRESHOLD (16)
84 #define IWL_MAX_BLINK_TBL (ARRAY_SIZE(blink_tbl) - 1) /* exclude SOLID_ON */
85 #define IWL_SOLID_BLINK_IDX (ARRAY_SIZE(blink_tbl) - 1)
86
87 /*
88  * Adjust led blink rate to compensate on a MAC Clock difference on every HW
89  * Led blink rate analysis showed an average deviation of 0% on 3945,
90  * 5% on 4965 HW and 20% on 5000 series and up.
91  * Need to compensate on the led on/off time per HW according to the deviation
92  * to achieve the desired led frequency
93  * The calculation is: (100-averageDeviation)/100 * blinkTime
94  * For code efficiency the calculation will be:
95  *     compensation = (100 - averageDeviation) * 64 / 100
96  *     NewBlinkTime = (compensation * BlinkTime) / 64
97  */
98 static inline u8 iwl_blink_compensation(struct iwl_priv *priv,
99                                     u8 time, u16 compensation)
100 {
101         if (!compensation) {
102                 IWL_ERR(priv, "undefined blink compensation: "
103                         "use pre-defined blinking time\n");
104                 return time;
105         }
106
107         return (u8)((time * compensation) >> 6);
108 }
109
110 /*  [0-256] -> [0..8] FIXME: we need [0..10] */
111 static inline int iwl_brightness_to_idx(enum led_brightness brightness)
112 {
113         return fls(0x000000FF & (u32)brightness);
114 }
115
116 /* Send led command */
117 static int iwl_send_led_cmd(struct iwl_priv *priv, struct iwl_led_cmd *led_cmd)
118 {
119         struct iwl_host_cmd cmd = {
120                 .id = REPLY_LEDS_CMD,
121                 .len = sizeof(struct iwl_led_cmd),
122                 .data = led_cmd,
123                 .flags = CMD_ASYNC,
124                 .callback = NULL,
125         };
126         u32 reg;
127
128         reg = iwl_read32(priv, CSR_LED_REG);
129         if (reg != (reg & CSR_LED_BSM_CTRL_MSK))
130                 iwl_write32(priv, CSR_LED_REG, reg & CSR_LED_BSM_CTRL_MSK);
131
132         return iwl_send_cmd(priv, &cmd);
133 }
134
135 /* Set led pattern command */
136 static int iwl_led_pattern(struct iwl_priv *priv, int led_id,
137                                unsigned int idx)
138 {
139         struct iwl_led_cmd led_cmd = {
140                 .id = led_id,
141                 .interval = IWL_DEF_LED_INTRVL
142         };
143
144         BUG_ON(idx > IWL_MAX_BLINK_TBL);
145
146         IWL_DEBUG_LED(priv, "Led blink time compensation= %u\n",
147                         priv->cfg->led_compensation);
148         led_cmd.on =
149                 iwl_blink_compensation(priv, blink_tbl[idx].on_time,
150                                         priv->cfg->led_compensation);
151         led_cmd.off =
152                 iwl_blink_compensation(priv, blink_tbl[idx].off_time,
153                                         priv->cfg->led_compensation);
154
155         return iwl_send_led_cmd(priv, &led_cmd);
156 }
157
158 /* Set led register off */
159 static int iwl_led_on_reg(struct iwl_priv *priv, int led_id)
160 {
161         IWL_DEBUG_LED(priv, "led on %d\n", led_id);
162         iwl_write32(priv, CSR_LED_REG, CSR_LED_REG_TRUN_ON);
163         return 0;
164 }
165
166 #if 0
167 /* Set led on command */
168 static int iwl_led_on(struct iwl_priv *priv, int led_id)
169 {
170         struct iwl_led_cmd led_cmd = {
171                 .id = led_id,
172                 .on = IWL_LED_SOLID,
173                 .off = 0,
174                 .interval = IWL_DEF_LED_INTRVL
175         };
176         return iwl_send_led_cmd(priv, &led_cmd);
177 }
178
179 /* Set led off command */
180 int iwl_led_off(struct iwl_priv *priv, int led_id)
181 {
182         struct iwl_led_cmd led_cmd = {
183                 .id = led_id,
184                 .on = 0,
185                 .off = 0,
186                 .interval = IWL_DEF_LED_INTRVL
187         };
188         IWL_DEBUG_LED(priv, "led off %d\n", led_id);
189         return iwl_send_led_cmd(priv, &led_cmd);
190 }
191 #endif
192
193
194 /* Set led register off */
195 static int iwl_led_off_reg(struct iwl_priv *priv, int led_id)
196 {
197         IWL_DEBUG_LED(priv, "LED Reg off\n");
198         iwl_write32(priv, CSR_LED_REG, CSR_LED_REG_TRUN_OFF);
199         return 0;
200 }
201
202 /*
203  * Set led register in case of disassociation according to rfkill state
204  */
205 static int iwl_led_associate(struct iwl_priv *priv, int led_id)
206 {
207         IWL_DEBUG_LED(priv, "Associated\n");
208         if (led_mode == IWL_LED_BLINK)
209                 priv->allow_blinking = 1;
210         return iwl_led_on_reg(priv, led_id);
211 }
212 static int iwl_led_disassociate(struct iwl_priv *priv, int led_id)
213 {
214         priv->allow_blinking = 0;
215
216         return 0;
217 }
218
219 /*
220  * brightness call back function for Tx/Rx LED
221  */
222 static int iwl_led_associated(struct iwl_priv *priv, int led_id)
223 {
224         if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
225             !test_bit(STATUS_READY, &priv->status))
226                 return 0;
227
228
229         /* start counting Tx/Rx bytes */
230         if (!priv->last_blink_time && priv->allow_blinking)
231                 priv->last_blink_time = jiffies;
232         return 0;
233 }
234
235 /*
236  * brightness call back for association and radio
237  */
238 static void iwl_led_brightness_set(struct led_classdev *led_cdev,
239                                        enum led_brightness brightness)
240 {
241         struct iwl_led *led = container_of(led_cdev, struct iwl_led, led_dev);
242         struct iwl_priv *priv = led->priv;
243
244         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
245                 return;
246
247
248         IWL_DEBUG_LED(priv, "Led type = %s brightness = %d\n",
249                         led_type_str[led->type], brightness);
250         switch (brightness) {
251         case LED_FULL:
252                 if (led->led_on)
253                         led->led_on(priv, IWL_LED_LINK);
254                 break;
255         case LED_OFF:
256                 if (led->led_off)
257                         led->led_off(priv, IWL_LED_LINK);
258                 break;
259         default:
260                 if (led->led_pattern) {
261                         int idx = iwl_brightness_to_idx(brightness);
262                         led->led_pattern(priv, IWL_LED_LINK, idx);
263                 }
264                 break;
265         }
266 }
267
268
269
270 /*
271  * Register led class with the system
272  */
273 static int iwl_leds_register_led(struct iwl_priv *priv, struct iwl_led *led,
274                                    enum led_type type, u8 set_led,
275                                    char *trigger)
276 {
277         struct device *device = wiphy_dev(priv->hw->wiphy);
278         int ret;
279
280         led->led_dev.name = led->name;
281         led->led_dev.brightness_set = iwl_led_brightness_set;
282         led->led_dev.default_trigger = trigger;
283
284         led->priv = priv;
285         led->type = type;
286
287         ret = led_classdev_register(device, &led->led_dev);
288         if (ret) {
289                 IWL_ERR(priv, "Error: failed to register led handler.\n");
290                 return ret;
291         }
292
293         led->registered = 1;
294
295         if (set_led && led->led_on)
296                 led->led_on(priv, IWL_LED_LINK);
297
298         return 0;
299 }
300
301
302 /*
303  * calculate blink rate according to last second Tx/Rx activities
304  */
305 static int iwl_get_blink_rate(struct iwl_priv *priv)
306 {
307         int i;
308         /* count both tx and rx traffic to be able to
309          * handle traffic in either direction
310          */
311         u64 current_tpt = priv->tx_stats.data_bytes +
312                           priv->rx_stats.data_bytes;
313         s64 tpt = current_tpt - priv->led_tpt;
314
315         if (tpt < 0) /* wraparound */
316                 tpt = -tpt;
317
318         IWL_DEBUG_LED(priv, "tpt %lld current_tpt %llu\n",
319                 (long long)tpt,
320                 (unsigned long long)current_tpt);
321         priv->led_tpt = current_tpt;
322
323         if (!priv->allow_blinking)
324                 i = IWL_MAX_BLINK_TBL;
325         else
326                 for (i = 0; i < IWL_MAX_BLINK_TBL; i++)
327                         if (tpt  > (blink_tbl[i].tpt * IWL_1MB_RATE))
328                                 break;
329
330         IWL_DEBUG_LED(priv, "LED BLINK IDX=%d\n", i);
331         return i;
332 }
333
334 /*
335  * this function called from handler. Since setting Led command can
336  * happen very frequent we postpone led command to be called from
337  * REPLY handler so we know ucode is up
338  */
339 void iwl_leds_background(struct iwl_priv *priv)
340 {
341         u8 blink_idx;
342
343         if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
344                 priv->last_blink_time = 0;
345                 return;
346         }
347         if (iwl_is_rfkill(priv)) {
348                 priv->last_blink_time = 0;
349                 return;
350         }
351
352         if (!priv->allow_blinking) {
353                 priv->last_blink_time = 0;
354                 if (priv->last_blink_rate != IWL_SOLID_BLINK_IDX) {
355                         priv->last_blink_rate = IWL_SOLID_BLINK_IDX;
356                         iwl_led_pattern(priv, IWL_LED_LINK,
357                                             IWL_SOLID_BLINK_IDX);
358                 }
359                 return;
360         }
361         if (!priv->last_blink_time ||
362             !time_after(jiffies, priv->last_blink_time +
363                         msecs_to_jiffies(1000)))
364                 return;
365
366         blink_idx = iwl_get_blink_rate(priv);
367
368         /* call only if blink rate change */
369         if (blink_idx != priv->last_blink_rate)
370                 iwl_led_pattern(priv, IWL_LED_LINK, blink_idx);
371
372         priv->last_blink_time = jiffies;
373         priv->last_blink_rate = blink_idx;
374 }
375
376 /* Register all led handler */
377 int iwl_leds_register(struct iwl_priv *priv)
378 {
379         char *trigger;
380         int ret;
381
382         priv->last_blink_rate = 0;
383         priv->led_tpt = 0;
384         priv->last_blink_time = 0;
385         priv->allow_blinking = 0;
386
387         trigger = ieee80211_get_radio_led_name(priv->hw);
388         snprintf(priv->led[IWL_LED_TRG_RADIO].name,
389                  sizeof(priv->led[IWL_LED_TRG_RADIO].name), "iwl-%s::radio",
390                  wiphy_name(priv->hw->wiphy));
391
392         priv->led[IWL_LED_TRG_RADIO].led_on = iwl_led_on_reg;
393         priv->led[IWL_LED_TRG_RADIO].led_off = iwl_led_off_reg;
394         priv->led[IWL_LED_TRG_RADIO].led_pattern = NULL;
395
396         ret = iwl_leds_register_led(priv, &priv->led[IWL_LED_TRG_RADIO],
397                                    IWL_LED_TRG_RADIO, 1, trigger);
398         if (ret)
399                 goto exit_fail;
400
401         trigger = ieee80211_get_assoc_led_name(priv->hw);
402         snprintf(priv->led[IWL_LED_TRG_ASSOC].name,
403                  sizeof(priv->led[IWL_LED_TRG_ASSOC].name), "iwl-%s::assoc",
404                  wiphy_name(priv->hw->wiphy));
405
406         ret = iwl_leds_register_led(priv, &priv->led[IWL_LED_TRG_ASSOC],
407                                    IWL_LED_TRG_ASSOC, 0, trigger);
408
409         /* for assoc always turn led on */
410         priv->led[IWL_LED_TRG_ASSOC].led_on = iwl_led_associate;
411         priv->led[IWL_LED_TRG_ASSOC].led_off = iwl_led_disassociate;
412         priv->led[IWL_LED_TRG_ASSOC].led_pattern = NULL;
413
414         if (ret)
415                 goto exit_fail;
416
417         trigger = ieee80211_get_rx_led_name(priv->hw);
418         snprintf(priv->led[IWL_LED_TRG_RX].name,
419                  sizeof(priv->led[IWL_LED_TRG_RX].name), "iwl-%s::RX",
420                  wiphy_name(priv->hw->wiphy));
421
422         ret = iwl_leds_register_led(priv, &priv->led[IWL_LED_TRG_RX],
423                                    IWL_LED_TRG_RX, 0, trigger);
424
425         priv->led[IWL_LED_TRG_RX].led_on = iwl_led_associated;
426         priv->led[IWL_LED_TRG_RX].led_off = iwl_led_associated;
427         priv->led[IWL_LED_TRG_RX].led_pattern = iwl_led_pattern;
428
429         if (ret)
430                 goto exit_fail;
431
432         trigger = ieee80211_get_tx_led_name(priv->hw);
433         snprintf(priv->led[IWL_LED_TRG_TX].name,
434                  sizeof(priv->led[IWL_LED_TRG_TX].name), "iwl-%s::TX",
435                  wiphy_name(priv->hw->wiphy));
436
437         ret = iwl_leds_register_led(priv, &priv->led[IWL_LED_TRG_TX],
438                                    IWL_LED_TRG_TX, 0, trigger);
439
440         priv->led[IWL_LED_TRG_TX].led_on = iwl_led_associated;
441         priv->led[IWL_LED_TRG_TX].led_off = iwl_led_associated;
442         priv->led[IWL_LED_TRG_TX].led_pattern = iwl_led_pattern;
443
444         if (ret)
445                 goto exit_fail;
446
447         return 0;
448
449 exit_fail:
450         iwl_leds_unregister(priv);
451         return ret;
452 }
453 EXPORT_SYMBOL(iwl_leds_register);
454
455 /* unregister led class */
456 static void iwl_leds_unregister_led(struct iwl_led *led, u8 set_led)
457 {
458         if (!led->registered)
459                 return;
460
461         led_classdev_unregister(&led->led_dev);
462
463         if (set_led)
464                 led->led_dev.brightness_set(&led->led_dev, LED_OFF);
465         led->registered = 0;
466 }
467
468 /* Unregister all led handlers */
469 void iwl_leds_unregister(struct iwl_priv *priv)
470 {
471         iwl_leds_unregister_led(&priv->led[IWL_LED_TRG_ASSOC], 0);
472         iwl_leds_unregister_led(&priv->led[IWL_LED_TRG_RX], 0);
473         iwl_leds_unregister_led(&priv->led[IWL_LED_TRG_TX], 0);
474         iwl_leds_unregister_led(&priv->led[IWL_LED_TRG_RADIO], 1);
475 }
476 EXPORT_SYMBOL(iwl_leds_unregister);
477