rfkill.c 23.6 KB
Newer Older
1
/*
2
 * Copyright (C) 2006 - 2007 Ivo van Doorn
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 * Copyright (C) 2007 Dmitry Torokhov
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the
 * Free Software Foundation, Inc.,
 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/workqueue.h>
#include <linux/capability.h>
#include <linux/list.h>
#include <linux/mutex.h>
#include <linux/rfkill.h>

M
Michael Buesch 已提交
30 31 32 33
/* Get declaration of rfkill_switch_all() to shut up sparse. */
#include "rfkill-input.h"


34 35 36 37 38 39
MODULE_AUTHOR("Ivo van Doorn <IvDoorn@gmail.com>");
MODULE_VERSION("1.0");
MODULE_DESCRIPTION("RF switch support");
MODULE_LICENSE("GPL");

static LIST_HEAD(rfkill_list);	/* list of registered rf switches */
40
static DEFINE_MUTEX(rfkill_global_mutex);
41

42
static unsigned int rfkill_default_state = RFKILL_STATE_UNBLOCKED;
43 44 45 46
module_param_named(default_state, rfkill_default_state, uint, 0444);
MODULE_PARM_DESC(default_state,
		 "Default initial state for all radio types, 0 = radio off");

47 48 49 50 51 52 53
struct rfkill_gsw_state {
	enum rfkill_state current_state;
	enum rfkill_state default_state;
};

static struct rfkill_gsw_state rfkill_global_states[RFKILL_TYPE_MAX];
static unsigned long rfkill_states_lockdflt[BITS_TO_LONGS(RFKILL_TYPE_MAX)];
54

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
static BLOCKING_NOTIFIER_HEAD(rfkill_notifier_list);


/**
 * register_rfkill_notifier - Add notifier to rfkill notifier chain
 * @nb: pointer to the new entry to add to the chain
 *
 * See blocking_notifier_chain_register() for return value and further
 * observations.
 *
 * Adds a notifier to the rfkill notifier chain.  The chain will be
 * called with a pointer to the relevant rfkill structure as a parameter,
 * refer to include/linux/rfkill.h for the possible events.
 *
 * Notifiers added to this chain are to always return NOTIFY_DONE.  This
 * chain is a blocking notifier chain: notifiers can sleep.
 *
 * Calls to this chain may have been done through a workqueue.  One must
 * assume unordered asynchronous behaviour, there is no way to know if
 * actions related to the event that generated the notification have been
 * carried out already.
 */
int register_rfkill_notifier(struct notifier_block *nb)
{
79
	BUG_ON(!nb);
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
	return blocking_notifier_chain_register(&rfkill_notifier_list, nb);
}
EXPORT_SYMBOL_GPL(register_rfkill_notifier);

/**
 * unregister_rfkill_notifier - remove notifier from rfkill notifier chain
 * @nb: pointer to the entry to remove from the chain
 *
 * See blocking_notifier_chain_unregister() for return value and further
 * observations.
 *
 * Removes a notifier from the rfkill notifier chain.
 */
int unregister_rfkill_notifier(struct notifier_block *nb)
{
95
	BUG_ON(!nb);
96 97 98 99
	return blocking_notifier_chain_unregister(&rfkill_notifier_list, nb);
}
EXPORT_SYMBOL_GPL(unregister_rfkill_notifier);

100 101 102 103 104 105 106 107 108

static void rfkill_led_trigger(struct rfkill *rfkill,
			       enum rfkill_state state)
{
#ifdef CONFIG_RFKILL_LEDS
	struct led_trigger *led = &rfkill->led_trigger;

	if (!led->name)
		return;
109
	if (state != RFKILL_STATE_UNBLOCKED)
110 111 112 113 114 115
		led_trigger_event(led, LED_OFF);
	else
		led_trigger_event(led, LED_FULL);
#endif /* CONFIG_RFKILL_LEDS */
}

