1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * IndyDog 0.3 A Hardware Watchdog Device for SGI IP22
5 * (c) Copyright 2002 Guido Guenther <agx@sigxcpu.org>,
8 * based on softdog.c by Alan Cox <alan@lxorguk.ukuu.org.uk>
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/types.h>
16 #include <linux/kernel.h>
19 #include <linux/miscdevice.h>
20 #include <linux/watchdog.h>
21 #include <linux/notifier.h>
22 #include <linux/reboot.h>
23 #include <linux/init.h>
24 #include <linux/uaccess.h>
25 #include <asm/sgi/mc.h>
27 static unsigned long indydog_alive
;
28 static DEFINE_SPINLOCK(indydog_lock
);
30 #define WATCHDOG_TIMEOUT 30 /* 30 sec default timeout */
32 static bool nowayout
= WATCHDOG_NOWAYOUT
;
33 module_param(nowayout
, bool, 0);
34 MODULE_PARM_DESC(nowayout
,
35 "Watchdog cannot be stopped once started (default="
36 __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");
38 static void indydog_start(void)
40 spin_lock(&indydog_lock
);
41 sgimc
->cpuctrl0
|= SGIMC_CCTRL0_WDOG
;
42 spin_unlock(&indydog_lock
);
45 static void indydog_stop(void)
47 spin_lock(&indydog_lock
);
48 sgimc
->cpuctrl0
&= ~SGIMC_CCTRL0_WDOG
;
49 spin_unlock(&indydog_lock
);
51 pr_info("Stopped watchdog timer\n");
54 static void indydog_ping(void)
60 * Allow only one person to hold it open
62 static int indydog_open(struct inode
*inode
, struct file
*file
)
64 if (test_and_set_bit(0, &indydog_alive
))
68 __module_get(THIS_MODULE
);
74 pr_info("Started watchdog timer\n");
76 return stream_open(inode
, file
);
79 static int indydog_release(struct inode
*inode
, struct file
*file
)
81 /* Shut off the timer.
82 * Lock it in if it's a module and we defined ...NOWAYOUT */
84 indydog_stop(); /* Turn the WDT off */
85 clear_bit(0, &indydog_alive
);
89 static ssize_t
indydog_write(struct file
*file
, const char *data
,
90 size_t len
, loff_t
*ppos
)
92 /* Refresh the timer. */
98 static long indydog_ioctl(struct file
*file
, unsigned int cmd
,
101 int options
, retval
= -EINVAL
;
102 static const struct watchdog_info ident
= {
103 .options
= WDIOF_KEEPALIVEPING
,
104 .firmware_version
= 0,
105 .identity
= "Hardware Watchdog for SGI IP22",
109 case WDIOC_GETSUPPORT
:
110 if (copy_to_user((struct watchdog_info
*)arg
,
111 &ident
, sizeof(ident
)))
114 case WDIOC_GETSTATUS
:
115 case WDIOC_GETBOOTSTATUS
:
116 return put_user(0, (int *)arg
);
117 case WDIOC_SETOPTIONS
:
119 if (get_user(options
, (int *)arg
))
121 if (options
& WDIOS_DISABLECARD
) {
125 if (options
& WDIOS_ENABLECARD
) {
131 case WDIOC_KEEPALIVE
:
134 case WDIOC_GETTIMEOUT
:
135 return put_user(WATCHDOG_TIMEOUT
, (int *)arg
);
141 static int indydog_notify_sys(struct notifier_block
*this,
142 unsigned long code
, void *unused
)
144 if (code
== SYS_DOWN
|| code
== SYS_HALT
)
145 indydog_stop(); /* Turn the WDT off */
150 static const struct file_operations indydog_fops
= {
151 .owner
= THIS_MODULE
,
153 .write
= indydog_write
,
154 .unlocked_ioctl
= indydog_ioctl
,
155 .compat_ioctl
= compat_ptr_ioctl
,
156 .open
= indydog_open
,
157 .release
= indydog_release
,
160 static struct miscdevice indydog_miscdev
= {
161 .minor
= WATCHDOG_MINOR
,
163 .fops
= &indydog_fops
,
166 static struct notifier_block indydog_notifier
= {
167 .notifier_call
= indydog_notify_sys
,
170 static int __init
watchdog_init(void)
174 ret
= register_reboot_notifier(&indydog_notifier
);
176 pr_err("cannot register reboot notifier (err=%d)\n", ret
);
180 ret
= misc_register(&indydog_miscdev
);
182 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
183 WATCHDOG_MINOR
, ret
);
184 unregister_reboot_notifier(&indydog_notifier
);
188 pr_info("Hardware Watchdog Timer for SGI IP22: 0.3\n");
193 static void __exit
watchdog_exit(void)
195 misc_deregister(&indydog_miscdev
);
196 unregister_reboot_notifier(&indydog_notifier
);
199 module_init(watchdog_init
);
200 module_exit(watchdog_exit
);
202 MODULE_AUTHOR("Guido Guenther <agx@sigxcpu.org>");
203 MODULE_DESCRIPTION("Hardware Watchdog Device for SGI IP22");
204 MODULE_LICENSE("GPL");