mmapper_kern.c 2.9 KB

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