Merge branch 'upstream-jeff' of git://git.kernel.org/pub/scm/linux/kernel/git/romieu...
[linux-2.6] / 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-4965.h"
43 #include "iwl-core.h"
44 #include "iwl-io.h"
45 #include "iwl-helpers.h"
46
47 #define IWL_1MB_RATE (128 * 1024)
48 #define IWL_LED_THRESHOLD (16)
49 #define IWL_MAX_BLINK_TBL (10)
50
51 static const struct {
52         u16 tpt;
53         u8 on_time;
54         u8 of_time;
55 } blink_tbl[] =
56 {
57         {300, 25, 25},
58         {200, 40, 40},
59         {100, 55, 55},
60         {70, 65, 65},
61         {50, 75, 75},
62         {20, 85, 85},
63         {15, 95, 95 },
64         {10, 110, 110},
65         {5, 130, 130},
66         {0, 167, 167}
67 };
68
69 static int iwl_led_cmd_callback(struct iwl_priv *priv,
70                                 struct iwl_cmd *cmd, struct sk_buff *skb)
71 {
72         return 1;
73 }
74
75
76 /* Send led command */
77 static int iwl_send_led_cmd(struct iwl_priv *priv,
78                             struct iwl4965_led_cmd *led_cmd)
79 {
80         struct iwl_host_cmd cmd = {
81                 .id = REPLY_LEDS_CMD,
82                 .len = sizeof(struct iwl4965_led_cmd),
83                 .data = led_cmd,
84                 .meta.flags = CMD_ASYNC,
85                 .meta.u.callback = iwl_led_cmd_callback
86         };
87         u32 reg;
88
89         reg = iwl_read32(priv, CSR_LED_REG);
90         if (reg != (reg & CSR_LED_BSM_CTRL_MSK))
91                 iwl_write32(priv, CSR_LED_REG, reg & CSR_LED_BSM_CTRL_MSK);
92
93         return iwl_send_cmd(priv, &cmd);
94 }
95
96
97 /* Set led on command */
98 static int iwl4965_led_on(struct iwl_priv *priv, int led_id)
99 {
100         struct iwl4965_led_cmd led_cmd = {
101                 .id = led_id,
102                 .on = IWL_LED_SOLID,
103                 .off = 0,
104                 .interval = IWL_DEF_LED_INTRVL
105         };
106         return iwl_send_led_cmd(priv, &led_cmd);
107 }
108
109 /* Set led on command */
110 static int iwl4965_led_pattern(struct iwl_priv *priv, int led_id,
111                                enum led_brightness brightness)
112 {
113         struct iwl4965_led_cmd led_cmd = {
114                 .id = led_id,
115                 .on = brightness,
116                 .off = brightness,
117                 .interval = IWL_DEF_LED_INTRVL
118         };
119         if (brightness == LED_FULL) {
120                 led_cmd.on = IWL_LED_SOLID;
121                 led_cmd.off = 0;
122         }
123         return iwl_send_led_cmd(priv, &led_cmd);
124 }
125
126 /* Set led register off */
127 static int iwl4965_led_on_reg(struct iwl_priv *priv, int led_id)
128 {
129         IWL_DEBUG_LED("led on %d\n", led_id);
130         iwl_write32(priv, CSR_LED_REG, CSR_LED_REG_TRUN_ON);
131         return 0;
132 }
133
134 #if 0
135 /* Set led off command */
136 int iwl4965_led_off(struct iwl_priv *priv, int led_id)
137 {
138         struct iwl4965_led_cmd led_cmd = {
139                 .id = led_id,
140                 .on = 0,
141                 .off = 0,
142                 .interval = IWL_DEF_LED_INTRVL
143         };
144         IWL_DEBUG_LED("led off %d\n", led_id);
145         return iwl_send_led_cmd(priv, &led_cmd);
146 }
147 #endif
148
149
150 /* Set led register off */
151 static int iwl4965_led_off_reg(struct iwl_priv *priv, int led_id)
152 {
153         IWL_DEBUG_LED("radio off\n");
154         iwl_write32(priv, CSR_LED_REG, CSR_LED_REG_TRUN_OFF);
155         return 0;
156 }
157
158 /* Set led blink command */
159 static int iwl4965_led_not_solid(struct iwl_priv *priv, int led_id,
160                                u8 brightness)
161 {
162         struct iwl4965_led_cmd led_cmd = {
163                 .id = led_id,
164                 .on = brightness,
165                 .off = brightness,
166                 .interval = IWL_DEF_LED_INTRVL
167         };
168
169         return iwl_send_led_cmd(priv, &led_cmd);
170 }
171
172
173 /*
174  * brightness call back function for Tx/Rx LED
175  */
176 static int iwl4965_led_associated(struct iwl_priv *priv, int led_id)
177 {
178         if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
179             !test_bit(STATUS_READY, &priv->status))
180                 return 0;
181
182
183         /* start counting Tx/Rx bytes */
184         if (!priv->last_blink_time && priv->allow_blinking)
185                 priv->last_blink_time = jiffies;
186         return 0;
187 }
188
189 /*
190  * brightness call back for association and radio
191  */
192 static void iwl4965_led_brightness_set(struct led_classdev *led_cdev,
193                                        enum led_brightness brightness)
194 {
195         struct iwl4965_led *led = container_of(led_cdev,
196                                                struct iwl4965_led, led_dev);
197         struct iwl_priv *priv = led->priv;
198
199         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
200                 return;
201
202         switch (brightness) {
203         case LED_FULL:
204                 if (led->type == IWL_LED_TRG_ASSOC)
205                         priv->allow_blinking = 1;
206
207                 if (led->led_on)
208                         led->led_on(priv, IWL_LED_LINK);
209                 break;
210         case LED_OFF:
211                 if (led->type == IWL_LED_TRG_ASSOC)
212                         priv->allow_blinking = 0;
213
214                 if (led->led_off)
215                         led->led_off(priv, IWL_LED_LINK);
216                 break;
217         default:
218                 if (led->led_pattern)
219                         led->led_pattern(priv, IWL_LED_LINK, brightness);
220                 break;
221         }
222 }
223
224
225
226 /*
227  * Register led class with the system
228  */
229 static int iwl_leds_register_led(struct iwl_priv *priv,
230                                    struct iwl4965_led *led,
231                                    enum led_type type, u8 set_led,
232                                    const char *name, char *trigger)
233 {
234         struct device *device = wiphy_dev(priv->hw->wiphy);
235         int ret;
236
237         led->led_dev.name = name;
238         led->led_dev.brightness_set = iwl4965_led_brightness_set;
239         led->led_dev.default_trigger = trigger;
240
241         led->priv = priv;
242         led->type = type;
243
244         ret = led_classdev_register(device, &led->led_dev);
245         if (ret) {
246                 IWL_ERROR("Error: failed to register led handler.\n");
247                 return ret;
248         }
249
250         led->registered = 1;
251
252         if (set_led && led->led_on)
253                 led->led_on(priv, IWL_LED_LINK);
254
255         return 0;
256 }
257
258
259 /*
260  * calculate blink rate according to last 2 sec Tx/Rx activities
261  */
262 static inline u8 get_blink_rate(struct iwl_priv *priv)
263 {
264         int i;
265         u8 blink_rate;
266         u64 current_tpt = priv->tx_stats[2].bytes + priv->rx_stats[2].bytes;
267         s64 tpt = current_tpt - priv->led_tpt;
268
269         if (tpt < 0) /* wrapparound */
270                 tpt = -tpt;
271
272         priv->led_tpt = current_tpt;
273
274         if (tpt < IWL_LED_THRESHOLD) {
275                 i = IWL_MAX_BLINK_TBL;
276         } else {
277                 for (i = 0; i < IWL_MAX_BLINK_TBL; i++)
278                         if (tpt  > (blink_tbl[i].tpt * IWL_1MB_RATE))
279                                 break;
280         }
281         /* if 0 frame is transfered */
282         if ((i == IWL_MAX_BLINK_TBL) || !priv->allow_blinking)
283                 blink_rate = IWL_LED_SOLID;
284         else
285                 blink_rate = blink_tbl[i].on_time;
286
287         return blink_rate;
288 }
289
290 static inline int is_rf_kill(struct iwl_priv *priv)
291 {
292         return test_bit(STATUS_RF_KILL_HW, &priv->status) ||
293                 test_bit(STATUS_RF_KILL_SW, &priv->status);
294 }
295
296 /*
297  * this function called from handler. Since setting Led command can
298  * happen very frequent we postpone led command to be called from
299  * REPLY handler so we know ucode is up
300  */
301 void iwl_leds_background(struct iwl_priv *priv)
302 {
303         u8 blink_rate;
304
305         if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
306                 priv->last_blink_time = 0;
307                 return;
308         }
309         if (is_rf_kill(priv)) {
310                 priv->last_blink_time = 0;
311                 return;
312         }
313
314         if (!priv->allow_blinking) {
315                 priv->last_blink_time = 0;
316                 if (priv->last_blink_rate != IWL_LED_SOLID) {
317                         priv->last_blink_rate = IWL_LED_SOLID;
318                         iwl4965_led_on(priv, IWL_LED_LINK);
319                 }
320                 return;
321         }
322         if (!priv->last_blink_time ||
323             !time_after(jiffies, priv->last_blink_time +
324                         msecs_to_jiffies(1000)))
325                 return;
326
327         blink_rate = get_blink_rate(priv);
328
329         /* call only if blink rate change */
330         if (blink_rate != priv->last_blink_rate) {
331                 if (blink_rate != IWL_LED_SOLID) {
332                         priv->last_blink_time = jiffies +
333                                                 msecs_to_jiffies(1000);
334                         iwl4965_led_not_solid(priv, IWL_LED_LINK, blink_rate);
335                 } else {
336                         priv->last_blink_time = 0;
337                         iwl4965_led_on(priv, IWL_LED_LINK);
338                 }
339         }
340
341         priv->last_blink_rate = blink_rate;
342 }
343 EXPORT_SYMBOL(iwl_leds_background);
344
345 /* Register all led handler */
346 int iwl_leds_register(struct iwl_priv *priv)
347 {
348         char *trigger;
349         char name[32];
350         int ret;
351
352         priv->last_blink_rate = 0;
353         priv->led_tpt = 0;
354         priv->last_blink_time = 0;
355         priv->allow_blinking = 0;
356
357         trigger = ieee80211_get_radio_led_name(priv->hw);
358         snprintf(name, sizeof(name), "iwl-%s:radio",
359                  wiphy_name(priv->hw->wiphy));
360
361         priv->led[IWL_LED_TRG_RADIO].led_on = iwl4965_led_on_reg;
362         priv->led[IWL_LED_TRG_RADIO].led_off = iwl4965_led_off_reg;
363         priv->led[IWL_LED_TRG_RADIO].led_pattern = NULL;
364
365         ret = iwl_leds_register_led(priv,
366                                    &priv->led[IWL_LED_TRG_RADIO],
367                                    IWL_LED_TRG_RADIO, 1,
368                                    name, trigger);
369         if (ret)
370                 goto exit_fail;
371
372         trigger = ieee80211_get_assoc_led_name(priv->hw);
373         snprintf(name, sizeof(name), "iwl-%s:assoc",
374                  wiphy_name(priv->hw->wiphy));
375
376         ret = iwl_leds_register_led(priv,
377                                    &priv->led[IWL_LED_TRG_ASSOC],
378                                    IWL_LED_TRG_ASSOC, 0,
379                                    name, trigger);
380         /* for assoc always turn led on */
381         priv->led[IWL_LED_TRG_ASSOC].led_on = iwl4965_led_on_reg;
382         priv->led[IWL_LED_TRG_ASSOC].led_off = iwl4965_led_on_reg;
383         priv->led[IWL_LED_TRG_ASSOC].led_pattern = NULL;
384
385         if (ret)
386                 goto exit_fail;
387
388         trigger = ieee80211_get_rx_led_name(priv->hw);
389         snprintf(name, sizeof(name), "iwl-%s:RX",
390                  wiphy_name(priv->hw->wiphy));
391
392
393         ret = iwl_leds_register_led(priv,
394                                    &priv->led[IWL_LED_TRG_RX],
395                                    IWL_LED_TRG_RX, 0,
396                                    name, trigger);
397
398         priv->led[IWL_LED_TRG_RX].led_on = iwl4965_led_associated;
399         priv->led[IWL_LED_TRG_RX].led_off = iwl4965_led_associated;
400         priv->led[IWL_LED_TRG_RX].led_pattern = iwl4965_led_pattern;
401
402         if (ret)
403                 goto exit_fail;
404
405         trigger = ieee80211_get_tx_led_name(priv->hw);
406         snprintf(name, sizeof(name), "iwl-%s:TX",
407                  wiphy_name(priv->hw->wiphy));
408         ret = iwl_leds_register_led(priv,
409                                    &priv->led[IWL_LED_TRG_TX],
410                                    IWL_LED_TRG_TX, 0,
411                                    name, trigger);
412         priv->led[IWL_LED_TRG_TX].led_on = iwl4965_led_associated;
413         priv->led[IWL_LED_TRG_TX].led_off = iwl4965_led_associated;
414         priv->led[IWL_LED_TRG_TX].led_pattern = iwl4965_led_pattern;
415
416         if (ret)
417                 goto exit_fail;
418
419         return 0;
420
421 exit_fail:
422         iwl_leds_unregister(priv);
423         return ret;
424 }
425 EXPORT_SYMBOL(iwl_leds_register);
426
427 /* unregister led class */
428 static void iwl_leds_unregister_led(struct iwl4965_led *led, u8 set_led)
429 {
430         if (!led->registered)
431                 return;
432
433         led_classdev_unregister(&led->led_dev);
434
435         if (set_led)
436                 led->led_dev.brightness_set(&led->led_dev, LED_OFF);
437         led->registered = 0;
438 }
439
440 /* Unregister all led handlers */
441 void iwl_leds_unregister(struct iwl_priv *priv)
442 {
443         iwl_leds_unregister_led(&priv->led[IWL_LED_TRG_ASSOC], 0);
444         iwl_leds_unregister_led(&priv->led[IWL_LED_TRG_RX], 0);
445         iwl_leds_unregister_led(&priv->led[IWL_LED_TRG_TX], 0);
446         iwl_leds_unregister_led(&priv->led[IWL_LED_TRG_RADIO], 1);
447 }
448 EXPORT_SYMBOL(iwl_leds_unregister);
449