blob: c3f53a92180a02c4a42533e4bd7ad4eddf56b4e2 [file] [log] [blame]
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001/*
Ivo van Doorn811aa9c2008-02-03 15:42:53 +01002 Copyright (C) 2004 - 2008 rt2x00 SourceForge Project
Ivo van Doorn95ea3622007-09-25 17:57:13 -07003 <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
Ivo van Doorn95ea3622007-09-25 17:57:13 -070026#include <linux/kernel.h>
27#include <linux/module.h>
28#include <linux/rfkill.h>
29
30#include "rt2x00.h"
31#include "rt2x00lib.h"
32
33static int rt2x00rfkill_toggle_radio(void *data, enum rfkill_state state)
34{
35 struct rt2x00_dev *rt2x00dev = data;
36 int retval = 0;
37
38 if (unlikely(!rt2x00dev))
39 return 0;
40
41 /*
Ivo van Doorn066cb632007-09-25 20:55:39 +020042 * Only continue if there are enabled interfaces.
Ivo van Doorn95ea3622007-09-25 17:57:13 -070043 */
Ivo van Doorn0262ab02008-08-29 21:04:26 +020044 if (!test_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags))
Ivo van Doorn95ea3622007-09-25 17:57:13 -070045 return 0;
46
John W. Linvilleff28bd92008-06-27 10:27:47 -040047 if (state == RFKILL_STATE_UNBLOCKED) {
Ivo van Doorn58169522008-09-08 18:46:29 +020048 INFO(rt2x00dev, "RFKILL event: enabling radio.\n");
Ivo van Doorn0262ab02008-08-29 21:04:26 +020049 clear_bit(DEVICE_STATE_DISABLED_RADIO_HW, &rt2x00dev->flags);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070050 retval = rt2x00lib_enable_radio(rt2x00dev);
John W. Linvilleff28bd92008-06-27 10:27:47 -040051 } else if (state == RFKILL_STATE_SOFT_BLOCKED) {
Ivo van Doorn58169522008-09-08 18:46:29 +020052 INFO(rt2x00dev, "RFKILL event: disabling radio.\n");
Ivo van Doorn0262ab02008-08-29 21:04:26 +020053 set_bit(DEVICE_STATE_DISABLED_RADIO_HW, &rt2x00dev->flags);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070054 rt2x00lib_disable_radio(rt2x00dev);
John W. Linvilleff28bd92008-06-27 10:27:47 -040055 } else {
Ivo van Doorn58169522008-09-08 18:46:29 +020056 WARNING(rt2x00dev, "RFKILL event: unknown state %d.\n", state);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070057 }
58
59 return retval;
60}
61
Ivo van Doorn50db7872008-07-04 14:51:39 +020062static int rt2x00rfkill_get_state(void *data, enum rfkill_state *state)
Ivo van Doorn95ea3622007-09-25 17:57:13 -070063{
Ivo van Doorn50db7872008-07-04 14:51:39 +020064 struct rt2x00_dev *rt2x00dev = data;
Ivo van Doorn95ea3622007-09-25 17:57:13 -070065
Ivo van Doorn58169522008-09-08 18:46:29 +020066 /*
67 * rfkill_poll reports 1 when the key has been pressed and the
68 * radio should be blocked.
69 */
70 *state = rt2x00dev->ops->lib->rfkill_poll(rt2x00dev) ?
71 RFKILL_STATE_SOFT_BLOCKED : RFKILL_STATE_UNBLOCKED;
Ivo van Doorn50db7872008-07-04 14:51:39 +020072
73 return 0;
74}
75
76static void rt2x00rfkill_poll(struct work_struct *work)
77{
78 struct rt2x00_dev *rt2x00dev =
79 container_of(work, struct rt2x00_dev, rfkill_work.work);
Ivo van Doorn58169522008-09-08 18:46:29 +020080 enum rfkill_state state;
Ivo van Doorn50db7872008-07-04 14:51:39 +020081
Ivo van Doorn58169522008-09-08 18:46:29 +020082 if (!test_bit(RFKILL_STATE_REGISTERED, &rt2x00dev->rfkill_state) ||
83 !test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags))
Ivo van Doorn50db7872008-07-04 14:51:39 +020084 return;
85
86 /*
Ivo van Doorn58169522008-09-08 18:46:29 +020087 * Poll latest state and report it to rfkill who should sort
88 * out if the state should be toggled or not.
Ivo van Doorn50db7872008-07-04 14:51:39 +020089 */
Ivo van Doorn58169522008-09-08 18:46:29 +020090 if (!rt2x00rfkill_get_state(rt2x00dev, &state))
91 rfkill_force_state(rt2x00dev->rfkill, state);
Ivo van Doorn50db7872008-07-04 14:51:39 +020092
93 queue_delayed_work(rt2x00dev->hw->workqueue,
94 &rt2x00dev->rfkill_work, RFKILL_POLL_INTERVAL);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070095}
96
Ivo van Doorn1682fe62008-03-13 15:38:03 +010097void rt2x00rfkill_register(struct rt2x00_dev *rt2x00dev)
Ivo van Doorn95ea3622007-09-25 17:57:13 -070098{
Ivo van Doorn58169522008-09-08 18:46:29 +020099 if (!test_bit(RFKILL_STATE_ALLOCATED, &rt2x00dev->rfkill_state) ||
100 test_bit(RFKILL_STATE_REGISTERED, &rt2x00dev->rfkill_state))
Ivo van Doorn1682fe62008-03-13 15:38:03 +0100101 return;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700102
Ivo van Doorn1682fe62008-03-13 15:38:03 +0100103 if (rfkill_register(rt2x00dev->rfkill)) {
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700104 ERROR(rt2x00dev, "Failed to register rfkill handler.\n");
Ivo van Doorn1682fe62008-03-13 15:38:03 +0100105 return;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700106 }
107
Ivo van Doorn1682fe62008-03-13 15:38:03 +0100108 __set_bit(RFKILL_STATE_REGISTERED, &rt2x00dev->rfkill_state);
109
Ivo van Doornd9890b82007-10-27 13:40:51 +0200110 /*
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.
114 */
Ivo van Doorn50db7872008-07-04 14:51:39 +0200115 rt2x00rfkill_poll(&rt2x00dev->rfkill_work.work);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700116}
117
118void rt2x00rfkill_unregister(struct rt2x00_dev *rt2x00dev)
119{
Ivo van Doorn58169522008-09-08 18:46:29 +0200120 if (!test_bit(RFKILL_STATE_ALLOCATED, &rt2x00dev->rfkill_state) ||
Ivo van Doorn1682fe62008-03-13 15:38:03 +0100121 !test_bit(RFKILL_STATE_REGISTERED, &rt2x00dev->rfkill_state))
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700122 return;
123
Ivo van Doorn50db7872008-07-04 14:51:39 +0200124 cancel_delayed_work_sync(&rt2x00dev->rfkill_work);
125
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700126 rfkill_unregister(rt2x00dev->rfkill);
Ivo van Doorn1682fe62008-03-13 15:38:03 +0100127
128 __clear_bit(RFKILL_STATE_REGISTERED, &rt2x00dev->rfkill_state);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700129}
130
Ivo van Doorn1682fe62008-03-13 15:38:03 +0100131void rt2x00rfkill_allocate(struct rt2x00_dev *rt2x00dev)
132{
Ivo van Doorn58169522008-09-08 18:46:29 +0200133 struct device *dev = wiphy_dev(rt2x00dev->hw->wiphy);
134
135 if (test_bit(RFKILL_STATE_ALLOCATED, &rt2x00dev->rfkill_state))
Ivo van Doorn1682fe62008-03-13 15:38:03 +0100136 return;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700137
Ivo van Doorn58169522008-09-08 18:46:29 +0200138 rt2x00dev->rfkill = rfkill_allocate(dev, RFKILL_TYPE_WLAN);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700139 if (!rt2x00dev->rfkill) {
140 ERROR(rt2x00dev, "Failed to allocate rfkill handler.\n");
Ivo van Doorn1682fe62008-03-13 15:38:03 +0100141 return;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700142 }
143
Ivo van Doorn58169522008-09-08 18:46:29 +0200144 __set_bit(RFKILL_STATE_ALLOCATED, &rt2x00dev->rfkill_state);
145
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700146 rt2x00dev->rfkill->name = rt2x00dev->ops->name;
147 rt2x00dev->rfkill->data = rt2x00dev;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700148 rt2x00dev->rfkill->toggle_radio = rt2x00rfkill_toggle_radio;
Gertjan van Wingerde5b5d13a2008-10-03 20:20:32 +0200149 if (test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags)) {
Ivo van Doorn58169522008-09-08 18:46:29 +0200150 rt2x00dev->rfkill->get_state = rt2x00rfkill_get_state;
Gertjan van Wingerde5b5d13a2008-10-03 20:20:32 +0200151 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;
156 }
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700157
Ivo van Doorn50db7872008-07-04 14:51:39 +0200158 INIT_DELAYED_WORK(&rt2x00dev->rfkill_work, rt2x00rfkill_poll);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700159
Ivo van Doorn1682fe62008-03-13 15:38:03 +0100160 return;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700161}
162
163void rt2x00rfkill_free(struct rt2x00_dev *rt2x00dev)
164{
Ivo van Doorn58169522008-09-08 18:46:29 +0200165 if (!test_bit(RFKILL_STATE_ALLOCATED, &rt2x00dev->flags))
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700166 return;
167
Ivo van Doorn50db7872008-07-04 14:51:39 +0200168 cancel_delayed_work_sync(&rt2x00dev->rfkill_work);
Ivo van Doorn1682fe62008-03-13 15:38:03 +0100169
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700170 rfkill_free(rt2x00dev->rfkill);
Ivo van Doorn1682fe62008-03-13 15:38:03 +0100171 rt2x00dev->rfkill = NULL;
172}