RFKILL: set the status of the leds on activation.
[safe/jmp/linux-2.6] / net / rfkill / rfkill.c
index 7d07175..d2d4565 100644 (file)
@@ -39,7 +39,7 @@ MODULE_LICENSE("GPL");
 static LIST_HEAD(rfkill_list); /* list of registered rf switches */
 static DEFINE_MUTEX(rfkill_mutex);
 
-static unsigned int rfkill_default_state = RFKILL_STATE_ON;
+static unsigned int rfkill_default_state = RFKILL_STATE_UNBLOCKED;
 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");
@@ -98,13 +98,23 @@ static void rfkill_led_trigger(struct rfkill *rfkill,
 
        if (!led->name)
                return;
-       if (state == RFKILL_STATE_OFF)
+       if (state != RFKILL_STATE_UNBLOCKED)
                led_trigger_event(led, LED_OFF);
        else
                led_trigger_event(led, LED_FULL);
 #endif /* CONFIG_RFKILL_LEDS */
 }
 
+#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 */
+
 static void notify_rfkill_state_change(struct rfkill *rfkill)
 {
        blocking_notifier_call_chain(&rfkill_notifier_list,
@@ -128,6 +138,29 @@ static void update_rfkill_state(struct rfkill *rfkill)
        }
 }
 
+/**
+ * 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
+ *
+ * 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.
+ *
+ * Note that the @force parameter cannot override a (possibly cached)
+ * state of RFKILL_STATE_HARD_BLOCKED.  Any device making use of
+ * 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.
+ *
+ * Caller must have acquired rfkill->mutex.
+ */
 static int rfkill_toggle_radio(struct rfkill *rfkill,
                                enum rfkill_state state,
                                int force)
@@ -141,9 +174,28 @@ static int rfkill_toggle_radio(struct rfkill *rfkill,
            !rfkill->get_state(rfkill->data, &newstate))
                rfkill->state = newstate;
 
+       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;
+       }
+
        if (force || state != rfkill->state) {
                retval = rfkill->toggle_radio(rfkill->data, state);
-               if (!retval)
+               /* never allow a HARD->SOFT downgrade! */
+               if (!retval && rfkill->state != RFKILL_STATE_HARD_BLOCKED)
                        rfkill->state = state;
        }
 
