blob: d3336966b6b5169cc776f729bc06b73459c8f4ff [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
42#include "iwl-3945.h"
43#include "iwl-helpers.h"
44
Mohamed Abbasab53d8a2008-03-25 16:33:36 -070045
46static const struct {
47 u16 brightness;
48 u8 on_time;
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +080049 u8 off_time;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -070050} blink_tbl[] =
51{
52 {300, 25, 25},
53 {200, 40, 40},
54 {100, 55, 55},
55 {70, 65, 65},
56 {50, 75, 75},
57 {20, 85, 85},
58 {15, 95, 95 },
59 {10, 110, 110},
60 {5, 130, 130},
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +080061 {0, 167, 167},
62 /*SOLID_ON*/
63 {-1, IWL_LED_SOLID, 0}
Mohamed Abbasab53d8a2008-03-25 16:33:36 -070064};
65
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +080066#define IWL_1MB_RATE (128 * 1024)
67#define IWL_LED_THRESHOLD (16)
68#define IWL_MAX_BLINK_TBL (ARRAY_SIZE(blink_tbl) - 1) /*Exclude Solid on*/
69#define IWL_SOLID_BLINK_IDX (ARRAY_SIZE(blink_tbl) - 1)
70
Mohamed Abbasab53d8a2008-03-25 16:33:36 -070071static int iwl3945_led_cmd_callback(struct iwl3945_priv *priv,
72 struct iwl3945_cmd *cmd,
73 struct sk_buff *skb)
74{
75 return 1;
76}
77
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +080078static inline int iwl3945_brightness_to_idx(enum led_brightness brightness)
79{
80 return fls(0x000000FF & (u32)brightness);
81}
Mohamed Abbasab53d8a2008-03-25 16:33:36 -070082
83/* Send led command */
84static int iwl_send_led_cmd(struct iwl3945_priv *priv,
85 struct iwl3945_led_cmd *led_cmd)
86{
87 struct iwl3945_host_cmd cmd = {
88 .id = REPLY_LEDS_CMD,
89 .len = sizeof(struct iwl3945_led_cmd),
90 .data = led_cmd,
91 .meta.flags = CMD_ASYNC,
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +080092 .meta.u.callback = iwl3945_led_cmd_callback,
Mohamed Abbasab53d8a2008-03-25 16:33:36 -070093 };
94
95 return iwl3945_send_cmd(priv, &cmd);
96}
97
98
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +080099
100/* Set led on command */
101static int iwl3945_led_pattern(struct iwl3945_priv *priv, int led_id,
102 unsigned int idx)
103{
104 struct iwl3945_led_cmd led_cmd = {
105 .id = led_id,
106 .interval = IWL_DEF_LED_INTRVL
107 };
108
109 BUG_ON(idx > IWL_MAX_BLINK_TBL);
110
111 led_cmd.on = blink_tbl[idx].on_time;
112 led_cmd.off = blink_tbl[idx].off_time;
113
114 return iwl_send_led_cmd(priv, &led_cmd);
115}
116
117
118#if 1
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700119/* Set led on command */
120static int iwl3945_led_on(struct iwl3945_priv *priv, int led_id)
121{
122 struct iwl3945_led_cmd led_cmd = {
123 .id = led_id,
124 .on = IWL_LED_SOLID,
125 .off = 0,
126 .interval = IWL_DEF_LED_INTRVL
127 };
128 return iwl_send_led_cmd(priv, &led_cmd);
129}
130
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700131/* Set led off command */
132static int iwl3945_led_off(struct iwl3945_priv *priv, int led_id)
133{
134 struct iwl3945_led_cmd led_cmd = {
135 .id = led_id,
136 .on = 0,
137 .off = 0,
138 .interval = IWL_DEF_LED_INTRVL
139 };
140 IWL_DEBUG_LED("led off %d\n", led_id);
141 return iwl_send_led_cmd(priv, &led_cmd);
142}
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +0800143#endif
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700144
145
146/*
147 * brightness call back function for Tx/Rx LED
148 */
149static int iwl3945_led_associated(struct iwl3945_priv *priv, int led_id)
150{
151 if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
152 !test_bit(STATUS_READY, &priv->status))
153 return 0;
154
155
156 /* start counting Tx/Rx bytes */
157 if (!priv->last_blink_time && priv->allow_blinking)
158 priv->last_blink_time = jiffies;
159 return 0;
160}
161
162/*
163 * brightness call back for association and radio
164 */
165static void iwl3945_led_brightness_set(struct led_classdev *led_cdev,
166 enum led_brightness brightness)
167{
168 struct iwl3945_led *led = container_of(led_cdev,
169 struct iwl3945_led, led_dev);
170 struct iwl3945_priv *priv = led->priv;
171
172 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
173 return;
174
175 switch (brightness) {
176 case LED_FULL:
177 if (led->type == IWL_LED_TRG_ASSOC) {
178 priv->allow_blinking = 1;
179 IWL_DEBUG_LED("MAC is associated\n");
180 }
181 if (led->led_on)
182 led->led_on(priv, IWL_LED_LINK);
183 break;
184 case LED_OFF:
185 if (led->type == IWL_LED_TRG_ASSOC) {
186 priv->allow_blinking = 0;
187 IWL_DEBUG_LED("MAC is disassociated\n");
188 }
189 if (led->led_off)
190 led->led_off(priv, IWL_LED_LINK);
191 break;
192 default:
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +0800193 if (led->led_pattern) {
194 int idx = iwl3945_brightness_to_idx(brightness);
195 led->led_pattern(priv, IWL_LED_LINK, idx);
196 }
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700197 break;
198 }
199}
200
201
202
203/*
204 * Register led class with the system
205 */
206static int iwl3945_led_register_led(struct iwl3945_priv *priv,
207 struct iwl3945_led *led,
208 enum led_type type, u8 set_led,
Sven Wegener5cbbb372008-08-01 21:57:16 +0200209 char *trigger)
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700210{
211 struct device *device = wiphy_dev(priv->hw->wiphy);
212 int ret;
213
Sven Wegener5cbbb372008-08-01 21:57:16 +0200214 led->led_dev.name = led->name;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700215 led->led_dev.brightness_set = iwl3945_led_brightness_set;
216 led->led_dev.default_trigger = trigger;
217
Marcin Slusarzb6b16192008-06-08 13:13:06 +0200218 led->priv = priv;
219 led->type = type;
220
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700221 ret = led_classdev_register(device, &led->led_dev);
222 if (ret) {
223 IWL_ERROR("Error: failed to register led handler.\n");
224 return ret;
225 }
226
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700227 led->registered = 1;
228
229 if (set_led && led->led_on)
230 led->led_on(priv, IWL_LED_LINK);
231 return 0;
232}
233
234
235/*
236 * calculate blink rate according to last 2 sec Tx/Rx activities
237 */
238static inline u8 get_blink_rate(struct iwl3945_priv *priv)
239{
240 int index;
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +0800241 u64 current_tpt = priv->rxtxpackets;
242 s64 tpt = current_tpt - priv->led_tpt;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700243
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +0800244 if (tpt < 0)
245 tpt = -tpt;
246 priv->led_tpt = current_tpt;
247
248 if (!priv->allow_blinking)
249 index = IWL_MAX_BLINK_TBL;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700250 else
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +0800251 for (index = 0; index < IWL_MAX_BLINK_TBL; index++)
252 if (tpt > (blink_tbl[index].brightness * IWL_1MB_RATE))
253 break;
254 return index;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700255}
256
257static inline int is_rf_kill(struct iwl3945_priv *priv)
258{
259 return test_bit(STATUS_RF_KILL_HW, &priv->status) ||
260 test_bit(STATUS_RF_KILL_SW, &priv->status);
261}
262
263/*
264 * this function called from handler. Since setting Led command can
265 * happen very frequent we postpone led command to be called from
266 * REPLY handler so we know ucode is up
267 */
268void iwl3945_led_background(struct iwl3945_priv *priv)
269{
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +0800270 u8 blink_idx;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700271
272 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
273 priv->last_blink_time = 0;
274 return;
275 }
276 if (is_rf_kill(priv)) {
277 priv->last_blink_time = 0;
278 return;
279 }
280
281 if (!priv->allow_blinking) {
282 priv->last_blink_time = 0;
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +0800283 if (priv->last_blink_rate != IWL_SOLID_BLINK_IDX) {
284 priv->last_blink_rate = IWL_SOLID_BLINK_IDX;
285 iwl3945_led_pattern(priv, IWL_LED_LINK,
286 IWL_SOLID_BLINK_IDX);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700287 }
288 return;
289 }
290 if (!priv->last_blink_time ||
291 !time_after(jiffies, priv->last_blink_time +
292 msecs_to_jiffies(1000)))
293 return;
294
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +0800295 blink_idx = get_blink_rate(priv);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700296
297 /* call only if blink rate change */
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +0800298 if (blink_idx != priv->last_blink_rate)
299 iwl3945_led_pattern(priv, IWL_LED_LINK, blink_idx);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700300
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +0800301 priv->last_blink_time = jiffies;
302 priv->last_blink_rate = blink_idx;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700303 priv->rxtxpackets = 0;
304}
305
306
307/* Register all led handler */
308int iwl3945_led_register(struct iwl3945_priv *priv)
309{
310 char *trigger;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700311 int ret;
312
313 priv->last_blink_rate = 0;
314 priv->rxtxpackets = 0;
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +0800315 priv->led_tpt = 0;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700316 priv->last_blink_time = 0;
317 priv->allow_blinking = 0;
318
319 trigger = ieee80211_get_radio_led_name(priv->hw);
Sven Wegener5cbbb372008-08-01 21:57:16 +0200320 snprintf(priv->led[IWL_LED_TRG_RADIO].name,
321 sizeof(priv->led[IWL_LED_TRG_RADIO].name), "iwl-%s:radio",
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700322 wiphy_name(priv->hw->wiphy));
323
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +0800324 priv->led[IWL_LED_TRG_RADIO].led_on = iwl3945_led_on;
325 priv->led[IWL_LED_TRG_RADIO].led_off = iwl3945_led_off;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700326 priv->led[IWL_LED_TRG_RADIO].led_pattern = NULL;
327
328 ret = iwl3945_led_register_led(priv,
329 &priv->led[IWL_LED_TRG_RADIO],
Sven Wegener5cbbb372008-08-01 21:57:16 +0200330 IWL_LED_TRG_RADIO, 1, trigger);
331
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700332 if (ret)
333 goto exit_fail;
334
335 trigger = ieee80211_get_assoc_led_name(priv->hw);
Sven Wegener5cbbb372008-08-01 21:57:16 +0200336 snprintf(priv->led[IWL_LED_TRG_ASSOC].name,
337 sizeof(priv->led[IWL_LED_TRG_ASSOC].name), "iwl-%s:assoc",
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700338 wiphy_name(priv->hw->wiphy));
339
340 ret = iwl3945_led_register_led(priv,
341 &priv->led[IWL_LED_TRG_ASSOC],
Sven Wegener5cbbb372008-08-01 21:57:16 +0200342 IWL_LED_TRG_ASSOC, 0, trigger);
343
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700344 /* for assoc always turn led on */
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +0800345 priv->led[IWL_LED_TRG_ASSOC].led_on = iwl3945_led_on;
346 priv->led[IWL_LED_TRG_ASSOC].led_off = iwl3945_led_on;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700347 priv->led[IWL_LED_TRG_ASSOC].led_pattern = NULL;
348
349 if (ret)
350 goto exit_fail;
351
352 trigger = ieee80211_get_rx_led_name(priv->hw);
Sven Wegener5cbbb372008-08-01 21:57:16 +0200353 snprintf(priv->led[IWL_LED_TRG_RX].name,
354 sizeof(priv->led[IWL_LED_TRG_RX].name), "iwl-%s:RX",
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700355 wiphy_name(priv->hw->wiphy));
356
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700357 ret = iwl3945_led_register_led(priv,
358 &priv->led[IWL_LED_TRG_RX],
Sven Wegener5cbbb372008-08-01 21:57:16 +0200359 IWL_LED_TRG_RX, 0, trigger);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700360
361 priv->led[IWL_LED_TRG_RX].led_on = iwl3945_led_associated;
362 priv->led[IWL_LED_TRG_RX].led_off = iwl3945_led_associated;
363 priv->led[IWL_LED_TRG_RX].led_pattern = iwl3945_led_pattern;
364
365 if (ret)
366 goto exit_fail;
367
368 trigger = ieee80211_get_tx_led_name(priv->hw);
Sven Wegener5cbbb372008-08-01 21:57:16 +0200369 snprintf(priv->led[IWL_LED_TRG_TX].name,
370 sizeof(priv->led[IWL_LED_TRG_TX].name), "iwl-%s:TX",
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700371 wiphy_name(priv->hw->wiphy));
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +0800372
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700373 ret = iwl3945_led_register_led(priv,
374 &priv->led[IWL_LED_TRG_TX],
Sven Wegener5cbbb372008-08-01 21:57:16 +0200375 IWL_LED_TRG_TX, 0, trigger);
376
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700377 priv->led[IWL_LED_TRG_TX].led_on = iwl3945_led_associated;
378 priv->led[IWL_LED_TRG_TX].led_off = iwl3945_led_associated;
379 priv->led[IWL_LED_TRG_TX].led_pattern = iwl3945_led_pattern;
380
381 if (ret)
382 goto exit_fail;
383
384 return 0;
385
386exit_fail:
387 iwl3945_led_unregister(priv);
388 return ret;
389}
390
391
392/* unregister led class */
393static void iwl3945_led_unregister_led(struct iwl3945_led *led, u8 set_led)
394{
395 if (!led->registered)
396 return;
397
398 led_classdev_unregister(&led->led_dev);
399
400 if (set_led)
401 led->led_dev.brightness_set(&led->led_dev, LED_OFF);
402 led->registered = 0;
403}
404
405/* Unregister all led handlers */
406void iwl3945_led_unregister(struct iwl3945_priv *priv)
407{
408 iwl3945_led_unregister_led(&priv->led[IWL_LED_TRG_ASSOC], 0);
409 iwl3945_led_unregister_led(&priv->led[IWL_LED_TRG_RX], 0);
410 iwl3945_led_unregister_led(&priv->led[IWL_LED_TRG_TX], 0);
411 iwl3945_led_unregister_led(&priv->led[IWL_LED_TRG_RADIO], 1);
412}
413