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