Merge branch 'merge' of git://git.kernel.org/pub/scm/linux/kernel/git/paulus/powerpc
[linux-2.6] / arch / um / drivers / mmapper_kern.c
1 /*
2  * arch/um/drivers/mmapper_kern.c
3  *
4  * BRIEF MODULE DESCRIPTION
5  *
6  * Copyright (C) 2000 RidgeRun, Inc.
7  * Author: RidgeRun, Inc.
8  *         Greg Lonnon glonnon@ridgerun.com or info@ridgerun.com
9  *
10  */
11
12 #include <linux/init.h> 
13 #include <linux/module.h>
14 #include <linux/mm.h> 
15 #include <linux/miscdevice.h>
16 #include <asm/uaccess.h>
17 #include "mem_user.h"
18  
19 /* These are set in mmapper_init, which is called at boot time */
20 static unsigned long mmapper_size;
21 static unsigned long p_buf = 0;
22 static char *v_buf = NULL;
23
24 static ssize_t
25 mmapper_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
26 {
27         return simple_read_from_buffer(buf, count, ppos, v_buf, mmapper_size);
28 }
29
30 static ssize_t
31 mmapper_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
32 {
33         if (*ppos > mmapper_size)
34                 return -EINVAL;
35
36         if (count > mmapper_size - *ppos)
37                 count = mmapper_size - *ppos;
38
39         if (copy_from_user(&v_buf[*ppos], buf, count))
40                 return -EFAULT;
41         
42         return count;
43 }
44
45 static int 
46 mmapper_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
47          unsigned long arg)
48 {
49         return(-ENOIOCTLCMD);
50 }
51
52 static int 
53 mmapper_mmap(struct file *file, struct vm_area_struct * vma)
54 {
55         int ret = -EINVAL;
56         int size;
57
58         if (vma->vm_pgoff != 0)
59                 goto out;
60         
61         size = vma->vm_end - vma->vm_start;
62         if(size > mmapper_size) return(-EFAULT);
63
64         /* XXX A comment above remap_pfn_range says it should only be
65          * called when the mm semaphore is held
66          */
67         if (remap_pfn_range(vma, vma->vm_start, p_buf >> PAGE_SHIFT, size,
68                              vma->vm_page_prot))
69                 goto out;
70         ret = 0;
71 out:
72         return ret;
73 }
74
75 static int
76 mmapper_open(struct inode *inode, struct file *file)
77 {
78         return 0;
79 }
80
81 static int 
82 mmapper_release(struct inode *inode, struct file *file)
83 {
84         return 0;
85 }
86
87 static const struct file_operations mmapper_fops = {
88         .owner          = THIS_MODULE,
89         .read           = mmapper_read,
90         .write          = mmapper_write,
91         .ioctl          = mmapper_ioctl,
92         .mmap           = mmapper_mmap,
93         .open           = mmapper_open,
94         .release        = mmapper_release,
95 };
96
97 /* No locking needed - only used (and modified) by below initcall and exitcall. */
98 static struct miscdevice mmapper_dev = {
99         .minor          = MISC_DYNAMIC_MINOR,
100         .name           = "mmapper",
101         .fops           = &mmapper_fops
102 };
103
104 static int __init mmapper_init(void)
105 {
106         int err;
107
108         printk(KERN_INFO "Mapper v0.1\n");
109
110         v_buf = (char *) find_iomem("mmapper", &mmapper_size);
111         if(mmapper_size == 0){
112                 printk(KERN_ERR "mmapper_init - find_iomem failed\n");
113                 goto out;
114         }
115
116         err = misc_register(&mmapper_dev);
117         if(err){
118                 printk(KERN_ERR "mmapper - misc_register failed, err = %d\n",
119                        err);
120                 goto out;
121         }
122
123         p_buf = __pa(v_buf);
124 out:
125         return 0;
126 }
127
128 static void mmapper_exit(void)
129 {
130         misc_deregister(&mmapper_dev);
131 }
132
133 module_init(mmapper_init);
134 module_exit(mmapper_exit);
135
136 MODULE_AUTHOR("Greg Lonnon <glonnon@ridgerun.com>");
137 MODULE_DESCRIPTION("DSPLinux simulator mmapper driver");
138 /*
139  * ---------------------------------------------------------------------------
140  * Local variables:
141  * c-file-style: "linux"
142  * End:
143  */