800e0a61a7f589f1f649b7f6417cf6488267f4d5
[openwrt.git] / package / b43 / src / 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
28
29 /* Returns TRUE, if the radio is enabled in hardware. */
30 static bool b43_is_hw_radio_enabled(struct b43_wldev *dev)
31 {
32         if (dev->phy.rev >= 3) {
33                 if (!(b43_read32(dev, B43_MMIO_RADIO_HWENABLED_HI)
34                       & B43_MMIO_RADIO_HWENABLED_HI_MASK))
35                         return 1;
36         } else {
37                 if (b43_read16(dev, B43_MMIO_RADIO_HWENABLED_LO)
38                     & B43_MMIO_RADIO_HWENABLED_LO_MASK)
39                         return 1;
40         }
41         return 0;
42 }
43
44 /* The poll callback for the hardware button. */
45 static void b43_rfkill_poll(struct input_polled_dev *poll_dev)
46 {
47         struct b43_wldev *dev = poll_dev->private;
48         struct b43_wl *wl = dev->wl;
49         bool enabled;
50
51         mutex_lock(&wl->mutex);
52         B43_WARN_ON(b43_status(dev) < B43_STAT_INITIALIZED);
53         enabled = b43_is_hw_radio_enabled(dev);
54         if (unlikely(enabled != dev->radio_hw_enable)) {
55                 dev->radio_hw_enable = enabled;
56                 b43info(wl, "Radio hardware status changed to %s\n",
57                         enabled ? "ENABLED" : "DISABLED");
58                 mutex_unlock(&wl->mutex);
59                 input_report_key(poll_dev->input, KEY_WLAN, enabled);
60         } else
61                 mutex_unlock(&wl->mutex);
62 }
63
64 /* Called when the RFKILL toggled in software.
65  * This is called without locking. */
66 static int b43_rfkill_soft_toggle(void *data, enum rfkill_state state)
67 {
68         struct b43_wldev *dev = data;
69         struct b43_wl *wl = dev->wl;
70         int err = 0;
71
72         mutex_lock(&wl->mutex);
73         if (b43_status(dev) < B43_STAT_INITIALIZED)
74                 goto out_unlock;
75
76         switch (state) {
77         case RFKILL_STATE_ON:
78                 if (!dev->radio_hw_enable) {
79                         /* No luck. We can't toggle the hardware RF-kill
80                          * button from software. */
81                         err = -EBUSY;
82                         goto out_unlock;
83                 }
84                 if (!dev->phy.radio_on)
85                         b43_radio_turn_on(dev);
86                 break;
87         case RFKILL_STATE_OFF:
88                 if (dev->phy.radio_on)
89                         b43_radio_turn_off(dev, 0);
90                 break;
91         }
92
93 out_unlock:
94         mutex_unlock(&wl->mutex);
95
96         return err;
97 }
98
99 char * b43_rfkill_led_name(struct b43_wldev *dev)
100 {
101         struct b43_wl *wl = dev->wl;
102
103         if (!wl->rfkill.rfkill)
104                 return NULL;
105         return rfkill_get_led_name(wl->rfkill.rfkill);
106 }
107
108 void b43_rfkill_init(struct b43_wldev *dev)
109 {
110         struct b43_wl *wl = dev->wl;
111         struct b43_rfkill *rfk = &(wl->rfkill);
112         int err;
113
114         if (rfk->rfkill) {
115                 err = rfkill_register(rfk->rfkill);
116                 if (err) {
117                         b43warn(wl, "Failed to register RF-kill button\n");
118                         goto err_free_rfk;
119                 }
120         }
121         if (rfk->poll_dev) {
122                 err = input_register_polled_device(rfk->poll_dev);
123                 if (err) {
124                         b43warn(wl, "Failed to register RF-kill polldev\n");
125                         goto err_free_polldev;
126                 }
127         }
128
129         return;
130 err_free_rfk:
131         rfkill_free(rfk->rfkill);
132         rfk->rfkill = NULL;
133 err_free_polldev:
134         input_free_polled_device(rfk->poll_dev);
135         rfk->poll_dev = NULL;
136 }
137
138 void b43_rfkill_exit(struct b43_wldev *dev)
139 {
140         struct b43_rfkill *rfk = &(dev->wl->rfkill);
141
142         if (rfk->poll_dev)
143                 input_unregister_polled_device(rfk->poll_dev);
144         if (rfk->rfkill)
145                 rfkill_unregister(rfk->rfkill);
146 }
147
148 void b43_rfkill_alloc(struct b43_wldev *dev)
149 {
150         struct b43_wl *wl = dev->wl;
151         struct b43_rfkill *rfk = &(wl->rfkill);
152
153         snprintf(rfk->name, sizeof(rfk->name),
154                  "b43-%s", wiphy_name(wl->hw->wiphy));
155
156         rfk->rfkill = rfkill_allocate(dev->dev->dev, RFKILL_TYPE_WLAN);
157         if (!rfk->rfkill) {
158                 b43warn(wl, "Failed to allocate RF-kill button\n");
159                 return;
160         }
161         rfk->rfkill->name = rfk->name;
162         rfk->rfkill->state = RFKILL_STATE_ON;
163         rfk->rfkill->data = dev;
164         rfk->rfkill->toggle_radio = b43_rfkill_soft_toggle;
165         rfk->rfkill->user_claim_unsupported = 1;
166
167         rfk->poll_dev = input_allocate_polled_device();
168         if (rfk->poll_dev) {
169                 rfk->poll_dev->private = dev;
170                 rfk->poll_dev->poll = b43_rfkill_poll;
171                 rfk->poll_dev->poll_interval = 1000; /* msecs */
172         } else
173                 b43warn(wl, "Failed to allocate RF-kill polldev\n");
174 }
175
176 void b43_rfkill_free(struct b43_wldev *dev)
177 {
178         struct b43_rfkill *rfk = &(dev->wl->rfkill);
179
180         input_free_polled_device(rfk->poll_dev);
181         rfk->poll_dev = NULL;
182         rfkill_free(rfk->rfkill);
183         rfk->rfkill = NULL;
184 }