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"
65 {-1, IWL_LED_SOLID, 0}
68 #define IWL_1MB_RATE (128 * 1024)
69 #define IWL_LED_THRESHOLD (16)
70 #define IWL_MAX_BLINK_TBL (ARRAY_SIZE(blink_tbl) - 1) /*Exclude Solid on*/
71 #define IWL_SOLID_BLINK_IDX (ARRAY_SIZE(blink_tbl) - 1)
73 static int iwl3945_led_cmd_callback(struct iwl_priv *priv,
80 static inline int iwl3945_brightness_to_idx(enum led_brightness brightness)
82 return fls(0x000000FF & (u32)brightness);
85 /* Send led command */
86 static int iwl_send_led_cmd(struct iwl_priv *priv,
87 struct iwl_led_cmd *led_cmd)
89 struct iwl_host_cmd cmd = {
91 .len = sizeof(struct iwl_led_cmd),
93 .meta.flags = CMD_ASYNC,
94 .meta.u.callback = iwl3945_led_cmd_callback,
97 return iwl_send_cmd(priv, &cmd);
102 /* Set led on command */
103 static int iwl3945_led_pattern(struct iwl_priv *priv, int led_id,
106 struct iwl_led_cmd led_cmd = {
108 .interval = IWL_DEF_LED_INTRVL
111 BUG_ON(idx > IWL_MAX_BLINK_TBL);
113 led_cmd.on = blink_tbl[idx].on_time;
114 led_cmd.off = blink_tbl[idx].off_time;
116 return iwl_send_led_cmd(priv, &led_cmd);
120 /* Set led on command */
121 static int iwl3945_led_on(struct iwl_priv *priv, int led_id)
123 struct iwl_led_cmd led_cmd = {
127 .interval = IWL_DEF_LED_INTRVL
129 return iwl_send_led_cmd(priv, &led_cmd);
132 /* Set led off command */
133 static int iwl3945_led_off(struct iwl_priv *priv, int led_id)
135 struct iwl_led_cmd led_cmd = {
139 .interval = IWL_DEF_LED_INTRVL
141 IWL_DEBUG_LED(priv, "led off %d\n", led_id);
142 return iwl_send_led_cmd(priv, &led_cmd);
146 * brightness call back function for Tx/Rx LED
148 static int iwl3945_led_associated(struct iwl_priv *priv, int led_id)
150 if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
151 !test_bit(STATUS_READY, &priv->status))
155 /* start counting Tx/Rx bytes */
156 if (!priv->last_blink_time && priv->allow_blinking)
157 priv->last_blink_time = jiffies;
162 * brightness call back for association and radio
164 static void iwl3945_led_brightness_set(struct led_classdev *led_cdev,
165 enum led_brightness brightness)
167 struct iwl_led *led = container_of(led_cdev,
168 struct iwl_led, led_dev);
169 struct iwl_priv *priv = led->priv;
171 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
174 switch (brightness) {
176 if (led->type == IWL_LED_TRG_ASSOC) {
177 priv->allow_blinking = 1;
178 IWL_DEBUG_LED(priv, "MAC is associated\n");
181 led->led_on(priv, IWL_LED_LINK);
184 if (led->type == IWL_LED_TRG_ASSOC) {
185 priv->allow_blinking = 0;
186 IWL_DEBUG_LED(priv, "MAC is disassociated\n");
189 led->led_off(priv, IWL_LED_LINK);
192 if (led->led_pattern) {
193 int idx = iwl3945_brightness_to_idx(brightness);
194 led->led_pattern(priv, IWL_LED_LINK, idx);
203 * Register led class with the system
205 static int iwl3945_led_register_led(struct iwl_priv *priv,
207 enum led_type type, u8 set_led,
210 struct device *device = wiphy_dev(priv->hw->wiphy);
213 led->led_dev.name = led->name;
214 led->led_dev.brightness_set = iwl3945_led_brightness_set;
215 led->led_dev.default_trigger = trigger;
220 ret = led_classdev_register(device, &led->led_dev);
222 IWL_ERR(priv, "Error: failed to register led handler.\n");
228 if (set_led && led->led_on)
229 led->led_on(priv, IWL_LED_LINK);
235 * calculate blink rate according to last 2 sec Tx/Rx activities
237 static inline u8 get_blink_rate(struct iwl_priv *priv)
240 u64 current_tpt = priv->rxtxpackets;
241 s64 tpt = current_tpt - priv->led_tpt;
245 priv->led_tpt = current_tpt;
247 if (!priv->allow_blinking)
248 index = IWL_MAX_BLINK_TBL;
250 for (index = 0; index < IWL_MAX_BLINK_TBL; index++)
251 if (tpt > (blink_tbl[index].brightness * IWL_1MB_RATE))
256 static inline int is_rf_kill(struct iwl_priv *priv)
258 return test_bit(STATUS_RF_KILL_HW, &priv->status) ||
259 test_bit(STATUS_RF_KILL_SW, &priv->status);
263 * this function called from handler. Since setting Led command can
264 * happen very frequent we postpone led command to be called from
265 * REPLY handler so we know ucode is up
267 void iwl3945_led_background(struct iwl_priv *priv)
271 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
272 priv->last_blink_time = 0;
275 if (is_rf_kill(priv)) {
276 priv->last_blink_time = 0;
280 if (!priv->allow_blinking) {
281 priv->last_blink_time = 0;
282 if (priv->last_blink_rate != IWL_SOLID_BLINK_IDX) {
283 priv->last_blink_rate = IWL_SOLID_BLINK_IDX;
284 iwl3945_led_pattern(priv, IWL_LED_LINK,
285 IWL_SOLID_BLINK_IDX);
289 if (!priv->last_blink_time ||
290 !time_after(jiffies, priv->last_blink_time +
291 msecs_to_jiffies(1000)))
294 blink_idx = get_blink_rate(priv);
296 /* call only if blink rate change */
297 if (blink_idx != priv->last_blink_rate)
298 iwl3945_led_pattern(priv, IWL_LED_LINK, blink_idx);
300 priv->last_blink_time = jiffies;
301 priv->last_blink_rate = blink_idx;
302 priv->rxtxpackets = 0;
306 /* Register all led handler */
307 int iwl3945_led_register(struct iwl_priv *priv)
312 priv->last_blink_rate = 0;
313 priv->rxtxpackets = 0;
315 priv->last_blink_time = 0;
316 priv->allow_blinking = 0;
318 trigger = ieee80211_get_radio_led_name(priv->hw);
319 snprintf(priv->led[IWL_LED_TRG_RADIO].name,
320 sizeof(priv->led[IWL_LED_TRG_RADIO].name), "iwl-%s::radio",
321 wiphy_name(priv->hw->wiphy));
323 priv->led[IWL_LED_TRG_RADIO].led_on = iwl3945_led_on;
324 priv->led[IWL_LED_TRG_RADIO].led_off = iwl3945_led_off;
325 priv->led[IWL_LED_TRG_RADIO].led_pattern = NULL;
327 ret = iwl3945_led_register_led(priv,
328 &priv->led[IWL_LED_TRG_RADIO],
329 IWL_LED_TRG_RADIO, 1, trigger);
334 trigger = ieee80211_get_assoc_led_name(priv->hw);
335 snprintf(priv->led[IWL_LED_TRG_ASSOC].name,
336 sizeof(priv->led[IWL_LED_TRG_ASSOC].name), "iwl-%s::assoc",
337 wiphy_name(priv->hw->wiphy));
339 ret = iwl3945_led_register_led(priv,
340 &priv->led[IWL_LED_TRG_ASSOC],
341 IWL_LED_TRG_ASSOC, 0, trigger);
343 /* for assoc always turn led on */
344 priv->led[IWL_LED_TRG_ASSOC].led_on = iwl3945_led_on;
345 priv->led[IWL_LED_TRG_ASSOC].led_off = iwl3945_led_on;
346 priv->led[IWL_LED_TRG_ASSOC].led_pattern = NULL;
351 trigger = ieee80211_get_rx_led_name(priv->hw);
352 snprintf(priv->led[IWL_LED_TRG_RX].name,
353 sizeof(priv->led[IWL_LED_TRG_RX].name), "iwl-%s::RX",
354 wiphy_name(priv->hw->wiphy));
356 ret = iwl3945_led_register_led(priv,
357 &priv->led[IWL_LED_TRG_RX],
358 IWL_LED_TRG_RX, 0, trigger);
360 priv->led[IWL_LED_TRG_RX].led_on = iwl3945_led_associated;
361 priv->led[IWL_LED_TRG_RX].led_off = iwl3945_led_associated;
362 priv->led[IWL_LED_TRG_RX].led_pattern = iwl3945_led_pattern;
367 trigger = ieee80211_get_tx_led_name(priv->hw);
368 snprintf(priv->led[IWL_LED_TRG_TX].name,
369 sizeof(priv->led[IWL_LED_TRG_TX].name), "iwl-%s::TX",
370 wiphy_name(priv->hw->wiphy));
372 ret = iwl3945_led_register_led(priv,
373 &priv->led[IWL_LED_TRG_TX],
374 IWL_LED_TRG_TX, 0, trigger);
376 priv->led[IWL_LED_TRG_TX].led_on = iwl3945_led_associated;
377 priv->led[IWL_LED_TRG_TX].led_off = iwl3945_led_associated;
378 priv->led[IWL_LED_TRG_TX].led_pattern = iwl3945_led_pattern;
386 iwl3945_led_unregister(priv);
391 /* unregister led class */
392 static void iwl3945_led_unregister_led(struct iwl_led *led, u8 set_led)
394 if (!led->registered)
397 led_classdev_unregister(&led->led_dev);
400 led->led_dev.brightness_set(&led->led_dev, LED_OFF);
404 /* Unregister all led handlers */
405 void iwl3945_led_unregister(struct iwl_priv *priv)
407 iwl3945_led_unregister_led(&priv->led[IWL_LED_TRG_ASSOC], 0);
408 iwl3945_led_unregister_led(&priv->led[IWL_LED_TRG_RX], 0);
409 iwl3945_led_unregister_led(&priv->led[IWL_LED_TRG_TX], 0);
410 iwl3945_led_unregister_led(&priv->led[IWL_LED_TRG_RADIO], 1);