iwlwifi: define structures and functions externally for customization
[deliverable/linux.git] / drivers / net / wireless / iwlwifi / iwl-3945-led.c
CommitLineData
ab53d8af
MA
1/******************************************************************************
2 *
01f8162a 3 * Copyright(c) 2003 - 2009 Intel Corporation. All rights reserved.
ab53d8af
MA
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:
759ef89f 22 * Intel Linux Wireless <ilw@linux.intel.com>
ab53d8af
MA
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>
ab53d8af
MA
30#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
600c0e11 41#include "iwl-commands.h"
ab53d8af 42#include "iwl-3945.h"
518099a8
SO
43#include "iwl-core.h"
44#include "iwl-dev.h"
ab53d8af 45
ab53d8af
MA
46
47static const struct {
48 u16 brightness;
49 u8 on_time;
9a9ad0cd 50 u8 off_time;
ab53d8af
MA
51} 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},
9a9ad0cd
AK
62 {0, 167, 167},
63 /*SOLID_ON*/
64 {-1, IWL_LED_SOLID, 0}
ab53d8af
MA
65};
66
9a9ad0cd
AK
67#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
4a8a4322 72static int iwl3945_led_cmd_callback(struct iwl_priv *priv,
c2d79b48 73 struct iwl_cmd *cmd,
ab53d8af
MA
74 struct sk_buff *skb)
75{
76 return 1;
77}
78
9a9ad0cd
AK
79static inline int iwl3945_brightness_to_idx(enum led_brightness brightness)
80{
81 return fls(0x000000FF & (u32)brightness);
82}
ab53d8af
MA
83
84/* Send led command */
4a8a4322 85static int iwl_send_led_cmd(struct iwl_priv *priv,
4c897253 86 struct iwl_led_cmd *led_cmd)
ab53d8af 87{
c2d79b48 88 struct iwl_host_cmd cmd = {
ab53d8af 89 .id = REPLY_LEDS_CMD,
4c897253 90 .len = sizeof(struct iwl_led_cmd),
ab53d8af
MA
91 .data = led_cmd,
92 .meta.flags = CMD_ASYNC,
9a9ad0cd 93 .meta.u.callback = iwl3945_led_cmd_callback,
ab53d8af
MA
94 };
95
518099a8 96 return iwl_send_cmd(priv, &cmd);
ab53d8af
MA
97}
98
99
9a9ad0cd 100
ab53d8af 101/* Set led on command */
4a8a4322 102static int iwl3945_led_pattern(struct iwl_priv *priv, int led_id,
9a9ad0cd 103 unsigned int idx)
ab53d8af 104{
4c897253 105 struct iwl_led_cmd led_cmd = {
ab53d8af 106 .id = led_id,
ab53d8af
MA
107 .interval = IWL_DEF_LED_INTRVL
108 };
9a9ad0cd
AK
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
ab53d8af
MA
115 return iwl_send_led_cmd(priv, &led_cmd);
116}
117
9a9ad0cd 118
ab53d8af 119/* Set led on command */
4a8a4322 120static int iwl3945_led_on(struct iwl_priv *priv, int led_id)
ab53d8af 121{
4c897253 122 struct iwl_led_cmd led_cmd = {
ab53d8af 123 .id = led_id,
9a9ad0cd
AK
124 .on = IWL_LED_SOLID,
125 .off = 0,
ab53d8af
MA
126 .interval = IWL_DEF_LED_INTRVL
127 };
ab53d8af
MA
128 return iwl_send_led_cmd(priv, &led_cmd);
129}
130
ab53d8af 131/* Set led off command */
4a8a4322 132static int iwl3945_led_off(struct iwl_priv *priv, int led_id)
ab53d8af 133{
4c897253 134 struct iwl_led_cmd led_cmd = {
ab53d8af
MA
135 .id = led_id,
136 .on = 0,
137 .off = 0,
138 .interval = IWL_DEF_LED_INTRVL
139 };
e1623446 140 IWL_DEBUG_LED(priv, "led off %d\n", led_id);
ab53d8af
MA
141 return iwl_send_led_cmd(priv, &led_cmd);
142}
ab53d8af
MA
143
144/*
145 * brightness call back function for Tx/Rx LED
146 */
4a8a4322 147static int iwl3945_led_associated(struct iwl_priv *priv, int led_id)
ab53d8af
MA
148{
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);
4a8a4322 168 struct iwl_priv *priv = led->priv;
ab53d8af
MA
169
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;
e1623446 177 IWL_DEBUG_LED(priv, "MAC is associated\n");
ab53d8af
MA
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;
e1623446 185 IWL_DEBUG_LED(priv, "MAC is disassociated\n");
ab53d8af
MA
186 }
187 if (led->led_off)
188 led->led_off(priv, IWL_LED_LINK);
189 break;
190 default:
9a9ad0cd
AK
191 if (led->led_pattern) {
192 int idx = iwl3945_brightness_to_idx(brightness);
193 led->led_pattern(priv, IWL_LED_LINK, idx);
194 }
ab53d8af
MA
195 break;
196 }
197}
198
199
200
201/*
202 * Register led class with the system
203 */
4a8a4322 204static int iwl3945_led_register_led(struct iwl_priv *priv,
ab53d8af
MA
205 struct iwl3945_led *led,
206 enum led_type type, u8 set_led,
5cbbb376 207 char *trigger)
ab53d8af
MA
208{
209 struct device *device = wiphy_dev(priv->hw->wiphy);
210 int ret;
211
5cbbb376 212 led->led_dev.name = led->name;
ab53d8af
MA
213 led->led_dev.brightness_set = iwl3945_led_brightness_set;
214 led->led_dev.default_trigger = trigger;
215
b6b16196
MS
216 led->priv = priv;
217 led->type = type;
218
ab53d8af
MA
219 ret = led_classdev_register(device, &led->led_dev);
220 if (ret) {
15b1687c 221 IWL_ERR(priv, "Error: failed to register led handler.\n");
ab53d8af
MA
222 return ret;
223 }
224
ab53d8af
MA
225 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 */
4a8a4322 236static inline u8 get_blink_rate(struct iwl_priv *priv)
ab53d8af
MA
237{
238 int index;
9a9ad0cd
AK
239 u64 current_tpt = priv->rxtxpackets;
240 s64 tpt = current_tpt - priv->led_tpt;
ab53d8af 241
9a9ad0cd
AK
242 if (tpt < 0)
243 tpt = -tpt;
244 priv->led_tpt = current_tpt;
245
246 if (!priv->allow_blinking)
247 index = IWL_MAX_BLINK_TBL;
248 else
249 for (index = 0; index < IWL_MAX_BLINK_TBL; index++)
250 if (tpt > (blink_tbl[index].brightness * IWL_1MB_RATE))
251 break;
252 return index;
ab53d8af
MA
253}
254
4a8a4322 255static inline int is_rf_kill(struct iwl_priv *priv)
ab53d8af
MA
256{
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 */
4a8a4322 266void iwl3945_led_background(struct iwl_priv *priv)
ab53d8af 267{
9a9ad0cd 268 u8 blink_idx;
ab53d8af
MA
269
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;
9a9ad0cd
AK
281 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);
ab53d8af
MA
285 }
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
9a9ad0cd 293 blink_idx = get_blink_rate(priv);
ab53d8af
MA
294
295 /* call only if blink rate change */
9a9ad0cd
AK
296 if (blink_idx != priv->last_blink_rate)
297 iwl3945_led_pattern(priv, IWL_LED_LINK, blink_idx);
ab53d8af 298
9a9ad0cd
AK
299 priv->last_blink_time = jiffies;
300 priv->last_blink_rate = blink_idx;
ab53d8af
MA
301 priv->rxtxpackets = 0;
302}
303
304
305/* Register all led handler */
4a8a4322 306int iwl3945_led_register(struct iwl_priv *priv)
ab53d8af
MA
307{
308 char *trigger;
ab53d8af
MA
309 int ret;
310
311 priv->last_blink_rate = 0;
312 priv->rxtxpackets = 0;
9a9ad0cd 313 priv->led_tpt = 0;
ab53d8af
MA
314 priv->last_blink_time = 0;
315 priv->allow_blinking = 0;
316
317 trigger = ieee80211_get_radio_led_name(priv->hw);
f2c7e521
AK
318 snprintf(priv->led39[IWL_LED_TRG_RADIO].name,
319 sizeof(priv->led39[IWL_LED_TRG_RADIO].name), "iwl-%s:radio",
ab53d8af
MA
320 wiphy_name(priv->hw->wiphy));
321
f2c7e521
AK
322 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;
ab53d8af
MA
325
326 ret = iwl3945_led_register_led(priv,
f2c7e521 327 &priv->led39[IWL_LED_TRG_RADIO],
5cbbb376
SW
328 IWL_LED_TRG_RADIO, 1, trigger);
329
ab53d8af
MA
330 if (ret)
331 goto exit_fail;
332
333 trigger = ieee80211_get_assoc_led_name(priv->hw);
f2c7e521
AK
334 snprintf(priv->led39[IWL_LED_TRG_ASSOC].name,
335 sizeof(priv->led39[IWL_LED_TRG_ASSOC].name), "iwl-%s:assoc",
ab53d8af
MA
336 wiphy_name(priv->hw->wiphy));
337
338 ret = iwl3945_led_register_led(priv,
f2c7e521 339 &priv->led39[IWL_LED_TRG_ASSOC],
5cbbb376
SW
340 IWL_LED_TRG_ASSOC, 0, trigger);
341
ab53d8af 342 /* for assoc always turn led on */
f2c7e521
AK
343 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;
ab53d8af
MA
346
347 if (ret)
348 goto exit_fail;
349
350 trigger = ieee80211_get_rx_led_name(priv->hw);
f2c7e521
AK
351 snprintf(priv->led39[IWL_LED_TRG_RX].name,
352 sizeof(priv->led39[IWL_LED_TRG_RX].name), "iwl-%s:RX",
ab53d8af
MA
353 wiphy_name(priv->hw->wiphy));
354
ab53d8af 355 ret = iwl3945_led_register_led(priv,
f2c7e521 356 &priv->led39[IWL_LED_TRG_RX],
5cbbb376 357 IWL_LED_TRG_RX, 0, trigger);
ab53d8af 358
f2c7e521
AK
359 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;
ab53d8af
MA
362
363 if (ret)
364 goto exit_fail;
365
366 trigger = ieee80211_get_tx_led_name(priv->hw);
f2c7e521
AK
367 snprintf(priv->led39[IWL_LED_TRG_TX].name,
368 sizeof(priv->led39[IWL_LED_TRG_TX].name), "iwl-%s:TX",
ab53d8af 369 wiphy_name(priv->hw->wiphy));
9a9ad0cd 370
ab53d8af 371 ret = iwl3945_led_register_led(priv,
f2c7e521 372 &priv->led39[IWL_LED_TRG_TX],
5cbbb376
SW
373 IWL_LED_TRG_TX, 0, trigger);
374
f2c7e521
AK
375 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;
ab53d8af
MA
378
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 */
4a8a4322 404void iwl3945_led_unregister(struct iwl_priv *priv)
ab53d8af 405{
f2c7e521
AK
406 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);
ab53d8af
MA
410}
411
This page took 0.208616 seconds and 5 git commands to generate.