116 117 118 119 120 121 122 123 124 125
#ifdef CONFIG_RFKILL_LEDS
static void rfkill_led_trigger_activate(struct led_classdev *led)
{
	struct rfkill *rfkill = container_of(led->trigger,
			struct rfkill, led_trigger);

	rfkill_led_trigger(rfkill, rfkill->state);
}
#endif /* CONFIG_RFKILL_LEDS */

126 127
static void notify_rfkill_state_change(struct rfkill *rfkill)
{
128
	rfkill_led_trigger(rfkill, rfkill->state);
129 130 131 132 133
	blocking_notifier_call_chain(&rfkill_notifier_list,
			RFKILL_STATE_CHANGED,
			rfkill);
}

134 135
static void update_rfkill_state(struct rfkill *rfkill)
{
136
	enum rfkill_state newstate, oldstate;
137 138 139

	if (rfkill->get_state) {
		mutex_lock(&rfkill->mutex);
140 141
		if (!rfkill->get_state(rfkill->data, &newstate)) {
			oldstate = rfkill->state;
142
			rfkill->state = newstate;
143 144 145
			if (oldstate != newstate)
				notify_rfkill_state_change(rfkill);
		}
146 147 148 149
		mutex_unlock(&rfkill->mutex);
	}
}

150 151 152 153 154 155 156 157
/**
 * rfkill_toggle_radio - wrapper for toggle_radio hook
 * @rfkill: the rfkill struct to use
 * @force: calls toggle_radio even if cache says it is not needed,
 *	and also makes sure notifications of the state will be
 *	sent even if it didn't change
 * @state: the new state to call toggle_radio() with
 *
158 159 160 161
 * Calls rfkill->toggle_radio, enforcing the API for toggle_radio
 * calls and handling all the red tape such as issuing notifications
 * if the call is successful.
 *
162 163
 * Suspended devices are not touched at all, and -EAGAIN is returned.
 *
164 165
 * Note that the @force parameter cannot override a (possibly cached)
 * state of RFKILL_STATE_HARD_BLOCKED.  Any device making use of
166 167 168 169 170 171 172
 * RFKILL_STATE_HARD_BLOCKED implements either get_state() or
 * rfkill_force_state(), so the cache either is bypassed or valid.
 *
 * Note that we do call toggle_radio for RFKILL_STATE_SOFT_BLOCKED
 * even if the radio is in RFKILL_STATE_HARD_BLOCKED state, so as to
 * give the driver a hint that it should double-BLOCK the transmitter.
 *
173
 * Caller must have acquired rfkill->mutex.
174
 */
175
static int rfkill_toggle_radio(struct rfkill *rfkill,
176 177
				enum rfkill_state state,
				int force)
178
{
M
Michael Buesch 已提交
179
	int retval = 0;
180 181
	enum rfkill_state oldstate, newstate;

182 183 184
	if (unlikely(rfkill->dev.power.power_state.event & PM_EVENT_SLEEP))
		return -EBUSY;

185 186
	oldstate = rfkill->state;

187
	if (rfkill->get_state && !force &&
188 189
	    !rfkill->get_state(rfkill->data, &newstate))
		rfkill->state = newstate;
190

191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
	switch (state) {
	case RFKILL_STATE_HARD_BLOCKED:
		/* typically happens when refreshing hardware state,
		 * such as on resume */
		state = RFKILL_STATE_SOFT_BLOCKED;
		break;
	case RFKILL_STATE_UNBLOCKED:
		/* force can't override this, only rfkill_force_state() can */
		if (rfkill->state == RFKILL_STATE_HARD_BLOCKED)
			return -EPERM;
		break;
	case RFKILL_STATE_SOFT_BLOCKED:
		/* nothing to do, we want to give drivers the hint to double
		 * BLOCK even a transmitter that is already in state
		 * RFKILL_STATE_HARD_BLOCKED */
		break;
207
	default:
208 209 210
		WARN(1, KERN_WARNING
			"rfkill: illegal state %d passed as parameter "
			"to rfkill_toggle_radio\n", state);
211
		return -EINVAL;
212 213
	}

214
	if (force || state != rfkill->state) {
215
		retval = rfkill->toggle_radio(rfkill->data, state);
216 217
		/* never allow a HARD->SOFT downgrade! */
		if (!retval && rfkill->state != RFKILL_STATE_HARD_BLOCKED)
218 219 220
			rfkill->state = state;
	}

221
	if (force || rfkill->state != oldstate)
222
		notify_rfkill_state_change(rfkill);
223

224 225 226 227
	return retval;
}

