1 /******************************************************************************
3 * Copyright(c) 2003 - 2008 Intel Corporation. All rights reserved.
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.
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
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
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
21 * Contact Information:
22 * Intel Linux Wireless <ilw@linux.intel.com>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
25 *****************************************************************************/
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>
41 #include "iwl-commands.h"
62 {-1, IWL_LED_SOLID
, 0}
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)
70 static int iwl3945_led_cmd_callback(struct iwl_priv
*priv
,
77 static inline int iwl3945_brightness_to_idx(enum led_brightness brightness
)
79 return fls(0x000000FF & (u32
)brightness
);
82 /* Send led command */
83 static int iwl_send_led_cmd(struct iwl_priv
*priv
,
84 struct iwl_led_cmd
*led_cmd
)
86 struct iwl_host_cmd cmd
= {
88 .len
= sizeof(struct iwl_led_cmd
),
90 .meta
.flags
= CMD_ASYNC
,
91 .meta
.u
.callback
= iwl3945_led_cmd_callback
,
94 return iwl3945_send_cmd(priv
, &cmd
);
99 /* Set led on command */
100 static int iwl3945_led_pattern(struct iwl_priv
*priv
, int led_id
,
103 struct iwl_led_cmd led_cmd
= {
105 .interval
= IWL_DEF_LED_INTRVL
108 BUG_ON(idx
> IWL_MAX_BLINK_TBL
);
110 led_cmd
.on
= blink_tbl
[idx
].on_time
;
111 led_cmd
.off
= blink_tbl
[idx
].off_time
;
113 return iwl_send_led_cmd(priv
, &led_cmd
);
117 /* Set led on command */
118 static int iwl3945_led_on(struct iwl_priv
*priv
, int led_id
)
120 struct iwl_led_cmd led_cmd
= {
124 .interval
= IWL_DEF_LED_INTRVL
126 return iwl_send_led_cmd(priv
, &led_cmd
);
129 /* Set led off command */
130 static int iwl3945_led_off(struct iwl_priv
*priv
, int led_id
)
132 struct iwl_led_cmd led_cmd
= {
136 .interval
= IWL_DEF_LED_INTRVL
138 IWL_DEBUG_LED("led off %d\n", led_id
);
139 return iwl_send_led_cmd(priv
, &led_cmd
);
143 * brightness call back function for Tx/Rx LED
145 static int iwl3945_led_associated(struct iwl_priv
*priv
, int led_id
)
147 if (test_bit(STATUS_EXIT_PENDING
, &priv
->status
) ||
148 !test_bit(STATUS_READY
, &priv
->status
))
152 /* start counting Tx/Rx bytes */
153 if (!priv
->last_blink_time
&& priv
->allow_blinking
)
154 priv
->last_blink_time
= jiffies
;
159 * brightness call back for association and radio
161 static void iwl3945_led_brightness_set(struct led_classdev
*led_cdev
,
162 enum led_brightness brightness
)
164 struct iwl3945_led
*led
= container_of(led_cdev
,
165 struct iwl3945_led
, led_dev
);
166 struct iwl_priv
*priv
= led
->priv
;
168 if (test_bit(STATUS_EXIT_PENDING
, &priv
->status
))
171 switch (brightness
) {
173 if (led
->type
== IWL_LED_TRG_ASSOC
) {
174 priv
->allow_blinking
= 1;
175 IWL_DEBUG_LED("MAC is associated\n");
178 led
->led_on(priv
, IWL_LED_LINK
);
181 if (led
->type
== IWL_LED_TRG_ASSOC
) {
182 priv
->allow_blinking
= 0;
183 IWL_DEBUG_LED("MAC is disassociated\n");
186 led
->led_off(priv
, IWL_LED_LINK
);
189 if (led
->led_pattern
) {
190 int idx
= iwl3945_brightness_to_idx(brightness
);
191 led
->led_pattern(priv
, IWL_LED_LINK
, idx
);
200 * Register led class with the system
202 static int iwl3945_led_register_led(struct iwl_priv
*priv
,
203 struct iwl3945_led
*led
,
204 enum led_type type
, u8 set_led
,
207 struct device
*device
= wiphy_dev(priv
->hw
->wiphy
);
210 led
->led_dev
.name
= led
->name
;
211 led
->led_dev
.brightness_set
= iwl3945_led_brightness_set
;
212 led
->led_dev
.default_trigger
= trigger
;
217 ret
= led_classdev_register(device
, &led
->led_dev
);
219 IWL_ERR(priv
, "Error: failed to register led handler.\n");
225 if (set_led
&& led
->led_on
)
226 led
->led_on(priv
, IWL_LED_LINK
);
232 * calculate blink rate according to last 2 sec Tx/Rx activities
234 static inline u8
get_blink_rate(struct iwl_priv
*priv
)
237 u64 current_tpt
= priv
->rxtxpackets
;
238 s64 tpt
= current_tpt
- priv
->led_tpt
;
242 priv
->led_tpt
= current_tpt
;
244 if (!priv
->allow_blinking
)
245 index
= IWL_MAX_BLINK_TBL
;
247 for (index
= 0; index
< IWL_MAX_BLINK_TBL
; index
++)
248 if (tpt
> (blink_tbl
[index
].brightness
* IWL_1MB_RATE
))
253 static inline int is_rf_kill(struct iwl_priv
*priv
)
255 return test_bit(STATUS_RF_KILL_HW
, &priv
->status
) ||
256 test_bit(STATUS_RF_KILL_SW
, &priv
->status
);
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
264 void iwl3945_led_background(struct iwl_priv
*priv
)
268 if (test_bit(STATUS_EXIT_PENDING
, &priv
->status
)) {
269 priv
->last_blink_time
= 0;
272 if (is_rf_kill(priv
)) {
273 priv
->last_blink_time
= 0;
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
);
286 if (!priv
->last_blink_time
||
287 !time_after(jiffies
, priv
->last_blink_time
+
288 msecs_to_jiffies(1000)))
291 blink_idx
= get_blink_rate(priv
);
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
);
297 priv
->last_blink_time
= jiffies
;
298 priv
->last_blink_rate
= blink_idx
;
299 priv
->rxtxpackets
= 0;
303 /* Register all led handler */
304 int iwl3945_led_register(struct iwl_priv
*priv
)
309 priv
->last_blink_rate
= 0;
310 priv
->rxtxpackets
= 0;
312 priv
->last_blink_time
= 0;
313 priv
->allow_blinking
= 0;
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
));
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
;
324 ret
= iwl3945_led_register_led(priv
,
325 &priv
->led39
[IWL_LED_TRG_RADIO
],
326 IWL_LED_TRG_RADIO
, 1, trigger
);
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
));
336 ret
= iwl3945_led_register_led(priv
,
337 &priv
->led39
[IWL_LED_TRG_ASSOC
],
338 IWL_LED_TRG_ASSOC
, 0, trigger
);
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
;
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
));
353 ret
= iwl3945_led_register_led(priv
,
354 &priv
->led39
[IWL_LED_TRG_RX
],
355 IWL_LED_TRG_RX
, 0, trigger
);
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
;
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
));
369 ret
= iwl3945_led_register_led(priv
,
370 &priv
->led39
[IWL_LED_TRG_TX
],
371 IWL_LED_TRG_TX
, 0, trigger
);
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
;
383 iwl3945_led_unregister(priv
);
388 /* unregister led class */
389 static void iwl3945_led_unregister_led(struct iwl3945_led
*led
, u8 set_led
)
391 if (!led
->registered
)
394 led_classdev_unregister(&led
->led_dev
);
397 led
->led_dev
.brightness_set(&led
->led_dev
, LED_OFF
);
401 /* Unregister all led handlers */
402 void iwl3945_led_unregister(struct iwl_priv
*priv
)
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);