2 * Copyright (C) 2006 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];
40 static int rfkill_toggle_radio(struct rfkill *rfkill,
41 enum rfkill_state state)
45 retval = mutex_lock_interruptible(&rfkill->mutex);
49 if (state != rfkill->state) {
50 retval = rfkill->toggle_radio(rfkill->data, state);
52 rfkill->state = state;
55 mutex_unlock(&rfkill->mutex);
60 * rfkill_switch_all - Toggle state of all switches of given type
61 * @type: type of interfaces to be affeceted
62 * @state: the new state
64 * This function toggles state of all switches of given type unless
65 * a specific switch is claimed by userspace in which case it is
69 void rfkill_switch_all(enum rfkill_type type, enum rfkill_state state)
71 struct rfkill *rfkill;
73 mutex_lock(&rfkill_mutex);
75 rfkill_states[type] = state;
77 list_for_each_entry(rfkill, &rfkill_list, node) {
78 if (!rfkill->user_claim)
79 rfkill_toggle_radio(rfkill, state);
82 mutex_unlock(&rfkill_mutex);
84 EXPORT_SYMBOL(rfkill_switch_all);
86 static ssize_t rfkill_name_show(struct device *dev,
87 struct device_attribute *attr,
90 struct rfkill *rfkill = to_rfkill(dev);
92 return sprintf(buf, "%s\n", rfkill->name);
95 static ssize_t rfkill_type_show(struct device *dev,
96 struct device_attribute *attr,
99 struct rfkill *rfkill = to_rfkill(dev);
102 switch (rfkill->type) {
103 case RFKILL_TYPE_WLAN:
106 case RFKILL_TYPE_BLUETOOTH:
113 return sprintf(buf, "%s\n", type);
116 static ssize_t rfkill_state_show(struct device *dev,
117 struct device_attribute *attr,
120 struct rfkill *rfkill = to_rfkill(dev);
122 return sprintf(buf, "%d\n", rfkill->state);
125 static ssize_t rfkill_state_store(struct device *dev,
126 struct device_attribute *attr,
127 const char *buf, size_t count)
129 struct rfkill *rfkill = to_rfkill(dev);
130 unsigned int state = simple_strtoul(buf, NULL, 0);
133 if (!capable(CAP_NET_ADMIN))
136 error = rfkill_toggle_radio(rfkill,
137 state ? RFKILL_STATE_ON : RFKILL_STATE_OFF);
144 static ssize_t rfkill_claim_show(struct device *dev,
145 struct device_attribute *attr,
148 struct rfkill *rfkill = to_rfkill(dev);
150 return sprintf(buf, "%d", rfkill->user_claim);
153 static ssize_t rfkill_claim_store(struct device *dev,
154 struct device_attribute *attr,
155 const char *buf, size_t count)
157 struct rfkill *rfkill = to_rfkill(dev);
158 bool claim = !!simple_strtoul(buf, NULL, 0);
161 if (!capable(CAP_NET_ADMIN))
165 * Take the global lock to make sure the kernel is not in
166 * the middle of rfkill_switch_all
168 error = mutex_lock_interruptible(&rfkill_mutex);
172 if (rfkill->user_claim != claim) {
174 rfkill_toggle_radio(rfkill,
175 rfkill_states[rfkill->type]);
176 rfkill->user_claim = claim;
179 mutex_unlock(&rfkill_mutex);
184 static struct device_attribute rfkill_dev_attrs[] = {
185 __ATTR(name, S_IRUGO, rfkill_name_show, NULL),
186 __ATTR(type, S_IRUGO, rfkill_type_show, NULL),
187 __ATTR(state, S_IRUGO|S_IWUSR, rfkill_state_show, rfkill_state_store),
188 __ATTR(claim, S_IRUGO|S_IWUSR, rfkill_claim_show, rfkill_claim_store),
192 static void rfkill_release(struct device *dev)
194 struct rfkill *rfkill = to_rfkill(dev);
197 module_put(THIS_MODULE);
201 static int rfkill_suspend(struct device *dev, pm_message_t state)
203 struct rfkill *rfkill = to_rfkill(dev);
205 if (dev->power.power_state.event != state.event) {
206 if (state.event == PM_EVENT_SUSPEND) {
207 mutex_lock(&rfkill->mutex);
209 if (rfkill->state == RFKILL_STATE_ON)
210 rfkill->toggle_radio(rfkill->data,
213 mutex_unlock(&rfkill->mutex);
216 dev->power.power_state = state;
222 static int rfkill_resume(struct device *dev)
224 struct rfkill *rfkill = to_rfkill(dev);
226 if (dev->power.power_state.event != PM_EVENT_ON) {
227 mutex_lock(&rfkill->mutex);
229 if (rfkill->state == RFKILL_STATE_ON)
230 rfkill->toggle_radio(rfkill->data, RFKILL_STATE_ON);
232 mutex_unlock(&rfkill->mutex);
235 dev->power.power_state = PMSG_ON;
239 #define rfkill_suspend NULL
240 #define rfkill_resume NULL
243 static struct class rfkill_class = {
245 .dev_release = rfkill_release,
246 .dev_attrs = rfkill_dev_attrs,
247 .suspend = rfkill_suspend,
248 .resume = rfkill_resume,
251 static int rfkill_add_switch(struct rfkill *rfkill)
255 retval = mutex_lock_interruptible(&rfkill_mutex);
259 retval = rfkill_toggle_radio(rfkill, rfkill_states[rfkill->type]);
263 list_add_tail(&rfkill->node, &rfkill_list);
266 mutex_unlock(&rfkill_mutex);
270 static void rfkill_remove_switch(struct rfkill *rfkill)
272 mutex_lock(&rfkill_mutex);
273 list_del_init(&rfkill->node);
274 rfkill_toggle_radio(rfkill, RFKILL_STATE_OFF);
275 mutex_unlock(&rfkill_mutex);
279 * rfkill_allocate - allocate memory for rfkill structure.
280 * @parent: device that has rf switch on it
281 * @type: type of the switch (RFKILL_TYPE_*)
283 * This function should be called by the network driver when it needs
284 * rfkill structure. Once the structure is allocated the driver shoud
285 * finish its initialization by setting name, private data, enable_radio
286 * and disable_radio methods and then register it with rfkill_register().
287 * NOTE: If registration fails the structure shoudl be freed by calling
288 * rfkill_free() otherwise rfkill_unregister() should be used.
290 struct rfkill *rfkill_allocate(struct device *parent, enum rfkill_type type)
292 struct rfkill *rfkill;
295 rfkill = kzalloc(sizeof(struct rfkill), GFP_KERNEL);
299 mutex_init(&rfkill->mutex);
300 INIT_LIST_HEAD(&rfkill->node);
304 dev->class = &rfkill_class;
305 dev->parent = parent;
306 device_initialize(dev);
308 __module_get(THIS_MODULE);
312 EXPORT_SYMBOL(rfkill_allocate);
315 * rfkill_free - Mark rfkill structure for deletion
316 * @rfkill: rfkill structure to be destroyed
318 * Decrements reference count of rfkill structure so it is destoryed.
319 * Note that rfkill_free() should _not_ be called after rfkill_unregister().
321 void rfkill_free(struct rfkill *rfkill)
324 put_device(&rfkill->dev);
326 EXPORT_SYMBOL(rfkill_free);
329 * rfkill_register - Register a rfkill structure.
330 * @rfkill: rfkill structure to be registered
332 * This function should be called by the network driver when the rfkill
333 * structure needs to be registered. Immediately from registration the
334 * switch driver should be able to service calls to toggle_radio.
336 int rfkill_register(struct rfkill *rfkill)
338 static atomic_t rfkill_no = ATOMIC_INIT(0);
339 struct device *dev = &rfkill->dev;
342 if (!rfkill->toggle_radio)
345 error = rfkill_add_switch(rfkill);
349 snprintf(dev->bus_id, sizeof(dev->bus_id),
350 "rfkill%ld", (long)atomic_inc_return(&rfkill_no) - 1);
352 error = device_add(dev);
354 rfkill_remove_switch(rfkill);
360 EXPORT_SYMBOL(rfkill_register);
363 * rfkill_unregister - Uegister a rfkill structure.
364 * @rfkill: rfkill structure to be unregistered
366 * This function should be called by the network driver during device
367 * teardown to destroy rfkill structure. Note that rfkill_free() should
368 * _not_ be called after rfkill_unregister().
370 void rfkill_unregister(struct rfkill *rfkill)
372 device_del(&rfkill->dev);
373 rfkill_remove_switch(rfkill);
374 put_device(&rfkill->dev);
376 EXPORT_SYMBOL(rfkill_unregister);
379 * Rfkill module initialization/deinitialization.
381 static int __init rfkill_init(void)
386 for (i = 0; i < ARRAY_SIZE(rfkill_states); i++)
387 rfkill_states[i] = RFKILL_STATE_ON;
389 error = class_register(&rfkill_class);
391 printk(KERN_ERR "rfkill: unable to register rfkill class\n");
398 static void __exit rfkill_exit(void)
400 class_unregister(&rfkill_class);
403 module_init(rfkill_init);
404 module_exit(rfkill_exit);