libc: implement SNDCTL I/O control handling

In the same vein as the terminal and block I/O controls, the sound
controls are implemented via poperty files and match the OSS
API ([1] features a nice overview while [2] is v3 and [3] gives
in-depth information on the current v4.x API we eventually might want
to implement).

  [1] https://wiki.freebsd.org/RyanBeasley/ioctlref/
  [2] http://www.opensound.com/pguide/oss.pdf
  [3] http://manuals.opensound.com/developer/

The controls currently implemented are the ones used by the cmus OSS
output plugin, which was the driving factor behind the implementation.
It uses the obsolete (v3) API and does not check if the requested
parameter was actually set, which should be done according to the
official OSS documentation.

At the moment it is not possible to set or rather change any
parameters. In case the requested setting differs from the parameters
of the underlying Audio_out session - in contrast to the suggestion in
the OSS manual - we do not silently adjust the parameters returned
to the callee but outright fail the I/O control operation.

The following list contains all currently handled I/O controls.

  * SNDCTL_DSP_CHANNELS sets the number of channels. We return the
    available channels here and return ENOTSUP if it differs from
    the requested number of channels.

  * SNDCTL_DSP_GETOSPACE returns amount of playback data that can
    be written without blocking. For now it amounts the space left
    in the Audio_out packet-stream.

  * SNDCTL_DSP_POST forces playback to start. We do nothing and return
    success.

  * SNDCTL_DSP_RESET is supposed to reset the device when it is
    active before any parameters are changed. We do nothing and return
    success.

  * SNDCTL_DSP_SAMPLESIZE sets the sample size. We return the
    sample size of the underlying Audio_out session and return ENOTSUP
    if it differs from the requested number of channels.

  * SNDCTL_DSP_SETFRAGMENT sets the buffer size hint. We ignore the
    hint and return success.

  * SNDCTL_DSP_SPEED sets the samplerate. For now, we always return
    the rate of the underlying Audio_out session and return ENOTSUP
    if it differs from the requested one.

This commit serves as a starting point for further implementing the
OSS API by exploring more users, e.g. as VirtualBox/Qt5/SDL2 audio
backend or a more sophisticated progam like sndiod.

Issue #3891.
This commit is contained in:
Josef Söntgen 2020-09-08 16:39:23 +02:00 committed by Christian Helmuth
parent bcf1cc6397
commit 3d2b0cab93
2 changed files with 213 additions and 0 deletions

View File

