blob: c0ef803c7c709d2178f028585f00c658bf6441c6 [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>
Thomas Gleixnerd63c4892009-10-10 15:36:34 +000019
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
Akinobu Mita0388fae2011-01-12 16:59:28 -080040 return simple_write_to_buffer(v_buf, mmapper_size, ppos, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041}
42
Arnd Bergmannd5f1d542010-04-27 16:24:23 +020043static long mmapper_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
Jeff Dikecb8fa612007-10-16 01:27:34 -070045 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046}
47
Jeff Dikecb8fa612007-10-16 01:27:34 -070048static int mmapper_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
50 int ret = -EINVAL;
51 int size;
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 if (vma->vm_pgoff != 0)
54 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Jeff Dikecb8fa612007-10-16 01:27:34 -070056 size = vma->vm_end - vma->vm_start;
57 if (size > mmapper_size)
58 return -EFAULT;
59
60 /*
61 * XXX A comment above remap_pfn_range says it should only be
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 * called when the mm semaphore is held
63 */
64 if (remap_pfn_range(vma, vma->vm_start, p_buf >> PAGE_SHIFT, size,
Jeff Dikecb8fa612007-10-16 01:27:34 -070065 vma->vm_page_prot))
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 goto out;
67 ret = 0;
68out:
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 return ret;
70}
71
Jeff Dikecb8fa612007-10-16 01:27:34 -070072static int mmapper_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
74 return 0;
75}
76
Jeff Dikecb8fa612007-10-16 01:27:34 -070077static int mmapper_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
79 return 0;
80}
81
Jeff Dike5e7672e2006-09-27 01:50:33 -070082static const struct file_operations mmapper_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 .owner = THIS_MODULE,
84 .read = mmapper_read,
85 .write = mmapper_write,
Arnd Bergmannd5f1d542010-04-27 16:24:23 +020086 .unlocked_ioctl = mmapper_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 .mmap = mmapper_mmap,
88 .open = mmapper_open,
89 .release = mmapper_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +020090 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -070091};
92
Jeff Dikecb8fa612007-10-16 01:27:34 -070093/*
94 * No locking needed - only used (and modified) by below initcall and exitcall.
95 */
Paolo 'Blaisorblade' Giarrusso1ba0ce62006-10-19 23:28:26 -070096static struct miscdevice mmapper_dev = {
Jeff Dike3df59522005-06-08 15:47:50 -070097 .minor = MISC_DYNAMIC_MINOR,
98 .name = "mmapper",
99 .fops = &mmapper_fops
100};
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102static int __init mmapper_init(void)
103{
Jeff Dike3df59522005-06-08 15:47:50 -0700104 int err;
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 printk(KERN_INFO "Mapper v0.1\n");
107
108 v_buf = (char *) find_iomem("mmapper", &mmapper_size);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700109 if (mmapper_size == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 printk(KERN_ERR "mmapper_init - find_iomem failed\n");
Thomas Gleixnerd63c4892009-10-10 15:36:34 +0000111 return -ENODEV;
Jeff Dike3df59522005-06-08 15:47:50 -0700112 }
Thomas Gleixnerd63c4892009-10-10 15:36:34 +0000113 p_buf = __pa(v_buf);
Jeff Dike3df59522005-06-08 15:47:50 -0700114
115 err = misc_register(&mmapper_dev);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700116 if (err) {
Jeff Dike3df59522005-06-08 15:47:50 -0700117 printk(KERN_ERR "mmapper - misc_register failed, err = %d\n",
118 err);
Justin P. Mattock6eab04a2011-04-08 19:49:08 -0700119 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 }
Jeff Dike3df59522005-06-08 15:47:50 -0700121 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
124static void mmapper_exit(void)
125{
Jeff Dike3df59522005-06-08 15:47:50 -0700126 misc_deregister(&mmapper_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
129module_init(mmapper_init);
130module_exit(mmapper_exit);
131
132MODULE_AUTHOR("Greg Lonnon <glonnon@ridgerun.com>");
133MODULE_DESCRIPTION("DSPLinux simulator mmapper driver");
Randy Dunlap07ecb792011-01-12 16:59:27 -0800134MODULE_LICENSE("GPL");