blob: 0bf78ff8901102f55c19369fce6f030932b0ef2d [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * arch/um/drivers/mmapper_kern.c
4 *
5 * BRIEF MODULE DESCRIPTION
6 *
7 * Copyright (C) 2000 RidgeRun, Inc.
8 * Author: RidgeRun, Inc.
9 * Greg Lonnon glonnon@ridgerun.com or info@ridgerun.com
10 *
11 */
12
Jeff Dikecb8fa612007-10-16 01:27:34 -070013#include <linux/stddef.h>
14#include <linux/types.h>
Alexey Dobriyan4e950f62007-07-30 02:36:13 +040015#include <linux/fs.h>
Jeff Dikecb8fa612007-10-16 01:27:34 -070016#include <linux/init.h>
Jeff Dike3df59522005-06-08 15:47:50 -070017#include <linux/miscdevice.h>
Jeff Dikecb8fa612007-10-16 01:27:34 -070018#include <linux/module.h>
19#include <linux/mm.h>
Thomas Gleixnerd63c4892009-10-10 15:36:34 +000020
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080021#include <linux/uaccess.h>
Al Viro37185b32012-10-08 03:27:32 +010022#include <mem_user.h>
Jeff Dikecb8fa612007-10-16 01:27:34 -070023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024/* These are set in mmapper_init, which is called at boot time */
25static unsigned long mmapper_size;
Jeff Dikecb8fa612007-10-16 01:27:34 -070026static unsigned long p_buf;
27static char *v_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Jeff Dikecb8fa612007-10-16 01:27:34 -070029static ssize_t mmapper_read(struct file *file, char __user *buf, size_t count,
30 loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
Al Viro6a029a92005-08-27 06:48:15 +010032 return simple_read_from_buffer(buf, count, ppos, v_buf, mmapper_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033}
34
Jeff Dikecb8fa612007-10-16 01:27:34 -070035static ssize_t mmapper_write(struct file *file, const char __user *buf,
36 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
Al Viro6a029a92005-08-27 06:48:15 +010038 if (*ppos > mmapper_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 return -EINVAL;
40
Akinobu Mita0388fae2011-01-12 16:59:28 -080041 return simple_write_to_buffer(v_buf, mmapper_size, ppos, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042}
43
Arnd Bergmannd5f1d542010-04-27 16:24:23 +020044static long mmapper_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
Jeff Dikecb8fa612007-10-16 01:27:34 -070046 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047}
48
Jeff Dikecb8fa612007-10-16 01:27:34 -070049static int mmapper_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
51 int ret = -EINVAL;
52 int size;
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 if (vma->vm_pgoff != 0)
55 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Jeff Dikecb8fa612007-10-16 01:27:34 -070057 size = vma->vm_end - vma->vm_start;
58 if (size > mmapper_size)
59 return -EFAULT;
60
61 /*
62 * XXX A comment above remap_pfn_range says it should only be
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 * called when the mm semaphore is held
64 */
65 if (remap_pfn_range(vma, vma->vm_start, p_buf >> PAGE_SHIFT, size,
Jeff Dikecb8fa612007-10-16 01:27:34 -070066 vma->vm_page_prot))
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 goto out;
68 ret = 0;
69out:
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 return ret;
71}
72
Jeff Dikecb8fa612007-10-16 01:27:34 -070073static int mmapper_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
75 return 0;
76}
77
Jeff Dikecb8fa612007-10-16 01:27:34 -070078static int mmapper_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
80 return 0;
81}
82
Jeff Dike5e7672e2006-09-27 01:50:33 -070083static const struct file_operations mmapper_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 .owner = THIS_MODULE,
85 .read = mmapper_read,
86 .write = mmapper_write,
Arnd Bergmannd5f1d542010-04-27 16:24:23 +020087 .unlocked_ioctl = mmapper_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 .mmap = mmapper_mmap,
89 .open = mmapper_open,
90 .release = mmapper_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +020091 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -070092};
93
Jeff Dikecb8fa612007-10-16 01:27:34 -070094/*
95 * No locking needed - only used (and modified) by below initcall and exitcall.
96 */
Paolo 'Blaisorblade' Giarrusso1ba0ce62006-10-19 23:28:26 -070097static struct miscdevice mmapper_dev = {
Jeff Dike3df59522005-06-08 15:47:50 -070098 .minor = MISC_DYNAMIC_MINOR,
99 .name = "mmapper",
100 .fops = &mmapper_fops
101};
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103static int __init mmapper_init(void)
104{
Jeff Dike3df59522005-06-08 15:47:50 -0700105 int err;
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 printk(KERN_INFO "Mapper v0.1\n");
108
109 v_buf = (char *) find_iomem("mmapper", &mmapper_size);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700110 if (mmapper_size == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 printk(KERN_ERR "mmapper_init - find_iomem failed\n");
Thomas Gleixnerd63c4892009-10-10 15:36:34 +0000112 return -ENODEV;
Jeff Dike3df59522005-06-08 15:47:50 -0700113 }
Thomas Gleixnerd63c4892009-10-10 15:36:34 +0000114 p_buf = __pa(v_buf);
Jeff Dike3df59522005-06-08 15:47:50 -0700115
116 err = misc_register(&mmapper_dev);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700117 if (err) {
Jeff Dike3df59522005-06-08 15:47:50 -0700118 printk(KERN_ERR "mmapper - misc_register failed, err = %d\n",
119 err);
Justin P. Mattock6eab04a2011-04-08 19:49:08 -0700120 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 }
Jeff Dike3df59522005-06-08 15:47:50 -0700122 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
125static void mmapper_exit(void)
126{
Jeff Dike3df59522005-06-08 15:47:50 -0700127 misc_deregister(&mmapper_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
130module_init(mmapper_init);
131module_exit(mmapper_exit);
132
133MODULE_AUTHOR("Greg Lonnon <glonnon@ridgerun.com>");
134MODULE_DESCRIPTION("DSPLinux simulator mmapper driver");
Randy Dunlap07ecb792011-01-12 16:59:27 -0800135MODULE_LICENSE("GPL");