blob: 910699c4e04f633accfa15c47cb797d94539917d [file] [log] [blame]
Ivo van Doorncf4328c2007-05-07 00:34:20 -07001/*
Ivo van Doornfe242cf2007-09-27 14:57:05 -07002 * Copyright (C) 2006 - 2007 Ivo van Doorn
Ivo van Doorncf4328c2007-05-07 00:34:20 -07003 * Copyright (C) 2007 Dmitry Torokhov
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the
17 * Free Software Foundation, Inc.,
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21#include <linux/kernel.h>
22#include <linux/module.h>
23#include <linux/init.h>
24#include <linux/workqueue.h>
25#include <linux/capability.h>
26#include <linux/list.h>
27#include <linux/mutex.h>
28#include <linux/rfkill.h>
29
Michael Buesch27366222007-11-02 20:18:11 +010030/* Get declaration of rfkill_switch_all() to shut up sparse. */
31#include "rfkill-input.h"
32
33
Ivo van Doorncf4328c2007-05-07 00:34:20 -070034MODULE_AUTHOR("Ivo van Doorn <IvDoorn@gmail.com>");
35MODULE_VERSION("1.0");
36MODULE_DESCRIPTION("RF switch support");
37MODULE_LICENSE("GPL");
38
39static LIST_HEAD(rfkill_list); /* list of registered rf switches */
40static DEFINE_MUTEX(rfkill_mutex);
41
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -030042static unsigned int rfkill_default_state = RFKILL_STATE_UNBLOCKED;
Henrique de Moraes Holschuhe954b0b2008-06-23 17:22:59 -030043module_param_named(default_state, rfkill_default_state, uint, 0444);
44MODULE_PARM_DESC(default_state,
45 "Default initial state for all radio types, 0 = radio off");
46
Henrique de Moraes Holschuh99619202008-08-02 15:10:58 -030047struct rfkill_gsw_state {
48 enum rfkill_state current_state;
49 enum rfkill_state default_state;
50};
51
52static struct rfkill_gsw_state rfkill_global_states[RFKILL_TYPE_MAX];
53static unsigned long rfkill_states_lockdflt[BITS_TO_LONGS(RFKILL_TYPE_MAX)];
Ivo van Doorncf4328c2007-05-07 00:34:20 -070054
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -030055static BLOCKING_NOTIFIER_HEAD(rfkill_notifier_list);
56
57
58/**
59 * register_rfkill_notifier - Add notifier to rfkill notifier chain
60 * @nb: pointer to the new entry to add to the chain
61 *
62 * See blocking_notifier_chain_register() for return value and further
63 * observations.
64 *
65 * Adds a notifier to the rfkill notifier chain. The chain will be
66 * called with a pointer to the relevant rfkill structure as a parameter,
67 * refer to include/linux/rfkill.h for the possible events.
68 *
69 * Notifiers added to this chain are to always return NOTIFY_DONE. This
70 * chain is a blocking notifier chain: notifiers can sleep.
71 *
72 * Calls to this chain may have been done through a workqueue. One must
73 * assume unordered asynchronous behaviour, there is no way to know if
74 * actions related to the event that generated the notification have been
75 * carried out already.
76 */
77int register_rfkill_notifier(struct notifier_block *nb)
78{
Henrique de Moraes Holschuhf745ba02008-08-26 11:57:59 -030079 BUG_ON(!nb);
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -030080 return blocking_notifier_chain_register(&rfkill_notifier_list, nb);
81}
82EXPORT_SYMBOL_GPL(register_rfkill_notifier);
83
84/**
85 * unregister_rfkill_notifier - remove notifier from rfkill notifier chain
86 * @nb: pointer to the entry to remove from the chain
87 *
88 * See blocking_notifier_chain_unregister() for return value and further
89 * observations.
90 *
91 * Removes a notifier from the rfkill notifier chain.
92 */
93int unregister_rfkill_notifier(struct notifier_block *nb)
94{
Henrique de Moraes Holschuhf745ba02008-08-26 11:57:59 -030095 BUG_ON(!nb);
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -030096 return blocking_notifier_chain_unregister(&rfkill_notifier_list, nb);
97}
98EXPORT_SYMBOL_GPL(unregister_rfkill_notifier);
99
Michael Buesch135900c2007-09-27 21:33:12 +0200100
101static void rfkill_led_trigger(struct rfkill *rfkill,
102 enum rfkill_state state)
103{
104#ifdef CONFIG_RFKILL_LEDS
105 struct led_trigger *led = &rfkill->led_trigger;
106
107 if (!led->name)
108 return;
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300109 if (state != RFKILL_STATE_UNBLOCKED)
Michael Buesch135900c2007-09-27 21:33:12 +0200110 led_trigger_event(led, LED_OFF);
111 else
112 led_trigger_event(led, LED_FULL);
113#endif /* CONFIG_RFKILL_LEDS */
114}
115
Dmitry Baryshkov96185662008-07-22 14:21:59 +0400116#ifdef CONFIG_RFKILL_LEDS
117static void rfkill_led_trigger_activate(struct led_classdev *led)
118{
119 struct rfkill *rfkill = container_of(led->trigger,
120 struct rfkill, led_trigger);
121
122 rfkill_led_trigger(rfkill, rfkill->state);
123}
124#endif /* CONFIG_RFKILL_LEDS */
125
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300126static void notify_rfkill_state_change(struct rfkill *rfkill)
127{
128 blocking_notifier_call_chain(&rfkill_notifier_list,
129 RFKILL_STATE_CHANGED,
130 rfkill);
131}
132
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300133static void update_rfkill_state(struct rfkill *rfkill)
134{
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300135 enum rfkill_state newstate, oldstate;
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300136
137 if (rfkill->get_state) {
138 mutex_lock(&rfkill->mutex);
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300139 if (!rfkill->get_state(rfkill->data, &newstate)) {
140 oldstate = rfkill->state;
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300141 rfkill->state = newstate;
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300142 if (oldstate != newstate)
143 notify_rfkill_state_change(rfkill);
144 }
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300145 mutex_unlock(&rfkill->mutex);
146 }
147}
148
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300149/**
150 * rfkill_toggle_radio - wrapper for toggle_radio hook
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300151 * @rfkill: the rfkill struct to use
152 * @force: calls toggle_radio even if cache says it is not needed,
153 * and also makes sure notifications of the state will be
154 * sent even if it didn't change
155 * @state: the new state to call toggle_radio() with
156 *
Henrique de Moraes Holschuh0f687e92008-07-03 13:14:56 -0300157 * Calls rfkill->toggle_radio, enforcing the API for toggle_radio
158 * calls and handling all the red tape such as issuing notifications
159 * if the call is successful.
160 *
Henrique de Moraes Holschuhe10e0df2008-08-02 14:56:25 -0300161 * Suspended devices are not touched at all, and -EAGAIN is returned.
162 *
Henrique de Moraes Holschuh435307a2008-07-21 21:18:22 -0300163 * Note that the @force parameter cannot override a (possibly cached)
164 * state of RFKILL_STATE_HARD_BLOCKED. Any device making use of
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300165 * RFKILL_STATE_HARD_BLOCKED implements either get_state() or
166 * rfkill_force_state(), so the cache either is bypassed or valid.
167 *
168 * Note that we do call toggle_radio for RFKILL_STATE_SOFT_BLOCKED
169 * even if the radio is in RFKILL_STATE_HARD_BLOCKED state, so as to
170 * give the driver a hint that it should double-BLOCK the transmitter.
171 *
Henrique de Moraes Holschuh064af112008-07-21 21:18:20 -0300172 * Caller must have acquired rfkill->mutex.
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300173 */
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700174static int rfkill_toggle_radio(struct rfkill *rfkill,
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300175 enum rfkill_state state,
176 int force)
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700177{
Michael Buesch7f4c5342007-11-28 17:49:34 +0100178 int retval = 0;
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300179 enum rfkill_state oldstate, newstate;
180
Henrique de Moraes Holschuhe10e0df2008-08-02 14:56:25 -0300181 if (unlikely(rfkill->dev.power.power_state.event & PM_EVENT_SLEEP))
182 return -EBUSY;
183
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300184 oldstate = rfkill->state;
185
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300186 if (rfkill->get_state && !force &&
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300187 !rfkill->get_state(rfkill->data, &newstate))
188 rfkill->state = newstate;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700189
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300190 switch (state) {
191 case RFKILL_STATE_HARD_BLOCKED:
192 /* typically happens when refreshing hardware state,
193 * such as on resume */
194 state = RFKILL_STATE_SOFT_BLOCKED;
195 break;
196 case RFKILL_STATE_UNBLOCKED:
197 /* force can't override this, only rfkill_force_state() can */
198 if (rfkill->state == RFKILL_STATE_HARD_BLOCKED)
199 return -EPERM;
200 break;
201 case RFKILL_STATE_SOFT_BLOCKED:
202 /* nothing to do, we want to give drivers the hint to double
203 * BLOCK even a transmitter that is already in state
204 * RFKILL_STATE_HARD_BLOCKED */
205 break;
Henrique de Moraes Holschuh96c87602008-08-02 15:11:00 -0300206 default:
Henrique de Moraes Holschuhf745ba02008-08-26 11:57:59 -0300207 WARN(1, KERN_WARNING
208 "rfkill: illegal state %d passed as parameter "
209 "to rfkill_toggle_radio\n", state);
Henrique de Moraes Holschuh96c87602008-08-02 15:11:00 -0300210 return -EINVAL;
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300211 }
212
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300213 if (force || state != rfkill->state) {
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700214 retval = rfkill->toggle_radio(rfkill->data, state);
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300215 /* never allow a HARD->SOFT downgrade! */
216 if (!retval && rfkill->state != RFKILL_STATE_HARD_BLOCKED)
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700217 rfkill->state = state;
218 }
219
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300220 if (force || rfkill->state != oldstate) {
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300221 rfkill_led_trigger(rfkill, rfkill->state);
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300222 notify_rfkill_state_change(rfkill);
223 }
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300224
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700225 return retval;
226}
227
228/**
Henrique de Moraes Holschuh99619202008-08-02 15:10:58 -0300229 * __rfkill_switch_all - Toggle state of all switches of given type
Henrique de Moraes Holschuh435307a2008-07-21 21:18:22 -0300230 * @type: type of interfaces to be affected
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700231 * @state: the new state
232 *
Henrique de Moraes Holschuh435307a2008-07-21 21:18:22 -0300233 * This function toggles the state of all switches of given type,
234 * unless a specific switch is claimed by userspace (in which case,
Henrique de Moraes Holschuhe10e0df2008-08-02 14:56:25 -0300235 * that switch is left alone) or suspended.
Henrique de Moraes Holschuh99619202008-08-02 15:10:58 -0300236 *
237 * Caller must have acquired rfkill_mutex.
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700238 */
Henrique de Moraes Holschuh99619202008-08-02 15:10:58 -0300239static void __rfkill_switch_all(const enum rfkill_type type,
240 const enum rfkill_state state)
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700241{
242 struct rfkill *rfkill;
243
Henrique de Moraes Holschuhf745ba02008-08-26 11:57:59 -0300244 if (WARN((state >= RFKILL_STATE_MAX || type >= RFKILL_TYPE_MAX),
245 KERN_WARNING
246 "rfkill: illegal state %d or type %d "
247 "passed as parameter to __rfkill_switch_all\n",
248 state, type))
Henrique de Moraes Holschuh96c87602008-08-02 15:11:00 -0300249 return;
250
Henrique de Moraes Holschuh99619202008-08-02 15:10:58 -0300251 rfkill_global_states[type].current_state = state;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700252 list_for_each_entry(rfkill, &rfkill_list, node) {
Henrique de Moraes Holschuh064af112008-07-21 21:18:20 -0300253 if ((!rfkill->user_claim) && (rfkill->type == type)) {
254 mutex_lock(&rfkill->mutex);
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300255 rfkill_toggle_radio(rfkill, state, 0);
Henrique de Moraes Holschuh064af112008-07-21 21:18:20 -0300256 mutex_unlock(&rfkill->mutex);
257 }
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700258 }
Henrique de Moraes Holschuh99619202008-08-02 15:10:58 -0300259}
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700260
Henrique de Moraes Holschuh99619202008-08-02 15:10:58 -0300261/**
262 * rfkill_switch_all - Toggle state of all switches of given type
263 * @type: type of interfaces to be affected
264 * @state: the new state
265 *
266 * Acquires rfkill_mutex and calls __rfkill_switch_all(@type, @state).
267 * Please refer to __rfkill_switch_all() for details.
268 */
269void rfkill_switch_all(enum rfkill_type type, enum rfkill_state state)
270{
271 mutex_lock(&rfkill_mutex);
272 __rfkill_switch_all(type, state);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700273 mutex_unlock(&rfkill_mutex);
274}
275EXPORT_SYMBOL(rfkill_switch_all);
276
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300277/**
Henrique de Moraes Holschuh4081f002008-06-23 17:23:07 -0300278 * rfkill_epo - emergency power off all transmitters
279 *
Henrique de Moraes Holschuhe10e0df2008-08-02 14:56:25 -0300280 * This kicks all non-suspended rfkill devices to RFKILL_STATE_SOFT_BLOCKED,
281 * ignoring everything in its path but rfkill_mutex and rfkill->mutex.
Henrique de Moraes Holschuh99619202008-08-02 15:10:58 -0300282 *
283 * The global state before the EPO is saved and can be restored later
284 * using rfkill_restore_states().
Henrique de Moraes Holschuh4081f002008-06-23 17:23:07 -0300285 */
286void rfkill_epo(void)
287{
288 struct rfkill *rfkill;
Henrique de Moraes Holschuh99619202008-08-02 15:10:58 -0300289 int i;
Henrique de Moraes Holschuh4081f002008-06-23 17:23:07 -0300290
291 mutex_lock(&rfkill_mutex);
292 list_for_each_entry(rfkill, &rfkill_list, node) {
Henrique de Moraes Holschuh064af112008-07-21 21:18:20 -0300293 mutex_lock(&rfkill->mutex);
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300294 rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1);
Henrique de Moraes Holschuh064af112008-07-21 21:18:20 -0300295 mutex_unlock(&rfkill->mutex);
Henrique de Moraes Holschuh4081f002008-06-23 17:23:07 -0300296 }
Henrique de Moraes Holschuh99619202008-08-02 15:10:58 -0300297 for (i = 0; i < RFKILL_TYPE_MAX; i++) {
298 rfkill_global_states[i].default_state =
299 rfkill_global_states[i].current_state;
300 rfkill_global_states[i].current_state =
301 RFKILL_STATE_SOFT_BLOCKED;
302 }
Henrique de Moraes Holschuh4081f002008-06-23 17:23:07 -0300303 mutex_unlock(&rfkill_mutex);
304}
305EXPORT_SYMBOL_GPL(rfkill_epo);
306
307/**
Henrique de Moraes Holschuh99619202008-08-02 15:10:58 -0300308 * rfkill_restore_states - restore global states
309 *
310 * Restore (and sync switches to) the global state from the
311 * states in rfkill_default_states. This can undo the effects of
312 * a call to rfkill_epo().
313 */
314void rfkill_restore_states(void)
315{
316 int i;
317
318 mutex_lock(&rfkill_mutex);
319 for (i = 0; i < RFKILL_TYPE_MAX; i++)
320 __rfkill_switch_all(i, rfkill_global_states[i].default_state);
321 mutex_unlock(&rfkill_mutex);
322}
323EXPORT_SYMBOL_GPL(rfkill_restore_states);
324
325/**
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300326 * rfkill_force_state - Force the internal rfkill radio state
327 * @rfkill: pointer to the rfkill class to modify.
328 * @state: the current radio state the class should be forced to.
329 *
330 * This function updates the internal state of the radio cached
331 * by the rfkill class. It should be used when the driver gets
332 * a notification by the firmware/hardware of the current *real*
333 * state of the radio rfkill switch.
334 *
Henrique de Moraes Holschuh2fd9b222008-07-21 21:18:17 -0300335 * Devices which are subject to external changes on their rfkill
336 * state (such as those caused by a hardware rfkill line) MUST
337 * have their driver arrange to call rfkill_force_state() as soon
338 * as possible after such a change.
339 *
340 * This function may not be called from an atomic context.
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300341 */
342int rfkill_force_state(struct rfkill *rfkill, enum rfkill_state state)
343{
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300344 enum rfkill_state oldstate;
345
Henrique de Moraes Holschuhf745ba02008-08-26 11:57:59 -0300346 BUG_ON(!rfkill);
347 if (WARN((state >= RFKILL_STATE_MAX),
348 KERN_WARNING
349 "rfkill: illegal state %d passed as parameter "
350 "to rfkill_force_state\n", state))
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300351 return -EINVAL;
352
353 mutex_lock(&rfkill->mutex);
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300354
355 oldstate = rfkill->state;
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300356 rfkill->state = state;
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300357
358 if (state != oldstate)
359 notify_rfkill_state_change(rfkill);
360
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300361 mutex_unlock(&rfkill->mutex);
362
363 return 0;
364}
365EXPORT_SYMBOL(rfkill_force_state);
366
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700367static ssize_t rfkill_name_show(struct device *dev,
368 struct device_attribute *attr,
369 char *buf)
370{
371 struct rfkill *rfkill = to_rfkill(dev);
372
373 return sprintf(buf, "%s\n", rfkill->name);
374}
375
Henrique de Moraes Holschuh99c632e2008-06-23 17:23:04 -0300376static const char *rfkill_get_type_str(enum rfkill_type type)
377{
378 switch (type) {
379 case RFKILL_TYPE_WLAN:
380 return "wlan";
381 case RFKILL_TYPE_BLUETOOTH:
382 return "bluetooth";
383 case RFKILL_TYPE_UWB:
384 return "ultrawideband";
385 case RFKILL_TYPE_WIMAX:
386 return "wimax";
387 case RFKILL_TYPE_WWAN:
388 return "wwan";
389 default:
390 BUG();
391 }
392}
393
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700394static ssize_t rfkill_type_show(struct device *dev,
395 struct device_attribute *attr,
396 char *buf)
397{
398 struct rfkill *rfkill = to_rfkill(dev);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700399
Henrique de Moraes Holschuh99c632e2008-06-23 17:23:04 -0300400 return sprintf(buf, "%s\n", rfkill_get_type_str(rfkill->type));
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700401}
402
403static ssize_t rfkill_state_show(struct device *dev,
404 struct device_attribute *attr,
405 char *buf)
406{
407 struct rfkill *rfkill = to_rfkill(dev);
408
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300409 update_rfkill_state(rfkill);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700410 return sprintf(buf, "%d\n", rfkill->state);
411}
412
413static ssize_t rfkill_state_store(struct device *dev,
414 struct device_attribute *attr,
415 const char *buf, size_t count)
416{
417 struct rfkill *rfkill = to_rfkill(dev);
Henrique de Moraes Holschuh849e0572008-08-26 11:57:57 -0300418 unsigned long state;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700419 int error;
420
421 if (!capable(CAP_NET_ADMIN))
422 return -EPERM;
423
Henrique de Moraes Holschuh849e0572008-08-26 11:57:57 -0300424 error = strict_strtoul(buf, 0, &state);
425 if (error)
426 return error;
427
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300428 /* RFKILL_STATE_HARD_BLOCKED is illegal here... */
429 if (state != RFKILL_STATE_UNBLOCKED &&
430 state != RFKILL_STATE_SOFT_BLOCKED)
431 return -EINVAL;
432
Michael Buesch7f4c5342007-11-28 17:49:34 +0100433 if (mutex_lock_interruptible(&rfkill->mutex))
434 return -ERESTARTSYS;
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300435 error = rfkill_toggle_radio(rfkill, state, 0);
Michael Buesch7f4c5342007-11-28 17:49:34 +0100436 mutex_unlock(&rfkill->mutex);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700437
Michael Buesch7f4c5342007-11-28 17:49:34 +0100438 return error ? error : count;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700439}
440
441static ssize_t rfkill_claim_show(struct device *dev,
442 struct device_attribute *attr,
443 char *buf)
444{
445 struct rfkill *rfkill = to_rfkill(dev);
446
Felipe Balbi01b510b2008-08-26 11:57:58 -0300447 return sprintf(buf, "%d\n", rfkill->user_claim);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700448}
449
450static ssize_t rfkill_claim_store(struct device *dev,
451 struct device_attribute *attr,
452 const char *buf, size_t count)
453{
454 struct rfkill *rfkill = to_rfkill(dev);
Henrique de Moraes Holschuh849e0572008-08-26 11:57:57 -0300455 unsigned long claim_tmp;
456 bool claim;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700457 int error;
458
459 if (!capable(CAP_NET_ADMIN))
460 return -EPERM;
461
Henrique de Moraes Holschuh064af112008-07-21 21:18:20 -0300462 if (rfkill->user_claim_unsupported)
463 return -EOPNOTSUPP;
464
Henrique de Moraes Holschuh849e0572008-08-26 11:57:57 -0300465 error = strict_strtoul(buf, 0, &claim_tmp);
466 if (error)
467 return error;
468 claim = !!claim_tmp;
469
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700470 /*
471 * Take the global lock to make sure the kernel is not in
472 * the middle of rfkill_switch_all
473 */
474 error = mutex_lock_interruptible(&rfkill_mutex);
475 if (error)
476 return error;
477
478 if (rfkill->user_claim != claim) {
Henrique de Moraes Holschuh064af112008-07-21 21:18:20 -0300479 if (!claim) {
480 mutex_lock(&rfkill->mutex);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700481 rfkill_toggle_radio(rfkill,
Henrique de Moraes Holschuh99619202008-08-02 15:10:58 -0300482 rfkill_global_states[rfkill->type].current_state,
483 0);
Henrique de Moraes Holschuh064af112008-07-21 21:18:20 -0300484 mutex_unlock(&rfkill->mutex);
485 }
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700486 rfkill->user_claim = claim;
487 }
488
489 mutex_unlock(&rfkill_mutex);
490
Michael Buesch20405c02007-09-27 21:34:23 +0200491 return error ? error : count;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700492}
493
494static struct device_attribute rfkill_dev_attrs[] = {
495 __ATTR(name, S_IRUGO, rfkill_name_show, NULL),
496 __ATTR(type, S_IRUGO, rfkill_type_show, NULL),
Ivo van Doornc81de6a2007-07-18 15:38:03 -0700497 __ATTR(state, S_IRUGO|S_IWUSR, rfkill_state_show, rfkill_state_store),
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700498 __ATTR(claim, S_IRUGO|S_IWUSR, rfkill_claim_show, rfkill_claim_store),
499 __ATTR_NULL
500};
501
502static void rfkill_release(struct device *dev)
503{
504 struct rfkill *rfkill = to_rfkill(dev);
505
506 kfree(rfkill);
507 module_put(THIS_MODULE);
508}
509
510#ifdef CONFIG_PM
511static int rfkill_suspend(struct device *dev, pm_message_t state)
512{
513 struct rfkill *rfkill = to_rfkill(dev);
514
515 if (dev->power.power_state.event != state.event) {
Rafael J. Wysocki3a2d5b72008-02-23 19:13:25 +0100516 if (state.event & PM_EVENT_SLEEP) {
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300517 /* Stop transmitter, keep state, no notifies */
518 update_rfkill_state(rfkill);
519
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700520 mutex_lock(&rfkill->mutex);
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300521 rfkill->toggle_radio(rfkill->data,
522 RFKILL_STATE_SOFT_BLOCKED);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700523 mutex_unlock(&rfkill->mutex);
524 }
525
526 dev->power.power_state = state;
527 }
528
529 return 0;
530}
531
532static int rfkill_resume(struct device *dev)
533{
534 struct rfkill *rfkill = to_rfkill(dev);
535
536 if (dev->power.power_state.event != PM_EVENT_ON) {
537 mutex_lock(&rfkill->mutex);
538
Henrique de Moraes Holschuhe10e0df2008-08-02 14:56:25 -0300539 dev->power.power_state.event = PM_EVENT_ON;
540
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300541 /* restore radio state AND notify everybody */
542 rfkill_toggle_radio(rfkill, rfkill->state, 1);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700543
544 mutex_unlock(&rfkill->mutex);
545 }
546
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700547 return 0;
548}
549#else
550#define rfkill_suspend NULL
551#define rfkill_resume NULL
552#endif
553
Henrique de Moraes Holschuhffb67c32008-06-23 17:23:05 -0300554static int rfkill_blocking_uevent_notifier(struct notifier_block *nb,
555 unsigned long eventid,
556 void *data)
557{
558 struct rfkill *rfkill = (struct rfkill *)data;
559
560 switch (eventid) {
561 case RFKILL_STATE_CHANGED:
562 kobject_uevent(&rfkill->dev.kobj, KOBJ_CHANGE);
563 break;
564 default:
565 break;
566 }
567
568 return NOTIFY_DONE;
569}
570
571static struct notifier_block rfkill_blocking_uevent_nb = {
572 .notifier_call = rfkill_blocking_uevent_notifier,
573 .priority = 0,
574};
575
576static int rfkill_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
577{
578 struct rfkill *rfkill = to_rfkill(dev);
579 int error;
580
581 error = add_uevent_var(env, "RFKILL_NAME=%s", rfkill->name);
582 if (error)
583 return error;
584 error = add_uevent_var(env, "RFKILL_TYPE=%s",
585 rfkill_get_type_str(rfkill->type));
586 if (error)
587 return error;
588 error = add_uevent_var(env, "RFKILL_STATE=%d", rfkill->state);
589 return error;
590}
591
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700592static struct class rfkill_class = {
593 .name = "rfkill",
594 .dev_release = rfkill_release,
595 .dev_attrs = rfkill_dev_attrs,
596 .suspend = rfkill_suspend,
597 .resume = rfkill_resume,
Henrique de Moraes Holschuhffb67c32008-06-23 17:23:05 -0300598 .dev_uevent = rfkill_dev_uevent,
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700599};
600
Henrique de Moraes Holschuh02589f62008-08-02 15:10:57 -0300601static int rfkill_check_duplicity(const struct rfkill *rfkill)
602{
603 struct rfkill *p;
604 unsigned long seen[BITS_TO_LONGS(RFKILL_TYPE_MAX)];
605
606 memset(seen, 0, sizeof(seen));
607
608 list_for_each_entry(p, &rfkill_list, node) {
Henrique de Moraes Holschuhf745ba02008-08-26 11:57:59 -0300609 if (WARN((p == rfkill), KERN_WARNING
610 "rfkill: illegal attempt to register "
611 "an already registered rfkill struct\n"))
Henrique de Moraes Holschuh02589f62008-08-02 15:10:57 -0300612 return -EEXIST;
Henrique de Moraes Holschuh02589f62008-08-02 15:10:57 -0300613 set_bit(p->type, seen);
614 }
615
616 /* 0: first switch of its kind */
617 return test_bit(rfkill->type, seen);
618}
619
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700620static int rfkill_add_switch(struct rfkill *rfkill)
621{
Henrique de Moraes Holschuh02589f62008-08-02 15:10:57 -0300622 int error;
623
Michael Buesch7319f1e2007-10-28 15:16:50 +0100624 mutex_lock(&rfkill_mutex);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700625
Henrique de Moraes Holschuh02589f62008-08-02 15:10:57 -0300626 error = rfkill_check_duplicity(rfkill);
627 if (error < 0)
628 goto unlock_out;
629
Henrique de Moraes Holschuh99619202008-08-02 15:10:58 -0300630 if (!error) {
631 /* lock default after first use */
632 set_bit(rfkill->type, rfkill_states_lockdflt);
633 rfkill_global_states[rfkill->type].current_state =
634 rfkill_global_states[rfkill->type].default_state;
635 }
636
637 rfkill_toggle_radio(rfkill,
638 rfkill_global_states[rfkill->type].current_state,
639 0);
Henrique de Moraes Holschuhfd4484a2008-07-03 13:14:57 -0300640
641 list_add_tail(&rfkill->node, &rfkill_list);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700642
Henrique de Moraes Holschuh02589f62008-08-02 15:10:57 -0300643 error = 0;
644unlock_out:
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700645 mutex_unlock(&rfkill_mutex);
Michael Buesch7319f1e2007-10-28 15:16:50 +0100646
Henrique de Moraes Holschuh02589f62008-08-02 15:10:57 -0300647 return error;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700648}
649
650static void rfkill_remove_switch(struct rfkill *rfkill)
651{
652 mutex_lock(&rfkill_mutex);
653 list_del_init(&rfkill->node);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700654 mutex_unlock(&rfkill_mutex);
Henrique de Moraes Holschuh064af112008-07-21 21:18:20 -0300655
656 mutex_lock(&rfkill->mutex);
657 rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1);
658 mutex_unlock(&rfkill->mutex);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700659}
660
661/**
662 * rfkill_allocate - allocate memory for rfkill structure.
663 * @parent: device that has rf switch on it
Ivo van Doorn234a0ca2007-09-13 09:20:42 +0200664 * @type: type of the switch (RFKILL_TYPE_*)
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700665 *
666 * This function should be called by the network driver when it needs
Henrique de Moraes Holschuh435307a2008-07-21 21:18:22 -0300667 * rfkill structure. Once the structure is allocated the driver should
668 * finish its initialization by setting the name, private data, enable_radio
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700669 * and disable_radio methods and then register it with rfkill_register().
Henrique de Moraes Holschuh435307a2008-07-21 21:18:22 -0300670 *
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700671 * NOTE: If registration fails the structure shoudl be freed by calling
672 * rfkill_free() otherwise rfkill_unregister() should be used.
673 */
Henrique de Moraes Holschuh77fba132008-08-02 15:10:59 -0300674struct rfkill * __must_check rfkill_allocate(struct device *parent,
675 enum rfkill_type type)
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700676{
677 struct rfkill *rfkill;
678 struct device *dev;
679
Henrique de Moraes Holschuhf745ba02008-08-26 11:57:59 -0300680 if (WARN((type >= RFKILL_TYPE_MAX),
681 KERN_WARNING
682 "rfkill: illegal type %d passed as parameter "
683 "to rfkill_allocate\n", type))
684 return NULL;
685
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700686 rfkill = kzalloc(sizeof(struct rfkill), GFP_KERNEL);
Ivo van Doornd007da12007-05-19 12:24:39 -0700687 if (!rfkill)
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700688 return NULL;
689
690 mutex_init(&rfkill->mutex);
691 INIT_LIST_HEAD(&rfkill->node);
692 rfkill->type = type;
693
694 dev = &rfkill->dev;
695 dev->class = &rfkill_class;
696 dev->parent = parent;
697 device_initialize(dev);
698
699 __module_get(THIS_MODULE);
700
701 return rfkill;
702}
703EXPORT_SYMBOL(rfkill_allocate);
704
705/**
706 * rfkill_free - Mark rfkill structure for deletion
707 * @rfkill: rfkill structure to be destroyed
708 *
Henrique de Moraes Holschuh435307a2008-07-21 21:18:22 -0300709 * Decrements reference count of the rfkill structure so it is destroyed.
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700710 * Note that rfkill_free() should _not_ be called after rfkill_unregister().
711 */
712void rfkill_free(struct rfkill *rfkill)
713{
714 if (rfkill)
715 put_device(&rfkill->dev);
716}
717EXPORT_SYMBOL(rfkill_free);
718
Michael Buesch135900c2007-09-27 21:33:12 +0200719static void rfkill_led_trigger_register(struct rfkill *rfkill)
720{
721#ifdef CONFIG_RFKILL_LEDS
722 int error;
723
Dmitry Baryshkov7c4f4572008-07-22 14:17:37 +0400724 if (!rfkill->led_trigger.name)
725 rfkill->led_trigger.name = rfkill->dev.bus_id;
Dmitry Baryshkov96185662008-07-22 14:21:59 +0400726 if (!rfkill->led_trigger.activate)
727 rfkill->led_trigger.activate = rfkill_led_trigger_activate;
Michael Buesch135900c2007-09-27 21:33:12 +0200728 error = led_trigger_register(&rfkill->led_trigger);
729 if (error)
730 rfkill->led_trigger.name = NULL;
731#endif /* CONFIG_RFKILL_LEDS */
732}
733
734static void rfkill_led_trigger_unregister(struct rfkill *rfkill)
735{
736#ifdef CONFIG_RFKILL_LEDS
Henrique de Moraes Holschuh37f55e92008-07-21 21:18:18 -0300737 if (rfkill->led_trigger.name) {
Michael Buesch135900c2007-09-27 21:33:12 +0200738 led_trigger_unregister(&rfkill->led_trigger);
Henrique de Moraes Holschuh37f55e92008-07-21 21:18:18 -0300739 rfkill->led_trigger.name = NULL;
740 }
Michael Buesch135900c2007-09-27 21:33:12 +0200741#endif
742}
743
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700744/**
745 * rfkill_register - Register a rfkill structure.
746 * @rfkill: rfkill structure to be registered
747 *
748 * This function should be called by the network driver when the rfkill
749 * structure needs to be registered. Immediately from registration the
750 * switch driver should be able to service calls to toggle_radio.
751 */
Henrique de Moraes Holschuh77fba132008-08-02 15:10:59 -0300752int __must_check rfkill_register(struct rfkill *rfkill)
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700753{
754 static atomic_t rfkill_no = ATOMIC_INIT(0);
755 struct device *dev = &rfkill->dev;
756 int error;
757
Henrique de Moraes Holschuhf745ba02008-08-26 11:57:59 -0300758 if (WARN((!rfkill || !rfkill->toggle_radio ||
759 rfkill->type >= RFKILL_TYPE_MAX ||
760 rfkill->state >= RFKILL_STATE_MAX),
761 KERN_WARNING
762 "rfkill: attempt to register a "
763 "badly initialized rfkill struct\n"))
Henrique de Moraes Holschuh96c87602008-08-02 15:11:00 -0300764 return -EINVAL;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700765
Michael Buesch8a8f1c02007-10-28 13:07:54 +0100766 snprintf(dev->bus_id, sizeof(dev->bus_id),
767 "rfkill%ld", (long)atomic_inc_return(&rfkill_no) - 1);
768
769 rfkill_led_trigger_register(rfkill);
770
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700771 error = rfkill_add_switch(rfkill);
Eric Paris632041f2008-01-13 16:20:56 -0500772 if (error) {
773 rfkill_led_trigger_unregister(rfkill);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700774 return error;
Eric Paris632041f2008-01-13 16:20:56 -0500775 }
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700776
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700777 error = device_add(dev);
778 if (error) {
779 rfkill_remove_switch(rfkill);
Henrique de Moraes Holschuh37f55e92008-07-21 21:18:18 -0300780 rfkill_led_trigger_unregister(rfkill);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700781 return error;
782 }
783
784 return 0;
785}
786EXPORT_SYMBOL(rfkill_register);
787
788/**
Henrique de Moraes Holschuhc8fcd902008-06-23 17:22:57 -0300789 * rfkill_unregister - Unregister a rfkill structure.
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700790 * @rfkill: rfkill structure to be unregistered
791 *
792 * This function should be called by the network driver during device
793 * teardown to destroy rfkill structure. Note that rfkill_free() should
794 * _not_ be called after rfkill_unregister().
795 */
796void rfkill_unregister(struct rfkill *rfkill)
797{
Henrique de Moraes Holschuhf745ba02008-08-26 11:57:59 -0300798 BUG_ON(!rfkill);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700799 device_del(&rfkill->dev);
800 rfkill_remove_switch(rfkill);
Michael Buesch8a8f1c02007-10-28 13:07:54 +0100801 rfkill_led_trigger_unregister(rfkill);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700802 put_device(&rfkill->dev);
803}
804EXPORT_SYMBOL(rfkill_unregister);
805
Henrique de Moraes Holschuh99619202008-08-02 15:10:58 -0300806/**
807 * rfkill_set_default - set initial value for a switch type
808 * @type - the type of switch to set the default state of
809 * @state - the new default state for that group of switches
810 *
811 * Sets the initial state rfkill should use for a given type.
812 * The following initial states are allowed: RFKILL_STATE_SOFT_BLOCKED
813 * and RFKILL_STATE_UNBLOCKED.
814 *
815 * This function is meant to be used by platform drivers for platforms
816 * that can save switch state across power down/reboot.
817 *
818 * The default state for each switch type can be changed exactly once.
819 * After a switch of that type is registered, the default state cannot
820 * be changed anymore. This guards against multiple drivers it the
821 * same platform trying to set the initial switch default state, which
822 * is not allowed.
823 *
824 * Returns -EPERM if the state has already been set once or is in use,
825 * so drivers likely want to either ignore or at most printk(KERN_NOTICE)
826 * if this function returns -EPERM.
827 *
828 * Returns 0 if the new default state was set, or an error if it
829 * could not be set.
830 */
831int rfkill_set_default(enum rfkill_type type, enum rfkill_state state)
832{
833 int error;
834
Henrique de Moraes Holschuhf745ba02008-08-26 11:57:59 -0300835 if (WARN((type >= RFKILL_TYPE_MAX ||
836 (state != RFKILL_STATE_SOFT_BLOCKED &&
837 state != RFKILL_STATE_UNBLOCKED)),
838 KERN_WARNING
839 "rfkill: illegal state %d or type %d passed as "
840 "parameter to rfkill_set_default\n", state, type))
Henrique de Moraes Holschuh99619202008-08-02 15:10:58 -0300841 return -EINVAL;
842
843 mutex_lock(&rfkill_mutex);
844
845 if (!test_and_set_bit(type, rfkill_states_lockdflt)) {
846 rfkill_global_states[type].default_state = state;
847 error = 0;
848 } else
849 error = -EPERM;
850
851 mutex_unlock(&rfkill_mutex);
852 return error;
853}
854EXPORT_SYMBOL_GPL(rfkill_set_default);
855
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700856/*
857 * Rfkill module initialization/deinitialization.
858 */
859static int __init rfkill_init(void)
860{
861 int error;
862 int i;
863
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300864 /* RFKILL_STATE_HARD_BLOCKED is illegal here... */
865 if (rfkill_default_state != RFKILL_STATE_SOFT_BLOCKED &&
866 rfkill_default_state != RFKILL_STATE_UNBLOCKED)
Henrique de Moraes Holschuhe954b0b2008-06-23 17:22:59 -0300867 return -EINVAL;
868
Henrique de Moraes Holschuh99619202008-08-02 15:10:58 -0300869 for (i = 0; i < RFKILL_TYPE_MAX; i++)
870 rfkill_global_states[i].default_state = rfkill_default_state;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700871
872 error = class_register(&rfkill_class);
873 if (error) {
874 printk(KERN_ERR "rfkill: unable to register rfkill class\n");
875 return error;
876 }
877
Henrique de Moraes Holschuhffb67c32008-06-23 17:23:05 -0300878 register_rfkill_notifier(&rfkill_blocking_uevent_nb);
879
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700880 return 0;
881}
882
883static void __exit rfkill_exit(void)
884{
Henrique de Moraes Holschuhffb67c32008-06-23 17:23:05 -0300885 unregister_rfkill_notifier(&rfkill_blocking_uevent_nb);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700886 class_unregister(&rfkill_class);
887}
888
Michael Buesch2bf236d2007-10-28 14:39:02 +0100889subsys_initcall(rfkill_init);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700890module_exit(rfkill_exit);