/**
228
 * __rfkill_switch_all - Toggle state of all switches of given type
229
 * @type: type of interfaces to be affected
230 231
 * @state: the new state
 *
232 233
 * This function toggles the state of all switches of given type,
 * unless a specific switch is claimed by userspace (in which case,
234
 * that switch is left alone) or suspended.
235
 *
236
 * Caller must have acquired rfkill_global_mutex.
237
 */
238 239
static void __rfkill_switch_all(const enum rfkill_type type,
				const enum rfkill_state state)
240 241 242
{
	struct rfkill *rfkill;

243 244 245 246 247
	if (WARN((state >= RFKILL_STATE_MAX || type >= RFKILL_TYPE_MAX),
			KERN_WARNING
			"rfkill: illegal state %d or type %d "
			"passed as parameter to __rfkill_switch_all\n",
			state, type))
248 249
		return;

250
	rfkill_global_states[type].current_state = state;
251
	list_for_each_entry(rfkill, &rfkill_list, node) {
252 253
		if ((!rfkill->user_claim) && (rfkill->type == type)) {
			mutex_lock(&rfkill->mutex);
254
			rfkill_toggle_radio(rfkill, state, 0);
255 256
			mutex_unlock(&rfkill->mutex);
		}
257
	}
258
}
259

260 261 262 263 264
/**
 * rfkill_switch_all - Toggle state of all switches of given type
 * @type: type of interfaces to be affected
 * @state: the new state
 *
265
 * Acquires rfkill_global_mutex and calls __rfkill_switch_all(@type, @state).
266 267 268 269
 * Please refer to __rfkill_switch_all() for details.
 */
void rfkill_switch_all(enum rfkill_type type, enum rfkill_state state)
{
270
	mutex_lock(&rfkill_global_mutex);
271
	__rfkill_switch_all(type, state);
272
	mutex_unlock(&rfkill_global_mutex);
273 274 275
}
EXPORT_SYMBOL(rfkill_switch_all);

276 277 278
/**
 * rfkill_epo - emergency power off all transmitters
 *
279
 * This kicks all non-suspended rfkill devices to RFKILL_STATE_SOFT_BLOCKED,
280
 * ignoring everything in its path but rfkill_global_mutex and rfkill->mutex.
281 282 283
 *
 * The global state before the EPO is saved and can be restored later
 * using rfkill_restore_states().
284 285 286 287
 */
void rfkill_epo(void)
{
	struct rfkill *rfkill;
288
	int i;
289

290 291
	mutex_lock(&rfkill_global_mutex);

292
	list_for_each_entry(rfkill, &rfkill_list, node) {
293
		mutex_lock(&rfkill->mutex);
294
		rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1);
295
		mutex_unlock(&rfkill->mutex);
296
	}
297 298 299 300 301 302
	for (i = 0; i < RFKILL_TYPE_MAX; i++) {
		rfkill_global_states[i].default_state =
				rfkill_global_states[i].current_state;
		rfkill_global_states[i].current_state =
				RFKILL_STATE_SOFT_BLOCKED;
	}
303
	mutex_unlock(&rfkill_global_mutex);
304 305 306
}
EXPORT_SYMBOL_GPL(rfkill_epo);

307 308 309 310 311 312 313 314 315 316 317
/**
 * rfkill_restore_states - restore global states
 *
 * Restore (and sync switches to) the global state from the
 * states in rfkill_default_states.  This can undo the effects of
 * a call to rfkill_epo().
 */
void rfkill_restore_states(void)
{
	int i;

318 319
	mutex_lock(&rfkill_global_mutex);

320 321
	for (i = 0; i < RFKILL_TYPE_MAX; i++)
		__rfkill_switch_all(i, rfkill_global_states[i].default_state);
322
	mutex_unlock(&rfkill_global_mutex);
323 324 325
}
EXPORT_SYMBOL_GPL(rfkill_restore_states);

