===================================================================
@@ -9,6 +9,7 @@
#include <linux/device.h>
#include <linux/slab.h>
#include <linux/sched.h>
+#include <linux/ktime.h>
#include <linux/capability.h>
#include <linux/suspend.h>
#include <linux/pm.h>
@@ -28,6 +29,12 @@ static unsigned long events_in_progress;
static DEFINE_SPINLOCK(events_lock);
+static void pm_wakeup_timer_fn(unsigned long data);
+
+static DEFINE_TIMER(events_timer, pm_wakeup_timer_fn, 0, 0);
+static unsigned long events_timer_expires;
+static unsigned long delayed_count;
+
/*
* 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
@@ -103,17 +110,23 @@ void pm_relax(void)
}
/**
- * pm_wakeup_work_fn - Deferred closing of a wakeup event.
+ * pm_wakeup_timer_fn - Deferred closing of a wakeup event.
*
* Execute pm_relax() for a wakeup event detected in the past and free the
* work item object used for queuing up the work.
*/
-static void pm_wakeup_work_fn(struct work_struct *work)
+static void pm_wakeup_timer_fn(unsigned long data)
{
- struct delayed_work *dwork = to_delayed_work(work);
+ unsigned long flags;
- pm_relax();
- kfree(dwork);
+ spin_lock_irqsave(&events_lock, flags);
+ if (events_timer_expires && time_after(jiffies, events_timer_expires)) {
+ events_in_progress -= delayed_count;
+ event_count += delayed_count;
+ delayed_count = 0;
+ events_timer_expires = 0;
+ }
+ spin_unlock_irqrestore(&events_lock, flags);
}
/**
@@ -132,19 +145,31 @@ static void pm_wakeup_work_fn(struct wor
void pm_wakeup_event(struct device *dev, unsigned int msec)
{
unsigned long flags;
- struct delayed_work *dwork;
-
- dwork = msec ? kzalloc(sizeof(*dwork), GFP_ATOMIC) : NULL;
spin_lock_irqsave(&events_lock, flags);
if (dev)
dev->power.wakeup_count++;
- if (dwork) {
- INIT_DELAYED_WORK(dwork, pm_wakeup_work_fn);
- schedule_delayed_work(dwork, msecs_to_jiffies(msec));
+ if (msec) {
+ ktime_t kt;
+ struct timespec ts;
+ unsigned long expires;
+
+ kt = ktime_get();
+ kt = ktime_add_ns(kt, msec * NSEC_PER_MSEC);
+ ts = ktime_to_timespec(kt);
+ expires = timespec_to_jiffies(&ts);
+ if (!expires)
+ expires = 1;
+
+ if (!events_timer_expires
+ || time_after(expires, events_timer_expires)) {
+ mod_timer(&events_timer, expires);
+ events_timer_expires = expires;
+ }
events_in_progress++;
+ delayed_count++;
} else {
event_count++;
}