mmapper_kern.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  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 [email protected] or [email protected]
  10. *
  11. */
  12. #include <linux/stddef.h>
  13. #include <linux/types.h>
  14. #include <linux/fs.h>
  15. #include <linux/init.h>
  16. #include <linux/miscdevice.h>
  17. #include <linux/module.h>
  18. #include <linux/mm.h>
  19. #include <linux/uaccess.h>
  20. #include <mem_user.h>
  21. /* These are set in mmapper_init, which is called at boot time */
  22. static unsigned long mmapper_size;
  23. static unsigned long p_buf;
  24. static char *v_buf;
  25. static ssize_t mmapper_read(struct file *file, char __user *buf, size_t count,
  26. loff_t *ppos)
  27. {
  28. return simple_read_from_buffer(buf, count, ppos, v_buf, mmapper_size);
  29. }
  30. static ssize_t mmapper_write(struct file *file, const char __user *buf,
  31. size_t count, loff_t *ppos)
  32. {
  33. if (*ppos > mmapper_size)
  34. return -EINVAL;
  35. return simple_write_to_buffer(v_buf, mmapper_size, ppos, buf, count);
  36. }
  37. static long mmapper_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  38. {
  39. return -ENOIOCTLCMD;
  40. }
  41. static int mmapper_mmap(struct file *file, struct vm_area_struct *vma)
  42. {
  43. int ret = -EINVAL;
  44. int size;
  45. if (vma->vm_pgoff != 0)
  46. goto out;
  47. size = vma->vm_end - vma->vm_start;
  48. if (size > mmapper_size)
  49. return -EFAULT;
  50. /*
  51. * XXX A comment above remap_pfn_range says it should only be
  52. * called when the mm semaphore is held
  53. */
  54. if (remap_pfn_range(vma, vma->vm_start, p_buf >> PAGE_SHIFT, size,
  55. vma->vm_page_prot))
  56. goto out;
  57. ret = 0;
  58. out:
  59. return ret;
  60. }
  61. static int mmapper_open(struct inode *inode, struct file *file)
  62. {
  63. return 0;
  64. }
  65. static int mmapper_release(struct inode *inode, struct file *file)
  66. {
  67. return 0;
  68. }
  69. static const struct file_operations mmapper_fops = {
  70. .owner = THIS_MODULE,
  71. .read = mmapper_read,
  72. .write = mmapper_write,
  73. .unlocked_ioctl = mmapper_ioctl,
  74. .mmap = mmapper_mmap,
  75. .open = mmapper_open,
  76. .release = mmapper_release,
  77. .llseek = default_llseek,
  78. };
  79. /*
  80. * No locking needed - only used (and modified) by below initcall and exitcall.
  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. return -ENODEV;
  95. }
  96. p_buf = __pa(v_buf);
  97. err = misc_register(&mmapper_dev);
  98. if (err) {
  99. printk(KERN_ERR "mmapper - misc_register failed, err = %d\n",
  100. err);
  101. return err;
  102. }
  103. return 0;
  104. }
  105. static void __exit mmapper_exit(void)
  106. {
  107. misc_deregister(&mmapper_dev);
  108. }
  109. module_init(mmapper_init);
  110. module_exit(mmapper_exit);
  111. MODULE_AUTHOR("Greg Lonnon <[email protected]>");
  112. MODULE_DESCRIPTION("DSPLinux simulator mmapper driver");
  113. MODULE_LICENSE("GPL");