Blame src/timer/timer_hw.c

Packit 4a16fb
/*
Packit 4a16fb
 *  Timer Interface - main file
Packit 4a16fb
 *  Copyright (c) 1998-2001 by Jaroslav Kysela <perex@perex.cz>
Packit 4a16fb
 *
Packit 4a16fb
 *
Packit 4a16fb
 *   This library is free software; you can redistribute it and/or modify
Packit 4a16fb
 *   it under the terms of the GNU Lesser General Public License as
Packit 4a16fb
 *   published by the Free Software Foundation; either version 2.1 of
Packit 4a16fb
 *   the License, or (at your option) any later version.
Packit 4a16fb
 *
Packit 4a16fb
 *   This program is distributed in the hope that it will be useful,
Packit 4a16fb
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 4a16fb
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 4a16fb
 *   GNU Lesser General Public License for more details.
Packit 4a16fb
 *
Packit 4a16fb
 *   You should have received a copy of the GNU Lesser General Public
Packit 4a16fb
 *   License along with this library; if not, write to the Free Software
Packit 4a16fb
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
Packit 4a16fb
 *
Packit 4a16fb
 */
Packit 4a16fb
Packit 4a16fb
#include "timer_local.h"
Packit 4a16fb
Packit 4a16fb
#ifndef PIC
Packit 4a16fb
/* entry for static linking */
Packit 4a16fb
const char *_snd_module_timer_hw = "";
Packit 4a16fb
#endif
Packit 4a16fb
Packit 4a16fb
#define SNDRV_FILE_TIMER		ALSA_DEVICE_DIRECTORY "timer"
Packit 4a16fb
#define SNDRV_TIMER_VERSION_MAX	SNDRV_PROTOCOL_VERSION(2, 0, 5)
Packit 4a16fb
Packit 4a16fb
#define SNDRV_TIMER_IOCTL_STATUS_OLD	_IOW('T', 0x14, struct snd_timer_status)
Packit 4a16fb
Packit 4a16fb
enum {
Packit 4a16fb
	SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20),
Packit 4a16fb
	SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21),
Packit 4a16fb
	SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22),
Packit 4a16fb
	SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23),
