1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-06-01 10:38:13 +00:00

Kernel/Devices: Use try_create_device helper for SB16

This commit is contained in:
Liav A 2021-09-16 22:24:55 +03:00 committed by Idan Horowitz
parent fd4397a430
commit 44f5f72add
5 changed files with 26 additions and 22 deletions

View file

@ -22,6 +22,10 @@ UNMAP_AFTER_INIT void DeviceManagement::initialize()
s_the.ensure_instance();
}
UNMAP_AFTER_INIT void DeviceManagement::attach_sb16_device(SB16 const& device)
{
m_sb16_device = device;
}
UNMAP_AFTER_INIT void DeviceManagement::attach_console_device(ConsoleDevice const& device)
{

View file

@ -18,6 +18,7 @@
#include <Kernel/Devices/ConsoleDevice.h>
#include <Kernel/Devices/Device.h>
#include <Kernel/Devices/NullDevice.h>
#include <Kernel/Devices/SB16.h>
#include <Kernel/UnixTypes.h>
namespace Kernel {
@ -34,6 +35,9 @@ public:
bool is_console_device_attached() const { return !m_console_device.is_null(); }
void attach_console_device(ConsoleDevice const&);
// FIXME: Once we have a singleton for managing many sound cards, remove this from here
void attach_sb16_device(SB16 const&);
void after_inserting_device(Badge<Device>, Device&);
void before_device_removal(Badge<Device>, Device&);
@ -57,6 +61,8 @@ public:
private:
RefPtr<NullDevice> m_null_device;
RefPtr<ConsoleDevice> m_console_device;
// FIXME: Once we have a singleton for managing many sound cards, remove this from here
RefPtr<SB16> m_sb16_device;
MutexProtected<HashMap<u32, Device*>> m_devices;
};

View file

@ -5,10 +5,10 @@
*/
#include <AK/Memory.h>
#include <AK/Singleton.h>
#include <AK/StringView.h>
#include <Kernel/Arch/x86/InterruptDisabler.h>
#include <Kernel/Debug.h>
#include <Kernel/Devices/DeviceManagement.h>
#include <Kernel/Devices/SB16.h>
#include <Kernel/IO.h>
#include <Kernel/Memory/AnonymousVMObject.h>
@ -60,8 +60,6 @@ void SB16::set_sample_rate(uint16_t hz)
dsp_write((u8)hz);
}
static Singleton<SB16> s_the;
UNMAP_AFTER_INIT SB16::SB16()
: IRQHandler(SB16_DEFAULT_IRQ)
// FIXME: We can't change version numbers later, i.e. after the sound card is initialized.
@ -74,7 +72,7 @@ UNMAP_AFTER_INIT SB16::~SB16()
{
}
UNMAP_AFTER_INIT void SB16::detect()
UNMAP_AFTER_INIT RefPtr<SB16> SB16::try_detect_and_create()
{
IO::out8(0x226, 1);
IO::delay(32);
@ -82,19 +80,11 @@ UNMAP_AFTER_INIT void SB16::detect()
auto data = dsp_read();
if (data != 0xaa)
return;
SB16::create();
}
UNMAP_AFTER_INIT void SB16::create()
{
s_the.ensure_instance();
s_the->after_inserting();
}
SB16& SB16::the()
{
return *s_the;
return {};
auto device_or_error = DeviceManagement::try_create_device<SB16>();
if (device_or_error.is_error())
return {};
return device_or_error.release_value();
}
UNMAP_AFTER_INIT void SB16::initialize()

View file

@ -18,13 +18,12 @@ class SB16;
class SB16 final : public IRQHandler
, public CharacterDevice {
friend class DeviceManagement;
public:
SB16();
virtual ~SB16() override;
static void detect();
static void create();
static SB16& the();
static RefPtr<SB16> try_detect_and_create();
// ^CharacterDevice
virtual bool can_read(const OpenFileDescription&, size_t) const override;
@ -37,6 +36,8 @@ public:
virtual KResult ioctl(OpenFileDescription&, unsigned, Userspace<void*>) override;
private:
SB16();
// ^IRQHandler
virtual bool handle_irq(const RegisterState&) override;

View file

@ -317,7 +317,10 @@ void init_stage2(void*)
(void)FullDevice::must_create().leak_ref();
(void)RandomDevice::must_create().leak_ref();
PTYMultiplexer::initialize();
SB16::detect();
// FIXME: Once we have a singleton for managing many sound cards, remove this from here
if (auto device = SB16::try_detect_and_create(); !!device)
DeviceManagement::the().attach_sb16_device(*device);
StorageManagement::the().initialize(kernel_command_line().root_device(), kernel_command_line().is_force_pio());
if (VirtualFileSystem::the().mount_root(StorageManagement::the().root_filesystem()).is_error()) {