[WATCHDOG] at32ap700x_wdt.c - Add nowayout + MAGICCLOSE features

Add nowayout + MAGICCLOSE features.

Signed-off-by: Hans-Christian Egtvedt <hcegtvedt@atmel.com>
Cc: Haavard Skinnemoen <hskinnemoen@atmel.com>
Signed-off-by: Wim Van Sebroeck <wim@iguana.be>
Cc: Andrew Morton <akpm@linux-foundation.org>

diff --git a/drivers/char/watchdog/at32ap700x_wdt.c b/drivers/char/watchdog/at32ap700x_wdt.c
index 6e7c958..fcd55c6 100644
--- a/drivers/char/watchdog/at32ap700x_wdt.c
+++ b/drivers/char/watchdog/at32ap700x_wdt.c
@@ -30,6 +30,11 @@
 		"Timeout value. Limited to be 1 or 2 seconds. (default="
 		__MODULE_STRING(TIMEOUT_DEFAULT) ")");
 
+static int nowayout = WATCHDOG_NOWAYOUT;
+module_param(nowayout, int, 0);
+MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
+		__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
+
 /* Watchdog registers and write/read macro */
 #define WDT_CTRL		0x00
 #define WDT_CTRL_EN		   0
@@ -54,6 +59,7 @@
 };
 
 static struct wdt_at32ap700x *wdt;
+static char expect_release;
 
 /*
  * Disable the watchdog.
@@ -102,15 +108,19 @@
 }
 
 /*
- * Close the watchdog device. If CONFIG_WATCHDOG_NOWAYOUT is _not_ defined then
- * the watchdog is also disabled.
+ * Close the watchdog device.
  */
 static int at32_wdt_close(struct inode *inode, struct file *file)
 {
-#ifndef CONFIG_WATCHDOG_NOWAYOUT
-	at32_wdt_stop();
-#endif
+	if (expect_release == 42) {
+		at32_wdt_stop();
+	} else {
+		dev_dbg(wdt->miscdev.parent,
+			"Unexpected close, not stopping watchdog!\n");
+		at32_wdt_pat();
+	}
 	clear_bit(1, &wdt->users);
+	expect_release = 0;
 	return 0;
 }
 
@@ -136,7 +146,9 @@
 
 static struct watchdog_info at32_wdt_info = {
 	.identity	= "at32ap700x watchdog",
-	.options	= WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
+	.options	= WDIOF_SETTIMEOUT |
+			  WDIOF_KEEPALIVEPING |
+			  WDIOF_MAGICCLOSE,
 };
 
 /*
@@ -191,10 +203,35 @@
 	return ret;
 }
 
-static ssize_t at32_wdt_write(struct file *file, const char *data, size_t len,
-				loff_t *ppos)
+static ssize_t at32_wdt_write(struct file *file, const char __user *data,
+				size_t len, loff_t *ppos)
 {
-	at32_wdt_pat();
+	/* See if we got the magic character 'V' and reload the timer */
+	if (len) {
+		if (!nowayout) {
+			size_t i;
+
+			/*
+			 * note: just in case someone wrote the magic
+			 * character five months ago...
+			 */
+			expect_release = 0;
+
+			/*
+			 * scan to see whether or not we got the magic
+			 * character
+			 */
+			for (i = 0; i != len; i++) {
+				char c;
+				if (get_user(c, data+i))
+					return -EFAULT;
+				if (c == 'V')
+					expect_release = 42;
+			}
+		}
+		/* someone wrote to us, we should pat the watchdog */
+		at32_wdt_pat();
+	}
 	return len;
 }
 
@@ -255,8 +292,9 @@
 
 	platform_set_drvdata(pdev, wdt);
 	wdt->miscdev.parent = &pdev->dev;
-	dev_info(&pdev->dev, "AT32AP700X WDT at 0x%p, timeout %d sec\n",
-		wdt->regs, wdt->timeout);
+	dev_info(&pdev->dev,
+		"AT32AP700X WDT at 0x%p, timeout %d sec (nowayout=%d)\n",
+		wdt->regs, wdt->timeout, nowayout);
 
 	return 0;
 
@@ -271,6 +309,10 @@
 static int __exit at32_wdt_remove(struct platform_device *pdev)
 {
 	if (wdt && platform_get_drvdata(pdev) == wdt) {
+		/* Stop the timer before we leave */
+		if (!nowayout)
+			at32_wdt_stop();
+
 		misc_deregister(&wdt->miscdev);
 		iounmap(wdt->regs);
 		kfree(wdt);