blob: 67b2f55a602f6db174f95f4e0b843f4adda97d2e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Jeff Dikecb8fa612007-10-16 01:27:34 -070012#include <linux/stddef.h>
13#include <linux/types.h>
Alexey Dobriyan4e950f62007-07-30 02:36:13 +040014#include <linux/fs.h>
Jeff Dikecb8fa612007-10-16 01:27:34 -070015#include <linux/init.h>
Jeff Dike3df59522005-06-08 15:47:50 -070016#include <linux/miscdevice.h>
Jeff Dikecb8fa612007-10-16 01:27:34 -070017#include <linux/module.h>
18#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "mem_user.h"
Jeff Dikecb8fa612007-10-16 01:27:34 -070021
Linus Torvalds1da177e2005-04-16 15:20:36 -070022/* These are set in mmapper_init, which is called at boot time */
23static unsigned long mmapper_size;
Jeff Dikecb8fa612007-10-16 01:27:34 -070024static unsigned long p_buf;
25static char *v_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Jeff Dikecb8fa612007-10-16 01:27:34 -070027static ssize_t mmapper_read(struct file *file, char __user *buf, size_t count,
28 loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029{
Al Viro6a029a92005-08-27 06:48:15 +010030 return simple_read_from_buffer(buf, count, ppos, v_buf, mmapper_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031}
32
Jeff Dikecb8fa612007-10-16 01:27:34 -070033static ssize_t mmapper_write(struct file *file, const char __user *buf,
34 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
Al Viro6a029a92005-08-27 06:48:15 +010036 if (*ppos > mmapper_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 return -EINVAL;
38
Al Viro6a029a92005-08-27 06:48:15 +010039 if (count > mmapper_size - *ppos)
40 count = mmapper_size - *ppos;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Al Viro6a029a92005-08-27 06:48:15 +010042 if (copy_from_user(&v_buf[*ppos], buf, count))
43 return -EFAULT;
Jeff Dikecb8fa612007-10-16 01:27:34 -070044
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 return count;
46}
47
Jeff Dikecb8fa612007-10-16 01:27:34 -070048static int mmapper_ioctl(struct inode *inode, struct file *file,
49 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
Jeff Dikecb8fa612007-10-16 01:27:34 -070051 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052}
53
Jeff Dikecb8fa612007-10-16 01:27:34 -070054static int mmapper_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
56 int ret = -EINVAL;
57 int size;
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 if (vma->vm_pgoff != 0)
60 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Jeff Dikecb8fa612007-10-16 01:27:34 -070062 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 * called when the mm semaphore is held
69 */
70 if (remap_pfn_range(vma, vma->vm_start, p_buf >> PAGE_SHIFT, size,
Jeff Dikecb8fa612007-10-16 01:27:34 -070071 vma->vm_page_prot))
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 goto out;
73 ret = 0;
74out:
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 return ret;
76}
77
Jeff Dikecb8fa612007-10-16 01:27:34 -070078static int mmapper_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
80 return 0;
81}
82
Jeff Dikecb8fa612007-10-16 01:27:34 -070083static int mmapper_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
85 return 0;
86}
87
Jeff Dike5e7672e2006-09-27 01:50:33 -070088static const struct file_operations mmapper_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 .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
Jeff Dikecb8fa612007-10-16 01:27:34 -070098/*
99 * No locking needed - only used (and modified) by below initcall and exitcall.
100 */
Paolo 'Blaisorblade' Giarrusso1ba0ce62006-10-19 23:28:26 -0700101static struct miscdevice mmapper_dev = {
Jeff Dike3df59522005-06-08 15:47:50 -0700102 .minor = MISC_DYNAMIC_MINOR,
103 .name = "mmapper",
104 .fops = &mmapper_fops
105};
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107static int __init mmapper_init(void)
108{
Jeff Dike3df59522005-06-08 15:47:50 -0700109 int err;
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 printk(KERN_INFO "Mapper v0.1\n");
112
113 v_buf = (char *) find_iomem("mmapper", &mmapper_size);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700114 if (mmapper_size == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 printk(KERN_ERR "mmapper_init - find_iomem failed\n");
Jeff Dike3df59522005-06-08 15:47:50 -0700116 goto out;
117 }
118
119 err = misc_register(&mmapper_dev);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700120 if (err) {
Jeff Dike3df59522005-06-08 15:47:50 -0700121 printk(KERN_ERR "mmapper - misc_register failed, err = %d\n",
122 err);
123 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 }
125
126 p_buf = __pa(v_buf);
Jeff Dike3df59522005-06-08 15:47:50 -0700127out:
128 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
131static void mmapper_exit(void)
132{
Jeff Dike3df59522005-06-08 15:47:50 -0700133 misc_deregister(&mmapper_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
136module_init(mmapper_init);
137module_exit(mmapper_exit);
138
139MODULE_AUTHOR("Greg Lonnon <glonnon@ridgerun.com>");
140MODULE_DESCRIPTION("DSPLinux simulator mmapper driver");