326 327 328 329 330 331 332 333 334 335 336 337 338
/**
 * rfkill_get_global_state - returns global state for a type
 * @type: the type to get the global state of
 *
 * Returns the current global state for a given wireless
 * device type.
 */
enum rfkill_state rfkill_get_global_state(const enum rfkill_type type)
{
	return rfkill_global_states[type].current_state;
}
EXPORT_SYMBOL_GPL(rfkill_get_global_state);

339 340 341 342 343 344 345 346 347 348
/**
 * rfkill_force_state - Force the internal rfkill radio state
 * @rfkill: pointer to the rfkill class to modify.
 * @state: the current radio state the class should be forced to.
 *
 * This function updates the internal state of the radio cached
 * by the rfkill class.  It should be used when the driver gets
 * a notification by the firmware/hardware of the current *real*
 * state of the radio rfkill switch.
 *
349 350 351 352 353 354
 * Devices which are subject to external changes on their rfkill
 * state (such as those caused by a hardware rfkill line) MUST
 * have their driver arrange to call rfkill_force_state() as soon
 * as possible after such a change.
 *
 * This function may not be called from an atomic context.
355 356 357
 */
int rfkill_force_state(struct rfkill *rfkill, enum rfkill_state state)
{
358 359
	enum rfkill_state oldstate;

360 361 362 363 364
	BUG_ON(!rfkill);
	if (WARN((state >= RFKILL_STATE_MAX),
			KERN_WARNING
			"rfkill: illegal state %d passed as parameter "
			"to rfkill_force_state\n", state))
365 366 367
		return -EINVAL;

	mutex_lock(&rfkill->mutex);
368 369

	oldstate = rfkill->state;
370
	rfkill->state = state;
371 372 373 374

	if (state != oldstate)
		notify_rfkill_state_change(rfkill);

375 376 377 378 379 380
	mutex_unlock(&rfkill->mutex);

	return 0;
}
EXPORT_SYMBOL(rfkill_force_state);

381 382 383 384 385 386 387 388 389
static ssize_t rfkill_name_show(struct device *dev,
				struct device_attribute *attr,
				char *buf)
{
	struct rfkill *rfkill = to_rfkill(dev);

	return sprintf(buf, "%s\n", rfkill->name);
}

390
static const char *rfkill_get_type_str(enum rfkill_type type)
391
{
392
	switch (type) {
393
	case RFKILL_TYPE_WLAN:
394
		return "wlan";
395
	case RFKILL_TYPE_BLUETOOTH:
396
		return "bluetooth";
397
	case RFKILL_TYPE_UWB:
398
		return "ultrawideband";
399
	case RFKILL_TYPE_WIMAX:
400
		return "wimax";
401
	case RFKILL_TYPE_WWAN:
402
		return "wwan";
403 404 405
	default:
		BUG();
	}
406 407 408 409 410 411 412
}

static ssize_t rfkill_type_show(struct device *dev,
				struct device_attribute *attr,
				char *buf)
{
	struct rfkill *rfkill = to_rfkill(dev);
413

414
	return sprintf(buf, "%s\n", rfkill_get_type_str(rfkill->type));
415 416 417 418 419 420 421 422
}

static ssize_t rfkill_state_show(struct device *dev,
				 struct device_attribute *attr,
				 char *buf)
{
	struct rfkill *rfkill = to_rfkill(dev);

423
	update_rfkill_state(rfkill);
424 425 426 427 428 429 430 431
	return sprintf(buf, "%d\n", rfkill->state);
}

