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