~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/drivers/char/wdt.c

Version: ~ [ 2.4.0 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  *      Industrial Computer Source WDT500/501 driver for Linux 2.1.x
  3  *
  4  *      (c) Copyright 1996-1997 Alan Cox <alan@redhat.com>, All Rights Reserved.
  5  *                              http://www.redhat.com
  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  *      Neither Alan Cox nor CymruNet Ltd. admit liability nor provide 
 13  *      warranty for any of this software. This material is provided 
 14  *      "AS-IS" and at no charge.       
 15  *
 16  *      (c) Copyright 1995    Alan Cox <alan@lxorguk.ukuu.org.uk>
 17  *
 18  *      Release 0.08.
 19  *
 20  *      Fixes
 21  *              Dave Gregorich  :       Modularisation and minor bugs
 22  *              Alan Cox        :       Added the watchdog ioctl() stuff
 23  *              Alan Cox        :       Fixed the reboot problem (as noted by
 24  *                                      Matt Crocker).
 25  *              Alan Cox        :       Added wdt= boot option
 26  *              Alan Cox        :       Cleaned up copy/user stuff
 27  *              Tim Hockin      :       Added insmod parameters, comment cleanup
 28  *                                      Parameterized timeout
 29  *              Tigran Aivazian :       Restructured wdt_init() to handle failures
 30  */
 31 
 32 #include <linux/config.h>
 33 #include <linux/module.h>
 34 #include <linux/version.h>
 35 #include <linux/types.h>
 36 #include <linux/errno.h>
 37 #include <linux/kernel.h>
 38 #include <linux/sched.h>
 39 #include <linux/smp_lock.h>
 40 #include <linux/miscdevice.h>
 41 #include <linux/watchdog.h>
 42 #include "wd501p.h"
 43 #include <linux/malloc.h>
 44 #include <linux/ioport.h>
 45 #include <linux/fcntl.h>
 46 #include <asm/io.h>
 47 #include <asm/uaccess.h>
 48 #include <asm/system.h>
 49 #include <linux/notifier.h>
 50 #include <linux/reboot.h>
 51 #include <linux/init.h>
 52 
 53 static int wdt_is_open;
 54 
 55 /*
 56  *      You must set these - there is no sane way to probe for this board.
 57  *      You can use wdt=x,y to set these now.
 58  */
 59  
 60 static int io=0x240;
 61 static int irq=11;
 62 
 63 #define WD_TIMO (100*60)                /* 1 minute */
 64 
 65 #ifndef MODULE
 66 
 67 /**
 68  *      wdt_setup:
 69  *      @str: command line string
 70  *
 71  *      Setup options. The board isn't really probe-able so we have to
 72  *      get the user to tell us the configuration. Sane people build it 
 73  *      modular but the others come here.
 74  */
 75  
 76 static int __init wdt_setup(char *str)
 77 {
 78         int ints[4];
 79 
 80         str = get_options (str, ARRAY_SIZE(ints), ints);
 81 
 82         if (ints[0] > 0)
 83         {
 84                 io = ints[1];
 85                 if(ints[0] > 1)
 86                         irq = ints[2];
 87         }
 88 
 89         return 1;
 90 }
 91 
 92 __setup("wdt=", wdt_setup);
 93 
 94 #endif /* !MODULE */
 95 
 96 MODULE_PARM(io, "i");
 97 MODULE_PARM_DESC(io, "WDT io port (default=0x240)");
 98 MODULE_PARM(irq, "i");
 99 MODULE_PARM_DESC(irq, "WDT irq (default=11)");
100  
101 /*
102  *      Programming support
103  */
104  
105 static void wdt_ctr_mode(int ctr, int mode)
106 {
107         ctr<<=6;
108         ctr|=0x30;
109         ctr|=(mode<<1);
110         outb_p(ctr, WDT_CR);
111 }
112 
113 static void wdt_ctr_load(int ctr, int val)
114 {
115         outb_p(val&0xFF, WDT_COUNT0+ctr);
116         outb_p(val>>8, WDT_COUNT0+ctr);
117 }
118 
119 /*
120  *      Kernel methods.
121  */
122  
123  
124 /**
125  *      wdt_status:
126  *      
127  *      Extract the status information from a WDT watchdog device. There are
128  *      several board variants so we have to know which bits are valid. Some
129  *      bits default to one and some to zero in order to be maximally painful.
130  *
131  *      we then map the bits onto the status ioctl flags.
132  */
133  
134 static int wdt_status(void)
135 {
136         /*
137          *      Status register to bit flags
138          */
139          
140         int flag=0;
141         unsigned char status=inb_p(WDT_SR);
142         status|=FEATUREMAP1;
143         status&=~FEATUREMAP2;   
144         
145         if(!(status&WDC_SR_TGOOD))
146                 flag|=WDIOF_OVERHEAT;
147         if(!(status&WDC_SR_PSUOVER))
148                 flag|=WDIOF_POWEROVER;
149         if(!(status&WDC_SR_PSUUNDR))
150                 flag|=WDIOF_POWERUNDER;
151         if(!(status&WDC_SR_FANGOOD))
152                 flag|=WDIOF_FANFAULT;
153         if(status&WDC_SR_ISOI0)
154                 flag|=WDIOF_EXTERN1;
155         if(status&WDC_SR_ISII1)
156                 flag|=WDIOF_EXTERN2;
157         return flag;
158 }
159 
160 /**
161  *      wdt_interrupt:
162  *      @irq:           Interrupt number
163  *      @dev_id:        Unused as we don't allow multiple devices.
164  *      @regs:          Unused.
165  *
166  *      Handle an interrupt from the board. These are raised when the status
167  *      map changes in what the board considers an interesting way. That means
168  *      a failure condition occuring.
169  */
170  
171 void wdt_interrupt(int irq, void *dev_id, struct pt_regs *regs)
172 {
173         /*
174          *      Read the status register see what is up and
175          *      then printk it. 
176          */
177          
178         unsigned char status=inb_p(WDT_SR);
179         
180         status|=FEATUREMAP1;
181         status&=~FEATUREMAP2;   
182         
183         printk(KERN_CRIT "WDT status %d\n", status);
184         
185         if(!(status&WDC_SR_TGOOD))
186                 printk(KERN_CRIT "Overheat alarm.(%d)\n",inb_p(WDT_RT));
187         if(!(status&WDC_SR_PSUOVER))
188                 printk(KERN_CRIT "PSU over voltage.\n");
189         if(!(status&WDC_SR_PSUUNDR))
190                 printk(KERN_CRIT "PSU under voltage.\n");
191         if(!(status&WDC_SR_FANGOOD))
192                 printk(KERN_CRIT "Possible fan fault.\n");
193         if(!(status&WDC_SR_WCCR))
194 #ifdef SOFTWARE_REBOOT
195 #ifdef ONLY_TESTING
196                 printk(KERN_CRIT "Would Reboot.\n");
197 #else           
198                 printk(KERN_CRIT "Initiating system reboot.\n");
199                 machine_restart(NULL);
200 #endif          
201 #else
202                 printk(KERN_CRIT "Reset in 5ms.\n");
203 #endif          
204 }
205 
206 
207 static long long wdt_llseek(struct file *file, long long offset, int origin)
208 {
209         return -ESPIPE;
210 }
211 
212 /**
213  *      wdt_ping:
214  *
215  *      Reload counter one with the watchdog timeout. We don't bother reloading
216  *      the cascade counter. 
217  */
218  
219 static void wdt_ping(void)
220 {
221         /* Write a watchdog value */
222         inb_p(WDT_DC);
223         wdt_ctr_mode(1,2);
224         wdt_ctr_load(1,WD_TIMO);                /* Timeout */
225         outb_p(0, WDT_DC);
226 }
227 
228 /**
229  *      wdt_write:
230  *      @file: file handle to the watchdog
231  *      @buf: buffer to write (unused as data does not matter here 
232  *      @count: count of bytes
233  *      @ppos: pointer to the position to write. No seeks allowed
234  *
235  *      A write to a watchdog device is defined as a keepalive signal. Any
236  *      write of data will do, as we we don't define content meaning.
237  */
238  
239 static ssize_t wdt_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
240 {
241         /*  Can't seek (pwrite) on this device  */
242         if (ppos != &file->f_pos)
243                 return -ESPIPE;
244 
245         if(count)
246         {
247                 wdt_ping();
248                 return 1;
249         }
250         return 0;
251 }
252 
253 /**
254  *      wdt_read:
255  *      @file: file handle to the watchdog board
256  *      @buf: buffer to write 1 byte into
257  *      @count: length of buffer
258  *      @ptr: offset (no seek allowed)
259  *
260  *      Read reports the temperature in degrees Fahrenheit. The API is in
261  *      farenheit. It was designed by an imperial measurement luddite.
262  */
263  
264 static ssize_t wdt_read(struct file *file, char *buf, size_t count, loff_t *ptr)
265 {
266         unsigned short c=inb_p(WDT_RT);
267         unsigned char cp;
268         
269         /*  Can't seek (pread) on this device  */
270         if (ptr != &file->f_pos)
271                 return -ESPIPE;
272 
273         switch(MINOR(file->f_dentry->d_inode->i_rdev))
274         {
275                 case TEMP_MINOR:
276                         c*=11;
277                         c/=15;
278                         cp=c+7;
279                         if(copy_to_user(buf,&cp,1))
280                                 return -EFAULT;
281                         return 1;
282                 default:
283                         return -EINVAL;
284         }
285 }
286 
287 /**
288  *      wdt_ioctl:
289  *      @inode: inode of the device
290  *      @file: file handle to the device
291  *      @cmd: watchdog command
292  *      @arg: argument pointer
293  *
294  *      The watchdog API defines a common set of functions for all watchdogs
295  *      according to their available features. We only actually usefully support
296  *      querying capabilities and current status. 
297  */
298  
299 static int wdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
300         unsigned long arg)
301 {
302         static struct watchdog_info ident=
303         {
304                 WDIOF_OVERHEAT|WDIOF_POWERUNDER|WDIOF_POWEROVER
305                         |WDIOF_EXTERN1|WDIOF_EXTERN2|WDIOF_FANFAULT,
306                 1,
307                 "WDT500/501"
308         };
309         
310         ident.options&=WDT_OPTION_MASK; /* Mask down to the card we have */
311         switch(cmd)
312         {
313                 default:
314                         return -ENOIOCTLCMD;
315                 case WDIOC_GETSUPPORT:
316                         return copy_to_user((struct watchdog_info *)arg, &ident, sizeof(ident))?-EFAULT:0;
317 
318                 case WDIOC_GETSTATUS:
319                         return put_user(wdt_status(),(int *)arg);
320                 case WDIOC_GETBOOTSTATUS:
321                         return put_user(0, (int *)arg);
322                 case WDIOC_KEEPALIVE:
323                         wdt_ping();
324                         return 0;
325         }
326 }
327 
328 /**
329  *      wdt_open:
330  *      @inode: inode of device
331  *      @file: file handle to device
332  *
333  *      One of our two misc devices has been opened. The watchdog device is
334  *      single open and on opening we load the counters. Counter zero is a 
335  *      100Hz cascade, into counter 1 which downcounts to reboot. When the
336  *      counter triggers counter 2 downcounts the length of the reset pulse
337  *      which set set to be as long as possible. 
338  */
339  
340 static int wdt_open(struct inode *inode, struct file *file)
341 {
342         switch(MINOR(inode->i_rdev))
343         {
344                 case WATCHDOG_MINOR:
345                         if(wdt_is_open)
346                                 return -EBUSY;
347                         /*
348                          *      Activate 
349                          */
350          
351                         wdt_is_open=1;
352                         inb_p(WDT_DC);          /* Disable */
353                         wdt_ctr_mode(0,3);
354                         wdt_ctr_mode(1,2);
355                         wdt_ctr_mode(2,0);
356                         wdt_ctr_load(0, 8948);          /* count at 100Hz */
357                         wdt_ctr_load(1,WD_TIMO);        /* Timeout 120 seconds */
358                         wdt_ctr_load(2,65535);
359                         outb_p(0, WDT_DC);      /* Enable */
360                         return 0;
361                 case TEMP_MINOR:
362                         return 0;
363                 default:
364                         return -ENODEV;
365         }
366 }
367 
368 /**
369  *      wdt_close:
370  *      @inode: inode to board
371  *      @file: file handle to board
372  *
373  *      The watchdog has a configurable API. There is a religious dispute 
374  *      between people who want their watchdog to be able to shut down and 
375  *      those who want to be sure if the watchdog manager dies the machine
376  *      reboots. In the former case we disable the counters, in the latter
377  *      case you have to open it again very soon.
378  */
379  
380 static int wdt_release(struct inode *inode, struct file *file)
381 {
382         lock_kernel();
383         if(MINOR(inode->i_rdev)==WATCHDOG_MINOR)
384         {
385 #ifndef CONFIG_WATCHDOG_NOWAYOUT        
386                 inb_p(WDT_DC);          /* Disable counters */
387                 wdt_ctr_load(2,0);      /* 0 length reset pulses now */
388 #endif          
389                 wdt_is_open=0;
390         }
391         unlock_kernel();
392         return 0;
393 }
394 
395 /**
396  *      notify_sys:
397  *      @this: our notifier block
398  *      @code: the event being reported
399  *      @unused: unused
400  *
401  *      Our notifier is called on system shutdowns. We want to turn the card
402  *      off at reboot otherwise the machine will reboot again during memory
403  *      test or worse yet during the following fsck. This would suck, in fact
404  *      trust me - if it happens it does suck.
405  */
406 
407 static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
408         void *unused)
409 {
410         if(code==SYS_DOWN || code==SYS_HALT)
411         {
412                 /* Turn the card off */
413                 inb_p(WDT_DC);
414                 wdt_ctr_load(2,0);
415         }
416         return NOTIFY_DONE;
417 }
418  
419 /*
420  *      Kernel Interfaces
421  */
422  
423  
424 static struct file_operations wdt_fops = {
425         owner:          THIS_MODULE,
426         llseek:         wdt_llseek,
427         read:           wdt_read,
428         write:          wdt_write,
429         ioctl:          wdt_ioctl,
430         open:           wdt_open,
431         release:        wdt_release,
432 };
433 
434 static struct miscdevice wdt_miscdev=
435 {
436         WATCHDOG_MINOR,
437         "watchdog",
438         &wdt_fops
439 };
440 
441 #ifdef CONFIG_WDT_501
442 static struct miscdevice temp_miscdev=
443 {
444         TEMP_MINOR,
445         "temperature",
446         &wdt_fops
447 };
448 #endif
449 
450 /*
451  *      The WDT card needs to learn about soft shutdowns in order to
452  *      turn the timebomb registers off. 
453  */
454  
455 static struct notifier_block wdt_notifier=
456 {
457         wdt_notify_sys,
458         NULL,
459         0
460 };
461 
462 /**
463  *      cleanup_module:
464  *
465  *      Unload the watchdog. You cannot do this with any file handles open.
466  *      If your watchdog is set to continue ticking on close and you unload
467  *      it, well it keeps ticking. We won't get the interrupt but the board
468  *      will not touch PC memory so all is fine. You just have to load a new
469  *      module in 60 seconds or reboot.
470  */
471  
472 static void __exit wdt_exit(void)
473 {
474         misc_deregister(&wdt_miscdev);
475 #ifdef CONFIG_WDT_501   
476         misc_deregister(&temp_miscdev);
477 #endif  
478         unregister_reboot_notifier(&wdt_notifier);
479         release_region(io,8);
480         free_irq(irq, NULL);
481 }
482 
483 /**
484  *      wdt_init:
485  *
486  *      Set up the WDT watchdog board. All we have to do is grab the
487  *      resources we require and bitch if anyone beat us to them.
488  *      The open() function will actually kick the board off.
489  */
490  
491 static int __init wdt_init(void)
492 {
493         int ret;
494 
495         ret = misc_register(&wdt_miscdev);
496         if (ret) {
497                 printk(KERN_ERR "wdt: can't misc_register on minor=%d\n", WATCHDOG_MINOR);
498                 goto out;
499         }
500         ret = request_irq(irq, wdt_interrupt, SA_INTERRUPT, "wdt501p", NULL);
501         if(ret) {
502                 printk(KERN_ERR "wdt: IRQ %d is not free.\n", irq);
503                 goto outmisc;
504         }
505         if (!request_region(io, 8, "wdt501p")) {
506                 printk(KERN_ERR "wdt: IO %X is not free.\n", io);
507                 ret = -EBUSY;
508                 goto outirq;
509         }
510         ret = register_reboot_notifier(&wdt_notifier);
511         if(ret) {
512                 printk(KERN_ERR "wdt: can't register reboot notifier (err=%d)\n", ret);
513                 goto outreg;
514         }
515 
516 #ifdef CONFIG_WDT_501
517         ret = misc_register(&temp_miscdev);
518         if (ret) {
519                 printk(KERN_ERR "wdt: can't misc_register (temp) on minor=%d\n", TEMP_MINOR);
520                 goto outrbt;
521         }
522 #endif
523 
524         ret = 0;
525         printk(KERN_INFO "WDT500/501-P driver 0.07 at %X (Interrupt %d)\n", io, irq);
526 out:
527         return ret;
528 
529 #ifdef CONFIG_WDT_501
530 outrbt:
531         unregister_reboot_notifier(&wdt_notifier);
532 #endif
533 
534 outreg:
535         release_region(io,8);
536 outirq:
537         free_irq(irq, NULL);
538 outmisc:
539         misc_deregister(&wdt_miscdev);
540         goto out;
541 }
542 
543 module_init(wdt_init);
544 module_exit(wdt_exit);
545 
546 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.