iwl-led.c 10.8 KB
Newer Older
Mohamed Abbas's avatar
Mohamed Abbas committed
1
2
/******************************************************************************
 *
3
 * Copyright(c) 2003 - 2009 Intel Corporation. All rights reserved.
Mohamed Abbas's avatar
Mohamed Abbas committed
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>
Mohamed Abbas's avatar
Mohamed Abbas committed
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"
Mohamed Abbas's avatar
Mohamed Abbas committed
42
#include "iwl-core.h"
43
#include "iwl-io.h"
Mohamed Abbas's avatar
Mohamed Abbas committed
44

Tomas Winkler's avatar
Tomas Winkler committed
45
46
47
48
49
50
51
52
53
54
55
#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 */


Mohamed Abbas's avatar
Mohamed Abbas committed
56
static const struct {
57
	u16 tpt;	/* Mb/s */
Mohamed Abbas's avatar
Mohamed Abbas committed
58
	u8 on_time;
Tomas Winkler's avatar
Tomas Winkler committed
59
	u8 off_time;
Mohamed Abbas's avatar
Mohamed Abbas committed
60
61
62
63
64
65
66
67
} blink_tbl[] =
{
	{300, 25, 25},
	{200, 40, 40},
	{100, 55, 55},
	{70, 65, 65},
	{50, 75, 75},
	{20, 85, 85},
68
69
70
	{10, 95, 95},
	{5, 110, 110},
	{1, 130, 130},
71
72
73
	{0, 167, 167},
/* SOLID_ON */
	{-1, IWL_LED_SOLID, 0}
Mohamed Abbas's avatar
Mohamed Abbas committed
74
75
};

76
77
78
79
80
81
82
#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)

/*  [0-256] -> [0..8] FIXME: we need [0..10] */
static inline int iwl_brightness_to_idx(enum led_brightness brightness)
Mohamed Abbas's avatar
Mohamed Abbas committed
83
{
84
	return fls(0x000000FF & (u32)brightness);
Mohamed Abbas's avatar
Mohamed Abbas committed
85
86
87
}

/* Send led command */
88
static int iwl_send_led_cmd(struct iwl_priv *priv, struct iwl_led_cmd *led_cmd)
Mohamed Abbas's avatar
Mohamed Abbas committed
89
90
91
{
	struct iwl_host_cmd cmd = {
		.id = REPLY_LEDS_CMD,
92
		.len = sizeof(struct iwl_led_cmd),
Mohamed Abbas's avatar
Mohamed Abbas committed
93
		.data = led_cmd,
94
95
		.flags = CMD_ASYNC,
		.callback = NULL,
Mohamed Abbas's avatar
Mohamed Abbas committed
96
97
98
	};
	u32 reg;

99
	reg = iwl_read32(priv, CSR_LED_REG);
Mohamed Abbas's avatar
Mohamed Abbas committed
100
	if (reg != (reg & CSR_LED_BSM_CTRL_MSK))
101
		iwl_write32(priv, CSR_LED_REG, reg & CSR_LED_BSM_CTRL_MSK);
Mohamed Abbas's avatar
Mohamed Abbas committed
102
103
104
105

	return iwl_send_cmd(priv, &cmd);
}

106
/* Set led pattern command */
107
static int iwl_led_pattern(struct iwl_priv *priv, int led_id,
108
			       unsigned int idx)
Mohamed Abbas's avatar
Mohamed Abbas committed
109
{
110
	struct iwl_led_cmd led_cmd = {
Mohamed Abbas's avatar
Mohamed Abbas committed
111
112
113
114
		.id = led_id,
		.interval = IWL_DEF_LED_INTRVL
	};

115
116
117
118
119
	BUG_ON(idx > IWL_MAX_BLINK_TBL);

	led_cmd.on = blink_tbl[idx].on_time;
	led_cmd.off = blink_tbl[idx].off_time;

Mohamed Abbas's avatar
Mohamed Abbas committed
120
121
122
123
	return iwl_send_led_cmd(priv, &led_cmd);
}

/* Set led register off */
124
static int iwl_led_on_reg(struct iwl_priv *priv, int led_id)
Mohamed Abbas's avatar
Mohamed Abbas committed
125
{
126
	IWL_DEBUG_LED(priv, "led on %d\n", led_id);
127
	iwl_write32(priv, CSR_LED_REG, CSR_LED_REG_TRUN_ON);
Mohamed Abbas's avatar
Mohamed Abbas committed
128
129
130
131
	return 0;
}

