1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Advantech Single Board Computer WDT driver
4 *
5 * (c) Copyright 2000-2001 Marek Michalkiewicz <marekm@linux.org.pl>
6 *
7 * Based on acquirewdt.c which is based on wdt.c.
8 * Original copyright messages:
9 *
10 * (c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>,
11 * All Rights Reserved.
12 *
13 * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
14 * warranty for any of this software. This material is provided
15 * "AS-IS" and at no charge.
16 *
17 * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk>
18 *
19 * 14-Dec-2001 Matt Domsch <Matt_Domsch@dell.com>
20 * Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT
21 *
22 * 16-Oct-2002 Rob Radez <rob@osinvestor.com>
23 * Clean up ioctls, clean up init + exit, add expect close support,
24 * add wdt_start and wdt_stop as parameters.
25 */
26
27 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28
29 #include <linux/module.h>
30 #include <linux/moduleparam.h>
31 #include <linux/types.h>
32 #include <linux/miscdevice.h>
33 #include <linux/watchdog.h>
34 #include <linux/fs.h>
35 #include <linux/ioport.h>
36 #include <linux/platform_device.h>
37 #include <linux/init.h>
38 #include <linux/io.h>
39 #include <linux/uaccess.h>
40
41
42 #define DRV_NAME "advantechwdt"
43 #define WATCHDOG_NAME "Advantech WDT"
44 #define WATCHDOG_TIMEOUT 60 /* 60 sec default timeout */
45
46 /* the watchdog platform device */
47 static struct platform_device *advwdt_platform_device;
48 static unsigned long advwdt_is_open;
49 static char adv_expect_close;
50
51 /*
52 * You must set these - there is no sane way to probe for this board.
53 *
54 * To enable or restart, write the timeout value in seconds (1 to 63)
55 * to I/O port wdt_start. To disable, read I/O port wdt_stop.
56 * Both are 0x443 for most boards (tested on a PCA-6276VE-00B1), but
57 * check your manual (at least the PCA-6159 seems to be different -
58 * the manual says wdt_stop is 0x43, not 0x443).
59 * (0x43 is also a write-only control register for the 8254 timer!)
60 */
61
62 static int wdt_stop = 0x443;
63 module_param(wdt_stop, int, 0);
64 MODULE_PARM_DESC(wdt_stop, "Advantech WDT 'stop' io port (default 0x443)");
65
66 static int wdt_start = 0x443;
67 module_param(wdt_start, int, 0);
68 MODULE_PARM_DESC(wdt_start, "Advantech WDT 'start' io port (default 0x443)");
69
70 static int timeout = WATCHDOG_TIMEOUT; /* in seconds */
71 module_param(timeout, int, 0);
72 MODULE_PARM_DESC(timeout,
73 "Watchdog timeout in seconds. 1<= timeout <=63, default="
74 __MODULE_STRING(WATCHDOG_TIMEOUT) ".");
75
76 static bool nowayout = WATCHDOG_NOWAYOUT;
77 module_param(nowayout, bool, 0);
78 MODULE_PARM_DESC(nowayout,
79 "Watchdog cannot be stopped once started (default="
80 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
81
82 /*
83 * Watchdog Operations
84 */
85
advwdt_ping(void)86 static void advwdt_ping(void)
87 {
88 /* Write a watchdog value */
89 outb_p(timeout, wdt_start);
90 }
91
advwdt_disable(void)92 static void advwdt_disable(void)
93 {
94 inb_p(wdt_stop);
95 }
96
advwdt_set_heartbeat(int t)97 static int advwdt_set_heartbeat(int t)
98 {
99 if (t < 1 || t > 63)
100 return -EINVAL;
101 timeout = t;
102 return 0;
103 }
104
105 /*
106 * /dev/watchdog handling
107 */
108
advwdt_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)109 static ssize_t advwdt_write(struct file *file, const char __user *buf,
110 size_t count, loff_t *ppos)
111 {
112 if (count) {
113 if (!nowayout) {
114 size_t i;
115
116 adv_expect_close = 0;
117
118 for (i = 0; i != count; i++) {
119 char c;
120 if (get_user(c, buf + i))
121 return -EFAULT;
122 if (c == 'V')
123 adv_expect_close = 42;
124 }
125 }
126 advwdt_ping();
127 }
128 return count;
129 }
130
advwdt_ioctl(struct file * file,unsigned int cmd,unsigned long arg)131 static long advwdt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
132 {
133 int new_timeout;
134 void __user *argp = (void __user *)arg;
135 int __user *p = argp;
136 static const struct watchdog_info ident = {
137 .options = WDIOF_KEEPALIVEPING |
138 WDIOF_SETTIMEOUT |
139 WDIOF_MAGICCLOSE,
140 .firmware_version = 1,
141 .identity = WATCHDOG_NAME,
142 };
143
144 switch (cmd) {
145 case WDIOC_GETSUPPORT:
146 if (copy_to_user(argp, &ident, sizeof(ident)))
147 return -EFAULT;
148 break;
149
150 case WDIOC_GETSTATUS:
151 case WDIOC_GETBOOTSTATUS:
152 return put_user(0, p);
153
154 case WDIOC_SETOPTIONS:
155 {
156 int options, retval = -EINVAL;
157
158 if (get_user(options, p))
159 return -EFAULT;
160 if (options & WDIOS_DISABLECARD) {
161 advwdt_disable();
162 retval = 0;
163 }
164 if (options & WDIOS_ENABLECARD) {
165 advwdt_ping();
166 retval = 0;
167 }
168 return retval;
169 }
170 case WDIOC_KEEPALIVE:
171 advwdt_ping();
172 break;
173
174 case WDIOC_SETTIMEOUT:
175 if (get_user(new_timeout, p))
176 return -EFAULT;
177 if (advwdt_set_heartbeat(new_timeout))
178 return -EINVAL;
179 advwdt_ping();
180 fallthrough;
181 case WDIOC_GETTIMEOUT:
182 return put_user(timeout, p);
183 default:
184 return -ENOTTY;
185 }
186 return 0;
187 }
188
advwdt_open(struct inode * inode,struct file * file)189 static int advwdt_open(struct inode *inode, struct file *file)
190 {
191 if (test_and_set_bit(0, &advwdt_is_open))
192 return -EBUSY;
193 /*
194 * Activate
195 */
196
197 advwdt_ping();
198 return stream_open(inode, file);
199 }
200
advwdt_close(struct inode * inode,struct file * file)201 static int advwdt_close(struct inode *inode, struct file *file)
202 {
203 if (adv_expect_close == 42) {
204 advwdt_disable();
205 } else {
206 pr_crit("Unexpected close, not stopping watchdog!\n");
207 advwdt_ping();
208 }
209 clear_bit(0, &advwdt_is_open);
210 adv_expect_close = 0;
211 return 0;
212 }
213
214 /*
215 * Kernel Interfaces
216 */
217
218 static const struct file_operations advwdt_fops = {
219 .owner = THIS_MODULE,
220 .write = advwdt_write,
221 .unlocked_ioctl = advwdt_ioctl,
222 .compat_ioctl = compat_ptr_ioctl,
223 .open = advwdt_open,
224 .release = advwdt_close,
225 };
226
227 static struct miscdevice advwdt_miscdev = {
228 .minor = WATCHDOG_MINOR,
229 .name = "watchdog",
230 .fops = &advwdt_fops,
231 };
232
233 /*
234 * Init & exit routines
235 */
236
advwdt_probe(struct platform_device * dev)237 static int __init advwdt_probe(struct platform_device *dev)
238 {
239 int ret;
240
241 if (wdt_stop != wdt_start) {
242 if (!request_region(wdt_stop, 1, WATCHDOG_NAME)) {
243 pr_err("I/O address 0x%04x already in use\n",
244 wdt_stop);
245 ret = -EIO;
246 goto out;
247 }
248 }
249
250 if (!request_region(wdt_start, 1, WATCHDOG_NAME)) {
251 pr_err("I/O address 0x%04x already in use\n", wdt_start);
252 ret = -EIO;
253 goto unreg_stop;
254 }
255
256 /* Check that the heartbeat value is within it's range ;
257 * if not reset to the default */
258 if (advwdt_set_heartbeat(timeout)) {
259 advwdt_set_heartbeat(WATCHDOG_TIMEOUT);
260 pr_info("timeout value must be 1<=x<=63, using %d\n", timeout);
261 }
262
263 ret = misc_register(&advwdt_miscdev);
264 if (ret != 0) {
265 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
266 WATCHDOG_MINOR, ret);
267 goto unreg_regions;
268 }
269 pr_info("initialized. timeout=%d sec (nowayout=%d)\n",
270 timeout, nowayout);
271 out:
272 return ret;
273 unreg_regions:
274 release_region(wdt_start, 1);
275 unreg_stop:
276 if (wdt_stop != wdt_start)
277 release_region(wdt_stop, 1);
278 goto out;
279 }
280
advwdt_remove(struct platform_device * dev)281 static void advwdt_remove(struct platform_device *dev)
282 {
283 misc_deregister(&advwdt_miscdev);
284 release_region(wdt_start, 1);
285 if (wdt_stop != wdt_start)
286 release_region(wdt_stop, 1);
287 }
288
advwdt_shutdown(struct platform_device * dev)289 static void advwdt_shutdown(struct platform_device *dev)
290 {
291 /* Turn the WDT off if we have a soft shutdown */
292 advwdt_disable();
293 }
294
295 static struct platform_driver advwdt_driver = {
296 .remove_new = advwdt_remove,
297 .shutdown = advwdt_shutdown,
298 .driver = {
299 .name = DRV_NAME,
300 },
301 };
302
advwdt_init(void)303 static int __init advwdt_init(void)
304 {
305 int err;
306
307 pr_info("WDT driver for Advantech single board computer initialising\n");
308
309 advwdt_platform_device = platform_device_register_simple(DRV_NAME,
310 -1, NULL, 0);
311 if (IS_ERR(advwdt_platform_device))
312 return PTR_ERR(advwdt_platform_device);
313
314 err = platform_driver_probe(&advwdt_driver, advwdt_probe);
315 if (err)
316 goto unreg_platform_device;
317
318 return 0;
319
320 unreg_platform_device:
321 platform_device_unregister(advwdt_platform_device);
322 return err;
323 }
324
advwdt_exit(void)325 static void __exit advwdt_exit(void)
326 {
327 platform_device_unregister(advwdt_platform_device);
328 platform_driver_unregister(&advwdt_driver);
329 pr_info("Watchdog Module Unloaded\n");
330 }
331
332 module_init(advwdt_init);
333 module_exit(advwdt_exit);
334
335 MODULE_LICENSE("GPL");
336 MODULE_AUTHOR("Marek Michalkiewicz <marekm@linux.org.pl>");
337 MODULE_DESCRIPTION("Advantech Single Board Computer WDT driver");
338