mmapper_kern.c 2.95 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
2
3
4
5
6
7
8
9
10
11
/*
 * arch/um/drivers/mmapper_kern.c
 *
 * BRIEF MODULE DESCRIPTION
 *
 * Copyright (C) 2000 RidgeRun, Inc.
 * Author: RidgeRun, Inc.
 *         Greg Lonnon glonnon@ridgerun.com or info@ridgerun.com
 *
 */

12
#include <linux/init.h> 
Linus Torvalds's avatar
Linus Torvalds committed
13
14
#include <linux/module.h>
#include <linux/mm.h> 
15
#include <linux/miscdevice.h>
Linus Torvalds's avatar
Linus Torvalds committed
16
17
18
19
20
21
22
23
24
25
#include <asm/uaccess.h>
#include "mem_user.h"
#include "user_util.h"
 
/* These are set in mmapper_init, which is called at boot time */
static unsigned long mmapper_size;
static unsigned long p_buf = 0;
static char *v_buf = NULL;

static ssize_t
26
mmapper_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
Linus Torvalds's avatar
Linus Torvalds committed
27
{
28
	return simple_read_from_buffer(buf, count, ppos, v_buf, mmapper_size);
Linus Torvalds's avatar
Linus Torvalds committed
29
30
31
}

static ssize_t
32
mmapper_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
Linus Torvalds's avatar
Linus Torvalds committed
33
{
34
	if (*ppos > mmapper_size)
Linus Torvalds's avatar
Linus Torvalds committed
35
36
		return -EINVAL;

37
38
	if (count > mmapper_size - *ppos)
		count = mmapper_size - *ppos;
Linus Torvalds's avatar
Linus Torvalds committed
39

40
41
	if (copy_from_user(&v_buf[*ppos], buf, count))
		return -EFAULT;
Linus Torvalds's avatar
Linus Torvalds committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
	
	return count;
}

static int 
mmapper_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
	 unsigned long arg)
{
	return(-ENOIOCTLCMD);
}

static int 
mmapper_mmap(struct file *file, struct vm_area_struct * vma)
{
	int ret = -EINVAL;
	int size;

	if (vma->vm_pgoff != 0)
		goto out;
	
	size = vma->vm_end - vma->vm_start;
	if(size > mmapper_size) return(-EFAULT);

	/* XXX A comment above remap_pfn_range says it should only be
	 * called when the mm semaphore is held
	 */
	if (remap_pfn_range(vma, vma->vm_start, p_buf >> PAGE_SHIFT, size,
			     vma->vm_page_prot))
		goto out;
	ret = 0;
out:
	return ret;
}

static int
mmapper_open(struct inode *inode, struct file *file)
{
	return 0;
}

static int 
mmapper_release(struct inode *inode, struct file *file)
{
	return 0;
}

Jeff Dike's avatar
Jeff Dike committed
88
static const struct file_operations mmapper_fops = {
Linus Torvalds's avatar
Linus Torvalds committed
89
90
91
92
93
94
95
96
97
	.owner		= THIS_MODULE,
	.read		= mmapper_read,
	.write		= mmapper_write,
	.ioctl		= mmapper_ioctl,
	.mmap		= mmapper_mmap,
	.open		= mmapper_open,
	.release	= mmapper_release,
};

98
99
/* No locking needed - only used (and modified) by below initcall and exitcall. */
static struct miscdevice mmapper_dev = {
100
101
102
103
104
	.minor		= MISC_DYNAMIC_MINOR,
	.name		= "mmapper",
	.fops		= &mmapper_fops
};

Linus Torvalds's avatar
Linus Torvalds committed
105
106
static int __init mmapper_init(void)
{
107
108
	int err;

Linus Torvalds's avatar
Linus Torvalds committed
109
110
111
112
113
	printk(KERN_INFO "Mapper v0.1\n");

	v_buf = (char *) find_iomem("mmapper", &mmapper_size);
	if(mmapper_size == 0){
		printk(KERN_ERR "mmapper_init - find_iomem failed\n");
114
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
115
116
	}

117
118
119
120
121
122
	err = misc_register(&mmapper_dev);
	if(err){
		printk(KERN_ERR "mmapper - misc_register failed, err = %d\n",
		       err);
		goto out;
	}
Linus Torvalds's avatar
Linus Torvalds committed
123

124
125
126
	p_buf = __pa(v_buf);
out:
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
127
128
129
130
}

static void mmapper_exit(void)
{
131
	misc_deregister(&mmapper_dev);
Linus Torvalds's avatar
Linus Torvalds committed
132
133
134
135
136
137
138
139
140
141
142
143
144
}

module_init(mmapper_init);
module_exit(mmapper_exit);

MODULE_AUTHOR("Greg Lonnon <glonnon@ridgerun.com>");
MODULE_DESCRIPTION("DSPLinux simulator mmapper driver");
/*
 * ---------------------------------------------------------------------------
 * Local variables:
 * c-file-style: "linux"
 * End:
 */