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 * James P. Ketrenos <ipw2100-admin@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>
44 #include "iwl-helpers.h"
46 #ifdef CONFIG_IWLWIFI_DEBUG
47 static const char *led_type_str
[] = {
48 __stringify(IWL_LED_TRG_TX
),
49 __stringify(IWL_LED_TRG_RX
),
50 __stringify(IWL_LED_TRG_ASSOC
),
51 __stringify(IWL_LED_TRG_RADIO
),
54 #endif /* CONFIG_IWLWIFI_DEBUG */
74 {-1, IWL_LED_SOLID
, 0}
77 #define IWL_1MB_RATE (128 * 1024)
78 #define IWL_LED_THRESHOLD (16)
79 #define IWL_MAX_BLINK_TBL (ARRAY_SIZE(blink_tbl) - 1) /* exclude SOLID_ON */
80 #define IWL_SOLID_BLINK_IDX (ARRAY_SIZE(blink_tbl) - 1)
82 /* [0-256] -> [0..8] FIXME: we need [0..10] */
83 static inline int iwl_brightness_to_idx(enum led_brightness brightness
)
85 return fls(0x000000FF & (u32
)brightness
);
88 /* Send led command */
89 static int iwl_send_led_cmd(struct iwl_priv
*priv
, struct iwl_led_cmd
*led_cmd
)
91 struct iwl_host_cmd cmd
= {
93 .len
= sizeof(struct iwl_led_cmd
),
95 .meta
.flags
= CMD_ASYNC
,
96 .meta
.u
.callback
= NULL
,
100 reg
= iwl_read32(priv
, CSR_LED_REG
);
101 if (reg
!= (reg
& CSR_LED_BSM_CTRL_MSK
))
102 iwl_write32(priv
, CSR_LED_REG
, reg
& CSR_LED_BSM_CTRL_MSK
);
104 return iwl_send_cmd(priv
, &cmd
);
107 /* Set led pattern command */
108 static int iwl4965_led_pattern(struct iwl_priv
*priv
, int led_id
,
111 struct iwl_led_cmd led_cmd
= {
113 .interval
= IWL_DEF_LED_INTRVL
116 BUG_ON(idx
> IWL_MAX_BLINK_TBL
);
118 led_cmd
.on
= blink_tbl
[idx
].on_time
;
119 led_cmd
.off
= blink_tbl
[idx
].off_time
;
121 return iwl_send_led_cmd(priv
, &led_cmd
);
124 /* Set led register off */
125 static int iwl4965_led_on_reg(struct iwl_priv
*priv
, int led_id
)
127 IWL_DEBUG_LED("led on %d\n", led_id
);
128 iwl_write32(priv
, CSR_LED_REG
, CSR_LED_REG_TRUN_ON
);
133 /* Set led on command */
134 static int iwl4965_led_on(struct iwl_priv
*priv
, int led_id
)
136 struct iwl_led_cmd led_cmd
= {
140 .interval
= IWL_DEF_LED_INTRVL
142 return iwl_send_led_cmd(priv
, &led_cmd
);
145 /* Set led off command */
146 int iwl4965_led_off(struct iwl_priv
*priv
, int led_id
)
148 struct iwl_led_cmd led_cmd
= {
152 .interval
= IWL_DEF_LED_INTRVL
154 IWL_DEBUG_LED("led off %d\n", led_id
);
155 return iwl_send_led_cmd(priv
, &led_cmd
);
160 /* Set led register off */
161 static int iwl4965_led_off_reg(struct iwl_priv
*priv
, int led_id
)
163 IWL_DEBUG_LED("LED Reg off\n");
164 iwl_write32(priv
, CSR_LED_REG
, CSR_LED_REG_TRUN_OFF
);
169 * Set led register in case of disassociation according to rfkill state
171 static int iwl_led_associate(struct iwl_priv
*priv
, int led_id
)
173 IWL_DEBUG_LED("Associated\n");
174 priv
->allow_blinking
= 1;
175 return iwl4965_led_on_reg(priv
, led_id
);
177 static int iwl_led_disassociate(struct iwl_priv
*priv
, int led_id
)
179 priv
->allow_blinking
= 0;
180 if (iwl_is_rfkill(priv
))
181 iwl4965_led_off_reg(priv
, led_id
);
183 iwl4965_led_on_reg(priv
, led_id
);
189 * brightness call back function for Tx/Rx LED
191 static int iwl_led_associated(struct iwl_priv
*priv
, int led_id
)
193 if (test_bit(STATUS_EXIT_PENDING
, &priv
->status
) ||
194 !test_bit(STATUS_READY
, &priv
->status
))
198 /* start counting Tx/Rx bytes */
199 if (!priv
->last_blink_time
&& priv
->allow_blinking
)
200 priv
->last_blink_time
= jiffies
;
205 * brightness call back for association and radio
207 static void iwl_led_brightness_set(struct led_classdev
*led_cdev
,
208 enum led_brightness brightness
)
210 struct iwl_led
*led
= container_of(led_cdev
, struct iwl_led
, led_dev
);
211 struct iwl_priv
*priv
= led
->priv
;
213 if (test_bit(STATUS_EXIT_PENDING
, &priv
->status
))
217 IWL_DEBUG_LED("Led type = %s brightness = %d\n",
218 led_type_str
[led
->type
], brightness
);
219 switch (brightness
) {
222 led
->led_on(priv
, IWL_LED_LINK
);
226 led
->led_off(priv
, IWL_LED_LINK
);
229 if (led
->led_pattern
) {
230 int idx
= iwl_brightness_to_idx(brightness
);
231 led
->led_pattern(priv
, IWL_LED_LINK
, idx
);
240 * Register led class with the system
242 static int iwl_leds_register_led(struct iwl_priv
*priv
, struct iwl_led
*led
,
243 enum led_type type
, u8 set_led
,
246 struct device
*device
= wiphy_dev(priv
->hw
->wiphy
);
249 led
->led_dev
.name
= led
->name
;
250 led
->led_dev
.brightness_set
= iwl_led_brightness_set
;
251 led
->led_dev
.default_trigger
= trigger
;
256 ret
= led_classdev_register(device
, &led
->led_dev
);
258 IWL_ERROR("Error: failed to register led handler.\n");
264 if (set_led
&& led
->led_on
)
265 led
->led_on(priv
, IWL_LED_LINK
);
272 * calculate blink rate according to last 2 sec Tx/Rx activities
274 static int iwl_get_blink_rate(struct iwl_priv
*priv
)
277 u64 current_tpt
= priv
->tx_stats
[2].bytes
;
278 /* FIXME: + priv->rx_stats[2].bytes; */
279 s64 tpt
= current_tpt
- priv
->led_tpt
;
281 if (tpt
< 0) /* wrapparound */
284 IWL_DEBUG_LED("tpt %lld current_tpt %llu\n",
286 (unsigned long long)current_tpt
);
287 priv
->led_tpt
= current_tpt
;
289 if (!priv
->allow_blinking
)
290 i
= IWL_MAX_BLINK_TBL
;
292 for (i
= 0; i
< IWL_MAX_BLINK_TBL
; i
++)
293 if (tpt
> (blink_tbl
[i
].tpt
* IWL_1MB_RATE
))
296 IWL_DEBUG_LED("LED BLINK IDX=%d", i
);
301 * this function called from handler. Since setting Led command can
302 * happen very frequent we postpone led command to be called from
303 * REPLY handler so we know ucode is up
305 void iwl_leds_background(struct iwl_priv
*priv
)
309 if (test_bit(STATUS_EXIT_PENDING
, &priv
->status
)) {
310 priv
->last_blink_time
= 0;
313 if (iwl_is_rfkill(priv
)) {
314 priv
->last_blink_time
= 0;
318 if (!priv
->allow_blinking
) {
319 priv
->last_blink_time
= 0;
320 if (priv
->last_blink_rate
!= IWL_SOLID_BLINK_IDX
) {
321 priv
->last_blink_rate
= IWL_SOLID_BLINK_IDX
;
322 iwl4965_led_pattern(priv
, IWL_LED_LINK
,
323 IWL_SOLID_BLINK_IDX
);
327 if (!priv
->last_blink_time
||
328 !time_after(jiffies
, priv
->last_blink_time
+
329 msecs_to_jiffies(1000)))
332 blink_idx
= iwl_get_blink_rate(priv
);
334 /* call only if blink rate change */
335 if (blink_idx
!= priv
->last_blink_rate
)
336 iwl4965_led_pattern(priv
, IWL_LED_LINK
, blink_idx
);
338 priv
->last_blink_time
= jiffies
;
339 priv
->last_blink_rate
= blink_idx
;
341 EXPORT_SYMBOL(iwl_leds_background
);
343 /* Register all led handler */
344 int iwl_leds_register(struct iwl_priv
*priv
)
349 priv
->last_blink_rate
= 0;
351 priv
->last_blink_time
= 0;
352 priv
->allow_blinking
= 0;
354 trigger
= ieee80211_get_radio_led_name(priv
->hw
);
355 snprintf(priv
->led
[IWL_LED_TRG_RADIO
].name
,
356 sizeof(priv
->led
[IWL_LED_TRG_RADIO
].name
), "iwl-%s:radio",
357 wiphy_name(priv
->hw
->wiphy
));
359 priv
->led
[IWL_LED_TRG_RADIO
].led_on
= iwl4965_led_on_reg
;
360 priv
->led
[IWL_LED_TRG_RADIO
].led_off
= iwl4965_led_off_reg
;
361 priv
->led
[IWL_LED_TRG_RADIO
].led_pattern
= NULL
;
363 ret
= iwl_leds_register_led(priv
, &priv
->led
[IWL_LED_TRG_RADIO
],
364 IWL_LED_TRG_RADIO
, 1, trigger
);
368 trigger
= ieee80211_get_assoc_led_name(priv
->hw
);
369 snprintf(priv
->led
[IWL_LED_TRG_ASSOC
].name
,
370 sizeof(priv
->led
[IWL_LED_TRG_ASSOC
].name
), "iwl-%s:assoc",
371 wiphy_name(priv
->hw
->wiphy
));
373 ret
= iwl_leds_register_led(priv
, &priv
->led
[IWL_LED_TRG_ASSOC
],
374 IWL_LED_TRG_ASSOC
, 0, trigger
);
376 /* for assoc always turn led on */
377 priv
->led
[IWL_LED_TRG_ASSOC
].led_on
= iwl_led_associate
;
378 priv
->led
[IWL_LED_TRG_ASSOC
].led_off
= iwl_led_disassociate
;
379 priv
->led
[IWL_LED_TRG_ASSOC
].led_pattern
= NULL
;
384 trigger
= ieee80211_get_rx_led_name(priv
->hw
);
385 snprintf(priv
->led
[IWL_LED_TRG_RX
].name
,
386 sizeof(priv
->led
[IWL_LED_TRG_RX
].name
), "iwl-%s:RX",
387 wiphy_name(priv
->hw
->wiphy
));
389 ret
= iwl_leds_register_led(priv
, &priv
->led
[IWL_LED_TRG_RX
],
390 IWL_LED_TRG_RX
, 0, trigger
);
392 priv
->led
[IWL_LED_TRG_RX
].led_on
= iwl_led_associated
;
393 priv
->led
[IWL_LED_TRG_RX
].led_off
= iwl_led_associated
;
394 priv
->led
[IWL_LED_TRG_RX
].led_pattern
= iwl4965_led_pattern
;
399 trigger
= ieee80211_get_tx_led_name(priv
->hw
);
400 snprintf(priv
->led
[IWL_LED_TRG_TX
].name
,
401 sizeof(priv
->led
[IWL_LED_TRG_TX
].name
), "iwl-%s:TX",
402 wiphy_name(priv
->hw
->wiphy
));
404 ret
= iwl_leds_register_led(priv
, &priv
->led
[IWL_LED_TRG_TX
],
405 IWL_LED_TRG_TX
, 0, trigger
);
407 priv
->led
[IWL_LED_TRG_TX
].led_on
= iwl_led_associated
;
408 priv
->led
[IWL_LED_TRG_TX
].led_off
= iwl_led_associated
;
409 priv
->led
[IWL_LED_TRG_TX
].led_pattern
= iwl4965_led_pattern
;
417 iwl_leds_unregister(priv
);
420 EXPORT_SYMBOL(iwl_leds_register
);
422 /* unregister led class */
423 static void iwl_leds_unregister_led(struct iwl_led
*led
, u8 set_led
)
425 if (!led
->registered
)
428 led_classdev_unregister(&led
->led_dev
);
431 led
->led_dev
.brightness_set(&led
->led_dev
, LED_OFF
);
435 /* Unregister all led handlers */
436 void iwl_leds_unregister(struct iwl_priv
*priv
)
438 iwl_leds_unregister_led(&priv
->led
[IWL_LED_TRG_ASSOC
], 0);
439 iwl_leds_unregister_led(&priv
->led
[IWL_LED_TRG_RX
], 0);
440 iwl_leds_unregister_led(&priv
->led
[IWL_LED_TRG_TX
], 0);
441 iwl_leds_unregister_led(&priv
->led
[IWL_LED_TRG_RADIO
], 1);
443 EXPORT_SYMBOL(iwl_leds_unregister
);