Merge with rsync://rsync.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git
[linux-2.6/verdex.git] / drivers / char / watchdog / wdrtas.c
blob619e2ffca33ff4bd62f28a6b519fa63f189feeb1
1 /*
2 * FIXME: add wdrtas_get_status and wdrtas_get_boot_status as soon as
3 * RTAS calls are available
4 */
6 /*
7 * RTAS watchdog driver
9 * (C) Copyright IBM Corp. 2005
10 * device driver to exploit watchdog RTAS functions
12 * Authors : Utz Bacher <utz.bacher@de.ibm.com>
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.
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.
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.
29 #include <linux/config.h>
30 #include <linux/fs.h>
31 #include <linux/init.h>
32 #include <linux/kernel.h>
33 #include <linux/miscdevice.h>
34 #include <linux/module.h>
35 #include <linux/notifier.h>
36 #include <linux/reboot.h>
37 #include <linux/types.h>
38 #include <linux/watchdog.h>
40 #include <asm/rtas.h>
41 #include <asm/uaccess.h>
43 #define WDRTAS_MAGIC_CHAR 42
44 #define WDRTAS_SUPPORTED_MASK (WDIOF_SETTIMEOUT | \
45 WDIOF_MAGICCLOSE)
47 MODULE_AUTHOR("Utz Bacher <utz.bacher@de.ibm.com>");
48 MODULE_DESCRIPTION("RTAS watchdog driver");
49 MODULE_LICENSE("GPL");
50 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
51 MODULE_ALIAS_MISCDEV(TEMP_MINOR);
53 #ifdef CONFIG_WATCHDOG_NOWAYOUT
54 static int wdrtas_nowayout = 1;
55 #else
56 static int wdrtas_nowayout = 0;
57 #endif
59 static atomic_t wdrtas_miscdev_open = ATOMIC_INIT(0);
60 static char wdrtas_expect_close = 0;
62 static int wdrtas_interval;
64 #define WDRTAS_THERMAL_SENSOR 3
65 static int wdrtas_token_get_sensor_state;
66 #define WDRTAS_SURVEILLANCE_IND 9000
67 static int wdrtas_token_set_indicator;
68 #define WDRTAS_SP_SPI 28
69 static int wdrtas_token_get_sp;
70 static int wdrtas_token_event_scan;
72 #define WDRTAS_DEFAULT_INTERVAL 300
74 #define WDRTAS_LOGBUFFER_LEN 128
75 static char wdrtas_logbuffer[WDRTAS_LOGBUFFER_LEN];
78 /*** watchdog access functions */
80 /**
81 * wdrtas_set_interval - sets the watchdog interval
82 * @interval: new interval
84 * returns 0 on success, <0 on failures
86 * wdrtas_set_interval sets the watchdog keepalive interval by calling the
87 * RTAS function set-indicator (surveillance). The unit of interval is
88 * seconds.
90 static int
91 wdrtas_set_interval(int interval)
93 long result;
94 static int print_msg = 10;
96 /* rtas uses minutes */
97 interval = (interval + 59) / 60;
99 result = rtas_call(wdrtas_token_set_indicator, 3, 1, NULL,
100 WDRTAS_SURVEILLANCE_IND, 0, interval);
101 if ( (result < 0) && (print_msg) ) {
102 printk(KERN_ERR "wdrtas: setting the watchdog to %i "
103 "timeout failed: %li\n", interval, result);
104 print_msg--;
107 return result;
111 * wdrtas_get_interval - returns the current watchdog interval
112 * @fallback_value: value (in seconds) to use, if the RTAS call fails
114 * returns the interval
116 * wdrtas_get_interval returns the current watchdog keepalive interval
117 * as reported by the RTAS function ibm,get-system-parameter. The unit
118 * of the return value is seconds.
120 static int
121 wdrtas_get_interval(int fallback_value)
123 long result;
124 char value[4];
126 result = rtas_call(wdrtas_token_get_sp, 3, 1, NULL,
127 WDRTAS_SP_SPI, (void *)__pa(&value), 4);
128 if ( (value[0] != 0) || (value[1] != 2) || (value[3] != 0) ||
129 (result < 0) ) {
130 printk(KERN_WARNING "wdrtas: could not get sp_spi watchdog "
131 "timeout (%li). Continuing\n", result);
132 return fallback_value;
135 /* rtas uses minutes */
136 return ((int)value[2]) * 60;
140 * wdrtas_timer_start - starts watchdog
142 * wdrtas_timer_start starts the watchdog by calling the RTAS function
143 * set-interval (surveillance)
145 static void
146 wdrtas_timer_start(void)
148 wdrtas_set_interval(wdrtas_interval);
152 * wdrtas_timer_stop - stops watchdog
154 * wdrtas_timer_stop stops the watchdog timer by calling the RTAS function
155 * set-interval (surveillance)
157 static void
158 wdrtas_timer_stop(void)
160 wdrtas_set_interval(0);
164 * wdrtas_log_scanned_event - logs an event we received during keepalive
166 * wdrtas_log_scanned_event prints a message to the log buffer dumping
167 * the results of the last event-scan call
169 static void
170 wdrtas_log_scanned_event(void)
172 int i;
174 for (i = 0; i < WDRTAS_LOGBUFFER_LEN; i += 16)
175 printk(KERN_INFO "wdrtas: dumping event (line %i/%i), data = "
176 "%02x %02x %02x %02x %02x %02x %02x %02x "
177 "%02x %02x %02x %02x %02x %02x %02x %02x\n",
178 (i / 16) + 1, (WDRTAS_LOGBUFFER_LEN / 16),
179 wdrtas_logbuffer[i + 0], wdrtas_logbuffer[i + 1],
180 wdrtas_logbuffer[i + 2], wdrtas_logbuffer[i + 3],
181 wdrtas_logbuffer[i + 4], wdrtas_logbuffer[i + 5],
182 wdrtas_logbuffer[i + 6], wdrtas_logbuffer[i + 7],
183 wdrtas_logbuffer[i + 8], wdrtas_logbuffer[i + 9],
184 wdrtas_logbuffer[i + 10], wdrtas_logbuffer[i + 11],
185 wdrtas_logbuffer[i + 12], wdrtas_logbuffer[i + 13],
186 wdrtas_logbuffer[i + 14], wdrtas_logbuffer[i + 15]);
190 * wdrtas_timer_keepalive - resets watchdog timer to keep system alive
192 * wdrtas_timer_keepalive restarts the watchdog timer by calling the
193 * RTAS function event-scan and repeats these calls as long as there are
194 * events available. All events will be dumped.
196 static void
197 wdrtas_timer_keepalive(void)
199 long result;
201 do {
202 result = rtas_call(wdrtas_token_event_scan, 4, 1, NULL,
203 RTAS_EVENT_SCAN_ALL_EVENTS, 0,
204 (void *)__pa(wdrtas_logbuffer),
205 WDRTAS_LOGBUFFER_LEN);
206 if (result < 0)
207 printk(KERN_ERR "wdrtas: event-scan failed: %li\n",
208 result);
209 if (result == 0)
210 wdrtas_log_scanned_event();
211 } while (result == 0);
215 * wdrtas_get_temperature - returns current temperature
217 * returns temperature or <0 on failures
219 * wdrtas_get_temperature returns the current temperature in Fahrenheit. It
220 * uses the RTAS call get-sensor-state, token 3 to do so
222 static int
223 wdrtas_get_temperature(void)
225 long result;
226 int temperature = 0;
228 result = rtas_call(wdrtas_token_get_sensor_state, 2, 2,
229 (void *)__pa(&temperature),
230 WDRTAS_THERMAL_SENSOR, 0);
232 if (result < 0)
233 printk(KERN_WARNING "wdrtas: reading the thermal sensor "
234 "faild: %li\n", result);
235 else
236 temperature = ((temperature * 9) / 5) + 32; /* fahrenheit */
238 return temperature;
242 * wdrtas_get_status - returns the status of the watchdog
244 * returns a bitmask of defines WDIOF_... as defined in
245 * include/linux/watchdog.h
247 static int
248 wdrtas_get_status(void)
250 return 0; /* TODO */
254 * wdrtas_get_boot_status - returns the reason for the last boot
256 * returns a bitmask of defines WDIOF_... as defined in
257 * include/linux/watchdog.h, indicating why the watchdog rebooted the system
259 static int
260 wdrtas_get_boot_status(void)
262 return 0; /* TODO */
265 /*** watchdog API and operations stuff */
267 /* wdrtas_write - called when watchdog device is written to
268 * @file: file structure
269 * @buf: user buffer with data
270 * @len: amount to data written
271 * @ppos: position in file
273 * returns the number of successfully processed characters, which is always
274 * the number of bytes passed to this function
276 * wdrtas_write processes all the data given to it and looks for the magic
277 * character 'V'. This character allows the watchdog device to be closed
278 * properly.
280 static ssize_t
281 wdrtas_write(struct file *file, const char __user *buf,
282 size_t len, loff_t *ppos)
284 int i;
285 char c;
287 if (!len)
288 goto out;
290 if (!wdrtas_nowayout) {
291 wdrtas_expect_close = 0;
292 /* look for 'V' */
293 for (i = 0; i < len; i++) {
294 if (get_user(c, buf + i))
295 return -EFAULT;
296 /* allow to close device */
297 if (c == 'V')
298 wdrtas_expect_close = WDRTAS_MAGIC_CHAR;
302 wdrtas_timer_keepalive();
304 out:
305 return len;
309 * wdrtas_ioctl - ioctl function for the watchdog device
310 * @inode: inode structure
311 * @file: file structure
312 * @cmd: command for ioctl
313 * @arg: argument pointer
315 * returns 0 on success, <0 on failure
317 * wdrtas_ioctl implements the watchdog API ioctls
319 static int
320 wdrtas_ioctl(struct inode *inode, struct file *file,
321 unsigned int cmd, unsigned long arg)
323 int __user *argp = (void *)arg;
324 int i;
325 static struct watchdog_info wdinfo = {
326 .options = WDRTAS_SUPPORTED_MASK,
327 .firmware_version = 0,
328 .identity = "wdrtas"
331 switch (cmd) {
332 case WDIOC_GETSUPPORT:
333 if (copy_to_user(argp, &wdinfo, sizeof(wdinfo)))
334 return -EFAULT;
335 return 0;
337 case WDIOC_GETSTATUS:
338 i = wdrtas_get_status();
339 return put_user(i, argp);
341 case WDIOC_GETBOOTSTATUS:
342 i = wdrtas_get_boot_status();
343 return put_user(i, argp);
345 case WDIOC_GETTEMP:
346 if (wdrtas_token_get_sensor_state == RTAS_UNKNOWN_SERVICE)
347 return -EOPNOTSUPP;
349 i = wdrtas_get_temperature();
350 return put_user(i, argp);
352 case WDIOC_SETOPTIONS:
353 if (get_user(i, argp))
354 return -EFAULT;
355 if (i & WDIOS_DISABLECARD)
356 wdrtas_timer_stop();
357 if (i & WDIOS_ENABLECARD) {
358 wdrtas_timer_keepalive();
359 wdrtas_timer_start();
361 if (i & WDIOS_TEMPPANIC) {
362 /* not implemented. Done by H8 */
364 return 0;
366 case WDIOC_KEEPALIVE:
367 wdrtas_timer_keepalive();
368 return 0;
370 case WDIOC_SETTIMEOUT:
371 if (get_user(i, argp))
372 return -EFAULT;
374 if (wdrtas_set_interval(i))
375 return -EINVAL;
377 wdrtas_timer_keepalive();
379 if (wdrtas_token_get_sp == RTAS_UNKNOWN_SERVICE)
380 wdrtas_interval = i;
381 else
382 wdrtas_interval = wdrtas_get_interval(i);
383 /* fallthrough */
385 case WDIOC_GETTIMEOUT:
386 return put_user(wdrtas_interval, argp);
388 default:
389 return -ENOIOCTLCMD;
394 * wdrtas_open - open function of watchdog device
395 * @inode: inode structure
396 * @file: file structure
398 * returns 0 on success, -EBUSY if the file has been opened already, <0 on
399 * other failures
401 * function called when watchdog device is opened
403 static int
404 wdrtas_open(struct inode *inode, struct file *file)
406 /* only open once */
407 if (atomic_inc_return(&wdrtas_miscdev_open) > 1) {
408 atomic_dec(&wdrtas_miscdev_open);
409 return -EBUSY;
412 wdrtas_timer_start();
413 wdrtas_timer_keepalive();
415 return nonseekable_open(inode, file);
419 * wdrtas_close - close function of watchdog device
420 * @inode: inode structure
421 * @file: file structure
423 * returns 0 on success
425 * close function. Always succeeds
427 static int
428 wdrtas_close(struct inode *inode, struct file *file)
430 /* only stop watchdog, if this was announced using 'V' before */
431 if (wdrtas_expect_close == WDRTAS_MAGIC_CHAR)
432 wdrtas_timer_stop();
433 else {
434 printk(KERN_WARNING "wdrtas: got unexpected close. Watchdog "
435 "not stopped.\n");
436 wdrtas_timer_keepalive();
439 wdrtas_expect_close = 0;
440 atomic_dec(&wdrtas_miscdev_open);
441 return 0;
445 * wdrtas_temp_read - gives back the temperature in fahrenheit
446 * @file: file structure
447 * @buf: user buffer
448 * @count: number of bytes to be read
449 * @ppos: position in file
451 * returns always 1 or -EFAULT in case of user space copy failures, <0 on
452 * other failures
454 * wdrtas_temp_read gives the temperature to the users by copying this
455 * value as one byte into the user space buffer. The unit is Fahrenheit...
457 static ssize_t
458 wdrtas_temp_read(struct file *file, char __user *buf,
459 size_t count, loff_t *ppos)
461 int temperature = 0;
463 temperature = wdrtas_get_temperature();
464 if (temperature < 0)
465 return temperature;
467 if (copy_to_user(buf, &temperature, 1))
468 return -EFAULT;
470 return 1;
474 * wdrtas_temp_open - open function of temperature device
475 * @inode: inode structure
476 * @file: file structure
478 * returns 0 on success, <0 on failure
480 * function called when temperature device is opened
482 static int
483 wdrtas_temp_open(struct inode *inode, struct file *file)
485 return nonseekable_open(inode, file);
489 * wdrtas_temp_close - close function of temperature device
490 * @inode: inode structure
491 * @file: file structure
493 * returns 0 on success
495 * close function. Always succeeds
497 static int
498 wdrtas_temp_close(struct inode *inode, struct file *file)
500 return 0;
504 * wdrtas_reboot - reboot notifier function
505 * @nb: notifier block structure
506 * @code: reboot code
507 * @ptr: unused
509 * returns NOTIFY_DONE
511 * wdrtas_reboot stops the watchdog in case of a reboot
513 static int
514 wdrtas_reboot(struct notifier_block *this, unsigned long code, void *ptr)
516 if ( (code==SYS_DOWN) || (code==SYS_HALT) )
517 wdrtas_timer_stop();
519 return NOTIFY_DONE;
522 /*** initialization stuff */
524 static struct file_operations wdrtas_fops = {
525 .owner = THIS_MODULE,
526 .llseek = no_llseek,
527 .write = wdrtas_write,
528 .ioctl = wdrtas_ioctl,
529 .open = wdrtas_open,
530 .release = wdrtas_close,
533 static struct miscdevice wdrtas_miscdev = {
534 .minor = WATCHDOG_MINOR,
535 .name = "watchdog",
536 .fops = &wdrtas_fops,
539 static struct file_operations wdrtas_temp_fops = {
540 .owner = THIS_MODULE,
541 .llseek = no_llseek,
542 .read = wdrtas_temp_read,
543 .open = wdrtas_temp_open,
544 .release = wdrtas_temp_close,
547 static struct miscdevice wdrtas_tempdev = {
548 .minor = TEMP_MINOR,
549 .name = "temperature",
550 .fops = &wdrtas_temp_fops,
553 static struct notifier_block wdrtas_notifier = {
554 .notifier_call = wdrtas_reboot,
558 * wdrtas_get_tokens - reads in RTAS tokens
560 * returns 0 on succes, <0 on failure
562 * wdrtas_get_tokens reads in the tokens for the RTAS calls used in
563 * this watchdog driver. It tolerates, if "get-sensor-state" and
564 * "ibm,get-system-parameter" are not available.
566 static int
567 wdrtas_get_tokens(void)
569 wdrtas_token_get_sensor_state = rtas_token("get-sensor-state");
570 if (wdrtas_token_get_sensor_state == RTAS_UNKNOWN_SERVICE) {
571 printk(KERN_WARNING "wdrtas: couldn't get token for "
572 "get-sensor-state. Trying to continue without "
573 "temperature support.\n");
576 wdrtas_token_get_sp = rtas_token("ibm,get-system-parameter");
577 if (wdrtas_token_get_sp == RTAS_UNKNOWN_SERVICE) {
578 printk(KERN_WARNING "wdrtas: couldn't get token for "
579 "ibm,get-system-parameter. Trying to continue with "
580 "a default timeout value of %i seconds.\n",
581 WDRTAS_DEFAULT_INTERVAL);
584 wdrtas_token_set_indicator = rtas_token("set-indicator");
585 if (wdrtas_token_set_indicator == RTAS_UNKNOWN_SERVICE) {
586 printk(KERN_ERR "wdrtas: couldn't get token for "
587 "set-indicator. Terminating watchdog code.\n");
588 return -EIO;
591 wdrtas_token_event_scan = rtas_token("event-scan");
592 if (wdrtas_token_event_scan == RTAS_UNKNOWN_SERVICE) {
593 printk(KERN_ERR "wdrtas: couldn't get token for event-scan. "
594 "Terminating watchdog code.\n");
595 return -EIO;
598 return 0;
602 * wdrtas_unregister_devs - unregisters the misc dev handlers
604 * wdrtas_register_devs unregisters the watchdog and temperature watchdog
605 * misc devs
607 static void
608 wdrtas_unregister_devs(void)
610 misc_deregister(&wdrtas_miscdev);
611 if (wdrtas_token_get_sensor_state != RTAS_UNKNOWN_SERVICE)
612 misc_deregister(&wdrtas_tempdev);
616 * wdrtas_register_devs - registers the misc dev handlers
618 * returns 0 on succes, <0 on failure
620 * wdrtas_register_devs registers the watchdog and temperature watchdog
621 * misc devs
623 static int
624 wdrtas_register_devs(void)
626 int result;
628 result = misc_register(&wdrtas_miscdev);
629 if (result) {
630 printk(KERN_ERR "wdrtas: couldn't register watchdog misc "
631 "device. Terminating watchdog code.\n");
632 return result;
635 if (wdrtas_token_get_sensor_state != RTAS_UNKNOWN_SERVICE) {
636 result = misc_register(&wdrtas_tempdev);
637 if (result) {
638 printk(KERN_WARNING "wdrtas: couldn't register "
639 "watchdog temperature misc device. Continuing "
640 "without temperature support.\n");
641 wdrtas_token_get_sensor_state = RTAS_UNKNOWN_SERVICE;
645 return 0;
649 * wdrtas_init - init function of the watchdog driver
651 * returns 0 on succes, <0 on failure
653 * registers the file handlers and the reboot notifier
655 static int __init
656 wdrtas_init(void)
658 if (wdrtas_get_tokens())
659 return -ENODEV;
661 if (wdrtas_register_devs())
662 return -ENODEV;
664 if (register_reboot_notifier(&wdrtas_notifier)) {
665 printk(KERN_ERR "wdrtas: could not register reboot notifier. "
666 "Terminating watchdog code.\n");
667 wdrtas_unregister_devs();
668 return -ENODEV;
671 if (wdrtas_token_get_sp == RTAS_UNKNOWN_SERVICE)
672 wdrtas_interval = WDRTAS_DEFAULT_INTERVAL;
673 else
674 wdrtas_interval = wdrtas_get_interval(WDRTAS_DEFAULT_INTERVAL);
676 return 0;
680 * wdrtas_exit - exit function of the watchdog driver
682 * unregisters the file handlers and the reboot notifier
684 static void __exit
685 wdrtas_exit(void)
687 if (!wdrtas_nowayout)
688 wdrtas_timer_stop();
690 wdrtas_unregister_devs();
692 unregister_reboot_notifier(&wdrtas_notifier);
695 module_init(wdrtas_init);
696 module_exit(wdrtas_exit);