File: /usr/src/linux/drivers/char/advantechwdt.c

1     /*
2      *	Advantech Single Board Computer WDT driver for Linux 2.4.x
3      *
4      *	(c) Copyright 2000-2001 Marek Michalkiewicz <marekm@linux.org.pl>
5      *
6      *	Based on acquirewdt.c which is based on wdt.c.
7      *	Original copyright messages:
8      *
9      *	(c) Copyright 1996 Alan Cox <alan@redhat.com>, All Rights Reserved.
10      *				http://www.redhat.com
11      *
12      *	This program is free software; you can redistribute it and/or
13      *	modify it under the terms of the GNU General Public License
14      *	as published by the Free Software Foundation; either version
15      *	2 of the License, or (at your option) any later version.
16      *	
17      *	Neither Alan Cox nor CymruNet Ltd. admit liability nor provide 
18      *	warranty for any of this software. This material is provided 
19      *	"AS-IS" and at no charge.	
20      *
21      *	(c) Copyright 1995    Alan Cox <alan@redhat.com>
22      *
23      */
24     
25     #include <linux/config.h>
26     #include <linux/module.h>
27     #include <linux/version.h>
28     #include <linux/types.h>
29     #include <linux/errno.h>
30     #include <linux/kernel.h>
31     #include <linux/sched.h>
32     #include <linux/miscdevice.h>
33     #include <linux/watchdog.h>
34     #include <linux/slab.h>
35     #include <linux/ioport.h>
36     #include <linux/fcntl.h>
37     #include <asm/io.h>
38     #include <asm/uaccess.h>
39     #include <asm/system.h>
40     #include <linux/notifier.h>
41     #include <linux/reboot.h>
42     #include <linux/init.h>
43     #include <linux/spinlock.h>
44     #include <linux/smp_lock.h>
45     
46     static int advwdt_is_open;
47     static spinlock_t advwdt_lock;
48     
49     /*
50      *	You must set these - there is no sane way to probe for this board.
51      *
52      *	To enable or restart, write the timeout value in seconds (1 to 63)
53      *	to I/O port WDT_START.  To disable, read I/O port WDT_STOP.
54      *	Both are 0x443 for most boards (tested on a PCA-6276VE-00B1), but
55      *	check your manual (at least the PCA-6159 seems to be different -
56      *	the manual says WDT_STOP is 0x43, not 0x443).
57      *	(0x43 is also a write-only control register for the 8254 timer!)
58      *
59      *	TODO: module parameters to set the I/O port addresses and NOWAYOUT
60      *	option at load time.
61      */
62      
63     #define WDT_STOP 0x443
64     #define WDT_START 0x443
65     
66     #define WD_TIMO 60		/* 1 minute */
67     
68     /*
69      *	Kernel methods.
70      */
71      
72     static void
73     advwdt_ping(void)
74     {
75     	/* Write a watchdog value */
76     	outb_p(WD_TIMO, WDT_START);
77     }
78     
79     static ssize_t
80     advwdt_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
81     {
82     	/*  Can't seek (pwrite) on this device  */
83     	if (ppos != &file->f_pos)
84     		return -ESPIPE;
85     
86     	if (count) {
87     		advwdt_ping();
88     		return 1;
89     	}
90     	return 0;
91     }
92     
93     static ssize_t
94     advwdt_read(struct file *file, char *buf, size_t count, loff_t *ppos)
95     {
96     	return -EINVAL;
97     }
98     
99     static int
100     advwdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
101     	  unsigned long arg)
102     {
103     	static struct watchdog_info ident = {
104     		WDIOF_KEEPALIVEPING, 1, "Advantech WDT"
105     	};
106     	
107     	switch (cmd) {
108     	case WDIOC_GETSUPPORT:
109     	  if (copy_to_user((struct watchdog_info *)arg, &ident, sizeof(ident)))
110     	    return -EFAULT;
111     	  break;
112     	  
113     	case WDIOC_GETSTATUS:
114     	  if (copy_to_user((int *)arg, &advwdt_is_open,  sizeof(int)))
115     	    return -EFAULT;
116     	  break;
117     
118     	case WDIOC_KEEPALIVE:
119     	  advwdt_ping();
120     	  break;
121     
122     	default:
123     	  return -ENOTTY;
124     	}
125     	return 0;
126     }
127     
128     static int
129     advwdt_open(struct inode *inode, struct file *file)
130     {
131     	switch (MINOR(inode->i_rdev)) {
132     		case WATCHDOG_MINOR:
133     			spin_lock(&advwdt_lock);
134     			if (advwdt_is_open) {
135     				spin_unlock(&advwdt_lock);
136     				return -EBUSY;
137     			}
138     			/*
139     			 *	Activate 
140     			 */
141     	 
142     			advwdt_is_open = 1;
143     			advwdt_ping();
144     			spin_unlock(&advwdt_lock);
145     			return 0;
146     		default:
147     			return -ENODEV;
148     	}
149     }
150     
151     static int
152     advwdt_close(struct inode *inode, struct file *file)
153     {
154     	lock_kernel();
155     	if (MINOR(inode->i_rdev) == WATCHDOG_MINOR) {
156     		spin_lock(&advwdt_lock);
157     #ifndef CONFIG_WATCHDOG_NOWAYOUT	
158     		inb_p(WDT_STOP);
159     #endif		
160     		advwdt_is_open = 0;
161     		spin_unlock(&advwdt_lock);
162     	}
163     	unlock_kernel();
164     	return 0;
165     }
166     
167     /*
168      *	Notifier for system down
169      */
170     
171     static int
172     advwdt_notify_sys(struct notifier_block *this, unsigned long code,
173     	void *unused)
174     {
175     	if (code == SYS_DOWN || code == SYS_HALT) {
176     		/* Turn the WDT off */
177     		inb_p(WDT_STOP);
178     	}
179     	return NOTIFY_DONE;
180     }
181      
182     /*
183      *	Kernel Interfaces
184      */
185      
186     static struct file_operations advwdt_fops = {
187     	owner:		THIS_MODULE,
188     	read:		advwdt_read,
189     	write:		advwdt_write,
190     	ioctl:		advwdt_ioctl,
191     	open:		advwdt_open,
192     	release:	advwdt_close,
193     };
194     
195     static struct miscdevice advwdt_miscdev = {
196     	WATCHDOG_MINOR,
197     	"watchdog",
198     	&advwdt_fops
199     };
200     
201     /*
202      *	The WDT needs to learn about soft shutdowns in order to
203      *	turn the timebomb registers off. 
204      */
205      
206     static struct notifier_block advwdt_notifier = {
207     	advwdt_notify_sys,
208     	NULL,
209     	0
210     };
211     
212     static int __init
213     advwdt_init(void)
214     {
215     	printk("WDT driver for Advantech single board computer initialising.\n");
216     
217     	spin_lock_init(&advwdt_lock);
218     	misc_register(&advwdt_miscdev);
219     #if WDT_START != WDT_STOP
220     	request_region(WDT_STOP, 1, "Advantech WDT");
221     #endif
222     	request_region(WDT_START, 1, "Advantech WDT");
223     	register_reboot_notifier(&advwdt_notifier);
224     	return 0;
225     }
226     
227     static void __exit
228     advwdt_exit(void)
229     {
230     	misc_deregister(&advwdt_miscdev);
231     	unregister_reboot_notifier(&advwdt_notifier);
232     #if WDT_START != WDT_STOP
233     	release_region(WDT_STOP,1);
234     #endif
235     	release_region(WDT_START,1);
236     }
237     
238     module_init(advwdt_init);
239     module_exit(advwdt_exit);
240     
241     MODULE_LICENSE("GPL");
242     
243     /* end of advantechwdt.c */
244     
245