Kernel/Devices: Introduce the Device Control Device

This device will assist userspace to manage hotplug events.
A userspace application reads a DeviceEvent entry until the return value
is zero which indicates no events that are queued and waiting for
processing.
Trying to read with a buffer smaller than sizeof(DeviceEvent) results in
EOVERFLOW.
For now, there's no ioctl mechanism for this device but in the future an
acknowledgement mechanism can be implemented via ioctl(2) interface.
This commit is contained in:
Liav A 2021-12-20 11:10:35 +02:00 committed by Linus Groh
parent b60e19fd34
commit b1ca39411b
Notes: sideshowbarker 2024-07-17 20:25:13 +09:00
7 changed files with 151 additions and 0 deletions

View file

@ -0,0 +1,53 @@
/*
* Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <Kernel/Devices/DeviceControlDevice.h>
#include <Kernel/Devices/DeviceManagement.h>
namespace Kernel {
UNMAP_AFTER_INIT NonnullRefPtr<DeviceControlDevice> DeviceControlDevice::must_create()
{
auto device_control_device_or_error = DeviceManagement::try_create_device<DeviceControlDevice>();
// FIXME: Find a way to propagate errors
VERIFY(!device_control_device_or_error.is_error());
return device_control_device_or_error.release_value();
}
bool DeviceControlDevice::can_read(const OpenFileDescription&, size_t) const
{
return true;
}
UNMAP_AFTER_INIT DeviceControlDevice::DeviceControlDevice()
: CharacterDevice(2, 10)
{
}
UNMAP_AFTER_INIT DeviceControlDevice::~DeviceControlDevice()
{
}
ErrorOr<size_t> DeviceControlDevice::read(OpenFileDescription&, u64, UserOrKernelBuffer& buffer, size_t size)
{
auto device_event = DeviceManagement::the().dequeue_top_device_event({});
if (!device_event.has_value())
return 0;
if (size < sizeof(DeviceEvent))
return Error::from_errno(EOVERFLOW);
size_t nread = 0;
TRY(buffer.write(&device_event.value(), nread, sizeof(DeviceEvent)));
nread += sizeof(DeviceEvent);
return nread;
}
ErrorOr<void> DeviceControlDevice::ioctl(OpenFileDescription&, unsigned, Userspace<void*>)
{
return Error::from_errno(ENOTSUP);
}
}