alsa-lib/src/pcm/pcm_hw.c

460 lines
11 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"
typedef struct {
2000-06-21 14:59:20 +00:00
snd_pcm_t *handle;
int fd;
} 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-06-21 14:59:20 +00:00
static int snd_pcm_hw_close(void *private)
2000-05-08 18:53:38 +00:00
{
2000-06-21 14:59:20 +00:00
snd_pcm_hw_t *hw = (snd_pcm_hw_t*) private;
int fd = hw->fd;
free(private);
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-06-21 14:59:20 +00:00
static int snd_pcm_hw_nonblock(void *private, int nonblock)
2000-05-08 18:53:38 +00:00
{
long flags;
2000-06-21 14:59:20 +00:00
snd_pcm_hw_t *hw = (snd_pcm_hw_t*) private;
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-06-21 14:59:20 +00:00
static int snd_pcm_hw_info(void *private, snd_pcm_info_t * info)
2000-05-08 18:53:38 +00:00
{
2000-06-21 14:59:20 +00:00
snd_pcm_hw_t *hw = (snd_pcm_hw_t*) private;
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;
}
static int snd_pcm_hw_params_info(void *private, snd_pcm_params_info_t * info)
{
snd_pcm_hw_t *hw = (snd_pcm_hw_t*) private;
int fd = hw->fd;
if (ioctl(fd, SND_PCM_IOCTL_PARAMS_INFO, info) < 0)
return -errno;
return 0;
}
2000-06-21 14:59:20 +00:00
static int snd_pcm_hw_params(void *private, snd_pcm_params_t * params)
2000-05-08 18:53:38 +00:00
{
2000-06-21 14:59:20 +00:00
snd_pcm_hw_t *hw = (snd_pcm_hw_t*) private;
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-06-21 14:59:20 +00:00
static int snd_pcm_hw_setup(void *private, snd_pcm_setup_t * setup)
2000-05-08 18:53:38 +00:00
{
2000-06-21 14:59:20 +00:00
snd_pcm_hw_t *hw = (snd_pcm_hw_t*) private;
int fd = hw->fd;
if (ioctl(fd, SND_PCM_IOCTL_SETUP, setup) < 0)
2000-05-08 18:53:38 +00:00
return -errno;
return 0;
}
2000-06-21 14:59:20 +00:00
static int snd_pcm_hw_channel_setup(void *private, snd_pcm_channel_setup_t * setup)
2000-05-08 18:53:38 +00:00
{
2000-06-21 14:59:20 +00:00
snd_pcm_hw_t *hw = (snd_pcm_hw_t*) private;
int fd = hw->fd;
if (ioctl(fd, SND_PCM_IOCTL_CHANNEL_SETUP, setup) < 0)
2000-05-08 18:53:38 +00:00
return -errno;
return 0;
}
2000-06-21 14:59:20 +00:00
static int snd_pcm_hw_status(void *private, snd_pcm_status_t * status)
2000-05-08 18:53:38 +00:00
{
2000-06-21 14:59:20 +00:00
snd_pcm_hw_t *hw = (snd_pcm_hw_t*) private;
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-06-21 14:59:20 +00:00
static ssize_t snd_pcm_hw_state(void *private)
2000-05-09 10:46:43 +00:00
{
2000-06-21 14:59:20 +00:00
snd_pcm_hw_t *hw = (snd_pcm_hw_t*) private;
int fd = hw->fd;
snd_pcm_status_t status;
if (ioctl(fd, SND_PCM_IOCTL_STATUS, status) < 0)
2000-05-09 10:46:43 +00:00
return -errno;
2000-06-05 11:38:02 +00:00
return status.state;
}
2000-06-21 14:59:20 +00:00
static ssize_t snd_pcm_hw_frame_io(void *private, int update UNUSED)
{
2000-06-21 14:59:20 +00:00
snd_pcm_hw_t *hw = (snd_pcm_hw_t*) private;
int fd = hw->fd;
ssize_t pos = ioctl(fd, SND_PCM_IOCTL_FRAME_IO);
if (pos < 0)
return -errno;
return pos;
2000-05-09 10:46:43 +00:00
}
2000-06-21 14:59:20 +00:00
static int snd_pcm_hw_prepare(void *private)
2000-05-08 18:53:38 +00:00
{
2000-06-21 14:59:20 +00:00
snd_pcm_hw_t *hw = (snd_pcm_hw_t*) private;
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-06-21 14:59:20 +00:00
static int snd_pcm_hw_go(void *private)
2000-05-08 18:53:38 +00:00
{
2000-06-21 14:59:20 +00:00
snd_pcm_hw_t *hw = (snd_pcm_hw_t*) private;
int fd = hw->fd;
if (ioctl(fd, SND_PCM_IOCTL_GO) < 0)
2000-05-08 18:53:38 +00:00
return -errno;
return 0;
}
2000-06-21 14:59:20 +00:00
static int snd_pcm_hw_drain(void *private)
2000-05-08 18:53:38 +00:00
{
2000-06-21 14:59:20 +00:00
snd_pcm_hw_t *hw = (snd_pcm_hw_t*) private;
int fd = hw->fd;
if (ioctl(fd, SND_PCM_IOCTL_DRAIN) < 0)
2000-05-08 18:53:38 +00:00
return -errno;
return 0;
}
2000-06-21 14:59:20 +00:00
static int snd_pcm_hw_flush(void *private)
2000-05-08 18:53:38 +00:00
{
2000-06-21 14:59:20 +00:00
snd_pcm_hw_t *hw = (snd_pcm_hw_t*) private;
int fd = hw->fd;
if (ioctl(fd, SND_PCM_IOCTL_FLUSH) < 0)
2000-05-08 18:53:38 +00:00
return -errno;
return 0;
}
2000-06-21 14:59:20 +00:00
static int snd_pcm_hw_pause(void *private, int enable)
2000-05-08 18:53:38 +00:00
{
2000-06-21 14:59:20 +00:00
snd_pcm_hw_t *hw = (snd_pcm_hw_t*) private;
int fd = hw->fd;
if (ioctl(fd, SND_PCM_IOCTL_PAUSE, &enable) < 0)
2000-05-08 18:53:38 +00:00
return -errno;
return 0;
}
2000-06-21 14:59:20 +00:00
static ssize_t snd_pcm_hw_frame_data(void *private, off_t offset)
2000-05-29 19:53:30 +00:00
{
ssize_t result;
2000-06-21 14:59:20 +00:00
snd_pcm_hw_t *hw = (snd_pcm_hw_t*) private;
snd_pcm_t *handle = hw->handle;
int fd = hw->fd;
if (handle->mmap_status && handle->mmap_control)
return snd_pcm_mmap_frame_data(handle, offset);
result = ioctl(fd, SND_PCM_IOCTL_FRAME_DATA, offset);
if (result < 0)
return -errno;
return result;
2000-05-29 19:53:30 +00:00
}
2000-07-01 14:17:25 +00:00
static ssize_t snd_pcm_hw_write(void *private, snd_timestamp_t tstamp, const void *buffer, size_t size)
2000-05-08 18:53:38 +00:00
{
ssize_t result;
2000-06-21 14:59:20 +00:00
snd_pcm_hw_t *hw = (snd_pcm_hw_t*) private;
int fd = hw->fd;
snd_xfer_t xfer;
2000-07-01 14:17:25 +00:00
xfer.tstamp = tstamp;
xfer.buf = (char*) buffer;
xfer.count = size;
result = ioctl(fd, SND_PCM_IOCTL_WRITE_FRAMES, &xfer);
2000-05-08 18:53:38 +00:00
if (result < 0)
return -errno;
return result;
}
2000-07-01 14:17:25 +00:00
static ssize_t snd_pcm_hw_writev(void *private, snd_timestamp_t tstamp, const struct iovec *vector, unsigned long count)
2000-05-08 18:53:38 +00:00
{
ssize_t result;
2000-06-21 14:59:20 +00:00
snd_pcm_hw_t *hw = (snd_pcm_hw_t*) private;
int fd = hw->fd;
snd_xferv_t xferv;
2000-07-01 14:17:25 +00:00
xferv.tstamp = tstamp;
xferv.vector = vector;
xferv.count = count;
result = ioctl(fd, SND_PCM_IOCTL_WRITEV_FRAMES, &xferv);
2000-05-08 18:53:38 +00:00
if (result < 0)
return -errno;
return result;
}
2000-07-01 14:17:25 +00:00
static ssize_t snd_pcm_hw_read(void *private, snd_timestamp_t tstamp, void *buffer, size_t size)
2000-05-08 18:53:38 +00:00
{
ssize_t result;
2000-06-21 14:59:20 +00:00
snd_pcm_hw_t *hw = (snd_pcm_hw_t*) private;
int fd = hw->fd;
snd_xfer_t xfer;
2000-07-01 14:17:25 +00:00
xfer.tstamp = tstamp;
xfer.buf = buffer;
xfer.count = size;
result = ioctl(fd, SND_PCM_IOCTL_READ_FRAMES, &xfer);
2000-05-08 18:53:38 +00:00
if (result < 0)
return -errno;
return result;
}
2000-07-01 14:17:25 +00:00
ssize_t snd_pcm_hw_readv(void *private, snd_timestamp_t tstamp, const struct iovec *vector, unsigned long count)
2000-05-08 18:53:38 +00:00
{
ssize_t result;
2000-06-21 14:59:20 +00:00
snd_pcm_hw_t *hw = (snd_pcm_hw_t*) private;
int fd = hw->fd;
snd_xferv_t xferv;
2000-07-01 14:17:25 +00:00
xferv.tstamp = tstamp;
xferv.vector = vector;
xferv.count = count;
result = ioctl(fd, SND_PCM_IOCTL_READV_FRAMES, &xferv);
2000-05-08 18:53:38 +00:00
if (result < 0)
return -errno;
return result;
}
2000-06-21 14:59:20 +00:00
static int snd_pcm_hw_mmap_status(void *private, snd_pcm_mmap_status_t **status)
2000-05-08 18:53:38 +00:00
{
void *ptr;
2000-06-21 14:59:20 +00:00
snd_pcm_hw_t *hw = (snd_pcm_hw_t*) private;
ptr = mmap(NULL, sizeof(snd_pcm_mmap_control_t), PROT_READ, MAP_FILE|MAP_SHARED,
2000-06-21 14:59:20 +00:00
hw->fd, SND_PCM_MMAP_OFFSET_STATUS);
if (ptr == MAP_FAILED || ptr == NULL)
2000-05-08 18:53:38 +00:00
return -errno;
*status = ptr;
return 0;
}
2000-06-21 14:59:20 +00:00
static int snd_pcm_hw_mmap_control(void *private, snd_pcm_mmap_control_t **control)
{
void *ptr;
2000-06-21 14:59:20 +00:00
snd_pcm_hw_t *hw = (snd_pcm_hw_t*) private;
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);
if (ptr == MAP_FAILED || ptr == NULL)
return -errno;
*control = ptr;
2000-05-08 18:53:38 +00:00
return 0;
}
2000-06-21 14:59:20 +00:00
static int snd_pcm_hw_mmap_data(void *private, void **buffer, size_t bsize)
2000-05-08 18:53:38 +00:00
{
int prot;
void *daddr;
2000-06-21 14:59:20 +00:00
snd_pcm_hw_t *hw = (snd_pcm_hw_t*) private;
prot = hw->handle->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,
2000-06-21 14:59:20 +00:00
hw->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;
}
2000-06-21 14:59:20 +00:00
static int snd_pcm_hw_munmap_status(void *private UNUSED, snd_pcm_mmap_status_t *status)
{
if (munmap(status, sizeof(*status)) < 0)
return -errno;
return 0;
}
2000-06-21 14:59:20 +00:00
static int snd_pcm_hw_munmap_control(void *private UNUSED, snd_pcm_mmap_control_t *control)
2000-05-08 18:53:38 +00:00
{
if (munmap(control, sizeof(*control)) < 0)
2000-05-08 18:53:38 +00:00
return -errno;
return 0;
}
2000-06-21 14:59:20 +00:00
static int snd_pcm_hw_munmap_data(void *private UNUSED, void *buffer, size_t bsize)
2000-05-08 18:53:38 +00:00
{
if (munmap(buffer, bsize) < 0)
return -errno;
return 0;
}
2000-06-21 14:59:20 +00:00
static int snd_pcm_hw_file_descriptor(void *private)
2000-05-08 18:53:38 +00:00
{
2000-06-21 14:59:20 +00:00
snd_pcm_hw_t *hw = (snd_pcm_hw_t*) private;
return hw->fd;
2000-05-08 18:53:38 +00:00
}
2000-06-21 14:59:20 +00:00
static int snd_pcm_hw_channels_mask(void *private UNUSED,
bitset_t *client_vmask UNUSED)
2000-05-08 18:53:38 +00:00
{
return 0;
}
struct snd_pcm_ops snd_pcm_hw_ops = {
2000-06-21 14:59:20 +00:00
close: snd_pcm_hw_close,
nonblock: snd_pcm_hw_nonblock,
2000-05-08 18:53:38 +00:00
info: snd_pcm_hw_info,
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-05-08 18:53:38 +00:00
channel_setup: snd_pcm_hw_channel_setup,
2000-06-21 14:59:20 +00:00
status: snd_pcm_hw_status,
frame_io: snd_pcm_hw_frame_io,
state: snd_pcm_hw_state,
prepare: snd_pcm_hw_prepare,
go: snd_pcm_hw_go,
drain: snd_pcm_hw_drain,
flush: snd_pcm_hw_flush,
pause: snd_pcm_hw_pause,
frame_data: snd_pcm_hw_frame_data,
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_status: snd_pcm_hw_mmap_status,
2000-05-08 18:53:38 +00:00
mmap_control: snd_pcm_hw_mmap_control,
mmap_data: snd_pcm_hw_mmap_data,
munmap_status: snd_pcm_hw_munmap_status,
2000-05-08 18:53:38 +00:00
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
};
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;
*handlep = 0;
if ((ret = snd_ctl_open(&ctl, card)) < 0)
return ret;
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;
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
handle = calloc(1, sizeof(snd_pcm_t));
if (!handle) {
ret = -ENOMEM;
goto __end;
2000-05-08 18:53:38 +00:00
}
2000-06-21 14:59:20 +00:00
hw = calloc(1, sizeof(snd_pcm_hw_t));
if (!handle) {
free(handle);
ret = -ENOMEM;
goto __end;
2000-05-08 18:53:38 +00:00
}
2000-06-21 14:59:20 +00:00
hw->handle = handle;
hw->fd = fd;
handle->type = SND_PCM_TYPE_HW;
handle->stream = stream;
handle->ops = &snd_pcm_hw_ops;
handle->op_arg = hw;
handle->mode = mode;
handle->private = hw;
*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-06-21 14:59:20 +00:00
int snd_pcm_hw_open(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
}