static ssize_t rfkill_state_store(struct device *dev,
				  struct device_attribute *attr,
				  const char *buf, size_t count)
{
	struct rfkill *rfkill = to_rfkill(dev);
432
	unsigned long state;
433 434 435 436 437
	int error;

	if (!capable(CAP_NET_ADMIN))
		return -EPERM;

438 439 440 441
	error = strict_strtoul(buf, 0, &state);
	if (error)
		return error;

442 443 444 445 446
	/* RFKILL_STATE_HARD_BLOCKED is illegal here... */
	if (state != RFKILL_STATE_UNBLOCKED &&
	    state != RFKILL_STATE_SOFT_BLOCKED)
		return -EINVAL;

447 448 449
	error = mutex_lock_killable(&rfkill->mutex);
	if (error)
		return error;
450
	error = rfkill_toggle_radio(rfkill, state, 0);
M
Michael Buesch 已提交
451
	mutex_unlock(&rfkill->mutex);
452

M
Michael Buesch 已提交
453
	return error ? error : count;
454 455 456 457 458 459 460 461
}

static ssize_t rfkill_claim_show(struct device *dev,
				 struct device_attribute *attr,
				 char *buf)
{
	struct rfkill *rfkill = to_rfkill(dev);

F
Felipe Balbi 已提交
462
	return sprintf(buf, "%d\n", rfkill->user_claim);
463 464 465 466 467 468 469
}

static ssize_t rfkill_claim_store(struct device *dev,
				  struct device_attribute *attr,
				  const char *buf, size_t count)
{
	struct rfkill *rfkill = to_rfkill(dev);
470 471
	unsigned long claim_tmp;
	bool claim;
472 473 474 475 476
	int error;

	if (!capable(CAP_NET_ADMIN))
		return -EPERM;

477 478 479
	if (rfkill->user_claim_unsupported)
		return -EOPNOTSUPP;

480 481 482 483 484
	error = strict_strtoul(buf, 0, &claim_tmp);
	if (error)
		return error;
	claim = !!claim_tmp;

485 486 487 488
	/*
	 * Take the global lock to make sure the kernel is not in
	 * the middle of rfkill_switch_all
	 */
489
	error = mutex_lock_killable(&rfkill_global_mutex);
490 491 492 493
	if (error)
		return error;

	if (rfkill->user_claim != claim) {
494 495
		if (!claim) {
			mutex_lock(&rfkill->mutex);
496
			rfkill_toggle_radio(rfkill,
497 498
					rfkill_global_states[rfkill->type].current_state,
					0);
499 500
			mutex_unlock(&rfkill->mutex);
		}
501 502 503
		rfkill->user_claim = claim;
	}

504
	mutex_unlock(&rfkill_global_mutex);
505

506
	return error ? error : count;
507 508 509 510 511
}

static struct device_attribute rfkill_dev_attrs[] = {
	__ATTR(name, S_IRUGO, rfkill_name_show, NULL),
	__ATTR(type, S_IRUGO, rfkill_type_show, NULL),
512
	__ATTR(state, S_IRUGO|S_IWUSR, rfkill_state_show, rfkill_state_store),
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
	__ATTR(claim, S_IRUGO|S_IWUSR, rfkill_claim_show, rfkill_claim_store),
	__ATTR_NULL
};

static void rfkill_release(struct device *dev)
{
	struct rfkill *rfkill = to_rfkill(dev);

	kfree(rfkill);
	module_put(THIS_MODULE);
}

#ifdef CONFIG_PM
static int rfkill_suspend(struct device *dev, pm_message_t state)
{
528 529
	/* mark class device as suspended */
	if (dev->power.power_state.event != state.event)
530 531 532 533 534 535 536 537 538 539 540 541
		dev->power.power_state = state;

	return 0;
}

static int rfkill_resume(struct device *dev)
{
	struct rfkill *rfkill = to_rfkill(dev);

	if (dev->power.power_state.event != PM_EVENT_ON) {
		mutex_lock(&rfkill->mutex);

542 543
		dev->power.power_state.event = PM_EVENT_ON;

544 545
		/* restore radio state AND notify everybody */
		rfkill_toggle_radio(rfkill, rfkill->state, 1);
546 547 548 549 550 551 552 553 554 555 556

		mutex_unlock(&rfkill->mutex);
	}

	return 0;
}
#else
#define rfkill_suspend NULL
#define rfkill_resume NULL
#endif

