2021-09-11 09:19:20 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Badge.h>
|
2021-11-08 02:51:39 +03:00
|
|
|
#include <AK/Error.h>
|
2021-09-11 09:19:20 +03:00
|
|
|
#include <AK/OwnPtr.h>
|
|
|
|
#include <AK/Time.h>
|
|
|
|
#include <AK/Types.h>
|
2021-12-20 12:10:35 +03:00
|
|
|
#include <Kernel/API/DeviceEvent.h>
|
2021-09-11 09:19:20 +03:00
|
|
|
#include <Kernel/API/TimePage.h>
|
2021-10-15 01:01:29 +03:00
|
|
|
#include <Kernel/Arch/RegisterState.h>
|
2021-11-23 03:12:19 +03:00
|
|
|
#include <Kernel/Devices/CharacterDevice.h>
|
2021-09-16 22:23:25 +03:00
|
|
|
#include <Kernel/Devices/ConsoleDevice.h>
|
2021-09-11 09:19:20 +03:00
|
|
|
#include <Kernel/Devices/Device.h>
|
2021-12-20 12:10:35 +03:00
|
|
|
#include <Kernel/Devices/DeviceControlDevice.h>
|
2021-09-11 09:19:20 +03:00
|
|
|
#include <Kernel/Devices/NullDevice.h>
|
2022-08-19 21:53:40 +03:00
|
|
|
#include <Kernel/Library/LockRefPtr.h>
|
|
|
|
#include <Kernel/Library/NonnullLockRefPtrVector.h>
|
2021-09-11 09:19:20 +03:00
|
|
|
#include <Kernel/UnixTypes.h>
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
|
|
|
class DeviceManagement {
|
|
|
|
|
|
|
|
public:
|
|
|
|
DeviceManagement();
|
|
|
|
static void initialize();
|
|
|
|
static DeviceManagement& the();
|
|
|
|
void attach_null_device(NullDevice const&);
|
|
|
|
|
2021-12-20 12:10:35 +03:00
|
|
|
void attach_device_control_device(DeviceControlDevice const&);
|
|
|
|
|
2021-09-16 22:23:25 +03:00
|
|
|
bool is_console_device_attached() const { return !m_console_device.is_null(); }
|
|
|
|
void attach_console_device(ConsoleDevice const&);
|
|
|
|
|
2021-12-20 12:10:35 +03:00
|
|
|
Optional<DeviceEvent> dequeue_top_device_event(Badge<DeviceControlDevice>);
|
|
|
|
|
2021-09-11 09:19:20 +03:00
|
|
|
void after_inserting_device(Badge<Device>, Device&);
|
|
|
|
void before_device_removal(Badge<Device>, Device&);
|
|
|
|
|
|
|
|
void for_each(Function<void(Device&)>);
|
2022-02-24 21:03:26 +03:00
|
|
|
ErrorOr<void> try_for_each(Function<ErrorOr<void>(Device&)>);
|
2021-12-23 21:08:18 +03:00
|
|
|
Device* get_device(MajorNumber major, MinorNumber minor);
|
2021-09-16 22:23:25 +03:00
|
|
|
|
2021-09-11 09:19:20 +03:00
|
|
|
NullDevice const& null_device() const;
|
|
|
|
NullDevice& null_device();
|
|
|
|
|
2021-09-16 22:23:25 +03:00
|
|
|
ConsoleDevice const& console_device() const;
|
|
|
|
ConsoleDevice& console_device();
|
|
|
|
|
2022-01-26 23:00:45 +03:00
|
|
|
template<typename DeviceType, typename... Args>
|
2022-08-19 21:53:40 +03:00
|
|
|
static inline ErrorOr<NonnullLockRefPtr<DeviceType>> try_create_device(Args&&... args) requires(requires(Args... args) { DeviceType::try_create(args...); })
|
2022-01-26 23:00:45 +03:00
|
|
|
{
|
|
|
|
auto device = TRY(DeviceType::try_create(forward<Args>(args)...));
|
|
|
|
device->after_inserting();
|
|
|
|
return device;
|
|
|
|
}
|
|
|
|
|
2021-09-11 09:19:20 +03:00
|
|
|
template<typename DeviceType, typename... Args>
|
2022-08-19 21:53:40 +03:00
|
|
|
static inline ErrorOr<NonnullLockRefPtr<DeviceType>> try_create_device(Args&&... args)
|
2021-09-11 09:19:20 +03:00
|
|
|
{
|
2022-08-19 21:53:40 +03:00
|
|
|
auto device = TRY(adopt_nonnull_lock_ref_or_enomem(new (nothrow) DeviceType(forward<Args>(args)...)));
|
2021-09-11 09:19:20 +03:00
|
|
|
device->after_inserting();
|
|
|
|
return device;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2022-08-19 21:53:40 +03:00
|
|
|
LockRefPtr<NullDevice> m_null_device;
|
|
|
|
LockRefPtr<ConsoleDevice> m_console_device;
|
|
|
|
LockRefPtr<DeviceControlDevice> m_device_control_device;
|
2021-09-16 22:24:55 +03:00
|
|
|
// FIXME: Once we have a singleton for managing many sound cards, remove this from here
|
2022-08-18 22:46:28 +03:00
|
|
|
SpinlockProtected<HashMap<u64, Device*>> m_devices { LockRank::None };
|
2021-12-20 12:10:35 +03:00
|
|
|
|
2022-08-18 22:46:28 +03:00
|
|
|
mutable Spinlock m_event_queue_lock { LockRank::None };
|
2021-12-20 12:10:35 +03:00
|
|
|
CircularQueue<DeviceEvent, 100> m_event_queue;
|
2021-09-11 09:19:20 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|