iwl-led.c 12.1 KB
Newer Older
M
Mohamed Abbas 已提交
1 2
/******************************************************************************
 *
3
 * Copyright(c) 2003 - 2009 Intel Corporation. All rights reserved.
M
Mohamed Abbas 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
 *
 * The full GNU General Public License is included in this distribution in the
 * file called LICENSE.
 *
 * Contact Information:
22
 *  Intel Linux Wireless <ilw@linux.intel.com>
M
Mohamed Abbas 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
 *
 *****************************************************************************/


#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/pci.h>
#include <linux/dma-mapping.h>
#include <linux/delay.h>
#include <linux/skbuff.h>
#include <linux/netdevice.h>
#include <linux/wireless.h>
#include <net/mac80211.h>
#include <linux/etherdevice.h>
#include <asm/unaligned.h>

41
#include "iwl-dev.h"
M
Mohamed Abbas 已提交
42
#include "iwl-core.h"
43
#include "iwl-io.h"
M
Mohamed Abbas 已提交
44

45 46 47 48 49 50
/* default: IWL_LED_BLINK(0) using blinking index table */
static int led_mode;
module_param(led_mode, int, S_IRUGO);
MODULE_PARM_DESC(led_mode, "led mode: 0=blinking, 1=On(RF On)/Off(RF Off), "
			   "(default 0)\n");

T
Tomas Winkler 已提交
51 52 53 54 55 56 57 58 59 60 61
#ifdef CONFIG_IWLWIFI_DEBUG
static const char *led_type_str[] = {
	__stringify(IWL_LED_TRG_TX),
	__stringify(IWL_LED_TRG_RX),
	__stringify(IWL_LED_TRG_ASSOC),
	__stringify(IWL_LED_TRG_RADIO),
	NULL
};
#endif /* CONFIG_IWLWIFI_DEBUG */


M
Mohamed Abbas 已提交
62
static const struct {
63
	u16 tpt;	/* Mb/s */
M
Mohamed Abbas 已提交
64
	u8 on_time;
T
Tomas Winkler 已提交
65
	u8 off_time;
M
Mohamed Abbas 已提交
66 67 68 69 70 71 72 73
} blink_tbl[] =
{
	{300, 25, 25},
	{200, 40, 40},
	{100, 55, 55},
	{70, 65, 65},
	{50, 75, 75},
	{20, 85, 85},
W
Wey-Yi Guy 已提交
74 75 76
	{10, 95, 95},
	{5, 110, 110},
	{1, 130, 130},
77 78 79
	{0, 167, 167},
/* SOLID_ON */
	{-1, IWL_LED_SOLID, 0}
M
Mohamed Abbas 已提交
80 81
};

82 83 84 85 86
#define IWL_1MB_RATE (128 * 1024)
#define IWL_LED_THRESHOLD (16)
#define IWL_MAX_BLINK_TBL (ARRAY_SIZE(blink_tbl) - 1) /* exclude SOLID_ON */
#define IWL_SOLID_BLINK_IDX (ARRAY_SIZE(blink_tbl) - 1)

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
/*
 * Adjust led blink rate to compensate on a MAC Clock difference on every HW
 * Led blink rate analysis showed an average deviation of 0% on 3945,
 * 5% on 4965 HW and 20% on 5000 series and up.
 * Need to compensate on the led on/off time per HW according to the deviation
 * to achieve the desired led frequency
 * The calculation is: (100-averageDeviation)/100 * blinkTime
 * For code efficiency the calculation will be:
 *     compensation = (100 - averageDeviation) * 64 / 100
 *     NewBlinkTime = (compensation * BlinkTime) / 64
 */
static inline u8 iwl_blink_compensation(struct iwl_priv *priv,
				    u8 time, u16 compensation)
{
	if (!compensation) {
		IWL_ERR(priv, "undefined blink compensation: "
			"use pre-defined blinking time\n");
		return time;
	}

	return (u8)((time * compensation) >> 6);
}

110 111
/*  [0-256] -> [0..8] FIXME: we need [0..10] */
static inline int iwl_brightness_to_idx(enum led_brightness brightness)
M
Mohamed Abbas 已提交
112
{
113
	return fls(0x000000FF & (u32)brightness);
M
Mohamed Abbas 已提交
114 115 116
}

