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 int iwl3945_led_cmd_callback(struct iwl_priv *priv,
89 static inline int iwl3945_brightness_to_idx(enum led_brightness brightness)
91 return fls(0x000000FF & (u32)brightness);
94 /* Send led command */
95 static int iwl_send_led_cmd(struct iwl_priv *priv,
96 struct iwl_led_cmd *led_cmd)
98 struct iwl_host_cmd cmd = {
100 .len = sizeof(struct iwl_led_cmd),
102 .meta.flags = CMD_ASYNC,
103 .meta.u.callback = iwl3945_led_cmd_callback,
106 return iwl_send_cmd(priv, &cmd);
111 /* Set led on command */
112 static int iwl3945_led_pattern(struct iwl_priv *priv, int led_id,
115 struct iwl_led_cmd led_cmd = {
117 .interval = IWL_DEF_LED_INTRVL
120 BUG_ON(idx > IWL_MAX_BLINK_TBL);
122 led_cmd.on = blink_tbl[idx].on_time;
123 led_cmd.off = blink_tbl[idx].off_time;
125 return iwl_send_led_cmd(priv, &led_cmd);
129 /* Set led on command */
130 static int iwl3945_led_on(struct iwl_priv *priv, int led_id)
132 struct iwl_led_cmd led_cmd = {
136 .interval = IWL_DEF_LED_INTRVL
138 return iwl_send_led_cmd(priv, &led_cmd);
141 /* Set led off command */
142 static int iwl3945_led_off(struct iwl_priv *priv, int led_id)
144 struct iwl_led_cmd led_cmd = {
148 .interval = IWL_DEF_LED_INTRVL
150 IWL_DEBUG_LED(priv, "led off %d\n", led_id);
151 return iwl_send_led_cmd(priv, &led_cmd);
155 * Set led on in case of association
157 static int iwl3945_led_associate(struct iwl_priv *priv, int led_id)
159 IWL_DEBUG_LED(priv, "Associated\n");
161 priv->allow_blinking = 1;
162 return iwl3945_led_on(priv, led_id);
164 /* Set Led off in case of disassociation */
165 static int iwl3945_led_disassociate(struct iwl_priv *priv, int led_id)
167 IWL_DEBUG_LED(priv, "Disassociated\n");
169 priv->allow_blinking = 0;
175 * brightness call back function for Tx/Rx LED
177 static int iwl3945_led_associated(struct iwl_priv *priv, int led_id)
179 if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
180 !test_bit(STATUS_READY, &priv->status))
184 /* start counting Tx/Rx bytes */
185 if (!priv->last_blink_time && priv->allow_blinking)
186 priv->last_blink_time = jiffies;
191 * brightness call back for association and radio
193 static void iwl3945_led_brightness_set(struct led_classdev *led_cdev,
194 enum led_brightness brightness)
196 struct iwl_led *led = container_of(led_cdev,
197 struct iwl_led, led_dev);
198 struct iwl_priv *priv = led->priv;
200 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
203 IWL_DEBUG_LED(priv, "Led type = %s brightness = %d\n",
204 led_type_str[led->type], brightness);
206 switch (brightness) {
209 led->led_on(priv, IWL_LED_LINK);
213 led->led_off(priv, IWL_LED_LINK);
216 if (led->led_pattern) {
217 int idx = iwl3945_brightness_to_idx(brightness);
218 led->led_pattern(priv, IWL_LED_LINK, idx);
225 * Register led class with the system
227 static int iwl3945_led_register_led(struct iwl_priv *priv,
229 enum led_type type, u8 set_led,
232 struct device *device = wiphy_dev(priv->hw->wiphy);
235 led->led_dev.name = led->name;
236 led->led_dev.brightness_set = iwl3945_led_brightness_set;
237 led->led_dev.default_trigger = trigger;
242 ret = led_classdev_register(device, &led->led_dev);
244 IWL_ERR(priv, "Error: failed to register led handler.\n");
250 if (set_led && led->led_on)
251 led->led_on(priv, IWL_LED_LINK);
257 * calculate blink rate according to last 2 sec Tx/Rx activities
259 static inline u8 get_blink_rate(struct iwl_priv *priv)
262 s64 tpt = priv->rxtxpackets;
267 IWL_DEBUG_LED(priv, "tpt %lld \n", (long long)tpt);
269 if (!priv->allow_blinking)
270 index = IWL_MAX_BLINK_TBL;
272 for (index = 0; index < IWL_MAX_BLINK_TBL; index++)
273 if (tpt > (blink_tbl[index].brightness * IWL_1MB_RATE))
276 IWL_DEBUG_LED(priv, "LED BLINK IDX=%d\n", index);
281 * this function called from handler. Since setting Led command can
282 * happen very frequent we postpone led command to be called from
283 * REPLY handler so we know ucode is up
285 void iwl3945_led_background(struct iwl_priv *priv)
289 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
290 priv->last_blink_time = 0;
293 if (iwl_is_rfkill(priv)) {
294 priv->last_blink_time = 0;
298 if (!priv->allow_blinking) {
299 priv->last_blink_time = 0;
300 if (priv->last_blink_rate != IWL_SOLID_BLINK_IDX) {
301 priv->last_blink_rate = IWL_SOLID_BLINK_IDX;
302 iwl3945_led_pattern(priv, IWL_LED_LINK,
303 IWL_SOLID_BLINK_IDX);
307 if (!priv->last_blink_time ||
308 !time_after(jiffies, priv->last_blink_time +
309 msecs_to_jiffies(1000)))
312 blink_idx = get_blink_rate(priv);
314 /* call only if blink rate change */
315 if (blink_idx != priv->last_blink_rate)
316 iwl3945_led_pattern(priv, IWL_LED_LINK, blink_idx);
318 priv->last_blink_time = jiffies;
319 priv->last_blink_rate = blink_idx;
320 priv->rxtxpackets = 0;
324 /* Register all led handler */
325 int iwl3945_led_register(struct iwl_priv *priv)
330 priv->last_blink_rate = 0;
331 priv->rxtxpackets = 0;
333 priv->last_blink_time = 0;
334 priv->allow_blinking = 0;
336 trigger = ieee80211_get_radio_led_name(priv->hw);
337 snprintf(priv->led[IWL_LED_TRG_RADIO].name,
338 sizeof(priv->led[IWL_LED_TRG_RADIO].name), "iwl-%s::radio",
339 wiphy_name(priv->hw->wiphy));
341 priv->led[IWL_LED_TRG_RADIO].led_on = iwl3945_led_on;
342 priv->led[IWL_LED_TRG_RADIO].led_off = iwl3945_led_off;
343 priv->led[IWL_LED_TRG_RADIO].led_pattern = NULL;
345 ret = iwl3945_led_register_led(priv,
346 &priv->led[IWL_LED_TRG_RADIO],
347 IWL_LED_TRG_RADIO, 1, trigger);
352 trigger = ieee80211_get_assoc_led_name(priv->hw);
353 snprintf(priv->led[IWL_LED_TRG_ASSOC].name,
354 sizeof(priv->led[IWL_LED_TRG_ASSOC].name), "iwl-%s::assoc",
355 wiphy_name(priv->hw->wiphy));
357 ret = iwl3945_led_register_led(priv,
358 &priv->led[IWL_LED_TRG_ASSOC],
359 IWL_LED_TRG_ASSOC, 0, trigger);
361 /* for assoc always turn led on */
362 priv->led[IWL_LED_TRG_ASSOC].led_on = iwl3945_led_associate;
363 priv->led[IWL_LED_TRG_ASSOC].led_off = iwl3945_led_disassociate;
364 priv->led[IWL_LED_TRG_ASSOC].led_pattern = NULL;
369 trigger = ieee80211_get_rx_led_name(priv->hw);
370 snprintf(priv->led[IWL_LED_TRG_RX].name,
371 sizeof(priv->led[IWL_LED_TRG_RX].name), "iwl-%s::RX",
372 wiphy_name(priv->hw->wiphy));
374 ret = iwl3945_led_register_led(priv,
375 &priv->led[IWL_LED_TRG_RX],
376 IWL_LED_TRG_RX, 0, trigger);
378 priv->led[IWL_LED_TRG_RX].led_on = iwl3945_led_associated;
379 priv->led[IWL_LED_TRG_RX].led_off = iwl3945_led_associated;
380 priv->led[IWL_LED_TRG_RX].led_pattern = iwl3945_led_pattern;
385 trigger = ieee80211_get_tx_led_name(priv->hw);
386 snprintf(priv->led[IWL_LED_TRG_TX].name,
387 sizeof(priv->led[IWL_LED_TRG_TX].name), "iwl-%s::TX",
388 wiphy_name(priv->hw->wiphy));
390 ret = iwl3945_led_register_led(priv,
391 &priv->led[IWL_LED_TRG_TX],
392 IWL_LED_TRG_TX, 0, trigger);
394 priv->led[IWL_LED_TRG_TX].led_on = iwl3945_led_associated;
395 priv->led[IWL_LED_TRG_TX].led_off = iwl3945_led_associated;
396 priv->led[IWL_LED_TRG_TX].led_pattern = iwl3945_led_pattern;
404 iwl3945_led_unregister(priv);
409 /* unregister led class */
410 static void iwl3945_led_unregister_led(struct iwl_led *led, u8 set_led)
412 if (!led->registered)
415 led_classdev_unregister(&led->led_dev);
418 led->led_dev.brightness_set(&led->led_dev, LED_OFF);
422 /* Unregister all led handlers */
423 void iwl3945_led_unregister(struct iwl_priv *priv)
425 iwl3945_led_unregister_led(&priv->led[IWL_LED_TRG_ASSOC], 0);
426 iwl3945_led_unregister_led(&priv->led[IWL_LED_TRG_RX], 0);
427 iwl3945_led_unregister_led(&priv->led[IWL_LED_TRG_TX], 0);
428 iwl3945_led_unregister_led(&priv->led[IWL_LED_TRG_RADIO], 1);