blob: eb240323c40ad6a52883b079f2324c57ae9eb09f [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>
Arnd Bergmannbd6859f2008-05-20 19:16:56 +020019#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "mem_user.h"
Jeff Dikecb8fa612007-10-16 01:27:34 -070022
Linus Torvalds1da177e2005-04-16 15:20:36 -070023/* These are set in mmapper_init, which is called at boot time */
24static unsigned long mmapper_size;
Jeff Dikecb8fa612007-10-16 01:27:34 -070025static unsigned long p_buf;
26static char *v_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Jeff Dikecb8fa612007-10-16 01:27:34 -070028static ssize_t mmapper_read(struct file *file, char __user *buf, size_t count,
29 loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030{
Al Viro6a029a92005-08-27 06:48:15 +010031 return simple_read_from_buffer(buf, count, ppos, v_buf, mmapper_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032}
33
Jeff Dikecb8fa612007-10-16 01:27:34 -070034static ssize_t mmapper_write(struct file *file, const char __user *buf,
35 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
Al Viro6a029a92005-08-27 06:48:15 +010037 if (*ppos > mmapper_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 return -EINVAL;
39
Al Viro6a029a92005-08-27 06:48:15 +010040 if (count > mmapper_size - *ppos)
41 count = mmapper_size - *ppos;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Al Viro6a029a92005-08-27 06:48:15 +010043 if (copy_from_user(&v_buf[*ppos], buf, count))
44 return -EFAULT;
Jeff Dikecb8fa612007-10-16 01:27:34 -070045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 return count;
47}
48
Jeff Dikecb8fa612007-10-16 01:27:34 -070049static int mmapper_ioctl(struct inode *inode, struct file *file,
50 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
Jeff Dikecb8fa612007-10-16 01:27:34 -070052 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053}
54
Jeff Dikecb8fa612007-10-16 01:27:34 -070055static int mmapper_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
57 int ret = -EINVAL;
58 int size;
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 if (vma->vm_pgoff != 0)
61 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Jeff Dikecb8fa612007-10-16 01:27:34 -070063 size = vma->vm_end - vma->vm_start;
64 if (size > mmapper_size)
65 return -EFAULT;
66
67 /*
68 * XXX A comment above remap_pfn_range says it should only be
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 * called when the mm semaphore is held
70 */
71 if (remap_pfn_range(vma, vma->vm_start, p_buf >> PAGE_SHIFT, size,
Jeff Dikecb8fa612007-10-16 01:27:34 -070072 vma->vm_page_prot))
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 goto out;
74 ret = 0;
75out:
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 return ret;
77}
78
Jeff Dikecb8fa612007-10-16 01:27:34 -070079static int mmapper_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
Arnd Bergmannbd6859f2008-05-20 19:16:56 +020081 cycle_kernel_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 return 0;
83}
84
Jeff Dikecb8fa612007-10-16 01:27:34 -070085static int mmapper_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
87 return 0;
88}
89
Jeff Dike5e7672e2006-09-27 01:50:33 -070090static const struct file_operations mmapper_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 .owner = THIS_MODULE,
92 .read = mmapper_read,
93 .write = mmapper_write,
94 .ioctl = mmapper_ioctl,
95 .mmap = mmapper_mmap,
96 .open = mmapper_open,
97 .release = mmapper_release,
98};
99
Jeff Dikecb8fa612007-10-16 01:27:34 -0700100/*
101 * No locking needed - only used (and modified) by below initcall and exitcall.
102 */
Paolo 'Blaisorblade' Giarrusso1ba0ce62006-10-19 23:28:26 -0700103static struct miscdevice mmapper_dev = {
Jeff Dike3df59522005-06-08 15:47:50 -0700104 .minor = MISC_DYNAMIC_MINOR,
105 .name = "mmapper",
106 .fops = &mmapper_fops
107};
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109static int __init mmapper_init(void)
110{
Jeff Dike3df59522005-06-08 15:47:50 -0700111 int err;
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 printk(KERN_INFO "Mapper v0.1\n");
114
115 v_buf = (char *) find_iomem("mmapper", &mmapper_size);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700116 if (mmapper_size == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 printk(KERN_ERR "mmapper_init - find_iomem failed\n");
Jeff Dike3df59522005-06-08 15:47:50 -0700118 goto out;
119 }
120
121 err = misc_register(&mmapper_dev);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700122 if (err) {
Jeff Dike3df59522005-06-08 15:47:50 -0700123 printk(KERN_ERR "mmapper - misc_register failed, err = %d\n",
124 err);
125 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 }
127
128 p_buf = __pa(v_buf);
Jeff Dike3df59522005-06-08 15:47:50 -0700129out:
130 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
133static void mmapper_exit(void)
134{
Jeff Dike3df59522005-06-08 15:47:50 -0700135 misc_deregister(&mmapper_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
138module_init(mmapper_init);
139module_exit(mmapper_exit);
140
141MODULE_AUTHOR("Greg Lonnon <glonnon@ridgerun.com>");
142MODULE_DESCRIPTION("DSPLinux simulator mmapper driver");