blob: 4c3400574ada461b3dcb186469147a9ca8537db2 [file] [log] [blame]
Sven Anders485ae772006-08-24 17:11:50 +02001/*
2 * SMsC 37B787 Watchdog Timer driver for Linux 2.6.x.x
3 *
Alan Cox29fa0582008-10-27 15:17:56 +00004 * Based on acquirewdt.c by Alan Cox <alan@lxorguk.ukuu.org.uk>
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +00005 * and some other existing drivers
Sven Anders485ae772006-08-24 17:11:50 +02006 *
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.
Wim Van Sebroeck8386c8c2006-09-02 19:32:26 +020011 *
Sven Anders485ae772006-08-24 17:11:50 +020012 * The authors do NOT admit liability nor provide warranty for
13 * any of this software. This material is provided "AS-IS" in
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +000014 * the hope that it may be useful for others.
Sven Anders485ae772006-08-24 17:11:50 +020015 *
16 * (C) Copyright 2003-2006 Sven Anders <anders@anduras.de>
17 *
18 * History:
19 * 2003 - Created version 1.0 for Linux 2.4.x.
20 * 2006 - Ported to Linux 2.6, added nowayout and MAGICCLOSE
Alan Cox59846792008-05-19 14:09:00 +010021 * features. Released version 1.1
Sven Anders485ae772006-08-24 17:11:50 +020022 *
23 * Theory of operation:
24 *
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +000025 * A Watchdog Timer (WDT) is a hardware circuit that can
26 * reset the computer system in case of a software fault.
27 * You probably knew that already.
Sven Anders485ae772006-08-24 17:11:50 +020028 *
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +000029 * Usually a userspace daemon will notify the kernel WDT driver
30 * via the /dev/watchdog special device file that userspace is
31 * still alive, at regular intervals. When such a notification
32 * occurs, the driver will usually tell the hardware watchdog
33 * that everything is in order, and that the watchdog should wait
34 * for yet another little while to reset the system.
35 * If userspace fails (RAM error, kernel bug, whatever), the
36 * notifications cease to occur, and the hardware watchdog will
37 * reset the system (causing a reboot) after the timeout occurs.
Sven Anders485ae772006-08-24 17:11:50 +020038 *
39 * Create device with:
40 * mknod /dev/watchdog c 10 130
41 *
42 * For an example userspace keep-alive daemon, see:
Paul Bolle395cf962011-08-15 02:02:26 +020043 * Documentation/watchdog/wdt.txt
Sven Anders485ae772006-08-24 17:11:50 +020044 */
45
Joe Perches27c766a2012-02-15 15:06:19 -080046#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
47
Sven Anders485ae772006-08-24 17:11:50 +020048#include <linux/module.h>
49#include <linux/moduleparam.h>
50#include <linux/types.h>
51#include <linux/miscdevice.h>
52#include <linux/watchdog.h>
53#include <linux/delay.h>
54#include <linux/fs.h>
55#include <linux/ioport.h>
56#include <linux/notifier.h>
57#include <linux/reboot.h>
58#include <linux/init.h>
Wim Van Sebroeckaa1fd4d2006-09-02 20:53:19 +020059#include <linux/spinlock.h>
Alan Cox59846792008-05-19 14:09:00 +010060#include <linux/io.h>
61#include <linux/uaccess.h>
Sven Anders485ae772006-08-24 17:11:50 +020062
Sven Anders485ae772006-08-24 17:11:50 +020063#include <asm/system.h>
64
65/* enable support for minutes as units? */
66/* (does not always work correctly, so disabled by default!) */
67#define SMSC_SUPPORT_MINUTES
68#undef SMSC_SUPPORT_MINUTES
69
70#define MAX_TIMEOUT 255
71
72#define UNIT_SECOND 0
73#define UNIT_MINUTE 1
74
Alan Cox59846792008-05-19 14:09:00 +010075#define VERSION "1.1"
Sven Anders485ae772006-08-24 17:11:50 +020076
Alan Cox59846792008-05-19 14:09:00 +010077#define IOPORT 0x3F0
Sven Anders485ae772006-08-24 17:11:50 +020078#define IOPORT_SIZE 2
Alan Cox59846792008-05-19 14:09:00 +010079#define IODEV_NO 8
Sven Anders485ae772006-08-24 17:11:50 +020080
Alan Cox59846792008-05-19 14:09:00 +010081static int unit = UNIT_SECOND; /* timer's unit */
82static int timeout = 60; /* timeout value: default is 60 "units" */
83static unsigned long timer_enabled; /* is the timer enabled? */
Sven Anders485ae772006-08-24 17:11:50 +020084
85static char expect_close; /* is the close expected? */
86
Alexey Dobriyanc7dfd0c2007-11-01 16:27:08 -070087static DEFINE_SPINLOCK(io_lock);/* to guard the watchdog from io races */
Wim Van Sebroeckaa1fd4d2006-09-02 20:53:19 +020088
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010089static bool nowayout = WATCHDOG_NOWAYOUT;
Sven Anders485ae772006-08-24 17:11:50 +020090
91/* -- Low level function ----------------------------------------*/
92
93/* unlock the IO chip */
94
95static inline void open_io_config(void)
96{
Alan Cox59846792008-05-19 14:09:00 +010097 outb(0x55, IOPORT);
Sven Anders485ae772006-08-24 17:11:50 +020098 mdelay(1);
Alan Cox59846792008-05-19 14:09:00 +010099 outb(0x55, IOPORT);
Sven Anders485ae772006-08-24 17:11:50 +0200100}
101
102/* lock the IO chip */
103static inline void close_io_config(void)
104{
Alan Cox59846792008-05-19 14:09:00 +0100105 outb(0xAA, IOPORT);
Sven Anders485ae772006-08-24 17:11:50 +0200106}
107
108/* select the IO device */
109static inline void select_io_device(unsigned char devno)
110{
Alan Cox59846792008-05-19 14:09:00 +0100111 outb(0x07, IOPORT);
112 outb(devno, IOPORT+1);
Sven Anders485ae772006-08-24 17:11:50 +0200113}
114
115/* write to the control register */
116static inline void write_io_cr(unsigned char reg, unsigned char data)
117{
Alan Cox59846792008-05-19 14:09:00 +0100118 outb(reg, IOPORT);
119 outb(data, IOPORT+1);
Sven Anders485ae772006-08-24 17:11:50 +0200120}
121
122/* read from the control register */
123static inline char read_io_cr(unsigned char reg)
124{
Alan Cox59846792008-05-19 14:09:00 +0100125 outb(reg, IOPORT);
126 return inb(IOPORT+1);
Sven Anders485ae772006-08-24 17:11:50 +0200127}
128
129/* -- Medium level functions ------------------------------------*/
130
131static inline void gpio_bit12(unsigned char reg)
132{
Alan Cox59846792008-05-19 14:09:00 +0100133 /* -- General Purpose I/O Bit 1.2 --
134 * Bit 0, In/Out: 0 = Output, 1 = Input
135 * Bit 1, Polarity: 0 = No Invert, 1 = Invert
136 * Bit 2, Group Enable Intr.: 0 = Disable, 1 = Enable
137 * Bit 3/4, Function select: 00 = GPI/O, 01 = WDT, 10 = P17,
138 * 11 = Either Edge Triggered Intr. 2
139 * Bit 5/6 (Reserved)
140 * Bit 7, Output Type: 0 = Push Pull Bit, 1 = Open Drain
141 */
142 write_io_cr(0xE2, reg);
Sven Anders485ae772006-08-24 17:11:50 +0200143}
144
145static inline void gpio_bit13(unsigned char reg)
146{
Alan Cox59846792008-05-19 14:09:00 +0100147 /* -- General Purpose I/O Bit 1.3 --
148 * Bit 0, In/Out: 0 = Output, 1 = Input
149 * Bit 1, Polarity: 0 = No Invert, 1 = Invert
150 * Bit 2, Group Enable Intr.: 0 = Disable, 1 = Enable
151 * Bit 3, Function select: 0 = GPI/O, 1 = LED
152 * Bit 4-6 (Reserved)
153 * Bit 7, Output Type: 0 = Push Pull Bit, 1 = Open Drain
154 */
155 write_io_cr(0xE3, reg);
Sven Anders485ae772006-08-24 17:11:50 +0200156}
157
158static inline void wdt_timer_units(unsigned char new_units)
159{
Alan Cox59846792008-05-19 14:09:00 +0100160 /* -- Watchdog timer units --
161 * Bit 0-6 (Reserved)
162 * Bit 7, WDT Time-out Value Units Select
163 * (0 = Minutes, 1 = Seconds)
164 */
165 write_io_cr(0xF1, new_units);
Sven Anders485ae772006-08-24 17:11:50 +0200166}
167
168static inline void wdt_timeout_value(unsigned char new_timeout)
169{
Alan Cox59846792008-05-19 14:09:00 +0100170 /* -- Watchdog Timer Time-out Value --
171 * Bit 0-7 Binary coded units (0=Disabled, 1..255)
172 */
173 write_io_cr(0xF2, new_timeout);
Sven Anders485ae772006-08-24 17:11:50 +0200174}
175
176static inline void wdt_timer_conf(unsigned char conf)
177{
Alan Cox59846792008-05-19 14:09:00 +0100178 /* -- Watchdog timer configuration --
179 * Bit 0 Joystick enable: 0* = No Reset, 1 = Reset WDT upon
180 * Gameport I/O
181 * Bit 1 Keyboard enable: 0* = No Reset, 1 = Reset WDT upon KBD Intr.
182 * Bit 2 Mouse enable: 0* = No Reset, 1 = Reset WDT upon Mouse Intr
183 * Bit 3 Reset the timer
184 * (Wrong in SMsC documentation? Given as: PowerLED Timout
185 * Enabled)
186 * Bit 4-7 WDT Interrupt Mapping: (0000* = Disabled,
187 * 0001=IRQ1, 0010=(Invalid), 0011=IRQ3 to 1111=IRQ15)
188 */
189 write_io_cr(0xF3, conf);
Sven Anders485ae772006-08-24 17:11:50 +0200190}
191
192static inline void wdt_timer_ctrl(unsigned char reg)
193{
Alan Cox59846792008-05-19 14:09:00 +0100194 /* -- Watchdog timer control --
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300195 * Bit 0 Status Bit: 0 = Timer counting, 1 = Timeout occurred
Alan Cox59846792008-05-19 14:09:00 +0100196 * Bit 1 Power LED Toggle: 0 = Disable Toggle, 1 = Toggle at 1 Hz
197 * Bit 2 Force Timeout: 1 = Forces WD timeout event (self-cleaning)
198 * Bit 3 P20 Force Timeout enabled:
199 * 0 = P20 activity does not generate the WD timeout event
200 * 1 = P20 Allows rising edge of P20, from the keyboard
201 * controller, to force the WD timeout event.
202 * Bit 4 (Reserved)
203 * -- Soft power management --
204 * Bit 5 Stop Counter: 1 = Stop software power down counter
205 * set via register 0xB8, (self-cleaning)
206 * (Upon read: 0 = Counter running, 1 = Counter stopped)
207 * Bit 6 Restart Counter: 1 = Restart software power down counter
208 * set via register 0xB8, (self-cleaning)
209 * Bit 7 SPOFF: 1 = Force software power down (self-cleaning)
210 */
211 write_io_cr(0xF4, reg);
Sven Anders485ae772006-08-24 17:11:50 +0200212}
213
214/* -- Higher level functions ------------------------------------*/
215
216/* initialize watchdog */
217
218static void wb_smsc_wdt_initialize(void)
219{
Alan Cox59846792008-05-19 14:09:00 +0100220 unsigned char old;
Sven Anders485ae772006-08-24 17:11:50 +0200221
Wim Van Sebroeckaa1fd4d2006-09-02 20:53:19 +0200222 spin_lock(&io_lock);
Alan Cox59846792008-05-19 14:09:00 +0100223 open_io_config();
224 select_io_device(IODEV_NO);
Sven Anders485ae772006-08-24 17:11:50 +0200225
Alan Cox59846792008-05-19 14:09:00 +0100226 /* enable the watchdog */
227 gpio_bit13(0x08); /* Select pin 80 = LED not GPIO */
228 gpio_bit12(0x0A); /* Set pin 79 = WDT not
229 GPIO/Output/Polarity=Invert */
230 /* disable the timeout */
231 wdt_timeout_value(0);
Sven Anders485ae772006-08-24 17:11:50 +0200232
Alan Cox59846792008-05-19 14:09:00 +0100233 /* reset control register */
234 wdt_timer_ctrl(0x00);
Sven Anders485ae772006-08-24 17:11:50 +0200235
Alan Cox59846792008-05-19 14:09:00 +0100236 /* reset configuration register */
Sven Anders485ae772006-08-24 17:11:50 +0200237 wdt_timer_conf(0x00);
238
Alan Cox59846792008-05-19 14:09:00 +0100239 /* read old (timer units) register */
240 old = read_io_cr(0xF1) & 0x7F;
241 if (unit == UNIT_SECOND)
242 old |= 0x80; /* set to seconds */
Sven Anders485ae772006-08-24 17:11:50 +0200243
Alan Cox59846792008-05-19 14:09:00 +0100244 /* set the watchdog timer units */
245 wdt_timer_units(old);
Sven Anders485ae772006-08-24 17:11:50 +0200246
Alan Cox59846792008-05-19 14:09:00 +0100247 close_io_config();
Wim Van Sebroeckaa1fd4d2006-09-02 20:53:19 +0200248 spin_unlock(&io_lock);
Sven Anders485ae772006-08-24 17:11:50 +0200249}
250
251/* shutdown the watchdog */
252
253static void wb_smsc_wdt_shutdown(void)
254{
Wim Van Sebroeckaa1fd4d2006-09-02 20:53:19 +0200255 spin_lock(&io_lock);
Alan Cox59846792008-05-19 14:09:00 +0100256 open_io_config();
257 select_io_device(IODEV_NO);
Sven Anders485ae772006-08-24 17:11:50 +0200258
Alan Cox59846792008-05-19 14:09:00 +0100259 /* disable the watchdog */
260 gpio_bit13(0x09);
261 gpio_bit12(0x09);
Sven Anders485ae772006-08-24 17:11:50 +0200262
Alan Cox59846792008-05-19 14:09:00 +0100263 /* reset watchdog config register */
Sven Anders485ae772006-08-24 17:11:50 +0200264 wdt_timer_conf(0x00);
265
Alan Cox59846792008-05-19 14:09:00 +0100266 /* reset watchdog control register */
267 wdt_timer_ctrl(0x00);
Sven Anders485ae772006-08-24 17:11:50 +0200268
Alan Cox59846792008-05-19 14:09:00 +0100269 /* disable timeout */
270 wdt_timeout_value(0x00);
Sven Anders485ae772006-08-24 17:11:50 +0200271
Alan Cox59846792008-05-19 14:09:00 +0100272 close_io_config();
Wim Van Sebroeckaa1fd4d2006-09-02 20:53:19 +0200273 spin_unlock(&io_lock);
Sven Anders485ae772006-08-24 17:11:50 +0200274}
275
276/* set timeout => enable watchdog */
277
278static void wb_smsc_wdt_set_timeout(unsigned char new_timeout)
279{
Wim Van Sebroeckaa1fd4d2006-09-02 20:53:19 +0200280 spin_lock(&io_lock);
Alan Cox59846792008-05-19 14:09:00 +0100281 open_io_config();
282 select_io_device(IODEV_NO);
Sven Anders485ae772006-08-24 17:11:50 +0200283
Alan Cox59846792008-05-19 14:09:00 +0100284 /* set Power LED to blink, if we enable the timeout */
285 wdt_timer_ctrl((new_timeout == 0) ? 0x00 : 0x02);
Sven Anders485ae772006-08-24 17:11:50 +0200286
Alan Cox59846792008-05-19 14:09:00 +0100287 /* set timeout value */
288 wdt_timeout_value(new_timeout);
Sven Anders485ae772006-08-24 17:11:50 +0200289
Alan Cox59846792008-05-19 14:09:00 +0100290 close_io_config();
Wim Van Sebroeckaa1fd4d2006-09-02 20:53:19 +0200291 spin_unlock(&io_lock);
Sven Anders485ae772006-08-24 17:11:50 +0200292}
293
294/* get timeout */
295
296static unsigned char wb_smsc_wdt_get_timeout(void)
297{
Alan Cox59846792008-05-19 14:09:00 +0100298 unsigned char set_timeout;
Sven Anders485ae772006-08-24 17:11:50 +0200299
Wim Van Sebroeckaa1fd4d2006-09-02 20:53:19 +0200300 spin_lock(&io_lock);
Alan Cox59846792008-05-19 14:09:00 +0100301 open_io_config();
302 select_io_device(IODEV_NO);
303 set_timeout = read_io_cr(0xF2);
304 close_io_config();
Wim Van Sebroeckaa1fd4d2006-09-02 20:53:19 +0200305 spin_unlock(&io_lock);
Sven Anders485ae772006-08-24 17:11:50 +0200306
Alan Cox59846792008-05-19 14:09:00 +0100307 return set_timeout;
Sven Anders485ae772006-08-24 17:11:50 +0200308}
309
310/* disable watchdog */
311
312static void wb_smsc_wdt_disable(void)
313{
Alan Cox59846792008-05-19 14:09:00 +0100314 /* set the timeout to 0 to disable the watchdog */
315 wb_smsc_wdt_set_timeout(0);
Sven Anders485ae772006-08-24 17:11:50 +0200316}
317
318/* enable watchdog by setting the current timeout */
319
320static void wb_smsc_wdt_enable(void)
321{
Alan Cox59846792008-05-19 14:09:00 +0100322 /* set the current timeout... */
323 wb_smsc_wdt_set_timeout(timeout);
Sven Anders485ae772006-08-24 17:11:50 +0200324}
325
326/* reset the timer */
327
328static void wb_smsc_wdt_reset_timer(void)
329{
Wim Van Sebroeckaa1fd4d2006-09-02 20:53:19 +0200330 spin_lock(&io_lock);
Alan Cox59846792008-05-19 14:09:00 +0100331 open_io_config();
332 select_io_device(IODEV_NO);
Sven Anders485ae772006-08-24 17:11:50 +0200333
Alan Cox59846792008-05-19 14:09:00 +0100334 /* reset the timer */
Sven Anders485ae772006-08-24 17:11:50 +0200335 wdt_timeout_value(timeout);
336 wdt_timer_conf(0x08);
337
Alan Cox59846792008-05-19 14:09:00 +0100338 close_io_config();
Wim Van Sebroeckaa1fd4d2006-09-02 20:53:19 +0200339 spin_unlock(&io_lock);
Sven Anders485ae772006-08-24 17:11:50 +0200340}
341
342/* return, if the watchdog is enabled (timeout is set...) */
343
344static int wb_smsc_wdt_status(void)
345{
346 return (wb_smsc_wdt_get_timeout() == 0) ? 0 : WDIOF_KEEPALIVEPING;
347}
348
349
350/* -- File operations -------------------------------------------*/
351
352/* open => enable watchdog and set initial timeout */
353
354static int wb_smsc_wdt_open(struct inode *inode, struct file *file)
355{
356 /* /dev/watchdog can only be opened once */
357
Wim Van Sebroeckaa1fd4d2006-09-02 20:53:19 +0200358 if (test_and_set_bit(0, &timer_enabled))
Sven Anders485ae772006-08-24 17:11:50 +0200359 return -EBUSY;
360
361 if (nowayout)
362 __module_get(THIS_MODULE);
363
364 /* Reload and activate timer */
Sven Anders485ae772006-08-24 17:11:50 +0200365 wb_smsc_wdt_enable();
366
Joe Perches27c766a2012-02-15 15:06:19 -0800367 pr_info("Watchdog enabled. Timeout set to %d %s\n",
Alan Cox59846792008-05-19 14:09:00 +0100368 timeout, (unit == UNIT_SECOND) ? "second(s)" : "minute(s)");
Sven Anders485ae772006-08-24 17:11:50 +0200369
370 return nonseekable_open(inode, file);
371}
372
373/* close => shut off the timer */
374
375static int wb_smsc_wdt_release(struct inode *inode, struct file *file)
376{
377 /* Shut off the timer. */
378
379 if (expect_close == 42) {
Alan Cox59846792008-05-19 14:09:00 +0100380 wb_smsc_wdt_disable();
Joe Perches27c766a2012-02-15 15:06:19 -0800381 pr_info("Watchdog disabled, sleeping again...\n");
Sven Anders485ae772006-08-24 17:11:50 +0200382 } else {
Joe Perches27c766a2012-02-15 15:06:19 -0800383 pr_crit("Unexpected close, not stopping watchdog!\n");
Sven Anders485ae772006-08-24 17:11:50 +0200384 wb_smsc_wdt_reset_timer();
385 }
386
Wim Van Sebroeckaa1fd4d2006-09-02 20:53:19 +0200387 clear_bit(0, &timer_enabled);
Sven Anders485ae772006-08-24 17:11:50 +0200388 expect_close = 0;
389 return 0;
390}
391
392/* write => update the timer to keep the machine alive */
393
394static ssize_t wb_smsc_wdt_write(struct file *file, const char __user *data,
395 size_t len, loff_t *ppos)
396{
397 /* See if we got the magic character 'V' and reload the timer */
398 if (len) {
399 if (!nowayout) {
400 size_t i;
401
402 /* reset expect flag */
403 expect_close = 0;
404
Alan Cox59846792008-05-19 14:09:00 +0100405 /* scan to see whether or not we got the
406 magic character */
Sven Anders485ae772006-08-24 17:11:50 +0200407 for (i = 0; i != len; i++) {
408 char c;
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000409 if (get_user(c, data + i))
Sven Anders485ae772006-08-24 17:11:50 +0200410 return -EFAULT;
411 if (c == 'V')
412 expect_close = 42;
413 }
414 }
415
416 /* someone wrote to us, we should reload the timer */
417 wb_smsc_wdt_reset_timer();
418 }
419 return len;
420}
421
422/* ioctl => control interface */
423
Alan Cox59846792008-05-19 14:09:00 +0100424static long wb_smsc_wdt_ioctl(struct file *file,
425 unsigned int cmd, unsigned long arg)
Sven Anders485ae772006-08-24 17:11:50 +0200426{
427 int new_timeout;
428
429 union {
430 struct watchdog_info __user *ident;
431 int __user *i;
432 } uarg;
433
Alan Cox59846792008-05-19 14:09:00 +0100434 static const struct watchdog_info ident = {
Wim Van Sebroeck5f3b2752011-02-23 20:04:38 +0000435 .options = WDIOF_KEEPALIVEPING |
Alan Cox59846792008-05-19 14:09:00 +0100436 WDIOF_SETTIMEOUT |
Sven Anders485ae772006-08-24 17:11:50 +0200437 WDIOF_MAGICCLOSE,
438 .firmware_version = 0,
Wim Van Sebroeck5f3b2752011-02-23 20:04:38 +0000439 .identity = "SMsC 37B787 Watchdog",
Sven Anders485ae772006-08-24 17:11:50 +0200440 };
441
442 uarg.i = (int __user *)arg;
443
444 switch (cmd) {
Alan Cox59846792008-05-19 14:09:00 +0100445 case WDIOC_GETSUPPORT:
446 return copy_to_user(uarg.ident, &ident, sizeof(ident))
447 ? -EFAULT : 0;
448 case WDIOC_GETSTATUS:
449 return put_user(wb_smsc_wdt_status(), uarg.i);
450 case WDIOC_GETBOOTSTATUS:
451 return put_user(0, uarg.i);
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000452 case WDIOC_SETOPTIONS:
453 {
454 int options, retval = -EINVAL;
455
456 if (get_user(options, uarg.i))
457 return -EFAULT;
458
459 if (options & WDIOS_DISABLECARD) {
460 wb_smsc_wdt_disable();
461 retval = 0;
462 }
463 if (options & WDIOS_ENABLECARD) {
464 wb_smsc_wdt_enable();
465 retval = 0;
466 }
467 return retval;
468 }
Alan Cox59846792008-05-19 14:09:00 +0100469 case WDIOC_KEEPALIVE:
470 wb_smsc_wdt_reset_timer();
471 return 0;
472 case WDIOC_SETTIMEOUT:
473 if (get_user(new_timeout, uarg.i))
474 return -EFAULT;
475 /* the API states this is given in secs */
476 if (unit == UNIT_MINUTE)
477 new_timeout /= 60;
478 if (new_timeout < 0 || new_timeout > MAX_TIMEOUT)
479 return -EINVAL;
480 timeout = new_timeout;
481 wb_smsc_wdt_set_timeout(timeout);
482 /* fall through and return the new timeout... */
483 case WDIOC_GETTIMEOUT:
484 new_timeout = timeout;
485 if (unit == UNIT_MINUTE)
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +0000486 new_timeout *= 60;
Alan Cox59846792008-05-19 14:09:00 +0100487 return put_user(new_timeout, uarg.i);
Alan Cox59846792008-05-19 14:09:00 +0100488 default:
489 return -ENOTTY;
Sven Anders485ae772006-08-24 17:11:50 +0200490 }
491}
492
493/* -- Notifier funtions -----------------------------------------*/
494
Alan Cox59846792008-05-19 14:09:00 +0100495static int wb_smsc_wdt_notify_sys(struct notifier_block *this,
496 unsigned long code, void *unused)
Sven Anders485ae772006-08-24 17:11:50 +0200497{
Alan Cox59846792008-05-19 14:09:00 +0100498 if (code == SYS_DOWN || code == SYS_HALT) {
499 /* set timeout to 0, to avoid possible race-condition */
500 timeout = 0;
Sven Anders485ae772006-08-24 17:11:50 +0200501 wb_smsc_wdt_disable();
502 }
503 return NOTIFY_DONE;
504}
505
506/* -- Module's structures ---------------------------------------*/
507
Alan Cox59846792008-05-19 14:09:00 +0100508static const struct file_operations wb_smsc_wdt_fops = {
509 .owner = THIS_MODULE,
Sven Anders485ae772006-08-24 17:11:50 +0200510 .llseek = no_llseek,
511 .write = wb_smsc_wdt_write,
Alan Cox59846792008-05-19 14:09:00 +0100512 .unlocked_ioctl = wb_smsc_wdt_ioctl,
Sven Anders485ae772006-08-24 17:11:50 +0200513 .open = wb_smsc_wdt_open,
Wim Van Sebroeckaa1fd4d2006-09-02 20:53:19 +0200514 .release = wb_smsc_wdt_release,
Sven Anders485ae772006-08-24 17:11:50 +0200515};
516
Alan Cox59846792008-05-19 14:09:00 +0100517static struct notifier_block wb_smsc_wdt_notifier = {
Wim Van Sebroeckaa1fd4d2006-09-02 20:53:19 +0200518 .notifier_call = wb_smsc_wdt_notify_sys,
Sven Anders485ae772006-08-24 17:11:50 +0200519};
520
Alan Cox59846792008-05-19 14:09:00 +0100521static struct miscdevice wb_smsc_wdt_miscdev = {
Sven Anders485ae772006-08-24 17:11:50 +0200522 .minor = WATCHDOG_MINOR,
523 .name = "watchdog",
524 .fops = &wb_smsc_wdt_fops,
525};
526
527/* -- Module init functions -------------------------------------*/
528
529/* module's "constructor" */
530
531static int __init wb_smsc_wdt_init(void)
532{
533 int ret;
534
Joe Perches27c766a2012-02-15 15:06:19 -0800535 pr_info("SMsC 37B787 watchdog component driver "
536 VERSION " initialising...\n");
Sven Anders485ae772006-08-24 17:11:50 +0200537
538 if (!request_region(IOPORT, IOPORT_SIZE, "SMsC 37B787 watchdog")) {
Joe Perches27c766a2012-02-15 15:06:19 -0800539 pr_err("Unable to register IO port %#x\n", IOPORT);
Sven Anders485ae772006-08-24 17:11:50 +0200540 ret = -EBUSY;
541 goto out_pnp;
542 }
543
Alan Cox59846792008-05-19 14:09:00 +0100544 /* set new maximum, if it's too big */
545 if (timeout > MAX_TIMEOUT)
546 timeout = MAX_TIMEOUT;
Wim Van Sebroeckaa1fd4d2006-09-02 20:53:19 +0200547
Alan Cox59846792008-05-19 14:09:00 +0100548 /* init the watchdog timer */
549 wb_smsc_wdt_initialize();
Wim Van Sebroeckaa1fd4d2006-09-02 20:53:19 +0200550
Sven Anders485ae772006-08-24 17:11:50 +0200551 ret = register_reboot_notifier(&wb_smsc_wdt_notifier);
552 if (ret) {
Joe Perches27c766a2012-02-15 15:06:19 -0800553 pr_err("Unable to register reboot notifier err = %d\n", ret);
Sven Anders485ae772006-08-24 17:11:50 +0200554 goto out_io;
555 }
556
557 ret = misc_register(&wb_smsc_wdt_miscdev);
558 if (ret) {
Joe Perches27c766a2012-02-15 15:06:19 -0800559 pr_err("Unable to register miscdev on minor %d\n",
560 WATCHDOG_MINOR);
Sven Anders485ae772006-08-24 17:11:50 +0200561 goto out_rbt;
562 }
563
Alan Cox59846792008-05-19 14:09:00 +0100564 /* output info */
Joe Perches27c766a2012-02-15 15:06:19 -0800565 pr_info("Timeout set to %d %s\n",
Alan Cox59846792008-05-19 14:09:00 +0100566 timeout, (unit == UNIT_SECOND) ? "second(s)" : "minute(s)");
Joe Perches27c766a2012-02-15 15:06:19 -0800567 pr_info("Watchdog initialized and sleeping (nowayout=%d)...\n",
568 nowayout);
Sven Anders485ae772006-08-24 17:11:50 +0200569out_clean:
570 return ret;
571
572out_rbt:
573 unregister_reboot_notifier(&wb_smsc_wdt_notifier);
574
575out_io:
576 release_region(IOPORT, IOPORT_SIZE);
577
578out_pnp:
579 goto out_clean;
Wim Van Sebroeck8386c8c2006-09-02 19:32:26 +0200580}
Sven Anders485ae772006-08-24 17:11:50 +0200581
582/* module's "destructor" */
583
584static void __exit wb_smsc_wdt_exit(void)
585{
586 /* Stop the timer before we leave */
Alan Cox59846792008-05-19 14:09:00 +0100587 if (!nowayout) {
Sven Anders485ae772006-08-24 17:11:50 +0200588 wb_smsc_wdt_shutdown();
Joe Perches27c766a2012-02-15 15:06:19 -0800589 pr_info("Watchdog disabled\n");
Sven Anders485ae772006-08-24 17:11:50 +0200590 }
591
592 misc_deregister(&wb_smsc_wdt_miscdev);
593 unregister_reboot_notifier(&wb_smsc_wdt_notifier);
594 release_region(IOPORT, IOPORT_SIZE);
595
Joe Perches27c766a2012-02-15 15:06:19 -0800596 pr_info("SMsC 37B787 watchdog component driver removed\n");
Sven Anders485ae772006-08-24 17:11:50 +0200597}
598
599module_init(wb_smsc_wdt_init);
600module_exit(wb_smsc_wdt_exit);
601
602MODULE_AUTHOR("Sven Anders <anders@anduras.de>");
Alan Cox59846792008-05-19 14:09:00 +0100603MODULE_DESCRIPTION("Driver for SMsC 37B787 watchdog component (Version "
604 VERSION ")");
Sven Anders485ae772006-08-24 17:11:50 +0200605MODULE_LICENSE("GPL");
606
607MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
608
609#ifdef SMSC_SUPPORT_MINUTES
610module_param(unit, int, 0);
Alan Cox59846792008-05-19 14:09:00 +0100611MODULE_PARM_DESC(unit,
612 "set unit to use, 0=seconds or 1=minutes, default is 0");
Sven Anders485ae772006-08-24 17:11:50 +0200613#endif
614
Wim Van Sebroeckaa1fd4d2006-09-02 20:53:19 +0200615module_param(timeout, int, 0);
Sven Anders485ae772006-08-24 17:11:50 +0200616MODULE_PARM_DESC(timeout, "range is 1-255 units, default is 60");
617
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +0100618module_param(nowayout, bool, 0);
Alan Cox59846792008-05-19 14:09:00 +0100619MODULE_PARM_DESC(nowayout,
620 "Watchdog cannot be stopped once started (default="
621 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");