#include <stdarg.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <poll.h>
#include <sndio.h>
#include <stdlib.h>
#include <stdio.h>
#include "soundcard.h"
#ifdef DEBUG
#define DPRINTF(...) do { fprintf(stderr, __VA_ARGS__); } while (0)
#else
#define DPRINTF(...) do {} while (0)
#endif
#define GET_DEV(com) ((com) & 0xff)
#define INTARG (*(int*)argp)
struct control {
struct control *next;
int type;
int chan;
int addr;
int value;
int max;
};
static int mixer_ioctl(int, unsigned long, void *);
static int initialized;
static struct control *controls;
static struct sioctl_hdl *hdl;
static char *dev_name = SIO_DEVANY;
static struct pollfd *pfds;
int
_oss_ioctl(int fd, unsigned long com, ...)
{
va_list ap;
void *argp;
va_start(ap, com);
argp = va_arg(ap, void *);
va_end(ap);
if (IOCGROUP(com) == 'P')
return ENOTTY;
else if (IOCGROUP(com) == 'M')
return mixer_ioctl(fd, com, argp);
else
return (ioctl)(fd, com, argp);
}
static void
mixer_ondesc(void *unused, struct sioctl_desc *d, int val)
{
struct control *i, **pi;
int type;
if (d == NULL)
return;
for (pi = &controls; (i = *pi) != NULL; pi = &i->next) {
if (d->addr == i->addr) {
*pi = i->next;
free(i);
break;
}
}
if (d->type != SIOCTL_NUM || d->node0.unit >= 2 ||
strcmp(d->func, "level") != 0)
return;
if (d->group[0] == 0) {
if (strcmp(d->node0.name, "output") == 0)
type = SOUND_MIXER_VOLUME;
else if (strcmp(d->node0.name, "input") == 0)
type = SOUND_MIXER_MIC;
else
return;
} else if (strcmp(d->group, "hw") == 0) {
if (strcmp(d->node0.name, "output") == 0)
type = SOUND_MIXER_OGAIN;
else if (strcmp(d->node0.name, "input") == 0)
type = SOUND_MIXER_IGAIN;
else
return;
} else
return;
i = malloc(sizeof(struct control));
if (i == NULL) {
DPRINTF("%s: cannot allocate control\n", __func__);
return;
}
i->addr = d->addr;
i->chan = d->node0.unit;
i->max = d->maxval;
i->value = val;
i->type = type;
i->next = controls;
controls = i;
DPRINTF("%s: %d: used as %d, chan = %d, value = %d\n", __func__,
i->addr, i->type, i->chan, i->value);
}
static void
mixer_onval(void *unused, unsigned int addr, unsigned int value)
{
struct control *c;
for (c = controls; ; c = c->next) {
if (c == NULL) {
DPRINTF("%s: %d: change ignored\n", __func__, addr);
return;
}
if (c->addr == addr)
break;
}
DPRINTF("%s: %d: changed to %d\n", __func__, addr, value);
c->value = value;
}
static int
mixer_init(void)
{
if (initialized)
return hdl != NULL;
initialized = 1;
hdl = sioctl_open(dev_name, SIOCTL_READ | SIOCTL_WRITE, 0);
if (hdl == NULL) {
DPRINTF("%s: cannot open audio control device\n", __func__);
return 0;
}
pfds = calloc(sioctl_nfds(hdl), sizeof(struct pollfd));
if (pfds == NULL) {
DPRINTF("%s: cannot allocate pfds\n", __func__);
goto bad_close;
}
if (!sioctl_ondesc(hdl, mixer_ondesc, NULL)) {
DPRINTF("%s: cannot get controls descriptions\n", __func__);
goto bad_free;
}
if (!sioctl_onval(hdl, mixer_onval, NULL)) {
DPRINTF("%s: cannot get controls values\n", __func__);
goto bad_free;
}
return 1;
bad_free:
free(pfds);
bad_close:
sioctl_close(hdl);
return 0;
}
static int
mixer_ioctl(int fd, unsigned long com, void *argp)
{
struct control *c;
struct mixer_info *omi;
int idat = 0;
int v, n;
if (!mixer_init()) {
DPRINTF("%s: not initialized\n", __func__);
errno = EIO;
return -1;
}
n = sioctl_pollfd(hdl, pfds, POLLIN);
if (n > 0) {
n = poll(pfds, n, 0);
if (n == -1)
return -1;
if (n > 0)
sioctl_revents(hdl, pfds);
}
switch (com) {
case OSS_GETVERSION:
idat = SOUND_VERSION;
break;
case SOUND_MIXER_INFO:
case SOUND_OLD_MIXER_INFO:
omi = argp;
if (com == SOUND_MIXER_INFO)
omi->modify_counter = 1;
strlcpy(omi->id, dev_name, sizeof omi->id);
strlcpy(omi->name, dev_name, sizeof omi->name);
return 0;
case SOUND_MIXER_READ_RECSRC:
case SOUND_MIXER_READ_RECMASK:
idat = 0;
for (c = controls; c != NULL; c = c->next)
idat |= 1 << c->type;
idat &= (1 << SOUND_MIXER_MIC) | (1 << SOUND_MIXER_IGAIN);
DPRINTF("%s: SOUND_MIXER_READ_RECSRC: %d\n", __func__, idat);
break;
case SOUND_MIXER_READ_DEVMASK:
idat = 0;
for (c = controls; c != NULL; c = c->next)
idat |= 1 << c->type;
DPRINTF("%s: SOUND_MIXER_READ_DEVMASK: %d\n", __func__, idat);
break;
case SOUND_MIXER_READ_STEREODEVS:
idat = 0;
for (c = controls; c != NULL; c = c->next) {
if (c->chan == 1)
idat |= 1 << c->type;
}
DPRINTF("%s: SOUND_MIXER_STEREODEVS: %d\n", __func__, idat);
break;
case SOUND_MIXER_READ_CAPS:
idat = 0;
DPRINTF("%s: SOUND_MIXER_READ_CAPS: %d\n", __func__, idat);
break;
case SOUND_MIXER_WRITE_RECSRC:
case SOUND_MIXER_WRITE_R_RECSRC:
DPRINTF("%s: SOUND_MIXER_WRITE_RECSRC\n", __func__);
errno = EINVAL;
return -1;
default:
if (MIXER_READ(SOUND_MIXER_FIRST) <= com &&
com < MIXER_READ(SOUND_MIXER_NRDEVICES)) {
doread:
idat = 0;
n = GET_DEV(com);
for (c = controls; c != NULL; c = c->next) {
if (c->type != n)
continue;
v = (c->value * 100 + c->max / 2) / c->max;
if (c->chan == 1)
v <<= 8;
idat |= v;
}
DPRINTF("%s: MIXER_READ: %d: 0x%04x\n",
__func__, n, idat);
break;
} else if ((MIXER_WRITE_R(SOUND_MIXER_FIRST) <= com &&
com < MIXER_WRITE_R(SOUND_MIXER_NRDEVICES)) ||
(MIXER_WRITE(SOUND_MIXER_FIRST) <= com &&
com < MIXER_WRITE(SOUND_MIXER_NRDEVICES))) {
idat = INTARG;
n = GET_DEV(com);
for (c = controls; c != NULL; c = c->next) {
if (c->type != n)
continue;
v = idat;
if (c->chan == 1)
v >>= 8;
v &= 0xff;
if (v > 100)
v = 100;
v = (v * c->max + 50) / 100;
sioctl_setval(hdl, c->addr, v);
DPRINTF("%s: MIXER_WRITE: %d: %d\n",
__func__, n, v);
}
if (MIXER_WRITE(SOUND_MIXER_FIRST) <= com &&
com < MIXER_WRITE(SOUND_MIXER_NRDEVICES))
return 0;
goto doread;
} else {
errno = EINVAL;
return -1;
}
}
INTARG = idat;
return 0;
}