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