mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Atsushi Nemoto <anemo@mba.ocn.ne.jp>
To: Alessandro Zummo <a.zummo@towertech.it>
Cc: linux-kernel@vger.kernel.org, akpm@osdl.org
Subject: [PATCH] RTC: rtc-dev UIE emulation
Date: Sat, 29 Apr 2006 01:16:48 +0900 (JST)	[thread overview]
Message-ID: <20060429.011648.25910123.anemo@mba.ocn.ne.jp> (raw)

Import genrtc's RTC UIE emulation (CONFIG_GEN_RTC_X) to rtc-dev driver
with slight adjustments.  This makes UIE-less chips/drivers work
better with programs doing read/poll on /dev/rtc, such as hwclock.

Signed-off-by: Atsushi Nemoto <anemo@mba.ocn.ne.jp>

diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
index 65d090d..29ca46c 100644
--- a/drivers/rtc/Kconfig
+++ b/drivers/rtc/Kconfig
@@ -73,6 +73,12 @@ config RTC_INTF_DEV
 	  This driver can also be built as a module. If so, the module
 	  will be called rtc-dev.
 
+config RTC_INTF_DEV_X
+	bool "Extended RTC operation"
+	depends on RTC_INTF_DEV
+	help
+	  Provides an emulation for RTC_UIE.
+
 comment "RTC drivers"
 	depends on RTC_CLASS
 
diff --git a/drivers/rtc/rtc-dev.c b/drivers/rtc/rtc-dev.c
index b1e3e61..0bbd181 100644
--- a/drivers/rtc/rtc-dev.c
+++ b/drivers/rtc/rtc-dev.c
@@ -48,6 +48,93 @@ static int rtc_dev_open(struct inode *in
 	return err;
 }
 
+#ifdef CONFIG_RTC_INTF_DEV_X
+/*
+ * Routine to poll RTC seconds field for change as often as possible,
+ * after first RTC_UIE use timer to reduce polling
+ */
+static void rtc_uie_task(void *data)
+{
+	struct rtc_device *rtc = data;
+	struct rtc_time tm;
+	unsigned int tmp = rtc_read_time(&rtc->class_dev, &tm) ? 0 : tm.tm_sec;
+	int num = 0;
+
+	spin_lock_irq(&rtc->irq_lock);
+	if (rtc->stop_rtc_timers) {
+		rtc->stask_active = 0;
+		spin_unlock_irq(&rtc->irq_lock);
+		return;
+	}
+
+	if (rtc->oldsecs != tmp) {
+		num = (tmp + 60 - rtc->oldsecs) % 60;
+		rtc->oldsecs = tmp;
+
+		rtc->timer_task.expires = jiffies + HZ - (HZ/10);
+		rtc->ttask_active = 1;
+		rtc->stask_active = 0;
+		add_timer(&rtc->timer_task);
+	} else if (schedule_work(&rtc->uie_task) == 0)
+		rtc->stask_active = 0;
+	spin_unlock_irq(&rtc->irq_lock);
+	if (num)
+		rtc_update_irq(&rtc->class_dev, num, RTC_UF | RTC_IRQF);
+}
+
+static void rtc_uie_timer(unsigned long data)
+{
+	struct rtc_device *rtc = (struct rtc_device *)data;
+	unsigned long flags;
+	spin_lock_irqsave(&rtc->irq_lock, flags);
+	rtc->ttask_active = 0;
+	rtc->stask_active = 1;
+	if ((schedule_work(&rtc->uie_task) == 0))
+		rtc->stask_active = 0;
+	spin_unlock_irqrestore(&rtc->irq_lock, flags);
+}
+
+static void clear_uie(struct rtc_device *rtc)
+{
+	spin_lock_irq(&rtc->irq_lock);
+	rtc->stop_rtc_timers = 1;
+	if (rtc->ttask_active) {
+		spin_unlock_irq(&rtc->irq_lock);
+		del_timer_sync(&rtc->timer_task);
+		spin_lock(&rtc->irq_lock);
+		rtc->ttask_active = 0;
+	}
+	while (rtc->stask_active) {
+		spin_unlock_irq(&rtc->irq_lock);
+		schedule();
+		spin_lock_irq(&rtc->irq_lock);
+	}
+	rtc->irq_active = 0;
+	spin_unlock_irq(&rtc->irq_lock);
+}
+
+static void set_uie(struct rtc_device *rtc)
+{
+	int start = 0;
+	spin_lock_irq(&rtc->irq_lock);
+	if (!rtc->irq_active)
+		start = rtc->irq_active = 1;
+	rtc->irq_data = 0;
+	spin_unlock_irq(&rtc->irq_lock);
+	if (start) {
+		struct rtc_time tm;
+		rtc->stop_rtc_timers = 0;
+		INIT_WORK(&rtc->uie_task, rtc_uie_task, rtc);
+		rtc->oldsecs = rtc_read_time(&rtc->class_dev, &tm) ?
+			0 : tm.tm_sec;
+		setup_timer(&rtc->timer_task, rtc_uie_timer,
+			    (unsigned long)rtc);
+		rtc->stask_active = 1;
+		if (schedule_work(&rtc->uie_task) == 0)
+			rtc->stask_active = 0;
+	}
+}
+#endif /* CONFIG_RTC_INTF_DEV_X */
 
 static ssize_t
 rtc_dev_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
