]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/net/wireless/b43/rfkill.c
b43: Issue warning when RFKILL_INPUT is not enabled
[linux-2.6-omap-h63xx.git] / drivers / net / wireless / b43 / rfkill.c
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 #include "phy_common.h"
28
29 #include <linux/kmod.h>
30
31
32 /* Returns TRUE, if the radio is enabled in hardware. */
33 static bool b43_is_hw_radio_enabled(struct b43_wldev *dev)
34 {
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;
43         }
44         return 0;
45 }
46
47 /* Update the rfkill state */
48 static void b43_rfkill_update_state(struct b43_wldev *dev)
49 {
50         struct b43_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
64 /* The poll callback for the hardware button. */
65 static void b43_rfkill_poll(struct input_polled_dev *poll_dev)
66 {
67         struct b43_wldev *dev = poll_dev->private;
68         struct b43_wl *wl = dev->wl;
69         bool enabled;
70         bool report_change = 0;
71
72         mutex_lock(&wl->mutex);
73         if (unlikely(b43_status(dev) < B43_STAT_INITIALIZED)) {
74                 mutex_unlock(&wl->mutex);
75                 return;
76         }
77         enabled = b43_is_hw_radio_enabled(dev);
78         if (unlikely(enabled != dev->radio_hw_enable)) {
79                 dev->radio_hw_enable = enabled;
80                 report_change = 1;
81                 b43_rfkill_update_state(dev);
82                 b43info(wl, "Radio hardware status changed to %s\n",
83                         enabled ? "ENABLED" : "DISABLED");
84         }
85         mutex_unlock(&wl->mutex);
86
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         }
93 }
94
95 /* Called when the RFKILL toggled in software. */
96 static int b43_rfkill_soft_toggle(void *data, enum rfkill_state state)
97 {
98         struct b43_wldev *dev = data;
99         struct b43_wl *wl = dev->wl;
100         int err = -EBUSY;
101
102         if (!wl->rfkill.registered)
103                 return 0;
104
105         mutex_lock(&wl->mutex);
106         if (b43_status(dev) < B43_STAT_INITIALIZED)
107                 goto out_unlock;
108         err = 0;
109         switch (state) {
110         case RFKILL_STATE_UNBLOCKED:
111                 if (!dev->radio_hw_enable) {
112                         /* No luck. We can't toggle the hardware RF-kill
113                          * button from software. */
114                         err = -EBUSY;
115                         goto out_unlock;
116                 }
117                 if (!dev->phy.radio_on)
118                         b43_software_rfkill(dev, state);
119                 break;
120         case RFKILL_STATE_SOFT_BLOCKED:
121                 if (dev->phy.radio_on)
122                         b43_software_rfkill(dev, state);
123                 break;
124         default:
125                 b43warn(wl, "Received unexpected rfkill state %d.\n", state);
126                 break;
127         }
128 out_unlock:
129         mutex_unlock(&wl->mutex);
130
131         return err;
132 }
133
134 char * b43_rfkill_led_name(struct b43_wldev *dev)
135 {
136         struct b43_rfkill *rfk = &(dev->wl->rfkill);
137
138         if (!rfk->registered)
139                 return NULL;
140         return rfkill_get_led_name(rfk->rfkill);
141 }
142
143 void b43_rfkill_init(struct b43_wldev *dev)
144 {
145         struct b43_wl *wl = dev->wl;
146         struct b43_rfkill *rfk = &(wl->rfkill);
147         int err;
148
149         rfk->registered = 0;
150
151         rfk->rfkill = rfkill_allocate(dev->dev->dev, RFKILL_TYPE_WLAN);
152         if (!rfk->rfkill)
153                 goto out_error;
154         snprintf(rfk->name, sizeof(rfk->name),
155                  "b43-%s", wiphy_name(wl->hw->wiphy));
156         rfk->rfkill->name = rfk->name;
157         rfk->rfkill->state = RFKILL_STATE_UNBLOCKED;
158         rfk->rfkill->data = dev;
159         rfk->rfkill->toggle_radio = b43_rfkill_soft_toggle;
160         rfk->rfkill->user_claim_unsupported = 1;
161
162         rfk->poll_dev = input_allocate_polled_device();
163         if (!rfk->poll_dev) {
164                 rfkill_free(rfk->rfkill);
165                 goto err_freed_rfk;
166         }
167
168         rfk->poll_dev->private = dev;
169         rfk->poll_dev->poll = b43_rfkill_poll;
170         rfk->poll_dev->poll_interval = 1000; /* msecs */
171
172         rfk->poll_dev->input->name = rfk->name;
173         rfk->poll_dev->input->id.bustype = BUS_HOST;
174         rfk->poll_dev->input->id.vendor = dev->dev->bus->boardinfo.vendor;
175         rfk->poll_dev->input->evbit[0] = BIT(EV_KEY);
176         set_bit(KEY_WLAN, rfk->poll_dev->input->keybit);
177
178         err = rfkill_register(rfk->rfkill);
179         if (err)
180                 goto err_free_polldev;
181
182 #ifdef CONFIG_RFKILL_INPUT_MODULE
183         /* B43 RF-kill isn't useful without the rfkill-input subsystem.
184          * Try to load the module. */
185         err = request_module("rfkill-input");
186         if (err)
187                 b43warn(wl, "Failed to load the rfkill-input module. "
188                         "The built-in radio LED will not work.\n");
189 #endif /* CONFIG_RFKILL_INPUT */
190
191 #if !defined(CONFIG_RFKILL_INPUT) && !defined(CONFIG_RFKILL_INPUT_MODULE)
192         b43warn(wl, "The rfkill-input subsystem is not available. "
193                 "The built-in radio LED will not work.\n");
194 #endif
195
196         err = input_register_polled_device(rfk->poll_dev);
197         if (err)
198                 goto err_unreg_rfk;
199
200         rfk->registered = 1;
201
202         return;
203 err_unreg_rfk:
204         rfkill_unregister(rfk->rfkill);
205 err_free_polldev:
206         input_free_polled_device(rfk->poll_dev);
207         rfk->poll_dev = NULL;
208 err_freed_rfk:
209         rfk->rfkill = NULL;
210 out_error:
211         rfk->registered = 0;
212         b43warn(wl, "RF-kill button init failed\n");
213 }
214
215 void b43_rfkill_exit(struct b43_wldev *dev)
216 {
217         struct b43_rfkill *rfk = &(dev->wl->rfkill);
218
219         if (!rfk->registered)
220                 return;
221         rfk->registered = 0;
222
223         input_unregister_polled_device(rfk->poll_dev);
224         rfkill_unregister(rfk->rfkill);
225         input_free_polled_device(rfk->poll_dev);
226         rfk->poll_dev = NULL;
227         rfk->rfkill = NULL;
228 }