blob: 6bc73f0e8e293dd43d7be0f38d16954b1bba49fb [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
Arnd Bergmannd5f1d542010-04-27 16:24:23 +020049static long mmapper_ioctl(struct file *file, 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,
Arnd Bergmannd5f1d542010-04-27 16:24:23 +020092 .unlocked_ioctl = mmapper_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 .mmap = mmapper_mmap,
94 .open = mmapper_open,
95 .release = mmapper_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +020096 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -070097};
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");
Randy Dunlap07ecb792011-01-12 16:59:27 -0800140MODULE_LICENSE("GPL");