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 *****************************************************************************/
27 #ifdef CONFIG_IWLWIFI_LEDS
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/pci.h>
33 #include <linux/dma-mapping.h>
34 #include <linux/delay.h>
35 #include <linux/skbuff.h>
36 #include <linux/netdevice.h>
37 #include <linux/wireless.h>
38 #include <net/mac80211.h>
39 #include <linux/etherdevice.h>
40 #include <asm/unaligned.h>
42 #include "iwl-commands.h"
47 #ifdef CONFIG_IWLWIFI_DEBUG
48 static const char *led_type_str
[] = {
49 __stringify(IWL_LED_TRG_TX
),
50 __stringify(IWL_LED_TRG_RX
),
51 __stringify(IWL_LED_TRG_ASSOC
),
52 __stringify(IWL_LED_TRG_RADIO
),
55 #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 static void iwl3945_led_cmd_callback(struct iwl_priv
*priv
,
83 struct iwl_device_cmd
*cmd
,
88 static inline int iwl3945_brightness_to_idx(enum led_brightness brightness
)
90 return fls(0x000000FF & (u32
)brightness
);
93 /* Send led command */
94 static int iwl_send_led_cmd(struct iwl_priv
*priv
,
95 struct iwl_led_cmd
*led_cmd
)
97 struct iwl_host_cmd cmd
= {
99 .len
= sizeof(struct iwl_led_cmd
),
102 .callback
= iwl3945_led_cmd_callback
,
105 return iwl_send_cmd(priv
, &cmd
);
110 /* Set led on command */
111 static int iwl3945_led_pattern(struct iwl_priv
*priv
, int led_id
,
114 struct iwl_led_cmd led_cmd
= {
116 .interval
= IWL_DEF_LED_INTRVL
119 BUG_ON(idx
> IWL_MAX_BLINK_TBL
);
121 led_cmd
.on
= blink_tbl
[idx
].on_time
;
122 led_cmd
.off
= blink_tbl
[idx
].off_time
;
124 return iwl_send_led_cmd(priv
, &led_cmd
);
128 /* Set led on command */
129 static int iwl3945_led_on(struct iwl_priv
*priv
, int led_id
)
131 struct iwl_led_cmd led_cmd
= {
135 .interval
= IWL_DEF_LED_INTRVL
137 return iwl_send_led_cmd(priv
, &led_cmd
);
140 /* Set led off command */
141 static int iwl3945_led_off(struct iwl_priv
*priv
, int led_id
)
143 struct iwl_led_cmd led_cmd
= {
147 .interval
= IWL_DEF_LED_INTRVL
149 IWL_DEBUG_LED(priv
, "led off %d\n", led_id
);
150 return iwl_send_led_cmd(priv
, &led_cmd
);
154 * Set led on in case of association
156 static int iwl3945_led_associate(struct iwl_priv
*priv
, int led_id
)
158 IWL_DEBUG_LED(priv
, "Associated\n");
160 priv
->allow_blinking
= 1;
161 return iwl3945_led_on(priv
, led_id
);
163 /* Set Led off in case of disassociation */
164 static int iwl3945_led_disassociate(struct iwl_priv
*priv
, int led_id
)
166 IWL_DEBUG_LED(priv
, "Disassociated\n");
168 priv
->allow_blinking
= 0;
174 * brightness call back function for Tx/Rx LED
176 static int iwl3945_led_associated(struct iwl_priv
*priv
, int led_id
)
178 if (test_bit(STATUS_EXIT_PENDING
, &priv
->status
) ||
179 !test_bit(STATUS_READY
, &priv
->status
))
183 /* start counting Tx/Rx bytes */
184 if (!priv
->last_blink_time
&& priv
->allow_blinking
)
185 priv
->last_blink_time
= jiffies
;
190 * brightness call back for association and radio
192 static void iwl3945_led_brightness_set(struct led_classdev
*led_cdev
,
193 enum led_brightness brightness
)
195 struct iwl_led
*led
= container_of(led_cdev
,
196 struct iwl_led
, led_dev
);
197 struct iwl_priv
*priv
= led
->priv
;
199 if (test_bit(STATUS_EXIT_PENDING
, &priv
->status
))
202 IWL_DEBUG_LED(priv
, "Led type = %s brightness = %d\n",
203 led_type_str
[led
->type
], brightness
);
205 switch (brightness
) {
208 led
->led_on(priv
, IWL_LED_LINK
);
212 led
->led_off(priv
, IWL_LED_LINK
);
215 if (led
->led_pattern
) {
216 int idx
= iwl3945_brightness_to_idx(brightness
);
217 led
->led_pattern(priv
, IWL_LED_LINK
, idx
);
224 * Register led class with the system
226 static int iwl3945_led_register_led(struct iwl_priv
*priv
,
228 enum led_type type
, u8 set_led
,
231 struct device
*device
= wiphy_dev(priv
->hw
->wiphy
);
234 led
->led_dev
.name
= led
->name
;
235 led
->led_dev
.brightness_set
= iwl3945_led_brightness_set
;
236 led
->led_dev
.default_trigger
= trigger
;
241 ret
= led_classdev_register(device
, &led
->led_dev
);
243 IWL_ERR(priv
, "Error: failed to register led handler.\n");
249 if (set_led
&& led
->led_on
)
250 led
->led_on(priv
, IWL_LED_LINK
);
256 * calculate blink rate according to last 2 sec Tx/Rx activities
258 static inline u8
get_blink_rate(struct iwl_priv
*priv
)
261 s64 tpt
= priv
->rxtxpackets
;
266 IWL_DEBUG_LED(priv
, "tpt %lld \n", (long long)tpt
);
268 if (!priv
->allow_blinking
)
269 index
= IWL_MAX_BLINK_TBL
;
271 for (index
= 0; index
< IWL_MAX_BLINK_TBL
; index
++)
272 if (tpt
> (blink_tbl
[index
].brightness
* IWL_1MB_RATE
))
275 IWL_DEBUG_LED(priv
, "LED BLINK IDX=%d\n", index
);
280 * this function called from handler. Since setting Led command can
281 * happen very frequent we postpone led command to be called from
282 * REPLY handler so we know ucode is up
284 void iwl3945_led_background(struct iwl_priv
*priv
)
288 if (test_bit(STATUS_EXIT_PENDING
, &priv
->status
)) {
289 priv
->last_blink_time
= 0;
292 if (iwl_is_rfkill(priv
)) {
293 priv
->last_blink_time
= 0;
297 if (!priv
->allow_blinking
) {
298 priv
->last_blink_time
= 0;
299 if (priv
->last_blink_rate
!= IWL_SOLID_BLINK_IDX
) {
300 priv
->last_blink_rate
= IWL_SOLID_BLINK_IDX
;
301 iwl3945_led_pattern(priv
, IWL_LED_LINK
,
302 IWL_SOLID_BLINK_IDX
);
306 if (!priv
->last_blink_time
||
307 !time_after(jiffies
, priv
->last_blink_time
+
308 msecs_to_jiffies(1000)))
311 blink_idx
= get_blink_rate(priv
);
313 /* call only if blink rate change */
314 if (blink_idx
!= priv
->last_blink_rate
)
315 iwl3945_led_pattern(priv
, IWL_LED_LINK
, blink_idx
);
317 priv
->last_blink_time
= jiffies
;
318 priv
->last_blink_rate
= blink_idx
;
319 priv
->rxtxpackets
= 0;
323 /* Register all led handler */
324 int iwl3945_led_register(struct iwl_priv
*priv
)
329 priv
->last_blink_rate
= 0;
330 priv
->rxtxpackets
= 0;
332 priv
->last_blink_time
= 0;
333 priv
->allow_blinking
= 0;
335 trigger
= ieee80211_get_radio_led_name(priv
->hw
);
336 snprintf(priv
->led
[IWL_LED_TRG_RADIO
].name
,
337 sizeof(priv
->led
[IWL_LED_TRG_RADIO
].name
), "iwl-%s::radio",
338 wiphy_name(priv
->hw
->wiphy
));
340 priv
->led
[IWL_LED_TRG_RADIO
].led_on
= iwl3945_led_on
;
341 priv
->led
[IWL_LED_TRG_RADIO
].led_off
= iwl3945_led_off
;
342 priv
->led
[IWL_LED_TRG_RADIO
].led_pattern
= NULL
;
344 ret
= iwl3945_led_register_led(priv
,
345 &priv
->led
[IWL_LED_TRG_RADIO
],
346 IWL_LED_TRG_RADIO
, 1, trigger
);
351 trigger
= ieee80211_get_assoc_led_name(priv
->hw
);
352 snprintf(priv
->led
[IWL_LED_TRG_ASSOC
].name
,
353 sizeof(priv
->led
[IWL_LED_TRG_ASSOC
].name
), "iwl-%s::assoc",
354 wiphy_name(priv
->hw
->wiphy
));
356 ret
= iwl3945_led_register_led(priv
,
357 &priv
->led
[IWL_LED_TRG_ASSOC
],
358 IWL_LED_TRG_ASSOC
, 0, trigger
);
360 /* for assoc always turn led on */
361 priv
->led
[IWL_LED_TRG_ASSOC
].led_on
= iwl3945_led_associate
;
362 priv
->led
[IWL_LED_TRG_ASSOC
].led_off
= iwl3945_led_disassociate
;
363 priv
->led
[IWL_LED_TRG_ASSOC
].led_pattern
= NULL
;
368 trigger
= ieee80211_get_rx_led_name(priv
->hw
);
369 snprintf(priv
->led
[IWL_LED_TRG_RX
].name
,
370 sizeof(priv
->led
[IWL_LED_TRG_RX
].name
), "iwl-%s::RX",
371 wiphy_name(priv
->hw
->wiphy
));
373 ret
= iwl3945_led_register_led(priv
,
374 &priv
->led
[IWL_LED_TRG_RX
],
375 IWL_LED_TRG_RX
, 0, trigger
);
377 priv
->led
[IWL_LED_TRG_RX
].led_on
= iwl3945_led_associated
;
378 priv
->led
[IWL_LED_TRG_RX
].led_off
= iwl3945_led_associated
;
379 priv
->led
[IWL_LED_TRG_RX
].led_pattern
= iwl3945_led_pattern
;
384 trigger
= ieee80211_get_tx_led_name(priv
->hw
);
385 snprintf(priv
->led
[IWL_LED_TRG_TX
].name
,
386 sizeof(priv
->led
[IWL_LED_TRG_TX
].name
), "iwl-%s::TX",
387 wiphy_name(priv
->hw
->wiphy
));
389 ret
= iwl3945_led_register_led(priv
,
390 &priv
->led
[IWL_LED_TRG_TX
],
391 IWL_LED_TRG_TX
, 0, trigger
);
393 priv
->led
[IWL_LED_TRG_TX
].led_on
= iwl3945_led_associated
;
394 priv
->led
[IWL_LED_TRG_TX
].led_off
= iwl3945_led_associated
;
395 priv
->led
[IWL_LED_TRG_TX
].led_pattern
= iwl3945_led_pattern
;
403 iwl3945_led_unregister(priv
);
408 /* unregister led class */
409 static void iwl3945_led_unregister_led(struct iwl_led
*led
, u8 set_led
)
411 if (!led
->registered
)
414 led_classdev_unregister(&led
->led_dev
);
417 led
->led_dev
.brightness_set(&led
->led_dev
, LED_OFF
);
421 /* Unregister all led handlers */
422 void iwl3945_led_unregister(struct iwl_priv
*priv
)
424 iwl3945_led_unregister_led(&priv
->led
[IWL_LED_TRG_ASSOC
], 0);
425 iwl3945_led_unregister_led(&priv
->led
[IWL_LED_TRG_RX
], 0);
426 iwl3945_led_unregister_led(&priv
->led
[IWL_LED_TRG_TX
], 0);
427 iwl3945_led_unregister_led(&priv
->led
[IWL_LED_TRG_RADIO
], 1);