2  *      Acquire Single Board Computer Watchdog Timer driver
 
   4  *      Based on wdt.c. Original copyright messages:
 
   6  *      (c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>,
 
   9  *      This program is free software; you can redistribute it and/or
 
  10  *      modify it under the terms of the GNU General Public License
 
  11  *      as published by the Free Software Foundation; either version
 
  12  *      2 of the License, or (at your option) any later version.
 
  14  *      Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
 
  15  *      warranty for any of this software. This material is provided
 
  16  *      "AS-IS" and at no charge.
 
  18  *      (c) Copyright 1995    Alan Cox <alan@lxorguk.ukuu.org.uk>
 
  20  *      14-Dec-2001 Matt Domsch <Matt_Domsch@dell.com>
 
  21  *          Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT
 
  22  *          Can't add timeout - driver doesn't allow changing value
 
  26  *      Theory of Operation:
 
  27  *              The Watch-Dog Timer is provided to ensure that standalone
 
  28  *              Systems can always recover from catastrophic conditions that
 
  29  *              caused the CPU to crash. This condition may have occured by
 
  30  *              external EMI or a software bug. When the CPU stops working
 
  31  *              correctly, hardware on the board will either perform a hardware
 
  32  *              reset (cold boot) or a non-maskable interrupt (NMI) to bring the
 
  33  *              system back to a known state.
 
  35  *              The Watch-Dog Timer is controlled by two I/O Ports.
 
  36  *                443 hex       - Read  - Enable or refresh the Watch-Dog Timer
 
  37  *                043 hex       - Read  - Disable the Watch-Dog Timer
 
  39  *              To enable the Watch-Dog Timer, a read from I/O port 443h must
 
  40  *              be performed. This will enable and activate the countdown timer
 
  41  *              which will eventually time out and either reset the CPU or cause
 
  42  *              an NMI depending on the setting of a jumper. To ensure that this
 
  43  *              reset condition does not occur, the Watch-Dog Timer must be
 
  44  *              periodically refreshed by reading the same I/O port 443h.
 
  45  *              The Watch-Dog Timer is disabled by reading I/O port 043h.
 
  47  *              The Watch-Dog Timer Time-Out Period is set via jumpers.
 
  48  *              It can be 1, 2, 10, 20, 110 or 220 seconds.
 
  52  *      Includes, defines, variables, module parameters, ...
 
  56 #include <linux/module.h>               /* For module specific items */
 
  57 #include <linux/moduleparam.h>          /* For new moduleparam's */
 
  58 #include <linux/types.h>                /* For standard types (like size_t) */
 
  59 #include <linux/errno.h>                /* For the -ENODEV/... values */
 
  60 #include <linux/kernel.h>               /* For printk/panic/... */
 
  61 #include <linux/miscdevice.h>           /* For MODULE_ALIAS_MISCDEV
 
  63 #include <linux/watchdog.h>             /* For the watchdog specific items */
 
  64 #include <linux/fs.h>                   /* For file operations */
 
  65 #include <linux/ioport.h>               /* For io-port access */
 
  66 #include <linux/platform_device.h>      /* For platform_driver framework */
 
  67 #include <linux/init.h>                 /* For __init/__exit/... */
 
  68 #include <linux/uaccess.h>              /* For copy_to_user/put_user/... */
 
  69 #include <linux/io.h>                   /* For inb/outb/... */
 
  71 /* Module information */
 
  72 #define DRV_NAME "acquirewdt"
 
  73 #define PFX DRV_NAME ": "
 
  74 #define WATCHDOG_NAME "Acquire WDT"
 
  75 /* There is no way to see what the correct time-out period is */
 
  76 #define WATCHDOG_HEARTBEAT 0
 
  78 /* internal variables */
 
  79 /* the watchdog platform device */
 
  80 static struct platform_device *acq_platform_device;
 
  81 static unsigned long acq_is_open;
 
  82 static char expect_close;
 
  84 /* module parameters */
 
  85 /* You must set this - there is no sane way to probe for this board. */
 
  86 static int wdt_stop = 0x43;
 
  87 module_param(wdt_stop, int, 0);
 
  88 MODULE_PARM_DESC(wdt_stop, "Acquire WDT 'stop' io port (default 0x43)");
 
  90 /* You must set this - there is no sane way to probe for this board. */
 
  91 static int wdt_start = 0x443;
 
  92 module_param(wdt_start, int, 0);
 
  93 MODULE_PARM_DESC(wdt_start, "Acquire WDT 'start' io port (default 0x443)");
 
  95 static int nowayout = WATCHDOG_NOWAYOUT;
 
  96 module_param(nowayout, int, 0);
 
  97 MODULE_PARM_DESC(nowayout,
 
  98         "Watchdog cannot be stopped once started (default="
 
  99         __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
 
 102  *      Watchdog Operations
 
 105 static void acq_keepalive(void)
 
 107         /* Write a watchdog value */
 
 111 static void acq_stop(void)
 
 113         /* Turn the card off */
 
 118  *      /dev/watchdog handling
 
 121 static ssize_t acq_write(struct file *file, const char __user *buf,
 
 122                                                 size_t count, loff_t *ppos)
 
 124         /* See if we got the magic character 'V' and reload the timer */
 
 128                         /* note: just in case someone wrote the magic character
 
 129                            five months ago... */
 
 131                         /* scan to see whether or not we got the
 
 133                         for (i = 0; i != count; i++) {
 
 135                                 if (get_user(c, buf + i))
 
 141                 /* Well, anyhow someone wrote to us, we should
 
 142                                 return that favour */
 
 148 static long acq_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 
 150         int options, retval = -EINVAL;
 
 151         void __user *argp = (void __user *)arg;
 
 152         int __user *p = argp;
 
 153         static struct watchdog_info ident = {
 
 154                 .options = WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
 
 155                 .firmware_version = 1,
 
 156                 .identity = WATCHDOG_NAME,
 
 160         case WDIOC_GETSUPPORT:
 
 161                 return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
 
 163         case WDIOC_GETSTATUS:
 
 164         case WDIOC_GETBOOTSTATUS:
 
 165                 return put_user(0, p);
 
 167         case WDIOC_SETOPTIONS:
 
 169                 if (get_user(options, p))
 
 171                 if (options & WDIOS_DISABLECARD) {
 
 175                 if (options & WDIOS_ENABLECARD) {
 
 181         case WDIOC_KEEPALIVE:
 
 185         case WDIOC_GETTIMEOUT:
 
 186                 return put_user(WATCHDOG_HEARTBEAT, p);
 
 193 static int acq_open(struct inode *inode, struct file *file)
 
 195         if (test_and_set_bit(0, &acq_is_open))
 
 199                 __module_get(THIS_MODULE);
 
 203         return nonseekable_open(inode, file);
 
 206 static int acq_close(struct inode *inode, struct file *file)
 
 208         if (expect_close == 42) {
 
 212                         "Unexpected close, not stopping watchdog!\n");
 
 215         clear_bit(0, &acq_is_open);
 
 224 static const struct file_operations acq_fops = {
 
 225         .owner          = THIS_MODULE,
 
 228         .unlocked_ioctl = acq_ioctl,
 
 230         .release        = acq_close,
 
 233 static struct miscdevice acq_miscdev = {
 
 234         .minor  = WATCHDOG_MINOR,
 
 240  *      Init & exit routines
 
 243 static int __devinit acq_probe(struct platform_device *dev)
 
 247         if (wdt_stop != wdt_start) {
 
 248                 if (!request_region(wdt_stop, 1, WATCHDOG_NAME)) {
 
 250                             "I/O address 0x%04x already in use\n", wdt_stop);
 
 256         if (!request_region(wdt_start, 1, WATCHDOG_NAME)) {
 
 257                 printk(KERN_ERR PFX "I/O address 0x%04x already in use\n",
 
 262         ret = misc_register(&acq_miscdev);
 
 265                         "cannot register miscdev on minor=%d (err=%d)\n",
 
 266                                                         WATCHDOG_MINOR, ret);
 
 269         printk(KERN_INFO PFX "initialized. (nowayout=%d)\n", nowayout);
 
 273         release_region(wdt_start, 1);
 
 275         if (wdt_stop != wdt_start)
 
 276                 release_region(wdt_stop, 1);
 
 281 static int __devexit acq_remove(struct platform_device *dev)
 
 283         misc_deregister(&acq_miscdev);
 
 284         release_region(wdt_start, 1);
 
 285         if (wdt_stop != wdt_start)
 
 286                 release_region(wdt_stop, 1);
 
 291 static void acq_shutdown(struct platform_device *dev)
 
 293         /* Turn the WDT off if we have a soft shutdown */
 
 297 static struct platform_driver acquirewdt_driver = {
 
 299         .remove         = __devexit_p(acq_remove),
 
 300         .shutdown       = acq_shutdown,
 
 302                 .owner  = THIS_MODULE,
 
 307 static int __init acq_init(void)
 
 312               "WDT driver for Acquire single board computer initialising.\n");
 
 314         err = platform_driver_register(&acquirewdt_driver);
 
 318         acq_platform_device = platform_device_register_simple(DRV_NAME,
 
 320         if (IS_ERR(acq_platform_device)) {
 
 321                 err = PTR_ERR(acq_platform_device);
 
 322                 goto unreg_platform_driver;
 
 326 unreg_platform_driver:
 
 327         platform_driver_unregister(&acquirewdt_driver);
 
 331 static void __exit acq_exit(void)
 
 333         platform_device_unregister(acq_platform_device);
 
 334         platform_driver_unregister(&acquirewdt_driver);
 
 335         printk(KERN_INFO PFX "Watchdog Module Unloaded.\n");
 
 338 module_init(acq_init);
 
 339 module_exit(acq_exit);
 
 341 MODULE_AUTHOR("David Woodhouse");
 
 342 MODULE_DESCRIPTION("Acquire Inc. Single Board Computer Watchdog Timer driver");
 
 343 MODULE_LICENSE("GPL");
 
 344 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);