]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/net/wireless/b43legacy/rfkill.c
b9d38a4f286dd80c96840070ffd18ff63cca5101
[linux-2.6-omap-h63xx.git] / drivers / net / wireless / b43legacy / 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 "radio.h"
27 #include "b43legacy.h"
28
29
30 /* Returns TRUE, if the radio is enabled in hardware. */
31 static bool b43legacy_is_hw_radio_enabled(struct b43legacy_wldev *dev)
32 {
33         if (dev->phy.rev >= 3) {
34                 if (!(b43legacy_read32(dev, B43legacy_MMIO_RADIO_HWENABLED_HI)
35                       & B43legacy_MMIO_RADIO_HWENABLED_HI_MASK))
36                         return 1;
37         } else {
38                 if (b43legacy_read16(dev, B43legacy_MMIO_RADIO_HWENABLED_LO)
39                     & B43legacy_MMIO_RADIO_HWENABLED_LO_MASK)
40                         return 1;
41         }
42         return 0;
43 }
44
45 /* The poll callback for the hardware button. */
46 static void b43legacy_rfkill_poll(struct input_polled_dev *poll_dev)
47 {
48         struct b43legacy_wldev *dev = poll_dev->private;
49         struct b43legacy_wl *wl = dev->wl;
50         bool enabled;
51         bool report_change = 0;
52
53         mutex_lock(&wl->mutex);
54         B43legacy_WARN_ON(b43legacy_status(dev) < B43legacy_STAT_INITIALIZED);
55         enabled = b43legacy_is_hw_radio_enabled(dev);
56         if (unlikely(enabled != dev->radio_hw_enable)) {
57                 dev->radio_hw_enable = enabled;
58                 report_change = 1;
59                 b43legacyinfo(wl, "Radio hardware status changed to %s\n",
60                         enabled ? "ENABLED" : "DISABLED");
61         }
62         mutex_unlock(&wl->mutex);
63
64         if (unlikely(report_change))
65                 input_report_key(poll_dev->input, KEY_WLAN, enabled);
66 }
67
68 /* Called when the RFKILL toggled in software.
69  * This is called without locking. */
70 static int b43legacy_rfkill_soft_toggle(void *data, enum rfkill_state state)
71 {
72         struct b43legacy_wldev *dev = data;
73         struct b43legacy_wl *wl = dev->wl;
74         int err = 0;
75
76         if (!wl->rfkill.registered)
77                 return 0;
78
79         mutex_lock(&wl->mutex);
80         B43legacy_WARN_ON(b43legacy_status(dev) < B43legacy_STAT_INITIALIZED);
81         switch (state) {
82         case RFKILL_STATE_ON:
83                 if (!dev->radio_hw_enable) {
84                         /* No luck. We can't toggle the hardware RF-kill
85                          * button from software. */
86                         err = -EBUSY;
87                         goto out_unlock;
88                 }
89                 if (!dev->phy.radio_on)
90                         b43legacy_radio_turn_on(dev);
91                 break;
92         case RFKILL_STATE_OFF:
93                 if (dev->phy.radio_on)
94                         b43legacy_radio_turn_off(dev, 0);
95                 break;
96         }
97
98 out_unlock:
99         mutex_unlock(&wl->mutex);
100
101         return err;
102 }
103
104 char *b43legacy_rfkill_led_name(struct b43legacy_wldev *dev)
105 {
106         struct b43legacy_wl *wl = dev->wl;
107
108         if (!wl->rfkill.rfkill)
109                 return NULL;
110         return rfkill_get_led_name(wl->rfkill.rfkill);
111 }
112
113 void b43legacy_rfkill_init(struct b43legacy_wldev *dev)
114 {
115         struct b43legacy_wl *wl = dev->wl;
116         struct b43legacy_rfkill *rfk = &(wl->rfkill);
117         int err;
118
119         if (rfk->rfkill) {
120                 err = rfkill_register(rfk->rfkill);
121                 if (err) {
122                         b43legacywarn(wl, "Failed to register RF-kill button\n");
123                         goto err_free_rfk;
124                 }
125         }
126         if (rfk->poll_dev) {
127                 err = input_register_polled_device(rfk->poll_dev);
128                 if (err) {
129                         b43legacywarn(wl, "Failed to register RF-kill polldev\n");
130                         goto err_free_polldev;
131                 }
132         }
133
134         return;
135 err_free_rfk:
136         rfkill_free(rfk->rfkill);
137         rfk->rfkill = NULL;
138 err_free_polldev:
139         input_free_polled_device(rfk->poll_dev);
140         rfk->poll_dev = NULL;
141 }
142
143 void b43legacy_rfkill_exit(struct b43legacy_wldev *dev)
144 {
145         struct b43legacy_rfkill *rfk = &(dev->wl->rfkill);
146
147         if (rfk->poll_dev)
148                 input_unregister_polled_device(rfk->poll_dev);
149         if (rfk->rfkill)
150                 rfkill_unregister(rfk->rfkill);
151 }
152
153 void b43legacy_rfkill_alloc(struct b43legacy_wldev *dev)
154 {
155         struct b43legacy_wl *wl = dev->wl;
156         struct b43legacy_rfkill *rfk = &(wl->rfkill);
157
158         snprintf(rfk->name, sizeof(rfk->name),
159                  "b43legacy-%s", wiphy_name(wl->hw->wiphy));
160
161         rfk->rfkill = rfkill_allocate(dev->dev->dev, RFKILL_TYPE_WLAN);
162         if (!rfk->rfkill) {
163                 b43legacywarn(wl, "Failed to allocate RF-kill button\n");
164                 return;
165         }
166         rfk->rfkill->name = rfk->name;
167         rfk->rfkill->state = RFKILL_STATE_ON;
168         rfk->rfkill->data = dev;
169         rfk->rfkill->toggle_radio = b43legacy_rfkill_soft_toggle;
170         rfk->rfkill->user_claim_unsupported = 1;
171
172         rfk->poll_dev = input_allocate_polled_device();
173         if (rfk->poll_dev) {
174                 rfk->poll_dev->private = dev;
175                 rfk->poll_dev->poll = b43legacy_rfkill_poll;
176                 rfk->poll_dev->poll_interval = 1000; /* msecs */
177         } else
178                 b43legacywarn(wl, "Failed to allocate RF-kill polldev\n");
179 }
180
181 void b43legacy_rfkill_free(struct b43legacy_wldev *dev)
182 {
183         struct b43legacy_rfkill *rfk = &(dev->wl->rfkill);
184
185         input_free_polled_device(rfk->poll_dev);
186         rfk->poll_dev = NULL;
187         rfkill_free(rfk->rfkill);
188         rfk->rfkill = NULL;
189 }