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+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *	ICP Wafer 5823 Single Board Computer WDT driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *	http://www.icpamerica.com/wafer_5823.php
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *	May also work on other similar models
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *	(c) Copyright 2002 Justin Cormack <justin@street-vision.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *	Release 0.02
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  *	Based on advantechwdt.c which is based on wdt.c.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  *	Original copyright messages:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  *	(c) Copyright 1996-1997 Alan Cox <alan@lxorguk.ukuu.org.uk>,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  *						All Rights Reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  *	Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  *	warranty for any of this software. This material is provided
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  *	"AS-IS" and at no charge.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  *	(c) Copyright 1995    Alan Cox <alan@lxorguk.ukuu.org.uk>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include <linux/moduleparam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #include <linux/miscdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #include <linux/watchdog.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #include <linux/ioport.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #include <linux/notifier.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #include <linux/reboot.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #define WATCHDOG_NAME "Wafer 5823 WDT"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #define PFX WATCHDOG_NAME ": "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #define WD_TIMO 60			/* 60 sec default timeout */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) static unsigned long wafwdt_is_open;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) static char expect_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) static DEFINE_SPINLOCK(wafwdt_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49)  *	You must set these - there is no sane way to probe for this board.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51)  *	To enable, write the timeout value in seconds (1 to 255) to I/O
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52)  *	port WDT_START, then read the port to start the watchdog. To pat
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53)  *	the dog, read port WDT_STOP to stop the timer, then read WDT_START
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54)  *	to restart it again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) static int wdt_stop = 0x843;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) static int wdt_start = 0x443;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) static int timeout = WD_TIMO;  /* in seconds */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) module_param(timeout, int, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) MODULE_PARM_DESC(timeout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		"Watchdog timeout in seconds. 1 <= timeout <= 255, default="
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 				__MODULE_STRING(WD_TIMO) ".");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) static bool nowayout = WATCHDOG_NOWAYOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) module_param(nowayout, bool, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) MODULE_PARM_DESC(nowayout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		"Watchdog cannot be stopped once started (default="
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 				__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) static void wafwdt_ping(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	/* pat watchdog */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	spin_lock(&wafwdt_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	inb_p(wdt_stop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	inb_p(wdt_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	spin_unlock(&wafwdt_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) static void wafwdt_start(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	/* start up watchdog */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	outb_p(timeout, wdt_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	inb_p(wdt_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) static void wafwdt_stop(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	/* stop watchdog */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	inb_p(wdt_stop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) static ssize_t wafwdt_write(struct file *file, const char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 						size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	/* See if we got the magic character 'V' and reload the timer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	if (count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		if (!nowayout) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 			size_t i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 			/* In case it was set long ago */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 			expect_close = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 			/* scan to see whether or not we got the magic
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 			   character */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 			for (i = 0; i != count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 				char c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 				if (get_user(c, buf + i))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 					return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 				if (c == 'V')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 					expect_close = 42;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		/* Well, anyhow someone wrote to us, we should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		   return that favour */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		wafwdt_ping();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) static long wafwdt_ioctl(struct file *file, unsigned int cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 							unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	int new_timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	void __user *argp = (void __user *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	int __user *p = argp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	static const struct watchdog_info ident = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		.options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 							WDIOF_MAGICCLOSE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		.firmware_version = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		.identity = "Wafer 5823 WDT",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	case WDIOC_GETSUPPORT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		if (copy_to_user(argp, &ident, sizeof(ident)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	case WDIOC_GETSTATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	case WDIOC_GETBOOTSTATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		return put_user(0, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	case WDIOC_SETOPTIONS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		int options, retval = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		if (get_user(options, p))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		if (options & WDIOS_DISABLECARD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 			wafwdt_stop();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 			retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		if (options & WDIOS_ENABLECARD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 			wafwdt_start();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 			retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	case WDIOC_KEEPALIVE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		wafwdt_ping();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	case WDIOC_SETTIMEOUT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		if (get_user(new_timeout, p))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		if ((new_timeout < 1) || (new_timeout > 255))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		timeout = new_timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		wafwdt_stop();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		wafwdt_start();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	case WDIOC_GETTIMEOUT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		return put_user(timeout, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		return -ENOTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	return 0;
^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) static int wafwdt_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	if (test_and_set_bit(0, &wafwdt_is_open))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	 *      Activate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	wafwdt_start();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	return stream_open(inode, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) static int wafwdt_close(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	if (expect_close == 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		wafwdt_stop();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		pr_crit("WDT device closed unexpectedly.  WDT will not stop!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		wafwdt_ping();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	clear_bit(0, &wafwdt_is_open);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	expect_close = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)  *	Notifier for system down
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) static int wafwdt_notify_sys(struct notifier_block *this, unsigned long code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 								void *unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	if (code == SYS_DOWN || code == SYS_HALT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		wafwdt_stop();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)  *	Kernel Interfaces
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) static const struct file_operations wafwdt_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	.owner		= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	.llseek		= no_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	.write		= wafwdt_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	.unlocked_ioctl	= wafwdt_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	.compat_ioctl	= compat_ptr_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	.open		= wafwdt_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	.release	= wafwdt_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) static struct miscdevice wafwdt_miscdev = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	.minor	= WATCHDOG_MINOR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	.name	= "watchdog",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	.fops	= &wafwdt_fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)  *	The WDT needs to learn about soft shutdowns in order to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)  *	turn the timebomb registers off.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) static struct notifier_block wafwdt_notifier = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	.notifier_call = wafwdt_notify_sys,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) static int __init wafwdt_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	pr_info("WDT driver for Wafer 5823 single board computer initialising\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	if (timeout < 1 || timeout > 255) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		timeout = WD_TIMO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		pr_info("timeout value must be 1 <= x <= 255, using %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 			timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	if (wdt_stop != wdt_start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		if (!request_region(wdt_stop, 1, "Wafer 5823 WDT")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 			pr_err("I/O address 0x%04x already in use\n", wdt_stop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 			ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	if (!request_region(wdt_start, 1, "Wafer 5823 WDT")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		pr_err("I/O address 0x%04x already in use\n", wdt_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		goto error2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	ret = register_reboot_notifier(&wafwdt_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	if (ret != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		pr_err("cannot register reboot notifier (err=%d)\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		goto error3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	ret = misc_register(&wafwdt_miscdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	if (ret != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		pr_err("cannot register miscdev on minor=%d (err=%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		       WATCHDOG_MINOR, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		goto error4;
^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) 	pr_info("initialized. timeout=%d sec (nowayout=%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		timeout, nowayout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) error4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	unregister_reboot_notifier(&wafwdt_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) error3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	release_region(wdt_start, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) error2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	if (wdt_stop != wdt_start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		release_region(wdt_stop, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) static void __exit wafwdt_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	misc_deregister(&wafwdt_miscdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	unregister_reboot_notifier(&wafwdt_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	if (wdt_stop != wdt_start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		release_region(wdt_stop, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	release_region(wdt_start, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) module_init(wafwdt_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) module_exit(wafwdt_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) MODULE_AUTHOR("Justin Cormack");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) MODULE_DESCRIPTION("ICP Wafer 5823 Single Board Computer WDT driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) /* end of wafer5823wdt.c */