summaryrefslogtreecommitdiffstats
path: root/oss
diff options
context:
space:
mode:
authorTakashi Iwai <tiwai@suse.de>2006-02-21 16:13:57 +0000
committerTakashi Iwai <tiwai@suse.de>2006-02-21 16:13:57 +0000
commit6890d16836e85deed9e8ca91d095473347013ccc (patch)
treebf5fa3bcb64af29d6264ff925f8e667e02bd16fc /oss
parent6074e887419c70bed8a9177a4d78357156c7c611 (diff)
Structure reorganziation, added polyp plugin
- Reorganized the directory structure: Now each plugin(s) is loaded in own subdirectory. - Added polypaudio plugin by Pierre Ossman <ossman@cendio.se> - Fixed COPYING file to LGPL (under which all codes are released, so far)
Diffstat (limited to 'oss')
-rw-r--r--oss/Makefile.am14
-rw-r--r--oss/ctl_oss.c452
-rw-r--r--oss/pcm_oss.c423
3 files changed, 889 insertions, 0 deletions
diff --git a/oss/Makefile.am b/oss/Makefile.am
new file mode 100644
index 0000000..84223bc
--- /dev/null
+++ b/oss/Makefile.am
@@ -0,0 +1,14 @@
+asound_module_pcm_oss_LTLIBRARIES = libasound_module_pcm_oss.la
+asound_module_ctl_oss_LTLIBRARIES = libasound_module_ctl_oss.la
+
+asound_module_pcm_ossdir = $(libdir)/alsa-lib
+asound_module_ctl_ossdir = $(libdir)/alsa-lib
+
+AM_CFLAGS = -Wall -g @ALSA_CFLAGS@
+AM_LDFLAGS = -module -avoid-version -export-dynamic
+
+libasound_module_pcm_oss_la_SOURCES = pcm_oss.c
+libasound_module_pcm_oss_la_LIBADD = @ALSA_LIBS@
+
+libasound_module_ctl_oss_la_SOURCES = ctl_oss.c
+libasound_module_ctl_oss_la_LIBADD = @ALSA_LIBS@
diff --git a/oss/ctl_oss.c b/oss/ctl_oss.c
new file mode 100644
index 0000000..a7cfa8f
--- /dev/null
+++ b/oss/ctl_oss.c
@@ -0,0 +1,452 @@
+/*
+ * ALSA <-> OSS mixer control plugin
+ *
+ * Copyright (c) 2005 by Takashi Iwai <tiwai@suse.de>
+ *
+ * This library is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+/*
+ * TODO: implement the pseudo poll with thread (and pipe as pollfd)?
+ */
+
+#include <stdio.h>
+#include <sys/ioctl.h>
+#include <alsa/asoundlib.h>
+#include <alsa/control_external.h>
+#include <linux/soundcard.h>
+
+typedef struct snd_ctl_oss {
+ snd_ctl_ext_t ext;
+ char *device;
+ int fd;
+ int exclusive_input;
+ int stereo_mask;
+ int num_vol_ctls;
+ int vol_ctl[SOUND_MIXER_NRDEVICES];
+ int num_rec_items;
+ int rec_item[SOUND_MIXER_NRDEVICES];
+} snd_ctl_oss_t;
+
+static const char *vol_devices[SOUND_MIXER_NRDEVICES] = {
+ [SOUND_MIXER_VOLUME] = "Master Playback Volume",
+ [SOUND_MIXER_BASS] = "Tone Control - Bass",
+ [SOUND_MIXER_TREBLE] = "Tone Control - Treble",
+ [SOUND_MIXER_SYNTH] = "Synth Playback Volume",
+ [SOUND_MIXER_PCM] = "PCM Playback Volume",
+ [SOUND_MIXER_SPEAKER] = "PC Speaker Playback Volume",
+ [SOUND_MIXER_LINE] = "Line Playback Volume",
+ [SOUND_MIXER_MIC] = "Mic Playback Volume",
+ [SOUND_MIXER_CD] = "CD Playback Volume",
+ [SOUND_MIXER_IMIX] = "Monitor Mix Playback Volume",
+ [SOUND_MIXER_ALTPCM] = "Headphone Playback Volume",
+ [SOUND_MIXER_RECLEV] = "Capture Volume",
+ [SOUND_MIXER_IGAIN] = "Capture Volume",
+ [SOUND_MIXER_OGAIN] = "Playback Volume",
+ [SOUND_MIXER_LINE1] = "Aux Playback Volume",
+ [SOUND_MIXER_LINE2] = "Aux1 Playback Volume",
+ [SOUND_MIXER_LINE3] = "Line1 Playback Volume",
+ [SOUND_MIXER_DIGITAL1] = "IEC958 Playback Volume",
+ [SOUND_MIXER_DIGITAL2] = "Digital Playback Volume",
+ [SOUND_MIXER_DIGITAL3] = "Digital1 Playback Volume",
+ [SOUND_MIXER_PHONEIN] = "Phone Playback Volume",
+ [SOUND_MIXER_PHONEOUT] = "Master Mono Playback Volume",
+ [SOUND_MIXER_VIDEO] = "Video Playback Volume",
+ [SOUND_MIXER_RADIO] = "Radio Playback Volume",
+ [SOUND_MIXER_MONITOR] = "Monitor Playback Volume",
+};
+
+static const char *rec_devices[SOUND_MIXER_NRDEVICES] = {
+ [SOUND_MIXER_VOLUME] = "Mix Capture Switch",
+ [SOUND_MIXER_SYNTH] = "Synth Capture Switch",
+ [SOUND_MIXER_PCM] = "PCM Capture Switch",
+ [SOUND_MIXER_LINE] = "Line Capture Switch",
+ [SOUND_MIXER_MIC] = "Mic Capture Switch",
+ [SOUND_MIXER_CD] = "CD Capture Switch",
+ [SOUND_MIXER_LINE1] = "Aux Capture Switch",
+ [SOUND_MIXER_LINE2] = "Aux1 Capture Switch",
+ [SOUND_MIXER_LINE3] = "Line1 Capture Switch",
+ [SOUND_MIXER_DIGITAL1] = "IEC958 Capture Switch",
+ [SOUND_MIXER_DIGITAL2] = "Digital Capture Switch",
+ [SOUND_MIXER_DIGITAL3] = "Digital1 Capture Switch",
+ [SOUND_MIXER_PHONEIN] = "Phone Capture Switch",
+ [SOUND_MIXER_VIDEO] = "Video Capture Switch",
+ [SOUND_MIXER_RADIO] = "Radio Capture Switch",
+};
+
+static const char *rec_items[SOUND_MIXER_NRDEVICES] = {
+ [SOUND_MIXER_VOLUME] = "Mix",
+ [SOUND_MIXER_SYNTH] = "Synth",
+ [SOUND_MIXER_PCM] = "PCM",
+ [SOUND_MIXER_LINE] = "Line",
+ [SOUND_MIXER_MIC] = "Mic",
+ [SOUND_MIXER_CD] = "CD",
+ [SOUND_MIXER_LINE1] = "Aux",
+ [SOUND_MIXER_LINE2] = "Aux1",
+ [SOUND_MIXER_LINE3] = "Line1",
+ [SOUND_MIXER_DIGITAL1] = "IEC958",
+ [SOUND_MIXER_DIGITAL2] = "Digital",
+ [SOUND_MIXER_DIGITAL3] = "Digital1",
+ [SOUND_MIXER_PHONEIN] = "Phone",
+ [SOUND_MIXER_VIDEO] = "Video",
+ [SOUND_MIXER_RADIO] = "Radio",
+};
+
+static void oss_close(snd_ctl_ext_t *ext)
+{
+ snd_ctl_oss_t *oss = ext->private_data;
+
+ close(oss->fd);
+ free(oss->device);
+ free(oss);
+}
+
+static int oss_elem_count(snd_ctl_ext_t *ext)
+{
+ snd_ctl_oss_t *oss = ext->private_data;
+ int num;
+
+ num = oss->num_vol_ctls;
+ if (oss->exclusive_input)
+ num++;
+ else if (oss->num_rec_items)
+ num += oss->num_rec_items;
+ return num;
+}
+
+static int oss_elem_list(snd_ctl_ext_t *ext, unsigned int offset, snd_ctl_elem_id_t *id)
+{
+ snd_ctl_oss_t *oss = ext->private_data;
+
+ snd_ctl_elem_id_set_interface(id, SND_CTL_ELEM_IFACE_MIXER);
+ if (offset < oss->num_vol_ctls)
+ snd_ctl_elem_id_set_name(id, vol_devices[oss->vol_ctl[offset]]);
+ else if (oss->exclusive_input)
+ snd_ctl_elem_id_set_name(id, "Capture Source");
+ else {
+ offset -= oss->num_vol_ctls;
+ snd_ctl_elem_id_set_name(id, rec_devices[oss->rec_item[offset]]);
+ }
+ return 0;
+}
+
+#define OSS_KEY_DEVICE_MASK 0x1f
+#define OSS_KEY_CAPTURE_FLAG (1 << 8)
+#define OSS_KEY_CAPTURE_MUX (1 << 16)
+
+static snd_ctl_ext_key_t oss_find_elem(snd_ctl_ext_t *ext,
+ const snd_ctl_elem_id_t *id)
+{
+ snd_ctl_oss_t *oss = ext->private_data;
+ const char *name;
+ int i, key;
+
+ name = snd_ctl_elem_id_get_name(id);
+ if (! strcmp(name, "Capture Source")) {
+ if (oss->exclusive_input)
+ return OSS_KEY_CAPTURE_MUX;
+ else
+ return SND_CTL_EXT_KEY_NOT_FOUND;
+ }
+ for (i = 0; i < oss->num_vol_ctls; i++) {
+ key = oss->vol_ctl[i];
+ if (! strcmp(name, vol_devices[key]))
+ return key;
+ }
+ for (i = 0; i < oss->num_rec_items; i++) {
+ key = oss->rec_item[i];
+ if (! strcmp(name, rec_devices[key]))
+ return key | OSS_KEY_CAPTURE_FLAG;
+ }
+ return SND_CTL_EXT_KEY_NOT_FOUND;
+}
+
+static int oss_get_attribute(snd_ctl_ext_t *ext, snd_ctl_ext_key_t key,
+ int *type, unsigned int *acc, unsigned int *count)
+{
+ snd_ctl_oss_t *oss = ext->private_data;
+
+ if (key == OSS_KEY_CAPTURE_MUX) {
+ *type = SND_CTL_ELEM_TYPE_ENUMERATED;
+ *acc = SND_CTL_EXT_ACCESS_READWRITE;
+ *count = 1;
+ } else if (key & OSS_KEY_CAPTURE_FLAG) {
+ *type = SND_CTL_ELEM_TYPE_BOOLEAN;
+ *acc = SND_CTL_EXT_ACCESS_READWRITE;
+ *count = 1;
+ } else {
+ *type = SND_CTL_ELEM_TYPE_INTEGER;
+ *acc = SND_CTL_EXT_ACCESS_READWRITE;
+ if (oss->stereo_mask & (1 << key))
+ *count = 2;
+ else
+ *count = 1;
+ }
+ return 0;
+}
+
+static int oss_get_integer_info(snd_ctl_ext_t *ext, snd_ctl_ext_key_t key,
+ long *imin, long *imax, long *istep)
+{
+ *istep = 0;
+ *imin = 0;
+ *imax = 100;
+ return 0;
+}
+
+static int oss_get_enumerated_info(snd_ctl_ext_t *ext, snd_ctl_ext_key_t key,
+ unsigned int *items)
+{
+ snd_ctl_oss_t *oss = ext->private_data;
+
+ *items = oss->num_rec_items;
+ return 0;
+}
+
+static int oss_get_enumerated_name(snd_ctl_ext_t *ext, snd_ctl_ext_key_t key,
+ unsigned int item, char *name, size_t name_max_len)
+{
+ snd_ctl_oss_t *oss = ext->private_data;
+
+ if (item >= oss->num_rec_items)
+ return -EINVAL;
+ item = oss->rec_item[item];
+ strncpy(name, rec_items[item], name_max_len - 1);
+ name[name_max_len - 1] = 0;
+ return 0;
+}
+
+static int oss_read_integer(snd_ctl_ext_t *ext, snd_ctl_ext_key_t key, long *value)
+{
+ snd_ctl_oss_t *oss = ext->private_data;
+ int val;
+
+ if (key & OSS_KEY_CAPTURE_FLAG) {
+ key &= OSS_KEY_DEVICE_MASK;
+ if (ioctl(oss->fd, SOUND_MIXER_READ_RECSRC, &val) < 0)
+ return -errno;
+ *value = (val & (1 << key)) ? 1 : 0;
+ } else {
+ if (ioctl(oss->fd, MIXER_READ(key), &val) < 0)
+ return -errno;
+ *value = val & 0xff;
+ if (oss->stereo_mask & (1 << key))
+ value[1] = (val >> 8) & 0xff;
+ }
+ return 0;
+}
+
+static int oss_read_enumerated(snd_ctl_ext_t *ext, snd_ctl_ext_key_t key, unsigned int *items)
+{
+ snd_ctl_oss_t *oss = ext->private_data;
+ int i, val;
+
+ *items = 0;
+ if (ioctl(oss->fd, SOUND_MIXER_READ_RECSRC, &val) < 0)
+ return -errno;
+ for (i = 0; i < oss->num_rec_items; i++) {
+ if (val & (1 << oss->rec_item[i])) {
+ *items = i;
+ break;
+ }
+ }
+ return 0;
+}
+
+static int oss_write_integer(snd_ctl_ext_t *ext, snd_ctl_ext_key_t key, long *value)
+{
+ snd_ctl_oss_t *oss = ext->private_data;
+ int val, oval;
+
+ if (key & OSS_KEY_CAPTURE_FLAG) {
+ key &= OSS_KEY_DEVICE_MASK;
+ if (ioctl(oss->fd, SOUND_MIXER_READ_RECSRC, &oval) < 0)
+ return -errno;
+ if (*value)
+ val = oval | (1 << key);
+ else
+ val = oval & ~(1 << key);
+ if (oval == val)
+ return 0;
+ if (ioctl(oss->fd, SOUND_MIXER_WRITE_RECSRC, &val) < 0)
+ return -errno;
+ return 1;
+ } else {
+ val = *value;
+ if (oss->stereo_mask & (1 << key))
+ val |= value[1] << 8;
+ if (ioctl(oss->fd, MIXER_READ(key), &oval) < 0)
+ return -errno;
+ if (oval == val)
+ return 0;
+ if (ioctl(oss->fd, MIXER_WRITE(key), &val) < 0)
+ return -errno;
+ return 1;
+ }
+}
+
+static int oss_write_enumerated(snd_ctl_ext_t *ext, snd_ctl_ext_key_t key,
+ unsigned int *items)
+{
+ snd_ctl_oss_t *oss = ext->private_data;
+ int val, oval;
+
+ if (ioctl(oss->fd, SOUND_MIXER_READ_RECSRC, &oval) < 0)
+ return -errno;
+ val = 1 << oss->rec_item[*items];
+ if (val == oval)
+ return 0;
+ if (ioctl(oss->fd, SOUND_MIXER_WRITE_RECSRC, &val) < 0)
+ return -errno;
+ return 1;
+}
+
+static int oss_read_event(snd_ctl_ext_t *ext, snd_ctl_elem_id_t *id,
+ unsigned int *event_mask)
+{
+ return -EAGAIN;
+}
+
+static snd_ctl_ext_callback_t oss_ext_callback = {
+ .close = oss_close,
+ .elem_count = oss_elem_count,
+ .elem_list = oss_elem_list,
+ .find_elem = oss_find_elem,
+ .get_attribute = oss_get_attribute,
+ .get_integer_info = oss_get_integer_info,
+ .get_enumerated_info = oss_get_enumerated_info,
+ .get_enumerated_name = oss_get_enumerated_name,
+ .read_integer = oss_read_integer,
+ .read_enumerated = oss_read_enumerated,
+ .write_integer = oss_write_integer,
+ .write_enumerated = oss_write_enumerated,
+ .read_event = oss_read_event,
+};
+
+
+SND_CTL_PLUGIN_DEFINE_FUNC(oss)
+{
+ snd_config_iterator_t it, next;
+ const char *device = "/dev/mixer";
+ struct mixer_info mixinfo;
+ int i, err, val;
+ snd_ctl_oss_t *oss;
+
+ snd_config_for_each(it, next, conf) {
+ snd_config_t *n = snd_config_iterator_entry(it);
+ const char *id;
+ if (snd_config_get_id(n, &id) < 0)
+ continue;
+ if (strcmp(id, "comment") == 0 || strcmp(id, "type") == 0)
+ continue;
+ if (strcmp(id, "device") == 0) {
+ if (snd_config_get_string(n, &device) < 0) {
+ SNDERR("Invalid type for %s", id);
+ return -EINVAL;
+ }
+ continue;
+ }
+ SNDERR("Unknown field %s", id);
+ return -EINVAL;
+ }
+
+ oss = calloc(1, sizeof(*oss));
+ oss->device = strdup(device);
+ oss->fd = -1;
+ if (oss->device == NULL) {
+ SNDERR("cannot allocate");
+ free(oss);
+ return -ENOMEM;
+ }
+ oss->fd = open(device, O_RDWR);
+ if (oss->fd < 0) {
+ err = -errno;
+ SNDERR("Cannot open device %s", device);
+ goto error;
+ }
+
+ if (ioctl(oss->fd, SOUND_MIXER_INFO, &mixinfo) < 0) {
+ err = -errno;
+ SNDERR("Cannot get mixer info for device %s", device);
+ goto error;
+ }
+
+ oss->ext.version = SND_CTL_EXT_VERSION;
+ oss->ext.card_idx = 0; /* FIXME */
+ strncpy(oss->ext.id, mixinfo.id, sizeof(oss->ext.id) - 1);
+ strcpy(oss->ext.driver, "OSS-Emulation");
+ strncpy(oss->ext.name, mixinfo.name, sizeof(oss->ext.name) - 1);
+ strncpy(oss->ext.longname, mixinfo.name, sizeof(oss->ext.longname) - 1);
+ strncpy(oss->ext.mixername, mixinfo.name, sizeof(oss->ext.mixername) - 1);
+ oss->ext.poll_fd = -1;
+ oss->ext.callback = &oss_ext_callback;
+ oss->ext.private_data = oss;
+
+ oss->num_vol_ctls = 0;
+ val = 0;
+ if (ioctl(oss->fd, SOUND_MIXER_READ_DEVMASK, &val) < 0)
+ perror("ctl_oss: DEVMASK error");
+ else {
+ for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
+ if ((val & (1 << i)) && vol_devices[i])
+ oss->vol_ctl[oss->num_vol_ctls++] = i;
+ }
+ }
+
+ if (ioctl(oss->fd, SOUND_MIXER_READ_STEREODEVS, &oss->stereo_mask) < 0)
+ perror("ctl_oss: STEREODEVS error");
+ val = 0;
+ if (ioctl(oss->fd, SOUND_MIXER_READ_CAPS, &val) < 0)
+ perror("ctl_oss: MIXER_CAPS error");
+ else if (val & SOUND_CAP_EXCL_INPUT)
+ oss->exclusive_input = 1;
+
+ oss->num_rec_items = 0;
+ val = 0;
+ if (ioctl(oss->fd, SOUND_MIXER_READ_RECMASK, &val) < 0)
+ perror("ctl_oss: MIXER_RECMASK error");
+ else {
+ for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
+ if (val & (1 << i)) {
+ if (oss->exclusive_input) {
+ if (! rec_items[i])
+ continue;
+ } else {
+ if (! rec_devices[i])
+ continue;
+ }
+ oss->rec_item[oss->num_rec_items++] = i;
+ }
+ }
+ }
+ if (! oss->num_rec_items)
+ oss->exclusive_input = 0;
+
+ err = snd_ctl_ext_create(&oss->ext, name, mode);
+ if (err < 0)
+ goto error;
+
+ *handlep = oss->ext.handle;
+ return 0;
+
+ error:
+ if (oss->fd >= 0)
+ close(oss->fd);
+ free(oss->device);
+ free(oss);
+ return err;
+}
+
+SND_CTL_PLUGIN_SYMBOL(oss);
diff --git a/oss/pcm_oss.c b/oss/pcm_oss.c
new file mode 100644
index 0000000..fb520f8
--- /dev/null
+++ b/oss/pcm_oss.c
@@ -0,0 +1,423 @@
+/*
+ * ALSA <-> OSS PCM I/O plugin
+ *
+ * Copyright (c) 2005 by Takashi Iwai <tiwai@suse.de>
+ *
+ * This library is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include <stdio.h>
+#include <sys/ioctl.h>
+#include <alsa/asoundlib.h>
+#include <alsa/pcm_external.h>
+#include <linux/soundcard.h>
+
+typedef struct snd_pcm_oss {
+ snd_pcm_ioplug_t io;
+ char *device;
+ int fd;
+ int fragment_set;
+ int caps;
+ int format;
+ unsigned int period_shift;
+ unsigned int periods;
+ unsigned int frame_bytes;
+} snd_pcm_oss_t;
+
+static snd_pcm_sframes_t oss_write(snd_pcm_ioplug_t *io,
+ const snd_pcm_channel_area_t *areas,
+ snd_pcm_uframes_t offset,
+ snd_pcm_uframes_t size)
+{
+ snd_pcm_oss_t *oss = io->private_data;
+ const char *buf;
+ ssize_t result;
+
+ /* we handle only an interleaved buffer */
+ buf = (char *)areas->addr + (areas->first + areas->step * offset) / 8;
+ size *= oss->frame_bytes;
+ result = write(oss->fd, buf, size);
+ if (result <= 0)
+ return result;
+ return result / oss->frame_bytes;
+}
+
+static snd_pcm_sframes_t oss_read(snd_pcm_ioplug_t *io,
+ const snd_pcm_channel_area_t *areas,
+ snd_pcm_uframes_t offset,
+ snd_pcm_uframes_t size)
+{
+ snd_pcm_oss_t *oss = io->private_data;
+ char *buf;
+ ssize_t result;
+
+ /* we handle only an interleaved buffer */
+ buf = (char *)areas->addr + (areas->first + areas->step * offset) / 8;
+ size *= oss->frame_bytes;
+ result = read(oss->fd, buf, size);
+ if (result <= 0)
+ return result;
+ return result / oss->frame_bytes;
+}
+
+static snd_pcm_sframes_t oss_pointer(snd_pcm_ioplug_t *io)
+{
+ snd_pcm_oss_t *oss = io->private_data;
+ struct count_info info;
+ int ptr;
+
+ if (ioctl(oss->fd, io->stream == SND_PCM_STREAM_PLAYBACK ?
+ SNDCTL_DSP_GETOPTR : SNDCTL_DSP_GETIPTR, &info) < 0) {
+ fprintf(stderr, "*** OSS: oss_pointer error\n");
+ return 0;
+ }
+ ptr = snd_pcm_bytes_to_frames(io->pcm, info.ptr);
+ return ptr;
+}
+
+static int oss_start(snd_pcm_ioplug_t *io)
+{
+ snd_pcm_oss_t *oss = io->private_data;
+ int tmp = io->stream == SND_PCM_STREAM_PLAYBACK ?
+ PCM_ENABLE_OUTPUT : PCM_ENABLE_INPUT;
+
+ if (ioctl(oss->fd, SNDCTL_DSP_SETTRIGGER, &tmp) < 0) {
+ fprintf(stderr, "*** OSS: trigger failed\n");
+ if (io->stream == SND_PCM_STREAM_CAPTURE)
+ /* fake read to trigger */
+ read(oss->fd, &tmp, 0);
+ }
+ return 0;
+}
+
+static int oss_stop(snd_pcm_ioplug_t *io)
+{
+ snd_pcm_oss_t *oss = io->private_data;
+ int tmp = 0;
+
+ ioctl(oss->fd, SNDCTL_DSP_SETTRIGGER, &tmp);
+ return 0;
+}
+
+static int oss_drain(snd_pcm_ioplug_t *io)
+{
+ snd_pcm_oss_t *oss = io->private_data;
+
+ if (io->stream == SND_PCM_STREAM_PLAYBACK)
+ ioctl(oss->fd, SNDCTL_DSP_SYNC);
+ return 0;
+}
+
+static int oss_prepare(snd_pcm_ioplug_t *io)
+{
+ snd_pcm_oss_t *oss = io->private_data;
+ int tmp;
+
+ ioctl(oss->fd, SNDCTL_DSP_RESET);
+
+ tmp = io->channels;
+ if (ioctl(oss->fd, SNDCTL_DSP_CHANNELS, &tmp) < 0) {
+ perror("SNDCTL_DSP_CHANNELS");
+ return -EINVAL;
+ }
+ tmp = oss->format;
+ if (ioctl(oss->fd, SNDCTL_DSP_SETFMT, &tmp) < 0) {
+ perror("SNDCTL_DSP_SETFMT");
+ return -EINVAL;
+ }
+ tmp = io->rate;
+ if (ioctl(oss->fd, SNDCTL_DSP_SPEED, &tmp) < 0 ||
+ tmp > io->rate * 1.01 || tmp < io->rate * 0.99) {
+ perror("SNDCTL_DSP_SPEED");
+ return -EINVAL;
+ }
+ return 0;
+}
+
+static int oss_hw_params(snd_pcm_ioplug_t *io, snd_pcm_hw_params_t *params)
+{
+ snd_pcm_oss_t *oss = io->private_data;
+ int i, tmp, err;
+ unsigned int period_bytes;
+ long oflags, flags;
+
+ oss->frame_bytes = (snd_pcm_format_physical_width(io->format) * io->channels) / 8;
+ switch (io->format) {
+ case SND_PCM_FORMAT_U8:
+ oss->format = AFMT_U8;
+ break;
+ case SND_PCM_FORMAT_S16_LE:
+ oss->format = AFMT_S16_LE;
+ break;
+ case SND_PCM_FORMAT_S16_BE:
+ oss->format = AFMT_S16_BE;
+ break;
+ default:
+ fprintf(stderr, "*** OSS: unsupported format %s\n", snd_pcm_format_name(io->format));
+ return -EINVAL;
+ }
+ period_bytes = io->period_size * oss->frame_bytes;
+ oss->period_shift = 0;
+ for (i = 31; i >= 4; i--) {
+ if (period_bytes & (1U << i)) {
+ oss->period_shift = i;
+ break;
+ }
+ }
+ if (! oss->period_shift) {
+ fprintf(stderr, "*** OSS: invalid period size %d\n", (int)io->period_size);
+ return -EINVAL;
+ }
+
+ _retry:
+ tmp = oss->period_shift | (oss->periods << 16);
+ if (ioctl(oss->fd, SNDCTL_DSP_SETFRAGMENT, &tmp) < 0) {
+ if (! oss->fragment_set) {
+ perror("SNDCTL_DSP_SETFRAGMENT");
+ fprintf(stderr, "*** period shift = %d, periods = %d\n", oss->period_shift, oss->periods);
+ return -EINVAL;
+ }
+ /* OSS has no proper way to reinitialize the fragments */
+ /* try to reopen the device */
+ close(oss->fd);
+ oss->fd = open(oss->device, io->stream == SND_PCM_STREAM_PLAYBACK ?
+ O_WRONLY : O_RDONLY);
+ if (oss->fd < 0) {
+ err = -errno;
+ SNDERR("Cannot reopen the device %s", oss->device);
+ return err;
+ }
+ io->poll_fd = oss->fd;
+ io->poll_events = io->stream == SND_PCM_STREAM_PLAYBACK ?
+ POLLOUT : POLLIN;
+ snd_pcm_ioplug_reinit_status(io);
+ oss->fragment_set = 0;
+ goto _retry;
+ }
+ oss->fragment_set = 1;
+
+ if ((flags = fcntl(oss->fd, F_GETFL)) < 0) {
+ err = -errno;
+ perror("F_GETFL");
+ } else {
+ oflags = flags;
+ if (io->nonblock)
+ flags |= O_NONBLOCK;
+ else
+ flags &= ~O_NONBLOCK;
+ if (flags != oflags &&
+ fcntl(oss->fd, F_SETFL, flags) < 0) {
+ err = -errno;
+ perror("F_SETFL");
+ }
+ }
+
+ return 0;
+}
+
+#define ARRAY_SIZE(ary) (sizeof(ary)/sizeof(ary[0]))
+
+static int oss_hw_constraint(snd_pcm_oss_t *oss)
+{
+ snd_pcm_ioplug_t *io = &oss->io;
+ static snd_pcm_access_t access_list[] = {
+ SND_PCM_ACCESS_RW_INTERLEAVED,
+ SND_PCM_ACCESS_MMAP_INTERLEAVED
+ };
+ unsigned int nformats;
+ unsigned int format[5];
+ unsigned int nchannels;
+ unsigned int channel[6];
+ /* period and buffer bytes must be power of two */
+ static unsigned int bytes_list[] = {
+ 1U<<8, 1U<<9, 1U<<10, 1U<<11, 1U<<12, 1U<<13, 1U<<14, 1U<<15,
+ 1U<<16, 1U<<17, 1U<<18, 1U<<19, 1U<<20, 1U<<21, 1U<<22, 1U<<23
+ };
+ int i, err, tmp;
+
+ /* check trigger */
+ oss->caps = 0;
+ if (ioctl(oss->fd, SNDCTL_DSP_GETCAPS, &oss->caps) >= 0) {
+ if (! (oss->caps & DSP_CAP_TRIGGER))
+ fprintf(stderr, "*** OSS: trigger is not supported!\n");
+ }
+
+ /* access type - interleaved only */
+ if ((err = snd_pcm_ioplug_set_param_list(io, SND_PCM_IOPLUG_HW_ACCESS,
+ ARRAY_SIZE(access_list), access_list)) < 0)
+ return err;
+
+ /* supported formats */
+ tmp = 0;
+ ioctl(oss->fd, SNDCTL_DSP_GETFMTS, &tmp);
+ nformats = 0;
+ if (tmp & AFMT_U8)
+ format[nformats++] = SND_PCM_FORMAT_U8;
+ if (tmp & AFMT_S16_LE)
+ format[nformats++] = SND_PCM_FORMAT_S16_LE;
+ if (tmp & AFMT_S16_BE)
+ format[nformats++] = SND_PCM_FORMAT_S16_BE;
+ if (tmp & AFMT_MU_LAW)
+ format[nformats++] = SND_PCM_FORMAT_MU_LAW;
+ if (! nformats)
+ format[nformats++] = SND_PCM_FORMAT_S16;
+ if ((err = snd_pcm_ioplug_set_param_list(io, SND_PCM_IOPLUG_HW_FORMAT,
+ nformats, format)) < 0)
+ return err;
+
+ /* supported channels */
+ nchannels = 0;
+ for (i = 0; i < 6; i++) {
+ tmp = i + 1;
+ if (ioctl(oss->fd, SNDCTL_DSP_CHANNELS, &tmp) >= 0)
+ channel[nchannels++] = tmp;
+ }
+ if (! nchannels) /* assume 2ch stereo */
+ err = snd_pcm_ioplug_set_param_minmax(io, SND_PCM_IOPLUG_HW_CHANNELS,
+ 2, 2);
+ else
+ err = snd_pcm_ioplug_set_param_list(io, SND_PCM_IOPLUG_HW_CHANNELS,
+ nchannels, channel);
+ if (err < 0)
+ return err;
+
+ /* supported rates */
+ /* FIXME: should query? */
+ err = snd_pcm_ioplug_set_param_minmax(io, SND_PCM_IOPLUG_HW_RATE, 8000, 480000);
+ if (err < 0)
+ return err;
+
+ /* period size (in power of two) */
+ err = snd_pcm_ioplug_set_param_list(io, SND_PCM_IOPLUG_HW_PERIOD_BYTES,
+ ARRAY_SIZE(bytes_list), bytes_list);
+ if (err < 0)
+ return err;
+ /* periods */
+ err = snd_pcm_ioplug_set_param_minmax(io, SND_PCM_IOPLUG_HW_PERIODS, 2, 1024);
+ if (err < 0)
+ return err;
+ /* buffer size (in power of two) */
+ err = snd_pcm_ioplug_set_param_list(io, SND_PCM_IOPLUG_HW_BUFFER_BYTES,
+ ARRAY_SIZE(bytes_list), bytes_list);
+ if (err < 0)
+ return err;
+
+ return 0;
+}
+
+
+static int oss_close(snd_pcm_ioplug_t *io)
+{
+ snd_pcm_oss_t *oss = io->private_data;
+
+ close(oss->fd);
+ free(oss->device);
+ free(oss);
+ return 0;
+}
+
+static snd_pcm_ioplug_callback_t oss_playback_callback = {
+ .start = oss_start,
+ .stop = oss_stop,
+ .transfer = oss_write,
+ .pointer = oss_pointer,
+ .close = oss_close,
+ .hw_params = oss_hw_params,
+ .prepare = oss_prepare,
+ .drain = oss_drain,
+};
+
+static snd_pcm_ioplug_callback_t oss_capture_callback = {
+ .start = oss_start,
+ .stop = oss_stop,
+ .transfer = oss_read,
+ .pointer = oss_pointer,
+ .close = oss_close,
+ .hw_params = oss_hw_params,
+ .prepare = oss_prepare,
+ .drain = oss_drain,
+};
+
+
+SND_PCM_PLUGIN_DEFINE_FUNC(oss)
+{
+ snd_config_iterator_t i, next;
+ const char *device = "/dev/dsp";
+ int err;
+ snd_pcm_oss_t *oss;
+
+ snd_config_for_each(i, next, conf) {
+ snd_config_t *n = snd_config_iterator_entry(i);
+ const char *id;
+ if (snd_config_get_id(n, &id) < 0)
+ continue;
+ if (strcmp(id, "comment") == 0 || strcmp(id, "type") == 0)
+ continue;
+ if (strcmp(id, "device") == 0) {
+ if (snd_config_get_string(n, &device) < 0) {
+ SNDERR("Invalid type for %s", id);
+ return -EINVAL;
+ }
+ continue;
+ }
+ SNDERR("Unknown field %s", id);
+ return -EINVAL;
+ }
+
+ oss = calloc(1, sizeof(*oss));
+ oss->device = strdup(device);
+ if (oss->device == NULL) {
+ SNDERR("cannot allocate");
+ free(oss);
+ return -ENOMEM;
+ }
+ oss->fd = open(device, stream == SND_PCM_STREAM_PLAYBACK ?
+ O_WRONLY : O_RDONLY);
+ if (oss->fd < 0) {
+ err = -errno;
+ SNDERR("Cannot open device %s", device);
+ goto error;
+ }
+
+ oss->io.version = SND_PCM_IOPLUG_VERSION;
+ oss->io.name = "ALSA <-> OSS PCM I/O Plugin";
+ oss->io.poll_fd = oss->fd;
+ oss->io.poll_events = stream == SND_PCM_STREAM_PLAYBACK ? POLLOUT : POLLIN;
+ oss->io.mmap_rw = 0;
+ oss->io.callback = stream == SND_PCM_STREAM_PLAYBACK ?
+ &oss_playback_callback : &oss_capture_callback;
+ oss->io.private_data = oss;
+
+ err = snd_pcm_ioplug_create(&oss->io, name, stream, mode);
+ if (err < 0)
+ goto error;
+
+ if ((err = oss_hw_constraint(oss)) < 0) {
+ snd_pcm_ioplug_delete(&oss->io);
+ return err;
+ }
+
+ *pcmp = oss->io.pcm;
+ return 0;
+
+ error:
+ if (oss->fd >= 0)
+ close(oss->fd);
+ free(oss->device);
+ free(oss);
+ return err;
+}
+
+SND_PCM_PLUGIN_SYMBOL(oss);