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 | a2e1d52 | 2008-03-31 15:53:44 +0200 | [diff] [blame^] | 75 | void rt2x00leds_led_assoc(struct rt2x00_dev *rt2x00dev, bool enabled) |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 76 | { |
Ivo van Doorn | a2e1d52 | 2008-03-31 15:53:44 +0200 | [diff] [blame^] | 77 | struct rt2x00_led *led = &rt2x00dev->led_assoc; |
| 78 | unsigned int brightness; |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 79 | |
Ivo van Doorn | a2e1d52 | 2008-03-31 15:53:44 +0200 | [diff] [blame^] | 80 | if ((led->type != LED_TYPE_ASSOC) || !(led->flags & LED_REGISTERED)) |
| 81 | return; |
| 82 | |
| 83 | brightness = enabled ? LED_FULL : LED_OFF; |
| 84 | if (brightness != led->led_dev.brightness) { |
| 85 | led->led_dev.brightness_set(&led->led_dev, brightness); |
| 86 | led->led_dev.brightness = brightness; |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 87 | } |
Ivo van Doorn | a2e1d52 | 2008-03-31 15:53:44 +0200 | [diff] [blame^] | 88 | } |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 89 | |
Ivo van Doorn | a2e1d52 | 2008-03-31 15:53:44 +0200 | [diff] [blame^] | 90 | void rt2x00leds_led_radio(struct rt2x00_dev *rt2x00dev, bool enabled) |
| 91 | { |
| 92 | struct rt2x00_led *led = &rt2x00dev->led_radio; |
| 93 | unsigned int brightness; |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 94 | |
Ivo van Doorn | a2e1d52 | 2008-03-31 15:53:44 +0200 | [diff] [blame^] | 95 | if ((led->type != LED_TYPE_RADIO) || !(led->flags & LED_REGISTERED)) |
| 96 | return; |
| 97 | |
| 98 | brightness = enabled ? LED_FULL : LED_OFF; |
| 99 | if (brightness != led->led_dev.brightness) { |
| 100 | led->led_dev.brightness_set(&led->led_dev, brightness); |
| 101 | led->led_dev.brightness = brightness; |
| 102 | } |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | static int rt2x00leds_register_led(struct rt2x00_dev *rt2x00dev, |
| 106 | struct rt2x00_led *led, |
Ivo van Doorn | a2e1d52 | 2008-03-31 15:53:44 +0200 | [diff] [blame^] | 107 | const char *name) |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 108 | { |
| 109 | struct device *device = wiphy_dev(rt2x00dev->hw->wiphy); |
| 110 | int retval; |
| 111 | |
| 112 | led->led_dev.name = name; |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 113 | |
| 114 | retval = led_classdev_register(device, &led->led_dev); |
| 115 | if (retval) { |
| 116 | ERROR(rt2x00dev, "Failed to register led handler.\n"); |
| 117 | return retval; |
| 118 | } |
| 119 | |
Ivo van Doorn | a2e1d52 | 2008-03-31 15:53:44 +0200 | [diff] [blame^] | 120 | led->flags |= LED_REGISTERED; |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
Ivo van Doorn | 72fa559 | 2008-03-09 22:47:08 +0100 | [diff] [blame] | 125 | void rt2x00leds_register(struct rt2x00_dev *rt2x00dev) |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 126 | { |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 127 | char dev_name[16]; |
| 128 | char name[32]; |
| 129 | int retval; |
Ivo van Doorn | a2e1d52 | 2008-03-31 15:53:44 +0200 | [diff] [blame^] | 130 | unsigned long on_period; |
| 131 | unsigned long off_period; |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 132 | |
| 133 | snprintf(dev_name, sizeof(dev_name), "%s-%s", |
| 134 | rt2x00dev->ops->name, wiphy_name(rt2x00dev->hw->wiphy)); |
| 135 | |
Ivo van Doorn | a2e1d52 | 2008-03-31 15:53:44 +0200 | [diff] [blame^] | 136 | if (rt2x00dev->led_radio.flags & LED_INITIALIZED) { |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 137 | snprintf(name, sizeof(name), "%s:radio", dev_name); |
| 138 | |
| 139 | retval = rt2x00leds_register_led(rt2x00dev, |
| 140 | &rt2x00dev->led_radio, |
Ivo van Doorn | a2e1d52 | 2008-03-31 15:53:44 +0200 | [diff] [blame^] | 141 | name); |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 142 | if (retval) |
| 143 | goto exit_fail; |
| 144 | } |
| 145 | |
Ivo van Doorn | a2e1d52 | 2008-03-31 15:53:44 +0200 | [diff] [blame^] | 146 | if (rt2x00dev->led_assoc.flags & LED_INITIALIZED) { |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 147 | snprintf(name, sizeof(name), "%s:assoc", dev_name); |
| 148 | |
| 149 | retval = rt2x00leds_register_led(rt2x00dev, |
| 150 | &rt2x00dev->led_assoc, |
Ivo van Doorn | a2e1d52 | 2008-03-31 15:53:44 +0200 | [diff] [blame^] | 151 | name); |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 152 | if (retval) |
| 153 | goto exit_fail; |
| 154 | } |
| 155 | |
Ivo van Doorn | a2e1d52 | 2008-03-31 15:53:44 +0200 | [diff] [blame^] | 156 | if (rt2x00dev->led_qual.flags & LED_INITIALIZED) { |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 157 | snprintf(name, sizeof(name), "%s:quality", dev_name); |
| 158 | |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 159 | retval = rt2x00leds_register_led(rt2x00dev, |
| 160 | &rt2x00dev->led_qual, |
Ivo van Doorn | a2e1d52 | 2008-03-31 15:53:44 +0200 | [diff] [blame^] | 161 | name); |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 162 | if (retval) |
| 163 | goto exit_fail; |
| 164 | } |
| 165 | |
Ivo van Doorn | a2e1d52 | 2008-03-31 15:53:44 +0200 | [diff] [blame^] | 166 | /* |
| 167 | * Initialize blink time to default value: |
| 168 | * On period: 70ms |
| 169 | * Off period: 30ms |
| 170 | */ |
| 171 | if (rt2x00dev->led_radio.led_dev.blink_set) { |
| 172 | on_period = 70; |
| 173 | off_period = 30; |
| 174 | rt2x00dev->led_radio.led_dev.blink_set( |
| 175 | &rt2x00dev->led_radio.led_dev, &on_period, &off_period); |
| 176 | } |
| 177 | |
Ivo van Doorn | 72fa559 | 2008-03-09 22:47:08 +0100 | [diff] [blame] | 178 | return; |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 179 | |
| 180 | exit_fail: |
| 181 | rt2x00leds_unregister(rt2x00dev); |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 182 | } |
| 183 | |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 184 | static void rt2x00leds_unregister_led(struct rt2x00_led *led) |
| 185 | { |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 186 | led_classdev_unregister(&led->led_dev); |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 187 | led->led_dev.brightness_set(&led->led_dev, LED_OFF); |
Ivo van Doorn | a2e1d52 | 2008-03-31 15:53:44 +0200 | [diff] [blame^] | 188 | led->flags &= ~LED_REGISTERED; |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | void rt2x00leds_unregister(struct rt2x00_dev *rt2x00dev) |
| 192 | { |
Ivo van Doorn | a2e1d52 | 2008-03-31 15:53:44 +0200 | [diff] [blame^] | 193 | if (rt2x00dev->led_qual.flags & LED_REGISTERED) |
| 194 | rt2x00leds_unregister_led(&rt2x00dev->led_qual); |
| 195 | if (rt2x00dev->led_assoc.flags & LED_REGISTERED) |
| 196 | rt2x00leds_unregister_led(&rt2x00dev->led_assoc); |
| 197 | if (rt2x00dev->led_radio.flags & LED_REGISTERED) |
| 198 | rt2x00leds_unregister_led(&rt2x00dev->led_radio); |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | void rt2x00leds_suspend(struct rt2x00_dev *rt2x00dev) |
| 202 | { |
Ivo van Doorn | a2e1d52 | 2008-03-31 15:53:44 +0200 | [diff] [blame^] | 203 | if (rt2x00dev->led_qual.flags & LED_REGISTERED) |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 204 | led_classdev_suspend(&rt2x00dev->led_qual.led_dev); |
Ivo van Doorn | a2e1d52 | 2008-03-31 15:53:44 +0200 | [diff] [blame^] | 205 | if (rt2x00dev->led_assoc.flags & LED_REGISTERED) |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 206 | led_classdev_suspend(&rt2x00dev->led_assoc.led_dev); |
Ivo van Doorn | a2e1d52 | 2008-03-31 15:53:44 +0200 | [diff] [blame^] | 207 | if (rt2x00dev->led_radio.flags & LED_REGISTERED) |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 208 | led_classdev_suspend(&rt2x00dev->led_radio.led_dev); |
| 209 | } |
| 210 | |
| 211 | void rt2x00leds_resume(struct rt2x00_dev *rt2x00dev) |
| 212 | { |
Ivo van Doorn | a2e1d52 | 2008-03-31 15:53:44 +0200 | [diff] [blame^] | 213 | if (rt2x00dev->led_radio.flags & LED_REGISTERED) |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 214 | led_classdev_resume(&rt2x00dev->led_radio.led_dev); |
Ivo van Doorn | a2e1d52 | 2008-03-31 15:53:44 +0200 | [diff] [blame^] | 215 | if (rt2x00dev->led_assoc.flags & LED_REGISTERED) |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 216 | led_classdev_resume(&rt2x00dev->led_assoc.led_dev); |
Ivo van Doorn | a2e1d52 | 2008-03-31 15:53:44 +0200 | [diff] [blame^] | 217 | if (rt2x00dev->led_qual.flags & LED_REGISTERED) |
Ivo van Doorn | 3b640f2 | 2008-02-03 15:54:11 +0100 | [diff] [blame] | 218 | led_classdev_resume(&rt2x00dev->led_qual.led_dev); |
| 219 | } |