Commit | Line | Data |
---|---|---|
ab53d8af MA |
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 | * James P. Ketrenos <ipw2100-admin@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> | |
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 | ||
41 | #include "iwl-3945.h" | |
42 | #include "iwl-helpers.h" | |
43 | ||
ab53d8af MA |
44 | |
45 | static const struct { | |
46 | u16 brightness; | |
47 | u8 on_time; | |
9a9ad0cd | 48 | u8 off_time; |
ab53d8af MA |
49 | } blink_tbl[] = |
50 | { | |
51 | {300, 25, 25}, | |
52 | {200, 40, 40}, | |
53 | {100, 55, 55}, | |
54 | {70, 65, 65}, | |
55 | {50, 75, 75}, | |
56 | {20, 85, 85}, | |
57 | {15, 95, 95 }, | |
58 | {10, 110, 110}, | |
59 | {5, 130, 130}, | |
9a9ad0cd AK |
60 | {0, 167, 167}, |
61 | /*SOLID_ON*/ | |
62 | {-1, IWL_LED_SOLID, 0} | |
ab53d8af MA |
63 | }; |
64 | ||
9a9ad0cd AK |
65 | #define IWL_1MB_RATE (128 * 1024) |
66 | #define IWL_LED_THRESHOLD (16) | |
67 | #define IWL_MAX_BLINK_TBL (ARRAY_SIZE(blink_tbl) - 1) /*Exclude Solid on*/ | |
68 | #define IWL_SOLID_BLINK_IDX (ARRAY_SIZE(blink_tbl) - 1) | |
69 | ||
ab53d8af MA |
70 | static int iwl3945_led_cmd_callback(struct iwl3945_priv *priv, |
71 | struct iwl3945_cmd *cmd, | |
72 | struct sk_buff *skb) | |
73 | { | |
74 | return 1; | |
75 | } | |
76 | ||
9a9ad0cd AK |
77 | static inline int iwl3945_brightness_to_idx(enum led_brightness brightness) |
78 | { | |
79 | return fls(0x000000FF & (u32)brightness); | |
80 | } | |
ab53d8af MA |
81 | |
82 | /* Send led command */ | |
83 | static int iwl_send_led_cmd(struct iwl3945_priv *priv, | |
84 | struct iwl3945_led_cmd *led_cmd) | |
85 | { | |
86 | struct iwl3945_host_cmd cmd = { | |
87 | .id = REPLY_LEDS_CMD, | |
88 | .len = sizeof(struct iwl3945_led_cmd), | |
89 | .data = led_cmd, | |
90 | .meta.flags = CMD_ASYNC, | |
9a9ad0cd | 91 | .meta.u.callback = iwl3945_led_cmd_callback, |
ab53d8af MA |
92 | }; |
93 | ||
94 | return iwl3945_send_cmd(priv, &cmd); | |
95 | } | |
96 | ||
97 | ||
9a9ad0cd | 98 | |
ab53d8af | 99 | /* Set led on command */ |
9a9ad0cd AK |
100 | static int iwl3945_led_pattern(struct iwl3945_priv *priv, int led_id, |
101 | unsigned int idx) | |
ab53d8af MA |
102 | { |
103 | struct iwl3945_led_cmd led_cmd = { | |
104 | .id = led_id, | |
ab53d8af MA |
105 | .interval = IWL_DEF_LED_INTRVL |
106 | }; | |
9a9ad0cd AK |
107 | |
108 | BUG_ON(idx > IWL_MAX_BLINK_TBL); | |
109 | ||
110 | led_cmd.on = blink_tbl[idx].on_time; | |
111 | led_cmd.off = blink_tbl[idx].off_time; | |
112 | ||
ab53d8af MA |
113 | return iwl_send_led_cmd(priv, &led_cmd); |
114 | } | |
115 | ||
9a9ad0cd AK |
116 | |
117 | #if 1 | |
ab53d8af | 118 | /* Set led on command */ |
9a9ad0cd | 119 | static int iwl3945_led_on(struct iwl3945_priv *priv, int led_id) |
ab53d8af MA |
120 | { |
121 | struct iwl3945_led_cmd led_cmd = { | |
122 | .id = led_id, | |
9a9ad0cd AK |
123 | .on = IWL_LED_SOLID, |
124 | .off = 0, | |
ab53d8af MA |
125 | .interval = IWL_DEF_LED_INTRVL |
126 | }; | |
ab53d8af MA |
127 | return iwl_send_led_cmd(priv, &led_cmd); |
128 | } | |
129 | ||
ab53d8af MA |
130 | /* Set led off command */ |
131 | static int iwl3945_led_off(struct iwl3945_priv *priv, int led_id) | |
132 | { | |
133 | struct iwl3945_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 | } | |
9a9ad0cd | 142 | #endif |
ab53d8af MA |
143 | |
144 | ||
145 | /* | |
146 | * brightness call back function for Tx/Rx LED | |
147 | */ | |
148 | static int iwl3945_led_associated(struct iwl3945_priv *priv, int led_id) | |
149 | { | |
150 | if (test_bit(STATUS_EXIT_PENDING, &priv->status) || | |
151 | !test_bit(STATUS_READY, &priv->status)) | |
152 | return 0; | |
153 | ||
154 | ||
155 | /* start counting Tx/Rx bytes */ | |
156 | if (!priv->last_blink_time && priv->allow_blinking) | |
157 | priv->last_blink_time = jiffies; | |
158 | return 0; | |
159 | } | |
160 | ||
161 | /* | |
162 | * brightness call back for association and radio | |
163 | */ | |
164 | static void iwl3945_led_brightness_set(struct led_classdev *led_cdev, | |
165 | enum led_brightness brightness) | |
166 | { | |
167 | struct iwl3945_led *led = container_of(led_cdev, | |
168 | struct iwl3945_led, led_dev); | |
169 | struct iwl3945_priv *priv = led->priv; | |
170 | ||
171 | if (test_bit(STATUS_EXIT_PENDING, &priv->status)) | |
172 | return; | |
173 | ||
174 | switch (brightness) { | |
175 | case LED_FULL: | |
176 | if (led->type == IWL_LED_TRG_ASSOC) { | |
177 | priv->allow_blinking = 1; | |
178 | IWL_DEBUG_LED("MAC is associated\n"); | |
179 | } | |
180 | if (led->led_on) | |
181 | led->led_on(priv, IWL_LED_LINK); | |
182 | break; | |
183 | case LED_OFF: | |
184 | if (led->type == IWL_LED_TRG_ASSOC) { | |
185 | priv->allow_blinking = 0; | |
186 | IWL_DEBUG_LED("MAC is disassociated\n"); | |
187 | } | |
188 | if (led->led_off) | |
189 | led->led_off(priv, IWL_LED_LINK); | |
190 | break; | |
191 | default: | |
9a9ad0cd AK |
192 | if (led->led_pattern) { |
193 | int idx = iwl3945_brightness_to_idx(brightness); | |
194 | led->led_pattern(priv, IWL_LED_LINK, idx); | |
195 | } | |
ab53d8af MA |
196 | break; |
197 | } | |
198 | } | |
199 | ||
200 | ||
201 | ||
202 | /* | |
203 | * Register led class with the system | |
204 | */ | |
205 | static int iwl3945_led_register_led(struct iwl3945_priv *priv, | |
206 | struct iwl3945_led *led, | |
207 | enum led_type type, u8 set_led, | |
5cbbb376 | 208 | char *trigger) |
ab53d8af MA |
209 | { |
210 | struct device *device = wiphy_dev(priv->hw->wiphy); | |
211 | int ret; | |
212 | ||
5cbbb376 | 213 | led->led_dev.name = led->name; |
ab53d8af MA |
214 | led->led_dev.brightness_set = iwl3945_led_brightness_set; |
215 | led->led_dev.default_trigger = trigger; | |
216 | ||
b6b16196 MS |
217 | led->priv = priv; |
218 | led->type = type; | |
219 | ||
ab53d8af MA |
220 | ret = led_classdev_register(device, &led->led_dev); |
221 | if (ret) { | |
222 | IWL_ERROR("Error: failed to register led handler.\n"); | |
223 | return ret; | |
224 | } | |
225 | ||
ab53d8af MA |
226 | led->registered = 1; |
227 | ||
228 | if (set_led && led->led_on) | |
229 | led->led_on(priv, IWL_LED_LINK); | |
230 | return 0; | |
231 | } | |
232 | ||
233 | ||
234 | /* | |
235 | * calculate blink rate according to last 2 sec Tx/Rx activities | |
236 | */ | |
237 | static inline u8 get_blink_rate(struct iwl3945_priv *priv) | |
238 | { | |
239 | int index; | |
9a9ad0cd AK |
240 | u64 current_tpt = priv->rxtxpackets; |
241 | s64 tpt = current_tpt - priv->led_tpt; | |
ab53d8af | 242 | |
9a9ad0cd AK |
243 | if (tpt < 0) |
244 | tpt = -tpt; | |
245 | priv->led_tpt = current_tpt; | |
246 | ||
247 | if (!priv->allow_blinking) | |
248 | index = IWL_MAX_BLINK_TBL; | |
249 | else | |
250 | for (index = 0; index < IWL_MAX_BLINK_TBL; index++) | |
251 | if (tpt > (blink_tbl[index].brightness * IWL_1MB_RATE)) | |
252 | break; | |
253 | return index; | |
ab53d8af MA |
254 | } |
255 | ||
256 | static inline int is_rf_kill(struct iwl3945_priv *priv) | |
257 | { | |
258 | return test_bit(STATUS_RF_KILL_HW, &priv->status) || | |
259 | test_bit(STATUS_RF_KILL_SW, &priv->status); | |
260 | } | |
261 | ||
262 | /* | |
263 | * this function called from handler. Since setting Led command can | |
264 | * happen very frequent we postpone led command to be called from | |
265 | * REPLY handler so we know ucode is up | |
266 | */ | |
267 | void iwl3945_led_background(struct iwl3945_priv *priv) | |
268 | { | |
9a9ad0cd | 269 | u8 blink_idx; |
ab53d8af MA |
270 | |
271 | if (test_bit(STATUS_EXIT_PENDING, &priv->status)) { | |
272 | priv->last_blink_time = 0; | |
273 | return; | |
274 | } | |
275 | if (is_rf_kill(priv)) { | |
276 | priv->last_blink_time = 0; | |
277 | return; | |
278 | } | |
279 | ||
280 | if (!priv->allow_blinking) { | |
281 | priv->last_blink_time = 0; | |
9a9ad0cd AK |
282 | if (priv->last_blink_rate != IWL_SOLID_BLINK_IDX) { |
283 | priv->last_blink_rate = IWL_SOLID_BLINK_IDX; | |
284 | iwl3945_led_pattern(priv, IWL_LED_LINK, | |
285 | IWL_SOLID_BLINK_IDX); | |
ab53d8af MA |
286 | } |
287 | return; | |
288 | } | |
289 | if (!priv->last_blink_time || | |
290 | !time_after(jiffies, priv->last_blink_time + | |
291 | msecs_to_jiffies(1000))) | |
292 | return; | |
293 | ||
9a9ad0cd | 294 | blink_idx = get_blink_rate(priv); |
ab53d8af MA |
295 | |
296 | /* call only if blink rate change */ | |
9a9ad0cd AK |
297 | if (blink_idx != priv->last_blink_rate) |
298 | iwl3945_led_pattern(priv, IWL_LED_LINK, blink_idx); | |
ab53d8af | 299 | |
9a9ad0cd AK |
300 | priv->last_blink_time = jiffies; |
301 | priv->last_blink_rate = blink_idx; | |
ab53d8af MA |
302 | priv->rxtxpackets = 0; |
303 | } | |
304 | ||
305 | ||
306 | /* Register all led handler */ | |
307 | int iwl3945_led_register(struct iwl3945_priv *priv) | |
308 | { | |
309 | char *trigger; | |
ab53d8af MA |
310 | int ret; |
311 | ||
312 | priv->last_blink_rate = 0; | |
313 | priv->rxtxpackets = 0; | |
9a9ad0cd | 314 | priv->led_tpt = 0; |
ab53d8af MA |
315 | priv->last_blink_time = 0; |
316 | priv->allow_blinking = 0; | |
317 | ||
318 | trigger = ieee80211_get_radio_led_name(priv->hw); | |
5cbbb376 SW |
319 | snprintf(priv->led[IWL_LED_TRG_RADIO].name, |
320 | sizeof(priv->led[IWL_LED_TRG_RADIO].name), "iwl-%s:radio", | |
ab53d8af MA |
321 | wiphy_name(priv->hw->wiphy)); |
322 | ||
9a9ad0cd AK |
323 | priv->led[IWL_LED_TRG_RADIO].led_on = iwl3945_led_on; |
324 | priv->led[IWL_LED_TRG_RADIO].led_off = iwl3945_led_off; | |
ab53d8af MA |
325 | priv->led[IWL_LED_TRG_RADIO].led_pattern = NULL; |
326 | ||
327 | ret = iwl3945_led_register_led(priv, | |
328 | &priv->led[IWL_LED_TRG_RADIO], | |
5cbbb376 SW |
329 | IWL_LED_TRG_RADIO, 1, trigger); |
330 | ||
ab53d8af MA |
331 | if (ret) |
332 | goto exit_fail; | |
333 | ||
334 | trigger = ieee80211_get_assoc_led_name(priv->hw); | |
5cbbb376 SW |
335 | snprintf(priv->led[IWL_LED_TRG_ASSOC].name, |
336 | sizeof(priv->led[IWL_LED_TRG_ASSOC].name), "iwl-%s:assoc", | |
ab53d8af MA |
337 | wiphy_name(priv->hw->wiphy)); |
338 | ||
339 | ret = iwl3945_led_register_led(priv, | |
340 | &priv->led[IWL_LED_TRG_ASSOC], | |
5cbbb376 SW |
341 | IWL_LED_TRG_ASSOC, 0, trigger); |
342 | ||
ab53d8af | 343 | /* for assoc always turn led on */ |
9a9ad0cd AK |
344 | priv->led[IWL_LED_TRG_ASSOC].led_on = iwl3945_led_on; |
345 | priv->led[IWL_LED_TRG_ASSOC].led_off = iwl3945_led_on; | |
ab53d8af MA |
346 | priv->led[IWL_LED_TRG_ASSOC].led_pattern = NULL; |
347 | ||
348 | if (ret) | |
349 | goto exit_fail; | |
350 | ||
351 | trigger = ieee80211_get_rx_led_name(priv->hw); | |
5cbbb376 SW |
352 | snprintf(priv->led[IWL_LED_TRG_RX].name, |
353 | sizeof(priv->led[IWL_LED_TRG_RX].name), "iwl-%s:RX", | |
ab53d8af MA |
354 | wiphy_name(priv->hw->wiphy)); |
355 | ||
ab53d8af MA |
356 | ret = iwl3945_led_register_led(priv, |
357 | &priv->led[IWL_LED_TRG_RX], | |
5cbbb376 | 358 | IWL_LED_TRG_RX, 0, trigger); |
ab53d8af MA |
359 | |
360 | priv->led[IWL_LED_TRG_RX].led_on = iwl3945_led_associated; | |
361 | priv->led[IWL_LED_TRG_RX].led_off = iwl3945_led_associated; | |
362 | priv->led[IWL_LED_TRG_RX].led_pattern = iwl3945_led_pattern; | |
363 | ||
364 | if (ret) | |
365 | goto exit_fail; | |
366 | ||
367 | trigger = ieee80211_get_tx_led_name(priv->hw); | |
5cbbb376 SW |
368 | snprintf(priv->led[IWL_LED_TRG_TX].name, |
369 | sizeof(priv->led[IWL_LED_TRG_TX].name), "iwl-%s:TX", | |
ab53d8af | 370 | wiphy_name(priv->hw->wiphy)); |
9a9ad0cd | 371 | |
ab53d8af MA |
372 | ret = iwl3945_led_register_led(priv, |
373 | &priv->led[IWL_LED_TRG_TX], | |
5cbbb376 SW |
374 | IWL_LED_TRG_TX, 0, trigger); |
375 | ||
ab53d8af MA |
376 | priv->led[IWL_LED_TRG_TX].led_on = iwl3945_led_associated; |
377 | priv->led[IWL_LED_TRG_TX].led_off = iwl3945_led_associated; | |
378 | priv->led[IWL_LED_TRG_TX].led_pattern = iwl3945_led_pattern; | |
379 | ||
380 | if (ret) | |
381 | goto exit_fail; | |
382 | ||
383 | return 0; | |
384 | ||
385 | exit_fail: | |
386 | iwl3945_led_unregister(priv); | |
387 | return ret; | |
388 | } | |
389 | ||
390 | ||
391 | /* unregister led class */ | |
392 | static void iwl3945_led_unregister_led(struct iwl3945_led *led, u8 set_led) | |
393 | { | |
394 | if (!led->registered) | |
395 | return; | |
396 | ||
397 | led_classdev_unregister(&led->led_dev); | |
398 | ||
399 | if (set_led) | |
400 | led->led_dev.brightness_set(&led->led_dev, LED_OFF); | |
401 | led->registered = 0; | |
402 | } | |
403 | ||
404 | /* Unregister all led handlers */ | |
405 | void iwl3945_led_unregister(struct iwl3945_priv *priv) | |
406 | { | |
407 | iwl3945_led_unregister_led(&priv->led[IWL_LED_TRG_ASSOC], 0); | |
408 | iwl3945_led_unregister_led(&priv->led[IWL_LED_TRG_RX], 0); | |
409 | iwl3945_led_unregister_led(&priv->led[IWL_LED_TRG_TX], 0); | |
410 | iwl3945_led_unregister_led(&priv->led[IWL_LED_TRG_RADIO], 1); | |
411 | } | |
412 |