blob: b630f358d6741daed06725fb331f55fad1a974af [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{
79 return blocking_notifier_chain_register(&rfkill_notifier_list, nb);
80}
81EXPORT_SYMBOL_GPL(register_rfkill_notifier);
82
83/**
84 * unregister_rfkill_notifier - remove notifier from rfkill notifier chain
85 * @nb: pointer to the entry to remove from the chain
86 *
87 * See blocking_notifier_chain_unregister() for return value and further
88 * observations.
89 *
90 * Removes a notifier from the rfkill notifier chain.
91 */
92int unregister_rfkill_notifier(struct notifier_block *nb)
93{
94 return blocking_notifier_chain_unregister(&rfkill_notifier_list, nb);
95}
96EXPORT_SYMBOL_GPL(unregister_rfkill_notifier);
97
Michael Buesch135900c2007-09-27 21:33:12 +020098
99static void rfkill_led_trigger(struct rfkill *rfkill,
100 enum rfkill_state state)
101{
102#ifdef CONFIG_RFKILL_LEDS
103 struct led_trigger *led = &rfkill->led_trigger;
104
105 if (!led->name)
106 return;
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300107 if (state != RFKILL_STATE_UNBLOCKED)
Michael Buesch135900c2007-09-27 21:33:12 +0200108 led_trigger_event(led, LED_OFF);
109 else
110 led_trigger_event(led, LED_FULL);
111#endif /* CONFIG_RFKILL_LEDS */
112}
113
Dmitry Baryshkov96185662008-07-22 14:21:59 +0400114#ifdef CONFIG_RFKILL_LEDS
115static void rfkill_led_trigger_activate(struct led_classdev *led)
116{
117 struct rfkill *rfkill = container_of(led->trigger,
118 struct rfkill, led_trigger);
119
120 rfkill_led_trigger(rfkill, rfkill->state);
121}
122#endif /* CONFIG_RFKILL_LEDS */
123
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300124static void notify_rfkill_state_change(struct rfkill *rfkill)
125{
126 blocking_notifier_call_chain(&rfkill_notifier_list,
127 RFKILL_STATE_CHANGED,
128 rfkill);
129}
130
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300131static void update_rfkill_state(struct rfkill *rfkill)
132{
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300133 enum rfkill_state newstate, oldstate;
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300134
135 if (rfkill->get_state) {
136 mutex_lock(&rfkill->mutex);
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300137 if (!rfkill->get_state(rfkill->data, &newstate)) {
138 oldstate = rfkill->state;
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300139 rfkill->state = newstate;
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300140 if (oldstate != newstate)
141 notify_rfkill_state_change(rfkill);
142 }
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300143 mutex_unlock(&rfkill->mutex);
144 }
145}
146
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300147/**
148 * rfkill_toggle_radio - wrapper for toggle_radio hook
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300149 * @rfkill: the rfkill struct to use
150 * @force: calls toggle_radio even if cache says it is not needed,
151 * and also makes sure notifications of the state will be
152 * sent even if it didn't change
153 * @state: the new state to call toggle_radio() with
154 *
Henrique de Moraes Holschuh0f687e92008-07-03 13:14:56 -0300155 * Calls rfkill->toggle_radio, enforcing the API for toggle_radio
156 * calls and handling all the red tape such as issuing notifications
157 * if the call is successful.
158 *
Henrique de Moraes Holschuhe10e0df2008-08-02 14:56:25 -0300159 * Suspended devices are not touched at all, and -EAGAIN is returned.
160 *
Henrique de Moraes Holschuh435307a2008-07-21 21:18:22 -0300161 * Note that the @force parameter cannot override a (possibly cached)
162 * state of RFKILL_STATE_HARD_BLOCKED. Any device making use of
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300163 * RFKILL_STATE_HARD_BLOCKED implements either get_state() or
164 * rfkill_force_state(), so the cache either is bypassed or valid.
165 *
166 * Note that we do call toggle_radio for RFKILL_STATE_SOFT_BLOCKED
167 * even if the radio is in RFKILL_STATE_HARD_BLOCKED state, so as to
168 * give the driver a hint that it should double-BLOCK the transmitter.
169 *
Henrique de Moraes Holschuh064af112008-07-21 21:18:20 -0300170 * Caller must have acquired rfkill->mutex.
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300171 */
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700172static int rfkill_toggle_radio(struct rfkill *rfkill,
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300173 enum rfkill_state state,
174 int force)
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700175{
Michael Buesch7f4c5342007-11-28 17:49:34 +0100176 int retval = 0;
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300177 enum rfkill_state oldstate, newstate;
178
Henrique de Moraes Holschuhe10e0df2008-08-02 14:56:25 -0300179 if (unlikely(rfkill->dev.power.power_state.event & PM_EVENT_SLEEP))
180 return -EBUSY;
181
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300182 oldstate = rfkill->state;
183
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300184 if (rfkill->get_state && !force &&
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300185 !rfkill->get_state(rfkill->data, &newstate))
186 rfkill->state = newstate;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700187
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300188 switch (state) {
189 case RFKILL_STATE_HARD_BLOCKED:
190 /* typically happens when refreshing hardware state,
191 * such as on resume */
192 state = RFKILL_STATE_SOFT_BLOCKED;
193 break;
194 case RFKILL_STATE_UNBLOCKED:
195 /* force can't override this, only rfkill_force_state() can */
196 if (rfkill->state == RFKILL_STATE_HARD_BLOCKED)
197 return -EPERM;
198 break;
199 case RFKILL_STATE_SOFT_BLOCKED:
200 /* nothing to do, we want to give drivers the hint to double
201 * BLOCK even a transmitter that is already in state
202 * RFKILL_STATE_HARD_BLOCKED */
203 break;
Henrique de Moraes Holschuh96c87602008-08-02 15:11:00 -0300204 default:
205 return -EINVAL;
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300206 }
207
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300208 if (force || state != rfkill->state) {
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700209 retval = rfkill->toggle_radio(rfkill->data, state);
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300210 /* never allow a HARD->SOFT downgrade! */
211 if (!retval && rfkill->state != RFKILL_STATE_HARD_BLOCKED)
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700212 rfkill->state = state;
213 }
214
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300215 if (force || rfkill->state != oldstate) {
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300216 rfkill_led_trigger(rfkill, rfkill->state);
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300217 notify_rfkill_state_change(rfkill);
218 }
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300219
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700220 return retval;
221}
222
223/**
Henrique de Moraes Holschuh99619202008-08-02 15:10:58 -0300224 * __rfkill_switch_all - Toggle state of all switches of given type
Henrique de Moraes Holschuh435307a2008-07-21 21:18:22 -0300225 * @type: type of interfaces to be affected
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700226 * @state: the new state
227 *
Henrique de Moraes Holschuh435307a2008-07-21 21:18:22 -0300228 * This function toggles the state of all switches of given type,
229 * unless a specific switch is claimed by userspace (in which case,
Henrique de Moraes Holschuhe10e0df2008-08-02 14:56:25 -0300230 * that switch is left alone) or suspended.
Henrique de Moraes Holschuh99619202008-08-02 15:10:58 -0300231 *
232 * Caller must have acquired rfkill_mutex.
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700233 */
Henrique de Moraes Holschuh99619202008-08-02 15:10:58 -0300234static void __rfkill_switch_all(const enum rfkill_type type,
235 const enum rfkill_state state)
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700236{
237 struct rfkill *rfkill;
238
Henrique de Moraes Holschuh96c87602008-08-02 15:11:00 -0300239 if (unlikely(state >= RFKILL_STATE_MAX))
240 return;
241
Henrique de Moraes Holschuh99619202008-08-02 15:10:58 -0300242 rfkill_global_states[type].current_state = state;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700243 list_for_each_entry(rfkill, &rfkill_list, node) {
Henrique de Moraes Holschuh064af112008-07-21 21:18:20 -0300244 if ((!rfkill->user_claim) && (rfkill->type == type)) {
245 mutex_lock(&rfkill->mutex);
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300246 rfkill_toggle_radio(rfkill, state, 0);
Henrique de Moraes Holschuh064af112008-07-21 21:18:20 -0300247 mutex_unlock(&rfkill->mutex);
248 }
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700249 }
Henrique de Moraes Holschuh99619202008-08-02 15:10:58 -0300250}
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700251
Henrique de Moraes Holschuh99619202008-08-02 15:10:58 -0300252/**
253 * rfkill_switch_all - Toggle state of all switches of given type
254 * @type: type of interfaces to be affected
255 * @state: the new state
256 *
257 * Acquires rfkill_mutex and calls __rfkill_switch_all(@type, @state).
258 * Please refer to __rfkill_switch_all() for details.
259 */
260void rfkill_switch_all(enum rfkill_type type, enum rfkill_state state)
261{
262 mutex_lock(&rfkill_mutex);
263 __rfkill_switch_all(type, state);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700264 mutex_unlock(&rfkill_mutex);
265}
266EXPORT_SYMBOL(rfkill_switch_all);
267
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300268/**
Henrique de Moraes Holschuh4081f002008-06-23 17:23:07 -0300269 * rfkill_epo - emergency power off all transmitters
270 *
Henrique de Moraes Holschuhe10e0df2008-08-02 14:56:25 -0300271 * This kicks all non-suspended rfkill devices to RFKILL_STATE_SOFT_BLOCKED,
272 * ignoring everything in its path but rfkill_mutex and rfkill->mutex.
Henrique de Moraes Holschuh99619202008-08-02 15:10:58 -0300273 *
274 * The global state before the EPO is saved and can be restored later
275 * using rfkill_restore_states().
Henrique de Moraes Holschuh4081f002008-06-23 17:23:07 -0300276 */
277void rfkill_epo(void)
278{
279 struct rfkill *rfkill;
Henrique de Moraes Holschuh99619202008-08-02 15:10:58 -0300280 int i;
Henrique de Moraes Holschuh4081f002008-06-23 17:23:07 -0300281
282 mutex_lock(&rfkill_mutex);
283 list_for_each_entry(rfkill, &rfkill_list, node) {
Henrique de Moraes Holschuh064af112008-07-21 21:18:20 -0300284 mutex_lock(&rfkill->mutex);
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300285 rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1);
Henrique de Moraes Holschuh064af112008-07-21 21:18:20 -0300286 mutex_unlock(&rfkill->mutex);
Henrique de Moraes Holschuh4081f002008-06-23 17:23:07 -0300287 }
Henrique de Moraes Holschuh99619202008-08-02 15:10:58 -0300288 for (i = 0; i < RFKILL_TYPE_MAX; i++) {
289 rfkill_global_states[i].default_state =
290 rfkill_global_states[i].current_state;
291 rfkill_global_states[i].current_state =
292 RFKILL_STATE_SOFT_BLOCKED;
293 }
Henrique de Moraes Holschuh4081f002008-06-23 17:23:07 -0300294 mutex_unlock(&rfkill_mutex);
295}
296EXPORT_SYMBOL_GPL(rfkill_epo);
297
298/**
Henrique de Moraes Holschuh99619202008-08-02 15:10:58 -0300299 * rfkill_restore_states - restore global states
300 *
301 * Restore (and sync switches to) the global state from the
302 * states in rfkill_default_states. This can undo the effects of
303 * a call to rfkill_epo().
304 */
305void rfkill_restore_states(void)
306{
307 int i;
308
309 mutex_lock(&rfkill_mutex);
310 for (i = 0; i < RFKILL_TYPE_MAX; i++)
311 __rfkill_switch_all(i, rfkill_global_states[i].default_state);
312 mutex_unlock(&rfkill_mutex);
313}
314EXPORT_SYMBOL_GPL(rfkill_restore_states);
315
316/**
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300317 * rfkill_force_state - Force the internal rfkill radio state
318 * @rfkill: pointer to the rfkill class to modify.
319 * @state: the current radio state the class should be forced to.
320 *
321 * This function updates the internal state of the radio cached
322 * by the rfkill class. It should be used when the driver gets
323 * a notification by the firmware/hardware of the current *real*
324 * state of the radio rfkill switch.
325 *
Henrique de Moraes Holschuh2fd9b222008-07-21 21:18:17 -0300326 * Devices which are subject to external changes on their rfkill
327 * state (such as those caused by a hardware rfkill line) MUST
328 * have their driver arrange to call rfkill_force_state() as soon
329 * as possible after such a change.
330 *
331 * This function may not be called from an atomic context.
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300332 */
333int rfkill_force_state(struct rfkill *rfkill, enum rfkill_state state)
334{
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300335 enum rfkill_state oldstate;
336
Henrique de Moraes Holschuh96c87602008-08-02 15:11:00 -0300337 if (unlikely(state >= RFKILL_STATE_MAX))
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300338 return -EINVAL;
339
340 mutex_lock(&rfkill->mutex);
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300341
342 oldstate = rfkill->state;
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300343 rfkill->state = state;
Henrique de Moraes Holschuh79399a82008-06-23 17:23:03 -0300344
345 if (state != oldstate)
346 notify_rfkill_state_change(rfkill);
347
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300348 mutex_unlock(&rfkill->mutex);
349
350 return 0;
351}
352EXPORT_SYMBOL(rfkill_force_state);
353
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700354static ssize_t rfkill_name_show(struct device *dev,
355 struct device_attribute *attr,
356 char *buf)
357{
358 struct rfkill *rfkill = to_rfkill(dev);
359
360 return sprintf(buf, "%s\n", rfkill->name);
361}
362
Henrique de Moraes Holschuh99c632e2008-06-23 17:23:04 -0300363static const char *rfkill_get_type_str(enum rfkill_type type)
364{
365 switch (type) {
366 case RFKILL_TYPE_WLAN:
367 return "wlan";
368 case RFKILL_TYPE_BLUETOOTH:
369 return "bluetooth";
370 case RFKILL_TYPE_UWB:
371 return "ultrawideband";
372 case RFKILL_TYPE_WIMAX:
373 return "wimax";
374 case RFKILL_TYPE_WWAN:
375 return "wwan";
376 default:
377 BUG();
378 }
379}
380
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700381static ssize_t rfkill_type_show(struct device *dev,
382 struct device_attribute *attr,
383 char *buf)
384{
385 struct rfkill *rfkill = to_rfkill(dev);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700386
Henrique de Moraes Holschuh99c632e2008-06-23 17:23:04 -0300387 return sprintf(buf, "%s\n", rfkill_get_type_str(rfkill->type));
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700388}
389
390static ssize_t rfkill_state_show(struct device *dev,
391 struct device_attribute *attr,
392 char *buf)
393{
394 struct rfkill *rfkill = to_rfkill(dev);
395
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300396 update_rfkill_state(rfkill);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700397 return sprintf(buf, "%d\n", rfkill->state);
398}
399
400static ssize_t rfkill_state_store(struct device *dev,
401 struct device_attribute *attr,
402 const char *buf, size_t count)
403{
404 struct rfkill *rfkill = to_rfkill(dev);
Henrique de Moraes Holschuh849e0572008-08-26 11:57:57 -0300405 unsigned long state;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700406 int error;
407
408 if (!capable(CAP_NET_ADMIN))
409 return -EPERM;
410
Henrique de Moraes Holschuh849e0572008-08-26 11:57:57 -0300411 error = strict_strtoul(buf, 0, &state);
412 if (error)
413 return error;
414
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300415 /* RFKILL_STATE_HARD_BLOCKED is illegal here... */
416 if (state != RFKILL_STATE_UNBLOCKED &&
417 state != RFKILL_STATE_SOFT_BLOCKED)
418 return -EINVAL;
419
Michael Buesch7f4c5342007-11-28 17:49:34 +0100420 if (mutex_lock_interruptible(&rfkill->mutex))
421 return -ERESTARTSYS;
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300422 error = rfkill_toggle_radio(rfkill, state, 0);
Michael Buesch7f4c5342007-11-28 17:49:34 +0100423 mutex_unlock(&rfkill->mutex);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700424
Michael Buesch7f4c5342007-11-28 17:49:34 +0100425 return error ? error : count;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700426}
427
428static ssize_t rfkill_claim_show(struct device *dev,
429 struct device_attribute *attr,
430 char *buf)
431{
432 struct rfkill *rfkill = to_rfkill(dev);
433
Felipe Balbi01b510b2008-08-26 11:57:58 -0300434 return sprintf(buf, "%d\n", rfkill->user_claim);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700435}
436
437static ssize_t rfkill_claim_store(struct device *dev,
438 struct device_attribute *attr,
439 const char *buf, size_t count)
440{
441 struct rfkill *rfkill = to_rfkill(dev);
Henrique de Moraes Holschuh849e0572008-08-26 11:57:57 -0300442 unsigned long claim_tmp;
443 bool claim;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700444 int error;
445
446 if (!capable(CAP_NET_ADMIN))
447 return -EPERM;
448
Henrique de Moraes Holschuh064af112008-07-21 21:18:20 -0300449 if (rfkill->user_claim_unsupported)
450 return -EOPNOTSUPP;
451
Henrique de Moraes Holschuh849e0572008-08-26 11:57:57 -0300452 error = strict_strtoul(buf, 0, &claim_tmp);
453 if (error)
454 return error;
455 claim = !!claim_tmp;
456
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700457 /*
458 * Take the global lock to make sure the kernel is not in
459 * the middle of rfkill_switch_all
460 */
461 error = mutex_lock_interruptible(&rfkill_mutex);
462 if (error)
463 return error;
464
465 if (rfkill->user_claim != claim) {
Henrique de Moraes Holschuh064af112008-07-21 21:18:20 -0300466 if (!claim) {
467 mutex_lock(&rfkill->mutex);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700468 rfkill_toggle_radio(rfkill,
Henrique de Moraes Holschuh99619202008-08-02 15:10:58 -0300469 rfkill_global_states[rfkill->type].current_state,
470 0);
Henrique de Moraes Holschuh064af112008-07-21 21:18:20 -0300471 mutex_unlock(&rfkill->mutex);
472 }
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700473 rfkill->user_claim = claim;
474 }
475
476 mutex_unlock(&rfkill_mutex);
477
Michael Buesch20405c02007-09-27 21:34:23 +0200478 return error ? error : count;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700479}
480
481static struct device_attribute rfkill_dev_attrs[] = {
482 __ATTR(name, S_IRUGO, rfkill_name_show, NULL),
483 __ATTR(type, S_IRUGO, rfkill_type_show, NULL),
Ivo van Doornc81de6a2007-07-18 15:38:03 -0700484 __ATTR(state, S_IRUGO|S_IWUSR, rfkill_state_show, rfkill_state_store),
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700485 __ATTR(claim, S_IRUGO|S_IWUSR, rfkill_claim_show, rfkill_claim_store),
486 __ATTR_NULL
487};
488
489static void rfkill_release(struct device *dev)
490{
491 struct rfkill *rfkill = to_rfkill(dev);
492
493 kfree(rfkill);
494 module_put(THIS_MODULE);
495}
496
497#ifdef CONFIG_PM
498static int rfkill_suspend(struct device *dev, pm_message_t state)
499{
500 struct rfkill *rfkill = to_rfkill(dev);
501
502 if (dev->power.power_state.event != state.event) {
Rafael J. Wysocki3a2d5b72008-02-23 19:13:25 +0100503 if (state.event & PM_EVENT_SLEEP) {
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300504 /* Stop transmitter, keep state, no notifies */
505 update_rfkill_state(rfkill);
506
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700507 mutex_lock(&rfkill->mutex);
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300508 rfkill->toggle_radio(rfkill->data,
509 RFKILL_STATE_SOFT_BLOCKED);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700510 mutex_unlock(&rfkill->mutex);
511 }
512
513 dev->power.power_state = state;
514 }
515
516 return 0;
517}
518
519static int rfkill_resume(struct device *dev)
520{
521 struct rfkill *rfkill = to_rfkill(dev);
522
523 if (dev->power.power_state.event != PM_EVENT_ON) {
524 mutex_lock(&rfkill->mutex);
525
Henrique de Moraes Holschuhe10e0df2008-08-02 14:56:25 -0300526 dev->power.power_state.event = PM_EVENT_ON;
527
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300528 /* restore radio state AND notify everybody */
529 rfkill_toggle_radio(rfkill, rfkill->state, 1);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700530
531 mutex_unlock(&rfkill->mutex);
532 }
533
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700534 return 0;
535}
536#else
537#define rfkill_suspend NULL
538#define rfkill_resume NULL
539#endif
540
Henrique de Moraes Holschuhffb67c32008-06-23 17:23:05 -0300541static int rfkill_blocking_uevent_notifier(struct notifier_block *nb,
542 unsigned long eventid,
543 void *data)
544{
545 struct rfkill *rfkill = (struct rfkill *)data;
546
547 switch (eventid) {
548 case RFKILL_STATE_CHANGED:
549 kobject_uevent(&rfkill->dev.kobj, KOBJ_CHANGE);
550 break;
551 default:
552 break;
553 }
554
555 return NOTIFY_DONE;
556}
557
558static struct notifier_block rfkill_blocking_uevent_nb = {
559 .notifier_call = rfkill_blocking_uevent_notifier,
560 .priority = 0,
561};
562
563static int rfkill_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
564{
565 struct rfkill *rfkill = to_rfkill(dev);
566 int error;
567
568 error = add_uevent_var(env, "RFKILL_NAME=%s", rfkill->name);
569 if (error)
570 return error;
571 error = add_uevent_var(env, "RFKILL_TYPE=%s",
572 rfkill_get_type_str(rfkill->type));
573 if (error)
574 return error;
575 error = add_uevent_var(env, "RFKILL_STATE=%d", rfkill->state);
576 return error;
577}
578
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700579static struct class rfkill_class = {
580 .name = "rfkill",
581 .dev_release = rfkill_release,
582 .dev_attrs = rfkill_dev_attrs,
583 .suspend = rfkill_suspend,
584 .resume = rfkill_resume,
Henrique de Moraes Holschuhffb67c32008-06-23 17:23:05 -0300585 .dev_uevent = rfkill_dev_uevent,
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700586};
587
Henrique de Moraes Holschuh02589f62008-08-02 15:10:57 -0300588static int rfkill_check_duplicity(const struct rfkill *rfkill)
589{
590 struct rfkill *p;
591 unsigned long seen[BITS_TO_LONGS(RFKILL_TYPE_MAX)];
592
593 memset(seen, 0, sizeof(seen));
594
595 list_for_each_entry(p, &rfkill_list, node) {
596 if (p == rfkill) {
597 WARN_ON(1);
598 return -EEXIST;
599 }
600 set_bit(p->type, seen);
601 }
602
603 /* 0: first switch of its kind */
604 return test_bit(rfkill->type, seen);
605}
606
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700607static int rfkill_add_switch(struct rfkill *rfkill)
608{
Henrique de Moraes Holschuh02589f62008-08-02 15:10:57 -0300609 int error;
610
Michael Buesch7319f1e2007-10-28 15:16:50 +0100611 mutex_lock(&rfkill_mutex);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700612
Henrique de Moraes Holschuh02589f62008-08-02 15:10:57 -0300613 error = rfkill_check_duplicity(rfkill);
614 if (error < 0)
615 goto unlock_out;
616
Henrique de Moraes Holschuh99619202008-08-02 15:10:58 -0300617 if (!error) {
618 /* lock default after first use */
619 set_bit(rfkill->type, rfkill_states_lockdflt);
620 rfkill_global_states[rfkill->type].current_state =
621 rfkill_global_states[rfkill->type].default_state;
622 }
623
624 rfkill_toggle_radio(rfkill,
625 rfkill_global_states[rfkill->type].current_state,
626 0);
Henrique de Moraes Holschuhfd4484a2008-07-03 13:14:57 -0300627
628 list_add_tail(&rfkill->node, &rfkill_list);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700629
Henrique de Moraes Holschuh02589f62008-08-02 15:10:57 -0300630 error = 0;
631unlock_out:
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700632 mutex_unlock(&rfkill_mutex);
Michael Buesch7319f1e2007-10-28 15:16:50 +0100633
Henrique de Moraes Holschuh02589f62008-08-02 15:10:57 -0300634 return error;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700635}
636
637static void rfkill_remove_switch(struct rfkill *rfkill)
638{
639 mutex_lock(&rfkill_mutex);
640 list_del_init(&rfkill->node);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700641 mutex_unlock(&rfkill_mutex);
Henrique de Moraes Holschuh064af112008-07-21 21:18:20 -0300642
643 mutex_lock(&rfkill->mutex);
644 rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1);
645 mutex_unlock(&rfkill->mutex);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700646}
647
648/**
649 * rfkill_allocate - allocate memory for rfkill structure.
650 * @parent: device that has rf switch on it
Ivo van Doorn234a0ca2007-09-13 09:20:42 +0200651 * @type: type of the switch (RFKILL_TYPE_*)
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700652 *
653 * This function should be called by the network driver when it needs
Henrique de Moraes Holschuh435307a2008-07-21 21:18:22 -0300654 * rfkill structure. Once the structure is allocated the driver should
655 * finish its initialization by setting the name, private data, enable_radio
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700656 * and disable_radio methods and then register it with rfkill_register().
Henrique de Moraes Holschuh435307a2008-07-21 21:18:22 -0300657 *
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700658 * NOTE: If registration fails the structure shoudl be freed by calling
659 * rfkill_free() otherwise rfkill_unregister() should be used.
660 */
Henrique de Moraes Holschuh77fba132008-08-02 15:10:59 -0300661struct rfkill * __must_check rfkill_allocate(struct device *parent,
662 enum rfkill_type type)
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700663{
664 struct rfkill *rfkill;
665 struct device *dev;
666
667 rfkill = kzalloc(sizeof(struct rfkill), GFP_KERNEL);
Ivo van Doornd007da12007-05-19 12:24:39 -0700668 if (!rfkill)
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700669 return NULL;
670
671 mutex_init(&rfkill->mutex);
672 INIT_LIST_HEAD(&rfkill->node);
673 rfkill->type = type;
674
675 dev = &rfkill->dev;
676 dev->class = &rfkill_class;
677 dev->parent = parent;
678 device_initialize(dev);
679
680 __module_get(THIS_MODULE);
681
682 return rfkill;
683}
684EXPORT_SYMBOL(rfkill_allocate);
685
686/**
687 * rfkill_free - Mark rfkill structure for deletion
688 * @rfkill: rfkill structure to be destroyed
689 *
Henrique de Moraes Holschuh435307a2008-07-21 21:18:22 -0300690 * Decrements reference count of the rfkill structure so it is destroyed.
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700691 * Note that rfkill_free() should _not_ be called after rfkill_unregister().
692 */
693void rfkill_free(struct rfkill *rfkill)
694{
695 if (rfkill)
696 put_device(&rfkill->dev);
697}
698EXPORT_SYMBOL(rfkill_free);
699
Michael Buesch135900c2007-09-27 21:33:12 +0200700static void rfkill_led_trigger_register(struct rfkill *rfkill)
701{
702#ifdef CONFIG_RFKILL_LEDS
703 int error;
704
Dmitry Baryshkov7c4f4572008-07-22 14:17:37 +0400705 if (!rfkill->led_trigger.name)
706 rfkill->led_trigger.name = rfkill->dev.bus_id;
Dmitry Baryshkov96185662008-07-22 14:21:59 +0400707 if (!rfkill->led_trigger.activate)
708 rfkill->led_trigger.activate = rfkill_led_trigger_activate;
Michael Buesch135900c2007-09-27 21:33:12 +0200709 error = led_trigger_register(&rfkill->led_trigger);
710 if (error)
711 rfkill->led_trigger.name = NULL;
712#endif /* CONFIG_RFKILL_LEDS */
713}
714
715static void rfkill_led_trigger_unregister(struct rfkill *rfkill)
716{
717#ifdef CONFIG_RFKILL_LEDS
Henrique de Moraes Holschuh37f55e92008-07-21 21:18:18 -0300718 if (rfkill->led_trigger.name) {
Michael Buesch135900c2007-09-27 21:33:12 +0200719 led_trigger_unregister(&rfkill->led_trigger);
Henrique de Moraes Holschuh37f55e92008-07-21 21:18:18 -0300720 rfkill->led_trigger.name = NULL;
721 }
Michael Buesch135900c2007-09-27 21:33:12 +0200722#endif
723}
724
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700725/**
726 * rfkill_register - Register a rfkill structure.
727 * @rfkill: rfkill structure to be registered
728 *
729 * This function should be called by the network driver when the rfkill
730 * structure needs to be registered. Immediately from registration the
731 * switch driver should be able to service calls to toggle_radio.
732 */
Henrique de Moraes Holschuh77fba132008-08-02 15:10:59 -0300733int __must_check rfkill_register(struct rfkill *rfkill)
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700734{
735 static atomic_t rfkill_no = ATOMIC_INIT(0);
736 struct device *dev = &rfkill->dev;
737 int error;
738
739 if (!rfkill->toggle_radio)
740 return -EINVAL;
Michael Buesch7319f1e2007-10-28 15:16:50 +0100741 if (rfkill->type >= RFKILL_TYPE_MAX)
742 return -EINVAL;
Henrique de Moraes Holschuh96c87602008-08-02 15:11:00 -0300743 if (rfkill->state >= RFKILL_STATE_MAX)
744 return -EINVAL;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700745
Michael Buesch8a8f1c02007-10-28 13:07:54 +0100746 snprintf(dev->bus_id, sizeof(dev->bus_id),
747 "rfkill%ld", (long)atomic_inc_return(&rfkill_no) - 1);
748
749 rfkill_led_trigger_register(rfkill);
750
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700751 error = rfkill_add_switch(rfkill);
Eric Paris632041f2008-01-13 16:20:56 -0500752 if (error) {
753 rfkill_led_trigger_unregister(rfkill);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700754 return error;
Eric Paris632041f2008-01-13 16:20:56 -0500755 }
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700756
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700757 error = device_add(dev);
758 if (error) {
759 rfkill_remove_switch(rfkill);
Henrique de Moraes Holschuh37f55e92008-07-21 21:18:18 -0300760 rfkill_led_trigger_unregister(rfkill);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700761 return error;
762 }
763
764 return 0;
765}
766EXPORT_SYMBOL(rfkill_register);
767
768/**
Henrique de Moraes Holschuhc8fcd902008-06-23 17:22:57 -0300769 * rfkill_unregister - Unregister a rfkill structure.
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700770 * @rfkill: rfkill structure to be unregistered
771 *
772 * This function should be called by the network driver during device
773 * teardown to destroy rfkill structure. Note that rfkill_free() should
774 * _not_ be called after rfkill_unregister().
775 */
776void rfkill_unregister(struct rfkill *rfkill)
777{
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700778 device_del(&rfkill->dev);
779 rfkill_remove_switch(rfkill);
Michael Buesch8a8f1c02007-10-28 13:07:54 +0100780 rfkill_led_trigger_unregister(rfkill);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700781 put_device(&rfkill->dev);
782}
783EXPORT_SYMBOL(rfkill_unregister);
784
Henrique de Moraes Holschuh99619202008-08-02 15:10:58 -0300785/**
786 * rfkill_set_default - set initial value for a switch type
787 * @type - the type of switch to set the default state of
788 * @state - the new default state for that group of switches
789 *
790 * Sets the initial state rfkill should use for a given type.
791 * The following initial states are allowed: RFKILL_STATE_SOFT_BLOCKED
792 * and RFKILL_STATE_UNBLOCKED.
793 *
794 * This function is meant to be used by platform drivers for platforms
795 * that can save switch state across power down/reboot.
796 *
797 * The default state for each switch type can be changed exactly once.
798 * After a switch of that type is registered, the default state cannot
799 * be changed anymore. This guards against multiple drivers it the
800 * same platform trying to set the initial switch default state, which
801 * is not allowed.
802 *
803 * Returns -EPERM if the state has already been set once or is in use,
804 * so drivers likely want to either ignore or at most printk(KERN_NOTICE)
805 * if this function returns -EPERM.
806 *
807 * Returns 0 if the new default state was set, or an error if it
808 * could not be set.
809 */
810int rfkill_set_default(enum rfkill_type type, enum rfkill_state state)
811{
812 int error;
813
814 if (type >= RFKILL_TYPE_MAX ||
815 (state != RFKILL_STATE_SOFT_BLOCKED &&
816 state != RFKILL_STATE_UNBLOCKED))
817 return -EINVAL;
818
819 mutex_lock(&rfkill_mutex);
820
821 if (!test_and_set_bit(type, rfkill_states_lockdflt)) {
822 rfkill_global_states[type].default_state = state;
823 error = 0;
824 } else
825 error = -EPERM;
826
827 mutex_unlock(&rfkill_mutex);
828 return error;
829}
830EXPORT_SYMBOL_GPL(rfkill_set_default);
831
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700832/*
833 * Rfkill module initialization/deinitialization.
834 */
835static int __init rfkill_init(void)
836{
837 int error;
838 int i;
839
Henrique de Moraes Holschuh50056572008-06-23 17:46:42 -0300840 /* RFKILL_STATE_HARD_BLOCKED is illegal here... */
841 if (rfkill_default_state != RFKILL_STATE_SOFT_BLOCKED &&
842 rfkill_default_state != RFKILL_STATE_UNBLOCKED)
Henrique de Moraes Holschuhe954b0b2008-06-23 17:22:59 -0300843 return -EINVAL;
844
Henrique de Moraes Holschuh99619202008-08-02 15:10:58 -0300845 for (i = 0; i < RFKILL_TYPE_MAX; i++)
846 rfkill_global_states[i].default_state = rfkill_default_state;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700847
848 error = class_register(&rfkill_class);
849 if (error) {
850 printk(KERN_ERR "rfkill: unable to register rfkill class\n");
851 return error;
852 }
853
Henrique de Moraes Holschuhffb67c32008-06-23 17:23:05 -0300854 register_rfkill_notifier(&rfkill_blocking_uevent_nb);
855
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700856 return 0;
857}
858
859static void __exit rfkill_exit(void)
860{
Henrique de Moraes Holschuhffb67c32008-06-23 17:23:05 -0300861 unregister_rfkill_notifier(&rfkill_blocking_uevent_nb);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700862 class_unregister(&rfkill_class);
863}
864
Michael Buesch2bf236d2007-10-28 14:39:02 +0100865subsys_initcall(rfkill_init);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700866module_exit(rfkill_exit);