rt2x00leds.c 6.4 KB
Newer Older
1
/*
2
	Copyright (C) 2004 - 2009 rt2x00 SourceForge Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
	<http://rt2x00.serialmonkey.com>

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the
	Free Software Foundation, Inc.,
	59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

/*
	Module: rt2x00lib
	Abstract: rt2x00 led specific routines.
 */

#include <linux/kernel.h>
#include <linux/module.h>

#include "rt2x00.h"
#include "rt2x00lib.h"

void rt2x00leds_led_quality(struct rt2x00_dev *rt2x00dev, int rssi)
{
34 35 36 37
	struct rt2x00_led *led = &rt2x00dev->led_qual;
	unsigned int brightness;

	if ((led->type != LED_TYPE_QUALITY) || !(led->flags & LED_REGISTERED))
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
		return;

	/*
	 * Led handling requires a positive value for the rssi,
	 * to do that correctly we need to add the correction.
	 */
	rssi += rt2x00dev->rssi_offset;

	/*
	 * Get the rssi level, this is used to convert the rssi
	 * to a LED value inside the range LED_OFF - LED_FULL.
	 */
	if (rssi <= 30)
		rssi = 0;
	else if (rssi <= 39)
		rssi = 1;
	else if (rssi <= 49)
		rssi = 2;
	else if (rssi <= 53)
		rssi = 3;
	else if (rssi <= 63)
		rssi = 4;
	else
		rssi = 5;

	/*
	 * Note that we must _not_ send LED_OFF since the driver
	 * is going to calculate the value and might use it in a
	 * division.
	 */
68 69 70 71 72
	brightness = ((LED_FULL / 6) * rssi) + 1;
	if (brightness != led->led_dev.brightness) {
		led->led_dev.brightness_set(&led->led_dev, brightness);
		led->led_dev.brightness = brightness;
	}
73 74
}

I
Ivo van Doorn 已提交
75
static void rt2x00led_led_simple(struct rt2x00_led *led, bool enabled)
76
{
I
Ivo van Doorn 已提交
77
	unsigned int brightness = enabled ? LED_FULL : LED_OFF;
78

I
Ivo van Doorn 已提交
79
	if (!(led->flags & LED_REGISTERED))
80 81
		return;

I
Ivo van Doorn 已提交
82 83
	led->led_dev.brightness_set(&led->led_dev, brightness);
	led->led_dev.brightness = brightness;
84 85
}

I
Ivo van Doorn 已提交
86
void rt2x00led_led_activity(struct rt2x00_dev *rt2x00dev, bool enabled)
87
{
I
Ivo van Doorn 已提交
88 89 90
	if (rt2x00dev->led_qual.type == LED_TYPE_ACTIVITY)
		rt2x00led_led_simple(&rt2x00dev->led_qual, enabled);
}
91

I
Ivo van Doorn 已提交
92 93 94 95
void rt2x00leds_led_assoc(struct rt2x00_dev *rt2x00dev, bool enabled)
{
	if (rt2x00dev->led_assoc.type == LED_TYPE_ASSOC)
		rt2x00led_led_simple(&rt2x00dev->led_assoc, enabled);
96
}
97

98 99
void rt2x00leds_led_radio(struct rt2x00_dev *rt2x00dev, bool enabled)
{
A
Andrew Price 已提交
100
	if (rt2x00dev->led_radio.type == LED_TYPE_RADIO)
I
Ivo van Doorn 已提交
101
		rt2x00led_led_simple(&rt2x00dev->led_radio, enabled);
102 103 104 105
}

static int rt2x00leds_register_led(struct rt2x00_dev *rt2x00dev,
				   struct rt2x00_led *led,
106
				   const char *name)
107 108 109 110 111
{
	struct device *device = wiphy_dev(rt2x00dev->hw->wiphy);
	int retval;

	led->led_dev.name = name;
I
Ivo van Doorn 已提交
112 113
	led->led_dev.brightness = LED_OFF;

114 115 116 117 118 119
	retval = led_classdev_register(device, &led->led_dev);
	if (retval) {
		ERROR(rt2x00dev, "Failed to register led handler.\n");
		return retval;
	}

120
	led->flags |= LED_REGISTERED;
121 122 123 124

	return 0;
}

