]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/rfkill/rfkill.c
typo fix in net/rfkill/rfkill.c
[mirror_ubuntu-artful-kernel.git] / net / rfkill / rfkill.c
CommitLineData
cf4328cd 1/*
fe242cfd 2 * Copyright (C) 2006 - 2007 Ivo van Doorn
cf4328cd
ID
3 * 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
27366223
MB
30/* Get declaration of rfkill_switch_all() to shut up sparse. */
31#include "rfkill-input.h"
32
33
cf4328cd
ID
34MODULE_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
135900c1
MB
44
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
cf4328cd
ID
60static int rfkill_toggle_radio(struct rfkill *rfkill,
61 enum rfkill_state state)
62{
7f4c5341 63 int retval = 0;
cf4328cd
ID
64
65 if (state != rfkill->state) {
66 retval = rfkill->toggle_radio(rfkill->data, state);
135900c1 67 if (!retval) {
cf4328cd 68 rfkill->state = state;
135900c1
MB
69 rfkill_led_trigger(rfkill, state);
70 }
cf4328cd
ID
71 }
72
cf4328cd
ID
73 return retval;
74}
75
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
80 *
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.
84 */
85
86void rfkill_switch_all(enum rfkill_type type, enum rfkill_state state)
87{
88 struct rfkill *rfkill;
89
90 mutex_lock(&rfkill_mutex);
91
92 rfkill_states[type] = state;
93
94 list_for_each_entry(rfkill, &rfkill_list, node) {
95 if (!rfkill->user_claim)
96 rfkill_toggle_radio(rfkill, state);
97 }
98
99 mutex_unlock(&rfkill_mutex);
100}
101EXPORT_SYMBOL(rfkill_switch_all);
102
103static ssize_t rfkill_name_show(struct device *dev,
104 struct device_attribute *attr,
105 char *buf)
106{
107 struct rfkill *rfkill = to_rfkill(dev);
108
109 return sprintf(buf, "%s\n", rfkill->name);
110}
111
112static ssize_t rfkill_type_show(struct device *dev,
113 struct device_attribute *attr,
114 char *buf)
115{
116 struct rfkill *rfkill = to_rfkill(dev);
117 const char *type;
118
119 switch (rfkill->type) {
120 case RFKILL_TYPE_WLAN:
121 type = "wlan";
122 break;
123 case RFKILL_TYPE_BLUETOOTH:
124 type = "bluetooth";
125 break;
e0665486
ID
126 case RFKILL_TYPE_UWB:
127 type = "ultrawideband";
128 break;
303d9bf6
IPG
129 case RFKILL_TYPE_WIMAX:
130 type = "wimax";
131 break;
cf4328cd
ID
132 default:
133 BUG();
134 }
135
136 return sprintf(buf, "%s\n", type);
137}
138
139static ssize_t rfkill_state_show(struct device *dev,
140 struct device_attribute *attr,
141 char *buf)
142{
143 struct rfkill *rfkill = to_rfkill(dev);
144
145 return sprintf(buf, "%d\n", rfkill->state);
146}
147
148static ssize_t rfkill_state_store(struct device *dev,
149 struct device_attribute *attr,
150 const char *buf, size_t count)
151{
152 struct rfkill *rfkill = to_rfkill(dev);
153 unsigned int state = simple_strtoul(buf, NULL, 0);
154 int error;
155
156 if (!capable(CAP_NET_ADMIN))
157 return -EPERM;
158
7f4c5341
MB
159 if (mutex_lock_interruptible(&rfkill->mutex))
160 return -ERESTARTSYS;
cf4328cd
ID
161 error = rfkill_toggle_radio(rfkill,
162 state ? RFKILL_STATE_ON : RFKILL_STATE_OFF);
7f4c5341 163 mutex_unlock(&rfkill->mutex);
cf4328cd 164
7f4c5341 165 return error ? error : count;
cf4328cd
ID
166}
167
168static ssize_t rfkill_claim_show(struct device *dev,
169 struct device_attribute *attr,
170 char *buf)
171{
172 struct rfkill *rfkill = to_rfkill(dev);
173
174 return sprintf(buf, "%d", rfkill->user_claim);
175}
176
177static ssize_t rfkill_claim_store(struct device *dev,
178 struct device_attribute *attr,
179 const char *buf, size_t count)
180{
181 struct rfkill *rfkill = to_rfkill(dev);
182 bool claim = !!simple_strtoul(buf, NULL, 0);
183 int error;
184
185 if (!capable(CAP_NET_ADMIN))
186 return -EPERM;
187
188 /*
189 * Take the global lock to make sure the kernel is not in
190 * the middle of rfkill_switch_all
191 */
192 error = mutex_lock_interruptible(&rfkill_mutex);
193 if (error)
194 return error;
195
20405c08
MB
196 if (rfkill->user_claim_unsupported) {
197 error = -EOPNOTSUPP;
198 goto out_unlock;
199 }
cf4328cd
ID
200 if (rfkill->user_claim != claim) {
201 if (!claim)
202 rfkill_toggle_radio(rfkill,
203 rfkill_states[rfkill->type]);
204 rfkill->user_claim = claim;
205 }
206
20405c08 207out_unlock:
cf4328cd
ID
208 mutex_unlock(&rfkill_mutex);
209
20405c08 210 return error ? error : count;
cf4328cd
ID
211}
212
213static struct device_attribute rfkill_dev_attrs[] = {
214 __ATTR(name, S_IRUGO, rfkill_name_show, NULL),
215 __ATTR(type, S_IRUGO, rfkill_type_show, NULL),
c81de6ad 216 __ATTR(state, S_IRUGO|S_IWUSR, rfkill_state_show, rfkill_state_store),
cf4328cd
ID
217 __ATTR(claim, S_IRUGO|S_IWUSR, rfkill_claim_show, rfkill_claim_store),
218 __ATTR_NULL
219};
220
221static void rfkill_release(struct device *dev)
222{
223 struct rfkill *rfkill = to_rfkill(dev);
224
225 kfree(rfkill);
226 module_put(THIS_MODULE);
227}
228
229#ifdef CONFIG_PM
230static int rfkill_suspend(struct device *dev, pm_message_t state)
231{
232 struct rfkill *rfkill = to_rfkill(dev);
233
234 if (dev->power.power_state.event != state.event) {
235 if (state.event == PM_EVENT_SUSPEND) {
236 mutex_lock(&rfkill->mutex);
237
238 if (rfkill->state == RFKILL_STATE_ON)
239 rfkill->toggle_radio(rfkill->data,
240 RFKILL_STATE_OFF);
241
242 mutex_unlock(&rfkill->mutex);
243 }
244
245 dev->power.power_state = state;
246 }
247
248 return 0;
249}
250
251static int rfkill_resume(struct device *dev)
252{
253 struct rfkill *rfkill = to_rfkill(dev);
254
255 if (dev->power.power_state.event != PM_EVENT_ON) {
256 mutex_lock(&rfkill->mutex);
257
258 if (rfkill->state == RFKILL_STATE_ON)
259 rfkill->toggle_radio(rfkill->data, RFKILL_STATE_ON);
260
261 mutex_unlock(&rfkill->mutex);
262 }
263
264 dev->power.power_state = PMSG_ON;
265 return 0;
266}
267#else
268#define rfkill_suspend NULL
269#define rfkill_resume NULL
270#endif
271
272static struct class rfkill_class = {
273 .name = "rfkill",
274 .dev_release = rfkill_release,
275 .dev_attrs = rfkill_dev_attrs,
276 .suspend = rfkill_suspend,
277 .resume = rfkill_resume,
278};
279
280static int rfkill_add_switch(struct rfkill *rfkill)
281{
7319f1e6 282 int error;
cf4328cd 283
7319f1e6 284 mutex_lock(&rfkill_mutex);
cf4328cd 285
7319f1e6
MB
286 error = rfkill_toggle_radio(rfkill, rfkill_states[rfkill->type]);
287 if (!error)
288 list_add_tail(&rfkill->node, &rfkill_list);
cf4328cd 289
cf4328cd 290 mutex_unlock(&rfkill_mutex);
7319f1e6
MB
291
292 return error;
cf4328cd
ID
293}
294
295static void rfkill_remove_switch(struct rfkill *rfkill)
296{
297 mutex_lock(&rfkill_mutex);
298 list_del_init(&rfkill->node);
299 rfkill_toggle_radio(rfkill, RFKILL_STATE_OFF);
300 mutex_unlock(&rfkill_mutex);
301}
302
303/**
304 * rfkill_allocate - allocate memory for rfkill structure.
305 * @parent: device that has rf switch on it
234a0ca6 306 * @type: type of the switch (RFKILL_TYPE_*)
cf4328cd
ID
307 *
308 * This function should be called by the network driver when it needs
309 * rfkill structure. Once the structure is allocated the driver shoud
310 * finish its initialization by setting name, private data, enable_radio
311 * and disable_radio methods and then register it with rfkill_register().
312 * NOTE: If registration fails the structure shoudl be freed by calling
313 * rfkill_free() otherwise rfkill_unregister() should be used.
314 */
315struct rfkill *rfkill_allocate(struct device *parent, enum rfkill_type type)
316{
317 struct rfkill *rfkill;
318 struct device *dev;
319
320 rfkill = kzalloc(sizeof(struct rfkill), GFP_KERNEL);
d007da1f 321 if (!rfkill)
cf4328cd
ID
322 return NULL;
323
324 mutex_init(&rfkill->mutex);
325 INIT_LIST_HEAD(&rfkill->node);
326 rfkill->type = type;
327
328 dev = &rfkill->dev;
329 dev->class = &rfkill_class;
330 dev->parent = parent;
331 device_initialize(dev);
332
333 __module_get(THIS_MODULE);
334
335 return rfkill;
336}
337EXPORT_SYMBOL(rfkill_allocate);
338
339/**
340 * rfkill_free - Mark rfkill structure for deletion
341 * @rfkill: rfkill structure to be destroyed
342 *
1dbede87 343 * Decrements reference count of rfkill structure so it is destroyed.
cf4328cd
ID
344 * Note that rfkill_free() should _not_ be called after rfkill_unregister().
345 */
346void rfkill_free(struct rfkill *rfkill)
347{
348 if (rfkill)
349 put_device(&rfkill->dev);
350}
351EXPORT_SYMBOL(rfkill_free);
352
135900c1
MB
353static void rfkill_led_trigger_register(struct rfkill *rfkill)
354{
355#ifdef CONFIG_RFKILL_LEDS
356 int error;
357
358 rfkill->led_trigger.name = rfkill->dev.bus_id;
359 error = led_trigger_register(&rfkill->led_trigger);
360 if (error)
361 rfkill->led_trigger.name = NULL;
362#endif /* CONFIG_RFKILL_LEDS */
363}
364
365static void rfkill_led_trigger_unregister(struct rfkill *rfkill)
366{
367#ifdef CONFIG_RFKILL_LEDS
368 if (rfkill->led_trigger.name)
369 led_trigger_unregister(&rfkill->led_trigger);
370#endif
371}
372
cf4328cd
ID
373/**
374 * rfkill_register - Register a rfkill structure.
375 * @rfkill: rfkill structure to be registered
376 *
377 * This function should be called by the network driver when the rfkill
378 * structure needs to be registered. Immediately from registration the
379 * switch driver should be able to service calls to toggle_radio.
380 */
381int rfkill_register(struct rfkill *rfkill)
382{
383 static atomic_t rfkill_no = ATOMIC_INIT(0);
384 struct device *dev = &rfkill->dev;
385 int error;
386
387 if (!rfkill->toggle_radio)
388 return -EINVAL;
7319f1e6
MB
389 if (rfkill->type >= RFKILL_TYPE_MAX)
390 return -EINVAL;
cf4328cd 391
8a8f1c04
MB
392 snprintf(dev->bus_id, sizeof(dev->bus_id),
393 "rfkill%ld", (long)atomic_inc_return(&rfkill_no) - 1);
394
395 rfkill_led_trigger_register(rfkill);
396
cf4328cd 397 error = rfkill_add_switch(rfkill);
632041f3
EP
398 if (error) {
399 rfkill_led_trigger_unregister(rfkill);
cf4328cd 400 return error;
632041f3 401 }
cf4328cd 402
cf4328cd
ID
403 error = device_add(dev);
404 if (error) {
632041f3 405 rfkill_led_trigger_unregister(rfkill);
cf4328cd
ID
406 rfkill_remove_switch(rfkill);
407 return error;
408 }
409
410 return 0;
411}
412EXPORT_SYMBOL(rfkill_register);
413
414/**
415 * rfkill_unregister - Uegister a rfkill structure.
416 * @rfkill: rfkill structure to be unregistered
417 *
418 * This function should be called by the network driver during device
419 * teardown to destroy rfkill structure. Note that rfkill_free() should
420 * _not_ be called after rfkill_unregister().
421 */
422void rfkill_unregister(struct rfkill *rfkill)
423{
424 device_del(&rfkill->dev);
425 rfkill_remove_switch(rfkill);
8a8f1c04 426 rfkill_led_trigger_unregister(rfkill);
cf4328cd
ID
427 put_device(&rfkill->dev);
428}
429EXPORT_SYMBOL(rfkill_unregister);
430
431/*
432 * Rfkill module initialization/deinitialization.
433 */
434static int __init rfkill_init(void)
435{
436 int error;
437 int i;
438
439 for (i = 0; i < ARRAY_SIZE(rfkill_states); i++)
440 rfkill_states[i] = RFKILL_STATE_ON;
441
442 error = class_register(&rfkill_class);
443 if (error) {
444 printk(KERN_ERR "rfkill: unable to register rfkill class\n");
445 return error;
446 }
447
448 return 0;
449}
450
451static void __exit rfkill_exit(void)
452{
453 class_unregister(&rfkill_class);
454}
455
2bf236d5 456subsys_initcall(rfkill_init);
cf4328cd 457module_exit(rfkill_exit);