MERGE-master-patchset-edits
[linux-2.6/openmoko-kernel.git] / drivers / net / wireless / rt2x00 / rt2x00rfkill.c
blobc3f53a92180a02c4a42533e4bd7ad4eddf56b4e2
1 /*
2 Copyright (C) 2004 - 2008 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.
22 Module: rt2x00rfkill
23 Abstract: rt2x00 rfkill routines.
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/rfkill.h>
30 #include "rt2x00.h"
31 #include "rt2x00lib.h"
33 static int rt2x00rfkill_toggle_radio(void *data, enum rfkill_state state)
35 struct rt2x00_dev *rt2x00dev = data;
36 int retval = 0;
38 if (unlikely(!rt2x00dev))
39 return 0;
42 * Only continue if there are enabled interfaces.
44 if (!test_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags))
45 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);
59 return retval;
62 static int rt2x00rfkill_get_state(void *data, enum rfkill_state *state)
64 struct rt2x00_dev *rt2x00dev = data;
67 * rfkill_poll reports 1 when the key has been pressed and the
68 * radio should be blocked.
70 *state = rt2x00dev->ops->lib->rfkill_poll(rt2x00dev) ?
71 RFKILL_STATE_SOFT_BLOCKED : RFKILL_STATE_UNBLOCKED;
73 return 0;
76 static void rt2x00rfkill_poll(struct work_struct *work)
78 struct rt2x00_dev *rt2x00dev =
79 container_of(work, struct rt2x00_dev, rfkill_work.work);
80 enum rfkill_state state;
82 if (!test_bit(RFKILL_STATE_REGISTERED, &rt2x00dev->rfkill_state) ||
83 !test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags))
84 return;
87 * Poll latest state and report it to rfkill who should sort
88 * out if the state should be toggled or not.
90 if (!rt2x00rfkill_get_state(rt2x00dev, &state))
91 rfkill_force_state(rt2x00dev->rfkill, state);
93 queue_delayed_work(rt2x00dev->hw->workqueue,
94 &rt2x00dev->rfkill_work, RFKILL_POLL_INTERVAL);
97 void rt2x00rfkill_register(struct rt2x00_dev *rt2x00dev)
99 if (!test_bit(RFKILL_STATE_ALLOCATED, &rt2x00dev->rfkill_state) ||
100 test_bit(RFKILL_STATE_REGISTERED, &rt2x00dev->rfkill_state))
101 return;
103 if (rfkill_register(rt2x00dev->rfkill)) {
104 ERROR(rt2x00dev, "Failed to register rfkill handler.\n");
105 return;
108 __set_bit(RFKILL_STATE_REGISTERED, &rt2x00dev->rfkill_state);
111 * Force initial poll which will detect the initial device state,
112 * and correctly sends the signal to the rfkill layer about this
113 * state.
115 rt2x00rfkill_poll(&rt2x00dev->rfkill_work.work);
118 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 return;
124 cancel_delayed_work_sync(&rt2x00dev->rfkill_work);
126 rfkill_unregister(rt2x00dev->rfkill);
128 __clear_bit(RFKILL_STATE_REGISTERED, &rt2x00dev->rfkill_state);
131 void rt2x00rfkill_allocate(struct rt2x00_dev *rt2x00dev)
133 struct device *dev = wiphy_dev(rt2x00dev->hw->wiphy);
135 if (test_bit(RFKILL_STATE_ALLOCATED, &rt2x00dev->rfkill_state))
136 return;
138 rt2x00dev->rfkill = rfkill_allocate(dev, RFKILL_TYPE_WLAN);
139 if (!rt2x00dev->rfkill) {
140 ERROR(rt2x00dev, "Failed to allocate rfkill handler.\n");
141 return;
144 __set_bit(RFKILL_STATE_ALLOCATED, &rt2x00dev->rfkill_state);
146 rt2x00dev->rfkill->name = rt2x00dev->ops->name;
147 rt2x00dev->rfkill->data = rt2x00dev;
148 rt2x00dev->rfkill->toggle_radio = rt2x00rfkill_toggle_radio;
149 if (test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags)) {
150 rt2x00dev->rfkill->get_state = rt2x00rfkill_get_state;
151 rt2x00dev->rfkill->state =
152 rt2x00dev->ops->lib->rfkill_poll(rt2x00dev) ?
153 RFKILL_STATE_SOFT_BLOCKED : RFKILL_STATE_UNBLOCKED;
154 } else {
155 rt2x00dev->rfkill->state = RFKILL_STATE_UNBLOCKED;
158 INIT_DELAYED_WORK(&rt2x00dev->rfkill_work, rt2x00rfkill_poll);
160 return;
163 void rt2x00rfkill_free(struct rt2x00_dev *rt2x00dev)
165 if (!test_bit(RFKILL_STATE_ALLOCATED, &rt2x00dev->flags))
166 return;
168 cancel_delayed_work_sync(&rt2x00dev->rfkill_work);
170 rfkill_free(rt2x00dev->rfkill);
171 rt2x00dev->rfkill = NULL;