123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475 |
- #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
- #include <linux/module.h>
- #include <linux/moduleparam.h>
- #include <linux/miscdevice.h>
- #include <linux/watchdog.h>
- #include <linux/ioport.h>
- #include <linux/spinlock.h>
- #include <linux/notifier.h>
- #include <linux/reboot.h>
- #include <linux/init.h>
- #include <linux/pnp.h>
- #include <linux/fs.h>
- #include <linux/semaphore.h>
- #include <linux/io.h>
- #include <linux/uaccess.h>
- #define SC1200_MODULE_VER "build 20020303"
- #define SC1200_MODULE_NAME "sc1200wdt"
- #define MAX_TIMEOUT 255
- #define PMIR (io)
- #define PMDR (io+1)
- #define FER1 0x00
- #define FER2 0x01
- #define PMC1 0x02
- #define PMC2 0x03
- #define PMC3 0x04
- #define WDTO 0x05
- #define WDCF 0x06
- #define WDST 0x07
- #define KBC_IRQ 0x01
- #define MSE_IRQ 0x02
- #define UART1_IRQ 0x03
- #define UART2_IRQ 0x04
- static int timeout = 1;
- static int io = -1;
- static int io_len = 2;
- static unsigned long open_flag;
- static char expect_close;
- static DEFINE_SPINLOCK(sc1200wdt_lock);
- #if defined CONFIG_PNP
- static int isapnp = 1;
- static struct pnp_dev *wdt_dev;
- module_param(isapnp, int, 0);
- MODULE_PARM_DESC(isapnp,
- "When set to 0 driver ISA PnP support will be disabled");
- #endif
- module_param_hw(io, int, ioport, 0);
- MODULE_PARM_DESC(io, "io port");
- module_param(timeout, int, 0);
- MODULE_PARM_DESC(timeout, "range is 0-255 minutes, default is 1");
- static bool nowayout = WATCHDOG_NOWAYOUT;
- module_param(nowayout, bool, 0);
- MODULE_PARM_DESC(nowayout,
- "Watchdog cannot be stopped once started (default="
- __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
- static inline void __sc1200wdt_read_data(unsigned char index,
- unsigned char *data)
- {
- outb_p(index, PMIR);
- *data = inb(PMDR);
- }
- static void sc1200wdt_read_data(unsigned char index, unsigned char *data)
- {
- spin_lock(&sc1200wdt_lock);
- __sc1200wdt_read_data(index, data);
- spin_unlock(&sc1200wdt_lock);
- }
- static inline void __sc1200wdt_write_data(unsigned char index,
- unsigned char data)
- {
- outb_p(index, PMIR);
- outb(data, PMDR);
- }
- static inline void sc1200wdt_write_data(unsigned char index,
- unsigned char data)
- {
- spin_lock(&sc1200wdt_lock);
- __sc1200wdt_write_data(index, data);
- spin_unlock(&sc1200wdt_lock);
- }
- static void sc1200wdt_start(void)
- {
- unsigned char reg;
- spin_lock(&sc1200wdt_lock);
- __sc1200wdt_read_data(WDCF, ®);
-
- reg |= (KBC_IRQ | MSE_IRQ | UART1_IRQ | UART2_IRQ);
- __sc1200wdt_write_data(WDCF, reg);
-
- __sc1200wdt_write_data(WDTO, timeout);
- spin_unlock(&sc1200wdt_lock);
- }
- static void sc1200wdt_stop(void)
- {
- sc1200wdt_write_data(WDTO, 0);
- }
- static inline int sc1200wdt_status(void)
- {
- unsigned char ret;
- sc1200wdt_read_data(WDST, &ret);
-
- return (ret & 0x01) ? 0 : WDIOF_KEEPALIVEPING;
- }
- static int sc1200wdt_open(struct inode *inode, struct file *file)
- {
-
- if (test_and_set_bit(0, &open_flag))
- return -EBUSY;
- if (timeout > MAX_TIMEOUT)
- timeout = MAX_TIMEOUT;
- sc1200wdt_start();
- pr_info("Watchdog enabled, timeout = %d min(s)", timeout);
- return stream_open(inode, file);
- }
- static long sc1200wdt_ioctl(struct file *file, unsigned int cmd,
- unsigned long arg)
- {
- int new_timeout;
- void __user *argp = (void __user *)arg;
- int __user *p = argp;
- static const struct watchdog_info ident = {
- .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT |
- WDIOF_MAGICCLOSE,
- .firmware_version = 0,
- .identity = "PC87307/PC97307",
- };
- switch (cmd) {
- case WDIOC_GETSUPPORT:
- if (copy_to_user(argp, &ident, sizeof(ident)))
- return -EFAULT;
- return 0;
- case WDIOC_GETSTATUS:
- return put_user(sc1200wdt_status(), p);
- case WDIOC_GETBOOTSTATUS:
- return put_user(0, p);
- case WDIOC_SETOPTIONS:
- {
- int options, retval = -EINVAL;
- if (get_user(options, p))
- return -EFAULT;
- if (options & WDIOS_DISABLECARD) {
- sc1200wdt_stop();
- retval = 0;
- }
- if (options & WDIOS_ENABLECARD) {
- sc1200wdt_start();
- retval = 0;
- }
- return retval;
- }
- case WDIOC_KEEPALIVE:
- sc1200wdt_write_data(WDTO, timeout);
- return 0;
- case WDIOC_SETTIMEOUT:
- if (get_user(new_timeout, p))
- return -EFAULT;
-
- new_timeout /= 60;
- if (new_timeout < 0 || new_timeout > MAX_TIMEOUT)
- return -EINVAL;
- timeout = new_timeout;
- sc1200wdt_write_data(WDTO, timeout);
- fallthrough;
- case WDIOC_GETTIMEOUT:
- return put_user(timeout * 60, p);
- default:
- return -ENOTTY;
- }
- }
- static int sc1200wdt_release(struct inode *inode, struct file *file)
- {
- if (expect_close == 42) {
- sc1200wdt_stop();
- pr_info("Watchdog disabled\n");
- } else {
- sc1200wdt_write_data(WDTO, timeout);
- pr_crit("Unexpected close!, timeout = %d min(s)\n", timeout);
- }
- clear_bit(0, &open_flag);
- expect_close = 0;
- return 0;
- }
- static ssize_t sc1200wdt_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;
- }
- }
- sc1200wdt_write_data(WDTO, timeout);
- return len;
- }
- return 0;
- }
- static int sc1200wdt_notify_sys(struct notifier_block *this,
- unsigned long code, void *unused)
- {
- if (code == SYS_DOWN || code == SYS_HALT)
- sc1200wdt_stop();
- return NOTIFY_DONE;
- }
- static struct notifier_block sc1200wdt_notifier = {
- .notifier_call = sc1200wdt_notify_sys,
- };
- static const struct file_operations sc1200wdt_fops = {
- .owner = THIS_MODULE,
- .llseek = no_llseek,
- .write = sc1200wdt_write,
- .unlocked_ioctl = sc1200wdt_ioctl,
- .compat_ioctl = compat_ptr_ioctl,
- .open = sc1200wdt_open,
- .release = sc1200wdt_release,
- };
- static struct miscdevice sc1200wdt_miscdev = {
- .minor = WATCHDOG_MINOR,
- .name = "watchdog",
- .fops = &sc1200wdt_fops,
- };
- static int __init sc1200wdt_probe(void)
- {
-
- unsigned char reg;
- sc1200wdt_read_data(PMC3, ®);
- reg &= 0x0f;
- return (reg == 0x0e) ? 0 : -ENODEV;
- }
- #if defined CONFIG_PNP
- static const struct pnp_device_id scl200wdt_pnp_devices[] = {
-
- {.id = "NSC0800", .driver_data = 0},
- {.id = ""},
- };
- static int scl200wdt_pnp_probe(struct pnp_dev *dev,
- const struct pnp_device_id *dev_id)
- {
-
- if (wdt_dev || !isapnp)
- return -EBUSY;
- wdt_dev = dev;
- io = pnp_port_start(wdt_dev, 0);
- io_len = pnp_port_len(wdt_dev, 0);
- if (!request_region(io, io_len, SC1200_MODULE_NAME)) {
- pr_err("Unable to register IO port %#x\n", io);
- return -EBUSY;
- }
- pr_info("PnP device found at io port %#x/%d\n", io, io_len);
- return 0;
- }
- static void scl200wdt_pnp_remove(struct pnp_dev *dev)
- {
- if (wdt_dev) {
- release_region(io, io_len);
- wdt_dev = NULL;
- }
- }
- static struct pnp_driver scl200wdt_pnp_driver = {
- .name = "scl200wdt",
- .id_table = scl200wdt_pnp_devices,
- .probe = scl200wdt_pnp_probe,
- .remove = scl200wdt_pnp_remove,
- };
- #endif
- static int __init sc1200wdt_init(void)
- {
- int ret;
- pr_info("%s\n", SC1200_MODULE_VER);
- #if defined CONFIG_PNP
- if (isapnp) {
- ret = pnp_register_driver(&scl200wdt_pnp_driver);
- if (ret)
- goto out_clean;
- }
- #endif
- if (io == -1) {
- pr_err("io parameter must be specified\n");
- ret = -EINVAL;
- goto out_pnp;
- }
- #if defined CONFIG_PNP
-
- if (isapnp)
- pnp_unregister_driver(&scl200wdt_pnp_driver);
- isapnp = 0;
- #endif
- if (!request_region(io, io_len, SC1200_MODULE_NAME)) {
- pr_err("Unable to register IO port %#x\n", io);
- ret = -EBUSY;
- goto out_pnp;
- }
- ret = sc1200wdt_probe();
- if (ret)
- goto out_io;
- ret = register_reboot_notifier(&sc1200wdt_notifier);
- if (ret) {
- pr_err("Unable to register reboot notifier err = %d\n", ret);
- goto out_io;
- }
- ret = misc_register(&sc1200wdt_miscdev);
- if (ret) {
- pr_err("Unable to register miscdev on minor %d\n",
- WATCHDOG_MINOR);
- goto out_rbt;
- }
-
- out_clean:
- return ret;
- out_rbt:
- unregister_reboot_notifier(&sc1200wdt_notifier);
- out_io:
- release_region(io, io_len);
- out_pnp:
- #if defined CONFIG_PNP
- if (isapnp)
- pnp_unregister_driver(&scl200wdt_pnp_driver);
- #endif
- goto out_clean;
- }
- static void __exit sc1200wdt_exit(void)
- {
- misc_deregister(&sc1200wdt_miscdev);
- unregister_reboot_notifier(&sc1200wdt_notifier);
- #if defined CONFIG_PNP
- if (isapnp)
- pnp_unregister_driver(&scl200wdt_pnp_driver);
- else
- #endif
- release_region(io, io_len);
- }
- module_init(sc1200wdt_init);
- module_exit(sc1200wdt_exit);
- MODULE_AUTHOR("Zwane Mwaikambo <zwane@commfireservices.com>");
- MODULE_DESCRIPTION(
- "Driver for National Semiconductor PC87307/PC97307 watchdog component");
- MODULE_LICENSE("GPL");
|