blob: 61250e6a7d1b32a0867aa495efdbae3258f1d838 [file] [log] [blame]
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07001/******************************************************************************
2 *
3 * Copyright(c) 2003 - 2008 Intel Corporation. All rights reserved.
4 *
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.
8 *
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
12 * more details.
13 *
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
17 *
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
20 *
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
24 *
25 *****************************************************************************/
26
27
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>
41
Tomas Winkler3e0d4cb2008-04-24 11:55:38 -070042#include "iwl-dev.h"
Mohamed Abbasab53d8a2008-03-25 16:33:36 -070043#include "iwl-core.h"
Tomas Winklerfee12472008-04-03 16:05:21 -070044#include "iwl-io.h"
Mohamed Abbasab53d8a2008-03-25 16:33:36 -070045#include "iwl-helpers.h"
46
Tomas Winkler0eee6122008-07-11 11:53:36 +080047#ifdef CONFIG_IWLWIFI_DEBUG
48static const char *led_type_str[] = {
49 __stringify(IWL_LED_TRG_TX),
50 __stringify(IWL_LED_TRG_RX),
51 __stringify(IWL_LED_TRG_ASSOC),
52 __stringify(IWL_LED_TRG_RADIO),
53 NULL
54};
55#endif /* CONFIG_IWLWIFI_DEBUG */
56
57
Mohamed Abbasab53d8a2008-03-25 16:33:36 -070058static const struct {
59 u16 tpt;
60 u8 on_time;
Tomas Winkler0eee6122008-07-11 11:53:36 +080061 u8 off_time;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -070062} blink_tbl[] =
63{
64 {300, 25, 25},
65 {200, 40, 40},
66 {100, 55, 55},
67 {70, 65, 65},
68 {50, 75, 75},
69 {20, 85, 85},
70 {15, 95, 95 },
71 {10, 110, 110},
72 {5, 130, 130},
Tomas Winklerec1a7462008-07-11 11:53:37 +080073 {0, 167, 167},
74/* SOLID_ON */
75 {-1, IWL_LED_SOLID, 0}
Mohamed Abbasab53d8a2008-03-25 16:33:36 -070076};
77
Tomas Winklerec1a7462008-07-11 11:53:37 +080078#define IWL_1MB_RATE (128 * 1024)
79#define IWL_LED_THRESHOLD (16)
80#define IWL_MAX_BLINK_TBL (ARRAY_SIZE(blink_tbl) - 1) /* exclude SOLID_ON */
81#define IWL_SOLID_BLINK_IDX (ARRAY_SIZE(blink_tbl) - 1)
82
83/* [0-256] -> [0..8] FIXME: we need [0..10] */
84static inline int iwl_brightness_to_idx(enum led_brightness brightness)
Mohamed Abbasab53d8a2008-03-25 16:33:36 -070085{
Tomas Winklerec1a7462008-07-11 11:53:37 +080086 return fls(0x000000FF & (u32)brightness);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -070087}
88
Mohamed Abbasab53d8a2008-03-25 16:33:36 -070089/* Send led command */
Tomas Winklerec1a7462008-07-11 11:53:37 +080090static int iwl_send_led_cmd(struct iwl_priv *priv, struct iwl_led_cmd *led_cmd)
Mohamed Abbasab53d8a2008-03-25 16:33:36 -070091{
92 struct iwl_host_cmd cmd = {
93 .id = REPLY_LEDS_CMD,
Tomas Winklerec1a7462008-07-11 11:53:37 +080094 .len = sizeof(struct iwl_led_cmd),
Mohamed Abbasab53d8a2008-03-25 16:33:36 -070095 .data = led_cmd,
96 .meta.flags = CMD_ASYNC,
Tomas Winklerec1a7462008-07-11 11:53:37 +080097 .meta.u.callback = NULL,
Mohamed Abbasab53d8a2008-03-25 16:33:36 -070098 };
99 u32 reg;
100
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700101 reg = iwl_read32(priv, CSR_LED_REG);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700102 if (reg != (reg & CSR_LED_BSM_CTRL_MSK))
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700103 iwl_write32(priv, CSR_LED_REG, reg & CSR_LED_BSM_CTRL_MSK);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700104
105 return iwl_send_cmd(priv, &cmd);
106}
107
Tomas Winklerec1a7462008-07-11 11:53:37 +0800108/* Set led pattern command */
Abhijeet Kolekar3eb20112008-07-11 11:53:42 +0800109static int iwl4965_led_pattern(struct iwl_priv *priv, int led_id,
110 unsigned int idx)
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700111{
Tomas Winklerec1a7462008-07-11 11:53:37 +0800112 struct iwl_led_cmd led_cmd = {
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700113 .id = led_id,
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700114 .interval = IWL_DEF_LED_INTRVL
115 };
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700116
Tomas Winklerec1a7462008-07-11 11:53:37 +0800117 BUG_ON(idx > IWL_MAX_BLINK_TBL);
118
119 led_cmd.on = blink_tbl[idx].on_time;
120 led_cmd.off = blink_tbl[idx].off_time;
121
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700122 return iwl_send_led_cmd(priv, &led_cmd);
123}
124
125/* Set led register off */
126static int iwl4965_led_on_reg(struct iwl_priv *priv, int led_id)
127{
128 IWL_DEBUG_LED("led on %d\n", led_id);
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700129 iwl_write32(priv, CSR_LED_REG, CSR_LED_REG_TRUN_ON);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700130 return 0;
131}
132
133#if 0
Tomas Winklerec1a7462008-07-11 11:53:37 +0800134/* Set led on command */
135static int iwl4965_led_on(struct iwl_priv *priv, int led_id)
136{
137 struct iwl_led_cmd led_cmd = {
138 .id = led_id,
139 .on = IWL_LED_SOLID,
140 .off = 0,
141 .interval = IWL_DEF_LED_INTRVL
142 };
143 return iwl_send_led_cmd(priv, &led_cmd);
144}
145
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700146/* Set led off command */
147int iwl4965_led_off(struct iwl_priv *priv, int led_id)
148{
Tomas Winklerec1a7462008-07-11 11:53:37 +0800149 struct iwl_led_cmd led_cmd = {
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700150 .id = led_id,
151 .on = 0,
152 .off = 0,
153 .interval = IWL_DEF_LED_INTRVL
154 };
155 IWL_DEBUG_LED("led off %d\n", led_id);
156 return iwl_send_led_cmd(priv, &led_cmd);
157}
158#endif
159
160
161/* Set led register off */
162static int iwl4965_led_off_reg(struct iwl_priv *priv, int led_id)
163{
164 IWL_DEBUG_LED("radio off\n");
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700165 iwl_write32(priv, CSR_LED_REG, CSR_LED_REG_TRUN_OFF);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700166 return 0;
167}
168
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700169/*
170 * brightness call back function for Tx/Rx LED
171 */
Tomas Winklerec1a7462008-07-11 11:53:37 +0800172static int iwl_led_associated(struct iwl_priv *priv, int led_id)
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700173{
174 if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
175 !test_bit(STATUS_READY, &priv->status))
176 return 0;
177
178
179 /* start counting Tx/Rx bytes */
180 if (!priv->last_blink_time && priv->allow_blinking)
181 priv->last_blink_time = jiffies;
182 return 0;
183}
184
185/*
186 * brightness call back for association and radio
187 */
Tomas Winkler0eee6122008-07-11 11:53:36 +0800188static void iwl_led_brightness_set(struct led_classdev *led_cdev,
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700189 enum led_brightness brightness)
190{
Tomas Winkler0eee6122008-07-11 11:53:36 +0800191 struct iwl_led *led = container_of(led_cdev, struct iwl_led, led_dev);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700192 struct iwl_priv *priv = led->priv;
193
194 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
195 return;
196
Tomas Winkler0eee6122008-07-11 11:53:36 +0800197
198 IWL_DEBUG_LED("Led type = %s brightness = %d\n",
199 led_type_str[led->type], brightness);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700200 switch (brightness) {
201 case LED_FULL:
202 if (led->type == IWL_LED_TRG_ASSOC)
203 priv->allow_blinking = 1;
204
205 if (led->led_on)
206 led->led_on(priv, IWL_LED_LINK);
207 break;
208 case LED_OFF:
209 if (led->type == IWL_LED_TRG_ASSOC)
210 priv->allow_blinking = 0;
211
212 if (led->led_off)
213 led->led_off(priv, IWL_LED_LINK);
214 break;
215 default:
Tomas Winklerec1a7462008-07-11 11:53:37 +0800216 if (led->led_pattern) {
217 int idx = iwl_brightness_to_idx(brightness);
218 led->led_pattern(priv, IWL_LED_LINK, idx);
219 }
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700220 break;
221 }
222}
223
224
225
226/*
227 * Register led class with the system
228 */
Tomas Winkler0eee6122008-07-11 11:53:36 +0800229static int iwl_leds_register_led(struct iwl_priv *priv, struct iwl_led *led,
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700230 enum led_type type, u8 set_led,
231 const char *name, char *trigger)
232{
233 struct device *device = wiphy_dev(priv->hw->wiphy);
234 int ret;
235
236 led->led_dev.name = name;
Tomas Winkler0eee6122008-07-11 11:53:36 +0800237 led->led_dev.brightness_set = iwl_led_brightness_set;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700238 led->led_dev.default_trigger = trigger;
239
Tomas Winklera571ea42008-03-28 16:21:11 -0700240 led->priv = priv;
241 led->type = type;
242
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700243 ret = led_classdev_register(device, &led->led_dev);
244 if (ret) {
245 IWL_ERROR("Error: failed to register led handler.\n");
246 return ret;
247 }
248
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700249 led->registered = 1;
250
251 if (set_led && led->led_on)
252 led->led_on(priv, IWL_LED_LINK);
Tomas Winklera571ea42008-03-28 16:21:11 -0700253
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700254 return 0;
255}
256
257
258/*
259 * calculate blink rate according to last 2 sec Tx/Rx activities
260 */
Tomas Winklerec1a7462008-07-11 11:53:37 +0800261static int iwl_get_blink_rate(struct iwl_priv *priv)
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700262{
263 int i;
Tomas Winkler0eee6122008-07-11 11:53:36 +0800264 u64 current_tpt = priv->tx_stats[2].bytes;
265 /* FIXME: + priv->rx_stats[2].bytes; */
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700266 s64 tpt = current_tpt - priv->led_tpt;
267
268 if (tpt < 0) /* wrapparound */
269 tpt = -tpt;
270
Andrew Morton03121102008-07-22 23:50:04 -0700271 IWL_DEBUG_LED("tpt %lld current_tpt %llu\n",
272 (long long)tpt,
273 (unsigned long long)current_tpt);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700274 priv->led_tpt = current_tpt;
275
Tomas Winklerec1a7462008-07-11 11:53:37 +0800276 if (!priv->allow_blinking)
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700277 i = IWL_MAX_BLINK_TBL;
Tomas Winklerec1a7462008-07-11 11:53:37 +0800278 else
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700279 for (i = 0; i < IWL_MAX_BLINK_TBL; i++)
280 if (tpt > (blink_tbl[i].tpt * IWL_1MB_RATE))
281 break;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700282
Tomas Winklerec1a7462008-07-11 11:53:37 +0800283 IWL_DEBUG_LED("LED BLINK IDX=%d", i);
284 return i;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700285}
286
287static inline int is_rf_kill(struct iwl_priv *priv)
288{
289 return test_bit(STATUS_RF_KILL_HW, &priv->status) ||
290 test_bit(STATUS_RF_KILL_SW, &priv->status);
291}
292
293/*
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
297 */
298void iwl_leds_background(struct iwl_priv *priv)
299{
Tomas Winklerec1a7462008-07-11 11:53:37 +0800300 u8 blink_idx;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700301
302 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
303 priv->last_blink_time = 0;
304 return;
305 }
306 if (is_rf_kill(priv)) {
307 priv->last_blink_time = 0;
308 return;
309 }
310
311 if (!priv->allow_blinking) {
312 priv->last_blink_time = 0;
Tomas Winklerec1a7462008-07-11 11:53:37 +0800313 if (priv->last_blink_rate != IWL_SOLID_BLINK_IDX) {
314 priv->last_blink_rate = IWL_SOLID_BLINK_IDX;
315 iwl4965_led_pattern(priv, IWL_LED_LINK,
316 IWL_SOLID_BLINK_IDX);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700317 }
318 return;
319 }
320 if (!priv->last_blink_time ||
321 !time_after(jiffies, priv->last_blink_time +
322 msecs_to_jiffies(1000)))
323 return;
324
Tomas Winklerec1a7462008-07-11 11:53:37 +0800325 blink_idx = iwl_get_blink_rate(priv);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700326
327 /* call only if blink rate change */
Tomas Winklerec1a7462008-07-11 11:53:37 +0800328 if (blink_idx != priv->last_blink_rate)
329 iwl4965_led_pattern(priv, IWL_LED_LINK, blink_idx);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700330
Tomas Winkler0eee6122008-07-11 11:53:36 +0800331 priv->last_blink_time = jiffies;
Tomas Winklerec1a7462008-07-11 11:53:37 +0800332 priv->last_blink_rate = blink_idx;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700333}
334EXPORT_SYMBOL(iwl_leds_background);
335
336/* Register all led handler */
337int iwl_leds_register(struct iwl_priv *priv)
338{
339 char *trigger;
340 char name[32];
341 int ret;
342
343 priv->last_blink_rate = 0;
344 priv->led_tpt = 0;
345 priv->last_blink_time = 0;
346 priv->allow_blinking = 0;
347
348 trigger = ieee80211_get_radio_led_name(priv->hw);
349 snprintf(name, sizeof(name), "iwl-%s:radio",
350 wiphy_name(priv->hw->wiphy));
351
352 priv->led[IWL_LED_TRG_RADIO].led_on = iwl4965_led_on_reg;
353 priv->led[IWL_LED_TRG_RADIO].led_off = iwl4965_led_off_reg;
354 priv->led[IWL_LED_TRG_RADIO].led_pattern = NULL;
355
Tomas Winkler0eee6122008-07-11 11:53:36 +0800356 ret = iwl_leds_register_led(priv, &priv->led[IWL_LED_TRG_RADIO],
357 IWL_LED_TRG_RADIO, 1, name, trigger);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700358 if (ret)
359 goto exit_fail;
360
361 trigger = ieee80211_get_assoc_led_name(priv->hw);
362 snprintf(name, sizeof(name), "iwl-%s:assoc",
363 wiphy_name(priv->hw->wiphy));
364
Tomas Winkler0eee6122008-07-11 11:53:36 +0800365 ret = iwl_leds_register_led(priv, &priv->led[IWL_LED_TRG_ASSOC],
366 IWL_LED_TRG_ASSOC, 0, name, trigger);
367
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700368 /* for assoc always turn led on */
369 priv->led[IWL_LED_TRG_ASSOC].led_on = iwl4965_led_on_reg;
370 priv->led[IWL_LED_TRG_ASSOC].led_off = iwl4965_led_on_reg;
371 priv->led[IWL_LED_TRG_ASSOC].led_pattern = NULL;
372
373 if (ret)
374 goto exit_fail;
375
376 trigger = ieee80211_get_rx_led_name(priv->hw);
Tomas Winkler0eee6122008-07-11 11:53:36 +0800377 snprintf(name, sizeof(name), "iwl-%s:RX", wiphy_name(priv->hw->wiphy));
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700378
379
Tomas Winkler0eee6122008-07-11 11:53:36 +0800380 ret = iwl_leds_register_led(priv, &priv->led[IWL_LED_TRG_RX],
381 IWL_LED_TRG_RX, 0, name, trigger);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700382
Tomas Winklerec1a7462008-07-11 11:53:37 +0800383 priv->led[IWL_LED_TRG_RX].led_on = iwl_led_associated;
384 priv->led[IWL_LED_TRG_RX].led_off = iwl_led_associated;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700385 priv->led[IWL_LED_TRG_RX].led_pattern = iwl4965_led_pattern;
386
387 if (ret)
388 goto exit_fail;
389
390 trigger = ieee80211_get_tx_led_name(priv->hw);
Tomas Winkler0eee6122008-07-11 11:53:36 +0800391 snprintf(name, sizeof(name), "iwl-%s:TX", wiphy_name(priv->hw->wiphy));
392 ret = iwl_leds_register_led(priv, &priv->led[IWL_LED_TRG_TX],
393 IWL_LED_TRG_TX, 0, name, trigger);
394
Tomas Winklerec1a7462008-07-11 11:53:37 +0800395 priv->led[IWL_LED_TRG_TX].led_on = iwl_led_associated;
396 priv->led[IWL_LED_TRG_TX].led_off = iwl_led_associated;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700397 priv->led[IWL_LED_TRG_TX].led_pattern = iwl4965_led_pattern;
398
399 if (ret)
400 goto exit_fail;
401
402 return 0;
403
404exit_fail:
405 iwl_leds_unregister(priv);
406 return ret;
407}
408EXPORT_SYMBOL(iwl_leds_register);
409
410/* unregister led class */
Tomas Winkler0eee6122008-07-11 11:53:36 +0800411static void iwl_leds_unregister_led(struct iwl_led *led, u8 set_led)
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700412{
413 if (!led->registered)
414 return;
415
416 led_classdev_unregister(&led->led_dev);
417
418 if (set_led)
419 led->led_dev.brightness_set(&led->led_dev, LED_OFF);
420 led->registered = 0;
421}
422
423/* Unregister all led handlers */
424void iwl_leds_unregister(struct iwl_priv *priv)
425{
426 iwl_leds_unregister_led(&priv->led[IWL_LED_TRG_ASSOC], 0);
427 iwl_leds_unregister_led(&priv->led[IWL_LED_TRG_RX], 0);
428 iwl_leds_unregister_led(&priv->led[IWL_LED_TRG_TX], 0);
429 iwl_leds_unregister_led(&priv->led[IWL_LED_TRG_RADIO], 1);
430}
431EXPORT_SYMBOL(iwl_leds_unregister);
432