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 | |
Al Viro | 6a029a9 | 2005-08-27 06:48:15 +0100 | [diff] [blame] | 12 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/module.h> |
| 14 | #include <linux/mm.h> |
Jeff Dike | 3df5952 | 2005-06-08 15:47:50 -0700 | [diff] [blame] | 15 | #include <linux/miscdevice.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <asm/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include "mem_user.h" |
| 18 | #include "user_util.h" |
| 19 | |
| 20 | /* These are set in mmapper_init, which is called at boot time */ |
| 21 | static unsigned long mmapper_size; |
| 22 | static unsigned long p_buf = 0; |
| 23 | static char *v_buf = NULL; |
| 24 | |
| 25 | static ssize_t |
Al Viro | 6a029a9 | 2005-08-27 06:48:15 +0100 | [diff] [blame] | 26 | mmapper_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | { |
Al Viro | 6a029a9 | 2005-08-27 06:48:15 +0100 | [diff] [blame] | 28 | return simple_read_from_buffer(buf, count, ppos, v_buf, mmapper_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | static ssize_t |
Al Viro | 6a029a9 | 2005-08-27 06:48:15 +0100 | [diff] [blame] | 32 | mmapper_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | { |
Al Viro | 6a029a9 | 2005-08-27 06:48:15 +0100 | [diff] [blame] | 34 | if (*ppos > mmapper_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | return -EINVAL; |
| 36 | |
Al Viro | 6a029a9 | 2005-08-27 06:48:15 +0100 | [diff] [blame] | 37 | if (count > mmapper_size - *ppos) |
| 38 | count = mmapper_size - *ppos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Al Viro | 6a029a9 | 2005-08-27 06:48:15 +0100 | [diff] [blame] | 40 | if (copy_from_user(&v_buf[*ppos], buf, count)) |
| 41 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
| 43 | return count; |
| 44 | } |
| 45 | |
| 46 | static int |
| 47 | mmapper_ioctl(struct inode *inode, struct file *file, unsigned int cmd, |
| 48 | unsigned long arg) |
| 49 | { |
| 50 | return(-ENOIOCTLCMD); |
| 51 | } |
| 52 | |
| 53 | static int |
| 54 | mmapper_mmap(struct file *file, struct vm_area_struct * vma) |
| 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; |
| 61 | |
| 62 | size = vma->vm_end - vma->vm_start; |
| 63 | if(size > mmapper_size) return(-EFAULT); |
| 64 | |
| 65 | /* XXX A comment above remap_pfn_range says it should only be |
| 66 | * called when the mm semaphore is held |
| 67 | */ |
| 68 | if (remap_pfn_range(vma, vma->vm_start, p_buf >> PAGE_SHIFT, size, |
| 69 | vma->vm_page_prot)) |
| 70 | goto out; |
| 71 | ret = 0; |
| 72 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | return ret; |
| 74 | } |
| 75 | |
| 76 | static int |
| 77 | mmapper_open(struct inode *inode, struct file *file) |
| 78 | { |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | static int |
| 83 | mmapper_release(struct inode *inode, struct file *file) |
| 84 | { |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | static struct file_operations mmapper_fops = { |
| 89 | .owner = THIS_MODULE, |
| 90 | .read = mmapper_read, |
| 91 | .write = mmapper_write, |
| 92 | .ioctl = mmapper_ioctl, |
| 93 | .mmap = mmapper_mmap, |
| 94 | .open = mmapper_open, |
| 95 | .release = mmapper_release, |
| 96 | }; |
| 97 | |
Jeff Dike | 3df5952 | 2005-06-08 15:47:50 -0700 | [diff] [blame] | 98 | static struct miscdevice mmapper_dev = { |
| 99 | .minor = MISC_DYNAMIC_MINOR, |
| 100 | .name = "mmapper", |
| 101 | .fops = &mmapper_fops |
| 102 | }; |
| 103 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | static int __init mmapper_init(void) |
| 105 | { |
Jeff Dike | 3df5952 | 2005-06-08 15:47:50 -0700 | [diff] [blame] | 106 | int err; |
| 107 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | printk(KERN_INFO "Mapper v0.1\n"); |
| 109 | |
| 110 | v_buf = (char *) find_iomem("mmapper", &mmapper_size); |
| 111 | if(mmapper_size == 0){ |
| 112 | printk(KERN_ERR "mmapper_init - find_iomem failed\n"); |
Jeff Dike | 3df5952 | 2005-06-08 15:47:50 -0700 | [diff] [blame] | 113 | goto out; |
| 114 | } |
| 115 | |
| 116 | err = misc_register(&mmapper_dev); |
| 117 | if(err){ |
| 118 | printk(KERN_ERR "mmapper - misc_register failed, err = %d\n", |
| 119 | err); |
| 120 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | p_buf = __pa(v_buf); |
Jeff Dike | 3df5952 | 2005-06-08 15:47:50 -0700 | [diff] [blame] | 124 | out: |
| 125 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | static void mmapper_exit(void) |
| 129 | { |
Jeff Dike | 3df5952 | 2005-06-08 15:47:50 -0700 | [diff] [blame] | 130 | misc_deregister(&mmapper_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | module_init(mmapper_init); |
| 134 | module_exit(mmapper_exit); |
| 135 | |
| 136 | MODULE_AUTHOR("Greg Lonnon <glonnon@ridgerun.com>"); |
| 137 | MODULE_DESCRIPTION("DSPLinux simulator mmapper driver"); |
| 138 | /* |
| 139 | * --------------------------------------------------------------------------- |
| 140 | * Local variables: |
| 141 | * c-file-style: "linux" |
| 142 | * End: |
| 143 | */ |