blob: 04b29716d356a0f0dbc158c13529a8ccf9cb9ffb [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 Doorn066cb632007-09-25 20:55:39 +020044 if (!test_bit(DEVICE_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 Doorn95ea3622007-09-25 17:57:13 -070048 INFO(rt2x00dev, "Hardware button pressed, enabling radio.\n");
Ivo van Doorn81873e92007-10-06 14:14:06 +020049 __clear_bit(DEVICE_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 Doorn95ea3622007-09-25 17:57:13 -070052 INFO(rt2x00dev, "Hardware button pressed, disabling radio.\n");
Ivo van Doorn81873e92007-10-06 14:14:06 +020053 __set_bit(DEVICE_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 {
56 WARNING(rt2x00dev, "Received unexpected rfkill state %d.\n",
57 state);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070058 }
59
60 return retval;
61}
62
Ivo van Doorn50db7872008-07-04 14:51:39 +020063static int rt2x00rfkill_get_state(void *data, enum rfkill_state *state)
Ivo van Doorn95ea3622007-09-25 17:57:13 -070064{
Ivo van Doorn50db7872008-07-04 14:51:39 +020065 struct rt2x00_dev *rt2x00dev = data;
Ivo van Doorn95ea3622007-09-25 17:57:13 -070066
Ivo van Doorn50db7872008-07-04 14:51:39 +020067 *state = rt2x00dev->rfkill->state;
68
69 return 0;
70}
71
72static void rt2x00rfkill_poll(struct work_struct *work)
73{
74 struct rt2x00_dev *rt2x00dev =
75 container_of(work, struct rt2x00_dev, rfkill_work.work);
76 int state;
77
78 if (!test_bit(RFKILL_STATE_REGISTERED, &rt2x00dev->rfkill_state))
79 return;
80
81 /*
82 * rfkill_poll reports 1 when the key has been pressed and the
83 * radio should be blocked.
84 */
85 state = !rt2x00dev->ops->lib->rfkill_poll(rt2x00dev) ?
86 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
87
88 rfkill_force_state(rt2x00dev->rfkill, state);
89
90 queue_delayed_work(rt2x00dev->hw->workqueue,
91 &rt2x00dev->rfkill_work, RFKILL_POLL_INTERVAL);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070092}
93
Ivo van Doorn1682fe62008-03-13 15:38:03 +010094void rt2x00rfkill_register(struct rt2x00_dev *rt2x00dev)
Ivo van Doorn95ea3622007-09-25 17:57:13 -070095{
Ivo van Doorn1682fe62008-03-13 15:38:03 +010096 if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags) ||
97 !test_bit(RFKILL_STATE_ALLOCATED, &rt2x00dev->rfkill_state))
98 return;
Ivo van Doorn95ea3622007-09-25 17:57:13 -070099
Ivo van Doorn1682fe62008-03-13 15:38:03 +0100100 if (rfkill_register(rt2x00dev->rfkill)) {
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700101 ERROR(rt2x00dev, "Failed to register rfkill handler.\n");
Ivo van Doorn1682fe62008-03-13 15:38:03 +0100102 return;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700103 }
104
Ivo van Doorn1682fe62008-03-13 15:38:03 +0100105 __set_bit(RFKILL_STATE_REGISTERED, &rt2x00dev->rfkill_state);
106
Ivo van Doornd9890b82007-10-27 13:40:51 +0200107 /*
108 * Force initial poll which will detect the initial device state,
109 * and correctly sends the signal to the rfkill layer about this
110 * state.
111 */
Ivo van Doorn50db7872008-07-04 14:51:39 +0200112 rt2x00rfkill_poll(&rt2x00dev->rfkill_work.work);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700113}
114
115void rt2x00rfkill_unregister(struct rt2x00_dev *rt2x00dev)
116{
Ivo van Doorn1682fe62008-03-13 15:38:03 +0100117 if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags) ||
118 !test_bit(RFKILL_STATE_REGISTERED, &rt2x00dev->rfkill_state))
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700119 return;
120
Ivo van Doorn50db7872008-07-04 14:51:39 +0200121 cancel_delayed_work_sync(&rt2x00dev->rfkill_work);
122
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700123 rfkill_unregister(rt2x00dev->rfkill);
Ivo van Doorn1682fe62008-03-13 15:38:03 +0100124
125 __clear_bit(RFKILL_STATE_REGISTERED, &rt2x00dev->rfkill_state);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700126}
127
Ivo van Doorn1682fe62008-03-13 15:38:03 +0100128void rt2x00rfkill_allocate(struct rt2x00_dev *rt2x00dev)
129{
Ivo van Doorn066cb632007-09-25 20:55:39 +0200130 if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags))
Ivo van Doorn1682fe62008-03-13 15:38:03 +0100131 return;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700132
Ivo van Doorn1682fe62008-03-13 15:38:03 +0100133 rt2x00dev->rfkill =
134 rfkill_allocate(wiphy_dev(rt2x00dev->hw->wiphy), RFKILL_TYPE_WLAN);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700135 if (!rt2x00dev->rfkill) {
136 ERROR(rt2x00dev, "Failed to allocate rfkill handler.\n");
Ivo van Doorn1682fe62008-03-13 15:38:03 +0100137 return;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700138 }
139
140 rt2x00dev->rfkill->name = rt2x00dev->ops->name;
141 rt2x00dev->rfkill->data = rt2x00dev;
Ivo van Doornd9890b82007-10-27 13:40:51 +0200142 rt2x00dev->rfkill->state = -1;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700143 rt2x00dev->rfkill->toggle_radio = rt2x00rfkill_toggle_radio;
Ivo van Doorn50db7872008-07-04 14:51:39 +0200144 rt2x00dev->rfkill->get_state = rt2x00rfkill_get_state;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700145
Ivo van Doorn50db7872008-07-04 14:51:39 +0200146 INIT_DELAYED_WORK(&rt2x00dev->rfkill_work, rt2x00rfkill_poll);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700147
Ivo van Doorn1682fe62008-03-13 15:38:03 +0100148 return;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700149}
150
151void rt2x00rfkill_free(struct rt2x00_dev *rt2x00dev)
152{
Ivo van Doorn1682fe62008-03-13 15:38:03 +0100153 if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags) ||
154 !test_bit(RFKILL_STATE_ALLOCATED, &rt2x00dev->rfkill_state))
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700155 return;
156
Ivo van Doorn50db7872008-07-04 14:51:39 +0200157 cancel_delayed_work_sync(&rt2x00dev->rfkill_work);
Ivo van Doorn1682fe62008-03-13 15:38:03 +0100158
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700159 rfkill_free(rt2x00dev->rfkill);
Ivo van Doorn1682fe62008-03-13 15:38:03 +0100160 rt2x00dev->rfkill = NULL;
161}