[B43]: RF-kill support
[linux-2.6.git] / 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 static void b43_notify_rfkill_press(struct work_struct *work)
30 {
31         struct b43_rfkill *rfk = container_of(work, struct b43_rfkill,
32                                               notify_work);
33         struct b43_wl *wl = container_of(rfk, struct b43_wl, rfkill);
34         struct b43_wldev *dev;
35         enum rfkill_state state;
36
37         mutex_lock(&wl->mutex);
38         dev = wl->current_dev;
39         if (b43_status(dev) < B43_STAT_INITIALIZED) {
40                 mutex_unlock(&wl->mutex);
41                 return;
42         }
43         if (dev->radio_hw_enable)
44                 state = RFKILL_STATE_ON;
45         else
46                 state = RFKILL_STATE_OFF;
47         b43info(wl, "Radio hardware status changed to %s\n",
48                 dev->radio_hw_enable ? "ENABLED" : "DISABLED");
49         mutex_unlock(&wl->mutex);
50
51         if (rfk->rfkill) {
52                 /* Be careful. This calls back into the software toggle routines.
53                  * So we must unlock before calling. */
54                 rfkill_switch_all(rfk->rfkill->type, state);
55         }
56 }
57
58 /* Called when the RFKILL toggled in hardware.
59  * This is called with the mutex locked. */
60 void b43_rfkill_toggled(struct b43_wldev *dev, bool on)
61 {
62         struct b43_wl *wl = dev->wl;
63
64         B43_WARN_ON(b43_status(dev) < B43_STAT_INITIALIZED);
65         /* Update the RF status asynchronously, as rfkill will
66          * call back into the software toggle handler.
67          * This would deadlock if done synchronously. */
68         queue_work(wl->hw->workqueue, &wl->rfkill.notify_work);
69 }
70
71 /* Called when the RFKILL toggled in software.
72  * This is called without locking. */
73 static int b43_rfkill_soft_toggle(void *data, enum rfkill_state state)
74 {
75         struct b43_wldev *dev = data;
76         struct b43_wl *wl = dev->wl;
77         int err = 0;
78
79         mutex_lock(&wl->mutex);
80         if (b43_status(dev) < B43_STAT_INITIALIZED)
81                 goto out_unlock;
82
83         switch (state) {
84         case RFKILL_STATE_ON:
85                 if (!dev->radio_hw_enable) {
86                         /* No luck. We can't toggle the hardware RF-kill
87                          * button from software. */
88                         err = -EBUSY;
89                         goto out_unlock;
90                 }
91                 if (!dev->phy.radio_on)
92                         b43_radio_turn_on(dev);
93                 break;
94         case RFKILL_STATE_OFF:
95                 if (dev->phy.radio_on)
96                         b43_radio_turn_off(dev, 0);
97                 break;
98         }
99
100 out_unlock:
101         mutex_unlock(&wl->mutex);
102
103         return err;
104 }
105
106 char * b43_rfkill_led_name(struct b43_wldev *dev)
107 {
108         struct b43_wl *wl = dev->wl;
109
110         if (!wl->rfkill.rfkill)
111                 return NULL;
112         return rfkill_get_led_name(wl->rfkill.rfkill);
113 }
114
115 void b43_rfkill_init(struct b43_wldev *dev)
116 {
117         struct b43_wl *wl = dev->wl;
118         struct b43_rfkill *rfk = &(wl->rfkill);
119         int err;
120
121         snprintf(rfk->name, sizeof(rfk->name),
122                  "b43-%s", wiphy_name(wl->hw->wiphy));
123         rfk->rfkill = rfkill_allocate(dev->dev->dev, RFKILL_TYPE_WLAN);
124         if (!rfk->rfkill)
125                 goto error;
126         rfk->rfkill->name = rfk->name;
127         rfk->rfkill->state = RFKILL_STATE_ON;
128         rfk->rfkill->data = dev;
129         rfk->rfkill->toggle_radio = b43_rfkill_soft_toggle;
130         rfk->rfkill->user_claim_unsupported = 1;
131
132         INIT_WORK(&rfk->notify_work, b43_notify_rfkill_press);
133
134         err = rfkill_register(rfk->rfkill);
135         if (err)
136                 goto error;
137
138         return;
139 error:
140         b43warn(dev->wl, "Failed to initialize the RF-kill button\n");
141         rfkill_free(rfk->rfkill);
142         rfk->rfkill = NULL;
143 }
144
145 void b43_rfkill_exit(struct b43_wldev *dev)
146 {
147         struct b43_rfkill *rfk = &(dev->wl->rfkill);
148
149         if (!rfk->rfkill)
150                 return;
151         cancel_work_sync(&rfk->notify_work);
152         rfkill_unregister(rfk->rfkill);
153         rfkill_free(rfk->rfkill);
154         rfk->rfkill = NULL;
155 }