blob: fab137365000af01210d2ab71e3a9d005b19a8da [file] [log] [blame]
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07001/******************************************************************************
2 *
Reinette Chatre01f81622009-01-08 10:20:02 -08003 * Copyright(c) 2003 - 2009 Intel Corporation. All rights reserved.
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07004 *
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"
Mohamed Abbasab53d8a2008-03-25 16:33:36 -070042#include "iwl-3945.h"
Samuel Ortiz518099a2009-01-19 15:30:27 -080043#include "iwl-core.h"
44#include "iwl-dev.h"
Mohamed Abbasab53d8a2008-03-25 16:33:36 -070045
Mohamed Abbasab53d8a2008-03-25 16:33:36 -070046
47static const struct {
48 u16 brightness;
49 u8 on_time;
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +080050 u8 off_time;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -070051} blink_tbl[] =
52{
53 {300, 25, 25},
54 {200, 40, 40},
55 {100, 55, 55},
56 {70, 65, 65},
57 {50, 75, 75},
58 {20, 85, 85},
59 {15, 95, 95 },
60 {10, 110, 110},
61 {5, 130, 130},
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +080062 {0, 167, 167},
63 /*SOLID_ON*/
64 {-1, IWL_LED_SOLID, 0}
Mohamed Abbasab53d8a2008-03-25 16:33:36 -070065};
66
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +080067#define IWL_1MB_RATE (128 * 1024)
68#define IWL_LED_THRESHOLD (16)
69#define IWL_MAX_BLINK_TBL (ARRAY_SIZE(blink_tbl) - 1) /*Exclude Solid on*/
70#define IWL_SOLID_BLINK_IDX (ARRAY_SIZE(blink_tbl) - 1)
71
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +080072static int iwl3945_led_cmd_callback(struct iwl_priv *priv,
Winkler, Tomasc2d79b482008-12-19 10:37:34 +080073 struct iwl_cmd *cmd,
Mohamed Abbasab53d8a2008-03-25 16:33:36 -070074 struct sk_buff *skb)
75{
76 return 1;
77}
78
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +080079static inline int iwl3945_brightness_to_idx(enum led_brightness brightness)
80{
81 return fls(0x000000FF & (u32)brightness);
82}
Mohamed Abbasab53d8a2008-03-25 16:33:36 -070083
84/* Send led command */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +080085static int iwl_send_led_cmd(struct iwl_priv *priv,
Tomas Winkler4c897252008-12-19 10:37:05 +080086 struct iwl_led_cmd *led_cmd)
Mohamed Abbasab53d8a2008-03-25 16:33:36 -070087{
Winkler, Tomasc2d79b482008-12-19 10:37:34 +080088 struct iwl_host_cmd cmd = {
Mohamed Abbasab53d8a2008-03-25 16:33:36 -070089 .id = REPLY_LEDS_CMD,
Tomas Winkler4c897252008-12-19 10:37:05 +080090 .len = sizeof(struct iwl_led_cmd),
Mohamed Abbasab53d8a2008-03-25 16:33:36 -070091 .data = led_cmd,
92 .meta.flags = CMD_ASYNC,
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +080093 .meta.u.callback = iwl3945_led_cmd_callback,
Mohamed Abbasab53d8a2008-03-25 16:33:36 -070094 };
95
Samuel Ortiz518099a2009-01-19 15:30:27 -080096 return iwl_send_cmd(priv, &cmd);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -070097}
98
99
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +0800100
101/* Set led on command */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800102static int iwl3945_led_pattern(struct iwl_priv *priv, int led_id,
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +0800103 unsigned int idx)
104{
Tomas Winkler4c897252008-12-19 10:37:05 +0800105 struct iwl_led_cmd led_cmd = {
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +0800106 .id = led_id,
107 .interval = IWL_DEF_LED_INTRVL
108 };
109
110 BUG_ON(idx > IWL_MAX_BLINK_TBL);
111
112 led_cmd.on = blink_tbl[idx].on_time;
113 led_cmd.off = blink_tbl[idx].off_time;
114
115 return iwl_send_led_cmd(priv, &led_cmd);
116}
117
118
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700119/* Set led on command */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800120static int iwl3945_led_on(struct iwl_priv *priv, int led_id)
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700121{
Tomas Winkler4c897252008-12-19 10:37:05 +0800122 struct iwl_led_cmd led_cmd = {
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700123 .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 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800132static int iwl3945_led_off(struct iwl_priv *priv, int led_id)
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700133{
Tomas Winkler4c897252008-12-19 10:37:05 +0800134 struct iwl_led_cmd led_cmd = {
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700135 .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}
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700143
144/*
145 * brightness call back function for Tx/Rx LED
146 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800147static int iwl3945_led_associated(struct iwl_priv *priv, int led_id)
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700148{
149 if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
150 !test_bit(STATUS_READY, &priv->status))
151 return 0;
152
153
154 /* start counting Tx/Rx bytes */
155 if (!priv->last_blink_time && priv->allow_blinking)
156 priv->last_blink_time = jiffies;
157 return 0;
158}
159
160/*
161 * brightness call back for association and radio
162 */
163static void iwl3945_led_brightness_set(struct led_classdev *led_cdev,
164 enum led_brightness brightness)
165{
166 struct iwl3945_led *led = container_of(led_cdev,
167 struct iwl3945_led, led_dev);
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800168 struct iwl_priv *priv = led->priv;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700169
170 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
171 return;
172
173 switch (brightness) {
174 case LED_FULL:
175 if (led->type == IWL_LED_TRG_ASSOC) {
176 priv->allow_blinking = 1;
177 IWL_DEBUG_LED("MAC is associated\n");
178 }
179 if (led->led_on)
180 led->led_on(priv, IWL_LED_LINK);
181 break;
182 case LED_OFF:
183 if (led->type == IWL_LED_TRG_ASSOC) {
184 priv->allow_blinking = 0;
185 IWL_DEBUG_LED("MAC is disassociated\n");
186 }
187 if (led->led_off)
188 led->led_off(priv, IWL_LED_LINK);
189 break;
190 default:
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +0800191 if (led->led_pattern) {
192 int idx = iwl3945_brightness_to_idx(brightness);
193 led->led_pattern(priv, IWL_LED_LINK, idx);
194 }
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700195 break;
196 }
197}
198
199
200
201/*
202 * Register led class with the system
203 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800204static int iwl3945_led_register_led(struct iwl_priv *priv,
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700205 struct iwl3945_led *led,
206 enum led_type type, u8 set_led,
Sven Wegener5cbbb372008-08-01 21:57:16 +0200207 char *trigger)
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700208{
209 struct device *device = wiphy_dev(priv->hw->wiphy);
210 int ret;
211
Sven Wegener5cbbb372008-08-01 21:57:16 +0200212 led->led_dev.name = led->name;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700213 led->led_dev.brightness_set = iwl3945_led_brightness_set;
214 led->led_dev.default_trigger = trigger;
215
Marcin Slusarzb6b16192008-06-08 13:13:06 +0200216 led->priv = priv;
217 led->type = type;
218
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700219 ret = led_classdev_register(device, &led->led_dev);
220 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800221 IWL_ERR(priv, "Error: failed to register led handler.\n");
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700222 return ret;
223 }
224
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700225 led->registered = 1;
226
227 if (set_led && led->led_on)
228 led->led_on(priv, IWL_LED_LINK);
229 return 0;
230}
231
232
233/*
234 * calculate blink rate according to last 2 sec Tx/Rx activities
235 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800236static inline u8 get_blink_rate(struct iwl_priv *priv)
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700237{
238 int index;
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +0800239 u64 current_tpt = priv->rxtxpackets;
240 s64 tpt = current_tpt - priv->led_tpt;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700241
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +0800242 if (tpt < 0)
243 tpt = -tpt;
244 priv->led_tpt = current_tpt;
245
246 if (!priv->allow_blinking)
247 index = IWL_MAX_BLINK_TBL;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700248 else
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +0800249 for (index = 0; index < IWL_MAX_BLINK_TBL; index++)
250 if (tpt > (blink_tbl[index].brightness * IWL_1MB_RATE))
251 break;
252 return index;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700253}
254
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800255static inline int is_rf_kill(struct iwl_priv *priv)
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700256{
257 return test_bit(STATUS_RF_KILL_HW, &priv->status) ||
258 test_bit(STATUS_RF_KILL_SW, &priv->status);
259}
260
261/*
262 * this function called from handler. Since setting Led command can
263 * happen very frequent we postpone led command to be called from
264 * REPLY handler so we know ucode is up
265 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800266void iwl3945_led_background(struct iwl_priv *priv)
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700267{
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +0800268 u8 blink_idx;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700269
270 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
271 priv->last_blink_time = 0;
272 return;
273 }
274 if (is_rf_kill(priv)) {
275 priv->last_blink_time = 0;
276 return;
277 }
278
279 if (!priv->allow_blinking) {
280 priv->last_blink_time = 0;
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +0800281 if (priv->last_blink_rate != IWL_SOLID_BLINK_IDX) {
282 priv->last_blink_rate = IWL_SOLID_BLINK_IDX;
283 iwl3945_led_pattern(priv, IWL_LED_LINK,
284 IWL_SOLID_BLINK_IDX);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700285 }
286 return;
287 }
288 if (!priv->last_blink_time ||
289 !time_after(jiffies, priv->last_blink_time +
290 msecs_to_jiffies(1000)))
291 return;
292
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +0800293 blink_idx = get_blink_rate(priv);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700294
295 /* call only if blink rate change */
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +0800296 if (blink_idx != priv->last_blink_rate)
297 iwl3945_led_pattern(priv, IWL_LED_LINK, blink_idx);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700298
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +0800299 priv->last_blink_time = jiffies;
300 priv->last_blink_rate = blink_idx;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700301 priv->rxtxpackets = 0;
302}
303
304
305/* Register all led handler */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800306int iwl3945_led_register(struct iwl_priv *priv)
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700307{
308 char *trigger;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700309 int ret;
310
311 priv->last_blink_rate = 0;
312 priv->rxtxpackets = 0;
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +0800313 priv->led_tpt = 0;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700314 priv->last_blink_time = 0;
315 priv->allow_blinking = 0;
316
317 trigger = ieee80211_get_radio_led_name(priv->hw);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800318 snprintf(priv->led39[IWL_LED_TRG_RADIO].name,
319 sizeof(priv->led39[IWL_LED_TRG_RADIO].name), "iwl-%s:radio",
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700320 wiphy_name(priv->hw->wiphy));
321
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800322 priv->led39[IWL_LED_TRG_RADIO].led_on = iwl3945_led_on;
323 priv->led39[IWL_LED_TRG_RADIO].led_off = iwl3945_led_off;
324 priv->led39[IWL_LED_TRG_RADIO].led_pattern = NULL;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700325
326 ret = iwl3945_led_register_led(priv,
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800327 &priv->led39[IWL_LED_TRG_RADIO],
Sven Wegener5cbbb372008-08-01 21:57:16 +0200328 IWL_LED_TRG_RADIO, 1, trigger);
329
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700330 if (ret)
331 goto exit_fail;
332
333 trigger = ieee80211_get_assoc_led_name(priv->hw);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800334 snprintf(priv->led39[IWL_LED_TRG_ASSOC].name,
335 sizeof(priv->led39[IWL_LED_TRG_ASSOC].name), "iwl-%s:assoc",
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700336 wiphy_name(priv->hw->wiphy));
337
338 ret = iwl3945_led_register_led(priv,
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800339 &priv->led39[IWL_LED_TRG_ASSOC],
Sven Wegener5cbbb372008-08-01 21:57:16 +0200340 IWL_LED_TRG_ASSOC, 0, trigger);
341
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700342 /* for assoc always turn led on */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800343 priv->led39[IWL_LED_TRG_ASSOC].led_on = iwl3945_led_on;
344 priv->led39[IWL_LED_TRG_ASSOC].led_off = iwl3945_led_on;
345 priv->led39[IWL_LED_TRG_ASSOC].led_pattern = NULL;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700346
347 if (ret)
348 goto exit_fail;
349
350 trigger = ieee80211_get_rx_led_name(priv->hw);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800351 snprintf(priv->led39[IWL_LED_TRG_RX].name,
352 sizeof(priv->led39[IWL_LED_TRG_RX].name), "iwl-%s:RX",
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700353 wiphy_name(priv->hw->wiphy));
354
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700355 ret = iwl3945_led_register_led(priv,
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800356 &priv->led39[IWL_LED_TRG_RX],
Sven Wegener5cbbb372008-08-01 21:57:16 +0200357 IWL_LED_TRG_RX, 0, trigger);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700358
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800359 priv->led39[IWL_LED_TRG_RX].led_on = iwl3945_led_associated;
360 priv->led39[IWL_LED_TRG_RX].led_off = iwl3945_led_associated;
361 priv->led39[IWL_LED_TRG_RX].led_pattern = iwl3945_led_pattern;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700362
363 if (ret)
364 goto exit_fail;
365
366 trigger = ieee80211_get_tx_led_name(priv->hw);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800367 snprintf(priv->led39[IWL_LED_TRG_TX].name,
368 sizeof(priv->led39[IWL_LED_TRG_TX].name), "iwl-%s:TX",
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700369 wiphy_name(priv->hw->wiphy));
Abhijeet Kolekar9a9ad0c2008-07-11 11:53:41 +0800370
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700371 ret = iwl3945_led_register_led(priv,
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800372 &priv->led39[IWL_LED_TRG_TX],
Sven Wegener5cbbb372008-08-01 21:57:16 +0200373 IWL_LED_TRG_TX, 0, trigger);
374
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800375 priv->led39[IWL_LED_TRG_TX].led_on = iwl3945_led_associated;
376 priv->led39[IWL_LED_TRG_TX].led_off = iwl3945_led_associated;
377 priv->led39[IWL_LED_TRG_TX].led_pattern = iwl3945_led_pattern;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700378
379 if (ret)
380 goto exit_fail;
381
382 return 0;
383
384exit_fail:
385 iwl3945_led_unregister(priv);
386 return ret;
387}
388
389
390/* unregister led class */
391static void iwl3945_led_unregister_led(struct iwl3945_led *led, u8 set_led)
392{
393 if (!led->registered)
394 return;
395
396 led_classdev_unregister(&led->led_dev);
397
398 if (set_led)
399 led->led_dev.brightness_set(&led->led_dev, LED_OFF);
400 led->registered = 0;
401}
402
403/* Unregister all led handlers */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800404void iwl3945_led_unregister(struct iwl_priv *priv)
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700405{
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800406 iwl3945_led_unregister_led(&priv->led39[IWL_LED_TRG_ASSOC], 0);
407 iwl3945_led_unregister_led(&priv->led39[IWL_LED_TRG_RX], 0);
408 iwl3945_led_unregister_led(&priv->led39[IWL_LED_TRG_TX], 0);
409 iwl3945_led_unregister_led(&priv->led39[IWL_LED_TRG_RADIO], 1);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -0700410}
411