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 MODULE_AUTHOR("Ivo van Doorn <IvDoorn@gmail.com>");
31 MODULE_VERSION("1.0");
32 MODULE_DESCRIPTION("RF switch support");
33 MODULE_LICENSE("GPL");
35 static LIST_HEAD(rfkill_list); /* list of registered rf switches */
36 static DEFINE_MUTEX(rfkill_mutex);
38 static enum rfkill_state rfkill_states[RFKILL_TYPE_MAX];
41 static void rfkill_led_trigger(struct rfkill *rfkill,
42 enum rfkill_state state)
44 #ifdef CONFIG_RFKILL_LEDS
45 struct led_trigger *led = &rfkill->led_trigger;
49 if (state == RFKILL_STATE_OFF)
50 led_trigger_event(led, LED_OFF);
52 led_trigger_event(led, LED_FULL);
53 #endif /* CONFIG_RFKILL_LEDS */
56 static int rfkill_toggle_radio(struct rfkill *rfkill,
57 enum rfkill_state state)
61 retval = mutex_lock_interruptible(&rfkill->mutex);
65 if (state != rfkill->state) {
66 retval = rfkill->toggle_radio(rfkill->data, state);
68 rfkill->state = state;
69 rfkill_led_trigger(rfkill, state);
73 mutex_unlock(&rfkill->mutex);
78 * rfkill_switch_all - Toggle state of all switches of given type
79 * @type: type of interfaces to be affeceted
80 * @state: the new state
82 * This function toggles state of all switches of given type unless
83 * a specific switch is claimed by userspace in which case it is
87 void rfkill_switch_all(enum rfkill_type type, enum rfkill_state state)
89 struct rfkill *rfkill;
91 mutex_lock(&rfkill_mutex);
93 rfkill_states[type] = state;
95 list_for_each_entry(rfkill, &rfkill_list, node) {
96 if (!rfkill->user_claim)
97 rfkill_toggle_radio(rfkill, state);
100 mutex_unlock(&rfkill_mutex);
102 EXPORT_SYMBOL(rfkill_switch_all);
104 static ssize_t rfkill_name_show(struct device *dev,
105 struct device_attribute *attr,
108 struct rfkill *rfkill = to_rfkill(dev);
110 return sprintf(buf, "%s\n", rfkill->name);
113 static ssize_t rfkill_type_show(struct device *dev,
114 struct device_attribute *attr,
117 struct rfkill *rfkill = to_rfkill(dev);
120 switch (rfkill->type) {
121 case RFKILL_TYPE_WLAN:
124 case RFKILL_TYPE_BLUETOOTH:
127 case RFKILL_TYPE_UWB:
128 type = "ultrawideband";
134 return sprintf(buf, "%s\n", type);
137 static ssize_t rfkill_state_show(struct device *dev,
138 struct device_attribute *attr,
141 struct rfkill *rfkill = to_rfkill(dev);
143 return sprintf(buf, "%d\n", rfkill->state);
146 static ssize_t rfkill_state_store(struct device *dev,
147 struct device_attribute *attr,
148 const char *buf, size_t count)
150 struct rfkill *rfkill = to_rfkill(dev);
151 unsigned int state = simple_strtoul(buf, NULL, 0);
154 if (!capable(CAP_NET_ADMIN))
157 error = rfkill_toggle_radio(rfkill,
158 state ? RFKILL_STATE_ON : RFKILL_STATE_OFF);
165 static ssize_t rfkill_claim_show(struct device *dev,
166 struct device_attribute *attr,
169 struct rfkill *rfkill = to_rfkill(dev);
171 return sprintf(buf, "%d", rfkill->user_claim);
174 static ssize_t rfkill_claim_store(struct device *dev,
175 struct device_attribute *attr,
176 const char *buf, size_t count)
178 struct rfkill *rfkill = to_rfkill(dev);
179 bool claim = !!simple_strtoul(buf, NULL, 0);
182 if (!capable(CAP_NET_ADMIN))
186 * Take the global lock to make sure the kernel is not in
187 * the middle of rfkill_switch_all
189 error = mutex_lock_interruptible(&rfkill_mutex);
193 if (rfkill->user_claim_unsupported) {
197 if (rfkill->user_claim != claim) {
199 rfkill_toggle_radio(rfkill,
200 rfkill_states[rfkill->type]);
201 rfkill->user_claim = claim;
205 mutex_unlock(&rfkill_mutex);
207 return error ? error : count;
210 static struct device_attribute rfkill_dev_attrs[] = {
211 __ATTR(name, S_IRUGO, rfkill_name_show, NULL),
212 __ATTR(type, S_IRUGO, rfkill_type_show, NULL),
213 __ATTR(state, S_IRUGO|S_IWUSR, rfkill_state_show, rfkill_state_store),
214 __ATTR(claim, S_IRUGO|S_IWUSR, rfkill_claim_show, rfkill_claim_store),
218 static void rfkill_release(struct device *dev)
220 struct rfkill *rfkill = to_rfkill(dev);
223 module_put(THIS_MODULE);
227 static int rfkill_suspend(struct device *dev, pm_message_t state)
229 struct rfkill *rfkill = to_rfkill(dev);
231 if (dev->power.power_state.event != state.event) {
232 if (state.event == PM_EVENT_SUSPEND) {
233 mutex_lock(&rfkill->mutex);
235 if (rfkill->state == RFKILL_STATE_ON)
236 rfkill->toggle_radio(rfkill->data,
239 mutex_unlock(&rfkill->mutex);
242 dev->power.power_state = state;
248 static int rfkill_resume(struct device *dev)
250 struct rfkill *rfkill = to_rfkill(dev);
252 if (dev->power.power_state.event != PM_EVENT_ON) {
253 mutex_lock(&rfkill->mutex);
255 if (rfkill->state == RFKILL_STATE_ON)
256 rfkill->toggle_radio(rfkill->data, RFKILL_STATE_ON);
258 mutex_unlock(&rfkill->mutex);
261 dev->power.power_state = PMSG_ON;
265 #define rfkill_suspend NULL
266 #define rfkill_resume NULL
269 static struct class rfkill_class = {
271 .dev_release = rfkill_release,
272 .dev_attrs = rfkill_dev_attrs,
273 .suspend = rfkill_suspend,
274 .resume = rfkill_resume,
277 static int rfkill_add_switch(struct rfkill *rfkill)
281 retval = mutex_lock_interruptible(&rfkill_mutex);
285 retval = rfkill_toggle_radio(rfkill, rfkill_states[rfkill->type]);
289 list_add_tail(&rfkill->node, &rfkill_list);
292 mutex_unlock(&rfkill_mutex);
296 static void rfkill_remove_switch(struct rfkill *rfkill)
298 mutex_lock(&rfkill_mutex);
299 list_del_init(&rfkill->node);
300 rfkill_toggle_radio(rfkill, RFKILL_STATE_OFF);
301 mutex_unlock(&rfkill_mutex);
305 * rfkill_allocate - allocate memory for rfkill structure.
306 * @parent: device that has rf switch on it
307 * @type: type of the switch (RFKILL_TYPE_*)
309 * This function should be called by the network driver when it needs
310 * rfkill structure. Once the structure is allocated the driver shoud
311 * finish its initialization by setting name, private data, enable_radio
312 * and disable_radio methods and then register it with rfkill_register().
313 * NOTE: If registration fails the structure shoudl be freed by calling
314 * rfkill_free() otherwise rfkill_unregister() should be used.
316 struct rfkill *rfkill_allocate(struct device *parent, enum rfkill_type type)
318 struct rfkill *rfkill;
321 rfkill = kzalloc(sizeof(struct rfkill), GFP_KERNEL);
325 mutex_init(&rfkill->mutex);
326 INIT_LIST_HEAD(&rfkill->node);
330 dev->class = &rfkill_class;
331 dev->parent = parent;
332 device_initialize(dev);
334 __module_get(THIS_MODULE);
338 EXPORT_SYMBOL(rfkill_allocate);
341 * rfkill_free - Mark rfkill structure for deletion
342 * @rfkill: rfkill structure to be destroyed
344 * Decrements reference count of rfkill structure so it is destoryed.
345 * Note that rfkill_free() should _not_ be called after rfkill_unregister().
347 void rfkill_free(struct rfkill *rfkill)
350 put_device(&rfkill->dev);
352 EXPORT_SYMBOL(rfkill_free);
354 static void rfkill_led_trigger_register(struct rfkill *rfkill)
356 #ifdef CONFIG_RFKILL_LEDS
359 rfkill->led_trigger.name = rfkill->dev.bus_id;
360 error = led_trigger_register(&rfkill->led_trigger);
362 rfkill->led_trigger.name = NULL;
363 #endif /* CONFIG_RFKILL_LEDS */
366 static void rfkill_led_trigger_unregister(struct rfkill *rfkill)
368 #ifdef CONFIG_RFKILL_LEDS
369 if (rfkill->led_trigger.name)
370 led_trigger_unregister(&rfkill->led_trigger);
375 * rfkill_register - Register a rfkill structure.
376 * @rfkill: rfkill structure to be registered
378 * This function should be called by the network driver when the rfkill
379 * structure needs to be registered. Immediately from registration the
380 * switch driver should be able to service calls to toggle_radio.
382 int rfkill_register(struct rfkill *rfkill)
384 static atomic_t rfkill_no = ATOMIC_INIT(0);
385 struct device *dev = &rfkill->dev;
388 if (!rfkill->toggle_radio)
391 error = rfkill_add_switch(rfkill);
395 snprintf(dev->bus_id, sizeof(dev->bus_id),
396 "rfkill%ld", (long)atomic_inc_return(&rfkill_no) - 1);
398 error = device_add(dev);
400 rfkill_remove_switch(rfkill);
403 rfkill_led_trigger_register(rfkill);
407 EXPORT_SYMBOL(rfkill_register);
410 * rfkill_unregister - Uegister a rfkill structure.
411 * @rfkill: rfkill structure to be unregistered
413 * This function should be called by the network driver during device
414 * teardown to destroy rfkill structure. Note that rfkill_free() should
415 * _not_ be called after rfkill_unregister().
417 void rfkill_unregister(struct rfkill *rfkill)
419 rfkill_led_trigger_unregister(rfkill);
420 device_del(&rfkill->dev);
421 rfkill_remove_switch(rfkill);
422 put_device(&rfkill->dev);
424 EXPORT_SYMBOL(rfkill_unregister);
427 * Rfkill module initialization/deinitialization.
429 static int __init rfkill_init(void)
434 for (i = 0; i < ARRAY_SIZE(rfkill_states); i++)
435 rfkill_states[i] = RFKILL_STATE_ON;
437 error = class_register(&rfkill_class);
439 printk(KERN_ERR "rfkill: unable to register rfkill class\n");
446 static void __exit rfkill_exit(void)
448 class_unregister(&rfkill_class);
451 module_init(rfkill_init);
452 module_exit(rfkill_exit);