blob: b34bef4891b8634a88231082e5818e3b30c350d2 [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:
Winkler, Tomas759ef892008-12-09 11:28:58 -080022 * Intel Linux Wireless <ilw@linux.intel.com>
Mohamed Abbasab53d8a2008-03-25 16:33:36 -070023 * 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>
Mohamed Abbasab53d8a2008-03-25 16:33:36 -070030#include <linux/init.h>
31#include <linux/pci.h>
32#include <linux/dma-mapping.h>
33#include <linux/delay.h>
34#include <linux/skbuff.h>
35#include <linux/netdevice.h>
36#include <linux/wireless.h>
37#include <net/mac80211.h>
38#include <linux/etherdevice.h>
39#include <asm/unaligned.h>
40
Tomas Winkler600c0e12008-12-19 10:37:04 +080041#include "iwl-commands.h"
Tomas Winkler69d00d22008-12-19 10:37:02 +080042#include "iwl-3945-commands.h"
Mohamed Abbasab53d8a2008-03-25 16:33:36 -070043#include "iwl-3945.h"
Mohamed Abbasab53d8a2008-03-25 16:33:36 -070044
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,
Tomas Winkler4c897252008-12-19 10:37:05 +080085 struct iwl_led_cmd *led_cmd)
Mohamed Abbasab53d8a2008-03-25 16:33:36 -070086{
87 struct iwl3945_host_cmd cmd = {
88 .id = REPLY_LEDS_CMD,
Tomas Winkler4c897252008-12-19 10:37:05 +080089 .len = sizeof(struct iwl_led_cmd),
Mohamed Abbasab53d8a2008-03-25 16:33:36 -070090 .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{
Tomas Winkler4c897252008-12-19 10:37:05 +0800104 struct iwl_led_cmd led_cmd = {
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +0800105 .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
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700118/* Set led on command */
119static int iwl3945_led_on(struct iwl3945_priv *priv, int led_id)
120{
Tomas Winkler4c897252008-12-19 10:37:05 +0800121 struct iwl_led_cmd led_cmd = {
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700122 .id = led_id,
123 .on = IWL_LED_SOLID,
124 .off = 0,
125 .interval = IWL_DEF_LED_INTRVL
126 };
127 return iwl_send_led_cmd(priv, &led_cmd);
128}
129
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700130/* Set led off command */
131static int iwl3945_led_off(struct iwl3945_priv *priv, int led_id)
132{
Tomas Winkler4c897252008-12-19 10:37:05 +0800133 struct iwl_led_cmd led_cmd = {
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700134 .id = led_id,
135 .on = 0,
136 .off = 0,
137 .interval = IWL_DEF_LED_INTRVL
138 };
139 IWL_DEBUG_LED("led off %d\n", led_id);
140 return iwl_send_led_cmd(priv, &led_cmd);
141}
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700142
143/*
144 * brightness call back function for Tx/Rx LED
145 */
146static int iwl3945_led_associated(struct iwl3945_priv *priv, int led_id)
147{
148 if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
149 !test_bit(STATUS_READY, &priv->status))
150 return 0;
151
152
153 /* start counting Tx/Rx bytes */
154 if (!priv->last_blink_time && priv->allow_blinking)
155 priv->last_blink_time = jiffies;
156 return 0;
157}
158
159/*
160 * brightness call back for association and radio
161 */
162static void iwl3945_led_brightness_set(struct led_classdev *led_cdev,
163 enum led_brightness brightness)
164{
165 struct iwl3945_led *led = container_of(led_cdev,
166 struct iwl3945_led, led_dev);
167 struct iwl3945_priv *priv = led->priv;
168
169 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
170 return;
171
172 switch (brightness) {
173 case LED_FULL:
174 if (led->type == IWL_LED_TRG_ASSOC) {
175 priv->allow_blinking = 1;
176 IWL_DEBUG_LED("MAC is associated\n");
177 }
178 if (led->led_on)
179 led->led_on(priv, IWL_LED_LINK);
180 break;
181 case LED_OFF:
182 if (led->type == IWL_LED_TRG_ASSOC) {
183 priv->allow_blinking = 0;
184 IWL_DEBUG_LED("MAC is disassociated\n");
185 }
186 if (led->led_off)
187 led->led_off(priv, IWL_LED_LINK);
188 break;
189 default:
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +0800190 if (led->led_pattern) {
191 int idx = iwl3945_brightness_to_idx(brightness);
192 led->led_pattern(priv, IWL_LED_LINK, idx);
193 }
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700194 break;
195 }
196}
197
198
199
200/*
201 * Register led class with the system
202 */
203static int iwl3945_led_register_led(struct iwl3945_priv *priv,
204 struct iwl3945_led *led,
205 enum led_type type, u8 set_led,
Sven Wegener5cbbb372008-08-01 21:57:16 +0200206 char *trigger)
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700207{
208 struct device *device = wiphy_dev(priv->hw->wiphy);
209 int ret;
210
Sven Wegener5cbbb372008-08-01 21:57:16 +0200211 led->led_dev.name = led->name;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700212 led->led_dev.brightness_set = iwl3945_led_brightness_set;
213 led->led_dev.default_trigger = trigger;
214
Marcin Slusarzb6b16192008-06-08 13:13:06 +0200215 led->priv = priv;
216 led->type = type;
217
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700218 ret = led_classdev_register(device, &led->led_dev);
219 if (ret) {
220 IWL_ERROR("Error: failed to register led handler.\n");
221 return ret;
222 }
223
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700224 led->registered = 1;
225
226 if (set_led && led->led_on)
227 led->led_on(priv, IWL_LED_LINK);
228 return 0;
229}
230
231
232/*
233 * calculate blink rate according to last 2 sec Tx/Rx activities
234 */
235static inline u8 get_blink_rate(struct iwl3945_priv *priv)
236{
237 int index;
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +0800238 u64 current_tpt = priv->rxtxpackets;
239 s64 tpt = current_tpt - priv->led_tpt;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700240
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +0800241 if (tpt < 0)
242 tpt = -tpt;
243 priv->led_tpt = current_tpt;
244
245 if (!priv->allow_blinking)
246 index = IWL_MAX_BLINK_TBL;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700247 else
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +0800248 for (index = 0; index < IWL_MAX_BLINK_TBL; index++)
249 if (tpt > (blink_tbl[index].brightness * IWL_1MB_RATE))
250 break;
251 return index;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700252}
253
254static inline int is_rf_kill(struct iwl3945_priv *priv)
255{
256 return test_bit(STATUS_RF_KILL_HW, &priv->status) ||
257 test_bit(STATUS_RF_KILL_SW, &priv->status);
258}
259
260/*
261 * this function called from handler. Since setting Led command can
262 * happen very frequent we postpone led command to be called from
263 * REPLY handler so we know ucode is up
264 */
265void iwl3945_led_background(struct iwl3945_priv *priv)
266{
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +0800267 u8 blink_idx;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700268
269 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
270 priv->last_blink_time = 0;
271 return;
272 }
273 if (is_rf_kill(priv)) {
274 priv->last_blink_time = 0;
275 return;
276 }
277
278 if (!priv->allow_blinking) {
279 priv->last_blink_time = 0;
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +0800280 if (priv->last_blink_rate != IWL_SOLID_BLINK_IDX) {
281 priv->last_blink_rate = IWL_SOLID_BLINK_IDX;
282 iwl3945_led_pattern(priv, IWL_LED_LINK,
283 IWL_SOLID_BLINK_IDX);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700284 }
285 return;
286 }
287 if (!priv->last_blink_time ||
288 !time_after(jiffies, priv->last_blink_time +
289 msecs_to_jiffies(1000)))
290 return;
291
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +0800292 blink_idx = get_blink_rate(priv);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700293
294 /* call only if blink rate change */
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +0800295 if (blink_idx != priv->last_blink_rate)
296 iwl3945_led_pattern(priv, IWL_LED_LINK, blink_idx);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700297
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +0800298 priv->last_blink_time = jiffies;
299 priv->last_blink_rate = blink_idx;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700300 priv->rxtxpackets = 0;
301}
302
303
304/* Register all led handler */
305int iwl3945_led_register(struct iwl3945_priv *priv)
306{
307 char *trigger;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700308 int ret;
309
310 priv->last_blink_rate = 0;
311 priv->rxtxpackets = 0;
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +0800312 priv->led_tpt = 0;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700313 priv->last_blink_time = 0;
314 priv->allow_blinking = 0;
315
316 trigger = ieee80211_get_radio_led_name(priv->hw);
Sven Wegener5cbbb372008-08-01 21:57:16 +0200317 snprintf(priv->led[IWL_LED_TRG_RADIO].name,
318 sizeof(priv->led[IWL_LED_TRG_RADIO].name), "iwl-%s:radio",
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700319 wiphy_name(priv->hw->wiphy));
320
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +0800321 priv->led[IWL_LED_TRG_RADIO].led_on = iwl3945_led_on;
322 priv->led[IWL_LED_TRG_RADIO].led_off = iwl3945_led_off;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700323 priv->led[IWL_LED_TRG_RADIO].led_pattern = NULL;
324
325 ret = iwl3945_led_register_led(priv,
326 &priv->led[IWL_LED_TRG_RADIO],
Sven Wegener5cbbb372008-08-01 21:57:16 +0200327 IWL_LED_TRG_RADIO, 1, trigger);
328
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700329 if (ret)
330 goto exit_fail;
331
332 trigger = ieee80211_get_assoc_led_name(priv->hw);
Sven Wegener5cbbb372008-08-01 21:57:16 +0200333 snprintf(priv->led[IWL_LED_TRG_ASSOC].name,
334 sizeof(priv->led[IWL_LED_TRG_ASSOC].name), "iwl-%s:assoc",
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700335 wiphy_name(priv->hw->wiphy));
336
337 ret = iwl3945_led_register_led(priv,
338 &priv->led[IWL_LED_TRG_ASSOC],
Sven Wegener5cbbb372008-08-01 21:57:16 +0200339 IWL_LED_TRG_ASSOC, 0, trigger);
340
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700341 /* for assoc always turn led on */
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +0800342 priv->led[IWL_LED_TRG_ASSOC].led_on = iwl3945_led_on;
343 priv->led[IWL_LED_TRG_ASSOC].led_off = iwl3945_led_on;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700344 priv->led[IWL_LED_TRG_ASSOC].led_pattern = NULL;
345
346 if (ret)
347 goto exit_fail;
348
349 trigger = ieee80211_get_rx_led_name(priv->hw);
Sven Wegener5cbbb372008-08-01 21:57:16 +0200350 snprintf(priv->led[IWL_LED_TRG_RX].name,
351 sizeof(priv->led[IWL_LED_TRG_RX].name), "iwl-%s:RX",
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700352 wiphy_name(priv->hw->wiphy));
353
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700354 ret = iwl3945_led_register_led(priv,
355 &priv->led[IWL_LED_TRG_RX],
Sven Wegener5cbbb372008-08-01 21:57:16 +0200356 IWL_LED_TRG_RX, 0, trigger);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700357
358 priv->led[IWL_LED_TRG_RX].led_on = iwl3945_led_associated;
359 priv->led[IWL_LED_TRG_RX].led_off = iwl3945_led_associated;
360 priv->led[IWL_LED_TRG_RX].led_pattern = iwl3945_led_pattern;
361
362 if (ret)
363 goto exit_fail;
364
365 trigger = ieee80211_get_tx_led_name(priv->hw);
Sven Wegener5cbbb372008-08-01 21:57:16 +0200366 snprintf(priv->led[IWL_LED_TRG_TX].name,
367 sizeof(priv->led[IWL_LED_TRG_TX].name), "iwl-%s:TX",
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700368 wiphy_name(priv->hw->wiphy));
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +0800369
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700370 ret = iwl3945_led_register_led(priv,
371 &priv->led[IWL_LED_TRG_TX],
Sven Wegener5cbbb372008-08-01 21:57:16 +0200372 IWL_LED_TRG_TX, 0, trigger);
373
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700374 priv->led[IWL_LED_TRG_TX].led_on = iwl3945_led_associated;
375 priv->led[IWL_LED_TRG_TX].led_off = iwl3945_led_associated;
376 priv->led[IWL_LED_TRG_TX].led_pattern = iwl3945_led_pattern;
377
378 if (ret)
379 goto exit_fail;
380
381 return 0;
382
383exit_fail:
384 iwl3945_led_unregister(priv);
385 return ret;
386}
387
388
389/* unregister led class */
390static void iwl3945_led_unregister_led(struct iwl3945_led *led, u8 set_led)
391{
392 if (!led->registered)
393 return;
394
395 led_classdev_unregister(&led->led_dev);
396
397 if (set_led)
398 led->led_dev.brightness_set(&led->led_dev, LED_OFF);
399 led->registered = 0;
400}
401
402/* Unregister all led handlers */
403void iwl3945_led_unregister(struct iwl3945_priv *priv)
404{
405 iwl3945_led_unregister_led(&priv->led[IWL_LED_TRG_ASSOC], 0);
406 iwl3945_led_unregister_led(&priv->led[IWL_LED_TRG_RX], 0);
407 iwl3945_led_unregister_led(&priv->led[IWL_LED_TRG_TX], 0);
408 iwl3945_led_unregister_led(&priv->led[IWL_LED_TRG_RADIO], 1);
409}
410