[Lguest] RTC for lguest

Grant Grundler grundler at parisc-linux.org
Sun Mar 2 05:04:38 EST 2008


Rusty,
Thanks again for the lguest tutorial at lca2008.

Patch below adds a RTC driver that provides minimal functionality
for lguest. I've only tested 2.6.23 under Qemu if that matters.
In general, I think the rtc-lguest has to treat the host as a
read-only HW clock and any additional features being added
need to keep that in mind.

I'm not certain I have the Kconfig entry quite right but it
works for me. Would be nice if someone with Kbuild-fu would
take a quick look at it. I didn't really want "tristate"
since it should not be a module if LGUEST is enabled.
But if the kernel is being booted as a "host", I don't want
rtc-lguest enabled either....suggests a run-time check is needed
since we don't want to displace what normally would be /dev/rtc0.

enjoy!
grant

Signed-off-by: Grant Grundler <grundler at parisc-linux.org>

--- linux-2.6.23/drivers/rtc/Kconfig-ORIG	2008-02-03 23:20:09.000000000 +1100
+++ linux-2.6.23/drivers/rtc/Kconfig	2008-02-04 16:54:51.000000000 +1100
@@ -117,6 +117,16 @@
 	  This driver can also be built as a module. If so, the module
 	  will be called rtc-test.
 
+config RTC_DRV_LGUEST
+	tristate "Lguest RTC psuedo-device"
+	default LGUEST
+	help
+	  If you say yes, you get /dev/rtc support for Lguest.
+	  This psuedo driver provides a fake RTC to the guest by
+	  communicating with the host. In effect, the host is a
+	  read-only "real time" clock source.
+	  This driver will automatically be selected if LGUEST is enabled.
+
 comment "I2C RTC drivers"
 	depends on I2C
 
--- linux-2.6.23/drivers/rtc/Makefile-ORIG	2008-02-03 23:20:29.000000000 +1100
+++ linux-2.6.23/drivers/rtc/Makefile	2008-02-03 23:37:44.000000000 +1100
@@ -28,6 +28,7 @@
 obj-$(CONFIG_RTC_DRV_DS1742)	+= rtc-ds1742.o
 obj-$(CONFIG_RTC_DRV_EP93XX)	+= rtc-ep93xx.o
 obj-$(CONFIG_RTC_DRV_ISL1208)	+= rtc-isl1208.o
+obj-$(CONFIG_RTC_DRV_LGUEST)	+= rtc-lguest.o
 obj-$(CONFIG_RTC_DRV_M41T80)	+= rtc-m41t80.o
 obj-$(CONFIG_RTC_DRV_M48T59)	+= rtc-m48t59.o
 obj-$(CONFIG_RTC_DRV_M48T86)	+= rtc-m48t86.o
