Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *	60xx Single Board Computer Watchdog Timer driver for Linux 2.2.x
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *	Based on acquirewdt.c by Alan Cox.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *	The author does NOT admit liability nor provide warranty for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *	any of this software. This material is provided "AS-IS" in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *	the hope that it may be useful for others.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  *	(c) Copyright 2000    Jakob Oestergaard <jakob@unthought.net>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  *           12/4 - 2000      [Initial revision]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  *           25/4 - 2000      Added /dev/watchdog support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  *           09/5 - 2001      [smj@oro.net] fixed fop_write to "return 1"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  *					on success
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  *           12/4 - 2002      [rob@osinvestor.com] eliminate fop_read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  *                            fix possible wdt_is_open race
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  *                            add CONFIG_WATCHDOG_NOWAYOUT support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  *                            remove lock_kernel/unlock_kernel pairs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  *                            added KERN_* to printk's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  *                            got rid of extraneous comments
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  *                            changed watchdog_info to correctly reflect what
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  *			      the driver offers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  *			      added WDIOC_GETSTATUS, WDIOC_GETBOOTSTATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  *			      WDIOC_SETTIMEOUT, WDIOC_GETTIMEOUT, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  *			      WDIOC_SETOPTIONS ioctls
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  *           09/8 - 2003      [wim@iguana.be] cleanup of trailing spaces
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  *                            use module_param
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  *                            made timeout (the emulated heartbeat) a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  *			      module_param
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  *                            made the keepalive ping an internal subroutine
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  *                            made wdt_stop and wdt_start module params
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  *                            added extra printk's for startup problems
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  *                            added MODULE_AUTHOR and MODULE_DESCRIPTION info
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  *  This WDT driver is different from the other Linux WDT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  *  drivers in the following ways:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39)  *  *)  The driver will ping the watchdog by itself, because this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  *      particular WDT has a very short timeout (one second) and it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  *      would be insane to count on any userspace daemon always
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  *      getting scheduled within that time frame.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) #include <linux/moduleparam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) #include <linux/timer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) #include <linux/miscdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) #include <linux/watchdog.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) #include <linux/ioport.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) #include <linux/notifier.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) #include <linux/reboot.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) #define OUR_NAME "sbc60xxwdt"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) #define PFX OUR_NAME ": "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67)  * You must set these - The driver cannot probe for the settings
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) static int wdt_stop = 0x45;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) module_param(wdt_stop, int, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) MODULE_PARM_DESC(wdt_stop, "SBC60xx WDT 'stop' io port (default 0x45)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) static int wdt_start = 0x443;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) module_param(wdt_start, int, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) MODULE_PARM_DESC(wdt_start, "SBC60xx WDT 'start' io port (default 0x443)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79)  * The 60xx board can use watchdog timeout values from one second
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80)  * to several minutes.  The default is one second, so if we reset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81)  * the watchdog every ~250ms we should be safe.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) #define WDT_INTERVAL (HZ/4+1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87)  * We must not require too good response from the userspace daemon.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88)  * Here we require the userspace daemon to send us a heartbeat
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89)  * char to /dev/watchdog every 30 seconds.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90)  * If the daemon pulses us every 25 seconds, we can still afford
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91)  * a 5 second scheduling delay on the (high priority) daemon. That
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92)  * should be sufficient for a box under any load.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) #define WATCHDOG_TIMEOUT 30		/* 30 sec default timeout */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) static int timeout = WATCHDOG_TIMEOUT;	/* in seconds, multiplied by HZ to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 					   get seconds to wait for a ping */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) module_param(timeout, int, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) MODULE_PARM_DESC(timeout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	"Watchdog timeout in seconds. (1<=timeout<=3600, default="
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 				__MODULE_STRING(WATCHDOG_TIMEOUT) ")");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static bool nowayout = WATCHDOG_NOWAYOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) module_param(nowayout, bool, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) MODULE_PARM_DESC(nowayout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	"Watchdog cannot be stopped once started (default="
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 				__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) static void wdt_timer_ping(struct timer_list *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) static DEFINE_TIMER(timer, wdt_timer_ping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) static unsigned long next_heartbeat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) static unsigned long wdt_is_open;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) static char wdt_expect_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)  *	Whack the dog
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static void wdt_timer_ping(struct timer_list *unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	/* If we got a heartbeat pulse within the WDT_US_INTERVAL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	 * we agree to ping the WDT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	if (time_before(jiffies, next_heartbeat)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		/* Ping the WDT by reading from wdt_start */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		inb_p(wdt_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		/* Re-set the timer interval */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		mod_timer(&timer, jiffies + WDT_INTERVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		pr_warn("Heartbeat lost! Will not ping the watchdog\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)  * Utility routines
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) static void wdt_startup(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	next_heartbeat = jiffies + (timeout * HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	/* Start the timer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	mod_timer(&timer, jiffies + WDT_INTERVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	pr_info("Watchdog timer is now enabled\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) static void wdt_turnoff(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	/* Stop the timer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	del_timer_sync(&timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	inb_p(wdt_stop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	pr_info("Watchdog timer is now disabled...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) static void wdt_keepalive(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	/* user land ping */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	next_heartbeat = jiffies + (timeout * HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)  * /dev/watchdog handling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) static ssize_t fop_write(struct file *file, const char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 						size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	/* See if we got the magic character 'V' and reload the timer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	if (count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		if (!nowayout) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 			size_t ofs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 			/* note: just in case someone wrote the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 			   magic character five months ago... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 			wdt_expect_close = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 			/* scan to see whether or not we got the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 			   magic character */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 			for (ofs = 0; ofs != count; ofs++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 				char c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 				if (get_user(c, buf + ofs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 					return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 				if (c == 'V')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 					wdt_expect_close = 42;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		/* Well, anyhow someone wrote to us, we should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		   return that favour */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		wdt_keepalive();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) static int fop_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	/* Just in case we're already talking to someone... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	if (test_and_set_bit(0, &wdt_is_open))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	if (nowayout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		__module_get(THIS_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	/* Good, fire up the show */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	wdt_startup();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	return stream_open(inode, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) static int fop_close(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	if (wdt_expect_close == 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		wdt_turnoff();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		del_timer(&timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		pr_crit("device file closed unexpectedly. Will not stop the WDT!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	clear_bit(0, &wdt_is_open);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	wdt_expect_close = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) static long fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	void __user *argp = (void __user *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	int __user *p = argp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	static const struct watchdog_info ident = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		.options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 							WDIOF_MAGICCLOSE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		.firmware_version = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		.identity = "SBC60xx",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	case WDIOC_GETSUPPORT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	case WDIOC_GETSTATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	case WDIOC_GETBOOTSTATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		return put_user(0, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	case WDIOC_SETOPTIONS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		int new_options, retval = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		if (get_user(new_options, p))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		if (new_options & WDIOS_DISABLECARD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 			wdt_turnoff();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 			retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		if (new_options & WDIOS_ENABLECARD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 			wdt_startup();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 			retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	case WDIOC_KEEPALIVE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		wdt_keepalive();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	case WDIOC_SETTIMEOUT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		int new_timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		if (get_user(new_timeout, p))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		/* arbitrary upper limit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		if (new_timeout < 1 || new_timeout > 3600)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		timeout = new_timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		wdt_keepalive();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	case WDIOC_GETTIMEOUT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		return put_user(timeout, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		return -ENOTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) static const struct file_operations wdt_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	.owner		= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	.llseek		= no_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	.write		= fop_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	.open		= fop_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	.release	= fop_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	.unlocked_ioctl	= fop_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	.compat_ioctl	= compat_ptr_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) static struct miscdevice wdt_miscdev = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	.minor = WATCHDOG_MINOR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	.name = "watchdog",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	.fops = &wdt_fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)  *	Notifier for system down
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	void *unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	if (code == SYS_DOWN || code == SYS_HALT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		wdt_turnoff();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)  *	The WDT needs to learn about soft shutdowns in order to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)  *	turn the timebomb registers off.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) static struct notifier_block wdt_notifier = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	.notifier_call = wdt_notify_sys,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) static void __exit sbc60xxwdt_unload(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	wdt_turnoff();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	/* Deregister */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	misc_deregister(&wdt_miscdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	unregister_reboot_notifier(&wdt_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	if ((wdt_stop != 0x45) && (wdt_stop != wdt_start))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		release_region(wdt_stop, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	release_region(wdt_start, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) static int __init sbc60xxwdt_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	int rc = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	if (timeout < 1 || timeout > 3600) { /* arbitrary upper limit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		timeout = WATCHDOG_TIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		pr_info("timeout value must be 1 <= x <= 3600, using %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 			timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	if (!request_region(wdt_start, 1, "SBC 60XX WDT")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 		pr_err("I/O address 0x%04x already in use\n", wdt_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 		rc = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	/* We cannot reserve 0x45 - the kernel already has! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	if (wdt_stop != 0x45 && wdt_stop != wdt_start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		if (!request_region(wdt_stop, 1, "SBC 60XX WDT")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 			pr_err("I/O address 0x%04x already in use\n", wdt_stop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 			rc = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 			goto err_out_region1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	rc = register_reboot_notifier(&wdt_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 		pr_err("cannot register reboot notifier (err=%d)\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 		goto err_out_region2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	rc = misc_register(&wdt_miscdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 		pr_err("cannot register miscdev on minor=%d (err=%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 		       wdt_miscdev.minor, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 		goto err_out_reboot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	pr_info("WDT driver for 60XX single board computer initialised. timeout=%d sec (nowayout=%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 		timeout, nowayout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) err_out_reboot:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	unregister_reboot_notifier(&wdt_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) err_out_region2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	if (wdt_stop != 0x45 && wdt_stop != wdt_start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 		release_region(wdt_stop, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) err_out_region1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	release_region(wdt_start, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) module_init(sbc60xxwdt_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) module_exit(sbc60xxwdt_unload);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) MODULE_AUTHOR("Jakob Oestergaard <jakob@unthought.net>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) MODULE_DESCRIPTION("60xx Single Board Computer Watchdog Timer driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) MODULE_LICENSE("GPL");