|
Packit Service |
db8eaa |
/*
|
|
Packit Service |
db8eaa |
* Hardware dependent Interface - main file for hardware access
|
|
Packit Service |
db8eaa |
* Copyright (c) 2001 by Jaroslav Kysela <perex@perex.cz>
|
|
Packit Service |
db8eaa |
*
|
|
Packit Service |
db8eaa |
*
|
|
Packit Service |
db8eaa |
* This library is free software; you can redistribute it and/or modify
|
|
Packit Service |
db8eaa |
* it under the terms of the GNU Lesser General Public License as
|
|
Packit Service |
db8eaa |
* published by the Free Software Foundation; either version 2.1 of
|
|
Packit Service |
db8eaa |
* the License, or (at your option) any later version.
|
|
Packit Service |
db8eaa |
*
|
|
Packit Service |
db8eaa |
* This program is distributed in the hope that it will be useful,
|
|
Packit Service |
db8eaa |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
db8eaa |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit Service |
db8eaa |
* GNU Lesser General Public License for more details.
|
|
Packit Service |
db8eaa |
*
|
|
Packit Service |
db8eaa |
* You should have received a copy of the GNU Lesser General Public
|
|
Packit Service |
db8eaa |
* License along with this library; if not, write to the Free Software
|
|
Packit Service |
db8eaa |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
Packit Service |
db8eaa |
*
|
|
Packit Service |
db8eaa |
*/
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
#include <stdio.h>
|
|
Packit Service |
db8eaa |
#include <stdlib.h>
|
|
Packit Service |
db8eaa |
#include <unistd.h>
|
|
Packit Service |
db8eaa |
#include <string.h>
|
|
Packit Service |
db8eaa |
#include <fcntl.h>
|
|
Packit Service |
db8eaa |
#include <sys/ioctl.h>
|
|
Packit Service |
db8eaa |
#include "hwdep_local.h"
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
#ifndef PIC
|
|
Packit Service |
db8eaa |
/* entry for static linking */
|
|
Packit Service |
db8eaa |
const char *_snd_module_hwdep_hw = "";
|
|
Packit Service |
db8eaa |
#endif
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
#define SNDRV_FILE_HWDEP ALSA_DEVICE_DIRECTORY "hwC%iD%i"
|
|
Packit Service |
db8eaa |
#define SNDRV_HWDEP_VERSION_MAX SNDRV_PROTOCOL_VERSION(1, 0, 1)
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
static int snd_hwdep_hw_close(snd_hwdep_t *hwdep)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
int res;
|
|
Packit Service |
db8eaa |
assert(hwdep);
|
|
Packit Service |
db8eaa |
res = close(hwdep->poll_fd) < 0 ? -errno : 0;
|
|
Packit Service |
db8eaa |
return res;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
static int snd_hwdep_hw_nonblock(snd_hwdep_t *hwdep, int nonblock)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
long flags;
|
|
Packit Service |
db8eaa |
assert(hwdep);
|
|
Packit Service |
db8eaa |
if ((flags = fcntl(hwdep->poll_fd, F_GETFL)) < 0)
|
|
Packit Service |
db8eaa |
return -errno;
|
|
Packit Service |
db8eaa |
if (nonblock)
|
|
Packit Service |
db8eaa |
flags |= O_NONBLOCK;
|
|
Packit Service |
db8eaa |
else
|
|
Packit Service |
db8eaa |
flags &= ~O_NONBLOCK;
|
|
Packit Service |
db8eaa |
if (fcntl(hwdep->poll_fd, F_SETFL, flags) < 0)
|
|
Packit Service |
db8eaa |
return -errno;
|
|
Packit Service |
db8eaa |
return 0;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
static int snd_hwdep_hw_info(snd_hwdep_t *hwdep, snd_hwdep_info_t *info)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
assert(hwdep && info);
|
|
Packit Service |
db8eaa |
if (ioctl(hwdep->poll_fd, SNDRV_HWDEP_IOCTL_INFO, info) < 0)
|
|
Packit Service |
db8eaa |
return -errno;
|
|
Packit Service |
db8eaa |
return 0;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
static int snd_hwdep_hw_ioctl(snd_hwdep_t *hwdep, unsigned int request, void * arg)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
assert(hwdep);
|
|
Packit Service |
db8eaa |
if (ioctl(hwdep->poll_fd, request, arg) < 0)
|
|
Packit Service |
db8eaa |
return -errno;
|
|
Packit Service |
db8eaa |
return 0;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
static ssize_t snd_hwdep_hw_write(snd_hwdep_t *hwdep, const void *buffer, size_t size)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
ssize_t result;
|
|
Packit Service |
db8eaa |
assert(hwdep && (buffer || size == 0));
|
|
Packit Service |
db8eaa |
result = write(hwdep->poll_fd, buffer, size);
|
|
Packit Service |
db8eaa |
if (result < 0)
|
|
Packit Service |
db8eaa |
return -errno;
|
|
Packit Service |
db8eaa |
return result;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
static ssize_t snd_hwdep_hw_read(snd_hwdep_t *hwdep, void *buffer, size_t size)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
ssize_t result;
|
|
Packit Service |
db8eaa |
assert(hwdep && (buffer || size == 0));
|
|
Packit Service |
db8eaa |
result = read(hwdep->poll_fd, buffer, size);
|
|
Packit Service |
db8eaa |
if (result < 0)
|
|
Packit Service |
db8eaa |
return -errno;
|
|
Packit Service |
db8eaa |
return result;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
static const snd_hwdep_ops_t snd_hwdep_hw_ops = {
|
|
Packit Service |
db8eaa |
.close = snd_hwdep_hw_close,
|
|
Packit Service |
db8eaa |
.nonblock = snd_hwdep_hw_nonblock,
|
|
Packit Service |
db8eaa |
.info = snd_hwdep_hw_info,
|
|
Packit Service |
db8eaa |
.ioctl = snd_hwdep_hw_ioctl,
|
|
Packit Service |
db8eaa |
.write = snd_hwdep_hw_write,
|
|
Packit Service |
db8eaa |
.read = snd_hwdep_hw_read,
|
|
Packit Service |
db8eaa |
};
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
int snd_hwdep_hw_open(snd_hwdep_t **handle, const char *name, int card, int device, int mode)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
int fd, ver, ret;
|
|
Packit Service |
db8eaa |
char filename[sizeof(SNDRV_FILE_HWDEP) + 20];
|
|
Packit Service |
db8eaa |
snd_hwdep_t *hwdep;
|
|
Packit Service |
db8eaa |
assert(handle);
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
*handle = NULL;
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
if (card < 0 || card >= SND_MAX_CARDS)
|
|
Packit Service |
db8eaa |
return -EINVAL;
|
|
Packit Service |
db8eaa |
sprintf(filename, SNDRV_FILE_HWDEP, card, device);
|
|
Packit Service |
db8eaa |
fd = snd_open_device(filename, mode);
|
|
Packit Service |
db8eaa |
if (fd < 0) {
|
|
Packit Service |
db8eaa |
snd_card_load(card);
|
|
Packit Service |
db8eaa |
fd = snd_open_device(filename, mode);
|
|
Packit Service |
db8eaa |
if (fd < 0)
|
|
Packit Service |
db8eaa |
return -errno;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
if (ioctl(fd, SNDRV_HWDEP_IOCTL_PVERSION, &ver) < 0) {
|
|
Packit Service |
db8eaa |
ret = -errno;
|
|
Packit Service |
db8eaa |
close(fd);
|
|
Packit Service |
db8eaa |
return ret;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
if (SNDRV_PROTOCOL_INCOMPATIBLE(ver, SNDRV_HWDEP_VERSION_MAX)) {
|
|
Packit Service |
db8eaa |
close(fd);
|
|
Packit Service |
db8eaa |
return -SND_ERROR_INCOMPATIBLE_VERSION;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
hwdep = (snd_hwdep_t *) calloc(1, sizeof(snd_hwdep_t));
|
|
Packit Service |
db8eaa |
if (hwdep == NULL) {
|
|
Packit Service |
db8eaa |
close(fd);
|
|
Packit Service |
db8eaa |
return -ENOMEM;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
hwdep->name = strdup(name);
|
|
Packit Service |
db8eaa |
hwdep->poll_fd = fd;
|
|
Packit Service |
db8eaa |
hwdep->mode = mode;
|
|
Packit Service |
db8eaa |
hwdep->type = SND_HWDEP_TYPE_HW;
|
|
Packit Service |
db8eaa |
hwdep->ops = &snd_hwdep_hw_ops;
|
|
Packit Service |
db8eaa |
*handle = hwdep;
|
|
Packit Service |
db8eaa |
return 0;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
int _snd_hwdep_hw_open(snd_hwdep_t **hwdep, char *name,
|
|
Packit Service |
db8eaa |
snd_config_t *root ATTRIBUTE_UNUSED,
|
|
Packit Service |
db8eaa |
snd_config_t *conf, int mode)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
snd_config_iterator_t i, next;
|
|
Packit Service |
db8eaa |
long card = -1, device = 0;
|
|
Packit Service |
db8eaa |
const char *str;
|
|
Packit Service |
db8eaa |
int err;
|
|
Packit Service |
db8eaa |
snd_config_for_each(i, next, conf) {
|
|
Packit Service |
db8eaa |
snd_config_t *n = snd_config_iterator_entry(i);
|
|
Packit Service |
db8eaa |
const char *id;
|
|
Packit Service |
db8eaa |
if (snd_config_get_id(n, &id) < 0)
|
|
Packit Service |
db8eaa |
continue;
|
|
Packit Service |
db8eaa |
if (_snd_conf_generic_id(id))
|
|
Packit Service |
db8eaa |
continue;
|
|
Packit Service |
db8eaa |
if (strcmp(id, "card") == 0) {
|
|
Packit Service |
db8eaa |
err = snd_config_get_integer(n, &card;;
|
|
Packit Service |
db8eaa |
if (err < 0) {
|
|
Packit Service |
db8eaa |
err = snd_config_get_string(n, &str);
|
|
Packit Service |
db8eaa |
if (err < 0)
|
|
Packit Service |
db8eaa |
return -EINVAL;
|
|
Packit Service |
db8eaa |
card = snd_card_get_index(str);
|
|
Packit Service |
db8eaa |
if (card < 0)
|
|
Packit Service |
db8eaa |
return card;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
continue;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
if (strcmp(id, "device") == 0) {
|
|
Packit Service |
db8eaa |
err = snd_config_get_integer(n, &device);
|
|
Packit Service |
db8eaa |
if (err < 0)
|
|
Packit Service |
db8eaa |
return err;
|
|
Packit Service |
db8eaa |
continue;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
SNDERR("Unexpected field %s", id);
|
|
Packit Service |
db8eaa |
return -EINVAL;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
if (card < 0)
|
|
Packit Service |
db8eaa |
return -EINVAL;
|
|
Packit Service |
db8eaa |
return snd_hwdep_hw_open(hwdep, name, card, device, mode);
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
SND_DLSYM_BUILD_VERSION(_snd_hwdep_hw_open, SND_HWDEP_DLSYM_VERSION);
|