Blame src/control/control_hw.c

Packit 4a16fb
/*
Packit 4a16fb
 *  Control Interface - Hardware
Packit 4a16fb
 *  Copyright (c) 1998,1999,2000 by Jaroslav Kysela <perex@perex.cz>
Packit 4a16fb
 *  Copyright (c) 2000 by 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 <signal.h>
Packit 4a16fb
#include <string.h>
Packit 4a16fb
#include <fcntl.h>
Packit 4a16fb
#include <sys/ioctl.h>
Packit 4a16fb
#include "control_local.h"
Packit 4a16fb
Packit 4a16fb
#ifndef PIC
Packit 4a16fb
/* entry for static linking */
Packit 4a16fb
const char *_snd_module_control_hw = "";
Packit 4a16fb
#endif
Packit 4a16fb
Packit 4a16fb
#ifndef F_SETSIG
Packit 4a16fb
#define F_SETSIG 10
Packit 4a16fb
#endif
Packit 4a16fb
Packit 4a16fb
#ifndef DOC_HIDDEN
Packit 4a16fb
#define SNDRV_FILE_CONTROL	ALSA_DEVICE_DIRECTORY "controlC%i"
Packit 4a16fb
#define SNDRV_CTL_VERSION_MAX	SNDRV_PROTOCOL_VERSION(2, 0, 4)
Packit 4a16fb
Packit 4a16fb
typedef struct {
Packit 4a16fb
	int card;
Packit 4a16fb
	int fd;
Packit 4a16fb
	unsigned int protocol;
Packit 4a16fb
} snd_ctl_hw_t;
Packit 4a16fb
#endif /* DOC_HIDDEN */
Packit 4a16fb
Packit 4a16fb
static int snd_ctl_hw_close(snd_ctl_t *handle)
Packit 4a16fb
{
Packit 4a16fb
	snd_ctl_hw_t *hw = handle->private_data;
Packit 4a16fb
	int res;
Packit 4a16fb
	res = close(hw->fd) < 0 ? -errno : 0;
Packit 4a16fb
	free(hw);
Packit 4a16fb
	return res;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_ctl_hw_nonblock(snd_ctl_t *handle, int nonblock)
Packit 4a16fb
{
Packit 4a16fb
	snd_ctl_hw_t *hw = handle->private_data;
Packit 4a16fb
	long flags;
Packit 4a16fb
	int fd = hw->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 (nonblock)
Packit 4a16fb
		flags |= O_NONBLOCK;
Packit 4a16fb
	else
Packit 4a16fb
		flags &= ~O_NONBLOCK;
Packit 4a16fb
	if (fcntl(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_ctl_hw_async(snd_ctl_t *ctl, int sig, pid_t pid)
Packit 4a16fb
{
Packit 4a16fb
	long flags;
Packit 4a16fb
	snd_ctl_hw_t *hw = ctl->private_data;
Packit 4a16fb
	int fd = hw->fd;
Packit 4a16fb
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
	if (fcntl(fd, F_SETSIG, (long)sig) < 0) {
Packit 4a16fb
		SYSERR("F_SETSIG failed");
Packit 4a16fb
		return -errno;
Packit 4a16fb
	}
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_ctl_hw_subscribe_events(snd_ctl_t *handle, int subscribe)
Packit 4a16fb
{
Packit 4a16fb
	snd_ctl_hw_t *hw = handle->private_data;
Packit 4a16fb
	if (ioctl(hw->fd, SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS, &subscribe) < 0) {
Packit 4a16fb
		SYSERR("SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS failed");
Packit 4a16fb
		return -errno;
Packit 4a16fb
	}
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_ctl_hw_card_info(snd_ctl_t *handle, snd_ctl_card_info_t *info)
Packit 4a16fb
{
Packit 4a16fb
	snd_ctl_hw_t *hw = handle->private_data;
Packit 4a16fb
	if (ioctl(hw->fd, SNDRV_CTL_IOCTL_CARD_INFO, info) < 0) {
Packit 4a16fb
		SYSERR("SNDRV_CTL_IOCTL_CARD_INFO failed");
Packit 4a16fb
		return -errno;
Packit 4a16fb
	}
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_ctl_hw_elem_list(snd_ctl_t *handle, snd_ctl_elem_list_t *list)
Packit 4a16fb
{
Packit 4a16fb
	snd_ctl_hw_t *hw = handle->private_data;
Packit 4a16fb
	if (ioctl(hw->fd, SNDRV_CTL_IOCTL_ELEM_LIST, list) < 0)
Packit 4a16fb
		return -errno;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_ctl_hw_elem_info(snd_ctl_t *handle, snd_ctl_elem_info_t *info)
Packit 4a16fb
{
Packit 4a16fb
	snd_ctl_hw_t *hw = handle->private_data;
Packit 4a16fb
	if (ioctl(hw->fd, SNDRV_CTL_IOCTL_ELEM_INFO, info) < 0)
Packit 4a16fb
		return -errno;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_ctl_hw_elem_add(snd_ctl_t *handle, snd_ctl_elem_info_t *info)
Packit 4a16fb
{
Packit 4a16fb
	snd_ctl_hw_t *hw = handle->private_data;
Packit 4a16fb
Packit 4a16fb
	if (info->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED &&
Packit 4a16fb
	    hw->protocol < SNDRV_PROTOCOL_VERSION(2, 0, 7))
Packit 4a16fb
		return -ENXIO;
Packit 4a16fb
Packit 4a16fb
	if (ioctl(hw->fd, SNDRV_CTL_IOCTL_ELEM_ADD, info) < 0)
Packit 4a16fb
		return -errno;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_ctl_hw_elem_replace(snd_ctl_t *handle, snd_ctl_elem_info_t *info)
Packit 4a16fb
{
Packit 4a16fb
	snd_ctl_hw_t *hw = handle->private_data;
Packit 4a16fb
Packit 4a16fb
	if (info->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED &&
Packit 4a16fb
	    hw->protocol < SNDRV_PROTOCOL_VERSION(2, 0, 7))
Packit 4a16fb
		return -ENXIO;
Packit 4a16fb
Packit 4a16fb
	if (ioctl(hw->fd, SNDRV_CTL_IOCTL_ELEM_REPLACE, info) < 0)
Packit 4a16fb
		return -errno;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_ctl_hw_elem_remove(snd_ctl_t *handle, snd_ctl_elem_id_t *id)
Packit 4a16fb
{
Packit 4a16fb
	snd_ctl_hw_t *hw = handle->private_data;
Packit 4a16fb
	if (ioctl(hw->fd, SNDRV_CTL_IOCTL_ELEM_REMOVE, id) < 0)
Packit 4a16fb
		return -errno;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_ctl_hw_elem_read(snd_ctl_t *handle, snd_ctl_elem_value_t *control)
Packit 4a16fb
{
Packit 4a16fb
	snd_ctl_hw_t *hw = handle->private_data;
Packit 4a16fb
	if (ioctl(hw->fd, SNDRV_CTL_IOCTL_ELEM_READ, control) < 0)
Packit 4a16fb
		return -errno;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_ctl_hw_elem_write(snd_ctl_t *handle, snd_ctl_elem_value_t *control)
Packit 4a16fb
{
Packit 4a16fb
	snd_ctl_hw_t *hw = handle->private_data;
Packit 4a16fb
	if (ioctl(hw->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, control) < 0)
Packit 4a16fb
		return -errno;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_ctl_hw_elem_lock(snd_ctl_t *handle, snd_ctl_elem_id_t *id)
Packit 4a16fb
{
Packit 4a16fb
	snd_ctl_hw_t *hw = handle->private_data;
Packit 4a16fb
	if (ioctl(hw->fd, SNDRV_CTL_IOCTL_ELEM_LOCK, id) < 0)
Packit 4a16fb
		return -errno;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_ctl_hw_elem_unlock(snd_ctl_t *handle, snd_ctl_elem_id_t *id)
Packit 4a16fb
{
Packit 4a16fb
	snd_ctl_hw_t *hw = handle->private_data;
Packit 4a16fb
	if (ioctl(hw->fd, SNDRV_CTL_IOCTL_ELEM_UNLOCK, id) < 0)
Packit 4a16fb
		return -errno;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_ctl_hw_elem_tlv(snd_ctl_t *handle, int op_flag,
Packit 4a16fb
			       unsigned int numid,
Packit 4a16fb
			       unsigned int *tlv, unsigned int tlv_size)
Packit 4a16fb
{
Packit 4a16fb
	unsigned int inum;
Packit 4a16fb
	snd_ctl_hw_t *hw = handle->private_data;
Packit 4a16fb
	struct snd_ctl_tlv *xtlv;
Packit 4a16fb
	
Packit 4a16fb
	/* we don't support TLV on protocol ver 2.0.3 or earlier */
Packit 4a16fb
	if (hw->protocol < SNDRV_PROTOCOL_VERSION(2, 0, 4))
Packit 4a16fb
		return -ENXIO;
Packit 4a16fb
Packit 4a16fb
	switch (op_flag) {
Packit 4a16fb
	case -1: inum = SNDRV_CTL_IOCTL_TLV_COMMAND; break;
Packit 4a16fb
 	case 0:	inum = SNDRV_CTL_IOCTL_TLV_READ; break;
Packit 4a16fb
	case 1:	inum = SNDRV_CTL_IOCTL_TLV_WRITE; break;
Packit 4a16fb
	default: return -EINVAL;
Packit 4a16fb
	}
Packit 4a16fb
	xtlv = malloc(sizeof(struct snd_ctl_tlv) + tlv_size);
Packit 4a16fb
	if (xtlv == NULL)
Packit 4a16fb
		return -ENOMEM; 
Packit 4a16fb
	xtlv->numid = numid;
Packit 4a16fb
	xtlv->length = tlv_size;
Packit 4a16fb
	memcpy(xtlv->tlv, tlv, tlv_size);
Packit 4a16fb
	if (ioctl(hw->fd, inum, xtlv) < 0) {
Packit 4a16fb
		free(xtlv);
Packit 4a16fb
		return -errno;
Packit 4a16fb
	}
Packit 4a16fb
	if (op_flag == 0) {
Packit 4a16fb
		unsigned int size;
Packit 4a16fb
		size = xtlv->tlv[SNDRV_CTL_TLVO_LEN] + 2 * sizeof(unsigned int);
Packit 4a16fb
		if (size > tlv_size) {
Packit 4a16fb
			free(xtlv);
Packit 4a16fb
			return -EFAULT;
Packit 4a16fb
		}
Packit 4a16fb
		memcpy(tlv, xtlv->tlv, size);
Packit 4a16fb
	}
Packit 4a16fb
	free(xtlv);
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_ctl_hw_hwdep_next_device(snd_ctl_t *handle, int * device)
Packit 4a16fb
{
Packit 4a16fb
	snd_ctl_hw_t *hw = handle->private_data;
Packit 4a16fb
	if (ioctl(hw->fd, SNDRV_CTL_IOCTL_HWDEP_NEXT_DEVICE, device) < 0)
Packit 4a16fb
		return -errno;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_ctl_hw_hwdep_info(snd_ctl_t *handle, snd_hwdep_info_t * info)
Packit 4a16fb
{
Packit 4a16fb
	snd_ctl_hw_t *hw = handle->private_data;
Packit 4a16fb
	if (ioctl(hw->fd, SNDRV_CTL_IOCTL_HWDEP_INFO, info) < 0)
Packit 4a16fb
		return -errno;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_ctl_hw_pcm_next_device(snd_ctl_t *handle, int * device)
Packit 4a16fb
{
Packit 4a16fb
	snd_ctl_hw_t *hw = handle->private_data;
Packit 4a16fb
	if (ioctl(hw->fd, SNDRV_CTL_IOCTL_PCM_NEXT_DEVICE, device) < 0)
Packit 4a16fb
		return -errno;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_ctl_hw_pcm_info(snd_ctl_t *handle, snd_pcm_info_t * info)
Packit 4a16fb
{
Packit 4a16fb
	snd_ctl_hw_t *hw = handle->private_data;
Packit 4a16fb
	if (ioctl(hw->fd, SNDRV_CTL_IOCTL_PCM_INFO, info) < 0)
Packit 4a16fb
		return -errno;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_ctl_hw_pcm_prefer_subdevice(snd_ctl_t *handle, int subdev)
Packit 4a16fb
{
Packit 4a16fb
	snd_ctl_hw_t *hw = handle->private_data;
Packit 4a16fb
	if (ioctl(hw->fd, SNDRV_CTL_IOCTL_PCM_PREFER_SUBDEVICE, &subdev) < 0)
Packit 4a16fb
		return -errno;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_ctl_hw_rawmidi_next_device(snd_ctl_t *handle, int * device)
Packit 4a16fb
{
Packit 4a16fb
	snd_ctl_hw_t *hw = handle->private_data;
Packit 4a16fb
	if (ioctl(hw->fd, SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE, device) < 0)
Packit 4a16fb
		return -errno;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_ctl_hw_rawmidi_info(snd_ctl_t *handle, snd_rawmidi_info_t * info)
Packit 4a16fb
{
Packit 4a16fb
	snd_ctl_hw_t *hw = handle->private_data;
Packit 4a16fb
	if (ioctl(hw->fd, SNDRV_CTL_IOCTL_RAWMIDI_INFO, info) < 0)
Packit 4a16fb
		return -errno;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_ctl_hw_rawmidi_prefer_subdevice(snd_ctl_t *handle, int subdev)
Packit 4a16fb
{
Packit 4a16fb
	snd_ctl_hw_t *hw = handle->private_data;
Packit 4a16fb
	if (ioctl(hw->fd, SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE, &subdev) < 0)
Packit 4a16fb
		return -errno;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_ctl_hw_set_power_state(snd_ctl_t *handle, unsigned int state)
Packit 4a16fb
{
Packit 4a16fb
	snd_ctl_hw_t *hw = handle->private_data;
Packit 4a16fb
	if (ioctl(hw->fd, SNDRV_CTL_IOCTL_POWER, &state) < 0)
Packit 4a16fb
		return -errno;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_ctl_hw_get_power_state(snd_ctl_t *handle, unsigned int *state)
Packit 4a16fb
{
Packit 4a16fb
	snd_ctl_hw_t *hw = handle->private_data;
Packit 4a16fb
	if (ioctl(hw->fd, SNDRV_CTL_IOCTL_POWER_STATE, state) < 0)
Packit 4a16fb
		return -errno;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_ctl_hw_read(snd_ctl_t *handle, snd_ctl_event_t *event)
Packit 4a16fb
{
Packit 4a16fb
	snd_ctl_hw_t *hw = handle->private_data;
Packit 4a16fb
	ssize_t res = read(hw->fd, event, sizeof(*event));
Packit 4a16fb
	if (res <= 0)
Packit 4a16fb
		return -errno;
Packit 4a16fb
	if (CHECK_SANITY(res != sizeof(*event))) {
Packit 4a16fb
		SNDMSG("snd_ctl_hw_read: read size error (req:%d, got:%d)\n",
Packit 4a16fb
		       sizeof(*event), res);
Packit 4a16fb
		return -EINVAL;
Packit 4a16fb
	}
Packit 4a16fb
	return 1;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static const snd_ctl_ops_t snd_ctl_hw_ops = {
Packit 4a16fb
	.close = snd_ctl_hw_close,
Packit 4a16fb
	.nonblock = snd_ctl_hw_nonblock,
Packit 4a16fb
	.async = snd_ctl_hw_async,
Packit 4a16fb
	.subscribe_events = snd_ctl_hw_subscribe_events,
Packit 4a16fb
	.card_info = snd_ctl_hw_card_info,
Packit 4a16fb
	.element_list = snd_ctl_hw_elem_list,
Packit 4a16fb
	.element_info = snd_ctl_hw_elem_info,
Packit 4a16fb
	.element_add = snd_ctl_hw_elem_add,
Packit 4a16fb
	.element_replace = snd_ctl_hw_elem_replace,
Packit 4a16fb
	.element_remove = snd_ctl_hw_elem_remove,
Packit 4a16fb
	.element_read = snd_ctl_hw_elem_read,
Packit 4a16fb
	.element_write = snd_ctl_hw_elem_write,
Packit 4a16fb
	.element_lock = snd_ctl_hw_elem_lock,
Packit 4a16fb
	.element_unlock = snd_ctl_hw_elem_unlock,
Packit 4a16fb
	.element_tlv = snd_ctl_hw_elem_tlv,
Packit 4a16fb
	.hwdep_next_device = snd_ctl_hw_hwdep_next_device,
Packit 4a16fb
	.hwdep_info = snd_ctl_hw_hwdep_info,
Packit 4a16fb
	.pcm_next_device = snd_ctl_hw_pcm_next_device,
Packit 4a16fb
	.pcm_info = snd_ctl_hw_pcm_info,
Packit 4a16fb
	.pcm_prefer_subdevice = snd_ctl_hw_pcm_prefer_subdevice,
Packit 4a16fb
	.rawmidi_next_device = snd_ctl_hw_rawmidi_next_device,
Packit 4a16fb
	.rawmidi_info = snd_ctl_hw_rawmidi_info,
Packit 4a16fb
	.rawmidi_prefer_subdevice = snd_ctl_hw_rawmidi_prefer_subdevice,
Packit 4a16fb
	.set_power_state = snd_ctl_hw_set_power_state,
Packit 4a16fb
	.get_power_state = snd_ctl_hw_get_power_state,
Packit 4a16fb
	.read = snd_ctl_hw_read,
Packit 4a16fb
};
Packit 4a16fb
Packit 4a16fb
int snd_ctl_hw_open(snd_ctl_t **handle, const char *name, int card, int mode)
Packit 4a16fb
{
Packit 4a16fb
	int fd, ver;
Packit 4a16fb
	char filename[sizeof(SNDRV_FILE_CONTROL) + 10];
Packit 4a16fb
	int fmode;
Packit 4a16fb
	snd_ctl_t *ctl;
Packit 4a16fb
	snd_ctl_hw_t *hw;
Packit 4a16fb
	int err;
Packit 4a16fb
Packit 4a16fb
	*handle = NULL;	
Packit 4a16fb
Packit 4a16fb
	if (CHECK_SANITY(card < 0 || card >= SND_MAX_CARDS)) {
Packit 4a16fb
		SNDMSG("Invalid card index %d", card);
Packit 4a16fb
		return -EINVAL;
Packit 4a16fb
	}
Packit 4a16fb
	sprintf(filename, SNDRV_FILE_CONTROL, card);
Packit 4a16fb
	if (mode & SND_CTL_READONLY)
Packit 4a16fb
		fmode = O_RDONLY;
Packit 4a16fb
	else
Packit 4a16fb
		fmode = O_RDWR;
Packit 4a16fb
	if (mode & SND_CTL_NONBLOCK)
Packit 4a16fb
		fmode |= O_NONBLOCK;
Packit 4a16fb
	if (mode & SND_CTL_ASYNC)
Packit 4a16fb
		fmode |= O_ASYNC;
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
			return -errno;
Packit 4a16fb
	}
Packit 4a16fb
	if (ioctl(fd, SNDRV_CTL_IOCTL_PVERSION, &ver) < 0) {
Packit 4a16fb
		err = -errno;
Packit 4a16fb
		close(fd);
Packit 4a16fb
		return err;
Packit 4a16fb
	}
Packit 4a16fb
	if (SNDRV_PROTOCOL_INCOMPATIBLE(ver, SNDRV_CTL_VERSION_MAX)) {
Packit 4a16fb
		close(fd);
Packit 4a16fb
		return -SND_ERROR_INCOMPATIBLE_VERSION;
Packit 4a16fb
	}
Packit 4a16fb
	hw = calloc(1, sizeof(snd_ctl_hw_t));
Packit 4a16fb
	if (hw == NULL) {
Packit 4a16fb
		close(fd);
Packit 4a16fb
		return -ENOMEM;
Packit 4a16fb
	}
Packit 4a16fb
	hw->card = card;
Packit 4a16fb
	hw->fd = fd;
Packit 4a16fb
	hw->protocol = ver;
Packit 4a16fb
Packit 4a16fb
	err = snd_ctl_new(&ctl, SND_CTL_TYPE_HW, name);
Packit 4a16fb
	if (err < 0) {
Packit 4a16fb
		close(fd);
Packit 4a16fb
		free(hw);
Packit 4a16fb
		return err;
Packit 4a16fb
	}
Packit 4a16fb
	ctl->ops = &snd_ctl_hw_ops;
Packit 4a16fb
	ctl->private_data = hw;
Packit 4a16fb
	ctl->poll_fd = fd;
Packit 4a16fb
	*handle = ctl;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
int _snd_ctl_hw_open(snd_ctl_t **handlep, char *name, snd_config_t *root ATTRIBUTE_UNUSED, snd_config_t *conf, int mode)
Packit 4a16fb
{
Packit 4a16fb
	snd_config_iterator_t i, next;
Packit 4a16fb
	long card = -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_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
		return -EINVAL;
Packit 4a16fb
	}
Packit 4a16fb
	if (card < 0)
Packit 4a16fb
		return -EINVAL;
Packit 4a16fb
	return snd_ctl_hw_open(handlep, name, card, mode);
Packit 4a16fb
}
Packit 4a16fb
SND_DLSYM_BUILD_VERSION(_snd_ctl_hw_open, SND_CONTROL_DLSYM_VERSION);