123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618 |
- #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
- #include <linux/module.h>
- #include <linux/moduleparam.h>
- #include <linux/types.h>
- #include <linux/miscdevice.h>
- #include <linux/watchdog.h>
- #include <linux/delay.h>
- #include <linux/fs.h>
- #include <linux/ioport.h>
- #include <linux/notifier.h>
- #include <linux/reboot.h>
- #include <linux/init.h>
- #include <linux/spinlock.h>
- #include <linux/io.h>
- #include <linux/uaccess.h>
- #define SMSC_SUPPORT_MINUTES
- #undef SMSC_SUPPORT_MINUTES
- #define MAX_TIMEOUT 255
- #define UNIT_SECOND 0
- #define UNIT_MINUTE 1
- #define VERSION "1.1"
- #define IOPORT 0x3F0
- #define IOPORT_SIZE 2
- #define IODEV_NO 8
- static int unit = UNIT_SECOND;
- static int timeout = 60;
- static unsigned long timer_enabled;
- static char expect_close;
- static DEFINE_SPINLOCK(io_lock);
- static bool nowayout = WATCHDOG_NOWAYOUT;
- static inline void open_io_config(void)
- {
- outb(0x55, IOPORT);
- mdelay(1);
- outb(0x55, IOPORT);
- }
- static inline void close_io_config(void)
- {
- outb(0xAA, IOPORT);
- }
- static inline void select_io_device(unsigned char devno)
- {
- outb(0x07, IOPORT);
- outb(devno, IOPORT+1);
- }
- static inline void write_io_cr(unsigned char reg, unsigned char data)
- {
- outb(reg, IOPORT);
- outb(data, IOPORT+1);
- }
- static inline char read_io_cr(unsigned char reg)
- {
- outb(reg, IOPORT);
- return inb(IOPORT+1);
- }
- static inline void gpio_bit12(unsigned char reg)
- {
-
- write_io_cr(0xE2, reg);
- }
- static inline void gpio_bit13(unsigned char reg)
- {
-
- write_io_cr(0xE3, reg);
- }
- static inline void wdt_timer_units(unsigned char new_units)
- {
-
- write_io_cr(0xF1, new_units);
- }
- static inline void wdt_timeout_value(unsigned char new_timeout)
- {
-
- write_io_cr(0xF2, new_timeout);
- }
- static inline void wdt_timer_conf(unsigned char conf)
- {
-
- write_io_cr(0xF3, conf);
- }
- static inline void wdt_timer_ctrl(unsigned char reg)
- {
-
- write_io_cr(0xF4, reg);
- }
- static void wb_smsc_wdt_initialize(void)
- {
- unsigned char old;
- spin_lock(&io_lock);
- open_io_config();
- select_io_device(IODEV_NO);
-
- gpio_bit13(0x08);
- gpio_bit12(0x0A);
-
- wdt_timeout_value(0);
-
- wdt_timer_ctrl(0x00);
-
- wdt_timer_conf(0x00);
-
- old = read_io_cr(0xF1) & 0x7F;
- if (unit == UNIT_SECOND)
- old |= 0x80;
-
- wdt_timer_units(old);
- close_io_config();
- spin_unlock(&io_lock);
- }
- static void wb_smsc_wdt_shutdown(void)
- {
- spin_lock(&io_lock);
- open_io_config();
- select_io_device(IODEV_NO);
-
- gpio_bit13(0x09);
- gpio_bit12(0x09);
-
- wdt_timer_conf(0x00);
-
- wdt_timer_ctrl(0x00);
-
- wdt_timeout_value(0x00);
- close_io_config();
- spin_unlock(&io_lock);
- }
- static void wb_smsc_wdt_set_timeout(unsigned char new_timeout)
- {
- spin_lock(&io_lock);
- open_io_config();
- select_io_device(IODEV_NO);
-
- wdt_timer_ctrl((new_timeout == 0) ? 0x00 : 0x02);
-
- wdt_timeout_value(new_timeout);
- close_io_config();
- spin_unlock(&io_lock);
- }
- static unsigned char wb_smsc_wdt_get_timeout(void)
- {
- unsigned char set_timeout;
- spin_lock(&io_lock);
- open_io_config();
- select_io_device(IODEV_NO);
- set_timeout = read_io_cr(0xF2);
- close_io_config();
- spin_unlock(&io_lock);
- return set_timeout;
- }
- static void wb_smsc_wdt_disable(void)
- {
-
- wb_smsc_wdt_set_timeout(0);
- }
- static void wb_smsc_wdt_enable(void)
- {
-
- wb_smsc_wdt_set_timeout(timeout);
- }
- static void wb_smsc_wdt_reset_timer(void)
- {
- spin_lock(&io_lock);
- open_io_config();
- select_io_device(IODEV_NO);
-
- wdt_timeout_value(timeout);
- wdt_timer_conf(0x08);
- close_io_config();
- spin_unlock(&io_lock);
- }
- static int wb_smsc_wdt_status(void)
- {
- return (wb_smsc_wdt_get_timeout() == 0) ? 0 : WDIOF_KEEPALIVEPING;
- }
- static int wb_smsc_wdt_open(struct inode *inode, struct file *file)
- {
-
- if (test_and_set_bit(0, &timer_enabled))
- return -EBUSY;
- if (nowayout)
- __module_get(THIS_MODULE);
-
- wb_smsc_wdt_enable();
- pr_info("Watchdog enabled. Timeout set to %d %s\n",
- timeout, (unit == UNIT_SECOND) ? "second(s)" : "minute(s)");
- return nonseekable_open(inode, file);
- }
- static int wb_smsc_wdt_release(struct inode *inode, struct file *file)
- {
-
- if (expect_close == 42) {
- wb_smsc_wdt_disable();
- pr_info("Watchdog disabled, sleeping again...\n");
- } else {
- pr_crit("Unexpected close, not stopping watchdog!\n");
- wb_smsc_wdt_reset_timer();
- }
- clear_bit(0, &timer_enabled);
- expect_close = 0;
- return 0;
- }
- static ssize_t wb_smsc_wdt_write(struct file *file, const char __user *data,
- size_t len, loff_t *ppos)
- {
-
- if (len) {
- if (!nowayout) {
- size_t i;
-
- expect_close = 0;
-
- for (i = 0; i != len; i++) {
- char c;
- if (get_user(c, data + i))
- return -EFAULT;
- if (c == 'V')
- expect_close = 42;
- }
- }
-
- wb_smsc_wdt_reset_timer();
- }
- return len;
- }
- static long wb_smsc_wdt_ioctl(struct file *file,
- unsigned int cmd, unsigned long arg)
- {
- int new_timeout;
- union {
- struct watchdog_info __user *ident;
- int __user *i;
- } uarg;
- static const struct watchdog_info ident = {
- .options = WDIOF_KEEPALIVEPING |
- WDIOF_SETTIMEOUT |
- WDIOF_MAGICCLOSE,
- .firmware_version = 0,
- .identity = "SMsC 37B787 Watchdog",
- };
- uarg.i = (int __user *)arg;
- switch (cmd) {
- case WDIOC_GETSUPPORT:
- return copy_to_user(uarg.ident, &ident, sizeof(ident))
- ? -EFAULT : 0;
- case WDIOC_GETSTATUS:
- return put_user(wb_smsc_wdt_status(), uarg.i);
- case WDIOC_GETBOOTSTATUS:
- return put_user(0, uarg.i);
- case WDIOC_SETOPTIONS:
- {
- int options, retval = -EINVAL;
- if (get_user(options, uarg.i))
- return -EFAULT;
- if (options & WDIOS_DISABLECARD) {
- wb_smsc_wdt_disable();
- retval = 0;
- }
- if (options & WDIOS_ENABLECARD) {
- wb_smsc_wdt_enable();
- retval = 0;
- }
- return retval;
- }
- case WDIOC_KEEPALIVE:
- wb_smsc_wdt_reset_timer();
- return 0;
- case WDIOC_SETTIMEOUT:
- if (get_user(new_timeout, uarg.i))
- return -EFAULT;
-
- if (unit == UNIT_MINUTE)
- new_timeout /= 60;
- if (new_timeout < 0 || new_timeout > MAX_TIMEOUT)
- return -EINVAL;
- timeout = new_timeout;
- wb_smsc_wdt_set_timeout(timeout);
-
- case WDIOC_GETTIMEOUT:
- new_timeout = timeout;
- if (unit == UNIT_MINUTE)
- new_timeout *= 60;
- return put_user(new_timeout, uarg.i);
- default:
- return -ENOTTY;
- }
- }
- static int wb_smsc_wdt_notify_sys(struct notifier_block *this,
- unsigned long code, void *unused)
- {
- if (code == SYS_DOWN || code == SYS_HALT) {
-
- timeout = 0;
- wb_smsc_wdt_disable();
- }
- return NOTIFY_DONE;
- }
- static const struct file_operations wb_smsc_wdt_fops = {
- .owner = THIS_MODULE,
- .llseek = no_llseek,
- .write = wb_smsc_wdt_write,
- .unlocked_ioctl = wb_smsc_wdt_ioctl,
- .open = wb_smsc_wdt_open,
- .release = wb_smsc_wdt_release,
- };
- static struct notifier_block wb_smsc_wdt_notifier = {
- .notifier_call = wb_smsc_wdt_notify_sys,
- };
- static struct miscdevice wb_smsc_wdt_miscdev = {
- .minor = WATCHDOG_MINOR,
- .name = "watchdog",
- .fops = &wb_smsc_wdt_fops,
- };
- static int __init wb_smsc_wdt_init(void)
- {
- int ret;
- pr_info("SMsC 37B787 watchdog component driver "
- VERSION " initialising...\n");
- if (!request_region(IOPORT, IOPORT_SIZE, "SMsC 37B787 watchdog")) {
- pr_err("Unable to register IO port %#x\n", IOPORT);
- ret = -EBUSY;
- goto out_pnp;
- }
-
- if (timeout > MAX_TIMEOUT)
- timeout = MAX_TIMEOUT;
-
- wb_smsc_wdt_initialize();
- ret = register_reboot_notifier(&wb_smsc_wdt_notifier);
- if (ret) {
- pr_err("Unable to register reboot notifier err = %d\n", ret);
- goto out_io;
- }
- ret = misc_register(&wb_smsc_wdt_miscdev);
- if (ret) {
- pr_err("Unable to register miscdev on minor %d\n",
- WATCHDOG_MINOR);
- goto out_rbt;
- }
-
- pr_info("Timeout set to %d %s\n",
- timeout, (unit == UNIT_SECOND) ? "second(s)" : "minute(s)");
- pr_info("Watchdog initialized and sleeping (nowayout=%d)...\n",
- nowayout);
- out_clean:
- return ret;
- out_rbt:
- unregister_reboot_notifier(&wb_smsc_wdt_notifier);
- out_io:
- release_region(IOPORT, IOPORT_SIZE);
- out_pnp:
- goto out_clean;
- }
- static void __exit wb_smsc_wdt_exit(void)
- {
-
- if (!nowayout) {
- wb_smsc_wdt_shutdown();
- pr_info("Watchdog disabled\n");
- }
- misc_deregister(&wb_smsc_wdt_miscdev);
- unregister_reboot_notifier(&wb_smsc_wdt_notifier);
- release_region(IOPORT, IOPORT_SIZE);
- pr_info("SMsC 37B787 watchdog component driver removed\n");
- }
- module_init(wb_smsc_wdt_init);
- module_exit(wb_smsc_wdt_exit);
- MODULE_AUTHOR("Sven Anders <anders@anduras.de>");
- MODULE_DESCRIPTION("Driver for SMsC 37B787 watchdog component (Version "
- VERSION ")");
- MODULE_LICENSE("GPL");
- #ifdef SMSC_SUPPORT_MINUTES
- module_param(unit, int, 0);
- MODULE_PARM_DESC(unit,
- "set unit to use, 0=seconds or 1=minutes, default is 0");
- #endif
- module_param(timeout, int, 0);
- MODULE_PARM_DESC(timeout, "range is 1-255 units, default is 60");
- module_param(nowayout, bool, 0);
- MODULE_PARM_DESC(nowayout,
- "Watchdog cannot be stopped once started (default="
- __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
|