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];
50 static void rfkill_led_trigger(struct rfkill *rfkill,
51 enum rfkill_state state)
53 #ifdef CONFIG_RFKILL_LEDS
54 struct led_trigger *led = &rfkill->led_trigger;
58 if (state == RFKILL_STATE_OFF)
59 led_trigger_event(led, LED_OFF);
61 led_trigger_event(led, LED_FULL);
62 #endif /* CONFIG_RFKILL_LEDS */
65 static void update_rfkill_state(struct rfkill *rfkill)
67 enum rfkill_state newstate;
69 if (rfkill->get_state) {
70 mutex_lock(&rfkill->mutex);
71 if (!rfkill->get_state(rfkill->data, &newstate))
72 rfkill->state = newstate;
73 mutex_unlock(&rfkill->mutex);
77 static int rfkill_toggle_radio(struct rfkill *rfkill,
78 enum rfkill_state state,
82 enum rfkill_state oldstate, newstate;
84 oldstate = rfkill->state;
86 if (rfkill->get_state && !force &&
87 !rfkill->get_state(rfkill->data, &newstate))
88 rfkill->state = newstate;
90 if (force || state != rfkill->state) {
91 retval = rfkill->toggle_radio(rfkill->data, state);
93 rfkill->state = state;
96 if (force || rfkill->state != oldstate)
97 rfkill_led_trigger(rfkill, rfkill->state);
103 * rfkill_switch_all - Toggle state of all switches of given type
104 * @type: type of interfaces to be affeceted
105 * @state: the new state
107 * This function toggles state of all switches of given type unless
108 * a specific switch is claimed by userspace in which case it is
111 void rfkill_switch_all(enum rfkill_type type, enum rfkill_state state)
113 struct rfkill *rfkill;
115 mutex_lock(&rfkill_mutex);
117 rfkill_states[type] = state;
119 list_for_each_entry(rfkill, &rfkill_list, node) {
120 if ((!rfkill->user_claim) && (rfkill->type == type))
121 rfkill_toggle_radio(rfkill, state, 0);
124 mutex_unlock(&rfkill_mutex);
126 EXPORT_SYMBOL(rfkill_switch_all);
129 * rfkill_force_state - Force the internal rfkill radio state
130 * @rfkill: pointer to the rfkill class to modify.
131 * @state: the current radio state the class should be forced to.
133 * This function updates the internal state of the radio cached
134 * by the rfkill class. It should be used when the driver gets
135 * a notification by the firmware/hardware of the current *real*
136 * state of the radio rfkill switch.
138 * It may not be called from an atomic context.
140 int rfkill_force_state(struct rfkill *rfkill, enum rfkill_state state)
142 if (state != RFKILL_STATE_OFF &&
143 state != RFKILL_STATE_ON)
146 mutex_lock(&rfkill->mutex);
147 rfkill->state = state;
148 mutex_unlock(&rfkill->mutex);
152 EXPORT_SYMBOL(rfkill_force_state);
154 static ssize_t rfkill_name_show(struct device *dev,
155 struct device_attribute *attr,
158 struct rfkill *rfkill = to_rfkill(dev);
160 return sprintf(buf, "%s\n", rfkill->name);
163 static ssize_t rfkill_type_show(struct device *dev,
164 struct device_attribute *attr,
167 struct rfkill *rfkill = to_rfkill(dev);
170 switch (rfkill->type) {
171 case RFKILL_TYPE_WLAN:
174 case RFKILL_TYPE_BLUETOOTH:
177 case RFKILL_TYPE_UWB:
178 type = "ultrawideband";
180 case RFKILL_TYPE_WIMAX:
183 case RFKILL_TYPE_WWAN:
190 return sprintf(buf, "%s\n", type);
193 static ssize_t rfkill_state_show(struct device *dev,
194 struct device_attribute *attr,
197 struct rfkill *rfkill = to_rfkill(dev);
199 update_rfkill_state(rfkill);
200 return sprintf(buf, "%d\n", rfkill->state);
203 static ssize_t rfkill_state_store(struct device *dev,
204 struct device_attribute *attr,
205 const char *buf, size_t count)
207 struct rfkill *rfkill = to_rfkill(dev);
208 unsigned int state = simple_strtoul(buf, NULL, 0);
211 if (!capable(CAP_NET_ADMIN))
214 if (mutex_lock_interruptible(&rfkill->mutex))
216 error = rfkill_toggle_radio(rfkill,
217 state ? RFKILL_STATE_ON : RFKILL_STATE_OFF,
219 mutex_unlock(&rfkill->mutex);
221 return error ? error : count;
224 static ssize_t rfkill_claim_show(struct device *dev,
225 struct device_attribute *attr,
228 struct rfkill *rfkill = to_rfkill(dev);
230 return sprintf(buf, "%d", rfkill->user_claim);
233 static ssize_t rfkill_claim_store(struct device *dev,
234 struct device_attribute *attr,
235 const char *buf, size_t count)
237 struct rfkill *rfkill = to_rfkill(dev);
238 bool claim = !!simple_strtoul(buf, NULL, 0);
241 if (!capable(CAP_NET_ADMIN))
245 * Take the global lock to make sure the kernel is not in
246 * the middle of rfkill_switch_all
248 error = mutex_lock_interruptible(&rfkill_mutex);
252 if (rfkill->user_claim_unsupported) {
256 if (rfkill->user_claim != claim) {
258 rfkill_toggle_radio(rfkill,
259 rfkill_states[rfkill->type],
261 rfkill->user_claim = claim;
265 mutex_unlock(&rfkill_mutex);
267 return error ? error : count;
270 static struct device_attribute rfkill_dev_attrs[] = {
271 __ATTR(name, S_IRUGO, rfkill_name_show, NULL),
272 __ATTR(type, S_IRUGO, rfkill_type_show, NULL),
273 __ATTR(state, S_IRUGO|S_IWUSR, rfkill_state_show, rfkill_state_store),
274 __ATTR(claim, S_IRUGO|S_IWUSR, rfkill_claim_show, rfkill_claim_store),
278 static void rfkill_release(struct device *dev)
280 struct rfkill *rfkill = to_rfkill(dev);
283 module_put(THIS_MODULE);
287 static int rfkill_suspend(struct device *dev, pm_message_t state)
289 struct rfkill *rfkill = to_rfkill(dev);
291 if (dev->power.power_state.event != state.event) {
292 if (state.event & PM_EVENT_SLEEP) {
293 /* Stop transmitter, keep state, no notifies */
294 update_rfkill_state(rfkill);
296 mutex_lock(&rfkill->mutex);
297 rfkill->toggle_radio(rfkill->data, RFKILL_STATE_OFF);
298 mutex_unlock(&rfkill->mutex);
301 dev->power.power_state = state;
307 static int rfkill_resume(struct device *dev)
309 struct rfkill *rfkill = to_rfkill(dev);
311 if (dev->power.power_state.event != PM_EVENT_ON) {
312 mutex_lock(&rfkill->mutex);
314 /* restore radio state AND notify everybody */
315 rfkill_toggle_radio(rfkill, rfkill->state, 1);
317 mutex_unlock(&rfkill->mutex);
320 dev->power.power_state = PMSG_ON;
324 #define rfkill_suspend NULL
325 #define rfkill_resume NULL
328 static struct class rfkill_class = {
330 .dev_release = rfkill_release,
331 .dev_attrs = rfkill_dev_attrs,
332 .suspend = rfkill_suspend,
333 .resume = rfkill_resume,
336 static int rfkill_add_switch(struct rfkill *rfkill)
340 mutex_lock(&rfkill_mutex);
342 error = rfkill_toggle_radio(rfkill, rfkill_states[rfkill->type], 0);
344 list_add_tail(&rfkill->node, &rfkill_list);
346 mutex_unlock(&rfkill_mutex);
351 static void rfkill_remove_switch(struct rfkill *rfkill)
353 mutex_lock(&rfkill_mutex);
354 list_del_init(&rfkill->node);
355 rfkill_toggle_radio(rfkill, RFKILL_STATE_OFF, 1);
356 mutex_unlock(&rfkill_mutex);
360 * rfkill_allocate - allocate memory for rfkill structure.
361 * @parent: device that has rf switch on it
362 * @type: type of the switch (RFKILL_TYPE_*)
364 * This function should be called by the network driver when it needs
365 * rfkill structure. Once the structure is allocated the driver shoud
366 * finish its initialization by setting name, private data, enable_radio
367 * and disable_radio methods and then register it with rfkill_register().
368 * NOTE: If registration fails the structure shoudl be freed by calling
369 * rfkill_free() otherwise rfkill_unregister() should be used.
371 struct rfkill *rfkill_allocate(struct device *parent, enum rfkill_type type)
373 struct rfkill *rfkill;
376 rfkill = kzalloc(sizeof(struct rfkill), GFP_KERNEL);
380 mutex_init(&rfkill->mutex);
381 INIT_LIST_HEAD(&rfkill->node);
385 dev->class = &rfkill_class;
386 dev->parent = parent;
387 device_initialize(dev);
389 __module_get(THIS_MODULE);
393 EXPORT_SYMBOL(rfkill_allocate);
396 * rfkill_free - Mark rfkill structure for deletion
397 * @rfkill: rfkill structure to be destroyed
399 * Decrements reference count of rfkill structure so it is destroyed.
400 * Note that rfkill_free() should _not_ be called after rfkill_unregister().
402 void rfkill_free(struct rfkill *rfkill)
405 put_device(&rfkill->dev);
407 EXPORT_SYMBOL(rfkill_free);
409 static void rfkill_led_trigger_register(struct rfkill *rfkill)
411 #ifdef CONFIG_RFKILL_LEDS
414 rfkill->led_trigger.name = rfkill->dev.bus_id;
415 error = led_trigger_register(&rfkill->led_trigger);
417 rfkill->led_trigger.name = NULL;
418 #endif /* CONFIG_RFKILL_LEDS */
421 static void rfkill_led_trigger_unregister(struct rfkill *rfkill)
423 #ifdef CONFIG_RFKILL_LEDS
424 if (rfkill->led_trigger.name)
425 led_trigger_unregister(&rfkill->led_trigger);
430 * rfkill_register - Register a rfkill structure.
431 * @rfkill: rfkill structure to be registered
433 * This function should be called by the network driver when the rfkill
434 * structure needs to be registered. Immediately from registration the
435 * switch driver should be able to service calls to toggle_radio.
437 int rfkill_register(struct rfkill *rfkill)
439 static atomic_t rfkill_no = ATOMIC_INIT(0);
440 struct device *dev = &rfkill->dev;
443 if (!rfkill->toggle_radio)
445 if (rfkill->type >= RFKILL_TYPE_MAX)
448 snprintf(dev->bus_id, sizeof(dev->bus_id),
449 "rfkill%ld", (long)atomic_inc_return(&rfkill_no) - 1);
451 rfkill_led_trigger_register(rfkill);
453 error = rfkill_add_switch(rfkill);
455 rfkill_led_trigger_unregister(rfkill);
459 error = device_add(dev);
461 rfkill_led_trigger_unregister(rfkill);
462 rfkill_remove_switch(rfkill);
468 EXPORT_SYMBOL(rfkill_register);
471 * rfkill_unregister - Unregister a rfkill structure.
472 * @rfkill: rfkill structure to be unregistered
474 * This function should be called by the network driver during device
475 * teardown to destroy rfkill structure. Note that rfkill_free() should
476 * _not_ be called after rfkill_unregister().
478 void rfkill_unregister(struct rfkill *rfkill)
480 device_del(&rfkill->dev);
481 rfkill_remove_switch(rfkill);
482 rfkill_led_trigger_unregister(rfkill);
483 put_device(&rfkill->dev);
485 EXPORT_SYMBOL(rfkill_unregister);
488 * Rfkill module initialization/deinitialization.
490 static int __init rfkill_init(void)
495 if (rfkill_default_state != RFKILL_STATE_OFF &&
496 rfkill_default_state != RFKILL_STATE_ON)
499 for (i = 0; i < ARRAY_SIZE(rfkill_states); i++)
500 rfkill_states[i] = rfkill_default_state;
502 error = class_register(&rfkill_class);
504 printk(KERN_ERR "rfkill: unable to register rfkill class\n");
511 static void __exit rfkill_exit(void)
513 class_unregister(&rfkill_class);
516 subsys_initcall(rfkill_init);
517 module_exit(rfkill_exit);