#if 0
132
/* Set led on command */
133
static int iwl_led_on(struct iwl_priv *priv, int led_id)
134
135
136
137
138
139
140
141
142
143
{
	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);
}

Mohamed Abbas's avatar
Mohamed Abbas committed
144
/* Set led off command */
145
int iwl_led_off(struct iwl_priv *priv, int led_id)
Mohamed Abbas's avatar
Mohamed Abbas committed
146
{
147
	struct iwl_led_cmd led_cmd = {
Mohamed Abbas's avatar
Mohamed Abbas committed
148
149
150
151
152
		.id = led_id,
		.on = 0,
		.off = 0,
		.interval = IWL_DEF_LED_INTRVL
	};
153
	IWL_DEBUG_LED(priv, "led off %d\n", led_id);
Mohamed Abbas's avatar
Mohamed Abbas committed
154
155
156
157
158
159
	return iwl_send_led_cmd(priv, &led_cmd);
}
#endif


/* Set led register off */
160
static int iwl_led_off_reg(struct iwl_priv *priv, int led_id)
Mohamed Abbas's avatar
Mohamed Abbas committed
161
{
162
	IWL_DEBUG_LED(priv, "LED Reg off\n");
163
	iwl_write32(priv, CSR_LED_REG, CSR_LED_REG_TRUN_OFF);
Mohamed Abbas's avatar
Mohamed Abbas committed
164
165
166
	return 0;
}

167
168
169
170
171
/*
 * Set led register in case of disassociation according to rfkill state
 */
static int iwl_led_associate(struct iwl_priv *priv, int led_id)
{
172
	IWL_DEBUG_LED(priv, "Associated\n");
173
	priv->allow_blinking = 1;
174
	return iwl_led_on_reg(priv, led_id);
175
176
177
178
179
180
181
182
}
static int iwl_led_disassociate(struct iwl_priv *priv, int led_id)
{
	priv->allow_blinking = 0;

	return 0;
}

Mohamed Abbas's avatar
Mohamed Abbas committed
183
184
185
/*
 * brightness call back function for Tx/Rx LED
 */
186
static int iwl_led_associated(struct iwl_priv *priv, int led_id)
Mohamed Abbas's avatar
Mohamed Abbas committed
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
{
	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
 */
Tomas Winkler's avatar
Tomas Winkler committed
202
static void iwl_led_brightness_set(struct led_classdev *led_cdev,
Mohamed Abbas's avatar
Mohamed Abbas committed
203
204
				       enum led_brightness brightness)
{
Tomas Winkler's avatar
Tomas Winkler committed
205
	struct iwl_led *led = container_of(led_cdev, struct iwl_led, led_dev);
Mohamed Abbas's avatar
Mohamed Abbas committed
206
207
208
209
210
	struct iwl_priv *priv = led->priv;

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

Tomas Winkler's avatar
Tomas Winkler committed
211

212
	IWL_DEBUG_LED(priv, "Led type = %s brightness = %d\n",
Tomas Winkler's avatar
Tomas Winkler committed
213
			led_type_str[led->type], brightness);
Mohamed Abbas's avatar
Mohamed Abbas committed
214
215
216
217
218
219
220
221
222
223
	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:
224
225
226
227
		if (led->led_pattern) {
			int idx = iwl_brightness_to_idx(brightness);
			led->led_pattern(priv, IWL_LED_LINK, idx);
		}
Mohamed Abbas's avatar
Mohamed Abbas committed
228
229
230
231
232
233
234
235
236
		break;
	}
}



/*
 * Register led class with the system
 */
Tomas Winkler's avatar
Tomas Winkler committed
237
static int iwl_leds_register_led(struct iwl_priv *priv, struct iwl_led *led,
Mohamed Abbas's avatar
Mohamed Abbas committed
238
				   enum led_type type, u8 set_led,
239
				   char *trigger)
Mohamed Abbas's avatar
Mohamed Abbas committed
240
241
242
243
{
	struct device *device = wiphy_dev(priv->hw->wiphy);
	int ret;

244
	led->led_dev.name = led->name;
Tomas Winkler's avatar
Tomas Winkler committed
245
	led->led_dev.brightness_set = iwl_led_brightness_set;
Mohamed Abbas's avatar
Mohamed Abbas committed
246
247
	led->led_dev.default_trigger = trigger;

248
249
250
	led->priv = priv;
	led->type = type;

Mohamed Abbas's avatar
Mohamed Abbas committed
251
252
	ret = led_classdev_register(device, &led->led_dev);
	if (ret) {
253
		IWL_ERR(priv, "Error: failed to register led handler.\n");
Mohamed Abbas's avatar
Mohamed Abbas committed
254
255
256
257
258
259
260
		return ret;
	}

	led->registered = 1;

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

Mohamed Abbas's avatar
Mohamed Abbas committed
262
263
264
265
266
	return 0;
}


