Blame view

drivers/watchdog/pcwd.c 26.9 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
  /*
   * PC Watchdog Driver
   * by Ken Hollis (khollis@bitgate.com)
   *
f9146f26d   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
5
   * Permission granted from Simon Machell (smachell@berkprod.com)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
   * Written for the Linux Kernel, and GPLed by Ken Hollis
   *
   * 960107	Added request_region routines, modulized the whole thing.
   * 960108	Fixed end-of-file pointer (Thanks to Dan Hollis), added
   *		WD_TIMEOUT define.
   * 960216	Added eof marker on the file, and changed verbose messages.
   * 960716	Made functional and cosmetic changes to the source for
   *		inclusion in Linux 2.0.x kernels, thanks to Alan Cox.
   * 960717	Removed read/seek routines, replaced with ioctl.  Also, added
   *		check_region command due to Alan's suggestion.
   * 960821	Made changes to compile in newer 2.0.x kernels.  Added
   *		"cold reboot sense" entry.
   * 960825	Made a few changes to code, deleted some defines and made
   *		typedefs to replace them.  Made heartbeat reset only available
   *		via ioctl, and removed the write routine.
   * 960828	Added new items for PC Watchdog Rev.C card.
   * 960829	Changed around all of the IOCTLs, added new features,
   *		added watchdog disable/re-enable routines.  Added firmware
   *		version reporting.  Added read routine for temperature.
   *		Removed some extra defines, added an autodetect Revision
   *		routine.
143a2e54b   Wim Van Sebroeck   [WATCHDOG] More c...
27
28
   * 961006	Revised some documentation, fixed some cosmetic bugs.  Made
   *		drivers to panic the system if it's overheating at bootup.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
29
30
   * 961118	Changed some verbiage on some of the output, tidied up
   *		code bits, and added compatibility to 2.1.x.
143a2e54b   Wim Van Sebroeck   [WATCHDOG] More c...
31
   * 970912	Enabled board on open and disable on close.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
32
   * 971107	Took account of recent VFS changes (broke read).
143a2e54b   Wim Van Sebroeck   [WATCHDOG] More c...
33
34
35
36
37
38
   * 971210	Disable board on initialisation in case board already ticking.
   * 971222	Changed open/close for temperature handling
   *		Michael Meskes <meskes@debian.org>.
   * 980112	Used minor numbers from include/linux/miscdevice.h
   * 990403	Clear reset status after reading control status register in
   *		pcwd_showprevstate(). [Marc Boucher <marc@mbsi.ca>]
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
39
40
41
42
   * 990605	Made changes to code to support Firmware 1.22a, added
   *		fairly useless proc entry.
   * 990610	removed said useless proc code for the merge <alan>
   * 000403	Removed last traces of proc code. <davej>
261dcc70a   Alan Cox   [WATCHDOG 32/57] ...
43
44
   * 011214	Added nowayout module option to override
   *		CONFIG_WATCHDOG_NOWAYOUT <Matt_Domsch@dell.com>
143a2e54b   Wim Van Sebroeck   [WATCHDOG] More c...
45
   *		Added timeout module option to override default
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
46
47
48
49
   */
  
  /*
   *	A bells and whistles driver is available from http://www.pcwd.de/
261dcc70a   Alan Cox   [WATCHDOG 32/57] ...
50
51
   *	More info available at http://www.berkprod.com/ or
   *	http://www.pcwatchdog.com/
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
52
   */
fd41fa616   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
53
54
55
56
57
58
59
60
61
62
  #include <linux/module.h>	/* For module specific items */
  #include <linux/moduleparam.h>	/* For new moduleparam's */
  #include <linux/types.h>	/* For standard types (like size_t) */
  #include <linux/errno.h>	/* For the -ENODEV/... values */
  #include <linux/kernel.h>	/* For printk/panic/... */
  #include <linux/delay.h>	/* For mdelay function */
  #include <linux/timer.h>	/* For timer related operations */
  #include <linux/jiffies.h>	/* For jiffies stuff */
  #include <linux/miscdevice.h>	/* For MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR) */
  #include <linux/watchdog.h>	/* For the watchdog specific items */
5aa15050a   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
63
  #include <linux/reboot.h>	/* For kernel_power_off() */
fd41fa616   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
64
65
  #include <linux/init.h>		/* For __init/__exit/... */
  #include <linux/fs.h>		/* For file operations */
5aa15050a   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
66
  #include <linux/isa.h>		/* For isa devices */
fd41fa616   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
67
68
  #include <linux/ioport.h>	/* For io-port access */
  #include <linux/spinlock.h>	/* For spin_lock/spin_unlock/... */
261dcc70a   Alan Cox   [WATCHDOG 32/57] ...
69
70
  #include <linux/uaccess.h>	/* For copy_to_user/put_user/... */
  #include <linux/io.h>		/* For inb/outb/... */
fd41fa616   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
71
72
  
  /* Module and version information */
5aa15050a   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
73
74
  #define WATCHDOG_VERSION "1.20"
  #define WATCHDOG_DATE "18 Feb 2007"
a7122f916   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
75
76
77
  #define WATCHDOG_DRIVER_NAME "ISA-PC Watchdog"
  #define WATCHDOG_NAME "pcwd"
  #define PFX WATCHDOG_NAME ": "
143a2e54b   Wim Van Sebroeck   [WATCHDOG] More c...
78
79
  #define DRIVER_VERSION WATCHDOG_DRIVER_NAME " driver, v" WATCHDOG_VERSION "
  "
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
80
81
82
83
84
85
86
87
88
89
90
91
  
  /*
   * It should be noted that PCWD_REVISION_B was removed because A and B
   * are essentially the same types of card, with the exception that B
   * has temperature reporting.  Since I didn't receive a Rev.B card,
   * the Rev.B card is not supported.  (It's a good thing too, as they
   * are no longer in production.)
   */
  #define	PCWD_REVISION_A		1
  #define	PCWD_REVISION_C		2
  
  /*
5aa15050a   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
92
93
94
95
96
97
98
99
100
   * These are the auto-probe addresses available.
   *
   * Revision A only uses ports 0x270 and 0x370.  Revision C introduced 0x350.
   * Revision A has an address range of 2 addresses, while Revision C has 4.
   */
  #define PCWD_ISA_NR_CARDS	3
  static int pcwd_ioports[] = { 0x270, 0x350, 0x370, 0x000 };
  
  /*
8f0235dcc   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
101
102
103
104
   * These are the defines that describe the control status bits for the
   * PCI-PC Watchdog card.
  */
  /* Port 1 : Control Status #1 for the PC Watchdog card, revision A. */
