Loading...
1/*
2 * Eurotech CPU-1220/1410/1420 on board WDT driver
3 *
4 * (c) Copyright 2001 Ascensit <support@ascensit.com>
5 * (c) Copyright 2001 Rodolfo Giometti <giometti@ascensit.com>
6 * (c) Copyright 2002 Rob Radez <rob@osinvestor.com>
7 *
8 * Based on wdt.c.
9 * Original copyright messages:
10 *
11 * (c) Copyright 1996-1997 Alan Cox <alan@lxorguk.ukuu.org.uk>,
12 * All Rights Reserved.
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version
17 * 2 of the License, or (at your option) any later version.
18 *
19 * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
20 * warranty for any of this software. This material is provided
21 * "AS-IS" and at no charge.
22 *
23 * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk>*
24 */
25
26/* Changelog:
27 *
28 * 2001 - Rodolfo Giometti
29 * Initial release
30 *
31 * 2002/04/25 - Rob Radez
32 * clean up #includes
33 * clean up locking
34 * make __setup param unique
35 * proper options in watchdog_info
36 * add WDIOC_GETSTATUS and WDIOC_SETOPTIONS ioctls
37 * add expect_close support
38 *
39 * 2002.05.30 - Joel Becker <joel.becker@oracle.com>
40 * Added Matt Domsch's nowayout module option.
41 */
42
43/*
44 * The eurotech CPU-1220/1410/1420's watchdog is a part
45 * of the on-board SUPER I/O device SMSC FDC 37B782.
46 */
47
48#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
49
50#include <linux/interrupt.h>
51#include <linux/module.h>
52#include <linux/moduleparam.h>
53#include <linux/types.h>
54#include <linux/miscdevice.h>
55#include <linux/watchdog.h>
56#include <linux/fs.h>
57#include <linux/ioport.h>
58#include <linux/notifier.h>
59#include <linux/reboot.h>
60#include <linux/init.h>
61#include <linux/io.h>
62#include <linux/uaccess.h>
63
64
65static unsigned long eurwdt_is_open;
66static int eurwdt_timeout;
67static char eur_expect_close;
68static DEFINE_SPINLOCK(eurwdt_lock);
69
70/*
71 * You must set these - there is no sane way to probe for this board.
72 */
73
74static int io = 0x3f0;
75static int irq = 10;
76static char *ev = "int";
77
78#define WDT_TIMEOUT 60 /* 1 minute */
79
80static bool nowayout = WATCHDOG_NOWAYOUT;
81module_param(nowayout, bool, 0);
82MODULE_PARM_DESC(nowayout,
83 "Watchdog cannot be stopped once started (default="
84 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
85
86/*
87 * Some symbolic names
88 */
89
90#define WDT_CTRL_REG 0x30
91#define WDT_OUTPIN_CFG 0xe2
92#define WDT_EVENT_INT 0x00
93#define WDT_EVENT_REBOOT 0x08
94#define WDT_UNIT_SEL 0xf1
95#define WDT_UNIT_SECS 0x80
96#define WDT_TIMEOUT_VAL 0xf2
97#define WDT_TIMER_CFG 0xf3
98
99
100module_param(io, int, 0);
101MODULE_PARM_DESC(io, "Eurotech WDT io port (default=0x3f0)");
102module_param(irq, int, 0);
103MODULE_PARM_DESC(irq, "Eurotech WDT irq (default=10)");
104module_param(ev, charp, 0);
105MODULE_PARM_DESC(ev, "Eurotech WDT event type (default is `int')");
106
107
108/*
109 * Programming support
110 */
111
112static inline void eurwdt_write_reg(u8 index, u8 data)
113{
114 outb(index, io);
115 outb(data, io+1);
116}
117
118static inline void eurwdt_lock_chip(void)
119{
120 outb(0xaa, io);
121}
122
123static inline void eurwdt_unlock_chip(void)
124{
125 outb(0x55, io);
126 eurwdt_write_reg(0x07, 0x08); /* set the logical device */
127}
128
129static inline void eurwdt_set_timeout(int timeout)
130{
131 eurwdt_write_reg(WDT_TIMEOUT_VAL, (u8) timeout);
132}
133
134static inline void eurwdt_disable_timer(void)
135{
136 eurwdt_set_timeout(0);
137}
138
139static void eurwdt_activate_timer(void)
140{
141 eurwdt_disable_timer();
142 eurwdt_write_reg(WDT_CTRL_REG, 0x01); /* activate the WDT */
143 eurwdt_write_reg(WDT_OUTPIN_CFG,
144 !strcmp("int", ev) ? WDT_EVENT_INT : WDT_EVENT_REBOOT);
145
146 /* Setting interrupt line */
147 if (irq == 2 || irq > 15 || irq < 0) {
148 pr_err("invalid irq number\n");
149 irq = 0; /* if invalid we disable interrupt */
150 }
151 if (irq == 0)
152 pr_info("interrupt disabled\n");
153
154 eurwdt_write_reg(WDT_TIMER_CFG, irq << 4);
155
156 eurwdt_write_reg(WDT_UNIT_SEL, WDT_UNIT_SECS); /* we use seconds */
157 eurwdt_set_timeout(0); /* the default timeout */
158}
159
160
161/*
162 * Kernel methods.
163 */
164
165static irqreturn_t eurwdt_interrupt(int irq, void *dev_id)
166{
167 pr_crit("timeout WDT timeout\n");
168
169#ifdef ONLY_TESTING
170 pr_crit("Would Reboot\n");
171#else
172 pr_crit("Initiating system reboot\n");
173 emergency_restart();
174#endif
175 return IRQ_HANDLED;
176}
177
178
179/**
180 * eurwdt_ping:
181 *
182 * Reload counter one with the watchdog timeout.
183 */
184
185static void eurwdt_ping(void)
186{
187 /* Write the watchdog default value */
188 eurwdt_set_timeout(eurwdt_timeout);
189}
190
191/**
192 * eurwdt_write:
193 * @file: file handle to the watchdog
194 * @buf: buffer to write (unused as data does not matter here
195 * @count: count of bytes
196 * @ppos: pointer to the position to write. No seeks allowed
197 *
198 * A write to a watchdog device is defined as a keepalive signal. Any
199 * write of data will do, as we we don't define content meaning.
200 */
201
202static ssize_t eurwdt_write(struct file *file, const char __user *buf,
203size_t count, loff_t *ppos)
204{
205 if (count) {
206 if (!nowayout) {
207 size_t i;
208
209 eur_expect_close = 0;
210
211 for (i = 0; i != count; i++) {
212 char c;
213 if (get_user(c, buf + i))
214 return -EFAULT;
215 if (c == 'V')
216 eur_expect_close = 42;
217 }
218 }
219 spin_lock(&eurwdt_lock);
220 eurwdt_ping(); /* the default timeout */
221 spin_unlock(&eurwdt_lock);
222 }
223 return count;
224}
225
226/**
227 * eurwdt_ioctl:
228 * @file: file handle to the device
229 * @cmd: watchdog command
230 * @arg: argument pointer
231 *
232 * The watchdog API defines a common set of functions for all watchdogs
233 * according to their available features.
234 */
235
236static long eurwdt_ioctl(struct file *file,
237 unsigned int cmd, unsigned long arg)
238{
239 void __user *argp = (void __user *)arg;
240 int __user *p = argp;
241 static const struct watchdog_info ident = {
242 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT
243 | WDIOF_MAGICCLOSE,
244 .firmware_version = 1,
245 .identity = "WDT Eurotech CPU-1220/1410",
246 };
247
248 int time;
249 int options, retval = -EINVAL;
250
251 switch (cmd) {
252 case WDIOC_GETSUPPORT:
253 return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
254
255 case WDIOC_GETSTATUS:
256 case WDIOC_GETBOOTSTATUS:
257 return put_user(0, p);
258
259 case WDIOC_SETOPTIONS:
260 if (get_user(options, p))
261 return -EFAULT;
262 spin_lock(&eurwdt_lock);
263 if (options & WDIOS_DISABLECARD) {
264 eurwdt_disable_timer();
265 retval = 0;
266 }
267 if (options & WDIOS_ENABLECARD) {
268 eurwdt_activate_timer();
269 eurwdt_ping();
270 retval = 0;
271 }
272 spin_unlock(&eurwdt_lock);
273 return retval;
274
275 case WDIOC_KEEPALIVE:
276 spin_lock(&eurwdt_lock);
277 eurwdt_ping();
278 spin_unlock(&eurwdt_lock);
279 return 0;
280
281 case WDIOC_SETTIMEOUT:
282 if (copy_from_user(&time, p, sizeof(int)))
283 return -EFAULT;
284
285 /* Sanity check */
286 if (time < 0 || time > 255)
287 return -EINVAL;
288
289 spin_lock(&eurwdt_lock);
290 eurwdt_timeout = time;
291 eurwdt_set_timeout(time);
292 spin_unlock(&eurwdt_lock);
293 /* Fall */
294
295 case WDIOC_GETTIMEOUT:
296 return put_user(eurwdt_timeout, p);
297
298 default:
299 return -ENOTTY;
300 }
301}
302
303/**
304 * eurwdt_open:
305 * @inode: inode of device
306 * @file: file handle to device
307 *
308 * The misc device has been opened. The watchdog device is single
309 * open and on opening we load the counter.
310 */
311
312static int eurwdt_open(struct inode *inode, struct file *file)
313{
314 if (test_and_set_bit(0, &eurwdt_is_open))
315 return -EBUSY;
316 eurwdt_timeout = WDT_TIMEOUT; /* initial timeout */
317 /* Activate the WDT */
318 eurwdt_activate_timer();
319 return nonseekable_open(inode, file);
320}
321
322/**
323 * eurwdt_release:
324 * @inode: inode to board
325 * @file: file handle to board
326 *
327 * The watchdog has a configurable API. There is a religious dispute
328 * between people who want their watchdog to be able to shut down and
329 * those who want to be sure if the watchdog manager dies the machine
330 * reboots. In the former case we disable the counters, in the latter
331 * case you have to open it again very soon.
332 */
333
334static int eurwdt_release(struct inode *inode, struct file *file)
335{
336 if (eur_expect_close == 42)
337 eurwdt_disable_timer();
338 else {
339 pr_crit("Unexpected close, not stopping watchdog!\n");
340 eurwdt_ping();
341 }
342 clear_bit(0, &eurwdt_is_open);
343 eur_expect_close = 0;
344 return 0;
345}
346
347/**
348 * eurwdt_notify_sys:
349 * @this: our notifier block
350 * @code: the event being reported
351 * @unused: unused
352 *
353 * Our notifier is called on system shutdowns. We want to turn the card
354 * off at reboot otherwise the machine will reboot again during memory
355 * test or worse yet during the following fsck. This would suck, in fact
356 * trust me - if it happens it does suck.
357 */
358
359static int eurwdt_notify_sys(struct notifier_block *this, unsigned long code,
360 void *unused)
361{
362 if (code == SYS_DOWN || code == SYS_HALT)
363 eurwdt_disable_timer(); /* Turn the card off */
364
365 return NOTIFY_DONE;
366}
367
368/*
369 * Kernel Interfaces
370 */
371
372
373static const struct file_operations eurwdt_fops = {
374 .owner = THIS_MODULE,
375 .llseek = no_llseek,
376 .write = eurwdt_write,
377 .unlocked_ioctl = eurwdt_ioctl,
378 .open = eurwdt_open,
379 .release = eurwdt_release,
380};
381
382static struct miscdevice eurwdt_miscdev = {
383 .minor = WATCHDOG_MINOR,
384 .name = "watchdog",
385 .fops = &eurwdt_fops,
386};
387
388/*
389 * The WDT card needs to learn about soft shutdowns in order to
390 * turn the timebomb registers off.
391 */
392
393static struct notifier_block eurwdt_notifier = {
394 .notifier_call = eurwdt_notify_sys,
395};
396
397/**
398 * cleanup_module:
399 *
400 * Unload the watchdog. You cannot do this with any file handles open.
401 * If your watchdog is set to continue ticking on close and you unload
402 * it, well it keeps ticking. We won't get the interrupt but the board
403 * will not touch PC memory so all is fine. You just have to load a new
404 * module in 60 seconds or reboot.
405 */
406
407static void __exit eurwdt_exit(void)
408{
409 eurwdt_lock_chip();
410
411 misc_deregister(&eurwdt_miscdev);
412
413 unregister_reboot_notifier(&eurwdt_notifier);
414 release_region(io, 2);
415 free_irq(irq, NULL);
416}
417
418/**
419 * eurwdt_init:
420 *
421 * Set up the WDT watchdog board. After grabbing the resources
422 * we require we need also to unlock the device.
423 * The open() function will actually kick the board off.
424 */
425
426static int __init eurwdt_init(void)
427{
428 int ret;
429
430 ret = request_irq(irq, eurwdt_interrupt, 0, "eurwdt", NULL);
431 if (ret) {
432 pr_err("IRQ %d is not free\n", irq);
433 goto out;
434 }
435
436 if (!request_region(io, 2, "eurwdt")) {
437 pr_err("IO %X is not free\n", io);
438 ret = -EBUSY;
439 goto outirq;
440 }
441
442 ret = register_reboot_notifier(&eurwdt_notifier);
443 if (ret) {
444 pr_err("can't register reboot notifier (err=%d)\n", ret);
445 goto outreg;
446 }
447
448 ret = misc_register(&eurwdt_miscdev);
449 if (ret) {
450 pr_err("can't misc_register on minor=%d\n", WATCHDOG_MINOR);
451 goto outreboot;
452 }
453
454 eurwdt_unlock_chip();
455
456 ret = 0;
457 pr_info("Eurotech WDT driver 0.01 at %X (Interrupt %d) - timeout event: %s\n",
458 io, irq, (!strcmp("int", ev) ? "int" : "reboot"));
459
460out:
461 return ret;
462
463outreboot:
464 unregister_reboot_notifier(&eurwdt_notifier);
465
466outreg:
467 release_region(io, 2);
468
469outirq:
470 free_irq(irq, NULL);
471 goto out;
472}
473
474module_init(eurwdt_init);
475module_exit(eurwdt_exit);
476
477MODULE_AUTHOR("Rodolfo Giometti");
478MODULE_DESCRIPTION("Driver for Eurotech CPU-1220/1410 on board watchdog");
479MODULE_LICENSE("GPL");
480MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Eurotech CPU-1220/1410/1420 on board WDT driver
4 *
5 * (c) Copyright 2001 Ascensit <support@ascensit.com>
6 * (c) Copyright 2001 Rodolfo Giometti <giometti@ascensit.com>
7 * (c) Copyright 2002 Rob Radez <rob@osinvestor.com>
8 *
9 * Based on wdt.c.
10 * Original copyright messages:
11 *
12 * (c) Copyright 1996-1997 Alan Cox <alan@lxorguk.ukuu.org.uk>,
13 * All Rights Reserved.
14 *
15 * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
16 * warranty for any of this software. This material is provided
17 * "AS-IS" and at no charge.
18 *
19 * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk>*
20 */
21
22/* Changelog:
23 *
24 * 2001 - Rodolfo Giometti
25 * Initial release
26 *
27 * 2002/04/25 - Rob Radez
28 * clean up #includes
29 * clean up locking
30 * make __setup param unique
31 * proper options in watchdog_info
32 * add WDIOC_GETSTATUS and WDIOC_SETOPTIONS ioctls
33 * add expect_close support
34 *
35 * 2002.05.30 - Joel Becker <joel.becker@oracle.com>
36 * Added Matt Domsch's nowayout module option.
37 */
38
39/*
40 * The eurotech CPU-1220/1410/1420's watchdog is a part
41 * of the on-board SUPER I/O device SMSC FDC 37B782.
42 */
43
44#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
45
46#include <linux/interrupt.h>
47#include <linux/module.h>
48#include <linux/moduleparam.h>
49#include <linux/types.h>
50#include <linux/miscdevice.h>
51#include <linux/watchdog.h>
52#include <linux/fs.h>
53#include <linux/ioport.h>
54#include <linux/notifier.h>
55#include <linux/reboot.h>
56#include <linux/init.h>
57#include <linux/io.h>
58#include <linux/uaccess.h>
59
60
61static unsigned long eurwdt_is_open;
62static int eurwdt_timeout;
63static char eur_expect_close;
64static DEFINE_SPINLOCK(eurwdt_lock);
65
66/*
67 * You must set these - there is no sane way to probe for this board.
68 */
69
70static int io = 0x3f0;
71static int irq = 10;
72static char *ev = "int";
73
74#define WDT_TIMEOUT 60 /* 1 minute */
75
76static bool nowayout = WATCHDOG_NOWAYOUT;
77module_param(nowayout, bool, 0);
78MODULE_PARM_DESC(nowayout,
79 "Watchdog cannot be stopped once started (default="
80 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
81
82/*
83 * Some symbolic names
84 */
85
86#define WDT_CTRL_REG 0x30
87#define WDT_OUTPIN_CFG 0xe2
88#define WDT_EVENT_INT 0x00
89#define WDT_EVENT_REBOOT 0x08
90#define WDT_UNIT_SEL 0xf1
91#define WDT_UNIT_SECS 0x80
92#define WDT_TIMEOUT_VAL 0xf2
93#define WDT_TIMER_CFG 0xf3
94
95
96module_param_hw(io, int, ioport, 0);
97MODULE_PARM_DESC(io, "Eurotech WDT io port (default=0x3f0)");
98module_param_hw(irq, int, irq, 0);
99MODULE_PARM_DESC(irq, "Eurotech WDT irq (default=10)");
100module_param(ev, charp, 0);
101MODULE_PARM_DESC(ev, "Eurotech WDT event type (default is `int')");
102
103
104/*
105 * Programming support
106 */
107
108static inline void eurwdt_write_reg(u8 index, u8 data)
109{
110 outb(index, io);
111 outb(data, io+1);
112}
113
114static inline void eurwdt_lock_chip(void)
115{
116 outb(0xaa, io);
117}
118
119static inline void eurwdt_unlock_chip(void)
120{
121 outb(0x55, io);
122 eurwdt_write_reg(0x07, 0x08); /* set the logical device */
123}
124
125static inline void eurwdt_set_timeout(int timeout)
126{
127 eurwdt_write_reg(WDT_TIMEOUT_VAL, (u8) timeout);
128}
129
130static inline void eurwdt_disable_timer(void)
131{
132 eurwdt_set_timeout(0);
133}
134
135static void eurwdt_activate_timer(void)
136{
137 eurwdt_disable_timer();
138 eurwdt_write_reg(WDT_CTRL_REG, 0x01); /* activate the WDT */
139 eurwdt_write_reg(WDT_OUTPIN_CFG,
140 !strcmp("int", ev) ? WDT_EVENT_INT : WDT_EVENT_REBOOT);
141
142 /* Setting interrupt line */
143 if (irq == 2 || irq > 15 || irq < 0) {
144 pr_err("invalid irq number\n");
145 irq = 0; /* if invalid we disable interrupt */
146 }
147 if (irq == 0)
148 pr_info("interrupt disabled\n");
149
150 eurwdt_write_reg(WDT_TIMER_CFG, irq << 4);
151
152 eurwdt_write_reg(WDT_UNIT_SEL, WDT_UNIT_SECS); /* we use seconds */
153 eurwdt_set_timeout(0); /* the default timeout */
154}
155
156
157/*
158 * Kernel methods.
159 */
160
161static irqreturn_t eurwdt_interrupt(int irq, void *dev_id)
162{
163 pr_crit("timeout WDT timeout\n");
164
165#ifdef ONLY_TESTING
166 pr_crit("Would Reboot\n");
167#else
168 pr_crit("Initiating system reboot\n");
169 emergency_restart();
170#endif
171 return IRQ_HANDLED;
172}
173
174
175/**
176 * eurwdt_ping:
177 *
178 * Reload counter one with the watchdog timeout.
179 */
180
181static void eurwdt_ping(void)
182{
183 /* Write the watchdog default value */
184 eurwdt_set_timeout(eurwdt_timeout);
185}
186
187/**
188 * eurwdt_write:
189 * @file: file handle to the watchdog
190 * @buf: buffer to write (unused as data does not matter here
191 * @count: count of bytes
192 * @ppos: pointer to the position to write. No seeks allowed
193 *
194 * A write to a watchdog device is defined as a keepalive signal. Any
195 * write of data will do, as we don't define content meaning.
196 */
197
198static ssize_t eurwdt_write(struct file *file, const char __user *buf,
199size_t count, loff_t *ppos)
200{
201 if (count) {
202 if (!nowayout) {
203 size_t i;
204
205 eur_expect_close = 0;
206
207 for (i = 0; i != count; i++) {
208 char c;
209 if (get_user(c, buf + i))
210 return -EFAULT;
211 if (c == 'V')
212 eur_expect_close = 42;
213 }
214 }
215 spin_lock(&eurwdt_lock);
216 eurwdt_ping(); /* the default timeout */
217 spin_unlock(&eurwdt_lock);
218 }
219 return count;
220}
221
222/**
223 * eurwdt_ioctl:
224 * @file: file handle to the device
225 * @cmd: watchdog command
226 * @arg: argument pointer
227 *
228 * The watchdog API defines a common set of functions for all watchdogs
229 * according to their available features.
230 */
231
232static long eurwdt_ioctl(struct file *file,
233 unsigned int cmd, unsigned long arg)
234{
235 void __user *argp = (void __user *)arg;
236 int __user *p = argp;
237 static const struct watchdog_info ident = {
238 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT
239 | WDIOF_MAGICCLOSE,
240 .firmware_version = 1,
241 .identity = "WDT Eurotech CPU-1220/1410",
242 };
243
244 int time;
245 int options, retval = -EINVAL;
246
247 switch (cmd) {
248 case WDIOC_GETSUPPORT:
249 return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
250
251 case WDIOC_GETSTATUS:
252 case WDIOC_GETBOOTSTATUS:
253 return put_user(0, p);
254
255 case WDIOC_SETOPTIONS:
256 if (get_user(options, p))
257 return -EFAULT;
258 spin_lock(&eurwdt_lock);
259 if (options & WDIOS_DISABLECARD) {
260 eurwdt_disable_timer();
261 retval = 0;
262 }
263 if (options & WDIOS_ENABLECARD) {
264 eurwdt_activate_timer();
265 eurwdt_ping();
266 retval = 0;
267 }
268 spin_unlock(&eurwdt_lock);
269 return retval;
270
271 case WDIOC_KEEPALIVE:
272 spin_lock(&eurwdt_lock);
273 eurwdt_ping();
274 spin_unlock(&eurwdt_lock);
275 return 0;
276
277 case WDIOC_SETTIMEOUT:
278 if (copy_from_user(&time, p, sizeof(int)))
279 return -EFAULT;
280
281 /* Sanity check */
282 if (time < 0 || time > 255)
283 return -EINVAL;
284
285 spin_lock(&eurwdt_lock);
286 eurwdt_timeout = time;
287 eurwdt_set_timeout(time);
288 spin_unlock(&eurwdt_lock);
289 fallthrough;
290
291 case WDIOC_GETTIMEOUT:
292 return put_user(eurwdt_timeout, p);
293
294 default:
295 return -ENOTTY;
296 }
297}
298
299/**
300 * eurwdt_open:
301 * @inode: inode of device
302 * @file: file handle to device
303 *
304 * The misc device has been opened. The watchdog device is single
305 * open and on opening we load the counter.
306 */
307
308static int eurwdt_open(struct inode *inode, struct file *file)
309{
310 if (test_and_set_bit(0, &eurwdt_is_open))
311 return -EBUSY;
312 eurwdt_timeout = WDT_TIMEOUT; /* initial timeout */
313 /* Activate the WDT */
314 eurwdt_activate_timer();
315 return stream_open(inode, file);
316}
317
318/**
319 * eurwdt_release:
320 * @inode: inode to board
321 * @file: file handle to board
322 *
323 * The watchdog has a configurable API. There is a religious dispute
324 * between people who want their watchdog to be able to shut down and
325 * those who want to be sure if the watchdog manager dies the machine
326 * reboots. In the former case we disable the counters, in the latter
327 * case you have to open it again very soon.
328 */
329
330static int eurwdt_release(struct inode *inode, struct file *file)
331{
332 if (eur_expect_close == 42)
333 eurwdt_disable_timer();
334 else {
335 pr_crit("Unexpected close, not stopping watchdog!\n");
336 eurwdt_ping();
337 }
338 clear_bit(0, &eurwdt_is_open);
339 eur_expect_close = 0;
340 return 0;
341}
342
343/**
344 * eurwdt_notify_sys:
345 * @this: our notifier block
346 * @code: the event being reported
347 * @unused: unused
348 *
349 * Our notifier is called on system shutdowns. We want to turn the card
350 * off at reboot otherwise the machine will reboot again during memory
351 * test or worse yet during the following fsck. This would suck, in fact
352 * trust me - if it happens it does suck.
353 */
354
355static int eurwdt_notify_sys(struct notifier_block *this, unsigned long code,
356 void *unused)
357{
358 if (code == SYS_DOWN || code == SYS_HALT)
359 eurwdt_disable_timer(); /* Turn the card off */
360
361 return NOTIFY_DONE;
362}
363
364/*
365 * Kernel Interfaces
366 */
367
368
369static const struct file_operations eurwdt_fops = {
370 .owner = THIS_MODULE,
371 .llseek = no_llseek,
372 .write = eurwdt_write,
373 .unlocked_ioctl = eurwdt_ioctl,
374 .compat_ioctl = compat_ptr_ioctl,
375 .open = eurwdt_open,
376 .release = eurwdt_release,
377};
378
379static struct miscdevice eurwdt_miscdev = {
380 .minor = WATCHDOG_MINOR,
381 .name = "watchdog",
382 .fops = &eurwdt_fops,
383};
384
385/*
386 * The WDT card needs to learn about soft shutdowns in order to
387 * turn the timebomb registers off.
388 */
389
390static struct notifier_block eurwdt_notifier = {
391 .notifier_call = eurwdt_notify_sys,
392};
393
394/**
395 * eurwdt_exit:
396 *
397 * Unload the watchdog. You cannot do this with any file handles open.
398 * If your watchdog is set to continue ticking on close and you unload
399 * it, well it keeps ticking. We won't get the interrupt but the board
400 * will not touch PC memory so all is fine. You just have to load a new
401 * module in 60 seconds or reboot.
402 */
403
404static void __exit eurwdt_exit(void)
405{
406 eurwdt_lock_chip();
407
408 misc_deregister(&eurwdt_miscdev);
409
410 unregister_reboot_notifier(&eurwdt_notifier);
411 release_region(io, 2);
412 free_irq(irq, NULL);
413}
414
415/**
416 * eurwdt_init:
417 *
418 * Set up the WDT watchdog board. After grabbing the resources
419 * we require we need also to unlock the device.
420 * The open() function will actually kick the board off.
421 */
422
423static int __init eurwdt_init(void)
424{
425 int ret;
426
427 ret = request_irq(irq, eurwdt_interrupt, 0, "eurwdt", NULL);
428 if (ret) {
429 pr_err("IRQ %d is not free\n", irq);
430 goto out;
431 }
432
433 if (!request_region(io, 2, "eurwdt")) {
434 pr_err("IO %X is not free\n", io);
435 ret = -EBUSY;
436 goto outirq;
437 }
438
439 ret = register_reboot_notifier(&eurwdt_notifier);
440 if (ret) {
441 pr_err("can't register reboot notifier (err=%d)\n", ret);
442 goto outreg;
443 }
444
445 ret = misc_register(&eurwdt_miscdev);
446 if (ret) {
447 pr_err("can't misc_register on minor=%d\n", WATCHDOG_MINOR);
448 goto outreboot;
449 }
450
451 eurwdt_unlock_chip();
452
453 ret = 0;
454 pr_info("Eurotech WDT driver 0.01 at %X (Interrupt %d) - timeout event: %s\n",
455 io, irq, (!strcmp("int", ev) ? "int" : "reboot"));
456
457out:
458 return ret;
459
460outreboot:
461 unregister_reboot_notifier(&eurwdt_notifier);
462
463outreg:
464 release_region(io, 2);
465
466outirq:
467 free_irq(irq, NULL);
468 goto out;
469}
470
471module_init(eurwdt_init);
472module_exit(eurwdt_exit);
473
474MODULE_AUTHOR("Rodolfo Giometti");
475MODULE_DESCRIPTION("Driver for Eurotech CPU-1220/1410 on board watchdog");
476MODULE_LICENSE("GPL");