alsa-lib/src/pcm/pcm_hw.c

476 lines
12 KiB
C
Raw Normal View History

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"
#define SND_FILE_PCM_PLAYBACK "/dev/snd/pcmC%iD%ip"
#define SND_FILE_PCM_CAPTURE "/dev/snd/pcmC%iD%ic"
#define SND_PCM_VERSION_MAX SND_PROTOCOL_VERSION(2, 0, 0)
static int snd_pcm_hw_stream_close(snd_pcm_t *pcm, int stream)
2000-05-08 18:53:38 +00:00
{
snd_pcm_hw_t *hw = (snd_pcm_hw_t*) &pcm->private;
2000-05-29 19:53:30 +00:00
int fd = hw->stream[stream].fd;
if (fd >= 0)
if (close(fd))
2000-05-08 18:53:38 +00:00
return -errno;
return 0;
}
int snd_pcm_hw_stream_fd(snd_pcm_t *pcm, int stream)
2000-05-08 18:53:38 +00:00
{
snd_pcm_hw_t *hw;
if (!pcm)
return -EINVAL;
if (stream < 0 || stream > 1)
2000-05-08 18:53:38 +00:00
return -EINVAL;
hw = (snd_pcm_hw_t*) &pcm->private;
return hw->stream[stream].fd;
2000-05-08 18:53:38 +00:00
}
static int snd_pcm_hw_stream_nonblock(snd_pcm_t *pcm, int stream, int nonblock)
2000-05-08 18:53:38 +00:00
{
long flags;
snd_pcm_hw_t *hw = (snd_pcm_hw_t*) &pcm->private;
2000-05-29 19:53:30 +00:00
int fd = hw->stream[stream].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;
}
static int snd_pcm_hw_info(snd_pcm_t *pcm, snd_pcm_info_t * info)
{
int fd, stream;
2000-05-08 18:53:38 +00:00
snd_pcm_hw_t *hw = (snd_pcm_hw_t*) &pcm->private;
for (stream = 0; stream < 2; ++stream) {
fd = hw->stream[stream].fd;
2000-05-08 18:53:38 +00:00
if (fd >= 0)
break;
}
if (fd < 0)
return -EBADFD;
if (ioctl(fd, SND_PCM_IOCTL_INFO, info) < 0)
return -errno;
return 0;
}
static int snd_pcm_hw_stream_info(snd_pcm_t *pcm, snd_pcm_stream_info_t * info)
2000-05-08 18:53:38 +00:00
{
snd_pcm_hw_t *hw = (snd_pcm_hw_t*) &pcm->private;
2000-05-29 19:53:30 +00:00
int fd = hw->stream[info->stream].fd;
2000-05-08 18:53:38 +00:00
if (fd < 0)
return -EINVAL;
if (ioctl(fd, SND_PCM_IOCTL_STREAM_INFO, info) < 0)
2000-05-08 18:53:38 +00:00
return -errno;
return 0;
}
static int snd_pcm_hw_stream_params(snd_pcm_t *pcm, snd_pcm_stream_params_t * params)
2000-05-08 18:53:38 +00:00
{
snd_pcm_hw_t *hw = (snd_pcm_hw_t*) &pcm->private;
2000-05-29 19:53:30 +00:00
int fd = hw->stream[params->stream].fd;
if (ioctl(fd, SND_PCM_IOCTL_STREAM_PARAMS, params) < 0)
2000-05-08 18:53:38 +00:00
return -errno;
return 0;
}
static int snd_pcm_hw_stream_setup(snd_pcm_t *pcm, snd_pcm_stream_setup_t * setup)
2000-05-08 18:53:38 +00:00
{
snd_pcm_hw_t *hw = (snd_pcm_hw_t*) &pcm->private;
2000-05-29 19:53:30 +00:00
int fd = hw->stream[setup->stream].fd;
if (ioctl(fd, SND_PCM_IOCTL_STREAM_SETUP, setup) < 0)
2000-05-08 18:53:38 +00:00
return -errno;
return 0;
}
static int snd_pcm_hw_channel_setup(snd_pcm_t *pcm, int stream, snd_pcm_channel_setup_t * setup)
2000-05-08 18:53:38 +00:00
{
snd_pcm_hw_t *hw = (snd_pcm_hw_t*) &pcm->private;
2000-05-29 19:53:30 +00:00
int fd = hw->stream[stream].fd;
if (ioctl(fd, SND_PCM_IOCTL_CHANNEL_SETUP, setup) < 0)
2000-05-08 18:53:38 +00:00
return -errno;
return 0;
}
static int snd_pcm_hw_stream_status(snd_pcm_t *pcm, snd_pcm_stream_status_t * status)
2000-05-08 18:53:38 +00:00
{
snd_pcm_hw_t *hw = (snd_pcm_hw_t*) &pcm->private;
2000-05-29 19:53:30 +00:00
int fd = hw->stream[status->stream].fd;
if (ioctl(fd, SND_PCM_IOCTL_STREAM_STATUS, status) < 0)
2000-05-08 18:53:38 +00:00
return -errno;
return 0;
}
static int snd_pcm_hw_stream_update(snd_pcm_t *pcm, int stream)
2000-05-09 10:46:43 +00:00
{
snd_pcm_hw_t *hw = (snd_pcm_hw_t*) &pcm->private;
2000-05-29 19:53:30 +00:00
int fd = hw->stream[stream].fd;
if (ioctl(fd, SND_PCM_IOCTL_STREAM_UPDATE) < 0)
2000-05-09 10:46:43 +00:00
return -errno;
return 0;
}
static int snd_pcm_hw_stream_prepare(snd_pcm_t *pcm, int stream)
2000-05-08 18:53:38 +00:00
{
snd_pcm_hw_t *hw = (snd_pcm_hw_t*) &pcm->private;
2000-05-29 19:53:30 +00:00
int fd = hw->stream[stream].fd;
if (ioctl(fd, SND_PCM_IOCTL_STREAM_PREPARE) < 0)
2000-05-08 18:53:38 +00:00
return -errno;
return 0;
}
static int snd_pcm_hw_stream_go(snd_pcm_t *pcm, int stream)
2000-05-08 18:53:38 +00:00
{
snd_pcm_hw_t *hw = (snd_pcm_hw_t*) &pcm->private;
2000-05-29 19:53:30 +00:00
int fd = hw->stream[stream].fd;
if (ioctl(fd, SND_PCM_IOCTL_STREAM_GO) < 0)
2000-05-08 18:53:38 +00:00
return -errno;
return 0;
}
static int snd_pcm_hw_sync_go(snd_pcm_t *pcm, snd_pcm_sync_t *sync)
{
snd_pcm_hw_t *hw = (snd_pcm_hw_t*) &pcm->private;
2000-05-29 19:53:30 +00:00
int fd;
if (pcm->stream[SND_PCM_STREAM_PLAYBACK].open)
fd = hw->stream[SND_PCM_STREAM_PLAYBACK].fd;
2000-05-08 18:53:38 +00:00
else
fd = hw->stream[SND_PCM_STREAM_CAPTURE].fd;
2000-05-08 18:53:38 +00:00
if (ioctl(fd, SND_PCM_IOCTL_SYNC_GO, sync) < 0)
return -errno;
return 0;
}
static int snd_pcm_hw_stream_drain(snd_pcm_t *pcm, int stream)
2000-05-08 18:53:38 +00:00
{
snd_pcm_hw_t *hw = (snd_pcm_hw_t*) &pcm->private;
2000-05-29 19:53:30 +00:00
int fd = hw->stream[stream].fd;
if (ioctl(fd, SND_PCM_IOCTL_STREAM_DRAIN) < 0)
2000-05-08 18:53:38 +00:00
return -errno;
return 0;
}
static int snd_pcm_hw_stream_flush(snd_pcm_t *pcm, int stream)
2000-05-08 18:53:38 +00:00
{
snd_pcm_hw_t *hw = (snd_pcm_hw_t*) &pcm->private;
2000-05-29 19:53:30 +00:00
int fd = hw->stream[stream].fd;
if (ioctl(fd, SND_PCM_IOCTL_STREAM_FLUSH) < 0)
2000-05-08 18:53:38 +00:00
return -errno;
return 0;
}
static int snd_pcm_hw_stream_pause(snd_pcm_t *pcm, int stream, int enable)
2000-05-08 18:53:38 +00:00
{
snd_pcm_hw_t *hw = (snd_pcm_hw_t*) &pcm->private;
2000-05-29 19:53:30 +00:00
int fd = hw->stream[stream].fd;
if (ioctl(fd, SND_PCM_IOCTL_STREAM_PAUSE, &enable) < 0)
2000-05-08 18:53:38 +00:00
return -errno;
return 0;
}
2000-05-29 19:53:30 +00:00
static ssize_t snd_pcm_hw_stream_seek(snd_pcm_t *pcm, int stream, off_t offset)
{
snd_pcm_hw_t *hw = (snd_pcm_hw_t*) &pcm->private;
int fd = hw->stream[stream].fd;
return lseek(fd, offset, SEEK_CUR);
}
2000-05-08 18:53:38 +00:00
static ssize_t snd_pcm_hw_write(snd_pcm_t *pcm, const void *buffer, size_t size)
{
ssize_t result;
snd_pcm_hw_t *hw = (snd_pcm_hw_t*) &pcm->private;
2000-05-29 19:53:30 +00:00
int fd = hw->stream[SND_PCM_STREAM_PLAYBACK].fd;
2000-05-08 18:53:38 +00:00
result = write(fd, buffer, size);
if (result < 0)
return -errno;
return result;
}
static ssize_t snd_pcm_hw_writev(snd_pcm_t *pcm, const struct iovec *vector, unsigned long count)
{
ssize_t result;
snd_pcm_hw_t *hw = (snd_pcm_hw_t*) &pcm->private;
2000-05-29 19:53:30 +00:00
int fd = hw->stream[SND_PCM_STREAM_PLAYBACK].fd;
2000-05-08 18:53:38 +00:00
#if 0
result = writev(fd, vector, count);
#else
{
snd_v_args_t args;
args.vector = vector;
args.count = count;
result = ioctl(fd, SND_IOCTL_WRITEV, &args);
}
#endif
if (result < 0)
return -errno;
return result;
}
static ssize_t snd_pcm_hw_read(snd_pcm_t *pcm, void *buffer, size_t size)
{
ssize_t result;
snd_pcm_hw_t *hw = (snd_pcm_hw_t*) &pcm->private;
2000-05-29 19:53:30 +00:00
int fd = hw->stream[SND_PCM_STREAM_CAPTURE].fd;
2000-05-08 18:53:38 +00:00
result = read(fd, buffer, size);
if (result < 0)
return -errno;
return result;
}
ssize_t snd_pcm_hw_readv(snd_pcm_t *pcm, const struct iovec *vector, unsigned long count)
{
ssize_t result;
snd_pcm_hw_t *hw = (snd_pcm_hw_t*) &pcm->private;
2000-05-29 19:53:30 +00:00
int fd = hw->stream[SND_PCM_STREAM_CAPTURE].fd;
2000-05-08 18:53:38 +00:00
#if 0
result = readv(fd, vector, count);
#else
{
snd_v_args_t args;
args.vector = vector;
args.count = count;
result = ioctl(fd, SND_IOCTL_READV, &args);
}
#endif
if (result < 0)
return -errno;
return result;
}
static int snd_pcm_hw_mmap_control(snd_pcm_t *pcm, int stream, snd_pcm_mmap_control_t **control, size_t csize)
2000-05-08 18:53:38 +00:00
{
void *caddr;
snd_pcm_hw_t *hw = (snd_pcm_hw_t*) &pcm->private;
caddr = mmap(NULL, csize, PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED,
hw->stream[stream].fd, SND_PCM_MMAP_OFFSET_CONTROL);
2000-05-08 18:53:38 +00:00
if (caddr == MAP_FAILED || caddr == NULL)
return -errno;
*control = caddr;
return 0;
}
static int snd_pcm_hw_mmap_data(snd_pcm_t *pcm, int stream, void **buffer, size_t bsize)
2000-05-08 18:53:38 +00:00
{
int prot;
void *daddr;
snd_pcm_hw_t *hw = (snd_pcm_hw_t*) &pcm->private;
prot = stream == SND_PCM_STREAM_PLAYBACK ? PROT_WRITE : PROT_READ;
2000-05-08 18:53:38 +00:00
daddr = mmap(NULL, bsize, prot, MAP_FILE|MAP_SHARED,
hw->stream[stream].fd, SND_PCM_MMAP_OFFSET_DATA);
2000-05-08 18:53:38 +00:00
if (daddr == MAP_FAILED || daddr == NULL)
return -errno;
*buffer = daddr;
return 0;
}
static int snd_pcm_hw_munmap_control(snd_pcm_t *pcm UNUSED, int stream UNUSED, snd_pcm_mmap_control_t *control, size_t csize)
2000-05-08 18:53:38 +00:00
{
if (munmap(control, csize) < 0)
return -errno;
return 0;
}
static int snd_pcm_hw_munmap_data(snd_pcm_t *pcm UNUSED, int stream UNUSED, void *buffer, size_t bsize)
2000-05-08 18:53:38 +00:00
{
if (munmap(buffer, bsize) < 0)
return -errno;
return 0;
}
static int snd_pcm_hw_file_descriptor(snd_pcm_t *pcm, int stream)
2000-05-08 18:53:38 +00:00
{
snd_pcm_hw_t *hw = (snd_pcm_hw_t*) &pcm->private;
return hw->stream[stream].fd;
2000-05-08 18:53:38 +00:00
}
static int snd_pcm_hw_channels_mask(snd_pcm_t *pcm UNUSED, int stream UNUSED,
2000-05-08 18:53:38 +00:00
bitset_t *client_vmask UNUSED)
{
return 0;
}
struct snd_pcm_ops snd_pcm_hw_ops = {
stream_close: snd_pcm_hw_stream_close,
stream_nonblock: snd_pcm_hw_stream_nonblock,
2000-05-08 18:53:38 +00:00
info: snd_pcm_hw_info,
stream_info: snd_pcm_hw_stream_info,
stream_params: snd_pcm_hw_stream_params,
stream_setup: snd_pcm_hw_stream_setup,
2000-05-08 18:53:38 +00:00
channel_setup: snd_pcm_hw_channel_setup,
stream_status: snd_pcm_hw_stream_status,
stream_update: snd_pcm_hw_stream_update,
stream_prepare: snd_pcm_hw_stream_prepare,
stream_go: snd_pcm_hw_stream_go,
2000-05-08 18:53:38 +00:00
sync_go: snd_pcm_hw_sync_go,
stream_drain: snd_pcm_hw_stream_drain,
stream_flush: snd_pcm_hw_stream_flush,
stream_pause: snd_pcm_hw_stream_pause,
2000-05-29 19:53:30 +00:00
stream_seek: snd_pcm_hw_stream_seek,
2000-05-08 18:53:38 +00:00
write: snd_pcm_hw_write,
writev: snd_pcm_hw_writev,
read: snd_pcm_hw_read,
readv: snd_pcm_hw_readv,
mmap_control: snd_pcm_hw_mmap_control,
mmap_data: snd_pcm_hw_mmap_data,
munmap_control: snd_pcm_hw_munmap_control,
munmap_data: snd_pcm_hw_munmap_data,
file_descriptor: snd_pcm_hw_file_descriptor,
channels_mask: snd_pcm_hw_channels_mask,
2000-05-08 18:53:38 +00:00
};
static int snd_pcm_hw_open_stream(int card, int device, int stream, int subdevice, int fmode, snd_ctl_t *ctl, int *ver)
2000-05-08 18:53:38 +00:00
{
char filename[32];
char *filefmt;
int err, fd;
int attempt = 0;
snd_pcm_stream_info_t info;
switch (stream) {
case SND_PCM_STREAM_PLAYBACK:
2000-05-08 18:53:38 +00:00
filefmt = SND_FILE_PCM_PLAYBACK;
break;
case SND_PCM_STREAM_CAPTURE:
2000-05-08 18:53:38 +00:00
filefmt = SND_FILE_PCM_CAPTURE;
break;
default:
return -EINVAL;
}
if ((err = snd_ctl_pcm_stream_prefer_subdevice(ctl, device, stream, subdevice)) < 0)
2000-05-08 18:53:38 +00:00
return err;
sprintf(filename, filefmt, card, device);
__again:
if (attempt++ > 3) {
snd_ctl_close(ctl);
return -EBUSY;
}
if ((fd = open(filename, fmode)) < 0) {
err = -errno;
return err;
}
if (ioctl(fd, SND_PCM_IOCTL_PVERSION, ver) < 0) {
err = -errno;
close(fd);
return err;
}
if (SND_PROTOCOL_INCOMPATIBLE(*ver, SND_PCM_VERSION_MAX)) {
close(fd);
return -SND_ERROR_INCOMPATIBLE_VERSION;
}
if (subdevice >= 0) {
memset(&info, 0, sizeof(info));
if (ioctl(fd, SND_PCM_IOCTL_STREAM_INFO, &info) < 0) {
2000-05-08 18:53:38 +00:00
err = -errno;
close(fd);
return err;
}
if (info.subdevice != subdevice) {
close(fd);
goto __again;
}
}
return fd;
}
int snd_pcm_open_subdevice(snd_pcm_t **handle, int card, int device, int subdevice, int mode)
{
int fmode, ver, err;
snd_pcm_t *pcm;
snd_pcm_hw_t *hw;
snd_ctl_t *ctl;
int pfd = -1, cfd = -1;
if (!handle)
return -EFAULT;
*handle = NULL;
if (card < 0 || card >= SND_CARDS)
return -EINVAL;
if ((err = snd_ctl_open(&ctl, card)) < 0)
return err;
if (mode & SND_PCM_OPEN_PLAYBACK) {
fmode = O_RDWR;
if (mode & SND_PCM_NONBLOCK_PLAYBACK)
fmode |= O_NONBLOCK;
pfd = snd_pcm_hw_open_stream(card, device, SND_PCM_STREAM_PLAYBACK,
2000-05-08 18:53:38 +00:00
subdevice, fmode, ctl, &ver);
if (pfd < 0) {
snd_ctl_close(ctl);
return pfd;
}
}
if (mode & SND_PCM_OPEN_CAPTURE) {
fmode = O_RDWR;
if (mode & SND_PCM_NONBLOCK_CAPTURE)
fmode |= O_NONBLOCK;
cfd = snd_pcm_hw_open_stream(card, device, SND_PCM_STREAM_CAPTURE,
2000-05-08 18:53:38 +00:00
subdevice, fmode, ctl, &ver);
if (cfd < 0) {
if (pfd >= 0)
close(pfd);
snd_ctl_close(ctl);
return cfd;
}
}
snd_ctl_close(ctl);
if (pfd < 0 && cfd < 0)
return -EINVAL;
err = snd_pcm_abstract_open(handle, mode, SND_PCM_TYPE_HW, sizeof(snd_pcm_hw_t));
if (err < 0) {
if (pfd >= 0)
close(pfd);
if (cfd >= 0)
close(cfd);
return err;
}
pcm = *handle;
pcm->ops = &snd_pcm_hw_ops;
hw = (snd_pcm_hw_t*) &pcm->private;
hw->card = card;
hw->device = device;
hw->ver = ver;
hw->stream[SND_PCM_STREAM_PLAYBACK].fd = pfd;
hw->stream[SND_PCM_STREAM_CAPTURE].fd = cfd;
2000-05-08 18:53:38 +00:00
return 0;
}
int snd_pcm_open(snd_pcm_t **handle, int card, int device, int mode)
{
return snd_pcm_open_subdevice(handle, card, device, -1, mode);
}