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