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>
41 #include "iwl-commands.h"
64 {-1, IWL_LED_SOLID, 0}
67 #define IWL_1MB_RATE (128 * 1024)
68 #define IWL_LED_THRESHOLD (16)
69 #define IWL_MAX_BLINK_TBL (ARRAY_SIZE(blink_tbl) - 1) /*Exclude Solid on*/
70 #define IWL_SOLID_BLINK_IDX (ARRAY_SIZE(blink_tbl) - 1)
72 static int iwl3945_led_cmd_callback(struct iwl_priv *priv,
79 static inline int iwl3945_brightness_to_idx(enum led_brightness brightness)
81 return fls(0x000000FF & (u32)brightness);
84 /* Send led command */
85 static int iwl_send_led_cmd(struct iwl_priv *priv,
86 struct iwl_led_cmd *led_cmd)
88 struct iwl_host_cmd cmd = {
90 .len = sizeof(struct iwl_led_cmd),
92 .meta.flags = CMD_ASYNC,
93 .meta.u.callback = iwl3945_led_cmd_callback,
96 return iwl_send_cmd(priv, &cmd);
101 /* Set led on command */
102 static int iwl3945_led_pattern(struct iwl_priv *priv, int led_id,
105 struct iwl_led_cmd led_cmd = {
107 .interval = IWL_DEF_LED_INTRVL
110 BUG_ON(idx > IWL_MAX_BLINK_TBL);
112 led_cmd.on = blink_tbl[idx].on_time;
113 led_cmd.off = blink_tbl[idx].off_time;
115 return iwl_send_led_cmd(priv, &led_cmd);
119 /* Set led on command */
120 static int iwl3945_led_on(struct iwl_priv *priv, int led_id)
122 struct iwl_led_cmd led_cmd = {
126 .interval = IWL_DEF_LED_INTRVL
128 return iwl_send_led_cmd(priv, &led_cmd);
131 /* Set led off command */
132 static int iwl3945_led_off(struct iwl_priv *priv, int led_id)
134 struct iwl_led_cmd led_cmd = {
138 .interval = IWL_DEF_LED_INTRVL
140 IWL_DEBUG_LED("led off %d\n", led_id);
141 return iwl_send_led_cmd(priv, &led_cmd);
145 * brightness call back function for Tx/Rx LED
147 static int iwl3945_led_associated(struct iwl_priv *priv, int led_id)
149 if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
150 !test_bit(STATUS_READY, &priv->status))
154 /* start counting Tx/Rx bytes */
155 if (!priv->last_blink_time && priv->allow_blinking)
156 priv->last_blink_time = jiffies;
161 * brightness call back for association and radio
163 static void iwl3945_led_brightness_set(struct led_classdev *led_cdev,
164 enum led_brightness brightness)
166 struct iwl3945_led *led = container_of(led_cdev,
167 struct iwl3945_led, led_dev);
168 struct iwl_priv *priv = led->priv;
170 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
173 switch (brightness) {
175 if (led->type == IWL_LED_TRG_ASSOC) {
176 priv->allow_blinking = 1;
177 IWL_DEBUG_LED("MAC is associated\n");
180 led->led_on(priv, IWL_LED_LINK);
183 if (led->type == IWL_LED_TRG_ASSOC) {
184 priv->allow_blinking = 0;
185 IWL_DEBUG_LED("MAC is disassociated\n");
188 led->led_off(priv, IWL_LED_LINK);
191 if (led->led_pattern) {
192 int idx = iwl3945_brightness_to_idx(brightness);
193 led->led_pattern(priv, IWL_LED_LINK, idx);
202 * Register led class with the system
204 static int iwl3945_led_register_led(struct iwl_priv *priv,
205 struct iwl3945_led *led,
206 enum led_type type, u8 set_led,
209 struct device *device = wiphy_dev(priv->hw->wiphy);
212 led->led_dev.name = led->name;
213 led->led_dev.brightness_set = iwl3945_led_brightness_set;
214 led->led_dev.default_trigger = trigger;
219 ret = led_classdev_register(device, &led->led_dev);
221 IWL_ERR(priv, "Error: failed to register led handler.\n");
227 if (set_led && led->led_on)
228 led->led_on(priv, IWL_LED_LINK);
234 * calculate blink rate according to last 2 sec Tx/Rx activities
236 static inline u8 get_blink_rate(struct iwl_priv *priv)
239 u64 current_tpt = priv->rxtxpackets;
240 s64 tpt = current_tpt - priv->led_tpt;
244 priv->led_tpt = current_tpt;
246 if (!priv->allow_blinking)
247 index = IWL_MAX_BLINK_TBL;
249 for (index = 0; index < IWL_MAX_BLINK_TBL; index++)
250 if (tpt > (blink_tbl[index].brightness * IWL_1MB_RATE))
255 static inline int is_rf_kill(struct iwl_priv *priv)
257 return test_bit(STATUS_RF_KILL_HW, &priv->status) ||
258 test_bit(STATUS_RF_KILL_SW, &priv->status);
262 * this function called from handler. Since setting Led command can
263 * happen very frequent we postpone led command to be called from
264 * REPLY handler so we know ucode is up
266 void iwl3945_led_background(struct iwl_priv *priv)
270 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
271 priv->last_blink_time = 0;
274 if (is_rf_kill(priv)) {
275 priv->last_blink_time = 0;
279 if (!priv->allow_blinking) {
280 priv->last_blink_time = 0;
281 if (priv->last_blink_rate != IWL_SOLID_BLINK_IDX) {
282 priv->last_blink_rate = IWL_SOLID_BLINK_IDX;
283 iwl3945_led_pattern(priv, IWL_LED_LINK,
284 IWL_SOLID_BLINK_IDX);
288 if (!priv->last_blink_time ||
289 !time_after(jiffies, priv->last_blink_time +
290 msecs_to_jiffies(1000)))
293 blink_idx = get_blink_rate(priv);
295 /* call only if blink rate change */
296 if (blink_idx != priv->last_blink_rate)
297 iwl3945_led_pattern(priv, IWL_LED_LINK, blink_idx);
299 priv->last_blink_time = jiffies;
300 priv->last_blink_rate = blink_idx;
301 priv->rxtxpackets = 0;
305 /* Register all led handler */
306 int iwl3945_led_register(struct iwl_priv *priv)
311 priv->last_blink_rate = 0;
312 priv->rxtxpackets = 0;
314 priv->last_blink_time = 0;
315 priv->allow_blinking = 0;
317 trigger = ieee80211_get_radio_led_name(priv->hw);
318 snprintf(priv->led39[IWL_LED_TRG_RADIO].name,
319 sizeof(priv->led39[IWL_LED_TRG_RADIO].name), "iwl-%s:radio",
320 wiphy_name(priv->hw->wiphy));
322 priv->led39[IWL_LED_TRG_RADIO].led_on = iwl3945_led_on;
323 priv->led39[IWL_LED_TRG_RADIO].led_off = iwl3945_led_off;
324 priv->led39[IWL_LED_TRG_RADIO].led_pattern = NULL;
326 ret = iwl3945_led_register_led(priv,
327 &priv->led39[IWL_LED_TRG_RADIO],
328 IWL_LED_TRG_RADIO, 1, trigger);
333 trigger = ieee80211_get_assoc_led_name(priv->hw);
334 snprintf(priv->led39[IWL_LED_TRG_ASSOC].name,
335 sizeof(priv->led39[IWL_LED_TRG_ASSOC].name), "iwl-%s:assoc",
336 wiphy_name(priv->hw->wiphy));
338 ret = iwl3945_led_register_led(priv,
339 &priv->led39[IWL_LED_TRG_ASSOC],
340 IWL_LED_TRG_ASSOC, 0, trigger);
342 /* for assoc always turn led on */
343 priv->led39[IWL_LED_TRG_ASSOC].led_on = iwl3945_led_on;
344 priv->led39[IWL_LED_TRG_ASSOC].led_off = iwl3945_led_on;
345 priv->led39[IWL_LED_TRG_ASSOC].led_pattern = NULL;
350 trigger = ieee80211_get_rx_led_name(priv->hw);
351 snprintf(priv->led39[IWL_LED_TRG_RX].name,
352 sizeof(priv->led39[IWL_LED_TRG_RX].name), "iwl-%s:RX",
353 wiphy_name(priv->hw->wiphy));
355 ret = iwl3945_led_register_led(priv,
356 &priv->led39[IWL_LED_TRG_RX],
357 IWL_LED_TRG_RX, 0, trigger);
359 priv->led39[IWL_LED_TRG_RX].led_on = iwl3945_led_associated;
360 priv->led39[IWL_LED_TRG_RX].led_off = iwl3945_led_associated;
361 priv->led39[IWL_LED_TRG_RX].led_pattern = iwl3945_led_pattern;
366 trigger = ieee80211_get_tx_led_name(priv->hw);
367 snprintf(priv->led39[IWL_LED_TRG_TX].name,
368 sizeof(priv->led39[IWL_LED_TRG_TX].name), "iwl-%s:TX",
369 wiphy_name(priv->hw->wiphy));
371 ret = iwl3945_led_register_led(priv,
372 &priv->led39[IWL_LED_TRG_TX],
373 IWL_LED_TRG_TX, 0, trigger);
375 priv->led39[IWL_LED_TRG_TX].led_on = iwl3945_led_associated;
376 priv->led39[IWL_LED_TRG_TX].led_off = iwl3945_led_associated;
377 priv->led39[IWL_LED_TRG_TX].led_pattern = iwl3945_led_pattern;
385 iwl3945_led_unregister(priv);
390 /* unregister led class */
391 static void iwl3945_led_unregister_led(struct iwl3945_led *led, u8 set_led)
393 if (!led->registered)
396 led_classdev_unregister(&led->led_dev);
399 led->led_dev.brightness_set(&led->led_dev, LED_OFF);
403 /* Unregister all led handlers */
404 void iwl3945_led_unregister(struct iwl_priv *priv)
406 iwl3945_led_unregister_led(&priv->led39[IWL_LED_TRG_ASSOC], 0);
407 iwl3945_led_unregister_led(&priv->led39[IWL_LED_TRG_RX], 0);
408 iwl3945_led_unregister_led(&priv->led39[IWL_LED_TRG_TX], 0);
409 iwl3945_led_unregister_led(&priv->led39[IWL_LED_TRG_RADIO], 1);