/* Send led command */
117
static int iwl_send_led_cmd(struct iwl_priv *priv, struct iwl_led_cmd *led_cmd)
M
Mohamed Abbas 已提交
118 119 120
{
	struct iwl_host_cmd cmd = {
		.id = REPLY_LEDS_CMD,
121
		.len = sizeof(struct iwl_led_cmd),
M
Mohamed Abbas 已提交
122
		.data = led_cmd,
J
Johannes Berg 已提交
123 124
		.flags = CMD_ASYNC,
		.callback = NULL,
M
Mohamed Abbas 已提交
125 126 127
	};
	u32 reg;

128
	reg = iwl_read32(priv, CSR_LED_REG);
M
Mohamed Abbas 已提交
129
	if (reg != (reg & CSR_LED_BSM_CTRL_MSK))
130
		iwl_write32(priv, CSR_LED_REG, reg & CSR_LED_BSM_CTRL_MSK);
M
Mohamed Abbas 已提交
131 132 133 134

	return iwl_send_cmd(priv, &cmd);
}

135
/* Set led pattern command */
W
Wey-Yi Guy 已提交
136
static int iwl_led_pattern(struct iwl_priv *priv, int led_id,
137
			       unsigned int idx)
M
Mohamed Abbas 已提交
138
{
139
	struct iwl_led_cmd led_cmd = {
M
Mohamed Abbas 已提交
140 141 142 143
		.id = led_id,
		.interval = IWL_DEF_LED_INTRVL
	};

144 145
	BUG_ON(idx > IWL_MAX_BLINK_TBL);

146 147 148 149 150 151 152 153
	IWL_DEBUG_LED(priv, "Led blink time compensation= %u\n",
			priv->cfg->led_compensation);
	led_cmd.on =
		iwl_blink_compensation(priv, blink_tbl[idx].on_time,
					priv->cfg->led_compensation);
	led_cmd.off =
		iwl_blink_compensation(priv, blink_tbl[idx].off_time,
					priv->cfg->led_compensation);
154

M
Mohamed Abbas 已提交
155 156 157 158
	return iwl_send_led_cmd(priv, &led_cmd);
}

/* Set led register off */
W
Wey-Yi Guy 已提交
159
static int iwl_led_on_reg(struct iwl_priv *priv, int led_id)
M
Mohamed Abbas 已提交
160
{
161
	IWL_DEBUG_LED(priv, "led on %d\n", led_id);
162
	iwl_write32(priv, CSR_LED_REG, CSR_LED_REG_TRUN_ON);
M
Mohamed Abbas 已提交
163 164 165 166
	return 0;
}

#if 0
167
/* Set led on command */
W
Wey-Yi Guy 已提交
168
static int iwl_led_on(struct iwl_priv *priv, int led_id)
169 170 171 172 173 174 175 176 177 178
{
	struct iwl_led_cmd led_cmd = {
		.id = led_id,
		.on = IWL_LED_SOLID,
		.off = 0,
		.interval = IWL_DEF_LED_INTRVL
	};
	return iwl_send_led_cmd(priv, &led_cmd);
}

M
Mohamed Abbas 已提交
179
/* Set led off command */
W
Wey-Yi Guy 已提交
180
int iwl_led_off(struct iwl_priv *priv, int led_id)
M
Mohamed Abbas 已提交
181
{
182
	struct iwl_led_cmd led_cmd = {
M
Mohamed Abbas 已提交
183 184 185 186 187
		.id = led_id,
		.on = 0,
		.off = 0,
		.interval = IWL_DEF_LED_INTRVL
	};
188
	IWL_DEBUG_LED(priv, "led off %d\n", led_id);
M
Mohamed Abbas 已提交
189 190 191 192 193 194
	return iwl_send_led_cmd(priv, &led_cmd);
}
#endif


/* Set led register off */
W
Wey-Yi Guy 已提交
195
static int iwl_led_off_reg(struct iwl_priv *priv, int led_id)
M
Mohamed Abbas 已提交
196
{
197
	IWL_DEBUG_LED(priv, "LED Reg off\n");
198
	iwl_write32(priv, CSR_LED_REG, CSR_LED_REG_TRUN_OFF);
M
Mohamed Abbas 已提交
199 200 201
	return 0;
}

202 203 204 205 206
/*
 * Set led register in case of disassociation according to rfkill state
 */
