/*-
* Copyright (c) 1997, 2008 The NetBSD Foundation, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
static struct audiodevinfo *getdevinfo(file_t *);
static int opaque_to_enum(struct audiodevinfo *di, audio_mixer_name_t *label, int opq);
static int enum_to_ord(struct audiodevinfo *di, int enm);
static int enum_to_mask(struct audiodevinfo *di, int enm);
com = SCARG(uap, com);
DPRINTF(("%s: com=%s\n", __func__, compat_ossaudio_getcmd(com)));
retval[0] = 0;
ioctlf = fp->f_ops->fo_ioctl;
switch (com) {
case OSS_SNDCTL_DSP_RESET:
error = ioctlf(fp, AUDIO_FLUSH, NULL);
if (error) {
DPRINTF(("%s: AUDIO_FLUSH %d\n", __func__, error));
goto out;
}
break;
case OSS_SNDCTL_DSP_SYNC:
error = ioctlf(fp, AUDIO_DRAIN, NULL);
if (error) {
DPRINTF(("%s: AUDIO_DRAIN %d\n", __func__, error));
goto out;
}
break;
case OSS_SNDCTL_DSP_POST:
/* This call is merely advisory, and may be a nop. */
break;
case OSS_SNDCTL_DSP_SPEED:
AUDIO_INITINFO(&tmpinfo);
error = copyin(SCARG(uap, data), &idat, sizeof idat);
if (error) {
DPRINTF(("%s: SNDCTL_DSP_SPEED %d\n",
__func__, error));
goto out;
}
tmpinfo.play.sample_rate =
tmpinfo.record.sample_rate = idat;
DPRINTF(("%s: SNDCTL_DSP_SPEED > %d\n", __func__, idat));
/*
* The default NetBSD behavior if an unsupported sample rate
* is set is to return an error code and keep the rate at the
* default of 8000 Hz.
*
* However, the OSS expectation is a sample rate supported by
* the hardware is returned if the exact rate could not be set.
*
* So, if the chosen sample rate is invalid, set and return
* the current hardware rate.
*/
if (ioctlf(fp, AUDIO_SETINFO, &tmpinfo) != 0) {
error = ioctlf(fp, AUDIO_GETFORMAT, &hwfmt);
if (error) {
DPRINTF(("%s: AUDIO_GETFORMAT %d\n",
__func__, error));
goto out;
}
error = ioctlf(fp, AUDIO_GETINFO, &tmpinfo);
if (error) {
DPRINTF(("%s: AUDIO_GETINFO %d\n",
__func__, error));
goto out;
}
tmpinfo.play.sample_rate =
tmpinfo.record.sample_rate =
(tmpinfo.mode == AUMODE_RECORD) ?
hwfmt.record.sample_rate :
hwfmt.play.sample_rate;
error = ioctlf(fp, AUDIO_SETINFO, &tmpinfo);
if (error) {
DPRINTF(("%s: SNDCTL_DSP_SPEED %d = %d\n",
__func__, idat, error));
goto out;
}
}
/* FALLTHROUGH */
case OSS_SOUND_PCM_READ_RATE:
error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo);
if (error) {
DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
__func__, error));
goto out;
}
idat = GETPRINFO(&tmpinfo, sample_rate);
DPRINTF(("%s: SNDCTL_PCM_READ_RATE < %d\n", __func__, idat));
error = copyout(&idat, SCARG(uap, data), sizeof idat);
if (error) {
DPRINTF(("%s: SOUND_PCM_READ_RATE %d = %d\n",
__func__, idat, error));
goto out;
}
break;
case OSS_SNDCTL_DSP_STEREO:
AUDIO_INITINFO(&tmpinfo);
error = copyin(SCARG(uap, data), &idat, sizeof idat);
if (error) {
DPRINTF(("%s: SNDCTL_DSP_STEREO %d\n",
__func__, error));
goto out;
}
tmpinfo.play.channels =
tmpinfo.record.channels = idat ? 2 : 1;
error = ioctlf(fp, AUDIO_SETINFO, &tmpinfo);
if (error) {
DPRINTF(("%s: AUDIO_SETINFO %d\n",
__func__, error));
goto out;
}
error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo);
if (error) {
DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
__func__, error));
goto out;
}
idat = GETPRINFO(&tmpinfo, channels) - 1;
error = copyout(&idat, SCARG(uap, data), sizeof idat);
if (error) {
DPRINTF(("%s: SNDCTL_DSP_STEREO %d = %d\n",
__func__, idat, error));
goto out;
}
break;
case OSS_SNDCTL_DSP_GETBLKSIZE:
error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo);
if (error) {
DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
__func__, error));
goto out;
}
setblocksize(fp, &tmpinfo);
idat = tmpinfo.blocksize;
DPRINTF(("%s: SNDCTL_DSP_GETBLKSIZE < %d\n",
__func__, idat));
error = copyout(&idat, SCARG(uap, data), sizeof idat);
if (error) {
DPRINTF(("%s: SNDCTL_DSP_GETBLKSIZE %d = %d\n",
__func__, idat, error));
goto out;
}
break;
case OSS_SNDCTL_DSP_SETFMT:
AUDIO_INITINFO(&tmpinfo);
error = copyin(SCARG(uap, data), &idat, sizeof idat);
if (error) {
DPRINTF(("%s: SNDCTL_DSP_SETFMT %d\n",
__func__, error));
goto out;
}
switch (idat) {
case OSS_AFMT_MU_LAW:
tmpinfo.play.precision =
tmpinfo.record.precision = 8;
tmpinfo.play.encoding =
tmpinfo.record.encoding = AUDIO_ENCODING_ULAW;
break;
case OSS_AFMT_A_LAW:
tmpinfo.play.precision =
tmpinfo.record.precision = 8;
tmpinfo.play.encoding =
tmpinfo.record.encoding = AUDIO_ENCODING_ALAW;
break;
case OSS_AFMT_U8:
tmpinfo.play.precision =
tmpinfo.record.precision = 8;
tmpinfo.play.encoding =
tmpinfo.record.encoding = AUDIO_ENCODING_ULINEAR;
break;
case OSS_AFMT_S8:
tmpinfo.play.precision =
tmpinfo.record.precision = 8;
tmpinfo.play.encoding =
tmpinfo.record.encoding = AUDIO_ENCODING_SLINEAR;
break;
case OSS_AFMT_S16_LE:
tmpinfo.play.precision =
tmpinfo.record.precision = 16;
tmpinfo.play.encoding =
tmpinfo.record.encoding = AUDIO_ENCODING_SLINEAR_LE;
break;
case OSS_AFMT_S16_BE:
tmpinfo.play.precision =
tmpinfo.record.precision = 16;
tmpinfo.play.encoding =
tmpinfo.record.encoding = AUDIO_ENCODING_SLINEAR_BE;
break;
case OSS_AFMT_U16_LE:
tmpinfo.play.precision =
tmpinfo.record.precision = 16;
tmpinfo.play.encoding =
tmpinfo.record.encoding = AUDIO_ENCODING_ULINEAR_LE;
break;
case OSS_AFMT_U16_BE:
tmpinfo.play.precision =
tmpinfo.record.precision = 16;
tmpinfo.play.encoding =
tmpinfo.record.encoding = AUDIO_ENCODING_ULINEAR_BE;
break;
case OSS_AFMT_AC3:
tmpinfo.play.precision =
tmpinfo.record.precision = 16;
tmpinfo.play.encoding =
tmpinfo.record.encoding = AUDIO_ENCODING_AC3;
break;
default:
/*
* OSSv4 specifies that if an invalid format is chosen
* by an application then a sensible format supported
* by the hardware is returned.
*
* In this case, we pick S16LE since it's reasonably
* assumed to be supported by applications.
*/
tmpinfo.play.precision =
tmpinfo.record.precision = 16;
tmpinfo.play.encoding =
tmpinfo.record.encoding = AUDIO_ENCODING_SLINEAR_LE;
break;
}
DPRINTF(("%s: SNDCTL_DSP_SETFMT > 0x%x\n",
__func__, idat));
error = ioctlf(fp, AUDIO_SETINFO, &tmpinfo);
if (error) {
DPRINTF(("%s: AUDIO_SETINFO %d\n",
__func__, error));
goto out;
}
/* FALLTHROUGH */
case OSS_SOUND_PCM_READ_BITS:
error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo);
if (error) {
DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
__func__, error));
goto out;
}
encoding = GETPRINFO(&tmpinfo, encoding);
precision = GETPRINFO(&tmpinfo, precision);
switch (encoding) {
case AUDIO_ENCODING_ULAW:
idat = OSS_AFMT_MU_LAW;
break;
case AUDIO_ENCODING_ALAW:
idat = OSS_AFMT_A_LAW;
break;
case AUDIO_ENCODING_SLINEAR_LE:
if (precision == 16)
idat = OSS_AFMT_S16_LE;
else
idat = OSS_AFMT_S8;
break;
case AUDIO_ENCODING_SLINEAR_BE:
if (precision == 16)
idat = OSS_AFMT_S16_BE;
else
idat = OSS_AFMT_S8;
break;
case AUDIO_ENCODING_ULINEAR_LE:
if (precision == 16)
idat = OSS_AFMT_U16_LE;
else
idat = OSS_AFMT_U8;
break;
case AUDIO_ENCODING_ULINEAR_BE:
if (precision == 16)
idat = OSS_AFMT_U16_BE;
else
idat = OSS_AFMT_U8;
break;
case AUDIO_ENCODING_ADPCM:
idat = OSS_AFMT_IMA_ADPCM;
break;
case AUDIO_ENCODING_AC3:
idat = OSS_AFMT_AC3;
break;
default:
DPRINTF(("%s: SOUND_PCM_READ_BITS bad encoding %d\n",
__func__, tmpinfo.play.encoding));
error = EINVAL;
goto out;
}
DPRINTF(("%s: SOUND_PCM_READ_BITS < 0x%x\n",
__func__, idat));
error = copyout(&idat, SCARG(uap, data), sizeof idat);
if (error) {
DPRINTF(("%s: SOUND_PCM_READ_BITS %d = %d\n",
__func__, idat, error));
goto out;
}
break;
case OSS_SNDCTL_DSP_CHANNELS:
AUDIO_INITINFO(&tmpinfo);
error = copyin(SCARG(uap, data), &idat, sizeof idat);
if (error) {
DPRINTF(("%s: SNDCTL_DSP_CHANNELS %d\n",
__func__, error));
goto out;
}
error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo);
if (error) {
DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
__func__, error));
goto out;
}
setchannels(fp, tmpinfo.mode, idat);
/* FALLTHROUGH */
case OSS_SOUND_PCM_READ_CHANNELS:
error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo);
if (error) {
DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
__func__, error));
goto out;
}
idat = GETPRINFO(&tmpinfo, channels);
DPRINTF(("%s: SOUND_PCM_READ_CHANNELS < %d\n", __func__, idat));
error = copyout(&idat, SCARG(uap, data), sizeof idat);
if (error) {
DPRINTF(("%s: SOUND_PCM_READ_CHANNELS %d = %d\n",
__func__, idat, error));
goto out;
}
break;
case OSS_SOUND_PCM_WRITE_FILTER:
case OSS_SOUND_PCM_READ_FILTER:
error = EINVAL; /* XXX unimplemented */
DPRINTF(("%s: SOUND_PCM_{READ,WRITE}_FILTER filter\n",
__func__));
goto out;
case OSS_SNDCTL_DSP_SUBDIVIDE:
error = copyin(SCARG(uap, data), &idat, sizeof idat);
if (error) {
DPRINTF(("%s: SNDCTL_DSP_SUBDIVIDE %d\n",
__func__, error));
goto out;
}
error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo);
if (error) {
DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
__func__, error));
goto out;
}
setblocksize(fp, &tmpinfo);
if (idat == 0)
idat = tmpinfo.play.buffer_size / tmpinfo.blocksize;
idat = (tmpinfo.play.buffer_size / idat) & -4;
AUDIO_INITINFO(&tmpinfo);
tmpinfo.blocksize = idat;
error = ioctlf(fp, AUDIO_SETINFO, &tmpinfo);
if (error) {
DPRINTF(("%s: AUDIO_SETINFO %d\n",
__func__, error));
goto out;
}
idat = tmpinfo.play.buffer_size / tmpinfo.blocksize;
error = copyout(&idat, SCARG(uap, data), sizeof idat);
if (error) {
DPRINTF(("%s: SNDCTL_DSP_SUBDIVIDE %d = %d\n",
__func__, idat, error));
goto out;
}
break;
case OSS_SNDCTL_DSP_SETFRAGMENT:
AUDIO_INITINFO(&tmpinfo);
error = copyin(SCARG(uap, data), &idat, sizeof idat);
if (error) {
DPRINTF(("%s: SNDCTL_DSP_SETFRAGMENT %d\n",
__func__, error));
goto out;
}
if ((idat & 0xffff) < 4 || (idat & 0xffff) > 17) {
DPRINTF(("%s: SNDCTL_DSP_SETFRAGMENT bad ival%d\n",
__func__, idat));
error = EINVAL;
goto out;
}
tmpinfo.blocksize = 1 << (idat & 0xffff);
tmpinfo.hiwat = (idat >> 16) & 0x7fff;
DPRINTF(("%s: SNDCTL_DSP_SETFRAGMENT blksize=%d, "
"hiwat=%d\n", __func__, tmpinfo.blocksize, tmpinfo.hiwat));
if (tmpinfo.hiwat == 0) /* 0 means set to max */
tmpinfo.hiwat = 65536;
error = ioctlf(fp, AUDIO_SETINFO, &tmpinfo);
if (error) {
DPRINTF(("%s: AUDIO_SETINFO %d\n",
__func__, error));
goto out;
}
error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo);
if (error) {
DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
__func__, error));
goto out;
}
u = tmpinfo.blocksize;
for(idat = 0; u > 1; idat++, u >>= 1)
;
idat |= (tmpinfo.hiwat & 0x7fff) << 16;
error = copyout(&idat, SCARG(uap, data), sizeof idat);
if (error) {
DPRINTF(("%s: SNDCTL_DSP_SETFRAGMENT %d = %d\n",
__func__, idat, error));
goto out;
}
break;
case OSS_SNDCTL_DSP_GETFMTS:
for (idat = 0, tmpenc.index = 0;
ioctlf(fp, AUDIO_GETENC, &tmpenc) == 0;
tmpenc.index++) {
switch(tmpenc.encoding) {
case AUDIO_ENCODING_ULAW:
idat |= OSS_AFMT_MU_LAW;
break;
case AUDIO_ENCODING_ALAW:
idat |= OSS_AFMT_A_LAW;
break;
case AUDIO_ENCODING_SLINEAR:
idat |= OSS_AFMT_S8;
break;
case AUDIO_ENCODING_SLINEAR_LE:
if (tmpenc.precision == 16)
idat |= OSS_AFMT_S16_LE;
else
idat |= OSS_AFMT_S8;
break;
case AUDIO_ENCODING_SLINEAR_BE:
if (tmpenc.precision == 16)
idat |= OSS_AFMT_S16_BE;
else
idat |= OSS_AFMT_S8;
break;
case AUDIO_ENCODING_ULINEAR:
idat |= OSS_AFMT_U8;
break;
case AUDIO_ENCODING_ULINEAR_LE:
if (tmpenc.precision == 16)
idat |= OSS_AFMT_U16_LE;
else
idat |= OSS_AFMT_U8;
break;
case AUDIO_ENCODING_ULINEAR_BE:
if (tmpenc.precision == 16)
idat |= OSS_AFMT_U16_BE;
else
idat |= OSS_AFMT_U8;
break;
case AUDIO_ENCODING_ADPCM:
idat |= OSS_AFMT_IMA_ADPCM;
break;
case AUDIO_ENCODING_AC3:
idat |= OSS_AFMT_AC3;
break;
default:
DPRINTF(("%s: SNDCTL_DSP_GETFMTS unknown %d\n",
__func__, tmpenc.encoding));
break;
}
}
DPRINTF(("%s: SNDCTL_DSP_GETFMTS < 0x%x\n",
__func__, idat));
error = copyout(&idat, SCARG(uap, data), sizeof idat);
if (error) {
DPRINTF(("%s: SNDCTL_DSP_GETFMTS = %x = %d\n",
__func__, idat, error));
goto out;
}
break;
case OSS_SNDCTL_DSP_GETOSPACE:
error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo);
if (error) {
DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
__func__, error));
goto out;
}
setblocksize(fp, &tmpinfo);
memset(&bufinfo, 0, sizeof(bufinfo));
bufinfo.fragsize = tmpinfo.blocksize;
bufinfo.fragments = tmpinfo.hiwat -
(tmpinfo.play.seek + tmpinfo.blocksize - 1) /
tmpinfo.blocksize;
bufinfo.fragstotal = tmpinfo.hiwat;
bufinfo.bytes =
tmpinfo.hiwat * tmpinfo.blocksize - tmpinfo.play.seek;
error = copyout(&bufinfo, SCARG(uap, data), sizeof bufinfo);
if (error) {
DPRINTF(("%s: SNDCTL_DSP_GETOSPACE = %d\n",
__func__, error));
goto out;
}
break;
case OSS_SNDCTL_DSP_GETISPACE:
error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo);
if (error) {
DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
__func__, error));
goto out;
}
setblocksize(fp, &tmpinfo);
memset(&bufinfo, 0, sizeof(bufinfo));
bufinfo.fragsize = tmpinfo.blocksize;
bufinfo.fragments = tmpinfo.record.seek / tmpinfo.blocksize;
bufinfo.fragstotal =
tmpinfo.record.buffer_size / tmpinfo.blocksize;
bufinfo.bytes = tmpinfo.record.seek;
error = copyout(&bufinfo, SCARG(uap, data), sizeof bufinfo);
if (error) {
DPRINTF(("%s: SNDCTL_DSP_GETISPACE %d %d %d %d = %d\n",
__func__, bufinfo.fragsize, bufinfo.fragments,
bufinfo.fragstotal, bufinfo.bytes, error));
goto out;
}
break;
case OSS_SNDCTL_DSP_NONBLOCK:
idat = 1;
error = ioctlf(fp, FIONBIO, &idat);
if (error) {
DPRINTF(("%s: FIONBIO %d\n",
__func__, error));
goto out;
}
break;
case OSS_SNDCTL_DSP_GETCAPS:
error = ioctlf(fp, AUDIO_GETPROPS, &idata);
if (error) {
DPRINTF(("%s: AUDIO_GETPROPS %d\n",
__func__, error));
goto out;
}
idat = OSS_DSP_CAP_TRIGGER;
if (idata & AUDIO_PROP_FULLDUPLEX)
idat |= OSS_DSP_CAP_DUPLEX;
if (idata & AUDIO_PROP_MMAP)
idat |= OSS_DSP_CAP_MMAP;
DPRINTF(("%s: SNDCL_DSP_GETCAPS %s duplex, %smmap\n",
__func__, (idat & OSS_DSP_CAP_DUPLEX) ? "full" : "half",
(idat & OSS_DSP_CAP_MMAP) ? "" : "no "));
error = copyout(&idat, SCARG(uap, data), sizeof idat);
if (error) {
DPRINTF(("%s: SNDCTL_DSP_GETCAPS %x = %d\n", __func__,
idat, error));
goto out;
}
break;
case OSS_SNDCTL_DSP_SETTRIGGER:
error = copyin(SCARG(uap, data), &idat, sizeof idat);
if (error) {
DPRINTF(("%s: SNDCTL_DSP_SETTRIGGER: %d\n",
__func__, error));
goto out;
}
error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo);
if (error) {
DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
__func__, error));
goto out;
}
AUDIO_INITINFO(&tmpinfo);
if (tmpinfo.mode & AUMODE_PLAY)
tmpinfo.play.pause = (idat & OSS_PCM_ENABLE_OUTPUT) == 0;
if (tmpinfo.mode & AUMODE_RECORD)
tmpinfo.record.pause = (idat & OSS_PCM_ENABLE_INPUT) == 0;
(void)ioctlf(fp, AUDIO_SETINFO, &tmpinfo);
/* FALLTHRU */
case OSS_SNDCTL_DSP_GETTRIGGER:
error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo);
if (error) {
DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
__func__, error));
goto out;
}
idat = 0;
if ((tmpinfo.mode & AUMODE_PLAY) && !tmpinfo.play.pause)
idat |= OSS_PCM_ENABLE_OUTPUT;
if ((tmpinfo.mode & AUMODE_RECORD) && !tmpinfo.record.pause)
idat |= OSS_PCM_ENABLE_INPUT;
error = copyout(&idat, SCARG(uap, data), sizeof idat);
if (error) {
DPRINTF(("%s: SNDCTL_DSP_GETTRIGGER = %x = %d\n",
__func__, idat, error));
goto out;
}
break;
case OSS_SNDCTL_DSP_GETIPTR:
error = ioctlf(fp, AUDIO_GETIOFFS, &tmpoffs);
if (error) {
DPRINTF(("%s: AUDIO_GETIOFFS %d\n",
__func__, error));
goto out;
}
memset(&cntinfo, 0, sizeof(cntinfo));
cntinfo.bytes = tmpoffs.samples;
cntinfo.blocks = tmpoffs.deltablks;
cntinfo.ptr = tmpoffs.offset;
error = copyout(&cntinfo, SCARG(uap, data), sizeof cntinfo);
if (error) {
DPRINTF(("%s: SNDCTL_DSP_GETIPTR %d\n",
__func__, error));
goto out;
}
break;
case OSS_SNDCTL_DSP_GETOPTR:
error = ioctlf(fp, AUDIO_GETOOFFS, &tmpoffs);
if (error) {
DPRINTF(("%s: AUDIO_GETOOFFS %d\n",
__func__, error));
goto out;
}
memset(&cntinfo, 0, sizeof(cntinfo));
cntinfo.bytes = tmpoffs.samples;
cntinfo.blocks = tmpoffs.deltablks;
cntinfo.ptr = tmpoffs.offset;
error = copyout(&cntinfo, SCARG(uap, data), sizeof cntinfo);
if (error) {
DPRINTF(("%s: SNDCTL_DSP_GETOPTR %d\n",
__func__, error));
goto out;
}
break;
case OSS_SNDCTL_DSP_SETDUPLEX:
idat = 1;
error = ioctlf(fp, AUDIO_SETFD, &idat);
if (error) {
DPRINTF(("%s: AUDIO_SETFD %d = %d\n",
__func__, idat, error));
goto out;
}
break;
case OSS_SNDCTL_DSP_MAPINBUF:
DPRINTF(("%s: Unimplemented SNDCTL_DSP_MAPINBUF\n",
__func__));
error = EINVAL;
goto out;
case OSS_SNDCTL_DSP_MAPOUTBUF:
DPRINTF(("%s: Unimplemented SNDCTL_DSP_MAPOUTBUF\n",
__func__));
error = EINVAL;
goto out;
case OSS_SNDCTL_DSP_SETSYNCRO:
DPRINTF(("%s: Unimplemented SNDCTL_DSP_GETSYNCHRO\n",
__func__));
error = EINVAL;
goto out;
case OSS_SNDCTL_DSP_GETODELAY:
error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo);
if (error) {
DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
__func__, error));
goto out;
}
idat = tmpinfo.play.seek + tmpinfo.blocksize / 2;
error = copyout(&idat, SCARG(uap, data), sizeof idat);
if (error) {
DPRINTF(("%s: SNDCTL_DSP_GETODELAY %d\n",
__func__, error));
goto out;
}
break;
case OSS_SNDCTL_DSP_PROFILE:
/* This gives just a hint to the driver,
* implementing it as a NOP is ok
*/
DPRINTF(("%s: SNDCTL_DSP_PROFILE\n", __func__));
break;
default:
DPRINTF(("%s: Unimplemented 0x%lx\n", __func__, com));
error = EINVAL;
goto out;
}
out:
fd_putfile(SCARG(uap, fd));
return error;
}
/* If the NetBSD mixer device should have more than 32 devices
* some will not be available to Linux */
#define NETBSD_MAXDEVS 64
struct audiodevinfo {
int done;
dev_t dev;
int16_t devmap[OSS_SOUND_MIXER_NRDEVICES],
rdevmap[NETBSD_MAXDEVS];
char names[NETBSD_MAXDEVS][MAX_AUDIO_DEV_LEN];
int enum2opaque[NETBSD_MAXDEVS];
u_long devmask, recmask, stereomask;
u_long caps, source;
};
static int
opaque_to_enum(struct audiodevinfo *di, audio_mixer_name_t *label, int opq)
{
int i, o;
for (i = 0; i < NETBSD_MAXDEVS; i++) {
o = di->enum2opaque[i];
if (o == opq)
break;
if (o == -1 && label != NULL &&
!strncmp(di->names[i], label->name, sizeof di->names[i])) {
di->enum2opaque[i] = opq;
break;
}
}
if (i >= NETBSD_MAXDEVS)
i = -1;
/*printf("opq_to_enum %s %d -> %d\n", label->name, opq, i);*/
return (i);
}
static int
enum_to_ord(struct audiodevinfo *di, int enm)
{
if (enm >= NETBSD_MAXDEVS)
return (-1);
static int
enum_to_mask(struct audiodevinfo *di, int enm)
{
int m;
if (enm >= NETBSD_MAXDEVS)
return (0);
m = di->enum2opaque[enm];
if (m == -1)
m = 0;
/*printf("enum_to_mask %d -> %d\n", enm, di->enum2opaque[enm]);*/
return (m);
}
/*
* Collect the audio device information to allow faster
* emulation of the Linux mixer ioctls. Cache the information
* to eliminate the overhead of repeating all the ioctls needed
* to collect the information.
*/
static struct audiodevinfo *
getdevinfo(file_t *fp)
{
mixer_devinfo_t mi;
int i, j, e;
static const struct {
const char *name;
int code;
} *dp, devs[] = {
{ AudioNmicrophone, OSS_SOUND_MIXER_MIC },
{ AudioNline, OSS_SOUND_MIXER_LINE },
{ AudioNcd, OSS_SOUND_MIXER_CD },
{ AudioNdac, OSS_SOUND_MIXER_PCM },
{ AudioNaux, OSS_SOUND_MIXER_LINE1 },
{ AudioNrecord, OSS_SOUND_MIXER_IMIX },
{ AudioNmaster, OSS_SOUND_MIXER_VOLUME },
{ AudioNtreble, OSS_SOUND_MIXER_TREBLE },
{ AudioNbass, OSS_SOUND_MIXER_BASS },
{ AudioNspeaker, OSS_SOUND_MIXER_SPEAKER },
/* { AudioNheadphone, ?? },*/
{ AudioNoutput, OSS_SOUND_MIXER_OGAIN },
{ AudioNinput, OSS_SOUND_MIXER_IGAIN },
/* { AudioNmaster, OSS_SOUND_MIXER_SPEAKER },*/
/* { AudioNstereo, ?? },*/
/* { AudioNmono, ?? },*/
{ AudioNfmsynth, OSS_SOUND_MIXER_SYNTH },
/* { AudioNwave, OSS_SOUND_MIXER_PCM },*/
{ AudioNmidi, OSS_SOUND_MIXER_SYNTH },
/* { AudioNmixerout, ?? },*/
{ 0, -1 }
};
int (*ioctlf)(file_t *, u_long, void *) = fp->f_ops->fo_ioctl;
struct vnode *vp;
struct vattr va;
static struct audiodevinfo devcache;
struct audiodevinfo *di = &devcache;
int error, mlen, dlen;
/*
* Figure out what device it is so we can check if the
* cached data is valid.
*/
vp = fp->f_vnode;
if (vp->v_type != VCHR)
return 0;
vn_lock(vp, LK_SHARED | LK_RETRY);
error = VOP_GETATTR(vp, &va, kauth_cred_get());
VOP_UNLOCK(vp);
if (error)
return 0;
if (di->done && di->dev == va.va_rdev)
return di;
/*
* When AUDIO_SETINFO fails to set a channel count, the application's chosen
* number is out of range of what the kernel allows.
*
* When this happens, we use the current hardware settings. This is just in
* case an application is abusing SNDCTL_DSP_CHANNELS - OSSv4 always sets and
* returns a reasonable value, even if it wasn't what the user requested.
*
* XXX: If a device is opened for both playback and recording, and supports
* fewer channels for recording than playback, applications that do both will
* behave very strangely. OSS doesn't allow for reporting separate channel
* counts for recording and playback. This could be worked around by always
* mixing recorded data up to the same number of channels as is being used
* for playback.
*/
static void
setchannels(file_t *fp, int mode, int nchannels)
{
struct audio_info tmpinfo, hwfmt;
int (*ioctlf)(file_t *, u_long, void *);
/*
* Check that the blocksize is a power of 2 as OSS wants.
* If not, set it to be.
*/
static void
setblocksize(file_t *fp, struct audio_info *info)
{
struct audio_info set;
u_int s;
if (info->blocksize & (info->blocksize - 1)) {
for(s = 32; s < info->blocksize; s <<= 1)
continue;
AUDIO_INITINFO(&set);
set.blocksize = s;
fp->f_ops->fo_ioctl(fp, AUDIO_SETINFO, &set);
fp->f_ops->fo_ioctl(fp, AUDIO_GETBUFINFO, info);
}
}