@@ -227,6 +314,15 @@ static int rtc_dev_ioctl(struct inode *i
 			return -EFAULT;
 		break;
 
+#ifdef CONFIG_RTC_INTF_DEV_X
+	case RTC_UIE_OFF:
+		clear_uie(rtc);
+		return 0;
+
+	case RTC_UIE_ON:
+	        set_uie(rtc);
+		return 0;
+#endif
 	default:
 		err = -EINVAL;
 		break;
@@ -239,6 +335,9 @@ static int rtc_dev_release(struct inode 
 {
 	struct rtc_device *rtc = to_rtc_device(file->private_data);
 
+#ifdef CONFIG_RTC_INTF_DEV_X
+	clear_uie(rtc);
+#endif
 	if (rtc->ops->release)
 		rtc->ops->release(rtc->class_dev.dev);
 
diff --git a/include/linux/rtc.h b/include/linux/rtc.h
index ab61cd1..29199c3 100644
--- a/include/linux/rtc.h
+++ b/include/linux/rtc.h
@@ -155,6 +155,15 @@ struct rtc_device
 	struct rtc_task *irq_task;
 	spinlock_t irq_task_lock;
 	int irq_freq;
+#ifdef CONFIG_RTC_INTF_DEV_X
+	struct work_struct uie_task;
+	struct timer_list timer_task;
+	unsigned int oldsecs;
+	unsigned int irq_active :1;
+	unsigned int stop_rtc_timers :1;	/* don't requeue tasks */
+	unsigned int stask_active :1;		/* schedule_work */
+	unsigned int ttask_active :1;		/* timer_task */
+#endif
 };
 #define to_rtc_device(d) container_of(d, struct rtc_device, class_dev)
 

             reply	other threads:[~2006-04-28 16:16 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-04-28 16:16 Atsushi Nemoto [this message]
2006-04-29  6:23 ` Andrew Morton
     [not found]   ` <20060429093108.77ced705@inspiron>
2006-04-29 15:10     ` Atsushi Nemoto
2006-05-01 14:32       ` Atsushi Nemoto
2006-05-02 20:15         ` Alessandro Zummo
2006-05-03 16:43           ` Atsushi Nemoto

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20060429.011648.25910123.anemo@mba.ocn.ne.jp \
    --to=anemo@mba.ocn.ne.jp \
    --cc=a.zummo@towertech.it \
    --cc=akpm@osdl.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®