rfkill: fix minor typo in kernel doc
[safe/jmp/linux-2.6] / net / rfkill / rfkill.c
index 637a9f0..f95081a 100644 (file)
 #include <linux/mutex.h>
 #include <linux/rfkill.h>
 
+/* Get declaration of rfkill_switch_all() to shut up sparse. */
+#include "rfkill-input.h"
+
+
 MODULE_AUTHOR("Ivo van Doorn <IvDoorn@gmail.com>");
 MODULE_VERSION("1.0");
 MODULE_DESCRIPTION("RF switch support");
@@ -37,22 +41,35 @@ static DEFINE_MUTEX(rfkill_mutex);
 
 static enum rfkill_state rfkill_states[RFKILL_TYPE_MAX];
 
+
+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;
+       if (state == RFKILL_STATE_OFF)
+               led_trigger_event(led, LED_OFF);
+       else
+               led_trigger_event(led, LED_FULL);
+#endif /* CONFIG_RFKILL_LEDS */
+}
+
 static int rfkill_toggle_radio(struct rfkill *rfkill,
                                enum rfkill_state state)
 {
-       int retval;
-
-       retval = mutex_lock_interruptible(&rfkill->mutex);
-       if (retval)
-               return retval;
+       int retval = 0;
 
        if (state != rfkill->state) {
                retval = rfkill->toggle_radio(rfkill->data, state);
-               if (!retval)
+               if (!retval) {
                        rfkill->state = state;
+                       rfkill_led_trigger(rfkill, state);
+               }
        }
 
-       mutex_unlock(&rfkill->mutex);
        return retval;
 }
 
@@ -75,7 +92,7 @@ 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)
+               if ((!rfkill->user_claim) && (rfkill->type == type))
                        rfkill_toggle_radio(rfkill, state);
        }
 
@@ -109,6 +126,9 @@ static ssize_t rfkill_type_show(struct device *dev,
        case RFKILL_TYPE_UWB:
                type = "ultrawideband";
                break;
+       case RFKILL_TYPE_WIMAX:
+               type = "wimax";
+               break;
        default:
                BUG();
        }
@@ -136,12 +156,13 @@ static ssize_t rfkill_state_store(struct device *dev,
        if (!capable(CAP_NET_ADMIN))
                return -EPERM;
 
+       if (mutex_lock_interruptible(&rfkill->mutex))
+               return -ERESTARTSYS;
        error = rfkill_toggle_radio(rfkill,
                        state ? RFKILL_STATE_ON : RFKILL_STATE_OFF);
-       if (error)
-               return error;
+       mutex_unlock(&rfkill->mutex);
 
-       return count;
+       return error ? error : count;
 }
 
 static ssize_t rfkill_claim_show(struct device *dev,
@@ -172,6 +193,10 @@ 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)
                        rfkill_toggle_radio(rfkill,
@@ -179,9 +204,10 @@ static ssize_t rfkill_claim_store(struct device *dev,
                rfkill->user_claim = claim;
        }
 
+out_unlock:
        mutex_unlock(&rfkill_mutex);
 
-       return count;
+       return error ? error : count;
 }
 
 static struct device_attribute rfkill_dev_attrs[] = {
@@ -206,7 +232,7 @@ 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_SUSPEND) {
+               if (state.event & PM_EVENT_SLEEP) {
                        mutex_lock(&rfkill->mutex);
 
                        if (rfkill->state == RFKILL_STATE_ON)
@@ -253,21 +279,17 @@ static struct class rfkill_class = {
 
 static int rfkill_add_switch(struct rfkill *rfkill)
 {
-       int retval;
-
-       retval = mutex_lock_interruptible(&rfkill_mutex);
-       if (retval)
-               return retval;
+       int error;
 
-       retval = rfkill_toggle_radio(rfkill, rfkill_states[rfkill->type]);
-       if (retval)
-               goto out;
+       mutex_lock(&rfkill_mutex);
 
-       list_add_tail(&rfkill->node, &rfkill_list);
+       error = rfkill_toggle_radio(rfkill, rfkill_states[rfkill->type]);
+       if (!error)
+               list_add_tail(&rfkill->node, &rfkill_list);
 
- out:
        mutex_unlock(&rfkill_mutex);
-       return retval;
+
+       return error;
 }
 
 static void rfkill_remove_switch(struct rfkill *rfkill)
@@ -318,7 +340,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 destoryed.
+ * Decrements reference count of rfkill structure so it is destroyed.
  * Note that rfkill_free() should _not_ be called after rfkill_unregister().
  */
 void rfkill_free(struct rfkill *rfkill)
@@ -328,6 +350,26 @@ void rfkill_free(struct rfkill *rfkill)
 }
 EXPORT_SYMBOL(rfkill_free);
 
+static void rfkill_led_trigger_register(struct rfkill *rfkill)
+{
+#ifdef CONFIG_RFKILL_LEDS
+       int error;
+
+       rfkill->led_trigger.name = rfkill->dev.bus_id;
+       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
+       if (rfkill->led_trigger.name)
+               led_trigger_unregister(&rfkill->led_trigger);
+#endif
+}
+
 /**
  * rfkill_register - Register a rfkill structure.
  * @rfkill: rfkill structure to be registered
@@ -344,16 +386,23 @@ int rfkill_register(struct rfkill *rfkill)
 
        if (!rfkill->toggle_radio)
                return -EINVAL;
-
-       error = rfkill_add_switch(rfkill);
-       if (error)
-               return error;
+       if (rfkill->type >= RFKILL_TYPE_MAX)
+               return -EINVAL;
 
        snprintf(dev->bus_id, sizeof(dev->bus_id),
                 "rfkill%ld", (long)atomic_inc_return(&rfkill_no) - 1);
 
+       rfkill_led_trigger_register(rfkill);
+
+       error = rfkill_add_switch(rfkill);
+       if (error) {
+               rfkill_led_trigger_unregister(rfkill);
+               return error;
+       }
+
        error = device_add(dev);
        if (error) {
+               rfkill_led_trigger_unregister(rfkill);
                rfkill_remove_switch(rfkill);
                return error;
        }
@@ -363,7 +412,7 @@ int rfkill_register(struct rfkill *rfkill)
 EXPORT_SYMBOL(rfkill_register);
 
 /**
- * rfkill_unregister - Uegister a rfkill structure.
+ * rfkill_unregister - Unregister a rfkill structure.
  * @rfkill: rfkill structure to be unregistered
  *
  * This function should be called by the network driver during device
@@ -374,6 +423,7 @@ void rfkill_unregister(struct rfkill *rfkill)
 {
        device_del(&rfkill->dev);
        rfkill_remove_switch(rfkill);
+       rfkill_led_trigger_unregister(rfkill);
        put_device(&rfkill->dev);
 }
 EXPORT_SYMBOL(rfkill_unregister);
@@ -403,5 +453,5 @@ static void __exit rfkill_exit(void)
        class_unregister(&rfkill_class);
 }
 
-module_init(rfkill_init);
+subsys_initcall(rfkill_init);
 module_exit(rfkill_exit);