Blame src/rawmidi/rawmidi_hw.c

Packit 4a16fb
/*
Packit 4a16fb
 *  RawMIDI - Hardware
Packit 4a16fb
 *  Copyright (c) 2000 by Jaroslav Kysela <perex@perex.cz>
Packit 4a16fb
 *                        Abramo Bagnara <abramo@alsa-project.org>
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 <stdio.h>
Packit 4a16fb
#include <stdlib.h>
Packit 4a16fb
#include <unistd.h>
Packit 4a16fb
#include <string.h>
Packit 4a16fb
#include <fcntl.h>
Packit 4a16fb
#include <sys/ioctl.h>
Packit 4a16fb
#include "../control/control_local.h"
Packit 4a16fb
#include "rawmidi_local.h"
Packit 4a16fb
Packit 4a16fb
#ifndef PIC
Packit 4a16fb
/* entry for static linking */
Packit 4a16fb
const char *_snd_module_rawmidi_hw = "";
Packit 4a16fb
#endif
Packit 4a16fb
Packit 4a16fb
#define SNDRV_FILE_RAWMIDI		ALSA_DEVICE_DIRECTORY "midiC%iD%i"
Packit 4a16fb
#define SNDRV_RAWMIDI_VERSION_MAX	SNDRV_PROTOCOL_VERSION(2, 0, 0)
Packit 4a16fb
Packit 4a16fb
#ifndef DOC_HIDDEN
Packit 4a16fb
typedef struct {
Packit 4a16fb
	int open;
Packit 4a16fb
	int fd;
Packit 4a16fb
	int card, device, subdevice;
Packit 4a16fb
} snd_rawmidi_hw_t;
Packit 4a16fb
#endif
Packit 4a16fb
Packit 4a16fb
static int snd_rawmidi_hw_close(snd_rawmidi_t *rmidi)
Packit 4a16fb
{
Packit 4a16fb
	snd_rawmidi_hw_t *hw = rmidi->private_data;
Packit 4a16fb
	int err = 0;
Packit 4a16fb
Packit 4a16fb
	hw->open--;
Packit 4a16fb
	if (hw->open)
Packit 4a16fb
		return 0;
Packit 4a16fb
	if (close(hw->fd)) {
Packit 4a16fb
		err = -errno;
Packit 4a16fb
		SYSERR("close failed\n");
Packit 4a16fb
	}
Packit 4a16fb
	free(hw);
Packit 4a16fb
	return err;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_rawmidi_hw_nonblock(snd_rawmidi_t *rmidi, int nonblock)
Packit 4a16fb
{
Packit 4a16fb
	snd_rawmidi_hw_t *hw = rmidi->private_data;
Packit 4a16fb
	long flags;
Packit 4a16fb
Packit 4a16fb
	if ((flags = fcntl(hw->fd, F_GETFL)) < 0) {
Packit 4a16fb
		SYSERR("F_GETFL failed");
Packit 4a16fb
		return -errno;
Packit 4a16fb
	}
Packit 4a16fb
	if (nonblock)
Packit 4a16fb
		flags |= O_NONBLOCK;
Packit 4a16fb
	else
Packit 4a16fb
		flags &= ~O_NONBLOCK;
Packit 4a16fb
	if (fcntl(hw->fd, F_SETFL, flags) < 0) {
Packit 4a16fb
		SYSERR("F_SETFL for O_NONBLOCK failed");
Packit 4a16fb
		return -errno;
Packit 4a16fb
	}
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_rawmidi_hw_info(snd_rawmidi_t *rmidi, snd_rawmidi_info_t * info)
Packit 4a16fb
{
Packit 4a16fb
	snd_rawmidi_hw_t *hw = rmidi->private_data;
Packit 4a16fb
	info->stream = rmidi->stream;
Packit 4a16fb
	if (ioctl(hw->fd, SNDRV_RAWMIDI_IOCTL_INFO, info) < 0) {
Packit 4a16fb
		SYSERR("SNDRV_RAWMIDI_IOCTL_INFO failed");
Packit 4a16fb
		return -errno;
Packit 4a16fb
	}
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_rawmidi_hw_params(snd_rawmidi_t *rmidi, snd_rawmidi_params_t * params)
Packit 4a16fb
{
Packit 4a16fb
	snd_rawmidi_hw_t *hw = rmidi->private_data;
Packit 4a16fb
	params->stream = rmidi->stream;
Packit 4a16fb
	if (ioctl(hw->fd, SNDRV_RAWMIDI_IOCTL_PARAMS, params) < 0) {
Packit 4a16fb
		SYSERR("SNDRV_RAWMIDI_IOCTL_PARAMS failed");
Packit 4a16fb
		return -errno;
Packit 4a16fb
	}
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_rawmidi_hw_status(snd_rawmidi_t *rmidi, snd_rawmidi_status_t * status)
Packit 4a16fb
{
Packit 4a16fb
	snd_rawmidi_hw_t *hw = rmidi->private_data;
Packit 4a16fb
	status->stream = rmidi->stream;
Packit 4a16fb
	if (ioctl(hw->fd, SNDRV_RAWMIDI_IOCTL_STATUS, status) < 0) {
Packit 4a16fb
		SYSERR("SNDRV_RAWMIDI_IOCTL_STATUS failed");
Packit 4a16fb
		return -errno;
Packit 4a16fb
	}
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_rawmidi_hw_drop(snd_rawmidi_t *rmidi)
Packit 4a16fb
{
Packit 4a16fb
	snd_rawmidi_hw_t *hw = rmidi->private_data;
Packit 4a16fb
	int str = rmidi->stream;
Packit 4a16fb
	if (ioctl(hw->fd, SNDRV_RAWMIDI_IOCTL_DROP, &str) < 0) {
Packit 4a16fb
		SYSERR("SNDRV_RAWMIDI_IOCTL_DROP failed");
Packit 4a16fb
		return -errno;
Packit 4a16fb
	}
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_rawmidi_hw_drain(snd_rawmidi_t *rmidi)
Packit 4a16fb
{
Packit 4a16fb
	snd_rawmidi_hw_t *hw = rmidi->private_data;
Packit 4a16fb
	int str = rmidi->stream;
Packit 4a16fb
	if (ioctl(hw->fd, SNDRV_RAWMIDI_IOCTL_DRAIN, &str) < 0) {
Packit 4a16fb
		SYSERR("SNDRV_RAWMIDI_IOCTL_DRAIN failed");
Packit 4a16fb
		return -errno;
Packit 4a16fb
	}
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static ssize_t snd_rawmidi_hw_write(snd_rawmidi_t *rmidi, const void *buffer, size_t size)
Packit 4a16fb
{
Packit 4a16fb
	snd_rawmidi_hw_t *hw = rmidi->private_data;
Packit 4a16fb
	ssize_t result;
Packit 4a16fb
	result = write(hw->fd, buffer, size);
Packit 4a16fb
	if (result < 0)
Packit 4a16fb
		return -errno;
Packit 4a16fb
	return result;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static ssize_t snd_rawmidi_hw_read(snd_rawmidi_t *rmidi, void *buffer, size_t size)
Packit 4a16fb
{
Packit 4a16fb
	snd_rawmidi_hw_t *hw = rmidi->private_data;
Packit 4a16fb
	ssize_t result;
Packit 4a16fb
	result = read(hw->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_rawmidi_ops_t snd_rawmidi_hw_ops = {
Packit 4a16fb
	.close = snd_rawmidi_hw_close,
Packit 4a16fb
	.nonblock = snd_rawmidi_hw_nonblock,
Packit 4a16fb
	.info = snd_rawmidi_hw_info,
Packit 4a16fb
	.params = snd_rawmidi_hw_params,
Packit 4a16fb
	.status = snd_rawmidi_hw_status,
Packit 4a16fb
	.drop = snd_rawmidi_hw_drop,
Packit 4a16fb
	.drain = snd_rawmidi_hw_drain,
Packit 4a16fb
	.write = snd_rawmidi_hw_write,
Packit 4a16fb
	.read = snd_rawmidi_hw_read,
Packit 4a16fb
};
Packit 4a16fb
Packit 4a16fb
Packit 4a16fb
int snd_rawmidi_hw_open(snd_rawmidi_t **inputp, snd_rawmidi_t **outputp,
Packit 4a16fb
			const char *name, int card, int device, int subdevice,
Packit 4a16fb
			int mode)
Packit 4a16fb
{
Packit 4a16fb
	int fd, ver, ret;
Packit 4a16fb
	int attempt = 0;
Packit 4a16fb
	char filename[sizeof(SNDRV_FILE_RAWMIDI) + 20];
Packit 4a16fb
	snd_ctl_t *ctl;
Packit 4a16fb
	snd_rawmidi_t *rmidi;
Packit 4a16fb
	snd_rawmidi_hw_t *hw = NULL;
Packit 4a16fb
	snd_rawmidi_info_t info;
Packit 4a16fb
	int fmode;
Packit 4a16fb
Packit 4a16fb
	if (inputp)
Packit 4a16fb
		*inputp = NULL;
Packit 4a16fb
	if (outputp)
Packit 4a16fb
		*outputp = NULL;
Packit 4a16fb
	if (!inputp && !outputp)
Packit 4a16fb
		return -EINVAL;
Packit 4a16fb
	
Packit 4a16fb
	if ((ret = snd_ctl_hw_open(&ctl, NULL, card, 0)) < 0)
Packit 4a16fb
		return ret;
Packit 4a16fb
	sprintf(filename, SNDRV_FILE_RAWMIDI, card, device);
Packit 4a16fb
Packit 4a16fb
      __again:
Packit 4a16fb
      	if (attempt++ > 3) {
Packit 4a16fb
      		snd_ctl_close(ctl);
Packit 4a16fb
      		return -EBUSY;
Packit 4a16fb
      	}
Packit 4a16fb
      	ret = snd_ctl_rawmidi_prefer_subdevice(ctl, subdevice);
Packit 4a16fb
	if (ret < 0) {
Packit 4a16fb
		snd_ctl_close(ctl);
Packit 4a16fb
		return ret;
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	if (!inputp)
Packit 4a16fb
		fmode = O_WRONLY;
Packit 4a16fb
	else if (!outputp)
Packit 4a16fb
		fmode = O_RDONLY;
Packit 4a16fb
	else
Packit 4a16fb
		fmode = O_RDWR;
Packit 4a16fb
Packit 4a16fb
	if (mode & SND_RAWMIDI_APPEND) {
Packit 4a16fb
		assert(outputp);
Packit 4a16fb
		fmode |= O_APPEND;
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	if (mode & SND_RAWMIDI_NONBLOCK) {
Packit 4a16fb
		fmode |= O_NONBLOCK;
Packit 4a16fb
	}
Packit 4a16fb
	
Packit 4a16fb
	if (mode & SND_RAWMIDI_SYNC) {
Packit 4a16fb
		fmode |= O_SYNC;
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	assert(!(mode & ~(SND_RAWMIDI_APPEND|SND_RAWMIDI_NONBLOCK|SND_RAWMIDI_SYNC)));
Packit 4a16fb
Packit 4a16fb
	fd = snd_open_device(filename, fmode);
Packit 4a16fb
	if (fd < 0) {
Packit 4a16fb
		snd_card_load(card);
Packit 4a16fb
		fd = snd_open_device(filename, fmode);
Packit 4a16fb
		if (fd < 0) {
Packit 4a16fb
			snd_ctl_close(ctl);
Packit 4a16fb
			SYSERR("open %s failed", filename);
Packit 4a16fb
			return -errno;
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
	if (ioctl(fd, SNDRV_RAWMIDI_IOCTL_PVERSION, &ver) < 0) {
Packit 4a16fb
		ret = -errno;
Packit 4a16fb
		SYSERR("SNDRV_RAWMIDI_IOCTL_PVERSION failed");
Packit 4a16fb
		close(fd);
Packit 4a16fb
		snd_ctl_close(ctl);
Packit 4a16fb
		return ret;
Packit 4a16fb
	}
Packit 4a16fb
	if (SNDRV_PROTOCOL_INCOMPATIBLE(ver, SNDRV_RAWMIDI_VERSION_MAX)) {
Packit 4a16fb
		close(fd);
Packit 4a16fb
		snd_ctl_close(ctl);
Packit 4a16fb
		return -SND_ERROR_INCOMPATIBLE_VERSION;
Packit 4a16fb
	}
Packit 4a16fb
	if (subdevice >= 0) {
Packit 4a16fb
		memset(&info, 0, sizeof(info));
Packit 4a16fb
		info.stream = outputp ? SNDRV_RAWMIDI_STREAM_OUTPUT : SNDRV_RAWMIDI_STREAM_INPUT;
Packit 4a16fb
		if (ioctl(fd, SNDRV_RAWMIDI_IOCTL_INFO, &info) < 0) {
Packit 4a16fb
			SYSERR("SNDRV_RAWMIDI_IOCTL_INFO failed");
Packit 4a16fb
			ret = -errno;
Packit 4a16fb
			close(fd);
Packit 4a16fb
			snd_ctl_close(ctl);
Packit 4a16fb
			return ret;
Packit 4a16fb
		}
Packit 4a16fb
		if (info.subdevice != (unsigned int) subdevice) {
Packit 4a16fb
			close(fd);
Packit 4a16fb
			goto __again;
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
	snd_ctl_close(ctl);
Packit 4a16fb
Packit 4a16fb
	hw = calloc(1, sizeof(snd_rawmidi_hw_t));
Packit 4a16fb
	if (hw == NULL)
Packit 4a16fb
		goto _nomem;
Packit 4a16fb
	hw->card = card;
Packit 4a16fb
	hw->device = device;
Packit 4a16fb
	hw->subdevice = subdevice;
Packit 4a16fb
	hw->fd = fd;
Packit 4a16fb
Packit 4a16fb
	if (inputp) {
Packit 4a16fb
		rmidi = calloc(1, sizeof(snd_rawmidi_t));
Packit 4a16fb
		if (rmidi == NULL)
Packit 4a16fb
			goto _nomem;
Packit 4a16fb
		if (name)
Packit 4a16fb
			rmidi->name = strdup(name);
Packit 4a16fb
		rmidi->type = SND_RAWMIDI_TYPE_HW;
Packit 4a16fb
		rmidi->stream = SND_RAWMIDI_STREAM_INPUT;
Packit 4a16fb
		rmidi->mode = mode;
Packit 4a16fb
		rmidi->poll_fd = fd;
Packit 4a16fb
		rmidi->ops = &snd_rawmidi_hw_ops;
Packit 4a16fb
		rmidi->private_data = hw;
Packit 4a16fb
		hw->open++;
Packit 4a16fb
		*inputp = rmidi;
Packit 4a16fb
	}
Packit 4a16fb
	if (outputp) {
Packit 4a16fb
		rmidi = calloc(1, sizeof(snd_rawmidi_t));
Packit 4a16fb
		if (rmidi == NULL)
Packit 4a16fb
			goto _nomem;
Packit 4a16fb
		if (name)
Packit 4a16fb
			rmidi->name = strdup(name);
Packit 4a16fb
		rmidi->type = SND_RAWMIDI_TYPE_HW;
Packit 4a16fb
		rmidi->stream = SND_RAWMIDI_STREAM_OUTPUT;
Packit 4a16fb
		rmidi->mode = mode;
Packit 4a16fb
		rmidi->poll_fd = fd;
Packit 4a16fb
		rmidi->ops = &snd_rawmidi_hw_ops;
Packit 4a16fb
		rmidi->private_data = hw;
Packit 4a16fb
		hw->open++;
Packit 4a16fb
		*outputp = rmidi;
Packit 4a16fb
	}
Packit 4a16fb
	return 0;
Packit 4a16fb
Packit 4a16fb
 _nomem:
Packit 4a16fb
	close(fd);
Packit 4a16fb
	free(hw);
Packit 4a16fb
	if (inputp)
Packit 4a16fb
		free(*inputp);
Packit 4a16fb
	if (outputp)
Packit 4a16fb
		free(*outputp);
Packit 4a16fb
	return -ENOMEM;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
int _snd_rawmidi_hw_open(snd_rawmidi_t **inputp, snd_rawmidi_t **outputp,
Packit 4a16fb
			 char *name, 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 card = -1, device = 0, subdevice = -1;
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_rawmidi_conf_generic_id(id))
Packit 4a16fb
			continue;
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
		return -EINVAL;
Packit 4a16fb
	}
Packit 4a16fb
	if (card < 0)
Packit 4a16fb
		return -EINVAL;
Packit 4a16fb
	return snd_rawmidi_hw_open(inputp, outputp, name, card, device, subdevice, mode);
Packit 4a16fb
}
Packit 4a16fb
SND_DLSYM_BUILD_VERSION(_snd_rawmidi_hw_open, SND_RAWMIDI_DLSYM_VERSION);