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 * Writing the magic 'V' sequence allows the next close to turn 126 * off the watchdog. 127 */ 128 129 static ssize_t watchdog_write(struct file *file, const char __user *data, 130 size_t len, loff_t *ppos) 131 { 132 size_t i; 133 char c; 134 135 if (len == 0) 136 return 0; 137 138 /* 139 * Note: just in case someone wrote the magic character 140 * five months ago... 141 */ 142 clear_bit(WDOG_ALLOW_RELEASE, &wdd->status); 143 144 /* scan to see whether or not we got the magic character */ 145 for (i = 0; i != len; i++) { 146 if (get_user(c, data + i)) 147 return -EFAULT; 148 if (c == 'V') 149 set_bit(WDOG_ALLOW_RELEASE, &wdd->status); 150 } 151 152 /* someone wrote to us, so we send the watchdog a keepalive ping */ 153 watchdog_ping(wdd); 154 155 return len; 156 } 157 158 /* 159 * watchdog_ioctl: handle the different ioctl's for the watchdog device. 160 * @file: file handle to the device 161 * @cmd: watchdog command 162 * @arg: argument pointer 163 * 164 * The watchdog API defines a common set of functions for all watchdogs 165 * according to their available features. 166 */ 167 168 static long watchdog_ioctl(struct file *file, unsigned int cmd, 169 unsigned long arg) 170 { 171 void __user *argp = (void __user *)arg; 172 int __user *p = argp; 173 unsigned int val; 174 int err; 175 176 switch (cmd) { 177 case WDIOC_GETSUPPORT: 178 return copy_to_user(argp, wdd->info, 179 sizeof(struct watchdog_info)) ? -EFAULT : 0; 180 case WDIOC_GETSTATUS: 181 val = wdd->ops->status ? wdd->ops->status(wdd) : 0; 182 return put_user(val, p); 183 case WDIOC_GETBOOTSTATUS: 184 return put_user(wdd->bootstatus, p); 185 case WDIOC_SETOPTIONS: 186 if (get_user(val, p)) 187 return -EFAULT; 188 if (val & WDIOS_DISABLECARD) { 189 err = watchdog_stop(wdd); 190 if (err < 0) 191 return err; 192 } 193 if (val & WDIOS_ENABLECARD) { 194 err = watchdog_start(wdd); 195 if (err < 0) 196 return err; 197 } 198 return 0; 199 case WDIOC_KEEPALIVE: 200 if (!(wdd->info->options & WDIOF_KEEPALIVEPING)) 201 return -EOPNOTSUPP; 202 watchdog_ping(wdd); 203 return 0; 204 case WDIOC_SETTIMEOUT: 205 if ((wdd->ops->set_timeout == NULL) || 206 !(wdd->info->options & WDIOF_SETTIMEOUT)) 207 return -EOPNOTSUPP; 208 if (get_user(val, p)) 209 return -EFAULT; 210 err = wdd->ops->set_timeout(wdd, val); 211 if (err < 0) 212 return err; 213 wdd->timeout = val; 214 /* If the watchdog is active then we send a keepalive ping 215 * to make sure that the watchdog keep's running (and if 216 * possible that it takes the new timeout) */ 217 watchdog_ping(wdd); 218 /* Fall */ 219 case WDIOC_GETTIMEOUT: 220 /* timeout == 0 means that we don't know the timeout */ 221 if (wdd->timeout == 0) 222 return -EOPNOTSUPP; 223 return put_user(wdd->timeout, p); 224 default: 225 return -ENOTTY; 226 } 227 } 228 229 /* 230 * watchdog_open: open the /dev/watchdog device. 231 * @inode: inode of device 232 * @file: file handle to device 233 * 234 * When the /dev/watchdog device gets opened, we start the watchdog. 235 * Watch out: the /dev/watchdog device is single open, so we make sure 236 * it can only be opened once. 237 */ 238 239 static int watchdog_open(struct inode *inode, struct file *file) 240 { 241 int err = -EBUSY; 242 243 /* the watchdog is single open! */ 244 if (test_and_set_bit(WDOG_DEV_OPEN, &wdd->status)) 245 return -EBUSY; 246 247 /* 248 * If the /dev/watchdog device is open, we don't want the module 249 * to be unloaded. 250 */ 251 if (!try_module_get(wdd->ops->owner)) 252 goto out; 253 254 err = watchdog_start(wdd); 255 if (err < 0) 256 goto out_mod; 257 258 /* dev/watchdog is a virtual (and thus non-seekable) filesystem */ 259 return nonseekable_open(inode, file); 260 261 out_mod: 262 module_put(wdd->ops->owner); 263 out: 264 clear_bit(WDOG_DEV_OPEN, &wdd->status); 265 return err; 266 } 267 268 /* 269 * watchdog_release: release the /dev/watchdog device. 270 * @inode: inode of device 271 * @file: file handle to device 272 * 273 * This is the code for when /dev/watchdog gets closed. We will only 274 * stop the watchdog when we have received the magic char, else the 275 * watchdog will keep running. 276 */ 277 278 static int watchdog_release(struct inode *inode, struct file *file) 279 { 280 int err = -EBUSY; 281 282 /* 283 * We only stop the watchdog if we received the magic character 284 * or if WDIOF_MAGICCLOSE is not set 285 */ 286 if (test_and_clear_bit(WDOG_ALLOW_RELEASE, &wdd->status) || 287 !(wdd->info->options & WDIOF_MAGICCLOSE)) 288 err = watchdog_stop(wdd); 289 290 /* If the watchdog was not stopped, send a keepalive ping */ 291 if (err < 0) { 292 pr_crit("%s: watchdog did not stop!\n", wdd->info->identity); 293 watchdog_ping(wdd); 294 } 295 296 /* Allow the owner module to be unloaded again */ 297 module_put(wdd->ops->owner); 298 299 /* make sure that /dev/watchdog can be re-opened */ 300 clear_bit(WDOG_DEV_OPEN, &wdd->status); 301 302 return 0; 303 } 304 305 static const struct file_operations watchdog_fops = { 306 .owner = THIS_MODULE, 307 .write = watchdog_write, 308 .unlocked_ioctl = watchdog_ioctl, 309 .open = watchdog_open, 310 .release = watchdog_release, 311 }; 312 313 static struct miscdevice watchdog_miscdev = { 314 .minor = WATCHDOG_MINOR, 315 .name = "watchdog", 316 .fops = &watchdog_fops, 317 }; 318 319 /* 320 * watchdog_dev_register: 321 * @watchdog: watchdog device 322 * 323 * Register a watchdog device as /dev/watchdog. /dev/watchdog 324 * is actually a miscdevice and thus we set it up like that. 325 */ 326 327 int watchdog_dev_register(struct watchdog_device *watchdog) 328 { 329 int err; 330 331 /* Only one device can register for /dev/watchdog */ 332 if (test_and_set_bit(0, &watchdog_dev_busy)) { 333 pr_err("only one watchdog can use /dev/watchdog.\n"); 334 return -EBUSY; 335 } 336 337 wdd = watchdog; 338 339 err = misc_register(&watchdog_miscdev); 340 if (err != 0) { 341 pr_err("%s: cannot register miscdev on minor=%d (err=%d).\n", 342 watchdog->info->identity, WATCHDOG_MINOR, err); 343 goto out; 344 } 345 346 return 0; 347 348 out: 349 wdd = NULL; 350 clear_bit(0, &watchdog_dev_busy); 351 return err; 352 } 353 354 /* 355 * watchdog_dev_unregister: 356 * @watchdog: watchdog device 357 * 358 * Deregister the /dev/watchdog device. 359 */ 360 361 int watchdog_dev_unregister(struct watchdog_device *watchdog) 362 { 363 /* Check that a watchdog device was registered in the past */ 364 if (!test_bit(0, &watchdog_dev_busy) || !wdd) 365 return -ENODEV; 366 367 /* We can only unregister the watchdog device that was registered */ 368 if (watchdog != wdd) { 369 pr_err("%s: watchdog was not registered as /dev/watchdog.\n", 370 watchdog->info->identity); 371 return -ENODEV; 372 } 373 374 misc_deregister(&watchdog_miscdev); 375 wdd = NULL; 376 clear_bit(0, &watchdog_dev_busy); 377 return 0; 378 } 379