static int iwl_led_associate(struct iwl_priv *priv, int led_id)
{
207
	IWL_DEBUG_LED(priv, "Associated\n");
208 209
	if (led_mode == IWL_LED_BLINK)
		priv->allow_blinking = 1;
W
Wey-Yi Guy 已提交
210
	return iwl_led_on_reg(priv, led_id);
211 212 213 214 215 216 217 218
}
static int iwl_led_disassociate(struct iwl_priv *priv, int led_id)
{
	priv->allow_blinking = 0;

	return 0;
}

M
Mohamed Abbas 已提交
219 220 221
/*
 * brightness call back function for Tx/Rx LED
 */
222
static int iwl_led_associated(struct iwl_priv *priv, int led_id)
M
Mohamed Abbas 已提交
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
{
	if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
	    !test_bit(STATUS_READY, &priv->status))
		return 0;


	/* start counting Tx/Rx bytes */
	if (!priv->last_blink_time && priv->allow_blinking)
		priv->last_blink_time = jiffies;
	return 0;
}

/*
 * brightness call back for association and radio
 */
T
Tomas Winkler 已提交
238
static void iwl_led_brightness_set(struct led_classdev *led_cdev,
M
Mohamed Abbas 已提交
239 240
				       enum led_brightness brightness)
{
T
Tomas Winkler 已提交
241
	struct iwl_led *led = container_of(led_cdev, struct iwl_led, led_dev);
M
Mohamed Abbas 已提交
242 243 244 245 246
	struct iwl_priv *priv = led->priv;

	if (test_bit(STATUS_EXIT_PENDING, &priv->status))
		return;

T
Tomas Winkler 已提交
247

248
	IWL_DEBUG_LED(priv, "Led type = %s brightness = %d\n",
T
Tomas Winkler 已提交
249
			led_type_str[led->type], brightness);
M
Mohamed Abbas 已提交
250 251 252 253 254 255 256 257 258 259
	switch (brightness) {
	case LED_FULL:
		if (led->led_on)
			led->led_on(priv, IWL_LED_LINK);
		break;
	case LED_OFF:
		if (led->led_off)
			led->led_off(priv, IWL_LED_LINK);
		break;
	default:
260 261 262 263
		if (led->led_pattern) {
			int idx = iwl_brightness_to_idx(brightness);
			led->led_pattern(priv, IWL_LED_LINK, idx);
		}
M
Mohamed Abbas 已提交
264 265 266 267 268 269 270 271 272
		break;
	}
}



/*
 * Register led class with the system
 */
T
Tomas Winkler 已提交
273
static int iwl_leds_register_led(struct iwl_priv *priv, struct iwl_led *led,
M
Mohamed Abbas 已提交
274
				   enum led_type type, u8 set_led,
275
				   char *trigger)
M
Mohamed Abbas 已提交
276 277 278 279
{
	struct device *device = wiphy_dev(priv->hw->wiphy);
	int ret;

280
	led->led_dev.name = led->name;
T
Tomas Winkler 已提交
281
	led->led_dev.brightness_set = iwl_led_brightness_set;
M
Mohamed Abbas 已提交
282 283
	led->led_dev.default_trigger = trigger;

284 285 286
	led->priv = priv;
	led->type = type;

M
Mohamed Abbas 已提交
287 288
	ret = led_classdev_register(device, &led->led_dev);
	if (ret) {
289
		IWL_ERR(priv, "Error: failed to register led handler.\n");
M
Mohamed Abbas 已提交
290 291 292 293 294 295 296
		return ret;
	}

	led->registered = 1;

	if (set_led && led->led_on)
		led->led_on(priv, IWL_LED_LINK);
297

M
Mohamed Abbas 已提交
298 299 300 301 302
	return 0;
}


/*
303
 * calculate blink rate according to last second Tx/Rx activities
M
Mohamed Abbas 已提交
304
 */