/*
267
 * calculate blink rate according to last second Tx/Rx activities
Mohamed Abbas's avatar
Mohamed Abbas committed
268
 */
269
static int iwl_get_blink_rate(struct iwl_priv *priv)
Mohamed Abbas's avatar
Mohamed Abbas committed
270
271
{
	int i;
272
273
274
	/* count both tx and rx traffic to be able to
	 * handle traffic in either direction
	 */
275
276
	u64 current_tpt = priv->tx_stats.data_bytes +
			  priv->rx_stats.data_bytes;
Mohamed Abbas's avatar
Mohamed Abbas committed
277
278
	s64 tpt = current_tpt - priv->led_tpt;

279
	if (tpt < 0) /* wraparound */
Mohamed Abbas's avatar
Mohamed Abbas committed
280
281
		tpt = -tpt;

282
	IWL_DEBUG_LED(priv, "tpt %lld current_tpt %llu\n",
283
284
		(long long)tpt,
		(unsigned long long)current_tpt);
Mohamed Abbas's avatar
Mohamed Abbas committed
285
286
	priv->led_tpt = current_tpt;

287
	if (!priv->allow_blinking)
Mohamed Abbas's avatar
Mohamed Abbas committed
288
		i = IWL_MAX_BLINK_TBL;
289
	else
Mohamed Abbas's avatar
Mohamed Abbas committed
290
291
292
293
		for (i = 0; i < IWL_MAX_BLINK_TBL; i++)
			if (tpt  > (blink_tbl[i].tpt * IWL_1MB_RATE))
				break;

294
	IWL_DEBUG_LED(priv, "LED BLINK IDX=%d\n", i);
295
	return i;
Mohamed Abbas's avatar
Mohamed Abbas committed
296
297
298
299
300
301
302
303
304
}

/*
 * 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)
{
305
	u8 blink_idx;
Mohamed Abbas's avatar
Mohamed Abbas committed
306
307
308
309
310

	if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
		priv->last_blink_time = 0;
		return;
	}
311
	if (iwl_is_rfkill(priv)) {
Mohamed Abbas's avatar
Mohamed Abbas committed
312
313
314
315
316
317
		priv->last_blink_time = 0;
		return;
	}

	if (!priv->allow_blinking) {
		priv->last_blink_time = 0;
318
319
		if (priv->last_blink_rate != IWL_SOLID_BLINK_IDX) {
			priv->last_blink_rate = IWL_SOLID_BLINK_IDX;
320
			iwl_led_pattern(priv, IWL_LED_LINK,
321
					    IWL_SOLID_BLINK_IDX);
Mohamed Abbas's avatar
Mohamed Abbas committed
322
323
324
325
326
327
328
329
		}
		return;
	}
	if (!priv->last_blink_time ||
	    !time_after(jiffies, priv->last_blink_time +
			msecs_to_jiffies(1000)))
		return;

330
	blink_idx = iwl_get_blink_rate(priv);
Mohamed Abbas's avatar
Mohamed Abbas committed
331
332

	/* call only if blink rate change */
333
	if (blink_idx != priv->last_blink_rate)
334
		iwl_led_pattern(priv, IWL_LED_LINK, blink_idx);
Mohamed Abbas's avatar
Mohamed Abbas committed
335

Tomas Winkler's avatar
Tomas Winkler committed
336
	priv->last_blink_time = jiffies;
337
	priv->last_blink_rate = blink_idx;
Mohamed Abbas's avatar
Mohamed Abbas committed
338
339
340
341
342
343
344
345
346
347
348
349
350
351
}

