1 /******************************************************************************
3 * Copyright(c) 2003 - 2009 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>
45 #ifdef CONFIG_IWLWIFI_DEBUG
46 static const char *led_type_str
[] = {
47 __stringify(IWL_LED_TRG_TX
),
48 __stringify(IWL_LED_TRG_RX
),
49 __stringify(IWL_LED_TRG_ASSOC
),
50 __stringify(IWL_LED_TRG_RADIO
),
53 #endif /* CONFIG_IWLWIFI_DEBUG */
73 {-1, IWL_LED_SOLID
, 0}
76 #define IWL_1MB_RATE (128 * 1024)
77 #define IWL_LED_THRESHOLD (16)
78 #define IWL_MAX_BLINK_TBL (ARRAY_SIZE(blink_tbl) - 1) /* exclude SOLID_ON */
79 #define IWL_SOLID_BLINK_IDX (ARRAY_SIZE(blink_tbl) - 1)
82 * Adjust led blink rate to compensate on a MAC Clock difference on every HW
83 * Led blink rate analysis showed an average deviation of 0% on 3945,
84 * 5% on 4965 HW and 20% on 5000 series and up.
85 * Need to compensate on the led on/off time per HW according to the deviation
86 * to achieve the desired led frequency
87 * The calculation is: (100-averageDeviation)/100 * blinkTime
88 * For code efficiency the calculation will be:
89 * compensation = (100 - averageDeviation) * 64 / 100
90 * NewBlinkTime = (compensation * BlinkTime) / 64
92 static inline u8
iwl_blink_compensation(struct iwl_priv
*priv
,
93 u8 time
, u16 compensation
)
96 IWL_ERR(priv
, "undefined blink compensation: "
97 "use pre-defined blinking time\n");
101 return (u8
)((time
* compensation
) >> 6);
104 /* [0-256] -> [0..8] FIXME: we need [0..10] */
105 static inline int iwl_brightness_to_idx(enum led_brightness brightness
)
107 return fls(0x000000FF & (u32
)brightness
);
110 /* Send led command */
111 static int iwl_send_led_cmd(struct iwl_priv
*priv
, struct iwl_led_cmd
*led_cmd
)
113 struct iwl_host_cmd cmd
= {
114 .id
= REPLY_LEDS_CMD
,
115 .len
= sizeof(struct iwl_led_cmd
),
122 reg
= iwl_read32(priv
, CSR_LED_REG
);
123 if (reg
!= (reg
& CSR_LED_BSM_CTRL_MSK
))
124 iwl_write32(priv
, CSR_LED_REG
, reg
& CSR_LED_BSM_CTRL_MSK
);
126 return iwl_send_cmd(priv
, &cmd
);
129 /* Set led pattern command */
130 static int iwl_led_pattern(struct iwl_priv
*priv
, int led_id
,
133 struct iwl_led_cmd led_cmd
= {
135 .interval
= IWL_DEF_LED_INTRVL
138 BUG_ON(idx
> IWL_MAX_BLINK_TBL
);
140 IWL_DEBUG_LED(priv
, "Led blink time compensation= %u\n",
141 priv
->cfg
->led_compensation
);
143 iwl_blink_compensation(priv
, blink_tbl
[idx
].on_time
,
144 priv
->cfg
->led_compensation
);
146 iwl_blink_compensation(priv
, blink_tbl
[idx
].off_time
,
147 priv
->cfg
->led_compensation
);
149 return iwl_send_led_cmd(priv
, &led_cmd
);
152 /* Set led register off */
153 static int iwl_led_on_reg(struct iwl_priv
*priv
, int led_id
)
155 IWL_DEBUG_LED(priv
, "led on %d\n", led_id
);
156 iwl_write32(priv
, CSR_LED_REG
, CSR_LED_REG_TRUN_ON
);
161 /* Set led on command */
162 static int iwl_led_on(struct iwl_priv
*priv
, int led_id
)
164 struct iwl_led_cmd led_cmd
= {
168 .interval
= IWL_DEF_LED_INTRVL
170 return iwl_send_led_cmd(priv
, &led_cmd
);
173 /* Set led off command */
174 int iwl_led_off(struct iwl_priv
*priv
, int led_id
)
176 struct iwl_led_cmd led_cmd
= {
180 .interval
= IWL_DEF_LED_INTRVL
182 IWL_DEBUG_LED(priv
, "led off %d\n", led_id
);
183 return iwl_send_led_cmd(priv
, &led_cmd
);
188 /* Set led register off */
189 static int iwl_led_off_reg(struct iwl_priv
*priv
, int led_id
)
191 IWL_DEBUG_LED(priv
, "LED Reg off\n");
192 iwl_write32(priv
, CSR_LED_REG
, CSR_LED_REG_TRUN_OFF
);
197 * Set led register in case of disassociation according to rfkill state
199 static int iwl_led_associate(struct iwl_priv
*priv
, int led_id
)
201 IWL_DEBUG_LED(priv
, "Associated\n");
202 priv
->allow_blinking
= 1;
203 return iwl_led_on_reg(priv
, led_id
);
205 static int iwl_led_disassociate(struct iwl_priv
*priv
, int led_id
)
207 priv
->allow_blinking
= 0;
213 * brightness call back function for Tx/Rx LED
215 static int iwl_led_associated(struct iwl_priv
*priv
, int led_id
)
217 if (test_bit(STATUS_EXIT_PENDING
, &priv
->status
) ||
218 !test_bit(STATUS_READY
, &priv
->status
))
222 /* start counting Tx/Rx bytes */
223 if (!priv
->last_blink_time
&& priv
->allow_blinking
)
224 priv
->last_blink_time
= jiffies
;
229 * brightness call back for association and radio
231 static void iwl_led_brightness_set(struct led_classdev
*led_cdev
,
232 enum led_brightness brightness
)
234 struct iwl_led
*led
= container_of(led_cdev
, struct iwl_led
, led_dev
);
235 struct iwl_priv
*priv
= led
->priv
;
237 if (test_bit(STATUS_EXIT_PENDING
, &priv
->status
))
241 IWL_DEBUG_LED(priv
, "Led type = %s brightness = %d\n",
242 led_type_str
[led
->type
], brightness
);
243 switch (brightness
) {
246 led
->led_on(priv
, IWL_LED_LINK
);
250 led
->led_off(priv
, IWL_LED_LINK
);
253 if (led
->led_pattern
) {
254 int idx
= iwl_brightness_to_idx(brightness
);
255 led
->led_pattern(priv
, IWL_LED_LINK
, idx
);
264 * Register led class with the system
266 static int iwl_leds_register_led(struct iwl_priv
*priv
, struct iwl_led
*led
,
267 enum led_type type
, u8 set_led
,
270 struct device
*device
= wiphy_dev(priv
->hw
->wiphy
);
273 led
->led_dev
.name
= led
->name
;
274 led
->led_dev
.brightness_set
= iwl_led_brightness_set
;
275 led
->led_dev
.default_trigger
= trigger
;
280 ret
= led_classdev_register(device
, &led
->led_dev
);
282 IWL_ERR(priv
, "Error: failed to register led handler.\n");
288 if (set_led
&& led
->led_on
)
289 led
->led_on(priv
, IWL_LED_LINK
);
296 * calculate blink rate according to last second Tx/Rx activities
298 static int iwl_get_blink_rate(struct iwl_priv
*priv
)
301 /* count both tx and rx traffic to be able to
302 * handle traffic in either direction
304 u64 current_tpt
= priv
->tx_stats
.data_bytes
+
305 priv
->rx_stats
.data_bytes
;
306 s64 tpt
= current_tpt
- priv
->led_tpt
;
308 if (tpt
< 0) /* wraparound */
311 IWL_DEBUG_LED(priv
, "tpt %lld current_tpt %llu\n",
313 (unsigned long long)current_tpt
);
314 priv
->led_tpt
= current_tpt
;
316 if (!priv
->allow_blinking
)
317 i
= IWL_MAX_BLINK_TBL
;
319 for (i
= 0; i
< IWL_MAX_BLINK_TBL
; i
++)
320 if (tpt
> (blink_tbl
[i
].tpt
* IWL_1MB_RATE
))
323 IWL_DEBUG_LED(priv
, "LED BLINK IDX=%d\n", i
);
328 * this function called from handler. Since setting Led command can
329 * happen very frequent we postpone led command to be called from
330 * REPLY handler so we know ucode is up
332 void iwl_leds_background(struct iwl_priv
*priv
)
336 if (test_bit(STATUS_EXIT_PENDING
, &priv
->status
)) {
337 priv
->last_blink_time
= 0;
340 if (iwl_is_rfkill(priv
)) {
341 priv
->last_blink_time
= 0;
345 if (!priv
->allow_blinking
) {
346 priv
->last_blink_time
= 0;
347 if (priv
->last_blink_rate
!= IWL_SOLID_BLINK_IDX
) {
348 priv
->last_blink_rate
= IWL_SOLID_BLINK_IDX
;
349 iwl_led_pattern(priv
, IWL_LED_LINK
,
350 IWL_SOLID_BLINK_IDX
);
354 if (!priv
->last_blink_time
||
355 !time_after(jiffies
, priv
->last_blink_time
+
356 msecs_to_jiffies(1000)))
359 blink_idx
= iwl_get_blink_rate(priv
);
361 /* call only if blink rate change */
362 if (blink_idx
!= priv
->last_blink_rate
)
363 iwl_led_pattern(priv
, IWL_LED_LINK
, blink_idx
);
365 priv
->last_blink_time
= jiffies
;
366 priv
->last_blink_rate
= blink_idx
;
369 /* Register all led handler */
370 int iwl_leds_register(struct iwl_priv
*priv
)
375 priv
->last_blink_rate
= 0;
377 priv
->last_blink_time
= 0;
378 priv
->allow_blinking
= 0;
380 trigger
= ieee80211_get_radio_led_name(priv
->hw
);
381 snprintf(priv
->led
[IWL_LED_TRG_RADIO
].name
,
382 sizeof(priv
->led
[IWL_LED_TRG_RADIO
].name
), "iwl-%s::radio",
383 wiphy_name(priv
->hw
->wiphy
));
385 priv
->led
[IWL_LED_TRG_RADIO
].led_on
= iwl_led_on_reg
;
386 priv
->led
[IWL_LED_TRG_RADIO
].led_off
= iwl_led_off_reg
;
387 priv
->led
[IWL_LED_TRG_RADIO
].led_pattern
= NULL
;
389 ret
= iwl_leds_register_led(priv
, &priv
->led
[IWL_LED_TRG_RADIO
],
390 IWL_LED_TRG_RADIO
, 1, trigger
);
394 trigger
= ieee80211_get_assoc_led_name(priv
->hw
);
395 snprintf(priv
->led
[IWL_LED_TRG_ASSOC
].name
,
396 sizeof(priv
->led
[IWL_LED_TRG_ASSOC
].name
), "iwl-%s::assoc",
397 wiphy_name(priv
->hw
->wiphy
));
399 ret
= iwl_leds_register_led(priv
, &priv
->led
[IWL_LED_TRG_ASSOC
],
400 IWL_LED_TRG_ASSOC
, 0, trigger
);
402 /* for assoc always turn led on */
403 priv
->led
[IWL_LED_TRG_ASSOC
].led_on
= iwl_led_associate
;
404 priv
->led
[IWL_LED_TRG_ASSOC
].led_off
= iwl_led_disassociate
;
405 priv
->led
[IWL_LED_TRG_ASSOC
].led_pattern
= NULL
;
410 trigger
= ieee80211_get_rx_led_name(priv
->hw
);
411 snprintf(priv
->led
[IWL_LED_TRG_RX
].name
,
412 sizeof(priv
->led
[IWL_LED_TRG_RX
].name
), "iwl-%s::RX",
413 wiphy_name(priv
->hw
->wiphy
));
415 ret
= iwl_leds_register_led(priv
, &priv
->led
[IWL_LED_TRG_RX
],
416 IWL_LED_TRG_RX
, 0, trigger
);
418 priv
->led
[IWL_LED_TRG_RX
].led_on
= iwl_led_associated
;
419 priv
->led
[IWL_LED_TRG_RX
].led_off
= iwl_led_associated
;
420 priv
->led
[IWL_LED_TRG_RX
].led_pattern
= iwl_led_pattern
;
425 trigger
= ieee80211_get_tx_led_name(priv
->hw
);
426 snprintf(priv
->led
[IWL_LED_TRG_TX
].name
,
427 sizeof(priv
->led
[IWL_LED_TRG_TX
].name
), "iwl-%s::TX",
428 wiphy_name(priv
->hw
->wiphy
));
430 ret
= iwl_leds_register_led(priv
, &priv
->led
[IWL_LED_TRG_TX
],
431 IWL_LED_TRG_TX
, 0, trigger
);
433 priv
->led
[IWL_LED_TRG_TX
].led_on
= iwl_led_associated
;
434 priv
->led
[IWL_LED_TRG_TX
].led_off
= iwl_led_associated
;
435 priv
->led
[IWL_LED_TRG_TX
].led_pattern
= iwl_led_pattern
;
443 iwl_leds_unregister(priv
);
446 EXPORT_SYMBOL(iwl_leds_register
);
448 /* unregister led class */
449 static void iwl_leds_unregister_led(struct iwl_led
*led
, u8 set_led
)
451 if (!led
->registered
)
454 led_classdev_unregister(&led
->led_dev
);
457 led
->led_dev
.brightness_set(&led
->led_dev
, LED_OFF
);
461 /* Unregister all led handlers */
462 void iwl_leds_unregister(struct iwl_priv
*priv
)
464 iwl_leds_unregister_led(&priv
->led
[IWL_LED_TRG_ASSOC
], 0);
465 iwl_leds_unregister_led(&priv
->led
[IWL_LED_TRG_RX
], 0);
466 iwl_leds_unregister_led(&priv
->led
[IWL_LED_TRG_TX
], 0);
467 iwl_leds_unregister_led(&priv
->led
[IWL_LED_TRG_RADIO
], 1);
469 EXPORT_SYMBOL(iwl_leds_unregister
);