--- /dev/null	2008-02-06 20:05:08.534205825 +1100
+++ linux-2.6.23/drivers/rtc/rtc-lguest.c	2008-02-06 20:13:53.000000000 +1100
@@ -0,0 +1,215 @@
+/*
+ * An lguest RTC device/driver
+ * Copyright (C) 2008 Grant Grundler <grundler at parisc-linux.org>
+ *
+ * This driver only happened because "Rusty" Russell gave an lguest
+ * tutorial at LCA2008. Thanks Rusty!
+ *
+ * General framework borrowed from rtc-test.c:
+ *     Original Author: Alessandro Zummo <a.zummo at towertech.it>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#include <linux/module.h>
+#include <linux/err.h>
+#include <linux/rtc.h>
+#include <linux/platform_device.h>
+#include <linux/lguest.h>	/* for hcall() proto */
+
+static struct platform_device *lg_rtc0;
+
+#define ENABLE_RTC_ALARM 0
+#if ENABLE_RTC_ALARM
+static int lguest_rtc_read_alarm(struct device *dev,
+	struct rtc_wkalrm *alrm)
+{
+	/* FIXME: probably want to make an hcall() here instead */
+	return 0;
+}
+
+static int lguest_rtc_set_alarm(struct device *dev,
+	struct rtc_wkalrm *alrm)
+{
+	/* FIXME: probably want to make an hcall() here instead */
+	return 0;
+}
+#endif
+
+static int lguest_rtc_read_time(struct device *dev,
+	struct rtc_time *tm)
+{
+#if 1
+	rtc_time_to_tm(get_seconds(), tm);
+#else
+#error FIXME - add hcall to ask host the time.
+#endif
+	return 0;
+}
+
+static int lguest_rtc_set_time(struct device *dev,
+	struct rtc_time *tm)
+{
+	return 0;	/* guests not permitted to set the time */
+}
+
+static int lguest_rtc_set_mmss(struct device *dev, unsigned long secs)
+{
+	return 0;
+}
+
+static int lguest_rtc_proc(struct device *dev, struct seq_file *seq)
+{
+	struct platform_device *plat_dev = to_platform_device(dev);
+
+	seq_printf(seq, "lg_rtc\t\t: yes\n");
+	seq_printf(seq, "id\t\t: %d\n", plat_dev->id);
+
+	return 0;
+}
+
+static int lguest_rtc_ioctl(struct device *dev, unsigned int cmd,
+	unsigned long arg)
+{
+	switch (cmd) {
+	case RTC_PIE_ON:
+	case RTC_PIE_OFF:
+	case RTC_UIE_ON:
+	case RTC_UIE_OFF:
+	case RTC_AIE_ON:
+	case RTC_AIE_OFF:
+		return 0;
+
+	default:
+		return -ENOIOCTLCMD;
+	}
+}
+
+static const struct rtc_class_ops lguest_rtc_ops = {
+	.proc = lguest_rtc_proc,
+	.read_time = lguest_rtc_read_time,
+	.set_time = lguest_rtc_set_time,
+#if ENABLE_RTC_ALARM
+	.read_alarm = lguest_rtc_read_alarm,
+	.set_alarm = lguest_rtc_set_alarm,
+#endif
+	.set_mmss = lguest_rtc_set_mmss,
+	.ioctl = lguest_rtc_ioctl,
+};
+
+static ssize_t lguest_irq_show(struct device *dev,
+				struct device_attribute *attr, char *buf)
+{
+	return sprintf(buf, "%d\n", 42);
+}
+
+static ssize_t lguest_irq_store(struct device *dev,
+				struct device_attribute *attr,
+				const char *buf, size_t count)
+{
+	int retval;
+	struct platform_device *plat_dev = to_platform_device(dev);
+	struct rtc_device *rtc = platform_get_drvdata(plat_dev);
+
+	retval = count;
+	local_irq_disable();
+	if (strncmp(buf, "tick", 4) == 0)
+		rtc_update_irq(rtc, 1, RTC_PF | RTC_IRQF);
+	else if (strncmp(buf, "alarm", 5) == 0)
+		rtc_update_irq(rtc, 1, RTC_AF | RTC_IRQF);
+	else if (strncmp(buf, "update", 6) == 0)
+		rtc_update_irq(rtc, 1, RTC_UF | RTC_IRQF);
+	else
+		retval = -EINVAL;
+	local_irq_enable();
+
+	return retval;
+}
+static DEVICE_ATTR(irq, S_IRUGO | S_IWUSR, lguest_irq_show, lguest_irq_store);
+
+static int lguest_probe(struct platform_device *plat_dev)
+{
+	int err;
+	struct rtc_device *rtc;
+
+	rtc = rtc_device_register("rtc-lguest", &plat_dev->dev,
+					&lguest_rtc_ops, THIS_MODULE);
+	if (IS_ERR(rtc)) {
+		err = PTR_ERR(rtc);
+		return err;
+	}
+
+	err = device_create_file(&plat_dev->dev, &dev_attr_irq);
+	if (err)
+		goto err;
+
+	platform_set_drvdata(plat_dev, rtc);
+
+	return 0;
+
+err:
+	rtc_device_unregister(rtc);
+	return err;
+}
+
+static int __devexit lguest_remove(struct platform_device *plat_dev)
+{
+	struct rtc_device *rtc = platform_get_drvdata(plat_dev);
+
+	rtc_device_unregister(rtc);
+	device_remove_file(&plat_dev->dev, &dev_attr_irq);
+
+	return 0;
+}
+
+static struct platform_driver lguest_drv = {
+	.probe	= lguest_probe,
+	.remove = __devexit_p(lguest_remove),
+	.driver = {
+		.name = "rtc-lguest",
+		.owner = THIS_MODULE,
+	},
+};
+
+static int __init lguest_init(void)
+{
+	int err;
+
+	err = platform_driver_register(&lguest_drv);
+	if (err)
+		return err;
+
+	lg_rtc0 = platform_device_alloc("rtc-lguest", 0);
+	if (lg_rtc0 == NULL) {
+		err = -ENOMEM;
+		goto exit_driver_unregister;
+	}
+
+	err = platform_device_add(lg_rtc0);
+	if (err)
+		goto exit_free_rtc0;
+
+	return 0;
+
+exit_free_rtc0:
+	platform_device_put(lg_rtc0);
+
+exit_driver_unregister:
+	platform_driver_unregister(&lguest_drv);
+	return err;
+}
+
+static void __exit lguest_exit(void)
+{
+	platform_device_unregister(lg_rtc0);
+	platform_driver_unregister(&lguest_drv);
+}
+
+MODULE_AUTHOR("Grant Grundler <grundler at parisc-linux.org>");
+MODULE_DESCRIPTION("lguest RTC driver");
+MODULE_LICENSE("GPL");
+
+module_init(lguest_init);
+module_exit(lguest_exit);



More information about the Lguest mailing list