2000-05-08 18:53:38 +00:00
|
|
|
/*
|
|
|
|
|
* PCM - Hardware
|
|
|
|
|
* Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org>
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* This library is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU Library General Public License as
|
|
|
|
|
* published by the Free Software Foundation; either version 2 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 Library General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
#include <sys/mman.h>
|
|
|
|
|
#include "pcm_local.h"
|
|
|
|
|
|
2000-06-01 21:58:25 +00:00
|
|
|
typedef struct {
|
|
|
|
|
int fd;
|
2000-07-17 15:33:29 +00:00
|
|
|
int card, device, subdevice;
|
2000-09-24 09:57:26 +00:00
|
|
|
int mmap_emulation;
|
2000-06-01 21:58:25 +00:00
|
|
|
} snd_pcm_hw_t;
|
|
|
|
|
|
2000-06-21 14:59:20 +00:00
|
|
|
#define SND_FILE_PCM_STREAM_PLAYBACK "/dev/snd/pcmC%iD%ip"
|
|
|
|
|
#define SND_FILE_PCM_STREAM_CAPTURE "/dev/snd/pcmC%iD%ic"
|
2000-05-08 18:53:38 +00:00
|
|
|
#define SND_PCM_VERSION_MAX SND_PROTOCOL_VERSION(2, 0, 0)
|
|
|
|
|
|
2000-09-11 15:49:10 +00:00
|
|
|
static int snd_pcm_hw_close(snd_pcm_t *pcm)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
2000-09-11 15:49:10 +00:00
|
|
|
snd_pcm_hw_t *hw = pcm->private;
|
2000-06-21 14:59:20 +00:00
|
|
|
int fd = hw->fd;
|
2000-09-11 15:49:10 +00:00
|
|
|
free(hw);
|
2000-05-29 19:53:30 +00:00
|
|
|
if (fd >= 0)
|
|
|
|
|
if (close(fd))
|
2000-05-08 18:53:38 +00:00
|
|
|
return -errno;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2000-09-11 15:49:10 +00:00
|
|
|
static int snd_pcm_hw_nonblock(snd_pcm_t *pcm, int nonblock)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
|
|
|
|
long flags;
|
2000-09-11 15:49:10 +00:00
|
|
|
snd_pcm_hw_t *hw = pcm->private;
|
2000-06-21 14:59:20 +00:00
|
|
|
int fd = hw->fd;
|
2000-05-08 18:53:38 +00:00
|
|
|
|
|
|
|
|
if ((flags = fcntl(fd, F_GETFL)) < 0)
|
|
|
|
|
return -errno;
|
|
|
|
|
if (nonblock)
|
|
|
|
|
flags |= O_NONBLOCK;
|
|
|
|
|
else
|
|
|
|
|
flags &= ~O_NONBLOCK;
|
|
|
|
|
if (fcntl(fd, F_SETFL, flags) < 0)
|
|
|
|
|
return -errno;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2000-09-11 15:49:10 +00:00
|
|
|
static int snd_pcm_hw_info(snd_pcm_t *pcm, snd_pcm_info_t * info)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
2000-09-11 15:49:10 +00:00
|
|
|
snd_pcm_hw_t *hw = pcm->private;
|
2000-06-21 14:59:20 +00:00
|
|
|
int fd = hw->fd;
|
2000-05-08 18:53:38 +00:00
|
|
|
if (ioctl(fd, SND_PCM_IOCTL_INFO, info) < 0)
|
|
|
|
|
return -errno;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2000-09-11 15:49:10 +00:00
|
|
|
static int snd_pcm_hw_params_info(snd_pcm_t *pcm, snd_pcm_params_info_t * info)
|
2000-07-01 10:38:29 +00:00
|
|
|
{
|
2000-09-11 15:49:10 +00:00
|
|
|
snd_pcm_hw_t *hw = pcm->private;
|
2000-07-01 10:38:29 +00:00
|
|
|
int fd = hw->fd;
|
|
|
|
|
if (ioctl(fd, SND_PCM_IOCTL_PARAMS_INFO, info) < 0)
|
|
|
|
|
return -errno;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2000-09-11 15:49:10 +00:00
|
|
|
static int snd_pcm_hw_params(snd_pcm_t *pcm, snd_pcm_params_t * params)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
2000-09-11 15:49:10 +00:00
|
|
|
snd_pcm_hw_t *hw = pcm->private;
|
2000-06-21 14:59:20 +00:00
|
|
|
int fd = hw->fd;
|
|
|
|
|
if (ioctl(fd, SND_PCM_IOCTL_PARAMS, params) < 0)
|
2000-05-08 18:53:38 +00:00
|
|
|
return -errno;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2000-09-11 15:49:10 +00:00
|
|
|
static int snd_pcm_hw_setup(snd_pcm_t *pcm, snd_pcm_setup_t * setup)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
2000-09-11 15:49:10 +00:00
|
|
|
snd_pcm_hw_t *hw = pcm->private;
|
2000-06-21 14:59:20 +00:00
|
|
|
int fd = hw->fd;
|
|
|
|
|
if (ioctl(fd, SND_PCM_IOCTL_SETUP, setup) < 0)
|
2000-05-08 18:53:38 +00:00
|
|
|
return -errno;
|
2000-09-24 09:57:26 +00:00
|
|
|
if (setup->mmap_shape == SND_PCM_MMAP_UNSPECIFIED) {
|
|
|
|
|
if (setup->xfer_mode == SND_PCM_XFER_INTERLEAVED)
|
|
|
|
|
setup->mmap_shape = SND_PCM_MMAP_INTERLEAVED;
|
|
|
|
|
else
|
|
|
|
|
setup->mmap_shape = SND_PCM_MMAP_NONINTERLEAVED;
|
|
|
|
|
hw->mmap_emulation = 1;
|
|
|
|
|
} else
|
|
|
|
|
hw->mmap_emulation = 0;
|
2000-05-08 18:53:38 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2000-09-11 15:49:10 +00:00
|
|
|
static int snd_pcm_hw_channel_info(snd_pcm_t *pcm, snd_pcm_channel_info_t * info)
|
2000-08-28 09:14:37 +00:00
|
|
|
{
|
2000-09-11 15:49:10 +00:00
|
|
|
snd_pcm_hw_t *hw = pcm->private;
|
2000-08-28 09:14:37 +00:00
|
|
|
int fd = hw->fd;
|
|
|
|
|
if (ioctl(fd, SND_PCM_IOCTL_CHANNEL_INFO, info) < 0)
|
|
|
|
|
return -errno;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2000-09-11 15:49:10 +00:00
|
|
|
static int snd_pcm_hw_channel_params(snd_pcm_t *pcm, snd_pcm_channel_params_t * params)
|
2000-08-28 09:14:37 +00:00
|
|
|
{
|
2000-09-11 15:49:10 +00:00
|
|
|
snd_pcm_hw_t *hw = pcm->private;
|
2000-08-28 09:14:37 +00:00
|
|
|
int fd = hw->fd;
|
|
|
|
|
if (ioctl(fd, SND_PCM_IOCTL_CHANNEL_PARAMS, params) < 0)
|
|
|
|
|
return -errno;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2000-09-11 15:49:10 +00:00
|
|
|
static int snd_pcm_hw_channel_setup(snd_pcm_t *pcm, snd_pcm_channel_setup_t * setup)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
2000-09-11 15:49:10 +00:00
|
|
|
snd_pcm_hw_t *hw = pcm->private;
|
2000-06-21 14:59:20 +00:00
|
|
|
int fd = hw->fd;
|
2000-05-27 16:52:17 +00:00
|
|
|
if (ioctl(fd, SND_PCM_IOCTL_CHANNEL_SETUP, setup) < 0)
|
2000-05-08 18:53:38 +00:00
|
|
|
return -errno;
|
2000-09-24 09:57:26 +00:00
|
|
|
if (hw->mmap_emulation) {
|
|
|
|
|
if (pcm->setup.mmap_shape == SND_PCM_MMAP_INTERLEAVED) {
|
2000-10-07 16:59:48 +00:00
|
|
|
setup->running_area.addr = pcm->mmap_data;
|
|
|
|
|
setup->running_area.first = setup->channel * pcm->bits_per_sample;
|
|
|
|
|
setup->running_area.step = pcm->bits_per_frame;
|
2000-09-24 09:57:26 +00:00
|
|
|
} else {
|
2000-10-07 16:59:48 +00:00
|
|
|
setup->running_area.addr = pcm->mmap_data + setup->channel * pcm->setup.buffer_size * pcm->bits_per_sample / 8;
|
|
|
|
|
setup->running_area.first = 0;
|
|
|
|
|
setup->running_area.step = pcm->bits_per_sample;
|
2000-09-24 09:57:26 +00:00
|
|
|
}
|
2000-10-07 16:59:48 +00:00
|
|
|
setup->stopped_area = setup->running_area;
|
|
|
|
|
} else {
|
|
|
|
|
setup->running_area.addr = (char *)pcm->mmap_data + (long)setup->running_area.addr;
|
|
|
|
|
setup->stopped_area.addr = setup->running_area.addr;
|
|
|
|
|
}
|
2000-05-08 18:53:38 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2000-09-11 15:49:10 +00:00
|
|
|
static int snd_pcm_hw_status(snd_pcm_t *pcm, snd_pcm_status_t * status)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
2000-09-11 15:49:10 +00:00
|
|
|
snd_pcm_hw_t *hw = pcm->private;
|
2000-06-21 14:59:20 +00:00
|
|
|
int fd = hw->fd;
|
|
|
|
|
if (ioctl(fd, SND_PCM_IOCTL_STATUS, status) < 0)
|
2000-05-08 18:53:38 +00:00
|
|
|
return -errno;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2000-09-11 15:49:10 +00:00
|
|
|
static int snd_pcm_hw_state(snd_pcm_t *pcm)
|
2000-05-09 10:46:43 +00:00
|
|
|
{
|
2000-10-05 10:26:07 +00:00
|
|
|
return pcm->mmap_status->state;
|
2000-06-01 21:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
2000-09-24 09:57:26 +00:00
|
|
|
static int snd_pcm_hw_delay(snd_pcm_t *pcm, ssize_t *delayp)
|
2000-06-01 21:58:25 +00:00
|
|
|
{
|
2000-09-11 15:49:10 +00:00
|
|
|
snd_pcm_hw_t *hw = pcm->private;
|
2000-06-21 14:59:20 +00:00
|
|
|
int fd = hw->fd;
|
2000-09-24 09:57:26 +00:00
|
|
|
if (ioctl(fd, SND_PCM_IOCTL_DELAY, delayp) < 0)
|
2000-06-01 21:58:25 +00:00
|
|
|
return -errno;
|
2000-09-24 09:57:26 +00:00
|
|
|
return 0;
|
2000-05-09 10:46:43 +00:00
|
|
|
}
|
|
|
|
|
|
2000-09-11 15:49:10 +00:00
|
|
|
static int snd_pcm_hw_prepare(snd_pcm_t *pcm)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
2000-09-11 15:49:10 +00:00
|
|
|
snd_pcm_hw_t *hw = pcm->private;
|
2000-06-21 14:59:20 +00:00
|
|
|
int fd = hw->fd;
|
|
|
|
|
if (ioctl(fd, SND_PCM_IOCTL_PREPARE) < 0)
|
2000-05-08 18:53:38 +00:00
|
|
|
return -errno;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2000-09-24 09:57:26 +00:00
|
|
|
static int snd_pcm_hw_start(snd_pcm_t *pcm)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
2000-09-11 15:49:10 +00:00
|
|
|
snd_pcm_hw_t *hw = pcm->private;
|
2000-06-21 14:59:20 +00:00
|
|
|
int fd = hw->fd;
|
2000-09-24 09:57:26 +00:00
|
|
|
if (ioctl(fd, SND_PCM_IOCTL_START) < 0)
|
2000-05-08 18:53:38 +00:00
|
|
|
return -errno;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2000-10-02 06:59:59 +00:00
|
|
|
static int snd_pcm_hw_drop(snd_pcm_t *pcm)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
2000-09-11 15:49:10 +00:00
|
|
|
snd_pcm_hw_t *hw = pcm->private;
|
2000-06-21 14:59:20 +00:00
|
|
|
int fd = hw->fd;
|
2000-10-02 06:59:59 +00:00
|
|
|
if (ioctl(fd, SND_PCM_IOCTL_DROP) < 0)
|
2000-05-08 18:53:38 +00:00
|
|
|
return -errno;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2000-09-29 20:49:18 +00:00
|
|
|
static int snd_pcm_hw_drain(snd_pcm_t *pcm)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
2000-09-11 15:49:10 +00:00
|
|
|
snd_pcm_hw_t *hw = pcm->private;
|
2000-06-21 14:59:20 +00:00
|
|
|
int fd = hw->fd;
|
2000-09-29 20:49:18 +00:00
|
|
|
if (ioctl(fd, SND_PCM_IOCTL_DRAIN) < 0)
|
2000-05-08 18:53:38 +00:00
|
|
|
return -errno;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2000-09-11 15:49:10 +00:00
|
|
|
static int snd_pcm_hw_pause(snd_pcm_t *pcm, int enable)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
2000-09-11 15:49:10 +00:00
|
|
|
snd_pcm_hw_t *hw = pcm->private;
|
2000-06-21 14:59:20 +00:00
|
|
|
int fd = hw->fd;
|
2000-07-24 08:19:34 +00:00
|
|
|
if (ioctl(fd, SND_PCM_IOCTL_PAUSE, enable) < 0)
|
2000-05-08 18:53:38 +00:00
|
|
|
return -errno;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2000-09-26 09:46:05 +00:00
|
|
|
static ssize_t snd_pcm_hw_rewind(snd_pcm_t *pcm, size_t frames)
|
2000-05-29 19:53:30 +00:00
|
|
|
{
|
2000-10-07 16:59:48 +00:00
|
|
|
ssize_t hw_avail;
|
2000-09-26 09:46:05 +00:00
|
|
|
if (pcm->setup.xrun_mode == SND_PCM_XRUN_ASAP) {
|
|
|
|
|
ssize_t d;
|
|
|
|
|
int err = snd_pcm_hw_delay(pcm, &d);
|
|
|
|
|
if (err < 0)
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2000-10-07 16:59:48 +00:00
|
|
|
hw_avail = snd_pcm_mmap_hw_avail(pcm);
|
|
|
|
|
if (hw_avail <= 0)
|
2000-09-26 09:46:05 +00:00
|
|
|
return 0;
|
2000-10-07 16:59:48 +00:00
|
|
|
if (frames > (size_t)hw_avail)
|
|
|
|
|
frames = hw_avail;
|
2000-09-26 09:46:05 +00:00
|
|
|
snd_pcm_mmap_appl_backward(pcm, frames);
|
|
|
|
|
return frames;
|
2000-05-29 19:53:30 +00:00
|
|
|
}
|
|
|
|
|
|
2000-09-24 09:57:26 +00:00
|
|
|
static ssize_t snd_pcm_hw_writei(snd_pcm_t *pcm, const void *buffer, size_t size)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
|
|
|
|
ssize_t result;
|
2000-09-11 15:49:10 +00:00
|
|
|
snd_pcm_hw_t *hw = pcm->private;
|
2000-06-21 14:59:20 +00:00
|
|
|
int fd = hw->fd;
|
2000-09-24 09:57:26 +00:00
|
|
|
snd_xferi_t xferi;
|
|
|
|
|
xferi.buf = (char*) buffer;
|
|
|
|
|
xferi.frames = size;
|
|
|
|
|
result = ioctl(fd, SND_PCM_IOCTL_WRITEI_FRAMES, &xferi);
|
2000-05-08 18:53:38 +00:00
|
|
|
if (result < 0)
|
|
|
|
|
return -errno;
|
2000-09-25 17:17:38 +00:00
|
|
|
return xferi.result;
|
2000-05-08 18:53:38 +00:00
|
|
|
}
|
|
|
|
|
|
2000-09-24 09:57:26 +00:00
|
|
|
static ssize_t snd_pcm_hw_writen(snd_pcm_t *pcm, void **bufs, size_t size)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
|
|
|
|
ssize_t result;
|
2000-09-11 15:49:10 +00:00
|
|
|
snd_pcm_hw_t *hw = pcm->private;
|
2000-06-21 14:59:20 +00:00
|
|
|
int fd = hw->fd;
|
2000-09-24 09:57:26 +00:00
|
|
|
snd_xfern_t xfern;
|
|
|
|
|
xfern.bufs = bufs;
|
|
|
|
|
xfern.frames = size;
|
|
|
|
|
result = ioctl(fd, SND_PCM_IOCTL_WRITEN_FRAMES, &xfern);
|
2000-05-08 18:53:38 +00:00
|
|
|
if (result < 0)
|
|
|
|
|
return -errno;
|
2000-09-25 17:17:38 +00:00
|
|
|
return xfern.result;
|
2000-05-08 18:53:38 +00:00
|
|
|
}
|
|
|
|
|
|
2000-09-24 09:57:26 +00:00
|
|
|
static ssize_t snd_pcm_hw_readi(snd_pcm_t *pcm, void *buffer, size_t size)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
|
|
|
|
ssize_t result;
|
2000-09-11 15:49:10 +00:00
|
|
|
snd_pcm_hw_t *hw = pcm->private;
|
2000-06-21 14:59:20 +00:00
|
|
|
int fd = hw->fd;
|
2000-09-24 09:57:26 +00:00
|
|
|
snd_xferi_t xferi;
|
|
|
|
|
xferi.buf = buffer;
|
|
|
|
|
xferi.frames = size;
|
|
|
|
|
result = ioctl(fd, SND_PCM_IOCTL_READI_FRAMES, &xferi);
|
2000-05-08 18:53:38 +00:00
|
|
|
if (result < 0)
|
|
|
|
|
return -errno;
|
2000-09-25 17:17:38 +00:00
|
|
|
return xferi.result;
|
2000-05-08 18:53:38 +00:00
|
|
|
}
|
|
|
|
|
|
2000-09-24 09:57:26 +00:00
|
|
|
ssize_t snd_pcm_hw_readn(snd_pcm_t *pcm, void **bufs, size_t size)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
|
|
|
|
ssize_t result;
|
2000-09-11 15:49:10 +00:00
|
|
|
snd_pcm_hw_t *hw = pcm->private;
|
2000-06-21 14:59:20 +00:00
|
|
|
int fd = hw->fd;
|
2000-09-24 09:57:26 +00:00
|
|
|
snd_xfern_t xfern;
|
|
|
|
|
xfern.bufs = bufs;
|
|
|
|
|
xfern.frames = size;
|
|
|
|
|
result = ioctl(fd, SND_PCM_IOCTL_READN_FRAMES, &xfern);
|
2000-05-08 18:53:38 +00:00
|
|
|
if (result < 0)
|
|
|
|
|
return -errno;
|
2000-09-25 17:17:38 +00:00
|
|
|
return xfern.result;
|
2000-05-08 18:53:38 +00:00
|
|
|
}
|
|
|
|
|
|
2000-09-24 09:57:26 +00:00
|
|
|
static int snd_pcm_hw_mmap_status(snd_pcm_t *pcm)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
2000-09-11 15:49:10 +00:00
|
|
|
snd_pcm_hw_t *hw = pcm->private;
|
2000-08-31 11:21:05 +00:00
|
|
|
void *ptr;
|
|
|
|
|
ptr = mmap(NULL, sizeof(snd_pcm_mmap_status_t), PROT_READ, MAP_FILE|MAP_SHARED,
|
2000-06-21 14:59:20 +00:00
|
|
|
hw->fd, SND_PCM_MMAP_OFFSET_STATUS);
|
2000-06-10 12:39:51 +00:00
|
|
|
if (ptr == MAP_FAILED || ptr == NULL)
|
2000-05-08 18:53:38 +00:00
|
|
|
return -errno;
|
2000-09-24 09:57:26 +00:00
|
|
|
pcm->mmap_status = ptr;
|
2000-06-10 12:39:51 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2000-09-24 09:57:26 +00:00
|
|
|
static int snd_pcm_hw_mmap_control(snd_pcm_t *pcm)
|
2000-06-10 12:39:51 +00:00
|
|
|
{
|
2000-09-11 15:49:10 +00:00
|
|
|
snd_pcm_hw_t *hw = pcm->private;
|
2000-08-31 11:21:05 +00:00
|
|
|
void *ptr;
|
2000-06-10 12:39:51 +00:00
|
|
|
ptr = mmap(NULL, sizeof(snd_pcm_mmap_control_t), PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED,
|
2000-06-21 14:59:20 +00:00
|
|
|
hw->fd, SND_PCM_MMAP_OFFSET_CONTROL);
|
2000-06-10 12:39:51 +00:00
|
|
|
if (ptr == MAP_FAILED || ptr == NULL)
|
|
|
|
|
return -errno;
|
2000-09-24 09:57:26 +00:00
|
|
|
pcm->mmap_control = ptr;
|
2000-05-08 18:53:38 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2000-09-24 09:57:26 +00:00
|
|
|
static int snd_pcm_hw_mmap_data(snd_pcm_t *pcm)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
2000-09-11 15:49:10 +00:00
|
|
|
snd_pcm_hw_t *hw = pcm->private;
|
2000-08-31 11:21:05 +00:00
|
|
|
void *ptr;
|
2000-09-24 09:57:26 +00:00
|
|
|
if (hw->mmap_emulation) {
|
|
|
|
|
ptr = malloc(snd_pcm_frames_to_bytes(pcm, pcm->setup.buffer_size));
|
|
|
|
|
if (!ptr)
|
|
|
|
|
return -ENOMEM;
|
|
|
|
|
} else {
|
|
|
|
|
int prot;
|
|
|
|
|
prot = PROT_WRITE | PROT_READ;
|
|
|
|
|
ptr = mmap(NULL, pcm->setup.mmap_bytes,
|
|
|
|
|
prot, MAP_FILE|MAP_SHARED,
|
|
|
|
|
hw->fd, SND_PCM_MMAP_OFFSET_DATA);
|
|
|
|
|
if (ptr == MAP_FAILED || ptr == NULL)
|
|
|
|
|
return -errno;
|
|
|
|
|
}
|
|
|
|
|
pcm->mmap_data = ptr;
|
2000-05-08 18:53:38 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2000-09-24 09:57:26 +00:00
|
|
|
static int snd_pcm_hw_munmap_status(snd_pcm_t *pcm)
|
2000-06-10 12:39:51 +00:00
|
|
|
{
|
2000-10-07 16:59:48 +00:00
|
|
|
if (munmap((void*)pcm->mmap_status, sizeof(*pcm->mmap_status)) < 0)
|
2000-06-10 12:39:51 +00:00
|
|
|
return -errno;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2000-09-24 09:57:26 +00:00
|
|
|
static int snd_pcm_hw_munmap_control(snd_pcm_t *pcm)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
2000-09-24 09:57:26 +00:00
|
|
|
if (munmap(pcm->mmap_control, sizeof(*pcm->mmap_control)) < 0)
|
2000-05-08 18:53:38 +00:00
|
|
|
return -errno;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2000-09-24 09:57:26 +00:00
|
|
|
static int snd_pcm_hw_munmap_data(snd_pcm_t *pcm)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
2000-09-11 15:49:10 +00:00
|
|
|
snd_pcm_hw_t *hw = pcm->private;
|
2000-09-24 09:57:26 +00:00
|
|
|
if (hw->mmap_emulation)
|
|
|
|
|
free(pcm->mmap_data);
|
|
|
|
|
else
|
|
|
|
|
if (munmap(pcm->mmap_data, pcm->setup.mmap_bytes) < 0)
|
|
|
|
|
return -errno;
|
2000-05-08 18:53:38 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2000-09-24 09:57:26 +00:00
|
|
|
static ssize_t snd_pcm_hw_mmap_forward(snd_pcm_t *pcm, size_t size)
|
|
|
|
|
{
|
|
|
|
|
snd_pcm_hw_t *hw = pcm->private;
|
|
|
|
|
if (hw->mmap_emulation && pcm->stream == SND_PCM_STREAM_PLAYBACK)
|
|
|
|
|
return snd_pcm_write_mmap(pcm, size);
|
|
|
|
|
snd_pcm_mmap_appl_forward(pcm, size);
|
|
|
|
|
return size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static ssize_t snd_pcm_hw_avail_update(snd_pcm_t *pcm)
|
|
|
|
|
{
|
|
|
|
|
snd_pcm_hw_t *hw = pcm->private;
|
|
|
|
|
int fd = hw->fd;
|
|
|
|
|
size_t avail;
|
|
|
|
|
ssize_t err;
|
2000-10-07 16:59:48 +00:00
|
|
|
if (pcm->setup.ready_mode == SND_PCM_READY_ASAP ||
|
|
|
|
|
pcm->setup.xrun_mode == SND_PCM_XRUN_ASAP) {
|
2000-09-24 09:57:26 +00:00
|
|
|
ssize_t d;
|
|
|
|
|
int err = ioctl(fd, SND_PCM_IOCTL_DELAY, &d);
|
|
|
|
|
if (err < 0)
|
|
|
|
|
return -errno;
|
|
|
|
|
}
|
2000-10-05 10:26:07 +00:00
|
|
|
if (pcm->stream == SND_PCM_STREAM_PLAYBACK) {
|
|
|
|
|
avail = snd_pcm_mmap_playback_avail(pcm);
|
|
|
|
|
} else {
|
|
|
|
|
avail = snd_pcm_mmap_capture_avail(pcm);
|
|
|
|
|
if (avail > 0 && hw->mmap_emulation) {
|
|
|
|
|
err = snd_pcm_read_mmap(pcm, avail);
|
|
|
|
|
if (err < 0)
|
|
|
|
|
return err;
|
|
|
|
|
assert((size_t)err == avail);
|
2000-09-24 09:57:26 +00:00
|
|
|
return err;
|
2000-10-05 10:26:07 +00:00
|
|
|
}
|
2000-09-24 09:57:26 +00:00
|
|
|
}
|
2000-10-07 16:59:48 +00:00
|
|
|
if (avail >= pcm->setup.buffer_size)
|
|
|
|
|
return -EPIPE;
|
2000-09-24 09:57:26 +00:00
|
|
|
return avail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int snd_pcm_hw_poll_descriptor(snd_pcm_t *pcm)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
2000-09-11 15:49:10 +00:00
|
|
|
snd_pcm_hw_t *hw = pcm->private;
|
2000-06-21 14:59:20 +00:00
|
|
|
return hw->fd;
|
2000-05-08 18:53:38 +00:00
|
|
|
}
|
|
|
|
|
|
2000-09-11 15:49:10 +00:00
|
|
|
static int snd_pcm_hw_channels_mask(snd_pcm_t *pcm ATTRIBUTE_UNUSED,
|
2000-09-24 09:57:26 +00:00
|
|
|
bitset_t *cmask ATTRIBUTE_UNUSED)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2000-09-11 15:49:10 +00:00
|
|
|
static void snd_pcm_hw_dump(snd_pcm_t *pcm, FILE *fp)
|
2000-07-17 15:33:29 +00:00
|
|
|
{
|
2000-09-11 15:49:10 +00:00
|
|
|
snd_pcm_hw_t *hw = pcm->private;
|
2000-07-17 15:33:29 +00:00
|
|
|
char *name = "Unknown";
|
|
|
|
|
snd_card_get_name(hw->card, &name);
|
|
|
|
|
fprintf(fp, "Hardware PCM card %d '%s' device %d subdevice %d\n",
|
|
|
|
|
hw->card, name, hw->device, hw->subdevice);
|
|
|
|
|
free(name);
|
2000-09-11 15:49:10 +00:00
|
|
|
if (pcm->valid_setup) {
|
2000-07-17 15:33:29 +00:00
|
|
|
fprintf(fp, "\nIts setup is:\n");
|
2000-09-11 15:49:10 +00:00
|
|
|
snd_pcm_dump_setup(pcm, fp);
|
2000-07-17 15:33:29 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2000-05-08 18:53:38 +00:00
|
|
|
struct snd_pcm_ops snd_pcm_hw_ops = {
|
2000-06-21 14:59:20 +00:00
|
|
|
close: snd_pcm_hw_close,
|
2000-05-08 18:53:38 +00:00
|
|
|
info: snd_pcm_hw_info,
|
2000-07-01 10:38:29 +00:00
|
|
|
params_info: snd_pcm_hw_params_info,
|
2000-06-21 14:59:20 +00:00
|
|
|
params: snd_pcm_hw_params,
|
|
|
|
|
setup: snd_pcm_hw_setup,
|
2000-09-24 09:57:26 +00:00
|
|
|
channel_info: snd_pcm_hw_channel_info,
|
|
|
|
|
channel_params: snd_pcm_hw_channel_params,
|
|
|
|
|
channel_setup: snd_pcm_hw_channel_setup,
|
2000-07-19 17:21:15 +00:00
|
|
|
dump: snd_pcm_hw_dump,
|
2000-09-24 09:57:26 +00:00
|
|
|
nonblock: snd_pcm_hw_nonblock,
|
|
|
|
|
mmap_status: snd_pcm_hw_mmap_status,
|
|
|
|
|
mmap_control: snd_pcm_hw_mmap_control,
|
|
|
|
|
mmap_data: snd_pcm_hw_mmap_data,
|
|
|
|
|
munmap_status: snd_pcm_hw_munmap_status,
|
|
|
|
|
munmap_control: snd_pcm_hw_munmap_control,
|
|
|
|
|
munmap_data: snd_pcm_hw_munmap_data,
|
2000-07-19 17:21:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct snd_pcm_fast_ops snd_pcm_hw_fast_ops = {
|
2000-06-21 14:59:20 +00:00
|
|
|
status: snd_pcm_hw_status,
|
|
|
|
|
state: snd_pcm_hw_state,
|
2000-09-24 09:57:26 +00:00
|
|
|
delay: snd_pcm_hw_delay,
|
2000-06-21 14:59:20 +00:00
|
|
|
prepare: snd_pcm_hw_prepare,
|
2000-09-24 09:57:26 +00:00
|
|
|
start: snd_pcm_hw_start,
|
2000-10-02 06:59:59 +00:00
|
|
|
drop: snd_pcm_hw_drop,
|
2000-09-29 20:49:18 +00:00
|
|
|
drain: snd_pcm_hw_drain,
|
2000-06-21 14:59:20 +00:00
|
|
|
pause: snd_pcm_hw_pause,
|
2000-09-26 09:46:05 +00:00
|
|
|
rewind: snd_pcm_hw_rewind,
|
2000-09-24 09:57:26 +00:00
|
|
|
writei: snd_pcm_hw_writei,
|
|
|
|
|
writen: snd_pcm_hw_writen,
|
|
|
|
|
readi: snd_pcm_hw_readi,
|
|
|
|
|
readn: snd_pcm_hw_readn,
|
|
|
|
|
poll_descriptor: snd_pcm_hw_poll_descriptor,
|
2000-05-27 16:52:17 +00:00
|
|
|
channels_mask: snd_pcm_hw_channels_mask,
|
2000-09-24 09:57:26 +00:00
|
|
|
avail_update: snd_pcm_hw_avail_update,
|
|
|
|
|
mmap_forward: snd_pcm_hw_mmap_forward,
|
2000-05-08 18:53:38 +00:00
|
|
|
};
|
|
|
|
|
|
2000-06-21 14:59:20 +00:00
|
|
|
int snd_pcm_hw_open_subdevice(snd_pcm_t **handlep, int card, int device, int subdevice, int stream, int mode)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
|
|
|
|
char filename[32];
|
|
|
|
|
char *filefmt;
|
2000-06-21 14:59:20 +00:00
|
|
|
int ver;
|
|
|
|
|
int ret = 0, fd = -1;
|
2000-05-08 18:53:38 +00:00
|
|
|
int attempt = 0;
|
2000-06-21 14:59:20 +00:00
|
|
|
snd_pcm_info_t info;
|
|
|
|
|
int fmode;
|
|
|
|
|
snd_ctl_t *ctl;
|
|
|
|
|
snd_pcm_t *handle;
|
|
|
|
|
snd_pcm_hw_t *hw;
|
|
|
|
|
|
2000-08-24 17:07:44 +00:00
|
|
|
assert(handlep);
|
2000-06-21 14:59:20 +00:00
|
|
|
|
2000-09-11 15:49:10 +00:00
|
|
|
if ((ret = snd_ctl_hw_open(&ctl, card)) < 0)
|
2000-06-21 14:59:20 +00:00
|
|
|
return ret;
|
|
|
|
|
|
2000-05-27 16:52:17 +00:00
|
|
|
switch (stream) {
|
|
|
|
|
case SND_PCM_STREAM_PLAYBACK:
|
2000-06-21 14:59:20 +00:00
|
|
|
filefmt = SND_FILE_PCM_STREAM_PLAYBACK;
|
2000-05-08 18:53:38 +00:00
|
|
|
break;
|
2000-05-27 16:52:17 +00:00
|
|
|
case SND_PCM_STREAM_CAPTURE:
|
2000-06-21 14:59:20 +00:00
|
|
|
filefmt = SND_FILE_PCM_STREAM_CAPTURE;
|
2000-05-08 18:53:38 +00:00
|
|
|
break;
|
|
|
|
|
default:
|
2000-06-04 16:24:04 +00:00
|
|
|
assert(0);
|
2000-05-08 18:53:38 +00:00
|
|
|
}
|
2000-06-21 14:59:20 +00:00
|
|
|
if ((ret = snd_ctl_pcm_prefer_subdevice(ctl, subdevice)) < 0)
|
|
|
|
|
goto __end;
|
2000-05-08 18:53:38 +00:00
|
|
|
sprintf(filename, filefmt, card, device);
|
|
|
|
|
|
|
|
|
|
__again:
|
|
|
|
|
if (attempt++ > 3) {
|
2000-06-21 14:59:20 +00:00
|
|
|
ret = -EBUSY;
|
|
|
|
|
goto __end;
|
2000-05-08 18:53:38 +00:00
|
|
|
}
|
2000-06-21 14:59:20 +00:00
|
|
|
fmode = O_RDWR;
|
|
|
|
|
if (mode & SND_PCM_NONBLOCK)
|
|
|
|
|
fmode |= O_NONBLOCK;
|
2000-05-08 18:53:38 +00:00
|
|
|
if ((fd = open(filename, fmode)) < 0) {
|
2000-06-21 14:59:20 +00:00
|
|
|
ret = -errno;
|
|
|
|
|
goto __end;
|
2000-05-08 18:53:38 +00:00
|
|
|
}
|
2000-06-21 14:59:20 +00:00
|
|
|
if (ioctl(fd, SND_PCM_IOCTL_PVERSION, &ver) < 0) {
|
|
|
|
|
ret = -errno;
|
|
|
|
|
goto __end;
|
2000-05-08 18:53:38 +00:00
|
|
|
}
|
2000-06-21 14:59:20 +00:00
|
|
|
if (SND_PROTOCOL_INCOMPATIBLE(ver, SND_PCM_VERSION_MAX)) {
|
|
|
|
|
ret = -SND_ERROR_INCOMPATIBLE_VERSION;
|
|
|
|
|
goto __end;
|
2000-05-08 18:53:38 +00:00
|
|
|
}
|
|
|
|
|
if (subdevice >= 0) {
|
|
|
|
|
memset(&info, 0, sizeof(info));
|
2000-06-21 14:59:20 +00:00
|
|
|
if (ioctl(fd, SND_PCM_IOCTL_INFO, &info) < 0) {
|
|
|
|
|
ret = -errno;
|
|
|
|
|
goto __end;
|
2000-05-08 18:53:38 +00:00
|
|
|
}
|
|
|
|
|
if (info.subdevice != subdevice) {
|
|
|
|
|
close(fd);
|
|
|
|
|
goto __again;
|
|
|
|
|
}
|
|
|
|
|
}
|
2000-06-21 14:59:20 +00:00
|
|
|
hw = calloc(1, sizeof(snd_pcm_hw_t));
|
2000-09-24 09:57:26 +00:00
|
|
|
if (!hw) {
|
2000-06-21 14:59:20 +00:00
|
|
|
ret = -ENOMEM;
|
|
|
|
|
goto __end;
|
2000-05-08 18:53:38 +00:00
|
|
|
}
|
2000-07-17 15:33:29 +00:00
|
|
|
hw->card = card;
|
|
|
|
|
hw->device = device;
|
|
|
|
|
hw->subdevice = subdevice;
|
2000-06-21 14:59:20 +00:00
|
|
|
hw->fd = fd;
|
2000-09-24 09:57:26 +00:00
|
|
|
|
|
|
|
|
handle = calloc(1, sizeof(snd_pcm_t));
|
|
|
|
|
if (!handle) {
|
|
|
|
|
free(hw);
|
|
|
|
|
ret = -ENOMEM;
|
|
|
|
|
goto __end;
|
|
|
|
|
}
|
2000-06-21 14:59:20 +00:00
|
|
|
handle->type = SND_PCM_TYPE_HW;
|
|
|
|
|
handle->stream = stream;
|
|
|
|
|
handle->ops = &snd_pcm_hw_ops;
|
2000-09-11 15:49:10 +00:00
|
|
|
handle->op_arg = handle;
|
2000-07-19 17:21:15 +00:00
|
|
|
handle->fast_ops = &snd_pcm_hw_fast_ops;
|
2000-09-11 15:49:10 +00:00
|
|
|
handle->fast_op_arg = handle;
|
2000-06-21 14:59:20 +00:00
|
|
|
handle->mode = mode;
|
|
|
|
|
handle->private = hw;
|
2000-09-24 09:57:26 +00:00
|
|
|
ret = snd_pcm_init(handle);
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
snd_pcm_close(handle);
|
|
|
|
|
snd_ctl_close(ctl);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2000-06-21 14:59:20 +00:00
|
|
|
*handlep = handle;
|
|
|
|
|
|
|
|
|
|
__end:
|
|
|
|
|
if (ret < 0 && fd >= 0)
|
|
|
|
|
close(fd);
|
2000-05-08 18:53:38 +00:00
|
|
|
snd_ctl_close(ctl);
|
2000-06-21 14:59:20 +00:00
|
|
|
return ret;
|
2000-05-08 18:53:38 +00:00
|
|
|
}
|
|
|
|
|
|
2000-09-24 09:57:26 +00:00
|
|
|
int snd_pcm_hw_open_device(snd_pcm_t **handlep, int card, int device, int stream, int mode)
|
2000-05-08 18:53:38 +00:00
|
|
|
{
|
2000-06-21 14:59:20 +00:00
|
|
|
return snd_pcm_hw_open_subdevice(handlep, card, device, -1, stream, mode);
|
2000-05-08 18:53:38 +00:00
|
|
|
}
|
|
|
|
|
|
2000-09-24 09:57:26 +00:00
|
|
|
int _snd_pcm_hw_open(snd_pcm_t **pcmp, char *name, snd_config_t *conf,
|
|
|
|
|
int stream, int mode)
|
|
|
|
|
{
|
|
|
|
|
snd_config_iterator_t i;
|
|
|
|
|
long card = -1, device = 0, subdevice = -1;
|
|
|
|
|
char *str;
|
|
|
|
|
int err;
|
|
|
|
|
snd_config_foreach(i, conf) {
|
|
|
|
|
snd_config_t *n = snd_config_entry(i);
|
|
|
|
|
if (strcmp(n->id, "comment") == 0)
|
|
|
|
|
continue;
|
|
|
|
|
if (strcmp(n->id, "type") == 0)
|
|
|
|
|
continue;
|
|
|
|
|
if (strcmp(n->id, "stream") == 0)
|
|
|
|
|
continue;
|
|
|
|
|
if (strcmp(n->id, "card") == 0) {
|
|
|
|
|
err = snd_config_integer_get(n, &card);
|
|
|
|
|
if (err < 0) {
|
|
|
|
|
err = snd_config_string_get(n, &str);
|
|
|
|
|
if (err < 0)
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
card = snd_card_get_index(str);
|
|
|
|
|
if (card < 0)
|
|
|
|
|
return card;
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (strcmp(n->id, "device") == 0) {
|
|
|
|
|
err = snd_config_integer_get(n, &device);
|
|
|
|
|
if (err < 0)
|
|
|
|
|
return err;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (strcmp(n->id, "subdevice") == 0) {
|
|
|
|
|
err = snd_config_integer_get(n, &subdevice);
|
|
|
|
|
if (err < 0)
|
|
|
|
|
return err;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
|
|
|
|
if (card < 0)
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
return snd_pcm_hw_open_subdevice(pcmp, card, device, subdevice, stream, mode);
|
|
|
|
|
}
|
|
|
|
|
|