ps3: gelic: updown_lock semaphore to mutex
[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 #include <linux/kmod.h>
29
30
31 /* Returns TRUE, if the radio is enabled in hardware. */
32 static bool b43_is_hw_radio_enabled(struct b43_wldev *dev)
33 {
34         if (dev->phy.rev >= 3) {
35                 if (!(b43_read32(dev, B43_MMIO_RADIO_HWENABLED_HI)
36                       & B43_MMIO_RADIO_HWENABLED_HI_MASK))
37                         return 1;
38         } else {
39                 if (b43_read16(dev, B43_MMIO_RADIO_HWENABLED_LO)
40                     & B43_MMIO_RADIO_HWENABLED_LO_MASK)
41                         return 1;
42         }
43         return 0;
44 }
45
46 /* The poll callback for the hardware button. */
47 static void b43_rfkill_poll(struct input_polled_dev *poll_dev)
48 {
49         struct b43_wldev *dev = poll_dev->private;
50         struct b43_wl *wl = dev->wl;
51         bool enabled;
52         bool report_change = 0;
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                 report_change = 1;
63                 b43info(wl, "Radio hardware status changed to %s\n",
64                         enabled ? "ENABLED" : "DISABLED");
65         }
66         mutex_unlock(&wl->mutex);
67
68         /* send the radio switch event to the system - note both a key press
69          * and a release are required */
70         if (unlikely(report_change)) {
71                 input_report_key(poll_dev->input, KEY_WLAN, 1);
72                 input_report_key(poll_dev->input, KEY_WLAN, 0);
73         }
74 }
75
76 /* Called when the RFKILL toggled in software. */
77 static int b43_rfkill_soft_toggle(void *data, enum rfkill_state state)
78 {
79         struct b43_wldev *dev = data;
80         struct b43_wl *wl = dev->wl;
81         int err = -EBUSY;
82
83         if (!wl->rfkill.registered)
84                 return 0;
85
86         mutex_lock(&wl->mutex);
87         if (b43_status(dev) < B43_STAT_INITIALIZED)
88                 goto out_unlock;
89         err = 0;
90         switch (state) {
91         case RFKILL_STATE_ON:
92                 if (!dev->radio_hw_enable) {
93                         /* No luck. We can't toggle the hardware RF-kill
94                          * button from software. */
95                         err = -EBUSY;
96                         goto out_unlock;
97                 }
98                 if (!dev->phy.radio_on)
99                         b43_radio_turn_on(dev);
100                 break;
101         case RFKILL_STATE_OFF:
102                 if (dev->phy.radio_on)
103                         b43_radio_turn_off(dev, 0);
104                 break;
105         }
106 out_unlock:
107         mutex_unlock(&wl->mutex);
108
109         return err;
110 }
111
112 char * b43_rfkill_led_name(struct b43_wldev *dev)
113 {
114         struct b43_rfkill *rfk = &(dev->wl->rfkill);
115
116         if (!rfk->registered)
117                 return NULL;
118         return rfkill_get_led_name(rfk->rfkill);
119 }
120
121 void b43_rfkill_init(struct b43_wldev *dev)
122 {
123         struct b43_wl *wl = dev->wl;
124         struct b43_rfkill *rfk = &(wl->rfkill);
125         int err;
126
127         rfk->registered = 0;
128
129         rfk->rfkill = rfkill_allocate(dev->dev->dev, RFKILL_TYPE_WLAN);
130         if (!rfk->rfkill)
131                 goto out_error;
132         snprintf(rfk->name, sizeof(rfk->name),
133                  "b43-%s", wiphy_name(wl->hw->wiphy));
134         rfk->rfkill->name = rfk->name;
135         rfk->rfkill->state = RFKILL_STATE_ON;
136         rfk->rfkill->data = dev;
137         rfk->rfkill->toggle_radio = b43_rfkill_soft_toggle;
138         rfk->rfkill->user_claim_unsupported = 1;
139
140         rfk->poll_dev = input_allocate_polled_device();
141         if (!rfk->poll_dev) {
142                 rfkill_free(rfk->rfkill);
143                 goto err_freed_rfk;
144         }
145
146         rfk->poll_dev->private = dev;
147         rfk->poll_dev->poll = b43_rfkill_poll;
148         rfk->poll_dev->poll_interval = 1000; /* msecs */
149
150         rfk->poll_dev->input->name = rfk->name;
151         rfk->poll_dev->input->id.bustype = BUS_HOST;
152         rfk->poll_dev->input->id.vendor = dev->dev->bus->boardinfo.vendor;
153         rfk->poll_dev->input->evbit[0] = BIT(EV_KEY);
154         set_bit(KEY_WLAN, rfk->poll_dev->input->keybit);
155
156         err = rfkill_register(rfk->rfkill);
157         if (err)
158                 goto err_free_polldev;
159
160 #ifdef CONFIG_RFKILL_INPUT_MODULE
161         /* B43 RF-kill isn't useful without the rfkill-input subsystem.
162          * Try to load the module. */
163         err = request_module("rfkill-input");
164         if (err)
165                 b43warn(wl, "Failed to load the rfkill-input module. "
166                         "The built-in radio LED will not work.\n");
167 #endif /* CONFIG_RFKILL_INPUT */
168
169         err = input_register_polled_device(rfk->poll_dev);
170         if (err)
171                 goto err_unreg_rfk;
172
173         rfk->registered = 1;
174
175         return;
176 err_unreg_rfk:
177         rfkill_unregister(rfk->rfkill);
178 err_free_polldev:
179         input_free_polled_device(rfk->poll_dev);
180         rfk->poll_dev = NULL;
181 err_freed_rfk:
182         rfk->rfkill = NULL;
183 out_error:
184         rfk->registered = 0;
185         b43warn(wl, "RF-kill button init failed\n");
186 }
187
188 void b43_rfkill_exit(struct b43_wldev *dev)
189 {
190         struct b43_rfkill *rfk = &(dev->wl->rfkill);
191
192         if (!rfk->registered)
193                 return;
194         rfk->registered = 0;
195
196         input_unregister_polled_device(rfk->poll_dev);
197         rfkill_unregister(rfk->rfkill);
198         input_free_polled_device(rfk->poll_dev);
199         rfk->poll_dev = NULL;
200         rfk->rfkill = NULL;
201 }