blob: d22f9e5c0eac5f483da649cfb7da2201c8888458 [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
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{
81 return 0;
82}
83
Jeff Dikecb8fa612007-10-16 01:27:34 -070084static int mmapper_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
86 return 0;
87}
88
Jeff Dike5e7672e2006-09-27 01:50:33 -070089static const struct file_operations mmapper_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 .owner = THIS_MODULE,
91 .read = mmapper_read,
92 .write = mmapper_write,
93 .ioctl = mmapper_ioctl,
94 .mmap = mmapper_mmap,
95 .open = mmapper_open,
96 .release = mmapper_release,
97};
98
Jeff Dikecb8fa612007-10-16 01:27:34 -070099/*
100 * No locking needed - only used (and modified) by below initcall and exitcall.
101 */
Paolo 'Blaisorblade' Giarrusso1ba0ce62006-10-19 23:28:26 -0700102static struct miscdevice mmapper_dev = {
Jeff Dike3df59522005-06-08 15:47:50 -0700103 .minor = MISC_DYNAMIC_MINOR,
104 .name = "mmapper",
105 .fops = &mmapper_fops
106};
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108static int __init mmapper_init(void)
109{
Jeff Dike3df59522005-06-08 15:47:50 -0700110 int err;
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 printk(KERN_INFO "Mapper v0.1\n");
113
114 v_buf = (char *) find_iomem("mmapper", &mmapper_size);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700115 if (mmapper_size == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 printk(KERN_ERR "mmapper_init - find_iomem failed\n");
Thomas Gleixnerd63c4892009-10-10 15:36:34 +0000117 return -ENODEV;
Jeff Dike3df59522005-06-08 15:47:50 -0700118 }
Thomas Gleixnerd63c4892009-10-10 15:36:34 +0000119 p_buf = __pa(v_buf);
Jeff Dike3df59522005-06-08 15:47:50 -0700120
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);
Thomas Gleixnerd63c4892009-10-10 15:36:34 +0000125 return err;;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 }
Jeff Dike3df59522005-06-08 15:47:50 -0700127 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
130static void mmapper_exit(void)
131{
Jeff Dike3df59522005-06-08 15:47:50 -0700132 misc_deregister(&mmapper_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
135module_init(mmapper_init);
136module_exit(mmapper_exit);
137
138MODULE_AUTHOR("Greg Lonnon <glonnon@ridgerun.com>");
139MODULE_DESCRIPTION("DSPLinux simulator mmapper driver");