Merge master.kernel.org:/pub/scm/linux/kernel/git/wim/linux-2.6-watchdog
[pandora-kernel.git] / drivers / char / watchdog / smsc37b787_wdt.c
index 47141c0..d3cb0a7 100644 (file)
@@ -8,7 +8,7 @@
  *     modify it under the terms of the GNU General Public License
  *     as published by the Free Software Foundation; either version
  *     2 of the License, or (at your option) any later version.
- *     
+ *
  *     The authors do NOT admit liability nor provide warranty for
  *     any of this software. This material is provided "AS-IS" in
  *      the hope that it may be useful for others.
@@ -54,6 +54,7 @@
 #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 ----------------------------------------*/
@@ -210,6 +211,7 @@ static void wb_smsc_wdt_initialize(void)
 {
         unsigned char old;
 
+       spin_lock(&io_lock);
         open_io_config();
         select_io_device(IODEV_NO);
 
@@ -234,12 +236,14 @@ static void wb_smsc_wdt_initialize(void)
         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);
 
@@ -257,12 +261,14 @@ static void wb_smsc_wdt_shutdown(void)
         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);
 
@@ -273,6 +279,7 @@ static void wb_smsc_wdt_set_timeout(unsigned char new_timeout)
         wdt_timeout_value(new_timeout);
 
         close_io_config();
+       spin_unlock(&io_lock);
 }
 
 /* get timeout */
@@ -281,10 +288,12 @@ static unsigned char wb_smsc_wdt_get_timeout(void)
 {
         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;
 }
@@ -309,6 +318,7 @@ static void wb_smsc_wdt_enable(void)
 
 static void wb_smsc_wdt_reset_timer(void)
 {
+       spin_lock(&io_lock);
         open_io_config();
         select_io_device(IODEV_NO);
 
@@ -317,6 +327,7 @@ static void wb_smsc_wdt_reset_timer(void)
        wdt_timer_conf(0x08);
 
         close_io_config();
+       spin_unlock(&io_lock);
 }
 
 /* return, if the watchdog is enabled (timeout is set...) */
@@ -335,14 +346,13 @@ static int wb_smsc_wdt_open(struct inode *inode, struct file *file)
 {
        /* /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)");
@@ -364,7 +374,7 @@ static int wb_smsc_wdt_release(struct inode *inode, struct file *file)
                wb_smsc_wdt_reset_timer();
        }
 
-       timer_enabled = 0;
+       clear_bit(0, &timer_enabled);
        expect_close = 0;
        return 0;
 }
@@ -422,10 +432,11 @@ static int wb_smsc_wdt_ioctl(struct inode *inode, struct file *file,
 
        switch (cmd) {
                default:
-                       return -ENOTTY; 
+                       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);
@@ -499,19 +510,19 @@ static int wb_smsc_wdt_notify_sys(struct notifier_block *this, unsigned long cod
 
 /* -- Module's structures ---------------------------------------*/
 
-static struct file_operations wb_smsc_wdt_fops =
+static const struct file_operations wb_smsc_wdt_fops =
 {
        .owner          = THIS_MODULE,
        .llseek         = no_llseek,
        .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 =
@@ -529,6 +540,8 @@ static int __init wb_smsc_wdt_init(void)
 {
        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")) {
@@ -537,6 +550,13 @@ static int __init wb_smsc_wdt_init(void)
                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);
@@ -549,13 +569,6 @@ static int __init wb_smsc_wdt_init(void)
                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);
@@ -573,7 +586,7 @@ out_io:
 
 out_pnp:
        goto out_clean;
-}      
+}
 
 /* module's "destructor" */
 
@@ -607,8 +620,8 @@ module_param(unit, int, 0);
 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);
-MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
+MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");