2 Copyright (C) 2004 - 2007 rt2x00 SourceForge Project
3 <http://rt2x00.serialmonkey.com>
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.
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.
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.
23 Abstract: rt2x00 rfkill routines.
26 #include <linux/input-polldev.h>
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/rfkill.h>
32 #include "rt2x00lib.h"
34 static int rt2x00rfkill_toggle_radio(void *data, enum rfkill_state state)
36 struct rt2x00_dev *rt2x00dev = data;
39 if (unlikely(!rt2x00dev))
43 * Only continue if there are enabled interfaces.
45 if (!test_bit(DEVICE_STARTED, &rt2x00dev->flags))
48 if (state == RFKILL_STATE_ON) {
49 INFO(rt2x00dev, "Hardware button pressed, enabling radio.\n");
50 __clear_bit(DEVICE_DISABLED_RADIO_HW, &rt2x00dev->flags);
51 retval = rt2x00lib_enable_radio(rt2x00dev);
52 } else if (state == RFKILL_STATE_OFF) {
53 INFO(rt2x00dev, "Hardware button pressed, disabling radio.\n");
54 __set_bit(DEVICE_DISABLED_RADIO_HW, &rt2x00dev->flags);
55 rt2x00lib_disable_radio(rt2x00dev);
61 static void rt2x00rfkill_poll(struct input_polled_dev *poll_dev)
63 struct rt2x00_dev *rt2x00dev = poll_dev->private;
64 int state = rt2x00dev->ops->lib->rfkill_poll(rt2x00dev);
66 if (rt2x00dev->rfkill->state != state) {
67 input_report_key(poll_dev->input, KEY_WLAN, 1);
68 input_report_key(poll_dev->input, KEY_WLAN, 0);
72 int rt2x00rfkill_register(struct rt2x00_dev *rt2x00dev)
76 if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags))
79 retval = rfkill_register(rt2x00dev->rfkill);
81 ERROR(rt2x00dev, "Failed to register rfkill handler.\n");
85 retval = input_register_polled_device(rt2x00dev->poll_dev);
87 ERROR(rt2x00dev, "Failed to register polled device.\n");
88 rfkill_unregister(rt2x00dev->rfkill);
93 * Force initial poll which will detect the initial device state,
94 * and correctly sends the signal to the rfkill layer about this
97 rt2x00rfkill_poll(rt2x00dev->poll_dev);
102 void rt2x00rfkill_unregister(struct rt2x00_dev *rt2x00dev)
104 if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags))
107 input_unregister_polled_device(rt2x00dev->poll_dev);
108 rfkill_unregister(rt2x00dev->rfkill);
111 int rt2x00rfkill_allocate(struct rt2x00_dev *rt2x00dev)
113 struct device *device = wiphy_dev(rt2x00dev->hw->wiphy);
115 if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags))
118 rt2x00dev->rfkill = rfkill_allocate(device, RFKILL_TYPE_WLAN);
119 if (!rt2x00dev->rfkill) {
120 ERROR(rt2x00dev, "Failed to allocate rfkill handler.\n");
124 rt2x00dev->rfkill->name = rt2x00dev->ops->name;
125 rt2x00dev->rfkill->data = rt2x00dev;
126 rt2x00dev->rfkill->state = -1;
127 rt2x00dev->rfkill->toggle_radio = rt2x00rfkill_toggle_radio;
129 rt2x00dev->poll_dev = input_allocate_polled_device();
130 if (!rt2x00dev->poll_dev) {
131 ERROR(rt2x00dev, "Failed to allocate polled device.\n");
132 goto exit_free_rfkill;
135 rt2x00dev->poll_dev->private = rt2x00dev;
136 rt2x00dev->poll_dev->poll = rt2x00rfkill_poll;
137 rt2x00dev->poll_dev->poll_interval = RFKILL_POLL_INTERVAL;
139 rt2x00dev->poll_dev->input->name = rt2x00dev->ops->name;
140 rt2x00dev->poll_dev->input->phys = wiphy_name(rt2x00dev->hw->wiphy);
141 rt2x00dev->poll_dev->input->id.bustype = BUS_HOST;
142 rt2x00dev->poll_dev->input->id.vendor = 0x1814;
143 rt2x00dev->poll_dev->input->id.product = rt2x00dev->chip.rt;
144 rt2x00dev->poll_dev->input->id.version = rt2x00dev->chip.rev;
145 rt2x00dev->poll_dev->input->dev.parent = device;
146 rt2x00dev->poll_dev->input->evbit[0] = BIT(EV_KEY);
147 set_bit(KEY_WLAN, rt2x00dev->poll_dev->input->keybit);
152 rfkill_free(rt2x00dev->rfkill);
158 void rt2x00rfkill_free(struct rt2x00_dev *rt2x00dev)
160 if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags))
163 input_free_polled_device(rt2x00dev->poll_dev);
164 rfkill_free(rt2x00dev->rfkill);