]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/char/watchdog/wdt977.c
[PATCH] consolidate CONFIG_WATCHDOG_NOWAYOUT handling
[mirror_ubuntu-artful-kernel.git] / drivers / char / watchdog / wdt977.c
CommitLineData
1da177e4
LT
1/*
2 * Wdt977 0.03: A Watchdog Device for Netwinder W83977AF chip
3 *
4 * (c) Copyright 1998 Rebel.com (Woody Suwalski <woody@netwinder.org>)
5 *
6 * -----------------------
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 *
13 * -----------------------
14 * 14-Dec-2001 Matt Domsch <Matt_Domsch@dell.com>
15 * Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT
16 * 19-Dec-2001 Woody Suwalski: Netwinder fixes, ioctl interface
17 * 06-Jan-2002 Woody Suwalski: For compatibility, convert all timeouts
18 * from minutes to seconds.
19 * 07-Jul-2003 Daniele Bellucci: Audit return code of misc_register in
20 * nwwatchdog_init.
21 */
22
23#include <linux/module.h>
24#include <linux/moduleparam.h>
25#include <linux/config.h>
26#include <linux/types.h>
27#include <linux/kernel.h>
28#include <linux/fs.h>
29#include <linux/miscdevice.h>
30#include <linux/init.h>
31#include <linux/watchdog.h>
32#include <linux/notifier.h>
33#include <linux/reboot.h>
34
35#include <asm/io.h>
36#include <asm/system.h>
37#include <asm/mach-types.h>
38#include <asm/uaccess.h>
39
40#define PFX "Wdt977: "
41#define WATCHDOG_MINOR 130
42
43#define DEFAULT_TIMEOUT 60 /* default timeout in seconds */
44
45static int timeout = DEFAULT_TIMEOUT;
46static int timeoutM; /* timeout in minutes */
47static unsigned long timer_alive;
48static int testmode;
49static char expect_close;
50
51module_param(timeout, int, 0);
52MODULE_PARM_DESC(timeout,"Watchdog timeout in seconds (60..15300), default=" __MODULE_STRING(DEFAULT_TIMEOUT) ")");
53module_param(testmode, int, 0);
54MODULE_PARM_DESC(testmode,"Watchdog testmode (1 = no reboot), default=0");
55
4bfdf378 56static int nowayout = WATCHDOG_NOWAYOUT;
1da177e4
LT
57module_param(nowayout, int, 0);
58MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
59
60/*
61 * Start the watchdog
62 */
63
64static int wdt977_start(void)
65{
66 /* unlock the SuperIO chip */
67 outb(0x87,0x370);
68 outb(0x87,0x370);
69
70 /* select device Aux2 (device=8) and set watchdog regs F2, F3 and F4
71 * F2 has the timeout in minutes
72 * F3 could be set to the POWER LED blink (with GP17 set to PowerLed)
73 * at timeout, and to reset timer on kbd/mouse activity (not impl.)
74 * F4 is used to just clear the TIMEOUT'ed state (bit 0)
75 */
76 outb(0x07,0x370);
77 outb(0x08,0x371);
78 outb(0xF2,0x370);
79 outb(timeoutM,0x371);
80 outb(0xF3,0x370);
81 outb(0x00,0x371); /* another setting is 0E for kbd/mouse/LED */
82 outb(0xF4,0x370);
83 outb(0x00,0x371);
84
85 /* at last select device Aux1 (dev=7) and set GP16 as a watchdog output */
86 /* in test mode watch the bit 1 on F4 to indicate "triggered" */
87 if (!testmode)
88 {
89 outb(0x07,0x370);
90 outb(0x07,0x371);
91 outb(0xE6,0x370);
92 outb(0x08,0x371);
93 }
94
95 /* lock the SuperIO chip */
96 outb(0xAA,0x370);
97
98 printk(KERN_INFO PFX "activated.\n");
99
100 return 0;
101}
102
103/*
104 * Stop the watchdog
105 */
106
107static int wdt977_stop(void)
108{
109 /* unlock the SuperIO chip */
110 outb(0x87,0x370);
111 outb(0x87,0x370);
112
113 /* select device Aux2 (device=8) and set watchdog regs F2,F3 and F4
114 * F3 is reset to its default state
115 * F4 can clear the TIMEOUT'ed state (bit 0) - back to default
116 * We can not use GP17 as a PowerLed, as we use its usage as a RedLed
117 */
118 outb(0x07,0x370);
119 outb(0x08,0x371);
120 outb(0xF2,0x370);
121 outb(0xFF,0x371);
122 outb(0xF3,0x370);
123 outb(0x00,0x371);
124 outb(0xF4,0x370);
125 outb(0x00,0x371);
126 outb(0xF2,0x370);
127 outb(0x00,0x371);
128
129 /* at last select device Aux1 (dev=7) and set GP16 as a watchdog output */
130 outb(0x07,0x370);
131 outb(0x07,0x371);
132 outb(0xE6,0x370);
133 outb(0x08,0x371);
134
135 /* lock the SuperIO chip */
136 outb(0xAA,0x370);
137
138 printk(KERN_INFO PFX "shutdown.\n");
139
140 return 0;
141}
142
143/*
144 * Send a keepalive ping to the watchdog
145 * This is done by simply re-writing the timeout to reg. 0xF2
146 */
147
148static int wdt977_keepalive(void)
149{
150 /* unlock the SuperIO chip */
151 outb(0x87,0x370);
152 outb(0x87,0x370);
153
154 /* select device Aux2 (device=8) and kicks watchdog reg F2 */
155 /* F2 has the timeout in minutes */
156 outb(0x07,0x370);
157 outb(0x08,0x371);
158 outb(0xF2,0x370);
159 outb(timeoutM,0x371);
160
161 /* lock the SuperIO chip */
162 outb(0xAA,0x370);
163
164 return 0;
165}
166
167/*
168 * Set the watchdog timeout value
169 */
170
171static int wdt977_set_timeout(int t)
172{
173 int tmrval;
174
175 /* convert seconds to minutes, rounding up */
176 tmrval = (t + 59) / 60;
177
178 if (machine_is_netwinder()) {
179 /* we have a hw bug somewhere, so each 977 minute is actually only 30sec
180 * this limits the max timeout to half of device max of 255 minutes...
181 */
182 tmrval += tmrval;
183 }
184
185 if ((tmrval < 1) || (tmrval > 255))
186 return -EINVAL;
187
188 /* timeout is the timeout in seconds, timeoutM is the timeout in minutes) */
189 timeout = t;
190 timeoutM = tmrval;
191 return 0;
192}
193
194/*
195 * Get the watchdog status
196 */
197
198static int wdt977_get_status(int *status)
199{
200 int new_status;
201
202 *status=0;
203
204 /* unlock the SuperIO chip */
205 outb(0x87,0x370);
206 outb(0x87,0x370);
207
208 /* select device Aux2 (device=8) and read watchdog reg F4 */
209 outb(0x07,0x370);
210 outb(0x08,0x371);
211 outb(0xF4,0x370);
212 new_status = inb(0x371);
213
214 /* lock the SuperIO chip */
215 outb(0xAA,0x370);
216
217 if (new_status & 1)
218 *status |= WDIOF_CARDRESET;
219
220 return 0;
221}
222
223
224/*
225 * /dev/watchdog handling
226 */
227
228static int wdt977_open(struct inode *inode, struct file *file)
229{
230 /* If the watchdog is alive we don't need to start it again */
231 if( test_and_set_bit(0,&timer_alive) )
232 return -EBUSY;
233
234 if (nowayout)
235 __module_get(THIS_MODULE);
236
237 wdt977_start();
238 return nonseekable_open(inode, file);
239}
240
241static int wdt977_release(struct inode *inode, struct file *file)
242{
243 /*
244 * Shut off the timer.
245 * Lock it in if it's a module and we set nowayout
246 */
247 if (expect_close == 42)
248 {
249 wdt977_stop();
250 clear_bit(0,&timer_alive);
251 } else {
252 printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
253 wdt977_keepalive();
254 }
255 expect_close = 0;
256 return 0;
257}
258
259
260/*
261 * wdt977_write:
262 * @file: file handle to the watchdog
263 * @buf: buffer to write (unused as data does not matter here
264 * @count: count of bytes
265 * @ppos: pointer to the position to write. No seeks allowed
266 *
267 * A write to a watchdog device is defined as a keepalive signal. Any
268 * write of data will do, as we we don't define content meaning.
269 */
270
271static ssize_t wdt977_write(struct file *file, const char __user *buf,
272 size_t count, loff_t *ppos)
273{
274 if (count) {
275 if (!nowayout) {
276 size_t i;
277
278 /* In case it was set long ago */
279 expect_close = 0;
280
281 for (i = 0; i != count; i++) {
282 char c;
283 if (get_user(c, buf + i))
284 return -EFAULT;
285 if (c == 'V')
286 expect_close = 42;
287 }
288 }
289
290 wdt977_keepalive();
291 }
292 return count;
293}
294
295/*
296 * wdt977_ioctl:
297 * @inode: inode of the device
298 * @file: file handle to the device
299 * @cmd: watchdog command
300 * @arg: argument pointer
301 *
302 * The watchdog API defines a common set of functions for all watchdogs
303 * according to their available features.
304 */
305
306static struct watchdog_info ident = {
307 .options = WDIOF_SETTIMEOUT |
308 WDIOF_MAGICCLOSE |
309 WDIOF_KEEPALIVEPING,
310 .firmware_version = 1,
311 .identity = "Winbond 83977",
312};
313
314static int wdt977_ioctl(struct inode *inode, struct file *file,
315 unsigned int cmd, unsigned long arg)
316{
317 int status;
318 int new_options, retval = -EINVAL;
319 int new_timeout;
320 union {
321 struct watchdog_info __user *ident;
322 int __user *i;
323 } uarg;
324
325 uarg.i = (int __user *)arg;
326
327 switch(cmd)
328 {
329 default:
330 return -ENOIOCTLCMD;
331
332 case WDIOC_GETSUPPORT:
333 return copy_to_user(uarg.ident, &ident,
334 sizeof(ident)) ? -EFAULT : 0;
335
336 case WDIOC_GETSTATUS:
337 wdt977_get_status(&status);
338 return put_user(status, uarg.i);
339
340 case WDIOC_GETBOOTSTATUS:
341 return put_user(0, uarg.i);
342
343 case WDIOC_KEEPALIVE:
344 wdt977_keepalive();
345 return 0;
346
347 case WDIOC_SETOPTIONS:
348 if (get_user (new_options, uarg.i))
349 return -EFAULT;
350
351 if (new_options & WDIOS_DISABLECARD) {
352 wdt977_stop();
353 retval = 0;
354 }
355
356 if (new_options & WDIOS_ENABLECARD) {
357 wdt977_start();
358 retval = 0;
359 }
360
361 return retval;
362
363 case WDIOC_SETTIMEOUT:
364 if (get_user(new_timeout, uarg.i))
365 return -EFAULT;
366
367 if (wdt977_set_timeout(new_timeout))
368 return -EINVAL;
369
370 wdt977_keepalive();
371 /* Fall */
372
373 case WDIOC_GETTIMEOUT:
374 return put_user(timeout, uarg.i);
375
376 }
377}
378
379static int wdt977_notify_sys(struct notifier_block *this, unsigned long code,
380 void *unused)
381{
382 if(code==SYS_DOWN || code==SYS_HALT)
383 wdt977_stop();
384 return NOTIFY_DONE;
385}
386
387static struct file_operations wdt977_fops=
388{
389 .owner = THIS_MODULE,
390 .llseek = no_llseek,
391 .write = wdt977_write,
392 .ioctl = wdt977_ioctl,
393 .open = wdt977_open,
394 .release = wdt977_release,
395};
396
397static struct miscdevice wdt977_miscdev=
398{
399 .minor = WATCHDOG_MINOR,
400 .name = "watchdog",
401 .fops = &wdt977_fops,
402};
403
404static struct notifier_block wdt977_notifier = {
405 .notifier_call = wdt977_notify_sys,
406};
407
408static int __init nwwatchdog_init(void)
409{
410 int retval;
411 if (!machine_is_netwinder())
412 return -ENODEV;
413
414 /* Check that the timeout value is within it's range ; if not reset to the default */
415 if (wdt977_set_timeout(timeout)) {
416 wdt977_set_timeout(DEFAULT_TIMEOUT);
417 printk(KERN_INFO PFX "timeout value must be 60<timeout<15300, using %d\n",
418 DEFAULT_TIMEOUT);
419 }
420
421 retval = register_reboot_notifier(&wdt977_notifier);
422 if (retval) {
423 printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
424 retval);
425 return retval;
426 }
427
428 retval = misc_register(&wdt977_miscdev);
429 if (retval) {
430 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
431 WATCHDOG_MINOR, retval);
432 unregister_reboot_notifier(&wdt977_notifier);
433 return retval;
434 }
435
436 printk(KERN_INFO PFX "initialized. timeout=%d sec (nowayout=%d, testmode = %i)\n",
437 timeout, nowayout, testmode);
438
439 return 0;
440}
441
442static void __exit nwwatchdog_exit(void)
443{
444 misc_deregister(&wdt977_miscdev);
445 unregister_reboot_notifier(&wdt977_notifier);
446}
447
448module_init(nwwatchdog_init);
449module_exit(nwwatchdog_exit);
450
451MODULE_AUTHOR("Woody Suwalski <woody@netwinder.org>");
452MODULE_DESCRIPTION("W83977AF Watchdog driver");
453MODULE_LICENSE("GPL");
454MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);