Merge branch 'for-2.6.24' of master.kernel.org:/pub/scm/linux/kernel/git/galak/powerp...
[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
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         bool report_change = 0;
51
52         mutex_lock(&wl->mutex);
53         B43_WARN_ON(b43_status(dev) < B43_STAT_INITIALIZED);
54         enabled = b43_is_hw_radio_enabled(dev);
55         if (unlikely(enabled != dev->radio_hw_enable)) {
56                 dev->radio_hw_enable = enabled;
57                 report_change = 1;
58                 b43info(wl, "Radio hardware status changed to %s\n",
59                         enabled ? "ENABLED" : "DISABLED");
60         }
61         mutex_unlock(&wl->mutex);
62
63         if (unlikely(report_change))
64                 input_report_key(poll_dev->input, KEY_WLAN, enabled);
65 }
66
67 /* Called when the RFKILL toggled in software. */
68 static int b43_rfkill_soft_toggle(void *data, enum rfkill_state state)
69 {
70         struct b43_wldev *dev = data;
71         struct b43_wl *wl = dev->wl;
72         int err = 0;
73
74         if (!wl->rfkill.registered)
75                 return 0;
76
77         mutex_lock(&wl->mutex);
78         B43_WARN_ON(b43_status(dev) < B43_STAT_INITIALIZED);
79         switch (state) {
80         case RFKILL_STATE_ON:
81                 if (!dev->radio_hw_enable) {
82                         /* No luck. We can't toggle the hardware RF-kill
83                          * button from software. */
84                         err = -EBUSY;
85                         goto out_unlock;
86                 }
87                 if (!dev->phy.radio_on)
88                         b43_radio_turn_on(dev);
89                 break;
90         case RFKILL_STATE_OFF:
91                 if (dev->phy.radio_on)
92                         b43_radio_turn_off(dev, 0);
93                 break;
94         }
95 out_unlock:
96         mutex_unlock(&wl->mutex);
97
98         return err;
99 }
100
101 char * b43_rfkill_led_name(struct b43_wldev *dev)
102 {
103         struct b43_rfkill *rfk = &(dev->wl->rfkill);
104
105         if (!rfk->registered)
106                 return NULL;
107         return rfkill_get_led_name(rfk->rfkill);
108 }
109
110 void b43_rfkill_init(struct b43_wldev *dev)
111 {
112         struct b43_wl *wl = dev->wl;
113         struct b43_rfkill *rfk = &(wl->rfkill);
114         int err;
115
116         rfk->registered = 0;
117
118         rfk->rfkill = rfkill_allocate(dev->dev->dev, RFKILL_TYPE_WLAN);
119         if (!rfk->rfkill)
120                 goto out_error;
121         snprintf(rfk->name, sizeof(rfk->name),
122                  "b43-%s", wiphy_name(wl->hw->wiphy));
123         rfk->rfkill->name = rfk->name;
124         rfk->rfkill->state = RFKILL_STATE_ON;
125         rfk->rfkill->data = dev;
126         rfk->rfkill->toggle_radio = b43_rfkill_soft_toggle;
127         rfk->rfkill->user_claim_unsupported = 1;
128
129         rfk->poll_dev = input_allocate_polled_device();
130         if (!rfk->poll_dev)
131                 goto err_free_rfk;
132         rfk->poll_dev->private = dev;
133         rfk->poll_dev->poll = b43_rfkill_poll;
134         rfk->poll_dev->poll_interval = 1000; /* msecs */
135
136         err = rfkill_register(rfk->rfkill);
137         if (err)
138                 goto err_free_polldev;
139         err = input_register_polled_device(rfk->poll_dev);
140         if (err)
141                 goto err_unreg_rfk;
142
143         rfk->registered = 1;
144
145         return;
146 err_unreg_rfk:
147         rfkill_unregister(rfk->rfkill);
148 err_free_polldev:
149         input_free_polled_device(rfk->poll_dev);
150         rfk->poll_dev = NULL;
151 err_free_rfk:
152         rfkill_free(rfk->rfkill);
153         rfk->rfkill = NULL;
154 out_error:
155         rfk->registered = 0;
156         b43warn(wl, "RF-kill button init failed\n");
157 }
158
159 void b43_rfkill_exit(struct b43_wldev *dev)
160 {
161         struct b43_rfkill *rfk = &(dev->wl->rfkill);
162
163         if (!rfk->registered)
164                 return;
165         rfk->registered = 0;
166
167         input_unregister_polled_device(rfk->poll_dev);
168         rfkill_unregister(rfk->rfkill);
169         input_free_polled_device(rfk->poll_dev);
170         rfk->poll_dev = NULL;
171         rfkill_free(rfk->rfkill);
172         rfk->rfkill = NULL;
173 }