blob: 3edc585dcfa6ddbb75b9cc580194f13ad3c10c5f [file] [log] [blame]
Ivo van Doorncf4328c2007-05-07 00:34:20 -07001/*
Ivo van Doornfe242cf2007-09-27 14:57:05 -07002 * Copyright (C) 2006 - 2007 Ivo van Doorn
Ivo van Doorncf4328c2007-05-07 00:34:20 -07003 * Copyright (C) 2007 Dmitry Torokhov
4 *
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.
9 *
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.
14 *
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.
19 */
20
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>
29
Michael Buesch27366222007-11-02 20:18:11 +010030/* Get declaration of rfkill_switch_all() to shut up sparse. */
31#include "rfkill-input.h"
32
33
Ivo van Doorncf4328c2007-05-07 00:34:20 -070034MODULE_AUTHOR("Ivo van Doorn <IvDoorn@gmail.com>");
35MODULE_VERSION("1.0");
36MODULE_DESCRIPTION("RF switch support");
37MODULE_LICENSE("GPL");
38
39static LIST_HEAD(rfkill_list); /* list of registered rf switches */
40static DEFINE_MUTEX(rfkill_mutex);
41
Henrique de Moraes Holschuhe954b0b2008-06-23 17:22:59 -030042static unsigned int rfkill_default_state = RFKILL_STATE_ON;
43module_param_named(default_state, rfkill_default_state, uint, 0444);
44MODULE_PARM_DESC(default_state,
45 "Default initial state for all radio types, 0 = radio off");
46
Ivo van Doorncf4328c2007-05-07 00:34:20 -070047static enum rfkill_state rfkill_states[RFKILL_TYPE_MAX];
48
Michael Buesch135900c2007-09-27 21:33:12 +020049
50static void rfkill_led_trigger(struct rfkill *rfkill,
51 enum rfkill_state state)
52{
53#ifdef CONFIG_RFKILL_LEDS
54 struct led_trigger *led = &rfkill->led_trigger;
55
56 if (!led->name)
57 return;
58 if (state == RFKILL_STATE_OFF)
59 led_trigger_event(led, LED_OFF);
60 else
61 led_trigger_event(led, LED_FULL);
62#endif /* CONFIG_RFKILL_LEDS */
63}
64
Ivo van Doorncf4328c2007-05-07 00:34:20 -070065static int rfkill_toggle_radio(struct rfkill *rfkill,
66 enum rfkill_state state)
67{
Michael Buesch7f4c5342007-11-28 17:49:34 +010068 int retval = 0;
Ivo van Doorncf4328c2007-05-07 00:34:20 -070069
70 if (state != rfkill->state) {
71 retval = rfkill->toggle_radio(rfkill->data, state);
Michael Buesch135900c2007-09-27 21:33:12 +020072 if (!retval) {
Ivo van Doorncf4328c2007-05-07 00:34:20 -070073 rfkill->state = state;
Michael Buesch135900c2007-09-27 21:33:12 +020074 rfkill_led_trigger(rfkill, state);
75 }
Ivo van Doorncf4328c2007-05-07 00:34:20 -070076 }
77
Ivo van Doorncf4328c2007-05-07 00:34:20 -070078 return retval;
79}
80
81/**
82 * rfkill_switch_all - Toggle state of all switches of given type
83 * @type: type of interfaces to be affeceted
84 * @state: the new state
85 *
86 * This function toggles state of all switches of given type unless
87 * a specific switch is claimed by userspace in which case it is
88 * left alone.
89 */
90
91void rfkill_switch_all(enum rfkill_type type, enum rfkill_state state)
92{
93 struct rfkill *rfkill;
94
95 mutex_lock(&rfkill_mutex);
96
97 rfkill_states[type] = state;
98
99 list_for_each_entry(rfkill, &rfkill_list, node) {
Carlos Corbacho89796f62008-04-12 16:39:47 +0100100 if ((!rfkill->user_claim) && (rfkill->type == type))
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700101 rfkill_toggle_radio(rfkill, state);
102 }
103
104 mutex_unlock(&rfkill_mutex);
105}
106EXPORT_SYMBOL(rfkill_switch_all);
107
108static ssize_t rfkill_name_show(struct device *dev,
109 struct device_attribute *attr,
110 char *buf)
111{
112 struct rfkill *rfkill = to_rfkill(dev);
113
114 return sprintf(buf, "%s\n", rfkill->name);
115}
116
117static ssize_t rfkill_type_show(struct device *dev,
118 struct device_attribute *attr,
119 char *buf)
120{
121 struct rfkill *rfkill = to_rfkill(dev);
122 const char *type;
123
124 switch (rfkill->type) {
125 case RFKILL_TYPE_WLAN:
126 type = "wlan";
127 break;
128 case RFKILL_TYPE_BLUETOOTH:
129 type = "bluetooth";
130 break;
Ivo van Doorne06654862007-09-13 09:21:31 +0200131 case RFKILL_TYPE_UWB:
132 type = "ultrawideband";
133 break;
Iñaky Pérez-González303d9bf2008-01-23 13:40:27 -0800134 case RFKILL_TYPE_WIMAX:
135 type = "wimax";
136 break;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700137 default:
138 BUG();
139 }
140
141 return sprintf(buf, "%s\n", type);
142}
143
144static ssize_t rfkill_state_show(struct device *dev,
145 struct device_attribute *attr,
146 char *buf)
147{
148 struct rfkill *rfkill = to_rfkill(dev);
149
150 return sprintf(buf, "%d\n", rfkill->state);
151}
152
153static ssize_t rfkill_state_store(struct device *dev,
154 struct device_attribute *attr,
155 const char *buf, size_t count)
156{
157 struct rfkill *rfkill = to_rfkill(dev);
158 unsigned int state = simple_strtoul(buf, NULL, 0);
159 int error;
160
161 if (!capable(CAP_NET_ADMIN))
162 return -EPERM;
163
Michael Buesch7f4c5342007-11-28 17:49:34 +0100164 if (mutex_lock_interruptible(&rfkill->mutex))
165 return -ERESTARTSYS;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700166 error = rfkill_toggle_radio(rfkill,
167 state ? RFKILL_STATE_ON : RFKILL_STATE_OFF);
Michael Buesch7f4c5342007-11-28 17:49:34 +0100168 mutex_unlock(&rfkill->mutex);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700169
Michael Buesch7f4c5342007-11-28 17:49:34 +0100170 return error ? error : count;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700171}
172
173static ssize_t rfkill_claim_show(struct device *dev,
174 struct device_attribute *attr,
175 char *buf)
176{
177 struct rfkill *rfkill = to_rfkill(dev);
178
179 return sprintf(buf, "%d", rfkill->user_claim);
180}
181
182static ssize_t rfkill_claim_store(struct device *dev,
183 struct device_attribute *attr,
184 const char *buf, size_t count)
185{
186 struct rfkill *rfkill = to_rfkill(dev);
187 bool claim = !!simple_strtoul(buf, NULL, 0);
188 int error;
189
190 if (!capable(CAP_NET_ADMIN))
191 return -EPERM;
192
193 /*
194 * Take the global lock to make sure the kernel is not in
195 * the middle of rfkill_switch_all
196 */
197 error = mutex_lock_interruptible(&rfkill_mutex);
198 if (error)
199 return error;
200
Michael Buesch20405c02007-09-27 21:34:23 +0200201 if (rfkill->user_claim_unsupported) {
202 error = -EOPNOTSUPP;
203 goto out_unlock;
204 }
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700205 if (rfkill->user_claim != claim) {
206 if (!claim)
207 rfkill_toggle_radio(rfkill,
208 rfkill_states[rfkill->type]);
209 rfkill->user_claim = claim;
210 }
211
Michael Buesch20405c02007-09-27 21:34:23 +0200212out_unlock:
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700213 mutex_unlock(&rfkill_mutex);
214
Michael Buesch20405c02007-09-27 21:34:23 +0200215 return error ? error : count;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700216}
217
218static struct device_attribute rfkill_dev_attrs[] = {
219 __ATTR(name, S_IRUGO, rfkill_name_show, NULL),
220 __ATTR(type, S_IRUGO, rfkill_type_show, NULL),
Ivo van Doornc81de6a2007-07-18 15:38:03 -0700221 __ATTR(state, S_IRUGO|S_IWUSR, rfkill_state_show, rfkill_state_store),
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700222 __ATTR(claim, S_IRUGO|S_IWUSR, rfkill_claim_show, rfkill_claim_store),
223 __ATTR_NULL
224};
225
226static void rfkill_release(struct device *dev)
227{
228 struct rfkill *rfkill = to_rfkill(dev);
229
230 kfree(rfkill);
231 module_put(THIS_MODULE);
232}
233
234#ifdef CONFIG_PM
235static int rfkill_suspend(struct device *dev, pm_message_t state)
236{
237 struct rfkill *rfkill = to_rfkill(dev);
238
239 if (dev->power.power_state.event != state.event) {
Rafael J. Wysocki3a2d5b72008-02-23 19:13:25 +0100240 if (state.event & PM_EVENT_SLEEP) {
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700241 mutex_lock(&rfkill->mutex);
242
243 if (rfkill->state == RFKILL_STATE_ON)
244 rfkill->toggle_radio(rfkill->data,
245 RFKILL_STATE_OFF);
246
247 mutex_unlock(&rfkill->mutex);
248 }
249
250 dev->power.power_state = state;
251 }
252
253 return 0;
254}
255
256static int rfkill_resume(struct device *dev)
257{
258 struct rfkill *rfkill = to_rfkill(dev);
259
260 if (dev->power.power_state.event != PM_EVENT_ON) {
261 mutex_lock(&rfkill->mutex);
262
263 if (rfkill->state == RFKILL_STATE_ON)
264 rfkill->toggle_radio(rfkill->data, RFKILL_STATE_ON);
265
266 mutex_unlock(&rfkill->mutex);
267 }
268
269 dev->power.power_state = PMSG_ON;
270 return 0;
271}
272#else
273#define rfkill_suspend NULL
274#define rfkill_resume NULL
275#endif
276
277static struct class rfkill_class = {
278 .name = "rfkill",
279 .dev_release = rfkill_release,
280 .dev_attrs = rfkill_dev_attrs,
281 .suspend = rfkill_suspend,
282 .resume = rfkill_resume,
283};
284
285static int rfkill_add_switch(struct rfkill *rfkill)
286{
Michael Buesch7319f1e2007-10-28 15:16:50 +0100287 int error;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700288
Michael Buesch7319f1e2007-10-28 15:16:50 +0100289 mutex_lock(&rfkill_mutex);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700290
Michael Buesch7319f1e2007-10-28 15:16:50 +0100291 error = rfkill_toggle_radio(rfkill, rfkill_states[rfkill->type]);
292 if (!error)
293 list_add_tail(&rfkill->node, &rfkill_list);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700294
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700295 mutex_unlock(&rfkill_mutex);
Michael Buesch7319f1e2007-10-28 15:16:50 +0100296
297 return error;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700298}
299
300static void rfkill_remove_switch(struct rfkill *rfkill)
301{
302 mutex_lock(&rfkill_mutex);
303 list_del_init(&rfkill->node);
304 rfkill_toggle_radio(rfkill, RFKILL_STATE_OFF);
305 mutex_unlock(&rfkill_mutex);
306}
307
308/**
309 * rfkill_allocate - allocate memory for rfkill structure.
310 * @parent: device that has rf switch on it
Ivo van Doorn234a0ca2007-09-13 09:20:42 +0200311 * @type: type of the switch (RFKILL_TYPE_*)
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700312 *
313 * This function should be called by the network driver when it needs
314 * rfkill structure. Once the structure is allocated the driver shoud
315 * finish its initialization by setting name, private data, enable_radio
316 * and disable_radio methods and then register it with rfkill_register().
317 * NOTE: If registration fails the structure shoudl be freed by calling
318 * rfkill_free() otherwise rfkill_unregister() should be used.
319 */
320struct rfkill *rfkill_allocate(struct device *parent, enum rfkill_type type)
321{
322 struct rfkill *rfkill;
323 struct device *dev;
324
325 rfkill = kzalloc(sizeof(struct rfkill), GFP_KERNEL);
Ivo van Doornd007da12007-05-19 12:24:39 -0700326 if (!rfkill)
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700327 return NULL;
328
329 mutex_init(&rfkill->mutex);
330 INIT_LIST_HEAD(&rfkill->node);
331 rfkill->type = type;
332
333 dev = &rfkill->dev;
334 dev->class = &rfkill_class;
335 dev->parent = parent;
336 device_initialize(dev);
337
338 __module_get(THIS_MODULE);
339
340 return rfkill;
341}
342EXPORT_SYMBOL(rfkill_allocate);
343
344/**
345 * rfkill_free - Mark rfkill structure for deletion
346 * @rfkill: rfkill structure to be destroyed
347 *
Oliver Pinter1dbede82008-02-03 17:55:45 +0200348 * Decrements reference count of rfkill structure so it is destroyed.
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700349 * Note that rfkill_free() should _not_ be called after rfkill_unregister().
350 */
351void rfkill_free(struct rfkill *rfkill)
352{
353 if (rfkill)
354 put_device(&rfkill->dev);
355}
356EXPORT_SYMBOL(rfkill_free);
357
Michael Buesch135900c2007-09-27 21:33:12 +0200358static void rfkill_led_trigger_register(struct rfkill *rfkill)
359{
360#ifdef CONFIG_RFKILL_LEDS
361 int error;
362
363 rfkill->led_trigger.name = rfkill->dev.bus_id;
364 error = led_trigger_register(&rfkill->led_trigger);
365 if (error)
366 rfkill->led_trigger.name = NULL;
367#endif /* CONFIG_RFKILL_LEDS */
368}
369
370static void rfkill_led_trigger_unregister(struct rfkill *rfkill)
371{
372#ifdef CONFIG_RFKILL_LEDS
373 if (rfkill->led_trigger.name)
374 led_trigger_unregister(&rfkill->led_trigger);
375#endif
376}
377
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700378/**
379 * rfkill_register - Register a rfkill structure.
380 * @rfkill: rfkill structure to be registered
381 *
382 * This function should be called by the network driver when the rfkill
383 * structure needs to be registered. Immediately from registration the
384 * switch driver should be able to service calls to toggle_radio.
385 */
386int rfkill_register(struct rfkill *rfkill)
387{
388 static atomic_t rfkill_no = ATOMIC_INIT(0);
389 struct device *dev = &rfkill->dev;
390 int error;
391
392 if (!rfkill->toggle_radio)
393 return -EINVAL;
Michael Buesch7319f1e2007-10-28 15:16:50 +0100394 if (rfkill->type >= RFKILL_TYPE_MAX)
395 return -EINVAL;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700396
Michael Buesch8a8f1c02007-10-28 13:07:54 +0100397 snprintf(dev->bus_id, sizeof(dev->bus_id),
398 "rfkill%ld", (long)atomic_inc_return(&rfkill_no) - 1);
399
400 rfkill_led_trigger_register(rfkill);
401
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700402 error = rfkill_add_switch(rfkill);
Eric Paris632041f2008-01-13 16:20:56 -0500403 if (error) {
404 rfkill_led_trigger_unregister(rfkill);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700405 return error;
Eric Paris632041f2008-01-13 16:20:56 -0500406 }
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700407
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700408 error = device_add(dev);
409 if (error) {
Eric Paris632041f2008-01-13 16:20:56 -0500410 rfkill_led_trigger_unregister(rfkill);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700411 rfkill_remove_switch(rfkill);
412 return error;
413 }
414
415 return 0;
416}
417EXPORT_SYMBOL(rfkill_register);
418
419/**
Henrique de Moraes Holschuhc8fcd902008-06-23 17:22:57 -0300420 * rfkill_unregister - Unregister a rfkill structure.
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700421 * @rfkill: rfkill structure to be unregistered
422 *
423 * This function should be called by the network driver during device
424 * teardown to destroy rfkill structure. Note that rfkill_free() should
425 * _not_ be called after rfkill_unregister().
426 */
427void rfkill_unregister(struct rfkill *rfkill)
428{
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700429 device_del(&rfkill->dev);
430 rfkill_remove_switch(rfkill);
Michael Buesch8a8f1c02007-10-28 13:07:54 +0100431 rfkill_led_trigger_unregister(rfkill);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700432 put_device(&rfkill->dev);
433}
434EXPORT_SYMBOL(rfkill_unregister);
435
436/*
437 * Rfkill module initialization/deinitialization.
438 */
439static int __init rfkill_init(void)
440{
441 int error;
442 int i;
443
Henrique de Moraes Holschuhe954b0b2008-06-23 17:22:59 -0300444 if (rfkill_default_state != RFKILL_STATE_OFF &&
445 rfkill_default_state != RFKILL_STATE_ON)
446 return -EINVAL;
447
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700448 for (i = 0; i < ARRAY_SIZE(rfkill_states); i++)
Henrique de Moraes Holschuhe954b0b2008-06-23 17:22:59 -0300449 rfkill_states[i] = rfkill_default_state;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700450
451 error = class_register(&rfkill_class);
452 if (error) {
453 printk(KERN_ERR "rfkill: unable to register rfkill class\n");
454 return error;
455 }
456
457 return 0;
458}
459
460static void __exit rfkill_exit(void)
461{
462 class_unregister(&rfkill_class);
463}
464
Michael Buesch2bf236d2007-10-28 14:39:02 +0100465subsys_initcall(rfkill_init);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700466module_exit(rfkill_exit);