This commit was manufactured by cvs2svn to create branch 'fedora'.
[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/config.h>
28 #include <linux/types.h>
29 #include <linux/miscdevice.h>
30 #include <linux/init.h>
31 #include <asm/io.h>
32 #include <asm/uaccess.h>
33 #include <asm/types.h>
34 #include <asm/crash.h>
35
36 #define CRASH_VERSION   "1.0"
37
38 /*
39  *  These are the file operation functions that allow crash utility
40  *  access to physical memory.
41  */
42
43 static loff_t 
44 crash_llseek(struct file * file, loff_t offset, int orig)
45 {
46         switch (orig) {
47                 case 0:
48                         file->f_pos = offset;
49                         return file->f_pos;
50                 case 1:
51                         file->f_pos += offset;
52                         return file->f_pos;
53                 default:
54                         return -EINVAL;
55         }
56 }
57
58 /*
59  *  Determine the page address for an address offset value, 
60  *  get a virtual address for it, and copy it out.
61  *  Accesses must fit within a page.
62  */
63 static ssize_t
64 crash_read(struct file *file, char *buf, size_t count, loff_t *poff)
65 {
66         void *vaddr;
67         struct page *page;
68         u64 offset;
69         ssize_t read;
70
71         offset = *poff;
72         if (offset >> PAGE_SHIFT != (offset+count-1) >> PAGE_SHIFT) 
73                 return -EINVAL;
74
75         vaddr = map_virtual(offset, &page);
76         if (!vaddr)
77                 return -EFAULT;
78
79         if (copy_to_user(buf, vaddr, count)) {
80                 unmap_virtual(page);
81                 return -EFAULT;
82         }
83         unmap_virtual(page);
84
85         read = count;
86         *poff += read;
87         return read;
88 }
89
90 static struct file_operations crash_fops = {
91         owner:          THIS_MODULE,
92         llseek:         crash_llseek,
93         read:           crash_read,
94 };
95
96 static struct miscdevice crash_dev = {
97         MISC_DYNAMIC_MINOR,
98         "crash",
99         &crash_fops
100 };
101
102 static int __init
103 crash_init(void)
104 {
105         int ret;
106
107         ret = misc_register(&crash_dev);
108         if (ret) {
109                 printk(KERN_ERR 
110                     "crash memory driver: cannot misc_register (MISC_DYNAMIC_MINOR)\n");
111                 goto out;
112         }
113         
114         ret = 0;
115         printk(KERN_INFO "crash memory driver: version %s\n", CRASH_VERSION);
116 out:
117         return ret;
118 }
119
120 static void __exit
121 crash_cleanup_module(void)
122 {
123         misc_deregister(&crash_dev);
124 }
125
126 module_init(crash_init);
127 module_exit(crash_cleanup_module);
128
129 MODULE_LICENSE("GPL");