iwlagn: delay ict interrupt.
[linux-2.6] / 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 /* The poll callback for the hardware button. */
48 static void b43_rfkill_poll(struct rfkill *rfkill, void *data)
49 {
50         struct b43_wldev *dev = data;
51         struct b43_wl *wl = dev->wl;
52         bool enabled;
53
54         mutex_lock(&wl->mutex);
55         if (unlikely(b43_status(dev) < B43_STAT_INITIALIZED)) {
56                 mutex_unlock(&wl->mutex);
57                 return;
58         }
59         enabled = b43_is_hw_radio_enabled(dev);
60         if (unlikely(enabled != dev->radio_hw_enable)) {
61                 dev->radio_hw_enable = enabled;
62                 b43info(wl, "Radio hardware status changed to %s\n",
63                         enabled ? "ENABLED" : "DISABLED");
64                 enabled = !rfkill_set_hw_state(rfkill, !enabled);
65                 if (enabled != dev->phy.radio_on)
66                         b43_software_rfkill(dev, !enabled);
67         }
68         mutex_unlock(&wl->mutex);
69 }
70
71 /* Called when the RFKILL toggled in software. */
72 static int b43_rfkill_soft_set(void *data, bool blocked)
73 {
74         struct b43_wldev *dev = data;
75         struct b43_wl *wl = dev->wl;
76         int err = -EINVAL;
77
78         if (WARN_ON(!wl->rfkill.registered))
79                 return -EINVAL;
80
81         mutex_lock(&wl->mutex);
82
83         if (b43_status(dev) < B43_STAT_INITIALIZED)
84                 goto out_unlock;
85
86         if (!dev->radio_hw_enable)
87                 goto out_unlock;
88
89         if (!blocked != dev->phy.radio_on)
90                 b43_software_rfkill(dev, blocked);
91         err = 0;
92 out_unlock:
93         mutex_unlock(&wl->mutex);
94         return err;
95 }
96
97 const char *b43_rfkill_led_name(struct b43_wldev *dev)
98 {
99         struct b43_rfkill *rfk = &(dev->wl->rfkill);
100
101         if (!rfk->registered)
102                 return NULL;
103         return rfkill_get_led_trigger_name(rfk->rfkill);
104 }
105
106 static const struct rfkill_ops b43_rfkill_ops = {
107         .set_block = b43_rfkill_soft_set,
108         .poll = b43_rfkill_poll,
109 };
110
111 void b43_rfkill_init(struct b43_wldev *dev)
112 {
113         struct b43_wl *wl = dev->wl;
114         struct b43_rfkill *rfk = &(wl->rfkill);
115         int err;
116
117         rfk->registered = 0;
118
119         snprintf(rfk->name, sizeof(rfk->name),
120                  "b43-%s", wiphy_name(wl->hw->wiphy));
121
122         rfk->rfkill = rfkill_alloc(rfk->name,
123                                    dev->dev->dev,
124                                    RFKILL_TYPE_WLAN,
125                                    &b43_rfkill_ops, dev);
126         if (!rfk->rfkill)
127                 goto out_error;
128
129         err = rfkill_register(rfk->rfkill);
130         if (err)
131                 goto err_free;
132
133         rfk->registered = 1;
134
135         return;
136  err_free:
137         rfkill_destroy(rfk->rfkill);
138  out_error:
139         rfk->registered = 0;
140         b43warn(wl, "RF-kill button init failed\n");
141 }
142
143 void b43_rfkill_exit(struct b43_wldev *dev)
144 {
145         struct b43_rfkill *rfk = &(dev->wl->rfkill);
146
147         if (!rfk->registered)
148                 return;
149         rfk->registered = 0;
150
151         rfkill_unregister(rfk->rfkill);
152         rfkill_destroy(rfk->rfkill);
153         rfk->rfkill = NULL;
154 }