Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / drivers / char / crash.c
1 /*
2  *  linux/drivers/char/crash.c
3  *
4  *  Copyright (C) 2004  Dave Anderson <anderson@redhat.com>
5  *  Copyright (C) 2004  Red Hat, Inc.
6  */
7
8 /******************************************************************************
9  *
10  *   This program is free software; you can redistribute it and/or modify
11  *   it under the terms of the GNU General Public License as published by
12  *   the Free Software Foundation; either version 2, or (at your option)
13  *   any later version.
14  *
15  *   This program is distributed in the hope that it will be useful,
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *   GNU General Public License for more details.
19  *
20  *   You should have received a copy of the GNU General Public License
21  *   along with this program; if not, write to the Free Software
22  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  *
24  *****************************************************************************/
25
26 #include <linux/module.h>
27 #include <linux/types.h>
28 #include <linux/miscdevice.h>
29 #include <linux/init.h>
30 #include <asm/io.h>
31 #include <asm/uaccess.h>
32 #include <asm/types.h>
33 #include <asm/crash.h>
34
35 #define CRASH_VERSION   "1.0"
36
37 /*
38  *  These are the file operation functions that allow crash utility
39  *  access to physical memory.
40  */
41
42 static loff_t 
43 crash_llseek(struct file * file, loff_t offset, int orig)
44 {
45         switch (orig) {
46         case 0:
47                 file->f_pos = offset;
48                 return file->f_pos;
49         case 1:
50                 file->f_pos += offset;
51                 return file->f_pos;
52         default:
53                 return -EINVAL;
54         }
55 }
56
57 /*
58  *  Determine the page address for an address offset value, 
59  *  get a virtual address for it, and copy it out.
60  *  Accesses must fit within a page.
61  */
62 static ssize_t
63 crash_read(struct file *file, char *buf, size_t count, loff_t *poff)
64 {
65         void *vaddr;
66         struct page *page;
67         u64 offset;
68         ssize_t read;
69
70         offset = *poff;
71         if (offset >> PAGE_SHIFT != (offset+count-1) >> PAGE_SHIFT) 
72                 return -EINVAL;
73
74         vaddr = map_virtual(offset, &page);
75         if (!vaddr)
76                 return -EFAULT;
77
78         if (copy_to_user(buf, vaddr, count)) {
79                 unmap_virtual(page);
80                 return -EFAULT;
81         }
82         unmap_virtual(page);
83
84         read = count;
85         *poff += read;
86         return read;
87 }
88
89 static struct file_operations crash_fops = {
90         .owner = THIS_MODULE,
91         .llseek = crash_llseek,
92         .read = crash_read,
93 };
94
95 static struct miscdevice crash_dev = {
96         MISC_DYNAMIC_MINOR,
97         "crash",
98         &crash_fops
99 };
100
101 static int __init
102 crash_init(void)
103 {
104         int ret;
105
106         ret = misc_register(&crash_dev);
107         if (ret) {
108                 printk(KERN_ERR 
109                     "crash memory driver: cannot misc_register (MISC_DYNAMIC_MINOR)\n");
110                 goto out;
111         }
112         
113         ret = 0;
114         printk(KERN_INFO "crash memory driver: version %s\n", CRASH_VERSION);
115 out:
116         return ret;
117 }
118
119 static void __exit
120 crash_cleanup_module(void)
121 {
122         misc_deregister(&crash_dev);
123 }
124
125 module_init(crash_init);
126 module_exit(crash_cleanup_module);
127
128 MODULE_LICENSE("GPL");