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)) { |
Alan Cox | 3dfd621 | 2012-05-11 12:00:22 +0200 | [diff] [blame^] | 111 | dev_info(wddev->dev, "nowayout prevents watchdog being stopped!\n"); |
Wim Van Sebroeck | 7e192b9 | 2011-07-22 18:59:17 +0000 | [diff] [blame] | 112 | return err; |
| 113 | } |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 114 | |
Viresh Kumar | 257f8c4a | 2012-03-12 09:51:56 +0530 | [diff] [blame] | 115 | if (watchdog_active(wddev)) { |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 116 | err = wddev->ops->stop(wddev); |
| 117 | if (err < 0) |
| 118 | return err; |
| 119 | |
H Hartley Sweeten | cb7efc0 | 2011-08-03 15:38:20 -0700 | [diff] [blame] | 120 | clear_bit(WDOG_ACTIVE, &wddev->status); |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 121 | } |
| 122 | return 0; |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /* |
| 126 | * watchdog_write: writes to the watchdog. |
| 127 | * @file: file from VFS |
| 128 | * @data: user address of data |
| 129 | * @len: length of data |
| 130 | * @ppos: pointer to the file offset |
| 131 | * |
| 132 | * 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] | 133 | * Writing the magic 'V' sequence allows the next close to turn |
Wim Van Sebroeck | 7e192b9 | 2011-07-22 18:59:17 +0000 | [diff] [blame] | 134 | * off the watchdog (if 'nowayout' is not set). |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 135 | */ |
| 136 | |
| 137 | static ssize_t watchdog_write(struct file *file, const char __user *data, |
| 138 | size_t len, loff_t *ppos) |
| 139 | { |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 140 | struct watchdog_device *wdd = file->private_data; |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 141 | size_t i; |
| 142 | char c; |
| 143 | |
| 144 | if (len == 0) |
| 145 | return 0; |
| 146 | |
Wim Van Sebroeck | 017cf08 | 2011-07-22 18:58:54 +0000 | [diff] [blame] | 147 | /* |
| 148 | * Note: just in case someone wrote the magic character |
| 149 | * five months ago... |
| 150 | */ |
| 151 | clear_bit(WDOG_ALLOW_RELEASE, &wdd->status); |
| 152 | |
| 153 | /* scan to see whether or not we got the magic character */ |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 154 | for (i = 0; i != len; i++) { |
| 155 | if (get_user(c, data + i)) |
| 156 | return -EFAULT; |
Wim Van Sebroeck | 017cf08 | 2011-07-22 18:58:54 +0000 | [diff] [blame] | 157 | if (c == 'V') |
| 158 | set_bit(WDOG_ALLOW_RELEASE, &wdd->status); |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /* someone wrote to us, so we send the watchdog a keepalive ping */ |
| 162 | watchdog_ping(wdd); |
| 163 | |
| 164 | return len; |
| 165 | } |
| 166 | |
| 167 | /* |
Wim Van Sebroeck | 2fa0356 | 2011-07-22 18:56:38 +0000 | [diff] [blame] | 168 | * watchdog_ioctl: handle the different ioctl's for the watchdog device. |
| 169 | * @file: file handle to the device |
| 170 | * @cmd: watchdog command |
| 171 | * @arg: argument pointer |
| 172 | * |
| 173 | * The watchdog API defines a common set of functions for all watchdogs |
| 174 | * according to their available features. |
| 175 | */ |
| 176 | |
| 177 | static long watchdog_ioctl(struct file *file, unsigned int cmd, |
| 178 | unsigned long arg) |
| 179 | { |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 180 | struct watchdog_device *wdd = file->private_data; |
Wim Van Sebroeck | 2fa0356 | 2011-07-22 18:56:38 +0000 | [diff] [blame] | 181 | void __user *argp = (void __user *)arg; |
| 182 | int __user *p = argp; |
| 183 | unsigned int val; |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 184 | int err; |
Wim Van Sebroeck | 2fa0356 | 2011-07-22 18:56:38 +0000 | [diff] [blame] | 185 | |
Wim Van Sebroeck | 78d88fc | 2011-07-22 18:59:49 +0000 | [diff] [blame] | 186 | if (wdd->ops->ioctl) { |
| 187 | err = wdd->ops->ioctl(wdd, cmd, arg); |
| 188 | if (err != -ENOIOCTLCMD) |
| 189 | return err; |
| 190 | } |
| 191 | |
Wim Van Sebroeck | 2fa0356 | 2011-07-22 18:56:38 +0000 | [diff] [blame] | 192 | switch (cmd) { |
| 193 | case WDIOC_GETSUPPORT: |
| 194 | return copy_to_user(argp, wdd->info, |
| 195 | sizeof(struct watchdog_info)) ? -EFAULT : 0; |
| 196 | case WDIOC_GETSTATUS: |
| 197 | val = wdd->ops->status ? wdd->ops->status(wdd) : 0; |
| 198 | return put_user(val, p); |
| 199 | case WDIOC_GETBOOTSTATUS: |
| 200 | return put_user(wdd->bootstatus, p); |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 201 | case WDIOC_SETOPTIONS: |
| 202 | if (get_user(val, p)) |
| 203 | return -EFAULT; |
| 204 | if (val & WDIOS_DISABLECARD) { |
| 205 | err = watchdog_stop(wdd); |
| 206 | if (err < 0) |
| 207 | return err; |
| 208 | } |
| 209 | if (val & WDIOS_ENABLECARD) { |
| 210 | err = watchdog_start(wdd); |
| 211 | if (err < 0) |
| 212 | return err; |
| 213 | } |
| 214 | return 0; |
Wim Van Sebroeck | c2dc00e | 2011-07-22 18:57:23 +0000 | [diff] [blame] | 215 | case WDIOC_KEEPALIVE: |
| 216 | if (!(wdd->info->options & WDIOF_KEEPALIVEPING)) |
| 217 | return -EOPNOTSUPP; |
| 218 | watchdog_ping(wdd); |
| 219 | return 0; |
Wim Van Sebroeck | 014d694 | 2011-07-22 18:58:21 +0000 | [diff] [blame] | 220 | case WDIOC_SETTIMEOUT: |
| 221 | if ((wdd->ops->set_timeout == NULL) || |
| 222 | !(wdd->info->options & WDIOF_SETTIMEOUT)) |
| 223 | return -EOPNOTSUPP; |
| 224 | if (get_user(val, p)) |
| 225 | return -EFAULT; |
Wim Van Sebroeck | 3f43f68 | 2011-07-22 19:00:16 +0000 | [diff] [blame] | 226 | if ((wdd->max_timeout != 0) && |
| 227 | (val < wdd->min_timeout || val > wdd->max_timeout)) |
| 228 | return -EINVAL; |
Wim Van Sebroeck | 014d694 | 2011-07-22 18:58:21 +0000 | [diff] [blame] | 229 | err = wdd->ops->set_timeout(wdd, val); |
| 230 | if (err < 0) |
| 231 | return err; |
Wim Van Sebroeck | 014d694 | 2011-07-22 18:58:21 +0000 | [diff] [blame] | 232 | /* If the watchdog is active then we send a keepalive ping |
| 233 | * to make sure that the watchdog keep's running (and if |
| 234 | * possible that it takes the new timeout) */ |
| 235 | watchdog_ping(wdd); |
| 236 | /* Fall */ |
| 237 | case WDIOC_GETTIMEOUT: |
| 238 | /* timeout == 0 means that we don't know the timeout */ |
| 239 | if (wdd->timeout == 0) |
| 240 | return -EOPNOTSUPP; |
| 241 | return put_user(wdd->timeout, p); |
Viresh Kumar | fd7b673 | 2012-03-16 09:14:00 +0100 | [diff] [blame] | 242 | case WDIOC_GETTIMELEFT: |
| 243 | if (!wdd->ops->get_timeleft) |
| 244 | return -EOPNOTSUPP; |
| 245 | |
| 246 | return put_user(wdd->ops->get_timeleft(wdd), p); |
Wim Van Sebroeck | 2fa0356 | 2011-07-22 18:56:38 +0000 | [diff] [blame] | 247 | default: |
| 248 | return -ENOTTY; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | /* |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 253 | * watchdog_open: open the /dev/watchdog* devices. |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 254 | * @inode: inode of device |
| 255 | * @file: file handle to device |
| 256 | * |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 257 | * When the /dev/watchdog* device gets opened, we start the watchdog. |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 258 | * Watch out: the /dev/watchdog device is single open, so we make sure |
| 259 | * it can only be opened once. |
| 260 | */ |
| 261 | |
| 262 | static int watchdog_open(struct inode *inode, struct file *file) |
| 263 | { |
| 264 | int err = -EBUSY; |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 265 | struct watchdog_device *wdd; |
| 266 | |
| 267 | /* Get the corresponding watchdog device */ |
| 268 | if (imajor(inode) == MISC_MAJOR) |
| 269 | wdd = old_wdd; |
| 270 | else |
| 271 | wdd = container_of(inode->i_cdev, struct watchdog_device, cdev); |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 272 | |
| 273 | /* the watchdog is single open! */ |
| 274 | if (test_and_set_bit(WDOG_DEV_OPEN, &wdd->status)) |
| 275 | return -EBUSY; |
| 276 | |
| 277 | /* |
| 278 | * If the /dev/watchdog device is open, we don't want the module |
| 279 | * to be unloaded. |
| 280 | */ |
| 281 | if (!try_module_get(wdd->ops->owner)) |
| 282 | goto out; |
| 283 | |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 284 | err = watchdog_start(wdd); |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 285 | if (err < 0) |
| 286 | goto out_mod; |
| 287 | |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 288 | file->private_data = wdd; |
| 289 | |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 290 | /* dev/watchdog is a virtual (and thus non-seekable) filesystem */ |
| 291 | return nonseekable_open(inode, file); |
| 292 | |
| 293 | out_mod: |
| 294 | module_put(wdd->ops->owner); |
| 295 | out: |
| 296 | clear_bit(WDOG_DEV_OPEN, &wdd->status); |
| 297 | return err; |
| 298 | } |
| 299 | |
| 300 | /* |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 301 | * watchdog_release: release the watchdog device. |
| 302 | * @inode: inode of device |
| 303 | * @file: file handle to device |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 304 | * |
Wim Van Sebroeck | 017cf08 | 2011-07-22 18:58:54 +0000 | [diff] [blame] | 305 | * 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] | 306 | * stop the watchdog when we have received the magic char (and nowayout |
| 307 | * was not set), else the watchdog will keep running. |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 308 | */ |
| 309 | |
| 310 | static int watchdog_release(struct inode *inode, struct file *file) |
| 311 | { |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 312 | struct watchdog_device *wdd = file->private_data; |
Wim Van Sebroeck | 017cf08 | 2011-07-22 18:58:54 +0000 | [diff] [blame] | 313 | int err = -EBUSY; |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 314 | |
Wim Van Sebroeck | 017cf08 | 2011-07-22 18:58:54 +0000 | [diff] [blame] | 315 | /* |
| 316 | * We only stop the watchdog if we received the magic character |
Wim Van Sebroeck | 7e192b9 | 2011-07-22 18:59:17 +0000 | [diff] [blame] | 317 | * or if WDIOF_MAGICCLOSE is not set. If nowayout was set then |
| 318 | * watchdog_stop will fail. |
Wim Van Sebroeck | 017cf08 | 2011-07-22 18:58:54 +0000 | [diff] [blame] | 319 | */ |
| 320 | if (test_and_clear_bit(WDOG_ALLOW_RELEASE, &wdd->status) || |
| 321 | !(wdd->info->options & WDIOF_MAGICCLOSE)) |
| 322 | err = watchdog_stop(wdd); |
| 323 | |
| 324 | /* If the watchdog was not stopped, send a keepalive ping */ |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 325 | if (err < 0) { |
Alan Cox | 3dfd621 | 2012-05-11 12:00:22 +0200 | [diff] [blame^] | 326 | dev_crit(wdd->dev, "watchdog did not stop!\n"); |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 327 | watchdog_ping(wdd); |
| 328 | } |
| 329 | |
| 330 | /* Allow the owner module to be unloaded again */ |
| 331 | module_put(wdd->ops->owner); |
| 332 | |
| 333 | /* make sure that /dev/watchdog can be re-opened */ |
| 334 | clear_bit(WDOG_DEV_OPEN, &wdd->status); |
| 335 | |
| 336 | return 0; |
| 337 | } |
| 338 | |
| 339 | static const struct file_operations watchdog_fops = { |
| 340 | .owner = THIS_MODULE, |
| 341 | .write = watchdog_write, |
Wim Van Sebroeck | 2fa0356 | 2011-07-22 18:56:38 +0000 | [diff] [blame] | 342 | .unlocked_ioctl = watchdog_ioctl, |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 343 | .open = watchdog_open, |
| 344 | .release = watchdog_release, |
| 345 | }; |
| 346 | |
| 347 | static struct miscdevice watchdog_miscdev = { |
| 348 | .minor = WATCHDOG_MINOR, |
| 349 | .name = "watchdog", |
| 350 | .fops = &watchdog_fops, |
| 351 | }; |
| 352 | |
| 353 | /* |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 354 | * watchdog_dev_register: register a watchdog device |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 355 | * @watchdog: watchdog device |
| 356 | * |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 357 | * Register a watchdog device including handling the legacy |
| 358 | * /dev/watchdog node. /dev/watchdog is actually a miscdevice and |
| 359 | * thus we set it up like that. |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 360 | */ |
| 361 | |
| 362 | int watchdog_dev_register(struct watchdog_device *watchdog) |
| 363 | { |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 364 | int err, devno; |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 365 | |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 366 | if (watchdog->id == 0) { |
Alan Cox | d6b469d | 2012-05-11 12:00:20 +0200 | [diff] [blame] | 367 | watchdog_miscdev.parent = watchdog->parent; |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 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 | } |