305
static int iwl_get_blink_rate(struct iwl_priv *priv)
M
Mohamed Abbas 已提交
306 307
{
	int i;
308 309 310
	/* count both tx and rx traffic to be able to
	 * handle traffic in either direction
	 */
311 312
	u64 current_tpt = priv->tx_stats.data_bytes +
			  priv->rx_stats.data_bytes;
M
Mohamed Abbas 已提交
313 314
	s64 tpt = current_tpt - priv->led_tpt;

T
Tomas Winkler 已提交
315
	if (tpt < 0) /* wraparound */
M
Mohamed Abbas 已提交
316 317
		tpt = -tpt;

318
	IWL_DEBUG_LED(priv, "tpt %lld current_tpt %llu\n",
319 320
		(long long)tpt,
		(unsigned long long)current_tpt);
M
Mohamed Abbas 已提交
321 322
	priv->led_tpt = current_tpt;

323
	if (!priv->allow_blinking)
M
Mohamed Abbas 已提交
324
		i = IWL_MAX_BLINK_TBL;
325
	else
M
Mohamed Abbas 已提交
326 327 328 329
		for (i = 0; i < IWL_MAX_BLINK_TBL; i++)
			if (tpt  > (blink_tbl[i].tpt * IWL_1MB_RATE))
				break;

330
	IWL_DEBUG_LED(priv, "LED BLINK IDX=%d\n", i);
331
	return i;
M
Mohamed Abbas 已提交
332 333 334 335 336 337 338 339 340
}

/*
 * this function called from handler. Since setting Led command can
 * happen very frequent we postpone led command to be called from
 * REPLY handler so we know ucode is up
 */
void iwl_leds_background(struct iwl_priv *priv)
{
341
	u8 blink_idx;
M
Mohamed Abbas 已提交
342 343 344 345 346

	if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
		priv->last_blink_time = 0;
		return;
	}
347
	if (iwl_is_rfkill(priv)) {
M
Mohamed Abbas 已提交
348 349 350 351 352 353
		priv->last_blink_time = 0;
		return;
	}

	if (!priv->allow_blinking) {
		priv->last_blink_time = 0;
354 355
		if (priv->last_blink_rate != IWL_SOLID_BLINK_IDX) {
			priv->last_blink_rate = IWL_SOLID_BLINK_IDX;
W
Wey-Yi Guy 已提交
356
			iwl_led_pattern(priv, IWL_LED_LINK,
357
					    IWL_SOLID_BLINK_IDX);
M
Mohamed Abbas 已提交
358 359 360 361 362 363 364 365
		}
		return;
	}
	if (!priv->last_blink_time ||
	    !time_after(jiffies, priv->last_blink_time +
			msecs_to_jiffies(1000)))
		return;

366
	blink_idx = iwl_get_blink_rate(priv);
M
Mohamed Abbas 已提交
367 368

	/* call only if blink rate change */
369
	if (blink_idx != priv->last_blink_rate)
W
Wey-Yi Guy 已提交
370
		iwl_led_pattern(priv, IWL_LED_LINK, blink_idx);
M
Mohamed Abbas 已提交
371

T
Tomas Winkler 已提交
372
	priv->last_blink_time = jiffies;
373
	priv->last_blink_rate = blink_idx;
M
Mohamed Abbas 已提交
374 375 376 377 378 379 380 381 382 383 384 385 386 387
}

