rt2x00rfkill.c 4.76 KB
Newer Older
1
2
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
42
43
44
45
46
47
/*
	Copyright (C) 2004 - 2007 rt2x00 SourceForge Project
	<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.
 */

/*
 * Set enviroment defines for rt2x00.h
 */
#define DRV_NAME "rt2x00lib"

#include <linux/input-polldev.h>
#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;

	/*
48
	 * Only continue if there are enabled interfaces.
49
	 */
50
	if (!test_bit(DEVICE_STARTED, &rt2x00dev->flags))
51
52
53
54
		return 0;

	if (state == RFKILL_STATE_ON) {
		INFO(rt2x00dev, "Hardware button pressed, enabling radio.\n");
55
		__clear_bit(DEVICE_DISABLED_RADIO_HW, &rt2x00dev->flags);
56
57
58
		retval = rt2x00lib_enable_radio(rt2x00dev);
	} else if (state == RFKILL_STATE_OFF) {
		INFO(rt2x00dev, "Hardware button pressed, disabling radio.\n");
59
		__set_bit(DEVICE_DISABLED_RADIO_HW, &rt2x00dev->flags);
60
61
62
63
64
65
66
67
68
69
70
		rt2x00lib_disable_radio(rt2x00dev);
	}

	return retval;
}

static void rt2x00rfkill_poll(struct input_polled_dev *poll_dev)
{
	struct rt2x00_dev *rt2x00dev = poll_dev->private;
	int state = rt2x00dev->ops->lib->rfkill_poll(rt2x00dev);

71
	if (rt2x00dev->rfkill->state != state) {
72
		input_report_key(poll_dev->input, KEY_WLAN, 1);
73
74
		input_report_key(poll_dev->input, KEY_WLAN, 0);
	}
75
76
77
78
79
80
}

int rt2x00rfkill_register(struct rt2x00_dev *rt2x00dev)
{
	int retval;

81
	if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags))
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
		return 0;

	retval = rfkill_register(rt2x00dev->rfkill);
	if (retval) {
		ERROR(rt2x00dev, "Failed to register rfkill handler.\n");
		return retval;
	}

	retval = input_register_polled_device(rt2x00dev->poll_dev);
	if (retval) {
		ERROR(rt2x00dev, "Failed to register polled device.\n");
		rfkill_unregister(rt2x00dev->rfkill);
		return retval;
	}

	return 0;
}

void rt2x00rfkill_unregister(struct rt2x00_dev *rt2x00dev)
{
102
	if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags))
103
104
105
106
107
108
109
110
111
112
		return;

	input_unregister_polled_device(rt2x00dev->poll_dev);
	rfkill_unregister(rt2x00dev->rfkill);
}

int rt2x00rfkill_allocate(struct rt2x00_dev *rt2x00dev)
{
	struct device *device = wiphy_dev(rt2x00dev->hw->wiphy);

113
	if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags))
114
115
116
117
118
		return 0;

	rt2x00dev->rfkill = rfkill_allocate(device, RFKILL_TYPE_WLAN);
	if (!rt2x00dev->rfkill) {
		ERROR(rt2x00dev, "Failed to allocate rfkill handler.\n");
119
		goto exit;
120
121
122
123
124
125
126
127
128
129
	}

	rt2x00dev->rfkill->name = rt2x00dev->ops->name;
	rt2x00dev->rfkill->data = rt2x00dev;
	rt2x00dev->rfkill->state = rt2x00dev->ops->lib->rfkill_poll(rt2x00dev);
	rt2x00dev->rfkill->toggle_radio = rt2x00rfkill_toggle_radio;

	rt2x00dev->poll_dev = input_allocate_polled_device();
	if (!rt2x00dev->poll_dev) {
		ERROR(rt2x00dev, "Failed to allocate polled device.\n");
130
		goto exit_free_rfkill;
131
132
133
134
135
136
	}

	rt2x00dev->poll_dev->private = rt2x00dev;
	rt2x00dev->poll_dev->poll = rt2x00rfkill_poll;
	rt2x00dev->poll_dev->poll_interval = RFKILL_POLL_INTERVAL;

137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
	rt2x00dev->poll_dev->input = input_allocate_device();
	if (!rt2x00dev->poll_dev->input) {
		ERROR(rt2x00dev, "Failed to allocate input device.\n");
		goto exit_free_polldev;
	}

	rt2x00dev->poll_dev->input->name = rt2x00dev->ops->name;
	rt2x00dev->poll_dev->input->phys = wiphy_name(rt2x00dev->hw->wiphy);
	rt2x00dev->poll_dev->input->id.bustype = BUS_HOST;
	rt2x00dev->poll_dev->input->id.vendor = 0x1814;
	rt2x00dev->poll_dev->input->id.product = rt2x00dev->chip.rt;
	rt2x00dev->poll_dev->input->id.version = rt2x00dev->chip.rev;
	rt2x00dev->poll_dev->input->dev.parent = device;
	rt2x00dev->poll_dev->input->evbit[0] = BIT(EV_KEY);
	set_bit(KEY_WLAN, rt2x00dev->poll_dev->input->keybit);

153
	return 0;
154
155
156
157
158
159
160
161
162

exit_free_polldev:
	input_free_polled_device(rt2x00dev->poll_dev);

exit_free_rfkill:
	rfkill_free(rt2x00dev->rfkill);

exit:
	return -ENOMEM;
163
164
165
166
}

void rt2x00rfkill_free(struct rt2x00_dev *rt2x00dev)
{
167
	if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags))
168
169
		return;

170
	input_free_device(rt2x00dev->poll_dev->input);
171
172
173
	input_free_polled_device(rt2x00dev->poll_dev);
	rfkill_free(rt2x00dev->rfkill);
}