4206f0c4d   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
105
106
107
108
109
  #define WD_WDRST		0x01	/* Previously reset state */
  #define WD_T110			0x02	/* Temperature overheat sense */
  #define WD_HRTBT		0x04	/* Heartbeat sense */
  #define WD_RLY2			0x08	/* External relay triggered */
  #define WD_SRLY2		0x80	/* Software external relay triggered */
8f0235dcc   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
110
  /* Port 1 : Control Status #1 for the PC Watchdog card, revision C. */
4206f0c4d   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
111
112
113
  #define WD_REVC_WTRP		0x01	/* Watchdog Trip status */
  #define WD_REVC_HRBT		0x02	/* Watchdog Heartbeat */
  #define WD_REVC_TTRP		0x04	/* Temperature Trip status */
261dcc70a   Alan Cox   [WATCHDOG 32/57] ...
114
115
  #define WD_REVC_RL2A		0x08	/* Relay 2 activated by
  							on-board processor */
4206f0c4d   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
116
117
118
  #define WD_REVC_RL1A		0x10	/* Relay 1 active */
  #define WD_REVC_R2DS		0x40	/* Relay 2 disable */
  #define WD_REVC_RLY2		0x80	/* Relay 2 activated? */
8f0235dcc   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
119
120
121
  /* Port 2 : Control Status #2 */
  #define WD_WDIS			0x10	/* Watchdog Disabled */
  #define WD_ENTP			0x20	/* Watchdog Enable Temperature Trip */
261dcc70a   Alan Cox   [WATCHDOG 32/57] ...
122
123
  #define WD_SSEL			0x40	/* Watchdog Switch Select
  							(1:SW1 <-> 0:SW2) */
8f0235dcc   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
124
  #define WD_WCMD			0x80	/* Watchdog Command Mode */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
125
126
127
128
129
130
  
  /* max. time we give an ISA watchdog card to process a command */
  /* 500ms for each 4 bit response (according to spec.) */
  #define ISA_COMMAND_TIMEOUT     1000
  
  /* Watchdog's internal commands */
fd41fa616   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
131
132
133
134
135
136
  #define CMD_ISA_IDLE			0x00
  #define CMD_ISA_VERSION_INTEGER		0x01
  #define CMD_ISA_VERSION_TENTH		0x02
  #define CMD_ISA_VERSION_HUNDRETH	0x03
  #define CMD_ISA_VERSION_MINOR		0x04
  #define CMD_ISA_SWITCH_SETTINGS		0x05
369fa2529   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
137
138
139
140
  #define CMD_ISA_RESET_PC		0x06
  #define CMD_ISA_ARM_0			0x07
  #define CMD_ISA_ARM_30			0x08
  #define CMD_ISA_ARM_60			0x09
fd41fa616   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
141
142
143
  #define CMD_ISA_DELAY_TIME_2SECS	0x0A
  #define CMD_ISA_DELAY_TIME_4SECS	0x0B
  #define CMD_ISA_DELAY_TIME_8SECS	0x0C
369fa2529   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
144
  #define CMD_ISA_RESET_RELAYS		0x0D
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
145

f3dc07330   Wim Van Sebroeck   [WATCHDOG] pcwd_u...
146
  /* Watchdog's Dip Switch heartbeat values */
