This source file includes following definitions.
- wdt_timer_ping
- wdt_change
- wdt_startup
- wdt_turnoff
- wdt_keepalive
- fop_write
- fop_open
- fop_close
- fop_ioctl
- wdt_notify_sys
- w83877f_wdt_unload
- w83877f_wdt_init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
42
43 #include <linux/module.h>
44 #include <linux/moduleparam.h>
45 #include <linux/types.h>
46 #include <linux/timer.h>
47 #include <linux/jiffies.h>
48 #include <linux/miscdevice.h>
49 #include <linux/watchdog.h>
50 #include <linux/fs.h>
51 #include <linux/ioport.h>
52 #include <linux/notifier.h>
53 #include <linux/reboot.h>
54 #include <linux/init.h>
55 #include <linux/io.h>
56 #include <linux/uaccess.h>
57
58 #define OUR_NAME "w83877f_wdt"
59
60 #define ENABLE_W83877F_PORT 0x3F0
61 #define ENABLE_W83877F 0x87
62 #define DISABLE_W83877F 0xAA
63 #define WDT_PING 0x443
64 #define WDT_REGISTER 0x14
65 #define WDT_ENABLE 0x9C
66 #define WDT_DISABLE 0x8C
67
68
69
70
71
72
73 #define WDT_INTERVAL (HZ/4+1)
74
75
76
77
78
79
80
81 #define WATCHDOG_TIMEOUT 30
82
83 static int timeout = WATCHDOG_TIMEOUT;
84 module_param(timeout, int, 0);
85 MODULE_PARM_DESC(timeout,
86 "Watchdog timeout in seconds. (1<=timeout<=3600, default="
87 __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
88
89
90 static bool nowayout = WATCHDOG_NOWAYOUT;
91 module_param(nowayout, bool, 0);
92 MODULE_PARM_DESC(nowayout,
93 "Watchdog cannot be stopped once started (default="
94 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
95
96 static void wdt_timer_ping(struct timer_list *);
97 static DEFINE_TIMER(timer, wdt_timer_ping);
98 static unsigned long next_heartbeat;
99 static unsigned long wdt_is_open;
100 static char wdt_expect_close;
101 static DEFINE_SPINLOCK(wdt_spinlock);
102
103
104
105
106
107 static void wdt_timer_ping(struct timer_list *unused)
108 {
109
110
111
112 if (time_before(jiffies, next_heartbeat)) {
113
114 spin_lock(&wdt_spinlock);
115
116
117 inb_p(WDT_PING);
118
119
120 mod_timer(&timer, jiffies + WDT_INTERVAL);
121
122 spin_unlock(&wdt_spinlock);
123
124 } else
125 pr_warn("Heartbeat lost! Will not ping the watchdog\n");
126 }
127
128
129
130
131
132 static void wdt_change(int writeval)
133 {
134 unsigned long flags;
135 spin_lock_irqsave(&wdt_spinlock, flags);
136
137
138 inb_p(WDT_PING);
139
140
141 outb_p(ENABLE_W83877F, ENABLE_W83877F_PORT);
142 outb_p(ENABLE_W83877F, ENABLE_W83877F_PORT);
143
144
145 outb_p(WDT_REGISTER, ENABLE_W83877F_PORT);
146 outb_p(writeval, ENABLE_W83877F_PORT+1);
147
148
149 outb_p(DISABLE_W83877F, ENABLE_W83877F_PORT);
150
151 spin_unlock_irqrestore(&wdt_spinlock, flags);
152 }
153
154 static void wdt_startup(void)
155 {
156 next_heartbeat = jiffies + (timeout * HZ);
157
158
159 mod_timer(&timer, jiffies + WDT_INTERVAL);
160
161 wdt_change(WDT_ENABLE);
162
163 pr_info("Watchdog timer is now enabled\n");
164 }
165
166 static void wdt_turnoff(void)
167 {
168
169 del_timer(&timer);
170
171 wdt_change(WDT_DISABLE);
172
173 pr_info("Watchdog timer is now disabled...\n");
174 }
175
176 static void wdt_keepalive(void)
177 {
178
179 next_heartbeat = jiffies + (timeout * HZ);
180 }
181
182
183
184
185
186 static ssize_t fop_write(struct file *file, const char __user *buf,
187 size_t count, loff_t *ppos)
188 {
189
190 if (count) {
191 if (!nowayout) {
192 size_t ofs;
193
194
195
196 wdt_expect_close = 0;
197
198
199
200 for (ofs = 0; ofs != count; ofs++) {
201 char c;
202 if (get_user(c, buf + ofs))
203 return -EFAULT;
204 if (c == 'V')
205 wdt_expect_close = 42;
206 }
207 }
208
209
210 wdt_keepalive();
211 }
212 return count;
213 }
214
215 static int fop_open(struct inode *inode, struct file *file)
216 {
217
218 if (test_and_set_bit(0, &wdt_is_open))
219 return -EBUSY;
220
221
222 wdt_startup();
223 return stream_open(inode, file);
224 }
225
226 static int fop_close(struct inode *inode, struct file *file)
227 {
228 if (wdt_expect_close == 42)
229 wdt_turnoff();
230 else {
231 del_timer(&timer);
232 pr_crit("device file closed unexpectedly. Will not stop the WDT!\n");
233 }
234 clear_bit(0, &wdt_is_open);
235 wdt_expect_close = 0;
236 return 0;
237 }
238
239 static long fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
240 {
241 void __user *argp = (void __user *)arg;
242 int __user *p = argp;
243 static const struct watchdog_info ident = {
244 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT
245 | WDIOF_MAGICCLOSE,
246 .firmware_version = 1,
247 .identity = "W83877F",
248 };
249
250 switch (cmd) {
251 case WDIOC_GETSUPPORT:
252 return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
253 case WDIOC_GETSTATUS:
254 case WDIOC_GETBOOTSTATUS:
255 return put_user(0, p);
256 case WDIOC_SETOPTIONS:
257 {
258 int new_options, retval = -EINVAL;
259
260 if (get_user(new_options, p))
261 return -EFAULT;
262
263 if (new_options & WDIOS_DISABLECARD) {
264 wdt_turnoff();
265 retval = 0;
266 }
267
268 if (new_options & WDIOS_ENABLECARD) {
269 wdt_startup();
270 retval = 0;
271 }
272
273 return retval;
274 }
275 case WDIOC_KEEPALIVE:
276 wdt_keepalive();
277 return 0;
278 case WDIOC_SETTIMEOUT:
279 {
280 int new_timeout;
281
282 if (get_user(new_timeout, p))
283 return -EFAULT;
284
285
286 if (new_timeout < 1 || new_timeout > 3600)
287 return -EINVAL;
288
289 timeout = new_timeout;
290 wdt_keepalive();
291 }
292
293 case WDIOC_GETTIMEOUT:
294 return put_user(timeout, p);
295 default:
296 return -ENOTTY;
297 }
298 }
299
300 static const struct file_operations wdt_fops = {
301 .owner = THIS_MODULE,
302 .llseek = no_llseek,
303 .write = fop_write,
304 .open = fop_open,
305 .release = fop_close,
306 .unlocked_ioctl = fop_ioctl,
307 };
308
309 static struct miscdevice wdt_miscdev = {
310 .minor = WATCHDOG_MINOR,
311 .name = "watchdog",
312 .fops = &wdt_fops,
313 };
314
315
316
317
318
319 static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
320 void *unused)
321 {
322 if (code == SYS_DOWN || code == SYS_HALT)
323 wdt_turnoff();
324 return NOTIFY_DONE;
325 }
326
327
328
329
330
331
332 static struct notifier_block wdt_notifier = {
333 .notifier_call = wdt_notify_sys,
334 };
335
336 static void __exit w83877f_wdt_unload(void)
337 {
338 wdt_turnoff();
339
340
341 misc_deregister(&wdt_miscdev);
342
343 unregister_reboot_notifier(&wdt_notifier);
344 release_region(WDT_PING, 1);
345 release_region(ENABLE_W83877F_PORT, 2);
346 }
347
348 static int __init w83877f_wdt_init(void)
349 {
350 int rc = -EBUSY;
351
352 if (timeout < 1 || timeout > 3600) {
353 timeout = WATCHDOG_TIMEOUT;
354 pr_info("timeout value must be 1 <= x <= 3600, using %d\n",
355 timeout);
356 }
357
358 if (!request_region(ENABLE_W83877F_PORT, 2, "W83877F WDT")) {
359 pr_err("I/O address 0x%04x already in use\n",
360 ENABLE_W83877F_PORT);
361 rc = -EIO;
362 goto err_out;
363 }
364
365 if (!request_region(WDT_PING, 1, "W8387FF WDT")) {
366 pr_err("I/O address 0x%04x already in use\n", WDT_PING);
367 rc = -EIO;
368 goto err_out_region1;
369 }
370
371 rc = register_reboot_notifier(&wdt_notifier);
372 if (rc) {
373 pr_err("cannot register reboot notifier (err=%d)\n", rc);
374 goto err_out_region2;
375 }
376
377 rc = misc_register(&wdt_miscdev);
378 if (rc) {
379 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
380 wdt_miscdev.minor, rc);
381 goto err_out_reboot;
382 }
383
384 pr_info("WDT driver for W83877F initialised. timeout=%d sec (nowayout=%d)\n",
385 timeout, nowayout);
386
387 return 0;
388
389 err_out_reboot:
390 unregister_reboot_notifier(&wdt_notifier);
391 err_out_region2:
392 release_region(WDT_PING, 1);
393 err_out_region1:
394 release_region(ENABLE_W83877F_PORT, 2);
395 err_out:
396 return rc;
397 }
398
399 module_init(w83877f_wdt_init);
400 module_exit(w83877f_wdt_unload);
401
402 MODULE_AUTHOR("Scott and Bill Jennings");
403 MODULE_DESCRIPTION("Driver for watchdog timer in w83877f chip");
404 MODULE_LICENSE("GPL");