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)  *	Acquire Single Board Computer Watchdog Timer driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *	Based on wdt.c. Original copyright messages:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *	(c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *						All Rights Reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *	Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  *	warranty for any of this software. This material is provided
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  *	"AS-IS" and at no charge.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  *	(c) Copyright 1995    Alan Cox <alan@lxorguk.ukuu.org.uk>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  *	14-Dec-2001 Matt Domsch <Matt_Domsch@dell.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  *	    Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  *	    Can't add timeout - driver doesn't allow changing value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  *	Theory of Operation:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  *		The Watch-Dog Timer is provided to ensure that standalone
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  *		Systems can always recover from catastrophic conditions that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  *		caused the CPU to crash. This condition may have occurred by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  *		external EMI or a software bug. When the CPU stops working
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  *		correctly, hardware on the board will either perform a hardware
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  *		reset (cold boot) or a non-maskable interrupt (NMI) to bring the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  *		system back to a known state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  *		The Watch-Dog Timer is controlled by two I/O Ports.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  *		  443 hex	- Read	- Enable or refresh the Watch-Dog Timer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  *		  043 hex	- Read	- Disable the Watch-Dog Timer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  *		To enable the Watch-Dog Timer, a read from I/O port 443h must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  *		be performed. This will enable and activate the countdown timer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  *		which will eventually time out and either reset the CPU or cause
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  *		an NMI depending on the setting of a jumper. To ensure that this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39)  *		reset condition does not occur, the Watch-Dog Timer must be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  *		periodically refreshed by reading the same I/O port 443h.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  *		The Watch-Dog Timer is disabled by reading I/O port 043h.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  *		The Watch-Dog Timer Time-Out Period is set via jumpers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44)  *		It can be 1, 2, 10, 20, 110 or 220 seconds.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48)  *	Includes, defines, variables, module parameters, ...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) /* Includes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) #include <linux/module.h>		/* For module specific items */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) #include <linux/moduleparam.h>		/* For new moduleparam's */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) #include <linux/types.h>		/* For standard types (like size_t) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) #include <linux/errno.h>		/* For the -ENODEV/... values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) #include <linux/kernel.h>		/* For printk/panic/... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) #include <linux/miscdevice.h>		/* For struct miscdevice */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) #include <linux/watchdog.h>		/* For the watchdog specific items */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) #include <linux/fs.h>			/* For file operations */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) #include <linux/ioport.h>		/* For io-port access */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) #include <linux/platform_device.h>	/* For platform_driver framework */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) #include <linux/init.h>			/* For __init/__exit/... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) #include <linux/uaccess.h>		/* For copy_to_user/put_user/... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) #include <linux/io.h>			/* For inb/outb/... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) /* Module information */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) #define DRV_NAME "acquirewdt"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) #define WATCHDOG_NAME "Acquire WDT"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) /* There is no way to see what the correct time-out period is */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) #define WATCHDOG_HEARTBEAT 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) /* internal variables */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) /* the watchdog platform device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) static struct platform_device *acq_platform_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) static unsigned long acq_is_open;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) static char expect_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) /* module parameters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) /* You must set this - there is no sane way to probe for this board. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) static int wdt_stop = 0x43;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) module_param(wdt_stop, int, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) MODULE_PARM_DESC(wdt_stop, "Acquire WDT 'stop' io port (default 0x43)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) /* You must set this - there is no sane way to probe for this board. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) static int wdt_start = 0x443;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) module_param(wdt_start, int, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) MODULE_PARM_DESC(wdt_start, "Acquire WDT 'start' io port (default 0x443)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) static bool nowayout = WATCHDOG_NOWAYOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) module_param(nowayout, bool, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) MODULE_PARM_DESC(nowayout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	"Watchdog cannot be stopped once started (default="
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98)  *	Watchdog Operations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) static void acq_keepalive(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	/* Write a watchdog value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	inb_p(wdt_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) static void acq_stop(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	/* Turn the card off */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	inb_p(wdt_stop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)  *	/dev/watchdog handling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) static ssize_t acq_write(struct file *file, const char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 						size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	/* See if we got the magic character 'V' and reload the timer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	if (count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		if (!nowayout) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 			size_t i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 			/* note: just in case someone wrote the magic character
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 			   five months ago... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 			expect_close = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 			/* scan to see whether or not we got the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 			   magic character */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 			for (i = 0; i != count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 				char c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 				if (get_user(c, buf + i))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 					return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 				if (c == 'V')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 					expect_close = 42;
^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) 		/* Well, anyhow someone wrote to us, we should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 				return that favour */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		acq_keepalive();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) static long acq_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	int options, retval = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	void __user *argp = (void __user *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	int __user *p = argp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	static const struct watchdog_info ident = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		.options = WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		.firmware_version = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		.identity = WATCHDOG_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	case WDIOC_GETSUPPORT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	case WDIOC_GETSTATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	case WDIOC_GETBOOTSTATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		return put_user(0, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	case WDIOC_SETOPTIONS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		if (get_user(options, p))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		if (options & WDIOS_DISABLECARD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 			acq_stop();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 			retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		if (options & WDIOS_ENABLECARD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 			acq_keepalive();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 			retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	case WDIOC_KEEPALIVE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		acq_keepalive();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	case WDIOC_GETTIMEOUT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		return put_user(WATCHDOG_HEARTBEAT, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		return -ENOTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) static int acq_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	if (test_and_set_bit(0, &acq_is_open))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	if (nowayout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		__module_get(THIS_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	/* Activate */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	acq_keepalive();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	return stream_open(inode, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) static int acq_close(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	if (expect_close == 42) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		acq_stop();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		pr_crit("Unexpected close, not stopping watchdog!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		acq_keepalive();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	clear_bit(0, &acq_is_open);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	expect_close = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) }
^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)  *	Kernel Interfaces
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) static const struct file_operations acq_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	.owner		= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	.llseek		= no_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	.write		= acq_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	.unlocked_ioctl	= acq_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	.compat_ioctl	= compat_ptr_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	.open		= acq_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	.release	= acq_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) static struct miscdevice acq_miscdev = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	.minor	= WATCHDOG_MINOR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	.name	= "watchdog",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	.fops	= &acq_fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)  *	Init & exit routines
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) static int __init acq_probe(struct platform_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	if (wdt_stop != wdt_start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		if (!request_region(wdt_stop, 1, WATCHDOG_NAME)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 			pr_err("I/O address 0x%04x already in use\n", wdt_stop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 			ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	if (!request_region(wdt_start, 1, WATCHDOG_NAME)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		pr_err("I/O address 0x%04x already in use\n", wdt_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		goto unreg_stop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	ret = misc_register(&acq_miscdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	if (ret != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		pr_err("cannot register miscdev on minor=%d (err=%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		       WATCHDOG_MINOR, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		goto unreg_regions;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	pr_info("initialized. (nowayout=%d)\n", nowayout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) unreg_regions:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	release_region(wdt_start, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) unreg_stop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	if (wdt_stop != wdt_start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		release_region(wdt_stop, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) static int acq_remove(struct platform_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	misc_deregister(&acq_miscdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	release_region(wdt_start, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	if (wdt_stop != wdt_start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		release_region(wdt_stop, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) static void acq_shutdown(struct platform_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	/* Turn the WDT off if we have a soft shutdown */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	acq_stop();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) static struct platform_driver acquirewdt_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	.remove		= acq_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	.shutdown	= acq_shutdown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	.driver		= {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		.name	= DRV_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) static int __init acq_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	pr_info("WDT driver for Acquire single board computer initialising\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	acq_platform_device = platform_device_register_simple(DRV_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 								-1, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	if (IS_ERR(acq_platform_device))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		return PTR_ERR(acq_platform_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	err = platform_driver_probe(&acquirewdt_driver, acq_probe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		goto unreg_platform_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) unreg_platform_device:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	platform_device_unregister(acq_platform_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) static void __exit acq_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	platform_device_unregister(acq_platform_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	platform_driver_unregister(&acquirewdt_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	pr_info("Watchdog Module Unloaded\n");
^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) module_init(acq_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) module_exit(acq_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) MODULE_AUTHOR("David Woodhouse");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) MODULE_DESCRIPTION("Acquire Inc. Single Board Computer Watchdog Timer driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) MODULE_LICENSE("GPL");