blob: a0f8b8e0a24b52dc274267e4db22c4aa056191e4 [file] [log] [blame]
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001/*
2 Copyright (C) 2004 - 2007 rt2x00 SourceForge Project
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: rt2x00rfkill
23 Abstract: rt2x00 rfkill routines.
24 */
25
26/*
27 * Set enviroment defines for rt2x00.h
28 */
29#define DRV_NAME "rt2x00lib"
30
31#include <linux/input-polldev.h>
32#include <linux/kernel.h>
33#include <linux/module.h>
34#include <linux/rfkill.h>
35
36#include "rt2x00.h"
37#include "rt2x00lib.h"
38
39static int rt2x00rfkill_toggle_radio(void *data, enum rfkill_state state)
40{
41 struct rt2x00_dev *rt2x00dev = data;
42 int retval = 0;
43
44 if (unlikely(!rt2x00dev))
45 return 0;
46
47 /*
Ivo van Doorn066cb632007-09-25 20:55:39 +020048 * Only continue if there are enabled interfaces.
Ivo van Doorn95ea3622007-09-25 17:57:13 -070049 */
Ivo van Doorn066cb632007-09-25 20:55:39 +020050 if (!test_bit(DEVICE_STARTED, &rt2x00dev->flags))
Ivo van Doorn95ea3622007-09-25 17:57:13 -070051 return 0;
52
53 if (state == RFKILL_STATE_ON) {
54 INFO(rt2x00dev, "Hardware button pressed, enabling radio.\n");
Ivo van Doorn81873e92007-10-06 14:14:06 +020055 __clear_bit(DEVICE_DISABLED_RADIO_HW, &rt2x00dev->flags);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070056 retval = rt2x00lib_enable_radio(rt2x00dev);
57 } else if (state == RFKILL_STATE_OFF) {
58 INFO(rt2x00dev, "Hardware button pressed, disabling radio.\n");
Ivo van Doorn81873e92007-10-06 14:14:06 +020059 __set_bit(DEVICE_DISABLED_RADIO_HW, &rt2x00dev->flags);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070060 rt2x00lib_disable_radio(rt2x00dev);
61 }
62
63 return retval;
64}
65
66static void rt2x00rfkill_poll(struct input_polled_dev *poll_dev)
67{
68 struct rt2x00_dev *rt2x00dev = poll_dev->private;
69 int state = rt2x00dev->ops->lib->rfkill_poll(rt2x00dev);
70
71 if (rt2x00dev->rfkill->state != state)
72 input_report_key(poll_dev->input, KEY_WLAN, 1);
73}
74
75int rt2x00rfkill_register(struct rt2x00_dev *rt2x00dev)
76{
77 int retval;
78
Ivo van Doorn066cb632007-09-25 20:55:39 +020079 if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags))
Ivo van Doorn95ea3622007-09-25 17:57:13 -070080 return 0;
81
82 retval = rfkill_register(rt2x00dev->rfkill);
83 if (retval) {
84 ERROR(rt2x00dev, "Failed to register rfkill handler.\n");
85 return retval;
86 }
87
88 retval = input_register_polled_device(rt2x00dev->poll_dev);
89 if (retval) {
90 ERROR(rt2x00dev, "Failed to register polled device.\n");
91 rfkill_unregister(rt2x00dev->rfkill);
92 return retval;
93 }
94
95 return 0;
96}
97
98void rt2x00rfkill_unregister(struct rt2x00_dev *rt2x00dev)
99{
Ivo van Doorn066cb632007-09-25 20:55:39 +0200100 if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags))
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700101 return;
102
103 input_unregister_polled_device(rt2x00dev->poll_dev);
104 rfkill_unregister(rt2x00dev->rfkill);
105}
106
107int rt2x00rfkill_allocate(struct rt2x00_dev *rt2x00dev)
108{
109 struct device *device = wiphy_dev(rt2x00dev->hw->wiphy);
110
Ivo van Doorn066cb632007-09-25 20:55:39 +0200111 if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags))
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700112 return 0;
113
114 rt2x00dev->rfkill = rfkill_allocate(device, RFKILL_TYPE_WLAN);
115 if (!rt2x00dev->rfkill) {
116 ERROR(rt2x00dev, "Failed to allocate rfkill handler.\n");
117 return -ENOMEM;
118 }
119
120 rt2x00dev->rfkill->name = rt2x00dev->ops->name;
121 rt2x00dev->rfkill->data = rt2x00dev;
122 rt2x00dev->rfkill->state = rt2x00dev->ops->lib->rfkill_poll(rt2x00dev);
123 rt2x00dev->rfkill->toggle_radio = rt2x00rfkill_toggle_radio;
124
125 rt2x00dev->poll_dev = input_allocate_polled_device();
126 if (!rt2x00dev->poll_dev) {
127 ERROR(rt2x00dev, "Failed to allocate polled device.\n");
128 rfkill_free(rt2x00dev->rfkill);
129 return -ENOMEM;
130 }
131
132 rt2x00dev->poll_dev->private = rt2x00dev;
133 rt2x00dev->poll_dev->poll = rt2x00rfkill_poll;
134 rt2x00dev->poll_dev->poll_interval = RFKILL_POLL_INTERVAL;
135
136 return 0;
137}
138
139void rt2x00rfkill_free(struct rt2x00_dev *rt2x00dev)
140{
Ivo van Doorn066cb632007-09-25 20:55:39 +0200141 if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags))
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700142 return;
143
144 input_free_polled_device(rt2x00dev->poll_dev);
145 rfkill_free(rt2x00dev->rfkill);
146}