rfkill: add notifier chains support
[safe/jmp/linux-2.6] / net / rfkill / rfkill.c
index 3edc585..a561e35 100644 (file)
@@ -46,6 +46,49 @@ MODULE_PARM_DESC(default_state,
 
 static enum rfkill_state rfkill_states[RFKILL_TYPE_MAX];
 
+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);
+
 
 static void rfkill_led_trigger(struct rfkill *rfkill,
                               enum rfkill_state state)
@@ -62,17 +105,51 @@ static void rfkill_led_trigger(struct rfkill *rfkill,
 #endif /* CONFIG_RFKILL_LEDS */
 }
 
+static void notify_rfkill_state_change(struct rfkill *rfkill)
+{
+       blocking_notifier_call_chain(&rfkill_notifier_list,
+                       RFKILL_STATE_CHANGED,
+                       rfkill);
+}
+
+static void update_rfkill_state(struct rfkill *rfkill)
+{
+       enum rfkill_state newstate, oldstate;
+
+       if (rfkill->get_state) {
+               mutex_lock(&rfkill->mutex);
+               if (!rfkill->get_state(rfkill->data, &newstate)) {
+                       oldstate = rfkill->state;
+                       rfkill->state = newstate;
+                       if (oldstate != newstate)
+                               notify_rfkill_state_change(rfkill);
+               }
+               mutex_unlock(&rfkill->mutex);
+       }
+}
+
 static int rfkill_toggle_radio(struct rfkill *rfkill,
-                               enum rfkill_state state)
+                               enum rfkill_state state,
+                               int force)
 {
        int retval = 0;
+       enum rfkill_state oldstate, newstate;
+
+       oldstate = rfkill->state;
 
-       if (state != rfkill->state) {
+       if (rfkill->get_state && !force &&
+           !rfkill->get_state(rfkill->data, &newstate))
+               rfkill->state = newstate;
+
+       if (force || state != rfkill->state) {
                retval = rfkill->toggle_radio(rfkill->data, state);
-               if (!retval) {
+               if (!retval)
                        rfkill->state = state;
-                       rfkill_led_trigger(rfkill, state);
-               }
+       }
+
+       if (force || rfkill->state != oldstate) {
+               rfkill_led_trigger(rfkill, rfkill->state);
+               notify_rfkill_state_change(rfkill);
        }
 
        return retval;
@@ -87,7 +164,6 @@ static int rfkill_toggle_radio(struct rfkill *rfkill,
  * a specific switch is claimed by userspace in which case it is
  * left alone.
  */
-
 void rfkill_switch_all(enum rfkill_type type, enum rfkill_state state)
 {
        struct rfkill *rfkill;
@@ -98,13 +174,47 @@ void rfkill_switch_all(enum rfkill_type type, enum rfkill_state state)
 
        list_for_each_entry(rfkill, &rfkill_list, node) {
                if ((!rfkill->user_claim) && (rfkill->type == type))
-                       rfkill_toggle_radio(rfkill, state);
+                       rfkill_toggle_radio(rfkill, state, 0);
        }
 
        mutex_unlock(&rfkill_mutex);
 }
 EXPORT_SYMBOL(rfkill_switch_all);
 
+/**
+ * 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.
+ *
+ * It 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)
+               return -EINVAL;
+
+       mutex_lock(&rfkill->mutex);
+
+       oldstate = rfkill->state;
+       rfkill->state = state;
+
+       if (state != oldstate)
+               notify_rfkill_state_change(rfkill);
+
+       mutex_unlock(&rfkill->mutex);
+
+       return 0;
+}
+EXPORT_SYMBOL(rfkill_force_state);
+
 static ssize_t rfkill_name_show(struct device *dev,
                                struct device_attribute *attr,
                                char *buf)
@@ -134,6 +244,9 @@ static ssize_t rfkill_type_show(struct device *dev,
        case RFKILL_TYPE_WIMAX:
                type = "wimax";
                break;
+       case RFKILL_TYPE_WWAN:
+               type = "wwan";
+               break;
        default:
                BUG();
        }
@@ -147,6 +260,7 @@ static ssize_t rfkill_state_show(struct device *dev,
 {
        struct rfkill *rfkill = to_rfkill(dev);
 
+       update_rfkill_state(rfkill);
        return sprintf(buf, "%d\n", rfkill->state);
 }
 
@@ -164,7 +278,8 @@ static ssize_t rfkill_state_store(struct device *dev,
        if (mutex_lock_interruptible(&rfkill->mutex))
                return -ERESTARTSYS;
        error = rfkill_toggle_radio(rfkill,
-                       state ? RFKILL_STATE_ON : RFKILL_STATE_OFF);
+                       state ? RFKILL_STATE_ON : RFKILL_STATE_OFF,
+                       0);
        mutex_unlock(&rfkill->mutex);
 
        return error ? error : count;
@@ -205,7 +320,8 @@ static ssize_t rfkill_claim_store(struct device *dev,
        if (rfkill->user_claim != claim) {
                if (!claim)
                        rfkill_toggle_radio(rfkill,
-                                           rfkill_states[rfkill->type]);
+                                           rfkill_states[rfkill->type],
+                                           0);
                rfkill->user_claim = claim;
        }
 
@@ -238,12 +354,11 @@ static int rfkill_suspend(struct device *dev, pm_message_t state)
 
        if (dev->power.power_state.event != state.event) {
                if (state.event & PM_EVENT_SLEEP) {
-                       mutex_lock(&rfkill->mutex);
-
-                       if (rfkill->state == RFKILL_STATE_ON)
-                               rfkill->toggle_radio(rfkill->data,
-                                                    RFKILL_STATE_OFF);
+                       /* Stop transmitter, keep state, no notifies */
+                       update_rfkill_state(rfkill);
 
+                       mutex_lock(&rfkill->mutex);
+                       rfkill->toggle_radio(rfkill->data, RFKILL_STATE_OFF);
                        mutex_unlock(&rfkill->mutex);
                }
 
@@ -260,8 +375,8 @@ static int rfkill_resume(struct device *dev)
        if (dev->power.power_state.event != PM_EVENT_ON) {
                mutex_lock(&rfkill->mutex);
 
-               if (rfkill->state == RFKILL_STATE_ON)
-                       rfkill->toggle_radio(rfkill->data, RFKILL_STATE_ON);
+               /* restore radio state AND notify everybody */
+               rfkill_toggle_radio(rfkill, rfkill->state, 1);
 
                mutex_unlock(&rfkill->mutex);
        }
@@ -288,7 +403,7 @@ static int rfkill_add_switch(struct rfkill *rfkill)
 
        mutex_lock(&rfkill_mutex);
 
-       error = rfkill_toggle_radio(rfkill, rfkill_states[rfkill->type]);
+       error = rfkill_toggle_radio(rfkill, rfkill_states[rfkill->type], 0);
        if (!error)
                list_add_tail(&rfkill->node, &rfkill_list);
 
@@ -301,7 +416,7 @@ static void rfkill_remove_switch(struct rfkill *rfkill)
 {
        mutex_lock(&rfkill_mutex);
        list_del_init(&rfkill->node);
-       rfkill_toggle_radio(rfkill, RFKILL_STATE_OFF);
+       rfkill_toggle_radio(rfkill, RFKILL_STATE_OFF, 1);
        mutex_unlock(&rfkill_mutex);
 }