rfkill: export global states to rfkill-input
[linux-2.6.git] / net / rfkill / rfkill.c
index 910699c..fdf87d2 100644 (file)
@@ -37,7 +37,7 @@ MODULE_DESCRIPTION("RF switch support");
 MODULE_LICENSE("GPL");
 
 static LIST_HEAD(rfkill_list); /* list of registered rf switches */
-static DEFINE_MUTEX(rfkill_mutex);
+static DEFINE_MUTEX(rfkill_global_mutex);
 
 static unsigned int rfkill_default_state = RFKILL_STATE_UNBLOCKED;
 module_param_named(default_state, rfkill_default_state, uint, 0444);
@@ -125,6 +125,7 @@ static void rfkill_led_trigger_activate(struct led_classdev *led)
 
 static void notify_rfkill_state_change(struct rfkill *rfkill)
 {
+       rfkill_led_trigger(rfkill, rfkill->state);
        blocking_notifier_call_chain(&rfkill_notifier_list,
                        RFKILL_STATE_CHANGED,
                        rfkill);
@@ -217,10 +218,8 @@ static int rfkill_toggle_radio(struct rfkill *rfkill,
                        rfkill->state = state;
        }
 
-       if (force || rfkill->state != oldstate) {
-               rfkill_led_trigger(rfkill, rfkill->state);
+       if (force || rfkill->state != oldstate)
                notify_rfkill_state_change(rfkill);
-       }
 
        return retval;
 }
@@ -234,7 +233,7 @@ static int rfkill_toggle_radio(struct rfkill *rfkill,
  * unless a specific switch is claimed by userspace (in which case,
  * that switch is left alone) or suspended.
  *
- * Caller must have acquired rfkill_mutex.
+ * Caller must have acquired rfkill_global_mutex.
  */
 static void __rfkill_switch_all(const enum rfkill_type type,
                                const enum rfkill_state state)
@@ -263,14 +262,14 @@ static void __rfkill_switch_all(const enum rfkill_type type,
  * @type: type of interfaces to be affected
  * @state: the new state
  *
- * Acquires rfkill_mutex and calls __rfkill_switch_all(@type, @state).
+ * Acquires rfkill_global_mutex and calls __rfkill_switch_all(@type, @state).
  * Please refer to __rfkill_switch_all() for details.
  */
 void rfkill_switch_all(enum rfkill_type type, enum rfkill_state state)
 {
-       mutex_lock(&rfkill_mutex);
+       mutex_lock(&rfkill_global_mutex);
        __rfkill_switch_all(type, state);
-       mutex_unlock(&rfkill_mutex);
+       mutex_unlock(&rfkill_global_mutex);
 }
 EXPORT_SYMBOL(rfkill_switch_all);
 
@@ -278,7 +277,7 @@ EXPORT_SYMBOL(rfkill_switch_all);
  * rfkill_epo - emergency power off all transmitters
  *
  * This kicks all non-suspended rfkill devices to RFKILL_STATE_SOFT_BLOCKED,
- * ignoring everything in its path but rfkill_mutex and rfkill->mutex.
+ * ignoring everything in its path but rfkill_global_mutex and rfkill->mutex.
  *
  * The global state before the EPO is saved and can be restored later
  * using rfkill_restore_states().
@@ -288,7 +287,8 @@ void rfkill_epo(void)
        struct rfkill *rfkill;
        int i;
 
-       mutex_lock(&rfkill_mutex);
+       mutex_lock(&rfkill_global_mutex);
+
        list_for_each_entry(rfkill, &rfkill_list, node) {
                mutex_lock(&rfkill->mutex);
                rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1);
@@ -300,7 +300,7 @@ void rfkill_epo(void)
                rfkill_global_states[i].current_state =
                                RFKILL_STATE_SOFT_BLOCKED;
        }
-       mutex_unlock(&rfkill_mutex);
+       mutex_unlock(&rfkill_global_mutex);
 }
 EXPORT_SYMBOL_GPL(rfkill_epo);
 