Packit 4a16fb
};
Packit 4a16fb
Packit 4a16fb
static int snd_timer_hw_close(snd_timer_t *handle)
Packit 4a16fb
{
Packit 4a16fb
	snd_timer_t *tmr = handle;
Packit 4a16fb
	int res;
Packit 4a16fb
Packit 4a16fb
	if (!tmr)
Packit 4a16fb
		return -EINVAL;
Packit 4a16fb
	res = close(tmr->poll_fd) < 0 ? -errno : 0;
Packit 4a16fb
	return res;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_timer_hw_nonblock(snd_timer_t *timer, int nonblock)
Packit 4a16fb
{
Packit 4a16fb
	long flags;
Packit 4a16fb
	assert(timer);
Packit 4a16fb
	if ((flags = fcntl(timer->poll_fd, F_GETFL)) < 0)
Packit 4a16fb
		return -errno;
Packit 4a16fb
	if (nonblock)
Packit 4a16fb
		flags |= O_NONBLOCK;
Packit 4a16fb
	else
Packit 4a16fb
		flags &= ~O_NONBLOCK;
Packit 4a16fb
	if (fcntl(timer->poll_fd, F_SETFL, flags) < 0)
Packit 4a16fb
		return -errno;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_timer_hw_async(snd_timer_t *timer, int sig, pid_t pid)
Packit 4a16fb
{
Packit 4a16fb
	long flags;
Packit 4a16fb
	int fd;
Packit 4a16fb
Packit 4a16fb
	assert(timer);
Packit 4a16fb
	fd = timer->poll_fd;
Packit 4a16fb
	if ((flags = fcntl(fd, F_GETFL)) < 0) {
Packit 4a16fb
		SYSERR("F_GETFL failed");
Packit 4a16fb
		return -errno;
Packit 4a16fb
	}
Packit 4a16fb
	if (sig >= 0)
Packit 4a16fb
		flags |= O_ASYNC;
Packit 4a16fb
	else
Packit 4a16fb
		flags &= ~O_ASYNC;
Packit 4a16fb
	if (fcntl(fd, F_SETFL, flags) < 0) {
Packit 4a16fb
		SYSERR("F_SETFL for O_ASYNC failed");
Packit 4a16fb
		return -errno;
Packit 4a16fb
	}
Packit 4a16fb
	if (sig < 0)
Packit 4a16fb
		return 0;
Packit 4a16fb
#ifdef F_SETSIG
Packit 4a16fb
	if (fcntl(fd, F_SETSIG, (long)sig) < 0) {
Packit 4a16fb
		SYSERR("F_SETSIG failed");
Packit 4a16fb
		return -errno;
Packit 4a16fb
	}
Packit 4a16fb
#endif
Packit 4a16fb
	if (fcntl(fd, F_SETOWN, (long)pid) < 0) {
Packit 4a16fb
		SYSERR("F_SETOWN failed");
Packit 4a16fb
		return -errno;
Packit 4a16fb
	}
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_timer_hw_info(snd_timer_t *handle, snd_timer_info_t * info)
Packit 4a16fb
{
Packit 4a16fb
	snd_timer_t *tmr;
Packit 4a16fb
Packit 4a16fb
	tmr = handle;
Packit 4a16fb
	if (!tmr || !info)
Packit 4a16fb
		return -EINVAL;
Packit 4a16fb
	if (ioctl(tmr->poll_fd, SNDRV_TIMER_IOCTL_INFO, info) < 0)
Packit 4a16fb
		return -errno;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_timer_hw_params(snd_timer_t *handle, snd_timer_params_t * params)
Packit 4a16fb
{
Packit 4a16fb
	snd_timer_t *tmr;
Packit 4a16fb
Packit 4a16fb
	tmr = handle;
Packit 4a16fb
	if (!tmr || !params)
Packit 4a16fb
		return -EINVAL;
Packit 4a16fb
	if (ioctl(tmr->poll_fd, SNDRV_TIMER_IOCTL_PARAMS, params) < 0)
Packit 4a16fb
		return -errno;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_timer_hw_status(snd_timer_t *handle, snd_timer_status_t * status)
Packit 4a16fb
{
Packit 4a16fb
	snd_timer_t *tmr;
Packit 4a16fb
	int cmd;
Packit 4a16fb
Packit 4a16fb
	tmr = handle;
Packit 4a16fb
	if (!tmr || !status)
Packit 4a16fb
		return -EINVAL;
Packit 4a16fb
	if (tmr->version < SNDRV_PROTOCOL_VERSION(2, 0, 1))
Packit 4a16fb
		cmd = SNDRV_TIMER_IOCTL_STATUS_OLD;
Packit 4a16fb
	else
Packit 4a16fb
		cmd = SNDRV_TIMER_IOCTL_STATUS;
Packit 4a16fb
	if (ioctl(tmr->poll_fd, cmd, status) < 0)
Packit 4a16fb
		return -errno;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_timer_hw_start(snd_timer_t *handle)
Packit 4a16fb
{
Packit 4a16fb
	snd_timer_t *tmr;
Packit 4a16fb
	unsigned int cmd;
Packit 4a16fb
Packit 4a16fb
	tmr = handle;
Packit 4a16fb
	if (!tmr)
Packit 4a16fb
		return -EINVAL;
Packit 4a16fb
	if (tmr->version < SNDRV_PROTOCOL_VERSION(2, 0, 4))
Packit 4a16fb
		cmd = SNDRV_TIMER_IOCTL_START_OLD;
Packit 4a16fb
	else
Packit 4a16fb
		cmd = SNDRV_TIMER_IOCTL_START;
Packit 4a16fb
	if (ioctl(tmr->poll_fd, cmd) < 0)
Packit 4a16fb
		return -errno;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_timer_hw_stop(snd_timer_t *handle)
Packit 4a16fb
{
Packit 4a16fb
	snd_timer_t *tmr;
Packit 4a16fb
	unsigned int cmd;
Packit 4a16fb
Packit 4a16fb
	tmr = handle;
Packit 4a16fb
	if (!tmr)
Packit 4a16fb
		return -EINVAL;
Packit 4a16fb
	if (tmr->version < SNDRV_PROTOCOL_VERSION(2, 0, 4))
Packit 4a16fb
		cmd = SNDRV_TIMER_IOCTL_STOP_OLD;
Packit 4a16fb
	else
Packit 4a16fb
		cmd = SNDRV_TIMER_IOCTL_STOP;
Packit 4a16fb
	if (ioctl(tmr->poll_fd, cmd) < 0)
Packit 4a16fb
		return -errno;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_timer_hw_continue(snd_timer_t *handle)
Packit 4a16fb
{
Packit 4a16fb
	snd_timer_t *tmr;
Packit 4a16fb
	unsigned int cmd;
Packit 4a16fb
Packit 4a16fb
	tmr = handle;
Packit 4a16fb
	if (!tmr)
Packit 4a16fb
		return -EINVAL;
Packit 4a16fb
	if (tmr->version < SNDRV_PROTOCOL_VERSION(2, 0, 4))
Packit 4a16fb
		cmd = SNDRV_TIMER_IOCTL_CONTINUE_OLD;
Packit 4a16fb
	else
Packit 4a16fb
		cmd = SNDRV_TIMER_IOCTL_CONTINUE;
Packit 4a16fb
	if (ioctl(tmr->poll_fd, cmd) < 0)
Packit 4a16fb
		return -errno;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static ssize_t snd_timer_hw_read(snd_timer_t *handle, void *buffer, size_t size)
Packit 4a16fb
{
Packit 4a16fb
	snd_timer_t *tmr;
Packit 4a16fb
	ssize_t result;
Packit 4a16fb
Packit 4a16fb
	tmr = handle;
Packit 4a16fb
	if (!tmr || (!buffer && size > 0))
Packit 4a16fb
		return -EINVAL;
Packit 4a16fb
	result = read(tmr->poll_fd, buffer, size);
Packit 4a16fb
	if (result < 0)
Packit 4a16fb
		return -errno;
Packit 4a16fb
	return result;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static const snd_timer_ops_t snd_timer_hw_ops = {
Packit 4a16fb
	.close = snd_timer_hw_close,
Packit 4a16fb
	.nonblock = snd_timer_hw_nonblock,
Packit 4a16fb
	.async = snd_timer_hw_async,
Packit 4a16fb
	.info = snd_timer_hw_info,
Packit 4a16fb
	.params = snd_timer_hw_params,
Packit 4a16fb
	.status = snd_timer_hw_status,
Packit 4a16fb
	.rt_start = snd_timer_hw_start,
Packit 4a16fb
	.rt_stop = snd_timer_hw_stop,
Packit 4a16fb
	.rt_continue = snd_timer_hw_continue,
Packit 4a16fb
	.read = snd_timer_hw_read,
Packit 4a16fb
};
Packit 4a16fb
Packit 4a16fb
int snd_timer_hw_open(snd_timer_t **handle, const char *name, int dev_class, int dev_sclass, int card, int device, int subdevice, int mode)
Packit 4a16fb
{
Packit 4a16fb
	int fd, ver, tmode, ret;
Packit 4a16fb
	snd_timer_t *tmr;
Packit 4a16fb
	struct snd_timer_select sel;
Packit 4a16fb
Packit 4a16fb
	*handle = NULL;
Packit 4a16fb
Packit 4a16fb
	tmode = O_RDONLY;
Packit 4a16fb
	if (mode & SND_TIMER_OPEN_NONBLOCK)
Packit 4a16fb
		tmode |= O_NONBLOCK;	
Packit 4a16fb
	fd = snd_open_device(SNDRV_FILE_TIMER, tmode);
Packit 4a16fb
	if (fd < 0)
Packit 4a16fb
		return -errno;
Packit 4a16fb
	if (ioctl(fd, SNDRV_TIMER_IOCTL_PVERSION, &ver) < 0) {
Packit 4a16fb
		ret = -errno;
Packit 4a16fb
		close(fd);
Packit 4a16fb
		return ret;
Packit 4a16fb
	}
Packit 4a16fb
	if (SNDRV_PROTOCOL_INCOMPATIBLE(ver, SNDRV_TIMER_VERSION_MAX)) {
Packit 4a16fb
		close(fd);
Packit 4a16fb
		return -SND_ERROR_INCOMPATIBLE_VERSION;
Packit 4a16fb
	}
Packit 4a16fb
	if (mode & SND_TIMER_OPEN_TREAD) {
Packit 4a16fb
		int arg = 1;
Packit 4a16fb
		if (ver < SNDRV_PROTOCOL_VERSION(2, 0, 3)) {
Packit 4a16fb
			ret = -ENOTTY;
Packit 4a16fb
			goto __no_tread;
Packit 4a16fb
		}
Packit 4a16fb
		if (ioctl(fd, SNDRV_TIMER_IOCTL_TREAD, &arg) < 0) {
Packit 4a16fb
			ret = -errno;
Packit 4a16fb
		      __no_tread:
Packit 4a16fb
			close(fd);
Packit 4a16fb
			SNDMSG("extended read is not supported (SNDRV_TIMER_IOCTL_TREAD)");
Packit 4a16fb
			return ret;
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
	memset(&sel, 0, sizeof(sel));
Packit 4a16fb
	sel.id.dev_class = dev_class;
Packit 4a16fb
	sel.id.dev_sclass = dev_sclass;
Packit 4a16fb
	sel.id.card = card;
Packit 4a16fb
	sel.id.device = device;
Packit 4a16fb
	sel.id.subdevice = subdevice;
Packit 4a16fb
	if (ioctl(fd, SNDRV_TIMER_IOCTL_SELECT, &sel) < 0) {
Packit 4a16fb
		ret = -errno;
Packit 4a16fb
		close(fd);
Packit 4a16fb
		return ret;
Packit 4a16fb
	}
Packit 4a16fb
	tmr = (snd_timer_t *) calloc(1, sizeof(snd_timer_t));
Packit 4a16fb
	if (tmr == NULL) {
Packit 4a16fb
		close(fd);
Packit 4a16fb
		return -ENOMEM;
Packit 4a16fb
	}
Packit 4a16fb
	tmr->type = SND_TIMER_TYPE_HW;
Packit 4a16fb
	tmr->version = ver;
Packit 4a16fb
	tmr->mode = tmode;
Packit 4a16fb
	tmr->name = strdup(name);
Packit 4a16fb
	tmr->poll_fd = fd;
Packit 4a16fb
	tmr->ops = &snd_timer_hw_ops;
Packit 4a16fb
	INIT_LIST_HEAD(&tmr->async_handlers);
Packit 4a16fb
	*handle = tmr;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
int _snd_timer_hw_open(snd_timer_t **timer, char *name,
Packit 4a16fb
		       snd_config_t *root ATTRIBUTE_UNUSED,
Packit 4a16fb
		       snd_config_t *conf, int mode)
Packit 4a16fb
{
Packit 4a16fb
	snd_config_iterator_t i, next;
Packit 4a16fb
	long dev_class = SND_TIMER_CLASS_GLOBAL, dev_sclass = SND_TIMER_SCLASS_NONE;
Packit 4a16fb
	long card = 0, device = 0, subdevice = 0;
Packit 4a16fb
	const char *str;
Packit 4a16fb
	int err;
Packit 4a16fb
	snd_config_for_each(i, next, conf) {
Packit 4a16fb
		snd_config_t *n = snd_config_iterator_entry(i);
Packit 4a16fb
		const char *id;
Packit 4a16fb
		if (snd_config_get_id(n, &id) < 0)
Packit 4a16fb
			continue;
Packit 4a16fb
		if (_snd_conf_generic_id(id))
Packit 4a16fb
			continue;
Packit 4a16fb
		if (strcmp(id, "class") == 0) {
Packit 4a16fb
			err = snd_config_get_integer(n, &dev_class);
Packit 4a16fb
			if (err < 0)
Packit 4a16fb
				return err;
Packit 4a16fb
			continue;
Packit 4a16fb
		}
Packit 4a16fb
		if (strcmp(id, "sclass") == 0) {
Packit 4a16fb
			err = snd_config_get_integer(n, &dev_sclass);
Packit 4a16fb
			if (err < 0)
Packit 4a16fb
				return err;
Packit 4a16fb
			continue;
Packit 4a16fb
		}
Packit 4a16fb
		if (strcmp(id, "card") == 0) {
Packit 4a16fb
			err = snd_config_get_integer(n, &card;;
Packit 4a16fb
			if (err < 0) {
Packit 4a16fb
				err = snd_config_get_string(n, &str);
Packit 4a16fb
				if (err < 0)
Packit 4a16fb
					return -EINVAL;
Packit 4a16fb
				card = snd_card_get_index(str);
Packit 4a16fb
				if (card < 0)
Packit 4a16fb
					return card;
Packit 4a16fb
			}
Packit 4a16fb
			continue;
Packit 4a16fb
		}
Packit 4a16fb
		if (strcmp(id, "device") == 0) {
Packit 4a16fb
			err = snd_config_get_integer(n, &device);
Packit 4a16fb
			if (err < 0)
Packit 4a16fb
				return err;
Packit 4a16fb
			continue;
Packit 4a16fb
		}
Packit 4a16fb
		if (strcmp(id, "subdevice") == 0) {
Packit 4a16fb
			err = snd_config_get_integer(n, &subdevice);
Packit 4a16fb
			if (err < 0)
Packit 4a16fb
				return err;
Packit 4a16fb
			continue;
Packit 4a16fb
		}
Packit 4a16fb
		SNDERR("Unexpected field %s", id);
Packit 4a16fb
		return -EINVAL;
Packit 4a16fb
	}
Packit 4a16fb
	if (card < 0)
Packit 4a16fb
		return -EINVAL;
Packit 4a16fb
	return snd_timer_hw_open(timer, name, dev_class, dev_sclass, card, device, subdevice, mode);
Packit 4a16fb
}
Packit 4a16fb
SND_DLSYM_BUILD_VERSION(_snd_timer_hw_open, SND_TIMER_DLSYM_VERSION);