2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2019-08-18 07:54:52 +03:00
|
|
|
#include <Kernel/Devices/MBVGADevice.h>
|
|
|
|
#include <Kernel/Process.h>
|
|
|
|
#include <Kernel/VM/AnonymousVMObject.h>
|
|
|
|
#include <Kernel/VM/MemoryManager.h>
|
|
|
|
#include <LibC/errno_numbers.h>
|
|
|
|
#include <LibC/sys/ioctl_numbers.h>
|
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
namespace Kernel {
|
|
|
|
|
2019-08-18 07:54:52 +03:00
|
|
|
static MBVGADevice* s_the;
|
|
|
|
|
|
|
|
MBVGADevice& MBVGADevice::the()
|
|
|
|
{
|
|
|
|
return *s_the;
|
|
|
|
}
|
|
|
|
|
2021-02-19 23:29:46 +03:00
|
|
|
UNMAP_AFTER_INIT MBVGADevice::MBVGADevice(PhysicalAddress addr, size_t pitch, size_t width, size_t height)
|
2019-08-18 07:54:52 +03:00
|
|
|
: BlockDevice(29, 0)
|
|
|
|
, m_framebuffer_address(addr)
|
|
|
|
, m_framebuffer_pitch(pitch)
|
|
|
|
, m_framebuffer_width(width)
|
|
|
|
, m_framebuffer_height(height)
|
|
|
|
{
|
2021-01-10 16:39:20 +03:00
|
|
|
dbgln("MBVGADevice address={}, pitch={}, width={}, height={}", addr, pitch, width, height);
|
2019-08-18 07:54:52 +03:00
|
|
|
s_the = this;
|
|
|
|
}
|
|
|
|
|
2021-03-19 00:57:25 +03:00
|
|
|
KResultOr<Region*> MBVGADevice::mmap(Process& process, FileDescription&, const Range& range, u64 offset, int prot, bool shared)
|
2019-08-18 07:54:52 +03:00
|
|
|
{
|
2020-01-12 04:17:30 +03:00
|
|
|
REQUIRE_PROMISE(video);
|
2020-02-28 22:47:27 +03:00
|
|
|
if (!shared)
|
2021-01-21 01:11:17 +03:00
|
|
|
return ENODEV;
|
2021-01-28 07:23:20 +03:00
|
|
|
if (offset != 0)
|
|
|
|
return ENXIO;
|
2021-02-14 11:57:19 +03:00
|
|
|
if (range.size() != page_round_up(framebuffer_size_in_bytes()))
|
2021-01-28 07:23:20 +03:00
|
|
|
return EOVERFLOW;
|
|
|
|
|
2019-12-19 21:13:44 +03:00
|
|
|
auto vmobject = AnonymousVMObject::create_for_physical_range(m_framebuffer_address, framebuffer_size_in_bytes());
|
2020-01-28 22:48:07 +03:00
|
|
|
if (!vmobject)
|
2021-01-21 01:11:17 +03:00
|
|
|
return ENOMEM;
|
2021-02-08 17:45:40 +03:00
|
|
|
return process.space().allocate_region_with_vmobject(
|
2021-01-25 16:52:36 +03:00
|
|
|
range,
|
2020-01-28 22:48:07 +03:00
|
|
|
vmobject.release_nonnull(),
|
2019-08-18 07:54:52 +03:00
|
|
|
0,
|
|
|
|
"MBVGA Framebuffer",
|
2021-01-02 18:38:05 +03:00
|
|
|
prot,
|
|
|
|
shared);
|
2019-08-18 07:54:52 +03:00
|
|
|
}
|
|
|
|
|
2020-05-23 14:17:58 +03:00
|
|
|
int MBVGADevice::ioctl(FileDescription&, unsigned request, FlatPtr arg)
|
2019-08-18 07:54:52 +03:00
|
|
|
{
|
2020-01-12 04:17:30 +03:00
|
|
|
REQUIRE_PROMISE(video);
|
2019-08-18 07:54:52 +03:00
|
|
|
switch (request) {
|
|
|
|
case FB_IOCTL_GET_SIZE_IN_BYTES: {
|
|
|
|
auto* out = (size_t*)arg;
|
2020-07-31 01:17:25 +03:00
|
|
|
size_t value = framebuffer_size_in_bytes();
|
2020-09-12 06:11:07 +03:00
|
|
|
if (!copy_to_user(out, &value))
|
|
|
|
return -EFAULT;
|
2019-08-18 07:54:52 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
case FB_IOCTL_GET_BUFFER: {
|
|
|
|
auto* index = (int*)arg;
|
2020-07-31 01:17:25 +03:00
|
|
|
int value = 0;
|
2020-09-12 06:11:07 +03:00
|
|
|
if (!copy_to_user(index, &value))
|
|
|
|
return -EFAULT;
|
2019-08-18 07:54:52 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
case FB_IOCTL_GET_RESOLUTION: {
|
2020-07-31 01:17:25 +03:00
|
|
|
auto* user_resolution = (FBResolution*)arg;
|
|
|
|
FBResolution resolution;
|
|
|
|
resolution.pitch = m_framebuffer_pitch;
|
|
|
|
resolution.width = m_framebuffer_width;
|
|
|
|
resolution.height = m_framebuffer_height;
|
2020-09-12 06:11:07 +03:00
|
|
|
if (!copy_to_user(user_resolution, &resolution))
|
|
|
|
return -EFAULT;
|
2019-08-18 07:54:52 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
case FB_IOCTL_SET_RESOLUTION: {
|
2020-07-31 01:17:25 +03:00
|
|
|
auto* user_resolution = (FBResolution*)arg;
|
|
|
|
FBResolution resolution;
|
|
|
|
resolution.pitch = m_framebuffer_pitch;
|
|
|
|
resolution.width = m_framebuffer_width;
|
|
|
|
resolution.height = m_framebuffer_height;
|
2020-09-12 06:11:07 +03:00
|
|
|
if (!copy_to_user(user_resolution, &resolution))
|
|
|
|
return -EFAULT;
|
2019-08-18 07:54:52 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
};
|
|
|
|
}
|
2020-02-16 03:27:42 +03:00
|
|
|
|
2021-01-21 20:49:56 +03:00
|
|
|
String MBVGADevice::device_name() const
|
|
|
|
{
|
|
|
|
return String::formatted("fb{}", minor());
|
|
|
|
}
|
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
}
|