Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Dike | cb8fa61 | 2007-10-16 01:27:34 -0700 | [diff] [blame] | 12 | #include <linux/stddef.h> |
| 13 | #include <linux/types.h> |
Alexey Dobriyan | 4e950f6 | 2007-07-30 02:36:13 +0400 | [diff] [blame] | 14 | #include <linux/fs.h> |
Jeff Dike | cb8fa61 | 2007-10-16 01:27:34 -0700 | [diff] [blame] | 15 | #include <linux/init.h> |
Jeff Dike | 3df5952 | 2005-06-08 15:47:50 -0700 | [diff] [blame] | 16 | #include <linux/miscdevice.h> |
Jeff Dike | cb8fa61 | 2007-10-16 01:27:34 -0700 | [diff] [blame] | 17 | #include <linux/module.h> |
| 18 | #include <linux/mm.h> |
Thomas Gleixner | d63c489 | 2009-10-10 15:36:34 +0000 | [diff] [blame] | 19 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <asm/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include "mem_user.h" |
Jeff Dike | cb8fa61 | 2007-10-16 01:27:34 -0700 | [diff] [blame] | 22 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | /* These are set in mmapper_init, which is called at boot time */ |
| 24 | static unsigned long mmapper_size; |
Jeff Dike | cb8fa61 | 2007-10-16 01:27:34 -0700 | [diff] [blame] | 25 | static unsigned long p_buf; |
| 26 | static char *v_buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
Jeff Dike | cb8fa61 | 2007-10-16 01:27:34 -0700 | [diff] [blame] | 28 | static ssize_t mmapper_read(struct file *file, char __user *buf, size_t count, |
| 29 | loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | { |
Al Viro | 6a029a9 | 2005-08-27 06:48:15 +0100 | [diff] [blame] | 31 | return simple_read_from_buffer(buf, count, ppos, v_buf, mmapper_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | } |
| 33 | |
Jeff Dike | cb8fa61 | 2007-10-16 01:27:34 -0700 | [diff] [blame] | 34 | static ssize_t mmapper_write(struct file *file, const char __user *buf, |
| 35 | size_t count, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | { |
Al Viro | 6a029a9 | 2005-08-27 06:48:15 +0100 | [diff] [blame] | 37 | if (*ppos > mmapper_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | return -EINVAL; |
| 39 | |
Al Viro | 6a029a9 | 2005-08-27 06:48:15 +0100 | [diff] [blame] | 40 | if (count > mmapper_size - *ppos) |
| 41 | count = mmapper_size - *ppos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
Al Viro | 6a029a9 | 2005-08-27 06:48:15 +0100 | [diff] [blame] | 43 | if (copy_from_user(&v_buf[*ppos], buf, count)) |
| 44 | return -EFAULT; |
Jeff Dike | cb8fa61 | 2007-10-16 01:27:34 -0700 | [diff] [blame] | 45 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | return count; |
| 47 | } |
| 48 | |
Arnd Bergmann | d5f1d54 | 2010-04-27 16:24:23 +0200 | [diff] [blame] | 49 | static long mmapper_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | { |
Jeff Dike | cb8fa61 | 2007-10-16 01:27:34 -0700 | [diff] [blame] | 51 | return -ENOIOCTLCMD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Jeff Dike | cb8fa61 | 2007-10-16 01:27:34 -0700 | [diff] [blame] | 54 | static int mmapper_mmap(struct file *file, struct vm_area_struct *vma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | { |
| 56 | int ret = -EINVAL; |
| 57 | int size; |
| 58 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | if (vma->vm_pgoff != 0) |
| 60 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
Jeff Dike | cb8fa61 | 2007-10-16 01:27:34 -0700 | [diff] [blame] | 62 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | * called when the mm semaphore is held |
| 69 | */ |
| 70 | if (remap_pfn_range(vma, vma->vm_start, p_buf >> PAGE_SHIFT, size, |
Jeff Dike | cb8fa61 | 2007-10-16 01:27:34 -0700 | [diff] [blame] | 71 | vma->vm_page_prot)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | goto out; |
| 73 | ret = 0; |
| 74 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | return ret; |
| 76 | } |
| 77 | |
Jeff Dike | cb8fa61 | 2007-10-16 01:27:34 -0700 | [diff] [blame] | 78 | static int mmapper_open(struct inode *inode, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | { |
| 80 | return 0; |
| 81 | } |
| 82 | |
Jeff Dike | cb8fa61 | 2007-10-16 01:27:34 -0700 | [diff] [blame] | 83 | static int mmapper_release(struct inode *inode, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | { |
| 85 | return 0; |
| 86 | } |
| 87 | |
Jeff Dike | 5e7672e | 2006-09-27 01:50:33 -0700 | [diff] [blame] | 88 | static const struct file_operations mmapper_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | .owner = THIS_MODULE, |
| 90 | .read = mmapper_read, |
| 91 | .write = mmapper_write, |
Arnd Bergmann | d5f1d54 | 2010-04-27 16:24:23 +0200 | [diff] [blame] | 92 | .unlocked_ioctl = mmapper_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | .mmap = mmapper_mmap, |
| 94 | .open = mmapper_open, |
| 95 | .release = mmapper_release, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 96 | .llseek = default_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | }; |
| 98 | |
Jeff Dike | cb8fa61 | 2007-10-16 01:27:34 -0700 | [diff] [blame] | 99 | /* |
| 100 | * No locking needed - only used (and modified) by below initcall and exitcall. |
| 101 | */ |
Paolo 'Blaisorblade' Giarrusso | 1ba0ce6 | 2006-10-19 23:28:26 -0700 | [diff] [blame] | 102 | static struct miscdevice mmapper_dev = { |
Jeff Dike | 3df5952 | 2005-06-08 15:47:50 -0700 | [diff] [blame] | 103 | .minor = MISC_DYNAMIC_MINOR, |
| 104 | .name = "mmapper", |
| 105 | .fops = &mmapper_fops |
| 106 | }; |
| 107 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | static int __init mmapper_init(void) |
| 109 | { |
Jeff Dike | 3df5952 | 2005-06-08 15:47:50 -0700 | [diff] [blame] | 110 | int err; |
| 111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | printk(KERN_INFO "Mapper v0.1\n"); |
| 113 | |
| 114 | v_buf = (char *) find_iomem("mmapper", &mmapper_size); |
Jeff Dike | cb8fa61 | 2007-10-16 01:27:34 -0700 | [diff] [blame] | 115 | if (mmapper_size == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | printk(KERN_ERR "mmapper_init - find_iomem failed\n"); |
Thomas Gleixner | d63c489 | 2009-10-10 15:36:34 +0000 | [diff] [blame] | 117 | return -ENODEV; |
Jeff Dike | 3df5952 | 2005-06-08 15:47:50 -0700 | [diff] [blame] | 118 | } |
Thomas Gleixner | d63c489 | 2009-10-10 15:36:34 +0000 | [diff] [blame] | 119 | p_buf = __pa(v_buf); |
Jeff Dike | 3df5952 | 2005-06-08 15:47:50 -0700 | [diff] [blame] | 120 | |
| 121 | err = misc_register(&mmapper_dev); |
Jeff Dike | cb8fa61 | 2007-10-16 01:27:34 -0700 | [diff] [blame] | 122 | if (err) { |
Jeff Dike | 3df5952 | 2005-06-08 15:47:50 -0700 | [diff] [blame] | 123 | printk(KERN_ERR "mmapper - misc_register failed, err = %d\n", |
| 124 | err); |
Thomas Gleixner | d63c489 | 2009-10-10 15:36:34 +0000 | [diff] [blame] | 125 | return err;; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | } |
Jeff Dike | 3df5952 | 2005-06-08 15:47:50 -0700 | [diff] [blame] | 127 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | static void mmapper_exit(void) |
| 131 | { |
Jeff Dike | 3df5952 | 2005-06-08 15:47:50 -0700 | [diff] [blame] | 132 | misc_deregister(&mmapper_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | module_init(mmapper_init); |
| 136 | module_exit(mmapper_exit); |
| 137 | |
| 138 | MODULE_AUTHOR("Greg Lonnon <glonnon@ridgerun.com>"); |
| 139 | MODULE_DESCRIPTION("DSPLinux simulator mmapper driver"); |
Randy Dunlap | 07ecb79 | 2011-01-12 16:59:27 -0800 | [diff] [blame^] | 140 | MODULE_LICENSE("GPL"); |