Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * IndyDog 0.3 A Hardware Watchdog Device for SGI IP22 |
| 3 | * |
Alan Cox | 9b9dbcc | 2008-05-19 14:06:08 +0100 | [diff] [blame] | 4 | * (c) Copyright 2002 Guido Guenther <agx@sigxcpu.org>, |
| 5 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version |
| 10 | * 2 of the License, or (at your option) any later version. |
| 11 | * |
Alan Cox | 29fa058 | 2008-10-27 15:17:56 +0000 | [diff] [blame] | 12 | * based on softdog.c by Alan Cox <alan@lxorguk.ukuu.org.uk> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | */ |
| 14 | |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 15 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 16 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/module.h> |
| 18 | #include <linux/moduleparam.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/types.h> |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/fs.h> |
| 22 | #include <linux/mm.h> |
| 23 | #include <linux/miscdevice.h> |
| 24 | #include <linux/watchdog.h> |
| 25 | #include <linux/notifier.h> |
| 26 | #include <linux/reboot.h> |
| 27 | #include <linux/init.h> |
Alan Cox | 9b9dbcc | 2008-05-19 14:06:08 +0100 | [diff] [blame] | 28 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <asm/sgi/mc.h> |
| 30 | |
Alan Cox | 9b9dbcc | 2008-05-19 14:06:08 +0100 | [diff] [blame] | 31 | static unsigned long indydog_alive; |
Axel Lin | 1334f32 | 2011-11-29 13:54:01 +0800 | [diff] [blame] | 32 | static DEFINE_SPINLOCK(indydog_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #define WATCHDOG_TIMEOUT 30 /* 30 sec default timeout */ |
| 35 | |
Wim Van Sebroeck | 86a1e18 | 2012-03-05 16:51:11 +0100 | [diff] [blame] | 36 | static bool nowayout = WATCHDOG_NOWAYOUT; |
| 37 | module_param(nowayout, bool, 0); |
Alan Cox | 9b9dbcc | 2008-05-19 14:06:08 +0100 | [diff] [blame] | 38 | MODULE_PARM_DESC(nowayout, |
| 39 | "Watchdog cannot be stopped once started (default=" |
| 40 | __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
| 42 | static void indydog_start(void) |
| 43 | { |
Alan Cox | 9b9dbcc | 2008-05-19 14:06:08 +0100 | [diff] [blame] | 44 | spin_lock(&indydog_lock); |
Alexander Shiyan | 0c29c2e | 2014-02-15 13:21:51 +0400 | [diff] [blame] | 45 | sgimc->cpuctrl0 |= SGIMC_CCTRL0_WDOG; |
Alan Cox | 9b9dbcc | 2008-05-19 14:06:08 +0100 | [diff] [blame] | 46 | spin_unlock(&indydog_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | static void indydog_stop(void) |
| 50 | { |
Alan Cox | 9b9dbcc | 2008-05-19 14:06:08 +0100 | [diff] [blame] | 51 | spin_lock(&indydog_lock); |
Alexander Shiyan | 0c29c2e | 2014-02-15 13:21:51 +0400 | [diff] [blame] | 52 | sgimc->cpuctrl0 &= ~SGIMC_CCTRL0_WDOG; |
Alan Cox | 9b9dbcc | 2008-05-19 14:06:08 +0100 | [diff] [blame] | 53 | spin_unlock(&indydog_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 55 | pr_info("Stopped watchdog timer\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | static void indydog_ping(void) |
| 59 | { |
| 60 | sgimc->watchdogt = 0; |
| 61 | } |
| 62 | |
| 63 | /* |
| 64 | * Allow only one person to hold it open |
| 65 | */ |
| 66 | static int indydog_open(struct inode *inode, struct file *file) |
| 67 | { |
Alan Cox | 9b9dbcc | 2008-05-19 14:06:08 +0100 | [diff] [blame] | 68 | if (test_and_set_bit(0, &indydog_alive)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | return -EBUSY; |
| 70 | |
| 71 | if (nowayout) |
| 72 | __module_get(THIS_MODULE); |
| 73 | |
| 74 | /* Activate timer */ |
| 75 | indydog_start(); |
| 76 | indydog_ping(); |
| 77 | |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 78 | pr_info("Started watchdog timer\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
| 80 | return nonseekable_open(inode, file); |
| 81 | } |
| 82 | |
| 83 | static int indydog_release(struct inode *inode, struct file *file) |
| 84 | { |
| 85 | /* Shut off the timer. |
| 86 | * Lock it in if it's a module and we defined ...NOWAYOUT */ |
| 87 | if (!nowayout) |
| 88 | indydog_stop(); /* Turn the WDT off */ |
Alan Cox | 9b9dbcc | 2008-05-19 14:06:08 +0100 | [diff] [blame] | 89 | clear_bit(0, &indydog_alive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | return 0; |
| 91 | } |
| 92 | |
Alan Cox | 9b9dbcc | 2008-05-19 14:06:08 +0100 | [diff] [blame] | 93 | static ssize_t indydog_write(struct file *file, const char *data, |
| 94 | size_t len, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | { |
| 96 | /* Refresh the timer. */ |
Alan Cox | 9b9dbcc | 2008-05-19 14:06:08 +0100 | [diff] [blame] | 97 | if (len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | indydog_ping(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | return len; |
| 100 | } |
| 101 | |
Alan Cox | 9b9dbcc | 2008-05-19 14:06:08 +0100 | [diff] [blame] | 102 | static long indydog_ioctl(struct file *file, unsigned int cmd, |
| 103 | unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | { |
| 105 | int options, retval = -EINVAL; |
Wim Van Sebroeck | 42747d7 | 2009-12-26 18:55:22 +0000 | [diff] [blame] | 106 | static const struct watchdog_info ident = { |
Wim Van Sebroeck | e73a780 | 2009-05-11 18:33:00 +0000 | [diff] [blame] | 107 | .options = WDIOF_KEEPALIVEPING, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | .firmware_version = 0, |
| 109 | .identity = "Hardware Watchdog for SGI IP22", |
| 110 | }; |
| 111 | |
| 112 | switch (cmd) { |
Alan Cox | 9b9dbcc | 2008-05-19 14:06:08 +0100 | [diff] [blame] | 113 | case WDIOC_GETSUPPORT: |
| 114 | if (copy_to_user((struct watchdog_info *)arg, |
| 115 | &ident, sizeof(ident))) |
| 116 | return -EFAULT; |
| 117 | return 0; |
| 118 | case WDIOC_GETSTATUS: |
| 119 | case WDIOC_GETBOOTSTATUS: |
| 120 | return put_user(0, (int *)arg); |
Alan Cox | 9b9dbcc | 2008-05-19 14:06:08 +0100 | [diff] [blame] | 121 | case WDIOC_SETOPTIONS: |
| 122 | { |
| 123 | if (get_user(options, (int *)arg)) |
| 124 | return -EFAULT; |
| 125 | if (options & WDIOS_DISABLECARD) { |
| 126 | indydog_stop(); |
| 127 | retval = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | } |
Alan Cox | 9b9dbcc | 2008-05-19 14:06:08 +0100 | [diff] [blame] | 129 | if (options & WDIOS_ENABLECARD) { |
| 130 | indydog_start(); |
| 131 | retval = 0; |
| 132 | } |
| 133 | return retval; |
| 134 | } |
Wim Van Sebroeck | 0c06090 | 2008-07-18 11:41:17 +0000 | [diff] [blame] | 135 | case WDIOC_KEEPALIVE: |
| 136 | indydog_ping(); |
| 137 | return 0; |
| 138 | case WDIOC_GETTIMEOUT: |
| 139 | return put_user(WATCHDOG_TIMEOUT, (int *)arg); |
Alan Cox | 9b9dbcc | 2008-05-19 14:06:08 +0100 | [diff] [blame] | 140 | default: |
| 141 | return -ENOTTY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
Alan Cox | 9b9dbcc | 2008-05-19 14:06:08 +0100 | [diff] [blame] | 145 | static int indydog_notify_sys(struct notifier_block *this, |
| 146 | unsigned long code, void *unused) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | { |
| 148 | if (code == SYS_DOWN || code == SYS_HALT) |
| 149 | indydog_stop(); /* Turn the WDT off */ |
| 150 | |
| 151 | return NOTIFY_DONE; |
| 152 | } |
| 153 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 154 | static const struct file_operations indydog_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | .owner = THIS_MODULE, |
| 156 | .llseek = no_llseek, |
| 157 | .write = indydog_write, |
Alan Cox | 9b9dbcc | 2008-05-19 14:06:08 +0100 | [diff] [blame] | 158 | .unlocked_ioctl = indydog_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | .open = indydog_open, |
| 160 | .release = indydog_release, |
| 161 | }; |
| 162 | |
| 163 | static struct miscdevice indydog_miscdev = { |
| 164 | .minor = WATCHDOG_MINOR, |
| 165 | .name = "watchdog", |
| 166 | .fops = &indydog_fops, |
| 167 | }; |
| 168 | |
| 169 | static struct notifier_block indydog_notifier = { |
| 170 | .notifier_call = indydog_notify_sys, |
| 171 | }; |
| 172 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | static int __init watchdog_init(void) |
| 174 | { |
| 175 | int ret; |
| 176 | |
| 177 | ret = register_reboot_notifier(&indydog_notifier); |
| 178 | if (ret) { |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 179 | pr_err("cannot register reboot notifier (err=%d)\n", ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | return ret; |
| 181 | } |
| 182 | |
| 183 | ret = misc_register(&indydog_miscdev); |
| 184 | if (ret) { |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 185 | pr_err("cannot register miscdev on minor=%d (err=%d)\n", |
| 186 | WATCHDOG_MINOR, ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | unregister_reboot_notifier(&indydog_notifier); |
| 188 | return ret; |
| 189 | } |
| 190 | |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 191 | pr_info("Hardware Watchdog Timer for SGI IP22: 0.3\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | static void __exit watchdog_exit(void) |
| 197 | { |
| 198 | misc_deregister(&indydog_miscdev); |
| 199 | unregister_reboot_notifier(&indydog_notifier); |
| 200 | } |
| 201 | |
| 202 | module_init(watchdog_init); |
| 203 | module_exit(watchdog_exit); |
| 204 | |
| 205 | MODULE_AUTHOR("Guido Guenther <agx@sigxcpu.org>"); |
| 206 | MODULE_DESCRIPTION("Hardware Watchdog Device for SGI IP22"); |
| 207 | MODULE_LICENSE("GPL"); |