summaryrefslogtreecommitdiff
path: root/Kernel/Devices/Audio/Management.cpp
blob: abf57a4991363a4e5ff4eae23c505d35538a4c82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
 * Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/Singleton.h>
#include <Kernel/Bus/PCI/API.h>
#include <Kernel/Bus/PCI/IDs.h>
#include <Kernel/Devices/Audio/AC97.h>
#include <Kernel/Devices/Audio/Management.h>
#include <Kernel/Sections.h>

namespace Kernel {

static Singleton<AudioManagement> s_the;
static Atomic<u32> s_device_minor_number;

MajorNumber AudioManagement::audio_type_major_number()
{
    return 116;
}
MinorNumber AudioManagement::generate_storage_minor_number()
{
    auto minor_number = s_device_minor_number.load();
    s_device_minor_number++;
    return minor_number;
}

AudioManagement& AudioManagement::the()
{
    return *s_the;
}

UNMAP_AFTER_INIT AudioManagement::AudioManagement()
{
}

UNMAP_AFTER_INIT void AudioManagement::enumerate_hardware_controllers()
{
    if (!PCI::Access::is_disabled()) {
        MUST(PCI::enumerate([&](PCI::DeviceIdentifier const& device_identifier) {
            // Note: Only consider PCI audio controllers
            if (device_identifier.class_code().value() != to_underlying(PCI::ClassID::Multimedia)
                || device_identifier.subclass_code().value() != to_underlying(PCI::Multimedia::SubclassID::AudioController))
                return;

            dbgln("AC97: found audio controller at {}", device_identifier.address());
            auto ac97_device = AC97::try_create(device_identifier);
            if (ac97_device.is_error()) {
                // FIXME: Propagate errors properly
                dbgln("AudioManagement: failed to initialize AC97 device: {}", ac97_device.error());
                return;
            }
            m_controllers_list.append(ac97_device.release_value());
        }));
    }
}

UNMAP_AFTER_INIT void AudioManagement::enumerate_hardware_audio_channels()
{
    for (auto& controller : m_controllers_list)
        controller.detect_hardware_audio_channels({});
}

UNMAP_AFTER_INIT bool AudioManagement::initialize()
{

    /* Explanation on the flow:
     * 1. Enumerate all audio controllers connected to the system:
     *  a. Try to find the SB16 ISA-based controller.
     *  b. Enumerate the PCI bus and try to find audio controllers there too
     * 2. Ask each controller to detect the audio channels and instantiate AudioChannel objects.
     */
    enumerate_hardware_controllers();
    enumerate_hardware_audio_channels();

    if (m_controllers_list.is_empty()) {
        dbgln("No audio controller was initialized.");
        return false;
    }
    return true;
}

}