]> err.no Git - linux-2.6/blob - net/rfkill/rfkill.c
rfkill: add read-write rfkill switch support
[linux-2.6] / net / rfkill / rfkill.c
1 /*
2  * Copyright (C) 2006 - 2007 Ivo van Doorn
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
30 /* Get declaration of rfkill_switch_all() to shut up sparse. */
31 #include "rfkill-input.h"
32
33
34 MODULE_AUTHOR("Ivo van Doorn <IvDoorn@gmail.com>");
35 MODULE_VERSION("1.0");
36 MODULE_DESCRIPTION("RF switch support");
37 MODULE_LICENSE("GPL");
38
39 static LIST_HEAD(rfkill_list);  /* list of registered rf switches */
40 static DEFINE_MUTEX(rfkill_mutex);
41
42 static unsigned int rfkill_default_state = RFKILL_STATE_ON;
43 module_param_named(default_state, rfkill_default_state, uint, 0444);
44 MODULE_PARM_DESC(default_state,
45                  "Default initial state for all radio types, 0 = radio off");
46
47 static enum rfkill_state rfkill_states[RFKILL_TYPE_MAX];
48
49
50 static 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
65 static 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
77 static int rfkill_toggle_radio(struct rfkill *rfkill,
78                                 enum rfkill_state state)
79 {
80         int retval = 0;
81         enum rfkill_state oldstate, newstate;
82
83         oldstate = rfkill->state;
84
85         if (rfkill->get_state &&
86             !rfkill->get_state(rfkill->data, &newstate))
87                 rfkill->state = newstate;
88
89         if (state != rfkill->state) {
90                 retval = rfkill->toggle_radio(rfkill->data, state);
91                 if (!retval)
92                         rfkill->state = state;
93         }
94
95         if (rfkill->state != oldstate)
96                 rfkill_led_trigger(rfkill, rfkill->state);
97
98         return retval;
99 }
100
101 /**
102  * rfkill_switch_all - Toggle state of all switches of given type
103  * @type: type of interfaces to be affeceted
104  * @state: the new state
105  *
106  * This function toggles state of all switches of given type unless
107  * a specific switch is claimed by userspace in which case it is
108  * left alone.
109  */
110
111 void 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) {
120                 if ((!rfkill->user_claim) && (rfkill->type == type))
121                         rfkill_toggle_radio(rfkill, state);
122         }
123
124         mutex_unlock(&rfkill_mutex);
125 }
126 EXPORT_SYMBOL(rfkill_switch_all);
127
128 /**
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  */
140 int 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 }
152 EXPORT_SYMBOL(rfkill_force_state);
153
154 static 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
163 static 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;
177         case RFKILL_TYPE_UWB:
178                 type = "ultrawideband";
179                 break;
180         case RFKILL_TYPE_WIMAX:
181                 type = "wimax";
182                 break;
183         default:
184                 BUG();
185         }
186
187         return sprintf(buf, "%s\n", type);
188 }
189
190 static ssize_t rfkill_state_show(struct device *dev,
191                                  struct device_attribute *attr,
192                                  char *buf)
193 {
194         struct rfkill *rfkill = to_rfkill(dev);
195
196         update_rfkill_state(rfkill);
197         return sprintf(buf, "%d\n", rfkill->state);
198 }
199
200 static ssize_t rfkill_state_store(struct device *dev,
201                                   struct device_attribute *attr,
202                                   const char *buf, size_t count)
203 {
204         struct rfkill *rfkill = to_rfkill(dev);
205         unsigned int state = simple_strtoul(buf, NULL, 0);
206         int error;
207
208         if (!capable(CAP_NET_ADMIN))
209                 return -EPERM;
210
211         if (mutex_lock_interruptible(&rfkill->mutex))
212                 return -ERESTARTSYS;
213         error = rfkill_toggle_radio(rfkill,
214                         state ? RFKILL_STATE_ON : RFKILL_STATE_OFF);
215         mutex_unlock(&rfkill->mutex);
216
217         return error ? error : count;
218 }
219
220 static ssize_t rfkill_claim_show(struct device *dev,
221                                  struct device_attribute *attr,
222                                  char *buf)
223 {
224         struct rfkill *rfkill = to_rfkill(dev);
225
226         return sprintf(buf, "%d", rfkill->user_claim);
227 }
228
229 static ssize_t rfkill_claim_store(struct device *dev,
230                                   struct device_attribute *attr,
231                                   const char *buf, size_t count)
232 {
233         struct rfkill *rfkill = to_rfkill(dev);
234         bool claim = !!simple_strtoul(buf, NULL, 0);
235         int error;
236
237         if (!capable(CAP_NET_ADMIN))
238                 return -EPERM;
239
240         /*
241          * Take the global lock to make sure the kernel is not in
242          * the middle of rfkill_switch_all
243          */
244         error = mutex_lock_interruptible(&rfkill_mutex);
245         if (error)
246                 return error;
247
248         if (rfkill->user_claim_unsupported) {
249                 error = -EOPNOTSUPP;
250                 goto out_unlock;
251         }
252         if (rfkill->user_claim != claim) {
253                 if (!claim)
254                         rfkill_toggle_radio(rfkill,
255                                             rfkill_states[rfkill->type]);
256                 rfkill->user_claim = claim;
257         }
258
259 out_unlock:
260         mutex_unlock(&rfkill_mutex);
261
262         return error ? error : count;
263 }
264
265 static struct device_attribute rfkill_dev_attrs[] = {
266         __ATTR(name, S_IRUGO, rfkill_name_show, NULL),
267         __ATTR(type, S_IRUGO, rfkill_type_show, NULL),
268         __ATTR(state, S_IRUGO|S_IWUSR, rfkill_state_show, rfkill_state_store),
269         __ATTR(claim, S_IRUGO|S_IWUSR, rfkill_claim_show, rfkill_claim_store),
270         __ATTR_NULL
271 };
272
273 static void rfkill_release(struct device *dev)
274 {
275         struct rfkill *rfkill = to_rfkill(dev);
276
277         kfree(rfkill);
278         module_put(THIS_MODULE);
279 }
280
281 #ifdef CONFIG_PM
282 static int rfkill_suspend(struct device *dev, pm_message_t state)
283 {
284         struct rfkill *rfkill = to_rfkill(dev);
285
286         if (dev->power.power_state.event != state.event) {
287                 if (state.event & PM_EVENT_SLEEP) {
288                         mutex_lock(&rfkill->mutex);
289
290                         if (rfkill->state == RFKILL_STATE_ON)
291                                 rfkill->toggle_radio(rfkill->data,
292                                                      RFKILL_STATE_OFF);
293
294                         mutex_unlock(&rfkill->mutex);
295                 }
296
297                 dev->power.power_state = state;
298         }
299
300         return 0;
301 }
302
303 static int rfkill_resume(struct device *dev)
304 {
305         struct rfkill *rfkill = to_rfkill(dev);
306
307         if (dev->power.power_state.event != PM_EVENT_ON) {
308                 mutex_lock(&rfkill->mutex);
309
310                 if (rfkill->state == RFKILL_STATE_ON)
311                         rfkill->toggle_radio(rfkill->data, RFKILL_STATE_ON);
312
313                 mutex_unlock(&rfkill->mutex);
314         }
315
316         dev->power.power_state = PMSG_ON;
317         return 0;
318 }
319 #else
320 #define rfkill_suspend NULL
321 #define rfkill_resume NULL
322 #endif
323
324 static struct class rfkill_class = {
325         .name           = "rfkill",
326         .dev_release    = rfkill_release,
327         .dev_attrs      = rfkill_dev_attrs,
328         .suspend        = rfkill_suspend,
329         .resume         = rfkill_resume,
330 };
331
332 static int rfkill_add_switch(struct rfkill *rfkill)
333 {
334         int error;
335
336         mutex_lock(&rfkill_mutex);
337
338         error = rfkill_toggle_radio(rfkill, rfkill_states[rfkill->type]);
339         if (!error)
340                 list_add_tail(&rfkill->node, &rfkill_list);
341
342         mutex_unlock(&rfkill_mutex);
343
344         return error;
345 }
346
347 static void rfkill_remove_switch(struct rfkill *rfkill)
348 {
349         mutex_lock(&rfkill_mutex);
350         list_del_init(&rfkill->node);
351         rfkill_toggle_radio(rfkill, RFKILL_STATE_OFF);
352         mutex_unlock(&rfkill_mutex);
353 }
354
355 /**
356  * rfkill_allocate - allocate memory for rfkill structure.
357  * @parent: device that has rf switch on it
358  * @type: type of the switch (RFKILL_TYPE_*)
359  *
360  * This function should be called by the network driver when it needs
361  * rfkill structure. Once the structure is allocated the driver shoud
362  * finish its initialization by setting name, private data, enable_radio
363  * and disable_radio methods and then register it with rfkill_register().
364  * NOTE: If registration fails the structure shoudl be freed by calling
365  * rfkill_free() otherwise rfkill_unregister() should be used.
366  */
367 struct rfkill *rfkill_allocate(struct device *parent, enum rfkill_type type)
368 {
369         struct rfkill *rfkill;
370         struct device *dev;
371
372         rfkill = kzalloc(sizeof(struct rfkill), GFP_KERNEL);
373         if (!rfkill)
374                 return NULL;
375
376         mutex_init(&rfkill->mutex);
377         INIT_LIST_HEAD(&rfkill->node);
378         rfkill->type = type;
379
380         dev = &rfkill->dev;
381         dev->class = &rfkill_class;
382         dev->parent = parent;
383         device_initialize(dev);
384
385         __module_get(THIS_MODULE);
386
387         return rfkill;
388 }
389 EXPORT_SYMBOL(rfkill_allocate);
390
391 /**
392  * rfkill_free - Mark rfkill structure for deletion
393  * @rfkill: rfkill structure to be destroyed
394  *
395  * Decrements reference count of rfkill structure so it is destroyed.
396  * Note that rfkill_free() should _not_ be called after rfkill_unregister().
397  */
398 void rfkill_free(struct rfkill *rfkill)
399 {
400         if (rfkill)
401                 put_device(&rfkill->dev);
402 }
403 EXPORT_SYMBOL(rfkill_free);
404
405 static void rfkill_led_trigger_register(struct rfkill *rfkill)
406 {
407 #ifdef CONFIG_RFKILL_LEDS
408         int error;
409
410         rfkill->led_trigger.name = rfkill->dev.bus_id;
411         error = led_trigger_register(&rfkill->led_trigger);
412         if (error)
413                 rfkill->led_trigger.name = NULL;
414 #endif /* CONFIG_RFKILL_LEDS */
415 }
416
417 static void rfkill_led_trigger_unregister(struct rfkill *rfkill)
418 {
419 #ifdef CONFIG_RFKILL_LEDS
420         if (rfkill->led_trigger.name)
421                 led_trigger_unregister(&rfkill->led_trigger);
422 #endif
423 }
424
425 /**
426  * rfkill_register - Register a rfkill structure.
427  * @rfkill: rfkill structure to be registered
428  *
429  * This function should be called by the network driver when the rfkill
430  * structure needs to be registered. Immediately from registration the
431  * switch driver should be able to service calls to toggle_radio.
432  */
433 int rfkill_register(struct rfkill *rfkill)
434 {
435         static atomic_t rfkill_no = ATOMIC_INIT(0);
436         struct device *dev = &rfkill->dev;
437         int error;
438
439         if (!rfkill->toggle_radio)
440                 return -EINVAL;
441         if (rfkill->type >= RFKILL_TYPE_MAX)
442                 return -EINVAL;
443
444         snprintf(dev->bus_id, sizeof(dev->bus_id),
445                  "rfkill%ld", (long)atomic_inc_return(&rfkill_no) - 1);
446
447         rfkill_led_trigger_register(rfkill);
448
449         error = rfkill_add_switch(rfkill);
450         if (error) {
451                 rfkill_led_trigger_unregister(rfkill);
452                 return error;
453         }
454
455         error = device_add(dev);
456         if (error) {
457                 rfkill_led_trigger_unregister(rfkill);
458                 rfkill_remove_switch(rfkill);
459                 return error;
460         }
461
462         return 0;
463 }
464 EXPORT_SYMBOL(rfkill_register);
465
466 /**
467  * rfkill_unregister - Unregister a rfkill structure.
468  * @rfkill: rfkill structure to be unregistered
469  *
470  * This function should be called by the network driver during device
471  * teardown to destroy rfkill structure. Note that rfkill_free() should
472  * _not_ be called after rfkill_unregister().
473  */
474 void rfkill_unregister(struct rfkill *rfkill)
475 {
476         device_del(&rfkill->dev);
477         rfkill_remove_switch(rfkill);
478         rfkill_led_trigger_unregister(rfkill);
479         put_device(&rfkill->dev);
480 }
481 EXPORT_SYMBOL(rfkill_unregister);
482
483 /*
484  * Rfkill module initialization/deinitialization.
485  */
486 static int __init rfkill_init(void)
487 {
488         int error;
489         int i;
490
491         if (rfkill_default_state != RFKILL_STATE_OFF &&
492             rfkill_default_state != RFKILL_STATE_ON)
493                 return -EINVAL;
494
495         for (i = 0; i < ARRAY_SIZE(rfkill_states); i++)
496                 rfkill_states[i] = rfkill_default_state;
497
498         error = class_register(&rfkill_class);
499         if (error) {
500                 printk(KERN_ERR "rfkill: unable to register rfkill class\n");
501                 return error;
502         }
503
504         return 0;
505 }
506
507 static void __exit rfkill_exit(void)
508 {
509         class_unregister(&rfkill_class);
510 }
511
512 subsys_initcall(rfkill_init);
513 module_exit(rfkill_exit);