2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2020-08-25 01:35:19 +00:00
|
|
|
#include <AK/Singleton.h>
|
2019-04-29 02:55:54 +00:00
|
|
|
#include <Kernel/Devices/Device.h>
|
2021-09-11 06:19:20 +00:00
|
|
|
#include <Kernel/Devices/DeviceManagement.h>
|
2020-01-22 21:23:50 +00:00
|
|
|
#include <Kernel/FileSystem/InodeMetadata.h>
|
2021-08-14 08:40:37 +00:00
|
|
|
#include <Kernel/FileSystem/SysFS.h>
|
2022-04-22 21:26:36 +00:00
|
|
|
#include <Kernel/FileSystem/SysFS/Subsystems/DeviceIdentifiers/BlockDevicesDirectory.h>
|
|
|
|
#include <Kernel/FileSystem/SysFS/Subsystems/DeviceIdentifiers/CharacterDevicesDirectory.h>
|
2021-08-14 07:13:27 +00:00
|
|
|
#include <Kernel/Sections.h>
|
2019-02-15 23:47:20 +00:00
|
|
|
|
2020-02-16 00:27:42 +00:00
|
|
|
namespace Kernel {
|
|
|
|
|
2021-12-23 18:08:18 +00:00
|
|
|
Device::Device(MajorNumber major, MinorNumber minor)
|
2019-06-07 09:43:58 +00:00
|
|
|
: m_major(major)
|
|
|
|
, m_minor(minor)
|
2019-02-17 09:38:07 +00:00
|
|
|
{
|
2021-09-10 11:44:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Device::after_inserting()
|
|
|
|
{
|
2021-09-11 06:19:20 +00:00
|
|
|
DeviceManagement::the().after_inserting_device({}, *this);
|
2021-09-10 11:44:46 +00:00
|
|
|
VERIFY(!m_sysfs_component);
|
2021-08-14 08:40:37 +00:00
|
|
|
auto sys_fs_component = SysFSDeviceComponent::must_create(*this);
|
|
|
|
m_sysfs_component = sys_fs_component;
|
2022-04-22 21:26:36 +00:00
|
|
|
if (is_block_device()) {
|
|
|
|
SysFSBlockDevicesDirectory::the().devices_list({}).with([&](auto& list) -> void {
|
|
|
|
list.append(sys_fs_component);
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
VERIFY(is_character_device());
|
|
|
|
SysFSCharacterDevicesDirectory::the().devices_list({}).with([&](auto& list) -> void {
|
2021-08-14 08:40:37 +00:00
|
|
|
list.append(sys_fs_component);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-12-28 23:00:29 +00:00
|
|
|
void Device::will_be_destroyed()
|
2021-08-14 08:40:37 +00:00
|
|
|
{
|
2021-09-10 11:44:46 +00:00
|
|
|
VERIFY(m_sysfs_component);
|
2022-04-22 21:26:36 +00:00
|
|
|
if (is_block_device()) {
|
|
|
|
SysFSBlockDevicesDirectory::the().devices_list({}).with([&](auto& list) -> void {
|
|
|
|
list.remove(*m_sysfs_component);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
VERIFY(is_character_device());
|
|
|
|
SysFSCharacterDevicesDirectory::the().devices_list({}).with([&](auto& list) -> void {
|
|
|
|
list.remove(*m_sysfs_component);
|
|
|
|
});
|
|
|
|
}
|
2021-09-11 06:19:20 +00:00
|
|
|
DeviceManagement::the().before_device_removal({}, *this);
|
|
|
|
m_state = State::BeingRemoved;
|
2019-02-17 09:38:07 +00:00
|
|
|
}
|
|
|
|
|
2019-02-15 23:47:20 +00:00
|
|
|
Device::~Device()
|
|
|
|
{
|
2021-08-14 08:40:37 +00:00
|
|
|
VERIFY(m_state == State::BeingRemoved);
|
2019-02-15 23:47:20 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
ErrorOr<NonnullOwnPtr<KString>> Device::pseudo_path(OpenFileDescription const&) const
|
2019-02-15 23:47:20 +00:00
|
|
|
{
|
2021-11-29 11:03:33 +00:00
|
|
|
return KString::formatted("device:{},{}", major(), minor());
|
2019-08-17 09:17:36 +00:00
|
|
|
}
|
2020-02-16 00:27:42 +00:00
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
void Device::process_next_queued_request(Badge<AsyncDeviceRequest>, AsyncDeviceRequest const& completed_request)
|
2020-11-02 18:16:01 +00:00
|
|
|
{
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(m_requests_lock);
|
2021-02-06 23:59:32 +00:00
|
|
|
VERIFY(!m_requests.is_empty());
|
|
|
|
VERIFY(m_requests.first().ptr() == &completed_request);
|
|
|
|
m_requests.remove(m_requests.begin());
|
|
|
|
if (!m_requests.is_empty()) {
|
|
|
|
auto* next_request = m_requests.first().ptr();
|
|
|
|
next_request->do_start(move(lock));
|
2020-11-02 18:16:01 +00:00
|
|
|
}
|
|
|
|
|
2020-11-29 23:05:27 +00:00
|
|
|
evaluate_block_conditions();
|
2020-11-02 18:16:01 +00:00
|
|
|
}
|
|
|
|
|
2020-02-16 00:27:42 +00:00
|
|
|
}
|