blob: fb566902030a944746332c20dfdb80c1d94db58a [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
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -030065static void update_rfkill_state(struct rfkill *rfkill)
66{
67 enum rfkill_state newstate;
68
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);
74 }
75}
76
Ivo van Doorncf4328c2007-05-07 00:34:20 -070077static int rfkill_toggle_radio(struct rfkill *rfkill,
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -030078 enum rfkill_state state,
79 int force)
Ivo van Doorncf4328c2007-05-07 00:34:20 -070080{
Michael Buesch7f4c5342007-11-28 17:49:34 +010081 int retval = 0;
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -030082 enum rfkill_state oldstate, newstate;
83
84 oldstate = rfkill->state;
85
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -030086 if (rfkill->get_state && !force &&
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -030087 !rfkill->get_state(rfkill->data, &newstate))
88 rfkill->state = newstate;
Ivo van Doorncf4328c2007-05-07 00:34:20 -070089
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -030090 if (force || state != rfkill->state) {
Ivo van Doorncf4328c2007-05-07 00:34:20 -070091 retval = rfkill->toggle_radio(rfkill->data, state);
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -030092 if (!retval)
Ivo van Doorncf4328c2007-05-07 00:34:20 -070093 rfkill->state = state;
94 }
95
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -030096 if (force || rfkill->state != oldstate)
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -030097 rfkill_led_trigger(rfkill, rfkill->state);
98
Ivo van Doorncf4328c2007-05-07 00:34:20 -070099 return retval;
100}
101
102/**
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
106 *
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
109 * left alone.
110 */
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700111void rfkill_switch_all(enum rfkill_type type, enum rfkill_state state)
112{
113 struct rfkill *rfkill;
114
115 mutex_lock(&rfkill_mutex);
116
117 rfkill_states[type] = state;
118
119 list_for_each_entry(rfkill, &rfkill_list, node) {
Carlos Corbacho89796f62008-04-12 16:39:47 +0100120 if ((!rfkill->user_claim) && (rfkill->type == type))
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300121 rfkill_toggle_radio(rfkill, state, 0);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700122 }
123
124 mutex_unlock(&rfkill_mutex);
125}
126EXPORT_SYMBOL(rfkill_switch_all);
127
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300128/**
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.
132 *
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.
137 *
138 * It may not be called from an atomic context.
139 */
140int rfkill_force_state(struct rfkill *rfkill, enum rfkill_state state)
141{
142 if (state != RFKILL_STATE_OFF &&
143 state != RFKILL_STATE_ON)
144 return -EINVAL;
145
146 mutex_lock(&rfkill->mutex);
147 rfkill->state = state;
148 mutex_unlock(&rfkill->mutex);
149
150 return 0;
151}
152EXPORT_SYMBOL(rfkill_force_state);
153
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700154static ssize_t rfkill_name_show(struct device *dev,
155 struct device_attribute *attr,
156 char *buf)
157{
158 struct rfkill *rfkill = to_rfkill(dev);
159
160 return sprintf(buf, "%s\n", rfkill->name);
161}
162
163static ssize_t rfkill_type_show(struct device *dev,
164 struct device_attribute *attr,
165 char *buf)
166{
167 struct rfkill *rfkill = to_rfkill(dev);
168 const char *type;
169
170 switch (rfkill->type) {
171 case RFKILL_TYPE_WLAN:
172 type = "wlan";
173 break;
174 case RFKILL_TYPE_BLUETOOTH:
175 type = "bluetooth";
176 break;
Ivo van Doorne06654862007-09-13 09:21:31 +0200177 case RFKILL_TYPE_UWB:
178 type = "ultrawideband";
179 break;
Iñaky Pérez-González303d9bf2008-01-23 13:40:27 -0800180 case RFKILL_TYPE_WIMAX:
181 type = "wimax";
182 break;
Henrique de Moraes Holschuh477576a2008-06-23 17:23:01 -0300183 case RFKILL_TYPE_WWAN:
184 type = "wwan";
185 break;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700186 default:
187 BUG();
188 }
189
190 return sprintf(buf, "%s\n", type);
191}
192
193static ssize_t rfkill_state_show(struct device *dev,
194 struct device_attribute *attr,
195 char *buf)
196{
197 struct rfkill *rfkill = to_rfkill(dev);
198
Henrique de Moraes Holschuh801e49a2008-06-23 17:23:00 -0300199 update_rfkill_state(rfkill);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700200 return sprintf(buf, "%d\n", rfkill->state);
201}
202
203static ssize_t rfkill_state_store(struct device *dev,
204 struct device_attribute *attr,
205 const char *buf, size_t count)
206{
207 struct rfkill *rfkill = to_rfkill(dev);
208 unsigned int state = simple_strtoul(buf, NULL, 0);
209 int error;
210
211 if (!capable(CAP_NET_ADMIN))
212 return -EPERM;
213
Michael Buesch7f4c5342007-11-28 17:49:34 +0100214 if (mutex_lock_interruptible(&rfkill->mutex))
215 return -ERESTARTSYS;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700216 error = rfkill_toggle_radio(rfkill,
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300217 state ? RFKILL_STATE_ON : RFKILL_STATE_OFF,
218 0);
Michael Buesch7f4c5342007-11-28 17:49:34 +0100219 mutex_unlock(&rfkill->mutex);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700220
Michael Buesch7f4c5342007-11-28 17:49:34 +0100221 return error ? error : count;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700222}
223
224static ssize_t rfkill_claim_show(struct device *dev,
225 struct device_attribute *attr,
226 char *buf)
227{
228 struct rfkill *rfkill = to_rfkill(dev);
229
230 return sprintf(buf, "%d", rfkill->user_claim);
231}
232
233static ssize_t rfkill_claim_store(struct device *dev,
234 struct device_attribute *attr,
235 const char *buf, size_t count)
236{
237 struct rfkill *rfkill = to_rfkill(dev);
238 bool claim = !!simple_strtoul(buf, NULL, 0);
239 int error;
240
241 if (!capable(CAP_NET_ADMIN))
242 return -EPERM;
243
244 /*
245 * Take the global lock to make sure the kernel is not in
246 * the middle of rfkill_switch_all
247 */
248 error = mutex_lock_interruptible(&rfkill_mutex);
249 if (error)
250 return error;
251
Michael Buesch20405c02007-09-27 21:34:23 +0200252 if (rfkill->user_claim_unsupported) {
253 error = -EOPNOTSUPP;
254 goto out_unlock;
255 }
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700256 if (rfkill->user_claim != claim) {
257 if (!claim)
258 rfkill_toggle_radio(rfkill,
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300259 rfkill_states[rfkill->type],
260 0);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700261 rfkill->user_claim = claim;
262 }
263
Michael Buesch20405c02007-09-27 21:34:23 +0200264out_unlock:
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700265 mutex_unlock(&rfkill_mutex);
266
Michael Buesch20405c02007-09-27 21:34:23 +0200267 return error ? error : count;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700268}
269
270static struct device_attribute rfkill_dev_attrs[] = {
271 __ATTR(name, S_IRUGO, rfkill_name_show, NULL),
272 __ATTR(type, S_IRUGO, rfkill_type_show, NULL),
Ivo van Doornc81de6a2007-07-18 15:38:03 -0700273 __ATTR(state, S_IRUGO|S_IWUSR, rfkill_state_show, rfkill_state_store),
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700274 __ATTR(claim, S_IRUGO|S_IWUSR, rfkill_claim_show, rfkill_claim_store),
275 __ATTR_NULL
276};
277
278static void rfkill_release(struct device *dev)
279{
280 struct rfkill *rfkill = to_rfkill(dev);
281
282 kfree(rfkill);
283 module_put(THIS_MODULE);
284}
285
286#ifdef CONFIG_PM
287static int rfkill_suspend(struct device *dev, pm_message_t state)
288{
289 struct rfkill *rfkill = to_rfkill(dev);
290
291 if (dev->power.power_state.event != state.event) {
Rafael J. Wysocki3a2d5b72008-02-23 19:13:25 +0100292 if (state.event & PM_EVENT_SLEEP) {
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300293 /* Stop transmitter, keep state, no notifies */
294 update_rfkill_state(rfkill);
295
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700296 mutex_lock(&rfkill->mutex);
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300297 rfkill->toggle_radio(rfkill->data, RFKILL_STATE_OFF);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700298 mutex_unlock(&rfkill->mutex);
299 }
300
301 dev->power.power_state = state;
302 }
303
304 return 0;
305}
306
307static int rfkill_resume(struct device *dev)
308{
309 struct rfkill *rfkill = to_rfkill(dev);
310
311 if (dev->power.power_state.event != PM_EVENT_ON) {
312 mutex_lock(&rfkill->mutex);
313
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300314 /* restore radio state AND notify everybody */
315 rfkill_toggle_radio(rfkill, rfkill->state, 1);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700316
317 mutex_unlock(&rfkill->mutex);
318 }
319
320 dev->power.power_state = PMSG_ON;
321 return 0;
322}
323#else
324#define rfkill_suspend NULL
325#define rfkill_resume NULL
326#endif
327
328static struct class rfkill_class = {
329 .name = "rfkill",
330 .dev_release = rfkill_release,
331 .dev_attrs = rfkill_dev_attrs,
332 .suspend = rfkill_suspend,
333 .resume = rfkill_resume,
334};
335
336static int rfkill_add_switch(struct rfkill *rfkill)
337{
Michael Buesch7319f1e2007-10-28 15:16:50 +0100338 int error;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700339
Michael Buesch7319f1e2007-10-28 15:16:50 +0100340 mutex_lock(&rfkill_mutex);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700341
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300342 error = rfkill_toggle_radio(rfkill, rfkill_states[rfkill->type], 0);
Michael Buesch7319f1e2007-10-28 15:16:50 +0100343 if (!error)
344 list_add_tail(&rfkill->node, &rfkill_list);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700345
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700346 mutex_unlock(&rfkill_mutex);
Michael Buesch7319f1e2007-10-28 15:16:50 +0100347
348 return error;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700349}
350
351static void rfkill_remove_switch(struct rfkill *rfkill)
352{
353 mutex_lock(&rfkill_mutex);
354 list_del_init(&rfkill->node);
Henrique de Moraes Holschuh526324b2008-06-23 17:23:02 -0300355 rfkill_toggle_radio(rfkill, RFKILL_STATE_OFF, 1);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700356 mutex_unlock(&rfkill_mutex);
357}
358
359/**
360 * rfkill_allocate - allocate memory for rfkill structure.
361 * @parent: device that has rf switch on it
Ivo van Doorn234a0ca2007-09-13 09:20:42 +0200362 * @type: type of the switch (RFKILL_TYPE_*)
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700363 *
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.
370 */
371struct rfkill *rfkill_allocate(struct device *parent, enum rfkill_type type)
372{
373 struct rfkill *rfkill;
374 struct device *dev;
375
376 rfkill = kzalloc(sizeof(struct rfkill), GFP_KERNEL);
Ivo van Doornd007da12007-05-19 12:24:39 -0700377 if (!rfkill)
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700378 return NULL;
379
380 mutex_init(&rfkill->mutex);
381 INIT_LIST_HEAD(&rfkill->node);
382 rfkill->type = type;
383
384 dev = &rfkill->dev;
385 dev->class = &rfkill_class;
386 dev->parent = parent;
387 device_initialize(dev);
388
389 __module_get(THIS_MODULE);
390
391 return rfkill;
392}
393EXPORT_SYMBOL(rfkill_allocate);
394
395/**
396 * rfkill_free - Mark rfkill structure for deletion
397 * @rfkill: rfkill structure to be destroyed
398 *
Oliver Pinter1dbede82008-02-03 17:55:45 +0200399 * Decrements reference count of rfkill structure so it is destroyed.
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700400 * Note that rfkill_free() should _not_ be called after rfkill_unregister().
401 */
402void rfkill_free(struct rfkill *rfkill)
403{
404 if (rfkill)
405 put_device(&rfkill->dev);
406}
407EXPORT_SYMBOL(rfkill_free);
408
Michael Buesch135900c2007-09-27 21:33:12 +0200409static void rfkill_led_trigger_register(struct rfkill *rfkill)
410{
411#ifdef CONFIG_RFKILL_LEDS
412 int error;
413
414 rfkill->led_trigger.name = rfkill->dev.bus_id;
415 error = led_trigger_register(&rfkill->led_trigger);
416 if (error)
417 rfkill->led_trigger.name = NULL;
418#endif /* CONFIG_RFKILL_LEDS */
419}
420
421static void rfkill_led_trigger_unregister(struct rfkill *rfkill)
422{
423#ifdef CONFIG_RFKILL_LEDS
424 if (rfkill->led_trigger.name)
425 led_trigger_unregister(&rfkill->led_trigger);
426#endif
427}
428
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700429/**
430 * rfkill_register - Register a rfkill structure.
431 * @rfkill: rfkill structure to be registered
432 *
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.
436 */
437int rfkill_register(struct rfkill *rfkill)
438{
439 static atomic_t rfkill_no = ATOMIC_INIT(0);
440 struct device *dev = &rfkill->dev;
441 int error;
442
443 if (!rfkill->toggle_radio)
444 return -EINVAL;
Michael Buesch7319f1e2007-10-28 15:16:50 +0100445 if (rfkill->type >= RFKILL_TYPE_MAX)
446 return -EINVAL;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700447
Michael Buesch8a8f1c02007-10-28 13:07:54 +0100448 snprintf(dev->bus_id, sizeof(dev->bus_id),
449 "rfkill%ld", (long)atomic_inc_return(&rfkill_no) - 1);
450
451 rfkill_led_trigger_register(rfkill);
452
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700453 error = rfkill_add_switch(rfkill);
Eric Paris632041f2008-01-13 16:20:56 -0500454 if (error) {
455 rfkill_led_trigger_unregister(rfkill);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700456 return error;
Eric Paris632041f2008-01-13 16:20:56 -0500457 }
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700458
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700459 error = device_add(dev);
460 if (error) {
Eric Paris632041f2008-01-13 16:20:56 -0500461 rfkill_led_trigger_unregister(rfkill);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700462 rfkill_remove_switch(rfkill);
463 return error;
464 }
465
466 return 0;
467}
468EXPORT_SYMBOL(rfkill_register);
469
470/**
Henrique de Moraes Holschuhc8fcd902008-06-23 17:22:57 -0300471 * rfkill_unregister - Unregister a rfkill structure.
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700472 * @rfkill: rfkill structure to be unregistered
473 *
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().
477 */
478void rfkill_unregister(struct rfkill *rfkill)
479{
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700480 device_del(&rfkill->dev);
481 rfkill_remove_switch(rfkill);
Michael Buesch8a8f1c02007-10-28 13:07:54 +0100482 rfkill_led_trigger_unregister(rfkill);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700483 put_device(&rfkill->dev);
484}
485EXPORT_SYMBOL(rfkill_unregister);
486
487/*
488 * Rfkill module initialization/deinitialization.
489 */
490static int __init rfkill_init(void)
491{
492 int error;
493 int i;
494
Henrique de Moraes Holschuhe954b0b2008-06-23 17:22:59 -0300495 if (rfkill_default_state != RFKILL_STATE_OFF &&
496 rfkill_default_state != RFKILL_STATE_ON)
497 return -EINVAL;
498
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700499 for (i = 0; i < ARRAY_SIZE(rfkill_states); i++)
Henrique de Moraes Holschuhe954b0b2008-06-23 17:22:59 -0300500 rfkill_states[i] = rfkill_default_state;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700501
502 error = class_register(&rfkill_class);
503 if (error) {
504 printk(KERN_ERR "rfkill: unable to register rfkill class\n");
505 return error;
506 }
507
508 return 0;
509}
510
511static void __exit rfkill_exit(void)
512{
513 class_unregister(&rfkill_class);
514}
515
Michael Buesch2bf236d2007-10-28 14:39:02 +0100516subsys_initcall(rfkill_init);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700517module_exit(rfkill_exit);