4 * (c) Copyright 2010 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 pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #define DRV_NAME "wdt"
15 #define DRV_VERSION "0.01"
17 #include <linux/bug.h>
18 #include <linux/errno.h>
20 #include <linux/hrtimer.h>
21 #include <linux/kernel.h>
22 #include <linux/ktime.h>
23 #include <linux/init.h>
24 #include <linux/miscdevice.h>
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/platform_device.h>
28 #include <linux/spinlock.h>
29 #include <linux/uaccess.h>
30 #include <linux/watchdog.h>
32 #include <asm/xen/hypercall.h>
33 #include <xen/interface/sched.h>
35 static struct platform_device *platform_device;
36 static DEFINE_SPINLOCK(wdt_lock);
37 static struct sched_watchdog wdt;
38 static __kernel_time_t wdt_expires;
39 static bool is_active, expect_release;
41 #define WATCHDOG_TIMEOUT 60 /* in seconds */
42 static unsigned int timeout = WATCHDOG_TIMEOUT;
43 module_param(timeout, uint, S_IRUGO);
44 MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds "
45 "(default=" __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
47 static bool nowayout = WATCHDOG_NOWAYOUT;
48 module_param(nowayout, bool, S_IRUGO);
49 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
50 "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
52 static inline __kernel_time_t set_timeout(void)
54 wdt.timeout = timeout;
55 return ktime_to_timespec(ktime_get()).tv_sec + timeout;
58 static int xen_wdt_start(void)
60 __kernel_time_t expires;
65 expires = set_timeout();
67 err = HYPERVISOR_sched_op(SCHEDOP_watchdog, &wdt);
72 wdt_expires = expires;
77 spin_unlock(&wdt_lock);
82 static int xen_wdt_stop(void)
90 err = HYPERVISOR_sched_op(SCHEDOP_watchdog, &wdt);
94 spin_unlock(&wdt_lock);
99 static int xen_wdt_kick(void)
101 __kernel_time_t expires;
104 spin_lock(&wdt_lock);
106 expires = set_timeout();
108 err = HYPERVISOR_sched_op(SCHEDOP_watchdog, &wdt);
112 wdt_expires = expires;
114 spin_unlock(&wdt_lock);
119 static int xen_wdt_open(struct inode *inode, struct file *file)
123 /* /dev/watchdog can only be opened once */
124 if (xchg(&is_active, true))
127 err = xen_wdt_start();
129 err = xen_wdt_kick();
130 return err ?: nonseekable_open(inode, file);
133 static int xen_wdt_release(struct inode *inode, struct file *file)
138 err = xen_wdt_stop();
140 pr_crit("unexpected close, not stopping watchdog!\n");
144 expect_release = false;
148 static ssize_t xen_wdt_write(struct file *file, const char __user *data,
149 size_t len, loff_t *ppos)
151 /* See if we got the magic character 'V' and reload the timer */
156 /* in case it was set long ago */
157 expect_release = false;
159 /* scan to see whether or not we got the magic
161 for (i = 0; i != len; i++) {
163 if (get_user(c, data + i))
166 expect_release = true;
170 /* someone wrote to us, we should reload the timer */
176 static long xen_wdt_ioctl(struct file *file, unsigned int cmd,
179 int new_options, retval = -EINVAL;
181 int __user *argp = (void __user *)arg;
182 static const struct watchdog_info ident = {
183 .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
184 .firmware_version = 0,
185 .identity = DRV_NAME,
189 case WDIOC_GETSUPPORT:
190 return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
192 case WDIOC_GETSTATUS:
193 case WDIOC_GETBOOTSTATUS:
194 return put_user(0, argp);
196 case WDIOC_SETOPTIONS:
197 if (get_user(new_options, argp))
200 if (new_options & WDIOS_DISABLECARD)
201 retval = xen_wdt_stop();
202 if (new_options & WDIOS_ENABLECARD) {
203 retval = xen_wdt_start();
204 if (retval == -EBUSY)
205 retval = xen_wdt_kick();
209 case WDIOC_KEEPALIVE:
213 case WDIOC_SETTIMEOUT:
214 if (get_user(new_timeout, argp))
218 timeout = new_timeout;
221 case WDIOC_GETTIMEOUT:
222 return put_user(timeout, argp);
224 case WDIOC_GETTIMELEFT:
225 retval = wdt_expires - ktime_to_timespec(ktime_get()).tv_sec;
226 return put_user(retval, argp);
232 static const struct file_operations xen_wdt_fops = {
233 .owner = THIS_MODULE,
235 .write = xen_wdt_write,
236 .unlocked_ioctl = xen_wdt_ioctl,
237 .open = xen_wdt_open,
238 .release = xen_wdt_release,
241 static struct miscdevice xen_wdt_miscdev = {
242 .minor = WATCHDOG_MINOR,
244 .fops = &xen_wdt_fops,
247 static int xen_wdt_probe(struct platform_device *dev)
249 struct sched_watchdog wd = { .id = ~0 };
250 int ret = HYPERVISOR_sched_op(SCHEDOP_watchdog, &wd);
255 timeout = WATCHDOG_TIMEOUT;
256 pr_info("timeout value invalid, using %d\n", timeout);
259 ret = misc_register(&xen_wdt_miscdev);
261 pr_err("cannot register miscdev on minor=%d (%d)\n",
262 WATCHDOG_MINOR, ret);
266 pr_info("initialized (timeout=%ds, nowayout=%d)\n",
271 pr_info("not supported\n");
276 pr_info("bogus return value %d\n", ret);
283 static int xen_wdt_remove(struct platform_device *dev)
285 /* Stop the timer before we leave */
289 misc_deregister(&xen_wdt_miscdev);
294 static void xen_wdt_shutdown(struct platform_device *dev)
299 static int xen_wdt_suspend(struct platform_device *dev, pm_message_t state)
301 typeof(wdt.id) id = wdt.id;
302 int rc = xen_wdt_stop();
308 static int xen_wdt_resume(struct platform_device *dev)
313 return xen_wdt_start();
316 static struct platform_driver xen_wdt_driver = {
317 .probe = xen_wdt_probe,
318 .remove = xen_wdt_remove,
319 .shutdown = xen_wdt_shutdown,
320 .suspend = xen_wdt_suspend,
321 .resume = xen_wdt_resume,
327 static int __init xen_wdt_init_module(void)
334 pr_info("Xen WatchDog Timer Driver v%s\n", DRV_VERSION);
336 err = platform_driver_register(&xen_wdt_driver);
340 platform_device = platform_device_register_simple(DRV_NAME,
342 if (IS_ERR(platform_device)) {
343 err = PTR_ERR(platform_device);
344 platform_driver_unregister(&xen_wdt_driver);
350 static void __exit xen_wdt_cleanup_module(void)
352 platform_device_unregister(platform_device);
353 platform_driver_unregister(&xen_wdt_driver);
354 pr_info("module unloaded\n");
357 module_init(xen_wdt_init_module);
358 module_exit(xen_wdt_cleanup_module);
360 MODULE_AUTHOR("Jan Beulich <jbeulich@novell.com>");
361 MODULE_DESCRIPTION("Xen WatchDog Timer Driver");
362 MODULE_VERSION(DRV_VERSION);
363 MODULE_LICENSE("GPL");