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