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 /* the dev_t structure to store the dynamically allocated watchdog devices */ 48 static dev_t watchdog_devt; 49 /* the watchdog device behind /dev/watchdog */ 50 static struct watchdog_device *old_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 int err = 0; 65 66 mutex_lock(&wddev->lock); 67 68 if (!watchdog_active(wddev)) 69 goto out_ping; 70 71 if (wddev->ops->ping) 72 err = wddev->ops->ping(wddev); /* ping the watchdog */ 73 else 74 err = wddev->ops->start(wddev); /* restart watchdog */ 75 76 out_ping: 77 mutex_unlock(&wddev->lock); 78 return err; 79 } 80 81 /* 82 * watchdog_start: wrapper to start the watchdog. 83 * @wddev: the watchdog device to start 84 * 85 * Start the watchdog if it is not active and mark it active. 86 * This function returns zero on success or a negative errno code for 87 * failure. 88 */ 89 90 static int watchdog_start(struct watchdog_device *wddev) 91 { 92 int err = 0; 93 94 mutex_lock(&wddev->lock); 95 96 if (watchdog_active(wddev)) 97 goto out_start; 98 99 err = wddev->ops->start(wddev); 100 if (err == 0) 101 set_bit(WDOG_ACTIVE, &wddev->status); 102 103 out_start: 104 mutex_unlock(&wddev->lock); 105 return err; 106 } 107 108 /* 109 * watchdog_stop: wrapper to stop the watchdog. 110 * @wddev: the watchdog device to stop 111 * 112 * Stop the watchdog if it is still active and unmark it active. 113 * This function returns zero on success or a negative errno code for 114 * failure. 115 * If the 'nowayout' feature was set, the watchdog cannot be stopped. 116 */ 117 118 static int watchdog_stop(struct watchdog_device *wddev) 119 { 120 int err = 0; 121 122 mutex_lock(&wddev->lock); 123 124 if (!watchdog_active(wddev)) 125 goto out_stop; 126 127 if (test_bit(WDOG_NO_WAY_OUT, &wddev->status)) { 128 dev_info(wddev->dev, "nowayout prevents watchdog being stopped!\n"); 129 err = -EBUSY; 130 goto out_stop; 131 } 132 133 err = wddev->ops->stop(wddev); 134 if (err == 0) 135 clear_bit(WDOG_ACTIVE, &wddev->status); 136 137 out_stop: 138 mutex_unlock(&wddev->lock); 139 return err; 140 } 141 142 /* 143 * watchdog_get_status: wrapper to get the watchdog status 144 * @wddev: the watchdog device to get the status from 145 * @status: the status of the watchdog device 146 * 147 * Get the watchdog's status flags. 148 */ 149 150 static int watchdog_get_status(struct watchdog_device *wddev, 151 unsigned int *status) 152 { 153 int err = 0; 154 155 *status = 0; 156 if (!wddev->ops->status) 157 return -EOPNOTSUPP; 158 159 mutex_lock(&wddev->lock); 160 161 *status = wddev->ops->status(wddev); 162 163 mutex_unlock(&wddev->lock); 164 return err; 165 } 166 167 /* 168 * watchdog_set_timeout: set the watchdog timer timeout 169 * @wddev: the watchdog device to set the timeout for 170 * @timeout: timeout to set in seconds 171 */ 172 173 static int watchdog_set_timeout(struct watchdog_device *wddev, 174 unsigned int timeout) 175 { 176 int err; 177 178 if ((wddev->ops->set_timeout == NULL) || 179 !(wddev->info->options & WDIOF_SETTIMEOUT)) 180 return -EOPNOTSUPP; 181 182 if ((wddev->max_timeout != 0) && 183 (timeout < wddev->min_timeout || timeout > wddev->max_timeout)) 184 return -EINVAL; 185 186 mutex_lock(&wddev->lock); 187 188 err = wddev->ops->set_timeout(wddev, timeout); 189 190 mutex_unlock(&wddev->lock); 191 return err; 192 } 193 194 /* 195 * watchdog_get_timeleft: wrapper to get the time left before a reboot 196 * @wddev: the watchdog device to get the remaining time from 197 * @timeleft: the time that's left 198 * 199 * Get the time before a watchdog will reboot (if not pinged). 200 */ 201 202 static int watchdog_get_timeleft(struct watchdog_device *wddev, 203 unsigned int *timeleft) 204 { 205 int err = 0; 206 207 *timeleft = 0; 208 if (!wddev->ops->get_timeleft) 209 return -EOPNOTSUPP; 210 211 mutex_lock(&wddev->lock); 212 213 *timeleft = wddev->ops->get_timeleft(wddev); 214 215 mutex_unlock(&wddev->lock); 216 return err; 217 } 218 219 /* 220 * watchdog_ioctl_op: call the watchdog drivers ioctl op if defined 221 * @wddev: the watchdog device to do the ioctl on 222 * @cmd: watchdog command 223 * @arg: argument pointer 224 */ 225 226 static int watchdog_ioctl_op(struct watchdog_device *wddev, unsigned int cmd, 227 unsigned long arg) 228 { 229 int err; 230 231 if (!wddev->ops->ioctl) 232 return -ENOIOCTLCMD; 233 234 mutex_lock(&wddev->lock); 235 236 err = wddev->ops->ioctl(wddev, cmd, arg); 237 238 mutex_unlock(&wddev->lock); 239 return err; 240 } 241 242 /* 243 * watchdog_write: writes to the watchdog. 244 * @file: file from VFS 245 * @data: user address of data 246 * @len: length of data 247 * @ppos: pointer to the file offset 248 * 249 * A write to a watchdog device is defined as a keepalive ping. 250 * Writing the magic 'V' sequence allows the next close to turn 251 * off the watchdog (if 'nowayout' is not set). 252 */ 253 254 static ssize_t watchdog_write(struct file *file, const char __user *data, 255 size_t len, loff_t *ppos) 256 { 257 struct watchdog_device *wdd = file->private_data; 258 size_t i; 259 char c; 260 261 if (len == 0) 262 return 0; 263 264 /* 265 * Note: just in case someone wrote the magic character 266 * five months ago... 267 */ 268 clear_bit(WDOG_ALLOW_RELEASE, &wdd->status); 269 270 /* scan to see whether or not we got the magic character */ 271 for (i = 0; i != len; i++) { 272 if (get_user(c, data + i)) 273 return -EFAULT; 274 if (c == 'V') 275 set_bit(WDOG_ALLOW_RELEASE, &wdd->status); 276 } 277 278 /* someone wrote to us, so we send the watchdog a keepalive ping */ 279 watchdog_ping(wdd); 280 281 return len; 282 } 283 284 /* 285 * watchdog_ioctl: handle the different ioctl's for the watchdog device. 286 * @file: file handle to the device 287 * @cmd: watchdog command 288 * @arg: argument pointer 289 * 290 * The watchdog API defines a common set of functions for all watchdogs 291 * according to their available features. 292 */ 293 294 static long watchdog_ioctl(struct file *file, unsigned int cmd, 295 unsigned long arg) 296 { 297 struct watchdog_device *wdd = file->private_data; 298 void __user *argp = (void __user *)arg; 299 int __user *p = argp; 300 unsigned int val; 301 int err; 302 303 err = watchdog_ioctl_op(wdd, cmd, arg); 304 if (err != -ENOIOCTLCMD) 305 return err; 306 307 switch (cmd) { 308 case WDIOC_GETSUPPORT: 309 return copy_to_user(argp, wdd->info, 310 sizeof(struct watchdog_info)) ? -EFAULT : 0; 311 case WDIOC_GETSTATUS: 312 err = watchdog_get_status(wdd, &val); 313 if (err) 314 return err; 315 return put_user(val, p); 316 case WDIOC_GETBOOTSTATUS: 317 return put_user(wdd->bootstatus, p); 318 case WDIOC_SETOPTIONS: 319 if (get_user(val, p)) 320 return -EFAULT; 321 if (val & WDIOS_DISABLECARD) { 322 err = watchdog_stop(wdd); 323 if (err < 0) 324 return err; 325 } 326 if (val & WDIOS_ENABLECARD) { 327 err = watchdog_start(wdd); 328 if (err < 0) 329 return err; 330 } 331 return 0; 332 case WDIOC_KEEPALIVE: 333 if (!(wdd->info->options & WDIOF_KEEPALIVEPING)) 334 return -EOPNOTSUPP; 335 watchdog_ping(wdd); 336 return 0; 337 case WDIOC_SETTIMEOUT: 338 if (get_user(val, p)) 339 return -EFAULT; 340 err = watchdog_set_timeout(wdd, val); 341 if (err < 0) 342 return err; 343 /* If the watchdog is active then we send a keepalive ping 344 * to make sure that the watchdog keep's running (and if 345 * possible that it takes the new timeout) */ 346 watchdog_ping(wdd); 347 /* Fall */ 348 case WDIOC_GETTIMEOUT: 349 /* timeout == 0 means that we don't know the timeout */ 350 if (wdd->timeout == 0) 351 return -EOPNOTSUPP; 352 return put_user(wdd->timeout, p); 353 case WDIOC_GETTIMELEFT: 354 err = watchdog_get_timeleft(wdd, &val); 355 if (err) 356 return err; 357 return put_user(val, p); 358 default: 359 return -ENOTTY; 360 } 361 } 362 363 /* 364 * watchdog_open: open the /dev/watchdog* devices. 365 * @inode: inode of device 366 * @file: file handle to device 367 * 368 * When the /dev/watchdog* device gets opened, we start the watchdog. 369 * Watch out: the /dev/watchdog device is single open, so we make sure 370 * it can only be opened once. 371 */ 372 373 static int watchdog_open(struct inode *inode, struct file *file) 374 { 375 int err = -EBUSY; 376 struct watchdog_device *wdd; 377 378 /* Get the corresponding watchdog device */ 379 if (imajor(inode) == MISC_MAJOR) 380 wdd = old_wdd; 381 else 382 wdd = container_of(inode->i_cdev, struct watchdog_device, cdev); 383 384 /* the watchdog is single open! */ 385 if (test_and_set_bit(WDOG_DEV_OPEN, &wdd->status)) 386 return -EBUSY; 387 388 /* 389 * If the /dev/watchdog device is open, we don't want the module 390 * to be unloaded. 391 */ 392 if (!try_module_get(wdd->ops->owner)) 393 goto out; 394 395 err = watchdog_start(wdd); 396 if (err < 0) 397 goto out_mod; 398 399 file->private_data = wdd; 400 401 /* dev/watchdog is a virtual (and thus non-seekable) filesystem */ 402 return nonseekable_open(inode, file); 403 404 out_mod: 405 module_put(wdd->ops->owner); 406 out: 407 clear_bit(WDOG_DEV_OPEN, &wdd->status); 408 return err; 409 } 410 411 /* 412 * watchdog_release: release the watchdog device. 413 * @inode: inode of device 414 * @file: file handle to device 415 * 416 * This is the code for when /dev/watchdog gets closed. We will only 417 * stop the watchdog when we have received the magic char (and nowayout 418 * was not set), else the watchdog will keep running. 419 */ 420 421 static int watchdog_release(struct inode *inode, struct file *file) 422 { 423 struct watchdog_device *wdd = file->private_data; 424 int err = -EBUSY; 425 426 /* 427 * We only stop the watchdog if we received the magic character 428 * or if WDIOF_MAGICCLOSE is not set. If nowayout was set then 429 * watchdog_stop will fail. 430 */ 431 if (test_and_clear_bit(WDOG_ALLOW_RELEASE, &wdd->status) || 432 !(wdd->info->options & WDIOF_MAGICCLOSE)) 433 err = watchdog_stop(wdd); 434 435 /* If the watchdog was not stopped, send a keepalive ping */ 436 if (err < 0) { 437 dev_crit(wdd->dev, "watchdog did not stop!\n"); 438 watchdog_ping(wdd); 439 } 440 441 /* Allow the owner module to be unloaded again */ 442 module_put(wdd->ops->owner); 443 444 /* make sure that /dev/watchdog can be re-opened */ 445 clear_bit(WDOG_DEV_OPEN, &wdd->status); 446 447 return 0; 448 } 449 450 static const struct file_operations watchdog_fops = { 451 .owner = THIS_MODULE, 452 .write = watchdog_write, 453 .unlocked_ioctl = watchdog_ioctl, 454 .open = watchdog_open, 455 .release = watchdog_release, 456 }; 457 458 static struct miscdevice watchdog_miscdev = { 459 .minor = WATCHDOG_MINOR, 460 .name = "watchdog", 461 .fops = &watchdog_fops, 462 }; 463 464 /* 465 * watchdog_dev_register: register a watchdog device 466 * @watchdog: watchdog device 467 * 468 * Register a watchdog device including handling the legacy 469 * /dev/watchdog node. /dev/watchdog is actually a miscdevice and 470 * thus we set it up like that. 471 */ 472 473 int watchdog_dev_register(struct watchdog_device *watchdog) 474 { 475 int err, devno; 476 477 if (watchdog->id == 0) { 478 watchdog_miscdev.parent = watchdog->parent; 479 err = misc_register(&watchdog_miscdev); 480 if (err != 0) { 481 pr_err("%s: cannot register miscdev on minor=%d (err=%d).\n", 482 watchdog->info->identity, WATCHDOG_MINOR, err); 483 if (err == -EBUSY) 484 pr_err("%s: a legacy watchdog module is probably present.\n", 485 watchdog->info->identity); 486 return err; 487 } 488 old_wdd = watchdog; 489 } 490 491 /* Fill in the data structures */ 492 devno = MKDEV(MAJOR(watchdog_devt), watchdog->id); 493 cdev_init(&watchdog->cdev, &watchdog_fops); 494 watchdog->cdev.owner = watchdog->ops->owner; 495 496 /* Add the device */ 497 err = cdev_add(&watchdog->cdev, devno, 1); 498 if (err) { 499 pr_err("watchdog%d unable to add device %d:%d\n", 500 watchdog->id, MAJOR(watchdog_devt), watchdog->id); 501 if (watchdog->id == 0) { 502 misc_deregister(&watchdog_miscdev); 503 old_wdd = NULL; 504 } 505 } 506 return err; 507 } 508 509 /* 510 * watchdog_dev_unregister: unregister a watchdog device 511 * @watchdog: watchdog device 512 * 513 * Unregister the watchdog and if needed the legacy /dev/watchdog device. 514 */ 515 516 int watchdog_dev_unregister(struct watchdog_device *watchdog) 517 { 518 cdev_del(&watchdog->cdev); 519 if (watchdog->id == 0) { 520 misc_deregister(&watchdog_miscdev); 521 old_wdd = NULL; 522 } 523 return 0; 524 } 525 526 /* 527 * watchdog_dev_init: init dev part of watchdog core 528 * 529 * Allocate a range of chardev nodes to use for watchdog devices 530 */ 531 532 int __init watchdog_dev_init(void) 533 { 534 int err = alloc_chrdev_region(&watchdog_devt, 0, MAX_DOGS, "watchdog"); 535 if (err < 0) 536 pr_err("watchdog: unable to allocate char dev region\n"); 537 return err; 538 } 539 540 /* 541 * watchdog_dev_exit: exit dev part of watchdog core 542 * 543 * Release the range of chardev nodes used for watchdog devices 544 */ 545 546 void __exit watchdog_dev_exit(void) 547 { 548 unregister_chrdev_region(watchdog_devt, MAX_DOGS); 549 } 550