]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/net/wireless/iwlwifi/iwl-led.c
f547233c5b7923e88670943bdcc2366c0551b35a
[mirror_ubuntu-bionic-kernel.git] / drivers / net / wireless / iwlwifi / iwl-led.c
1 /******************************************************************************
2 *
3 * Copyright(c) 2003 - 2009 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-dev.h"
42 #include "iwl-core.h"
43 #include "iwl-io.h"
44
45 #ifdef CONFIG_IWLWIFI_DEBUG
46 static const char *led_type_str[] = {
47 __stringify(IWL_LED_TRG_TX),
48 __stringify(IWL_LED_TRG_RX),
49 __stringify(IWL_LED_TRG_ASSOC),
50 __stringify(IWL_LED_TRG_RADIO),
51 NULL
52 };
53 #endif /* CONFIG_IWLWIFI_DEBUG */
54
55
56 static const struct {
57 u16 tpt; /* Mb/s */
58 u8 on_time;
59 u8 off_time;
60 } blink_tbl[] =
61 {
62 {300, 25, 25},
63 {200, 40, 40},
64 {100, 55, 55},
65 {70, 65, 65},
66 {50, 75, 75},
67 {20, 85, 85},
68 {10, 95, 95},
69 {5, 110, 110},
70 {1, 130, 130},
71 {0, 167, 167},
72 /* SOLID_ON */
73 {-1, IWL_LED_SOLID, 0}
74 };
75
76 #define IWL_1MB_RATE (128 * 1024)
77 #define IWL_LED_THRESHOLD (16)
78 #define IWL_MAX_BLINK_TBL (ARRAY_SIZE(blink_tbl) - 1) /* exclude SOLID_ON */
79 #define IWL_SOLID_BLINK_IDX (ARRAY_SIZE(blink_tbl) - 1)
80
81 /*
82 * Adjust led blink rate to compensate on a MAC Clock difference on every HW
83 * Led blink rate analysis showed an average deviation of 0% on 3945,
84 * 5% on 4965 HW and 20% on 5000 series and up.
85 * Need to compensate on the led on/off time per HW according to the deviation
86 * to achieve the desired led frequency
87 * The calculation is: (100-averageDeviation)/100 * blinkTime
88 * For code efficiency the calculation will be:
89 * compensation = (100 - averageDeviation) * 64 / 100
90 * NewBlinkTime = (compensation * BlinkTime) / 64
91 */
92 static inline u8 iwl_blink_compensation(struct iwl_priv *priv,
93 u8 time, u16 compensation)
94 {
95 if (!compensation) {
96 IWL_ERR(priv, "undefined blink compensation: "
97 "use pre-defined blinking time\n");
98 return time;
99 }
100
101 return (u8)((time * compensation) >> 6);
102 }
103
104 /* [0-256] -> [0..8] FIXME: we need [0..10] */
105 static inline int iwl_brightness_to_idx(enum led_brightness brightness)
106 {
107 return fls(0x000000FF & (u32)brightness);
108 }
109
110 /* Send led command */
111 static int iwl_send_led_cmd(struct iwl_priv *priv, struct iwl_led_cmd *led_cmd)
112 {
113 struct iwl_host_cmd cmd = {
114 .id = REPLY_LEDS_CMD,
115 .len = sizeof(struct iwl_led_cmd),
116 .data = led_cmd,
117 .flags = CMD_ASYNC,
118 .callback = NULL,
119 };
120 u32 reg;
121
122 reg = iwl_read32(priv, CSR_LED_REG);
123 if (reg != (reg & CSR_LED_BSM_CTRL_MSK))
124 iwl_write32(priv, CSR_LED_REG, reg & CSR_LED_BSM_CTRL_MSK);
125
126 return iwl_send_cmd(priv, &cmd);
127 }
128
129 /* Set led pattern command */
130 static int iwl_led_pattern(struct iwl_priv *priv, int led_id,
131 unsigned int idx)
132 {
133 struct iwl_led_cmd led_cmd = {
134 .id = led_id,
135 .interval = IWL_DEF_LED_INTRVL
136 };
137
138 BUG_ON(idx > IWL_MAX_BLINK_TBL);
139
140 IWL_DEBUG_LED(priv, "Led blink time compensation= %u\n",
141 priv->cfg->led_compensation);
142 led_cmd.on =
143 iwl_blink_compensation(priv, blink_tbl[idx].on_time,
144 priv->cfg->led_compensation);
145 led_cmd.off =
146 iwl_blink_compensation(priv, blink_tbl[idx].off_time,
147 priv->cfg->led_compensation);
148
149 return iwl_send_led_cmd(priv, &led_cmd);
150 }
151
152 /* Set led register off */
153 static int iwl_led_on_reg(struct iwl_priv *priv, int led_id)
154 {
155 IWL_DEBUG_LED(priv, "led on %d\n", led_id);
156 iwl_write32(priv, CSR_LED_REG, CSR_LED_REG_TRUN_ON);
157 return 0;
158 }
159
160 #if 0
161 /* Set led on command */
162 static int iwl_led_on(struct iwl_priv *priv, int led_id)
163 {
164 struct iwl_led_cmd led_cmd = {
165 .id = led_id,
166 .on = IWL_LED_SOLID,
167 .off = 0,
168 .interval = IWL_DEF_LED_INTRVL
169 };
170 return iwl_send_led_cmd(priv, &led_cmd);
171 }
172
173 /* Set led off command */
174 int iwl_led_off(struct iwl_priv *priv, int led_id)
175 {
176 struct iwl_led_cmd led_cmd = {
177 .id = led_id,
178 .on = 0,
179 .off = 0,
180 .interval = IWL_DEF_LED_INTRVL
181 };
182 IWL_DEBUG_LED(priv, "led off %d\n", led_id);
183 return iwl_send_led_cmd(priv, &led_cmd);
184 }
185 #endif
186
187
188 /* Set led register off */
189 static int iwl_led_off_reg(struct iwl_priv *priv, int led_id)
190 {
191 IWL_DEBUG_LED(priv, "LED Reg off\n");
192 iwl_write32(priv, CSR_LED_REG, CSR_LED_REG_TRUN_OFF);
193 return 0;
194 }
195
196 /*
197 * Set led register in case of disassociation according to rfkill state
198 */
199 static int iwl_led_associate(struct iwl_priv *priv, int led_id)
200 {
201 IWL_DEBUG_LED(priv, "Associated\n");
202 priv->allow_blinking = 1;
203 return iwl_led_on_reg(priv, led_id);
204 }
205 static int iwl_led_disassociate(struct iwl_priv *priv, int led_id)
206 {
207 priv->allow_blinking = 0;
208
209 return 0;
210 }
211
212 /*
213 * brightness call back function for Tx/Rx LED
214 */
215 static int iwl_led_associated(struct iwl_priv *priv, int led_id)
216 {
217 if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
218 !test_bit(STATUS_READY, &priv->status))
219 return 0;
220
221
222 /* start counting Tx/Rx bytes */
223 if (!priv->last_blink_time && priv->allow_blinking)
224 priv->last_blink_time = jiffies;
225 return 0;
226 }
227
228 /*
229 * brightness call back for association and radio
230 */
231 static void iwl_led_brightness_set(struct led_classdev *led_cdev,
232 enum led_brightness brightness)
233 {
234 struct iwl_led *led = container_of(led_cdev, struct iwl_led, led_dev);
235 struct iwl_priv *priv = led->priv;
236
237 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
238 return;
239
240
241 IWL_DEBUG_LED(priv, "Led type = %s brightness = %d\n",
242 led_type_str[led->type], brightness);
243 switch (brightness) {
244 case LED_FULL:
245 if (led->led_on)
246 led->led_on(priv, IWL_LED_LINK);
247 break;
248 case LED_OFF:
249 if (led->led_off)
250 led->led_off(priv, IWL_LED_LINK);
251 break;
252 default:
253 if (led->led_pattern) {
254 int idx = iwl_brightness_to_idx(brightness);
255 led->led_pattern(priv, IWL_LED_LINK, idx);
256 }
257 break;
258 }
259 }
260
261
262
263 /*
264 * Register led class with the system
265 */
266 static int iwl_leds_register_led(struct iwl_priv *priv, struct iwl_led *led,
267 enum led_type type, u8 set_led,
268 char *trigger)
269 {
270 struct device *device = wiphy_dev(priv->hw->wiphy);
271 int ret;
272
273 led->led_dev.name = led->name;
274 led->led_dev.brightness_set = iwl_led_brightness_set;
275 led->led_dev.default_trigger = trigger;
276
277 led->priv = priv;
278 led->type = type;
279
280 ret = led_classdev_register(device, &led->led_dev);
281 if (ret) {
282 IWL_ERR(priv, "Error: failed to register led handler.\n");
283 return ret;
284 }
285
286 led->registered = 1;
287
288 if (set_led && led->led_on)
289 led->led_on(priv, IWL_LED_LINK);
290
291 return 0;
292 }
293
294
295 /*
296 * calculate blink rate according to last second Tx/Rx activities
297 */
298 static int iwl_get_blink_rate(struct iwl_priv *priv)
299 {
300 int i;
301 /* count both tx and rx traffic to be able to
302 * handle traffic in either direction
303 */
304 u64 current_tpt = priv->tx_stats.data_bytes +
305 priv->rx_stats.data_bytes;
306 s64 tpt = current_tpt - priv->led_tpt;
307
308 if (tpt < 0) /* wraparound */
309 tpt = -tpt;
310
311 IWL_DEBUG_LED(priv, "tpt %lld current_tpt %llu\n",
312 (long long)tpt,
313 (unsigned long long)current_tpt);
314 priv->led_tpt = current_tpt;
315
316 if (!priv->allow_blinking)
317 i = IWL_MAX_BLINK_TBL;
318 else
319 for (i = 0; i < IWL_MAX_BLINK_TBL; i++)
320 if (tpt > (blink_tbl[i].tpt * IWL_1MB_RATE))
321 break;
322
323 IWL_DEBUG_LED(priv, "LED BLINK IDX=%d\n", i);
324 return i;
325 }
326
327 /*
328 * this function called from handler. Since setting Led command can
329 * happen very frequent we postpone led command to be called from
330 * REPLY handler so we know ucode is up
331 */
332 void iwl_leds_background(struct iwl_priv *priv)
333 {
334 u8 blink_idx;
335
336 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
337 priv->last_blink_time = 0;
338 return;
339 }
340 if (iwl_is_rfkill(priv)) {
341 priv->last_blink_time = 0;
342 return;
343 }
344
345 if (!priv->allow_blinking) {
346 priv->last_blink_time = 0;
347 if (priv->last_blink_rate != IWL_SOLID_BLINK_IDX) {
348 priv->last_blink_rate = IWL_SOLID_BLINK_IDX;
349 iwl_led_pattern(priv, IWL_LED_LINK,
350 IWL_SOLID_BLINK_IDX);
351 }
352 return;
353 }
354 if (!priv->last_blink_time ||
355 !time_after(jiffies, priv->last_blink_time +
356 msecs_to_jiffies(1000)))
357 return;
358
359 blink_idx = iwl_get_blink_rate(priv);
360
361 /* call only if blink rate change */
362 if (blink_idx != priv->last_blink_rate)
363 iwl_led_pattern(priv, IWL_LED_LINK, blink_idx);
364
365 priv->last_blink_time = jiffies;
366 priv->last_blink_rate = blink_idx;
367 }
368
369 /* Register all led handler */
370 int iwl_leds_register(struct iwl_priv *priv)
371 {
372 char *trigger;
373 int ret;
374
375 priv->last_blink_rate = 0;
376 priv->led_tpt = 0;
377 priv->last_blink_time = 0;
378 priv->allow_blinking = 0;
379
380 trigger = ieee80211_get_radio_led_name(priv->hw);
381 snprintf(priv->led[IWL_LED_TRG_RADIO].name,
382 sizeof(priv->led[IWL_LED_TRG_RADIO].name), "iwl-%s::radio",
383 wiphy_name(priv->hw->wiphy));
384
385 priv->led[IWL_LED_TRG_RADIO].led_on = iwl_led_on_reg;
386 priv->led[IWL_LED_TRG_RADIO].led_off = iwl_led_off_reg;
387 priv->led[IWL_LED_TRG_RADIO].led_pattern = NULL;
388
389 ret = iwl_leds_register_led(priv, &priv->led[IWL_LED_TRG_RADIO],
390 IWL_LED_TRG_RADIO, 1, trigger);
391 if (ret)
392 goto exit_fail;
393
394 trigger = ieee80211_get_assoc_led_name(priv->hw);
395 snprintf(priv->led[IWL_LED_TRG_ASSOC].name,
396 sizeof(priv->led[IWL_LED_TRG_ASSOC].name), "iwl-%s::assoc",
397 wiphy_name(priv->hw->wiphy));
398
399 ret = iwl_leds_register_led(priv, &priv->led[IWL_LED_TRG_ASSOC],
400 IWL_LED_TRG_ASSOC, 0, trigger);
401
402 /* for assoc always turn led on */
403 priv->led[IWL_LED_TRG_ASSOC].led_on = iwl_led_associate;
404 priv->led[IWL_LED_TRG_ASSOC].led_off = iwl_led_disassociate;
405 priv->led[IWL_LED_TRG_ASSOC].led_pattern = NULL;
406
407 if (ret)
408 goto exit_fail;
409
410 trigger = ieee80211_get_rx_led_name(priv->hw);
411 snprintf(priv->led[IWL_LED_TRG_RX].name,
412 sizeof(priv->led[IWL_LED_TRG_RX].name), "iwl-%s::RX",
413 wiphy_name(priv->hw->wiphy));
414
415 ret = iwl_leds_register_led(priv, &priv->led[IWL_LED_TRG_RX],
416 IWL_LED_TRG_RX, 0, trigger);
417
418 priv->led[IWL_LED_TRG_RX].led_on = iwl_led_associated;
419 priv->led[IWL_LED_TRG_RX].led_off = iwl_led_associated;
420 priv->led[IWL_LED_TRG_RX].led_pattern = iwl_led_pattern;
421
422 if (ret)
423 goto exit_fail;
424
425 trigger = ieee80211_get_tx_led_name(priv->hw);
426 snprintf(priv->led[IWL_LED_TRG_TX].name,
427 sizeof(priv->led[IWL_LED_TRG_TX].name), "iwl-%s::TX",
428 wiphy_name(priv->hw->wiphy));
429
430 ret = iwl_leds_register_led(priv, &priv->led[IWL_LED_TRG_TX],
431 IWL_LED_TRG_TX, 0, trigger);
432
433 priv->led[IWL_LED_TRG_TX].led_on = iwl_led_associated;
434 priv->led[IWL_LED_TRG_TX].led_off = iwl_led_associated;
435 priv->led[IWL_LED_TRG_TX].led_pattern = iwl_led_pattern;
436
437 if (ret)
438 goto exit_fail;
439
440 return 0;
441
442 exit_fail:
443 iwl_leds_unregister(priv);
444 return ret;
445 }
446 EXPORT_SYMBOL(iwl_leds_register);
447
448 /* unregister led class */
449 static void iwl_leds_unregister_led(struct iwl_led *led, u8 set_led)
450 {
451 if (!led->registered)
452 return;
453
454 led_classdev_unregister(&led->led_dev);
455
456 if (set_led)
457 led->led_dev.brightness_set(&led->led_dev, LED_OFF);
458 led->registered = 0;
459 }
460
461 /* Unregister all led handlers */
462 void iwl_leds_unregister(struct iwl_priv *priv)
463 {
464 iwl_leds_unregister_led(&priv->led[IWL_LED_TRG_ASSOC], 0);
465 iwl_leds_unregister_led(&priv->led[IWL_LED_TRG_RX], 0);
466 iwl_leds_unregister_led(&priv->led[IWL_LED_TRG_TX], 0);
467 iwl_leds_unregister_led(&priv->led[IWL_LED_TRG_RADIO], 1);
468 }
469 EXPORT_SYMBOL(iwl_leds_unregister);
470