Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 1 | /* |
| 2 | Copyright (C) 2004 - 2008 rt2x00 SourceForge Project |
| 3 | <http://rt2x00.serialmonkey.com> |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation; either version 2 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with this program; if not, write to the |
| 17 | Free Software Foundation, Inc., |
| 18 | 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 19 | */ |
| 20 | |
| 21 | /* |
| 22 | Module: rt2x00lib |
| 23 | Abstract: rt2x00 led specific routines. |
| 24 | */ |
| 25 | |
| 26 | #include <linux/kernel.h> |
| 27 | #include <linux/module.h> |
| 28 | |
| 29 | #include "rt2x00.h" |
| 30 | #include "rt2x00lib.h" |
| 31 | |
| 32 | void rt2x00leds_led_quality(struct rt2x00_dev *rt2x00dev, int rssi) |
| 33 | { |
Ivo van Doorn | a2e1d52 | 2008-03-31 15:53:44 +0200 | [diff] [blame] | 34 | struct rt2x00_led *led = &rt2x00dev->led_qual; |
| 35 | unsigned int brightness; |
| 36 | |
| 37 | if ((led->type != LED_TYPE_QUALITY) || !(led->flags & LED_REGISTERED)) |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 38 | return; |
| 39 | |
| 40 | /* |
| 41 | * Led handling requires a positive value for the rssi, |
| 42 | * to do that correctly we need to add the correction. |
| 43 | */ |
| 44 | rssi += rt2x00dev->rssi_offset; |
| 45 | |
| 46 | /* |
| 47 | * Get the rssi level, this is used to convert the rssi |
| 48 | * to a LED value inside the range LED_OFF - LED_FULL. |
| 49 | */ |
| 50 | if (rssi <= 30) |
| 51 | rssi = 0; |
| 52 | else if (rssi <= 39) |
| 53 | rssi = 1; |
| 54 | else if (rssi <= 49) |
| 55 | rssi = 2; |
| 56 | else if (rssi <= 53) |
| 57 | rssi = 3; |
| 58 | else if (rssi <= 63) |
| 59 | rssi = 4; |
| 60 | else |
| 61 | rssi = 5; |
| 62 | |
| 63 | /* |
| 64 | * Note that we must _not_ send LED_OFF since the driver |
| 65 | * is going to calculate the value and might use it in a |
| 66 | * division. |
| 67 | */ |
Ivo van Doorn | a2e1d52 | 2008-03-31 15:53:44 +0200 | [diff] [blame] | 68 | brightness = ((LED_FULL / 6) * rssi) + 1; |
| 69 | if (brightness != led->led_dev.brightness) { |
| 70 | led->led_dev.brightness_set(&led->led_dev, brightness); |
| 71 | led->led_dev.brightness = brightness; |
| 72 | } |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 73 | } |
| 74 | |
Ivo van Doorn | 9a95dba | 2008-11-02 00:37:09 +0100 | [diff] [blame^] | 75 | static void rt2x00led_led_simple(struct rt2x00_led *led, bool enabled) |
Ivo van Doorn | 61c2b68 | 2008-04-21 19:01:09 +0200 | [diff] [blame] | 76 | { |
Ivo van Doorn | 9a95dba | 2008-11-02 00:37:09 +0100 | [diff] [blame^] | 77 | unsigned int brightness = enabled ? LED_FULL : LED_OFF; |
Ivo van Doorn | 61c2b68 | 2008-04-21 19:01:09 +0200 | [diff] [blame] | 78 | |
Ivo van Doorn | 9a95dba | 2008-11-02 00:37:09 +0100 | [diff] [blame^] | 79 | if (!(led->flags & LED_REGISTERED)) |
Ivo van Doorn | 61c2b68 | 2008-04-21 19:01:09 +0200 | [diff] [blame] | 80 | return; |
| 81 | |
Ivo van Doorn | 9a95dba | 2008-11-02 00:37:09 +0100 | [diff] [blame^] | 82 | led->led_dev.brightness_set(&led->led_dev, brightness); |
| 83 | led->led_dev.brightness = brightness; |
| 84 | } |
| 85 | |
| 86 | void rt2x00led_led_activity(struct rt2x00_dev *rt2x00dev, bool enabled) |
| 87 | { |
| 88 | if (rt2x00dev->led_qual.type == LED_TYPE_ACTIVITY) |
| 89 | rt2x00led_led_simple(&rt2x00dev->led_qual, enabled); |
Ivo van Doorn | 61c2b68 | 2008-04-21 19:01:09 +0200 | [diff] [blame] | 90 | } |
| 91 | |
Ivo van Doorn | a2e1d52 | 2008-03-31 15:53:44 +0200 | [diff] [blame] | 92 | void rt2x00leds_led_assoc(struct rt2x00_dev *rt2x00dev, bool enabled) |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 93 | { |
Ivo van Doorn | 9a95dba | 2008-11-02 00:37:09 +0100 | [diff] [blame^] | 94 | if (rt2x00dev->led_assoc.type == LED_TYPE_ASSOC) |
| 95 | rt2x00led_led_simple(&rt2x00dev->led_assoc, enabled); |
Ivo van Doorn | a2e1d52 | 2008-03-31 15:53:44 +0200 | [diff] [blame] | 96 | } |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 97 | |
Ivo van Doorn | a2e1d52 | 2008-03-31 15:53:44 +0200 | [diff] [blame] | 98 | void rt2x00leds_led_radio(struct rt2x00_dev *rt2x00dev, bool enabled) |
| 99 | { |
Ivo van Doorn | 9a95dba | 2008-11-02 00:37:09 +0100 | [diff] [blame^] | 100 | if (rt2x00dev->led_radio.type == LED_TYPE_ASSOC) |
| 101 | rt2x00led_led_simple(&rt2x00dev->led_radio, enabled); |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | static int rt2x00leds_register_led(struct rt2x00_dev *rt2x00dev, |
| 105 | struct rt2x00_led *led, |
Ivo van Doorn | a2e1d52 | 2008-03-31 15:53:44 +0200 | [diff] [blame] | 106 | const char *name) |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 107 | { |
| 108 | struct device *device = wiphy_dev(rt2x00dev->hw->wiphy); |
| 109 | int retval; |
| 110 | |
| 111 | led->led_dev.name = name; |
Ivo van Doorn | 9a95dba | 2008-11-02 00:37:09 +0100 | [diff] [blame^] | 112 | led->led_dev.brightness = LED_OFF; |
| 113 | |
| 114 | /* |
| 115 | * Ensure the LED is off, it might have been enabled |
| 116 | * by the hardware when the device was powered on. |
| 117 | */ |
| 118 | led->led_dev.brightness_set(&led->led_dev, LED_OFF); |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 119 | |
| 120 | retval = led_classdev_register(device, &led->led_dev); |
| 121 | if (retval) { |
| 122 | ERROR(rt2x00dev, "Failed to register led handler.\n"); |
| 123 | return retval; |
| 124 | } |
| 125 | |
Ivo van Doorn | a2e1d52 | 2008-03-31 15:53:44 +0200 | [diff] [blame] | 126 | led->flags |= LED_REGISTERED; |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
Ivo van Doorn | 72fa559 | 2008-03-09 22:47:08 +0100 | [diff] [blame] | 131 | void rt2x00leds_register(struct rt2x00_dev *rt2x00dev) |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 132 | { |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 133 | char dev_name[16]; |
| 134 | char name[32]; |
| 135 | int retval; |
Ivo van Doorn | a2e1d52 | 2008-03-31 15:53:44 +0200 | [diff] [blame] | 136 | unsigned long on_period; |
| 137 | unsigned long off_period; |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 138 | |
| 139 | snprintf(dev_name, sizeof(dev_name), "%s-%s", |
| 140 | rt2x00dev->ops->name, wiphy_name(rt2x00dev->hw->wiphy)); |
| 141 | |
Ivo van Doorn | a2e1d52 | 2008-03-31 15:53:44 +0200 | [diff] [blame] | 142 | if (rt2x00dev->led_radio.flags & LED_INITIALIZED) { |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 143 | snprintf(name, sizeof(name), "%s:radio", dev_name); |
| 144 | |
| 145 | retval = rt2x00leds_register_led(rt2x00dev, |
| 146 | &rt2x00dev->led_radio, |
Ivo van Doorn | a2e1d52 | 2008-03-31 15:53:44 +0200 | [diff] [blame] | 147 | name); |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 148 | if (retval) |
| 149 | goto exit_fail; |
| 150 | } |
| 151 | |
Ivo van Doorn | a2e1d52 | 2008-03-31 15:53:44 +0200 | [diff] [blame] | 152 | if (rt2x00dev->led_assoc.flags & LED_INITIALIZED) { |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 153 | snprintf(name, sizeof(name), "%s:assoc", dev_name); |
| 154 | |
| 155 | retval = rt2x00leds_register_led(rt2x00dev, |
| 156 | &rt2x00dev->led_assoc, |
Ivo van Doorn | a2e1d52 | 2008-03-31 15:53:44 +0200 | [diff] [blame] | 157 | name); |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 158 | if (retval) |
| 159 | goto exit_fail; |
| 160 | } |
| 161 | |
Ivo van Doorn | a2e1d52 | 2008-03-31 15:53:44 +0200 | [diff] [blame] | 162 | if (rt2x00dev->led_qual.flags & LED_INITIALIZED) { |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 163 | snprintf(name, sizeof(name), "%s:quality", dev_name); |
| 164 | |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 165 | retval = rt2x00leds_register_led(rt2x00dev, |
| 166 | &rt2x00dev->led_qual, |
Ivo van Doorn | a2e1d52 | 2008-03-31 15:53:44 +0200 | [diff] [blame] | 167 | name); |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 168 | if (retval) |
| 169 | goto exit_fail; |
| 170 | } |
| 171 | |
Ivo van Doorn | a2e1d52 | 2008-03-31 15:53:44 +0200 | [diff] [blame] | 172 | /* |
| 173 | * Initialize blink time to default value: |
| 174 | * On period: 70ms |
| 175 | * Off period: 30ms |
| 176 | */ |
| 177 | if (rt2x00dev->led_radio.led_dev.blink_set) { |
| 178 | on_period = 70; |
| 179 | off_period = 30; |
| 180 | rt2x00dev->led_radio.led_dev.blink_set( |
| 181 | &rt2x00dev->led_radio.led_dev, &on_period, &off_period); |
| 182 | } |
| 183 | |
Ivo van Doorn | 72fa559 | 2008-03-09 22:47:08 +0100 | [diff] [blame] | 184 | return; |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 185 | |
| 186 | exit_fail: |
| 187 | rt2x00leds_unregister(rt2x00dev); |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 188 | } |
| 189 | |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 190 | static void rt2x00leds_unregister_led(struct rt2x00_led *led) |
| 191 | { |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 192 | led_classdev_unregister(&led->led_dev); |
Ivo van Doorn | 9a95dba | 2008-11-02 00:37:09 +0100 | [diff] [blame^] | 193 | |
| 194 | /* |
| 195 | * This might look weird, but when we are unregistering while |
| 196 | * suspended the led is already off, and since we haven't |
| 197 | * fully resumed yet, access to the device might not be |
| 198 | * possible yet. |
| 199 | */ |
| 200 | if (!(led->led_dev.flags & LED_SUSPENDED)) |
| 201 | led->led_dev.brightness_set(&led->led_dev, LED_OFF); |
| 202 | |
Ivo van Doorn | a2e1d52 | 2008-03-31 15:53:44 +0200 | [diff] [blame] | 203 | led->flags &= ~LED_REGISTERED; |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | void rt2x00leds_unregister(struct rt2x00_dev *rt2x00dev) |
| 207 | { |
Ivo van Doorn | a2e1d52 | 2008-03-31 15:53:44 +0200 | [diff] [blame] | 208 | if (rt2x00dev->led_qual.flags & LED_REGISTERED) |
| 209 | rt2x00leds_unregister_led(&rt2x00dev->led_qual); |
| 210 | if (rt2x00dev->led_assoc.flags & LED_REGISTERED) |
| 211 | rt2x00leds_unregister_led(&rt2x00dev->led_assoc); |
| 212 | if (rt2x00dev->led_radio.flags & LED_REGISTERED) |
| 213 | rt2x00leds_unregister_led(&rt2x00dev->led_radio); |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 214 | } |
| 215 | |
Ivo van Doorn | 9a95dba | 2008-11-02 00:37:09 +0100 | [diff] [blame^] | 216 | static inline void rt2x00leds_suspend_led(struct rt2x00_led *led) |
| 217 | { |
| 218 | led_classdev_suspend(&led->led_dev); |
| 219 | |
| 220 | /* This shouldn't be needed, but just to be safe */ |
| 221 | led->led_dev.brightness_set(&led->led_dev, LED_OFF); |
| 222 | led->led_dev.brightness = LED_OFF; |
| 223 | } |
| 224 | |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 225 | void rt2x00leds_suspend(struct rt2x00_dev *rt2x00dev) |
| 226 | { |
Ivo van Doorn | a2e1d52 | 2008-03-31 15:53:44 +0200 | [diff] [blame] | 227 | if (rt2x00dev->led_qual.flags & LED_REGISTERED) |
Ivo van Doorn | 9a95dba | 2008-11-02 00:37:09 +0100 | [diff] [blame^] | 228 | rt2x00leds_suspend_led(&rt2x00dev->led_qual); |
Ivo van Doorn | a2e1d52 | 2008-03-31 15:53:44 +0200 | [diff] [blame] | 229 | if (rt2x00dev->led_assoc.flags & LED_REGISTERED) |
Ivo van Doorn | 9a95dba | 2008-11-02 00:37:09 +0100 | [diff] [blame^] | 230 | rt2x00leds_suspend_led(&rt2x00dev->led_assoc); |
Ivo van Doorn | a2e1d52 | 2008-03-31 15:53:44 +0200 | [diff] [blame] | 231 | if (rt2x00dev->led_radio.flags & LED_REGISTERED) |
Ivo van Doorn | 9a95dba | 2008-11-02 00:37:09 +0100 | [diff] [blame^] | 232 | rt2x00leds_suspend_led(&rt2x00dev->led_radio); |
| 233 | } |
| 234 | |
| 235 | static inline void rt2x00leds_resume_led(struct rt2x00_led *led) |
| 236 | { |
| 237 | led_classdev_resume(&led->led_dev); |
| 238 | |
| 239 | /* Device might have enabled the LEDS during resume */ |
| 240 | led->led_dev.brightness_set(&led->led_dev, LED_OFF); |
| 241 | led->led_dev.brightness = LED_OFF; |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | void rt2x00leds_resume(struct rt2x00_dev *rt2x00dev) |
| 245 | { |
Ivo van Doorn | a2e1d52 | 2008-03-31 15:53:44 +0200 | [diff] [blame] | 246 | if (rt2x00dev->led_radio.flags & LED_REGISTERED) |
Ivo van Doorn | 9a95dba | 2008-11-02 00:37:09 +0100 | [diff] [blame^] | 247 | rt2x00leds_resume_led(&rt2x00dev->led_radio); |
Ivo van Doorn | a2e1d52 | 2008-03-31 15:53:44 +0200 | [diff] [blame] | 248 | if (rt2x00dev->led_assoc.flags & LED_REGISTERED) |
Ivo van Doorn | 9a95dba | 2008-11-02 00:37:09 +0100 | [diff] [blame^] | 249 | rt2x00leds_resume_led(&rt2x00dev->led_assoc); |
Ivo van Doorn | a2e1d52 | 2008-03-31 15:53:44 +0200 | [diff] [blame] | 250 | if (rt2x00dev->led_qual.flags & LED_REGISTERED) |
Ivo van Doorn | 9a95dba | 2008-11-02 00:37:09 +0100 | [diff] [blame^] | 251 | rt2x00leds_resume_led(&rt2x00dev->led_qual); |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 252 | } |