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.
57  *	We only ping when the watchdog device is running.
58  */
59 
60 static int watchdog_ping(struct watchdog_device *wddev)
61 {
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;
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 
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;
163 	int err;
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);
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;
188 	case WDIOC_KEEPALIVE:
189 		if (!(wdd->info->options & WDIOF_KEEPALIVEPING))
190 			return -EOPNOTSUPP;
191 		watchdog_ping(wdd);
192 		return 0;
193 	default:
194 		return -ENOTTY;
195 	}
196 }
197 
198 /*
199  *	watchdog_open: open the /dev/watchdog device.
200  *	@inode: inode of device
201  *	@file: file handle to device
202  *
203  *	When the /dev/watchdog device gets opened, we start the watchdog.
204  *	Watch out: the /dev/watchdog device is single open, so we make sure
205  *	it can only be opened once.
206  */
207 
208 static int watchdog_open(struct inode *inode, struct file *file)
209 {
210 	int err = -EBUSY;
211 
212 	/* the watchdog is single open! */
213 	if (test_and_set_bit(WDOG_DEV_OPEN, &wdd->status))
214 		return -EBUSY;
215 
216 	/*
217 	 * If the /dev/watchdog device is open, we don't want the module
218 	 * to be unloaded.
219 	 */
220 	if (!try_module_get(wdd->ops->owner))
221 		goto out;
222 
223 	err = watchdog_start(wdd);
224 	if (err < 0)
225 		goto out_mod;
226 
227 	/* dev/watchdog is a virtual (and thus non-seekable) filesystem */
228 	return nonseekable_open(inode, file);
229 
230 out_mod:
231 	module_put(wdd->ops->owner);
232 out:
233 	clear_bit(WDOG_DEV_OPEN, &wdd->status);
234 	return err;
235 }
236 
237 /*
238  *      watchdog_release: release the /dev/watchdog device.
239  *      @inode: inode of device
240  *      @file: file handle to device
241  *
242  *	This is the code for when /dev/watchdog gets closed.
243  */
244 
245 static int watchdog_release(struct inode *inode, struct file *file)
246 {
247 	int err;
248 
249 	err = watchdog_stop(wdd);
250 	if (err < 0) {
251 		pr_crit("%s: watchdog did not stop!\n", wdd->info->identity);
252 		watchdog_ping(wdd);
253 	}
254 
255 	/* Allow the owner module to be unloaded again */
256 	module_put(wdd->ops->owner);
257 
258 	/* make sure that /dev/watchdog can be re-opened */
259 	clear_bit(WDOG_DEV_OPEN, &wdd->status);
260 
261 	return 0;
262 }
263 
264 static const struct file_operations watchdog_fops = {
265 	.owner		= THIS_MODULE,
266 	.write		= watchdog_write,
267 	.unlocked_ioctl	= watchdog_ioctl,
268 	.open		= watchdog_open,
269 	.release	= watchdog_release,
270 };
271 
272 static struct miscdevice watchdog_miscdev = {
273 	.minor		= WATCHDOG_MINOR,
274 	.name		= "watchdog",
275 	.fops		= &watchdog_fops,
276 };
277 
278 /*
279  *	watchdog_dev_register:
280  *	@watchdog: watchdog device
281  *
282  *	Register a watchdog device as /dev/watchdog. /dev/watchdog
283  *	is actually a miscdevice and thus we set it up like that.
284  */
285 
286 int watchdog_dev_register(struct watchdog_device *watchdog)
287 {
288 	int err;
289 
290 	/* Only one device can register for /dev/watchdog */
291 	if (test_and_set_bit(0, &watchdog_dev_busy)) {
292 		pr_err("only one watchdog can use /dev/watchdog.\n");
293 		return -EBUSY;
294 	}
295 
296 	wdd = watchdog;
297 
298 	err = misc_register(&watchdog_miscdev);
299 	if (err != 0) {
300 		pr_err("%s: cannot register miscdev on minor=%d (err=%d).\n",
301 			watchdog->info->identity, WATCHDOG_MINOR, err);
302 		goto out;
303 	}
304 
305 	return 0;
306 
307 out:
308 	wdd = NULL;
309 	clear_bit(0, &watchdog_dev_busy);
310 	return err;
311 }
312 
313 /*
314  *	watchdog_dev_unregister:
315  *	@watchdog: watchdog device
316  *
317  *	Deregister the /dev/watchdog device.
318  */
319 
320 int watchdog_dev_unregister(struct watchdog_device *watchdog)
321 {
322 	/* Check that a watchdog device was registered in the past */
323 	if (!test_bit(0, &watchdog_dev_busy) || !wdd)
324 		return -ENODEV;
325 
326 	/* We can only unregister the watchdog device that was registered */
327 	if (watchdog != wdd) {
328 		pr_err("%s: watchdog was not registered as /dev/watchdog.\n",
329 			watchdog->info->identity);
330 		return -ENODEV;
331 	}
332 
333 	misc_deregister(&watchdog_miscdev);
334 	wdd = NULL;
335 	clear_bit(0, &watchdog_dev_busy);
336 	return 0;
337 }
338