]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/net/wireless/iwlwifi/iwl-3945-led.c
b34bef4891b8634a88231082e5818e3b30c350d2
[mirror_ubuntu-artful-kernel.git] / drivers / net / wireless / iwlwifi / iwl-3945-led.c
1 /******************************************************************************
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 * Intel Linux Wireless <ilw@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/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
41 #include "iwl-commands.h"
42 #include "iwl-3945-commands.h"
43 #include "iwl-3945.h"
44
45
46 static const struct {
47 u16 brightness;
48 u8 on_time;
49 u8 off_time;
50 } 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},
61 {0, 167, 167},
62 /*SOLID_ON*/
63 {-1, IWL_LED_SOLID, 0}
64 };
65
66 #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
71 static 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
78 static inline int iwl3945_brightness_to_idx(enum led_brightness brightness)
79 {
80 return fls(0x000000FF & (u32)brightness);
81 }
82
83 /* Send led command */
84 static int iwl_send_led_cmd(struct iwl3945_priv *priv,
85 struct iwl_led_cmd *led_cmd)
86 {
87 struct iwl3945_host_cmd cmd = {
88 .id = REPLY_LEDS_CMD,
89 .len = sizeof(struct iwl_led_cmd),
90 .data = led_cmd,
91 .meta.flags = CMD_ASYNC,
92 .meta.u.callback = iwl3945_led_cmd_callback,
93 };
94
95 return iwl3945_send_cmd(priv, &cmd);
96 }
97
98
99
100 /* Set led on command */
101 static int iwl3945_led_pattern(struct iwl3945_priv *priv, int led_id,
102 unsigned int idx)
103 {
104 struct iwl_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 /* Set led on command */
119 static int iwl3945_led_on(struct iwl3945_priv *priv, int led_id)
120 {
121 struct iwl_led_cmd led_cmd = {
122 .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
130 /* Set led off command */
131 static int iwl3945_led_off(struct iwl3945_priv *priv, int led_id)
132 {
133 struct iwl_led_cmd led_cmd = {
134 .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 }
142
143 /*
144 * brightness call back function for Tx/Rx LED
145 */
146 static 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 */
162 static 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:
190 if (led->led_pattern) {
191 int idx = iwl3945_brightness_to_idx(brightness);
192 led->led_pattern(priv, IWL_LED_LINK, idx);
193 }
194 break;
195 }
196 }
197
198
199
200 /*
201 * Register led class with the system
202 */
203 static int iwl3945_led_register_led(struct iwl3945_priv *priv,
204 struct iwl3945_led *led,
205 enum led_type type, u8 set_led,
206 char *trigger)
207 {
208 struct device *device = wiphy_dev(priv->hw->wiphy);
209 int ret;
210
211 led->led_dev.name = led->name;
212 led->led_dev.brightness_set = iwl3945_led_brightness_set;
213 led->led_dev.default_trigger = trigger;
214
215 led->priv = priv;
216 led->type = type;
217
218 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
224 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 */
235 static inline u8 get_blink_rate(struct iwl3945_priv *priv)
236 {
237 int index;
238 u64 current_tpt = priv->rxtxpackets;
239 s64 tpt = current_tpt - priv->led_tpt;
240
241 if (tpt < 0)
242 tpt = -tpt;
243 priv->led_tpt = current_tpt;
244
245 if (!priv->allow_blinking)
246 index = IWL_MAX_BLINK_TBL;
247 else
248 for (index = 0; index < IWL_MAX_BLINK_TBL; index++)
249 if (tpt > (blink_tbl[index].brightness * IWL_1MB_RATE))
250 break;
251 return index;
252 }
253
254 static 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 */
265 void iwl3945_led_background(struct iwl3945_priv *priv)
266 {
267 u8 blink_idx;
268
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;
280 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);
284 }
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
292 blink_idx = get_blink_rate(priv);
293
294 /* call only if blink rate change */
295 if (blink_idx != priv->last_blink_rate)
296 iwl3945_led_pattern(priv, IWL_LED_LINK, blink_idx);
297
298 priv->last_blink_time = jiffies;
299 priv->last_blink_rate = blink_idx;
300 priv->rxtxpackets = 0;
301 }
302
303
304 /* Register all led handler */
305 int iwl3945_led_register(struct iwl3945_priv *priv)
306 {
307 char *trigger;
308 int ret;
309
310 priv->last_blink_rate = 0;
311 priv->rxtxpackets = 0;
312 priv->led_tpt = 0;
313 priv->last_blink_time = 0;
314 priv->allow_blinking = 0;
315
316 trigger = ieee80211_get_radio_led_name(priv->hw);
317 snprintf(priv->led[IWL_LED_TRG_RADIO].name,
318 sizeof(priv->led[IWL_LED_TRG_RADIO].name), "iwl-%s:radio",
319 wiphy_name(priv->hw->wiphy));
320
321 priv->led[IWL_LED_TRG_RADIO].led_on = iwl3945_led_on;
322 priv->led[IWL_LED_TRG_RADIO].led_off = iwl3945_led_off;
323 priv->led[IWL_LED_TRG_RADIO].led_pattern = NULL;
324
325 ret = iwl3945_led_register_led(priv,
326 &priv->led[IWL_LED_TRG_RADIO],
327 IWL_LED_TRG_RADIO, 1, trigger);
328
329 if (ret)
330 goto exit_fail;
331
332 trigger = ieee80211_get_assoc_led_name(priv->hw);
333 snprintf(priv->led[IWL_LED_TRG_ASSOC].name,
334 sizeof(priv->led[IWL_LED_TRG_ASSOC].name), "iwl-%s:assoc",
335 wiphy_name(priv->hw->wiphy));
336
337 ret = iwl3945_led_register_led(priv,
338 &priv->led[IWL_LED_TRG_ASSOC],
339 IWL_LED_TRG_ASSOC, 0, trigger);
340
341 /* for assoc always turn led on */
342 priv->led[IWL_LED_TRG_ASSOC].led_on = iwl3945_led_on;
343 priv->led[IWL_LED_TRG_ASSOC].led_off = iwl3945_led_on;
344 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);
350 snprintf(priv->led[IWL_LED_TRG_RX].name,
351 sizeof(priv->led[IWL_LED_TRG_RX].name), "iwl-%s:RX",
352 wiphy_name(priv->hw->wiphy));
353
354 ret = iwl3945_led_register_led(priv,
355 &priv->led[IWL_LED_TRG_RX],
356 IWL_LED_TRG_RX, 0, trigger);
357
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);
366 snprintf(priv->led[IWL_LED_TRG_TX].name,
367 sizeof(priv->led[IWL_LED_TRG_TX].name), "iwl-%s:TX",
368 wiphy_name(priv->hw->wiphy));
369
370 ret = iwl3945_led_register_led(priv,
371 &priv->led[IWL_LED_TRG_TX],
372 IWL_LED_TRG_TX, 0, trigger);
373
374 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
383 exit_fail:
384 iwl3945_led_unregister(priv);
385 return ret;
386 }
387
388
389 /* unregister led class */
390 static 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 */
403 void 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