summaryrefslogtreecommitdiffstats
path: root/package/rt2x00/src/rt2x00rfkill.c
blob: c08a2aa0eafe18b42f6490786a8e921657800714 (plain)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
	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: rt2x00lib
	Abstract: rt2x00 rfkill specific routines.
	Supported chipsets: RT2460, RT2560, rt2561, rt2561s, rt2661.
 */

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

#include <linux/rfkill.h>

#include "rt2x00.h"

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

	if (unlikely(!rt2x00dev))
		return 0;

	/*
	 * Only continue if we have an active interface,
	 * either monitor or non-monitor should be present.
	 */
	if (!is_interface_present(&rt2x00dev->interface) &&
	    !is_monitor_present(&rt2x00dev->interface))
		return 0;

	if (state == RFKILL_STATE_ON) {
		INFO(rt2x00dev, "Hardware button pressed, enabling radio.\n");
		__set_bit(DEVICE_ENABLED_RADIO_HW, &rt2x00dev->flags);
		retval = rt2x00lib_enable_radio(rt2x00dev);
	} else if (state == RFKILL_STATE_OFF) {
		INFO(rt2x00dev, "Hardware button pressed, disabling radio.\n");
		__clear_bit(DEVICE_ENABLED_RADIO_HW, &rt2x00dev->flags);
		rt2x00lib_disable_radio(rt2x00dev);
	}

	return retval;
}

static void rt2x00lib_rfkill_poll(struct work_struct *work)
{
	struct rt2x00_dev *rt2x00dev =
		container_of(work, struct rt2x00_dev, rfkill_work.work);

	rfkill_switch_all(rt2x00dev->rfkill->type,
		rt2x00dev->ops->lib->rfkill_poll(rt2x00dev));

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

int rt2x00lib_register_rfkill(struct rt2x00_dev *rt2x00dev)
{
	int status = rfkill_register(rt2x00dev->rfkill);
	if (status) {
		ERROR(rt2x00dev, "Failed to register rfkill handler.\n");
		return status;
	}

	rt2x00lib_rfkill_poll(&rt2x00dev->rfkill_work.work);

	return !schedule_delayed_work(&rt2x00dev->rfkill_work,
		RFKILL_POLL_INTERVAL);
}

void rt2x00lib_unregister_rfkill(struct rt2x00_dev *rt2x00dev)
{
	if (delayed_work_pending(&rt2x00dev->rfkill_work))
		cancel_rearming_delayed_workqueue(
			rt2x00dev->hw->workqueue, &rt2x00dev->rfkill_work);

	rfkill_unregister(rt2x00dev->rfkill);
}

int rt2x00lib_allocate_rfkill(struct rt2x00_dev *rt2x00dev)
{
	struct rfkill *rfkill;

	if (!test_bit(DEVICE_SUPPORT_HW_BUTTON, &rt2x00dev->flags))
		return 0;

	rfkill = rfkill_allocate(rt2x00dev->device, RFKILL_TYPE_WLAN);
	if (!rfkill) {
		ERROR(rt2x00dev, "Failed to allocate rfkill handler.\n");
		return -ENOMEM;
	}

	rfkill->name = rt2x00dev->ops->name;
	rfkill->data = rt2x00dev;
	rfkill->toggle_radio = rt2x00lib_toggle_radio;
	rt2x00dev->rfkill = rfkill;

	INIT_DELAYED_WORK(&rt2x00dev->rfkill_work, rt2x00lib_rfkill_poll);

	return 0;
}

void rt2x00lib_free_rfkill(struct rt2x00_dev *rt2x00dev)
{
	if (!test_bit(DEVICE_SUPPORT_HW_BUTTON, &rt2x00dev->flags))
		return;

	rfkill_free(rt2x00dev->rfkill);
}