@@ -157,12 +209,12 @@ static int rfkill_toggle_radio(struct rfkill *rfkill,
 
 /**
  * rfkill_switch_all - Toggle state of all switches of given type
- * @type: type of interfaces to be affeceted
+ * @type: type of interfaces to be affected
  * @state: the new state
  *
- * This function toggles state of all switches of given type unless
- * a specific switch is claimed by userspace in which case it is
- * left alone.
+ * 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).
  */
 void rfkill_switch_all(enum rfkill_type type, enum rfkill_state state)
 {
@@ -173,8 +225,11 @@ void rfkill_switch_all(enum rfkill_type type, enum rfkill_state state)
        rfkill_states[type] = state;
 
        list_for_each_entry(rfkill, &rfkill_list, node) {
-               if ((!rfkill->user_claim) && (rfkill->type == type))
+               if ((!rfkill->user_claim) && (rfkill->type == type)) {
+                       mutex_lock(&rfkill->mutex);
                        rfkill_toggle_radio(rfkill, state, 0);
+                       mutex_unlock(&rfkill->mutex);
+               }
        }
 
        mutex_unlock(&rfkill_mutex);
@@ -184,8 +239,8 @@ EXPORT_SYMBOL(rfkill_switch_all);
 /**
  * rfkill_epo - emergency power off all transmitters
  *
- * This kicks all rfkill devices to RFKILL_STATE_OFF, ignoring
- * everything in its path but rfkill_mutex.
+ * This kicks all rfkill devices to RFKILL_STATE_SOFT_BLOCKED, ignoring
+ * everything in its path but rfkill_mutex and rfkill->mutex.
  */
 void rfkill_epo(void)
 {
@@ -193,7 +248,9 @@ void rfkill_epo(void)
 
        mutex_lock(&rfkill_mutex);
        list_for_each_entry(rfkill, &rfkill_list, node) {
-               rfkill_toggle_radio(rfkill, RFKILL_STATE_OFF, 1);
+               mutex_lock(&rfkill->mutex);
+               rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1);
+               mutex_unlock(&rfkill->mutex);
        }
        mutex_unlock(&rfkill_mutex);
 }
@@ -209,14 +266,20 @@ EXPORT_SYMBOL_GPL(rfkill_epo);
  * a notification by the firmware/hardware of the current *real*
  * state of the radio rfkill switch.
  *
- * It may not be called from an atomic context.
+ * 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.
  */
 int rfkill_force_state(struct rfkill *rfkill, enum rfkill_state state)
 {
        enum rfkill_state oldstate;
 
-       if (state != RFKILL_STATE_OFF &&
-           state != RFKILL_STATE_ON)
+       if (state != RFKILL_STATE_SOFT_BLOCKED &&
+           state != RFKILL_STATE_UNBLOCKED &&
+           state != RFKILL_STATE_HARD_BLOCKED)
                return -EINVAL;
 
        mutex_lock(&rfkill->mutex);
@@ -290,11 +353,14 @@ static ssize_t rfkill_state_store(struct device *dev,
        if (!capable(CAP_NET_ADMIN))
                return -EPERM;
 
+       /* RFKILL_STATE_HARD_BLOCKED is illegal here... */
+       if (state != RFKILL_STATE_UNBLOCKED &&
+           state != RFKILL_STATE_SOFT_BLOCKED)
+               return -EINVAL;
+
        if (mutex_lock_interruptible(&rfkill->mutex))
                return -ERESTARTSYS;
-       error = rfkill_toggle_radio(rfkill,
-                       state ? RFKILL_STATE_ON : RFKILL_STATE_OFF,
-                       0);
+       error = rfkill_toggle_radio(rfkill, state, 0);
        mutex_unlock(&rfkill->mutex);
 
        return error ? error : count;
@@ -320,6 +386,9 @@ static ssize_t rfkill_claim_store(struct device *dev,
        if (!capable(CAP_NET_ADMIN))
                return -EPERM;
 
+       if (rfkill->user_claim_unsupported)
+               return -EOPNOTSUPP;
+
        /*
         * Take the global lock to make sure the kernel is not in
         * the middle of rfkill_switch_all
@@ -328,19 +397,17 @@ static ssize_t rfkill_claim_store(struct device *dev,
        if (error)
                return error;
 
-       if (rfkill->user_claim_unsupported) {
-               error = -EOPNOTSUPP;
-               goto out_unlock;
-       }
        if (rfkill->user_claim != claim) {
-               if (!claim)
+               if (!claim) {
+                       mutex_lock(&rfkill->mutex);
                        rfkill_toggle_radio(rfkill,
                                            rfkill_states[rfkill->type],
                                            0);
+                       mutex_unlock(&rfkill->mutex);
+               }
                rfkill->user_claim = claim;
        }
 
-out_unlock:
        mutex_unlock(&rfkill_mutex);
 
        return error ? error : count;
@@ -373,7 +440,8 @@ static int rfkill_suspend(struct device *dev, pm_message_t state)
                        update_rfkill_state(rfkill);
 
                        mutex_lock(&rfkill->mutex);
-                       rfkill->toggle_radio(rfkill->data, RFKILL_STATE_OFF);
+                       rfkill->toggle_radio(rfkill->data,
+                                               RFKILL_STATE_SOFT_BLOCKED);
                        mutex_unlock(&rfkill->mutex);
                }
 
@@ -453,25 +521,26 @@ static struct class rfkill_class = {
 
 static int rfkill_add_switch(struct rfkill *rfkill)
 {
-       int error;
-
        mutex_lock(&rfkill_mutex);
 
-       error = rfkill_toggle_radio(rfkill, rfkill_states[rfkill->type], 0);
-       if (!error)
-               list_add_tail(&rfkill->node, &rfkill_list);
+       rfkill_toggle_radio(rfkill, rfkill_states[rfkill->type], 0);
+
+       list_add_tail(&rfkill->node, &rfkill_list);
 
        mutex_unlock(&rfkill_mutex);
 
-       return error;
+       return 0;
 }
 
 static void rfkill_remove_switch(struct rfkill *rfkill)
 {
        mutex_lock(&rfkill_mutex);
        list_del_init(&rfkill->node);
-       rfkill_toggle_radio(rfkill, RFKILL_STATE_OFF, 1);
        mutex_unlock(&rfkill_mutex);
+
+       mutex_lock(&rfkill->mutex);
+       rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1);
+       mutex_unlock(&rfkill->mutex);
 }
 
 /**
@@ -480,9 +549,10 @@ static void rfkill_remove_switch(struct rfkill *rfkill)
  * @type: type of the switch (RFKILL_TYPE_*)
  *
  * This function should be called by the network driver when it needs
- * rfkill structure. Once the structure is allocated the driver shoud
- * finish its initialization by setting name, private data, enable_radio
+ * rfkill structure.  Once the structure is allocated the driver should
+ * finish its initialization by setting the name, private data, enable_radio
  * and disable_radio methods and then register it with rfkill_register().
+ *
  * NOTE: If registration fails the structure shoudl be freed by calling
  * rfkill_free() otherwise rfkill_unregister() should be used.
  */
@@ -514,7 +584,7 @@ EXPORT_SYMBOL(rfkill_allocate);
  * rfkill_free - Mark rfkill structure for deletion
  * @rfkill: rfkill structure to be destroyed
  *
- * Decrements reference count of rfkill structure so it is destroyed.
+ * Decrements reference count of the rfkill structure so it is destroyed.
  * Note that rfkill_free() should _not_ be called after rfkill_unregister().
  */
 void rfkill_free(struct rfkill *rfkill)
@@ -529,7 +599,10 @@ static void rfkill_led_trigger_register(struct rfkill *rfkill)
 #ifdef CONFIG_RFKILL_LEDS
        int error;
 
-       rfkill->led_trigger.name = rfkill->dev.bus_id;
+       if (!rfkill->led_trigger.name)
+               rfkill->led_trigger.name = rfkill->dev.bus_id;
+       if (!rfkill->led_trigger.activate)
+               rfkill->led_trigger.activate = rfkill_led_trigger_activate;
        error = led_trigger_register(&rfkill->led_trigger);
        if (error)
                rfkill->led_trigger.name = NULL;
@@ -539,8 +612,10 @@ static void rfkill_led_trigger_register(struct rfkill *rfkill)
 static void rfkill_led_trigger_unregister(struct rfkill *rfkill)
 {
 #ifdef CONFIG_RFKILL_LEDS
-       if (rfkill->led_trigger.name)
+       if (rfkill->led_trigger.name) {
                led_trigger_unregister(&rfkill->led_trigger);
+               rfkill->led_trigger.name = NULL;
+       }
 #endif
 }
 
@@ -576,8 +651,8 @@ int rfkill_register(struct rfkill *rfkill)
 
        error = device_add(dev);
        if (error) {
-               rfkill_led_trigger_unregister(rfkill);
                rfkill_remove_switch(rfkill);
+               rfkill_led_trigger_unregister(rfkill);
                return error;
        }
 
@@ -610,8 +685,9 @@ static int __init rfkill_init(void)
        int error;
        int i;
 
-       if (rfkill_default_state != RFKILL_STATE_OFF &&
-           rfkill_default_state != RFKILL_STATE_ON)
+       /* RFKILL_STATE_HARD_BLOCKED is illegal here... */
+       if (rfkill_default_state != RFKILL_STATE_SOFT_BLOCKED &&
+           rfkill_default_state != RFKILL_STATE_UNBLOCKED)
                return -EINVAL;
 
        for (i = 0; i < ARRAY_SIZE(rfkill_states); i++)