[WATCHDOG 38/57] stg7240_wdt: unlocked_ioctl
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / watchdog / sbc7240_wdt.c
1 /*
2 * NANO7240 SBC Watchdog device driver
3 *
4 * Based on w83877f.c by Scott Jennings,
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation;
9 *
10 * Software distributed under the License is distributed on an "AS IS"
11 * basis, WITHOUT WARRANTY OF ANY KIND, either express or
12 * implied. See the License for the specific language governing
13 * rights and limitations under the License.
14 *
15 * (c) Copyright 2007 Gilles GIGAN <gilles.gigan@jcu.edu.au>
16 *
17 */
18
19 #include <linux/fs.h>
20 #include <linux/init.h>
21 #include <linux/ioport.h>
22 #include <linux/jiffies.h>
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/miscdevice.h>
26 #include <linux/notifier.h>
27 #include <linux/reboot.h>
28 #include <linux/types.h>
29 #include <linux/watchdog.h>
30 #include <linux/io.h>
31 #include <linux/uaccess.h>
32 #include <asm/atomic.h>
33 #include <asm/system.h>
34
35 #define SBC7240_PREFIX "sbc7240_wdt: "
36
37 #define SBC7240_ENABLE_PORT 0x443
38 #define SBC7240_DISABLE_PORT 0x043
39 #define SBC7240_SET_TIMEOUT_PORT SBC7240_ENABLE_PORT
40 #define SBC7240_MAGIC_CHAR 'V'
41
42 #define SBC7240_TIMEOUT 30
43 #define SBC7240_MAX_TIMEOUT 255
44 static int timeout = SBC7240_TIMEOUT; /* in seconds */
45 module_param(timeout, int, 0);
46 MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds. (1<=timeout<="
47 __MODULE_STRING(SBC7240_MAX_TIMEOUT) ", default="
48 __MODULE_STRING(SBC7240_TIMEOUT) ")");
49
50 static int nowayout = WATCHDOG_NOWAYOUT;
51 module_param(nowayout, int, 0);
52 MODULE_PARM_DESC(nowayout, "Disable watchdog when closing device file");
53
54 #define SBC7240_OPEN_STATUS_BIT 0
55 #define SBC7240_ENABLED_STATUS_BIT 1
56 #define SBC7240_EXPECT_CLOSE_STATUS_BIT 2
57 static unsigned long wdt_status;
58
59 /*
60 * Utility routines
61 */
62
63 static void wdt_disable(void)
64 {
65 /* disable the watchdog */
66 if (test_and_clear_bit(SBC7240_ENABLED_STATUS_BIT, &wdt_status)) {
67 inb_p(SBC7240_DISABLE_PORT);
68 printk(KERN_INFO SBC7240_PREFIX
69 "Watchdog timer is now disabled.\n");
70 }
71 }
72
73 static void wdt_enable(void)
74 {
75 /* enable the watchdog */
76 if (!test_and_set_bit(SBC7240_ENABLED_STATUS_BIT, &wdt_status)) {
77 inb_p(SBC7240_ENABLE_PORT);
78 printk(KERN_INFO SBC7240_PREFIX
79 "Watchdog timer is now enabled.\n");
80 }
81 }
82
83 static int wdt_set_timeout(int t)
84 {
85 if (t < 1 || t > SBC7240_MAX_TIMEOUT) {
86 printk(KERN_ERR SBC7240_PREFIX
87 "timeout value must be 1<=x<=%d\n",
88 SBC7240_MAX_TIMEOUT);
89 return -1;
90 }
91 /* set the timeout */
92 outb_p((unsigned)t, SBC7240_SET_TIMEOUT_PORT);
93 timeout = t;
94 printk(KERN_INFO SBC7240_PREFIX "timeout set to %d seconds\n", t);
95 return 0;
96 }
97
98 /* Whack the dog */
99 static inline void wdt_keepalive(void)
100 {
101 if (test_bit(SBC7240_ENABLED_STATUS_BIT, &wdt_status))
102 inb_p(SBC7240_ENABLE_PORT);
103 }
104
105 /*
106 * /dev/watchdog handling
107 */
108 static ssize_t fop_write(struct file *file, const char __user *buf,
109 size_t count, loff_t *ppos)
110 {
111 size_t i;
112 char c;
113
114 if (count) {
115 if (!nowayout) {
116 clear_bit(SBC7240_EXPECT_CLOSE_STATUS_BIT,
117 &wdt_status);
118
119 /* is there a magic char ? */
120 for (i = 0; i != count; i++) {
121 if (get_user(c, buf + i))
122 return -EFAULT;
123 if (c == SBC7240_MAGIC_CHAR) {
124 set_bit(SBC7240_EXPECT_CLOSE_STATUS_BIT,
125 &wdt_status);
126 break;
127 }
128 }
129 }
130
131 wdt_keepalive();
132 }
133
134 return count;
135 }
136
137 static int fop_open(struct inode *inode, struct file *file)
138 {
139 if (test_and_set_bit(SBC7240_OPEN_STATUS_BIT, &wdt_status))
140 return -EBUSY;
141
142 wdt_enable();
143
144 return nonseekable_open(inode, file);
145 }
146
147 static int fop_close(struct inode *inode, struct file *file)
148 {
149 if (test_and_clear_bit(SBC7240_EXPECT_CLOSE_STATUS_BIT, &wdt_status)
150 || !nowayout) {
151 wdt_disable();
152 } else {
153 printk(KERN_CRIT SBC7240_PREFIX
154 "Unexpected close, not stopping watchdog!\n");
155 wdt_keepalive();
156 }
157
158 clear_bit(SBC7240_OPEN_STATUS_BIT, &wdt_status);
159 return 0;
160 }
161
162 static const struct watchdog_info ident = {
163 .options = WDIOF_KEEPALIVEPING|
164 WDIOF_SETTIMEOUT|
165 WDIOF_MAGICCLOSE,
166 .firmware_version = 1,
167 .identity = "SBC7240",
168 };
169
170
171 static long fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
172 {
173 switch (cmd) {
174 case WDIOC_GETSUPPORT:
175 return copy_to_user((void __user *)arg, &ident, sizeof(ident))
176 ? -EFAULT : 0;
177 case WDIOC_GETSTATUS:
178 case WDIOC_GETBOOTSTATUS:
179 return put_user(0, (int __user *)arg);
180 case WDIOC_KEEPALIVE:
181 wdt_keepalive();
182 return 0;
183 case WDIOC_SETOPTIONS:{
184 int options;
185 int retval = -EINVAL;
186
187 if (get_user(options, (int __user *)arg))
188 return -EFAULT;
189
190 if (options & WDIOS_DISABLECARD) {
191 wdt_disable();
192 retval = 0;
193 }
194
195 if (options & WDIOS_ENABLECARD) {
196 wdt_enable();
197 retval = 0;
198 }
199
200 return retval;
201 }
202 case WDIOC_SETTIMEOUT:{
203 int new_timeout;
204
205 if (get_user(new_timeout, (int __user *)arg))
206 return -EFAULT;
207
208 if (wdt_set_timeout(new_timeout))
209 return -EINVAL;
210
211 /* Fall through */
212 }
213 case WDIOC_GETTIMEOUT:
214 return put_user(timeout, (int __user *)arg);
215 default:
216 return -ENOTTY;
217 }
218 }
219
220 static const struct file_operations wdt_fops = {
221 .owner = THIS_MODULE,
222 .llseek = no_llseek,
223 .write = fop_write,
224 .open = fop_open,
225 .release = fop_close,
226 .unlocked_ioctl = fop_ioctl,
227 };
228
229 static struct miscdevice wdt_miscdev = {
230 .minor = WATCHDOG_MINOR,
231 .name = "watchdog",
232 .fops = &wdt_fops,
233 };
234
235 /*
236 * Notifier for system down
237 */
238
239 static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
240 void *unused)
241 {
242 if (code == SYS_DOWN || code == SYS_HALT)
243 wdt_disable();
244 return NOTIFY_DONE;
245 }
246
247 static struct notifier_block wdt_notifier = {
248 .notifier_call = wdt_notify_sys,
249 };
250
251 static void __exit sbc7240_wdt_unload(void)
252 {
253 printk(KERN_INFO SBC7240_PREFIX "Removing watchdog\n");
254 misc_deregister(&wdt_miscdev);
255
256 unregister_reboot_notifier(&wdt_notifier);
257 release_region(SBC7240_ENABLE_PORT, 1);
258 }
259
260 static int __init sbc7240_wdt_init(void)
261 {
262 int rc = -EBUSY;
263
264 if (!request_region(SBC7240_ENABLE_PORT, 1, "SBC7240 WDT")) {
265 printk(KERN_ERR SBC7240_PREFIX
266 "I/O address 0x%04x already in use\n",
267 SBC7240_ENABLE_PORT);
268 rc = -EIO;
269 goto err_out;
270 }
271
272 /* The IO port 0x043 used to disable the watchdog
273 * is already claimed by the system timer, so we
274 * cant request_region() it ...*/
275
276 if (timeout < 1 || timeout > SBC7240_MAX_TIMEOUT) {
277 timeout = SBC7240_TIMEOUT;
278 printk(KERN_INFO SBC7240_PREFIX
279 "timeout value must be 1<=x<=%d, using %d\n",
280 SBC7240_MAX_TIMEOUT, timeout);
281 }
282 wdt_set_timeout(timeout);
283 wdt_disable();
284
285 rc = register_reboot_notifier(&wdt_notifier);
286 if (rc) {
287 printk(KERN_ERR SBC7240_PREFIX
288 "cannot register reboot notifier (err=%d)\n", rc);
289 goto err_out_region;
290 }
291
292 rc = misc_register(&wdt_miscdev);
293 if (rc) {
294 printk(KERN_ERR SBC7240_PREFIX
295 "cannot register miscdev on minor=%d (err=%d)\n",
296 wdt_miscdev.minor, rc);
297 goto err_out_reboot_notifier;
298 }
299
300 printk(KERN_INFO SBC7240_PREFIX
301 "Watchdog driver for SBC7240 initialised (nowayout=%d)\n",
302 nowayout);
303
304 return 0;
305
306 err_out_reboot_notifier:
307 unregister_reboot_notifier(&wdt_notifier);
308 err_out_region:
309 release_region(SBC7240_ENABLE_PORT, 1);
310 err_out:
311 return rc;
312 }
313
314 module_init(sbc7240_wdt_init);
315 module_exit(sbc7240_wdt_unload);
316
317 MODULE_AUTHOR("Gilles Gigan");
318 MODULE_DESCRIPTION("Watchdog device driver for single board"
319 " computers EPIC Nano 7240 from iEi");
320 MODULE_LICENSE("GPL");
321 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
322