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