blob: 4ef6702fff822e881f84aad2491fc437978222ee [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Alan Cox04bedfa2009-03-22 10:46:42 +00002 * Industrial Computer Source WDT501 driver
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Alan Cox29fa0582008-10-27 15:17:56 +00004 * (c) Copyright 1996-1997 Alan Cox <alan@lxorguk.ukuu.org.uk>,
5 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
13 * warranty for any of this software. This material is provided
14 * "AS-IS" and at no charge.
15 *
16 * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk>
17 *
18 * Release 0.10.
19 *
20 * Fixes
21 * Dave Gregorich : Modularisation and minor bugs
22 * Alan Cox : Added the watchdog ioctl() stuff
23 * Alan Cox : Fixed the reboot problem (as noted by
24 * Matt Crocker).
25 * Alan Cox : Added wdt= boot option
26 * Alan Cox : Cleaned up copy/user stuff
Alan Cox9f2d1f02008-08-04 17:55:35 +010027 * Tim Hockin : Added insmod parameters, comment
28 * cleanup, parameterized timeout
29 * Tigran Aivazian : Restructured wdt_init() to handle
30 * failures
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 * Joel Becker : Added WDIOC_GET/SETTIMEOUT
32 * Matt Domsch : Added nowayout module option
33 */
34
Joe Perches27c766a2012-02-15 15:06:19 -080035#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/interrupt.h>
38#include <linux/module.h>
39#include <linux/moduleparam.h>
40#include <linux/types.h>
41#include <linux/miscdevice.h>
42#include <linux/watchdog.h>
43#include <linux/fs.h>
44#include <linux/ioport.h>
45#include <linux/notifier.h>
46#include <linux/reboot.h>
47#include <linux/init.h>
Alan Cox9f2d1f02008-08-04 17:55:35 +010048#include <linux/io.h>
49#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <asm/system.h>
52#include "wd501p.h"
53
54static unsigned long wdt_is_open;
55static char expect_close;
56
57/*
58 * Module parameters
59 */
60
61#define WD_TIMO 60 /* Default heartbeat = 60 seconds */
62
63static int heartbeat = WD_TIMO;
64static int wd_heartbeat;
65module_param(heartbeat, int, 0);
Alan Cox9f2d1f02008-08-04 17:55:35 +010066MODULE_PARM_DESC(heartbeat,
67 "Watchdog heartbeat in seconds. (0 < heartbeat < 65536, default="
68 __MODULE_STRING(WD_TIMO) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Andrey Panin4bfdf372005-07-27 11:43:58 -070070static int nowayout = WATCHDOG_NOWAYOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071module_param(nowayout, int, 0);
Alan Cox9f2d1f02008-08-04 17:55:35 +010072MODULE_PARM_DESC(nowayout,
73 "Watchdog cannot be stopped once started (default="
74 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76/* You must set these - there is no sane way to probe for this board. */
Alan Cox9f2d1f02008-08-04 17:55:35 +010077static int io = 0x240;
78static int irq = 11;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Alan Cox01c785d2008-01-09 21:36:01 -080080static DEFINE_SPINLOCK(wdt_lock);
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082module_param(io, int, 0);
83MODULE_PARM_DESC(io, "WDT io port (default=0x240)");
84module_param(irq, int, 0);
85MODULE_PARM_DESC(irq, "WDT irq (default=11)");
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087/* Support for the Fan Tachometer on the WDT501-P */
88static int tachometer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089module_param(tachometer, int, 0);
Alan Cox9f2d1f02008-08-04 17:55:35 +010090MODULE_PARM_DESC(tachometer,
91 "WDT501-P Fan Tachometer support (0=disable, default=0)");
Alan Cox04bedfa2009-03-22 10:46:42 +000092
93static int type = 500;
94module_param(type, int, 0);
95MODULE_PARM_DESC(type,
Randy Dunlap4724ba572010-05-03 11:42:52 -070096 "WDT501-P Card type (500 or 501, default=500)");
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98/*
99 * Programming support
100 */
101
102static void wdt_ctr_mode(int ctr, int mode)
103{
Alan Cox9f2d1f02008-08-04 17:55:35 +0100104 ctr <<= 6;
105 ctr |= 0x30;
106 ctr |= (mode << 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 outb_p(ctr, WDT_CR);
108}
109
110static void wdt_ctr_load(int ctr, int val)
111{
112 outb_p(val&0xFF, WDT_COUNT0+ctr);
113 outb_p(val>>8, WDT_COUNT0+ctr);
114}
115
116/**
117 * wdt_start:
118 *
119 * Start the watchdog driver.
120 */
121
122static int wdt_start(void)
123{
Alan Cox01c785d2008-01-09 21:36:01 -0800124 unsigned long flags;
125 spin_lock_irqsave(&wdt_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 inb_p(WDT_DC); /* Disable watchdog */
Alan Cox9f2d1f02008-08-04 17:55:35 +0100127 wdt_ctr_mode(0, 3); /* Program CTR0 for Mode 3:
128 Square Wave Generator */
129 wdt_ctr_mode(1, 2); /* Program CTR1 for Mode 2:
130 Rate Generator */
131 wdt_ctr_mode(2, 0); /* Program CTR2 for Mode 0:
132 Pulse on Terminal Count */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 wdt_ctr_load(0, 8948); /* Count at 100Hz */
Alan Cox9f2d1f02008-08-04 17:55:35 +0100134 wdt_ctr_load(1, wd_heartbeat); /* Heartbeat */
135 wdt_ctr_load(2, 65535); /* Length of reset pulse */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 outb_p(0, WDT_DC); /* Enable watchdog */
Alan Cox01c785d2008-01-09 21:36:01 -0800137 spin_unlock_irqrestore(&wdt_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 return 0;
139}
140
141/**
142 * wdt_stop:
143 *
144 * Stop the watchdog driver.
145 */
146
Alan Cox9f2d1f02008-08-04 17:55:35 +0100147static int wdt_stop(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
Alan Cox01c785d2008-01-09 21:36:01 -0800149 unsigned long flags;
150 spin_lock_irqsave(&wdt_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 /* Turn the card off */
152 inb_p(WDT_DC); /* Disable watchdog */
Alan Cox9f2d1f02008-08-04 17:55:35 +0100153 wdt_ctr_load(2, 0); /* 0 length reset pulses now */
Alan Cox01c785d2008-01-09 21:36:01 -0800154 spin_unlock_irqrestore(&wdt_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 return 0;
156}
157
158/**
159 * wdt_ping:
160 *
Alan Cox9f2d1f02008-08-04 17:55:35 +0100161 * Reload counter one with the watchdog heartbeat. We don't bother
162 * reloading the cascade counter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 */
164
Alan Cox04bedfa2009-03-22 10:46:42 +0000165static void wdt_ping(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Alan Cox01c785d2008-01-09 21:36:01 -0800167 unsigned long flags;
168 spin_lock_irqsave(&wdt_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 /* Write a watchdog value */
170 inb_p(WDT_DC); /* Disable watchdog */
Alan Cox9f2d1f02008-08-04 17:55:35 +0100171 wdt_ctr_mode(1, 2); /* Re-Program CTR1 for Mode 2:
172 Rate Generator */
173 wdt_ctr_load(1, wd_heartbeat); /* Heartbeat */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 outb_p(0, WDT_DC); /* Enable watchdog */
Alan Cox01c785d2008-01-09 21:36:01 -0800175 spin_unlock_irqrestore(&wdt_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
178/**
179 * wdt_set_heartbeat:
180 * @t: the new heartbeat value that needs to be set.
181 *
Alan Cox9f2d1f02008-08-04 17:55:35 +0100182 * Set a new heartbeat value for the watchdog device. If the heartbeat
183 * value is incorrect we keep the old value and return -EINVAL. If
184 * successful we return 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 */
Alan Cox9f2d1f02008-08-04 17:55:35 +0100186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187static int wdt_set_heartbeat(int t)
188{
Alan Cox9f2d1f02008-08-04 17:55:35 +0100189 if (t < 1 || t > 65535)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 return -EINVAL;
191
192 heartbeat = t;
193 wd_heartbeat = t * 100;
194 return 0;
195}
196
197/**
198 * wdt_get_status:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 *
200 * Extract the status information from a WDT watchdog device. There are
201 * several board variants so we have to know which bits are valid. Some
202 * bits default to one and some to zero in order to be maximally painful.
203 *
204 * we then map the bits onto the status ioctl flags.
205 */
206
Alan Cox04bedfa2009-03-22 10:46:42 +0000207static int wdt_get_status(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
Alan Cox01c785d2008-01-09 21:36:01 -0800209 unsigned char new_status;
Alan Cox04bedfa2009-03-22 10:46:42 +0000210 int status = 0;
Alan Cox01c785d2008-01-09 21:36:01 -0800211 unsigned long flags;
212
213 spin_lock_irqsave(&wdt_lock, flags);
214 new_status = inb_p(WDT_SR);
215 spin_unlock_irqrestore(&wdt_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (new_status & WDC_SR_ISOI0)
Alan Cox04bedfa2009-03-22 10:46:42 +0000218 status |= WDIOF_EXTERN1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 if (new_status & WDC_SR_ISII1)
Alan Cox04bedfa2009-03-22 10:46:42 +0000220 status |= WDIOF_EXTERN2;
221 if (type == 501) {
222 if (!(new_status & WDC_SR_TGOOD))
223 status |= WDIOF_OVERHEAT;
224 if (!(new_status & WDC_SR_PSUOVER))
225 status |= WDIOF_POWEROVER;
226 if (!(new_status & WDC_SR_PSUUNDR))
227 status |= WDIOF_POWERUNDER;
228 if (tachometer) {
229 if (!(new_status & WDC_SR_FANGOOD))
230 status |= WDIOF_FANFAULT;
231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
Alan Cox04bedfa2009-03-22 10:46:42 +0000233 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236/**
237 * wdt_get_temperature:
238 *
239 * Reports the temperature in degrees Fahrenheit. The API is in
240 * farenheit. It was designed by an imperial measurement luddite.
241 */
242
Alan Cox04bedfa2009-03-22 10:46:42 +0000243static int wdt_get_temperature(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Alan Cox01c785d2008-01-09 21:36:01 -0800245 unsigned short c;
246 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Alan Cox01c785d2008-01-09 21:36:01 -0800248 spin_lock_irqsave(&wdt_lock, flags);
249 c = inb_p(WDT_RT);
250 spin_unlock_irqrestore(&wdt_lock, flags);
Alan Cox04bedfa2009-03-22 10:46:42 +0000251 return (c * 11 / 15) + 7;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
Alan Cox04bedfa2009-03-22 10:46:42 +0000253
254static void wdt_decode_501(int status)
255{
256 if (!(status & WDC_SR_TGOOD))
Joe Perches27c766a2012-02-15 15:06:19 -0800257 pr_crit("Overheat alarm (%d)\n", inb_p(WDT_RT));
Alan Cox04bedfa2009-03-22 10:46:42 +0000258 if (!(status & WDC_SR_PSUOVER))
Joe Perches27c766a2012-02-15 15:06:19 -0800259 pr_crit("PSU over voltage\n");
Alan Cox04bedfa2009-03-22 10:46:42 +0000260 if (!(status & WDC_SR_PSUUNDR))
Joe Perches27c766a2012-02-15 15:06:19 -0800261 pr_crit("PSU under voltage\n");
Alan Cox04bedfa2009-03-22 10:46:42 +0000262}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264/**
265 * wdt_interrupt:
266 * @irq: Interrupt number
267 * @dev_id: Unused as we don't allow multiple devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 *
269 * Handle an interrupt from the board. These are raised when the status
270 * map changes in what the board considers an interesting way. That means
271 * a failure condition occurring.
272 */
273
David Howells7d12e782006-10-05 14:55:46 +0100274static irqreturn_t wdt_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 /*
277 * Read the status register see what is up and
278 * then printk it.
279 */
Alan Cox01c785d2008-01-09 21:36:01 -0800280 unsigned char status;
281
282 spin_lock(&wdt_lock);
283 status = inb_p(WDT_SR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Joe Perches27c766a2012-02-15 15:06:19 -0800285 pr_crit("WDT status %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Alan Cox04bedfa2009-03-22 10:46:42 +0000287 if (type == 501) {
288 wdt_decode_501(status);
289 if (tachometer) {
290 if (!(status & WDC_SR_FANGOOD))
Joe Perches27c766a2012-02-15 15:06:19 -0800291 pr_crit("Possible fan fault\n");
Alan Cox04bedfa2009-03-22 10:46:42 +0000292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
Ilpo Jarvinen59338d42007-10-23 13:40:54 -0700294 if (!(status & WDC_SR_WCCR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295#ifdef SOFTWARE_REBOOT
296#ifdef ONLY_TESTING
Joe Perches27c766a2012-02-15 15:06:19 -0800297 pr_crit("Would Reboot\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298#else
Joe Perches27c766a2012-02-15 15:06:19 -0800299 pr_crit("Initiating system reboot\n");
Eric W. Biedermanf82567e2005-07-26 11:53:19 -0600300 emergency_restart();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301#endif
302#else
Joe Perches27c766a2012-02-15 15:06:19 -0800303 pr_crit("Reset in 5ms\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304#endif
Ilpo Jarvinen59338d42007-10-23 13:40:54 -0700305 }
Alan Cox01c785d2008-01-09 21:36:01 -0800306 spin_unlock(&wdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 return IRQ_HANDLED;
308}
309
310
311/**
312 * wdt_write:
313 * @file: file handle to the watchdog
314 * @buf: buffer to write (unused as data does not matter here
315 * @count: count of bytes
316 * @ppos: pointer to the position to write. No seeks allowed
317 *
318 * A write to a watchdog device is defined as a keepalive signal. Any
319 * write of data will do, as we we don't define content meaning.
320 */
321
Alan Cox9f2d1f02008-08-04 17:55:35 +0100322static ssize_t wdt_write(struct file *file, const char __user *buf,
323 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
Alan Cox9f2d1f02008-08-04 17:55:35 +0100325 if (count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (!nowayout) {
327 size_t i;
328
329 /* In case it was set long ago */
330 expect_close = 0;
331
332 for (i = 0; i != count; i++) {
333 char c;
334 if (get_user(c, buf + i))
335 return -EFAULT;
336 if (c == 'V')
337 expect_close = 42;
338 }
339 }
340 wdt_ping();
341 }
342 return count;
343}
344
345/**
346 * wdt_ioctl:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 * @file: file handle to the device
348 * @cmd: watchdog command
349 * @arg: argument pointer
350 *
351 * The watchdog API defines a common set of functions for all watchdogs
352 * according to their available features. We only actually usefully support
353 * querying capabilities and current status.
354 */
355
Alan Cox9f2d1f02008-08-04 17:55:35 +0100356static long wdt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
358 void __user *argp = (void __user *)arg;
359 int __user *p = argp;
360 int new_heartbeat;
361 int status;
362
Andrew Mortonc1bf3ac2010-03-03 11:57:40 -0800363 struct watchdog_info ident = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 .options = WDIOF_SETTIMEOUT|
365 WDIOF_MAGICCLOSE|
366 WDIOF_KEEPALIVEPING,
367 .firmware_version = 1,
368 .identity = "WDT500/501",
369 };
370
371 /* Add options according to the card we have */
372 ident.options |= (WDIOF_EXTERN1|WDIOF_EXTERN2);
Alan Cox04bedfa2009-03-22 10:46:42 +0000373 if (type == 501) {
374 ident.options |= (WDIOF_OVERHEAT|WDIOF_POWERUNDER|
375 WDIOF_POWEROVER);
376 if (tachometer)
377 ident.options |= WDIOF_FANFAULT;
378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Alan Cox9f2d1f02008-08-04 17:55:35 +0100380 switch (cmd) {
Alan Cox9f2d1f02008-08-04 17:55:35 +0100381 case WDIOC_GETSUPPORT:
382 return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
383 case WDIOC_GETSTATUS:
Alan Cox04bedfa2009-03-22 10:46:42 +0000384 status = wdt_get_status();
Alan Cox9f2d1f02008-08-04 17:55:35 +0100385 return put_user(status, p);
386 case WDIOC_GETBOOTSTATUS:
387 return put_user(0, p);
388 case WDIOC_KEEPALIVE:
389 wdt_ping();
390 return 0;
391 case WDIOC_SETTIMEOUT:
392 if (get_user(new_heartbeat, p))
393 return -EFAULT;
394 if (wdt_set_heartbeat(new_heartbeat))
395 return -EINVAL;
396 wdt_ping();
397 /* Fall */
398 case WDIOC_GETTIMEOUT:
399 return put_user(heartbeat, p);
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000400 default:
401 return -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
403}
404
405/**
406 * wdt_open:
407 * @inode: inode of device
408 * @file: file handle to device
409 *
410 * The watchdog device has been opened. The watchdog device is single
411 * open and on opening we load the counters. Counter zero is a 100Hz
412 * cascade, into counter 1 which downcounts to reboot. When the counter
413 * triggers counter 2 downcounts the length of the reset pulse which
414 * set set to be as long as possible.
415 */
416
417static int wdt_open(struct inode *inode, struct file *file)
418{
Alan Cox9f2d1f02008-08-04 17:55:35 +0100419 if (test_and_set_bit(0, &wdt_is_open))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return -EBUSY;
421 /*
422 * Activate
423 */
424 wdt_start();
425 return nonseekable_open(inode, file);
426}
427
428/**
429 * wdt_release:
430 * @inode: inode to board
431 * @file: file handle to board
432 *
433 * The watchdog has a configurable API. There is a religious dispute
434 * between people who want their watchdog to be able to shut down and
435 * those who want to be sure if the watchdog manager dies the machine
436 * reboots. In the former case we disable the counters, in the latter
437 * case you have to open it again very soon.
438 */
439
440static int wdt_release(struct inode *inode, struct file *file)
441{
442 if (expect_close == 42) {
443 wdt_stop();
444 clear_bit(0, &wdt_is_open);
445 } else {
Joe Perches27c766a2012-02-15 15:06:19 -0800446 pr_crit("WDT device closed unexpectedly. WDT will not stop!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 wdt_ping();
448 }
449 expect_close = 0;
450 return 0;
451}
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453/**
454 * wdt_temp_read:
455 * @file: file handle to the watchdog board
456 * @buf: buffer to write 1 byte into
457 * @count: length of buffer
458 * @ptr: offset (no seek allowed)
459 *
460 * Temp_read reports the temperature in degrees Fahrenheit. The API is in
461 * farenheit. It was designed by an imperial measurement luddite.
462 */
463
Alan Cox9f2d1f02008-08-04 17:55:35 +0100464static ssize_t wdt_temp_read(struct file *file, char __user *buf,
465 size_t count, loff_t *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
Alan Cox04bedfa2009-03-22 10:46:42 +0000467 int temperature = wdt_get_temperature();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Alan Cox9f2d1f02008-08-04 17:55:35 +0100469 if (copy_to_user(buf, &temperature, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return -EFAULT;
471
472 return 1;
473}
474
475/**
476 * wdt_temp_open:
477 * @inode: inode of device
478 * @file: file handle to device
479 *
480 * The temperature device has been opened.
481 */
482
483static int wdt_temp_open(struct inode *inode, struct file *file)
484{
485 return nonseekable_open(inode, file);
486}
487
488/**
489 * wdt_temp_release:
490 * @inode: inode to board
491 * @file: file handle to board
492 *
493 * The temperature device has been closed.
494 */
495
496static int wdt_temp_release(struct inode *inode, struct file *file)
497{
498 return 0;
499}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501/**
502 * notify_sys:
503 * @this: our notifier block
504 * @code: the event being reported
505 * @unused: unused
506 *
507 * Our notifier is called on system shutdowns. We want to turn the card
508 * off at reboot otherwise the machine will reboot again during memory
509 * test or worse yet during the following fsck. This would suck, in fact
510 * trust me - if it happens it does suck.
511 */
512
513static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
514 void *unused)
515{
Alan Cox9f2d1f02008-08-04 17:55:35 +0100516 if (code == SYS_DOWN || code == SYS_HALT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 wdt_stop();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 return NOTIFY_DONE;
519}
520
521/*
522 * Kernel Interfaces
523 */
524
525
Arjan van de Ven62322d22006-07-03 00:24:21 -0700526static const struct file_operations wdt_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 .owner = THIS_MODULE,
528 .llseek = no_llseek,
529 .write = wdt_write,
Alan Cox9f2d1f02008-08-04 17:55:35 +0100530 .unlocked_ioctl = wdt_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 .open = wdt_open,
532 .release = wdt_release,
533};
534
535static struct miscdevice wdt_miscdev = {
536 .minor = WATCHDOG_MINOR,
537 .name = "watchdog",
538 .fops = &wdt_fops,
539};
540
Arjan van de Ven62322d22006-07-03 00:24:21 -0700541static const struct file_operations wdt_temp_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 .owner = THIS_MODULE,
543 .llseek = no_llseek,
544 .read = wdt_temp_read,
545 .open = wdt_temp_open,
546 .release = wdt_temp_release,
547};
548
549static struct miscdevice temp_miscdev = {
550 .minor = TEMP_MINOR,
551 .name = "temperature",
552 .fops = &wdt_temp_fops,
553};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555/*
556 * The WDT card needs to learn about soft shutdowns in order to
557 * turn the timebomb registers off.
558 */
559
560static struct notifier_block wdt_notifier = {
561 .notifier_call = wdt_notify_sys,
562};
563
564/**
565 * cleanup_module:
566 *
567 * Unload the watchdog. You cannot do this with any file handles open.
568 * If your watchdog is set to continue ticking on close and you unload
569 * it, well it keeps ticking. We won't get the interrupt but the board
570 * will not touch PC memory so all is fine. You just have to load a new
571 * module in 60 seconds or reboot.
572 */
573
574static void __exit wdt_exit(void)
575{
576 misc_deregister(&wdt_miscdev);
Alan Cox04bedfa2009-03-22 10:46:42 +0000577 if (type == 501)
578 misc_deregister(&temp_miscdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 unregister_reboot_notifier(&wdt_notifier);
580 free_irq(irq, NULL);
Alan Cox9f2d1f02008-08-04 17:55:35 +0100581 release_region(io, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
583
584/**
Wim Van Sebroeck5f3b2752011-02-23 20:04:38 +0000585 * wdt_init:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 *
587 * Set up the WDT watchdog board. All we have to do is grab the
588 * resources we require and bitch if anyone beat us to them.
589 * The open() function will actually kick the board off.
590 */
591
592static int __init wdt_init(void)
593{
594 int ret;
595
Alan Cox04bedfa2009-03-22 10:46:42 +0000596 if (type != 500 && type != 501) {
Joe Perches27c766a2012-02-15 15:06:19 -0800597 pr_err("unknown card type '%d'\n", type);
Alan Cox04bedfa2009-03-22 10:46:42 +0000598 return -ENODEV;
599 }
600
Alan Cox9f2d1f02008-08-04 17:55:35 +0100601 /* Check that the heartbeat value is within it's range;
602 if not reset to the default */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (wdt_set_heartbeat(heartbeat)) {
604 wdt_set_heartbeat(WD_TIMO);
Joe Perches27c766a2012-02-15 15:06:19 -0800605 pr_info("heartbeat value must be 0 < heartbeat < 65536, using %d\n",
606 WD_TIMO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
608
609 if (!request_region(io, 8, "wdt501p")) {
Joe Perches27c766a2012-02-15 15:06:19 -0800610 pr_err("I/O address 0x%04x already in use\n", io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 ret = -EBUSY;
612 goto out;
613 }
614
Yong Zhang86b59122011-09-07 16:10:55 +0800615 ret = request_irq(irq, wdt_interrupt, 0, "wdt501p", NULL);
Alan Cox9f2d1f02008-08-04 17:55:35 +0100616 if (ret) {
Joe Perches27c766a2012-02-15 15:06:19 -0800617 pr_err("IRQ %d is not free\n", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 goto outreg;
619 }
620
621 ret = register_reboot_notifier(&wdt_notifier);
Alan Cox9f2d1f02008-08-04 17:55:35 +0100622 if (ret) {
Joe Perches27c766a2012-02-15 15:06:19 -0800623 pr_err("cannot register reboot notifier (err=%d)\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 goto outirq;
625 }
626
Alan Cox04bedfa2009-03-22 10:46:42 +0000627 if (type == 501) {
628 ret = misc_register(&temp_miscdev);
629 if (ret) {
Joe Perches27c766a2012-02-15 15:06:19 -0800630 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
631 TEMP_MINOR, ret);
Alan Cox04bedfa2009-03-22 10:46:42 +0000632 goto outrbt;
633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636 ret = misc_register(&wdt_miscdev);
637 if (ret) {
Joe Perches27c766a2012-02-15 15:06:19 -0800638 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
639 WATCHDOG_MINOR, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 goto outmisc;
641 }
642
Joe Perches27c766a2012-02-15 15:06:19 -0800643 pr_info("WDT500/501-P driver 0.10 at 0x%04x (Interrupt %d). heartbeat=%d sec (nowayout=%d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 io, irq, heartbeat, nowayout);
Alan Cox04bedfa2009-03-22 10:46:42 +0000645 if (type == 501)
Joe Perches27c766a2012-02-15 15:06:19 -0800646 pr_info("Fan Tachometer is %s\n",
647 tachometer ? "Enabled" : "Disabled");
Alan Cox04bedfa2009-03-22 10:46:42 +0000648 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650outmisc:
Alan Cox04bedfa2009-03-22 10:46:42 +0000651 if (type == 501)
652 misc_deregister(&temp_miscdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653outrbt:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 unregister_reboot_notifier(&wdt_notifier);
655outirq:
656 free_irq(irq, NULL);
657outreg:
Alan Cox9f2d1f02008-08-04 17:55:35 +0100658 release_region(io, 8);
Alan Cox04bedfa2009-03-22 10:46:42 +0000659out:
660 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661}
662
663module_init(wdt_init);
664module_exit(wdt_exit);
665
666MODULE_AUTHOR("Alan Cox");
667MODULE_DESCRIPTION("Driver for ISA ICS watchdog cards (WDT500/501)");
668MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
669MODULE_ALIAS_MISCDEV(TEMP_MINOR);
670MODULE_LICENSE("GPL");