557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
static int rfkill_blocking_uevent_notifier(struct notifier_block *nb,
					unsigned long eventid,
					void *data)
{
	struct rfkill *rfkill = (struct rfkill *)data;

	switch (eventid) {
	case RFKILL_STATE_CHANGED:
		kobject_uevent(&rfkill->dev.kobj, KOBJ_CHANGE);
		break;
	default:
		break;
	}

	return NOTIFY_DONE;
}

static struct notifier_block rfkill_blocking_uevent_nb = {
	.notifier_call	= rfkill_blocking_uevent_notifier,
	.priority	= 0,
};

static int rfkill_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
{
	struct rfkill *rfkill = to_rfkill(dev);
	int error;

	error = add_uevent_var(env, "RFKILL_NAME=%s", rfkill->name);
	if (error)
		return error;
	error = add_uevent_var(env, "RFKILL_TYPE=%s",
				rfkill_get_type_str(rfkill->type));
	if (error)
		return error;
	error = add_uevent_var(env, "RFKILL_STATE=%d", rfkill->state);
	return error;
}

595 596 597 598 599 600
static struct class rfkill_class = {
	.name		= "rfkill",
	.dev_release	= rfkill_release,
	.dev_attrs	= rfkill_dev_attrs,
	.suspend	= rfkill_suspend,
	.resume		= rfkill_resume,
601
	.dev_uevent	= rfkill_dev_uevent,
602 603
};

604 605 606 607 608 609 610 611
static int rfkill_check_duplicity(const struct rfkill *rfkill)
{
	struct rfkill *p;
	unsigned long seen[BITS_TO_LONGS(RFKILL_TYPE_MAX)];

	memset(seen, 0, sizeof(seen));

	list_for_each_entry(p, &rfkill_list, node) {
612 613 614
		if (WARN((p == rfkill), KERN_WARNING
				"rfkill: illegal attempt to register "
				"an already registered rfkill struct\n"))
615 616 617 618 619 620 621 622
			return -EEXIST;
		set_bit(p->type, seen);
	}

	/* 0: first switch of its kind */
	return test_bit(rfkill->type, seen);
}

623 624
static int rfkill_add_switch(struct rfkill *rfkill)
{
625 626
	int error;

627
	mutex_lock(&rfkill_global_mutex);
628

629 630 631 632
	error = rfkill_check_duplicity(rfkill);
	if (error < 0)
		goto unlock_out;

633 634 635 636 637 638 639 640 641 642
	if (!error) {
		/* lock default after first use */
		set_bit(rfkill->type, rfkill_states_lockdflt);
		rfkill_global_states[rfkill->type].current_state =
			rfkill_global_states[rfkill->type].default_state;
	}

	rfkill_toggle_radio(rfkill,
			    rfkill_global_states[rfkill->type].current_state,
			    0);
643 644

	list_add_tail(&rfkill->node, &rfkill_list);
645

646 647
	error = 0;
unlock_out:
648
	mutex_unlock(&rfkill_global_mutex);
649

650
	return error;
651 652 653 654
}

static void rfkill_remove_switch(struct rfkill *rfkill)
{
655
	mutex_lock(&rfkill_global_mutex);
656
	list_del_init(&rfkill->node);
657
	mutex_unlock(&rfkill_global_mutex);
658 659 660 661

	mutex_lock(&rfkill->mutex);
	rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1);
	mutex_unlock(&rfkill->mutex);
662 663 664 665 666
}

/**
 * rfkill_allocate - allocate memory for rfkill structure.
 * @parent: device that has rf switch on it
I
Ivo van Doorn 已提交
667
 * @type: type of the switch (RFKILL_TYPE_*)
668 669
 *
 * This function should be called by the network driver when it needs
670 671
 * rfkill structure.  Once the structure is allocated the driver should
 * finish its initialization by setting the name, private data, enable_radio
672
 * and disable_radio methods and then register it with rfkill_register().
673
 *
674 675 676
 * NOTE: If registration fails the structure shoudl be freed by calling
 * rfkill_free() otherwise rfkill_unregister() should be used.
 */
