mirror of
https://github.com/RGBCube/serenity
synced 2025-05-18 09:25:07 +00:00

We have 3 new components: 1. The AudioManagement singleton. This class like in other subsystems, is responsible to find hardware audio controllers and keep a reference to them. 2. AudioController class - this class is the parent class for hardware controllers like the Sound Blaster 16 or Intel 82801AA (AC97). For now, this class has simple interface for getting and controlling sample rate of audio channels, as well a write interface for specific audio channel but not reading from it. One AudioController object might have multiple AudioChannel "child" objects to hold with reference counting. 3. AudioChannel class - this is based on the CharacterDevice class, and represents hardware PCM audio channel. It facilitates an ioctl interface which should be consistent across all supported hardware currently. It has a weak reference to a parent AudioController, and when trying to write to a channel, it redirects the data to the parent AudioController. Each audio channel device should be added into a new directory under the /dev filesystem called "audio".
73 lines
2.3 KiB
C++
73 lines
2.3 KiB
C++
/*
|
|
* Copyright (c) 2022, Liav A. <liavalb@hotmail.co.il>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#include <Kernel/Devices/Audio/Management.h>
|
|
#include <Kernel/Devices/DeviceManagement.h>
|
|
#include <Kernel/Devices/RandomDevice.h>
|
|
#include <Kernel/Random.h>
|
|
#include <Kernel/Sections.h>
|
|
#include <LibC/sys/ioctl_numbers.h>
|
|
|
|
namespace Kernel {
|
|
|
|
UNMAP_AFTER_INIT NonnullRefPtr<AudioChannel> AudioChannel::must_create(AudioController const& controller, size_t channel_index)
|
|
{
|
|
auto audio_device_or_error = DeviceManagement::try_create_device<AudioChannel>(controller, channel_index);
|
|
// FIXME: Find a way to propagate errors
|
|
VERIFY(!audio_device_or_error.is_error());
|
|
return audio_device_or_error.release_value();
|
|
}
|
|
|
|
AudioChannel::AudioChannel(AudioController const& controller, size_t channel_index)
|
|
: CharacterDevice(AudioManagement::the().audio_type_major_number(), AudioManagement::the().generate_storage_minor_number())
|
|
, m_controller(controller)
|
|
, m_channel_index(channel_index)
|
|
{
|
|
}
|
|
|
|
ErrorOr<void> AudioChannel::ioctl(OpenFileDescription&, unsigned request, Userspace<void*> arg)
|
|
{
|
|
auto controller = m_controller.strong_ref();
|
|
if (!controller)
|
|
return Error::from_errno(EIO);
|
|
switch (request) {
|
|
case SOUNDCARD_IOCTL_GET_SAMPLE_RATE: {
|
|
auto output = static_ptr_cast<u32*>(arg);
|
|
u32 sample_rate = 0;
|
|
sample_rate = TRY(controller->get_pcm_output_sample_rate(m_channel_index));
|
|
return copy_to_user(output, &sample_rate);
|
|
}
|
|
case SOUNDCARD_IOCTL_SET_SAMPLE_RATE: {
|
|
auto sample_rate = static_cast<u32>(arg.ptr());
|
|
TRY(controller->set_pcm_output_sample_rate(m_channel_index, sample_rate));
|
|
return {};
|
|
}
|
|
default:
|
|
return EINVAL;
|
|
}
|
|
}
|
|
|
|
bool AudioChannel::can_read(const OpenFileDescription&, u64) const
|
|
{
|
|
// FIXME: Implement input from device
|
|
return false;
|
|
}
|
|
|
|
ErrorOr<size_t> AudioChannel::read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t)
|
|
{
|
|
// FIXME: Implement input from device
|
|
return Error::from_errno(ENOTIMPL);
|
|
}
|
|
|
|
ErrorOr<size_t> AudioChannel::write(OpenFileDescription&, u64, UserOrKernelBuffer const& buffer, size_t size)
|
|
{
|
|
auto controller = m_controller.strong_ref();
|
|
if (!controller)
|
|
return Error::from_errno(EIO);
|
|
return controller->write(m_channel_index, buffer, size);
|
|
}
|
|
|
|
}
|