125
void rt2x00leds_register(struct rt2x00_dev *rt2x00dev)
126 127 128 129
{
	char dev_name[16];
	char name[32];
	int retval;
130 131
	unsigned long on_period;
	unsigned long off_period;
132 133 134 135

	snprintf(dev_name, sizeof(dev_name), "%s-%s",
		 rt2x00dev->ops->name, wiphy_name(rt2x00dev->hw->wiphy));

136
	if (rt2x00dev->led_radio.flags & LED_INITIALIZED) {
D
Danny Kukawka 已提交
137
		snprintf(name, sizeof(name), "%s::radio", dev_name);
138 139 140

		retval = rt2x00leds_register_led(rt2x00dev,
						 &rt2x00dev->led_radio,
141
						 name);
142 143 144 145
		if (retval)
			goto exit_fail;
	}

146
	if (rt2x00dev->led_assoc.flags & LED_INITIALIZED) {
D
Danny Kukawka 已提交
147
		snprintf(name, sizeof(name), "%s::assoc", dev_name);
148 149 150

		retval = rt2x00leds_register_led(rt2x00dev,
						 &rt2x00dev->led_assoc,
151
						 name);
152 153 154 155
		if (retval)
			goto exit_fail;
	}

156
	if (rt2x00dev->led_qual.flags & LED_INITIALIZED) {
D
Danny Kukawka 已提交
157
		snprintf(name, sizeof(name), "%s::quality", dev_name);
158 159 160

		retval = rt2x00leds_register_led(rt2x00dev,
						 &rt2x00dev->led_qual,
161
						 name);
162 163 164 165
		if (retval)
			goto exit_fail;
	}

166 167 168 169 170 171 172 173 174 175 176 177
	/*
	 * Initialize blink time to default value:
	 * On period: 70ms
	 * Off period: 30ms
	 */
	if (rt2x00dev->led_radio.led_dev.blink_set) {
		on_period = 70;
		off_period = 30;
		rt2x00dev->led_radio.led_dev.blink_set(
		    &rt2x00dev->led_radio.led_dev, &on_period, &off_period);
	}

178
	return;
179 180 181 182 183 184 185 186

exit_fail:
	rt2x00leds_unregister(rt2x00dev);
}

static void rt2x00leds_unregister_led(struct rt2x00_led *led)
{
	led_classdev_unregister(&led->led_dev);
I
Ivo van Doorn 已提交
187 188 189 190 191 192 193 194 195 196

	/*
	 * This might look weird, but when we are unregistering while
	 * suspended the led is already off, and since we haven't
	 * fully resumed yet, access to the device might not be
	 * possible yet.
	 */
	if (!(led->led_dev.flags & LED_SUSPENDED))
		led->led_dev.brightness_set(&led->led_dev, LED_OFF);

197
	led->flags &= ~LED_REGISTERED;
198 199 200 201
}

void rt2x00leds_unregister(struct rt2x00_dev *rt2x00dev)
{
202 203 204 205 206 207
	if (rt2x00dev->led_qual.flags & LED_REGISTERED)
		rt2x00leds_unregister_led(&rt2x00dev->led_qual);
	if (rt2x00dev->led_assoc.flags & LED_REGISTERED)
		rt2x00leds_unregister_led(&rt2x00dev->led_assoc);
	if (rt2x00dev->led_radio.flags & LED_REGISTERED)
		rt2x00leds_unregister_led(&rt2x00dev->led_radio);
208 209
}

I
Ivo van Doorn 已提交
210 211 212 213 214 215 216 217 218
static inline void rt2x00leds_suspend_led(struct rt2x00_led *led)
{
	led_classdev_suspend(&led->led_dev);

	/* This shouldn't be needed, but just to be safe */
	led->led_dev.brightness_set(&led->led_dev, LED_OFF);
	led->led_dev.brightness = LED_OFF;
}

219 220
void rt2x00leds_suspend(struct rt2x00_dev *rt2x00dev)
{
221
	if (rt2x00dev->led_qual.flags & LED_REGISTERED)
I
Ivo van Doorn 已提交
222
		rt2x00leds_suspend_led(&rt2x00dev->led_qual);
223
	if (rt2x00dev->led_assoc.flags & LED_REGISTERED)
I
Ivo van Doorn 已提交
224
		rt2x00leds_suspend_led(&rt2x00dev->led_assoc);
225
	if (rt2x00dev->led_radio.flags & LED_REGISTERED)
I
Ivo van Doorn 已提交
226 227 228 229 230 231 232 233 234 235
		rt2x00leds_suspend_led(&rt2x00dev->led_radio);
}

static inline void rt2x00leds_resume_led(struct rt2x00_led *led)
{
	led_classdev_resume(&led->led_dev);

	/* Device might have enabled the LEDS during resume */
	led->led_dev.brightness_set(&led->led_dev, LED_OFF);
	led->led_dev.brightness = LED_OFF;
236 237 238 239
}

void rt2x00leds_resume(struct rt2x00_dev *rt2x00dev)
{
240
	if (rt2x00dev->led_radio.flags & LED_REGISTERED)
I
Ivo van Doorn 已提交
241
		rt2x00leds_resume_led(&rt2x00dev->led_radio);
242
	if (rt2x00dev->led_assoc.flags & LED_REGISTERED)
I
Ivo van Doorn 已提交
243
		rt2x00leds_resume_led(&rt2x00dev->led_assoc);
244
	if (rt2x00dev->led_qual.flags & LED_REGISTERED)
I
Ivo van Doorn 已提交
245
		rt2x00leds_resume_led(&rt2x00dev->led_qual);
246
}