/* 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);
352
	snprintf(priv->led[IWL_LED_TRG_RADIO].name,
Danny Kukawka's avatar
Danny Kukawka committed
353
		 sizeof(priv->led[IWL_LED_TRG_RADIO].name), "iwl-%s::radio",
Mohamed Abbas's avatar
Mohamed Abbas committed
354
355
		 wiphy_name(priv->hw->wiphy));

356
357
	priv->led[IWL_LED_TRG_RADIO].led_on = iwl_led_on_reg;
	priv->led[IWL_LED_TRG_RADIO].led_off = iwl_led_off_reg;
Mohamed Abbas's avatar
Mohamed Abbas committed
358
359
	priv->led[IWL_LED_TRG_RADIO].led_pattern = NULL;

Tomas Winkler's avatar
Tomas Winkler committed
360
	ret = iwl_leds_register_led(priv, &priv->led[IWL_LED_TRG_RADIO],
361
				   IWL_LED_TRG_RADIO, 1, trigger);
Mohamed Abbas's avatar
Mohamed Abbas committed
362
363
364
365
	if (ret)
		goto exit_fail;

	trigger = ieee80211_get_assoc_led_name(priv->hw);
366
	snprintf(priv->led[IWL_LED_TRG_ASSOC].name,
Danny Kukawka's avatar
Danny Kukawka committed
367
		 sizeof(priv->led[IWL_LED_TRG_ASSOC].name), "iwl-%s::assoc",
Mohamed Abbas's avatar
Mohamed Abbas committed
368
369
		 wiphy_name(priv->hw->wiphy));

Tomas Winkler's avatar
Tomas Winkler committed
370
	ret = iwl_leds_register_led(priv, &priv->led[IWL_LED_TRG_ASSOC],
371
				   IWL_LED_TRG_ASSOC, 0, trigger);
Tomas Winkler's avatar
Tomas Winkler committed
372

Mohamed Abbas's avatar
Mohamed Abbas committed
373
	/* for assoc always turn led on */
374
375
	priv->led[IWL_LED_TRG_ASSOC].led_on = iwl_led_associate;
	priv->led[IWL_LED_TRG_ASSOC].led_off = iwl_led_disassociate;
Mohamed Abbas's avatar
Mohamed Abbas committed
376
377
378
379
380
381
	priv->led[IWL_LED_TRG_ASSOC].led_pattern = NULL;

	if (ret)
		goto exit_fail;

	trigger = ieee80211_get_rx_led_name(priv->hw);
382
	snprintf(priv->led[IWL_LED_TRG_RX].name,
Danny Kukawka's avatar
Danny Kukawka committed
383
		 sizeof(priv->led[IWL_LED_TRG_RX].name), "iwl-%s::RX",
384
		 wiphy_name(priv->hw->wiphy));
Mohamed Abbas's avatar
Mohamed Abbas committed
385

Tomas Winkler's avatar
Tomas Winkler committed
386
	ret = iwl_leds_register_led(priv, &priv->led[IWL_LED_TRG_RX],
387
				   IWL_LED_TRG_RX, 0, trigger);
Mohamed Abbas's avatar
Mohamed Abbas committed
388

389
390
	priv->led[IWL_LED_TRG_RX].led_on = iwl_led_associated;
	priv->led[IWL_LED_TRG_RX].led_off = iwl_led_associated;
391
	priv->led[IWL_LED_TRG_RX].led_pattern = iwl_led_pattern;
Mohamed Abbas's avatar
Mohamed Abbas committed
392
393
394
395
396

	if (ret)
		goto exit_fail;

	trigger = ieee80211_get_tx_led_name(priv->hw);
397
	snprintf(priv->led[IWL_LED_TRG_TX].name,
Danny Kukawka's avatar
Danny Kukawka committed
398
		 sizeof(priv->led[IWL_LED_TRG_TX].name), "iwl-%s::TX",
399
400
		 wiphy_name(priv->hw->wiphy));

Tomas Winkler's avatar
Tomas Winkler committed
401
	ret = iwl_leds_register_led(priv, &priv->led[IWL_LED_TRG_TX],
402
				   IWL_LED_TRG_TX, 0, trigger);
Tomas Winkler's avatar
Tomas Winkler committed
403

404
405
	priv->led[IWL_LED_TRG_TX].led_on = iwl_led_associated;
	priv->led[IWL_LED_TRG_TX].led_off = iwl_led_associated;
406
	priv->led[IWL_LED_TRG_TX].led_pattern = iwl_led_pattern;
Mohamed Abbas's avatar
Mohamed Abbas committed
407
408
409
410
411
412
413
414
415
416
417
418
419

	if (ret)
		goto exit_fail;

	return 0;

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

/* unregister led class */
Tomas Winkler's avatar
Tomas Winkler committed
420
static void iwl_leds_unregister_led(struct iwl_led *led, u8 set_led)
Mohamed Abbas's avatar
Mohamed Abbas committed
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
{
	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);