blob: 4cca203992e83490444868ee0d18358e063036a4 [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"
27
Larry Finger1a8d1222007-12-14 13:59:11 +010028#include <linux/kmod.h>
29
Michael Buesch8e9f7522007-09-27 21:35:34 +020030
Michael Buesch42bb4cd2007-09-28 14:22:33 +020031/* Returns TRUE, if the radio is enabled in hardware. */
32static bool b43_is_hw_radio_enabled(struct b43_wldev *dev)
Michael Buesch8e9f7522007-09-27 21:35:34 +020033{
Michael Buesch42bb4cd2007-09-28 14:22:33 +020034 if (dev->phy.rev >= 3) {
35 if (!(b43_read32(dev, B43_MMIO_RADIO_HWENABLED_HI)
36 & B43_MMIO_RADIO_HWENABLED_HI_MASK))
37 return 1;
38 } else {
39 if (b43_read16(dev, B43_MMIO_RADIO_HWENABLED_LO)
40 & B43_MMIO_RADIO_HWENABLED_LO_MASK)
41 return 1;
Michael Buesch8e9f7522007-09-27 21:35:34 +020042 }
Michael Buesch42bb4cd2007-09-28 14:22:33 +020043 return 0;
Michael Buesch8e9f7522007-09-27 21:35:34 +020044}
45
Michael Buesch42bb4cd2007-09-28 14:22:33 +020046/* The poll callback for the hardware button. */
47static void b43_rfkill_poll(struct input_polled_dev *poll_dev)
Michael Buesch8e9f7522007-09-27 21:35:34 +020048{
Michael Buesch42bb4cd2007-09-28 14:22:33 +020049 struct b43_wldev *dev = poll_dev->private;
Michael Buesch8e9f7522007-09-27 21:35:34 +020050 struct b43_wl *wl = dev->wl;
Michael Buesch42bb4cd2007-09-28 14:22:33 +020051 bool enabled;
Michael Buesch35c7e662007-11-03 14:34:32 +010052 bool report_change = 0;
Michael Buesch8e9f7522007-09-27 21:35:34 +020053
Michael Buesch42bb4cd2007-09-28 14:22:33 +020054 mutex_lock(&wl->mutex);
Larry Finger1a8d1222007-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;
Michael Buesch35c7e662007-11-03 14:34:32 +010062 report_change = 1;
Michael Buesch42bb4cd2007-09-28 14:22:33 +020063 b43info(wl, "Radio hardware status changed to %s\n",
64 enabled ? "ENABLED" : "DISABLED");
Michael Buesch35c7e662007-11-03 14:34:32 +010065 }
66 mutex_unlock(&wl->mutex);
67
Larry Finger1a8d1222007-12-14 13:59:11 +010068 /* send the radio switch event to the system - note both a key press
69 * and a release are required */
70 if (unlikely(report_change)) {
71 input_report_key(poll_dev->input, KEY_WLAN, 1);
72 input_report_key(poll_dev->input, KEY_WLAN, 0);
73 }
Michael Buesch8e9f7522007-09-27 21:35:34 +020074}
75
Michael Buesch80fda032007-10-28 17:27:10 +010076/* Called when the RFKILL toggled in software. */
Michael Buesch8e9f7522007-09-27 21:35:34 +020077static int b43_rfkill_soft_toggle(void *data, enum rfkill_state state)
78{
79 struct b43_wldev *dev = data;
80 struct b43_wl *wl = dev->wl;
Larry Finger1a8d1222007-12-14 13:59:11 +010081 int err = -EBUSY;
Michael Buesch8e9f7522007-09-27 21:35:34 +020082
Michael Buesch35c7e662007-11-03 14:34:32 +010083 if (!wl->rfkill.registered)
84 return 0;
Michael Buesch8e9f7522007-09-27 21:35:34 +020085
Michael Buesch35c7e662007-11-03 14:34:32 +010086 mutex_lock(&wl->mutex);
Larry Finger1a8d1222007-12-14 13:59:11 +010087 if (b43_status(dev) < B43_STAT_INITIALIZED)
88 goto out_unlock;
89 err = 0;
Michael Buesch8e9f7522007-09-27 21:35:34 +020090 switch (state) {
John W. Linvilleff28bd92008-06-27 10:27:47 -040091 case RFKILL_STATE_UNBLOCKED:
Michael Buesch8e9f7522007-09-27 21:35:34 +020092 if (!dev->radio_hw_enable) {
93 /* No luck. We can't toggle the hardware RF-kill
94 * button from software. */
95 err = -EBUSY;
96 goto out_unlock;
97 }
98 if (!dev->phy.radio_on)
99 b43_radio_turn_on(dev);
100 break;
John W. Linvilleff28bd92008-06-27 10:27:47 -0400101 case RFKILL_STATE_SOFT_BLOCKED:
Michael Buesch8e9f7522007-09-27 21:35:34 +0200102 if (dev->phy.radio_on)
103 b43_radio_turn_off(dev, 0);
104 break;
John W. Linvilleff28bd92008-06-27 10:27:47 -0400105 default:
106 b43warn(wl, "Received unexpected rfkill state %d.\n", state);
107 break;
Michael Buesch8e9f7522007-09-27 21:35:34 +0200108 }
Michael Buesch8e9f7522007-09-27 21:35:34 +0200109out_unlock:
110 mutex_unlock(&wl->mutex);
111
112 return err;
113}
114
115char * b43_rfkill_led_name(struct b43_wldev *dev)
116{
Michael Buesch35c7e662007-11-03 14:34:32 +0100117 struct b43_rfkill *rfk = &(dev->wl->rfkill);
Michael Buesch8e9f7522007-09-27 21:35:34 +0200118
Michael Buesch35c7e662007-11-03 14:34:32 +0100119 if (!rfk->registered)
Michael Buesch8e9f7522007-09-27 21:35:34 +0200120 return NULL;
Michael Buesch35c7e662007-11-03 14:34:32 +0100121 return rfkill_get_led_name(rfk->rfkill);
Michael Buesch8e9f7522007-09-27 21:35:34 +0200122}
123
124void b43_rfkill_init(struct b43_wldev *dev)
125{
126 struct b43_wl *wl = dev->wl;
127 struct b43_rfkill *rfk = &(wl->rfkill);
128 int err;
129
Michael Buesch35c7e662007-11-03 14:34:32 +0100130 rfk->registered = 0;
Michael Buesch42bb4cd2007-09-28 14:22:33 +0200131
132 rfk->rfkill = rfkill_allocate(dev->dev->dev, RFKILL_TYPE_WLAN);
Michael Buesch35c7e662007-11-03 14:34:32 +0100133 if (!rfk->rfkill)
134 goto out_error;
135 snprintf(rfk->name, sizeof(rfk->name),
136 "b43-%s", wiphy_name(wl->hw->wiphy));
Michael Buesch42bb4cd2007-09-28 14:22:33 +0200137 rfk->rfkill->name = rfk->name;
138 rfk->rfkill->state = RFKILL_STATE_ON;
139 rfk->rfkill->data = dev;
140 rfk->rfkill->toggle_radio = b43_rfkill_soft_toggle;
141 rfk->rfkill->user_claim_unsupported = 1;
142
143 rfk->poll_dev = input_allocate_polled_device();
Stefano Brivioa38db5b2008-01-13 18:30:14 +0100144 if (!rfk->poll_dev) {
145 rfkill_free(rfk->rfkill);
146 goto err_freed_rfk;
147 }
148
Michael Buesch35c7e662007-11-03 14:34:32 +0100149 rfk->poll_dev->private = dev;
150 rfk->poll_dev->poll = b43_rfkill_poll;
151 rfk->poll_dev->poll_interval = 1000; /* msecs */
152
Larry Finger1a8d1222007-12-14 13:59:11 +0100153 rfk->poll_dev->input->name = rfk->name;
154 rfk->poll_dev->input->id.bustype = BUS_HOST;
155 rfk->poll_dev->input->id.vendor = dev->dev->bus->boardinfo.vendor;
156 rfk->poll_dev->input->evbit[0] = BIT(EV_KEY);
157 set_bit(KEY_WLAN, rfk->poll_dev->input->keybit);
158
Michael Buesch35c7e662007-11-03 14:34:32 +0100159 err = rfkill_register(rfk->rfkill);
160 if (err)
161 goto err_free_polldev;
Larry Finger1a8d1222007-12-14 13:59:11 +0100162
163#ifdef CONFIG_RFKILL_INPUT_MODULE
164 /* B43 RF-kill isn't useful without the rfkill-input subsystem.
165 * Try to load the module. */
166 err = request_module("rfkill-input");
167 if (err)
168 b43warn(wl, "Failed to load the rfkill-input module. "
169 "The built-in radio LED will not work.\n");
170#endif /* CONFIG_RFKILL_INPUT */
171
Michael Buesch35c7e662007-11-03 14:34:32 +0100172 err = input_register_polled_device(rfk->poll_dev);
173 if (err)
174 goto err_unreg_rfk;
175
176 rfk->registered = 1;
177
178 return;
179err_unreg_rfk:
180 rfkill_unregister(rfk->rfkill);
181err_free_polldev:
182 input_free_polled_device(rfk->poll_dev);
183 rfk->poll_dev = NULL;
Stefano Brivioa38db5b2008-01-13 18:30:14 +0100184err_freed_rfk:
Michael Buesch35c7e662007-11-03 14:34:32 +0100185 rfk->rfkill = NULL;
186out_error:
187 rfk->registered = 0;
188 b43warn(wl, "RF-kill button init failed\n");
Michael Buesch42bb4cd2007-09-28 14:22:33 +0200189}
190
Michael Buesch35c7e662007-11-03 14:34:32 +0100191void b43_rfkill_exit(struct b43_wldev *dev)
Michael Buesch42bb4cd2007-09-28 14:22:33 +0200192{
193 struct b43_rfkill *rfk = &(dev->wl->rfkill);
194
Michael Buesch35c7e662007-11-03 14:34:32 +0100195 if (!rfk->registered)
196 return;
197 rfk->registered = 0;
198
199 input_unregister_polled_device(rfk->poll_dev);
200 rfkill_unregister(rfk->rfkill);
Michael Buesch42bb4cd2007-09-28 14:22:33 +0200201 input_free_polled_device(rfk->poll_dev);
202 rfk->poll_dev = NULL;
Michael Buesch8e9f7522007-09-27 21:35:34 +0200203 rfk->rfkill = NULL;
204}