blob: b9d38a4f286dd80c96840070ffd18ff63cca5101 [file] [log] [blame]
Larry Finger93bb7f32007-10-10 22:44:22 -05001/*
2
Larry Finger6be50832007-10-10 22:48:17 -05003 Broadcom B43 wireless driver
Larry Finger93bb7f32007-10-10 22:44:22 -05004 RFKILL support
5
6 Copyright (c) 2007 Michael Buesch <mb@bu3sch.de>
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
21 Boston, MA 02110-1301, USA.
22
23*/
24
25#include "rfkill.h"
26#include "radio.h"
27#include "b43legacy.h"
28
29
Larry Finger6be50832007-10-10 22:48:17 -050030/* Returns TRUE, if the radio is enabled in hardware. */
31static bool b43legacy_is_hw_radio_enabled(struct b43legacy_wldev *dev)
Larry Finger93bb7f32007-10-10 22:44:22 -050032{
Larry Finger6be50832007-10-10 22:48:17 -050033 if (dev->phy.rev >= 3) {
34 if (!(b43legacy_read32(dev, B43legacy_MMIO_RADIO_HWENABLED_HI)
35 & B43legacy_MMIO_RADIO_HWENABLED_HI_MASK))
36 return 1;
37 } else {
38 if (b43legacy_read16(dev, B43legacy_MMIO_RADIO_HWENABLED_LO)
39 & B43legacy_MMIO_RADIO_HWENABLED_LO_MASK)
40 return 1;
Larry Finger93bb7f32007-10-10 22:44:22 -050041 }
Larry Finger6be50832007-10-10 22:48:17 -050042 return 0;
Larry Finger93bb7f32007-10-10 22:44:22 -050043}
44
Larry Finger6be50832007-10-10 22:48:17 -050045/* The poll callback for the hardware button. */
46static void b43legacy_rfkill_poll(struct input_polled_dev *poll_dev)
Larry Finger93bb7f32007-10-10 22:44:22 -050047{
Larry Finger6be50832007-10-10 22:48:17 -050048 struct b43legacy_wldev *dev = poll_dev->private;
Larry Finger93bb7f32007-10-10 22:44:22 -050049 struct b43legacy_wl *wl = dev->wl;
Larry Finger6be50832007-10-10 22:48:17 -050050 bool enabled;
Stefano Briviodb9683fb12007-11-06 22:48:45 +010051 bool report_change = 0;
Larry Finger93bb7f32007-10-10 22:44:22 -050052
Larry Finger6be50832007-10-10 22:48:17 -050053 mutex_lock(&wl->mutex);
Larry Finger93bb7f32007-10-10 22:44:22 -050054 B43legacy_WARN_ON(b43legacy_status(dev) < B43legacy_STAT_INITIALIZED);
Larry Finger6be50832007-10-10 22:48:17 -050055 enabled = b43legacy_is_hw_radio_enabled(dev);
56 if (unlikely(enabled != dev->radio_hw_enable)) {
57 dev->radio_hw_enable = enabled;
Stefano Briviodb9683fb12007-11-06 22:48:45 +010058 report_change = 1;
Larry Finger6be50832007-10-10 22:48:17 -050059 b43legacyinfo(wl, "Radio hardware status changed to %s\n",
60 enabled ? "ENABLED" : "DISABLED");
Stefano Briviodb9683fb12007-11-06 22:48:45 +010061 }
62 mutex_unlock(&wl->mutex);
63
64 if (unlikely(report_change))
Larry Finger6be50832007-10-10 22:48:17 -050065 input_report_key(poll_dev->input, KEY_WLAN, enabled);
Larry Finger93bb7f32007-10-10 22:44:22 -050066}
67
68/* Called when the RFKILL toggled in software.
69 * This is called without locking. */
70static int b43legacy_rfkill_soft_toggle(void *data, enum rfkill_state state)
71{
72 struct b43legacy_wldev *dev = data;
73 struct b43legacy_wl *wl = dev->wl;
74 int err = 0;
75
Stefano Briviodb9683fb12007-11-06 22:48:45 +010076 if (!wl->rfkill.registered)
77 return 0;
Larry Finger93bb7f32007-10-10 22:44:22 -050078
Stefano Briviodb9683fb12007-11-06 22:48:45 +010079 mutex_lock(&wl->mutex);
80 B43legacy_WARN_ON(b43legacy_status(dev) < B43legacy_STAT_INITIALIZED);
Larry Finger93bb7f32007-10-10 22:44:22 -050081 switch (state) {
82 case RFKILL_STATE_ON:
83 if (!dev->radio_hw_enable) {
84 /* No luck. We can't toggle the hardware RF-kill
85 * button from software. */
86 err = -EBUSY;
87 goto out_unlock;
88 }
89 if (!dev->phy.radio_on)
90 b43legacy_radio_turn_on(dev);
91 break;
92 case RFKILL_STATE_OFF:
93 if (dev->phy.radio_on)
94 b43legacy_radio_turn_off(dev, 0);
95 break;
96 }
97
98out_unlock:
99 mutex_unlock(&wl->mutex);
100
101 return err;
102}
103
Jeff Garzik93a3b602007-11-23 21:50:20 -0500104char *b43legacy_rfkill_led_name(struct b43legacy_wldev *dev)
Larry Finger93bb7f32007-10-10 22:44:22 -0500105{
106 struct b43legacy_wl *wl = dev->wl;
107
108 if (!wl->rfkill.rfkill)
109 return NULL;
110 return rfkill_get_led_name(wl->rfkill.rfkill);
111}
112
113void b43legacy_rfkill_init(struct b43legacy_wldev *dev)
114{
115 struct b43legacy_wl *wl = dev->wl;
116 struct b43legacy_rfkill *rfk = &(wl->rfkill);
117 int err;
118
Larry Finger6be50832007-10-10 22:48:17 -0500119 if (rfk->rfkill) {
120 err = rfkill_register(rfk->rfkill);
121 if (err) {
122 b43legacywarn(wl, "Failed to register RF-kill button\n");
123 goto err_free_rfk;
124 }
125 }
126 if (rfk->poll_dev) {
127 err = input_register_polled_device(rfk->poll_dev);
128 if (err) {
129 b43legacywarn(wl, "Failed to register RF-kill polldev\n");
130 goto err_free_polldev;
131 }
132 }
Larry Finger93bb7f32007-10-10 22:44:22 -0500133
134 return;
Larry Finger6be50832007-10-10 22:48:17 -0500135err_free_rfk:
Larry Finger93bb7f32007-10-10 22:44:22 -0500136 rfkill_free(rfk->rfkill);
137 rfk->rfkill = NULL;
Larry Finger6be50832007-10-10 22:48:17 -0500138err_free_polldev:
139 input_free_polled_device(rfk->poll_dev);
140 rfk->poll_dev = NULL;
Larry Finger93bb7f32007-10-10 22:44:22 -0500141}
142
143void b43legacy_rfkill_exit(struct b43legacy_wldev *dev)
144{
145 struct b43legacy_rfkill *rfk = &(dev->wl->rfkill);
146
Larry Finger6be50832007-10-10 22:48:17 -0500147 if (rfk->poll_dev)
148 input_unregister_polled_device(rfk->poll_dev);
149 if (rfk->rfkill)
150 rfkill_unregister(rfk->rfkill);
151}
152
153void b43legacy_rfkill_alloc(struct b43legacy_wldev *dev)
154{
155 struct b43legacy_wl *wl = dev->wl;
156 struct b43legacy_rfkill *rfk = &(wl->rfkill);
157
158 snprintf(rfk->name, sizeof(rfk->name),
159 "b43legacy-%s", wiphy_name(wl->hw->wiphy));
160
161 rfk->rfkill = rfkill_allocate(dev->dev->dev, RFKILL_TYPE_WLAN);
162 if (!rfk->rfkill) {
163 b43legacywarn(wl, "Failed to allocate RF-kill button\n");
Larry Finger93bb7f32007-10-10 22:44:22 -0500164 return;
Larry Finger6be50832007-10-10 22:48:17 -0500165 }
166 rfk->rfkill->name = rfk->name;
167 rfk->rfkill->state = RFKILL_STATE_ON;
168 rfk->rfkill->data = dev;
169 rfk->rfkill->toggle_radio = b43legacy_rfkill_soft_toggle;
170 rfk->rfkill->user_claim_unsupported = 1;
171
172 rfk->poll_dev = input_allocate_polled_device();
173 if (rfk->poll_dev) {
174 rfk->poll_dev->private = dev;
175 rfk->poll_dev->poll = b43legacy_rfkill_poll;
176 rfk->poll_dev->poll_interval = 1000; /* msecs */
177 } else
178 b43legacywarn(wl, "Failed to allocate RF-kill polldev\n");
179}
180
181void b43legacy_rfkill_free(struct b43legacy_wldev *dev)
182{
183 struct b43legacy_rfkill *rfk = &(dev->wl->rfkill);
184
185 input_free_polled_device(rfk->poll_dev);
186 rfk->poll_dev = NULL;
Larry Finger93bb7f32007-10-10 22:44:22 -0500187 rfkill_free(rfk->rfkill);
188 rfk->rfkill = NULL;
189}