blob: 2fb4cecd50d884168d958bbd6f1c1114befeaeaf [file] [log] [blame]
Wim Van Sebroeck43316042011-07-22 18:55:18 +00001/*
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 */
46static unsigned long watchdog_dev_busy;
47/* the watchdog device behind /dev/watchdog */
48static 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.
57 */
58
59static int watchdog_ping(struct watchdog_device *wddev)
60{
61 if (wddev->ops->ping)
62 return wddev->ops->ping(wddev); /* ping the watchdog */
63 else
64 return wddev->ops->start(wddev); /* restart the watchdog */
65}
66
67/*
68 * watchdog_write: writes to the watchdog.
69 * @file: file from VFS
70 * @data: user address of data
71 * @len: length of data
72 * @ppos: pointer to the file offset
73 *
74 * A write to a watchdog device is defined as a keepalive ping.
75 */
76
77static ssize_t watchdog_write(struct file *file, const char __user *data,
78 size_t len, loff_t *ppos)
79{
80 size_t i;
81 char c;
82
83 if (len == 0)
84 return 0;
85
86 for (i = 0; i != len; i++) {
87 if (get_user(c, data + i))
88 return -EFAULT;
89 }
90
91 /* someone wrote to us, so we send the watchdog a keepalive ping */
92 watchdog_ping(wdd);
93
94 return len;
95}
96
97/*
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +000098 * watchdog_ioctl: handle the different ioctl's for the watchdog device.
99 * @file: file handle to the device
100 * @cmd: watchdog command
101 * @arg: argument pointer
102 *
103 * The watchdog API defines a common set of functions for all watchdogs
104 * according to their available features.
105 */
106
107static long watchdog_ioctl(struct file *file, unsigned int cmd,
108 unsigned long arg)
109{
110 void __user *argp = (void __user *)arg;
111 int __user *p = argp;
112 unsigned int val;
113
114 switch (cmd) {
115 case WDIOC_GETSUPPORT:
116 return copy_to_user(argp, wdd->info,
117 sizeof(struct watchdog_info)) ? -EFAULT : 0;
118 case WDIOC_GETSTATUS:
119 val = wdd->ops->status ? wdd->ops->status(wdd) : 0;
120 return put_user(val, p);
121 case WDIOC_GETBOOTSTATUS:
122 return put_user(wdd->bootstatus, p);
Wim Van Sebroeckc2dc00e2011-07-22 18:57:23 +0000123 case WDIOC_KEEPALIVE:
124 if (!(wdd->info->options & WDIOF_KEEPALIVEPING))
125 return -EOPNOTSUPP;
126 watchdog_ping(wdd);
127 return 0;
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +0000128 default:
129 return -ENOTTY;
130 }
131}
132
133/*
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000134 * watchdog_open: open the /dev/watchdog device.
135 * @inode: inode of device
136 * @file: file handle to device
137 *
138 * When the /dev/watchdog device gets opened, we start the watchdog.
139 * Watch out: the /dev/watchdog device is single open, so we make sure
140 * it can only be opened once.
141 */
142
143static int watchdog_open(struct inode *inode, struct file *file)
144{
145 int err = -EBUSY;
146
147 /* the watchdog is single open! */
148 if (test_and_set_bit(WDOG_DEV_OPEN, &wdd->status))
149 return -EBUSY;
150
151 /*
152 * If the /dev/watchdog device is open, we don't want the module
153 * to be unloaded.
154 */
155 if (!try_module_get(wdd->ops->owner))
156 goto out;
157
158 err = wdd->ops->start(wdd);
159 if (err < 0)
160 goto out_mod;
161
162 /* dev/watchdog is a virtual (and thus non-seekable) filesystem */
163 return nonseekable_open(inode, file);
164
165out_mod:
166 module_put(wdd->ops->owner);
167out:
168 clear_bit(WDOG_DEV_OPEN, &wdd->status);
169 return err;
170}
171
172/*
173 * watchdog_release: release the /dev/watchdog device.
174 * @inode: inode of device
175 * @file: file handle to device
176 *
177 * This is the code for when /dev/watchdog gets closed.
178 */
179
180static int watchdog_release(struct inode *inode, struct file *file)
181{
182 int err;
183
184 err = wdd->ops->stop(wdd);
185 if (err != 0) {
186 pr_crit("%s: watchdog did not stop!\n", wdd->info->identity);
187 watchdog_ping(wdd);
188 }
189
190 /* Allow the owner module to be unloaded again */
191 module_put(wdd->ops->owner);
192
193 /* make sure that /dev/watchdog can be re-opened */
194 clear_bit(WDOG_DEV_OPEN, &wdd->status);
195
196 return 0;
197}
198
199static const struct file_operations watchdog_fops = {
200 .owner = THIS_MODULE,
201 .write = watchdog_write,
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +0000202 .unlocked_ioctl = watchdog_ioctl,
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000203 .open = watchdog_open,
204 .release = watchdog_release,
205};
206
207static struct miscdevice watchdog_miscdev = {
208 .minor = WATCHDOG_MINOR,
209 .name = "watchdog",
210 .fops = &watchdog_fops,
211};
212
213/*
214 * watchdog_dev_register:
215 * @watchdog: watchdog device
216 *
217 * Register a watchdog device as /dev/watchdog. /dev/watchdog
218 * is actually a miscdevice and thus we set it up like that.
219 */
220
221int watchdog_dev_register(struct watchdog_device *watchdog)
222{
223 int err;
224
225 /* Only one device can register for /dev/watchdog */
226 if (test_and_set_bit(0, &watchdog_dev_busy)) {
227 pr_err("only one watchdog can use /dev/watchdog.\n");
228 return -EBUSY;
229 }
230
231 wdd = watchdog;
232
233 err = misc_register(&watchdog_miscdev);
234 if (err != 0) {
235 pr_err("%s: cannot register miscdev on minor=%d (err=%d).\n",
236 watchdog->info->identity, WATCHDOG_MINOR, err);
237 goto out;
238 }
239
240 return 0;
241
242out:
243 wdd = NULL;
244 clear_bit(0, &watchdog_dev_busy);
245 return err;
246}
247
248/*
249 * watchdog_dev_unregister:
250 * @watchdog: watchdog device
251 *
252 * Deregister the /dev/watchdog device.
253 */
254
255int watchdog_dev_unregister(struct watchdog_device *watchdog)
256{
257 /* Check that a watchdog device was registered in the past */
258 if (!test_bit(0, &watchdog_dev_busy) || !wdd)
259 return -ENODEV;
260
261 /* We can only unregister the watchdog device that was registered */
262 if (watchdog != wdd) {
263 pr_err("%s: watchdog was not registered as /dev/watchdog.\n",
264 watchdog->info->identity);
265 return -ENODEV;
266 }
267
268 misc_deregister(&watchdog_miscdev);
269 wdd = NULL;
270 clear_bit(0, &watchdog_dev_busy);
271 return 0;
272}