Kernel: Implement ioctl for the SB16 to change sample rate

Two new ioctl requests are used to get and set the sample rate of the
sound card. The SB16 device keeps track of the sample rate separately,
because I don't want to figure out how to read the sample rate from the
device; it's easier that way.

The soundcard write doesn't set the sample rate to 44100 Hz every time
anymore, as we want to change it externally.
This commit is contained in:
kleines Filmröllchen 2021-08-19 00:04:03 +02:00 committed by Ali Mohammad Pur
parent 2c9afaf5ac
commit d0ceaa24a6
3 changed files with 34 additions and 2 deletions

View file

@ -15,6 +15,7 @@
#include <Kernel/Memory/MemoryManager.h>
#include <Kernel/Sections.h>
#include <Kernel/Thread.h>
#include <LibC/sys/ioctl_numbers.h>
namespace Kernel {
@ -49,6 +50,8 @@ u8 SB16::dsp_read()
/* Changes the sample rate of sound output */
void SB16::set_sample_rate(uint16_t hz)
{
dbgln("SB16: Changing sample rate to {} Hz", hz);
m_sample_rate = hz;
dsp_write(0x41); // output
dsp_write((u8)(hz >> 8));
dsp_write((u8)hz);
@ -115,6 +118,30 @@ UNMAP_AFTER_INIT void SB16::initialize()
dmesgln("SB16: Found version {}.{}", m_major_version, vmin);
set_irq_register(SB16_DEFAULT_IRQ);
dmesgln("SB16: IRQ {}", get_irq_line());
set_sample_rate(m_sample_rate);
}
KResult SB16::ioctl(FileDescription&, unsigned request, Userspace<void*> arg)
{
switch (request) {
case SOUNDCARD_IOCTL_GET_SAMPLE_RATE: {
auto output = static_ptr_cast<u16*>(arg);
if (!copy_to_user(output, &m_sample_rate))
return EFAULT;
return KSuccess;
}
case SOUNDCARD_IOCTL_SET_SAMPLE_RATE: {
auto sample_rate_value = static_cast<u16>(arg.ptr());
if (sample_rate_value == 0)
return EINVAL;
if (m_sample_rate != sample_rate_value)
set_sample_rate(sample_rate_value);
return KSuccess;
}
default:
return EINVAL;
}
}
void SB16::set_irq_register(u8 irq_number)
@ -256,8 +283,6 @@ KResultOr<size_t> SB16::write(FileDescription&, u64, UserOrKernelBuffer const& d
u8 mode = (u8)SampleFormat::Signed | (u8)SampleFormat::Stereo;
const int sample_rate = 44100;
set_sample_rate(sample_rate);
if (!data.read(m_dma_region->vaddr().as_ptr(), length))
return EFAULT;
dma_start(length);

View file

@ -38,6 +38,8 @@ public:
virtual mode_t required_mode() const override { return 0220; }
virtual String device_name() const override { return "audio"; }
virtual KResult ioctl(FileDescription&, unsigned, Userspace<void*>) override;
private:
// ^IRQHandler
virtual bool handle_irq(const RegisterState&) override;
@ -57,6 +59,7 @@ private:
OwnPtr<Memory::Region> m_dma_region;
int m_major_version { 0 };
u16 m_sample_rate { 44100 };
WaitQueue m_irq_queue;
};

View file

@ -87,6 +87,8 @@ enum IOCtlNumber {
KCOV_SETBUFSIZE,
KCOV_ENABLE,
KCOV_DISABLE,
SOUNDCARD_IOCTL_SET_SAMPLE_RATE,
SOUNDCARD_IOCTL_GET_SAMPLE_RATE
};
#define TIOCGPGRP TIOCGPGRP
@ -128,3 +130,5 @@ enum IOCtlNumber {
#define FIBMAP FIBMAP
#define FIONBIO FIONBIO
#define FIONREAD FIONREAD
#define SOUNDCARD_IOCTL_SET_SAMPLE_RATE SOUNDCARD_IOCTL_SET_SAMPLE_RATE
#define SOUNDCARD_IOCTL_GET_SAMPLE_RATE SOUNDCARD_IOCTL_GET_SAMPLE_RATE