7944d3a5a   Wim Van Sebroeck   [WATCHDOG] more c...
147
  static const int heartbeat_tbl[] = {
f3dc07330   Wim Van Sebroeck   [WATCHDOG] pcwd_u...
148
149
150
151
152
153
154
155
156
  	20,	/* OFF-OFF-OFF	= 20 Sec  */
  	40,	/* OFF-OFF-ON	= 40 Sec  */
  	60,	/* OFF-ON-OFF	=  1 Min  */
  	300,	/* OFF-ON-ON	=  5 Min  */
  	600,	/* ON-OFF-OFF	= 10 Min  */
  	1800,	/* ON-OFF-ON	= 30 Min  */
  	3600,	/* ON-ON-OFF	=  1 Hour */
  	7200,	/* ON-ON-ON	=  2 hour */
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
157
158
159
160
161
162
163
164
165
166
167
168
  /*
   * We are using an kernel timer to do the pinging of the watchdog
   * every ~500ms. We try to set the internal heartbeat of the
   * watchdog to 2 ms.
   */
  
  #define WDT_INTERVAL (HZ/2+1)
  
  /* We can only use 1 card due to the /dev/watchdog restriction */
  static int cards_found;
  
  /* internal variables */
36cbaa877   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
169
  static unsigned long open_allowed;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
170
  static char expect_close;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
171
  static int temp_panic;
261dcc70a   Alan Cox   [WATCHDOG 32/57] ...
172
173
174
  
  /* this is private data for each ISA-PC watchdog card */
  static struct {
2891b6ad1   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
175
  	char fw_ver_str[6];		/* The cards firmware version */
a2be87860   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
176
  	int revision;			/* The card's revision */
261dcc70a   Alan Cox   [WATCHDOG 32/57] ...
177
178
179
180
  	int supports_temp;		/* Whether or not the card has
  						a temperature device */
  	int command_mode;		/* Whether or not the card is in
  						command mode */
a2be87860   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
181
182
183
184
185
186
  	int boot_status;		/* The card's boot status */
  	int io_addr;			/* The cards I/O address */
  	spinlock_t io_lock;		/* the lock for io operations */
  	struct timer_list timer;	/* The timer that pings the watchdog */
  	unsigned long next_heartbeat;	/* the next_heartbeat for the timer */
  } pcwd_private;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
187
188
  
  /* module parameters */
c324ab428   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
189
190
191
192
193
  #define QUIET	0	/* Default */
  #define VERBOSE	1	/* Verbose */
  #define DEBUG	2	/* print fancy stuff too */
  static int debug = QUIET;
  module_param(debug, int, 0);
261dcc70a   Alan Cox   [WATCHDOG 32/57] ...
194
195
  MODULE_PARM_DESC(debug,
  		"Debug level: 0=Quiet, 1=Verbose, 2=Debug (default=0)");
c324ab428   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
196

261dcc70a   Alan Cox   [WATCHDOG 32/57] ...
197
198
  /* default heartbeat = delay-time from dip-switches */
  #define WATCHDOG_HEARTBEAT 0
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
199
200
  static int heartbeat = WATCHDOG_HEARTBEAT;
  module_param(heartbeat, int, 0);
143a2e54b   Wim Van Sebroeck   [WATCHDOG] More c...
201
202
203
  MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. "
  	"(2 <= heartbeat <= 7200 or 0=delay-time from dip-switches, default="
  				__MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
204

4bfdf3783   Andrey Panin   [PATCH] consolida...
205
  static int nowayout = WATCHDOG_NOWAYOUT;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
206
  module_param(nowayout, int, 0);
261dcc70a   Alan Cox   [WATCHDOG 32/57] ...
207
208
209
  MODULE_PARM_DESC(nowayout,
  		"Watchdog cannot be stopped once started (default="
  				__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
210
211
212
213
214
215
216
217
218
219
  
  /*
   *	Internal functions
   */
  
  static int send_isa_command(int cmd)
  {
  	int i;
  	int control_status;
  	int port0, last_port0;	/* Double read for stabilising */
c324ab428   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
220
221
222
223
  	if (debug >= DEBUG)
  		printk(KERN_DEBUG PFX "sending following data cmd=0x%02x
  ",
  			cmd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
224
  	/* The WCMD bit must be 1 and the command is only 4 bits in size */
8f0235dcc   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
225
  	control_status = (cmd & 0x0F) | WD_WCMD;
a2be87860   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
226
  	outb_p(control_status, pcwd_private.io_addr + 2);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
227
  	udelay(ISA_COMMAND_TIMEOUT);
a2be87860   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
228
  	port0 = inb_p(pcwd_private.io_addr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
229
230
  	for (i = 0; i < 25; ++i) {
  		last_port0 = port0;
a2be87860   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
231
  		port0 = inb_p(pcwd_private.io_addr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
232
233
234
  
  		if (port0 == last_port0)
  			break;	/* Data is stable */
261dcc70a   Alan Cox   [WATCHDOG 32/57] ...
235
  		udelay(250);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
236
  	}
c324ab428   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
237
  	if (debug >= DEBUG)
143a2e54b   Wim Van Sebroeck   [WATCHDOG] More c...
238
239
240
  		printk(KERN_DEBUG PFX "received following data for "
  			"cmd=0x%02x: port0=0x%02x last_port0=0x%02x
  ",
c324ab428   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
241
  			cmd, port0, last_port0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
242
243
244
245
246
  	return port0;
  }
  
  static int set_command_mode(void)
  {
261dcc70a   Alan Cox   [WATCHDOG 32/57] ...
247
  	int i, found = 0, count = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
248
249
  
  	/* Set the card into command mode */
a2be87860   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
250
  	spin_lock(&pcwd_private.io_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
251
252
253
254
255
256
257
  	while ((!found) && (count < 3)) {
  		i = send_isa_command(CMD_ISA_IDLE);
  
  		if (i == 0x00)
  			found = 1;
  		else if (i == 0xF3) {
  			/* Card does not like what we've done to it */
a2be87860   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
258
  			outb_p(0x00, pcwd_private.io_addr + 2);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
259
  			udelay(1200);	/* Spec says wait 1ms */
a2be87860   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
260
  			outb_p(0x00, pcwd_private.io_addr + 2);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
261
262
263
264
  			udelay(ISA_COMMAND_TIMEOUT);
  		}
  		count++;
  	}
a2be87860   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
265
266
  	spin_unlock(&pcwd_private.io_lock);
  	pcwd_private.command_mode = found;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
267

c324ab428   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
268
269
270
271
  	if (debug >= DEBUG)
  		printk(KERN_DEBUG PFX "command_mode=%d
  ",
  				pcwd_private.command_mode);
7944d3a5a   Wim Van Sebroeck   [WATCHDOG] more c...
272
  	return found;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
273
274
275
276
277
  }
  
  static void unset_command_mode(void)
  {
  	/* Set the card into normal mode */
a2be87860   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
278
279
  	spin_lock(&pcwd_private.io_lock);
  	outb_p(0x00, pcwd_private.io_addr + 2);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
280
  	udelay(ISA_COMMAND_TIMEOUT);
a2be87860   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
281
  	spin_unlock(&pcwd_private.io_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
282

a2be87860   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
283
  	pcwd_private.command_mode = 0;
c324ab428   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
284
285
286
287
288
  
  	if (debug >= DEBUG)
  		printk(KERN_DEBUG PFX "command_mode=%d
  ",
  				pcwd_private.command_mode);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
289
  }
85875211a   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
290
291
292
293
294
  static inline void pcwd_check_temperature_support(void)
  {
  	if (inb(pcwd_private.io_addr) != 0xF0)
  		pcwd_private.supports_temp = 1;
  }
2891b6ad1   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
295
  static inline void pcwd_get_firmware(void)
af3b38d99   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
296
297
  {
  	int one, ten, hund, minor;
af3b38d99   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
298

6bbc20bc0   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
299
  	strcpy(pcwd_private.fw_ver_str, "ERROR");
af3b38d99   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
300
301
302
303
304
305
  
  	if (set_command_mode()) {
  		one = send_isa_command(CMD_ISA_VERSION_INTEGER);
  		ten = send_isa_command(CMD_ISA_VERSION_TENTH);
  		hund = send_isa_command(CMD_ISA_VERSION_HUNDRETH);
  		minor = send_isa_command(CMD_ISA_VERSION_MINOR);
261dcc70a   Alan Cox   [WATCHDOG 32/57] ...
306
307
  		sprintf(pcwd_private.fw_ver_str, "%c.%c%c%c",
  					one, ten, hund, minor);
af3b38d99   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
308
  	}
af3b38d99   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
309
  	unset_command_mode();
2891b6ad1   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
310
311
  
  	return;
af3b38d99   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
312
313
314
315
  }
  
  static inline int pcwd_get_option_switches(void)
  {
261dcc70a   Alan Cox   [WATCHDOG 32/57] ...
316
  	int option_switches = 0;
af3b38d99   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
317
318
319
320
321
322
323
  
  	if (set_command_mode()) {
  		/* Get switch settings */
  		option_switches = send_isa_command(CMD_ISA_SWITCH_SETTINGS);
  	}
  
  	unset_command_mode();
7944d3a5a   Wim Van Sebroeck   [WATCHDOG] more c...
324
  	return option_switches;
af3b38d99   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
325
326
327
328
  }
  
  static void pcwd_show_card_info(void)
  {
af3b38d99   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
329
330
331
332
  	int option_switches;
  
  	/* Get some extra info from the hardware (in command/debug/diag mode) */
  	if (pcwd_private.revision == PCWD_REVISION_A)
261dcc70a   Alan Cox   [WATCHDOG 32/57] ...
333
334
335
336
  		printk(KERN_INFO PFX
  			"ISA-PC Watchdog (REV.A) detected at port 0x%04x
  ",
  							pcwd_private.io_addr);
af3b38d99   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
337
  	else if (pcwd_private.revision == PCWD_REVISION_C) {
2891b6ad1   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
338
  		pcwd_get_firmware();
143a2e54b   Wim Van Sebroeck   [WATCHDOG] More c...
339
340
341
  		printk(KERN_INFO PFX "ISA-PC Watchdog (REV.C) detected at port "
  			"0x%04x (Firmware version: %s)
  ",
2891b6ad1   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
342
  			pcwd_private.io_addr, pcwd_private.fw_ver_str);
af3b38d99   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
343
  		option_switches = pcwd_get_option_switches();
143a2e54b   Wim Van Sebroeck   [WATCHDOG] More c...
344
345
346
  		printk(KERN_INFO PFX "Option switches (0x%02x): "
  			"Temperature Reset Enable=%s, Power On Delay=%s
  ",
af3b38d99   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
  			option_switches,
  			((option_switches & 0x10) ? "ON" : "OFF"),
  			((option_switches & 0x08) ? "ON" : "OFF"));
  
  		/* Reprogram internal heartbeat to 2 seconds */
  		if (set_command_mode()) {
  			send_isa_command(CMD_ISA_DELAY_TIME_2SECS);
  			unset_command_mode();
  		}
  	}
  
  	if (pcwd_private.supports_temp)
  		printk(KERN_INFO PFX "Temperature Option Detected
  ");
  
  	if (pcwd_private.boot_status & WDIOF_CARDRESET)
143a2e54b   Wim Van Sebroeck   [WATCHDOG] More c...
363
364
365
  		printk(KERN_INFO PFX
  			"Previous reboot was caused by the card
  ");
af3b38d99   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
366
367
  
  	if (pcwd_private.boot_status & WDIOF_OVERHEAT) {
261dcc70a   Alan Cox   [WATCHDOG 32/57] ...
368
369
370
371
372
373
  		printk(KERN_EMERG PFX
  			"Card senses a CPU Overheat. Panicking!
  ");
  		printk(KERN_EMERG PFX
  			"CPU Overheat
  ");
af3b38d99   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
374
375
376
  	}
  
  	if (pcwd_private.boot_status == 0)
261dcc70a   Alan Cox   [WATCHDOG 32/57] ...
377
378
379
  		printk(KERN_INFO PFX
  			"No previous trip detected - Cold boot or reset
  ");
af3b38d99   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
380
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
381
382
383
384
385
386
  static void pcwd_timer_ping(unsigned long data)
  {
  	int wdrst_stat;
  
  	/* If we got a heartbeat pulse within the WDT_INTERVAL
  	 * we agree to ping the WDT */
261dcc70a   Alan Cox   [WATCHDOG 32/57] ...
387
  	if (time_before(jiffies, pcwd_private.next_heartbeat)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
388
  		/* Ping the watchdog */
a2be87860   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
389
390
  		spin_lock(&pcwd_private.io_lock);
  		if (pcwd_private.revision == PCWD_REVISION_A) {
261dcc70a   Alan Cox   [WATCHDOG 32/57] ...
391
392
  			/*  Rev A cards are reset by setting the
  			    WD_WDRST bit in register 1 */
a2be87860   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
393
  			wdrst_stat = inb_p(pcwd_private.io_addr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
394
395
  			wdrst_stat &= 0x0F;
  			wdrst_stat |= WD_WDRST;
a2be87860   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
396
  			outb_p(wdrst_stat, pcwd_private.io_addr + 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
397
398
  		} else {
  			/* Re-trigger watchdog by writing to port 0 */
a2be87860   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
399
  			outb_p(0x00, pcwd_private.io_addr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
400
401
402
  		}
  
  		/* Re-set the timer interval */
a2be87860   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
403
  		mod_timer(&pcwd_private.timer, jiffies + WDT_INTERVAL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
404

a2be87860   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
405
  		spin_unlock(&pcwd_private.io_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
406
  	} else {
261dcc70a   Alan Cox   [WATCHDOG 32/57] ...
407
408
409
  		printk(KERN_WARNING PFX
  			"Heartbeat lost! Will not ping the watchdog
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
410
411
412
413
414
415
  	}
  }
  
  static int pcwd_start(void)
  {
  	int stat_reg;
a2be87860   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
416
  	pcwd_private.next_heartbeat = jiffies + (heartbeat * HZ);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
417
418
  
  	/* Start the timer */
a2be87860   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
419
  	mod_timer(&pcwd_private.timer, jiffies + WDT_INTERVAL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
420
421
  
  	/* Enable the port */
a2be87860   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
422
423
424
  	if (pcwd_private.revision == PCWD_REVISION_C) {
  		spin_lock(&pcwd_private.io_lock);
  		outb_p(0x00, pcwd_private.io_addr + 3);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
425
  		udelay(ISA_COMMAND_TIMEOUT);
a2be87860   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
426
427
  		stat_reg = inb_p(pcwd_private.io_addr + 2);
  		spin_unlock(&pcwd_private.io_lock);
8f0235dcc   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
428
  		if (stat_reg & WD_WDIS) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
429
430
431
432
433
  			printk(KERN_INFO PFX "Could not start watchdog
  ");
  			return -EIO;
  		}
  	}
c324ab428   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
434
435
436
437
  
  	if (debug >= VERBOSE)
  		printk(KERN_DEBUG PFX "Watchdog started
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
438
439
440
441
442
443
444
445
  	return 0;
  }
  
  static int pcwd_stop(void)
  {
  	int stat_reg;
  
  	/* Stop the timer */
a2be87860   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
446
  	del_timer(&pcwd_private.timer);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
447
448
  
  	/*  Disable the board  */
a2be87860   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
449
450
451
  	if (pcwd_private.revision == PCWD_REVISION_C) {
  		spin_lock(&pcwd_private.io_lock);
  		outb_p(0xA5, pcwd_private.io_addr + 3);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
452
  		udelay(ISA_COMMAND_TIMEOUT);
a2be87860   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
453
  		outb_p(0xA5, pcwd_private.io_addr + 3);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
454
  		udelay(ISA_COMMAND_TIMEOUT);
a2be87860   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
455
456
  		stat_reg = inb_p(pcwd_private.io_addr + 2);
  		spin_unlock(&pcwd_private.io_lock);
8f0235dcc   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
457
  		if ((stat_reg & WD_WDIS) == 0) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
458
459
460
461
462
  			printk(KERN_INFO PFX "Could not stop watchdog
  ");
  			return -EIO;
  		}
  	}
c324ab428   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
463
464
465
466
  
  	if (debug >= VERBOSE)
  		printk(KERN_DEBUG PFX "Watchdog stopped
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
467
468
469
470
471
472
  	return 0;
  }
  
  static int pcwd_keepalive(void)
  {
  	/* user land ping */
a2be87860   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
473
  	pcwd_private.next_heartbeat = jiffies + (heartbeat * HZ);
c324ab428   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
474
475
476
477
  
  	if (debug >= DEBUG)
  		printk(KERN_DEBUG PFX "Watchdog keepalive signal send
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
478
479
480
481
482
  	return 0;
  }
  
  static int pcwd_set_heartbeat(int t)
  {
261dcc70a   Alan Cox   [WATCHDOG 32/57] ...
483
  	if (t < 2 || t > 7200) /* arbitrary upper limit */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
484
485
486
  		return -EINVAL;
  
  	heartbeat = t;
c324ab428   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
487
488
489
490
491
  
  	if (debug >= VERBOSE)
  		printk(KERN_DEBUG PFX "New heartbeat: %d
  ",
  		       heartbeat);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
492
493
494
495
496
  	return 0;
  }
  
  static int pcwd_get_status(int *status)
  {
4206f0c4d   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
497
  	int control_status;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
498

261dcc70a   Alan Cox   [WATCHDOG 32/57] ...
499
  	*status = 0;
a2be87860   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
500
501
  	spin_lock(&pcwd_private.io_lock);
  	if (pcwd_private.revision == PCWD_REVISION_A)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
502
503
504
  		/* Rev A cards return status information from
  		 * the base register, which is used for the
  		 * temperature in other cards. */
4206f0c4d   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
505
  		control_status = inb(pcwd_private.io_addr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
506
507
508
509
510
511
  	else {
  		/* Rev C cards return card status in the base
  		 * address + 1 register. And use different bits
  		 * to indicate a card initiated reset, and an
  		 * over-temperature condition. And the reboot
  		 * status can be reset. */
4206f0c4d   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
512
  		control_status = inb(pcwd_private.io_addr + 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
513
  	}
a2be87860   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
514
  	spin_unlock(&pcwd_private.io_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
515

a2be87860   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
516
  	if (pcwd_private.revision == PCWD_REVISION_A) {
4206f0c4d   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
517
  		if (control_status & WD_WDRST)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
518
  			*status |= WDIOF_CARDRESET;
4206f0c4d   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
519
  		if (control_status & WD_T110) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
520
521
  			*status |= WDIOF_OVERHEAT;
  			if (temp_panic) {
261dcc70a   Alan Cox   [WATCHDOG 32/57] ...
522
523
524
  				printk(KERN_INFO PFX
  					"Temperature overheat trip!
  ");
68acc05d0   Eric W. Biederman   [PATCH] pcwd.c: C...
525
  				kernel_power_off();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
526
527
528
  			}
  		}
  	} else {
4206f0c4d   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
529
  		if (control_status & WD_REVC_WTRP)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
530
  			*status |= WDIOF_CARDRESET;
4206f0c4d   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
531
  		if (control_status & WD_REVC_TTRP) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
532
533
  			*status |= WDIOF_OVERHEAT;
  			if (temp_panic) {
261dcc70a   Alan Cox   [WATCHDOG 32/57] ...
534
535
536
  				printk(KERN_INFO PFX
  					"Temperature overheat trip!
  ");
68acc05d0   Eric W. Biederman   [PATCH] pcwd.c: C...
537
  				kernel_power_off();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
538
539
540
541
542
543
544
545
546
  			}
  		}
  	}
  
  	return 0;
  }
  
  static int pcwd_clear_status(void)
  {
4206f0c4d   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
547
  	int control_status;
a2be87860   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
548
549
  	if (pcwd_private.revision == PCWD_REVISION_C) {
  		spin_lock(&pcwd_private.io_lock);
4206f0c4d   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
550

c324ab428   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
551
  		if (debug >= VERBOSE)
261dcc70a   Alan Cox   [WATCHDOG 32/57] ...
552
553
554
  			printk(KERN_INFO PFX
  					"clearing watchdog trip status
  ");
c324ab428   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
555

4206f0c4d   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
556
  		control_status = inb_p(pcwd_private.io_addr + 1);
c324ab428   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
557
  		if (debug >= DEBUG) {
261dcc70a   Alan Cox   [WATCHDOG 32/57] ...
558
559
560
  			printk(KERN_DEBUG PFX "status was: 0x%02x
  ",
  				control_status);
c324ab428   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
561
562
563
564
  			printk(KERN_DEBUG PFX "sending: 0x%02x
  ",
  				(control_status & WD_REVC_R2DS));
  		}
4206f0c4d   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
565
  		/* clear reset status & Keep Relay 2 disable state as it is */
261dcc70a   Alan Cox   [WATCHDOG 32/57] ...
566
567
  		outb_p((control_status & WD_REVC_R2DS),
  						pcwd_private.io_addr + 1);
4206f0c4d   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
568

a2be87860   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
569
  		spin_unlock(&pcwd_private.io_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
570
571
572
573
574
575
576
  	}
  	return 0;
  }
  
  static int pcwd_get_temperature(int *temperature)
  {
  	/* check that port 0 gives temperature info and no command results */
a2be87860   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
577
  	if (pcwd_private.command_mode)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
578
579
580
  		return -1;
  
  	*temperature = 0;
a2be87860   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
581
  	if (!pcwd_private.supports_temp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
582
583
584
585
586
587
  		return -ENODEV;
  
  	/*
  	 * Convert celsius to fahrenheit, since this was
  	 * the decided 'standard' for this return value.
  	 */
a2be87860   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
588
589
590
  	spin_lock(&pcwd_private.io_lock);
  	*temperature = ((inb(pcwd_private.io_addr)) * 9 / 5) + 32;
  	spin_unlock(&pcwd_private.io_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
591

c324ab428   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
592
593
594
595
596
  	if (debug >= DEBUG) {
  		printk(KERN_DEBUG PFX "temperature is: %d F
  ",
  			*temperature);
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
597
598
599
600
601
602
  	return 0;
  }
  
  /*
   *	/dev/watchdog handling
   */
261dcc70a   Alan Cox   [WATCHDOG 32/57] ...
603
  static long pcwd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
604
605
606
607
608
609
  {
  	int rv;
  	int status;
  	int temperature;
  	int new_heartbeat;
  	int __user *argp = (int __user *)arg;
42747d712   Wim Van Sebroeck   [WATCHDOG] watchd...
610
  	static const struct watchdog_info ident = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
611
612
613
614
615
616
617
618
  		.options =		WDIOF_OVERHEAT |
  					WDIOF_CARDRESET |
  					WDIOF_KEEPALIVEPING |
  					WDIOF_SETTIMEOUT |
  					WDIOF_MAGICCLOSE,
  		.firmware_version =	1,
  		.identity =		"PCWD",
  	};
261dcc70a   Alan Cox   [WATCHDOG 32/57] ...
619
  	switch (cmd) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
620
  	case WDIOC_GETSUPPORT:
261dcc70a   Alan Cox   [WATCHDOG 32/57] ...
621
  		if (copy_to_user(argp, &ident, sizeof(ident)))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
622
623
624
625
626
627
628
629
  			return -EFAULT;
  		return 0;
  
  	case WDIOC_GETSTATUS:
  		pcwd_get_status(&status);
  		return put_user(status, argp);
  
  	case WDIOC_GETBOOTSTATUS:
a2be87860   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
630
  		return put_user(pcwd_private.boot_status, argp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
631
632
633
634
635
636
637
638
  
  	case WDIOC_GETTEMP:
  		if (pcwd_get_temperature(&temperature))
  			return -EFAULT;
  
  		return put_user(temperature, argp);
  
  	case WDIOC_SETOPTIONS:
261dcc70a   Alan Cox   [WATCHDOG 32/57] ...
639
640
  		if (pcwd_private.revision == PCWD_REVISION_C) {
  			if (get_user(rv, argp))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
641
  				return -EFAULT;
261dcc70a   Alan Cox   [WATCHDOG 32/57] ...
642
643
644
645
  			if (rv & WDIOS_DISABLECARD) {
  				status = pcwd_stop();
  				if (status < 0)
  					return status;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
646
  			}
261dcc70a   Alan Cox   [WATCHDOG 32/57] ...
647
648
649
650
  			if (rv & WDIOS_ENABLECARD) {
  				status = pcwd_start();
  				if (status < 0)
  					return status;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
651
  			}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
652
  			if (rv & WDIOS_TEMPPANIC)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
653
  				temp_panic = 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
  		}
  		return -EINVAL;
  
  	case WDIOC_KEEPALIVE:
  		pcwd_keepalive();
  		return 0;
  
  	case WDIOC_SETTIMEOUT:
  		if (get_user(new_heartbeat, argp))
  			return -EFAULT;
  
  		if (pcwd_set_heartbeat(new_heartbeat))
  			return -EINVAL;
  
  		pcwd_keepalive();
  		/* Fall */
  
  	case WDIOC_GETTIMEOUT:
  		return put_user(heartbeat, argp);
0c06090c9   Wim Van Sebroeck   [WATCHDOG] Coding...
673
674
675
  
  	default:
  		return -ENOTTY;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
  	}
  
  	return 0;
  }
  
  static ssize_t pcwd_write(struct file *file, const char __user *buf, size_t len,
  			  loff_t *ppos)
  {
  	if (len) {
  		if (!nowayout) {
  			size_t i;
  
  			/* In case it was set long ago */
  			expect_close = 0;
  
  			for (i = 0; i != len; i++) {
  				char c;
  
  				if (get_user(c, buf + i))
  					return -EFAULT;
  				if (c == 'V')
  					expect_close = 42;
  			}
  		}
  		pcwd_keepalive();
  	}
  	return len;
  }
  
  static int pcwd_open(struct inode *inode, struct file *file)
  {
261dcc70a   Alan Cox   [WATCHDOG 32/57] ...
707
  	if (test_and_set_bit(0, &open_allowed))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
708
  		return -EBUSY;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
709
710
  	if (nowayout)
  		__module_get(THIS_MODULE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
711
712
713
714
715
716
717
718
  	/* Activate */
  	pcwd_start();
  	pcwd_keepalive();
  	return nonseekable_open(inode, file);
  }
  
  static int pcwd_close(struct inode *inode, struct file *file)
  {
261dcc70a   Alan Cox   [WATCHDOG 32/57] ...
719
  	if (expect_close == 42)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
720
  		pcwd_stop();
261dcc70a   Alan Cox   [WATCHDOG 32/57] ...
721
722
723
724
  	else {
  		printk(KERN_CRIT PFX
  			"Unexpected close, not stopping watchdog!
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
725
726
727
  		pcwd_keepalive();
  	}
  	expect_close = 0;
261dcc70a   Alan Cox   [WATCHDOG 32/57] ...
728
  	clear_bit(0, &open_allowed);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
  	return 0;
  }
  
  /*
   *	/dev/temperature handling
   */
  
  static ssize_t pcwd_temp_read(struct file *file, char __user *buf, size_t count,
  			 loff_t *ppos)
  {
  	int temperature;
  
  	if (pcwd_get_temperature(&temperature))
  		return -EFAULT;
  
  	if (copy_to_user(buf, &temperature, 1))
  		return -EFAULT;
  
  	return 1;
  }
  
  static int pcwd_temp_open(struct inode *inode, struct file *file)
  {
a2be87860   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
752
  	if (!pcwd_private.supports_temp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
753
754
755
756
757
758
759
760
761
762
763
  		return -ENODEV;
  
  	return nonseekable_open(inode, file);
  }
  
  static int pcwd_temp_close(struct inode *inode, struct file *file)
  {
  	return 0;
  }
  
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
764
765
   *	Kernel Interfaces
   */
62322d255   Arjan van de Ven   [PATCH] make more...
766
  static const struct file_operations pcwd_fops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
767
768
769
  	.owner		= THIS_MODULE,
  	.llseek		= no_llseek,
  	.write		= pcwd_write,
261dcc70a   Alan Cox   [WATCHDOG 32/57] ...
770
  	.unlocked_ioctl	= pcwd_ioctl,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
771
772
773
774
775
776
777
778
779
  	.open		= pcwd_open,
  	.release	= pcwd_close,
  };
  
  static struct miscdevice pcwd_miscdev = {
  	.minor =	WATCHDOG_MINOR,
  	.name =		"watchdog",
  	.fops =		&pcwd_fops,
  };
62322d255   Arjan van de Ven   [PATCH] make more...
780
  static const struct file_operations pcwd_temp_fops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
781
782
783
784
785
786
787
788
789
790
791
792
  	.owner		= THIS_MODULE,
  	.llseek		= no_llseek,
  	.read		= pcwd_temp_read,
  	.open		= pcwd_temp_open,
  	.release	= pcwd_temp_close,
  };
  
  static struct miscdevice temp_miscdev = {
  	.minor =	TEMP_MINOR,
  	.name =		"temperature",
  	.fops =		&pcwd_temp_fops,
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
793
794
795
  /*
   *	Init & exit routines
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
796
797
798
  static inline int get_revision(void)
  {
  	int r = PCWD_REVISION_C;
a2be87860   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
799
  	spin_lock(&pcwd_private.io_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
800
801
  	/* REV A cards use only 2 io ports; test
  	 * presumes a floating bus reads as 0xff. */
a2be87860   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
802
803
  	if ((inb(pcwd_private.io_addr + 2) == 0xFF) ||
  	    (inb(pcwd_private.io_addr + 3) == 0xFF))
261dcc70a   Alan Cox   [WATCHDOG 32/57] ...
804
  		r = PCWD_REVISION_A;
a2be87860   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
805
  	spin_unlock(&pcwd_private.io_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
806
807
808
  
  	return r;
  }
5aa15050a   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
809
810
811
812
813
814
815
816
817
  /*
   *  The ISA cards have a heartbeat bit in one of the registers, which
   *  register is card dependent.  The heartbeat bit is monitored, and if
   *  found, is considered proof that a Berkshire card has been found.
   *  The initial rate is once per second at board start up, then twice
   *  per second for normal operation.
   */
  static int __devinit pcwd_isa_match(struct device *dev, unsigned int id)
  {
261dcc70a   Alan Cox   [WATCHDOG 32/57] ...
818
  	int base_addr = pcwd_ioports[id];
5aa15050a   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
819
820
821
822
823
824
825
826
827
  	int port0, last_port0;	/* Reg 0, in case it's REV A */
  	int port1, last_port1;	/* Register 1 for REV C cards */
  	int i;
  	int retval;
  
  	if (debug >= DEBUG)
  		printk(KERN_DEBUG PFX "pcwd_isa_match id=%d
  ",
  			id);
261dcc70a   Alan Cox   [WATCHDOG 32/57] ...
828
  	if (!request_region(base_addr, 4, "PCWD")) {
5aa15050a   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
  		printk(KERN_INFO PFX "Port 0x%04x unavailable
  ", base_addr);
  		return 0;
  	}
  
  	retval = 0;
  
  	port0 = inb_p(base_addr);	/* For REV A boards */
  	port1 = inb_p(base_addr + 1);	/* For REV C boards */
  	if (port0 != 0xff || port1 != 0xff) {
  		/* Not an 'ff' from a floating bus, so must be a card! */
  		for (i = 0; i < 4; ++i) {
  
  			msleep(500);
  
  			last_port0 = port0;
  			last_port1 = port1;
  
  			port0 = inb_p(base_addr);
  			port1 = inb_p(base_addr + 1);
  
  			/* Has either hearbeat bit changed?  */
  			if ((port0 ^ last_port0) & WD_HRTBT ||
  			    (port1 ^ last_port1) & WD_REVC_HRBT) {
  				retval = 1;
  				break;
  			}
  		}
  	}
261dcc70a   Alan Cox   [WATCHDOG 32/57] ...
858
  	release_region(base_addr, 4);
5aa15050a   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
859
860
861
862
863
  
  	return retval;
  }
  
  static int __devinit pcwd_isa_probe(struct device *dev, unsigned int id)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
864
865
  {
  	int ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
866

5aa15050a   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
867
868
869
870
  	if (debug >= DEBUG)
  		printk(KERN_DEBUG PFX "pcwd_isa_probe id=%d
  ",
  			id);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
871
872
  	cards_found++;
  	if (cards_found == 1)
261dcc70a   Alan Cox   [WATCHDOG 32/57] ...
873
874
  		printk(KERN_INFO PFX "v%s Ken Hollis (kenji@bitgate.com)
  ",
143a2e54b   Wim Van Sebroeck   [WATCHDOG] More c...
875
  							WATCHDOG_VERSION);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
876
877
878
879
880
881
  
  	if (cards_found > 1) {
  		printk(KERN_ERR PFX "This driver only supports 1 device
  ");
  		return -ENODEV;
  	}
5aa15050a   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
882
  	if (pcwd_ioports[id] == 0x0000) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
883
884
885
886
  		printk(KERN_ERR PFX "No I/O-Address for card detected
  ");
  		return -ENODEV;
  	}
5aa15050a   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
887
888
889
  	pcwd_private.io_addr = pcwd_ioports[id];
  
  	spin_lock_init(&pcwd_private.io_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
890
891
  
  	/* Check card's revision */
a2be87860   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
892
  	pcwd_private.revision = get_revision();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
893

261dcc70a   Alan Cox   [WATCHDOG 32/57] ...
894
895
  	if (!request_region(pcwd_private.io_addr,
  		(pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4, "PCWD")) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
896
897
  		printk(KERN_ERR PFX "I/O address 0x%04x already in use
  ",
a2be87860   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
898
  			pcwd_private.io_addr);
261dcc70a   Alan Cox   [WATCHDOG 32/57] ...
899
  		ret = -EIO;
5aa15050a   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
900
  		goto error_request_region;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
901
902
903
  	}
  
  	/* Initial variables */
a2be87860   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
904
  	pcwd_private.supports_temp = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
905
  	temp_panic = 0;
a2be87860   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
906
  	pcwd_private.boot_status = 0x0000;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
907
908
  
  	/* get the boot_status */
a2be87860   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
909
  	pcwd_get_status(&pcwd_private.boot_status);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
910
911
912
  
  	/* clear the "card caused reboot" flag */
  	pcwd_clear_status();
82eb7c505   Jiri Slaby   [WATCHDOG] timers...
913
  	setup_timer(&pcwd_private.timer, pcwd_timer_ping, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
914
915
916
917
918
  
  	/*  Disable the board  */
  	pcwd_stop();
  
  	/*  Check whether or not the card supports the temperature device */
85875211a   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
919
  	pcwd_check_temperature_support();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
920

af3b38d99   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
921
922
  	/* Show info about the card itself */
  	pcwd_show_card_info();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
923

f3dc07330   Wim Van Sebroeck   [WATCHDOG] pcwd_u...
924
925
926
  	/* If heartbeat = 0 then we use the heartbeat from the dip-switches */
  	if (heartbeat == 0)
  		heartbeat = heartbeat_tbl[(pcwd_get_option_switches() & 0x07)];
261dcc70a   Alan Cox   [WATCHDOG 32/57] ...
927
928
  	/* Check that the heartbeat value is within it's range;
  	   if not reset to the default */
fd41fa616   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
929
930
  	if (pcwd_set_heartbeat(heartbeat)) {
  		pcwd_set_heartbeat(WATCHDOG_HEARTBEAT);
261dcc70a   Alan Cox   [WATCHDOG 32/57] ...
931
932
933
934
  		printk(KERN_INFO PFX
  		  "heartbeat value must be 2 <= heartbeat <= 7200, using %d
  ",
  							WATCHDOG_HEARTBEAT);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
935
  	}
a2be87860   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
936
  	if (pcwd_private.supports_temp) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
937
938
  		ret = misc_register(&temp_miscdev);
  		if (ret) {
261dcc70a   Alan Cox   [WATCHDOG 32/57] ...
939
940
941
942
  			printk(KERN_ERR PFX
  			    "cannot register miscdev on minor=%d (err=%d)
  ",
  							TEMP_MINOR, ret);
5aa15050a   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
943
  			goto error_misc_register_temp;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
944
945
946
947
948
  		}
  	}
  
  	ret = misc_register(&pcwd_miscdev);
  	if (ret) {
261dcc70a   Alan Cox   [WATCHDOG 32/57] ...
949
950
951
952
  		printk(KERN_ERR PFX
  			"cannot register miscdev on minor=%d (err=%d)
  ",
  					WATCHDOG_MINOR, ret);
5aa15050a   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
953
  		goto error_misc_register_watchdog;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
954
955
956
957
958
959
960
  	}
  
  	printk(KERN_INFO PFX "initialized. heartbeat=%d sec (nowayout=%d)
  ",
  		heartbeat, nowayout);
  
  	return 0;
5aa15050a   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
961
962
963
964
965
  
  error_misc_register_watchdog:
  	if (pcwd_private.supports_temp)
  		misc_deregister(&temp_miscdev);
  error_misc_register_temp:
261dcc70a   Alan Cox   [WATCHDOG 32/57] ...
966
967
  	release_region(pcwd_private.io_addr,
  			(pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);
5aa15050a   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
968
969
970
971
  error_request_region:
  	pcwd_private.io_addr = 0x0000;
  	cards_found--;
  	return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
972
  }
5aa15050a   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
973
  static int __devexit pcwd_isa_remove(struct device *dev, unsigned int id)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
974
  {
5aa15050a   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
975
976
977
978
979
980
981
  	if (debug >= DEBUG)
  		printk(KERN_DEBUG PFX "pcwd_isa_remove id=%d
  ",
  			id);
  
  	if (!pcwd_private.io_addr)
  		return 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
982
983
984
985
986
987
  	/*  Disable the board  */
  	if (!nowayout)
  		pcwd_stop();
  
  	/* Deregister */
  	misc_deregister(&pcwd_miscdev);
a2be87860   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
988
  	if (pcwd_private.supports_temp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
989
  		misc_deregister(&temp_miscdev);
261dcc70a   Alan Cox   [WATCHDOG 32/57] ...
990
991
  	release_region(pcwd_private.io_addr,
  			(pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);
a2be87860   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
992
  	pcwd_private.io_addr = 0x0000;
f1c3a0567   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
993
  	cards_found--;
5aa15050a   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
994
995
  
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
996
  }
5aa15050a   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
997
  static void pcwd_isa_shutdown(struct device *dev, unsigned int id)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
998
  {
5aa15050a   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
999
1000
1001
1002
  	if (debug >= DEBUG)
  		printk(KERN_DEBUG PFX "pcwd_isa_shutdown id=%d
  ",
  			id);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1003

5aa15050a   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
1004
  	pcwd_stop();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1005
  }
5aa15050a   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
  static struct isa_driver pcwd_isa_driver = {
  	.match		= pcwd_isa_match,
  	.probe		= pcwd_isa_probe,
  	.remove		= __devexit_p(pcwd_isa_remove),
  	.shutdown	= pcwd_isa_shutdown,
  	.driver		= {
  		.owner	= THIS_MODULE,
  		.name	= WATCHDOG_NAME,
  	},
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1016
1017
1018
  
  static int __init pcwd_init_module(void)
  {
5aa15050a   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
1019
  	return isa_register_driver(&pcwd_isa_driver, PCWD_ISA_NR_CARDS);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1020
1021
1022
1023
  }
  
  static void __exit pcwd_cleanup_module(void)
  {
5aa15050a   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
1024
  	isa_unregister_driver(&pcwd_isa_driver);
369fa2529   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
1025
1026
  	printk(KERN_INFO PFX "Watchdog Module Unloaded.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1027
1028
1029
1030
  }
  
  module_init(pcwd_init_module);
  module_exit(pcwd_cleanup_module);
143a2e54b   Wim Van Sebroeck   [WATCHDOG] More c...
1031
1032
  MODULE_AUTHOR("Ken Hollis <kenji@bitgate.com>, "
  		"Wim Van Sebroeck <wim@iguana.be>");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1033
  MODULE_DESCRIPTION("Berkshire ISA-PC Watchdog driver");
5aa15050a   Wim Van Sebroeck   [WATCHDOG] pcwd.c...
1034
  MODULE_VERSION(WATCHDOG_VERSION);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1035
1036
1037
  MODULE_LICENSE("GPL");
  MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  MODULE_ALIAS_MISCDEV(TEMP_MINOR);