rfkill: do not allow userspace to override ALL RADIOS OFF
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / rfkill / rfkill.c
blobf0fff5ad0f48e06e774add30d818e05cf2173448
1 /*
2 * Copyright (C) 2006 - 2007 Ivo van Doorn
3 * Copyright (C) 2007 Dmitry Torokhov
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.
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.
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.
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>
30 /* Get declaration of rfkill_switch_all() to shut up sparse. */
31 #include "rfkill-input.h"
34 MODULE_AUTHOR("Ivo van Doorn <IvDoorn@gmail.com>");
35 MODULE_VERSION("1.0");
36 MODULE_DESCRIPTION("RF switch support");
37 MODULE_LICENSE("GPL");
39 static LIST_HEAD(rfkill_list); /* list of registered rf switches */
40 static DEFINE_MUTEX(rfkill_mutex);
42 static unsigned int rfkill_default_state = RFKILL_STATE_ON;
43 module_param_named(default_state, rfkill_default_state, uint, 0444);
44 MODULE_PARM_DESC(default_state,
45 "Default initial state for all radio types, 0 = radio off");
47 static enum rfkill_state rfkill_states[RFKILL_TYPE_MAX];
49 static BLOCKING_NOTIFIER_HEAD(rfkill_notifier_list);
52 /**
53 * register_rfkill_notifier - Add notifier to rfkill notifier chain
54 * @nb: pointer to the new entry to add to the chain
56 * See blocking_notifier_chain_register() for return value and further
57 * observations.
59 * Adds a notifier to the rfkill notifier chain. The chain will be
60 * called with a pointer to the relevant rfkill structure as a parameter,
61 * refer to include/linux/rfkill.h for the possible events.
63 * Notifiers added to this chain are to always return NOTIFY_DONE. This
64 * chain is a blocking notifier chain: notifiers can sleep.
66 * Calls to this chain may have been done through a workqueue. One must
67 * assume unordered asynchronous behaviour, there is no way to know if
68 * actions related to the event that generated the notification have been
69 * carried out already.
71 int register_rfkill_notifier(struct notifier_block *nb)
73 return blocking_notifier_chain_register(&rfkill_notifier_list, nb);
75 EXPORT_SYMBOL_GPL(register_rfkill_notifier);
77 /**
78 * unregister_rfkill_notifier - remove notifier from rfkill notifier chain
79 * @nb: pointer to the entry to remove from the chain
81 * See blocking_notifier_chain_unregister() for return value and further
82 * observations.
84 * Removes a notifier from the rfkill notifier chain.
86 int unregister_rfkill_notifier(struct notifier_block *nb)
88 return blocking_notifier_chain_unregister(&rfkill_notifier_list, nb);
90 EXPORT_SYMBOL_GPL(unregister_rfkill_notifier);
93 static void rfkill_led_trigger(struct rfkill *rfkill,
94 enum rfkill_state state)
96 #ifdef CONFIG_RFKILL_LEDS
97 struct led_trigger *led = &rfkill->led_trigger;
99 if (!led->name)
100 return;
101 if (state == RFKILL_STATE_OFF)
102 led_trigger_event(led, LED_OFF);
103 else
104 led_trigger_event(led, LED_FULL);
105 #endif /* CONFIG_RFKILL_LEDS */
108 static void notify_rfkill_state_change(struct rfkill *rfkill)
110 blocking_notifier_call_chain(&rfkill_notifier_list,
111 RFKILL_STATE_CHANGED,
112 rfkill);
115 static void update_rfkill_state(struct rfkill *rfkill)
117 enum rfkill_state newstate, oldstate;
119 if (rfkill->get_state) {
120 mutex_lock(&rfkill->mutex);
121 if (!rfkill->get_state(rfkill->data, &newstate)) {
122 oldstate = rfkill->state;
123 rfkill->state = newstate;
124 if (oldstate != newstate)
125 notify_rfkill_state_change(rfkill);
127 mutex_unlock(&rfkill->mutex);
131 static int rfkill_toggle_radio(struct rfkill *rfkill,
132 enum rfkill_state state,
133 int force)
135 int retval = 0;
136 enum rfkill_state oldstate, newstate;
138 oldstate = rfkill->state;
140 if (rfkill->get_state && !force &&
141 !rfkill->get_state(rfkill->data, &newstate))
142 rfkill->state = newstate;
144 if (force || state != rfkill->state) {
145 retval = rfkill->toggle_radio(rfkill->data, state);
146 if (!retval)
147 rfkill->state = state;
150 if (force || rfkill->state != oldstate) {
151 rfkill_led_trigger(rfkill, rfkill->state);
152 notify_rfkill_state_change(rfkill);
155 return retval;
159 * rfkill_switch_all - Toggle state of all switches of given type
160 * @type: type of interfaces to be affeceted
161 * @state: the new state
163 * This function toggles state of all switches of given type unless
164 * a specific switch is claimed by userspace in which case it is
165 * left alone.
167 void rfkill_switch_all(enum rfkill_type type, enum rfkill_state state)
169 struct rfkill *rfkill;
171 mutex_lock(&rfkill_mutex);
173 rfkill_states[type] = state;
175 list_for_each_entry(rfkill, &rfkill_list, node) {
176 if ((!rfkill->user_claim) && (rfkill->type == type))
177 rfkill_toggle_radio(rfkill, state, 0);
180 mutex_unlock(&rfkill_mutex);
182 EXPORT_SYMBOL(rfkill_switch_all);
185 * rfkill_epo - emergency power off all transmitters
187 * This kicks all rfkill devices to RFKILL_STATE_OFF, ignoring
188 * everything in its path but rfkill_mutex.
190 void rfkill_epo(void)
192 struct rfkill *rfkill;
194 mutex_lock(&rfkill_mutex);
195 list_for_each_entry(rfkill, &rfkill_list, node) {
196 rfkill_toggle_radio(rfkill, RFKILL_STATE_OFF, 1);
198 mutex_unlock(&rfkill_mutex);
200 EXPORT_SYMBOL_GPL(rfkill_epo);
203 * rfkill_force_state - Force the internal rfkill radio state
204 * @rfkill: pointer to the rfkill class to modify.
205 * @state: the current radio state the class should be forced to.
207 * This function updates the internal state of the radio cached
208 * by the rfkill class. It should be used when the driver gets
209 * a notification by the firmware/hardware of the current *real*
210 * state of the radio rfkill switch.
212 * It may not be called from an atomic context.
214 int rfkill_force_state(struct rfkill *rfkill, enum rfkill_state state)
216 enum rfkill_state oldstate;
218 if (state != RFKILL_STATE_OFF &&
219 state != RFKILL_STATE_ON)
220 return -EINVAL;
222 mutex_lock(&rfkill->mutex);
224 oldstate = rfkill->state;
225 rfkill->state = state;
227 if (state != oldstate)
228 notify_rfkill_state_change(rfkill);
230 mutex_unlock(&rfkill->mutex);
232 return 0;
234 EXPORT_SYMBOL(rfkill_force_state);
236 static ssize_t rfkill_name_show(struct device *dev,
237 struct device_attribute *attr,
238 char *buf)
240 struct rfkill *rfkill = to_rfkill(dev);
242 return sprintf(buf, "%s\n", rfkill->name);
245 static const char *rfkill_get_type_str(enum rfkill_type type)
247 switch (type) {
248 case RFKILL_TYPE_WLAN:
249 return "wlan";
250 case RFKILL_TYPE_BLUETOOTH:
251 return "bluetooth";
252 case RFKILL_TYPE_UWB:
253 return "ultrawideband";
254 case RFKILL_TYPE_WIMAX:
255 return "wimax";
256 case RFKILL_TYPE_WWAN:
257 return "wwan";
258 default:
259 BUG();
263 static ssize_t rfkill_type_show(struct device *dev,
264 struct device_attribute *attr,
265 char *buf)
267 struct rfkill *rfkill = to_rfkill(dev);
269 return sprintf(buf, "%s\n", rfkill_get_type_str(rfkill->type));
272 static ssize_t rfkill_state_show(struct device *dev,
273 struct device_attribute *attr,
274 char *buf)
276 struct rfkill *rfkill = to_rfkill(dev);
278 update_rfkill_state(rfkill);
279 return sprintf(buf, "%d\n", rfkill->state);
282 static ssize_t rfkill_state_store(struct device *dev,
283 struct device_attribute *attr,
284 const char *buf, size_t count)
286 struct rfkill *rfkill = to_rfkill(dev);
287 unsigned int state = simple_strtoul(buf, NULL, 0);
288 int error;
290 if (!capable(CAP_NET_ADMIN))
291 return -EPERM;
293 if (mutex_lock_interruptible(&rfkill->mutex))
294 return -ERESTARTSYS;
295 error = rfkill_toggle_radio(rfkill,
296 state ? RFKILL_STATE_ON : RFKILL_STATE_OFF,
298 mutex_unlock(&rfkill->mutex);
300 return error ? error : count;
303 static ssize_t rfkill_claim_show(struct device *dev,
304 struct device_attribute *attr,
305 char *buf)
307 struct rfkill *rfkill = to_rfkill(dev);
309 return sprintf(buf, "%d", rfkill->user_claim);
312 static ssize_t rfkill_claim_store(struct device *dev,
313 struct device_attribute *attr,
314 const char *buf, size_t count)
316 struct rfkill *rfkill = to_rfkill(dev);
317 bool claim = !!simple_strtoul(buf, NULL, 0);
318 int error;
320 if (!capable(CAP_NET_ADMIN))
321 return -EPERM;
324 * Take the global lock to make sure the kernel is not in
325 * the middle of rfkill_switch_all
327 error = mutex_lock_interruptible(&rfkill_mutex);
328 if (error)
329 return error;
331 if (rfkill->user_claim_unsupported) {
332 error = -EOPNOTSUPP;
333 goto out_unlock;
335 if (rfkill->user_claim != claim) {
336 if (!claim)
337 rfkill_toggle_radio(rfkill,
338 rfkill_states[rfkill->type],
340 rfkill->user_claim = claim;
343 out_unlock:
344 mutex_unlock(&rfkill_mutex);
346 return error ? error : count;
349 static struct device_attribute rfkill_dev_attrs[] = {
350 __ATTR(name, S_IRUGO, rfkill_name_show, NULL),
351 __ATTR(type, S_IRUGO, rfkill_type_show, NULL),
352 __ATTR(state, S_IRUGO|S_IWUSR, rfkill_state_show, rfkill_state_store),
353 __ATTR(claim, S_IRUGO|S_IWUSR, rfkill_claim_show, rfkill_claim_store),
354 __ATTR_NULL
357 static void rfkill_release(struct device *dev)
359 struct rfkill *rfkill = to_rfkill(dev);
361 kfree(rfkill);
362 module_put(THIS_MODULE);
365 #ifdef CONFIG_PM
366 static int rfkill_suspend(struct device *dev, pm_message_t state)
368 struct rfkill *rfkill = to_rfkill(dev);
370 if (dev->power.power_state.event != state.event) {
371 if (state.event == PM_EVENT_SUSPEND) {
372 /* Stop transmitter, keep state, no notifies */
374 update_rfkill_state(rfkill);
376 mutex_lock(&rfkill->mutex);
377 rfkill->toggle_radio(rfkill->data, RFKILL_STATE_OFF);
378 mutex_unlock(&rfkill->mutex);
381 dev->power.power_state = state;
384 return 0;
387 static int rfkill_resume(struct device *dev)
389 struct rfkill *rfkill = to_rfkill(dev);
391 if (dev->power.power_state.event != PM_EVENT_ON) {
392 mutex_lock(&rfkill->mutex);
394 /* restore radio state AND notify everybody */
395 rfkill_toggle_radio(rfkill, rfkill->state, 1);
397 mutex_unlock(&rfkill->mutex);
400 dev->power.power_state = PMSG_ON;
401 return 0;
403 #else
404 #define rfkill_suspend NULL
405 #define rfkill_resume NULL
406 #endif
408 static int rfkill_blocking_uevent_notifier(struct notifier_block *nb,
409 unsigned long eventid,
410 void *data)
412 struct rfkill *rfkill = (struct rfkill *)data;
414 switch (eventid) {
415 case RFKILL_STATE_CHANGED:
416 kobject_uevent(&rfkill->dev.kobj, KOBJ_CHANGE);
417 break;
418 default:
419 break;
422 return NOTIFY_DONE;
425 static struct notifier_block rfkill_blocking_uevent_nb = {
426 .notifier_call = rfkill_blocking_uevent_notifier,
427 .priority = 0,
430 static int rfkill_dev_uevent(struct device *dev, char **envp, int num_envp,
431 char *buffer, int buffer_size)
433 struct rfkill *rfkill = to_rfkill(dev);
434 int i = 0, length = 0;
436 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
437 "RFKILL_NAME=%s", rfkill->name);
438 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
439 "RFKILL_TYPE=%s", rfkill_get_type_str(rfkill->type));
440 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
441 "RFKILL_STATE=%d", rfkill->state);
442 envp[i] = NULL;
443 return 0;
446 static struct class rfkill_class = {
447 .name = "rfkill",
448 .dev_release = rfkill_release,
449 .dev_attrs = rfkill_dev_attrs,
450 .suspend = rfkill_suspend,
451 .resume = rfkill_resume,
452 .dev_uevent = rfkill_dev_uevent,
455 static int rfkill_add_switch(struct rfkill *rfkill)
457 int error;
459 mutex_lock(&rfkill_mutex);
461 error = rfkill_toggle_radio(rfkill, rfkill_states[rfkill->type], 0);
462 if (!error)
463 list_add_tail(&rfkill->node, &rfkill_list);
465 mutex_unlock(&rfkill_mutex);
467 return error;
470 static void rfkill_remove_switch(struct rfkill *rfkill)
472 mutex_lock(&rfkill_mutex);
473 list_del_init(&rfkill->node);
474 rfkill_toggle_radio(rfkill, RFKILL_STATE_OFF, 1);
475 mutex_unlock(&rfkill_mutex);
479 * rfkill_allocate - allocate memory for rfkill structure.
480 * @parent: device that has rf switch on it
481 * @type: type of the switch (RFKILL_TYPE_*)
483 * This function should be called by the network driver when it needs
484 * rfkill structure. Once the structure is allocated the driver shoud
485 * finish its initialization by setting name, private data, enable_radio
486 * and disable_radio methods and then register it with rfkill_register().
487 * NOTE: If registration fails the structure shoudl be freed by calling
488 * rfkill_free() otherwise rfkill_unregister() should be used.
490 struct rfkill *rfkill_allocate(struct device *parent, enum rfkill_type type)
492 struct rfkill *rfkill;
493 struct device *dev;
495 rfkill = kzalloc(sizeof(struct rfkill), GFP_KERNEL);
496 if (!rfkill)
497 return NULL;
499 mutex_init(&rfkill->mutex);
500 INIT_LIST_HEAD(&rfkill->node);
501 rfkill->type = type;
503 dev = &rfkill->dev;
504 dev->class = &rfkill_class;
505 dev->parent = parent;
506 device_initialize(dev);
508 __module_get(THIS_MODULE);
510 return rfkill;
512 EXPORT_SYMBOL(rfkill_allocate);
515 * rfkill_free - Mark rfkill structure for deletion
516 * @rfkill: rfkill structure to be destroyed
518 * Decrements reference count of rfkill structure so it is destroyed.
519 * Note that rfkill_free() should _not_ be called after rfkill_unregister().
521 void rfkill_free(struct rfkill *rfkill)
523 if (rfkill)
524 put_device(&rfkill->dev);
526 EXPORT_SYMBOL(rfkill_free);
528 static void rfkill_led_trigger_register(struct rfkill *rfkill)
530 #ifdef CONFIG_RFKILL_LEDS
531 int error;
533 rfkill->led_trigger.name = rfkill->dev.bus_id;
534 error = led_trigger_register(&rfkill->led_trigger);
535 if (error)
536 rfkill->led_trigger.name = NULL;
537 #endif /* CONFIG_RFKILL_LEDS */
540 static void rfkill_led_trigger_unregister(struct rfkill *rfkill)
542 #ifdef CONFIG_RFKILL_LEDS
543 if (rfkill->led_trigger.name)
544 led_trigger_unregister(&rfkill->led_trigger);
545 #endif
549 * rfkill_register - Register a rfkill structure.
550 * @rfkill: rfkill structure to be registered
552 * This function should be called by the network driver when the rfkill
553 * structure needs to be registered. Immediately from registration the
554 * switch driver should be able to service calls to toggle_radio.
556 int rfkill_register(struct rfkill *rfkill)
558 static atomic_t rfkill_no = ATOMIC_INIT(0);
559 struct device *dev = &rfkill->dev;
560 int error;
562 if (!rfkill->toggle_radio)
563 return -EINVAL;
564 if (rfkill->type >= RFKILL_TYPE_MAX)
565 return -EINVAL;
567 snprintf(dev->bus_id, sizeof(dev->bus_id),
568 "rfkill%ld", (long)atomic_inc_return(&rfkill_no) - 1);
570 rfkill_led_trigger_register(rfkill);
572 error = rfkill_add_switch(rfkill);
573 if (error) {
574 rfkill_led_trigger_unregister(rfkill);
575 return error;
578 error = device_add(dev);
579 if (error) {
580 rfkill_led_trigger_unregister(rfkill);
581 rfkill_remove_switch(rfkill);
582 return error;
585 return 0;
587 EXPORT_SYMBOL(rfkill_register);
590 * rfkill_unregister - Unregister a rfkill structure.
591 * @rfkill: rfkill structure to be unregistered
593 * This function should be called by the network driver during device
594 * teardown to destroy rfkill structure. Note that rfkill_free() should
595 * _not_ be called after rfkill_unregister().
597 void rfkill_unregister(struct rfkill *rfkill)
599 device_del(&rfkill->dev);
600 rfkill_remove_switch(rfkill);
601 rfkill_led_trigger_unregister(rfkill);
602 put_device(&rfkill->dev);
604 EXPORT_SYMBOL(rfkill_unregister);
607 * Rfkill module initialization/deinitialization.
609 static int __init rfkill_init(void)
611 int error;
612 int i;
614 if (rfkill_default_state != RFKILL_STATE_OFF &&
615 rfkill_default_state != RFKILL_STATE_ON)
616 return -EINVAL;
618 for (i = 0; i < ARRAY_SIZE(rfkill_states); i++)
619 rfkill_states[i] = rfkill_default_state;
621 error = class_register(&rfkill_class);
622 if (error) {
623 printk(KERN_ERR "rfkill: unable to register rfkill class\n");
624 return error;
627 register_rfkill_notifier(&rfkill_blocking_uevent_nb);
629 return 0;
632 static void __exit rfkill_exit(void)
634 unregister_rfkill_notifier(&rfkill_blocking_uevent_nb);
635 class_unregister(&rfkill_class);
638 subsys_initcall(rfkill_init);
639 module_exit(rfkill_exit);