watchdog: correct the name of the watchdog_core inlude file
[firefly-linux-kernel-4.4.55.git] / drivers / watchdog / watchdog_dev.c
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 #include "watchdog_core.h"
46
47 /* make sure we only register one /dev/watchdog device */
48 static unsigned long watchdog_dev_busy;
49 /* the watchdog device behind /dev/watchdog */
50 static struct watchdog_device *wdd;
51
52 /*
53  *      watchdog_ping: ping the watchdog.
54  *      @wddev: the watchdog device to ping
55  *
56  *      If the watchdog has no own ping operation then it needs to be
57  *      restarted via the start operation. This wrapper function does
58  *      exactly that.
59  *      We only ping when the watchdog device is running.
60  */
61
62 static int watchdog_ping(struct watchdog_device *wddev)
63 {
64         if (watchdog_active(wddev)) {
65                 if (wddev->ops->ping)
66                         return wddev->ops->ping(wddev);  /* ping the watchdog */
67                 else
68                         return wddev->ops->start(wddev); /* restart watchdog */
69         }
70         return 0;
71 }
72
73 /*
74  *      watchdog_start: wrapper to start the watchdog.
75  *      @wddev: the watchdog device to start
76  *
77  *      Start the watchdog if it is not active and mark it active.
78  *      This function returns zero on success or a negative errno code for
79  *      failure.
80  */
81
82 static int watchdog_start(struct watchdog_device *wddev)
83 {
84         int err;
85
86         if (!watchdog_active(wddev)) {
87                 err = wddev->ops->start(wddev);
88                 if (err < 0)
89                         return err;
90
91                 set_bit(WDOG_ACTIVE, &wddev->status);
92         }
93         return 0;
94 }
95
96 /*
97  *      watchdog_stop: wrapper to stop the watchdog.
98  *      @wddev: the watchdog device to stop
99  *
100  *      Stop the watchdog if it is still active and unmark it active.
101  *      This function returns zero on success or a negative errno code for
102  *      failure.
103  *      If the 'nowayout' feature was set, the watchdog cannot be stopped.
104  */
105
106 static int watchdog_stop(struct watchdog_device *wddev)
107 {
108         int err = -EBUSY;
109
110         if (test_bit(WDOG_NO_WAY_OUT, &wddev->status)) {
111                 pr_info("%s: nowayout prevents watchdog to be stopped!\n",
112                                                         wddev->info->identity);
113                 return err;
114         }
115
116         if (watchdog_active(wddev)) {
117                 err = wddev->ops->stop(wddev);
118                 if (err < 0)
119                         return err;
120
121                 clear_bit(WDOG_ACTIVE, &wddev->status);
122         }
123         return 0;
124 }
125
126 /*
127  *      watchdog_write: writes to the watchdog.
128  *      @file: file from VFS
129  *      @data: user address of data
130  *      @len: length of data
131  *      @ppos: pointer to the file offset
132  *
133  *      A write to a watchdog device is defined as a keepalive ping.
134  *      Writing the magic 'V' sequence allows the next close to turn
135  *      off the watchdog (if 'nowayout' is not set).
136  */
137
138 static ssize_t watchdog_write(struct file *file, const char __user *data,
139                                                 size_t len, loff_t *ppos)
140 {
141         size_t i;
142         char c;
143
144         if (len == 0)
145                 return 0;
146
147         /*
148          * Note: just in case someone wrote the magic character
149          * five months ago...
150          */
151         clear_bit(WDOG_ALLOW_RELEASE, &wdd->status);
152
153         /* scan to see whether or not we got the magic character */
154         for (i = 0; i != len; i++) {
155                 if (get_user(c, data + i))
156                         return -EFAULT;
157                 if (c == 'V')
158                         set_bit(WDOG_ALLOW_RELEASE, &wdd->status);
159         }
160
161         /* someone wrote to us, so we send the watchdog a keepalive ping */
162         watchdog_ping(wdd);
163
164         return len;
165 }
166
167 /*
168  *      watchdog_ioctl: handle the different ioctl's for the watchdog device.
169  *      @file: file handle to the device
170  *      @cmd: watchdog command
171  *      @arg: argument pointer
172  *
173  *      The watchdog API defines a common set of functions for all watchdogs
174  *      according to their available features.
175  */
176
177 static long watchdog_ioctl(struct file *file, unsigned int cmd,
178                                                         unsigned long arg)
179 {
180         void __user *argp = (void __user *)arg;
181         int __user *p = argp;
182         unsigned int val;
183         int err;
184
185         if (wdd->ops->ioctl) {
186                 err = wdd->ops->ioctl(wdd, cmd, arg);
187                 if (err != -ENOIOCTLCMD)
188                         return err;
189         }
190
191         switch (cmd) {
192         case WDIOC_GETSUPPORT:
193                 return copy_to_user(argp, wdd->info,
194                         sizeof(struct watchdog_info)) ? -EFAULT : 0;
195         case WDIOC_GETSTATUS:
196                 val = wdd->ops->status ? wdd->ops->status(wdd) : 0;
197                 return put_user(val, p);
198         case WDIOC_GETBOOTSTATUS:
199                 return put_user(wdd->bootstatus, p);
200         case WDIOC_SETOPTIONS:
201                 if (get_user(val, p))
202                         return -EFAULT;
203                 if (val & WDIOS_DISABLECARD) {
204                         err = watchdog_stop(wdd);
205                         if (err < 0)
206                                 return err;
207                 }
208                 if (val & WDIOS_ENABLECARD) {
209                         err = watchdog_start(wdd);
210                         if (err < 0)
211                                 return err;
212                 }
213                 return 0;
214         case WDIOC_KEEPALIVE:
215                 if (!(wdd->info->options & WDIOF_KEEPALIVEPING))
216                         return -EOPNOTSUPP;
217                 watchdog_ping(wdd);
218                 return 0;
219         case WDIOC_SETTIMEOUT:
220                 if ((wdd->ops->set_timeout == NULL) ||
221                     !(wdd->info->options & WDIOF_SETTIMEOUT))
222                         return -EOPNOTSUPP;
223                 if (get_user(val, p))
224                         return -EFAULT;
225                 if ((wdd->max_timeout != 0) &&
226                     (val < wdd->min_timeout || val > wdd->max_timeout))
227                                 return -EINVAL;
228                 err = wdd->ops->set_timeout(wdd, val);
229                 if (err < 0)
230                         return err;
231                 /* If the watchdog is active then we send a keepalive ping
232                  * to make sure that the watchdog keep's running (and if
233                  * possible that it takes the new timeout) */
234                 watchdog_ping(wdd);
235                 /* Fall */
236         case WDIOC_GETTIMEOUT:
237                 /* timeout == 0 means that we don't know the timeout */
238                 if (wdd->timeout == 0)
239                         return -EOPNOTSUPP;
240                 return put_user(wdd->timeout, p);
241         case WDIOC_GETTIMELEFT:
242                 if (!wdd->ops->get_timeleft)
243                         return -EOPNOTSUPP;
244
245                 return put_user(wdd->ops->get_timeleft(wdd), p);
246         default:
247                 return -ENOTTY;
248         }
249 }
250
251 /*
252  *      watchdog_open: open the /dev/watchdog device.
253  *      @inode: inode of device
254  *      @file: file handle to device
255  *
256  *      When the /dev/watchdog device gets opened, we start the watchdog.
257  *      Watch out: the /dev/watchdog device is single open, so we make sure
258  *      it can only be opened once.
259  */
260
261 static int watchdog_open(struct inode *inode, struct file *file)
262 {
263         int err = -EBUSY;
264
265         /* the watchdog is single open! */
266         if (test_and_set_bit(WDOG_DEV_OPEN, &wdd->status))
267                 return -EBUSY;
268
269         /*
270          * If the /dev/watchdog device is open, we don't want the module
271          * to be unloaded.
272          */
273         if (!try_module_get(wdd->ops->owner))
274                 goto out;
275
276         err = watchdog_start(wdd);
277         if (err < 0)
278                 goto out_mod;
279
280         /* dev/watchdog is a virtual (and thus non-seekable) filesystem */
281         return nonseekable_open(inode, file);
282
283 out_mod:
284         module_put(wdd->ops->owner);
285 out:
286         clear_bit(WDOG_DEV_OPEN, &wdd->status);
287         return err;
288 }
289
290 /*
291  *      watchdog_release: release the /dev/watchdog device.
292  *      @inode: inode of device
293  *      @file: file handle to device
294  *
295  *      This is the code for when /dev/watchdog gets closed. We will only
296  *      stop the watchdog when we have received the magic char (and nowayout
297  *      was not set), else the watchdog will keep running.
298  */
299
300 static int watchdog_release(struct inode *inode, struct file *file)
301 {
302         int err = -EBUSY;
303
304         /*
305          * We only stop the watchdog if we received the magic character
306          * or if WDIOF_MAGICCLOSE is not set. If nowayout was set then
307          * watchdog_stop will fail.
308          */
309         if (test_and_clear_bit(WDOG_ALLOW_RELEASE, &wdd->status) ||
310             !(wdd->info->options & WDIOF_MAGICCLOSE))
311                 err = watchdog_stop(wdd);
312
313         /* If the watchdog was not stopped, send a keepalive ping */
314         if (err < 0) {
315                 pr_crit("%s: watchdog did not stop!\n", wdd->info->identity);
316                 watchdog_ping(wdd);
317         }
318
319         /* Allow the owner module to be unloaded again */
320         module_put(wdd->ops->owner);
321
322         /* make sure that /dev/watchdog can be re-opened */
323         clear_bit(WDOG_DEV_OPEN, &wdd->status);
324
325         return 0;
326 }
327
328 static const struct file_operations watchdog_fops = {
329         .owner          = THIS_MODULE,
330         .write          = watchdog_write,
331         .unlocked_ioctl = watchdog_ioctl,
332         .open           = watchdog_open,
333         .release        = watchdog_release,
334 };
335
336 static struct miscdevice watchdog_miscdev = {
337         .minor          = WATCHDOG_MINOR,
338         .name           = "watchdog",
339         .fops           = &watchdog_fops,
340 };
341
342 /*
343  *      watchdog_dev_register:
344  *      @watchdog: watchdog device
345  *
346  *      Register a watchdog device as /dev/watchdog. /dev/watchdog
347  *      is actually a miscdevice and thus we set it up like that.
348  */
349
350 int watchdog_dev_register(struct watchdog_device *watchdog)
351 {
352         int err;
353
354         /* Only one device can register for /dev/watchdog */
355         if (test_and_set_bit(0, &watchdog_dev_busy)) {
356                 pr_err("only one watchdog can use /dev/watchdog\n");
357                 return -EBUSY;
358         }
359
360         wdd = watchdog;
361
362         err = misc_register(&watchdog_miscdev);
363         if (err != 0) {
364                 pr_err("%s: cannot register miscdev on minor=%d (err=%d)\n",
365                        watchdog->info->identity, WATCHDOG_MINOR, err);
366                 goto out;
367         }
368
369         return 0;
370
371 out:
372         wdd = NULL;
373         clear_bit(0, &watchdog_dev_busy);
374         return err;
375 }
376
377 /*
378  *      watchdog_dev_unregister:
379  *      @watchdog: watchdog device
380  *
381  *      Deregister the /dev/watchdog device.
382  */
383
384 int watchdog_dev_unregister(struct watchdog_device *watchdog)
385 {
386         /* Check that a watchdog device was registered in the past */
387         if (!test_bit(0, &watchdog_dev_busy) || !wdd)
388                 return -ENODEV;
389
390         /* We can only unregister the watchdog device that was registered */
391         if (watchdog != wdd) {
392                 pr_err("%s: watchdog was not registered as /dev/watchdog\n",
393                        watchdog->info->identity);
394                 return -ENODEV;
395         }
396
397         misc_deregister(&watchdog_miscdev);
398         wdd = NULL;
399         clear_bit(0, &watchdog_dev_busy);
400         return 0;
401 }