2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2020-08-25 04:35:19 +03:00
|
|
|
#include <AK/Singleton.h>
|
2019-04-29 05:55:54 +03:00
|
|
|
#include <Kernel/Devices/Device.h>
|
2020-01-23 00:23:50 +03:00
|
|
|
#include <Kernel/FileSystem/InodeMetadata.h>
|
2019-02-16 02:47:20 +03:00
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
namespace Kernel {
|
|
|
|
|
2021-08-07 22:34:11 +03:00
|
|
|
static Singleton<HashMap<u32, Device*>> s_all_devices;
|
2020-01-23 00:23:50 +03:00
|
|
|
|
|
|
|
HashMap<u32, Device*>& Device::all_devices()
|
|
|
|
{
|
|
|
|
return *s_all_devices;
|
|
|
|
}
|
|
|
|
|
2019-08-18 14:48:15 +03:00
|
|
|
void Device::for_each(Function<void(Device&)> callback)
|
|
|
|
{
|
2020-01-23 00:23:50 +03:00
|
|
|
for (auto& entry : all_devices())
|
|
|
|
callback(*entry.value);
|
2019-08-18 14:48:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Device* Device::get_device(unsigned major, unsigned minor)
|
|
|
|
{
|
2020-01-23 00:23:50 +03:00
|
|
|
auto it = all_devices().find(encoded_device(major, minor));
|
|
|
|
if (it == all_devices().end())
|
|
|
|
return nullptr;
|
|
|
|
return it->value;
|
2019-08-18 14:48:15 +03:00
|
|
|
}
|
|
|
|
|
2020-01-23 00:23:50 +03:00
|
|
|
Device::Device(unsigned major, unsigned minor)
|
2019-06-07 12:43:58 +03:00
|
|
|
: m_major(major)
|
|
|
|
, m_minor(minor)
|
2019-02-17 12:38:07 +03:00
|
|
|
{
|
2020-01-23 00:23:50 +03:00
|
|
|
u32 device_id = encoded_device(major, minor);
|
|
|
|
auto it = all_devices().find(device_id);
|
|
|
|
if (it != all_devices().end()) {
|
2021-01-10 16:39:20 +03:00
|
|
|
dbgln("Already registered {},{}: {}", major, minor, it->value->class_name());
|
2020-01-23 00:23:50 +03:00
|
|
|
}
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(!all_devices().contains(device_id));
|
2020-01-23 00:23:50 +03:00
|
|
|
all_devices().set(device_id, this);
|
2019-02-17 12:38:07 +03:00
|
|
|
}
|
|
|
|
|
2019-02-16 02:47:20 +03:00
|
|
|
Device::~Device()
|
|
|
|
{
|
2020-01-23 00:23:50 +03:00
|
|
|
all_devices().remove(encoded_device(m_major, m_minor));
|
2019-02-16 02:47:20 +03:00
|
|
|
}
|
|
|
|
|
2019-08-17 12:17:36 +03:00
|
|
|
String Device::absolute_path() const
|
2019-02-16 02:47:20 +03:00
|
|
|
{
|
2021-06-30 09:25:03 +03:00
|
|
|
// FIXME: Don't assume mount point for DevFs
|
|
|
|
return String::formatted("/dev/{}", device_name());
|
2019-02-16 02:47:20 +03:00
|
|
|
}
|
2019-08-17 12:17:36 +03:00
|
|
|
|
|
|
|
String Device::absolute_path(const FileDescription&) const
|
|
|
|
{
|
|
|
|
return absolute_path();
|
|
|
|
}
|
2020-02-16 03:27:42 +03:00
|
|
|
|
2020-11-02 21:16:01 +03:00
|
|
|
void Device::process_next_queued_request(Badge<AsyncDeviceRequest>, const AsyncDeviceRequest& completed_request)
|
|
|
|
{
|
2021-08-22 02:49:22 +03:00
|
|
|
SpinlockLocker lock(m_requests_lock);
|
2021-02-07 02:59:32 +03: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 21:16:01 +03:00
|
|
|
}
|
|
|
|
|
2020-11-30 02:05:27 +03:00
|
|
|
evaluate_block_conditions();
|
2020-11-02 21:16:01 +03:00
|
|
|
}
|
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
}
|