#include <linux/notifier.h>
#include <linux/reboot.h>
#include <linux/init.h>
+#include <linux/spinlock.h>
#include <asm/io.h>
#include <asm/uaccess.h>
#define MODNAME "smsc37b787_wdt: "
#define VERSION "1.1"
-#define WATCHDOG_MINOR 130
-
#define IOPORT 0x3F0
#define IOPORT_SIZE 2
#define IODEV_NO 8
static int unit = UNIT_SECOND; /* timer's unit */
static int timeout = 60; /* timeout value: default is 60 "units" */
-static int timer_enabled = 0; /* is the timer enabled? */
+static unsigned long timer_enabled = 0; /* is the timer enabled? */
static char expect_close; /* is the close expected? */
+static spinlock_t io_lock; /* to guard the watchdog from io races */
+
static int nowayout = WATCHDOG_NOWAYOUT;
/* -- Low level function ----------------------------------------*/
{
unsigned char old;
+ spin_lock(&io_lock);
open_io_config();
select_io_device(IODEV_NO);
wdt_timer_units(old);
close_io_config();
+ spin_unlock(&io_lock);
}
/* shutdown the watchdog */
static void wb_smsc_wdt_shutdown(void)
{
+ spin_lock(&io_lock);
open_io_config();
select_io_device(IODEV_NO);
wdt_timeout_value(0x00);
close_io_config();
+ spin_unlock(&io_lock);
}
/* set timeout => enable watchdog */
static void wb_smsc_wdt_set_timeout(unsigned char new_timeout)
{
+ spin_lock(&io_lock);
open_io_config();
select_io_device(IODEV_NO);
wdt_timeout_value(new_timeout);
close_io_config();
+ spin_unlock(&io_lock);
}
/* get timeout */
{
unsigned char set_timeout;
+ spin_lock(&io_lock);
open_io_config();
select_io_device(IODEV_NO);
set_timeout = read_io_cr(0xF2);
close_io_config();
+ spin_unlock(&io_lock);
return set_timeout;
}
static void wb_smsc_wdt_reset_timer(void)
{
+ spin_lock(&io_lock);
open_io_config();
select_io_device(IODEV_NO);
wdt_timer_conf(0x08);
close_io_config();
+ spin_unlock(&io_lock);
}
/* return, if the watchdog is enabled (timeout is set...) */
{
/* /dev/watchdog can only be opened once */
- if (timer_enabled)
+ if (test_and_set_bit(0, &timer_enabled))
return -EBUSY;
if (nowayout)
__module_get(THIS_MODULE);
/* Reload and activate timer */
- timer_enabled = 1;
wb_smsc_wdt_enable();
printk(KERN_INFO MODNAME "Watchdog enabled. Timeout set to %d %s.\n", timeout, (unit == UNIT_SECOND) ? "second(s)" : "minute(s)");
wb_smsc_wdt_reset_timer();
}
- timer_enabled = 0;
+ clear_bit(0, &timer_enabled);
expect_close = 0;
return 0;
}
return -ENOTTY;
case WDIOC_GETSUPPORT:
- return copy_to_user(uarg.ident, &ident, sizeof(ident));
+ return copy_to_user(uarg.ident, &ident,
+ sizeof(ident)) ? -EFAULT : 0;
case WDIOC_GETSTATUS:
return put_user(wb_smsc_wdt_status(), uarg.i);
.write = wb_smsc_wdt_write,
.ioctl = wb_smsc_wdt_ioctl,
.open = wb_smsc_wdt_open,
- .release = wb_smsc_wdt_release
+ .release = wb_smsc_wdt_release,
};
static struct notifier_block wb_smsc_wdt_notifier =
{
- .notifier_call = wb_smsc_wdt_notify_sys
+ .notifier_call = wb_smsc_wdt_notify_sys,
};
static struct miscdevice wb_smsc_wdt_miscdev =
{
int ret;
+ spin_lock_init(&io_lock);
+
printk("SMsC 37B787 watchdog component driver " VERSION " initialising...\n");
if (!request_region(IOPORT, IOPORT_SIZE, "SMsC 37B787 watchdog")) {
goto out_pnp;
}
+ // set new maximum, if it's too big
+ if (timeout > MAX_TIMEOUT)
+ timeout = MAX_TIMEOUT;
+
+ // init the watchdog timer
+ wb_smsc_wdt_initialize();
+
ret = register_reboot_notifier(&wb_smsc_wdt_notifier);
if (ret) {
printk(KERN_ERR MODNAME "Unable to register reboot notifier err = %d\n", ret);
goto out_rbt;
}
- // init the watchdog timer
- wb_smsc_wdt_initialize();
-
- // set new maximum, if it's too big
- if (timeout > MAX_TIMEOUT)
- timeout = MAX_TIMEOUT;
-
// output info
printk(KERN_INFO MODNAME "Timeout set to %d %s.\n", timeout, (unit == UNIT_SECOND) ? "second(s)" : "minute(s)");
printk(KERN_INFO MODNAME "Watchdog initialized and sleeping (nowayout=%d)...\n", nowayout);
MODULE_PARM_DESC(unit, "set unit to use, 0=seconds or 1=minutes, default is 0");
#endif
-module_param(timeout, int, 60);
+module_param(timeout, int, 0);
MODULE_PARM_DESC(timeout, "range is 1-255 units, default is 60");
module_param(nowayout, int, 0);