Loading...
1/*
2 Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com>
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
32void 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
75static void rt2x00led_led_simple(struct rt2x00_led *led, bool enabled)
76{
77 unsigned int brightness = enabled ? LED_FULL : LED_OFF;
78
79 if (!(led->flags & LED_REGISTERED))
80 return;
81
82 led->led_dev.brightness_set(&led->led_dev, brightness);
83 led->led_dev.brightness = brightness;
84}
85
86void 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);
90}
91
92void rt2x00leds_led_assoc(struct rt2x00_dev *rt2x00dev, bool enabled)
93{
94 if (rt2x00dev->led_assoc.type == LED_TYPE_ASSOC)
95 rt2x00led_led_simple(&rt2x00dev->led_assoc, enabled);
96}
97
98void rt2x00leds_led_radio(struct rt2x00_dev *rt2x00dev, bool enabled)
99{
100 if (rt2x00dev->led_radio.type == LED_TYPE_RADIO)
101 rt2x00led_led_simple(&rt2x00dev->led_radio, enabled);
102}
103
104static int rt2x00leds_register_led(struct rt2x00_dev *rt2x00dev,
105 struct rt2x00_led *led,
106 const char *name)
107{
108 struct device *device = wiphy_dev(rt2x00dev->hw->wiphy);
109 int retval;
110
111 led->led_dev.name = name;
112 led->led_dev.brightness = LED_OFF;
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
120 led->flags |= LED_REGISTERED;
121
122 return 0;
123}
124
125void rt2x00leds_register(struct rt2x00_dev *rt2x00dev)
126{
127 char dev_name[16];
128 char name[32];
129 int retval;
130 unsigned long on_period;
131 unsigned long off_period;
132
133 snprintf(dev_name, sizeof(dev_name), "%s-%s",
134 rt2x00dev->ops->name, wiphy_name(rt2x00dev->hw->wiphy));
135
136 if (rt2x00dev->led_radio.flags & LED_INITIALIZED) {
137 snprintf(name, sizeof(name), "%s::radio", dev_name);
138
139 retval = rt2x00leds_register_led(rt2x00dev,
140 &rt2x00dev->led_radio,
141 name);
142 if (retval)
143 goto exit_fail;
144 }
145
146 if (rt2x00dev->led_assoc.flags & LED_INITIALIZED) {
147 snprintf(name, sizeof(name), "%s::assoc", dev_name);
148
149 retval = rt2x00leds_register_led(rt2x00dev,
150 &rt2x00dev->led_assoc,
151 name);
152 if (retval)
153 goto exit_fail;
154 }
155
156 if (rt2x00dev->led_qual.flags & LED_INITIALIZED) {
157 snprintf(name, sizeof(name), "%s::quality", dev_name);
158
159 retval = rt2x00leds_register_led(rt2x00dev,
160 &rt2x00dev->led_qual,
161 name);
162 if (retval)
163 goto exit_fail;
164 }
165
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
178 return;
179
180exit_fail:
181 rt2x00leds_unregister(rt2x00dev);
182}
183
184static void rt2x00leds_unregister_led(struct rt2x00_led *led)
185{
186 led_classdev_unregister(&led->led_dev);
187
188 /*
189 * This might look weird, but when we are unregistering while
190 * suspended the led is already off, and since we haven't
191 * fully resumed yet, access to the device might not be
192 * possible yet.
193 */
194 if (!(led->led_dev.flags & LED_SUSPENDED))
195 led->led_dev.brightness_set(&led->led_dev, LED_OFF);
196
197 led->flags &= ~LED_REGISTERED;
198}
199
200void rt2x00leds_unregister(struct rt2x00_dev *rt2x00dev)
201{
202 if (rt2x00dev->led_qual.flags & LED_REGISTERED)
203 rt2x00leds_unregister_led(&rt2x00dev->led_qual);
204 if (rt2x00dev->led_assoc.flags & LED_REGISTERED)
205 rt2x00leds_unregister_led(&rt2x00dev->led_assoc);
206 if (rt2x00dev->led_radio.flags & LED_REGISTERED)
207 rt2x00leds_unregister_led(&rt2x00dev->led_radio);
208}
209
210static inline void rt2x00leds_suspend_led(struct rt2x00_led *led)
211{
212 led_classdev_suspend(&led->led_dev);
213
214 /* This shouldn't be needed, but just to be safe */
215 led->led_dev.brightness_set(&led->led_dev, LED_OFF);
216 led->led_dev.brightness = LED_OFF;
217}
218
219void rt2x00leds_suspend(struct rt2x00_dev *rt2x00dev)
220{
221 if (rt2x00dev->led_qual.flags & LED_REGISTERED)
222 rt2x00leds_suspend_led(&rt2x00dev->led_qual);
223 if (rt2x00dev->led_assoc.flags & LED_REGISTERED)
224 rt2x00leds_suspend_led(&rt2x00dev->led_assoc);
225 if (rt2x00dev->led_radio.flags & LED_REGISTERED)
226 rt2x00leds_suspend_led(&rt2x00dev->led_radio);
227}
228
229static inline void rt2x00leds_resume_led(struct rt2x00_led *led)
230{
231 led_classdev_resume(&led->led_dev);
232
233 /* Device might have enabled the LEDS during resume */
234 led->led_dev.brightness_set(&led->led_dev, LED_OFF);
235 led->led_dev.brightness = LED_OFF;
236}
237
238void rt2x00leds_resume(struct rt2x00_dev *rt2x00dev)
239{
240 if (rt2x00dev->led_radio.flags & LED_REGISTERED)
241 rt2x00leds_resume_led(&rt2x00dev->led_radio);
242 if (rt2x00dev->led_assoc.flags & LED_REGISTERED)
243 rt2x00leds_resume_led(&rt2x00dev->led_assoc);
244 if (rt2x00dev->led_qual.flags & LED_REGISTERED)
245 rt2x00leds_resume_led(&rt2x00dev->led_qual);
246}
1/*
2 Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com>
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, see <http://www.gnu.org/licenses/>.
17 */
18
19/*
20 Module: rt2x00lib
21 Abstract: rt2x00 led specific routines.
22 */
23
24#include <linux/kernel.h>
25#include <linux/module.h>
26
27#include "rt2x00.h"
28#include "rt2x00lib.h"
29
30void rt2x00leds_led_quality(struct rt2x00_dev *rt2x00dev, int rssi)
31{
32 struct rt2x00_led *led = &rt2x00dev->led_qual;
33 unsigned int brightness;
34
35 if ((led->type != LED_TYPE_QUALITY) || !(led->flags & LED_REGISTERED))
36 return;
37
38 /*
39 * Led handling requires a positive value for the rssi,
40 * to do that correctly we need to add the correction.
41 */
42 rssi += rt2x00dev->rssi_offset;
43
44 /*
45 * Get the rssi level, this is used to convert the rssi
46 * to a LED value inside the range LED_OFF - LED_FULL.
47 */
48 if (rssi <= 30)
49 rssi = 0;
50 else if (rssi <= 39)
51 rssi = 1;
52 else if (rssi <= 49)
53 rssi = 2;
54 else if (rssi <= 53)
55 rssi = 3;
56 else if (rssi <= 63)
57 rssi = 4;
58 else
59 rssi = 5;
60
61 /*
62 * Note that we must _not_ send LED_OFF since the driver
63 * is going to calculate the value and might use it in a
64 * division.
65 */
66 brightness = ((LED_FULL / 6) * rssi) + 1;
67 if (brightness != led->led_dev.brightness) {
68 led->led_dev.brightness_set(&led->led_dev, brightness);
69 led->led_dev.brightness = brightness;
70 }
71}
72
73static void rt2x00led_led_simple(struct rt2x00_led *led, bool enabled)
74{
75 unsigned int brightness = enabled ? LED_FULL : LED_OFF;
76
77 if (!(led->flags & LED_REGISTERED))
78 return;
79
80 led->led_dev.brightness_set(&led->led_dev, brightness);
81 led->led_dev.brightness = brightness;
82}
83
84void rt2x00led_led_activity(struct rt2x00_dev *rt2x00dev, bool enabled)
85{
86 if (rt2x00dev->led_qual.type == LED_TYPE_ACTIVITY)
87 rt2x00led_led_simple(&rt2x00dev->led_qual, enabled);
88}
89
90void rt2x00leds_led_assoc(struct rt2x00_dev *rt2x00dev, bool enabled)
91{
92 if (rt2x00dev->led_assoc.type == LED_TYPE_ASSOC)
93 rt2x00led_led_simple(&rt2x00dev->led_assoc, enabled);
94}
95
96void rt2x00leds_led_radio(struct rt2x00_dev *rt2x00dev, bool enabled)
97{
98 if (rt2x00dev->led_radio.type == LED_TYPE_RADIO)
99 rt2x00led_led_simple(&rt2x00dev->led_radio, enabled);
100}
101
102static int rt2x00leds_register_led(struct rt2x00_dev *rt2x00dev,
103 struct rt2x00_led *led,
104 const char *name)
105{
106 struct device *device = wiphy_dev(rt2x00dev->hw->wiphy);
107 int retval;
108
109 led->led_dev.name = name;
110 led->led_dev.brightness = LED_OFF;
111
112 retval = led_classdev_register(device, &led->led_dev);
113 if (retval) {
114 rt2x00_err(rt2x00dev, "Failed to register led handler\n");
115 return retval;
116 }
117
118 led->flags |= LED_REGISTERED;
119
120 return 0;
121}
122
123void rt2x00leds_register(struct rt2x00_dev *rt2x00dev)
124{
125 char name[36];
126 int retval;
127 unsigned long on_period;
128 unsigned long off_period;
129 const char *phy_name = wiphy_name(rt2x00dev->hw->wiphy);
130
131 if (rt2x00dev->led_radio.flags & LED_INITIALIZED) {
132 snprintf(name, sizeof(name), "%s-%s::radio",
133 rt2x00dev->ops->name, phy_name);
134
135 retval = rt2x00leds_register_led(rt2x00dev,
136 &rt2x00dev->led_radio,
137 name);
138 if (retval)
139 goto exit_fail;
140 }
141
142 if (rt2x00dev->led_assoc.flags & LED_INITIALIZED) {
143 snprintf(name, sizeof(name), "%s-%s::assoc",
144 rt2x00dev->ops->name, phy_name);
145
146 retval = rt2x00leds_register_led(rt2x00dev,
147 &rt2x00dev->led_assoc,
148 name);
149 if (retval)
150 goto exit_fail;
151 }
152
153 if (rt2x00dev->led_qual.flags & LED_INITIALIZED) {
154 snprintf(name, sizeof(name), "%s-%s::quality",
155 rt2x00dev->ops->name, phy_name);
156
157 retval = rt2x00leds_register_led(rt2x00dev,
158 &rt2x00dev->led_qual,
159 name);
160 if (retval)
161 goto exit_fail;
162 }
163
164 /*
165 * Initialize blink time to default value:
166 * On period: 70ms
167 * Off period: 30ms
168 */
169 if (rt2x00dev->led_radio.led_dev.blink_set) {
170 on_period = 70;
171 off_period = 30;
172 rt2x00dev->led_radio.led_dev.blink_set(
173 &rt2x00dev->led_radio.led_dev, &on_period, &off_period);
174 }
175
176 return;
177
178exit_fail:
179 rt2x00leds_unregister(rt2x00dev);
180}
181
182static void rt2x00leds_unregister_led(struct rt2x00_led *led)
183{
184 led_classdev_unregister(&led->led_dev);
185
186 /*
187 * This might look weird, but when we are unregistering while
188 * suspended the led is already off, and since we haven't
189 * fully resumed yet, access to the device might not be
190 * possible yet.
191 */
192 if (!(led->led_dev.flags & LED_SUSPENDED))
193 led->led_dev.brightness_set(&led->led_dev, LED_OFF);
194
195 led->flags &= ~LED_REGISTERED;
196}
197
198void rt2x00leds_unregister(struct rt2x00_dev *rt2x00dev)
199{
200 if (rt2x00dev->led_qual.flags & LED_REGISTERED)
201 rt2x00leds_unregister_led(&rt2x00dev->led_qual);
202 if (rt2x00dev->led_assoc.flags & LED_REGISTERED)
203 rt2x00leds_unregister_led(&rt2x00dev->led_assoc);
204 if (rt2x00dev->led_radio.flags & LED_REGISTERED)
205 rt2x00leds_unregister_led(&rt2x00dev->led_radio);
206}
207
208static inline void rt2x00leds_suspend_led(struct rt2x00_led *led)
209{
210 led_classdev_suspend(&led->led_dev);
211
212 /* This shouldn't be needed, but just to be safe */
213 led->led_dev.brightness_set(&led->led_dev, LED_OFF);
214 led->led_dev.brightness = LED_OFF;
215}
216
217void rt2x00leds_suspend(struct rt2x00_dev *rt2x00dev)
218{
219 if (rt2x00dev->led_qual.flags & LED_REGISTERED)
220 rt2x00leds_suspend_led(&rt2x00dev->led_qual);
221 if (rt2x00dev->led_assoc.flags & LED_REGISTERED)
222 rt2x00leds_suspend_led(&rt2x00dev->led_assoc);
223 if (rt2x00dev->led_radio.flags & LED_REGISTERED)
224 rt2x00leds_suspend_led(&rt2x00dev->led_radio);
225}
226
227static inline void rt2x00leds_resume_led(struct rt2x00_led *led)
228{
229 led_classdev_resume(&led->led_dev);
230
231 /* Device might have enabled the LEDS during resume */
232 led->led_dev.brightness_set(&led->led_dev, LED_OFF);
233 led->led_dev.brightness = LED_OFF;
234}
235
236void rt2x00leds_resume(struct rt2x00_dev *rt2x00dev)
237{
238 if (rt2x00dev->led_radio.flags & LED_REGISTERED)
239 rt2x00leds_resume_led(&rt2x00dev->led_radio);
240 if (rt2x00dev->led_assoc.flags & LED_REGISTERED)
241 rt2x00leds_resume_led(&rt2x00dev->led_assoc);
242 if (rt2x00dev->led_qual.flags & LED_REGISTERED)
243 rt2x00leds_resume_led(&rt2x00dev->led_qual);
244}