rfkill: preserve state across suspend
[safe/jmp/linux-2.6] / net / rfkill / rfkill.c
index d573579..5ad411d 100644 (file)
@@ -51,6 +51,7 @@ struct rfkill_gsw_state {
 
 static struct rfkill_gsw_state rfkill_global_states[RFKILL_TYPE_MAX];
 static unsigned long rfkill_states_lockdflt[BITS_TO_LONGS(RFKILL_TYPE_MAX)];
+static bool rfkill_epo_lock_active;
 
 static BLOCKING_NOTIFIER_HEAD(rfkill_notifier_list);
 
@@ -125,6 +126,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 +219,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;
 }
@@ -265,11 +265,14 @@ static void __rfkill_switch_all(const enum rfkill_type type,
  *
  * Acquires rfkill_global_mutex and calls __rfkill_switch_all(@type, @state).
  * Please refer to __rfkill_switch_all() for details.
+ *
+ * Does nothing if the EPO lock is active.
  */
 void rfkill_switch_all(enum rfkill_type type, enum rfkill_state state)
 {
        mutex_lock(&rfkill_global_mutex);
-       __rfkill_switch_all(type, state);
+       if (!rfkill_epo_lock_active)
+               __rfkill_switch_all(type, state);
        mutex_unlock(&rfkill_global_mutex);
 }
 EXPORT_SYMBOL(rfkill_switch_all);
@@ -290,6 +293,7 @@ void rfkill_epo(void)
 
        mutex_lock(&rfkill_global_mutex);
 
+       rfkill_epo_lock_active = true;
        list_for_each_entry(rfkill, &rfkill_list, node) {
                mutex_lock(&rfkill->mutex);
                rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1);
@@ -318,6 +322,7 @@ void rfkill_restore_states(void)
 
        mutex_lock(&rfkill_global_mutex);
 
+       rfkill_epo_lock_active = false;
        for (i = 0; i < RFKILL_TYPE_MAX; i++)
                __rfkill_switch_all(i, rfkill_global_states[i].default_state);
        mutex_unlock(&rfkill_global_mutex);
@@ -325,6 +330,48 @@ void rfkill_restore_states(void)
 EXPORT_SYMBOL_GPL(rfkill_restore_states);
 
 /**
+ * rfkill_remove_epo_lock - unlock state changes
+ *
+ * Used by rfkill-input manually unlock state changes, when
+ * the EPO switch is deactivated.
+ */
+void rfkill_remove_epo_lock(void)
+{
+       mutex_lock(&rfkill_global_mutex);
+       rfkill_epo_lock_active = false;
+       mutex_unlock(&rfkill_global_mutex);
+}
+EXPORT_SYMBOL_GPL(rfkill_remove_epo_lock);
+
+/**
+ * rfkill_is_epo_lock_active - returns true EPO is active
+ *
+ * Returns 0 (false) if there is NOT an active EPO contidion,
+ * and 1 (true) if there is an active EPO contition, which
+ * locks all radios in one of the BLOCKED states.
+ *
+ * Can be called in atomic context.
+ */
+bool rfkill_is_epo_lock_active(void)
+{
+       return rfkill_epo_lock_active;
+}
+EXPORT_SYMBOL_GPL(rfkill_is_epo_lock_active);
+
+/**
+ * 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.
@@ -432,9 +479,15 @@ 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 = rfkill_toggle_radio(rfkill, state, 0);
+       error = mutex_lock_killable(&rfkill->mutex);
+       if (error)
+               return error;
+
+       if (!rfkill_epo_lock_active)
+               error = rfkill_toggle_radio(rfkill, state, 0);
+       else
+               error = -EPERM;
+
        mutex_unlock(&rfkill->mutex);
 
        return error ? error : count;
@@ -473,12 +526,12 @@ 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_global_mutex);
+       error = mutex_lock_killable(&rfkill_global_mutex);
        if (error)
                return error;
 
        if (rfkill->user_claim != claim) {
-               if (!claim) {
+               if (!claim && !rfkill_epo_lock_active) {
                        mutex_lock(&rfkill->mutex);
                        rfkill_toggle_radio(rfkill,
                                        rfkill_global_states[rfkill->type].current_state,
@@ -514,19 +567,12 @@ 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;
-       }
+
+       /* store state for the resume handler */
+       rfkill->state_for_resume = rfkill->state;
 
        return 0;
 }
@@ -540,8 +586,17 @@ static int rfkill_resume(struct device *dev)
 
                dev->power.power_state.event = PM_EVENT_ON;
 
-               /* restore radio state AND notify everybody */
-               rfkill_toggle_radio(rfkill, rfkill->state, 1);
+               /*
+                * If we are under EPO, kick transmitter offline,
+                * otherwise restore to pre-suspend state.
+                *
+                * Issue a notification in any case
+                */
+               rfkill_toggle_radio(rfkill,
+                               rfkill_epo_lock_active ?
+                                       RFKILL_STATE_SOFT_BLOCKED :
+                                       rfkill->state_for_resume,
+                               1);
 
                mutex_unlock(&rfkill->mutex);
        }
@@ -616,7 +671,7 @@ static int rfkill_check_duplicity(const struct rfkill *rfkill)
        }
 
        /* 0: first switch of its kind */
-       return test_bit(rfkill->type, seen);
+       return (test_bit(rfkill->type, seen)) ? 1 : 0;
 }
 
 static int rfkill_add_switch(struct rfkill *rfkill)
@@ -724,7 +779,7 @@ static void rfkill_led_trigger_register(struct rfkill *rfkill)
        int error;
 
        if (!rfkill->led_trigger.name)
-               rfkill->led_trigger.name = rfkill->dev.bus_id;
+               rfkill->led_trigger.name = dev_name(&rfkill->dev);
        if (!rfkill->led_trigger.activate)
                rfkill->led_trigger.activate = rfkill_led_trigger_activate;
        error = led_trigger_register(&rfkill->led_trigger);
@@ -765,8 +820,7 @@ int __must_check rfkill_register(struct rfkill *rfkill)
                        "badly initialized rfkill struct\n"))
                return -EINVAL;
 
-       snprintf(dev->bus_id, sizeof(dev->bus_id),
-                "rfkill%ld", (long)atomic_inc_return(&rfkill_no) - 1);
+       dev_set_name(dev, "rfkill%ld", (long)atomic_inc_return(&rfkill_no) - 1);
 
        rfkill_led_trigger_register(rfkill);
 
@@ -846,6 +900,7 @@ int rfkill_set_default(enum rfkill_type type, enum rfkill_state state)
 
        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;