Переглянути джерело

Kernel/Devices: Use try_create_device helper for SB16

Liav A 3 роки тому
батько
коміт
44f5f72add

+ 4 - 0
Kernel/Devices/DeviceManagement.cpp

@@ -22,6 +22,10 @@ UNMAP_AFTER_INIT void DeviceManagement::initialize()
     s_the.ensure_instance();
     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)
 UNMAP_AFTER_INIT void DeviceManagement::attach_console_device(ConsoleDevice const& device)
 {
 {

+ 6 - 0
Kernel/Devices/DeviceManagement.h

@@ -18,6 +18,7 @@
 #include <Kernel/Devices/ConsoleDevice.h>
 #include <Kernel/Devices/ConsoleDevice.h>
 #include <Kernel/Devices/Device.h>
 #include <Kernel/Devices/Device.h>
 #include <Kernel/Devices/NullDevice.h>
 #include <Kernel/Devices/NullDevice.h>
+#include <Kernel/Devices/SB16.h>
 #include <Kernel/UnixTypes.h>
 #include <Kernel/UnixTypes.h>
 
 
 namespace Kernel {
 namespace Kernel {
@@ -34,6 +35,9 @@ public:
     bool is_console_device_attached() const { return !m_console_device.is_null(); }
     bool is_console_device_attached() const { return !m_console_device.is_null(); }
     void attach_console_device(ConsoleDevice const&);
     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 after_inserting_device(Badge<Device>, Device&);
     void before_device_removal(Badge<Device>, Device&);
     void before_device_removal(Badge<Device>, Device&);
 
 
@@ -57,6 +61,8 @@ public:
 private:
 private:
     RefPtr<NullDevice> m_null_device;
     RefPtr<NullDevice> m_null_device;
     RefPtr<ConsoleDevice> m_console_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;
     MutexProtected<HashMap<u32, Device*>> m_devices;
 };
 };
 
 

+ 7 - 17
Kernel/Devices/SB16.cpp

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

+ 5 - 4
Kernel/Devices/SB16.h

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

+ 4 - 1
Kernel/init.cpp

@@ -317,7 +317,10 @@ void init_stage2(void*)
     (void)FullDevice::must_create().leak_ref();
     (void)FullDevice::must_create().leak_ref();
     (void)RandomDevice::must_create().leak_ref();
     (void)RandomDevice::must_create().leak_ref();
     PTYMultiplexer::initialize();
     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());
     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()) {
     if (VirtualFileSystem::the().mount_root(StorageManagement::the().root_filesystem()).is_error()) {