Pull acpi_bus_register_driver into release branch
[pandora-kernel.git] / drivers / char / watchdog / sc1200wdt.c
1 /*
2  *      National Semiconductor PC87307/PC97307 (ala SC1200) WDT driver
3  *      (c) Copyright 2002 Zwane Mwaikambo <zwane@commfireservices.com>,
4  *                      All Rights Reserved.
5  *      Based on wdt.c and wdt977.c by Alan Cox and Woody Suwalski respectively.
6  *
7  *      This program is free software; you can redistribute it and/or
8  *      modify it under the terms of the GNU General Public License
9  *      as published by the Free Software Foundation; either version
10  *      2 of the License, or (at your option) any later version.
11  *
12  *      The author(s) of this software shall not be held liable for damages
13  *      of any nature resulting due to the use of this software. This
14  *      software is provided AS-IS with no warranties.
15  *
16  *      Changelog:
17  *      20020220 Zwane Mwaikambo        Code based on datasheet, no hardware.
18  *      20020221 Zwane Mwaikambo        Cleanups as suggested by Jeff Garzik and Alan Cox.
19  *      20020222 Zwane Mwaikambo        Added probing.
20  *      20020225 Zwane Mwaikambo        Added ISAPNP support.
21  *      20020412 Rob Radez              Broke out start/stop functions
22  *               <rob@osinvestor.com>   Return proper status instead of temperature warning
23  *                                      Add WDIOC_GETBOOTSTATUS and WDIOC_SETOPTIONS ioctls
24  *                                      Fix CONFIG_WATCHDOG_NOWAYOUT
25  *      20020530 Joel Becker            Add Matt Domsch's nowayout module option
26  *      20030116 Adam Belay             Updated to the latest pnp code
27  *
28  */
29
30 #include <linux/config.h>
31 #include <linux/module.h>
32 #include <linux/moduleparam.h>
33 #include <linux/miscdevice.h>
34 #include <linux/watchdog.h>
35 #include <linux/ioport.h>
36 #include <linux/spinlock.h>
37 #include <linux/notifier.h>
38 #include <linux/reboot.h>
39 #include <linux/init.h>
40 #include <linux/pnp.h>
41 #include <linux/fs.h>
42 #include <linux/pci.h>
43
44 #include <asm/semaphore.h>
45 #include <asm/io.h>
46 #include <asm/uaccess.h>
47
48 #define SC1200_MODULE_VER       "build 20020303"
49 #define SC1200_MODULE_NAME      "sc1200wdt"
50 #define PFX                     SC1200_MODULE_NAME ": "
51
52 #define MAX_TIMEOUT     255     /* 255 minutes */
53 #define PMIR            (io)    /* Power Management Index Register */
54 #define PMDR            (io+1)  /* Power Management Data Register */
55
56 /* Data Register indexes */
57 #define FER1            0x00    /* Function enable register 1 */
58 #define FER2            0x01    /* Function enable register 2 */
59 #define PMC1            0x02    /* Power Management Ctrl 1 */
60 #define PMC2            0x03    /* Power Management Ctrl 2 */
61 #define PMC3            0x04    /* Power Management Ctrl 3 */
62 #define WDTO            0x05    /* Watchdog timeout register */
63 #define WDCF            0x06    /* Watchdog config register */
64 #define WDST            0x07    /* Watchdog status register */
65
66 /* WDCF bitfields - which devices assert WDO */
67 #define KBC_IRQ         0x01    /* Keyboard Controller */
68 #define MSE_IRQ         0x02    /* Mouse */
69 #define UART1_IRQ       0x03    /* Serial0 */
70 #define UART2_IRQ       0x04    /* Serial1 */
71 /* 5 -7 are reserved */
72
73 static char banner[] __initdata = KERN_INFO PFX SC1200_MODULE_VER;
74 static int timeout = 1;
75 static int io = -1;
76 static int io_len = 2;          /* for non plug and play */
77 static struct semaphore open_sem;
78 static char expect_close;
79 static spinlock_t sc1200wdt_lock;       /* io port access serialisation */
80
81 #if defined CONFIG_PNP
82 static int isapnp = 1;
83 static struct pnp_dev *wdt_dev;
84
85 module_param(isapnp, int, 0);
86 MODULE_PARM_DESC(isapnp, "When set to 0 driver ISA PnP support will be disabled");
87 #endif
88
89 module_param(io, int, 0);
90 MODULE_PARM_DESC(io, "io port");
91 module_param(timeout, int, 0);
92 MODULE_PARM_DESC(timeout, "range is 0-255 minutes, default is 1");
93
94 static int nowayout = WATCHDOG_NOWAYOUT;
95 module_param(nowayout, int, 0);
96 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
97
98
99
100 /* Read from Data Register */
101 static inline void sc1200wdt_read_data(unsigned char index, unsigned char *data)
102 {
103         spin_lock(&sc1200wdt_lock);
104         outb_p(index, PMIR);
105         *data = inb(PMDR);
106         spin_unlock(&sc1200wdt_lock);
107 }
108
109
110 /* Write to Data Register */
111 static inline void sc1200wdt_write_data(unsigned char index, unsigned char data)
112 {
113         spin_lock(&sc1200wdt_lock);
114         outb_p(index, PMIR);
115         outb(data, PMDR);
116         spin_unlock(&sc1200wdt_lock);
117 }
118
119
120 static void sc1200wdt_start(void)
121 {
122         unsigned char reg;
123
124         sc1200wdt_read_data(WDCF, &reg);
125         /* assert WDO when any of the following interrupts are triggered too */
126         reg |= (KBC_IRQ | MSE_IRQ | UART1_IRQ | UART2_IRQ);
127         sc1200wdt_write_data(WDCF, reg);
128         /* set the timeout and get the ball rolling */
129         sc1200wdt_write_data(WDTO, timeout);
130 }
131
132
133 static void sc1200wdt_stop(void)
134 {
135         sc1200wdt_write_data(WDTO, 0);
136 }
137
138
139 /* This returns the status of the WDO signal, inactive high. */
140 static inline int sc1200wdt_status(void)
141 {
142         unsigned char ret;
143
144         sc1200wdt_read_data(WDST, &ret);
145         /* If the bit is inactive, the watchdog is enabled, so return
146          * KEEPALIVEPING which is a bit of a kludge because there's nothing
147          * else for enabled/disabled status
148          */
149         return (ret & 0x01) ? 0 : WDIOF_KEEPALIVEPING;  /* bits 1 - 7 are undefined */
150 }
151
152
153 static int sc1200wdt_open(struct inode *inode, struct file *file)
154 {
155         nonseekable_open(inode, file);
156
157         /* allow one at a time */
158         if (down_trylock(&open_sem))
159                 return -EBUSY;
160
161         if (timeout > MAX_TIMEOUT)
162                 timeout = MAX_TIMEOUT;
163
164         sc1200wdt_start();
165         printk(KERN_INFO PFX "Watchdog enabled, timeout = %d min(s)", timeout);
166
167         return 0;
168 }
169
170
171 static int sc1200wdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
172 {
173         int new_timeout;
174         void __user *argp = (void __user *)arg;
175         int __user *p = argp;
176         static struct watchdog_info ident = {
177                 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
178                 .firmware_version = 0,
179                 .identity = "PC87307/PC97307",
180         };
181
182         switch (cmd) {
183                 default:
184                         return -ENOIOCTLCMD;    /* Keep Pavel Machek amused ;) */
185
186                 case WDIOC_GETSUPPORT:
187                         if (copy_to_user(argp, &ident, sizeof ident))
188                                 return -EFAULT;
189                         return 0;
190
191                 case WDIOC_GETSTATUS:
192                         return put_user(sc1200wdt_status(), p);
193
194                 case WDIOC_GETBOOTSTATUS:
195                         return put_user(0, p);
196
197                 case WDIOC_KEEPALIVE:
198                         sc1200wdt_write_data(WDTO, timeout);
199                         return 0;
200
201                 case WDIOC_SETTIMEOUT:
202                         if (get_user(new_timeout, p))
203                                 return -EFAULT;
204
205                         /* the API states this is given in secs */
206                         new_timeout /= 60;
207                         if (new_timeout < 0 || new_timeout > MAX_TIMEOUT)
208                                 return -EINVAL;
209
210                         timeout = new_timeout;
211                         sc1200wdt_write_data(WDTO, timeout);
212                         /* fall through and return the new timeout */
213
214                 case WDIOC_GETTIMEOUT:
215                         return put_user(timeout * 60, p);
216
217                 case WDIOC_SETOPTIONS:
218                 {
219                         int options, retval = -EINVAL;
220
221                         if (get_user(options, p))
222                                 return -EFAULT;
223
224                         if (options & WDIOS_DISABLECARD) {
225                                 sc1200wdt_stop();
226                                 retval = 0;
227                         }
228
229                         if (options & WDIOS_ENABLECARD) {
230                                 sc1200wdt_start();
231                                 retval = 0;
232                         }
233
234                         return retval;
235                 }
236         }
237 }
238
239
240 static int sc1200wdt_release(struct inode *inode, struct file *file)
241 {
242         if (expect_close == 42) {
243                 sc1200wdt_stop();
244                 printk(KERN_INFO PFX "Watchdog disabled\n");
245         } else {
246                 sc1200wdt_write_data(WDTO, timeout);
247                 printk(KERN_CRIT PFX "Unexpected close!, timeout = %d min(s)\n", timeout);
248         }
249         up(&open_sem);
250         expect_close = 0;
251
252         return 0;
253 }
254
255
256 static ssize_t sc1200wdt_write(struct file *file, const char __user *data, size_t len, loff_t *ppos)
257 {
258         if (len) {
259                 if (!nowayout) {
260                         size_t i;
261
262                         expect_close = 0;
263
264                         for (i = 0; i != len; i++) {
265                                 char c;
266
267                                 if (get_user(c, data+i))
268                                         return -EFAULT;
269                                 if (c == 'V')
270                                         expect_close = 42;
271                         }
272                 }
273
274                 sc1200wdt_write_data(WDTO, timeout);
275                 return len;
276         }
277
278         return 0;
279 }
280
281
282 static int sc1200wdt_notify_sys(struct notifier_block *this, unsigned long code, void *unused)
283 {
284         if (code == SYS_DOWN || code == SYS_HALT)
285                 sc1200wdt_stop();
286
287         return NOTIFY_DONE;
288 }
289
290
291 static struct notifier_block sc1200wdt_notifier =
292 {
293         .notifier_call =        sc1200wdt_notify_sys,
294 };
295
296 static struct file_operations sc1200wdt_fops =
297 {
298         .owner          = THIS_MODULE,
299         .llseek         = no_llseek,
300         .write          = sc1200wdt_write,
301         .ioctl          = sc1200wdt_ioctl,
302         .open           = sc1200wdt_open,
303         .release        = sc1200wdt_release,
304 };
305
306 static struct miscdevice sc1200wdt_miscdev =
307 {
308         .minor          = WATCHDOG_MINOR,
309         .name           = "watchdog",
310         .fops           = &sc1200wdt_fops,
311 };
312
313
314 static int __init sc1200wdt_probe(void)
315 {
316         /* The probe works by reading the PMC3 register's default value of 0x0e
317          * there is one caveat, if the device disables the parallel port or any
318          * of the UARTs we won't be able to detect it.
319          * Nb. This could be done with accuracy by reading the SID registers, but
320          * we don't have access to those io regions.
321          */
322
323         unsigned char reg;
324
325         sc1200wdt_read_data(PMC3, &reg);
326         reg &= 0x0f;                            /* we don't want the UART busy bits */
327         return (reg == 0x0e) ? 0 : -ENODEV;
328 }
329
330
331 #if defined CONFIG_PNP
332
333 static struct pnp_device_id scl200wdt_pnp_devices[] = {
334         /* National Semiconductor PC87307/PC97307 watchdog component */
335         {.id = "NSC0800", .driver_data = 0},
336         {.id = ""},
337 };
338
339 static int scl200wdt_pnp_probe(struct pnp_dev * dev, const struct pnp_device_id *dev_id)
340 {
341         /* this driver only supports one card at a time */
342         if (wdt_dev || !isapnp)
343                 return -EBUSY;
344
345         wdt_dev = dev;
346         io = pnp_port_start(wdt_dev, 0);
347         io_len = pnp_port_len(wdt_dev, 0);
348
349         if (!request_region(io, io_len, SC1200_MODULE_NAME)) {
350                 printk(KERN_ERR PFX "Unable to register IO port %#x\n", io);
351                 return -EBUSY;
352         }
353
354         printk(KERN_INFO "scl200wdt: PnP device found at io port %#x/%d\n", io, io_len);
355         return 0;
356 }
357
358 static void scl200wdt_pnp_remove(struct pnp_dev * dev)
359 {
360         if (wdt_dev){
361                 release_region(io, io_len);
362                 wdt_dev = NULL;
363         }
364 }
365
366 static struct pnp_driver scl200wdt_pnp_driver = {
367         .name           = "scl200wdt",
368         .id_table       = scl200wdt_pnp_devices,
369         .probe          = scl200wdt_pnp_probe,
370         .remove         = scl200wdt_pnp_remove,
371 };
372
373 #endif /* CONFIG_PNP */
374
375
376 static int __init sc1200wdt_init(void)
377 {
378         int ret;
379
380         printk("%s\n", banner);
381
382         spin_lock_init(&sc1200wdt_lock);
383         sema_init(&open_sem, 1);
384
385 #if defined CONFIG_PNP
386         if (isapnp) {
387                 ret = pnp_register_driver(&scl200wdt_pnp_driver);
388                 if (ret)
389                         goto out_clean;
390         }
391 #endif
392
393         if (io == -1) {
394                 printk(KERN_ERR PFX "io parameter must be specified\n");
395                 ret = -EINVAL;
396                 goto out_clean;
397         }
398
399 #if defined CONFIG_PNP
400         /* now that the user has specified an IO port and we haven't detected
401          * any devices, disable pnp support */
402         isapnp = 0;
403         pnp_unregister_driver(&scl200wdt_pnp_driver);
404 #endif
405
406         if (!request_region(io, io_len, SC1200_MODULE_NAME)) {
407                 printk(KERN_ERR PFX "Unable to register IO port %#x\n", io);
408                 ret = -EBUSY;
409                 goto out_clean;
410         }
411
412         ret = sc1200wdt_probe();
413         if (ret)
414                 goto out_io;
415
416         ret = register_reboot_notifier(&sc1200wdt_notifier);
417         if (ret) {
418                 printk(KERN_ERR PFX "Unable to register reboot notifier err = %d\n", ret);
419                 goto out_io;
420         }
421
422         ret = misc_register(&sc1200wdt_miscdev);
423         if (ret) {
424                 printk(KERN_ERR PFX "Unable to register miscdev on minor %d\n", WATCHDOG_MINOR);
425                 goto out_rbt;
426         }
427
428         /* ret = 0 */
429
430 out_clean:
431         return ret;
432
433 out_rbt:
434         unregister_reboot_notifier(&sc1200wdt_notifier);
435
436 out_io:
437         release_region(io, io_len);
438
439         goto out_clean;
440 }
441
442
443 static void __exit sc1200wdt_exit(void)
444 {
445         misc_deregister(&sc1200wdt_miscdev);
446         unregister_reboot_notifier(&sc1200wdt_notifier);
447
448 #if defined CONFIG_PNP
449         if(isapnp)
450                 pnp_unregister_driver(&scl200wdt_pnp_driver);
451         else
452 #endif
453         release_region(io, io_len);
454 }
455
456 module_init(sc1200wdt_init);
457 module_exit(sc1200wdt_exit);
458
459 MODULE_AUTHOR("Zwane Mwaikambo <zwane@commfireservices.com>");
460 MODULE_DESCRIPTION("Driver for National Semiconductor PC87307/PC97307 watchdog component");
461 MODULE_LICENSE("GPL");
462 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);