/arch/um/drivers/mmapper_kern.c

https://bitbucket.org/evzijst/gittest · C · 150 lines · 103 code · 27 blank · 20 comment · 12 complexity · 2d5f17beab1b9d4abd5d73c41cd9d81b MD5 · raw file

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