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