Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]
Version: [ 2.6.11.8 ] [ 2.6.25 ] [ 2.6.25.8 ] [ 2.6.31.13 ] Architecture: [ i386 ]
  1 /*
  2  *      Watchdog for the 7101 PMU version found in the ALi M1535 chipsets
  3  *
  4  *      This program is free software; you can redistribute it and/or
  5  *      modify it under the terms of the GNU General Public License
  6  *      as published by the Free Software Foundation; either version
  7  *      2 of the License, or (at your option) any later version.
  8  */
  9 
 10 #include <linux/module.h>
 11 #include <linux/moduleparam.h>
 12 #include <linux/types.h>
 13 #include <linux/miscdevice.h>
 14 #include <linux/watchdog.h>
 15 #include <linux/ioport.h>
 16 #include <linux/notifier.h>
 17 #include <linux/reboot.h>
 18 #include <linux/init.h>
 19 #include <linux/fs.h>
 20 #include <linux/pci.h>
 21 
 22 #include <asm/uaccess.h>
 23 #include <asm/io.h>
 24 
 25 #define WATCHDOG_NAME "ALi_M1535"
 26 #define PFX WATCHDOG_NAME ": "
 27 #define WATCHDOG_TIMEOUT 60     /* 60 sec default timeout */
 28 
 29 /* internal variables */
 30 static unsigned long ali_is_open;
 31 static char ali_expect_release;
 32 static struct pci_dev *ali_pci;
 33 static u32 ali_timeout_bits;    /* stores the computed timeout */
 34 static spinlock_t ali_lock;     /* Guards the hardware */
 35 
 36 /* module parameters */
 37 static int timeout = WATCHDOG_TIMEOUT;
 38 module_param(timeout, int, 0);
 39 MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds. (0<timeout<18000, default=" __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
 40 
 41 #ifdef CONFIG_WATCHDOG_NOWAYOUT
 42 static int nowayout = 1;
 43 #else
 44 static int nowayout = 0;
 45 #endif
 46 
 47 module_param(nowayout, int, 0);
 48 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
 49 
 50 /*
 51  *      ali_start       -       start watchdog countdown
 52  *
 53  *      Starts the timer running providing the timer has a counter
 54  *      configuration set.
 55  */
 56 
 57 static void ali_start(void)
 58 {
 59         u32 val;
 60 
 61         spin_lock(&ali_lock);
 62 
 63         pci_read_config_dword(ali_pci, 0xCC, &val);
 64         val &= ~0x3F;   /* Mask count */
 65         val |= (1<<25) | ali_timeout_bits;
 66         pci_write_config_dword(ali_pci, 0xCC, val);
 67 
 68         spin_unlock(&ali_lock);
 69 }
 70 
 71 /*
 72  *      ali_stop        -       stop the timer countdown
 73  *
 74  *      Stop the ALi watchdog countdown
 75  */
 76 
 77 static void ali_stop(void)
 78 {
 79         u32 val;
 80 
 81         spin_lock(&ali_lock);
 82 
 83         pci_read_config_dword(ali_pci, 0xCC, &val);
 84         val &= ~0x3F;   /* Mask count to zero (disabled) */
 85         val &= ~(1<<25);/* and for safety mask the reset enable */
 86         pci_write_config_dword(ali_pci, 0xCC, val);
 87 
 88         spin_unlock(&ali_lock);
 89 }
 90 
 91 /*
 92  *      ali_keepalive   -       send a keepalive to the watchdog
 93  *
 94  *      Send a keepalive to the timer (actually we restart the timer).
 95  */
 96 
 97 static void ali_keepalive(void)
 98 {
 99         ali_start();
100 }
101 
102 /*
103  *      ali_settimer    -       compute the timer reload value
104  *      @t: time in seconds
105  *
106  *      Computes the timeout values needed
107  */
108 
109 static int ali_settimer(int t)
110 {
111         if(t < 0)
112                 return -EINVAL;
113         else if(t < 60)
114                 ali_timeout_bits = t|(1<<6);
115         else if(t < 3600)
116                 ali_timeout_bits = (t/60)|(1<<7);
117         else if(t < 18000)
118                 ali_timeout_bits = (t/300)|(1<<6)|(1<<7);
119         else return -EINVAL;
120 
121         timeout = t;
122         return 0;
123 }
124 
125 /*
126  *      /dev/watchdog handling
127  */
128 
129 /*
130  *      ali_write       -       writes to ALi watchdog
131  *      @file: file from VFS
132  *      @data: user address of data
133  *      @len: length of data
134  *      @ppos: pointer to the file offset
135  *
136  *      Handle a write to the ALi watchdog. Writing to the file pings
137  *      the watchdog and resets it. Writing the magic 'V' sequence allows
138  *      the next close to turn off the watchdog.
139  */
140 
141 static ssize_t ali_write(struct file *file, const char __user *data,
142                               size_t len, loff_t * ppos)
143 {
144         /* See if we got the magic character 'V' and reload the timer */
145         if (len) {
146                 if (!nowayout) {
147                         size_t i;
148 
149                         /* note: just in case someone wrote the magic character
150                          * five months ago... */
151                         ali_expect_release = 0;
152 
153                         /* scan to see whether or not we got the magic character */
154                         for (i = 0; i != len; i++) {
155                                 char c;
156                                 if(get_user(c, data+i))
157                                         return -EFAULT;
158                                 if (c == 'V')
159                                         ali_expect_release = 42;
160                         }
161                 }
162 
163                 /* someone wrote to us, we should reload the timer */
164                 ali_start();
165         }
166         return len;
167 }
168 
169 /*
170  *      ali_ioctl       -       handle watchdog ioctls
171  *      @inode: VFS inode
172  *      @file: VFS file pointer
173  *      @cmd: ioctl number
174  *      @arg: arguments to the ioctl
175  *
176  *      Handle the watchdog ioctls supported by the ALi driver. Really
177  *      we want an extension to enable irq ack monitoring and the like
178  */
179 
180 static int ali_ioctl(struct inode *inode, struct file *file,
181                           unsigned int cmd, unsigned long arg)
182 {
183         void __user *argp = (void __user *)arg;
184         int __user *p = argp;
185         static struct watchdog_info ident = {
186                 .options =              WDIOF_KEEPALIVEPING |
187                                         WDIOF_SETTIMEOUT |
188                                         WDIOF_MAGICCLOSE,
189                 .firmware_version =     0,
190                 .identity =             "ALi M1535 WatchDog Timer",
191         };
192 
193         switch (cmd) {
194                 case WDIOC_GETSUPPORT:
195                         return copy_to_user(argp, &ident,
196                                 sizeof (ident)) ? -EFAULT : 0;
197 
198                 case WDIOC_GETSTATUS:
199                 case WDIOC_GETBOOTSTATUS:
200                         return put_user(0, p);
201 
202                 case WDIOC_KEEPALIVE:
203                         ali_keepalive();
204                         return 0;
205 
206                 case WDIOC_SETOPTIONS:
207                 {
208                         int new_options, retval = -EINVAL;
209 
210                         if (get_user (new_options, p))
211                                 return -EFAULT;
212 
213                         if (new_options & WDIOS_DISABLECARD) {
214                                 ali_stop();
215                                 retval = 0;
216                         }
217 
218                         if (new_options & WDIOS_ENABLECARD) {
219                                 ali_start();
220                                 retval = 0;
221                         }
222 
223                         return retval;
224                 }
225 
226                 case WDIOC_SETTIMEOUT:
227                 {
228                         int new_timeout;
229 
230                         if (get_user(new_timeout, p))
231                                 return -EFAULT;
232 
233                         if (ali_settimer(new_timeout))
234                             return -EINVAL;
235 
236                         ali_keepalive();
237                         /* Fall */
238                 }
239 
240                 case WDIOC_GETTIMEOUT:
241                         return put_user(timeout, p);
242 
243                 default:
244                         return -ENOIOCTLCMD;
245         }
246 }
247 
248 /*
249  *      ali_open        -       handle open of ali watchdog
250  *      @inode: inode from VFS
251  *      @file: file from VFS
252  *
253  *      Open the ALi watchdog device. Ensure only one person opens it
254  *      at a time. Also start the watchdog running.
255  */
256 
257 static int ali_open(struct inode *inode, struct file *file)
258 {
259         /* /dev/watchdog can only be opened once */
260         if (test_and_set_bit(0, &ali_is_open))
261                 return -EBUSY;
262 
263         /* Activate */
264         ali_start();
265         return nonseekable_open(inode, file);
266 }
267 
268 /*
269  *      ali_release     -       close an ALi watchdog
270  *      @inode: inode from VFS
271  *      @file: file from VFS
272  *
273  *      Close the ALi watchdog device. Actual shutdown of the timer
274  *      only occurs if the magic sequence has been set.
275  */
276 
277 static int ali_release(struct inode *inode, struct file *file)
278 {
279         /*
280          *      Shut off the timer.
281          */
282         if (ali_expect_release == 42) {
283                 ali_stop();
284         } else {
285                 printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
286                 ali_keepalive();
287         }
288         clear_bit(0, &ali_is_open);
289         ali_expect_release = 0;
290         return 0;
291 }
292 
293 /*
294  *      ali_notify_sys  -       System down notifier
295  *
296  *      Notifier for system down
297  */
298 
299 
300 static int ali_notify_sys(struct notifier_block *this, unsigned long code, void *unused)
301 {
302         if (code==SYS_DOWN || code==SYS_HALT) {
303                 /* Turn the WDT off */
304                 ali_stop();
305         }
306 
307         return NOTIFY_DONE;
308 }
309 
310 /*
311  *      Data for PCI driver interface
312  *
313  *      This data only exists for exporting the supported
314  *      PCI ids via MODULE_DEVICE_TABLE.  We do not actually
315  *      register a pci_driver, because someone else might one day
316  *      want to register another driver on the same PCI id.
317  */
318 
319 static struct pci_device_id ali_pci_tbl[] = {
320         { PCI_VENDOR_ID_AL, 1535, PCI_ANY_ID, PCI_ANY_ID,},
321         { 0, },
322 };
323 MODULE_DEVICE_TABLE(pci, ali_pci_tbl);
324 
325 /*
326  *      ali_find_watchdog       -       find a 1535 and 7101
327  *
328  *      Scans the PCI hardware for a 1535 series bridge and matching 7101
329  *      watchdog device. This may be overtight but it is better to be safe
330  */
331 
332 static int __init ali_find_watchdog(void)
333 {
334         struct pci_dev *pdev;
335         u32 wdog;
336 
337         /* Check for a 1535 series bridge */
338         pdev = pci_find_device(PCI_VENDOR_ID_AL, 0x1535, NULL);
339         if(pdev == NULL)
340                 return -ENODEV;
341 
342         /* Check for the a 7101 PMU */
343         pdev = pci_find_device(PCI_VENDOR_ID_AL, 0x7101, NULL);
344         if(pdev == NULL)
345                 return -ENODEV;
346 
347         if(pci_enable_device(pdev))
348                 return -EIO;
349 
350         ali_pci = pdev;
351 
352         /*
353          *      Initialize the timer bits
354          */
355         pci_read_config_dword(pdev, 0xCC, &wdog);
356 
357         wdog &= ~0x3F;          /* Timer bits */
358         wdog &= ~((1<<27)|(1<<26)|(1<<25)|(1<<24));     /* Issued events */
359         wdog &= ~((1<<16)|(1<<13)|(1<<12)|(1<<11)|(1<<10)|(1<<9));      /* No monitor bits */
360 
361         pci_write_config_dword(pdev, 0xCC, wdog);
362 
363         return 0;
364 }
365 
366 /*
367  *      Kernel Interfaces
368  */
369 
370 static struct file_operations ali_fops = {
371         .owner =        THIS_MODULE,
372         .llseek =       no_llseek,
373         .write =        ali_write,
374         .ioctl =        ali_ioctl,
375         .open =         ali_open,
376         .release =      ali_release,
377 };
378 
379 static struct miscdevice ali_miscdev = {
380         .minor =        WATCHDOG_MINOR,
381         .name =         "watchdog",
382         .fops =         &ali_fops,
383 };
384 
385 static struct notifier_block ali_notifier = {
386         .notifier_call =        ali_notify_sys,
387 };
388 
389 /*
390  *      watchdog_init   -       module initialiser
391  *
392  *      Scan for a suitable watchdog and if so initialize it. Return an error
393  *      if we cannot, the error causes the module to unload
394  */
395 
396 static int __init watchdog_init(void)
397 {
398         int ret;
399 
400         spin_lock_init(&ali_lock);
401 
402         /* Check whether or not the hardware watchdog is there */
403         if (ali_find_watchdog() != 0) {
404                 return -ENODEV;
405         }
406 
407         /* Check that the timeout value is within it's range ; if not reset to the default */
408         if (timeout < 1 || timeout >= 18000) {
409                 timeout = WATCHDOG_TIMEOUT;
410                 printk(KERN_INFO PFX "timeout value must be 0<timeout<18000, using %d\n",
411                         timeout);
412         }
413 
414         /* Calculate the watchdog's timeout */
415         ali_settimer(timeout);
416 
417         ret = misc_register(&ali_miscdev);
418         if (ret != 0) {
419                 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
420                         WATCHDOG_MINOR, ret);
421                 goto out;
422         }
423 
424         ret = register_reboot_notifier(&ali_notifier);
425         if (ret != 0) {
426                 printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
427                         ret);
428                 goto unreg_miscdev;
429         }
430 
431         printk(KERN_INFO PFX "initialized. timeout=%d sec (nowayout=%d)\n",
432                 timeout, nowayout);
433 
434 out:
435         return ret;
436 unreg_miscdev:
437         misc_deregister(&ali_miscdev);
438         goto out;
439 }
440 
441 /*
442  *      watchdog_exit   -       module de-initialiser
443  *
444  *      Called while unloading a successfully installed watchdog module.
445  */
446 
447 static void __exit watchdog_exit(void)
448 {
449         /* Stop the timer before we leave */
450         ali_stop();
451 
452         /* Deregister */
453         unregister_reboot_notifier(&ali_notifier);
454         misc_deregister(&ali_miscdev);
455 }
456 
457 module_init(watchdog_init);
458 module_exit(watchdog_exit);
459 
460 MODULE_AUTHOR("Alan Cox");
461 MODULE_DESCRIPTION("ALi M1535 PMU Watchdog Timer driver");
462 MODULE_LICENSE("GPL");
463 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
464 
  This page was automatically generated by the LXR engine.