677 678
struct rfkill * __must_check rfkill_allocate(struct device *parent,
					     enum rfkill_type type)
679 680 681 682
{
	struct rfkill *rfkill;
	struct device *dev;

683 684 685 686 687 688
	if (WARN((type >= RFKILL_TYPE_MAX),
			KERN_WARNING
			"rfkill: illegal type %d passed as parameter "
			"to rfkill_allocate\n", type))
		return NULL;

689
	rfkill = kzalloc(sizeof(struct rfkill), GFP_KERNEL);
690
	if (!rfkill)
691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
		return NULL;

	mutex_init(&rfkill->mutex);
	INIT_LIST_HEAD(&rfkill->node);
	rfkill->type = type;

	dev = &rfkill->dev;
	dev->class = &rfkill_class;
	dev->parent = parent;
	device_initialize(dev);

	__module_get(THIS_MODULE);

	return rfkill;
}
EXPORT_SYMBOL(rfkill_allocate);

/**
 * rfkill_free - Mark rfkill structure for deletion
 * @rfkill: rfkill structure to be destroyed
 *
712
 * Decrements reference count of the rfkill structure so it is destroyed.
713 714 715 716 717 718 719 720 721
 * Note that rfkill_free() should _not_ be called after rfkill_unregister().
 */
void rfkill_free(struct rfkill *rfkill)
{
	if (rfkill)
		put_device(&rfkill->dev);
}
EXPORT_SYMBOL(rfkill_free);

722 723 724 725 726
static void rfkill_led_trigger_register(struct rfkill *rfkill)
{
#ifdef CONFIG_RFKILL_LEDS
	int error;

727 728
	if (!rfkill->led_trigger.name)
		rfkill->led_trigger.name = rfkill->dev.bus_id;
729 730
	if (!rfkill->led_trigger.activate)
		rfkill->led_trigger.activate = rfkill_led_trigger_activate;
731 732 733 734 735 736 737 738 739
	error = led_trigger_register(&rfkill->led_trigger);
	if (error)
		rfkill->led_trigger.name = NULL;
#endif /* CONFIG_RFKILL_LEDS */
}

static void rfkill_led_trigger_unregister(struct rfkill *rfkill)
{
#ifdef CONFIG_RFKILL_LEDS
740
	if (rfkill->led_trigger.name) {
741
		led_trigger_unregister(&rfkill->led_trigger);
742 743
		rfkill->led_trigger.name = NULL;
	}
744 745 746
#endif
}

747 748 749 750 751 752 753 754
/**
 * rfkill_register - Register a rfkill structure.
 * @rfkill: rfkill structure to be registered
 *
 * This function should be called by the network driver when the rfkill
 * structure needs to be registered. Immediately from registration the
 * switch driver should be able to service calls to toggle_radio.
 */
755
int __must_check rfkill_register(struct rfkill *rfkill)
756 757 758 759 760
{
	static atomic_t rfkill_no = ATOMIC_INIT(0);
	struct device *dev = &rfkill->dev;
	int error;

761 762 763 764 765 766
	if (WARN((!rfkill || !rfkill->toggle_radio ||
			rfkill->type >= RFKILL_TYPE_MAX ||
			rfkill->state >= RFKILL_STATE_MAX),
			KERN_WARNING
			"rfkill: attempt to register a "
			"badly initialized rfkill struct\n"))
767
		return -EINVAL;
768

769 770 771 772 773
	snprintf(dev->bus_id, sizeof(dev->bus_id),
		 "rfkill%ld", (long)atomic_inc_return(&rfkill_no) - 1);

	rfkill_led_trigger_register(rfkill);

774
	error = rfkill_add_switch(rfkill);
775 776
	if (error) {
		rfkill_led_trigger_unregister(rfkill);
777
		return error;
778
	}
779 780 781 782

	error = device_add(dev);
	if (error) {
		rfkill_remove_switch(rfkill);
783
		rfkill_led_trigger_unregister(rfkill);
784 785 786 787 788 789 790 791
		return error;
	}

	return 0;
}
EXPORT_SYMBOL(rfkill_register);