/* Register all led handler */
int iwl_leds_register(struct iwl_priv *priv)
{
	char *trigger;
	int ret;

	priv->last_blink_rate = 0;
	priv->led_tpt = 0;
	priv->last_blink_time = 0;
	priv->allow_blinking = 0;

	trigger = ieee80211_get_radio_led_name(priv->hw);
388
	snprintf(priv->led[IWL_LED_TRG_RADIO].name,
D
Danny Kukawka 已提交
389
		 sizeof(priv->led[IWL_LED_TRG_RADIO].name), "iwl-%s::radio",
M
Mohamed Abbas 已提交
390 391
		 wiphy_name(priv->hw->wiphy));

W
Wey-Yi Guy 已提交
392 393
	priv->led[IWL_LED_TRG_RADIO].led_on = iwl_led_on_reg;
	priv->led[IWL_LED_TRG_RADIO].led_off = iwl_led_off_reg;
M
Mohamed Abbas 已提交
394 395
	priv->led[IWL_LED_TRG_RADIO].led_pattern = NULL;

T
Tomas Winkler 已提交
396
	ret = iwl_leds_register_led(priv, &priv->led[IWL_LED_TRG_RADIO],
397
				   IWL_LED_TRG_RADIO, 1, trigger);
M
Mohamed Abbas 已提交
398 399 400 401
	if (ret)
		goto exit_fail;

	trigger = ieee80211_get_assoc_led_name(priv->hw);
402
	snprintf(priv->led[IWL_LED_TRG_ASSOC].name,
D
Danny Kukawka 已提交
403
		 sizeof(priv->led[IWL_LED_TRG_ASSOC].name), "iwl-%s::assoc",
M
Mohamed Abbas 已提交
404 405
		 wiphy_name(priv->hw->wiphy));

T
Tomas Winkler 已提交
406
	ret = iwl_leds_register_led(priv, &priv->led[IWL_LED_TRG_ASSOC],
407
				   IWL_LED_TRG_ASSOC, 0, trigger);
T
Tomas Winkler 已提交
408

M
Mohamed Abbas 已提交
409
	/* for assoc always turn led on */
410 411
	priv->led[IWL_LED_TRG_ASSOC].led_on = iwl_led_associate;
	priv->led[IWL_LED_TRG_ASSOC].led_off = iwl_led_disassociate;
M
Mohamed Abbas 已提交
412 413 414 415 416 417
	priv->led[IWL_LED_TRG_ASSOC].led_pattern = NULL;

	if (ret)
		goto exit_fail;

	trigger = ieee80211_get_rx_led_name(priv->hw);
418
	snprintf(priv->led[IWL_LED_TRG_RX].name,
D
Danny Kukawka 已提交
419
		 sizeof(priv->led[IWL_LED_TRG_RX].name), "iwl-%s::RX",
420
		 wiphy_name(priv->hw->wiphy));
M
Mohamed Abbas 已提交
421

T
Tomas Winkler 已提交
422
	ret = iwl_leds_register_led(priv, &priv->led[IWL_LED_TRG_RX],
423
				   IWL_LED_TRG_RX, 0, trigger);
M
Mohamed Abbas 已提交
424

425 426
	priv->led[IWL_LED_TRG_RX].led_on = iwl_led_associated;
	priv->led[IWL_LED_TRG_RX].led_off = iwl_led_associated;
W
Wey-Yi Guy 已提交
427
	priv->led[IWL_LED_TRG_RX].led_pattern = iwl_led_pattern;
M
Mohamed Abbas 已提交
428 429 430 431 432

	if (ret)
		goto exit_fail;

	trigger = ieee80211_get_tx_led_name(priv->hw);
433
	snprintf(priv->led[IWL_LED_TRG_TX].name,
D
Danny Kukawka 已提交
434
		 sizeof(priv->led[IWL_LED_TRG_TX].name), "iwl-%s::TX",
435 436
		 wiphy_name(priv->hw->wiphy));

T
Tomas Winkler 已提交
437
	ret = iwl_leds_register_led(priv, &priv->led[IWL_LED_TRG_TX],
438
				   IWL_LED_TRG_TX, 0, trigger);
T
Tomas Winkler 已提交
439

440 441
	priv->led[IWL_LED_TRG_TX].led_on = iwl_led_associated;
	priv->led[IWL_LED_TRG_TX].led_off = iwl_led_associated;
W
Wey-Yi Guy 已提交
442
	priv->led[IWL_LED_TRG_TX].led_pattern = iwl_led_pattern;
M
Mohamed Abbas 已提交
443 444 445 446 447 448 449 450 451 452 453 454 455

	if (ret)
		goto exit_fail;

	return 0;

exit_fail:
	iwl_leds_unregister(priv);
	return ret;
}
EXPORT_SYMBOL(iwl_leds_register);

/* unregister led class */
T
Tomas Winkler 已提交
456
static void iwl_leds_unregister_led(struct iwl_led *led, u8 set_led)
M
Mohamed Abbas 已提交
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
{
	if (!led->registered)
		return;

	led_classdev_unregister(&led->led_dev);

	if (set_led)
		led->led_dev.brightness_set(&led->led_dev, LED_OFF);
	led->registered = 0;
}

/* Unregister all led handlers */
void iwl_leds_unregister(struct iwl_priv *priv)
{
	iwl_leds_unregister_led(&priv->led[IWL_LED_TRG_ASSOC], 0);
	iwl_leds_unregister_led(&priv->led[IWL_LED_TRG_RX], 0);
	iwl_leds_unregister_led(&priv->led[IWL_LED_TRG_TX], 0);
	iwl_leds_unregister_led(&priv->led[IWL_LED_TRG_RADIO], 1);
}
EXPORT_SYMBOL(iwl_leds_unregister);