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