]> err.no Git - linux-2.6/blob - drivers/net/wireless/rt2x00/rt2x00leds.c
rt2x00: Fix quality/activity led handling
[linux-2.6] / drivers / net / wireless / rt2x00 / rt2x00leds.c
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 {
34         struct rt2x00_led *led = &rt2x00dev->led_qual;
35         unsigned int brightness;
36
37         if ((led->type != LED_TYPE_QUALITY) || !(led->flags & LED_REGISTERED))
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          */
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         }
73 }
74
75 void rt2x00led_led_activity(struct rt2x00_dev *rt2x00dev, bool enabled)
76 {
77         struct rt2x00_led *led = &rt2x00dev->led_qual;
78         unsigned int brightness;
79
80         if ((led->type != LED_TYPE_ACTIVITY) || !(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;
87         }
88 }
89
90 void rt2x00leds_led_assoc(struct rt2x00_dev *rt2x00dev, bool enabled)
91 {
92         struct rt2x00_led *led = &rt2x00dev->led_assoc;
93         unsigned int brightness;
94
95         if ((led->type != LED_TYPE_ASSOC) || !(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         }
103 }
104
105 void rt2x00leds_led_radio(struct rt2x00_dev *rt2x00dev, bool enabled)
106 {
107         struct rt2x00_led *led = &rt2x00dev->led_radio;
108         unsigned int brightness;
109
110         if ((led->type != LED_TYPE_RADIO) || !(led->flags & LED_REGISTERED))
111                 return;
112
113         brightness = enabled ? LED_FULL : LED_OFF;
114         if (brightness != led->led_dev.brightness) {
115                 led->led_dev.brightness_set(&led->led_dev, brightness);
116                 led->led_dev.brightness = brightness;
117         }
118 }
119
120 static int rt2x00leds_register_led(struct rt2x00_dev *rt2x00dev,
121                                    struct rt2x00_led *led,
122                                    const char *name)
123 {
124         struct device *device = wiphy_dev(rt2x00dev->hw->wiphy);
125         int retval;
126
127         led->led_dev.name = name;
128
129         retval = led_classdev_register(device, &led->led_dev);
130         if (retval) {
131                 ERROR(rt2x00dev, "Failed to register led handler.\n");
132                 return retval;
133         }
134
135         led->flags |= LED_REGISTERED;
136
137         return 0;
138 }
139
140 void rt2x00leds_register(struct rt2x00_dev *rt2x00dev)
141 {
142         char dev_name[16];
143         char name[32];
144         int retval;
145         unsigned long on_period;
146         unsigned long off_period;
147
148         snprintf(dev_name, sizeof(dev_name), "%s-%s",
149                  rt2x00dev->ops->name, wiphy_name(rt2x00dev->hw->wiphy));
150
151         if (rt2x00dev->led_radio.flags & LED_INITIALIZED) {
152                 snprintf(name, sizeof(name), "%s:radio", dev_name);
153
154                 retval = rt2x00leds_register_led(rt2x00dev,
155                                                  &rt2x00dev->led_radio,
156                                                  name);
157                 if (retval)
158                         goto exit_fail;
159         }
160
161         if (rt2x00dev->led_assoc.flags & LED_INITIALIZED) {
162                 snprintf(name, sizeof(name), "%s:assoc", dev_name);
163
164                 retval = rt2x00leds_register_led(rt2x00dev,
165                                                  &rt2x00dev->led_assoc,
166                                                  name);
167                 if (retval)
168                         goto exit_fail;
169         }
170
171         if (rt2x00dev->led_qual.flags & LED_INITIALIZED) {
172                 snprintf(name, sizeof(name), "%s:quality", dev_name);
173
174                 retval = rt2x00leds_register_led(rt2x00dev,
175                                                  &rt2x00dev->led_qual,
176                                                  name);
177                 if (retval)
178                         goto exit_fail;
179         }
180
181         /*
182          * Initialize blink time to default value:
183          * On period: 70ms
184          * Off period: 30ms
185          */
186         if (rt2x00dev->led_radio.led_dev.blink_set) {
187                 on_period = 70;
188                 off_period = 30;
189                 rt2x00dev->led_radio.led_dev.blink_set(
190                     &rt2x00dev->led_radio.led_dev, &on_period, &off_period);
191         }
192
193         return;
194
195 exit_fail:
196         rt2x00leds_unregister(rt2x00dev);
197 }
198
199 static void rt2x00leds_unregister_led(struct rt2x00_led *led)
200 {
201         led_classdev_unregister(&led->led_dev);
202         led->led_dev.brightness_set(&led->led_dev, LED_OFF);
203         led->flags &= ~LED_REGISTERED;
204 }
205
206 void rt2x00leds_unregister(struct rt2x00_dev *rt2x00dev)
207 {
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);
214 }
215
216 void rt2x00leds_suspend(struct rt2x00_dev *rt2x00dev)
217 {
218         if (rt2x00dev->led_qual.flags & LED_REGISTERED)
219                 led_classdev_suspend(&rt2x00dev->led_qual.led_dev);
220         if (rt2x00dev->led_assoc.flags & LED_REGISTERED)
221                 led_classdev_suspend(&rt2x00dev->led_assoc.led_dev);
222         if (rt2x00dev->led_radio.flags & LED_REGISTERED)
223                 led_classdev_suspend(&rt2x00dev->led_radio.led_dev);
224 }
225
226 void rt2x00leds_resume(struct rt2x00_dev *rt2x00dev)
227 {
228         if (rt2x00dev->led_radio.flags & LED_REGISTERED)
229                 led_classdev_resume(&rt2x00dev->led_radio.led_dev);
230         if (rt2x00dev->led_assoc.flags & LED_REGISTERED)
231                 led_classdev_resume(&rt2x00dev->led_assoc.led_dev);
232         if (rt2x00dev->led_qual.flags & LED_REGISTERED)
233                 led_classdev_resume(&rt2x00dev->led_qual.led_dev);
234 }