blob: afd10758c0376f6f68a38225cc401186a5a920b0 [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
47#define IWL_1MB_RATE (128 * 1024)
48#define IWL_LED_THRESHOLD (16)
49#define IWL_MAX_BLINK_TBL (10)
50
Tomas Winkler0eee6122008-07-11 11:53:36 +080051#ifdef CONFIG_IWLWIFI_DEBUG
52static const char *led_type_str[] = {
53 __stringify(IWL_LED_TRG_TX),
54 __stringify(IWL_LED_TRG_RX),
55 __stringify(IWL_LED_TRG_ASSOC),
56 __stringify(IWL_LED_TRG_RADIO),
57 NULL
58};
59#endif /* CONFIG_IWLWIFI_DEBUG */
60
61
Mohamed Abbasab53d8a2008-03-25 16:33:36 -070062static const struct {
63 u16 tpt;
64 u8 on_time;
Tomas Winkler0eee6122008-07-11 11:53:36 +080065 u8 off_time;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -070066} blink_tbl[] =
67{
68 {300, 25, 25},
69 {200, 40, 40},
70 {100, 55, 55},
71 {70, 65, 65},
72 {50, 75, 75},
73 {20, 85, 85},
74 {15, 95, 95 },
75 {10, 110, 110},
76 {5, 130, 130},
77 {0, 167, 167}
78};
79
80static int iwl_led_cmd_callback(struct iwl_priv *priv,
81 struct iwl_cmd *cmd, struct sk_buff *skb)
82{
83 return 1;
84}
85
86
87/* Send led command */
88static int iwl_send_led_cmd(struct iwl_priv *priv,
89 struct iwl4965_led_cmd *led_cmd)
90{
91 struct iwl_host_cmd cmd = {
92 .id = REPLY_LEDS_CMD,
93 .len = sizeof(struct iwl4965_led_cmd),
94 .data = led_cmd,
95 .meta.flags = CMD_ASYNC,
96 .meta.u.callback = iwl_led_cmd_callback
97 };
98 u32 reg;
99
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700100 reg = iwl_read32(priv, CSR_LED_REG);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700101 if (reg != (reg & CSR_LED_BSM_CTRL_MSK))
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700102 iwl_write32(priv, CSR_LED_REG, reg & CSR_LED_BSM_CTRL_MSK);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700103
104 return iwl_send_cmd(priv, &cmd);
105}
106
107
108/* Set led on command */
109static int iwl4965_led_on(struct iwl_priv *priv, int led_id)
110{
111 struct iwl4965_led_cmd led_cmd = {
112 .id = led_id,
113 .on = IWL_LED_SOLID,
114 .off = 0,
115 .interval = IWL_DEF_LED_INTRVL
116 };
117 return iwl_send_led_cmd(priv, &led_cmd);
118}
119
120/* Set led on command */
121static int iwl4965_led_pattern(struct iwl_priv *priv, int led_id,
122 enum led_brightness brightness)
123{
124 struct iwl4965_led_cmd led_cmd = {
125 .id = led_id,
126 .on = brightness,
127 .off = brightness,
128 .interval = IWL_DEF_LED_INTRVL
129 };
130 if (brightness == LED_FULL) {
131 led_cmd.on = IWL_LED_SOLID;
132 led_cmd.off = 0;
133 }
134 return iwl_send_led_cmd(priv, &led_cmd);
135}
136
137/* Set led register off */
138static int iwl4965_led_on_reg(struct iwl_priv *priv, int led_id)
139{
140 IWL_DEBUG_LED("led on %d\n", led_id);
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700141 iwl_write32(priv, CSR_LED_REG, CSR_LED_REG_TRUN_ON);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700142 return 0;
143}
144
145#if 0
146/* Set led off command */
147int iwl4965_led_off(struct iwl_priv *priv, int led_id)
148{
149 struct iwl4965_led_cmd led_cmd = {
150 .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 */
172static int iwl4965_led_associated(struct iwl_priv *priv, int led_id)
173{
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:
216 if (led->led_pattern)
217 led->led_pattern(priv, IWL_LED_LINK, brightness);
218 break;
219 }
220}
221
222
223
224/*
225 * Register led class with the system
226 */
Tomas Winkler0eee6122008-07-11 11:53:36 +0800227static int iwl_leds_register_led(struct iwl_priv *priv, struct iwl_led *led,
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700228 enum led_type type, u8 set_led,
229 const char *name, char *trigger)
230{
231 struct device *device = wiphy_dev(priv->hw->wiphy);
232 int ret;
233
234 led->led_dev.name = name;
Tomas Winkler0eee6122008-07-11 11:53:36 +0800235 led->led_dev.brightness_set = iwl_led_brightness_set;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700236 led->led_dev.default_trigger = trigger;
237
Tomas Winklera571ea4e2008-03-28 16:21:11 -0700238 led->priv = priv;
239 led->type = type;
240
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700241 ret = led_classdev_register(device, &led->led_dev);
242 if (ret) {
243 IWL_ERROR("Error: failed to register led handler.\n");
244 return ret;
245 }
246
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700247 led->registered = 1;
248
249 if (set_led && led->led_on)
250 led->led_on(priv, IWL_LED_LINK);
Tomas Winklera571ea4e2008-03-28 16:21:11 -0700251
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700252 return 0;
253}
254
255
256/*
257 * calculate blink rate according to last 2 sec Tx/Rx activities
258 */
259static inline u8 get_blink_rate(struct iwl_priv *priv)
260{
261 int i;
262 u8 blink_rate;
Tomas Winkler0eee6122008-07-11 11:53:36 +0800263 u64 current_tpt = priv->tx_stats[2].bytes;
264 /* FIXME: + priv->rx_stats[2].bytes; */
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700265 s64 tpt = current_tpt - priv->led_tpt;
266
267 if (tpt < 0) /* wrapparound */
268 tpt = -tpt;
269
Tomas Winkler0eee6122008-07-11 11:53:36 +0800270 IWL_DEBUG_LED("tpt %lld current_tpt %lld\n", tpt, current_tpt);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700271 priv->led_tpt = current_tpt;
272
273 if (tpt < IWL_LED_THRESHOLD) {
274 i = IWL_MAX_BLINK_TBL;
275 } else {
276 for (i = 0; i < IWL_MAX_BLINK_TBL; i++)
277 if (tpt > (blink_tbl[i].tpt * IWL_1MB_RATE))
278 break;
279 }
280 /* if 0 frame is transfered */
281 if ((i == IWL_MAX_BLINK_TBL) || !priv->allow_blinking)
282 blink_rate = IWL_LED_SOLID;
283 else
284 blink_rate = blink_tbl[i].on_time;
285
286 return blink_rate;
287}
288
289static inline int is_rf_kill(struct iwl_priv *priv)
290{
291 return test_bit(STATUS_RF_KILL_HW, &priv->status) ||
292 test_bit(STATUS_RF_KILL_SW, &priv->status);
293}
294
295/*
296 * this function called from handler. Since setting Led command can
297 * happen very frequent we postpone led command to be called from
298 * REPLY handler so we know ucode is up
299 */
300void iwl_leds_background(struct iwl_priv *priv)
301{
302 u8 blink_rate;
303
304 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
305 priv->last_blink_time = 0;
306 return;
307 }
308 if (is_rf_kill(priv)) {
309 priv->last_blink_time = 0;
310 return;
311 }
312
313 if (!priv->allow_blinking) {
314 priv->last_blink_time = 0;
315 if (priv->last_blink_rate != IWL_LED_SOLID) {
316 priv->last_blink_rate = IWL_LED_SOLID;
317 iwl4965_led_on(priv, IWL_LED_LINK);
318 }
319 return;
320 }
321 if (!priv->last_blink_time ||
322 !time_after(jiffies, priv->last_blink_time +
323 msecs_to_jiffies(1000)))
324 return;
325
326 blink_rate = get_blink_rate(priv);
327
328 /* call only if blink rate change */
329 if (blink_rate != priv->last_blink_rate) {
330 if (blink_rate != IWL_LED_SOLID) {
Tomas Winkler0eee6122008-07-11 11:53:36 +0800331 iwl4965_led_pattern(priv, IWL_LED_LINK, blink_rate);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700332 } else {
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700333 iwl4965_led_on(priv, IWL_LED_LINK);
334 }
335 }
336
Tomas Winkler0eee6122008-07-11 11:53:36 +0800337 priv->last_blink_time = jiffies;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700338 priv->last_blink_rate = blink_rate;
339}
340EXPORT_SYMBOL(iwl_leds_background);
341
342/* Register all led handler */
343int iwl_leds_register(struct iwl_priv *priv)
344{
345 char *trigger;
346 char name[32];
347 int ret;
348
349 priv->last_blink_rate = 0;
350 priv->led_tpt = 0;
351 priv->last_blink_time = 0;
352 priv->allow_blinking = 0;
353
354 trigger = ieee80211_get_radio_led_name(priv->hw);
355 snprintf(name, sizeof(name), "iwl-%s:radio",
356 wiphy_name(priv->hw->wiphy));
357
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;
361
Tomas Winkler0eee6122008-07-11 11:53:36 +0800362 ret = iwl_leds_register_led(priv, &priv->led[IWL_LED_TRG_RADIO],
363 IWL_LED_TRG_RADIO, 1, name, trigger);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700364 if (ret)
365 goto exit_fail;
366
367 trigger = ieee80211_get_assoc_led_name(priv->hw);
368 snprintf(name, sizeof(name), "iwl-%s:assoc",
369 wiphy_name(priv->hw->wiphy));
370
Tomas Winkler0eee6122008-07-11 11:53:36 +0800371 ret = iwl_leds_register_led(priv, &priv->led[IWL_LED_TRG_ASSOC],
372 IWL_LED_TRG_ASSOC, 0, name, trigger);
373
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700374 /* for assoc always turn led on */
375 priv->led[IWL_LED_TRG_ASSOC].led_on = iwl4965_led_on_reg;
376 priv->led[IWL_LED_TRG_ASSOC].led_off = iwl4965_led_on_reg;
377 priv->led[IWL_LED_TRG_ASSOC].led_pattern = NULL;
378
379 if (ret)
380 goto exit_fail;
381
382 trigger = ieee80211_get_rx_led_name(priv->hw);
Tomas Winkler0eee6122008-07-11 11:53:36 +0800383 snprintf(name, sizeof(name), "iwl-%s:RX", wiphy_name(priv->hw->wiphy));
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700384
385
Tomas Winkler0eee6122008-07-11 11:53:36 +0800386 ret = iwl_leds_register_led(priv, &priv->led[IWL_LED_TRG_RX],
387 IWL_LED_TRG_RX, 0, name, trigger);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700388
389 priv->led[IWL_LED_TRG_RX].led_on = iwl4965_led_associated;
390 priv->led[IWL_LED_TRG_RX].led_off = iwl4965_led_associated;
391 priv->led[IWL_LED_TRG_RX].led_pattern = iwl4965_led_pattern;
392
393 if (ret)
394 goto exit_fail;
395
396 trigger = ieee80211_get_tx_led_name(priv->hw);
Tomas Winkler0eee6122008-07-11 11:53:36 +0800397 snprintf(name, sizeof(name), "iwl-%s:TX", wiphy_name(priv->hw->wiphy));
398 ret = iwl_leds_register_led(priv, &priv->led[IWL_LED_TRG_TX],
399 IWL_LED_TRG_TX, 0, name, trigger);
400
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700401 priv->led[IWL_LED_TRG_TX].led_on = iwl4965_led_associated;
402 priv->led[IWL_LED_TRG_TX].led_off = iwl4965_led_associated;
403 priv->led[IWL_LED_TRG_TX].led_pattern = iwl4965_led_pattern;
404
405 if (ret)
406 goto exit_fail;
407
408 return 0;
409
410exit_fail:
411 iwl_leds_unregister(priv);
412 return ret;
413}
414EXPORT_SYMBOL(iwl_leds_register);
415
416/* unregister led class */
Tomas Winkler0eee6122008-07-11 11:53:36 +0800417static void iwl_leds_unregister_led(struct iwl_led *led, u8 set_led)
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700418{
419 if (!led->registered)
420 return;
421
422 led_classdev_unregister(&led->led_dev);
423
424 if (set_led)
425 led->led_dev.brightness_set(&led->led_dev, LED_OFF);
426 led->registered = 0;
427}
428
429/* Unregister all led handlers */
430void iwl_leds_unregister(struct iwl_priv *priv)
431{
432 iwl_leds_unregister_led(&priv->led[IWL_LED_TRG_ASSOC], 0);
433 iwl_leds_unregister_led(&priv->led[IWL_LED_TRG_RX], 0);
434 iwl_leds_unregister_led(&priv->led[IWL_LED_TRG_TX], 0);
435 iwl_leds_unregister_led(&priv->led[IWL_LED_TRG_RADIO], 1);
436}
437EXPORT_SYMBOL(iwl_leds_unregister);
438