1 /******************************************************************************
3 * Copyright(c) 2003 - 2008 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 * James P. Ketrenos <ipw2100-admin@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/version.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>
44 #include "iwl-helpers.h"
46 #define IWL_1MB_RATE (128 * 1024)
47 #define IWL_LED_THRESHOLD (16)
48 #define IWL_MAX_BLINK_TBL (10)
68 static int iwl_led_cmd_callback(struct iwl_priv *priv,
69 struct iwl_cmd *cmd, struct sk_buff *skb)
75 /* Send led command */
76 static int iwl_send_led_cmd(struct iwl_priv *priv,
77 struct iwl4965_led_cmd *led_cmd)
79 struct iwl_host_cmd cmd = {
81 .len = sizeof(struct iwl4965_led_cmd),
83 .meta.flags = CMD_ASYNC,
84 .meta.u.callback = iwl_led_cmd_callback
88 reg = iwl4965_read32(priv, CSR_LED_REG);
89 if (reg != (reg & CSR_LED_BSM_CTRL_MSK))
90 iwl4965_write32(priv, CSR_LED_REG, reg & CSR_LED_BSM_CTRL_MSK);
92 return iwl_send_cmd(priv, &cmd);
96 /* Set led on command */
97 static int iwl4965_led_on(struct iwl_priv *priv, int led_id)
99 struct iwl4965_led_cmd led_cmd = {
103 .interval = IWL_DEF_LED_INTRVL
105 return iwl_send_led_cmd(priv, &led_cmd);
108 /* Set led on command */
109 static int iwl4965_led_pattern(struct iwl_priv *priv, int led_id,
110 enum led_brightness brightness)
112 struct iwl4965_led_cmd led_cmd = {
116 .interval = IWL_DEF_LED_INTRVL
118 if (brightness == LED_FULL) {
119 led_cmd.on = IWL_LED_SOLID;
122 return iwl_send_led_cmd(priv, &led_cmd);
125 /* Set led register off */
126 static int iwl4965_led_on_reg(struct iwl_priv *priv, int led_id)
128 IWL_DEBUG_LED("led on %d\n", led_id);
129 iwl4965_write32(priv, CSR_LED_REG, CSR_LED_REG_TRUN_ON);
134 /* Set led off command */
135 int iwl4965_led_off(struct iwl_priv *priv, int led_id)
137 struct iwl4965_led_cmd led_cmd = {
141 .interval = IWL_DEF_LED_INTRVL
143 IWL_DEBUG_LED("led off %d\n", led_id);
144 return iwl_send_led_cmd(priv, &led_cmd);
149 /* Set led register off */
150 static int iwl4965_led_off_reg(struct iwl_priv *priv, int led_id)
152 IWL_DEBUG_LED("radio off\n");
153 iwl4965_write32(priv, CSR_LED_REG, CSR_LED_REG_TRUN_OFF);
157 /* Set led blink command */
158 static int iwl4965_led_not_solid(struct iwl_priv *priv, int led_id,
161 struct iwl4965_led_cmd led_cmd = {
165 .interval = IWL_DEF_LED_INTRVL
168 return iwl_send_led_cmd(priv, &led_cmd);
173 * brightness call back function for Tx/Rx LED
175 static int iwl4965_led_associated(struct iwl_priv *priv, int led_id)
177 if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
178 !test_bit(STATUS_READY, &priv->status))
182 /* start counting Tx/Rx bytes */
183 if (!priv->last_blink_time && priv->allow_blinking)
184 priv->last_blink_time = jiffies;
189 * brightness call back for association and radio
191 static void iwl4965_led_brightness_set(struct led_classdev *led_cdev,
192 enum led_brightness brightness)
194 struct iwl4965_led *led = container_of(led_cdev,
195 struct iwl4965_led, led_dev);
196 struct iwl_priv *priv = led->priv;
198 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
201 switch (brightness) {
203 if (led->type == IWL_LED_TRG_ASSOC)
204 priv->allow_blinking = 1;
207 led->led_on(priv, IWL_LED_LINK);
210 if (led->type == IWL_LED_TRG_ASSOC)
211 priv->allow_blinking = 0;
214 led->led_off(priv, IWL_LED_LINK);
217 if (led->led_pattern)
218 led->led_pattern(priv, IWL_LED_LINK, brightness);
226 * Register led class with the system
228 static int iwl_leds_register_led(struct iwl_priv *priv,
229 struct iwl4965_led *led,
230 enum led_type type, u8 set_led,
231 const char *name, char *trigger)
233 struct device *device = wiphy_dev(priv->hw->wiphy);
236 led->led_dev.name = name;
237 led->led_dev.brightness_set = iwl4965_led_brightness_set;
238 led->led_dev.default_trigger = trigger;
240 ret = led_classdev_register(device, &led->led_dev);
242 IWL_ERROR("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)
263 u64 current_tpt = priv->tx_stats[2].bytes + priv->rx_stats[2].bytes;
264 s64 tpt = current_tpt - priv->led_tpt;
266 if (tpt < 0) /* wrapparound */
269 priv->led_tpt = current_tpt;
271 if (tpt < IWL_LED_THRESHOLD) {
272 i = IWL_MAX_BLINK_TBL;
274 for (i = 0; i < IWL_MAX_BLINK_TBL; i++)
275 if (tpt > (blink_tbl[i].tpt * IWL_1MB_RATE))
278 /* if 0 frame is transfered */
279 if ((i == IWL_MAX_BLINK_TBL) || !priv->allow_blinking)
280 blink_rate = IWL_LED_SOLID;
282 blink_rate = blink_tbl[i].on_time;
287 static inline int is_rf_kill(struct iwl_priv *priv)
289 return test_bit(STATUS_RF_KILL_HW, &priv->status) ||
290 test_bit(STATUS_RF_KILL_SW, &priv->status);
294 * this function called from handler. Since setting Led command can
295 * happen very frequent we postpone led command to be called from
296 * REPLY handler so we know ucode is up
298 void iwl_leds_background(struct iwl_priv *priv)
302 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
303 priv->last_blink_time = 0;
306 if (is_rf_kill(priv)) {
307 priv->last_blink_time = 0;
311 if (!priv->allow_blinking) {
312 priv->last_blink_time = 0;
313 if (priv->last_blink_rate != IWL_LED_SOLID) {
314 priv->last_blink_rate = IWL_LED_SOLID;
315 iwl4965_led_on(priv, IWL_LED_LINK);
319 if (!priv->last_blink_time ||
320 !time_after(jiffies, priv->last_blink_time +
321 msecs_to_jiffies(1000)))
324 blink_rate = get_blink_rate(priv);
326 /* call only if blink rate change */
327 if (blink_rate != priv->last_blink_rate) {
328 if (blink_rate != IWL_LED_SOLID) {
329 priv->last_blink_time = jiffies +
330 msecs_to_jiffies(1000);
331 iwl4965_led_not_solid(priv, IWL_LED_LINK, blink_rate);
333 priv->last_blink_time = 0;
334 iwl4965_led_on(priv, IWL_LED_LINK);
338 priv->last_blink_rate = blink_rate;
340 EXPORT_SYMBOL(iwl_leds_background);
342 /* Register all led handler */
343 int iwl_leds_register(struct iwl_priv *priv)
349 priv->last_blink_rate = 0;
351 priv->last_blink_time = 0;
352 priv->allow_blinking = 0;
354 trigger = ieee80211_get_radio_led_name(priv->hw);
355 snprintf(name, sizeof(name), "iwl-%s:radio",
356 wiphy_name(priv->hw->wiphy));
358 priv->led[IWL_LED_TRG_RADIO].led_on = iwl4965_led_on_reg;
359 priv->led[IWL_LED_TRG_RADIO].led_off = iwl4965_led_off_reg;
360 priv->led[IWL_LED_TRG_RADIO].led_pattern = NULL;
362 ret = iwl_leds_register_led(priv,
363 &priv->led[IWL_LED_TRG_RADIO],
364 IWL_LED_TRG_RADIO, 1,
369 trigger = ieee80211_get_assoc_led_name(priv->hw);
370 snprintf(name, sizeof(name), "iwl-%s:assoc",
371 wiphy_name(priv->hw->wiphy));
373 ret = iwl_leds_register_led(priv,
374 &priv->led[IWL_LED_TRG_ASSOC],
375 IWL_LED_TRG_ASSOC, 0,
377 /* for assoc always turn led on */
378 priv->led[IWL_LED_TRG_ASSOC].led_on = iwl4965_led_on_reg;
379 priv->led[IWL_LED_TRG_ASSOC].led_off = iwl4965_led_on_reg;
380 priv->led[IWL_LED_TRG_ASSOC].led_pattern = NULL;
385 trigger = ieee80211_get_rx_led_name(priv->hw);
386 snprintf(name, sizeof(name), "iwl-%s:RX",
387 wiphy_name(priv->hw->wiphy));
390 ret = iwl_leds_register_led(priv,
391 &priv->led[IWL_LED_TRG_RX],
395 priv->led[IWL_LED_TRG_RX].led_on = iwl4965_led_associated;
396 priv->led[IWL_LED_TRG_RX].led_off = iwl4965_led_associated;
397 priv->led[IWL_LED_TRG_RX].led_pattern = iwl4965_led_pattern;
402 trigger = ieee80211_get_tx_led_name(priv->hw);
403 snprintf(name, sizeof(name), "iwl-%s:TX",
404 wiphy_name(priv->hw->wiphy));
405 ret = iwl_leds_register_led(priv,
406 &priv->led[IWL_LED_TRG_TX],
409 priv->led[IWL_LED_TRG_TX].led_on = iwl4965_led_associated;
410 priv->led[IWL_LED_TRG_TX].led_off = iwl4965_led_associated;
411 priv->led[IWL_LED_TRG_TX].led_pattern = iwl4965_led_pattern;
419 iwl_leds_unregister(priv);
422 EXPORT_SYMBOL(iwl_leds_register);
424 /* unregister led class */
425 static void iwl_leds_unregister_led(struct iwl4965_led *led, u8 set_led)
427 if (!led->registered)
430 led_classdev_unregister(&led->led_dev);
433 led->led_dev.brightness_set(&led->led_dev, LED_OFF);
437 /* Unregister all led handlers */
438 void iwl_leds_unregister(struct iwl_priv *priv)
440 iwl_leds_unregister_led(&priv->led[IWL_LED_TRG_ASSOC], 0);
441 iwl_leds_unregister_led(&priv->led[IWL_LED_TRG_RX], 0);
442 iwl_leds_unregister_led(&priv->led[IWL_LED_TRG_TX], 0);
443 iwl_leds_unregister_led(&priv->led[IWL_LED_TRG_RADIO], 1);
445 EXPORT_SYMBOL(iwl_leds_unregister);