Commit | Line | Data |
---|---|---|
43316044 WVS |
1 | /* |
2 | * watchdog_dev.c | |
3 | * | |
4 | * (c) Copyright 2008-2011 Alan Cox <alan@lxorguk.ukuu.org.uk>, | |
5 | * All Rights Reserved. | |
6 | * | |
7 | * (c) Copyright 2008-2011 Wim Van Sebroeck <wim@iguana.be>. | |
8 | * | |
9 | * | |
10 | * This source code is part of the generic code that can be used | |
11 | * by all the watchdog timer drivers. | |
12 | * | |
13 | * This part of the generic code takes care of the following | |
14 | * misc device: /dev/watchdog. | |
15 | * | |
16 | * Based on source code of the following authors: | |
17 | * Matt Domsch <Matt_Domsch@dell.com>, | |
18 | * Rob Radez <rob@osinvestor.com>, | |
19 | * Rusty Lynch <rusty@linux.co.intel.com> | |
20 | * Satyam Sharma <satyam@infradead.org> | |
21 | * Randy Dunlap <randy.dunlap@oracle.com> | |
22 | * | |
23 | * This program is free software; you can redistribute it and/or | |
24 | * modify it under the terms of the GNU General Public License | |
25 | * as published by the Free Software Foundation; either version | |
26 | * 2 of the License, or (at your option) any later version. | |
27 | * | |
28 | * Neither Alan Cox, CymruNet Ltd., Wim Van Sebroeck nor Iguana vzw. | |
29 | * admit liability nor provide warranty for any of this software. | |
30 | * This material is provided "AS-IS" and at no charge. | |
31 | */ | |
32 | ||
33 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
34 | ||
35 | #include <linux/module.h> /* For module stuff/... */ | |
36 | #include <linux/types.h> /* For standard types (like size_t) */ | |
37 | #include <linux/errno.h> /* For the -ENODEV/... values */ | |
38 | #include <linux/kernel.h> /* For printk/panic/... */ | |
39 | #include <linux/fs.h> /* For file operations */ | |
40 | #include <linux/watchdog.h> /* For watchdog specific items */ | |
41 | #include <linux/miscdevice.h> /* For handling misc devices */ | |
42 | #include <linux/init.h> /* For __init/__exit/... */ | |
43 | #include <linux/uaccess.h> /* For copy_to_user/put_user/... */ | |
44 | ||
45 | /* make sure we only register one /dev/watchdog device */ | |
46 | static unsigned long watchdog_dev_busy; | |
47 | /* the watchdog device behind /dev/watchdog */ | |
48 | static struct watchdog_device *wdd; | |
49 | ||
50 | /* | |
51 | * watchdog_ping: ping the watchdog. | |
52 | * @wddev: the watchdog device to ping | |
53 | * | |
54 | * If the watchdog has no own ping operation then it needs to be | |
55 | * restarted via the start operation. This wrapper function does | |
56 | * exactly that. | |
234445b4 | 57 | * We only ping when the watchdog device is running. |
43316044 WVS |
58 | */ |
59 | ||
60 | static int watchdog_ping(struct watchdog_device *wddev) | |
61 | { | |
234445b4 WVS |
62 | if (test_bit(WDOG_ACTIVE, &wdd->status)) { |
63 | if (wddev->ops->ping) | |
64 | return wddev->ops->ping(wddev); /* ping the watchdog */ | |
65 | else | |
66 | return wddev->ops->start(wddev); /* restart watchdog */ | |
67 | } | |
68 | return 0; | |
69 | } | |
70 | ||
71 | /* | |
72 | * watchdog_start: wrapper to start the watchdog. | |
73 | * @wddev: the watchdog device to start | |
74 | * | |
75 | * Start the watchdog if it is not active and mark it active. | |
76 | * This function returns zero on success or a negative errno code for | |
77 | * failure. | |
78 | */ | |
79 | ||
80 | static int watchdog_start(struct watchdog_device *wddev) | |
81 | { | |
82 | int err; | |
83 | ||
84 | if (!test_bit(WDOG_ACTIVE, &wdd->status)) { | |
85 | err = wddev->ops->start(wddev); | |
86 | if (err < 0) | |
87 | return err; | |
88 | ||
89 | set_bit(WDOG_ACTIVE, &wdd->status); | |
90 | } | |
91 | return 0; | |
92 | } | |
93 | ||
94 | /* | |
95 | * watchdog_stop: wrapper to stop the watchdog. | |
96 | * @wddev: the watchdog device to stop | |
97 | * | |
98 | * Stop the watchdog if it is still active and unmark it active. | |
99 | * This function returns zero on success or a negative errno code for | |
100 | * failure. | |
101 | */ | |
102 | ||
103 | static int watchdog_stop(struct watchdog_device *wddev) | |
104 | { | |
105 | int err; | |
106 | ||
107 | if (test_bit(WDOG_ACTIVE, &wdd->status)) { | |
108 | err = wddev->ops->stop(wddev); | |
109 | if (err < 0) | |
110 | return err; | |
111 | ||
112 | clear_bit(WDOG_ACTIVE, &wdd->status); | |
113 | } | |
114 | return 0; | |
43316044 WVS |
115 | } |
116 | ||
117 | /* | |
118 | * watchdog_write: writes to the watchdog. | |
119 | * @file: file from VFS | |
120 | * @data: user address of data | |
121 | * @len: length of data | |
122 | * @ppos: pointer to the file offset | |
123 | * | |
124 | * A write to a watchdog device is defined as a keepalive ping. | |
125 | */ | |
126 | ||
127 | static ssize_t watchdog_write(struct file *file, const char __user *data, | |
128 | size_t len, loff_t *ppos) | |
129 | { | |
130 | size_t i; | |
131 | char c; | |
132 | ||
133 | if (len == 0) | |
134 | return 0; | |
135 | ||
136 | for (i = 0; i != len; i++) { | |
137 | if (get_user(c, data + i)) | |
138 | return -EFAULT; | |
139 | } | |
140 | ||
141 | /* someone wrote to us, so we send the watchdog a keepalive ping */ | |
142 | watchdog_ping(wdd); | |
143 | ||
144 | return len; | |
145 | } | |
146 | ||
2fa03560 WVS |
147 | /* |
148 | * watchdog_ioctl: handle the different ioctl's for the watchdog device. | |
149 | * @file: file handle to the device | |
150 | * @cmd: watchdog command | |
151 | * @arg: argument pointer | |
152 | * | |
153 | * The watchdog API defines a common set of functions for all watchdogs | |
154 | * according to their available features. | |
155 | */ | |
156 | ||
157 | static long watchdog_ioctl(struct file *file, unsigned int cmd, | |
158 | unsigned long arg) | |
159 | { | |
160 | void __user *argp = (void __user *)arg; | |
161 | int __user *p = argp; | |
162 | unsigned int val; | |
234445b4 | 163 | int err; |
2fa03560 WVS |
164 | |
165 | switch (cmd) { | |
166 | case WDIOC_GETSUPPORT: | |
167 | return copy_to_user(argp, wdd->info, | |
168 | sizeof(struct watchdog_info)) ? -EFAULT : 0; | |
169 | case WDIOC_GETSTATUS: | |
170 | val = wdd->ops->status ? wdd->ops->status(wdd) : 0; | |
171 | return put_user(val, p); | |
172 | case WDIOC_GETBOOTSTATUS: | |
173 | return put_user(wdd->bootstatus, p); | |
234445b4 WVS |
174 | case WDIOC_SETOPTIONS: |
175 | if (get_user(val, p)) | |
176 | return -EFAULT; | |
177 | if (val & WDIOS_DISABLECARD) { | |
178 | err = watchdog_stop(wdd); | |
179 | if (err < 0) | |
180 | return err; | |
181 | } | |
182 | if (val & WDIOS_ENABLECARD) { | |
183 | err = watchdog_start(wdd); | |
184 | if (err < 0) | |
185 | return err; | |
186 | } | |
187 | return 0; | |
c2dc00e4 WVS |
188 | case WDIOC_KEEPALIVE: |
189 | if (!(wdd->info->options & WDIOF_KEEPALIVEPING)) | |
190 | return -EOPNOTSUPP; | |
191 | watchdog_ping(wdd); | |
192 | return 0; | |
014d694e WVS |
193 | case WDIOC_SETTIMEOUT: |
194 | if ((wdd->ops->set_timeout == NULL) || | |
195 | !(wdd->info->options & WDIOF_SETTIMEOUT)) | |
196 | return -EOPNOTSUPP; | |
197 | if (get_user(val, p)) | |
198 | return -EFAULT; | |
199 | err = wdd->ops->set_timeout(wdd, val); | |
200 | if (err < 0) | |
201 | return err; | |
202 | wdd->timeout = val; | |
203 | /* If the watchdog is active then we send a keepalive ping | |
204 | * to make sure that the watchdog keep's running (and if | |
205 | * possible that it takes the new timeout) */ | |
206 | watchdog_ping(wdd); | |
207 | /* Fall */ | |
208 | case WDIOC_GETTIMEOUT: | |
209 | /* timeout == 0 means that we don't know the timeout */ | |
210 | if (wdd->timeout == 0) | |
211 | return -EOPNOTSUPP; | |
212 | return put_user(wdd->timeout, p); | |
2fa03560 WVS |
213 | default: |
214 | return -ENOTTY; | |
215 | } | |
216 | } | |
217 | ||
43316044 WVS |
218 | /* |
219 | * watchdog_open: open the /dev/watchdog device. | |
220 | * @inode: inode of device | |
221 | * @file: file handle to device | |
222 | * | |
223 | * When the /dev/watchdog device gets opened, we start the watchdog. | |
224 | * Watch out: the /dev/watchdog device is single open, so we make sure | |
225 | * it can only be opened once. | |
226 | */ | |
227 | ||
228 | static int watchdog_open(struct inode *inode, struct file *file) | |
229 | { | |
230 | int err = -EBUSY; | |
231 | ||
232 | /* the watchdog is single open! */ | |
233 | if (test_and_set_bit(WDOG_DEV_OPEN, &wdd->status)) | |
234 | return -EBUSY; | |
235 | ||
236 | /* | |
237 | * If the /dev/watchdog device is open, we don't want the module | |
238 | * to be unloaded. | |
239 | */ | |
240 | if (!try_module_get(wdd->ops->owner)) | |
241 | goto out; | |
242 | ||
234445b4 | 243 | err = watchdog_start(wdd); |
43316044 WVS |
244 | if (err < 0) |
245 | goto out_mod; | |
246 | ||
247 | /* dev/watchdog is a virtual (and thus non-seekable) filesystem */ | |
248 | return nonseekable_open(inode, file); | |
249 | ||
250 | out_mod: | |
251 | module_put(wdd->ops->owner); | |
252 | out: | |
253 | clear_bit(WDOG_DEV_OPEN, &wdd->status); | |
254 | return err; | |
255 | } | |
256 | ||
257 | /* | |
258 | * watchdog_release: release the /dev/watchdog device. | |
259 | * @inode: inode of device | |
260 | * @file: file handle to device | |
261 | * | |
262 | * This is the code for when /dev/watchdog gets closed. | |
263 | */ | |
264 | ||
265 | static int watchdog_release(struct inode *inode, struct file *file) | |
266 | { | |
267 | int err; | |
268 | ||
234445b4 WVS |
269 | err = watchdog_stop(wdd); |
270 | if (err < 0) { | |
43316044 WVS |
271 | pr_crit("%s: watchdog did not stop!\n", wdd->info->identity); |
272 | watchdog_ping(wdd); | |
273 | } | |
274 | ||
275 | /* Allow the owner module to be unloaded again */ | |
276 | module_put(wdd->ops->owner); | |
277 | ||
278 | /* make sure that /dev/watchdog can be re-opened */ | |
279 | clear_bit(WDOG_DEV_OPEN, &wdd->status); | |
280 | ||
281 | return 0; | |
282 | } | |
283 | ||
284 | static const struct file_operations watchdog_fops = { | |
285 | .owner = THIS_MODULE, | |
286 | .write = watchdog_write, | |
2fa03560 | 287 | .unlocked_ioctl = watchdog_ioctl, |
43316044 WVS |
288 | .open = watchdog_open, |
289 | .release = watchdog_release, | |
290 | }; | |
291 | ||
292 | static struct miscdevice watchdog_miscdev = { | |
293 | .minor = WATCHDOG_MINOR, | |
294 | .name = "watchdog", | |
295 | .fops = &watchdog_fops, | |
296 | }; | |
297 | ||
298 | /* | |
299 | * watchdog_dev_register: | |
300 | * @watchdog: watchdog device | |
301 | * | |
302 | * Register a watchdog device as /dev/watchdog. /dev/watchdog | |
303 | * is actually a miscdevice and thus we set it up like that. | |
304 | */ | |
305 | ||
306 | int watchdog_dev_register(struct watchdog_device *watchdog) | |
307 | { | |
308 | int err; | |
309 | ||
310 | /* Only one device can register for /dev/watchdog */ | |
311 | if (test_and_set_bit(0, &watchdog_dev_busy)) { | |
312 | pr_err("only one watchdog can use /dev/watchdog.\n"); | |
313 | return -EBUSY; | |
314 | } | |
315 | ||
316 | wdd = watchdog; | |
317 | ||
318 | err = misc_register(&watchdog_miscdev); | |
319 | if (err != 0) { | |
320 | pr_err("%s: cannot register miscdev on minor=%d (err=%d).\n", | |
321 | watchdog->info->identity, WATCHDOG_MINOR, err); | |
322 | goto out; | |
323 | } | |
324 | ||
325 | return 0; | |
326 | ||
327 | out: | |
328 | wdd = NULL; | |
329 | clear_bit(0, &watchdog_dev_busy); | |
330 | return err; | |
331 | } | |
332 | ||
333 | /* | |
334 | * watchdog_dev_unregister: | |
335 | * @watchdog: watchdog device | |
336 | * | |
337 | * Deregister the /dev/watchdog device. | |
338 | */ | |
339 | ||
340 | int watchdog_dev_unregister(struct watchdog_device *watchdog) | |
341 | { | |
342 | /* Check that a watchdog device was registered in the past */ | |
343 | if (!test_bit(0, &watchdog_dev_busy) || !wdd) | |
344 | return -ENODEV; | |
345 | ||
346 | /* We can only unregister the watchdog device that was registered */ | |
347 | if (watchdog != wdd) { | |
348 | pr_err("%s: watchdog was not registered as /dev/watchdog.\n", | |
349 | watchdog->info->identity); | |
350 | return -ENODEV; | |
351 | } | |
352 | ||
353 | misc_deregister(&watchdog_miscdev); | |
354 | wdd = NULL; | |
355 | clear_bit(0, &watchdog_dev_busy); | |
356 | return 0; | |
357 | } |