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)  *	Industrial Computer Source WDT501 driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *	(c) Copyright 1996-1997 Alan Cox <alan@lxorguk.ukuu.org.uk>,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *						All Rights Reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *	Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *	warranty for any of this software. This material is provided
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *	"AS-IS" and at no charge.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  *	(c) Copyright 1995    Alan Cox <alan@lxorguk.ukuu.org.uk>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  *	Release 0.10.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  *	Fixes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  *		Dave Gregorich	:	Modularisation and minor bugs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  *		Alan Cox	:	Added the watchdog ioctl() stuff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  *		Alan Cox	:	Fixed the reboot problem (as noted by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  *					Matt Crocker).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  *		Alan Cox	:	Added wdt= boot option
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  *		Alan Cox	:	Cleaned up copy/user stuff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  *		Tim Hockin	:	Added insmod parameters, comment
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  *					cleanup, parameterized timeout
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  *		Tigran Aivazian	:	Restructured wdt_init() to handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  *					failures
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  *		Joel Becker	:	Added WDIOC_GET/SETTIMEOUT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  *		Matt Domsch	:	Added nowayout module option
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #include <linux/moduleparam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #include <linux/miscdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #include <linux/watchdog.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #include <linux/ioport.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #include <linux/notifier.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #include <linux/reboot.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) #include "wd501p.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) static unsigned long wdt_is_open;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) static char expect_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53)  *	Module parameters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) #define WD_TIMO 60			/* Default heartbeat = 60 seconds */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) static int heartbeat = WD_TIMO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) static int wd_heartbeat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) module_param(heartbeat, int, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) MODULE_PARM_DESC(heartbeat,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	"Watchdog heartbeat in seconds. (0 < heartbeat < 65536, default="
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 				__MODULE_STRING(WD_TIMO) ")");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) static bool nowayout = WATCHDOG_NOWAYOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) module_param(nowayout, bool, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) MODULE_PARM_DESC(nowayout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	"Watchdog cannot be stopped once started (default="
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 				__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) /* You must set these - there is no sane way to probe for this board. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) static int io = 0x240;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) static int irq = 11;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) static DEFINE_SPINLOCK(wdt_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) module_param_hw(io, int, ioport, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) MODULE_PARM_DESC(io, "WDT io port (default=0x240)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) module_param_hw(irq, int, irq, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) MODULE_PARM_DESC(irq, "WDT irq (default=11)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) /* Support for the Fan Tachometer on the WDT501-P */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) static int tachometer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) module_param(tachometer, int, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) MODULE_PARM_DESC(tachometer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		"WDT501-P Fan Tachometer support (0=disable, default=0)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) static int type = 500;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) module_param(type, int, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) MODULE_PARM_DESC(type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		"WDT501-P Card type (500 or 501, default=500)");
^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)  *	Programming support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) static void wdt_ctr_mode(int ctr, int mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	ctr <<= 6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	ctr |= 0x30;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	ctr |= (mode << 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	outb_p(ctr, WDT_CR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) static void wdt_ctr_load(int ctr, int val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	outb_p(val&0xFF, WDT_COUNT0+ctr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	outb_p(val>>8, WDT_COUNT0+ctr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)  *	wdt_start:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)  *	Start the watchdog driver.
^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 int wdt_start(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	spin_lock_irqsave(&wdt_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	inb_p(WDT_DC);			/* Disable watchdog */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	wdt_ctr_mode(0, 3);		/* Program CTR0 for Mode 3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 						Square Wave Generator */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	wdt_ctr_mode(1, 2);		/* Program CTR1 for Mode 2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 						Rate Generator */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	wdt_ctr_mode(2, 0);		/* Program CTR2 for Mode 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 						Pulse on Terminal Count */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	wdt_ctr_load(0, 8948);		/* Count at 100Hz */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	wdt_ctr_load(1, wd_heartbeat);	/* Heartbeat */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	wdt_ctr_load(2, 65535);		/* Length of reset pulse */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	outb_p(0, WDT_DC);		/* Enable watchdog */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	spin_unlock_irqrestore(&wdt_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^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)  *	wdt_stop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)  *	Stop the watchdog driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) static int wdt_stop(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	spin_lock_irqsave(&wdt_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	/* Turn the card off */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	inb_p(WDT_DC);			/* Disable watchdog */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	wdt_ctr_load(2, 0);		/* 0 length reset pulses now */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	spin_unlock_irqrestore(&wdt_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^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)  *	wdt_ping:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)  *	Reload counter one with the watchdog heartbeat. We don't bother
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)  *	reloading the cascade counter.
^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) static void wdt_ping(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	spin_lock_irqsave(&wdt_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	/* Write a watchdog value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	inb_p(WDT_DC);			/* Disable watchdog */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	wdt_ctr_mode(1, 2);		/* Re-Program CTR1 for Mode 2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 							Rate Generator */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	wdt_ctr_load(1, wd_heartbeat);	/* Heartbeat */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	outb_p(0, WDT_DC);		/* Enable watchdog */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	spin_unlock_irqrestore(&wdt_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)  *	wdt_set_heartbeat:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)  *	@t:		the new heartbeat value that needs to be set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)  *	Set a new heartbeat value for the watchdog device. If the heartbeat
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)  *	value is incorrect we keep the old value and return -EINVAL. If
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)  *	successful we return 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) static int wdt_set_heartbeat(int t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	if (t < 1 || t > 65535)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	heartbeat = t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	wd_heartbeat = t * 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^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)  *	wdt_get_status:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)  *	Extract the status information from a WDT watchdog device. There are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)  *	several board variants so we have to know which bits are valid. Some
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)  *	bits default to one and some to zero in order to be maximally painful.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)  *	we then map the bits onto the status ioctl flags.
^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 wdt_get_status(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	unsigned char new_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	int status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	spin_lock_irqsave(&wdt_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	new_status = inb_p(WDT_SR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	spin_unlock_irqrestore(&wdt_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	if (new_status & WDC_SR_ISOI0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		status |= WDIOF_EXTERN1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	if (new_status & WDC_SR_ISII1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		status |= WDIOF_EXTERN2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	if (type == 501) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		if (!(new_status & WDC_SR_TGOOD))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 			status |= WDIOF_OVERHEAT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		if (!(new_status & WDC_SR_PSUOVER))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 			status |= WDIOF_POWEROVER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		if (!(new_status & WDC_SR_PSUUNDR))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 			status |= WDIOF_POWERUNDER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		if (tachometer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 			if (!(new_status & WDC_SR_FANGOOD))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 				status |= WDIOF_FANFAULT;
^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) 	return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^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)  *	wdt_get_temperature:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)  *	Reports the temperature in degrees Fahrenheit. The API is in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)  *	farenheit. It was designed by an imperial measurement luddite.
^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 int wdt_get_temperature(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	unsigned short c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	spin_lock_irqsave(&wdt_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	c = inb_p(WDT_RT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	spin_unlock_irqrestore(&wdt_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	return (c * 11 / 15) + 7;
^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 void wdt_decode_501(int status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	if (!(status & WDC_SR_TGOOD))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		pr_crit("Overheat alarm (%d)\n", inb_p(WDT_RT));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	if (!(status & WDC_SR_PSUOVER))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		pr_crit("PSU over voltage\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	if (!(status & WDC_SR_PSUUNDR))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		pr_crit("PSU under voltage\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)  *	wdt_interrupt:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)  *	@irq:		Interrupt number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)  *	@dev_id:	Unused as we don't allow multiple devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)  *	Handle an interrupt from the board. These are raised when the status
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)  *	map changes in what the board considers an interesting way. That means
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)  *	a failure condition occurring.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) static irqreturn_t wdt_interrupt(int irq, void *dev_id)
^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) 	 *	Read the status register see what is up and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	 *	then printk it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	unsigned char status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	spin_lock(&wdt_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	status = inb_p(WDT_SR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	pr_crit("WDT status %d\n", status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	if (type == 501) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		wdt_decode_501(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		if (tachometer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 			if (!(status & WDC_SR_FANGOOD))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 				pr_crit("Possible fan fault\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	if (!(status & WDC_SR_WCCR)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) #ifdef SOFTWARE_REBOOT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) #ifdef ONLY_TESTING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		pr_crit("Would Reboot\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		pr_crit("Initiating system reboot\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		emergency_restart();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		pr_crit("Reset in 5ms\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	spin_unlock(&wdt_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	return IRQ_HANDLED;
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)  *	wdt_write:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)  *	@file: file handle to the watchdog
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)  *	@buf: buffer to write (unused as data does not matter here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)  *	@count: count of bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)  *	@ppos: pointer to the position to write. No seeks allowed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)  *	A write to a watchdog device is defined as a keepalive signal. Any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)  *	write of data will do, as we we don't define content meaning.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) static ssize_t wdt_write(struct file *file, const char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 						size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	if (count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		if (!nowayout) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 			size_t i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 			/* In case it was set long ago */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 			expect_close = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 			for (i = 0; i != count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 				char c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 				if (get_user(c, buf + i))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 					return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 				if (c == 'V')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 					expect_close = 42;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		wdt_ping();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)  *	wdt_ioctl:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)  *	@file: file handle to the device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)  *	@cmd: watchdog command
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)  *	@arg: argument pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)  *	The watchdog API defines a common set of functions for all watchdogs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)  *	according to their available features. We only actually usefully support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)  *	querying capabilities and current status.
^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) static long wdt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	void __user *argp = (void __user *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	int __user *p = argp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	int new_heartbeat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	struct watchdog_info ident = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 		.options =		WDIOF_SETTIMEOUT|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 					WDIOF_MAGICCLOSE|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 					WDIOF_KEEPALIVEPING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		.firmware_version =	1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 		.identity =		"WDT500/501",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	/* Add options according to the card we have */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	ident.options |= (WDIOF_EXTERN1|WDIOF_EXTERN2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	if (type == 501) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 		ident.options |= (WDIOF_OVERHEAT|WDIOF_POWERUNDER|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 							WDIOF_POWEROVER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		if (tachometer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 			ident.options |= WDIOF_FANFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	case WDIOC_GETSUPPORT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 		return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	case WDIOC_GETSTATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 		status = wdt_get_status();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 		return put_user(status, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	case WDIOC_GETBOOTSTATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 		return put_user(0, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	case WDIOC_KEEPALIVE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 		wdt_ping();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	case WDIOC_SETTIMEOUT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 		if (get_user(new_heartbeat, p))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 		if (wdt_set_heartbeat(new_heartbeat))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 		wdt_ping();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 		fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	case WDIOC_GETTIMEOUT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 		return put_user(heartbeat, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 		return -ENOTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)  *	wdt_open:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402)  *	@inode: inode of device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)  *	@file: file handle to device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)  *	The watchdog device has been opened. The watchdog device is single
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)  *	open and on opening we load the counters. Counter zero is a 100Hz
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)  *	cascade, into counter 1 which downcounts to reboot. When the counter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)  *	triggers counter 2 downcounts the length of the reset pulse which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)  *	set set to be as long as possible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) static int wdt_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	if (test_and_set_bit(0, &wdt_is_open))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	 *	Activate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	wdt_start();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	return stream_open(inode, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)  *	wdt_release:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425)  *	@inode: inode to board
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)  *	@file: file handle to board
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)  *	The watchdog has a configurable API. There is a religious dispute
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429)  *	between people who want their watchdog to be able to shut down and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430)  *	those who want to be sure if the watchdog manager dies the machine
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431)  *	reboots. In the former case we disable the counters, in the latter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432)  *	case you have to open it again very soon.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) static int wdt_release(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	if (expect_close == 42) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 		wdt_stop();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 		clear_bit(0, &wdt_is_open);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 		pr_crit("WDT device closed unexpectedly.  WDT will not stop!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 		wdt_ping();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	expect_close = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449)  *	wdt_temp_read:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)  *	@file: file handle to the watchdog board
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451)  *	@buf: buffer to write 1 byte into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452)  *	@count: length of buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)  *	@ptr: offset (no seek allowed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455)  *	Temp_read reports the temperature in degrees Fahrenheit. The API is in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456)  *	farenheit. It was designed by an imperial measurement luddite.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) static ssize_t wdt_temp_read(struct file *file, char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 						size_t count, loff_t *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	int temperature = wdt_get_temperature();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	if (copy_to_user(buf, &temperature, 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471)  *	wdt_temp_open:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472)  *	@inode: inode of device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473)  *	@file: file handle to device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475)  *	The temperature device has been opened.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) static int wdt_temp_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	return stream_open(inode, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484)  *	wdt_temp_release:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485)  *	@inode: inode to board
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486)  *	@file: file handle to board
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488)  *	The temperature device has been closed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) static int wdt_temp_release(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497)  *	notify_sys:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498)  *	@this: our notifier block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499)  *	@code: the event being reported
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500)  *	@unused: unused
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502)  *	Our notifier is called on system shutdowns. We want to turn the card
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503)  *	off at reboot otherwise the machine will reboot again during memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504)  *	test or worse yet during the following fsck. This would suck, in fact
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505)  *	trust me - if it happens it does suck.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	void *unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	if (code == SYS_DOWN || code == SYS_HALT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 		wdt_stop();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517)  *	Kernel Interfaces
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) static const struct file_operations wdt_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	.owner		= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	.llseek		= no_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	.write		= wdt_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	.unlocked_ioctl	= wdt_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	.compat_ioctl	= compat_ptr_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	.open		= wdt_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	.release	= wdt_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) static struct miscdevice wdt_miscdev = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	.minor	= WATCHDOG_MINOR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	.name	= "watchdog",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	.fops	= &wdt_fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) static const struct file_operations wdt_temp_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 	.owner		= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	.llseek		= no_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 	.read		= wdt_temp_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	.open		= wdt_temp_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 	.release	= wdt_temp_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) static struct miscdevice temp_miscdev = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 	.minor	= TEMP_MINOR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 	.name	= "temperature",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 	.fops	= &wdt_temp_fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552)  *	The WDT card needs to learn about soft shutdowns in order to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553)  *	turn the timebomb registers off.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) static struct notifier_block wdt_notifier = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 	.notifier_call = wdt_notify_sys,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561)  *	cleanup_module:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563)  *	Unload the watchdog. You cannot do this with any file handles open.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564)  *	If your watchdog is set to continue ticking on close and you unload
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565)  *	it, well it keeps ticking. We won't get the interrupt but the board
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566)  *	will not touch PC memory so all is fine. You just have to load a new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567)  *	module in 60 seconds or reboot.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) static void __exit wdt_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 	misc_deregister(&wdt_miscdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 	if (type == 501)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 		misc_deregister(&temp_miscdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 	unregister_reboot_notifier(&wdt_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 	free_irq(irq, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 	release_region(io, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581)  *	wdt_init:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583)  *	Set up the WDT watchdog board. All we have to do is grab the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584)  *	resources we require and bitch if anyone beat us to them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585)  *	The open() function will actually kick the board off.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) static int __init wdt_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 	if (type != 500 && type != 501) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 		pr_err("unknown card type '%d'\n", type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 	/* Check that the heartbeat value is within it's range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 	   if not reset to the default */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 	if (wdt_set_heartbeat(heartbeat)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 		wdt_set_heartbeat(WD_TIMO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 		pr_info("heartbeat value must be 0 < heartbeat < 65536, using %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 			WD_TIMO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 	if (!request_region(io, 8, "wdt501p")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 		pr_err("I/O address 0x%04x already in use\n", io);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 		ret = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 	ret = request_irq(irq, wdt_interrupt, 0, "wdt501p", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 		pr_err("IRQ %d is not free\n", irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 		goto outreg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 	ret = register_reboot_notifier(&wdt_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 		pr_err("cannot register reboot notifier (err=%d)\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 		goto outirq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 	if (type == 501) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 		ret = misc_register(&temp_miscdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 			pr_err("cannot register miscdev on minor=%d (err=%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 			       TEMP_MINOR, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 			goto outrbt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 	ret = misc_register(&wdt_miscdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 		pr_err("cannot register miscdev on minor=%d (err=%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 		       WATCHDOG_MINOR, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 		goto outmisc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 	pr_info("WDT500/501-P driver 0.10 at 0x%04x (Interrupt %d). heartbeat=%d sec (nowayout=%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 		io, irq, heartbeat, nowayout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 	if (type == 501)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 		pr_info("Fan Tachometer is %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 			tachometer ? "Enabled" : "Disabled");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) outmisc:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 	if (type == 501)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 		misc_deregister(&temp_miscdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) outrbt:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 	unregister_reboot_notifier(&wdt_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) outirq:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 	free_irq(irq, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) outreg:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 	release_region(io, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) module_init(wdt_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) module_exit(wdt_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) MODULE_AUTHOR("Alan Cox");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) MODULE_DESCRIPTION("Driver for ISA ICS watchdog cards (WDT500/501)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) MODULE_LICENSE("GPL");