/* * WAFER 582x Single Board Computer Watchdog Timer driver for Linux 2.2.x * * (C) Copyright 2001 Piergiorgio Ghezzo * * Based on sbc60xxwdt.c by Jakob Oestergaard. * Based on acquirewdt.c by Alan Cox. * * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version * 2 of the License, or (at your option) any later version. * * The author does NOT admit liability nor provide warranty for * any of this software. This material is provided "AS-IS" in * the hope that it may be useful for others. * * * HISTORY: * * 26/12/2001 [Initial revision] * 27/12/2001 [Added /proc/watchdog support] * * * --- Original text by Jakob Oestergaard -------------------------------- * * Theory of operation: * A Watchdog Timer (WDT) is a hardware circuit that can * reset the computer system in case of a software fault. * You probably knew that already. * * Usually a userspace daemon will notify the kernel WDT driver * via the /proc/watchdog special device file that userspace is * still alive, at regular intervals. When such a notification * occurs, the driver will usually tell the hardware watchdog * that everything is in order, and that the watchdog should wait * for yet another little while to reset the system. * If userspace fails (RAM error, kernel bug, whatever), the * notifications cease to occur, and the hardware watchdog will * reset the system (causing a reboot) after the timeout occurs. * * This WDT driver is different from the other Linux WDT * drivers in several ways: * *) The driver will ping the watchdog by itself, because this * particular WDT has a very short timeout (one second) and it * would be insane to count on any userspace daemon always * getting scheduled within that time frame. * *) This driver expects the userspace daemon to send a specific * character code ('V') to /dev/watchdog before closing the * /dev/watchdog file. If the userspace daemon closes the file * without sending this special character, the driver will assume * that the daemon (and userspace in general) died, and will * stop pinging the WDT without disabling it first. This will * cause a reboot. * * Why `V' ? Well, `V' is the character in ASCII for the value 86, * and we all know that 86 is _the_ most random number in the universe. * Therefore it is the letter that has the slightest chance of occurring * by chance, when the system becomes corrupted. * * ----------------------------------------------------------------------- */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define DEBUG(x) if (debug) { x }; #define OUR_NAME "wafer582xwdt" //#define CONFIG_582X_WDT_USE_PROC /*** Module parameters *******************************************************/ static int debug = 0; // Debug flag static int timeout = 7; // Watchdog timeout value (seconds) static int ping = 5; // Module ping delay (seconds) static int devtimeout = 10; // /dev/watchdog timeout value (seconds) static int proctimeout[10] = // /prox/n timeout value (seconds) { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; static unsigned long next_heartbeat[11] = // 10 proc + 1 dev { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; static int wdt_is_active = 0; static int wdt_dev_is_open = 0; static int wdt_expect_close = 0; MODULE_AUTHOR("Piergiorgio Ghezzo "); MODULE_DESCRIPTION("WDT driver for 582x single board computer"); MODULE_PARM(debug, "i"); MODULE_PARM(timeout, "i"); MODULE_PARM(ping, "i"); MODULE_PARM(devtimeout, "i"); /* You must set these - The driver cannot probe for the settings */ #define WDT_STOP 0x843 // watchdog start register #define WDT_START 0x443 // watchdog stop register static void wdt_timer_ping(unsigned long); static struct timer_list timer = { 0, 0, 0, 0, wdt_timer_ping }; /*** Whack the dog routine ***************************************************/ static void wdt_timer_ping(unsigned long data) { char IsGood = 'Y'; int Ciclo; DEBUG ( printk(OUR_NAME ": jiffies=%ld - wdt_dev_is_open=%d - wdt_is_active=%d\n", jiffies, wdt_dev_is_open, wdt_is_active); ) if (wdt_dev_is_open == 1 && time_after(jiffies, next_heartbeat[10])) IsGood = 'N'; #ifdef CONFIG_582X_WDT_USE_PROC for (Ciclo = 0; Ciclo < 10; Ciclo++) if (proctimeout[Ciclo] != 0 && time_after(jiffies, next_heartbeat[Ciclo])) IsGood = 'N'; #endif if (IsGood == 'Y') { /* Ping the WDT by reading from WDT_STOP and WDT_START */ inb_p(WDT_STOP); inb_p(WDT_START); /* Re-set the timer interval */ timer.expires = jiffies + (ping * HZ); add_timer(&timer); DEBUG ( printk(OUR_NAME ": Ping sent to the WDT\n"); ) } else { printk(OUR_NAME ": User heartbeat lost! Will not ping the watchdog\n"); } } static void wdt_startup(void) { /* Set the timeout */ outb_p(timeout, WDT_START); /* Start the timer */ timer.expires = jiffies + (ping * HZ); add_timer(&timer); printk(OUR_NAME ": Watchdog timer is now enabled.\n"); } static void wdt_turnoff(void) { /* Stop the timer */ del_timer(&timer); inb_p(WDT_STOP); printk(OUR_NAME ": Watchdog timer is now disabled.\n"); } /*** /dev/watchdog handling **************************************************/ static ssize_t wdt_dev_write(struct file * file, const char * buf, size_t count, loff_t * ppos) { /* We can't seek */ if (ppos != &file->f_pos) return -ESPIPE; /* See if we got the magic character */ if (count) { size_t ofs; /* note: just in case someone wrote the magic character * five months ago... */ wdt_expect_close = 0; /* now scan */ for (ofs = 0; ofs != count; ofs++) { char c; if (get_user(c, buf+ofs)) return -EFAULT; if (c == 'V') { DEBUG ( printk(OUR_NAME ": DEBUG: Received application data: special character\n"); ) wdt_expect_close = 1; } else DEBUG ( printk(OUR_NAME ": DEBUG: Received application data: other character\n"); ) } /* Well, anyhow someone wrote to us, we should return that favour */ next_heartbeat[10] = jiffies + (devtimeout * HZ); } return 0; } static ssize_t wdt_dev_read(struct file * file, char * buf, size_t count, loff_t * ppos) { /* No can do */ return -EINVAL; } static int wdt_dev_open(struct inode * inode, struct file * file) { switch (MINOR(inode->i_rdev)) { case WATCHDOG_MINOR: /* Just in case we're already talking to someone... */ if (wdt_dev_is_open) return -EBUSY; /* Good, fire up the show */ MOD_INC_USE_COUNT; next_heartbeat[10] = jiffies + (devtimeout * HZ); wdt_dev_is_open=1; if (wdt_is_active++ == 0) wdt_startup(); return 0; default: return -ENODEV; } } static int wdt_dev_close(struct inode * inode, struct file * file) { if (MINOR(inode->i_rdev) == WATCHDOG_MINOR) { if (wdt_expect_close) { if (wdt_is_active-- == 1) wdt_turnoff(); } else { del_timer(&timer); printk(OUR_NAME ": device file closed unexpectedly. I reboot the system!\n"); } } wdt_dev_is_open=0; MOD_DEC_USE_COUNT; return 0; } static struct file_operations wdt_dev_fops = { 0, wdt_dev_read, wdt_dev_write, 0, 0, 0, 0, wdt_dev_open, 0, wdt_dev_close }; static struct miscdevice wdt_miscdev = { WATCHDOG_MINOR, "watchdog", &wdt_dev_fops }; /*** /proc/watchdog handling *************************************************/ #ifdef CONFIG_582X_WDT_USE_PROC int wdt_proc_open(struct inode *inode, struct file *file) { MOD_INC_USE_COUNT; return 0; } int wdt_wdt_proc_close(struct inode *inode, struct file *file) { MOD_DEC_USE_COUNT; return 0; } static ssize_t status_output(struct file *file, char *Buffer, size_t len, loff_t *offset) { static int finished = 0; char OutString[500]; int Ciclo; if (finished) { finished = 0; return 0; } sprintf(OutString, "The watchdog is now %s.\n" "WDT timeout: %d\n" "Module ping: %d\n" "/dev/watchdog timeout: %d - %s\n" "/proc/watchdog/0 timeout: %d\n" "/proc/watchdog/1 timeout: %d\n" "/proc/watchdog/2 timeout: %d\n" "/proc/watchdog/3 timeout: %d\n" "/proc/watchdog/4 timeout: %d\n" "/proc/watchdog/5 timeout: %d\n" "/proc/watchdog/6 timeout: %d\n" "/proc/watchdog/7 timeout: %d\n" "/proc/watchdog/8 timeout: %d\n" "/proc/watchdog/9 timeout: %d\n", (wdt_is_active != 0 ? "enabled" : "disabled"), timeout, ping, devtimeout, (wdt_dev_is_open != 0 ? "enabled" : "disabled"), proctimeout[0], proctimeout[1], proctimeout[2], proctimeout[3], proctimeout[4], proctimeout[5], proctimeout[6], proctimeout[7], proctimeout[8], proctimeout[9]); for (Ciclo=0; Ciclof_dentry->d_name.name; /* Show out the timeout */ sprintf(OutString, "%d\n", proctimeout[(*FileName)-48]); for (Ciclo=0; Ciclo 4) return Length; /* Get the file name */ FileName = (char *)file->f_dentry->d_name.name; /* Get the timeout value from the userspace buffer */ for (Ciclo = 0; Ciclo < sizeof(Valore)-1 && Ciclo < Length; Ciclo++) get_user (Valore[Ciclo], Buffer + Ciclo); Valore[Ciclo] = '\0'; NewTimeout = (int) simple_strtoul(Valore, NULL, 0); OldTimeout = proctimeout[(*FileName)-48]; if (NewTimeout != 0) { next_heartbeat[(*FileName)-48] = jiffies + (NewTimeout * HZ); // Start a new check if (OldTimeout == 0) { DEBUG ( printk(OUR_NAME ": Start a new proc check %c\n", *FileName); ) if (wdt_is_active++ == 0) wdt_startup(); } } // Stop an existing check if (NewTimeout == 0 && OldTimeout != 0) { DEBUG ( printk(OUR_NAME ": Stop an existing proc check %c\n", *FileName); ) if (wdt_is_active-- == 1) wdt_turnoff(); } proctimeout[(*FileName)-48] = NewTimeout; return Length; } int wdt_proc_permission(struct inode *inode, int op) { /* We allow everybody to read from our files, */ /* but only root (uid 0) may write to them in the out files */ if (op == 4 || (op == 2 && current->euid == 0 && (inode->i_mode & S_IWUSR) != 0)) return 0; /* If it's anything else, access is denied */ return -EACCES; } /* File operations for our proc files */ static struct file_operations WDT_FileOps_Proc_Status = { NULL, status_output, NULL, NULL, NULL, NULL, NULL, wdt_proc_open, NULL, wdt_wdt_proc_close, }; static struct file_operations WDT_FileOps_Proc_Files = { NULL, proc_output, proc_input, NULL, NULL, NULL, NULL, wdt_proc_open, NULL, wdt_wdt_proc_close, }; /* Inode operations for our proc files */ static struct inode_operations WDT_Inode_Proc_Status = { &WDT_FileOps_Proc_Status, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, wdt_proc_permission }; static struct inode_operations WDT_Inode_Proc_Files = { &WDT_FileOps_Proc_Files, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, wdt_proc_permission }; /* Directory entries */ static struct proc_dir_entry WDT_Proc_Dir = { 0, 8, "watchdog", S_IFDIR | S_IRUGO | S_IWUSR, 1, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL }; static struct proc_dir_entry WDT_Proc_Status = { 0, 6, "status", S_IFREG | S_IRUGO, 1, 0, 0, 0, &WDT_Inode_Proc_Status, NULL, NULL, NULL, NULL, NULL }; static struct proc_dir_entry WDT_Proc_File0 = { 0, 1, "0", S_IFREG | S_IRUGO | S_IWUSR, 1, 0, 0, 0, &WDT_Inode_Proc_Files, NULL, NULL, NULL, NULL, NULL }; static struct proc_dir_entry WDT_Proc_File1 = { 0, 1, "1", S_IFREG | S_IRUGO | S_IWUSR, 1, 0, 0, 0, &WDT_Inode_Proc_Files, NULL, NULL, NULL, NULL, NULL }; static struct proc_dir_entry WDT_Proc_File2 = { 0, 1, "2", S_IFREG | S_IRUGO | S_IWUSR, 1, 0, 0, 0, &WDT_Inode_Proc_Files, NULL, NULL, NULL, NULL, NULL }; static struct proc_dir_entry WDT_Proc_File3 = { 0, 1, "3", S_IFREG | S_IRUGO | S_IWUSR, 1, 0, 0, 0, &WDT_Inode_Proc_Files, NULL, NULL, NULL, NULL, NULL }; static struct proc_dir_entry WDT_Proc_File4 = { 0, 1, "4", S_IFREG | S_IRUGO | S_IWUSR, 1, 0, 0, 0, &WDT_Inode_Proc_Files, NULL, NULL, NULL, NULL, NULL }; static struct proc_dir_entry WDT_Proc_File5 = { 0, 1, "5", S_IFREG | S_IRUGO | S_IWUSR, 1, 0, 0, 0, &WDT_Inode_Proc_Files, NULL, NULL, NULL, NULL, NULL }; static struct proc_dir_entry WDT_Proc_File6 = { 0, 1, "6", S_IFREG | S_IRUGO | S_IWUSR, 1, 0, 0, 0, &WDT_Inode_Proc_Files, NULL, NULL, NULL, NULL, NULL }; static struct proc_dir_entry WDT_Proc_File7 = { 0, 1, "7", S_IFREG | S_IRUGO | S_IWUSR, 1, 0, 0, 0, &WDT_Inode_Proc_Files, NULL, NULL, NULL, NULL, NULL }; static struct proc_dir_entry WDT_Proc_File8 = { 0, 1, "8", S_IFREG | S_IRUGO | S_IWUSR, 1, 0, 0, 0, &WDT_Inode_Proc_Files, NULL, NULL, NULL, NULL, NULL }; static struct proc_dir_entry WDT_Proc_File9 = { 0, 1, "9", S_IFREG | S_IRUGO | S_IWUSR, 1, 0, 0, 0, &WDT_Inode_Proc_Files, NULL, NULL, NULL, NULL, NULL }; #endif /*** Notifier for system down ************************************************/ static int wdt_notify_sys(struct notifier_block *this, unsigned long code, void *unused) { if (code == SYS_DOWN || code == SYS_HALT) wdt_turnoff(); return NOTIFY_DONE; } /* * The WDT needs to learn about soft shutdowns in order to * turn the timebomb registers off. */ static struct notifier_block wdt_notifier = { wdt_notify_sys, 0, 0 }; /*** Module routines *********************************************************/ #ifdef MODULE #define wafer582xwdt_init init_module void cleanup_module(void) { wdt_turnoff(); unregister_reboot_notifier(&wdt_notifier); #ifdef CONFIG_582X_WDT_USE_PROC proc_unregister(&WDT_Proc_Dir, WDT_Proc_File9.low_ino); proc_unregister(&WDT_Proc_Dir, WDT_Proc_File8.low_ino); proc_unregister(&WDT_Proc_Dir, WDT_Proc_File7.low_ino); proc_unregister(&WDT_Proc_Dir, WDT_Proc_File6.low_ino); proc_unregister(&WDT_Proc_Dir, WDT_Proc_File5.low_ino); proc_unregister(&WDT_Proc_Dir, WDT_Proc_File4.low_ino); proc_unregister(&WDT_Proc_Dir, WDT_Proc_File3.low_ino); proc_unregister(&WDT_Proc_Dir, WDT_Proc_File2.low_ino); proc_unregister(&WDT_Proc_Dir, WDT_Proc_File1.low_ino); proc_unregister(&WDT_Proc_Dir, WDT_Proc_File0.low_ino); proc_unregister(&WDT_Proc_Dir, WDT_Proc_Status.low_ino); proc_unregister(&proc_root, WDT_Proc_Dir.low_ino); #endif misc_deregister(&wdt_miscdev); release_region(WDT_STOP,1); release_region(WDT_START,1); } #endif #if defined(__init) int __init wafer582xwdt_init(void) #elif defined(__initfunc) __initfunc(int wafer582xwdt_init(void)) #else int wafer582xwdt_init(void) #endif { printk(OUR_NAME ": WDT driver for WAFER 582x single board computer initialising\n"); printk(OUR_NAME ": WDT timeout: %ds - Module ping: %ds - /dev/watchdog timeout: %ds\n", timeout, ping, devtimeout); request_region(WDT_STOP, 1, "WAFER 582x WDT"); request_region(WDT_START, 1, "WAFER 582x WDT"); misc_register(&wdt_miscdev); #ifdef CONFIG_582X_WDT_USE_PROC proc_register(&proc_root, &WDT_Proc_Dir); proc_register(&WDT_Proc_Dir, &WDT_Proc_Status); proc_register(&WDT_Proc_Dir, &WDT_Proc_File0); proc_register(&WDT_Proc_Dir, &WDT_Proc_File1); proc_register(&WDT_Proc_Dir, &WDT_Proc_File2); proc_register(&WDT_Proc_Dir, &WDT_Proc_File3); proc_register(&WDT_Proc_Dir, &WDT_Proc_File4); proc_register(&WDT_Proc_Dir, &WDT_Proc_File5); proc_register(&WDT_Proc_Dir, &WDT_Proc_File6); proc_register(&WDT_Proc_Dir, &WDT_Proc_File7); proc_register(&WDT_Proc_Dir, &WDT_Proc_File8); proc_register(&WDT_Proc_Dir, &WDT_Proc_File9); #endif register_reboot_notifier(&wdt_notifier); return 0; }