4 * (c) Copyright 2010,2011 Novell, Inc.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #define DRV_NAME "wdt"
13 #define DRV_VERSION "0.01"
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/bug.h>
17 #include <linux/errno.h>
19 #include <linux/hrtimer.h>
20 #include <linux/kernel.h>
21 #include <linux/ktime.h>
22 #include <linux/init.h>
23 #include <linux/miscdevice.h>
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/platform_device.h>
27 #include <linux/spinlock.h>
28 #include <linux/uaccess.h>
29 #include <linux/watchdog.h>
30 #ifdef CONFIG_PARAVIRT_XEN
32 #include <asm/xen/hypercall.h>
34 #include <xen/interface/sched.h>
36 static struct platform_device *platform_device;
37 static DEFINE_SPINLOCK(wdt_lock);
38 static struct sched_watchdog wdt;
39 static __kernel_time_t wdt_expires;
40 static bool is_active, expect_release;
42 #define WATCHDOG_TIMEOUT 60 /* in seconds */
43 static unsigned int timeout = WATCHDOG_TIMEOUT;
44 module_param(timeout, uint, S_IRUGO);
45 MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds "
46 "(default=" __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
48 static bool nowayout = WATCHDOG_NOWAYOUT;
49 module_param(nowayout, bool, S_IRUGO);
50 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
51 "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
53 static inline __kernel_time_t set_timeout(void)
55 wdt.timeout = timeout;
56 return ktime_to_timespec(ktime_get()).tv_sec + timeout;
59 static int xen_wdt_start(void)
61 __kernel_time_t expires;
66 expires = set_timeout();
68 err = HYPERVISOR_sched_op(SCHEDOP_watchdog, &wdt);
73 wdt_expires = expires;
78 spin_unlock(&wdt_lock);
83 static int xen_wdt_stop(void)
91 err = HYPERVISOR_sched_op(SCHEDOP_watchdog, &wdt);
95 spin_unlock(&wdt_lock);
100 static int xen_wdt_kick(void)
102 __kernel_time_t expires;
105 spin_lock(&wdt_lock);
107 expires = set_timeout();
109 err = HYPERVISOR_sched_op(SCHEDOP_watchdog, &wdt);
113 wdt_expires = expires;
115 spin_unlock(&wdt_lock);
120 static int xen_wdt_open(struct inode *inode, struct file *file)
124 /* /dev/watchdog can only be opened once */
125 if (xchg(&is_active, true))
128 err = xen_wdt_start();
130 err = xen_wdt_kick();
131 return err ?: nonseekable_open(inode, file);
134 static int xen_wdt_release(struct inode *inode, struct file *file)
139 pr_crit("unexpected close, not stopping watchdog!\n");
143 expect_release = false;
147 static ssize_t xen_wdt_write(struct file *file, const char __user *data,
148 size_t len, loff_t *ppos)
150 /* See if we got the magic character 'V' and reload the timer */
155 /* in case it was set long ago */
156 expect_release = false;
158 /* scan to see whether or not we got the magic
160 for (i = 0; i != len; i++) {
162 if (get_user(c, data + i))
165 expect_release = true;
169 /* someone wrote to us, we should reload the timer */
175 static long xen_wdt_ioctl(struct file *file, unsigned int cmd,
178 int new_options, retval = -EINVAL;
180 int __user *argp = (void __user *)arg;
181 static const struct watchdog_info ident = {
182 .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
183 .firmware_version = 0,
184 .identity = DRV_NAME,
188 case WDIOC_GETSUPPORT:
189 return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
191 case WDIOC_GETSTATUS:
192 case WDIOC_GETBOOTSTATUS:
193 return put_user(0, argp);
195 case WDIOC_SETOPTIONS:
196 if (get_user(new_options, argp))
199 if (new_options & WDIOS_DISABLECARD)
200 retval = xen_wdt_stop();
201 if (new_options & WDIOS_ENABLECARD) {
202 retval = xen_wdt_start();
203 if (retval == -EBUSY)
204 retval = xen_wdt_kick();
208 case WDIOC_KEEPALIVE:
212 case WDIOC_SETTIMEOUT:
213 if (get_user(new_timeout, argp))
217 timeout = new_timeout;
220 case WDIOC_GETTIMEOUT:
221 return put_user(timeout, argp);
223 case WDIOC_GETTIMELEFT:
224 retval = wdt_expires - ktime_to_timespec(ktime_get()).tv_sec;
225 return put_user(retval, argp);
231 static const struct file_operations xen_wdt_fops = {
232 .owner = THIS_MODULE,
234 .write = xen_wdt_write,
235 .unlocked_ioctl = xen_wdt_ioctl,
236 .open = xen_wdt_open,
237 .release = xen_wdt_release,
240 static struct miscdevice xen_wdt_miscdev = {
241 .minor = WATCHDOG_MINOR,
243 .fops = &xen_wdt_fops,
246 static int __devinit xen_wdt_probe(struct platform_device *dev)
248 struct sched_watchdog wd = { .id = ~0 };
249 int ret = HYPERVISOR_sched_op(SCHEDOP_watchdog, &wd);
254 timeout = WATCHDOG_TIMEOUT;
255 pr_info("timeout value invalid, using %d\n", timeout);
258 ret = misc_register(&xen_wdt_miscdev);
260 pr_err("cannot register miscdev on minor=%d (%d)\n",
261 WATCHDOG_MINOR, ret);
265 pr_info("initialized (timeout=%ds, nowayout=%d)\n",
270 pr_info("not supported\n");
275 pr_info("bogus return value %d\n", ret);
282 static int __devexit xen_wdt_remove(struct platform_device *dev)
284 /* Stop the timer before we leave */
288 misc_deregister(&xen_wdt_miscdev);
293 static void xen_wdt_shutdown(struct platform_device *dev)
298 static int xen_wdt_suspend(struct platform_device *dev, pm_message_t state)
300 return xen_wdt_stop();
303 static int xen_wdt_resume(struct platform_device *dev)
305 return xen_wdt_start();
308 static struct platform_driver xen_wdt_driver = {
309 .probe = xen_wdt_probe,
310 .remove = __devexit_p(xen_wdt_remove),
311 .shutdown = xen_wdt_shutdown,
312 .suspend = xen_wdt_suspend,
313 .resume = xen_wdt_resume,
315 .owner = THIS_MODULE,
320 static int __init xen_wdt_init_module(void)
324 #ifdef CONFIG_PARAVIRT_XEN
329 printk(KERN_INFO "Xen WatchDog Timer Driver v%s\n", DRV_VERSION);
331 err = platform_driver_register(&xen_wdt_driver);
335 platform_device = platform_device_register_simple(DRV_NAME,
337 if (IS_ERR(platform_device)) {
338 err = PTR_ERR(platform_device);
339 platform_driver_unregister(&xen_wdt_driver);
345 static void __exit xen_wdt_cleanup_module(void)
347 platform_device_unregister(platform_device);
348 platform_driver_unregister(&xen_wdt_driver);
349 pr_info("module unloaded\n");
352 module_init(xen_wdt_init_module);
353 module_exit(xen_wdt_cleanup_module);
355 MODULE_AUTHOR("Jan Beulich <jbeulich@novell.com>");
356 MODULE_DESCRIPTION("Xen WatchDog Timer Driver");
357 MODULE_VERSION(DRV_VERSION);
358 MODULE_LICENSE("GPL");
359 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);