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 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 struct watchdog_device *wdd = file->private_data; 142 size_t i; 143 char c; 144 145 if (len == 0) 146 return 0; 147 148 /* 149 * Note: just in case someone wrote the magic character 150 * five months ago... 151 */ 152 clear_bit(WDOG_ALLOW_RELEASE, &wdd->status); 153 154 /* scan to see whether or not we got the magic character */ 155 for (i = 0; i != len; i++) { 156 if (get_user(c, data + i)) 157 return -EFAULT; 158 if (c == 'V') 159 set_bit(WDOG_ALLOW_RELEASE, &wdd->status); 160 } 161 162 /* someone wrote to us, so we send the watchdog a keepalive ping */ 163 watchdog_ping(wdd); 164 165 return len; 166 } 167 168 /* 169 * watchdog_ioctl: handle the different ioctl's for the watchdog device. 170 * @file: file handle to the device 171 * @cmd: watchdog command 172 * @arg: argument pointer 173 * 174 * The watchdog API defines a common set of functions for all watchdogs 175 * according to their available features. 176 */ 177 178 static long watchdog_ioctl(struct file *file, unsigned int cmd, 179 unsigned long arg) 180 { 181 struct watchdog_device *wdd = file->private_data; 182 void __user *argp = (void __user *)arg; 183 int __user *p = argp; 184 unsigned int val; 185 int err; 186 187 if (wdd->ops->ioctl) { 188 err = wdd->ops->ioctl(wdd, cmd, arg); 189 if (err != -ENOIOCTLCMD) 190 return err; 191 } 192 193 switch (cmd) { 194 case WDIOC_GETSUPPORT: 195 return copy_to_user(argp, wdd->info, 196 sizeof(struct watchdog_info)) ? -EFAULT : 0; 197 case WDIOC_GETSTATUS: 198 val = wdd->ops->status ? wdd->ops->status(wdd) : 0; 199 return put_user(val, p); 200 case WDIOC_GETBOOTSTATUS: 201 return put_user(wdd->bootstatus, p); 202 case WDIOC_SETOPTIONS: 203 if (get_user(val, p)) 204 return -EFAULT; 205 if (val & WDIOS_DISABLECARD) { 206 err = watchdog_stop(wdd); 207 if (err < 0) 208 return err; 209 } 210 if (val & WDIOS_ENABLECARD) { 211 err = watchdog_start(wdd); 212 if (err < 0) 213 return err; 214 } 215 return 0; 216 case WDIOC_KEEPALIVE: 217 if (!(wdd->info->options & WDIOF_KEEPALIVEPING)) 218 return -EOPNOTSUPP; 219 watchdog_ping(wdd); 220 return 0; 221 case WDIOC_SETTIMEOUT: 222 if ((wdd->ops->set_timeout == NULL) || 223 !(wdd->info->options & WDIOF_SETTIMEOUT)) 224 return -EOPNOTSUPP; 225 if (get_user(val, p)) 226 return -EFAULT; 227 if ((wdd->max_timeout != 0) && 228 (val < wdd->min_timeout || val > wdd->max_timeout)) 229 return -EINVAL; 230 err = wdd->ops->set_timeout(wdd, val); 231 if (err < 0) 232 return err; 233 /* If the watchdog is active then we send a keepalive ping 234 * to make sure that the watchdog keep's running (and if 235 * possible that it takes the new timeout) */ 236 watchdog_ping(wdd); 237 /* Fall */ 238 case WDIOC_GETTIMEOUT: 239 /* timeout == 0 means that we don't know the timeout */ 240 if (wdd->timeout == 0) 241 return -EOPNOTSUPP; 242 return put_user(wdd->timeout, p); 243 case WDIOC_GETTIMELEFT: 244 if (!wdd->ops->get_timeleft) 245 return -EOPNOTSUPP; 246 247 return put_user(wdd->ops->get_timeleft(wdd), p); 248 default: 249 return -ENOTTY; 250 } 251 } 252 253 /* 254 * watchdog_open: open the /dev/watchdog* devices. 255 * @inode: inode of device 256 * @file: file handle to device 257 * 258 * When the /dev/watchdog* device gets opened, we start the watchdog. 259 * Watch out: the /dev/watchdog device is single open, so we make sure 260 * it can only be opened once. 261 */ 262 263 static int watchdog_open(struct inode *inode, struct file *file) 264 { 265 int err = -EBUSY; 266 struct watchdog_device *wdd; 267 268 /* Get the corresponding watchdog device */ 269 if (imajor(inode) == MISC_MAJOR) 270 wdd = old_wdd; 271 else 272 wdd = container_of(inode->i_cdev, struct watchdog_device, cdev); 273 274 /* the watchdog is single open! */ 275 if (test_and_set_bit(WDOG_DEV_OPEN, &wdd->status)) 276 return -EBUSY; 277 278 /* 279 * If the /dev/watchdog device is open, we don't want the module 280 * to be unloaded. 281 */ 282 if (!try_module_get(wdd->ops->owner)) 283 goto out; 284 285 err = watchdog_start(wdd); 286 if (err < 0) 287 goto out_mod; 288 289 file->private_data = wdd; 290 291 /* dev/watchdog is a virtual (and thus non-seekable) filesystem */ 292 return nonseekable_open(inode, file); 293 294 out_mod: 295 module_put(wdd->ops->owner); 296 out: 297 clear_bit(WDOG_DEV_OPEN, &wdd->status); 298 return err; 299 } 300 301 /* 302 * watchdog_release: release the watchdog device. 303 * @inode: inode of device 304 * @file: file handle to device 305 * 306 * This is the code for when /dev/watchdog gets closed. We will only 307 * stop the watchdog when we have received the magic char (and nowayout 308 * was not set), else the watchdog will keep running. 309 */ 310 311 static int watchdog_release(struct inode *inode, struct file *file) 312 { 313 struct watchdog_device *wdd = file->private_data; 314 int err = -EBUSY; 315 316 /* 317 * We only stop the watchdog if we received the magic character 318 * or if WDIOF_MAGICCLOSE is not set. If nowayout was set then 319 * watchdog_stop will fail. 320 */ 321 if (test_and_clear_bit(WDOG_ALLOW_RELEASE, &wdd->status) || 322 !(wdd->info->options & WDIOF_MAGICCLOSE)) 323 err = watchdog_stop(wdd); 324 325 /* If the watchdog was not stopped, send a keepalive ping */ 326 if (err < 0) { 327 pr_crit("%s: watchdog did not stop!\n", wdd->info->identity); 328 watchdog_ping(wdd); 329 } 330 331 /* Allow the owner module to be unloaded again */ 332 module_put(wdd->ops->owner); 333 334 /* make sure that /dev/watchdog can be re-opened */ 335 clear_bit(WDOG_DEV_OPEN, &wdd->status); 336 337 return 0; 338 } 339 340 static const struct file_operations watchdog_fops = { 341 .owner = THIS_MODULE, 342 .write = watchdog_write, 343 .unlocked_ioctl = watchdog_ioctl, 344 .open = watchdog_open, 345 .release = watchdog_release, 346 }; 347 348 static struct miscdevice watchdog_miscdev = { 349 .minor = WATCHDOG_MINOR, 350 .name = "watchdog", 351 .fops = &watchdog_fops, 352 }; 353 354 /* 355 * watchdog_dev_register: register a watchdog device 356 * @watchdog: watchdog device 357 * 358 * Register a watchdog device including handling the legacy 359 * /dev/watchdog node. /dev/watchdog is actually a miscdevice and 360 * thus we set it up like that. 361 */ 362 363 int watchdog_dev_register(struct watchdog_device *watchdog) 364 { 365 int err, devno; 366 367 if (watchdog->id == 0) { 368 watchdog_miscdev.parent = watchdog->parent; 369 err = misc_register(&watchdog_miscdev); 370 if (err != 0) { 371 pr_err("%s: cannot register miscdev on minor=%d (err=%d).\n", 372 watchdog->info->identity, WATCHDOG_MINOR, err); 373 if (err == -EBUSY) 374 pr_err("%s: a legacy watchdog module is probably present.\n", 375 watchdog->info->identity); 376 return err; 377 } 378 old_wdd = watchdog; 379 } 380 381 /* Fill in the data structures */ 382 devno = MKDEV(MAJOR(watchdog_devt), watchdog->id); 383 cdev_init(&watchdog->cdev, &watchdog_fops); 384 watchdog->cdev.owner = watchdog->ops->owner; 385 386 /* Add the device */ 387 err = cdev_add(&watchdog->cdev, devno, 1); 388 if (err) { 389 pr_err("watchdog%d unable to add device %d:%d\n", 390 watchdog->id, MAJOR(watchdog_devt), watchdog->id); 391 if (watchdog->id == 0) { 392 misc_deregister(&watchdog_miscdev); 393 old_wdd = NULL; 394 } 395 } 396 return err; 397 } 398 399 /* 400 * watchdog_dev_unregister: unregister a watchdog device 401 * @watchdog: watchdog device 402 * 403 * Unregister the watchdog and if needed the legacy /dev/watchdog device. 404 */ 405 406 int watchdog_dev_unregister(struct watchdog_device *watchdog) 407 { 408 cdev_del(&watchdog->cdev); 409 if (watchdog->id == 0) { 410 misc_deregister(&watchdog_miscdev); 411 old_wdd = NULL; 412 } 413 return 0; 414 } 415 416 /* 417 * watchdog_dev_init: init dev part of watchdog core 418 * 419 * Allocate a range of chardev nodes to use for watchdog devices 420 */ 421 422 int __init watchdog_dev_init(void) 423 { 424 int err = alloc_chrdev_region(&watchdog_devt, 0, MAX_DOGS, "watchdog"); 425 if (err < 0) 426 pr_err("watchdog: unable to allocate char dev region\n"); 427 return err; 428 } 429 430 /* 431 * watchdog_dev_exit: exit dev part of watchdog core 432 * 433 * Release the range of chardev nodes used for watchdog devices 434 */ 435 436 void __exit watchdog_dev_exit(void) 437 { 438 unregister_chrdev_region(watchdog_devt, MAX_DOGS); 439 } 440