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. |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 54 | * @wdd: the watchdog device to ping |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 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 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 62 | static int watchdog_ping(struct watchdog_device *wdd) |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 63 | { |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 64 | int err = 0; |
| 65 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 66 | mutex_lock(&wdd->lock); |
Hans de Goede | f4e9c82 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 67 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 68 | if (test_bit(WDOG_UNREGISTERED, &wdd->status)) { |
Hans de Goede | e907df3 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 69 | err = -ENODEV; |
| 70 | goto out_ping; |
| 71 | } |
| 72 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 73 | if (!watchdog_active(wdd)) |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 74 | goto out_ping; |
| 75 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 76 | if (wdd->ops->ping) |
| 77 | err = wdd->ops->ping(wdd); /* ping the watchdog */ |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 78 | else |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 79 | err = wdd->ops->start(wdd); /* restart watchdog */ |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 80 | |
| 81 | out_ping: |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 82 | mutex_unlock(&wdd->lock); |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 83 | return err; |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /* |
| 87 | * watchdog_start: wrapper to start the watchdog. |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 88 | * @wdd: the watchdog device to start |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 89 | * |
| 90 | * Start the watchdog if it is not active and mark it active. |
| 91 | * This function returns zero on success or a negative errno code for |
| 92 | * failure. |
| 93 | */ |
| 94 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 95 | static int watchdog_start(struct watchdog_device *wdd) |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 96 | { |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 97 | int err = 0; |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 98 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 99 | mutex_lock(&wdd->lock); |
Hans de Goede | f4e9c82 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 100 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 101 | if (test_bit(WDOG_UNREGISTERED, &wdd->status)) { |
Hans de Goede | e907df3 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 102 | err = -ENODEV; |
| 103 | goto out_start; |
| 104 | } |
| 105 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 106 | if (watchdog_active(wdd)) |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 107 | goto out_start; |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 108 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 109 | err = wdd->ops->start(wdd); |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 110 | if (err == 0) |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 111 | set_bit(WDOG_ACTIVE, &wdd->status); |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 112 | |
| 113 | out_start: |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 114 | mutex_unlock(&wdd->lock); |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 115 | return err; |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /* |
| 119 | * watchdog_stop: wrapper to stop the watchdog. |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 120 | * @wdd: the watchdog device to stop |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 121 | * |
| 122 | * Stop the watchdog if it is still active and unmark it active. |
| 123 | * This function returns zero on success or a negative errno code for |
| 124 | * failure. |
Wim Van Sebroeck | 7e192b9 | 2011-07-22 18:59:17 +0000 | [diff] [blame] | 125 | * If the 'nowayout' feature was set, the watchdog cannot be stopped. |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 126 | */ |
| 127 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 128 | static int watchdog_stop(struct watchdog_device *wdd) |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 129 | { |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 130 | int err = 0; |
| 131 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 132 | mutex_lock(&wdd->lock); |
Hans de Goede | f4e9c82 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 133 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 134 | if (test_bit(WDOG_UNREGISTERED, &wdd->status)) { |
Hans de Goede | e907df3 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 135 | err = -ENODEV; |
| 136 | goto out_stop; |
| 137 | } |
| 138 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 139 | if (!watchdog_active(wdd)) |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 140 | goto out_stop; |
Wim Van Sebroeck | 7e192b9 | 2011-07-22 18:59:17 +0000 | [diff] [blame] | 141 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 142 | if (test_bit(WDOG_NO_WAY_OUT, &wdd->status)) { |
| 143 | dev_info(wdd->dev, "nowayout prevents watchdog being stopped!\n"); |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 144 | err = -EBUSY; |
| 145 | goto out_stop; |
Wim Van Sebroeck | 7e192b9 | 2011-07-22 18:59:17 +0000 | [diff] [blame] | 146 | } |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 147 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 148 | err = wdd->ops->stop(wdd); |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 149 | if (err == 0) |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 150 | clear_bit(WDOG_ACTIVE, &wdd->status); |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 151 | |
| 152 | out_stop: |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 153 | mutex_unlock(&wdd->lock); |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 154 | return err; |
| 155 | } |
| 156 | |
| 157 | /* |
| 158 | * watchdog_get_status: wrapper to get the watchdog status |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 159 | * @wdd: the watchdog device to get the status from |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 160 | * @status: the status of the watchdog device |
| 161 | * |
| 162 | * Get the watchdog's status flags. |
| 163 | */ |
| 164 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 165 | static int watchdog_get_status(struct watchdog_device *wdd, |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 166 | unsigned int *status) |
| 167 | { |
| 168 | int err = 0; |
| 169 | |
| 170 | *status = 0; |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 171 | if (!wdd->ops->status) |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 172 | return -EOPNOTSUPP; |
| 173 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 174 | mutex_lock(&wdd->lock); |
Hans de Goede | f4e9c82 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 175 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 176 | if (test_bit(WDOG_UNREGISTERED, &wdd->status)) { |
Hans de Goede | e907df3 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 177 | err = -ENODEV; |
| 178 | goto out_status; |
| 179 | } |
| 180 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 181 | *status = wdd->ops->status(wdd); |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 182 | |
Hans de Goede | e907df3 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 183 | out_status: |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 184 | mutex_unlock(&wdd->lock); |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 185 | return err; |
| 186 | } |
| 187 | |
| 188 | /* |
| 189 | * watchdog_set_timeout: set the watchdog timer timeout |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 190 | * @wdd: the watchdog device to set the timeout for |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 191 | * @timeout: timeout to set in seconds |
| 192 | */ |
| 193 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 194 | static int watchdog_set_timeout(struct watchdog_device *wdd, |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 195 | unsigned int timeout) |
| 196 | { |
| 197 | int err; |
| 198 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 199 | if (!wdd->ops->set_timeout || !(wdd->info->options & WDIOF_SETTIMEOUT)) |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 200 | return -EOPNOTSUPP; |
| 201 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 202 | if (watchdog_timeout_invalid(wdd, timeout)) |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 203 | return -EINVAL; |
| 204 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 205 | mutex_lock(&wdd->lock); |
Hans de Goede | f4e9c82 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 206 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 207 | if (test_bit(WDOG_UNREGISTERED, &wdd->status)) { |
Hans de Goede | e907df3 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 208 | err = -ENODEV; |
| 209 | goto out_timeout; |
| 210 | } |
| 211 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 212 | err = wdd->ops->set_timeout(wdd, timeout); |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 213 | |
Hans de Goede | e907df3 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 214 | out_timeout: |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 215 | mutex_unlock(&wdd->lock); |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 216 | return err; |
| 217 | } |
| 218 | |
| 219 | /* |
| 220 | * watchdog_get_timeleft: wrapper to get the time left before a reboot |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 221 | * @wdd: the watchdog device to get the remaining time from |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 222 | * @timeleft: the time that's left |
| 223 | * |
| 224 | * Get the time before a watchdog will reboot (if not pinged). |
| 225 | */ |
| 226 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 227 | static int watchdog_get_timeleft(struct watchdog_device *wdd, |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 228 | unsigned int *timeleft) |
| 229 | { |
| 230 | int err = 0; |
| 231 | |
| 232 | *timeleft = 0; |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 233 | if (!wdd->ops->get_timeleft) |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 234 | return -EOPNOTSUPP; |
| 235 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 236 | mutex_lock(&wdd->lock); |
Hans de Goede | f4e9c82 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 237 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 238 | if (test_bit(WDOG_UNREGISTERED, &wdd->status)) { |
Hans de Goede | e907df3 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 239 | err = -ENODEV; |
| 240 | goto out_timeleft; |
| 241 | } |
| 242 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 243 | *timeleft = wdd->ops->get_timeleft(wdd); |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 244 | |
Hans de Goede | e907df3 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 245 | out_timeleft: |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 246 | mutex_unlock(&wdd->lock); |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 247 | return err; |
| 248 | } |
| 249 | |
Pratyush Anand | 33b7112 | 2015-12-17 17:53:59 +0530 | [diff] [blame^] | 250 | #ifdef CONFIG_WATCHDOG_SYSFS |
| 251 | static ssize_t nowayout_show(struct device *dev, struct device_attribute *attr, |
| 252 | char *buf) |
| 253 | { |
| 254 | struct watchdog_device *wdd = dev_get_drvdata(dev); |
| 255 | |
| 256 | return sprintf(buf, "%d\n", !!test_bit(WDOG_NO_WAY_OUT, &wdd->status)); |
| 257 | } |
| 258 | static DEVICE_ATTR_RO(nowayout); |
| 259 | |
| 260 | static ssize_t status_show(struct device *dev, struct device_attribute *attr, |
| 261 | char *buf) |
| 262 | { |
| 263 | struct watchdog_device *wdd = dev_get_drvdata(dev); |
| 264 | ssize_t status; |
| 265 | unsigned int val; |
| 266 | |
| 267 | status = watchdog_get_status(wdd, &val); |
| 268 | if (!status) |
| 269 | status = sprintf(buf, "%u\n", val); |
| 270 | |
| 271 | return status; |
| 272 | } |
| 273 | static DEVICE_ATTR_RO(status); |
| 274 | |
| 275 | static ssize_t bootstatus_show(struct device *dev, |
| 276 | struct device_attribute *attr, char *buf) |
| 277 | { |
| 278 | struct watchdog_device *wdd = dev_get_drvdata(dev); |
| 279 | |
| 280 | return sprintf(buf, "%u\n", wdd->bootstatus); |
| 281 | } |
| 282 | static DEVICE_ATTR_RO(bootstatus); |
| 283 | |
| 284 | static ssize_t timeleft_show(struct device *dev, struct device_attribute *attr, |
| 285 | char *buf) |
| 286 | { |
| 287 | struct watchdog_device *wdd = dev_get_drvdata(dev); |
| 288 | ssize_t status; |
| 289 | unsigned int val; |
| 290 | |
| 291 | status = watchdog_get_timeleft(wdd, &val); |
| 292 | if (!status) |
| 293 | status = sprintf(buf, "%u\n", val); |
| 294 | |
| 295 | return status; |
| 296 | } |
| 297 | static DEVICE_ATTR_RO(timeleft); |
| 298 | |
| 299 | static ssize_t timeout_show(struct device *dev, struct device_attribute *attr, |
| 300 | char *buf) |
| 301 | { |
| 302 | struct watchdog_device *wdd = dev_get_drvdata(dev); |
| 303 | |
| 304 | return sprintf(buf, "%u\n", wdd->timeout); |
| 305 | } |
| 306 | static DEVICE_ATTR_RO(timeout); |
| 307 | |
| 308 | static ssize_t identity_show(struct device *dev, struct device_attribute *attr, |
| 309 | char *buf) |
| 310 | { |
| 311 | struct watchdog_device *wdd = dev_get_drvdata(dev); |
| 312 | |
| 313 | return sprintf(buf, "%s\n", wdd->info->identity); |
| 314 | } |
| 315 | static DEVICE_ATTR_RO(identity); |
| 316 | |
| 317 | static ssize_t state_show(struct device *dev, struct device_attribute *attr, |
| 318 | char *buf) |
| 319 | { |
| 320 | struct watchdog_device *wdd = dev_get_drvdata(dev); |
| 321 | |
| 322 | if (watchdog_active(wdd)) |
| 323 | return sprintf(buf, "active\n"); |
| 324 | |
| 325 | return sprintf(buf, "inactive\n"); |
| 326 | } |
| 327 | static DEVICE_ATTR_RO(state); |
| 328 | |
| 329 | static umode_t wdt_is_visible(struct kobject *kobj, struct attribute *attr, |
| 330 | int n) |
| 331 | { |
| 332 | struct device *dev = container_of(kobj, struct device, kobj); |
| 333 | struct watchdog_device *wdd = dev_get_drvdata(dev); |
| 334 | umode_t mode = attr->mode; |
| 335 | |
| 336 | if (attr == &dev_attr_status.attr && !wdd->ops->status) |
| 337 | mode = 0; |
| 338 | else if (attr == &dev_attr_timeleft.attr && !wdd->ops->get_timeleft) |
| 339 | mode = 0; |
| 340 | |
| 341 | return mode; |
| 342 | } |
| 343 | static struct attribute *wdt_attrs[] = { |
| 344 | &dev_attr_state.attr, |
| 345 | &dev_attr_identity.attr, |
| 346 | &dev_attr_timeout.attr, |
| 347 | &dev_attr_timeleft.attr, |
| 348 | &dev_attr_bootstatus.attr, |
| 349 | &dev_attr_status.attr, |
| 350 | &dev_attr_nowayout.attr, |
| 351 | NULL, |
| 352 | }; |
| 353 | |
| 354 | static const struct attribute_group wdt_group = { |
| 355 | .attrs = wdt_attrs, |
| 356 | .is_visible = wdt_is_visible, |
| 357 | }; |
| 358 | __ATTRIBUTE_GROUPS(wdt); |
| 359 | #else |
| 360 | #define wdt_groups NULL |
| 361 | #endif |
| 362 | |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 363 | /* |
| 364 | * watchdog_ioctl_op: call the watchdog drivers ioctl op if defined |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 365 | * @wdd: the watchdog device to do the ioctl on |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 366 | * @cmd: watchdog command |
| 367 | * @arg: argument pointer |
| 368 | */ |
| 369 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 370 | static int watchdog_ioctl_op(struct watchdog_device *wdd, unsigned int cmd, |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 371 | unsigned long arg) |
| 372 | { |
| 373 | int err; |
| 374 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 375 | if (!wdd->ops->ioctl) |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 376 | return -ENOIOCTLCMD; |
| 377 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 378 | mutex_lock(&wdd->lock); |
Hans de Goede | f4e9c82 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 379 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 380 | if (test_bit(WDOG_UNREGISTERED, &wdd->status)) { |
Hans de Goede | e907df3 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 381 | err = -ENODEV; |
| 382 | goto out_ioctl; |
| 383 | } |
| 384 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 385 | err = wdd->ops->ioctl(wdd, cmd, arg); |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 386 | |
Hans de Goede | e907df3 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 387 | out_ioctl: |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 388 | mutex_unlock(&wdd->lock); |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 389 | return err; |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | /* |
| 393 | * watchdog_write: writes to the watchdog. |
| 394 | * @file: file from VFS |
| 395 | * @data: user address of data |
| 396 | * @len: length of data |
| 397 | * @ppos: pointer to the file offset |
| 398 | * |
| 399 | * 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] | 400 | * Writing the magic 'V' sequence allows the next close to turn |
Wim Van Sebroeck | 7e192b9 | 2011-07-22 18:59:17 +0000 | [diff] [blame] | 401 | * off the watchdog (if 'nowayout' is not set). |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 402 | */ |
| 403 | |
| 404 | static ssize_t watchdog_write(struct file *file, const char __user *data, |
| 405 | size_t len, loff_t *ppos) |
| 406 | { |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 407 | struct watchdog_device *wdd = file->private_data; |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 408 | size_t i; |
| 409 | char c; |
Alexander Usyskin | 5ef7966 | 2015-10-26 14:07:58 +0200 | [diff] [blame] | 410 | int err; |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 411 | |
| 412 | if (len == 0) |
| 413 | return 0; |
| 414 | |
Wim Van Sebroeck | 017cf08 | 2011-07-22 18:58:54 +0000 | [diff] [blame] | 415 | /* |
| 416 | * Note: just in case someone wrote the magic character |
| 417 | * five months ago... |
| 418 | */ |
| 419 | clear_bit(WDOG_ALLOW_RELEASE, &wdd->status); |
| 420 | |
| 421 | /* scan to see whether or not we got the magic character */ |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 422 | for (i = 0; i != len; i++) { |
| 423 | if (get_user(c, data + i)) |
| 424 | return -EFAULT; |
Wim Van Sebroeck | 017cf08 | 2011-07-22 18:58:54 +0000 | [diff] [blame] | 425 | if (c == 'V') |
| 426 | set_bit(WDOG_ALLOW_RELEASE, &wdd->status); |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | /* someone wrote to us, so we send the watchdog a keepalive ping */ |
Alexander Usyskin | 5ef7966 | 2015-10-26 14:07:58 +0200 | [diff] [blame] | 430 | err = watchdog_ping(wdd); |
| 431 | if (err < 0) |
| 432 | return err; |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 433 | |
| 434 | return len; |
| 435 | } |
| 436 | |
| 437 | /* |
Wim Van Sebroeck | 2fa0356 | 2011-07-22 18:56:38 +0000 | [diff] [blame] | 438 | * watchdog_ioctl: handle the different ioctl's for the watchdog device. |
| 439 | * @file: file handle to the device |
| 440 | * @cmd: watchdog command |
| 441 | * @arg: argument pointer |
| 442 | * |
| 443 | * The watchdog API defines a common set of functions for all watchdogs |
| 444 | * according to their available features. |
| 445 | */ |
| 446 | |
| 447 | static long watchdog_ioctl(struct file *file, unsigned int cmd, |
| 448 | unsigned long arg) |
| 449 | { |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 450 | struct watchdog_device *wdd = file->private_data; |
Wim Van Sebroeck | 2fa0356 | 2011-07-22 18:56:38 +0000 | [diff] [blame] | 451 | void __user *argp = (void __user *)arg; |
| 452 | int __user *p = argp; |
| 453 | unsigned int val; |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 454 | int err; |
Wim Van Sebroeck | 2fa0356 | 2011-07-22 18:56:38 +0000 | [diff] [blame] | 455 | |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 456 | err = watchdog_ioctl_op(wdd, cmd, arg); |
| 457 | if (err != -ENOIOCTLCMD) |
| 458 | return err; |
Wim Van Sebroeck | 78d88fc | 2011-07-22 18:59:49 +0000 | [diff] [blame] | 459 | |
Wim Van Sebroeck | 2fa0356 | 2011-07-22 18:56:38 +0000 | [diff] [blame] | 460 | switch (cmd) { |
| 461 | case WDIOC_GETSUPPORT: |
| 462 | return copy_to_user(argp, wdd->info, |
| 463 | sizeof(struct watchdog_info)) ? -EFAULT : 0; |
| 464 | case WDIOC_GETSTATUS: |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 465 | err = watchdog_get_status(wdd, &val); |
Wim Van Sebroeck | 8b9468d | 2012-06-26 20:07:21 +0200 | [diff] [blame] | 466 | if (err == -ENODEV) |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 467 | return err; |
Wim Van Sebroeck | 2fa0356 | 2011-07-22 18:56:38 +0000 | [diff] [blame] | 468 | return put_user(val, p); |
| 469 | case WDIOC_GETBOOTSTATUS: |
| 470 | return put_user(wdd->bootstatus, p); |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 471 | case WDIOC_SETOPTIONS: |
| 472 | if (get_user(val, p)) |
| 473 | return -EFAULT; |
| 474 | if (val & WDIOS_DISABLECARD) { |
| 475 | err = watchdog_stop(wdd); |
| 476 | if (err < 0) |
| 477 | return err; |
| 478 | } |
| 479 | if (val & WDIOS_ENABLECARD) { |
| 480 | err = watchdog_start(wdd); |
| 481 | if (err < 0) |
| 482 | return err; |
| 483 | } |
| 484 | return 0; |
Wim Van Sebroeck | c2dc00e | 2011-07-22 18:57:23 +0000 | [diff] [blame] | 485 | case WDIOC_KEEPALIVE: |
| 486 | if (!(wdd->info->options & WDIOF_KEEPALIVEPING)) |
| 487 | return -EOPNOTSUPP; |
Alexander Usyskin | 5ef7966 | 2015-10-26 14:07:58 +0200 | [diff] [blame] | 488 | return watchdog_ping(wdd); |
Wim Van Sebroeck | 014d694 | 2011-07-22 18:58:21 +0000 | [diff] [blame] | 489 | case WDIOC_SETTIMEOUT: |
Wim Van Sebroeck | 014d694 | 2011-07-22 18:58:21 +0000 | [diff] [blame] | 490 | if (get_user(val, p)) |
| 491 | return -EFAULT; |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 492 | err = watchdog_set_timeout(wdd, val); |
Wim Van Sebroeck | 014d694 | 2011-07-22 18:58:21 +0000 | [diff] [blame] | 493 | if (err < 0) |
| 494 | return err; |
Wim Van Sebroeck | 014d694 | 2011-07-22 18:58:21 +0000 | [diff] [blame] | 495 | /* If the watchdog is active then we send a keepalive ping |
| 496 | * to make sure that the watchdog keep's running (and if |
| 497 | * possible that it takes the new timeout) */ |
Alexander Usyskin | 5ef7966 | 2015-10-26 14:07:58 +0200 | [diff] [blame] | 498 | err = watchdog_ping(wdd); |
| 499 | if (err < 0) |
| 500 | return err; |
Wim Van Sebroeck | 014d694 | 2011-07-22 18:58:21 +0000 | [diff] [blame] | 501 | /* Fall */ |
| 502 | case WDIOC_GETTIMEOUT: |
| 503 | /* timeout == 0 means that we don't know the timeout */ |
| 504 | if (wdd->timeout == 0) |
| 505 | return -EOPNOTSUPP; |
| 506 | return put_user(wdd->timeout, p); |
Viresh Kumar | fd7b673 | 2012-03-16 09:14:00 +0100 | [diff] [blame] | 507 | case WDIOC_GETTIMELEFT: |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 508 | err = watchdog_get_timeleft(wdd, &val); |
| 509 | if (err) |
| 510 | return err; |
| 511 | return put_user(val, p); |
Wim Van Sebroeck | 2fa0356 | 2011-07-22 18:56:38 +0000 | [diff] [blame] | 512 | default: |
| 513 | return -ENOTTY; |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | /* |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 518 | * watchdog_open: open the /dev/watchdog* devices. |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 519 | * @inode: inode of device |
| 520 | * @file: file handle to device |
| 521 | * |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 522 | * When the /dev/watchdog* device gets opened, we start the watchdog. |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 523 | * Watch out: the /dev/watchdog device is single open, so we make sure |
| 524 | * it can only be opened once. |
| 525 | */ |
| 526 | |
| 527 | static int watchdog_open(struct inode *inode, struct file *file) |
| 528 | { |
| 529 | int err = -EBUSY; |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 530 | struct watchdog_device *wdd; |
| 531 | |
| 532 | /* Get the corresponding watchdog device */ |
| 533 | if (imajor(inode) == MISC_MAJOR) |
| 534 | wdd = old_wdd; |
| 535 | else |
| 536 | wdd = container_of(inode->i_cdev, struct watchdog_device, cdev); |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 537 | |
| 538 | /* the watchdog is single open! */ |
| 539 | if (test_and_set_bit(WDOG_DEV_OPEN, &wdd->status)) |
| 540 | return -EBUSY; |
| 541 | |
| 542 | /* |
| 543 | * If the /dev/watchdog device is open, we don't want the module |
| 544 | * to be unloaded. |
| 545 | */ |
| 546 | if (!try_module_get(wdd->ops->owner)) |
| 547 | goto out; |
| 548 | |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 549 | err = watchdog_start(wdd); |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 550 | if (err < 0) |
| 551 | goto out_mod; |
| 552 | |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 553 | file->private_data = wdd; |
| 554 | |
Hans de Goede | e907df3 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 555 | if (wdd->ops->ref) |
| 556 | wdd->ops->ref(wdd); |
| 557 | |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 558 | /* dev/watchdog is a virtual (and thus non-seekable) filesystem */ |
| 559 | return nonseekable_open(inode, file); |
| 560 | |
| 561 | out_mod: |
| 562 | module_put(wdd->ops->owner); |
| 563 | out: |
| 564 | clear_bit(WDOG_DEV_OPEN, &wdd->status); |
| 565 | return err; |
| 566 | } |
| 567 | |
| 568 | /* |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 569 | * watchdog_release: release the watchdog device. |
| 570 | * @inode: inode of device |
| 571 | * @file: file handle to device |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 572 | * |
Wim Van Sebroeck | 017cf08 | 2011-07-22 18:58:54 +0000 | [diff] [blame] | 573 | * 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] | 574 | * stop the watchdog when we have received the magic char (and nowayout |
| 575 | * was not set), else the watchdog will keep running. |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 576 | */ |
| 577 | |
| 578 | static int watchdog_release(struct inode *inode, struct file *file) |
| 579 | { |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 580 | struct watchdog_device *wdd = file->private_data; |
Wim Van Sebroeck | 017cf08 | 2011-07-22 18:58:54 +0000 | [diff] [blame] | 581 | int err = -EBUSY; |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 582 | |
Wim Van Sebroeck | 017cf08 | 2011-07-22 18:58:54 +0000 | [diff] [blame] | 583 | /* |
| 584 | * We only stop the watchdog if we received the magic character |
Wim Van Sebroeck | 7e192b9 | 2011-07-22 18:59:17 +0000 | [diff] [blame] | 585 | * or if WDIOF_MAGICCLOSE is not set. If nowayout was set then |
| 586 | * watchdog_stop will fail. |
Wim Van Sebroeck | 017cf08 | 2011-07-22 18:58:54 +0000 | [diff] [blame] | 587 | */ |
Hector Palacios | fcf9567 | 2013-04-08 17:06:32 +0200 | [diff] [blame] | 588 | if (!test_bit(WDOG_ACTIVE, &wdd->status)) |
| 589 | err = 0; |
| 590 | else if (test_and_clear_bit(WDOG_ALLOW_RELEASE, &wdd->status) || |
| 591 | !(wdd->info->options & WDIOF_MAGICCLOSE)) |
Wim Van Sebroeck | 017cf08 | 2011-07-22 18:58:54 +0000 | [diff] [blame] | 592 | err = watchdog_stop(wdd); |
| 593 | |
| 594 | /* If the watchdog was not stopped, send a keepalive ping */ |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 595 | if (err < 0) { |
Hans de Goede | e907df3 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 596 | mutex_lock(&wdd->lock); |
| 597 | if (!test_bit(WDOG_UNREGISTERED, &wdd->status)) |
| 598 | dev_crit(wdd->dev, "watchdog did not stop!\n"); |
| 599 | mutex_unlock(&wdd->lock); |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 600 | watchdog_ping(wdd); |
| 601 | } |
| 602 | |
| 603 | /* Allow the owner module to be unloaded again */ |
| 604 | module_put(wdd->ops->owner); |
| 605 | |
| 606 | /* make sure that /dev/watchdog can be re-opened */ |
| 607 | clear_bit(WDOG_DEV_OPEN, &wdd->status); |
| 608 | |
Hans de Goede | e907df3 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 609 | /* Note wdd may be gone after this, do not use after this! */ |
| 610 | if (wdd->ops->unref) |
| 611 | wdd->ops->unref(wdd); |
| 612 | |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 613 | return 0; |
| 614 | } |
| 615 | |
| 616 | static const struct file_operations watchdog_fops = { |
| 617 | .owner = THIS_MODULE, |
| 618 | .write = watchdog_write, |
Wim Van Sebroeck | 2fa0356 | 2011-07-22 18:56:38 +0000 | [diff] [blame] | 619 | .unlocked_ioctl = watchdog_ioctl, |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 620 | .open = watchdog_open, |
| 621 | .release = watchdog_release, |
| 622 | }; |
| 623 | |
| 624 | static struct miscdevice watchdog_miscdev = { |
| 625 | .minor = WATCHDOG_MINOR, |
| 626 | .name = "watchdog", |
| 627 | .fops = &watchdog_fops, |
| 628 | }; |
| 629 | |
| 630 | /* |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 631 | * watchdog_dev_register: register a watchdog device |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 632 | * @wdd: watchdog device |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 633 | * |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 634 | * Register a watchdog device including handling the legacy |
| 635 | * /dev/watchdog node. /dev/watchdog is actually a miscdevice and |
| 636 | * thus we set it up like that. |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 637 | */ |
| 638 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 639 | int watchdog_dev_register(struct watchdog_device *wdd) |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 640 | { |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 641 | int err, devno; |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 642 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 643 | if (wdd->id == 0) { |
| 644 | old_wdd = wdd; |
| 645 | watchdog_miscdev.parent = wdd->parent; |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 646 | err = misc_register(&watchdog_miscdev); |
| 647 | if (err != 0) { |
| 648 | pr_err("%s: cannot register miscdev on minor=%d (err=%d).\n", |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 649 | wdd->info->identity, WATCHDOG_MINOR, err); |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 650 | if (err == -EBUSY) |
| 651 | pr_err("%s: a legacy watchdog module is probably present.\n", |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 652 | wdd->info->identity); |
Guenter Roeck | 60403f7 | 2013-04-05 21:22:43 -0700 | [diff] [blame] | 653 | old_wdd = NULL; |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 654 | return err; |
| 655 | } |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 656 | } |
| 657 | |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 658 | /* Fill in the data structures */ |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 659 | devno = MKDEV(MAJOR(watchdog_devt), wdd->id); |
| 660 | cdev_init(&wdd->cdev, &watchdog_fops); |
| 661 | wdd->cdev.owner = wdd->ops->owner; |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 662 | |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 663 | /* Add the device */ |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 664 | err = cdev_add(&wdd->cdev, devno, 1); |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 665 | if (err) { |
| 666 | pr_err("watchdog%d unable to add device %d:%d\n", |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 667 | wdd->id, MAJOR(watchdog_devt), wdd->id); |
| 668 | if (wdd->id == 0) { |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 669 | misc_deregister(&watchdog_miscdev); |
| 670 | old_wdd = NULL; |
| 671 | } |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 672 | } |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 673 | return err; |
| 674 | } |
| 675 | |
| 676 | /* |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 677 | * watchdog_dev_unregister: unregister a watchdog device |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 678 | * @watchdog: watchdog device |
| 679 | * |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 680 | * Unregister the watchdog and if needed the legacy /dev/watchdog device. |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 681 | */ |
| 682 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 683 | int watchdog_dev_unregister(struct watchdog_device *wdd) |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 684 | { |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 685 | mutex_lock(&wdd->lock); |
| 686 | set_bit(WDOG_UNREGISTERED, &wdd->status); |
| 687 | mutex_unlock(&wdd->lock); |
Hans de Goede | e907df3 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 688 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 689 | cdev_del(&wdd->cdev); |
| 690 | if (wdd->id == 0) { |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 691 | misc_deregister(&watchdog_miscdev); |
| 692 | old_wdd = NULL; |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 693 | } |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 694 | return 0; |
| 695 | } |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 696 | |
Pratyush Anand | 906d7a5 | 2015-12-17 17:53:58 +0530 | [diff] [blame] | 697 | static struct class watchdog_class = { |
| 698 | .name = "watchdog", |
| 699 | .owner = THIS_MODULE, |
Pratyush Anand | 33b7112 | 2015-12-17 17:53:59 +0530 | [diff] [blame^] | 700 | .dev_groups = wdt_groups, |
Pratyush Anand | 906d7a5 | 2015-12-17 17:53:58 +0530 | [diff] [blame] | 701 | }; |
| 702 | |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 703 | /* |
| 704 | * watchdog_dev_init: init dev part of watchdog core |
| 705 | * |
| 706 | * Allocate a range of chardev nodes to use for watchdog devices |
| 707 | */ |
| 708 | |
Pratyush Anand | 906d7a5 | 2015-12-17 17:53:58 +0530 | [diff] [blame] | 709 | struct class * __init watchdog_dev_init(void) |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 710 | { |
Pratyush Anand | 906d7a5 | 2015-12-17 17:53:58 +0530 | [diff] [blame] | 711 | int err; |
| 712 | |
| 713 | err = class_register(&watchdog_class); |
| 714 | if (err < 0) { |
| 715 | pr_err("couldn't register class\n"); |
| 716 | return ERR_PTR(err); |
| 717 | } |
| 718 | |
| 719 | err = alloc_chrdev_region(&watchdog_devt, 0, MAX_DOGS, "watchdog"); |
| 720 | if (err < 0) { |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 721 | pr_err("watchdog: unable to allocate char dev region\n"); |
Pratyush Anand | 906d7a5 | 2015-12-17 17:53:58 +0530 | [diff] [blame] | 722 | class_unregister(&watchdog_class); |
| 723 | return ERR_PTR(err); |
| 724 | } |
| 725 | |
| 726 | return &watchdog_class; |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | /* |
| 730 | * watchdog_dev_exit: exit dev part of watchdog core |
| 731 | * |
| 732 | * Release the range of chardev nodes used for watchdog devices |
| 733 | */ |
| 734 | |
| 735 | void __exit watchdog_dev_exit(void) |
| 736 | { |
| 737 | unregister_chrdev_region(watchdog_devt, MAX_DOGS); |
Pratyush Anand | 906d7a5 | 2015-12-17 17:53:58 +0530 | [diff] [blame] | 738 | class_unregister(&watchdog_class); |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 739 | } |