📄 wdrtas.c
字号:
/* * FIXME: add wdrtas_get_status and wdrtas_get_boot_status as soon as * RTAS calls are available *//* * RTAS watchdog driver * * (C) Copyright IBM Corp. 2005 * device driver to exploit watchdog RTAS functions * * Authors : Utz Bacher <utz.bacher@de.ibm.com> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */#include <linux/config.h>#include <linux/fs.h>#include <linux/init.h>#include <linux/kernel.h>#include <linux/miscdevice.h>#include <linux/module.h>#include <linux/notifier.h>#include <linux/reboot.h>#include <linux/types.h>#include <linux/watchdog.h>#include <asm/rtas.h>#include <asm/uaccess.h>#define WDRTAS_MAGIC_CHAR 42#define WDRTAS_SUPPORTED_MASK (WDIOF_SETTIMEOUT | \ WDIOF_MAGICCLOSE)MODULE_AUTHOR("Utz Bacher <utz.bacher@de.ibm.com>");MODULE_DESCRIPTION("RTAS watchdog driver");MODULE_LICENSE("GPL");MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);MODULE_ALIAS_MISCDEV(TEMP_MINOR);#ifdef CONFIG_WATCHDOG_NOWAYOUTstatic int wdrtas_nowayout = 1;#elsestatic int wdrtas_nowayout = 0;#endifstatic atomic_t wdrtas_miscdev_open = ATOMIC_INIT(0);static char wdrtas_expect_close = 0;static int wdrtas_interval;#define WDRTAS_THERMAL_SENSOR 3static int wdrtas_token_get_sensor_state;#define WDRTAS_SURVEILLANCE_IND 9000static int wdrtas_token_set_indicator;#define WDRTAS_SP_SPI 28static int wdrtas_token_get_sp;static int wdrtas_token_event_scan;#define WDRTAS_DEFAULT_INTERVAL 300#define WDRTAS_LOGBUFFER_LEN 128static char wdrtas_logbuffer[WDRTAS_LOGBUFFER_LEN];/*** watchdog access functions *//** * wdrtas_set_interval - sets the watchdog interval * @interval: new interval * * returns 0 on success, <0 on failures * * wdrtas_set_interval sets the watchdog keepalive interval by calling the * RTAS function set-indicator (surveillance). The unit of interval is * seconds. */static intwdrtas_set_interval(int interval){ long result; static int print_msg = 10; /* rtas uses minutes */ interval = (interval + 59) / 60; result = rtas_call(wdrtas_token_set_indicator, 3, 1, NULL, WDRTAS_SURVEILLANCE_IND, 0, interval); if ( (result < 0) && (print_msg) ) { printk(KERN_ERR "wdrtas: setting the watchdog to %i " "timeout failed: %li\n", interval, result); print_msg--; } return result;}/** * wdrtas_get_interval - returns the current watchdog interval * @fallback_value: value (in seconds) to use, if the RTAS call fails * * returns the interval * * wdrtas_get_interval returns the current watchdog keepalive interval * as reported by the RTAS function ibm,get-system-parameter. The unit * of the return value is seconds. */static intwdrtas_get_interval(int fallback_value){ long result; char value[4]; result = rtas_call(wdrtas_token_get_sp, 3, 1, NULL, WDRTAS_SP_SPI, (void *)__pa(&value), 4); if ( (value[0] != 0) || (value[1] != 2) || (value[3] != 0) || (result < 0) ) { printk(KERN_WARNING "wdrtas: could not get sp_spi watchdog " "timeout (%li). Continuing\n", result); return fallback_value; } /* rtas uses minutes */ return ((int)value[2]) * 60;}/** * wdrtas_timer_start - starts watchdog * * wdrtas_timer_start starts the watchdog by calling the RTAS function * set-interval (surveillance) */static voidwdrtas_timer_start(void){ wdrtas_set_interval(wdrtas_interval);}/** * wdrtas_timer_stop - stops watchdog * * wdrtas_timer_stop stops the watchdog timer by calling the RTAS function * set-interval (surveillance) */static voidwdrtas_timer_stop(void){ wdrtas_set_interval(0);}/** * wdrtas_log_scanned_event - logs an event we received during keepalive * * wdrtas_log_scanned_event prints a message to the log buffer dumping * the results of the last event-scan call */static voidwdrtas_log_scanned_event(void){ int i; for (i = 0; i < WDRTAS_LOGBUFFER_LEN; i += 16) printk(KERN_INFO "wdrtas: dumping event (line %i/%i), data = " "%02x %02x %02x %02x %02x %02x %02x %02x " "%02x %02x %02x %02x %02x %02x %02x %02x\n", (i / 16) + 1, (WDRTAS_LOGBUFFER_LEN / 16), wdrtas_logbuffer[i + 0], wdrtas_logbuffer[i + 1], wdrtas_logbuffer[i + 2], wdrtas_logbuffer[i + 3], wdrtas_logbuffer[i + 4], wdrtas_logbuffer[i + 5], wdrtas_logbuffer[i + 6], wdrtas_logbuffer[i + 7], wdrtas_logbuffer[i + 8], wdrtas_logbuffer[i + 9], wdrtas_logbuffer[i + 10], wdrtas_logbuffer[i + 11], wdrtas_logbuffer[i + 12], wdrtas_logbuffer[i + 13], wdrtas_logbuffer[i + 14], wdrtas_logbuffer[i + 15]);}/** * wdrtas_timer_keepalive - resets watchdog timer to keep system alive * * wdrtas_timer_keepalive restarts the watchdog timer by calling the * RTAS function event-scan and repeats these calls as long as there are * events available. All events will be dumped. */static voidwdrtas_timer_keepalive(void){ long result; do { result = rtas_call(wdrtas_token_event_scan, 4, 1, NULL, RTAS_EVENT_SCAN_ALL_EVENTS, 0, (void *)__pa(wdrtas_logbuffer), WDRTAS_LOGBUFFER_LEN); if (result < 0) printk(KERN_ERR "wdrtas: event-scan failed: %li\n", result); if (result == 0) wdrtas_log_scanned_event(); } while (result == 0);}/** * wdrtas_get_temperature - returns current temperature * * returns temperature or <0 on failures * * wdrtas_get_temperature returns the current temperature in Fahrenheit. It * uses the RTAS call get-sensor-state, token 3 to do so */static intwdrtas_get_temperature(void){ long result; int temperature = 0; result = rtas_call(wdrtas_token_get_sensor_state, 2, 2, (void *)__pa(&temperature), WDRTAS_THERMAL_SENSOR, 0); if (result < 0) printk(KERN_WARNING "wdrtas: reading the thermal sensor " "faild: %li\n", result); else temperature = ((temperature * 9) / 5) + 32; /* fahrenheit */ return temperature;}/** * wdrtas_get_status - returns the status of the watchdog * * returns a bitmask of defines WDIOF_... as defined in * include/linux/watchdog.h */static intwdrtas_get_status(void){ return 0; /* TODO */}/** * wdrtas_get_boot_status - returns the reason for the last boot * * returns a bitmask of defines WDIOF_... as defined in * include/linux/watchdog.h, indicating why the watchdog rebooted the system */static intwdrtas_get_boot_status(void){ return 0; /* TODO */}/*** watchdog API and operations stuff *//* wdrtas_write - called when watchdog device is written to * @file: file structure * @buf: user buffer with data * @len: amount to data written * @ppos: position in file * * returns the number of successfully processed characters, which is always * the number of bytes passed to this function * * wdrtas_write processes all the data given to it and looks for the magic * character 'V'. This character allows the watchdog device to be closed * properly. */static ssize_twdrtas_write(struct file *file, const char __user *buf, size_t len, loff_t *ppos){ int i; char c; if (!len) goto out; if (!wdrtas_nowayout) { wdrtas_expect_close = 0; /* look for 'V' */ for (i = 0; i < len; i++) { if (get_user(c, buf + i)) return -EFAULT; /* allow to close device */ if (c == 'V') wdrtas_expect_close = WDRTAS_MAGIC_CHAR; } } wdrtas_timer_keepalive();out: return len;}/** * wdrtas_ioctl - ioctl function for the watchdog device * @inode: inode structure * @file: file structure * @cmd: command for ioctl * @arg: argument pointer * * returns 0 on success, <0 on failure * * wdrtas_ioctl implements the watchdog API ioctls */static intwdrtas_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg){ int __user *argp = (void __user *)arg; int i; static struct watchdog_info wdinfo = { .options = WDRTAS_SUPPORTED_MASK, .firmware_version = 0, .identity = "wdrtas" }; switch (cmd) { case WDIOC_GETSUPPORT: if (copy_to_user(argp, &wdinfo, sizeof(wdinfo))) return -EFAULT; return 0; case WDIOC_GETSTATUS: i = wdrtas_get_status(); return put_user(i, argp); case WDIOC_GETBOOTSTATUS: i = wdrtas_get_boot_status(); return put_user(i, argp); case WDIOC_GETTEMP: if (wdrtas_token_get_sensor_state == RTAS_UNKNOWN_SERVICE) return -EOPNOTSUPP; i = wdrtas_get_temperature();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -