rt2x00rfkill.c 4.6 KB
Newer Older
1
/*
I
Ivo van Doorn 已提交
2
	Copyright (C) 2004 - 2008 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 34 35 36 37 38 39 40 41
	<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: rt2x00rfkill
	Abstract: rt2x00 rfkill routines.
 */

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

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

static int rt2x00rfkill_toggle_radio(void *data, enum rfkill_state state)
{
	struct rt2x00_dev *rt2x00dev = data;
	int retval = 0;

	if (unlikely(!rt2x00dev))
		return 0;

	/*
42
	 * Only continue if there are enabled interfaces.
43
	 */
44
	if (!test_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags))
45 46
		return 0;

47
	if (state == RFKILL_STATE_UNBLOCKED) {
48
		INFO(rt2x00dev, "RFKILL event: enabling radio.\n");
49
		clear_bit(DEVICE_STATE_DISABLED_RADIO_HW, &rt2x00dev->flags);
50
		retval = rt2x00lib_enable_radio(rt2x00dev);
51
	} else if (state == RFKILL_STATE_SOFT_BLOCKED) {
52
		INFO(rt2x00dev, "RFKILL event: disabling radio.\n");
53
		set_bit(DEVICE_STATE_DISABLED_RADIO_HW, &rt2x00dev->flags);
54
		rt2x00lib_disable_radio(rt2x00dev);
55
	} else {
56
		WARNING(rt2x00dev, "RFKILL event: unknown state %d.\n", state);
57 58 59 60 61
	}

	return retval;
}

62
static int rt2x00rfkill_get_state(void *data, enum rfkill_state *state)
63
{
64
	struct rt2x00_dev *rt2x00dev = data;
65

66 67 68 69 70 71
	/*
	 * rfkill_poll reports 1 when the key has been pressed and the
	 * radio should be blocked.
	 */
	*state = rt2x00dev->ops->lib->rfkill_poll(rt2x00dev) ?
	    RFKILL_STATE_SOFT_BLOCKED : RFKILL_STATE_UNBLOCKED;
72 73 74 75 76 77 78 79

	return 0;
}

static void rt2x00rfkill_poll(struct work_struct *work)
{
	struct rt2x00_dev *rt2x00dev =
	    container_of(work, struct rt2x00_dev, rfkill_work.work);
80
	enum rfkill_state state;
81

82 83
	if (!test_bit(RFKILL_STATE_REGISTERED, &rt2x00dev->rfkill_state) ||
	    !test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags))
84 85 86
		return;

	/*
87 88
	 * Poll latest state and report it to rfkill who should sort
	 * out if the state should be toggled or not.
89
	 */
90 91
	if (!rt2x00rfkill_get_state(rt2x00dev, &state))
		rfkill_force_state(rt2x00dev->rfkill, state);
92 93 94

	queue_delayed_work(rt2x00dev->hw->workqueue,
			   &rt2x00dev->rfkill_work, RFKILL_POLL_INTERVAL);
95 96
}

97
void rt2x00rfkill_register(struct rt2x00_dev *rt2x00dev)
98
{
99 100
	if (!test_bit(RFKILL_STATE_ALLOCATED, &rt2x00dev->rfkill_state) ||
	    test_bit(RFKILL_STATE_REGISTERED, &rt2x00dev->rfkill_state))
101
		return;
102

103
	if (rfkill_register(rt2x00dev->rfkill)) {
104
		ERROR(rt2x00dev, "Failed to register rfkill handler.\n");
105
		return;
106 107
	}

108 109
	__set_bit(RFKILL_STATE_REGISTERED, &rt2x00dev->rfkill_state);

110 111 112 113 114
	/*
	 * Force initial poll which will detect the initial device state,
	 * and correctly sends the signal to the rfkill layer about this
	 * state.
	 */
115
	rt2x00rfkill_poll(&rt2x00dev->rfkill_work.work);
116 117 118 119
}

void rt2x00rfkill_unregister(struct rt2x00_dev *rt2x00dev)
{
120
	if (!test_bit(RFKILL_STATE_ALLOCATED, &rt2x00dev->rfkill_state) ||
121
	    !test_bit(RFKILL_STATE_REGISTERED, &rt2x00dev->rfkill_state))
122 123
		return;

124 125
	cancel_delayed_work_sync(&rt2x00dev->rfkill_work);

126
	rfkill_unregister(rt2x00dev->rfkill);
127 128

	__clear_bit(RFKILL_STATE_REGISTERED, &rt2x00dev->rfkill_state);
129 130
}

131 132
void rt2x00rfkill_allocate(struct rt2x00_dev *rt2x00dev)
{
133 134 135
	struct device *dev = wiphy_dev(rt2x00dev->hw->wiphy);

	if (test_bit(RFKILL_STATE_ALLOCATED, &rt2x00dev->rfkill_state))
136
		return;
137

138
	rt2x00dev->rfkill = rfkill_allocate(dev, RFKILL_TYPE_WLAN);
139 140
	if (!rt2x00dev->rfkill) {
		ERROR(rt2x00dev, "Failed to allocate rfkill handler.\n");
141
		return;
142 143
	}

144 145
	__set_bit(RFKILL_STATE_ALLOCATED, &rt2x00dev->rfkill_state);

146 147
	rt2x00dev->rfkill->name = rt2x00dev->ops->name;
	rt2x00dev->rfkill->data = rt2x00dev;
148
	rt2x00dev->rfkill->state = -1;
149
	rt2x00dev->rfkill->toggle_radio = rt2x00rfkill_toggle_radio;
150 151
	if (test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags))
		rt2x00dev->rfkill->get_state = rt2x00rfkill_get_state;
152

153
	INIT_DELAYED_WORK(&rt2x00dev->rfkill_work, rt2x00rfkill_poll);
154

155 156
	return;
}
157

158 159
void rt2x00rfkill_free(struct rt2x00_dev *rt2x00dev)
{
160
	if (!test_bit(RFKILL_STATE_ALLOCATED, &rt2x00dev->flags))
161
		return;
162

163
	cancel_delayed_work_sync(&rt2x00dev->rfkill_work);
164 165

	rfkill_free(rt2x00dev->rfkill);
166
	rt2x00dev->rfkill = NULL;
167
}