blob: 4b0c7d27a51f90374887d20a1974191f49c40bef [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. */
48static void b43legacy_rfkill_poll(struct input_polled_dev *poll_dev)
Larry Finger93bb7f32007-10-10 22:44:22 -050049{
Larry Finger6be50832007-10-10 22:48:17 -050050 struct b43legacy_wldev *dev = poll_dev->private;
Larry Finger93bb7f32007-10-10 22:44:22 -050051 struct b43legacy_wl *wl = dev->wl;
Larry Finger6be50832007-10-10 22:48:17 -050052 bool enabled;
Stefano Briviodb9683f2007-11-06 22:48:45 +010053 bool report_change = 0;
Larry Finger93bb7f32007-10-10 22:44:22 -050054
Larry Finger6be50832007-10-10 22:48:17 -050055 mutex_lock(&wl->mutex);
Larry Finger4ad36d72007-12-16 19:21:06 +010056 if (unlikely(b43legacy_status(dev) < B43legacy_STAT_INITIALIZED)) {
57 mutex_unlock(&wl->mutex);
58 return;
59 }
Larry Finger6be50832007-10-10 22:48:17 -050060 enabled = b43legacy_is_hw_radio_enabled(dev);
61 if (unlikely(enabled != dev->radio_hw_enable)) {
62 dev->radio_hw_enable = enabled;
Stefano Briviodb9683f2007-11-06 22:48:45 +010063 report_change = 1;
Larry Finger6be50832007-10-10 22:48:17 -050064 b43legacyinfo(wl, "Radio hardware status changed to %s\n",
65 enabled ? "ENABLED" : "DISABLED");
Stefano Briviodb9683f2007-11-06 22:48:45 +010066 }
67 mutex_unlock(&wl->mutex);
68
Larry Finger4ad36d72007-12-16 19:21:06 +010069 /* send the radio switch event to the system - note both a key press
70 * and a release are required */
71 if (unlikely(report_change)) {
72 input_report_key(poll_dev->input, KEY_WLAN, 1);
73 input_report_key(poll_dev->input, KEY_WLAN, 0);
74 }
Larry Finger93bb7f32007-10-10 22:44:22 -050075}
76
77/* Called when the RFKILL toggled in software.
78 * This is called without locking. */
79static int b43legacy_rfkill_soft_toggle(void *data, enum rfkill_state state)
80{
81 struct b43legacy_wldev *dev = data;
82 struct b43legacy_wl *wl = dev->wl;
Larry Finger4ad36d72007-12-16 19:21:06 +010083 int err = -EBUSY;
Larry Finger93bb7f32007-10-10 22:44:22 -050084
Stefano Briviodb9683f2007-11-06 22:48:45 +010085 if (!wl->rfkill.registered)
86 return 0;
Larry Finger93bb7f32007-10-10 22:44:22 -050087
Stefano Briviodb9683f2007-11-06 22:48:45 +010088 mutex_lock(&wl->mutex);
Larry Finger4ad36d72007-12-16 19:21:06 +010089 if (b43legacy_status(dev) < B43legacy_STAT_INITIALIZED)
90 goto out_unlock;
91 err = 0;
Larry Finger93bb7f32007-10-10 22:44:22 -050092 switch (state) {
John W. Linvilleff28bd92008-06-27 10:27:47 -040093 case RFKILL_STATE_UNBLOCKED:
Larry Finger93bb7f32007-10-10 22:44:22 -050094 if (!dev->radio_hw_enable) {
95 /* No luck. We can't toggle the hardware RF-kill
96 * button from software. */
97 err = -EBUSY;
98 goto out_unlock;
99 }
100 if (!dev->phy.radio_on)
101 b43legacy_radio_turn_on(dev);
102 break;
John W. Linvilleff28bd92008-06-27 10:27:47 -0400103 case RFKILL_STATE_SOFT_BLOCKED:
Larry Finger93bb7f32007-10-10 22:44:22 -0500104 if (dev->phy.radio_on)
105 b43legacy_radio_turn_off(dev, 0);
106 break;
John W. Linvilleff28bd92008-06-27 10:27:47 -0400107 default:
108 b43legacywarn(wl, "Received unexpected rfkill state %d.\n",
109 state);
110 break;
Larry Finger93bb7f32007-10-10 22:44:22 -0500111 }
112
113out_unlock:
114 mutex_unlock(&wl->mutex);
115
116 return err;
117}
118
Jeff Garzik93a3b602007-11-23 21:50:20 -0500119char *b43legacy_rfkill_led_name(struct b43legacy_wldev *dev)
Larry Finger93bb7f32007-10-10 22:44:22 -0500120{
Larry Finger4ad36d72007-12-16 19:21:06 +0100121 struct b43legacy_rfkill *rfk = &(dev->wl->rfkill);
Larry Finger93bb7f32007-10-10 22:44:22 -0500122
Larry Finger4ad36d72007-12-16 19:21:06 +0100123 if (!rfk->registered)
Larry Finger93bb7f32007-10-10 22:44:22 -0500124 return NULL;
Larry Finger4ad36d72007-12-16 19:21:06 +0100125 return rfkill_get_led_name(rfk->rfkill);
Larry Finger93bb7f32007-10-10 22:44:22 -0500126}
127
128void b43legacy_rfkill_init(struct b43legacy_wldev *dev)
129{
130 struct b43legacy_wl *wl = dev->wl;
131 struct b43legacy_rfkill *rfk = &(wl->rfkill);
132 int err;
133
Larry Finger4ad36d72007-12-16 19:21:06 +0100134 rfk->registered = 0;
Larry Finger6be50832007-10-10 22:48:17 -0500135
136 rfk->rfkill = rfkill_allocate(dev->dev->dev, RFKILL_TYPE_WLAN);
Larry Finger4ad36d72007-12-16 19:21:06 +0100137 if (!rfk->rfkill)
138 goto out_error;
139 snprintf(rfk->name, sizeof(rfk->name),
140 "b43legacy-%s", wiphy_name(wl->hw->wiphy));
Larry Finger6be50832007-10-10 22:48:17 -0500141 rfk->rfkill->name = rfk->name;
Adel Gadllahf97d1f42008-06-28 14:20:40 +0200142 rfk->rfkill->state = RFKILL_STATE_UNBLOCKED;
Larry Finger6be50832007-10-10 22:48:17 -0500143 rfk->rfkill->data = dev;
144 rfk->rfkill->toggle_radio = b43legacy_rfkill_soft_toggle;
Larry Finger6be50832007-10-10 22:48:17 -0500145
146 rfk->poll_dev = input_allocate_polled_device();
Stefano Brivio222b01b2008-01-13 18:35:52 +0100147 if (!rfk->poll_dev) {
148 rfkill_free(rfk->rfkill);
149 goto err_freed_rfk;
150 }
151
Larry Finger4ad36d72007-12-16 19:21:06 +0100152 rfk->poll_dev->private = dev;
153 rfk->poll_dev->poll = b43legacy_rfkill_poll;
154 rfk->poll_dev->poll_interval = 1000; /* msecs */
155
156 rfk->poll_dev->input->name = rfk->name;
157 rfk->poll_dev->input->id.bustype = BUS_HOST;
158 rfk->poll_dev->input->id.vendor = dev->dev->bus->boardinfo.vendor;
159 rfk->poll_dev->input->evbit[0] = BIT(EV_KEY);
160 set_bit(KEY_WLAN, rfk->poll_dev->input->keybit);
161
162 err = rfkill_register(rfk->rfkill);
163 if (err)
164 goto err_free_polldev;
165
166#ifdef CONFIG_RFKILL_INPUT_MODULE
167 /* B43legacy RF-kill isn't useful without the rfkill-input subsystem.
168 * Try to load the module. */
169 err = request_module("rfkill-input");
170 if (err)
171 b43legacywarn(wl, "Failed to load the rfkill-input module."
172 "The built-in radio LED will not work.\n");
173#endif /* CONFIG_RFKILL_INPUT */
174
175 err = input_register_polled_device(rfk->poll_dev);
176 if (err)
177 goto err_unreg_rfk;
178
179 rfk->registered = 1;
180
181 return;
182err_unreg_rfk:
183 rfkill_unregister(rfk->rfkill);
184err_free_polldev:
185 input_free_polled_device(rfk->poll_dev);
186 rfk->poll_dev = NULL;
Stefano Brivio222b01b2008-01-13 18:35:52 +0100187err_freed_rfk:
Larry Finger4ad36d72007-12-16 19:21:06 +0100188 rfk->rfkill = NULL;
189out_error:
190 rfk->registered = 0;
191 b43legacywarn(wl, "RF-kill button init failed\n");
Larry Finger6be50832007-10-10 22:48:17 -0500192}
193
Larry Finger4ad36d72007-12-16 19:21:06 +0100194void b43legacy_rfkill_exit(struct b43legacy_wldev *dev)
Larry Finger6be50832007-10-10 22:48:17 -0500195{
196 struct b43legacy_rfkill *rfk = &(dev->wl->rfkill);
197
Larry Finger4ad36d72007-12-16 19:21:06 +0100198 if (!rfk->registered)
199 return;
200 rfk->registered = 0;
201
202 input_unregister_polled_device(rfk->poll_dev);
203 rfkill_unregister(rfk->rfkill);
Larry Finger6be50832007-10-10 22:48:17 -0500204 input_free_polled_device(rfk->poll_dev);
205 rfk->poll_dev = NULL;
Larry Finger93bb7f32007-10-10 22:44:22 -0500206 rfk->rfkill = NULL;
207}
Larry Finger4ad36d72007-12-16 19:21:06 +0100208