Michael Buesch | 8e9f752 | 2007-09-27 21:35:34 +0200 | [diff] [blame] | 1 | /* |
| 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 Finger | 1a8d122 | 2007-12-14 13:59:11 +0100 | [diff] [blame] | 28 | #include <linux/kmod.h> |
| 29 | |
Michael Buesch | 8e9f752 | 2007-09-27 21:35:34 +0200 | [diff] [blame] | 30 | |
Michael Buesch | 42bb4cd | 2007-09-28 14:22:33 +0200 | [diff] [blame] | 31 | /* Returns TRUE, if the radio is enabled in hardware. */ |
| 32 | static bool b43_is_hw_radio_enabled(struct b43_wldev *dev) |
Michael Buesch | 8e9f752 | 2007-09-27 21:35:34 +0200 | [diff] [blame] | 33 | { |
Michael Buesch | 42bb4cd | 2007-09-28 14:22:33 +0200 | [diff] [blame] | 34 | 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 Buesch | 8e9f752 | 2007-09-27 21:35:34 +0200 | [diff] [blame] | 42 | } |
Michael Buesch | 42bb4cd | 2007-09-28 14:22:33 +0200 | [diff] [blame] | 43 | return 0; |
Michael Buesch | 8e9f752 | 2007-09-27 21:35:34 +0200 | [diff] [blame] | 44 | } |
| 45 | |
Adel Gadllah | bc19d6e | 2008-06-29 19:20:21 +0200 | [diff] [blame] | 46 | /* Update the rfkill state */ |
| 47 | static void b43_rfkill_update_state(struct b43_wldev *dev) |
| 48 | { |
| 49 | struct b43_rfkill *rfk = &(dev->wl->rfkill); |
| 50 | |
| 51 | if (!dev->radio_hw_enable) { |
| 52 | rfk->rfkill->state = RFKILL_STATE_HARD_BLOCKED; |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | if (!dev->phy.radio_on) |
| 57 | rfk->rfkill->state = RFKILL_STATE_SOFT_BLOCKED; |
| 58 | else |
| 59 | rfk->rfkill->state = RFKILL_STATE_UNBLOCKED; |
| 60 | |
| 61 | } |
| 62 | |
Michael Buesch | 42bb4cd | 2007-09-28 14:22:33 +0200 | [diff] [blame] | 63 | /* The poll callback for the hardware button. */ |
| 64 | static void b43_rfkill_poll(struct input_polled_dev *poll_dev) |
Michael Buesch | 8e9f752 | 2007-09-27 21:35:34 +0200 | [diff] [blame] | 65 | { |
Michael Buesch | 42bb4cd | 2007-09-28 14:22:33 +0200 | [diff] [blame] | 66 | struct b43_wldev *dev = poll_dev->private; |
Michael Buesch | 8e9f752 | 2007-09-27 21:35:34 +0200 | [diff] [blame] | 67 | struct b43_wl *wl = dev->wl; |
Michael Buesch | 42bb4cd | 2007-09-28 14:22:33 +0200 | [diff] [blame] | 68 | bool enabled; |
Michael Buesch | 35c7e66 | 2007-11-03 14:34:32 +0100 | [diff] [blame] | 69 | bool report_change = 0; |
Michael Buesch | 8e9f752 | 2007-09-27 21:35:34 +0200 | [diff] [blame] | 70 | |
Michael Buesch | 42bb4cd | 2007-09-28 14:22:33 +0200 | [diff] [blame] | 71 | mutex_lock(&wl->mutex); |
Larry Finger | 1a8d122 | 2007-12-14 13:59:11 +0100 | [diff] [blame] | 72 | if (unlikely(b43_status(dev) < B43_STAT_INITIALIZED)) { |
| 73 | mutex_unlock(&wl->mutex); |
| 74 | return; |
| 75 | } |
Michael Buesch | 42bb4cd | 2007-09-28 14:22:33 +0200 | [diff] [blame] | 76 | enabled = b43_is_hw_radio_enabled(dev); |
| 77 | if (unlikely(enabled != dev->radio_hw_enable)) { |
| 78 | dev->radio_hw_enable = enabled; |
Michael Buesch | 35c7e66 | 2007-11-03 14:34:32 +0100 | [diff] [blame] | 79 | report_change = 1; |
Adel Gadllah | bc19d6e | 2008-06-29 19:20:21 +0200 | [diff] [blame] | 80 | b43_rfkill_update_state(dev); |
Michael Buesch | 42bb4cd | 2007-09-28 14:22:33 +0200 | [diff] [blame] | 81 | b43info(wl, "Radio hardware status changed to %s\n", |
| 82 | enabled ? "ENABLED" : "DISABLED"); |
Michael Buesch | 35c7e66 | 2007-11-03 14:34:32 +0100 | [diff] [blame] | 83 | } |
| 84 | mutex_unlock(&wl->mutex); |
| 85 | |
Larry Finger | 1a8d122 | 2007-12-14 13:59:11 +0100 | [diff] [blame] | 86 | /* send the radio switch event to the system - note both a key press |
| 87 | * and a release are required */ |
| 88 | if (unlikely(report_change)) { |
| 89 | input_report_key(poll_dev->input, KEY_WLAN, 1); |
| 90 | input_report_key(poll_dev->input, KEY_WLAN, 0); |
| 91 | } |
Michael Buesch | 8e9f752 | 2007-09-27 21:35:34 +0200 | [diff] [blame] | 92 | } |
| 93 | |
Michael Buesch | 80fda03 | 2007-10-28 17:27:10 +0100 | [diff] [blame] | 94 | /* Called when the RFKILL toggled in software. */ |
Michael Buesch | 8e9f752 | 2007-09-27 21:35:34 +0200 | [diff] [blame] | 95 | static int b43_rfkill_soft_toggle(void *data, enum rfkill_state state) |
| 96 | { |
| 97 | struct b43_wldev *dev = data; |
| 98 | struct b43_wl *wl = dev->wl; |
Larry Finger | 1a8d122 | 2007-12-14 13:59:11 +0100 | [diff] [blame] | 99 | int err = -EBUSY; |
Michael Buesch | 8e9f752 | 2007-09-27 21:35:34 +0200 | [diff] [blame] | 100 | |
Michael Buesch | 35c7e66 | 2007-11-03 14:34:32 +0100 | [diff] [blame] | 101 | if (!wl->rfkill.registered) |
| 102 | return 0; |
Michael Buesch | 8e9f752 | 2007-09-27 21:35:34 +0200 | [diff] [blame] | 103 | |
Michael Buesch | 35c7e66 | 2007-11-03 14:34:32 +0100 | [diff] [blame] | 104 | mutex_lock(&wl->mutex); |
Larry Finger | 1a8d122 | 2007-12-14 13:59:11 +0100 | [diff] [blame] | 105 | if (b43_status(dev) < B43_STAT_INITIALIZED) |
| 106 | goto out_unlock; |
| 107 | err = 0; |
Michael Buesch | 8e9f752 | 2007-09-27 21:35:34 +0200 | [diff] [blame] | 108 | switch (state) { |
John W. Linville | ff28bd9 | 2008-06-27 10:27:47 -0400 | [diff] [blame] | 109 | case RFKILL_STATE_UNBLOCKED: |
Michael Buesch | 8e9f752 | 2007-09-27 21:35:34 +0200 | [diff] [blame] | 110 | if (!dev->radio_hw_enable) { |
| 111 | /* No luck. We can't toggle the hardware RF-kill |
| 112 | * button from software. */ |
| 113 | err = -EBUSY; |
| 114 | goto out_unlock; |
| 115 | } |
| 116 | if (!dev->phy.radio_on) |
| 117 | b43_radio_turn_on(dev); |
| 118 | break; |
John W. Linville | ff28bd9 | 2008-06-27 10:27:47 -0400 | [diff] [blame] | 119 | case RFKILL_STATE_SOFT_BLOCKED: |
Michael Buesch | 8e9f752 | 2007-09-27 21:35:34 +0200 | [diff] [blame] | 120 | if (dev->phy.radio_on) |
| 121 | b43_radio_turn_off(dev, 0); |
| 122 | break; |
John W. Linville | ff28bd9 | 2008-06-27 10:27:47 -0400 | [diff] [blame] | 123 | default: |
| 124 | b43warn(wl, "Received unexpected rfkill state %d.\n", state); |
| 125 | break; |
Michael Buesch | 8e9f752 | 2007-09-27 21:35:34 +0200 | [diff] [blame] | 126 | } |
Michael Buesch | 8e9f752 | 2007-09-27 21:35:34 +0200 | [diff] [blame] | 127 | out_unlock: |
| 128 | mutex_unlock(&wl->mutex); |
| 129 | |
| 130 | return err; |
| 131 | } |
| 132 | |
| 133 | char * b43_rfkill_led_name(struct b43_wldev *dev) |
| 134 | { |
Michael Buesch | 35c7e66 | 2007-11-03 14:34:32 +0100 | [diff] [blame] | 135 | struct b43_rfkill *rfk = &(dev->wl->rfkill); |
Michael Buesch | 8e9f752 | 2007-09-27 21:35:34 +0200 | [diff] [blame] | 136 | |
Michael Buesch | 35c7e66 | 2007-11-03 14:34:32 +0100 | [diff] [blame] | 137 | if (!rfk->registered) |
Michael Buesch | 8e9f752 | 2007-09-27 21:35:34 +0200 | [diff] [blame] | 138 | return NULL; |
Michael Buesch | 35c7e66 | 2007-11-03 14:34:32 +0100 | [diff] [blame] | 139 | return rfkill_get_led_name(rfk->rfkill); |
Michael Buesch | 8e9f752 | 2007-09-27 21:35:34 +0200 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | void b43_rfkill_init(struct b43_wldev *dev) |
| 143 | { |
| 144 | struct b43_wl *wl = dev->wl; |
| 145 | struct b43_rfkill *rfk = &(wl->rfkill); |
| 146 | int err; |
| 147 | |
Michael Buesch | 35c7e66 | 2007-11-03 14:34:32 +0100 | [diff] [blame] | 148 | rfk->registered = 0; |
Michael Buesch | 42bb4cd | 2007-09-28 14:22:33 +0200 | [diff] [blame] | 149 | |
| 150 | rfk->rfkill = rfkill_allocate(dev->dev->dev, RFKILL_TYPE_WLAN); |
Michael Buesch | 35c7e66 | 2007-11-03 14:34:32 +0100 | [diff] [blame] | 151 | if (!rfk->rfkill) |
| 152 | goto out_error; |
| 153 | snprintf(rfk->name, sizeof(rfk->name), |
| 154 | "b43-%s", wiphy_name(wl->hw->wiphy)); |
Michael Buesch | 42bb4cd | 2007-09-28 14:22:33 +0200 | [diff] [blame] | 155 | rfk->rfkill->name = rfk->name; |
Adel Gadllah | f97d1f4 | 2008-06-28 14:20:40 +0200 | [diff] [blame] | 156 | rfk->rfkill->state = RFKILL_STATE_UNBLOCKED; |
Michael Buesch | 42bb4cd | 2007-09-28 14:22:33 +0200 | [diff] [blame] | 157 | rfk->rfkill->data = dev; |
| 158 | rfk->rfkill->toggle_radio = b43_rfkill_soft_toggle; |
| 159 | rfk->rfkill->user_claim_unsupported = 1; |
| 160 | |
| 161 | rfk->poll_dev = input_allocate_polled_device(); |
Stefano Brivio | a38db5b | 2008-01-13 18:30:14 +0100 | [diff] [blame] | 162 | if (!rfk->poll_dev) { |
| 163 | rfkill_free(rfk->rfkill); |
| 164 | goto err_freed_rfk; |
| 165 | } |
| 166 | |
Michael Buesch | 35c7e66 | 2007-11-03 14:34:32 +0100 | [diff] [blame] | 167 | rfk->poll_dev->private = dev; |
| 168 | rfk->poll_dev->poll = b43_rfkill_poll; |
| 169 | rfk->poll_dev->poll_interval = 1000; /* msecs */ |
| 170 | |
Larry Finger | 1a8d122 | 2007-12-14 13:59:11 +0100 | [diff] [blame] | 171 | rfk->poll_dev->input->name = rfk->name; |
| 172 | rfk->poll_dev->input->id.bustype = BUS_HOST; |
| 173 | rfk->poll_dev->input->id.vendor = dev->dev->bus->boardinfo.vendor; |
| 174 | rfk->poll_dev->input->evbit[0] = BIT(EV_KEY); |
| 175 | set_bit(KEY_WLAN, rfk->poll_dev->input->keybit); |
| 176 | |
Michael Buesch | 35c7e66 | 2007-11-03 14:34:32 +0100 | [diff] [blame] | 177 | err = rfkill_register(rfk->rfkill); |
| 178 | if (err) |
| 179 | goto err_free_polldev; |
Larry Finger | 1a8d122 | 2007-12-14 13:59:11 +0100 | [diff] [blame] | 180 | |
| 181 | #ifdef CONFIG_RFKILL_INPUT_MODULE |
| 182 | /* B43 RF-kill isn't useful without the rfkill-input subsystem. |
| 183 | * Try to load the module. */ |
| 184 | err = request_module("rfkill-input"); |
| 185 | if (err) |
| 186 | b43warn(wl, "Failed to load the rfkill-input module. " |
| 187 | "The built-in radio LED will not work.\n"); |
| 188 | #endif /* CONFIG_RFKILL_INPUT */ |
| 189 | |
Michael Buesch | 35c7e66 | 2007-11-03 14:34:32 +0100 | [diff] [blame] | 190 | err = input_register_polled_device(rfk->poll_dev); |
| 191 | if (err) |
| 192 | goto err_unreg_rfk; |
| 193 | |
| 194 | rfk->registered = 1; |
| 195 | |
| 196 | return; |
| 197 | err_unreg_rfk: |
| 198 | rfkill_unregister(rfk->rfkill); |
| 199 | err_free_polldev: |
| 200 | input_free_polled_device(rfk->poll_dev); |
| 201 | rfk->poll_dev = NULL; |
Stefano Brivio | a38db5b | 2008-01-13 18:30:14 +0100 | [diff] [blame] | 202 | err_freed_rfk: |
Michael Buesch | 35c7e66 | 2007-11-03 14:34:32 +0100 | [diff] [blame] | 203 | rfk->rfkill = NULL; |
| 204 | out_error: |
| 205 | rfk->registered = 0; |
| 206 | b43warn(wl, "RF-kill button init failed\n"); |
Michael Buesch | 42bb4cd | 2007-09-28 14:22:33 +0200 | [diff] [blame] | 207 | } |
| 208 | |
Michael Buesch | 35c7e66 | 2007-11-03 14:34:32 +0100 | [diff] [blame] | 209 | void b43_rfkill_exit(struct b43_wldev *dev) |
Michael Buesch | 42bb4cd | 2007-09-28 14:22:33 +0200 | [diff] [blame] | 210 | { |
| 211 | struct b43_rfkill *rfk = &(dev->wl->rfkill); |
| 212 | |
Michael Buesch | 35c7e66 | 2007-11-03 14:34:32 +0100 | [diff] [blame] | 213 | if (!rfk->registered) |
| 214 | return; |
| 215 | rfk->registered = 0; |
| 216 | |
| 217 | input_unregister_polled_device(rfk->poll_dev); |
| 218 | rfkill_unregister(rfk->rfkill); |
Michael Buesch | 42bb4cd | 2007-09-28 14:22:33 +0200 | [diff] [blame] | 219 | input_free_polled_device(rfk->poll_dev); |
| 220 | rfk->poll_dev = NULL; |
Michael Buesch | 8e9f752 | 2007-09-27 21:35:34 +0200 | [diff] [blame] | 221 | rfk->rfkill = NULL; |
| 222 | } |