Merge branch 'pm-wakeirq'
[linux-2.6-block.git] / drivers / base / power / wakeup.c
index 15d27d782dc1f3243faf536ec7dc8c88e1c1e13a..51f15bc15774250a203b46c44b5682b36f450f1d 100644 (file)
@@ -57,6 +57,11 @@ static LIST_HEAD(wakeup_sources);
 
 static DECLARE_WAIT_QUEUE_HEAD(wakeup_count_wait_queue);
 
+static struct wakeup_source deleted_ws = {
+       .name = "deleted",
+       .lock =  __SPIN_LOCK_UNLOCKED(deleted_ws.lock),
+};
+
 /**
  * wakeup_source_prepare - Prepare a new wakeup source for initialization.
  * @ws: Wakeup source to prepare.
@@ -108,6 +113,34 @@ void wakeup_source_drop(struct wakeup_source *ws)
 }
 EXPORT_SYMBOL_GPL(wakeup_source_drop);
 
+/*
+ * Record wakeup_source statistics being deleted into a dummy wakeup_source.
+ */
+static void wakeup_source_record(struct wakeup_source *ws)
+{
+       unsigned long flags;
+
+       spin_lock_irqsave(&deleted_ws.lock, flags);
+
+       if (ws->event_count) {
+               deleted_ws.total_time =
+                       ktime_add(deleted_ws.total_time, ws->total_time);
+               deleted_ws.prevent_sleep_time =
+                       ktime_add(deleted_ws.prevent_sleep_time,
+                                 ws->prevent_sleep_time);
+               deleted_ws.max_time =
+                       ktime_compare(deleted_ws.max_time, ws->max_time) > 0 ?
+                               deleted_ws.max_time : ws->max_time;
+               deleted_ws.event_count += ws->event_count;
+               deleted_ws.active_count += ws->active_count;
+               deleted_ws.relax_count += ws->relax_count;
+               deleted_ws.expire_count += ws->expire_count;
+               deleted_ws.wakeup_count += ws->wakeup_count;
+       }
+
+       spin_unlock_irqrestore(&deleted_ws.lock, flags);
+}
+
 /**
  * wakeup_source_destroy - Destroy a struct wakeup_source object.
  * @ws: Wakeup source to destroy.
@@ -120,6 +153,7 @@ void wakeup_source_destroy(struct wakeup_source *ws)
                return;
 
        wakeup_source_drop(ws);
+       wakeup_source_record(ws);
        kfree(ws->name);
        kfree(ws);
 }
@@ -432,6 +466,20 @@ int device_set_wakeup_enable(struct device *dev, bool enable)
 }
 EXPORT_SYMBOL_GPL(device_set_wakeup_enable);
 
+/**
+ * wakeup_source_not_registered - validate the given wakeup source.
+ * @ws: Wakeup source to be validated.
+ */
+static bool wakeup_source_not_registered(struct wakeup_source *ws)
+{
+       /*
+        * Use timer struct to check if the given source is initialized
+        * by wakeup_source_add.
+        */
+       return ws->timer.function != pm_wakeup_timer_fn ||
+                  ws->timer.data != (unsigned long)ws;
+}
+
 /*
  * The functions below use the observation that each wakeup event starts a
  * period in which the system should not be suspended.  The moment this period
@@ -472,6 +520,10 @@ static void wakeup_source_activate(struct wakeup_source *ws)
 {
        unsigned int cec;
 
+       if (WARN_ONCE(wakeup_source_not_registered(ws),
+                       "unregistered wakeup source\n"))
+               return;
+
        /*
         * active wakeup source should bring the system
         * out of PM_SUSPEND_FREEZE state
@@ -975,6 +1027,8 @@ static int wakeup_sources_stats_show(struct seq_file *m, void *unused)
                print_wakeup_source_stats(m, ws);
        rcu_read_unlock();
 
+       print_wakeup_source_stats(m, &deleted_ws);
+
        return 0;
 }