rfkill: add type string helper
[firefly-linux-kernel-4.4.55.git] / 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 static BLOCKING_NOTIFIER_HEAD(rfkill_notifier_list);
50
51
52 /**
53  * register_rfkill_notifier - Add notifier to rfkill notifier chain
54  * @nb: pointer to the new entry to add to the chain
55  *
56  * See blocking_notifier_chain_register() for return value and further
57  * observations.
58  *
59  * Adds a notifier to the rfkill notifier chain.  The chain will be
60  * called with a pointer to the relevant rfkill structure as a parameter,
61  * refer to include/linux/rfkill.h for the possible events.
62  *
63  * Notifiers added to this chain are to always return NOTIFY_DONE.  This
64  * chain is a blocking notifier chain: notifiers can sleep.
65  *
66  * Calls to this chain may have been done through a workqueue.  One must
67  * assume unordered asynchronous behaviour, there is no way to know if
68  * actions related to the event that generated the notification have been
69  * carried out already.
70  */
71 int register_rfkill_notifier(struct notifier_block *nb)
72 {
73         return blocking_notifier_chain_register(&rfkill_notifier_list, nb);
74 }
75 EXPORT_SYMBOL_GPL(register_rfkill_notifier);
76
77 /**
78  * unregister_rfkill_notifier - remove notifier from rfkill notifier chain
79  * @nb: pointer to the entry to remove from the chain
80  *
81  * See blocking_notifier_chain_unregister() for return value and further
82  * observations.
83  *
84  * Removes a notifier from the rfkill notifier chain.
85  */
86 int unregister_rfkill_notifier(struct notifier_block *nb)
87 {
88         return blocking_notifier_chain_unregister(&rfkill_notifier_list, nb);
89 }
90 EXPORT_SYMBOL_GPL(unregister_rfkill_notifier);
91
92
93 static void rfkill_led_trigger(struct rfkill *rfkill,
94                                enum rfkill_state state)
95 {
96 #ifdef CONFIG_RFKILL_LEDS
97         struct led_trigger *led = &rfkill->led_trigger;
98
99         if (!led->name)
100                 return;
101         if (state == RFKILL_STATE_OFF)
102                 led_trigger_event(led, LED_OFF);
103         else
104                 led_trigger_event(led, LED_FULL);
105 #endif /* CONFIG_RFKILL_LEDS */
106 }
107
108 static void notify_rfkill_state_change(struct rfkill *rfkill)
109 {
110         blocking_notifier_call_chain(&rfkill_notifier_list,
111                         RFKILL_STATE_CHANGED,
112                         rfkill);
113 }
114
115 static void update_rfkill_state(struct rfkill *rfkill)
116 {
117         enum rfkill_state newstate, oldstate;
118
119         if (rfkill->get_state) {
120                 mutex_lock(&rfkill->mutex);
121                 if (!rfkill->get_state(rfkill->data, &newstate)) {
122                         oldstate = rfkill->state;
123                         rfkill->state = newstate;
124                         if (oldstate != newstate)
125                                 notify_rfkill_state_change(rfkill);
126                 }
127                 mutex_unlock(&rfkill->mutex);
128         }
129 }
130
131 static int rfkill_toggle_radio(struct rfkill *rfkill,
132                                 enum rfkill_state state,
133                                 int force)
134 {
135         int retval = 0;
136         enum rfkill_state oldstate, newstate;
137
138         oldstate = rfkill->state;
139
140         if (rfkill->get_state && !force &&
141             !rfkill->get_state(rfkill->data, &newstate))
142                 rfkill->state = newstate;
143
144         if (force || state != rfkill->state) {
145                 retval = rfkill->toggle_radio(rfkill->data, state);
146                 if (!retval)
147                         rfkill->state = state;
148         }
149
150         if (force || rfkill->state != oldstate) {
151                 rfkill_led_trigger(rfkill, rfkill->state);
152                 notify_rfkill_state_change(rfkill);
153         }
154
155         return retval;
156 }
157
158 /**
159  * rfkill_switch_all - Toggle state of all switches of given type
160  * @type: type of interfaces to be affeceted
161  * @state: the new state
162  *
163  * This function toggles state of all switches of given type unless
164  * a specific switch is claimed by userspace in which case it is
165  * left alone.
166  */
167 void rfkill_switch_all(enum rfkill_type type, enum rfkill_state state)
168 {
169         struct rfkill *rfkill;
170
171         mutex_lock(&rfkill_mutex);
172
173         rfkill_states[type] = state;
174
175         list_for_each_entry(rfkill, &rfkill_list, node) {
176                 if ((!rfkill->user_claim) && (rfkill->type == type))
177                         rfkill_toggle_radio(rfkill, state, 0);
178         }
179
180         mutex_unlock(&rfkill_mutex);
181 }
182 EXPORT_SYMBOL(rfkill_switch_all);
183
184 /**
185  * rfkill_force_state - Force the internal rfkill radio state
186  * @rfkill: pointer to the rfkill class to modify.
187  * @state: the current radio state the class should be forced to.
188  *
189  * This function updates the internal state of the radio cached
190  * by the rfkill class.  It should be used when the driver gets
191  * a notification by the firmware/hardware of the current *real*
192  * state of the radio rfkill switch.
193  *
194  * It may not be called from an atomic context.
195  */
196 int rfkill_force_state(struct rfkill *rfkill, enum rfkill_state state)
197 {
198         enum rfkill_state oldstate;
199
200         if (state != RFKILL_STATE_OFF &&
201             state != RFKILL_STATE_ON)
202                 return -EINVAL;
203
204         mutex_lock(&rfkill->mutex);
205
206         oldstate = rfkill->state;
207         rfkill->state = state;
208
209         if (state != oldstate)
210                 notify_rfkill_state_change(rfkill);
211
212         mutex_unlock(&rfkill->mutex);
213
214         return 0;
215 }
216 EXPORT_SYMBOL(rfkill_force_state);
217
218 static ssize_t rfkill_name_show(struct device *dev,
219                                 struct device_attribute *attr,
220                                 char *buf)
221 {
222         struct rfkill *rfkill = to_rfkill(dev);
223
224         return sprintf(buf, "%s\n", rfkill->name);
225 }
226
227 static const char *rfkill_get_type_str(enum rfkill_type type)
228 {
229         switch (type) {
230         case RFKILL_TYPE_WLAN:
231                 return "wlan";
232         case RFKILL_TYPE_BLUETOOTH:
233                 return "bluetooth";
234         case RFKILL_TYPE_UWB:
235                 return "ultrawideband";
236         case RFKILL_TYPE_WIMAX:
237                 return "wimax";
238         case RFKILL_TYPE_WWAN:
239                 return "wwan";
240         default:
241                 BUG();
242         }
243 }
244
245 static ssize_t rfkill_type_show(struct device *dev,
246                                 struct device_attribute *attr,
247                                 char *buf)
248 {
249         struct rfkill *rfkill = to_rfkill(dev);
250
251         return sprintf(buf, "%s\n", rfkill_get_type_str(rfkill->type));
252 }
253
254 static ssize_t rfkill_state_show(struct device *dev,
255                                  struct device_attribute *attr,
256                                  char *buf)
257 {
258         struct rfkill *rfkill = to_rfkill(dev);
259
260         update_rfkill_state(rfkill);
261         return sprintf(buf, "%d\n", rfkill->state);
262 }
263
264 static ssize_t rfkill_state_store(struct device *dev,
265                                   struct device_attribute *attr,
266                                   const char *buf, size_t count)
267 {
268         struct rfkill *rfkill = to_rfkill(dev);
269         unsigned int state = simple_strtoul(buf, NULL, 0);
270         int error;
271
272         if (!capable(CAP_NET_ADMIN))
273                 return -EPERM;
274
275         if (mutex_lock_interruptible(&rfkill->mutex))
276                 return -ERESTARTSYS;
277         error = rfkill_toggle_radio(rfkill,
278                         state ? RFKILL_STATE_ON : RFKILL_STATE_OFF,
279                         0);
280         mutex_unlock(&rfkill->mutex);
281
282         return error ? error : count;
283 }
284
285 static ssize_t rfkill_claim_show(struct device *dev,
286                                  struct device_attribute *attr,
287                                  char *buf)
288 {
289         struct rfkill *rfkill = to_rfkill(dev);
290
291         return sprintf(buf, "%d", rfkill->user_claim);
292 }
293
294 static ssize_t rfkill_claim_store(struct device *dev,
295                                   struct device_attribute *attr,
296                                   const char *buf, size_t count)
297 {
298         struct rfkill *rfkill = to_rfkill(dev);
299         bool claim = !!simple_strtoul(buf, NULL, 0);
300         int error;
301
302         if (!capable(CAP_NET_ADMIN))
303                 return -EPERM;
304
305         /*
306          * Take the global lock to make sure the kernel is not in
307          * the middle of rfkill_switch_all
308          */
309         error = mutex_lock_interruptible(&rfkill_mutex);
310         if (error)
311                 return error;
312
313         if (rfkill->user_claim_unsupported) {
314                 error = -EOPNOTSUPP;
315                 goto out_unlock;
316         }
317         if (rfkill->user_claim != claim) {
318                 if (!claim)
319                         rfkill_toggle_radio(rfkill,
320                                             rfkill_states[rfkill->type],
321                                             0);
322                 rfkill->user_claim = claim;
323         }
324
325 out_unlock:
326         mutex_unlock(&rfkill_mutex);
327
328         return error ? error : count;
329 }
330
331 static struct device_attribute rfkill_dev_attrs[] = {
332         __ATTR(name, S_IRUGO, rfkill_name_show, NULL),
333         __ATTR(type, S_IRUGO, rfkill_type_show, NULL),
334         __ATTR(state, S_IRUGO|S_IWUSR, rfkill_state_show, rfkill_state_store),
335         __ATTR(claim, S_IRUGO|S_IWUSR, rfkill_claim_show, rfkill_claim_store),
336         __ATTR_NULL
337 };
338
339 static void rfkill_release(struct device *dev)
340 {
341         struct rfkill *rfkill = to_rfkill(dev);
342
343         kfree(rfkill);
344         module_put(THIS_MODULE);
345 }
346
347 #ifdef CONFIG_PM
348 static int rfkill_suspend(struct device *dev, pm_message_t state)
349 {
350         struct rfkill *rfkill = to_rfkill(dev);
351
352         if (dev->power.power_state.event != state.event) {
353                 if (state.event & PM_EVENT_SLEEP) {
354                         /* Stop transmitter, keep state, no notifies */
355                         update_rfkill_state(rfkill);
356
357                         mutex_lock(&rfkill->mutex);
358                         rfkill->toggle_radio(rfkill->data, RFKILL_STATE_OFF);
359                         mutex_unlock(&rfkill->mutex);
360                 }
361
362                 dev->power.power_state = state;
363         }
364
365         return 0;
366 }
367
368 static int rfkill_resume(struct device *dev)
369 {
370         struct rfkill *rfkill = to_rfkill(dev);
371
372         if (dev->power.power_state.event != PM_EVENT_ON) {
373                 mutex_lock(&rfkill->mutex);
374
375                 /* restore radio state AND notify everybody */
376                 rfkill_toggle_radio(rfkill, rfkill->state, 1);
377
378                 mutex_unlock(&rfkill->mutex);
379         }
380
381         dev->power.power_state = PMSG_ON;
382         return 0;
383 }
384 #else
385 #define rfkill_suspend NULL
386 #define rfkill_resume NULL
387 #endif
388
389 static struct class rfkill_class = {
390         .name           = "rfkill",
391         .dev_release    = rfkill_release,
392         .dev_attrs      = rfkill_dev_attrs,
393         .suspend        = rfkill_suspend,
394         .resume         = rfkill_resume,
395 };
396
397 static int rfkill_add_switch(struct rfkill *rfkill)
398 {
399         int error;
400
401         mutex_lock(&rfkill_mutex);
402
403         error = rfkill_toggle_radio(rfkill, rfkill_states[rfkill->type], 0);
404         if (!error)
405                 list_add_tail(&rfkill->node, &rfkill_list);
406
407         mutex_unlock(&rfkill_mutex);
408
409         return error;
410 }
411
412 static void rfkill_remove_switch(struct rfkill *rfkill)
413 {
414         mutex_lock(&rfkill_mutex);
415         list_del_init(&rfkill->node);
416         rfkill_toggle_radio(rfkill, RFKILL_STATE_OFF, 1);
417         mutex_unlock(&rfkill_mutex);
418 }
419
420 /**
421  * rfkill_allocate - allocate memory for rfkill structure.
422  * @parent: device that has rf switch on it
423  * @type: type of the switch (RFKILL_TYPE_*)
424  *
425  * This function should be called by the network driver when it needs
426  * rfkill structure. Once the structure is allocated the driver shoud
427  * finish its initialization by setting name, private data, enable_radio
428  * and disable_radio methods and then register it with rfkill_register().
429  * NOTE: If registration fails the structure shoudl be freed by calling
430  * rfkill_free() otherwise rfkill_unregister() should be used.
431  */
432 struct rfkill *rfkill_allocate(struct device *parent, enum rfkill_type type)
433 {
434         struct rfkill *rfkill;
435         struct device *dev;
436
437         rfkill = kzalloc(sizeof(struct rfkill), GFP_KERNEL);
438         if (!rfkill)
439                 return NULL;
440
441         mutex_init(&rfkill->mutex);
442         INIT_LIST_HEAD(&rfkill->node);
443         rfkill->type = type;
444
445         dev = &rfkill->dev;
446         dev->class = &rfkill_class;
447         dev->parent = parent;
448         device_initialize(dev);
449
450         __module_get(THIS_MODULE);
451
452         return rfkill;
453 }
454 EXPORT_SYMBOL(rfkill_allocate);
455
456 /**
457  * rfkill_free - Mark rfkill structure for deletion
458  * @rfkill: rfkill structure to be destroyed
459  *
460  * Decrements reference count of rfkill structure so it is destroyed.
461  * Note that rfkill_free() should _not_ be called after rfkill_unregister().
462  */
463 void rfkill_free(struct rfkill *rfkill)
464 {
465         if (rfkill)
466                 put_device(&rfkill->dev);
467 }
468 EXPORT_SYMBOL(rfkill_free);
469
470 static void rfkill_led_trigger_register(struct rfkill *rfkill)
471 {
472 #ifdef CONFIG_RFKILL_LEDS
473         int error;
474
475         rfkill->led_trigger.name = rfkill->dev.bus_id;
476         error = led_trigger_register(&rfkill->led_trigger);
477         if (error)
478                 rfkill->led_trigger.name = NULL;
479 #endif /* CONFIG_RFKILL_LEDS */
480 }
481
482 static void rfkill_led_trigger_unregister(struct rfkill *rfkill)
483 {
484 #ifdef CONFIG_RFKILL_LEDS
485         if (rfkill->led_trigger.name)
486                 led_trigger_unregister(&rfkill->led_trigger);
487 #endif
488 }
489
490 /**
491  * rfkill_register - Register a rfkill structure.
492  * @rfkill: rfkill structure to be registered
493  *
494  * This function should be called by the network driver when the rfkill
495  * structure needs to be registered. Immediately from registration the
496  * switch driver should be able to service calls to toggle_radio.
497  */
498 int rfkill_register(struct rfkill *rfkill)
499 {
500         static atomic_t rfkill_no = ATOMIC_INIT(0);
501         struct device *dev = &rfkill->dev;
502         int error;
503
504         if (!rfkill->toggle_radio)
505                 return -EINVAL;
506         if (rfkill->type >= RFKILL_TYPE_MAX)
507                 return -EINVAL;
508
509         snprintf(dev->bus_id, sizeof(dev->bus_id),
510                  "rfkill%ld", (long)atomic_inc_return(&rfkill_no) - 1);
511
512         rfkill_led_trigger_register(rfkill);
513
514         error = rfkill_add_switch(rfkill);
515         if (error) {
516                 rfkill_led_trigger_unregister(rfkill);
517                 return error;
518         }
519
520         error = device_add(dev);
521         if (error) {
522                 rfkill_led_trigger_unregister(rfkill);
523                 rfkill_remove_switch(rfkill);
524                 return error;
525         }
526
527         return 0;
528 }
529 EXPORT_SYMBOL(rfkill_register);
530
531 /**
532  * rfkill_unregister - Unregister a rfkill structure.
533  * @rfkill: rfkill structure to be unregistered
534  *
535  * This function should be called by the network driver during device
536  * teardown to destroy rfkill structure. Note that rfkill_free() should
537  * _not_ be called after rfkill_unregister().
538  */
539 void rfkill_unregister(struct rfkill *rfkill)
540 {
541         device_del(&rfkill->dev);
542         rfkill_remove_switch(rfkill);
543         rfkill_led_trigger_unregister(rfkill);
544         put_device(&rfkill->dev);
545 }
546 EXPORT_SYMBOL(rfkill_unregister);
547
548 /*
549  * Rfkill module initialization/deinitialization.
550  */
551 static int __init rfkill_init(void)
552 {
553         int error;
554         int i;
555
556         if (rfkill_default_state != RFKILL_STATE_OFF &&
557             rfkill_default_state != RFKILL_STATE_ON)
558                 return -EINVAL;
559
560         for (i = 0; i < ARRAY_SIZE(rfkill_states); i++)
561                 rfkill_states[i] = rfkill_default_state;
562
563         error = class_register(&rfkill_class);
564         if (error) {
565                 printk(KERN_ERR "rfkill: unable to register rfkill class\n");
566                 return error;
567         }
568
569         return 0;
570 }
571
572 static void __exit rfkill_exit(void)
573 {
574         class_unregister(&rfkill_class);
575 }
576
577 subsys_initcall(rfkill_init);
578 module_exit(rfkill_exit);