nl80211: use GFP_ATOMIC for michael mic failure message
[deliverable/linux.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 */
15635744 40static DEFINE_MUTEX(rfkill_global_mutex);
cf4328cd 41
5005657c 42static unsigned int rfkill_default_state = RFKILL_STATE_UNBLOCKED;
e954b0b8
HMH
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
99619201
HMH
47struct rfkill_gsw_state {
48 enum rfkill_state current_state;
49 enum rfkill_state default_state;
50};
51
52static struct rfkill_gsw_state rfkill_global_states[RFKILL_TYPE_MAX];
53static unsigned long rfkill_states_lockdflt[BITS_TO_LONGS(RFKILL_TYPE_MAX)];
d003922d 54static bool rfkill_epo_lock_active;
cf4328cd 55
135900c1 56
f32f8b72 57#ifdef CONFIG_RFKILL_LEDS
135900c1
MB
58static void rfkill_led_trigger(struct rfkill *rfkill,
59 enum rfkill_state state)
60{
135900c1
MB
61 struct led_trigger *led = &rfkill->led_trigger;
62
63 if (!led->name)
64 return;
5005657c 65 if (state != RFKILL_STATE_UNBLOCKED)
135900c1
MB
66 led_trigger_event(led, LED_OFF);
67 else
68 led_trigger_event(led, LED_FULL);
135900c1
MB
69}
70
96185664
DB
71static void rfkill_led_trigger_activate(struct led_classdev *led)
72{
73 struct rfkill *rfkill = container_of(led->trigger,
74 struct rfkill, led_trigger);
75
76 rfkill_led_trigger(rfkill, rfkill->state);
77}
aeca78b9
AM
78#else
79static inline void rfkill_led_trigger(struct rfkill *rfkill,
80 enum rfkill_state state)
81{
82}
96185664
DB
83#endif /* CONFIG_RFKILL_LEDS */
84
4dec9b80 85static void rfkill_uevent(struct rfkill *rfkill)
79399a8d 86{
4dec9b80 87 kobject_uevent(&rfkill->dev.kobj, KOBJ_CHANGE);
79399a8d
HMH
88}
89
801e49af
HMH
90static void update_rfkill_state(struct rfkill *rfkill)
91{
79399a8d 92 enum rfkill_state newstate, oldstate;
801e49af
HMH
93
94 if (rfkill->get_state) {
95 mutex_lock(&rfkill->mutex);
79399a8d
HMH
96 if (!rfkill->get_state(rfkill->data, &newstate)) {
97 oldstate = rfkill->state;
801e49af 98 rfkill->state = newstate;
79399a8d 99 if (oldstate != newstate)
4dec9b80 100 rfkill_uevent(rfkill);
79399a8d 101 }
801e49af
HMH
102 mutex_unlock(&rfkill->mutex);
103 }
492301fb 104 rfkill_led_trigger(rfkill, rfkill->state);
801e49af
HMH
105}
106
5005657c
HMH
107/**
108 * rfkill_toggle_radio - wrapper for toggle_radio hook
5005657c
HMH
109 * @rfkill: the rfkill struct to use
110 * @force: calls toggle_radio even if cache says it is not needed,
111 * and also makes sure notifications of the state will be
112 * sent even if it didn't change
113 * @state: the new state to call toggle_radio() with
114 *
0f687e9a
HMH
115 * Calls rfkill->toggle_radio, enforcing the API for toggle_radio
116 * calls and handling all the red tape such as issuing notifications
117 * if the call is successful.
118 *
e10e0dfe
HMH
119 * Suspended devices are not touched at all, and -EAGAIN is returned.
120 *
435307a3
HMH
121 * Note that the @force parameter cannot override a (possibly cached)
122 * state of RFKILL_STATE_HARD_BLOCKED. Any device making use of
5005657c
HMH
123 * RFKILL_STATE_HARD_BLOCKED implements either get_state() or
124 * rfkill_force_state(), so the cache either is bypassed or valid.
125 *
126 * Note that we do call toggle_radio for RFKILL_STATE_SOFT_BLOCKED
127 * even if the radio is in RFKILL_STATE_HARD_BLOCKED state, so as to
128 * give the driver a hint that it should double-BLOCK the transmitter.
129 *
064af111 130 * Caller must have acquired rfkill->mutex.
5005657c 131 */
cf4328cd 132static int rfkill_toggle_radio(struct rfkill *rfkill,
526324b6
HMH
133 enum rfkill_state state,
134 int force)
cf4328cd 135{
7f4c5341 136 int retval = 0;
801e49af
HMH
137 enum rfkill_state oldstate, newstate;
138
e10e0dfe
HMH
139 if (unlikely(rfkill->dev.power.power_state.event & PM_EVENT_SLEEP))
140 return -EBUSY;
141
801e49af
HMH
142 oldstate = rfkill->state;
143
526324b6 144 if (rfkill->get_state && !force &&
492301fb 145 !rfkill->get_state(rfkill->data, &newstate)) {
801e49af 146 rfkill->state = newstate;
492301fb 147 }
cf4328cd 148
5005657c
HMH
149 switch (state) {
150 case RFKILL_STATE_HARD_BLOCKED:
151 /* typically happens when refreshing hardware state,
152 * such as on resume */
153 state = RFKILL_STATE_SOFT_BLOCKED;
154 break;
155 case RFKILL_STATE_UNBLOCKED:
156 /* force can't override this, only rfkill_force_state() can */
157 if (rfkill->state == RFKILL_STATE_HARD_BLOCKED)
158 return -EPERM;
159 break;
160 case RFKILL_STATE_SOFT_BLOCKED:
161 /* nothing to do, we want to give drivers the hint to double
162 * BLOCK even a transmitter that is already in state
163 * RFKILL_STATE_HARD_BLOCKED */
164 break;
96c87607 165 default:
f745ba03
HMH
166 WARN(1, KERN_WARNING
167 "rfkill: illegal state %d passed as parameter "
168 "to rfkill_toggle_radio\n", state);
96c87607 169 return -EINVAL;
5005657c
HMH
170 }
171
526324b6 172 if (force || state != rfkill->state) {
cf4328cd 173 retval = rfkill->toggle_radio(rfkill->data, state);
5005657c
HMH
174 /* never allow a HARD->SOFT downgrade! */
175 if (!retval && rfkill->state != RFKILL_STATE_HARD_BLOCKED)
cf4328cd
ID
176 rfkill->state = state;
177 }
178
417bd25a 179 if (force || rfkill->state != oldstate)
4dec9b80 180 rfkill_uevent(rfkill);
801e49af 181
492301fb 182 rfkill_led_trigger(rfkill, rfkill->state);
cf4328cd
ID
183 return retval;
184}
185
186/**
99619201 187 * __rfkill_switch_all - Toggle state of all switches of given type
435307a3 188 * @type: type of interfaces to be affected
cf4328cd
ID
189 * @state: the new state
190 *
435307a3
HMH
191 * This function toggles the state of all switches of given type,
192 * unless a specific switch is claimed by userspace (in which case,
e10e0dfe 193 * that switch is left alone) or suspended.
99619201 194 *
15635744 195 * Caller must have acquired rfkill_global_mutex.
cf4328cd 196 */
99619201
HMH
197static void __rfkill_switch_all(const enum rfkill_type type,
198 const enum rfkill_state state)
cf4328cd
ID
199{
200 struct rfkill *rfkill;
201
f745ba03
HMH
202 if (WARN((state >= RFKILL_STATE_MAX || type >= RFKILL_TYPE_MAX),
203 KERN_WARNING
204 "rfkill: illegal state %d or type %d "
205 "passed as parameter to __rfkill_switch_all\n",
206 state, type))
96c87607
HMH
207 return;
208
99619201 209 rfkill_global_states[type].current_state = state;
cf4328cd 210 list_for_each_entry(rfkill, &rfkill_list, node) {
621cac85 211 if (rfkill->type == type) {
064af111 212 mutex_lock(&rfkill->mutex);
526324b6 213 rfkill_toggle_radio(rfkill, state, 0);
064af111 214 mutex_unlock(&rfkill->mutex);
492301fb 215 rfkill_led_trigger(rfkill, rfkill->state);
064af111 216 }
cf4328cd 217 }
99619201 218}
cf4328cd 219
99619201
HMH
220/**
221 * rfkill_switch_all - Toggle state of all switches of given type
222 * @type: type of interfaces to be affected
223 * @state: the new state
224 *
15635744 225 * Acquires rfkill_global_mutex and calls __rfkill_switch_all(@type, @state).
99619201 226 * Please refer to __rfkill_switch_all() for details.
d003922d
HMH
227 *
228 * Does nothing if the EPO lock is active.
99619201
HMH
229 */
230void rfkill_switch_all(enum rfkill_type type, enum rfkill_state state)
231{
15635744 232 mutex_lock(&rfkill_global_mutex);
d003922d
HMH
233 if (!rfkill_epo_lock_active)
234 __rfkill_switch_all(type, state);
15635744 235 mutex_unlock(&rfkill_global_mutex);
cf4328cd
ID
236}
237EXPORT_SYMBOL(rfkill_switch_all);
238
4081f00d
HMH
239/**
240 * rfkill_epo - emergency power off all transmitters
241 *
e10e0dfe 242 * This kicks all non-suspended rfkill devices to RFKILL_STATE_SOFT_BLOCKED,
15635744 243 * ignoring everything in its path but rfkill_global_mutex and rfkill->mutex.
99619201
HMH
244 *
245 * The global state before the EPO is saved and can be restored later
246 * using rfkill_restore_states().
4081f00d
HMH
247 */
248void rfkill_epo(void)
249{
250 struct rfkill *rfkill;
99619201 251 int i;
4081f00d 252
15635744
HMH
253 mutex_lock(&rfkill_global_mutex);
254
d003922d 255 rfkill_epo_lock_active = true;
4081f00d 256 list_for_each_entry(rfkill, &rfkill_list, node) {
064af111 257 mutex_lock(&rfkill->mutex);
5005657c 258 rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1);
064af111 259 mutex_unlock(&rfkill->mutex);
4081f00d 260 }
99619201
HMH
261 for (i = 0; i < RFKILL_TYPE_MAX; i++) {
262 rfkill_global_states[i].default_state =
263 rfkill_global_states[i].current_state;
264 rfkill_global_states[i].current_state =
265 RFKILL_STATE_SOFT_BLOCKED;
266 }
15635744 267 mutex_unlock(&rfkill_global_mutex);
492301fb 268 rfkill_led_trigger(rfkill, rfkill->state);
4081f00d
HMH
269}
270EXPORT_SYMBOL_GPL(rfkill_epo);
271
99619201
HMH
272/**
273 * rfkill_restore_states - restore global states
274 *
275 * Restore (and sync switches to) the global state from the
276 * states in rfkill_default_states. This can undo the effects of
277 * a call to rfkill_epo().
278 */
279void rfkill_restore_states(void)
280{
281 int i;
282
15635744
HMH
283 mutex_lock(&rfkill_global_mutex);
284
d003922d 285 rfkill_epo_lock_active = false;
99619201
HMH
286 for (i = 0; i < RFKILL_TYPE_MAX; i++)
287 __rfkill_switch_all(i, rfkill_global_states[i].default_state);
15635744 288 mutex_unlock(&rfkill_global_mutex);
99619201
HMH
289}
290EXPORT_SYMBOL_GPL(rfkill_restore_states);
291
d003922d
HMH
292/**
293 * rfkill_remove_epo_lock - unlock state changes
294 *
295 * Used by rfkill-input manually unlock state changes, when
296 * the EPO switch is deactivated.
297 */
298void rfkill_remove_epo_lock(void)
299{
300 mutex_lock(&rfkill_global_mutex);
301 rfkill_epo_lock_active = false;
302 mutex_unlock(&rfkill_global_mutex);
303}
304EXPORT_SYMBOL_GPL(rfkill_remove_epo_lock);
305
306/**
307 * rfkill_is_epo_lock_active - returns true EPO is active
308 *
309 * Returns 0 (false) if there is NOT an active EPO contidion,
310 * and 1 (true) if there is an active EPO contition, which
311 * locks all radios in one of the BLOCKED states.
312 *
313 * Can be called in atomic context.
314 */
315bool rfkill_is_epo_lock_active(void)
316{
317 return rfkill_epo_lock_active;
318}
319EXPORT_SYMBOL_GPL(rfkill_is_epo_lock_active);
320
68d2413b
HMH
321/**
322 * rfkill_get_global_state - returns global state for a type
323 * @type: the type to get the global state of
324 *
325 * Returns the current global state for a given wireless
326 * device type.
327 */
328enum rfkill_state rfkill_get_global_state(const enum rfkill_type type)
329{
330 return rfkill_global_states[type].current_state;
331}
332EXPORT_SYMBOL_GPL(rfkill_get_global_state);
333
801e49af
HMH
334/**
335 * rfkill_force_state - Force the internal rfkill radio state
336 * @rfkill: pointer to the rfkill class to modify.
337 * @state: the current radio state the class should be forced to.
338 *
339 * This function updates the internal state of the radio cached
340 * by the rfkill class. It should be used when the driver gets
341 * a notification by the firmware/hardware of the current *real*
342 * state of the radio rfkill switch.
343 *
2fd9b221
HMH
344 * Devices which are subject to external changes on their rfkill
345 * state (such as those caused by a hardware rfkill line) MUST
346 * have their driver arrange to call rfkill_force_state() as soon
347 * as possible after such a change.
348 *
349 * This function may not be called from an atomic context.
801e49af
HMH
350 */
351int rfkill_force_state(struct rfkill *rfkill, enum rfkill_state state)
352{
79399a8d
HMH
353 enum rfkill_state oldstate;
354
f745ba03
HMH
355 BUG_ON(!rfkill);
356 if (WARN((state >= RFKILL_STATE_MAX),
357 KERN_WARNING
358 "rfkill: illegal state %d passed as parameter "
359 "to rfkill_force_state\n", state))
801e49af
HMH
360 return -EINVAL;
361
362 mutex_lock(&rfkill->mutex);
79399a8d
HMH
363
364 oldstate = rfkill->state;
801e49af 365 rfkill->state = state;
79399a8d
HMH
366
367 if (state != oldstate)
4dec9b80 368 rfkill_uevent(rfkill);
79399a8d 369
801e49af 370 mutex_unlock(&rfkill->mutex);
492301fb 371 rfkill_led_trigger(rfkill, rfkill->state);
801e49af
HMH
372
373 return 0;
374}
375EXPORT_SYMBOL(rfkill_force_state);
376
cf4328cd
ID
377static ssize_t rfkill_name_show(struct device *dev,
378 struct device_attribute *attr,
379 char *buf)
380{
381 struct rfkill *rfkill = to_rfkill(dev);
382
383 return sprintf(buf, "%s\n", rfkill->name);
384}
385
99c632e5 386static const char *rfkill_get_type_str(enum rfkill_type type)
cf4328cd 387{
99c632e5 388 switch (type) {
cf4328cd 389 case RFKILL_TYPE_WLAN:
99c632e5 390 return "wlan";
cf4328cd 391 case RFKILL_TYPE_BLUETOOTH:
99c632e5 392 return "bluetooth";
e0665486 393 case RFKILL_TYPE_UWB:
99c632e5 394 return "ultrawideband";
303d9bf6 395 case RFKILL_TYPE_WIMAX:
99c632e5 396 return "wimax";
477576a0 397 case RFKILL_TYPE_WWAN:
99c632e5 398 return "wwan";
cf4328cd
ID
399 default:
400 BUG();
401 }
99c632e5
HMH
402}
403
404static ssize_t rfkill_type_show(struct device *dev,
405 struct device_attribute *attr,
406 char *buf)
407{
408 struct rfkill *rfkill = to_rfkill(dev);
cf4328cd 409
99c632e5 410 return sprintf(buf, "%s\n", rfkill_get_type_str(rfkill->type));
cf4328cd
ID
411}
412
413static ssize_t rfkill_state_show(struct device *dev,
414 struct device_attribute *attr,
415 char *buf)
416{
417 struct rfkill *rfkill = to_rfkill(dev);
418
801e49af 419 update_rfkill_state(rfkill);
cf4328cd
ID
420 return sprintf(buf, "%d\n", rfkill->state);
421}
422
423static ssize_t rfkill_state_store(struct device *dev,
424 struct device_attribute *attr,
425 const char *buf, size_t count)
426{
427 struct rfkill *rfkill = to_rfkill(dev);
849e0576 428 unsigned long state;
cf4328cd
ID
429 int error;
430
431 if (!capable(CAP_NET_ADMIN))
432 return -EPERM;
433
849e0576
HMH
434 error = strict_strtoul(buf, 0, &state);
435 if (error)
436 return error;
437
5005657c
HMH
438 /* RFKILL_STATE_HARD_BLOCKED is illegal here... */
439 if (state != RFKILL_STATE_UNBLOCKED &&
440 state != RFKILL_STATE_SOFT_BLOCKED)
441 return -EINVAL;
442
cf4b4aab
HMH
443 error = mutex_lock_killable(&rfkill->mutex);
444 if (error)
445 return error;
d003922d
HMH
446
447 if (!rfkill_epo_lock_active)
448 error = rfkill_toggle_radio(rfkill, state, 0);
449 else
450 error = -EPERM;
451
7f4c5341 452 mutex_unlock(&rfkill->mutex);
cf4328cd 453
7f4c5341 454 return error ? error : count;
cf4328cd
ID
455}
456
457static ssize_t rfkill_claim_show(struct device *dev,
458 struct device_attribute *attr,
459 char *buf)
460{
621cac85 461 return sprintf(buf, "%d\n", 0);
cf4328cd
ID
462}
463
464static ssize_t rfkill_claim_store(struct device *dev,
465 struct device_attribute *attr,
466 const char *buf, size_t count)
467{
621cac85 468 return -EOPNOTSUPP;
cf4328cd
ID
469}
470
471static struct device_attribute rfkill_dev_attrs[] = {
472 __ATTR(name, S_IRUGO, rfkill_name_show, NULL),
473 __ATTR(type, S_IRUGO, rfkill_type_show, NULL),
c81de6ad 474 __ATTR(state, S_IRUGO|S_IWUSR, rfkill_state_show, rfkill_state_store),
cf4328cd
ID
475 __ATTR(claim, S_IRUGO|S_IWUSR, rfkill_claim_show, rfkill_claim_store),
476 __ATTR_NULL
477};
478
479static void rfkill_release(struct device *dev)
480{
481 struct rfkill *rfkill = to_rfkill(dev);
482
483 kfree(rfkill);
484 module_put(THIS_MODULE);
485}
486
487#ifdef CONFIG_PM
488static int rfkill_suspend(struct device *dev, pm_message_t state)
489{
f80b5e99
HMH
490 struct rfkill *rfkill = to_rfkill(dev);
491
bed7aac9
HMH
492 /* mark class device as suspended */
493 if (dev->power.power_state.event != state.event)
cf4328cd 494 dev->power.power_state = state;
cf4328cd 495
f80b5e99
HMH
496 /* store state for the resume handler */
497 rfkill->state_for_resume = rfkill->state;
498
cf4328cd
ID
499 return 0;
500}
501
502static int rfkill_resume(struct device *dev)
503{
504 struct rfkill *rfkill = to_rfkill(dev);
24689c85 505 enum rfkill_state newstate;
cf4328cd
ID
506
507 if (dev->power.power_state.event != PM_EVENT_ON) {
508 mutex_lock(&rfkill->mutex);
509
e10e0dfe
HMH
510 dev->power.power_state.event = PM_EVENT_ON;
511
24689c85
HMH
512 /*
513 * rfkill->state could have been modified before we got
514 * called, and won't be updated by rfkill_toggle_radio()
515 * in force mode. Sync it FIRST.
516 */
517 if (rfkill->get_state &&
518 !rfkill->get_state(rfkill->data, &newstate))
519 rfkill->state = newstate;
520
17670799
HMH
521 /*
522 * If we are under EPO, kick transmitter offline,
523 * otherwise restore to pre-suspend state.
524 *
525 * Issue a notification in any case
526 */
527 rfkill_toggle_radio(rfkill,
528 rfkill_epo_lock_active ?
529 RFKILL_STATE_SOFT_BLOCKED :
f80b5e99 530 rfkill->state_for_resume,
17670799 531 1);
cf4328cd
ID
532
533 mutex_unlock(&rfkill->mutex);
492301fb 534 rfkill_led_trigger(rfkill, rfkill->state);
cf4328cd
ID
535 }
536
cf4328cd
ID
537 return 0;
538}
539#else
540#define rfkill_suspend NULL
541#define rfkill_resume NULL
542#endif
543
ffb67c34
HMH
544static int rfkill_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
545{
546 struct rfkill *rfkill = to_rfkill(dev);
547 int error;
548
549 error = add_uevent_var(env, "RFKILL_NAME=%s", rfkill->name);
550 if (error)
551 return error;
552 error = add_uevent_var(env, "RFKILL_TYPE=%s",
553 rfkill_get_type_str(rfkill->type));
554 if (error)
555 return error;
556 error = add_uevent_var(env, "RFKILL_STATE=%d", rfkill->state);
557 return error;
558}
559
cf4328cd
ID
560static struct class rfkill_class = {
561 .name = "rfkill",
562 .dev_release = rfkill_release,
563 .dev_attrs = rfkill_dev_attrs,
564 .suspend = rfkill_suspend,
565 .resume = rfkill_resume,
ffb67c34 566 .dev_uevent = rfkill_dev_uevent,
cf4328cd
ID
567};
568
02589f60
HMH
569static int rfkill_check_duplicity(const struct rfkill *rfkill)
570{
571 struct rfkill *p;
572 unsigned long seen[BITS_TO_LONGS(RFKILL_TYPE_MAX)];
573
574 memset(seen, 0, sizeof(seen));
575
576 list_for_each_entry(p, &rfkill_list, node) {
f745ba03
HMH
577 if (WARN((p == rfkill), KERN_WARNING
578 "rfkill: illegal attempt to register "
579 "an already registered rfkill struct\n"))
02589f60 580 return -EEXIST;
02589f60
HMH
581 set_bit(p->type, seen);
582 }
583
584 /* 0: first switch of its kind */
4a9d9167 585 return (test_bit(rfkill->type, seen)) ? 1 : 0;
02589f60
HMH
586}
587
cf4328cd
ID
588static int rfkill_add_switch(struct rfkill *rfkill)
589{
02589f60
HMH
590 int error;
591
15635744 592 mutex_lock(&rfkill_global_mutex);
cf4328cd 593
02589f60
HMH
594 error = rfkill_check_duplicity(rfkill);
595 if (error < 0)
596 goto unlock_out;
597
99619201
HMH
598 if (!error) {
599 /* lock default after first use */
600 set_bit(rfkill->type, rfkill_states_lockdflt);
601 rfkill_global_states[rfkill->type].current_state =
602 rfkill_global_states[rfkill->type].default_state;
603 }
604
605 rfkill_toggle_radio(rfkill,
606 rfkill_global_states[rfkill->type].current_state,
607 0);
fd4484af
HMH
608
609 list_add_tail(&rfkill->node, &rfkill_list);
cf4328cd 610
02589f60
HMH
611 error = 0;
612unlock_out:
15635744 613 mutex_unlock(&rfkill_global_mutex);
7319f1e6 614
02589f60 615 return error;
cf4328cd
ID
616}
617
618static void rfkill_remove_switch(struct rfkill *rfkill)
619{
15635744 620 mutex_lock(&rfkill_global_mutex);
cf4328cd 621 list_del_init(&rfkill->node);
15635744 622 mutex_unlock(&rfkill_global_mutex);
064af111
HMH
623
624 mutex_lock(&rfkill->mutex);
625 rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1);
626 mutex_unlock(&rfkill->mutex);
cf4328cd
ID
627}
628
629/**
630 * rfkill_allocate - allocate memory for rfkill structure.
631 * @parent: device that has rf switch on it
234a0ca6 632 * @type: type of the switch (RFKILL_TYPE_*)
cf4328cd
ID
633 *
634 * This function should be called by the network driver when it needs
435307a3
HMH
635 * rfkill structure. Once the structure is allocated the driver should
636 * finish its initialization by setting the name, private data, enable_radio
cf4328cd 637 * and disable_radio methods and then register it with rfkill_register().
435307a3 638 *
cf4328cd
ID
639 * NOTE: If registration fails the structure shoudl be freed by calling
640 * rfkill_free() otherwise rfkill_unregister() should be used.
641 */
77fba13c
HMH
642struct rfkill * __must_check rfkill_allocate(struct device *parent,
643 enum rfkill_type type)
cf4328cd
ID
644{
645 struct rfkill *rfkill;
646 struct device *dev;
647
f745ba03
HMH
648 if (WARN((type >= RFKILL_TYPE_MAX),
649 KERN_WARNING
650 "rfkill: illegal type %d passed as parameter "
651 "to rfkill_allocate\n", type))
652 return NULL;
653
cf4328cd 654 rfkill = kzalloc(sizeof(struct rfkill), GFP_KERNEL);
d007da1f 655 if (!rfkill)
cf4328cd
ID
656 return NULL;
657
658 mutex_init(&rfkill->mutex);
659 INIT_LIST_HEAD(&rfkill->node);
660 rfkill->type = type;
661
662 dev = &rfkill->dev;
663 dev->class = &rfkill_class;
664 dev->parent = parent;
665 device_initialize(dev);
666
667 __module_get(THIS_MODULE);
668
669 return rfkill;
670}
671EXPORT_SYMBOL(rfkill_allocate);
672
673/**
674 * rfkill_free - Mark rfkill structure for deletion
675 * @rfkill: rfkill structure to be destroyed
676 *
435307a3 677 * Decrements reference count of the rfkill structure so it is destroyed.
cf4328cd
ID
678 * Note that rfkill_free() should _not_ be called after rfkill_unregister().
679 */
680void rfkill_free(struct rfkill *rfkill)
681{
682 if (rfkill)
683 put_device(&rfkill->dev);
684}
685EXPORT_SYMBOL(rfkill_free);
686
135900c1
MB
687static void rfkill_led_trigger_register(struct rfkill *rfkill)
688{
689#ifdef CONFIG_RFKILL_LEDS
690 int error;
691
7c4f4578 692 if (!rfkill->led_trigger.name)
fb28ad35 693 rfkill->led_trigger.name = dev_name(&rfkill->dev);
96185664
DB
694 if (!rfkill->led_trigger.activate)
695 rfkill->led_trigger.activate = rfkill_led_trigger_activate;
135900c1
MB
696 error = led_trigger_register(&rfkill->led_trigger);
697 if (error)
698 rfkill->led_trigger.name = NULL;
699#endif /* CONFIG_RFKILL_LEDS */
700}
701
702static void rfkill_led_trigger_unregister(struct rfkill *rfkill)
703{
704#ifdef CONFIG_RFKILL_LEDS
37f55e9d 705 if (rfkill->led_trigger.name) {
135900c1 706 led_trigger_unregister(&rfkill->led_trigger);
37f55e9d
HMH
707 rfkill->led_trigger.name = NULL;
708 }
135900c1
MB
709#endif
710}
711
cf4328cd
ID
712/**
713 * rfkill_register - Register a rfkill structure.
714 * @rfkill: rfkill structure to be registered
715 *
716 * This function should be called by the network driver when the rfkill
717 * structure needs to be registered. Immediately from registration the
718 * switch driver should be able to service calls to toggle_radio.
719 */
77fba13c 720int __must_check rfkill_register(struct rfkill *rfkill)
cf4328cd
ID
721{
722 static atomic_t rfkill_no = ATOMIC_INIT(0);
723 struct device *dev = &rfkill->dev;
724 int error;
725
f745ba03
HMH
726 if (WARN((!rfkill || !rfkill->toggle_radio ||
727 rfkill->type >= RFKILL_TYPE_MAX ||
728 rfkill->state >= RFKILL_STATE_MAX),
729 KERN_WARNING
730 "rfkill: attempt to register a "
731 "badly initialized rfkill struct\n"))
96c87607 732 return -EINVAL;
cf4328cd 733
fb28ad35 734 dev_set_name(dev, "rfkill%ld", (long)atomic_inc_return(&rfkill_no) - 1);
8a8f1c04
MB
735
736 rfkill_led_trigger_register(rfkill);
737
cf4328cd 738 error = rfkill_add_switch(rfkill);
632041f3
EP
739 if (error) {
740 rfkill_led_trigger_unregister(rfkill);
cf4328cd 741 return error;
632041f3 742 }
cf4328cd 743
cf4328cd
ID
744 error = device_add(dev);
745 if (error) {
746 rfkill_remove_switch(rfkill);
37f55e9d 747 rfkill_led_trigger_unregister(rfkill);
cf4328cd
ID
748 return error;
749 }
750
751 return 0;
752}
753EXPORT_SYMBOL(rfkill_register);
754
755/**
c8fcd905 756 * rfkill_unregister - Unregister a rfkill structure.
cf4328cd
ID
757 * @rfkill: rfkill structure to be unregistered
758 *
759 * This function should be called by the network driver during device
760 * teardown to destroy rfkill structure. Note that rfkill_free() should
761 * _not_ be called after rfkill_unregister().
762 */
763void rfkill_unregister(struct rfkill *rfkill)
764{
f745ba03 765 BUG_ON(!rfkill);
cf4328cd
ID
766 device_del(&rfkill->dev);
767 rfkill_remove_switch(rfkill);
8a8f1c04 768 rfkill_led_trigger_unregister(rfkill);
cf4328cd
ID
769 put_device(&rfkill->dev);
770}
771EXPORT_SYMBOL(rfkill_unregister);
772
99619201
HMH
773/**
774 * rfkill_set_default - set initial value for a switch type
775 * @type - the type of switch to set the default state of
776 * @state - the new default state for that group of switches
777 *
778 * Sets the initial state rfkill should use for a given type.
779 * The following initial states are allowed: RFKILL_STATE_SOFT_BLOCKED
780 * and RFKILL_STATE_UNBLOCKED.
781 *
782 * This function is meant to be used by platform drivers for platforms
783 * that can save switch state across power down/reboot.
784 *
785 * The default state for each switch type can be changed exactly once.
786 * After a switch of that type is registered, the default state cannot
787 * be changed anymore. This guards against multiple drivers it the
788 * same platform trying to set the initial switch default state, which
789 * is not allowed.
790 *
791 * Returns -EPERM if the state has already been set once or is in use,
792 * so drivers likely want to either ignore or at most printk(KERN_NOTICE)
793 * if this function returns -EPERM.
794 *
795 * Returns 0 if the new default state was set, or an error if it
796 * could not be set.
797 */
798int rfkill_set_default(enum rfkill_type type, enum rfkill_state state)
799{
800 int error;
801
f745ba03
HMH
802 if (WARN((type >= RFKILL_TYPE_MAX ||
803 (state != RFKILL_STATE_SOFT_BLOCKED &&
804 state != RFKILL_STATE_UNBLOCKED)),
805 KERN_WARNING
806 "rfkill: illegal state %d or type %d passed as "
807 "parameter to rfkill_set_default\n", state, type))
99619201
HMH
808 return -EINVAL;
809
15635744 810 mutex_lock(&rfkill_global_mutex);
99619201
HMH
811
812 if (!test_and_set_bit(type, rfkill_states_lockdflt)) {
813 rfkill_global_states[type].default_state = state;
68d2413b 814 rfkill_global_states[type].current_state = state;
99619201
HMH
815 error = 0;
816 } else
817 error = -EPERM;
818
15635744 819 mutex_unlock(&rfkill_global_mutex);
99619201
HMH
820 return error;
821}
822EXPORT_SYMBOL_GPL(rfkill_set_default);
823
cf4328cd
ID
824/*
825 * Rfkill module initialization/deinitialization.
826 */
827static int __init rfkill_init(void)
828{
829 int error;
830 int i;
831
5005657c
HMH
832 /* RFKILL_STATE_HARD_BLOCKED is illegal here... */
833 if (rfkill_default_state != RFKILL_STATE_SOFT_BLOCKED &&
834 rfkill_default_state != RFKILL_STATE_UNBLOCKED)
e954b0b8
HMH
835 return -EINVAL;
836
99619201
HMH
837 for (i = 0; i < RFKILL_TYPE_MAX; i++)
838 rfkill_global_states[i].default_state = rfkill_default_state;
cf4328cd
ID
839
840 error = class_register(&rfkill_class);
841 if (error) {
842 printk(KERN_ERR "rfkill: unable to register rfkill class\n");
843 return error;
844 }
845
846 return 0;
847}
848
849static void __exit rfkill_exit(void)
850{
851 class_unregister(&rfkill_class);
852}
853
2bf236d5 854subsys_initcall(rfkill_init);
cf4328cd 855module_exit(rfkill_exit);
This page took 0.266609 seconds and 5 git commands to generate.