blob: c6230a64505a050d6ccf92f9229c09e5b71588d4 [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
Larry Finger4ad36d72007-12-16 19:21:06 +010029#include <linux/kmod.h>
30
Larry Finger93bb7f32007-10-10 22:44:22 -050031
Larry Finger6be50832007-10-10 22:48:17 -050032/* Returns TRUE, if the radio is enabled in hardware. */
33static bool b43legacy_is_hw_radio_enabled(struct b43legacy_wldev *dev)
Larry Finger93bb7f32007-10-10 22:44:22 -050034{
Larry Finger6be50832007-10-10 22:48:17 -050035 if (dev->phy.rev >= 3) {
36 if (!(b43legacy_read32(dev, B43legacy_MMIO_RADIO_HWENABLED_HI)
37 & B43legacy_MMIO_RADIO_HWENABLED_HI_MASK))
38 return 1;
39 } else {
40 if (b43legacy_read16(dev, B43legacy_MMIO_RADIO_HWENABLED_LO)
41 & B43legacy_MMIO_RADIO_HWENABLED_LO_MASK)
42 return 1;
Larry Finger93bb7f32007-10-10 22:44:22 -050043 }
Larry Finger6be50832007-10-10 22:48:17 -050044 return 0;
Larry Finger93bb7f32007-10-10 22:44:22 -050045}
46
Larry Finger6be50832007-10-10 22:48:17 -050047/* The poll callback for the hardware button. */
Johannes Berg19d337d2009-06-02 13:01:37 +020048static void b43legacy_rfkill_poll(struct rfkill *rfkill, void *data)
Larry Finger93bb7f32007-10-10 22:44:22 -050049{
Johannes Berg19d337d2009-06-02 13:01:37 +020050 struct b43legacy_wldev *dev = data;
Larry Finger93bb7f32007-10-10 22:44:22 -050051 struct b43legacy_wl *wl = dev->wl;
Larry Finger6be50832007-10-10 22:48:17 -050052 bool enabled;
Larry Finger93bb7f32007-10-10 22:44:22 -050053
Larry Finger6be50832007-10-10 22:48:17 -050054 mutex_lock(&wl->mutex);
Larry Finger4ad36d72007-12-16 19:21:06 +010055 if (unlikely(b43legacy_status(dev) < B43legacy_STAT_INITIALIZED)) {
56 mutex_unlock(&wl->mutex);
57 return;
58 }
Larry Finger6be50832007-10-10 22:48:17 -050059 enabled = b43legacy_is_hw_radio_enabled(dev);
60 if (unlikely(enabled != dev->radio_hw_enable)) {
61 dev->radio_hw_enable = enabled;
62 b43legacyinfo(wl, "Radio hardware status changed to %s\n",
63 enabled ? "ENABLED" : "DISABLED");
Johannes Berg19d337d2009-06-02 13:01:37 +020064 enabled = !rfkill_set_hw_state(rfkill, !enabled);
65 if (enabled != dev->phy.radio_on) {
66 if (enabled)
67 b43legacy_radio_turn_on(dev);
68 else
69 b43legacy_radio_turn_off(dev, 0);
70 }
Stefano Briviodb9683f2007-11-06 22:48:45 +010071 }
72 mutex_unlock(&wl->mutex);
Larry Finger93bb7f32007-10-10 22:44:22 -050073}
74
75/* Called when the RFKILL toggled in software.
76 * This is called without locking. */
Johannes Berg19d337d2009-06-02 13:01:37 +020077static int b43legacy_rfkill_soft_set(void *data, bool blocked)
Larry Finger93bb7f32007-10-10 22:44:22 -050078{
79 struct b43legacy_wldev *dev = data;
80 struct b43legacy_wl *wl = dev->wl;
Johannes Berg19d337d2009-06-02 13:01:37 +020081 int ret = -EINVAL;
Larry Finger93bb7f32007-10-10 22:44:22 -050082
Stefano Briviodb9683f2007-11-06 22:48:45 +010083 if (!wl->rfkill.registered)
Johannes Berg19d337d2009-06-02 13:01:37 +020084 return -EINVAL;
Larry Finger93bb7f32007-10-10 22:44:22 -050085
Stefano Briviodb9683f2007-11-06 22:48:45 +010086 mutex_lock(&wl->mutex);
Larry Finger4ad36d72007-12-16 19:21:06 +010087 if (b43legacy_status(dev) < B43legacy_STAT_INITIALIZED)
88 goto out_unlock;
Johannes Berg19d337d2009-06-02 13:01:37 +020089
90 if (!dev->radio_hw_enable)
91 goto out_unlock;
92
93 if (!blocked != dev->phy.radio_on) {
94 if (!blocked)
Larry Finger93bb7f32007-10-10 22:44:22 -050095 b43legacy_radio_turn_on(dev);
Johannes Berg19d337d2009-06-02 13:01:37 +020096 else
Larry Finger93bb7f32007-10-10 22:44:22 -050097 b43legacy_radio_turn_off(dev, 0);
Larry Finger93bb7f32007-10-10 22:44:22 -050098 }
Johannes Berg19d337d2009-06-02 13:01:37 +020099 ret = 0;
Larry Finger93bb7f32007-10-10 22:44:22 -0500100
101out_unlock:
102 mutex_unlock(&wl->mutex);
Johannes Berg19d337d2009-06-02 13:01:37 +0200103 return ret;
Larry Finger93bb7f32007-10-10 22:44:22 -0500104}
105
Johannes Berg19d337d2009-06-02 13:01:37 +0200106const char *b43legacy_rfkill_led_name(struct b43legacy_wldev *dev)
Larry Finger93bb7f32007-10-10 22:44:22 -0500107{
Larry Finger4ad36d72007-12-16 19:21:06 +0100108 struct b43legacy_rfkill *rfk = &(dev->wl->rfkill);
Larry Finger93bb7f32007-10-10 22:44:22 -0500109
Larry Finger4ad36d72007-12-16 19:21:06 +0100110 if (!rfk->registered)
Larry Finger93bb7f32007-10-10 22:44:22 -0500111 return NULL;
Johannes Berg19d337d2009-06-02 13:01:37 +0200112 return rfkill_get_led_trigger_name(rfk->rfkill);
Larry Finger93bb7f32007-10-10 22:44:22 -0500113}
114
Johannes Berg19d337d2009-06-02 13:01:37 +0200115static const struct rfkill_ops b43legacy_rfkill_ops = {
116 .set_block = b43legacy_rfkill_soft_set,
117 .poll = b43legacy_rfkill_poll,
118};
119
Larry Finger93bb7f32007-10-10 22:44:22 -0500120void b43legacy_rfkill_init(struct b43legacy_wldev *dev)
121{
122 struct b43legacy_wl *wl = dev->wl;
123 struct b43legacy_rfkill *rfk = &(wl->rfkill);
124 int err;
125
Larry Finger4ad36d72007-12-16 19:21:06 +0100126 rfk->registered = 0;
Larry Finger6be50832007-10-10 22:48:17 -0500127
Larry Finger4ad36d72007-12-16 19:21:06 +0100128 snprintf(rfk->name, sizeof(rfk->name),
129 "b43legacy-%s", wiphy_name(wl->hw->wiphy));
Johannes Berg19d337d2009-06-02 13:01:37 +0200130 rfk->rfkill = rfkill_alloc(rfk->name,
131 dev->dev->dev,
132 RFKILL_TYPE_WLAN,
133 &b43legacy_rfkill_ops, dev);
134 if (!rfk->rfkill)
135 goto out_error;
Larry Finger4ad36d72007-12-16 19:21:06 +0100136
137 err = rfkill_register(rfk->rfkill);
138 if (err)
Johannes Berg19d337d2009-06-02 13:01:37 +0200139 goto err_free;
Larry Finger4ad36d72007-12-16 19:21:06 +0100140
141 rfk->registered = 1;
142
143 return;
Johannes Berg19d337d2009-06-02 13:01:37 +0200144 err_free:
145 rfkill_destroy(rfk->rfkill);
146 out_error:
Larry Finger4ad36d72007-12-16 19:21:06 +0100147 rfk->registered = 0;
148 b43legacywarn(wl, "RF-kill button init failed\n");
Larry Finger6be50832007-10-10 22:48:17 -0500149}
150
Larry Finger4ad36d72007-12-16 19:21:06 +0100151void b43legacy_rfkill_exit(struct b43legacy_wldev *dev)
Larry Finger6be50832007-10-10 22:48:17 -0500152{
153 struct b43legacy_rfkill *rfk = &(dev->wl->rfkill);
154
Larry Finger4ad36d72007-12-16 19:21:06 +0100155 if (!rfk->registered)
156 return;
157 rfk->registered = 0;
158
Larry Finger4ad36d72007-12-16 19:21:06 +0100159 rfkill_unregister(rfk->rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +0200160 rfkill_destroy(rfk->rfkill);
Larry Finger93bb7f32007-10-10 22:44:22 -0500161 rfk->rfkill = NULL;
162}
Larry Finger4ad36d72007-12-16 19:21:06 +0100163