MBVGADevice.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <Kernel/Devices/MBVGADevice.h>
  27. #include <Kernel/Process.h>
  28. #include <Kernel/VM/AnonymousVMObject.h>
  29. #include <Kernel/VM/MemoryManager.h>
  30. #include <LibC/errno_numbers.h>
  31. #include <LibC/sys/ioctl_numbers.h>
  32. namespace Kernel {
  33. static MBVGADevice* s_the;
  34. MBVGADevice& MBVGADevice::the()
  35. {
  36. return *s_the;
  37. }
  38. MBVGADevice::MBVGADevice(PhysicalAddress addr, size_t pitch, size_t width, size_t height)
  39. : BlockDevice(29, 0)
  40. , m_framebuffer_address(addr)
  41. , m_framebuffer_pitch(pitch)
  42. , m_framebuffer_width(width)
  43. , m_framebuffer_height(height)
  44. {
  45. dbgln("MBVGADevice address={}, pitch={}, width={}, height={}", addr, pitch, width, height);
  46. s_the = this;
  47. }
  48. KResultOr<Region*> MBVGADevice::mmap(Process& process, FileDescription&, VirtualAddress preferred_vaddr, size_t offset, size_t size, int prot, bool shared)
  49. {
  50. REQUIRE_PROMISE(video);
  51. if (!shared)
  52. return KResult(-ENODEV);
  53. ASSERT(offset == 0);
  54. ASSERT(size == framebuffer_size_in_bytes());
  55. auto vmobject = AnonymousVMObject::create_for_physical_range(m_framebuffer_address, framebuffer_size_in_bytes());
  56. if (!vmobject)
  57. return KResult(-ENOMEM);
  58. return process.allocate_region_with_vmobject(
  59. preferred_vaddr,
  60. framebuffer_size_in_bytes(),
  61. vmobject.release_nonnull(),
  62. 0,
  63. "MBVGA Framebuffer",
  64. prot,
  65. shared);
  66. }
  67. int MBVGADevice::ioctl(FileDescription&, unsigned request, FlatPtr arg)
  68. {
  69. REQUIRE_PROMISE(video);
  70. switch (request) {
  71. case FB_IOCTL_GET_SIZE_IN_BYTES: {
  72. auto* out = (size_t*)arg;
  73. size_t value = framebuffer_size_in_bytes();
  74. if (!copy_to_user(out, &value))
  75. return -EFAULT;
  76. return 0;
  77. }
  78. case FB_IOCTL_GET_BUFFER: {
  79. auto* index = (int*)arg;
  80. int value = 0;
  81. if (!copy_to_user(index, &value))
  82. return -EFAULT;
  83. return 0;
  84. }
  85. case FB_IOCTL_GET_RESOLUTION: {
  86. auto* user_resolution = (FBResolution*)arg;
  87. FBResolution resolution;
  88. resolution.pitch = m_framebuffer_pitch;
  89. resolution.width = m_framebuffer_width;
  90. resolution.height = m_framebuffer_height;
  91. if (!copy_to_user(user_resolution, &resolution))
  92. return -EFAULT;
  93. return 0;
  94. }
  95. case FB_IOCTL_SET_RESOLUTION: {
  96. auto* user_resolution = (FBResolution*)arg;
  97. FBResolution resolution;
  98. resolution.pitch = m_framebuffer_pitch;
  99. resolution.width = m_framebuffer_width;
  100. resolution.height = m_framebuffer_height;
  101. if (!copy_to_user(user_resolution, &resolution))
  102. return -EFAULT;
  103. return 0;
  104. }
  105. default:
  106. return -EINVAL;
  107. };
  108. }
  109. }