0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0011
0012 #include <linux/fs.h>
0013 #include <linux/init.h>
0014 #include <linux/ioport.h>
0015 #include <linux/jiffies.h>
0016 #include <linux/module.h>
0017 #include <linux/moduleparam.h>
0018 #include <linux/miscdevice.h>
0019 #include <linux/notifier.h>
0020 #include <linux/reboot.h>
0021 #include <linux/types.h>
0022 #include <linux/watchdog.h>
0023 #include <linux/io.h>
0024 #include <linux/uaccess.h>
0025 #include <linux/atomic.h>
0026
0027 #define SBC7240_ENABLE_PORT 0x443
0028 #define SBC7240_DISABLE_PORT 0x043
0029 #define SBC7240_SET_TIMEOUT_PORT SBC7240_ENABLE_PORT
0030 #define SBC7240_MAGIC_CHAR 'V'
0031
0032 #define SBC7240_TIMEOUT 30
0033 #define SBC7240_MAX_TIMEOUT 255
0034 static int timeout = SBC7240_TIMEOUT;
0035 module_param(timeout, int, 0);
0036 MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds. (1<=timeout<="
0037 __MODULE_STRING(SBC7240_MAX_TIMEOUT) ", default="
0038 __MODULE_STRING(SBC7240_TIMEOUT) ")");
0039
0040 static bool nowayout = WATCHDOG_NOWAYOUT;
0041 module_param(nowayout, bool, 0);
0042 MODULE_PARM_DESC(nowayout, "Disable watchdog when closing device file");
0043
0044 #define SBC7240_OPEN_STATUS_BIT 0
0045 #define SBC7240_ENABLED_STATUS_BIT 1
0046 #define SBC7240_EXPECT_CLOSE_STATUS_BIT 2
0047 static unsigned long wdt_status;
0048
0049
0050
0051
0052
0053 static void wdt_disable(void)
0054 {
0055
0056 if (test_and_clear_bit(SBC7240_ENABLED_STATUS_BIT, &wdt_status)) {
0057 inb_p(SBC7240_DISABLE_PORT);
0058 pr_info("Watchdog timer is now disabled\n");
0059 }
0060 }
0061
0062 static void wdt_enable(void)
0063 {
0064
0065 if (!test_and_set_bit(SBC7240_ENABLED_STATUS_BIT, &wdt_status)) {
0066 inb_p(SBC7240_ENABLE_PORT);
0067 pr_info("Watchdog timer is now enabled\n");
0068 }
0069 }
0070
0071 static int wdt_set_timeout(int t)
0072 {
0073 if (t < 1 || t > SBC7240_MAX_TIMEOUT) {
0074 pr_err("timeout value must be 1<=x<=%d\n", SBC7240_MAX_TIMEOUT);
0075 return -1;
0076 }
0077
0078 outb_p((unsigned)t, SBC7240_SET_TIMEOUT_PORT);
0079 timeout = t;
0080 pr_info("timeout set to %d seconds\n", t);
0081 return 0;
0082 }
0083
0084
0085 static inline void wdt_keepalive(void)
0086 {
0087 if (test_bit(SBC7240_ENABLED_STATUS_BIT, &wdt_status))
0088 inb_p(SBC7240_ENABLE_PORT);
0089 }
0090
0091
0092
0093
0094 static ssize_t fop_write(struct file *file, const char __user *buf,
0095 size_t count, loff_t *ppos)
0096 {
0097 size_t i;
0098 char c;
0099
0100 if (count) {
0101 if (!nowayout) {
0102 clear_bit(SBC7240_EXPECT_CLOSE_STATUS_BIT,
0103 &wdt_status);
0104
0105
0106 for (i = 0; i != count; i++) {
0107 if (get_user(c, buf + i))
0108 return -EFAULT;
0109 if (c == SBC7240_MAGIC_CHAR) {
0110 set_bit(SBC7240_EXPECT_CLOSE_STATUS_BIT,
0111 &wdt_status);
0112 break;
0113 }
0114 }
0115 }
0116
0117 wdt_keepalive();
0118 }
0119
0120 return count;
0121 }
0122
0123 static int fop_open(struct inode *inode, struct file *file)
0124 {
0125 if (test_and_set_bit(SBC7240_OPEN_STATUS_BIT, &wdt_status))
0126 return -EBUSY;
0127
0128 wdt_enable();
0129
0130 return stream_open(inode, file);
0131 }
0132
0133 static int fop_close(struct inode *inode, struct file *file)
0134 {
0135 if (test_and_clear_bit(SBC7240_EXPECT_CLOSE_STATUS_BIT, &wdt_status)
0136 || !nowayout) {
0137 wdt_disable();
0138 } else {
0139 pr_crit("Unexpected close, not stopping watchdog!\n");
0140 wdt_keepalive();
0141 }
0142
0143 clear_bit(SBC7240_OPEN_STATUS_BIT, &wdt_status);
0144 return 0;
0145 }
0146
0147 static const struct watchdog_info ident = {
0148 .options = WDIOF_KEEPALIVEPING|
0149 WDIOF_SETTIMEOUT|
0150 WDIOF_MAGICCLOSE,
0151 .firmware_version = 1,
0152 .identity = "SBC7240",
0153 };
0154
0155
0156 static long fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
0157 {
0158 switch (cmd) {
0159 case WDIOC_GETSUPPORT:
0160 return copy_to_user((void __user *)arg, &ident, sizeof(ident))
0161 ? -EFAULT : 0;
0162 case WDIOC_GETSTATUS:
0163 case WDIOC_GETBOOTSTATUS:
0164 return put_user(0, (int __user *)arg);
0165 case WDIOC_SETOPTIONS:
0166 {
0167 int options;
0168 int retval = -EINVAL;
0169
0170 if (get_user(options, (int __user *)arg))
0171 return -EFAULT;
0172
0173 if (options & WDIOS_DISABLECARD) {
0174 wdt_disable();
0175 retval = 0;
0176 }
0177
0178 if (options & WDIOS_ENABLECARD) {
0179 wdt_enable();
0180 retval = 0;
0181 }
0182
0183 return retval;
0184 }
0185 case WDIOC_KEEPALIVE:
0186 wdt_keepalive();
0187 return 0;
0188 case WDIOC_SETTIMEOUT:
0189 {
0190 int new_timeout;
0191
0192 if (get_user(new_timeout, (int __user *)arg))
0193 return -EFAULT;
0194
0195 if (wdt_set_timeout(new_timeout))
0196 return -EINVAL;
0197 }
0198 fallthrough;
0199 case WDIOC_GETTIMEOUT:
0200 return put_user(timeout, (int __user *)arg);
0201 default:
0202 return -ENOTTY;
0203 }
0204 }
0205
0206 static const struct file_operations wdt_fops = {
0207 .owner = THIS_MODULE,
0208 .llseek = no_llseek,
0209 .write = fop_write,
0210 .open = fop_open,
0211 .release = fop_close,
0212 .unlocked_ioctl = fop_ioctl,
0213 .compat_ioctl = compat_ptr_ioctl,
0214 };
0215
0216 static struct miscdevice wdt_miscdev = {
0217 .minor = WATCHDOG_MINOR,
0218 .name = "watchdog",
0219 .fops = &wdt_fops,
0220 };
0221
0222
0223
0224
0225
0226 static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
0227 void *unused)
0228 {
0229 if (code == SYS_DOWN || code == SYS_HALT)
0230 wdt_disable();
0231 return NOTIFY_DONE;
0232 }
0233
0234 static struct notifier_block wdt_notifier = {
0235 .notifier_call = wdt_notify_sys,
0236 };
0237
0238 static void __exit sbc7240_wdt_unload(void)
0239 {
0240 pr_info("Removing watchdog\n");
0241 misc_deregister(&wdt_miscdev);
0242
0243 unregister_reboot_notifier(&wdt_notifier);
0244 release_region(SBC7240_ENABLE_PORT, 1);
0245 }
0246
0247 static int __init sbc7240_wdt_init(void)
0248 {
0249 int rc = -EBUSY;
0250
0251 if (!request_region(SBC7240_ENABLE_PORT, 1, "SBC7240 WDT")) {
0252 pr_err("I/O address 0x%04x already in use\n",
0253 SBC7240_ENABLE_PORT);
0254 rc = -EIO;
0255 goto err_out;
0256 }
0257
0258
0259
0260
0261
0262 if (timeout < 1 || timeout > SBC7240_MAX_TIMEOUT) {
0263 timeout = SBC7240_TIMEOUT;
0264 pr_info("timeout value must be 1<=x<=%d, using %d\n",
0265 SBC7240_MAX_TIMEOUT, timeout);
0266 }
0267 wdt_set_timeout(timeout);
0268 wdt_disable();
0269
0270 rc = register_reboot_notifier(&wdt_notifier);
0271 if (rc) {
0272 pr_err("cannot register reboot notifier (err=%d)\n", rc);
0273 goto err_out_region;
0274 }
0275
0276 rc = misc_register(&wdt_miscdev);
0277 if (rc) {
0278 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
0279 wdt_miscdev.minor, rc);
0280 goto err_out_reboot_notifier;
0281 }
0282
0283 pr_info("Watchdog driver for SBC7240 initialised (nowayout=%d)\n",
0284 nowayout);
0285
0286 return 0;
0287
0288 err_out_reboot_notifier:
0289 unregister_reboot_notifier(&wdt_notifier);
0290 err_out_region:
0291 release_region(SBC7240_ENABLE_PORT, 1);
0292 err_out:
0293 return rc;
0294 }
0295
0296 module_init(sbc7240_wdt_init);
0297 module_exit(sbc7240_wdt_unload);
0298
0299 MODULE_AUTHOR("Gilles Gigan");
0300 MODULE_DESCRIPTION("Watchdog device driver for single board"
0301 " computers EPIC Nano 7240 from iEi");
0302 MODULE_LICENSE("GPL");