@ -109,6 +109,11 @@ class Libc::Vfs_plugin : public Plugin
*/
Ioctl_result _ioctl_dio(File_descriptor *, unsigned long, char *);
/**
* Sound related I/O controls
*/
Ioctl_result _ioctl_sndctl(File_descriptor *, unsigned long, char *);
/**
* Call functor 'fn' with ioctl info for the given file descriptor 'fd'
*

View File

@ -30,6 +30,7 @@
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <sys/disk.h>
#include <sys/soundcard.h>
#include <dlfcn.h>
/* libc plugin interface */
@ -1169,6 +1170,204 @@ Libc::Vfs_plugin::_ioctl_dio(File_descriptor *fd, unsigned long request, char *a
}
Libc::Vfs_plugin::Ioctl_result
Libc::Vfs_plugin::_ioctl_sndctl(File_descriptor *fd, unsigned long request, char *argp)
{
if (!argp)
return { true, EINVAL };
bool handled = false;
/*
* Initialize to "success" and any ioctl is required to set
* in case of error.
*
* This method will either return handled equals true if the I/O control
* was handled successfully or failed and the result is not successfull
* (see the end of this method).
*
*/
int result = 0;
if (request == SNDCTL_DSP_CHANNELS) {
monitor().monitor([&] {
_with_info(*fd, [&] (Xml_node info) {
if (info.type() != "oss") {
return;
}
unsigned int const avail_chans =
info.attribute_value("channels", 0U);
if (avail_chans == 0U) {
result = EINVAL;
return;
}
int const num_chans = *(int const*)argp;
if (num_chans < 0) {
result = EINVAL;
return;
}
if ((unsigned)num_chans != avail_chans) {
result = ENOTSUP;
return;
}
*(int*)argp = avail_chans;
handled = true;
});
return Fn::COMPLETE;
});
} else if (request == SNDCTL_DSP_GETOSPACE) {
monitor().monitor([&] {
_with_info(*fd, [&] (Xml_node info) {
if (info.type() != "oss") {
return;
}
unsigned int const frag_size =
info.attribute_value("frag_size", 0U);
unsigned int const frag_avail =
info.attribute_value("frag_avail", 0U);
if (!frag_avail || !frag_size) {
result = ENOTSUP;
return;
}
int const fragsize = (int)frag_size;
int const fragments = (int)frag_avail;
if (fragments < 0 || fragsize < 0) {
result = EINVAL;
return;
}
struct audio_buf_info *buf_info =
(struct audio_buf_info*)argp;
buf_info->fragments = fragments;
buf_info->fragsize = fragsize;
buf_info->bytes = fragments * fragsize;
handled = true;
});
return Fn::COMPLETE;
});
} else if (request == SNDCTL_DSP_POST) {
handled = true;
} else if (request == SNDCTL_DSP_RESET) {
handled = true;
} else if (request == SNDCTL_DSP_SAMPLESIZE) {
monitor().monitor([&] {
_with_info(*fd, [&] (Xml_node info) {
if (info.type() != "oss") {
return;
}
unsigned int const format =
info.attribute_value("format", ~0U);
if (format == ~0U) {
result = EINVAL;
return;
}
int const requested_fmt = *(int const*)argp;
if (requested_fmt != (int)format) {
result = ENOTSUP;
return;
}
handled = true;
});
return Fn::COMPLETE;
});
} else if (request == SNDCTL_DSP_SETFRAGMENT) {
monitor().monitor([&] {
_with_info(*fd, [&] (Xml_node info) {
if (info.type() != "oss") {
return;
}
unsigned int const frag_size =
info.attribute_value("frag_size", 0U);
unsigned int const frag_size_log2 =
frag_size ? Genode::log2(frag_size) : 0;
unsigned int const queue_size =
info.attribute_value("queue_size", 0U);
if (!queue_size || !frag_size_log2) {
result = ENOTSUP;
return;
}
/* ignore the given hint */
handled = true;
});
return Fn::COMPLETE;
});
} else if (request == SNDCTL_DSP_SPEED) {
monitor().monitor([&] {
_with_info(*fd, [&] (Xml_node info) {
if (info.type() != "oss") {
return;
}
unsigned int const samplerate =
info.attribute_value("sample_rate", 0U);
if (samplerate == 0U) {
result = EINVAL;
return;
}
int const speed = *(int const*)argp;
if (speed < 0) {
result = EINVAL;
return;
}
if ((unsigned)speed != samplerate) {
result = ENOTSUP;
return;
}
*(int*)argp = samplerate;
handled = true;
});
return Fn::COMPLETE;
});
}
/*
* Either handled or a failed attempt will mark the I/O control
* as handled.
*/
return { handled || result != 0, result };
}
int Libc::Vfs_plugin::ioctl(File_descriptor *fd, unsigned long request, char *argp)
{
Ioctl_result result { false, 0 };
@ -1184,6 +1383,15 @@ int Libc::Vfs_plugin::ioctl(File_descriptor *fd, unsigned long request, char *ar
case DIOCGMEDIASIZE:
result = _ioctl_dio(fd, request, argp);
break;
case SNDCTL_DSP_CHANNELS:
case SNDCTL_DSP_GETOSPACE:
case SNDCTL_DSP_POST:
case SNDCTL_DSP_RESET:
case SNDCTL_DSP_SAMPLESIZE:
case SNDCTL_DSP_SETFRAGMENT:
case SNDCTL_DSP_SPEED:
result = _ioctl_sndctl(fd, request, argp);
break;
default:
break;
}