MBVGADevice.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. static MBVGADevice* s_the;
  33. MBVGADevice& MBVGADevice::the()
  34. {
  35. return *s_the;
  36. }
  37. MBVGADevice::MBVGADevice(PhysicalAddress addr, int pitch, int width, int height)
  38. : BlockDevice(29, 0)
  39. , m_framebuffer_address(addr)
  40. , m_framebuffer_pitch(pitch)
  41. , m_framebuffer_width(width)
  42. , m_framebuffer_height(height)
  43. {
  44. dbg() << "MBVGADevice address=" << addr << ", pitch=" << pitch << ", width=" << width << ", height=" << height;
  45. s_the = this;
  46. }
  47. KResultOr<Region*> MBVGADevice::mmap(Process& process, FileDescription&, VirtualAddress preferred_vaddr, size_t offset, size_t size, int prot)
  48. {
  49. REQUIRE_PROMISE(video);
  50. ASSERT(offset == 0);
  51. ASSERT(size == framebuffer_size_in_bytes());
  52. auto vmobject = AnonymousVMObject::create_for_physical_range(m_framebuffer_address, framebuffer_size_in_bytes());
  53. auto* region = process.allocate_region_with_vmobject(
  54. preferred_vaddr,
  55. framebuffer_size_in_bytes(),
  56. move(vmobject),
  57. 0,
  58. "MBVGA Framebuffer",
  59. prot);
  60. dbg() << "MBVGADevice: mmap with size " << region->size() << " at " << region->vaddr();
  61. ASSERT(region);
  62. return region;
  63. }
  64. int MBVGADevice::ioctl(FileDescription&, unsigned request, unsigned arg)
  65. {
  66. REQUIRE_PROMISE(video);
  67. switch (request) {
  68. case FB_IOCTL_GET_SIZE_IN_BYTES: {
  69. auto* out = (size_t*)arg;
  70. if (!current->process().validate_write_typed(out))
  71. return -EFAULT;
  72. *out = framebuffer_size_in_bytes();
  73. return 0;
  74. }
  75. case FB_IOCTL_GET_BUFFER: {
  76. auto* index = (int*)arg;
  77. if (!current->process().validate_write_typed(index))
  78. return -EFAULT;
  79. *index = 0;
  80. return 0;
  81. }
  82. case FB_IOCTL_GET_RESOLUTION: {
  83. auto* resolution = (FBResolution*)arg;
  84. if (!current->process().validate_write_typed(resolution))
  85. return -EFAULT;
  86. resolution->pitch = m_framebuffer_pitch;
  87. resolution->width = m_framebuffer_width;
  88. resolution->height = m_framebuffer_height;
  89. return 0;
  90. }
  91. case FB_IOCTL_SET_RESOLUTION: {
  92. auto* resolution = (FBResolution*)arg;
  93. if (!current->process().validate_read_typed(resolution) || !current->process().validate_write_typed(resolution))
  94. return -EFAULT;
  95. resolution->pitch = m_framebuffer_pitch;
  96. resolution->width = m_framebuffer_width;
  97. resolution->height = m_framebuffer_height;
  98. return 0;
  99. }
  100. default:
  101. return -EINVAL;
  102. };
  103. }