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