summaryrefslogtreecommitdiff
path: root/Kernel/Devices
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2021-05-13 09:08:44 +0200
committerAndreas Kling <kling@serenityos.org>2021-05-13 23:28:40 +0200
commite46343bf9aadaf8b310821c8a92b131f2b726654 (patch)
tree4d027e4b413e82b8482865c8a8c3d9b37cdaa0ca /Kernel/Devices
parent25a45e639ac48bc75507f003c692f24310688e9b (diff)
downloadserenity-e46343bf9aadaf8b310821c8a92b131f2b726654.zip
Kernel: Make UserOrKernelBuffer R/W helpers return KResultOr<size_t>
This makes error propagation less cumbersome (and also exposed some places where we were not doing it.)
Diffstat (limited to 'Kernel/Devices')
-rw-r--r--Kernel/Devices/AsyncDeviceRequest.h4
-rw-r--r--Kernel/Devices/HID/KeyboardDevice.cpp10
-rw-r--r--Kernel/Devices/RandomDevice.cpp7
-rw-r--r--Kernel/Devices/SerialDevice.cpp15
4 files changed, 13 insertions, 23 deletions
diff --git a/Kernel/Devices/AsyncDeviceRequest.h b/Kernel/Devices/AsyncDeviceRequest.h
index ab8ed3c626..b5c0035627 100644
--- a/Kernel/Devices/AsyncDeviceRequest.h
+++ b/Kernel/Devices/AsyncDeviceRequest.h
@@ -90,7 +90,7 @@ public:
}
template<size_t BUFFER_BYTES, typename... Args>
- [[nodiscard]] bool write_to_buffer_buffered(UserOrKernelBuffer& buffer, Args... args)
+ [[nodiscard]] KResultOr<size_t> write_to_buffer_buffered(UserOrKernelBuffer& buffer, Args... args)
{
if (in_target_context(buffer))
return buffer.write_buffered<BUFFER_BYTES>(forward<Args>(args)...);
@@ -108,7 +108,7 @@ public:
}
template<size_t BUFFER_BYTES, typename... Args>
- [[nodiscard]] bool read_from_buffer_buffered(const UserOrKernelBuffer& buffer, Args... args)
+ [[nodiscard]] KResultOr<size_t> read_from_buffer_buffered(const UserOrKernelBuffer& buffer, Args... args)
{
if (in_target_context(buffer))
return buffer.read_buffered<BUFFER_BYTES>(forward<Args>(args)...);
diff --git a/Kernel/Devices/HID/KeyboardDevice.cpp b/Kernel/Devices/HID/KeyboardDevice.cpp
index f9e87e4ea7..b49603cbf9 100644
--- a/Kernel/Devices/HID/KeyboardDevice.cpp
+++ b/Kernel/Devices/HID/KeyboardDevice.cpp
@@ -298,13 +298,13 @@ KResultOr<size_t> KeyboardDevice::read(FileDescription&, u64, UserOrKernelBuffer
lock.unlock();
- ssize_t n = buffer.write_buffered<sizeof(Event)>(sizeof(Event), [&](u8* data, size_t data_bytes) {
+ auto result = buffer.write_buffered<sizeof(Event)>(sizeof(Event), [&](u8* data, size_t data_bytes) {
memcpy(data, &event, sizeof(Event));
- return (ssize_t)data_bytes;
+ return data_bytes;
});
- if (n < 0)
- return KResult((ErrnoCode)-n);
- VERIFY((size_t)n == sizeof(Event));
+ if (result.is_error())
+ return result.error();
+ VERIFY(result.value() == sizeof(Event));
nread += sizeof(Event);
lock.lock();
diff --git a/Kernel/Devices/RandomDevice.cpp b/Kernel/Devices/RandomDevice.cpp
index 589ed1eeb3..6ada0a36d4 100644
--- a/Kernel/Devices/RandomDevice.cpp
+++ b/Kernel/Devices/RandomDevice.cpp
@@ -25,13 +25,10 @@ bool RandomDevice::can_read(const FileDescription&, size_t) const
KResultOr<size_t> RandomDevice::read(FileDescription&, u64, UserOrKernelBuffer& buffer, size_t size)
{
- bool success = buffer.write_buffered<256>(size, [&](u8* data, size_t data_size) {
+ return buffer.write_buffered<256>(size, [&](u8* data, size_t data_size) {
get_good_random_bytes(data, data_size);
- return (ssize_t)data_size;
+ return data_size;
});
- if (!success)
- return EFAULT;
- return size;
}
KResultOr<size_t> RandomDevice::write(FileDescription&, u64, const UserOrKernelBuffer&, size_t size)
diff --git a/Kernel/Devices/SerialDevice.cpp b/Kernel/Devices/SerialDevice.cpp
index 263c684ffd..3be348172a 100644
--- a/Kernel/Devices/SerialDevice.cpp
+++ b/Kernel/Devices/SerialDevice.cpp
@@ -33,15 +33,11 @@ KResultOr<size_t> SerialDevice::read(FileDescription&, u64, UserOrKernelBuffer&
if (!(get_line_status() & DataReady))
return 0;
- ssize_t nwritten = buffer.write_buffered<128>(size, [&](u8* data, size_t data_size) {
+ return buffer.write_buffered<128>(size, [&](u8* data, size_t data_size) {
for (size_t i = 0; i < data_size; i++)
data[i] = IO::in8(m_base_addr);
- return (ssize_t)data_size;
+ return data_size;
});
- if (nwritten < 0)
- return KResult((ErrnoCode)-nwritten);
-
- return size;
}
bool SerialDevice::can_write(const FileDescription&, size_t) const
@@ -57,14 +53,11 @@ KResultOr<size_t> SerialDevice::write(FileDescription&, u64, const UserOrKernelB
if (!(get_line_status() & EmptyTransmitterHoldingRegister))
return 0;
- ssize_t nread = buffer.read_buffered<128>(size, [&](const u8* data, size_t data_size) {
+ return buffer.read_buffered<128>(size, [&](u8 const* data, size_t data_size) {
for (size_t i = 0; i < data_size; i++)
IO::out8(m_base_addr, data[i]);
- return (ssize_t)data_size;
+ return data_size;
});
- if (nread < 0)
- return KResult((ErrnoCode)-nread);
- return (size_t)nread;
}
String SerialDevice::device_name() const