Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 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 | |
Wim Van Sebroeck | 6cfb5aa | 2012-05-21 15:31:06 +0200 | [diff] [blame] | 45 | #include "watchdog_core.h" |
H Hartley Sweeten | 09a46e7 | 2012-04-20 13:28:24 -0700 | [diff] [blame] | 46 | |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 47 | /* the dev_t structure to store the dynamically allocated watchdog devices */ |
| 48 | static dev_t watchdog_devt; |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 49 | /* the watchdog device behind /dev/watchdog */ |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 50 | static struct watchdog_device *old_wdd; |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 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. |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 59 | * We only ping when the watchdog device is running. |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 60 | */ |
| 61 | |
| 62 | static int watchdog_ping(struct watchdog_device *wddev) |
| 63 | { |
Viresh Kumar | 257f8c4a | 2012-03-12 09:51:56 +0530 | [diff] [blame] | 64 | if (watchdog_active(wddev)) { |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 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 | |
Viresh Kumar | 257f8c4a | 2012-03-12 09:51:56 +0530 | [diff] [blame] | 86 | if (!watchdog_active(wddev)) { |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 87 | err = wddev->ops->start(wddev); |
| 88 | if (err < 0) |
| 89 | return err; |
| 90 | |
H Hartley Sweeten | cb7efc0 | 2011-08-03 15:38:20 -0700 | [diff] [blame] | 91 | set_bit(WDOG_ACTIVE, &wddev->status); |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 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. |
Wim Van Sebroeck | 7e192b9 | 2011-07-22 18:59:17 +0000 | [diff] [blame] | 103 | * If the 'nowayout' feature was set, the watchdog cannot be stopped. |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 104 | */ |
| 105 | |
| 106 | static int watchdog_stop(struct watchdog_device *wddev) |
| 107 | { |
Wim Van Sebroeck | 7e192b9 | 2011-07-22 18:59:17 +0000 | [diff] [blame] | 108 | int err = -EBUSY; |
| 109 | |
H Hartley Sweeten | cb7efc0 | 2011-08-03 15:38:20 -0700 | [diff] [blame] | 110 | if (test_bit(WDOG_NO_WAY_OUT, &wddev->status)) { |
Wim Van Sebroeck | 7e192b9 | 2011-07-22 18:59:17 +0000 | [diff] [blame] | 111 | pr_info("%s: nowayout prevents watchdog to be stopped!\n", |
H Hartley Sweeten | cb7efc0 | 2011-08-03 15:38:20 -0700 | [diff] [blame] | 112 | wddev->info->identity); |
Wim Van Sebroeck | 7e192b9 | 2011-07-22 18:59:17 +0000 | [diff] [blame] | 113 | return err; |
| 114 | } |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 115 | |
Viresh Kumar | 257f8c4a | 2012-03-12 09:51:56 +0530 | [diff] [blame] | 116 | if (watchdog_active(wddev)) { |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 117 | err = wddev->ops->stop(wddev); |
| 118 | if (err < 0) |
| 119 | return err; |
| 120 | |
H Hartley Sweeten | cb7efc0 | 2011-08-03 15:38:20 -0700 | [diff] [blame] | 121 | clear_bit(WDOG_ACTIVE, &wddev->status); |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 122 | } |
| 123 | return 0; |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 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. |
Wim Van Sebroeck | 017cf08 | 2011-07-22 18:58:54 +0000 | [diff] [blame] | 134 | * Writing the magic 'V' sequence allows the next close to turn |
Wim Van Sebroeck | 7e192b9 | 2011-07-22 18:59:17 +0000 | [diff] [blame] | 135 | * off the watchdog (if 'nowayout' is not set). |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 136 | */ |
| 137 | |
| 138 | static ssize_t watchdog_write(struct file *file, const char __user *data, |
| 139 | size_t len, loff_t *ppos) |
| 140 | { |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 141 | struct watchdog_device *wdd = file->private_data; |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 142 | size_t i; |
| 143 | char c; |
| 144 | |
| 145 | if (len == 0) |
| 146 | return 0; |
| 147 | |
Wim Van Sebroeck | 017cf08 | 2011-07-22 18:58:54 +0000 | [diff] [blame] | 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 */ |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 155 | for (i = 0; i != len; i++) { |
| 156 | if (get_user(c, data + i)) |
| 157 | return -EFAULT; |
Wim Van Sebroeck | 017cf08 | 2011-07-22 18:58:54 +0000 | [diff] [blame] | 158 | if (c == 'V') |
| 159 | set_bit(WDOG_ALLOW_RELEASE, &wdd->status); |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 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 | /* |
Wim Van Sebroeck | 2fa0356 | 2011-07-22 18:56:38 +0000 | [diff] [blame] | 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 | { |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 181 | struct watchdog_device *wdd = file->private_data; |
Wim Van Sebroeck | 2fa0356 | 2011-07-22 18:56:38 +0000 | [diff] [blame] | 182 | void __user *argp = (void __user *)arg; |
| 183 | int __user *p = argp; |
| 184 | unsigned int val; |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 185 | int err; |
Wim Van Sebroeck | 2fa0356 | 2011-07-22 18:56:38 +0000 | [diff] [blame] | 186 | |
Wim Van Sebroeck | 78d88fc | 2011-07-22 18:59:49 +0000 | [diff] [blame] | 187 | if (wdd->ops->ioctl) { |
| 188 | err = wdd->ops->ioctl(wdd, cmd, arg); |
| 189 | if (err != -ENOIOCTLCMD) |
| 190 | return err; |
| 191 | } |
| 192 | |
Wim Van Sebroeck | 2fa0356 | 2011-07-22 18:56:38 +0000 | [diff] [blame] | 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); |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 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; |
Wim Van Sebroeck | c2dc00e | 2011-07-22 18:57:23 +0000 | [diff] [blame] | 216 | case WDIOC_KEEPALIVE: |
| 217 | if (!(wdd->info->options & WDIOF_KEEPALIVEPING)) |
| 218 | return -EOPNOTSUPP; |
| 219 | watchdog_ping(wdd); |
| 220 | return 0; |
Wim Van Sebroeck | 014d694 | 2011-07-22 18:58:21 +0000 | [diff] [blame] | 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; |
Wim Van Sebroeck | 3f43f68 | 2011-07-22 19:00:16 +0000 | [diff] [blame] | 227 | if ((wdd->max_timeout != 0) && |
| 228 | (val < wdd->min_timeout || val > wdd->max_timeout)) |
| 229 | return -EINVAL; |
Wim Van Sebroeck | 014d694 | 2011-07-22 18:58:21 +0000 | [diff] [blame] | 230 | err = wdd->ops->set_timeout(wdd, val); |
| 231 | if (err < 0) |
| 232 | return err; |
Wim Van Sebroeck | 014d694 | 2011-07-22 18:58:21 +0000 | [diff] [blame] | 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); |
Viresh Kumar | fd7b673 | 2012-03-16 09:14:00 +0100 | [diff] [blame] | 243 | case WDIOC_GETTIMELEFT: |
| 244 | if (!wdd->ops->get_timeleft) |
| 245 | return -EOPNOTSUPP; |
| 246 | |
| 247 | return put_user(wdd->ops->get_timeleft(wdd), p); |
Wim Van Sebroeck | 2fa0356 | 2011-07-22 18:56:38 +0000 | [diff] [blame] | 248 | default: |
| 249 | return -ENOTTY; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | /* |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 254 | * watchdog_open: open the /dev/watchdog* devices. |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 255 | * @inode: inode of device |
| 256 | * @file: file handle to device |
| 257 | * |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 258 | * When the /dev/watchdog* device gets opened, we start the watchdog. |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 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; |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 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); |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 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 | |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 285 | err = watchdog_start(wdd); |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 286 | if (err < 0) |
| 287 | goto out_mod; |
| 288 | |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 289 | file->private_data = wdd; |
| 290 | |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 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 | /* |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 302 | * watchdog_release: release the watchdog device. |
| 303 | * @inode: inode of device |
| 304 | * @file: file handle to device |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 305 | * |
Wim Van Sebroeck | 017cf08 | 2011-07-22 18:58:54 +0000 | [diff] [blame] | 306 | * This is the code for when /dev/watchdog gets closed. We will only |
Wim Van Sebroeck | 7e192b9 | 2011-07-22 18:59:17 +0000 | [diff] [blame] | 307 | * stop the watchdog when we have received the magic char (and nowayout |
| 308 | * was not set), else the watchdog will keep running. |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 309 | */ |
| 310 | |
| 311 | static int watchdog_release(struct inode *inode, struct file *file) |
| 312 | { |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 313 | struct watchdog_device *wdd = file->private_data; |
Wim Van Sebroeck | 017cf08 | 2011-07-22 18:58:54 +0000 | [diff] [blame] | 314 | int err = -EBUSY; |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 315 | |
Wim Van Sebroeck | 017cf08 | 2011-07-22 18:58:54 +0000 | [diff] [blame] | 316 | /* |
| 317 | * We only stop the watchdog if we received the magic character |
Wim Van Sebroeck | 7e192b9 | 2011-07-22 18:59:17 +0000 | [diff] [blame] | 318 | * or if WDIOF_MAGICCLOSE is not set. If nowayout was set then |
| 319 | * watchdog_stop will fail. |
Wim Van Sebroeck | 017cf08 | 2011-07-22 18:58:54 +0000 | [diff] [blame] | 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 */ |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 326 | if (err < 0) { |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 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, |
Wim Van Sebroeck | 2fa0356 | 2011-07-22 18:56:38 +0000 | [diff] [blame] | 343 | .unlocked_ioctl = watchdog_ioctl, |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 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 | /* |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 355 | * watchdog_dev_register: register a watchdog device |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 356 | * @watchdog: watchdog device |
| 357 | * |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 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. |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 361 | */ |
| 362 | |
| 363 | int watchdog_dev_register(struct watchdog_device *watchdog) |
| 364 | { |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 365 | int err, devno; |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 366 | |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 367 | if (watchdog->id == 0) { |
| 368 | err = misc_register(&watchdog_miscdev); |
| 369 | if (err != 0) { |
| 370 | pr_err("%s: cannot register miscdev on minor=%d (err=%d).\n", |
| 371 | watchdog->info->identity, WATCHDOG_MINOR, err); |
| 372 | if (err == -EBUSY) |
| 373 | pr_err("%s: a legacy watchdog module is probably present.\n", |
| 374 | watchdog->info->identity); |
| 375 | return err; |
| 376 | } |
| 377 | old_wdd = watchdog; |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 380 | /* Fill in the data structures */ |
| 381 | devno = MKDEV(MAJOR(watchdog_devt), watchdog->id); |
| 382 | cdev_init(&watchdog->cdev, &watchdog_fops); |
| 383 | watchdog->cdev.owner = watchdog->ops->owner; |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 384 | |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 385 | /* Add the device */ |
| 386 | err = cdev_add(&watchdog->cdev, devno, 1); |
| 387 | if (err) { |
| 388 | pr_err("watchdog%d unable to add device %d:%d\n", |
| 389 | watchdog->id, MAJOR(watchdog_devt), watchdog->id); |
| 390 | if (watchdog->id == 0) { |
| 391 | misc_deregister(&watchdog_miscdev); |
| 392 | old_wdd = NULL; |
| 393 | } |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 394 | } |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 395 | return err; |
| 396 | } |
| 397 | |
| 398 | /* |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 399 | * watchdog_dev_unregister: unregister a watchdog device |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 400 | * @watchdog: watchdog device |
| 401 | * |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 402 | * Unregister the watchdog and if needed the legacy /dev/watchdog device. |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 403 | */ |
| 404 | |
| 405 | int watchdog_dev_unregister(struct watchdog_device *watchdog) |
| 406 | { |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 407 | cdev_del(&watchdog->cdev); |
| 408 | if (watchdog->id == 0) { |
| 409 | misc_deregister(&watchdog_miscdev); |
| 410 | old_wdd = NULL; |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 411 | } |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 412 | return 0; |
| 413 | } |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 414 | |
| 415 | /* |
| 416 | * watchdog_dev_init: init dev part of watchdog core |
| 417 | * |
| 418 | * Allocate a range of chardev nodes to use for watchdog devices |
| 419 | */ |
| 420 | |
| 421 | int __init watchdog_dev_init(void) |
| 422 | { |
| 423 | int err = alloc_chrdev_region(&watchdog_devt, 0, MAX_DOGS, "watchdog"); |
| 424 | if (err < 0) |
| 425 | pr_err("watchdog: unable to allocate char dev region\n"); |
| 426 | return err; |
| 427 | } |
| 428 | |
| 429 | /* |
| 430 | * watchdog_dev_exit: exit dev part of watchdog core |
| 431 | * |
| 432 | * Release the range of chardev nodes used for watchdog devices |
| 433 | */ |
| 434 | |
| 435 | void __exit watchdog_dev_exit(void) |
| 436 | { |
| 437 | unregister_chrdev_region(watchdog_devt, MAX_DOGS); |
| 438 | } |