blob: 3045debd5411e26d818309fc3a24c349af0ade4f [file] [log] [blame]
Utz Bacher031f7ed2005-06-23 09:43:34 +10001/*
2 * FIXME: add wdrtas_get_status and wdrtas_get_boot_status as soon as
3 * RTAS calls are available
4 */
5
6/*
7 * RTAS watchdog driver
8 *
9 * (C) Copyright IBM Corp. 2005
10 * device driver to exploit watchdog RTAS functions
11 *
12 * Authors : Utz Bacher <utz.bacher@de.ibm.com>
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2, or (at your option)
17 * any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 */
28
Joe Perches27c766a2012-02-15 15:06:19 -080029#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30
Utz Bacher031f7ed2005-06-23 09:43:34 +100031#include <linux/fs.h>
32#include <linux/init.h>
33#include <linux/kernel.h>
34#include <linux/miscdevice.h>
35#include <linux/module.h>
36#include <linux/notifier.h>
37#include <linux/reboot.h>
38#include <linux/types.h>
39#include <linux/watchdog.h>
Alan Coxdae67a22008-05-19 14:09:45 +010040#include <linux/uaccess.h>
Utz Bacher031f7ed2005-06-23 09:43:34 +100041
42#include <asm/rtas.h>
Utz Bacher031f7ed2005-06-23 09:43:34 +100043
44#define WDRTAS_MAGIC_CHAR 42
45#define WDRTAS_SUPPORTED_MASK (WDIOF_SETTIMEOUT | \
46 WDIOF_MAGICCLOSE)
47
48MODULE_AUTHOR("Utz Bacher <utz.bacher@de.ibm.com>");
49MODULE_DESCRIPTION("RTAS watchdog driver");
50MODULE_LICENSE("GPL");
51MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
52MODULE_ALIAS_MISCDEV(TEMP_MINOR);
53
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010054static bool wdrtas_nowayout = WATCHDOG_NOWAYOUT;
Utz Bacher031f7ed2005-06-23 09:43:34 +100055static atomic_t wdrtas_miscdev_open = ATOMIC_INIT(0);
Alan Coxdae67a22008-05-19 14:09:45 +010056static char wdrtas_expect_close;
Utz Bacher031f7ed2005-06-23 09:43:34 +100057
58static int wdrtas_interval;
59
60#define WDRTAS_THERMAL_SENSOR 3
61static int wdrtas_token_get_sensor_state;
62#define WDRTAS_SURVEILLANCE_IND 9000
63static int wdrtas_token_set_indicator;
64#define WDRTAS_SP_SPI 28
65static int wdrtas_token_get_sp;
66static int wdrtas_token_event_scan;
67
68#define WDRTAS_DEFAULT_INTERVAL 300
69
70#define WDRTAS_LOGBUFFER_LEN 128
71static char wdrtas_logbuffer[WDRTAS_LOGBUFFER_LEN];
72
73
74/*** watchdog access functions */
75
76/**
77 * wdrtas_set_interval - sets the watchdog interval
78 * @interval: new interval
79 *
80 * returns 0 on success, <0 on failures
81 *
82 * wdrtas_set_interval sets the watchdog keepalive interval by calling the
83 * RTAS function set-indicator (surveillance). The unit of interval is
84 * seconds.
85 */
Alan Coxdae67a22008-05-19 14:09:45 +010086
87static int wdrtas_set_interval(int interval)
Utz Bacher031f7ed2005-06-23 09:43:34 +100088{
89 long result;
90 static int print_msg = 10;
91
92 /* rtas uses minutes */
93 interval = (interval + 59) / 60;
94
95 result = rtas_call(wdrtas_token_set_indicator, 3, 1, NULL,
96 WDRTAS_SURVEILLANCE_IND, 0, interval);
Alan Coxdae67a22008-05-19 14:09:45 +010097 if (result < 0 && print_msg) {
Joe Perches27c766a2012-02-15 15:06:19 -080098 pr_err("setting the watchdog to %i timeout failed: %li\n",
99 interval, result);
Utz Bacher031f7ed2005-06-23 09:43:34 +1000100 print_msg--;
101 }
102
103 return result;
104}
105
Mark Nelsonb6966b12009-03-23 20:30:41 +0000106#define WDRTAS_SP_SPI_LEN 4
107
Utz Bacher031f7ed2005-06-23 09:43:34 +1000108/**
109 * wdrtas_get_interval - returns the current watchdog interval
110 * @fallback_value: value (in seconds) to use, if the RTAS call fails
111 *
112 * returns the interval
113 *
114 * wdrtas_get_interval returns the current watchdog keepalive interval
115 * as reported by the RTAS function ibm,get-system-parameter. The unit
116 * of the return value is seconds.
117 */
Alan Coxdae67a22008-05-19 14:09:45 +0100118static int wdrtas_get_interval(int fallback_value)
Utz Bacher031f7ed2005-06-23 09:43:34 +1000119{
120 long result;
Mark Nelsonb6966b12009-03-23 20:30:41 +0000121 char value[WDRTAS_SP_SPI_LEN];
Utz Bacher031f7ed2005-06-23 09:43:34 +1000122
Mark Nelsonb6966b12009-03-23 20:30:41 +0000123 spin_lock(&rtas_data_buf_lock);
124 memset(rtas_data_buf, 0, WDRTAS_SP_SPI_LEN);
Utz Bacher031f7ed2005-06-23 09:43:34 +1000125 result = rtas_call(wdrtas_token_get_sp, 3, 1, NULL,
Mark Nelsonb6966b12009-03-23 20:30:41 +0000126 WDRTAS_SP_SPI, __pa(rtas_data_buf),
127 WDRTAS_SP_SPI_LEN);
128
129 memcpy(value, rtas_data_buf, WDRTAS_SP_SPI_LEN);
130 spin_unlock(&rtas_data_buf_lock);
131
Alan Coxdae67a22008-05-19 14:09:45 +0100132 if (value[0] != 0 || value[1] != 2 || value[3] != 0 || result < 0) {
Joe Perches27c766a2012-02-15 15:06:19 -0800133 pr_warn("could not get sp_spi watchdog timeout (%li). Continuing\n",
134 result);
Utz Bacher031f7ed2005-06-23 09:43:34 +1000135 return fallback_value;
136 }
137
138 /* rtas uses minutes */
139 return ((int)value[2]) * 60;
140}
141
142/**
143 * wdrtas_timer_start - starts watchdog
144 *
145 * wdrtas_timer_start starts the watchdog by calling the RTAS function
146 * set-interval (surveillance)
147 */
Alan Coxdae67a22008-05-19 14:09:45 +0100148static void wdrtas_timer_start(void)
Utz Bacher031f7ed2005-06-23 09:43:34 +1000149{
150 wdrtas_set_interval(wdrtas_interval);
151}
152
153/**
154 * wdrtas_timer_stop - stops watchdog
155 *
156 * wdrtas_timer_stop stops the watchdog timer by calling the RTAS function
157 * set-interval (surveillance)
158 */
Alan Coxdae67a22008-05-19 14:09:45 +0100159static void wdrtas_timer_stop(void)
Utz Bacher031f7ed2005-06-23 09:43:34 +1000160{
161 wdrtas_set_interval(0);
162}
163
164/**
Utz Bacher031f7ed2005-06-23 09:43:34 +1000165 * wdrtas_timer_keepalive - resets watchdog timer to keep system alive
166 *
167 * wdrtas_timer_keepalive restarts the watchdog timer by calling the
168 * RTAS function event-scan and repeats these calls as long as there are
169 * events available. All events will be dumped.
170 */
Alan Coxdae67a22008-05-19 14:09:45 +0100171static void wdrtas_timer_keepalive(void)
Utz Bacher031f7ed2005-06-23 09:43:34 +1000172{
173 long result;
174
175 do {
176 result = rtas_call(wdrtas_token_event_scan, 4, 1, NULL,
177 RTAS_EVENT_SCAN_ALL_EVENTS, 0,
178 (void *)__pa(wdrtas_logbuffer),
179 WDRTAS_LOGBUFFER_LEN);
180 if (result < 0)
Joe Perches27c766a2012-02-15 15:06:19 -0800181 pr_err("event-scan failed: %li\n", result);
Utz Bacher031f7ed2005-06-23 09:43:34 +1000182 if (result == 0)
Andy Shevchenko48388062013-05-28 12:25:49 +0300183 print_hex_dump(KERN_INFO, "dumping event, data: ",
184 DUMP_PREFIX_OFFSET, 16, 1,
185 wdrtas_logbuffer, WDRTAS_LOGBUFFER_LEN, false);
Utz Bacher031f7ed2005-06-23 09:43:34 +1000186 } while (result == 0);
187}
188
189/**
190 * wdrtas_get_temperature - returns current temperature
191 *
192 * returns temperature or <0 on failures
193 *
194 * wdrtas_get_temperature returns the current temperature in Fahrenheit. It
195 * uses the RTAS call get-sensor-state, token 3 to do so
196 */
Alan Coxdae67a22008-05-19 14:09:45 +0100197static int wdrtas_get_temperature(void)
Utz Bacher031f7ed2005-06-23 09:43:34 +1000198{
Adrian Reber5ba762c2009-03-26 02:05:42 +0000199 int result;
Utz Bacher031f7ed2005-06-23 09:43:34 +1000200 int temperature = 0;
201
Adrian Reber5ba762c2009-03-26 02:05:42 +0000202 result = rtas_get_sensor(WDRTAS_THERMAL_SENSOR, 0, &temperature);
Utz Bacher031f7ed2005-06-23 09:43:34 +1000203
204 if (result < 0)
Joe Perches27c766a2012-02-15 15:06:19 -0800205 pr_warn("reading the thermal sensor failed: %i\n", result);
Utz Bacher031f7ed2005-06-23 09:43:34 +1000206 else
207 temperature = ((temperature * 9) / 5) + 32; /* fahrenheit */
208
209 return temperature;
210}
211
212/**
213 * wdrtas_get_status - returns the status of the watchdog
214 *
215 * returns a bitmask of defines WDIOF_... as defined in
216 * include/linux/watchdog.h
217 */
Alan Coxdae67a22008-05-19 14:09:45 +0100218static int wdrtas_get_status(void)
Utz Bacher031f7ed2005-06-23 09:43:34 +1000219{
220 return 0; /* TODO */
221}
222
223/**
224 * wdrtas_get_boot_status - returns the reason for the last boot
225 *
226 * returns a bitmask of defines WDIOF_... as defined in
227 * include/linux/watchdog.h, indicating why the watchdog rebooted the system
228 */
Alan Coxdae67a22008-05-19 14:09:45 +0100229static int wdrtas_get_boot_status(void)
Utz Bacher031f7ed2005-06-23 09:43:34 +1000230{
231 return 0; /* TODO */
232}
233
234/*** watchdog API and operations stuff */
235
236/* wdrtas_write - called when watchdog device is written to
237 * @file: file structure
238 * @buf: user buffer with data
239 * @len: amount to data written
240 * @ppos: position in file
241 *
242 * returns the number of successfully processed characters, which is always
243 * the number of bytes passed to this function
244 *
245 * wdrtas_write processes all the data given to it and looks for the magic
246 * character 'V'. This character allows the watchdog device to be closed
247 * properly.
248 */
Alan Coxdae67a22008-05-19 14:09:45 +0100249static ssize_t wdrtas_write(struct file *file, const char __user *buf,
Utz Bacher031f7ed2005-06-23 09:43:34 +1000250 size_t len, loff_t *ppos)
251{
252 int i;
253 char c;
254
255 if (!len)
256 goto out;
257
258 if (!wdrtas_nowayout) {
259 wdrtas_expect_close = 0;
260 /* look for 'V' */
261 for (i = 0; i < len; i++) {
262 if (get_user(c, buf + i))
263 return -EFAULT;
264 /* allow to close device */
265 if (c == 'V')
266 wdrtas_expect_close = WDRTAS_MAGIC_CHAR;
267 }
268 }
269
270 wdrtas_timer_keepalive();
271
272out:
273 return len;
274}
275
276/**
277 * wdrtas_ioctl - ioctl function for the watchdog device
Utz Bacher031f7ed2005-06-23 09:43:34 +1000278 * @file: file structure
279 * @cmd: command for ioctl
280 * @arg: argument pointer
281 *
282 * returns 0 on success, <0 on failure
283 *
284 * wdrtas_ioctl implements the watchdog API ioctls
285 */
Alan Coxdae67a22008-05-19 14:09:45 +0100286
287static long wdrtas_ioctl(struct file *file, unsigned int cmd,
288 unsigned long arg)
Utz Bacher031f7ed2005-06-23 09:43:34 +1000289{
Al Viroe896fd92005-12-15 09:18:05 +0000290 int __user *argp = (void __user *)arg;
Utz Bacher031f7ed2005-06-23 09:43:34 +1000291 int i;
Wim Van Sebroeck42747d72009-12-26 18:55:22 +0000292 static const struct watchdog_info wdinfo = {
Utz Bacher031f7ed2005-06-23 09:43:34 +1000293 .options = WDRTAS_SUPPORTED_MASK,
294 .firmware_version = 0,
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000295 .identity = "wdrtas",
Utz Bacher031f7ed2005-06-23 09:43:34 +1000296 };
297
298 switch (cmd) {
299 case WDIOC_GETSUPPORT:
300 if (copy_to_user(argp, &wdinfo, sizeof(wdinfo)))
301 return -EFAULT;
302 return 0;
303
304 case WDIOC_GETSTATUS:
305 i = wdrtas_get_status();
306 return put_user(i, argp);
307
308 case WDIOC_GETBOOTSTATUS:
309 i = wdrtas_get_boot_status();
310 return put_user(i, argp);
311
312 case WDIOC_GETTEMP:
313 if (wdrtas_token_get_sensor_state == RTAS_UNKNOWN_SERVICE)
314 return -EOPNOTSUPP;
315
316 i = wdrtas_get_temperature();
317 return put_user(i, argp);
318
319 case WDIOC_SETOPTIONS:
320 if (get_user(i, argp))
321 return -EFAULT;
322 if (i & WDIOS_DISABLECARD)
323 wdrtas_timer_stop();
324 if (i & WDIOS_ENABLECARD) {
325 wdrtas_timer_keepalive();
326 wdrtas_timer_start();
327 }
Alan Coxdae67a22008-05-19 14:09:45 +0100328 /* not implemented. Done by H8
Utz Bacher031f7ed2005-06-23 09:43:34 +1000329 if (i & WDIOS_TEMPPANIC) {
Alan Coxdae67a22008-05-19 14:09:45 +0100330 } */
Utz Bacher031f7ed2005-06-23 09:43:34 +1000331 return 0;
332
333 case WDIOC_KEEPALIVE:
334 wdrtas_timer_keepalive();
335 return 0;
336
337 case WDIOC_SETTIMEOUT:
338 if (get_user(i, argp))
339 return -EFAULT;
340
341 if (wdrtas_set_interval(i))
342 return -EINVAL;
343
344 wdrtas_timer_keepalive();
345
346 if (wdrtas_token_get_sp == RTAS_UNKNOWN_SERVICE)
347 wdrtas_interval = i;
348 else
349 wdrtas_interval = wdrtas_get_interval(i);
350 /* fallthrough */
351
352 case WDIOC_GETTIMEOUT:
353 return put_user(wdrtas_interval, argp);
354
355 default:
Samuel Tardieu795b89d2006-09-09 17:34:31 +0200356 return -ENOTTY;
Utz Bacher031f7ed2005-06-23 09:43:34 +1000357 }
358}
359
360/**
361 * wdrtas_open - open function of watchdog device
362 * @inode: inode structure
363 * @file: file structure
364 *
365 * returns 0 on success, -EBUSY if the file has been opened already, <0 on
366 * other failures
367 *
368 * function called when watchdog device is opened
369 */
Alan Coxdae67a22008-05-19 14:09:45 +0100370static int wdrtas_open(struct inode *inode, struct file *file)
Utz Bacher031f7ed2005-06-23 09:43:34 +1000371{
372 /* only open once */
373 if (atomic_inc_return(&wdrtas_miscdev_open) > 1) {
374 atomic_dec(&wdrtas_miscdev_open);
375 return -EBUSY;
376 }
377
378 wdrtas_timer_start();
379 wdrtas_timer_keepalive();
380
381 return nonseekable_open(inode, file);
382}
383
384/**
385 * wdrtas_close - close function of watchdog device
386 * @inode: inode structure
387 * @file: file structure
388 *
389 * returns 0 on success
390 *
391 * close function. Always succeeds
392 */
Alan Coxdae67a22008-05-19 14:09:45 +0100393static int wdrtas_close(struct inode *inode, struct file *file)
Utz Bacher031f7ed2005-06-23 09:43:34 +1000394{
395 /* only stop watchdog, if this was announced using 'V' before */
396 if (wdrtas_expect_close == WDRTAS_MAGIC_CHAR)
397 wdrtas_timer_stop();
398 else {
Joe Perches27c766a2012-02-15 15:06:19 -0800399 pr_warn("got unexpected close. Watchdog not stopped.\n");
Utz Bacher031f7ed2005-06-23 09:43:34 +1000400 wdrtas_timer_keepalive();
401 }
402
403 wdrtas_expect_close = 0;
404 atomic_dec(&wdrtas_miscdev_open);
405 return 0;
406}
407
408/**
409 * wdrtas_temp_read - gives back the temperature in fahrenheit
410 * @file: file structure
411 * @buf: user buffer
412 * @count: number of bytes to be read
413 * @ppos: position in file
414 *
415 * returns always 1 or -EFAULT in case of user space copy failures, <0 on
416 * other failures
417 *
418 * wdrtas_temp_read gives the temperature to the users by copying this
419 * value as one byte into the user space buffer. The unit is Fahrenheit...
420 */
Alan Coxdae67a22008-05-19 14:09:45 +0100421static ssize_t wdrtas_temp_read(struct file *file, char __user *buf,
Utz Bacher031f7ed2005-06-23 09:43:34 +1000422 size_t count, loff_t *ppos)
423{
424 int temperature = 0;
425
426 temperature = wdrtas_get_temperature();
427 if (temperature < 0)
428 return temperature;
429
430 if (copy_to_user(buf, &temperature, 1))
431 return -EFAULT;
432
433 return 1;
434}
435
436/**
437 * wdrtas_temp_open - open function of temperature device
438 * @inode: inode structure
439 * @file: file structure
440 *
441 * returns 0 on success, <0 on failure
442 *
443 * function called when temperature device is opened
444 */
Alan Coxdae67a22008-05-19 14:09:45 +0100445static int wdrtas_temp_open(struct inode *inode, struct file *file)
Utz Bacher031f7ed2005-06-23 09:43:34 +1000446{
447 return nonseekable_open(inode, file);
448}
449
450/**
451 * wdrtas_temp_close - close function of temperature device
452 * @inode: inode structure
453 * @file: file structure
454 *
455 * returns 0 on success
456 *
457 * close function. Always succeeds
458 */
Alan Coxdae67a22008-05-19 14:09:45 +0100459static int wdrtas_temp_close(struct inode *inode, struct file *file)
Utz Bacher031f7ed2005-06-23 09:43:34 +1000460{
461 return 0;
462}
463
464/**
465 * wdrtas_reboot - reboot notifier function
466 * @nb: notifier block structure
467 * @code: reboot code
468 * @ptr: unused
469 *
470 * returns NOTIFY_DONE
471 *
472 * wdrtas_reboot stops the watchdog in case of a reboot
473 */
Alan Coxdae67a22008-05-19 14:09:45 +0100474static int wdrtas_reboot(struct notifier_block *this,
475 unsigned long code, void *ptr)
Utz Bacher031f7ed2005-06-23 09:43:34 +1000476{
Alan Coxdae67a22008-05-19 14:09:45 +0100477 if (code == SYS_DOWN || code == SYS_HALT)
Utz Bacher031f7ed2005-06-23 09:43:34 +1000478 wdrtas_timer_stop();
479
480 return NOTIFY_DONE;
481}
482
483/*** initialization stuff */
484
Arjan van de Ven62322d22006-07-03 00:24:21 -0700485static const struct file_operations wdrtas_fops = {
Utz Bacher031f7ed2005-06-23 09:43:34 +1000486 .owner = THIS_MODULE,
487 .llseek = no_llseek,
488 .write = wdrtas_write,
Alan Coxdae67a22008-05-19 14:09:45 +0100489 .unlocked_ioctl = wdrtas_ioctl,
Utz Bacher031f7ed2005-06-23 09:43:34 +1000490 .open = wdrtas_open,
491 .release = wdrtas_close,
492};
493
494static struct miscdevice wdrtas_miscdev = {
495 .minor = WATCHDOG_MINOR,
496 .name = "watchdog",
497 .fops = &wdrtas_fops,
498};
499
Arjan van de Ven62322d22006-07-03 00:24:21 -0700500static const struct file_operations wdrtas_temp_fops = {
Utz Bacher031f7ed2005-06-23 09:43:34 +1000501 .owner = THIS_MODULE,
502 .llseek = no_llseek,
503 .read = wdrtas_temp_read,
504 .open = wdrtas_temp_open,
505 .release = wdrtas_temp_close,
506};
507
508static struct miscdevice wdrtas_tempdev = {
509 .minor = TEMP_MINOR,
510 .name = "temperature",
511 .fops = &wdrtas_temp_fops,
512};
513
514static struct notifier_block wdrtas_notifier = {
515 .notifier_call = wdrtas_reboot,
516};
517
518/**
519 * wdrtas_get_tokens - reads in RTAS tokens
520 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200521 * returns 0 on success, <0 on failure
Utz Bacher031f7ed2005-06-23 09:43:34 +1000522 *
523 * wdrtas_get_tokens reads in the tokens for the RTAS calls used in
524 * this watchdog driver. It tolerates, if "get-sensor-state" and
525 * "ibm,get-system-parameter" are not available.
526 */
Alan Coxdae67a22008-05-19 14:09:45 +0100527static int wdrtas_get_tokens(void)
Utz Bacher031f7ed2005-06-23 09:43:34 +1000528{
529 wdrtas_token_get_sensor_state = rtas_token("get-sensor-state");
530 if (wdrtas_token_get_sensor_state == RTAS_UNKNOWN_SERVICE) {
Joe Perches27c766a2012-02-15 15:06:19 -0800531 pr_warn("couldn't get token for get-sensor-state. Trying to continue without temperature support.\n");
Utz Bacher031f7ed2005-06-23 09:43:34 +1000532 }
533
534 wdrtas_token_get_sp = rtas_token("ibm,get-system-parameter");
535 if (wdrtas_token_get_sp == RTAS_UNKNOWN_SERVICE) {
Joe Perches27c766a2012-02-15 15:06:19 -0800536 pr_warn("couldn't get token for ibm,get-system-parameter. Trying to continue with a default timeout value of %i seconds.\n",
537 WDRTAS_DEFAULT_INTERVAL);
Utz Bacher031f7ed2005-06-23 09:43:34 +1000538 }
539
540 wdrtas_token_set_indicator = rtas_token("set-indicator");
541 if (wdrtas_token_set_indicator == RTAS_UNKNOWN_SERVICE) {
Joe Perches27c766a2012-02-15 15:06:19 -0800542 pr_err("couldn't get token for set-indicator. Terminating watchdog code.\n");
Utz Bacher031f7ed2005-06-23 09:43:34 +1000543 return -EIO;
544 }
545
546 wdrtas_token_event_scan = rtas_token("event-scan");
547 if (wdrtas_token_event_scan == RTAS_UNKNOWN_SERVICE) {
Joe Perches27c766a2012-02-15 15:06:19 -0800548 pr_err("couldn't get token for event-scan. Terminating watchdog code.\n");
Utz Bacher031f7ed2005-06-23 09:43:34 +1000549 return -EIO;
550 }
551
552 return 0;
553}
554
555/**
556 * wdrtas_unregister_devs - unregisters the misc dev handlers
557 *
558 * wdrtas_register_devs unregisters the watchdog and temperature watchdog
559 * misc devs
560 */
Alan Coxdae67a22008-05-19 14:09:45 +0100561static void wdrtas_unregister_devs(void)
Utz Bacher031f7ed2005-06-23 09:43:34 +1000562{
563 misc_deregister(&wdrtas_miscdev);
564 if (wdrtas_token_get_sensor_state != RTAS_UNKNOWN_SERVICE)
565 misc_deregister(&wdrtas_tempdev);
566}
567
568/**
569 * wdrtas_register_devs - registers the misc dev handlers
570 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200571 * returns 0 on success, <0 on failure
Utz Bacher031f7ed2005-06-23 09:43:34 +1000572 *
573 * wdrtas_register_devs registers the watchdog and temperature watchdog
574 * misc devs
575 */
Alan Coxdae67a22008-05-19 14:09:45 +0100576static int wdrtas_register_devs(void)
Utz Bacher031f7ed2005-06-23 09:43:34 +1000577{
578 int result;
579
580 result = misc_register(&wdrtas_miscdev);
581 if (result) {
Joe Perches27c766a2012-02-15 15:06:19 -0800582 pr_err("couldn't register watchdog misc device. Terminating watchdog code.\n");
Utz Bacher031f7ed2005-06-23 09:43:34 +1000583 return result;
584 }
585
586 if (wdrtas_token_get_sensor_state != RTAS_UNKNOWN_SERVICE) {
587 result = misc_register(&wdrtas_tempdev);
588 if (result) {
Joe Perches27c766a2012-02-15 15:06:19 -0800589 pr_warn("couldn't register watchdog temperature misc device. Continuing without temperature support.\n");
Utz Bacher031f7ed2005-06-23 09:43:34 +1000590 wdrtas_token_get_sensor_state = RTAS_UNKNOWN_SERVICE;
591 }
592 }
593
594 return 0;
595}
596
597/**
598 * wdrtas_init - init function of the watchdog driver
599 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200600 * returns 0 on success, <0 on failure
Utz Bacher031f7ed2005-06-23 09:43:34 +1000601 *
602 * registers the file handlers and the reboot notifier
603 */
Alan Coxdae67a22008-05-19 14:09:45 +0100604static int __init wdrtas_init(void)
Utz Bacher031f7ed2005-06-23 09:43:34 +1000605{
606 if (wdrtas_get_tokens())
607 return -ENODEV;
608
609 if (wdrtas_register_devs())
610 return -ENODEV;
611
612 if (register_reboot_notifier(&wdrtas_notifier)) {
Joe Perches27c766a2012-02-15 15:06:19 -0800613 pr_err("could not register reboot notifier. Terminating watchdog code.\n");
Utz Bacher031f7ed2005-06-23 09:43:34 +1000614 wdrtas_unregister_devs();
615 return -ENODEV;
616 }
617
618 if (wdrtas_token_get_sp == RTAS_UNKNOWN_SERVICE)
619 wdrtas_interval = WDRTAS_DEFAULT_INTERVAL;
620 else
621 wdrtas_interval = wdrtas_get_interval(WDRTAS_DEFAULT_INTERVAL);
622
623 return 0;
624}
625
626/**
627 * wdrtas_exit - exit function of the watchdog driver
628 *
629 * unregisters the file handlers and the reboot notifier
630 */
Alan Coxdae67a22008-05-19 14:09:45 +0100631static void __exit wdrtas_exit(void)
Utz Bacher031f7ed2005-06-23 09:43:34 +1000632{
633 if (!wdrtas_nowayout)
634 wdrtas_timer_stop();
635
636 wdrtas_unregister_devs();
637
638 unregister_reboot_notifier(&wdrtas_notifier);
639}
640
641module_init(wdrtas_init);
642module_exit(wdrtas_exit);