blob: 96047843cd5626c9642f2ae29ffc31377ab5dd11 [file] [log] [blame]
Michael Buesch8e9f7522007-09-27 21:35:34 +02001/*
2
3 Broadcom B43 wireless driver
4 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 "b43.h"
Michael Bueschef1a6282008-08-27 18:53:02 +020027#include "phy_common.h"
Michael Buesch8e9f7522007-09-27 21:35:34 +020028
Larry Finger1a8d12272007-12-14 13:59:11 +010029#include <linux/kmod.h>
30
Michael Buesch8e9f7522007-09-27 21:35:34 +020031
Michael Buesch42bb4cd2007-09-28 14:22:33 +020032/* Returns TRUE, if the radio is enabled in hardware. */
33static bool b43_is_hw_radio_enabled(struct b43_wldev *dev)
Michael Buesch8e9f7522007-09-27 21:35:34 +020034{
Michael Buesch42bb4cd2007-09-28 14:22:33 +020035 if (dev->phy.rev >= 3) {
36 if (!(b43_read32(dev, B43_MMIO_RADIO_HWENABLED_HI)
37 & B43_MMIO_RADIO_HWENABLED_HI_MASK))
38 return 1;
39 } else {
40 if (b43_read16(dev, B43_MMIO_RADIO_HWENABLED_LO)
41 & B43_MMIO_RADIO_HWENABLED_LO_MASK)
42 return 1;
Michael Buesch8e9f7522007-09-27 21:35:34 +020043 }
Michael Buesch42bb4cd2007-09-28 14:22:33 +020044 return 0;
Michael Buesch8e9f7522007-09-27 21:35:34 +020045}
46
Michael Buesch42bb4cd2007-09-28 14:22:33 +020047/* The poll callback for the hardware button. */
Johannes Berg19d337d2009-06-02 13:01:37 +020048static void b43_rfkill_poll(struct rfkill *rfkill, void *data)
Michael Buesch8e9f7522007-09-27 21:35:34 +020049{
Johannes Berg19d337d2009-06-02 13:01:37 +020050 struct b43_wldev *dev = data;
Michael Buesch8e9f7522007-09-27 21:35:34 +020051 struct b43_wl *wl = dev->wl;
Michael Buesch42bb4cd2007-09-28 14:22:33 +020052 bool enabled;
Michael Buesch8e9f7522007-09-27 21:35:34 +020053
Michael Buesch42bb4cd2007-09-28 14:22:33 +020054 mutex_lock(&wl->mutex);
Larry Finger1a8d12272007-12-14 13:59:11 +010055 if (unlikely(b43_status(dev) < B43_STAT_INITIALIZED)) {
56 mutex_unlock(&wl->mutex);
57 return;
58 }
Michael Buesch42bb4cd2007-09-28 14:22:33 +020059 enabled = b43_is_hw_radio_enabled(dev);
60 if (unlikely(enabled != dev->radio_hw_enable)) {
61 dev->radio_hw_enable = enabled;
62 b43info(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 b43_software_rfkill(dev, !enabled);
Michael Buesch35c7e662007-11-03 14:34:32 +010067 }
68 mutex_unlock(&wl->mutex);
Michael Buesch8e9f7522007-09-27 21:35:34 +020069}
70
Michael Buesch80fda032007-10-28 17:27:10 +010071/* Called when the RFKILL toggled in software. */
Johannes Berg19d337d2009-06-02 13:01:37 +020072static int b43_rfkill_soft_set(void *data, bool blocked)
Michael Buesch8e9f7522007-09-27 21:35:34 +020073{
74 struct b43_wldev *dev = data;
75 struct b43_wl *wl = dev->wl;
Johannes Berg19d337d2009-06-02 13:01:37 +020076 int err = -EINVAL;
Michael Buesch8e9f7522007-09-27 21:35:34 +020077
Johannes Berg19d337d2009-06-02 13:01:37 +020078 if (WARN_ON(!wl->rfkill.registered))
79 return -EINVAL;
Michael Buesch8e9f7522007-09-27 21:35:34 +020080
Michael Buesch35c7e662007-11-03 14:34:32 +010081 mutex_lock(&wl->mutex);
Johannes Berg19d337d2009-06-02 13:01:37 +020082
Larry Finger1a8d12272007-12-14 13:59:11 +010083 if (b43_status(dev) < B43_STAT_INITIALIZED)
84 goto out_unlock;
Johannes Berg19d337d2009-06-02 13:01:37 +020085
86 if (!dev->radio_hw_enable)
87 goto out_unlock;
88
89 if (!blocked != dev->phy.radio_on)
90 b43_software_rfkill(dev, blocked);
Larry Finger1a8d12272007-12-14 13:59:11 +010091 err = 0;
Michael Buesch8e9f7522007-09-27 21:35:34 +020092out_unlock:
93 mutex_unlock(&wl->mutex);
Michael Buesch8e9f7522007-09-27 21:35:34 +020094 return err;
95}
96
Johannes Berg19d337d2009-06-02 13:01:37 +020097const char *b43_rfkill_led_name(struct b43_wldev *dev)
Michael Buesch8e9f7522007-09-27 21:35:34 +020098{
Michael Buesch35c7e662007-11-03 14:34:32 +010099 struct b43_rfkill *rfk = &(dev->wl->rfkill);
Michael Buesch8e9f7522007-09-27 21:35:34 +0200100
Michael Buesch35c7e662007-11-03 14:34:32 +0100101 if (!rfk->registered)
Michael Buesch8e9f7522007-09-27 21:35:34 +0200102 return NULL;
Johannes Berg19d337d2009-06-02 13:01:37 +0200103 return rfkill_get_led_trigger_name(rfk->rfkill);
Michael Buesch8e9f7522007-09-27 21:35:34 +0200104}
105
Johannes Berg19d337d2009-06-02 13:01:37 +0200106static const struct rfkill_ops b43_rfkill_ops = {
107 .set_block = b43_rfkill_soft_set,
108 .poll = b43_rfkill_poll,
109};
110
Michael Buesch8e9f7522007-09-27 21:35:34 +0200111void b43_rfkill_init(struct b43_wldev *dev)
112{
113 struct b43_wl *wl = dev->wl;
114 struct b43_rfkill *rfk = &(wl->rfkill);
115 int err;
116
Michael Buesch35c7e662007-11-03 14:34:32 +0100117 rfk->registered = 0;
Michael Buesch42bb4cd2007-09-28 14:22:33 +0200118
Michael Buesch35c7e662007-11-03 14:34:32 +0100119 snprintf(rfk->name, sizeof(rfk->name),
120 "b43-%s", wiphy_name(wl->hw->wiphy));
Michael Buesch42bb4cd2007-09-28 14:22:33 +0200121
Johannes Berg19d337d2009-06-02 13:01:37 +0200122 rfk->rfkill = rfkill_alloc(rfk->name,
123 dev->dev->dev,
124 RFKILL_TYPE_WLAN,
125 &b43_rfkill_ops, dev);
126 if (!rfk->rfkill)
127 goto out_error;
Larry Finger1a8d12272007-12-14 13:59:11 +0100128
Michael Buesch35c7e662007-11-03 14:34:32 +0100129 err = rfkill_register(rfk->rfkill);
130 if (err)
Johannes Berg19d337d2009-06-02 13:01:37 +0200131 goto err_free;
Michael Buesch35c7e662007-11-03 14:34:32 +0100132
133 rfk->registered = 1;
134
135 return;
Johannes Berg19d337d2009-06-02 13:01:37 +0200136 err_free:
137 rfkill_destroy(rfk->rfkill);
138 out_error:
Michael Buesch35c7e662007-11-03 14:34:32 +0100139 rfk->registered = 0;
140 b43warn(wl, "RF-kill button init failed\n");
Michael Buesch42bb4cd2007-09-28 14:22:33 +0200141}
142
Michael Buesch35c7e662007-11-03 14:34:32 +0100143void b43_rfkill_exit(struct b43_wldev *dev)
Michael Buesch42bb4cd2007-09-28 14:22:33 +0200144{
145 struct b43_rfkill *rfk = &(dev->wl->rfkill);
146
Michael Buesch35c7e662007-11-03 14:34:32 +0100147 if (!rfk->registered)
148 return;
149 rfk->registered = 0;
150
Michael Buesch35c7e662007-11-03 14:34:32 +0100151 rfkill_unregister(rfk->rfkill);
Johannes Berg19d337d2009-06-02 13:01:37 +0200152 rfkill_destroy(rfk->rfkill);
Michael Buesch8e9f7522007-09-27 21:35:34 +0200153 rfk->rfkill = NULL;
154}