Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / net / rfkill / rfkill.c
blobd0d2b2e22fbef2e1d5ae96e0cac09d35dc7d83f8
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 enum rfkill_state rfkill_states[RFKILL_TYPE_MAX];
45 static void rfkill_led_trigger(struct rfkill *rfkill,
46 enum rfkill_state state)
48 #ifdef CONFIG_RFKILL_LEDS
49 struct led_trigger *led = &rfkill->led_trigger;
51 if (!led->name)
52 return;
53 if (state == RFKILL_STATE_OFF)
54 led_trigger_event(led, LED_OFF);
55 else
56 led_trigger_event(led, LED_FULL);
57 #endif /* CONFIG_RFKILL_LEDS */
60 static int rfkill_toggle_radio(struct rfkill *rfkill,
61 enum rfkill_state state)
63 int retval = 0;
65 if (state != rfkill->state) {
66 retval = rfkill->toggle_radio(rfkill->data, state);
67 if (!retval) {
68 rfkill->state = state;
69 rfkill_led_trigger(rfkill, state);
73 return retval;
76 /**
77 * rfkill_switch_all - Toggle state of all switches of given type
78 * @type: type of interfaces to be affeceted
79 * @state: the new state
81 * This function toggles state of all switches of given type unless
82 * a specific switch is claimed by userspace in which case it is
83 * left alone.
86 void rfkill_switch_all(enum rfkill_type type, enum rfkill_state state)
88 struct rfkill *rfkill;
90 mutex_lock(&rfkill_mutex);
92 rfkill_states[type] = state;
94 list_for_each_entry(rfkill, &rfkill_list, node) {
95 if (!rfkill->user_claim)
96 rfkill_toggle_radio(rfkill, state);
99 mutex_unlock(&rfkill_mutex);
101 EXPORT_SYMBOL(rfkill_switch_all);
103 static ssize_t rfkill_name_show(struct device *dev,
104 struct device_attribute *attr,
105 char *buf)
107 struct rfkill *rfkill = to_rfkill(dev);
109 return sprintf(buf, "%s\n", rfkill->name);
112 static ssize_t rfkill_type_show(struct device *dev,
113 struct device_attribute *attr,
114 char *buf)
116 struct rfkill *rfkill = to_rfkill(dev);
117 const char *type;
119 switch (rfkill->type) {
120 case RFKILL_TYPE_WLAN:
121 type = "wlan";
122 break;
123 case RFKILL_TYPE_BLUETOOTH:
124 type = "bluetooth";
125 break;
126 case RFKILL_TYPE_UWB:
127 type = "ultrawideband";
128 break;
129 case RFKILL_TYPE_WIMAX:
130 type = "wimax";
131 break;
132 default:
133 BUG();
136 return sprintf(buf, "%s\n", type);
139 static ssize_t rfkill_state_show(struct device *dev,
140 struct device_attribute *attr,
141 char *buf)
143 struct rfkill *rfkill = to_rfkill(dev);
145 return sprintf(buf, "%d\n", rfkill->state);
148 static ssize_t rfkill_state_store(struct device *dev,
149 struct device_attribute *attr,
150 const char *buf, size_t count)
152 struct rfkill *rfkill = to_rfkill(dev);
153 unsigned int state = simple_strtoul(buf, NULL, 0);
154 int error;
156 if (!capable(CAP_NET_ADMIN))
157 return -EPERM;
159 if (mutex_lock_interruptible(&rfkill->mutex))
160 return -ERESTARTSYS;
161 error = rfkill_toggle_radio(rfkill,
162 state ? RFKILL_STATE_ON : RFKILL_STATE_OFF);
163 mutex_unlock(&rfkill->mutex);
165 return error ? error : count;
168 static ssize_t rfkill_claim_show(struct device *dev,
169 struct device_attribute *attr,
170 char *buf)
172 struct rfkill *rfkill = to_rfkill(dev);
174 return sprintf(buf, "%d", rfkill->user_claim);
177 static ssize_t rfkill_claim_store(struct device *dev,
178 struct device_attribute *attr,
179 const char *buf, size_t count)
181 struct rfkill *rfkill = to_rfkill(dev);
182 bool claim = !!simple_strtoul(buf, NULL, 0);
183 int error;
185 if (!capable(CAP_NET_ADMIN))
186 return -EPERM;
189 * Take the global lock to make sure the kernel is not in
190 * the middle of rfkill_switch_all
192 error = mutex_lock_interruptible(&rfkill_mutex);
193 if (error)
194 return error;
196 if (rfkill->user_claim_unsupported) {
197 error = -EOPNOTSUPP;
198 goto out_unlock;
200 if (rfkill->user_claim != claim) {
201 if (!claim)
202 rfkill_toggle_radio(rfkill,
203 rfkill_states[rfkill->type]);
204 rfkill->user_claim = claim;
207 out_unlock:
208 mutex_unlock(&rfkill_mutex);
210 return error ? error : count;
213 static struct device_attribute rfkill_dev_attrs[] = {
214 __ATTR(name, S_IRUGO, rfkill_name_show, NULL),
215 __ATTR(type, S_IRUGO, rfkill_type_show, NULL),
216 __ATTR(state, S_IRUGO|S_IWUSR, rfkill_state_show, rfkill_state_store),
217 __ATTR(claim, S_IRUGO|S_IWUSR, rfkill_claim_show, rfkill_claim_store),
218 __ATTR_NULL
221 static void rfkill_release(struct device *dev)
223 struct rfkill *rfkill = to_rfkill(dev);
225 kfree(rfkill);
226 module_put(THIS_MODULE);
229 #ifdef CONFIG_PM
230 static int rfkill_suspend(struct device *dev, pm_message_t state)
232 struct rfkill *rfkill = to_rfkill(dev);
234 if (dev->power.power_state.event != state.event) {
235 <<<<<<< HEAD:net/rfkill/rfkill.c
236 if (state.event == PM_EVENT_SUSPEND) {
237 =======
238 if (state.event & PM_EVENT_SLEEP) {
239 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/rfkill/rfkill.c
240 mutex_lock(&rfkill->mutex);
242 if (rfkill->state == RFKILL_STATE_ON)
243 rfkill->toggle_radio(rfkill->data,
244 RFKILL_STATE_OFF);
246 mutex_unlock(&rfkill->mutex);
249 dev->power.power_state = state;
252 return 0;
255 static int rfkill_resume(struct device *dev)
257 struct rfkill *rfkill = to_rfkill(dev);
259 if (dev->power.power_state.event != PM_EVENT_ON) {
260 mutex_lock(&rfkill->mutex);
262 if (rfkill->state == RFKILL_STATE_ON)
263 rfkill->toggle_radio(rfkill->data, RFKILL_STATE_ON);
265 mutex_unlock(&rfkill->mutex);
268 dev->power.power_state = PMSG_ON;
269 return 0;
271 #else
272 #define rfkill_suspend NULL
273 #define rfkill_resume NULL
274 #endif
276 static struct class rfkill_class = {
277 .name = "rfkill",
278 .dev_release = rfkill_release,
279 .dev_attrs = rfkill_dev_attrs,
280 .suspend = rfkill_suspend,
281 .resume = rfkill_resume,
284 static int rfkill_add_switch(struct rfkill *rfkill)
286 int error;
288 mutex_lock(&rfkill_mutex);
290 error = rfkill_toggle_radio(rfkill, rfkill_states[rfkill->type]);
291 if (!error)
292 list_add_tail(&rfkill->node, &rfkill_list);
294 mutex_unlock(&rfkill_mutex);
296 return error;
299 static void rfkill_remove_switch(struct rfkill *rfkill)
301 mutex_lock(&rfkill_mutex);
302 list_del_init(&rfkill->node);
303 rfkill_toggle_radio(rfkill, RFKILL_STATE_OFF);
304 mutex_unlock(&rfkill_mutex);
308 * rfkill_allocate - allocate memory for rfkill structure.
309 * @parent: device that has rf switch on it
310 * @type: type of the switch (RFKILL_TYPE_*)
312 * This function should be called by the network driver when it needs
313 * rfkill structure. Once the structure is allocated the driver shoud
314 * finish its initialization by setting name, private data, enable_radio
315 * and disable_radio methods and then register it with rfkill_register().
316 * NOTE: If registration fails the structure shoudl be freed by calling
317 * rfkill_free() otherwise rfkill_unregister() should be used.
319 struct rfkill *rfkill_allocate(struct device *parent, enum rfkill_type type)
321 struct rfkill *rfkill;
322 struct device *dev;
324 rfkill = kzalloc(sizeof(struct rfkill), GFP_KERNEL);
325 if (!rfkill)
326 return NULL;
328 mutex_init(&rfkill->mutex);
329 INIT_LIST_HEAD(&rfkill->node);
330 rfkill->type = type;
332 dev = &rfkill->dev;
333 dev->class = &rfkill_class;
334 dev->parent = parent;
335 device_initialize(dev);
337 __module_get(THIS_MODULE);
339 return rfkill;
341 EXPORT_SYMBOL(rfkill_allocate);
344 * rfkill_free - Mark rfkill structure for deletion
345 * @rfkill: rfkill structure to be destroyed
347 * Decrements reference count of rfkill structure so it is destroyed.
348 * Note that rfkill_free() should _not_ be called after rfkill_unregister().
350 void rfkill_free(struct rfkill *rfkill)
352 if (rfkill)
353 put_device(&rfkill->dev);
355 EXPORT_SYMBOL(rfkill_free);
357 static void rfkill_led_trigger_register(struct rfkill *rfkill)
359 #ifdef CONFIG_RFKILL_LEDS
360 int error;
362 rfkill->led_trigger.name = rfkill->dev.bus_id;
363 error = led_trigger_register(&rfkill->led_trigger);
364 if (error)
365 rfkill->led_trigger.name = NULL;
366 #endif /* CONFIG_RFKILL_LEDS */
369 static void rfkill_led_trigger_unregister(struct rfkill *rfkill)
371 #ifdef CONFIG_RFKILL_LEDS
372 if (rfkill->led_trigger.name)
373 led_trigger_unregister(&rfkill->led_trigger);
374 #endif
378 * rfkill_register - Register a rfkill structure.
379 * @rfkill: rfkill structure to be registered
381 * This function should be called by the network driver when the rfkill
382 * structure needs to be registered. Immediately from registration the
383 * switch driver should be able to service calls to toggle_radio.
385 int rfkill_register(struct rfkill *rfkill)
387 static atomic_t rfkill_no = ATOMIC_INIT(0);
388 struct device *dev = &rfkill->dev;
389 int error;
391 if (!rfkill->toggle_radio)
392 return -EINVAL;
393 if (rfkill->type >= RFKILL_TYPE_MAX)
394 return -EINVAL;
396 snprintf(dev->bus_id, sizeof(dev->bus_id),
397 "rfkill%ld", (long)atomic_inc_return(&rfkill_no) - 1);
399 rfkill_led_trigger_register(rfkill);
401 error = rfkill_add_switch(rfkill);
402 if (error) {
403 rfkill_led_trigger_unregister(rfkill);
404 return error;
407 error = device_add(dev);
408 if (error) {
409 rfkill_led_trigger_unregister(rfkill);
410 rfkill_remove_switch(rfkill);
411 return error;
414 return 0;
416 EXPORT_SYMBOL(rfkill_register);
419 * rfkill_unregister - Uegister a rfkill structure.
420 * @rfkill: rfkill structure to be unregistered
422 * This function should be called by the network driver during device
423 * teardown to destroy rfkill structure. Note that rfkill_free() should
424 * _not_ be called after rfkill_unregister().
426 void rfkill_unregister(struct rfkill *rfkill)
428 device_del(&rfkill->dev);
429 rfkill_remove_switch(rfkill);
430 rfkill_led_trigger_unregister(rfkill);
431 put_device(&rfkill->dev);
433 EXPORT_SYMBOL(rfkill_unregister);
436 * Rfkill module initialization/deinitialization.
438 static int __init rfkill_init(void)
440 int error;
441 int i;
443 for (i = 0; i < ARRAY_SIZE(rfkill_states); i++)
444 rfkill_states[i] = RFKILL_STATE_ON;
446 error = class_register(&rfkill_class);
447 if (error) {
448 printk(KERN_ERR "rfkill: unable to register rfkill class\n");
449 return error;
452 return 0;
455 static void __exit rfkill_exit(void)
457 class_unregister(&rfkill_class);
460 subsys_initcall(rfkill_init);
461 module_exit(rfkill_exit);