/**
792
 * rfkill_unregister - Unregister a rfkill structure.
793 794 795 796 797 798 799 800
 * @rfkill: rfkill structure to be unregistered
 *
 * This function should be called by the network driver during device
 * teardown to destroy rfkill structure. Note that rfkill_free() should
 * _not_ be called after rfkill_unregister().
 */
void rfkill_unregister(struct rfkill *rfkill)
{
801
	BUG_ON(!rfkill);
802 803
	device_del(&rfkill->dev);
	rfkill_remove_switch(rfkill);
804
	rfkill_led_trigger_unregister(rfkill);
805 806 807 808
	put_device(&rfkill->dev);
}
EXPORT_SYMBOL(rfkill_unregister);

809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
/**
 * rfkill_set_default - set initial value for a switch type
 * @type - the type of switch to set the default state of
 * @state - the new default state for that group of switches
 *
 * Sets the initial state rfkill should use for a given type.
 * The following initial states are allowed: RFKILL_STATE_SOFT_BLOCKED
 * and RFKILL_STATE_UNBLOCKED.
 *
 * This function is meant to be used by platform drivers for platforms
 * that can save switch state across power down/reboot.
 *
 * The default state for each switch type can be changed exactly once.
 * After a switch of that type is registered, the default state cannot
 * be changed anymore.  This guards against multiple drivers it the
 * same platform trying to set the initial switch default state, which
 * is not allowed.
 *
 * Returns -EPERM if the state has already been set once or is in use,
 * so drivers likely want to either ignore or at most printk(KERN_NOTICE)
 * if this function returns -EPERM.
 *
 * Returns 0 if the new default state was set, or an error if it
 * could not be set.
 */
int rfkill_set_default(enum rfkill_type type, enum rfkill_state state)
{
	int error;

838 839 840 841 842 843
	if (WARN((type >= RFKILL_TYPE_MAX ||
			(state != RFKILL_STATE_SOFT_BLOCKED &&
			 state != RFKILL_STATE_UNBLOCKED)),
			KERN_WARNING
			"rfkill: illegal state %d or type %d passed as "
			"parameter to rfkill_set_default\n", state, type))
844 845
		return -EINVAL;

846
	mutex_lock(&rfkill_global_mutex);
847 848 849

	if (!test_and_set_bit(type, rfkill_states_lockdflt)) {
		rfkill_global_states[type].default_state = state;
850
		rfkill_global_states[type].current_state = state;
851 852 853 854
		error = 0;
	} else
		error = -EPERM;

855
	mutex_unlock(&rfkill_global_mutex);
856 857 858 859
	return error;
}
EXPORT_SYMBOL_GPL(rfkill_set_default);

860 861 862 863 864 865 866 867
/*
 * Rfkill module initialization/deinitialization.
 */
static int __init rfkill_init(void)
{
	int error;
	int i;

868 869 870
	/* RFKILL_STATE_HARD_BLOCKED is illegal here... */
	if (rfkill_default_state != RFKILL_STATE_SOFT_BLOCKED &&
	    rfkill_default_state != RFKILL_STATE_UNBLOCKED)
871 872
		return -EINVAL;

873 874
	for (i = 0; i < RFKILL_TYPE_MAX; i++)
		rfkill_global_states[i].default_state = rfkill_default_state;
875 876 877 878 879 880 881

	error = class_register(&rfkill_class);
	if (error) {
		printk(KERN_ERR "rfkill: unable to register rfkill class\n");
		return error;
	}

882 883
	register_rfkill_notifier(&rfkill_blocking_uevent_nb);

884 885 886 887 888
	return 0;
}

static void __exit rfkill_exit(void)
{
889
	unregister_rfkill_notifier(&rfkill_blocking_uevent_nb);
890 891 892
	class_unregister(&rfkill_class);
}

M
Michael Buesch 已提交
893
subsys_initcall(rfkill_init);
894
module_exit(rfkill_exit);