2000-01-10 10:07:53 +00:00
|
|
|
/*
|
|
|
|
|
* Hardware dependent Interface - main file
|
|
|
|
|
* Copyright (c) 2000 by Jaroslav Kysela <perex@suse.cz>
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* 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 <fcntl.h>
|
|
|
|
|
#include <sys/ioctl.h>
|
2001-01-31 17:26:56 +00:00
|
|
|
#include "local.h"
|
2000-01-10 10:07:53 +00:00
|
|
|
|
2001-01-29 14:27:53 +00:00
|
|
|
#define SNDRV_FILE_HWDEP "/dev/snd/hwC%iD%i"
|
|
|
|
|
#define SNDRV_HWDEP_VERSION_MAX SNDRV_PROTOCOL_VERSION(1, 0, 0)
|
2000-01-10 10:07:53 +00:00
|
|
|
|
2000-11-20 20:10:46 +00:00
|
|
|
struct _snd_hwdep {
|
2000-01-10 10:07:53 +00:00
|
|
|
int card;
|
|
|
|
|
int device;
|
|
|
|
|
int fd;
|
|
|
|
|
int mode;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
int snd_hwdep_open(snd_hwdep_t **handle, int card, int device, int mode)
|
|
|
|
|
{
|
|
|
|
|
int fd, ver;
|
|
|
|
|
char filename[32];
|
|
|
|
|
snd_hwdep_t *hwdep;
|
2001-02-12 23:51:49 +00:00
|
|
|
assert(handle);
|
2000-01-10 10:07:53 +00:00
|
|
|
|
|
|
|
|
*handle = NULL;
|
|
|
|
|
|
2000-12-01 12:58:34 +00:00
|
|
|
if (card < 0 || card >= 32)
|
2000-01-10 10:07:53 +00:00
|
|
|
return -EINVAL;
|
2001-01-29 14:27:53 +00:00
|
|
|
sprintf(filename, SNDRV_FILE_HWDEP, card, device);
|
2000-01-10 10:07:53 +00:00
|
|
|
if ((fd = open(filename, mode)) < 0) {
|
|
|
|
|
snd_card_load(card);
|
|
|
|
|
if ((fd = open(filename, mode)) < 0)
|
|
|
|
|
return -errno;
|
|
|
|
|
}
|
2001-01-29 14:27:53 +00:00
|
|
|
if (ioctl(fd, SNDRV_HWDEP_IOCTL_PVERSION, &ver) < 0) {
|
2000-01-10 10:07:53 +00:00
|
|
|
close(fd);
|
|
|
|
|
return -errno;
|
|
|
|
|
}
|
2001-01-29 14:27:53 +00:00
|
|
|
if (SNDRV_PROTOCOL_INCOMPATIBLE(ver, SNDRV_HWDEP_VERSION_MAX)) {
|
2000-01-10 10:07:53 +00:00
|
|
|
close(fd);
|
|
|
|
|
return -SND_ERROR_INCOMPATIBLE_VERSION;
|
|
|
|
|
}
|
|
|
|
|
hwdep = (snd_hwdep_t *) calloc(1, sizeof(snd_hwdep_t));
|
|
|
|
|
if (hwdep == NULL) {
|
|
|
|
|
close(fd);
|
|
|
|
|
return -ENOMEM;
|
|
|
|
|
}
|
|
|
|
|
hwdep->card = card;
|
|
|
|
|
hwdep->device = device;
|
|
|
|
|
hwdep->fd = fd;
|
|
|
|
|
hwdep->mode = mode;
|
|
|
|
|
*handle = hwdep;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int snd_hwdep_close(snd_hwdep_t *hwdep)
|
|
|
|
|
{
|
|
|
|
|
int res;
|
2001-02-12 23:51:49 +00:00
|
|
|
assert(hwdep);
|
2000-01-10 10:07:53 +00:00
|
|
|
res = close(hwdep->fd) < 0 ? -errno : 0;
|
|
|
|
|
free(hwdep);
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2001-02-12 23:51:49 +00:00
|
|
|
int snd_hwdep_poll_descriptors(snd_hwdep_t *hwdep, struct pollfd *pfds, unsigned int space)
|
2000-01-10 10:07:53 +00:00
|
|
|
{
|
2001-02-12 23:51:49 +00:00
|
|
|
assert(hwdep);
|
|
|
|
|
if (space >= 1) {
|
|
|
|
|
pfds->fd = hwdep->fd;
|
|
|
|
|
pfds->events = POLLOUT | POLLIN;
|
|
|
|
|
}
|
|
|
|
|
return 1;
|
2000-01-10 10:07:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int snd_hwdep_block_mode(snd_hwdep_t *hwdep, int enable)
|
|
|
|
|
{
|
|
|
|
|
long flags;
|
2001-02-12 23:51:49 +00:00
|
|
|
assert(hwdep);
|
2000-01-10 10:07:53 +00:00
|
|
|
if ((flags = fcntl(hwdep->fd, F_GETFL)) < 0)
|
|
|
|
|
return -errno;
|
|
|
|
|
if (enable)
|
|
|
|
|
flags |= O_NONBLOCK;
|
2001-04-28 18:40:05 +00:00
|
|
|
else
|
|
|
|
|
flags &= ~O_NONBLOCK;
|
2000-01-10 10:07:53 +00:00
|
|
|
if (fcntl(hwdep->fd, F_SETFL, flags) < 0)
|
|
|
|
|
return -errno;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2001-02-05 15:44:42 +00:00
|
|
|
int snd_hwdep_info(snd_hwdep_t *hwdep, snd_hwdep_info_t *info)
|
2000-01-10 10:07:53 +00:00
|
|
|
{
|
2001-02-12 23:51:49 +00:00
|
|
|
assert(hwdep && info);
|
2001-01-29 14:27:53 +00:00
|
|
|
if (ioctl(hwdep->fd, SNDRV_HWDEP_IOCTL_INFO, info) < 0)
|
2000-01-10 10:07:53 +00:00
|
|
|
return -errno;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2001-03-29 17:50:28 +00:00
|
|
|
int snd_hwdep_ioctl(snd_hwdep_t *hwdep, unsigned int request, void * arg)
|
2000-01-11 17:35:50 +00:00
|
|
|
{
|
2001-02-12 23:51:49 +00:00
|
|
|
assert(hwdep);
|
2000-01-11 17:35:50 +00:00
|
|
|
if (ioctl(hwdep->fd, request, arg) < 0)
|
|
|
|
|
return -errno;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2000-01-10 10:07:53 +00:00
|
|
|
ssize_t snd_hwdep_write(snd_hwdep_t *hwdep, const void *buffer, size_t size)
|
|
|
|
|
{
|
|
|
|
|
ssize_t result;
|
2001-02-12 23:51:49 +00:00
|
|
|
assert(hwdep && (buffer || size == 0));
|
2000-01-10 10:07:53 +00:00
|
|
|
result = write(hwdep->fd, buffer, size);
|
|
|
|
|
if (result < 0)
|
|
|
|
|
return -errno;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ssize_t snd_hwdep_read(snd_hwdep_t *hwdep, void *buffer, size_t size)
|
|
|
|
|
{
|
|
|
|
|
ssize_t result;
|
2001-02-12 23:51:49 +00:00
|
|
|
assert(hwdep && (buffer || size == 0));
|
2000-01-10 10:07:53 +00:00
|
|
|
result = read(hwdep->fd, buffer, size);
|
|
|
|
|
if (result < 0)
|
|
|
|
|
return -errno;
|
|
|
|
|
return result;
|
|
|
|
|
}
|