blob: 73d60a307129110de0aec93b57c6fdacd76d1b88 [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
42static enum rfkill_state rfkill_states[RFKILL_TYPE_MAX];
43
Michael Buesch135900c2007-09-27 21:33:12 +020044
45static void rfkill_led_trigger(struct rfkill *rfkill,
46 enum rfkill_state state)
47{
48#ifdef CONFIG_RFKILL_LEDS
49 struct led_trigger *led = &rfkill->led_trigger;
50
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 */
58}
59
Ivo van Doorncf4328c2007-05-07 00:34:20 -070060static int rfkill_toggle_radio(struct rfkill *rfkill,
61 enum rfkill_state state)
62{
63 int retval;
64
65 retval = mutex_lock_interruptible(&rfkill->mutex);
66 if (retval)
67 return retval;
68
69 if (state != rfkill->state) {
70 retval = rfkill->toggle_radio(rfkill->data, state);
Michael Buesch135900c2007-09-27 21:33:12 +020071 if (!retval) {
Ivo van Doorncf4328c2007-05-07 00:34:20 -070072 rfkill->state = state;
Michael Buesch135900c2007-09-27 21:33:12 +020073 rfkill_led_trigger(rfkill, state);
74 }
Ivo van Doorncf4328c2007-05-07 00:34:20 -070075 }
76
77 mutex_unlock(&rfkill->mutex);
78 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) {
100 if (!rfkill->user_claim)
101 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;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700134 default:
135 BUG();
136 }
137
138 return sprintf(buf, "%s\n", type);
139}
140
141static ssize_t rfkill_state_show(struct device *dev,
142 struct device_attribute *attr,
143 char *buf)
144{
145 struct rfkill *rfkill = to_rfkill(dev);
146
147 return sprintf(buf, "%d\n", rfkill->state);
148}
149
150static ssize_t rfkill_state_store(struct device *dev,
151 struct device_attribute *attr,
152 const char *buf, size_t count)
153{
154 struct rfkill *rfkill = to_rfkill(dev);
155 unsigned int state = simple_strtoul(buf, NULL, 0);
156 int error;
157
158 if (!capable(CAP_NET_ADMIN))
159 return -EPERM;
160
161 error = rfkill_toggle_radio(rfkill,
162 state ? RFKILL_STATE_ON : RFKILL_STATE_OFF);
163 if (error)
164 return error;
165
166 return count;
167}
168
169static ssize_t rfkill_claim_show(struct device *dev,
170 struct device_attribute *attr,
171 char *buf)
172{
173 struct rfkill *rfkill = to_rfkill(dev);
174
175 return sprintf(buf, "%d", rfkill->user_claim);
176}
177
178static ssize_t rfkill_claim_store(struct device *dev,
179 struct device_attribute *attr,
180 const char *buf, size_t count)
181{
182 struct rfkill *rfkill = to_rfkill(dev);
183 bool claim = !!simple_strtoul(buf, NULL, 0);
184 int error;
185
186 if (!capable(CAP_NET_ADMIN))
187 return -EPERM;
188
189 /*
190 * Take the global lock to make sure the kernel is not in
191 * the middle of rfkill_switch_all
192 */
193 error = mutex_lock_interruptible(&rfkill_mutex);
194 if (error)
195 return error;
196
Michael Buesch20405c02007-09-27 21:34:23 +0200197 if (rfkill->user_claim_unsupported) {
198 error = -EOPNOTSUPP;
199 goto out_unlock;
200 }
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700201 if (rfkill->user_claim != claim) {
202 if (!claim)
203 rfkill_toggle_radio(rfkill,
204 rfkill_states[rfkill->type]);
205 rfkill->user_claim = claim;
206 }
207
Michael Buesch20405c02007-09-27 21:34:23 +0200208out_unlock:
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700209 mutex_unlock(&rfkill_mutex);
210
Michael Buesch20405c02007-09-27 21:34:23 +0200211 return error ? error : count;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700212}
213
214static struct device_attribute rfkill_dev_attrs[] = {
215 __ATTR(name, S_IRUGO, rfkill_name_show, NULL),
216 __ATTR(type, S_IRUGO, rfkill_type_show, NULL),
Ivo van Doornc81de6a2007-07-18 15:38:03 -0700217 __ATTR(state, S_IRUGO|S_IWUSR, rfkill_state_show, rfkill_state_store),
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700218 __ATTR(claim, S_IRUGO|S_IWUSR, rfkill_claim_show, rfkill_claim_store),
219 __ATTR_NULL
220};
221
222static void rfkill_release(struct device *dev)
223{
224 struct rfkill *rfkill = to_rfkill(dev);
225
226 kfree(rfkill);
227 module_put(THIS_MODULE);
228}
229
230#ifdef CONFIG_PM
231static int rfkill_suspend(struct device *dev, pm_message_t state)
232{
233 struct rfkill *rfkill = to_rfkill(dev);
234
235 if (dev->power.power_state.event != state.event) {
236 if (state.event == PM_EVENT_SUSPEND) {
237 mutex_lock(&rfkill->mutex);
238
239 if (rfkill->state == RFKILL_STATE_ON)
240 rfkill->toggle_radio(rfkill->data,
241 RFKILL_STATE_OFF);
242
243 mutex_unlock(&rfkill->mutex);
244 }
245
246 dev->power.power_state = state;
247 }
248
249 return 0;
250}
251
252static int rfkill_resume(struct device *dev)
253{
254 struct rfkill *rfkill = to_rfkill(dev);
255
256 if (dev->power.power_state.event != PM_EVENT_ON) {
257 mutex_lock(&rfkill->mutex);
258
259 if (rfkill->state == RFKILL_STATE_ON)
260 rfkill->toggle_radio(rfkill->data, RFKILL_STATE_ON);
261
262 mutex_unlock(&rfkill->mutex);
263 }
264
265 dev->power.power_state = PMSG_ON;
266 return 0;
267}
268#else
269#define rfkill_suspend NULL
270#define rfkill_resume NULL
271#endif
272
273static struct class rfkill_class = {
274 .name = "rfkill",
275 .dev_release = rfkill_release,
276 .dev_attrs = rfkill_dev_attrs,
277 .suspend = rfkill_suspend,
278 .resume = rfkill_resume,
279};
280
281static int rfkill_add_switch(struct rfkill *rfkill)
282{
Michael Buesch7319f1e2007-10-28 15:16:50 +0100283 int error;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700284
Michael Buesch7319f1e2007-10-28 15:16:50 +0100285 mutex_lock(&rfkill_mutex);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700286
Michael Buesch7319f1e2007-10-28 15:16:50 +0100287 error = rfkill_toggle_radio(rfkill, rfkill_states[rfkill->type]);
288 if (!error)
289 list_add_tail(&rfkill->node, &rfkill_list);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700290
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700291 mutex_unlock(&rfkill_mutex);
Michael Buesch7319f1e2007-10-28 15:16:50 +0100292
293 return error;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700294}
295
296static void rfkill_remove_switch(struct rfkill *rfkill)
297{
298 mutex_lock(&rfkill_mutex);
299 list_del_init(&rfkill->node);
300 rfkill_toggle_radio(rfkill, RFKILL_STATE_OFF);
301 mutex_unlock(&rfkill_mutex);
302}
303
304/**
305 * rfkill_allocate - allocate memory for rfkill structure.
306 * @parent: device that has rf switch on it
Ivo van Doorn234a0ca2007-09-13 09:20:42 +0200307 * @type: type of the switch (RFKILL_TYPE_*)
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700308 *
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.
315 */
316struct rfkill *rfkill_allocate(struct device *parent, enum rfkill_type type)
317{
318 struct rfkill *rfkill;
319 struct device *dev;
320
321 rfkill = kzalloc(sizeof(struct rfkill), GFP_KERNEL);
Ivo van Doornd007da12007-05-19 12:24:39 -0700322 if (!rfkill)
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700323 return NULL;
324
325 mutex_init(&rfkill->mutex);
326 INIT_LIST_HEAD(&rfkill->node);
327 rfkill->type = type;
328
329 dev = &rfkill->dev;
330 dev->class = &rfkill_class;
331 dev->parent = parent;
332 device_initialize(dev);
333
334 __module_get(THIS_MODULE);
335
336 return rfkill;
337}
338EXPORT_SYMBOL(rfkill_allocate);
339
340/**
341 * rfkill_free - Mark rfkill structure for deletion
342 * @rfkill: rfkill structure to be destroyed
343 *
344 * Decrements reference count of rfkill structure so it is destoryed.
345 * Note that rfkill_free() should _not_ be called after rfkill_unregister().
346 */
347void rfkill_free(struct rfkill *rfkill)
348{
349 if (rfkill)
350 put_device(&rfkill->dev);
351}
352EXPORT_SYMBOL(rfkill_free);
353
Michael Buesch135900c2007-09-27 21:33:12 +0200354static void rfkill_led_trigger_register(struct rfkill *rfkill)
355{
356#ifdef CONFIG_RFKILL_LEDS
357 int error;
358
359 rfkill->led_trigger.name = rfkill->dev.bus_id;
360 error = led_trigger_register(&rfkill->led_trigger);
361 if (error)
362 rfkill->led_trigger.name = NULL;
363#endif /* CONFIG_RFKILL_LEDS */
364}
365
366static void rfkill_led_trigger_unregister(struct rfkill *rfkill)
367{
368#ifdef CONFIG_RFKILL_LEDS
369 if (rfkill->led_trigger.name)
370 led_trigger_unregister(&rfkill->led_trigger);
371#endif
372}
373
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700374/**
375 * rfkill_register - Register a rfkill structure.
376 * @rfkill: rfkill structure to be registered
377 *
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.
381 */
382int rfkill_register(struct rfkill *rfkill)
383{
384 static atomic_t rfkill_no = ATOMIC_INIT(0);
385 struct device *dev = &rfkill->dev;
386 int error;
387
388 if (!rfkill->toggle_radio)
389 return -EINVAL;
Michael Buesch7319f1e2007-10-28 15:16:50 +0100390 if (rfkill->type >= RFKILL_TYPE_MAX)
391 return -EINVAL;
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700392
Michael Buesch8a8f1c02007-10-28 13:07:54 +0100393 snprintf(dev->bus_id, sizeof(dev->bus_id),
394 "rfkill%ld", (long)atomic_inc_return(&rfkill_no) - 1);
395
396 rfkill_led_trigger_register(rfkill);
397
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700398 error = rfkill_add_switch(rfkill);
399 if (error)
400 return error;
401
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700402 error = device_add(dev);
403 if (error) {
404 rfkill_remove_switch(rfkill);
405 return error;
406 }
407
408 return 0;
409}
410EXPORT_SYMBOL(rfkill_register);
411
412/**
413 * rfkill_unregister - Uegister a rfkill structure.
414 * @rfkill: rfkill structure to be unregistered
415 *
416 * This function should be called by the network driver during device
417 * teardown to destroy rfkill structure. Note that rfkill_free() should
418 * _not_ be called after rfkill_unregister().
419 */
420void rfkill_unregister(struct rfkill *rfkill)
421{
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700422 device_del(&rfkill->dev);
423 rfkill_remove_switch(rfkill);
Michael Buesch8a8f1c02007-10-28 13:07:54 +0100424 rfkill_led_trigger_unregister(rfkill);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700425 put_device(&rfkill->dev);
426}
427EXPORT_SYMBOL(rfkill_unregister);
428
429/*
430 * Rfkill module initialization/deinitialization.
431 */
432static int __init rfkill_init(void)
433{
434 int error;
435 int i;
436
437 for (i = 0; i < ARRAY_SIZE(rfkill_states); i++)
438 rfkill_states[i] = RFKILL_STATE_ON;
439
440 error = class_register(&rfkill_class);
441 if (error) {
442 printk(KERN_ERR "rfkill: unable to register rfkill class\n");
443 return error;
444 }
445
446 return 0;
447}
448
449static void __exit rfkill_exit(void)
450{
451 class_unregister(&rfkill_class);
452}
453
Michael Buesch2bf236d2007-10-28 14:39:02 +0100454subsys_initcall(rfkill_init);
Ivo van Doorncf4328c2007-05-07 00:34:20 -0700455module_exit(rfkill_exit);