2007-05-07 15:34:20 +08:00
|
|
|
/*
|
2007-09-28 05:57:05 +08:00
|
|
|
* Copyright (C) 2006 - 2007 Ivo van Doorn
|
2007-05-07 15:34:20 +08:00
|
|
|
* 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>
|
|
|
|
|
2007-11-03 03:18:11 +08:00
|
|
|
/* Get declaration of rfkill_switch_all() to shut up sparse. */
|
|
|
|
#include "rfkill-input.h"
|
|
|
|
|
|
|
|
|
2007-05-07 15:34:20 +08:00
|
|
|
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 */
|
|
|
|
static DEFINE_MUTEX(rfkill_mutex);
|
|
|
|
|
2008-06-24 04:46:42 +08:00
|
|
|
static unsigned int rfkill_default_state = RFKILL_STATE_UNBLOCKED;
|
2008-06-24 04:22:59 +08:00
|
|
|
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");
|
|
|
|
|
2007-05-07 15:34:20 +08:00
|
|
|
static enum rfkill_state rfkill_states[RFKILL_TYPE_MAX];
|
|
|
|
|
2008-06-24 04:23:03 +08:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
return blocking_notifier_chain_unregister(&rfkill_notifier_list, nb);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(unregister_rfkill_notifier);
|
|
|
|
|
2007-09-28 03:33:12 +08:00
|
|
|
|
|
|
|
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;
|
2008-06-24 04:46:42 +08:00
|
|
|
if (state != RFKILL_STATE_UNBLOCKED)
|
2007-09-28 03:33:12 +08:00
|
|
|
led_trigger_event(led, LED_OFF);
|
|
|
|
else
|
|
|
|
led_trigger_event(led, LED_FULL);
|
|
|
|
#endif /* CONFIG_RFKILL_LEDS */
|
|
|
|
}
|
|
|
|
|
2008-07-22 18:21:59 +08:00
|
|
|
#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 */
|
|
|
|
|
2008-06-24 04:23:03 +08:00
|
|
|
static void notify_rfkill_state_change(struct rfkill *rfkill)
|
|
|
|
{
|
|
|
|
blocking_notifier_call_chain(&rfkill_notifier_list,
|
|
|
|
RFKILL_STATE_CHANGED,
|
|
|
|
rfkill);
|
|
|
|
}
|
|
|
|
|
2008-06-24 04:23:00 +08:00
|
|
|
static void update_rfkill_state(struct rfkill *rfkill)
|
|
|
|
{
|
2008-06-24 04:23:03 +08:00
|
|
|
enum rfkill_state newstate, oldstate;
|
2008-06-24 04:23:00 +08:00
|
|
|
|
|
|
|
if (rfkill->get_state) {
|
|
|
|
mutex_lock(&rfkill->mutex);
|
2008-06-24 04:23:03 +08:00
|
|
|
if (!rfkill->get_state(rfkill->data, &newstate)) {
|
|
|
|
oldstate = rfkill->state;
|
2008-06-24 04:23:00 +08:00
|
|
|
rfkill->state = newstate;
|
2008-06-24 04:23:03 +08:00
|
|
|
if (oldstate != newstate)
|
|
|
|
notify_rfkill_state_change(rfkill);
|
|
|
|
}
|
2008-06-24 04:23:00 +08:00
|
|
|
mutex_unlock(&rfkill->mutex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-24 04:46:42 +08:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*
|
2008-07-04 00:14:56 +08:00
|
|
|
* 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.
|
|
|
|
*
|
2008-07-22 08:18:22 +08:00
|
|
|
* Note that the @force parameter cannot override a (possibly cached)
|
|
|
|
* state of RFKILL_STATE_HARD_BLOCKED. Any device making use of
|
2008-06-24 04:46:42 +08:00
|
|
|
* 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.
|
|
|
|
*
|
2008-07-22 08:18:20 +08:00
|
|
|
* Caller must have acquired rfkill->mutex.
|
2008-06-24 04:46:42 +08:00
|
|
|
*/
|
2007-05-07 15:34:20 +08:00
|
|
|
static int rfkill_toggle_radio(struct rfkill *rfkill,
|
2008-06-24 04:23:02 +08:00
|
|
|
enum rfkill_state state,
|
|
|
|
int force)
|
2007-05-07 15:34:20 +08:00
|
|
|
{
|
2007-11-29 00:49:34 +08:00
|
|
|
int retval = 0;
|
2008-06-24 04:23:00 +08:00
|
|
|
enum rfkill_state oldstate, newstate;
|
|
|
|
|
|
|
|
oldstate = rfkill->state;
|
|
|
|
|
2008-06-24 04:23:02 +08:00
|
|
|
if (rfkill->get_state && !force &&
|
2008-06-24 04:23:00 +08:00
|
|
|
!rfkill->get_state(rfkill->data, &newstate))
|
|
|
|
rfkill->state = newstate;
|
2007-05-07 15:34:20 +08:00
|
|
|
|
2008-06-24 04:46:42 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-06-24 04:23:02 +08:00
|
|
|
if (force || state != rfkill->state) {
|
2007-05-07 15:34:20 +08:00
|
|
|
retval = rfkill->toggle_radio(rfkill->data, state);
|
2008-06-24 04:46:42 +08:00
|
|
|
/* never allow a HARD->SOFT downgrade! */
|
|
|
|
if (!retval && rfkill->state != RFKILL_STATE_HARD_BLOCKED)
|
2007-05-07 15:34:20 +08:00
|
|
|
rfkill->state = state;
|
|
|
|
}
|
|
|
|
|
2008-06-24 04:23:03 +08:00
|
|
|
if (force || rfkill->state != oldstate) {
|
2008-06-24 04:23:00 +08:00
|
|
|
rfkill_led_trigger(rfkill, rfkill->state);
|
2008-06-24 04:23:03 +08:00
|
|
|
notify_rfkill_state_change(rfkill);
|
|
|
|
}
|
2008-06-24 04:23:00 +08:00
|
|
|
|
2007-05-07 15:34:20 +08:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* rfkill_switch_all - Toggle state of all switches of given type
|
2008-07-22 08:18:22 +08:00
|
|
|
* @type: type of interfaces to be affected
|
2007-05-07 15:34:20 +08:00
|
|
|
* @state: the new state
|
|
|
|
*
|
2008-07-22 08:18:22 +08:00
|
|
|
* This function toggles the state of all switches of given type,
|
|
|
|
* unless a specific switch is claimed by userspace (in which case,
|
|
|
|
* that switch is left alone).
|
2007-05-07 15:34:20 +08:00
|
|
|
*/
|
|
|
|
void rfkill_switch_all(enum rfkill_type type, enum rfkill_state state)
|
|
|
|
{
|
|
|
|
struct rfkill *rfkill;
|
|
|
|
|
|
|
|
mutex_lock(&rfkill_mutex);
|
|
|
|
|
|
|
|
rfkill_states[type] = state;
|
|
|
|
|
|
|
|
list_for_each_entry(rfkill, &rfkill_list, node) {
|
2008-07-22 08:18:20 +08:00
|
|
|
if ((!rfkill->user_claim) && (rfkill->type == type)) {
|
|
|
|
mutex_lock(&rfkill->mutex);
|
2008-06-24 04:23:02 +08:00
|
|
|
rfkill_toggle_radio(rfkill, state, 0);
|
2008-07-22 08:18:20 +08:00
|
|
|
mutex_unlock(&rfkill->mutex);
|
|
|
|
}
|
2007-05-07 15:34:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
mutex_unlock(&rfkill_mutex);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(rfkill_switch_all);
|
|
|
|
|
2008-06-24 04:23:07 +08:00
|
|
|
/**
|
|
|
|
* rfkill_epo - emergency power off all transmitters
|
|
|
|
*
|
2008-06-24 04:46:42 +08:00
|
|
|
* This kicks all rfkill devices to RFKILL_STATE_SOFT_BLOCKED, ignoring
|
2008-07-22 08:18:20 +08:00
|
|
|
* everything in its path but rfkill_mutex and rfkill->mutex.
|
2008-06-24 04:23:07 +08:00
|
|
|
*/
|
|
|
|
void rfkill_epo(void)
|
|
|
|
{
|
|
|
|
struct rfkill *rfkill;
|
|
|
|
|
|
|
|
mutex_lock(&rfkill_mutex);
|
|
|
|
list_for_each_entry(rfkill, &rfkill_list, node) {
|
2008-07-22 08:18:20 +08:00
|
|
|
mutex_lock(&rfkill->mutex);
|
2008-06-24 04:46:42 +08:00
|
|
|
rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1);
|
2008-07-22 08:18:20 +08:00
|
|
|
mutex_unlock(&rfkill->mutex);
|
2008-06-24 04:23:07 +08:00
|
|
|
}
|
|
|
|
mutex_unlock(&rfkill_mutex);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(rfkill_epo);
|
|
|
|
|
2008-06-24 04:23:00 +08:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
2008-07-22 08:18:17 +08:00
|
|
|
* 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.
|
2008-06-24 04:23:00 +08:00
|
|
|
*/
|
|
|
|
int rfkill_force_state(struct rfkill *rfkill, enum rfkill_state state)
|
|
|
|
{
|
2008-06-24 04:23:03 +08:00
|
|
|
enum rfkill_state oldstate;
|
|
|
|
|
2008-06-24 04:46:42 +08:00
|
|
|
if (state != RFKILL_STATE_SOFT_BLOCKED &&
|
|
|
|
state != RFKILL_STATE_UNBLOCKED &&
|
|
|
|
state != RFKILL_STATE_HARD_BLOCKED)
|
2008-06-24 04:23:00 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
mutex_lock(&rfkill->mutex);
|
2008-06-24 04:23:03 +08:00
|
|
|
|
|
|
|
oldstate = rfkill->state;
|
2008-06-24 04:23:00 +08:00
|
|
|
rfkill->state = state;
|
2008-06-24 04:23:03 +08:00
|
|
|
|
|
|
|
if (state != oldstate)
|
|
|
|
notify_rfkill_state_change(rfkill);
|
|
|
|
|
2008-06-24 04:23:00 +08:00
|
|
|
mutex_unlock(&rfkill->mutex);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(rfkill_force_state);
|
|
|
|
|
2007-05-07 15:34:20 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2008-06-24 04:23:04 +08:00
|
|
|
static const char *rfkill_get_type_str(enum rfkill_type type)
|
2007-05-07 15:34:20 +08:00
|
|
|
{
|
2008-06-24 04:23:04 +08:00
|
|
|
switch (type) {
|
2007-05-07 15:34:20 +08:00
|
|
|
case RFKILL_TYPE_WLAN:
|
2008-06-24 04:23:04 +08:00
|
|
|
return "wlan";
|
2007-05-07 15:34:20 +08:00
|
|
|
case RFKILL_TYPE_BLUETOOTH:
|
2008-06-24 04:23:04 +08:00
|
|
|
return "bluetooth";
|
2007-09-13 15:21:31 +08:00
|
|
|
case RFKILL_TYPE_UWB:
|
2008-06-24 04:23:04 +08:00
|
|
|
return "ultrawideband";
|
2008-01-24 05:40:27 +08:00
|
|
|
case RFKILL_TYPE_WIMAX:
|
2008-06-24 04:23:04 +08:00
|
|
|
return "wimax";
|
2008-06-24 04:23:01 +08:00
|
|
|
case RFKILL_TYPE_WWAN:
|
2008-06-24 04:23:04 +08:00
|
|
|
return "wwan";
|
2007-05-07 15:34:20 +08:00
|
|
|
default:
|
|
|
|
BUG();
|
|
|
|
}
|
2008-06-24 04:23:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t rfkill_type_show(struct device *dev,
|
|
|
|
struct device_attribute *attr,
|
|
|
|
char *buf)
|
|
|
|
{
|
|
|
|
struct rfkill *rfkill = to_rfkill(dev);
|
2007-05-07 15:34:20 +08:00
|
|
|
|
2008-06-24 04:23:04 +08:00
|
|
|
return sprintf(buf, "%s\n", rfkill_get_type_str(rfkill->type));
|
2007-05-07 15:34:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t rfkill_state_show(struct device *dev,
|
|
|
|
struct device_attribute *attr,
|
|
|
|
char *buf)
|
|
|
|
{
|
|
|
|
struct rfkill *rfkill = to_rfkill(dev);
|
|
|
|
|
2008-06-24 04:23:00 +08:00
|
|
|
update_rfkill_state(rfkill);
|
2007-05-07 15:34:20 +08:00
|
|
|
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);
|
|
|
|
unsigned int state = simple_strtoul(buf, NULL, 0);
|
|
|
|
int error;
|
|
|
|
|
|
|
|
if (!capable(CAP_NET_ADMIN))
|
|
|
|
return -EPERM;
|
|
|
|
|
2008-06-24 04:46:42 +08:00
|
|
|
/* RFKILL_STATE_HARD_BLOCKED is illegal here... */
|
|
|
|
if (state != RFKILL_STATE_UNBLOCKED &&
|
|
|
|
state != RFKILL_STATE_SOFT_BLOCKED)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2007-11-29 00:49:34 +08:00
|
|
|
if (mutex_lock_interruptible(&rfkill->mutex))
|
|
|
|
return -ERESTARTSYS;
|
2008-06-24 04:46:42 +08:00
|
|
|
error = rfkill_toggle_radio(rfkill, state, 0);
|
2007-11-29 00:49:34 +08:00
|
|
|
mutex_unlock(&rfkill->mutex);
|
2007-05-07 15:34:20 +08:00
|
|
|
|
2007-11-29 00:49:34 +08:00
|
|
|
return error ? error : count;
|
2007-05-07 15:34:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t rfkill_claim_show(struct device *dev,
|
|
|
|
struct device_attribute *attr,
|
|
|
|
char *buf)
|
|
|
|
{
|
|
|
|
struct rfkill *rfkill = to_rfkill(dev);
|
|
|
|
|
|
|
|
return sprintf(buf, "%d", rfkill->user_claim);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
bool claim = !!simple_strtoul(buf, NULL, 0);
|
|
|
|
int error;
|
|
|
|
|
|
|
|
if (!capable(CAP_NET_ADMIN))
|
|
|
|
return -EPERM;
|
|
|
|
|
2008-07-22 08:18:20 +08:00
|
|
|
if (rfkill->user_claim_unsupported)
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
2007-05-07 15:34:20 +08:00
|
|
|
/*
|
|
|
|
* Take the global lock to make sure the kernel is not in
|
|
|
|
* the middle of rfkill_switch_all
|
|
|
|
*/
|
|
|
|
error = mutex_lock_interruptible(&rfkill_mutex);
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
|
|
|
|
if (rfkill->user_claim != claim) {
|
2008-07-22 08:18:20 +08:00
|
|
|
if (!claim) {
|
|
|
|
mutex_lock(&rfkill->mutex);
|
2007-05-07 15:34:20 +08:00
|
|
|
rfkill_toggle_radio(rfkill,
|
2008-06-24 04:23:02 +08:00
|
|
|
rfkill_states[rfkill->type],
|
|
|
|
0);
|
2008-07-22 08:18:20 +08:00
|
|
|
mutex_unlock(&rfkill->mutex);
|
|
|
|
}
|
2007-05-07 15:34:20 +08:00
|
|
|
rfkill->user_claim = claim;
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex_unlock(&rfkill_mutex);
|
|
|
|
|
2007-09-28 03:34:23 +08:00
|
|
|
return error ? error : count;
|
2007-05-07 15:34:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct device_attribute rfkill_dev_attrs[] = {
|
|
|
|
__ATTR(name, S_IRUGO, rfkill_name_show, NULL),
|
|
|
|
__ATTR(type, S_IRUGO, rfkill_type_show, NULL),
|
2007-07-19 06:38:03 +08:00
|
|
|
__ATTR(state, S_IRUGO|S_IWUSR, rfkill_state_show, rfkill_state_store),
|
2007-05-07 15:34:20 +08:00
|
|
|
__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)
|
|
|
|
{
|
|
|
|
struct rfkill *rfkill = to_rfkill(dev);
|
|
|
|
|
|
|
|
if (dev->power.power_state.event != state.event) {
|
2008-02-24 02:13:25 +08:00
|
|
|
if (state.event & PM_EVENT_SLEEP) {
|
2008-06-24 04:23:02 +08:00
|
|
|
/* Stop transmitter, keep state, no notifies */
|
|
|
|
update_rfkill_state(rfkill);
|
2007-05-07 15:34:20 +08:00
|
|
|
|
2008-06-24 04:23:02 +08:00
|
|
|
mutex_lock(&rfkill->mutex);
|
2008-06-24 04:46:42 +08:00
|
|
|
rfkill->toggle_radio(rfkill->data,
|
|
|
|
RFKILL_STATE_SOFT_BLOCKED);
|
2007-05-07 15:34:20 +08:00
|
|
|
mutex_unlock(&rfkill->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2008-06-24 04:23:02 +08:00
|
|
|
/* restore radio state AND notify everybody */
|
|
|
|
rfkill_toggle_radio(rfkill, rfkill->state, 1);
|
2007-05-07 15:34:20 +08:00
|
|
|
|
|
|
|
mutex_unlock(&rfkill->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
dev->power.power_state = PMSG_ON;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define rfkill_suspend NULL
|
|
|
|
#define rfkill_resume NULL
|
|
|
|
#endif
|
|
|
|
|
2008-06-24 04:23:05 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2007-05-07 15:34:20 +08:00
|
|
|
static struct class rfkill_class = {
|
|
|
|
.name = "rfkill",
|
|
|
|
.dev_release = rfkill_release,
|
|
|
|
.dev_attrs = rfkill_dev_attrs,
|
|
|
|
.suspend = rfkill_suspend,
|
|
|
|
.resume = rfkill_resume,
|
2008-06-24 04:23:05 +08:00
|
|
|
.dev_uevent = rfkill_dev_uevent,
|
2007-05-07 15:34:20 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static int rfkill_add_switch(struct rfkill *rfkill)
|
|
|
|
{
|
2007-10-28 22:16:50 +08:00
|
|
|
mutex_lock(&rfkill_mutex);
|
2007-05-07 15:34:20 +08:00
|
|
|
|
2008-07-04 00:14:57 +08:00
|
|
|
rfkill_toggle_radio(rfkill, rfkill_states[rfkill->type], 0);
|
|
|
|
|
|
|
|
list_add_tail(&rfkill->node, &rfkill_list);
|
2007-05-07 15:34:20 +08:00
|
|
|
|
|
|
|
mutex_unlock(&rfkill_mutex);
|
2007-10-28 22:16:50 +08:00
|
|
|
|
2008-07-04 00:14:57 +08:00
|
|
|
return 0;
|
2007-05-07 15:34:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void rfkill_remove_switch(struct rfkill *rfkill)
|
|
|
|
{
|
|
|
|
mutex_lock(&rfkill_mutex);
|
|
|
|
list_del_init(&rfkill->node);
|
|
|
|
mutex_unlock(&rfkill_mutex);
|
2008-07-22 08:18:20 +08:00
|
|
|
|
|
|
|
mutex_lock(&rfkill->mutex);
|
|
|
|
rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1);
|
|
|
|
mutex_unlock(&rfkill->mutex);
|
2007-05-07 15:34:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* rfkill_allocate - allocate memory for rfkill structure.
|
|
|
|
* @parent: device that has rf switch on it
|
2007-09-13 15:20:42 +08:00
|
|
|
* @type: type of the switch (RFKILL_TYPE_*)
|
2007-05-07 15:34:20 +08:00
|
|
|
*
|
|
|
|
* This function should be called by the network driver when it needs
|
2008-07-22 08:18:22 +08:00
|
|
|
* rfkill structure. Once the structure is allocated the driver should
|
|
|
|
* finish its initialization by setting the name, private data, enable_radio
|
2007-05-07 15:34:20 +08:00
|
|
|
* and disable_radio methods and then register it with rfkill_register().
|
2008-07-22 08:18:22 +08:00
|
|
|
*
|
2007-05-07 15:34:20 +08:00
|
|
|
* NOTE: If registration fails the structure shoudl be freed by calling
|
|
|
|
* rfkill_free() otherwise rfkill_unregister() should be used.
|
|
|
|
*/
|
|
|
|
struct rfkill *rfkill_allocate(struct device *parent, enum rfkill_type type)
|
|
|
|
{
|
|
|
|
struct rfkill *rfkill;
|
|
|
|
struct device *dev;
|
|
|
|
|
|
|
|
rfkill = kzalloc(sizeof(struct rfkill), GFP_KERNEL);
|
2007-05-20 03:24:39 +08:00
|
|
|
if (!rfkill)
|
2007-05-07 15:34:20 +08:00
|
|
|
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
|
|
|
|
*
|
2008-07-22 08:18:22 +08:00
|
|
|
* Decrements reference count of the rfkill structure so it is destroyed.
|
2007-05-07 15:34:20 +08:00
|
|
|
* 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);
|
|
|
|
|
2007-09-28 03:33:12 +08:00
|
|
|
static void rfkill_led_trigger_register(struct rfkill *rfkill)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_RFKILL_LEDS
|
|
|
|
int error;
|
|
|
|
|
2008-07-22 18:17:37 +08:00
|
|
|
if (!rfkill->led_trigger.name)
|
|
|
|
rfkill->led_trigger.name = rfkill->dev.bus_id;
|
2008-07-22 18:21:59 +08:00
|
|
|
if (!rfkill->led_trigger.activate)
|
|
|
|
rfkill->led_trigger.activate = rfkill_led_trigger_activate;
|
2007-09-28 03:33:12 +08:00
|
|
|
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
|
2008-07-22 08:18:18 +08:00
|
|
|
if (rfkill->led_trigger.name) {
|
2007-09-28 03:33:12 +08:00
|
|
|
led_trigger_unregister(&rfkill->led_trigger);
|
2008-07-22 08:18:18 +08:00
|
|
|
rfkill->led_trigger.name = NULL;
|
|
|
|
}
|
2007-09-28 03:33:12 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2007-05-07 15:34:20 +08:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
int rfkill_register(struct rfkill *rfkill)
|
|
|
|
{
|
|
|
|
static atomic_t rfkill_no = ATOMIC_INIT(0);
|
|
|
|
struct device *dev = &rfkill->dev;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
if (!rfkill->toggle_radio)
|
|
|
|
return -EINVAL;
|
2007-10-28 22:16:50 +08:00
|
|
|
if (rfkill->type >= RFKILL_TYPE_MAX)
|
|
|
|
return -EINVAL;
|
2007-05-07 15:34:20 +08:00
|
|
|
|
2007-10-28 20:07:54 +08:00
|
|
|
snprintf(dev->bus_id, sizeof(dev->bus_id),
|
|
|
|
"rfkill%ld", (long)atomic_inc_return(&rfkill_no) - 1);
|
|
|
|
|
|
|
|
rfkill_led_trigger_register(rfkill);
|
|
|
|
|
2007-05-07 15:34:20 +08:00
|
|
|
error = rfkill_add_switch(rfkill);
|
2008-01-14 05:20:56 +08:00
|
|
|
if (error) {
|
|
|
|
rfkill_led_trigger_unregister(rfkill);
|
2007-05-07 15:34:20 +08:00
|
|
|
return error;
|
2008-01-14 05:20:56 +08:00
|
|
|
}
|
2007-05-07 15:34:20 +08:00
|
|
|
|
|
|
|
error = device_add(dev);
|
|
|
|
if (error) {
|
|
|
|
rfkill_remove_switch(rfkill);
|
2008-07-22 08:18:18 +08:00
|
|
|
rfkill_led_trigger_unregister(rfkill);
|
2007-05-07 15:34:20 +08:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(rfkill_register);
|
|
|
|
|
|
|
|
/**
|
2008-06-24 04:22:57 +08:00
|
|
|
* rfkill_unregister - Unregister a rfkill structure.
|
2007-05-07 15:34:20 +08:00
|
|
|
* @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)
|
|
|
|
{
|
|
|
|
device_del(&rfkill->dev);
|
|
|
|
rfkill_remove_switch(rfkill);
|
2007-10-28 20:07:54 +08:00
|
|
|
rfkill_led_trigger_unregister(rfkill);
|
2007-05-07 15:34:20 +08:00
|
|
|
put_device(&rfkill->dev);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(rfkill_unregister);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Rfkill module initialization/deinitialization.
|
|
|
|
*/
|
|
|
|
static int __init rfkill_init(void)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
int i;
|
|
|
|
|
2008-06-24 04:46:42 +08:00
|
|
|
/* RFKILL_STATE_HARD_BLOCKED is illegal here... */
|
|
|
|
if (rfkill_default_state != RFKILL_STATE_SOFT_BLOCKED &&
|
|
|
|
rfkill_default_state != RFKILL_STATE_UNBLOCKED)
|
2008-06-24 04:22:59 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
2007-05-07 15:34:20 +08:00
|
|
|
for (i = 0; i < ARRAY_SIZE(rfkill_states); i++)
|
2008-06-24 04:22:59 +08:00
|
|
|
rfkill_states[i] = rfkill_default_state;
|
2007-05-07 15:34:20 +08:00
|
|
|
|
|
|
|
error = class_register(&rfkill_class);
|
|
|
|
if (error) {
|
|
|
|
printk(KERN_ERR "rfkill: unable to register rfkill class\n");
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2008-06-24 04:23:05 +08:00
|
|
|
register_rfkill_notifier(&rfkill_blocking_uevent_nb);
|
|
|
|
|
2007-05-07 15:34:20 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit rfkill_exit(void)
|
|
|
|
{
|
2008-06-24 04:23:05 +08:00
|
|
|
unregister_rfkill_notifier(&rfkill_blocking_uevent_nb);
|
2007-05-07 15:34:20 +08:00
|
|
|
class_unregister(&rfkill_class);
|
|
|
|
}
|
|
|
|
|
2007-10-28 21:39:02 +08:00
|
|
|
subsys_initcall(rfkill_init);
|
2007-05-07 15:34:20 +08:00
|
|
|
module_exit(rfkill_exit);
|