@@ -315,14 +315,28 @@ void rfkill_restore_states(void)
 {
        int i;
 
-       mutex_lock(&rfkill_mutex);
+       mutex_lock(&rfkill_global_mutex);
+
        for (i = 0; i < RFKILL_TYPE_MAX; i++)
                __rfkill_switch_all(i, rfkill_global_states[i].default_state);
-       mutex_unlock(&rfkill_mutex);
+       mutex_unlock(&rfkill_global_mutex);
 }
 EXPORT_SYMBOL_GPL(rfkill_restore_states);
 
 /**
+ * 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);
+
+/**
  * 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.
@@ -430,8 +444,9 @@ static ssize_t rfkill_state_store(struct device *dev,
            state != RFKILL_STATE_SOFT_BLOCKED)
                return -EINVAL;
 
-       if (mutex_lock_interruptible(&rfkill->mutex))
-               return -ERESTARTSYS;
+       error = mutex_lock_killable(&rfkill->mutex);
+       if (error)
+               return error;
        error = rfkill_toggle_radio(rfkill, state, 0);
        mutex_unlock(&rfkill->mutex);
 
@@ -471,7 +486,7 @@ static ssize_t rfkill_claim_store(struct device *dev,
         * Take the global lock to make sure the kernel is not in
         * the middle of rfkill_switch_all
         */
-       error = mutex_lock_interruptible(&rfkill_mutex);
+       error = mutex_lock_killable(&rfkill_global_mutex);
        if (error)
                return error;
 
@@ -486,7 +501,7 @@ static ssize_t rfkill_claim_store(struct device *dev,
                rfkill->user_claim = claim;
        }
 
-       mutex_unlock(&rfkill_mutex);
+       mutex_unlock(&rfkill_global_mutex);
 
        return error ? error : count;
 }
@@ -510,21 +525,9 @@ static void rfkill_release(struct device *dev)
 #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) {
-               if (state.event & PM_EVENT_SLEEP) {
-                       /* Stop transmitter, keep state, no notifies */
-                       update_rfkill_state(rfkill);
-
-                       mutex_lock(&rfkill->mutex);
-                       rfkill->toggle_radio(rfkill->data,
-                                               RFKILL_STATE_SOFT_BLOCKED);
-                       mutex_unlock(&rfkill->mutex);
-               }
-
+       /* mark class device as suspended */
+       if (dev->power.power_state.event != state.event)
                dev->power.power_state = state;
-       }
 
        return 0;
 }
@@ -621,7 +624,7 @@ static int rfkill_add_switch(struct rfkill *rfkill)
 {
        int error;
 
-       mutex_lock(&rfkill_mutex);
+       mutex_lock(&rfkill_global_mutex);
 
        error = rfkill_check_duplicity(rfkill);
        if (error < 0)
@@ -642,16 +645,16 @@ static int rfkill_add_switch(struct rfkill *rfkill)
 
        error = 0;
 unlock_out:
-       mutex_unlock(&rfkill_mutex);
+       mutex_unlock(&rfkill_global_mutex);
 
        return error;
 }
 
 static void rfkill_remove_switch(struct rfkill *rfkill)
 {
-       mutex_lock(&rfkill_mutex);
+       mutex_lock(&rfkill_global_mutex);
        list_del_init(&rfkill->node);
-       mutex_unlock(&rfkill_mutex);
+       mutex_unlock(&rfkill_global_mutex);
 
        mutex_lock(&rfkill->mutex);
        rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1);
@@ -840,15 +843,16 @@ int rfkill_set_default(enum rfkill_type type, enum rfkill_state state)
                        "parameter to rfkill_set_default\n", state, type))
                return -EINVAL;
 
-       mutex_lock(&rfkill_mutex);
+       mutex_lock(&rfkill_global_mutex);
 
        if (!test_and_set_bit(type, rfkill_states_lockdflt)) {
                rfkill_global_states[type].default_state = state;
+               rfkill_global_states[type].current_state = state;
                error = 0;
        } else
                error = -EPERM;
 
-       mutex_unlock(&rfkill_mutex);
+       mutex_unlock(&rfkill_global_mutex);
        return error;
 }
 EXPORT_SYMBOL_GPL(rfkill_set_default);